Include the current addon, terrain integrations, tools, regression tests and recorded development evidence.
174 lines
9.7 KiB
Python
174 lines
9.7 KiB
Python
"""Execute the actual shared-file transaction with injected disk failures.
|
|
Engine JSON/migration/menu checks are recorded separately in native evidence.
|
|
"""
|
|
import os
|
|
from pathlib import Path
|
|
from policy_harness import ROOT, body, run
|
|
p = Path(os.environ.get('ERR_PROFILE_CONFIG_SOURCE', ROOT/'addons/core/Scripts/Game/ERR/00_Core/ERR_ProfileConfig.c'))
|
|
method = body(p, 'static bool Commit(')
|
|
for before, after in [
|
|
('map<string, string>', 'Map'), ('FileHandle file', 'FileHandle* file'),
|
|
('file.', 'file->'), ('FileIO.', 'FileIO::'), ('FileMode.', 'FileMode::'),
|
|
('Replication.', 'Replication::')]:
|
|
method = method.replace(before, after)
|
|
run(r'''
|
|
#include <cassert>
|
|
#include <map>
|
|
#include <string>
|
|
using string=std::string;
|
|
using Map=std::map<string,string>;
|
|
const string PATH="Escapists.json";
|
|
std::map<string,string> files;
|
|
std::map<string,Map> documents;
|
|
int fault=0,serial=0;
|
|
namespace Replication {bool server=true;bool IsServer(){return server;}}
|
|
namespace FileMode {int WRITE=1;}
|
|
struct FileHandle {
|
|
string path;
|
|
void Write(string data){files[path]=fault==2?"CORRUPT":data;}
|
|
void Close(){}
|
|
};
|
|
namespace FileIO {
|
|
FileHandle* OpenFile(string path,int){static FileHandle h;h.path=path;return fault==1?nullptr:&h;}
|
|
bool FileExists(string path){return files.count(path);}
|
|
bool DeleteFile(string path){return files.erase(path);}
|
|
bool CopyFile(string from,string to){
|
|
if(!files.count(from))return false;
|
|
if(to==PATH+".bak"&&fault==3)return false;
|
|
files[to]=files[from];
|
|
if(to==PATH+".bak"&&fault==4)files[to]="CORRUPT";
|
|
if(from==PATH+".tmp"){
|
|
if(fault==5){files[to]="PARTIAL";return false;}
|
|
if(fault==6)files[to]="CORRUPT";
|
|
if(fault==7){files[to]="WRONG";documents["WRONG"]={{"mission","wrong"}};}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
struct Store {
|
|
static bool ContentsValid(Map){return true;}
|
|
static string Encode(Map values){string id="doc"+std::to_string(++serial);documents[id]=values;return id;}
|
|
static bool Document(string path,Map& values){
|
|
if(!files.count(path)||!documents.count(files[path]))return false;
|
|
values=documents[files[path]];return true;
|
|
}
|
|
static bool Same(Map a,Map b){return a==b;}
|
|
''' + method + r'''
|
|
};
|
|
int main(){
|
|
Map old={{"mission","old"},{"items","presets"},{"voice","url"}}, next=old,read;
|
|
next["mission"]="new";
|
|
for(int injected:{1,2,5,6,7}){
|
|
files.clear();fault=injected;
|
|
assert(!Store::Commit(next)&&!files.count(PATH)); // failed first save stays absent
|
|
}
|
|
for(int injected=1;injected<=7;injected++){
|
|
files.clear();fault=0;assert(Store::Commit(old));
|
|
fault=injected;assert(!Store::Commit(next));
|
|
assert(Store::Document(PATH,read)&&read==old); // current valid config remains
|
|
fault=0;assert(Store::Commit(next));
|
|
assert(Store::Document(PATH,read)&&read==next);
|
|
}
|
|
for(int injected:{1,2,5,6,7}){
|
|
files.clear();files[PATH]="CORRUPT";files[PATH+".bak"]=Store::Encode(old);
|
|
fault=injected;assert(!Store::Commit(next));
|
|
assert(Store::Document(PATH+".bak",read)&&read==old); // only good backup not replaced
|
|
fault=0;assert(Store::Commit(next));
|
|
assert(Store::Document(PATH,read)&&read==next);
|
|
}
|
|
files.clear();fault=0;assert(Store::Commit(old));assert(Store::Commit(next));
|
|
assert(Store::Document(PATH+".bak",read)&&read==old);
|
|
assert(!files.count(PATH+".tmp"));
|
|
Replication::server=false;assert(!Store::Commit(old));
|
|
assert(Store::Document(PATH,read)&&read==next);
|
|
}
|
|
''')
|
|
print('PASS: shared config transaction rejects client writes, preserves first-save/prior/fallback state across open, write, backup, partial-copy, readback and mismatch failures')
|
|
|
|
# Execute actual shared Save and Commit, with a small token document store.
|
|
# JSON lexing and non-terrain section parsers are explicit doubles. The terrain
|
|
# rejection runs the actual TerrainPreference.Parse method with a catalog double.
|
|
# No native filesystem atomicity or full JsonLoadContext implementation claim.
|
|
def translate(method):
|
|
for before, after in [
|
|
('map<string, string>', 'Map'), ('new Map()', 'Map()'),
|
|
('FileHandle file','FileHandle* file'), ('file.','file->'),
|
|
('FileIO.','FileIO::'), ('FileMode.','FileMode::'), ('Replication.','Replication::'),
|
|
('JsonSaveContext json','JsonSaveContext& json')]:
|
|
method=method.replace(before,after)
|
|
method=method.replace('foreach (string key, string value : changed)', 'for (auto [key,value] : changed)')
|
|
method=method.replace('foreach (string path : LegacyPaths())', 'for (string path : LegacyPaths())')
|
|
return method
|
|
helper='static bool ContentsValid(Map) {return true;}'
|
|
if 'static bool ContentsValid(' in p.read_text(): helper=translate(body(p,'static bool ContentsValid('))
|
|
terrain = body(ROOT/'addons/core/Scripts/Game/ERR/00_Core/ERR_TerrainPreference.c', 'static bool Parse(').replace('JsonLoadContext json', 'JsonLoadContext& json').replace('out ResourceName scenario', 'string& scenario').replace('ERR_TerrainCatalog.', 'ERR_TerrainCatalog::')
|
|
run(r"""
|
|
#include <cassert>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
struct string:std::string{using std::string::string;string(std::string s):std::string(s){}bool IsEmpty(){return empty();}int Length(){return size();}void TrimInPlace(){}};
|
|
struct Map:std::map<string,string>{using std::map<string,string>::map;bool Contains(string key){return count(key);}string Get(string key){return at(key);}void Set(string key,string value){(*this)[key]=value;}};
|
|
const string PATH="Escapists.json";
|
|
std::map<string,string> files;
|
|
std::map<string,Map> documents;
|
|
int serial=0,copies=0,writes=0;
|
|
namespace Replication{bool server=true;bool IsServer(){return server;}}
|
|
namespace FileMode{int WRITE=1;}
|
|
struct FileHandle{string path;void Write(string data){writes++;files[path]=data;}void Close(){}};
|
|
namespace FileIO{
|
|
FileHandle* OpenFile(string path,int){static FileHandle h;h.path=path;return &h;}
|
|
bool FileExists(string path){return files.count(path);}
|
|
bool DeleteFile(string path){return files.erase(path);}
|
|
bool CopyFile(string from,string to){if(!files.count(from))return false;copies++;files[to]=files[from];return true;}
|
|
}
|
|
struct JsonSaveContext{string token;string SaveToString(){return token;}};
|
|
struct JsonLoadContext{Map fields;bool ReadValue(string key,string&value){if(!fields.Contains(key))return false;value=fields.Get(key);return true;}};
|
|
namespace ERR_TerrainCatalog{string Key(string scenario){return scenario=="valid-saved-terrain"?"known":"";}}
|
|
struct Terrain{
|
|
""" + terrain + r"""
|
|
};
|
|
struct Store{
|
|
static std::vector<string> LegacyPaths(){return {"mission","mods","factions","terrain","items","voice"};}
|
|
static string Section(string path){return path;}
|
|
static bool Ensure(){return true;}
|
|
static string Encode(Map values){string id="doc"+std::to_string(++serial);documents[id]=values;return id;}
|
|
static bool Split(string text,Map& values){if(!documents.count(text))return false;values=documents[text];return true;}
|
|
static bool Valid(string section,string text){Map fields;if(!Split(text,fields))return false;if(section!="terrain")return true;JsonLoadContext json{fields};string scenario;return Terrain::Parse(json,scenario);}
|
|
static bool Document(string path,Map& values){return files.count(path)&&Split(files[path],values)&&values.Contains("configVersion")&&values.Get("configVersion")=="1";}
|
|
static bool Same(Map a,Map b){return a==b;}
|
|
""" + helper + translate(body(p,'static bool Commit(')) + translate(body(p,'static bool Save(')) + r"""
|
|
};
|
|
int main(){
|
|
auto validTerrain=Store::Encode({{"scenario","valid-saved-terrain"},{"futureTerrainKey","keep"}});
|
|
auto invalidTerrain=Store::Encode({{"scenario","malformed"},{"futureTerrainKey","keep"}});
|
|
auto mission=Store::Encode({{"timer","10"},{"unknownMissionKey","keep"}});
|
|
auto changedMission=Store::Encode({{"timer","20"}});
|
|
Map good={{"configVersion","1"},{"mission",mission},{"terrain",validTerrain},{"unknownRoot","keep"}},current=good,read;
|
|
current.Set("terrain",invalidTerrain);string originalGood=Store::Encode(good),originalBad=Store::Encode(current);
|
|
files[PATH]=originalBad;files[PATH+".bak"]=originalGood;
|
|
// Owner would reject current terrain and accept backup. An unrelated save must
|
|
// not destroy the only recovery document or report success with invalid data.
|
|
assert(!Store::Valid("terrain",current.Get("terrain"))&&Store::Valid("terrain",good.Get("terrain")));
|
|
JsonSaveContext change{changedMission};
|
|
assert(!Store::Save("mission",change));
|
|
assert(files[PATH]==originalBad&&files[PATH+".bak"]==originalGood&&copies==0&&writes==0&&files.size()==2);
|
|
// Repair the section through its recovered menu value. Preserve the only good
|
|
// backup; merge unknown fields and allow subsequent ordinary menu persistence.
|
|
JsonSaveContext repair{validTerrain};assert(Store::Save("terrain",repair));
|
|
assert(files[PATH+".bak"]==originalGood&&Store::Document(PATH,read));
|
|
assert(read.Get("unknownRoot")=="keep");Map fields;assert(Store::Split(read.Get("terrain"),fields)&&fields.Get("futureTerrainKey")=="keep");
|
|
string repaired=files[PATH];assert(Store::Save("mission",change));assert(files[PATH+".bak"]==repaired);
|
|
assert(Store::Document(PATH,read)&&Store::Split(read.Get("mission"),fields));assert(fields.Get("timer")=="20"&&fields.Get("unknownMissionKey")=="keep");
|
|
// Missing optional sections continue to use their owner defaults; unknown roots
|
|
// are not subject to this version's validators.
|
|
Map sparse={{"configVersion","1"},{"unrecognized","opaque"}};assert(Store::Commit(sparse));
|
|
// Existing defaults and each known section are checked; no invalid first write.
|
|
files.clear();copies=0;writes=0;assert(!Store::Commit(current)&&files.empty()&&copies==0&&writes==0);
|
|
// Updating an already healthy document retains ordinary last-valid backup.
|
|
files[PATH]=originalGood;assert(Store::Save("mission",change));assert(files[PATH+".bak"]==originalGood);
|
|
}
|
|
""")
|
|
print('PASS: actual Save/Commit preserves semantically valid backup across unrelated menu edits and repairs; unknown fields/default sections retained; actual terrain parser; other parsers, catalog and disk doubled')
|