55 lines
5.7 KiB
C++
55 lines
5.7 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#include "modern/ui/career_files.hpp"
|
|
#include <chrono>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
using namespace bmh::modern_ui;
|
|
namespace {
|
|
void require(bool ok,const char *why){if(!ok)throw std::runtime_error(why);}
|
|
template<class F> void rejected(F f,const char *why){bool caught=false;try{f();}catch(const std::exception&){caught=true;}require(caught,why);}
|
|
struct Fixture {
|
|
fs::path root=fs::temp_directory_path()/("modern-career-files-"+std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
|
|
Fixture(){fs::create_directories(root);}
|
|
~Fixture(){std::error_code e;fs::remove_all(root,e);}
|
|
};
|
|
Json metadata(const fs::path &file){const auto j=Json::parse(bmh::editor::read_file(file));if(j.at("world")!="synthetic")throw std::runtime_error("wrong world");return j;}
|
|
CareerFiles::Writer writer(int value){return [value](const fs::path &file){bmh::editor::write_atomic(file,Json{{"world","synthetic"},{"value",value}}.dump(),false);};}
|
|
void transactions(){
|
|
Fixture f;CareerFiles store(f.root/"careers",metadata);require(store.list().empty()&&!fs::exists(store.directory()),"browsing created storage");
|
|
auto first=store.put("Öffnung",writer(1));const auto original=bmh::editor::read_file(store.file(first.id));require(first.data.at("value")==1&&store.read(first.id,first.token).bytes==original.size(),"published save differs");
|
|
rejected([&]{store.put("o\xcc\x88" "FFNUNG",writer(2));},"casefold duplicate replaced save");
|
|
rejected([&]{store.put(first.name,writer(2),first.id,"stale");},"stale overwrite accepted");
|
|
rejected([&]{store.put(first.name,[](const auto &file){bmh::editor::write_atomic(file,"partial",false);throw std::runtime_error("disk error");},first.id,first.token);},"writer failure published");
|
|
rejected([&]{store.put(first.name,[](const auto &file){bmh::editor::write_atomic(file,"not a career",false);},first.id,first.token);},"invalid replacement published");
|
|
require(bmh::editor::read_file(store.file(first.id))==original,"failed replacement damaged original");
|
|
auto second=store.put(first.name,writer(2),first.id,first.token);require(second.data.at("value")==2&&second.token!=first.token,"confirmed overwrite lost new state");
|
|
rejected([&]{store.remove(first.id,first.token);},"old deletion token accepted");
|
|
const auto renamed=store.rename(second.id,second.token,"Neue Saison");require(!fs::exists(store.file(second.id))&&renamed.data==second.data,"rename altered data");
|
|
const auto exported=f.root/"export.obs";store.export_file(renamed.id,renamed.token,exported);require(bmh::editor::read_file(exported)==bmh::editor::read_file(store.file(renamed.id)),"export altered bytes");
|
|
rejected([&]{store.export_file(renamed.id,renamed.token,exported);},"export overwrote without consent");
|
|
rejected([&]{store.export_file(renamed.id,renamed.token,store.file("Other.obs"));},"export mutated internal library");
|
|
const auto imported=store.import_file(exported);require(imported.id!=renamed.id&&imported.data==renamed.data,"import replaced an existing entry");
|
|
bmh::editor::write_atomic(store.file(renamed.id),Json{{"world","synthetic"},{"value",3}}.dump());
|
|
rejected([&]{store.read(renamed.id,renamed.token);},"external mutation missed on read");
|
|
rejected([&]{store.rename(renamed.id,renamed.token,"Stale");},"external mutation missed on rename");
|
|
rejected([&]{store.remove(renamed.id,renamed.token);},"external mutation missed on delete");
|
|
rejected([&]{store.export_file(renamed.id,renamed.token,f.root/"stale.obs");},"external mutation missed on export");
|
|
store.remove(imported.id,imported.token);require(!fs::exists(store.file(imported.id)),"delete retained selected save");
|
|
bmh::editor::write_atomic(store.file("Corrupt.obs"),"damaged");const auto entries=store.list();bool corrupt=false;for(const auto &e:entries)if(e.id=="Corrupt.obs")corrupt=!e.valid()&&!e.token.empty();require(corrupt,"corrupt entry disappeared or became loadable");
|
|
for(const auto &p:fs::directory_iterator(store.directory()))require(!bmh::editor::utf8(p.path().filename()).starts_with(".pending-"),"staging leak");
|
|
}
|
|
void boundaries(){
|
|
Fixture f;CareerFiles store(f.root/"library",metadata);
|
|
for(const auto *name:{"../escape.obs","a/b.obs","a\\b.obs",".hidden.obs","wrong.json",""})rejected([&]{store.read(name);},"unsafe identifier accepted");
|
|
for(const auto *name:{"CON","../escape","", "line\nbreak"})rejected([&]{store.put(name,writer(1));},"unsafe filename accepted");
|
|
auto max=store.put(std::string(64,'x'),writer(1));const auto free=store.free_name(max.name);require(free!=max.name&&DemoFiles::clean_name(free)==free,"long-name import suffix invalid");
|
|
const auto before=store.list().size();rejected([&]{store.put("Wrong world",[](const auto &p){bmh::editor::write_atomic(p,"{\"world\":\"other\"}",false);});},"foreign world accepted");require(store.list().size()==before,"foreign import changed library");
|
|
std::error_code e;fs::create_symlink(store.file(max.id),store.file("Link.obs"),e);if(!e){rejected([&]{store.read("Link.obs");},"linked source accepted");rejected([&]{store.remove("Link.obs",max.token);},"linked source deleted");}
|
|
fs::create_directory_symlink(store.directory(),f.root/"linked",e);if(!e){CareerFiles linked(f.root/"linked",metadata);rejected([&]{linked.list();},"linked library accepted");}
|
|
// Recursive callback simulates concurrent writers. OS locks release after the failure.
|
|
rejected([&]{store.put("Outer",[&](const auto &){store.put("Inner",writer(7));});},"simultaneous writers accepted");
|
|
require(store.put("After failed lock",writer(8)).valid(),"failed writer retained lock");
|
|
}
|
|
}
|
|
int main(){try{transactions();boundaries();std::cout<<"career files: transactions, stale files, failed producers, imports, names and boundaries passed\n";return 0;}catch(const std::exception &e){std::cerr<<e.what()<<'\n';return 1;}}
|