Files
WoW-Hybrid-gateway/tests/patch_updater_cli_test.py

114 lines
6.8 KiB
Python

#!/usr/bin/env python3
"""Compile the actual updater CLI/wait functions with deterministic WinAPI operands."""
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
class UpdaterCLI(unittest.TestCase):
def test_cache_publication_follows_successful_processing(self):
import re
source = (ROOT / 'ops/patchserver/updater/OctoUpdater.cpp').read_text()
writes = list(re.finditer(r'std::ofstream(?:\s+\w+)?\s*\(cached(?:,|\s*,)', source))
self.assertTrue(writes)
self.assertGreater(writes[0].start(), source.index('apply_store(*s)'),
'manifest cache is published before updates are processed')
actual = source.split('// BEGIN successful update cache boundary', 1)[1].split('// END successful update cache boundary', 1)[0]
harness = r'''
#include <cassert>
#include <stdexcept>
''' + actual + r'''
void run(bool check, bool skipped, unsigned count, int fail_after, unsigned wanted_applied, unsigned wanted_publications) {
unsigned applied=0,published=0;bool failed=false;
try {
complete_update_pass(check,skipped,[&] {
for(unsigned i=0;i<count;++i){if(int(i)==fail_after)throw std::runtime_error("apply failed");++applied;}
return true;
},[&]{++published;});
} catch(const std::runtime_error&){failed=true;}
assert(applied==wanted_applied&&published==wanted_publications);
assert(failed==(!check&&!skipped&&fail_after>=0&&unsigned(fail_after)<count));
}
int main(){
run(false,false,2,-1,2,1); // successful application
run(false,false,0,-1,0,1); // successful empty todo
run(true,false,2,-1,0,0); // --check must not advance notification cache
run(false,true,2,-1,0,0); // running client prevented application
run(false,false,2,0,0,0); // first apply failed
run(false,false,2,1,1,0); // partial apply cannot acknowledge complete manifest
unsigned published=0;complete_update_pass(false,false,[]{return false;},[&]{++published;});assert(published==0); // caught maintenance failure also leaves cache unchanged
}
'''
with tempfile.TemporaryDirectory(prefix='updater-cache-') as tmp:
cpp=Path(tmp)/'test.cpp';exe=Path(tmp)/'test';cpp.write_text(harness)
subprocess.run(['g++','-std=c++20','-Wall','-Wextra','-Werror',str(cpp),'-o',str(exe)],check=True,capture_output=True)
subprocess.run([str(exe)],check=True,capture_output=True)
def test_actual_options_and_fail_closed_wait(self):
source = (ROOT / 'ops/patchserver/updater/OctoUpdater.cpp').read_text()
begin, end = '// BEGIN updater CLI and exit wait', '// END updater CLI and exit wait'
self.assertIn(begin, source, 'updater has no explicit launch/exit-wait contract')
actual = source.split(begin, 1)[1].split(end, 1)[0]
prefix = r'''
#include <cassert>
#include <cstdint>
#include <string>
#include <vector>
#include <stdexcept>
#include <initializer_list>
#include "client_update_policy.hpp"
using DWORD=uint32_t;using HANDLE=void*;
constexpr DWORD SYNCHRONIZE=0x100000, ERROR_INVALID_PARAMETER=87;
constexpr DWORD WAIT_OBJECT_0=0, WAIT_TIMEOUT=258, WAIT_FAILED=0xffffffff;
constexpr bool FALSE=false;
static DWORD error=0, outcome=0, observed_pid=0, observed_timeout=0;
static bool open_ok=true;static unsigned opens=0,closes=0,waits=0;
HANDLE OpenProcess(DWORD access,bool inherit,DWORD pid){assert(access==SYNCHRONIZE&&!inherit);++opens;observed_pid=pid;return open_ok?reinterpret_cast<void*>(1):nullptr;}
DWORD GetLastError(){return error;}
DWORD WaitForSingleObject(HANDLE h,DWORD timeout){assert(h);++waits;observed_timeout=timeout;return outcome;}
bool CloseHandle(HANDLE h){assert(h);++closes;return true;}
struct Fail:std::runtime_error{using std::runtime_error::runtime_error;};
'''
harness = r'''
UpdaterOptions parse(std::initializer_list<const wchar_t*> values){std::vector<wchar_t*> args;for(auto v:values)args.push_back(const_cast<wchar_t*>(v));return parse_updater_options(int(args.size()),args.data());}
void bad(std::initializer_list<const wchar_t*> values){bool threw=false;try{parse(values);}catch(const Fail&){threw=true;}assert(threw);}
int main(){
auto c=gateway::client_update::Config{{"launch","0"}};
auto o=parse({L"updater",L"--launch",L"--wait-pid",L"4294967295",L"--dir",L"C:\\Octo WoW",L"--repair",L"--no-dialog",L"-console",L"value with spaces"});
assert(o.wait_pid==0xffffffff&&o.directory==L"C:\\Octo WoW"&&o.repair&&o.no_dialog);
assert((o.game_args==std::vector<std::wstring>{L"-console",L"value with spaces"}));
assert(gateway::client_update::should_launch(c,o.explicit_launch,o.no_launch));
for(auto args:{parse({L"u",L"--launch",L"--no-launch"}),parse({L"u",L"--no-launch",L"--launch"})})assert(!gateway::client_update::should_launch(c,args.explicit_launch,args.no_launch));
auto check=parse({L"u",L"--check"});assert(check.check_only&&check.game_args.empty());
assert(!gateway::client_update::should_launch(c,false,false));
for(auto value:{L"0",L"-1",L"+1",L"4294967296",L"18446744073709551616",L"12x",L" 12",L""})bad({L"u",L"--wait-pid",value});
bad({L"u",L"--wait-pid"});bad({L"u",L"--wait-pid",L"12",L"--wait-pid",L"13"});bad({L"u",L"--dir"});
wait_for_client_exit(0);assert(opens==0);
wait_for_client_exit(123);assert(observed_pid==123&&observed_timeout==60000&&waits==1&&closes==1);
open_ok=false;error=ERROR_INVALID_PARAMETER;wait_for_client_exit(123);assert(waits==1&&closes==1);
// Failure must prevent the caller's subsequent install and launch actions.
for(int mode=0;mode<3;++mode){open_ok=mode!=0;error=5;outcome=mode==1?WAIT_TIMEOUT:WAIT_FAILED;unsigned install=0,launch=0;bool failed=false;auto old_closes=closes;
try{wait_for_client_exit(123);++install;++launch;}catch(const Fail&){failed=true;}
assert(failed&&install==0&&launch==0&&closes==old_closes+(open_ok?1:0));
}
}
'''
with tempfile.TemporaryDirectory(prefix='updater-cli-') as tmp:
cpp=Path(tmp)/'test.cpp';exe=Path(tmp)/'test'
cpp.write_text(prefix+actual+harness)
subprocess.run(['g++','-std=c++20','-Wall','-Wextra','-Werror','-I',str(ROOT/'native'),str(cpp),'-o',str(exe)],check=True,capture_output=True)
subprocess.run([str(exe)],check=True,capture_output=True)
# The actual entry point must return on failed wait before any update
# mutation; the legacy general update-error path may launch the old game.
entry=source.split('int wmain(int argc, wchar_t** argv)',1)[1]
first_mutation=entry.index('DeleteFileW(')
early=entry[:first_mutation]
self.assertIn('wait_for_client_exit(options.wait_pid)',early)
self.assertIn('return 1;',early)
self.assertIn('gateway::client_update::should_launch(cfg, options.explicit_launch, options.no_launch)',entry)
self.assertIn('launch_game(options.game_args)',entry)
if __name__=='__main__':unittest.main()