92 lines
4.2 KiB
Python
92 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise the Windows updater's real disk identity and SHA verification in an isolated Wine prefix."""
|
|
import argparse
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
|
|
here = Path(__file__).resolve().parent
|
|
source = (here / 'OctoUpdater.cpp').read_text()
|
|
|
|
|
|
def section(start, stop):
|
|
begin = source.index(start)
|
|
return source[begin:source.index(stop, begin)]
|
|
|
|
|
|
prelude = r'''
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <windows.h>
|
|
#include <bcrypt.h>
|
|
#include <cassert>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include "current_files.hpp"
|
|
namespace fs = std::filesystem;
|
|
struct Fail : std::runtime_error { using std::runtime_error::runtime_error; };
|
|
fs::path g_root, g_work;
|
|
'''
|
|
cases = r'''
|
|
int main() {
|
|
g_root = fs::current_path() / "fixture";
|
|
g_work = g_root / "_patch";
|
|
fs::create_directories(g_work);
|
|
const std::string path = "test addon.lua";
|
|
const auto target = at(path);
|
|
auto write = [](const fs::path& file, const char* text) { std::ofstream(file, std::ios::binary) << text; };
|
|
write(target, "current!");
|
|
const auto sha = real_sha_file(target);
|
|
const auto published_time = fs::last_write_time(target);
|
|
assert(!file_stamp(target, 8).empty());
|
|
std::ofstream(g_work / "verified.txt") << "8 " << sha << " " << path << "\n";
|
|
VerifiedFiles first;
|
|
assert(first.matches(path, 8, sha, false) && hashes == 1); // historical size-only entry
|
|
first.save();
|
|
VerifiedFiles current;
|
|
assert(current.matches(path, 8, sha, false) && hashes == 1); // existing persisted v2 hit
|
|
assert(current.matches(path, 8, sha, true) && hashes == 2); // repair always hashes
|
|
assert(!current.matches(path, 8, std::string(64, '0'), false) && hashes == 3);
|
|
|
|
Sleep(30);
|
|
write(target, "changed!");
|
|
fs::last_write_time(target, published_time); // same name, length, file ID, and visible mtime
|
|
assert(!current.matches(path, 8, sha, false) && hashes == 4);
|
|
write(target, "current!");
|
|
assert(current.matches(path, 8, sha, false) && hashes == 5);
|
|
current.save();
|
|
write(g_root / "replacement", "replace!");
|
|
fs::last_write_time(g_root / "replacement", fs::last_write_time(target));
|
|
assert(MoveFileExW((g_root / "replacement").c_str(), target.c_str(), MOVEFILE_REPLACE_EXISTING));
|
|
assert(!current.matches(path, 8, sha, false) && hashes == 6);
|
|
write(target, "current!");
|
|
assert(current.matches(path, 8, sha, false));
|
|
current.save();
|
|
fs::remove(target);
|
|
unsigned before = hashes;
|
|
assert(!current.matches(path, 8, sha, false) && hashes == before); // deleted previously verified target
|
|
write(target, "wrong size");
|
|
assert(!current.matches(path, 8, sha, false) && hashes == before);
|
|
}
|
|
'''
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--wine-prefix', required=True, type=Path, help='existing disposable Wine prefix')
|
|
parser.add_argument('--work-dir', type=Path, help='parent directory for the temporary fixture')
|
|
args = parser.parse_args()
|
|
code = (prelude + section('static std::wstring widen(', '// ---- RSA PKCS').replace('static std::string sha_file(', 'static std::string real_sha_file(')
|
|
+ '\nunsigned hashes = 0;\nstd::string sha_file(const fs::path& path) { ++hashes; return real_sha_file(path); }\n'
|
|
+ section('static void save_list_cache(', 'static std::vector<uint8_t> unhex(')
|
|
+ section('static std::string file_stamp(', 'using current_files::Outputs;') + cases)
|
|
with tempfile.TemporaryDirectory(dir=args.work_dir) as tmp:
|
|
root = Path(tmp).resolve()
|
|
(root / 'check.cpp').write_text(code)
|
|
subprocess.run(['x86_64-w64-mingw32-g++', '-std=c++20', '-O2', '-static', '-I', here,
|
|
'check.cpp', '-lbcrypt', '-o', 'check.exe'], cwd=root, check=True)
|
|
env = dict(os.environ, WINEPREFIX=str(args.wine_prefix.resolve()), WINEDEBUG='-all', DISPLAY='',
|
|
WAYLAND_DISPLAY='', WINEDLLOVERRIDES='winemenubuilder.exe=d')
|
|
subprocess.run(['wine', './check.exe'], cwd=root, env=env, check=True)
|
|
print('Disk identity, same-length corruption, changed expected SHA, full rehash, deletion and cache reuse passed.')
|