144 lines
7.7 KiB
Python
144 lines
7.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise signed policy parsing, historical PUT selection, base ordinals and legacy resumes."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
|
|
here = Path(__file__).resolve().parent
|
|
source = (here / 'OctoUpdater.cpp').read_text()
|
|
|
|
|
|
def section(start, stop):
|
|
pos = source.index(start)
|
|
return source[pos:source.index(stop, pos)]
|
|
|
|
|
|
prelude = r'''
|
|
#include <cassert>
|
|
#include <sstream>
|
|
#include "stock_base.hpp"
|
|
namespace fs = std::filesystem;
|
|
using current_files::Outputs;
|
|
struct Fail : std::runtime_error { using std::runtime_error::runtime_error; };
|
|
fs::path g_root, g_work;
|
|
std::string g_base, install_body;
|
|
std::string get_text(const std::string& path, bool compressed = true) {
|
|
if (path == "install.txt" && compressed) return install_body;
|
|
assert(path == "install.txt.sig" && !compressed);
|
|
return "sig";
|
|
}
|
|
bool verify(const std::string& body, const std::string& sig) { return body == install_body && sig == "sig"; }
|
|
|
|
current_files::RetiredPaths policy;
|
|
fs::path at(std::string path) {
|
|
assert(!policy.matches(path)); // even a metadata probe on a retired target fails the fixture
|
|
return g_root / current_files::path_key(path);
|
|
}
|
|
struct VerifiedFiles {
|
|
std::set<std::string> seen;
|
|
bool matches(const std::string& path, uint64_t size, const std::string& expected, bool full) {
|
|
assert(full);
|
|
seen.insert(current_files::path_key(path));
|
|
std::ifstream in(at(path), std::ios::binary);
|
|
std::string data{std::istreambuf_iterator<char>(in), {}};
|
|
return size == data.size() && expected == data;
|
|
}
|
|
};
|
|
'''
|
|
cases = r'''
|
|
int main() {
|
|
const std::string header = "OCTO-PATCH 1\nCHANNEL octo\n";
|
|
const std::string retire = "STEP POLICY retired-paths\nRETIRE AddOns/Old/\nRETIRE Other/Leaf\nRETIRE OTHER/LEAF\n";
|
|
const std::string before = "STEP FILES old\nPUT ADDONS\\OLD\\lua\\a untouched 9 blob 1 any\n"
|
|
"PUT Other/Leaf untouched 9 blob 1 any\nPUT AddOns/Oldish/file working 7 blob 1 any\n";
|
|
const std::string after = "STEP FILES new\nPUT addons/Old/new ignored 7 blob 1 any\n"
|
|
"PUT other/leaf.bak managed 7 blob 1 any\n";
|
|
Outputs published;
|
|
for (bool compact : {false,true}) {
|
|
auto body = header + (compact ? "LEGACY-SIGNATURE ff\n" : "") + before + retire + after + "END\n";
|
|
std::string sig;
|
|
auto parsed = parse(body, compact ? &sig : nullptr, 1, "octo");
|
|
policy = parsed.retired;
|
|
assert(policy.matches("oThEr\\leaf") && policy.matches("ADDONS/Old/deep/x"));
|
|
assert(!policy.matches("addons/Old") && !policy.matches("OTHER/leaf/file"));
|
|
assert(policy.matches("addons. //./OLD... /deep//x") && policy.matches("Other/./leaf. "));
|
|
assert(!policy.matches("addons/O ld/file") && !policy.matches("Other/le af"));
|
|
assert(!policy.matches("addons/Oldish/file") && !policy.matches("OTHER/leaf.bak"));
|
|
assert(current_files::file_outputs(parsed.steps,{"old"}).size() == 1);
|
|
published = current_files::file_outputs(parsed.steps,{"old","new"});
|
|
assert(published.size() == 2 && published.count("addons/oldish/file") && published.count("other/leaf.bak"));
|
|
}
|
|
const std::string step = "STEP POLICY retired-paths\n";
|
|
for (std::string path : {"/", "/x", "a//b", "a//", "a/../b", "./a", "foo.", "foo./x", "c:/a", "foo bar",
|
|
"foo\\bar", "a/<b", "a/*", "x\x01", "x\x7f", "nul.txt", "a/COM3.foo", "CONOUT$",
|
|
"Data/foo", "_PATCH/", ".build.info", "octoupdater.exe", "OctoUpdater.cfg"}) {
|
|
bool fail = false;
|
|
try { parse(header + step + "RETIRE " + path + "\nEND\n", nullptr, 1, "octo"); }
|
|
catch (const Fail&) { fail = true; }
|
|
assert(fail);
|
|
}
|
|
for (const auto& bad : {before + "RETIRE a\n", step, step + "RETIRE\n", step + "PUT file\n", retire + retire,
|
|
step + "RETIRE x y\n", std::string("STEP POLICY retired-paths other\nRETIRE x\n"),
|
|
std::string("STEP POLICY unknown\nRETIRE x\n")}) {
|
|
bool fail = false;
|
|
try { parse(header + bad + "END\n", nullptr, 1, "octo"); }
|
|
catch (const Fail&) { fail = true; }
|
|
assert(fail);
|
|
}
|
|
for (const std::string kind : {"CONTENT", "STORE"}) {
|
|
const auto build = "STEP " + kind + " change parent child\n";
|
|
for (const std::string row : {"FILE Addons//./Old./new sum 8 blob 6 zstd",
|
|
"CONF other/leAF sum 8 blob 6",
|
|
"ACTIVATE Addons/Old/deep/x parent child blob 6",
|
|
"REQ_SHA Other/Leaf sum", "REQ_SPAN Other/Leaf 0 8 sum",
|
|
"REQ_ABSENT Other/Leaf", "FILE Active/new sum 8 blob 6 zstd-patch Other/leaf ref"}) {
|
|
for (const auto& body : {retire + build + row + "\n", build + row + "\n" + retire}) {
|
|
bool conflict = false;
|
|
try { parse(header + body + "END\n", nullptr, 1, "octo"); }
|
|
catch (const Fail& e) { conflict = std::string(e.what()).find("references retired path") != std::string::npos; }
|
|
assert(conflict);
|
|
}
|
|
}
|
|
parse(header + retire + build + "FILE Addons/Oldish/x sum 8 blob 6 zstd-patch other/leaf.bak ref\n" +
|
|
"ACTIVATE .build.info parent child blob 6\nEND\n", nullptr, 1, "octo");
|
|
}
|
|
const auto legacy = parse(header + before + "END\n", nullptr, 1, "octo");
|
|
assert(legacy.retired.empty() && current_files::file_outputs(legacy.steps,{"old"}).size() == 3);
|
|
|
|
const std::string release = "OCTO-INSTALL 1\nRELEASE release\nBASE base/\n";
|
|
const std::string kept = "F working 7 AddOns/Oldish/file\nF managed 7 other/leaf.bak\n";
|
|
install_body = release + "F unused 6 ADDONS. /./old. //stale\n" + kept + "F unused 6 other/leaf. \nEND\n";
|
|
const auto stale = signed_install_list(policy);
|
|
assert(stale.files.size() == 4 && stale.files[0].retired && stale.files[3].retired);
|
|
assert(!stale.files[1].retired && stale.files[1].path == "AddOns/Oldish/file" && !stale.torrent_compatible());
|
|
install_body = release + kept + "END\n";
|
|
const auto fresh = signed_install_list(policy);
|
|
assert(fresh.torrent_compatible() && fresh.files.size() == 2 && fresh.files[1].path == "other/leaf.bak");
|
|
|
|
// A pre-marker install can contain player-edited retired bytes. Only the
|
|
// signed surviving sibling can establish that this old install is resumable.
|
|
g_root = fs::current_path() / "client";
|
|
g_work = g_root / "_patch";
|
|
fs::create_directories(g_work);
|
|
std::ofstream(g_work / "verified.txt") << "old resume evidence";
|
|
fs::create_directories(g_root / "addons/old");
|
|
std::ofstream(g_root / "addons/old/stale") << "player-edited";
|
|
fs::create_directories(g_root / "addons/oldish");
|
|
std::ofstream(g_root / "addons/oldish/file") << "working";
|
|
VerifiedFiles verified;
|
|
assert(legacy_partial_install(stale,published,verified));
|
|
assert(verified.seen == std::set<std::string>{"addons/oldish/file"});
|
|
std::ofstream(g_root / "addons/oldish/file") << "foreign";
|
|
assert(!legacy_partial_install(stale,published,verified));
|
|
}
|
|
'''
|
|
code = (prelude + section('struct Step {', 'static std::string g_base') +
|
|
section('struct InstallFile {', 'static std::string abs_url') +
|
|
section('struct InstallList {', 'static const InstallFile* base_activation') + cases)
|
|
with tempfile.TemporaryDirectory() as work:
|
|
root = Path(work)
|
|
(root / 'fixture.cpp').write_text(code)
|
|
subprocess.run(['c++', '-std=c++20', '-I', here, 'fixture.cpp', '-o', 'fixture'], cwd=root, check=True)
|
|
subprocess.run(['./fixture'], cwd=root, check=True)
|
|
print('Policy, Windows aliases, content conflicts, signed base ordinals, P2P and partial-install checks passed.')
|