mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-20 14:39:58 -04:00
Merge the save export and import for bug reports into the test line.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -2179,6 +2179,211 @@ class SavesTest(unittest.TestCase):
|
||||
self.assertEqual((acct / 'save.dat').read_bytes(), b'1') # nothing changed
|
||||
|
||||
|
||||
class SaveShareTest(unittest.TestCase):
|
||||
"""launcher/saves.py export_save and import_zip: one zip with the save and a manifest, for a bug report or another
|
||||
player, and the checks an import makes before it replaces the save."""
|
||||
|
||||
SAVE = {'account.sav': b'acct', 'option.sav': b'opt', 'slot_00/progress.sav': b'p0', 'slot_00/caption.sav': b'c0',
|
||||
'slot_02/progress.sav': b'p2'}
|
||||
|
||||
def setUp(self):
|
||||
self.tmp = Path(tempfile.mkdtemp(prefix='nx2navi-share-'))
|
||||
self.game = self.tmp / 'game'
|
||||
self.out = self.tmp / 'out'
|
||||
self.out.mkdir()
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.tmp, ignore_errors=True)
|
||||
|
||||
def files(self, folder, names):
|
||||
for n, data in names.items():
|
||||
(folder / n).parent.mkdir(parents=True, exist_ok=True)
|
||||
(folder / n).write_bytes(data)
|
||||
return folder
|
||||
|
||||
def game_with_save(self, game, names=None):
|
||||
self.files(saves.account_dir(game), names or self.SAVE)
|
||||
return game
|
||||
|
||||
def zip_of(self, members, manifest=None):
|
||||
"""A zip with members {name: bytes} and a manifest (export_save's keys unless given; None: none)."""
|
||||
path = self.tmp / f'made-{len(list(self.tmp.glob("made-*")))}.zip'
|
||||
with zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED) as z:
|
||||
if manifest is not False:
|
||||
m = manifest or {'format': saves.FORMAT, 'format_version': 1, 'title_id': '0400486024dec000',
|
||||
'game_version': '1.1.1', 'nx2navi': '0.6.1', 'exported': '2026-11-09T17:30:05Z'}
|
||||
z.writestr(saves.MANIFEST, json.dumps(m))
|
||||
for n, data in members.items():
|
||||
z.writestr(n, data)
|
||||
return path
|
||||
|
||||
def test_export_writes_one_zip_of_the_newest_save(self):
|
||||
self.game_with_save(self.game)
|
||||
tx = self.game / 'profile' / 'save-tx'
|
||||
self.files(tx, {'ready-7/option.sav': b'newer', 'ready-7/slot_00/progress.sav': b'p0 newer',
|
||||
'work/option.sav': b'not committed'})
|
||||
self.files(self.game, {'logs/nx2navi.log': b'log', 'nx2navi.ini': b'[graphics]'})
|
||||
r = saves.export_save(self.game, self.out, version='0.6.1', log=lambda m: None)
|
||||
self.assertRegex(r['path'].name, r'^NX2navi-save-\d{4}-\d\d-\d\d_\d\d-\d\d-\d\d\.zip$')
|
||||
self.assertEqual(r['extras'], [])
|
||||
self.assertEqual([p.name for p in self.out.iterdir()], [r['path'].name]) # no .part left behind
|
||||
with zipfile.ZipFile(r['path']) as z:
|
||||
names = sorted(z.namelist())
|
||||
self.assertEqual(names, ['manifest.json', 'save/option.sav', 'save/slot_00/progress.sav'])
|
||||
self.assertEqual(z.read('save/option.sav'), b'newer') # the finished commit the game would load
|
||||
m = json.loads(z.read('manifest.json'))
|
||||
self.assertEqual((m['format'], m['format_version'], m['title_id'], m['game_version'], m['nx2navi'],
|
||||
m['files'], m['bug_report']), (saves.FORMAT, 1, '0400486024dec000', '1.1.1', '0.6.1', 2,
|
||||
False))
|
||||
self.assertRegex(m['exported'], r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ$')
|
||||
r2 = saves.export_save(self.game, self.out, log=lambda m: None) # the same second: a name of its own
|
||||
self.assertNotEqual(r2['path'], r['path'])
|
||||
|
||||
def test_a_bug_report_adds_the_log_and_settings_and_never_keys(self):
|
||||
self.game_with_save(self.game, dict(self.SAVE, **{'prod.keys': b'secret', 'sub/title.keys': b'secret'}))
|
||||
self.files(self.game, {'logs/nx2navi.log': b'the log', 'logs/nx2navi.1.log': b'older', 'nx2navi.ini': b'[x]',
|
||||
'prod.keys': b'secret'})
|
||||
r = saves.export_save(self.game, self.out, bug_report=True, log=lambda m: None)
|
||||
self.assertTrue(r['path'].name.endswith('-bug-report.zip'))
|
||||
self.assertEqual(r['extras'], ['nx2navi.log', 'nx2navi.ini'])
|
||||
with zipfile.ZipFile(r['path']) as z:
|
||||
names = z.namelist()
|
||||
self.assertEqual(z.read('bug-report/nx2navi.log'), b'the log')
|
||||
self.assertEqual(z.read('bug-report/nx2navi.ini'), b'[x]')
|
||||
self.assertTrue(json.loads(z.read('manifest.json'))['bug_report'])
|
||||
self.assertFalse(any(b'secret' in z.read(n) for n in names))
|
||||
self.assertFalse([n for n in names if 'keys' in n])
|
||||
self.assertEqual(len([n for n in names if n.startswith('save/')]), len(self.SAVE))
|
||||
|
||||
def test_no_save_no_export(self):
|
||||
with self.assertRaises(saves.SaveError):
|
||||
saves.export_save(self.game, self.out, log=lambda m: None) # a game that never ran
|
||||
self.assertFalse((self.game / 'profile' / 'account.uid').exists()) # and the export made no account
|
||||
saves.account_dir(self.game).mkdir(parents=True)
|
||||
with self.assertRaises(saves.SaveError):
|
||||
saves.export_save(self.game, self.out, log=lambda m: None) # an account without files
|
||||
self.assertEqual(list(self.out.iterdir()), [])
|
||||
|
||||
def test_a_shared_save_replaces_the_save_and_keeps_a_backup(self):
|
||||
other = self.game_with_save(self.tmp / 'other')
|
||||
shared = saves.export_save(other, self.out, bug_report=True, version='0.6.1', log=lambda m: None)['path']
|
||||
self.game_with_save(self.game, {'option.sav': b'mine', 'slot_05/progress.sav': b'mine5'})
|
||||
acct = saves.account_dir(self.game)
|
||||
self.files(self.game / 'profile' / 'save-tx', {'ready-2/option.sav': b'mine newer'})
|
||||
r = saves.import_save(self.game, shared, log=lambda m: None)
|
||||
self.assertEqual(r['manifest']['nx2navi'], '0.6.1')
|
||||
self.assertIsNone(r['slot'])
|
||||
got = {p.relative_to(acct).as_posix(): p.read_bytes() for p in acct.rglob('*') if p.is_file()}
|
||||
self.assertEqual(got, self.SAVE) # every slot: slot_05 is gone too
|
||||
self.assertFalse((acct / 'nx2navi.log').exists()) # the bug report files stay out
|
||||
self.assertEqual((r['backup'] / 'option.sav').read_bytes(), b'mine newer')
|
||||
self.assertEqual(r['backup'].parent, self.game / 'saves')
|
||||
self.assertEqual(list((self.game / 'profile' / 'save-tx').iterdir()), [])
|
||||
self.assertTrue(shared.is_file()) # a picked zip is left where it was
|
||||
|
||||
def test_a_zip_in_the_saves_folder_is_taken_first(self):
|
||||
self.game_with_save(self.game)
|
||||
shared = saves.export_save(self.game_with_save(self.tmp / 'other', {'option.sav': b'theirs'}), self.out,
|
||||
log=lambda m: None)['path']
|
||||
d = saves.ensure_saves_dir(self.game)
|
||||
self.files(d, {'loose.sav': b'x'})
|
||||
shutil.copy2(shared, d / shared.name)
|
||||
src, _ = saves.waiting(self.game)
|
||||
self.assertEqual(src, d / shared.name)
|
||||
r = saves.import_save(self.game, log=lambda m: None)
|
||||
self.assertEqual((saves.account_dir(self.game) / 'option.sav').read_bytes(), b'theirs')
|
||||
self.assertFalse((saves.account_dir(self.game) / shared.name).exists()) # not copied in as a file
|
||||
done = [e for e in d.iterdir() if e.name.startswith('imported ')]
|
||||
self.assertEqual([p.name for p in done[0].iterdir()], [shared.name]) # moved out: not imported twice
|
||||
self.assertEqual(saves.waiting(self.game)[0], d) # the loose file waits on its own
|
||||
self.assertIsNotNone(r['backup'])
|
||||
# picking the saves folder itself takes what waits there, as the row does
|
||||
shutil.copy2(shared, d / 'again.zip')
|
||||
r = saves.import_save(self.game, d, log=lambda m: None)
|
||||
self.assertEqual(r['source'].name, 'again.zip')
|
||||
|
||||
def test_an_import_in_the_launcher_drops_the_game_menus_waiting_import(self):
|
||||
self.game_with_save(self.game)
|
||||
self.files(self.game / 'profile', {'save-import/option.sav': b'from the menu',
|
||||
'save-import.part/option.sav': b'half'})
|
||||
self.assertIsNotNone(saves.pending_import(self.game))
|
||||
shared = saves.export_save(self.game_with_save(self.tmp / 'other'), self.out, log=lambda m: None)['path']
|
||||
saves.import_save(self.game, shared, log=lambda m: None)
|
||||
self.assertIsNone(saves.pending_import(self.game))
|
||||
self.assertFalse((self.game / 'profile' / 'save-import.part').exists())
|
||||
|
||||
def test_zips_that_are_refused(self):
|
||||
acct = saves.account_dir(self.game_with_save(self.game))
|
||||
good = {'save/option.sav': b'o', 'save/slot_00/progress.sav': b'p'}
|
||||
manifest = {'format': saves.FORMAT, 'format_version': 1, 'title_id': '0400486024dec000'}
|
||||
notzip = self.tmp / 'not.zip'
|
||||
notzip.write_bytes(b'PK not really')
|
||||
cases = {
|
||||
'not a zip': notzip,
|
||||
'no manifest': self.zip_of(good, manifest=False),
|
||||
'another format': self.zip_of(good, dict(manifest, format='something-else')),
|
||||
'a newer format': self.zip_of(good, dict(manifest, format_version=2)),
|
||||
'no format version': self.zip_of(good, {'format': saves.FORMAT, 'title_id': '0400486024dec000'}),
|
||||
'another game': self.zip_of(good, dict(manifest, title_id='0100000000010000')),
|
||||
'a path outside the save': self.zip_of(dict(good, **{'save/../../evil.sav': b'x'}), manifest),
|
||||
'an absolute path': self.zip_of(dict(good, **{'save//abs.sav': b'x'}), manifest),
|
||||
'a drive': self.zip_of(dict(good, **{'save/C:/evil.sav': b'x'}), manifest),
|
||||
'a backslash': self.zip_of(dict(good, **{'save/..\\evil.sav': b'x'}), manifest),
|
||||
'not this game': self.zip_of({'save/save.dat': b'x', 'save/user/0001.bin': b'y'}, manifest),
|
||||
'no save': self.zip_of({'bug-report/nx2navi.log': b'l'}, manifest),
|
||||
}
|
||||
for why, path in cases.items():
|
||||
with self.subTest(why=why):
|
||||
with self.assertRaises(saves.SaveError):
|
||||
saves.import_save(self.game, path, log=lambda m: None)
|
||||
self.assertEqual((acct / 'option.sav').read_bytes(), self.SAVE['option.sav'])
|
||||
self.assertFalse((self.tmp / 'evil.sav').exists())
|
||||
self.assertFalse((self.game / 'saves').exists() and
|
||||
[e for e in (self.game / 'saves').iterdir() if e.name.startswith('backup ')])
|
||||
|
||||
def test_a_link_in_the_zip_is_refused(self):
|
||||
acct = saves.account_dir(self.game_with_save(self.game))
|
||||
path = self.zip_of({'save/option.sav': b'o'})
|
||||
with zipfile.ZipFile(path, 'a') as z:
|
||||
info = zipfile.ZipInfo('save/slot_00/progress.sav')
|
||||
info.external_attr = (stat.S_IFLNK | 0o777) << 16
|
||||
z.writestr(info, '../../../outside')
|
||||
with self.assertRaises(saves.SaveError):
|
||||
saves.import_save(self.game, path, log=lambda m: None)
|
||||
self.assertEqual((acct / 'option.sav').read_bytes(), self.SAVE['option.sav'])
|
||||
|
||||
def test_a_damaged_zip_changes_nothing(self):
|
||||
acct = saves.account_dir(self.game_with_save(self.game))
|
||||
path = self.zip_of({'save/option.sav': b'theirs' * 200, 'save/slot_00/progress.sav': b'p' * 300})
|
||||
data = bytearray(path.read_bytes())
|
||||
at = data.index(b'save/option.sav') + len('save/option.sav') + 4 # inside the first member's data
|
||||
data[at] ^= 0xff
|
||||
path.write_bytes(bytes(data))
|
||||
with self.assertRaises(saves.SaveError):
|
||||
saves.import_save(self.game, path, log=lambda m: None)
|
||||
self.assertEqual((acct / 'option.sav').read_bytes(), self.SAVE['option.sav'])
|
||||
self.assertFalse((self.game / 'profile' / 'save-tx' / 'import').exists())
|
||||
|
||||
def test_the_zip_of_the_games_menu_imports(self):
|
||||
# runtime/save_share.c writes the same layout with stored and deflated members; Python's reader takes both
|
||||
path = self.tmp / 'from-the-game.zip'
|
||||
with zipfile.ZipFile(path, 'w') as z:
|
||||
z.writestr(saves.MANIFEST, json.dumps({'format': saves.FORMAT, 'format_version': 1, 'exported_by': 'game',
|
||||
'title_id': '0400486024DEC000', 'game_version': '1.1.1'}))
|
||||
z.writestr('save/option.sav', b'stored', zipfile.ZIP_STORED)
|
||||
z.writestr('save/slot_01/progress.sav', b'deflated' * 50, zipfile.ZIP_DEFLATED)
|
||||
r = saves.import_save(self.game, path, log=lambda m: None) # a game that never ran: no backup
|
||||
self.assertIsNone(r['backup'])
|
||||
acct = saves.account_dir(self.game)
|
||||
self.assertEqual((acct / 'slot_01' / 'progress.sav').read_bytes(), b'deflated' * 50)
|
||||
|
||||
@unittest.skipUnless(os.name == 'nt', 'the shell\'s known folders are Windows only')
|
||||
def test_the_default_export_folder(self):
|
||||
d = saves.default_export_dir()
|
||||
self.assertTrue(d.is_dir())
|
||||
self.assertIn(d, (winsys.known_folder('downloads'), winsys.known_folder('desktop'), Path.home()))
|
||||
|
||||
|
||||
class IconTest(unittest.TestCase):
|
||||
def test_navi(self):
|
||||
try:
|
||||
@@ -2237,7 +2442,7 @@ class WindowTest(Tmp):
|
||||
r = subprocess.run([sys.executable, '-m', 'launcher', '--root', str(self.tmp / 'w'), '--selftest'],
|
||||
cwd=ROOT, capture_output=True, text=True, timeout=180)
|
||||
self.assertEqual(r.returncode, 0, r.stderr)
|
||||
self.assertIn('11 screens drawn', r.stdout)
|
||||
self.assertIn('12 screens drawn', r.stdout)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
88
tests/save_share_check.c
Normal file
88
tests/save_share_check.c
Normal file
@@ -0,0 +1,88 @@
|
||||
// runtime/fs_host.c, save_share.c and save_zip.c without the game, driven by tests/save_share_check.py. Each run of
|
||||
// this program is one start of the game on the profile folder it is given: the first storage use runs storage_init,
|
||||
// which makes a waiting import the save (fs_host.c import_pending). The rest of the runtime is stubbed below: only what
|
||||
// the save paths call does anything.
|
||||
// save_share_check export PROFILE DIR [bug] save_share_export into DIR; prints "result N path P"
|
||||
// save_share_check import PROFILE ZIP save_share_import; prints "result N detail D waiting W"
|
||||
// save_share_check start PROFILE a start and nothing else; prints "done D waiting W"
|
||||
// save_share_check cancel PROFILE save_share_cancel; prints "result N waiting W"
|
||||
// Backups go to saves/ beside this program (save_share_backup_dir), as the game's go beside the game.
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "save_share.h"
|
||||
|
||||
// fs.h's own declarations of these would clash with the stubs below, which only have to link
|
||||
void fs_host_set_profile(const char *dir);
|
||||
const char *fs_save_import_done(void);
|
||||
int fs_save_export(int (*visit)(void *ctx, const char *rel, const uint8_t *data, size_t n), void *ctx);
|
||||
|
||||
static int count_file(void *ctx, const char *rel, const uint8_t *data, size_t n) {
|
||||
(void)ctx, (void)rel, (void)data, (void)n;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ---- stubs for the runtime around fs_host.c (host.h and the files that define these in the game)
|
||||
const int rt_game_version = 111;
|
||||
const char rt_port_version[] = "9.9.9-check";
|
||||
|
||||
void rt_log(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
fputs("log: ", stderr);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
fputc('\n', stderr);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void rt_fatal(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
fputs("FATAL: ", stderr);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
fputc('\n', stderr);
|
||||
va_end(ap);
|
||||
exit(99);
|
||||
}
|
||||
|
||||
const char *rt_log_path(void) { return getenv("SAVE_SHARE_CHECK_LOG"); }
|
||||
void *rt_config(void) { return NULL; } // fs_host_set_profile names the profile first
|
||||
void account_uid(uint64_t uid[2]) { uid[0] = 0x1111222233334444ull, uid[1] = 0x5555666677778888ull; }
|
||||
int folder_downloads(char *out, size_t cap) { (void)out, (void)cap; return 0; }
|
||||
|
||||
// never reached on the save paths: RomFS, the host function registry, guest memory, the probes
|
||||
#define NEVER(name) \
|
||||
void name(void) { rt_fatal(#name " called"); }
|
||||
NEVER(fs_init) NEVER(hostfn_override) NEVER(hostfn_register) NEVER(mem_kernel_alloc) NEVER(mem_kernel_free)
|
||||
NEVER(mem_thaw) NEVER(romfs_close) NEVER(romfs_entry) NEVER(romfs_entry_type) NEVER(romfs_file_count)
|
||||
NEVER(romfs_handle) NEVER(romfs_image_path) NEVER(romfs_list) NEVER(romfs_open) NEVER(romfs_overlay)
|
||||
NEVER(romfs_overlay_count) NEVER(romfs_read) NEVER(romfs_size) NEVER(rt_call)
|
||||
int romfs_owns(const char *path) { (void)path; return 0; }
|
||||
void fs_trace(const char *what, const char *path) { (void)what, (void)path; }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 3) return 2;
|
||||
fs_host_set_profile(argv[2]);
|
||||
const char *mode = argv[1];
|
||||
if (!strcmp(mode, "export") && argc >= 4) {
|
||||
char path[2048];
|
||||
SaveShareResult r = save_share_export(argv[3], argc > 4 && !strcmp(argv[4], "bug"), path, sizeof path);
|
||||
printf("result %d path %s\n", (int)r, path);
|
||||
} else if (!strcmp(mode, "import") && argc == 4) {
|
||||
char detail[256];
|
||||
SaveShareResult r = save_share_import(argv[3], detail, sizeof detail);
|
||||
printf("result %d detail %s waiting %d\n", (int)r, detail, save_share_pending());
|
||||
} else if (!strcmp(mode, "start")) {
|
||||
fs_save_export(count_file, NULL); // the first storage use: storage_init and its import_pending
|
||||
printf("done %s waiting %d\n", fs_save_import_done(), save_share_pending());
|
||||
} else if (!strcmp(mode, "cancel")) {
|
||||
int r = save_share_cancel();
|
||||
printf("result %d waiting %d\n", r, save_share_pending());
|
||||
} else {
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
196
tests/save_share_check.py
Normal file
196
tests/save_share_check.py
Normal file
@@ -0,0 +1,196 @@
|
||||
"""Build and run tests/save_share_check.c: the game's side of a shared save without the game. runtime/fs_host.c,
|
||||
save_share.c and save_zip.c with the rest of the runtime stubbed, each run of the program one start of the game on a
|
||||
profile folder made here:
|
||||
|
||||
- export: the zip holds the save the game would load next (a finished commit waiting in save-tx before the committed
|
||||
save), its manifest as the launcher writes it (launcher/saves.py check_manifest takes it), no key file; with a bug
|
||||
report the log and nx2navi.ini; nothing saved yet is refused
|
||||
- import: zips the launcher exports wait in profile/save-import; at the next start the save the game would load goes
|
||||
to saves/backup <time> beside the program, the import becomes the save (the finished commit it replaces too), and
|
||||
the start says where the old save went; a second start changes nothing
|
||||
- refusals (no manifest, another game, a newer format, a path outside the save, a damaged member, no save files):
|
||||
nothing waits and the save stays; Cancel drops a waiting import
|
||||
|
||||
Usage: save_share_check.py (Python 3.12 or later; no game files)
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
|
||||
ACCOUNT = '5555666677778888' + '1111222233334444' # tests/save_share_check.c account_uid, as fs_host.c names it
|
||||
|
||||
from launcher import saves # noqa: E402
|
||||
|
||||
|
||||
def compiler():
|
||||
"""$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn)."""
|
||||
if os.environ.get('CC'):
|
||||
return os.environ['CC']
|
||||
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
|
||||
return str(MSYS_BIN / 'clang.exe')
|
||||
return shutil.which('clang') or 'clang'
|
||||
|
||||
|
||||
def tree(folder):
|
||||
folder = Path(folder)
|
||||
return {p.relative_to(folder).as_posix(): p.read_bytes() for p in folder.rglob('*') if p.is_file()} \
|
||||
if folder.is_dir() else None
|
||||
|
||||
|
||||
def main():
|
||||
work = ROOT / 'build' / 'test' / 'save_share'
|
||||
shutil.rmtree(work, ignore_errors=True)
|
||||
work.mkdir(parents=True)
|
||||
exe = work / ('save_share_check.exe' if os.name == 'nt' else 'save_share_check')
|
||||
env = dict(os.environ)
|
||||
if os.name == 'nt' and MSYS_BIN.exists():
|
||||
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
|
||||
rt = ROOT / 'runtime'
|
||||
plat = ['platform/win32.c', 'platform/common.c'] if os.name == 'nt' else ['platform/linux.c', 'platform/common.c']
|
||||
libs = ['-lbcrypt', '-lwinmm', '-lpsapi', '-lshell32', '-lole32', '-luser32'] if os.name == 'nt' else \
|
||||
['-lpthread', '-ldl', '-lm']
|
||||
subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-g', '-w', '-I', str(rt), str(ROOT / 'tests' /
|
||||
'save_share_check.c'), *(str(rt / f) for f in ('fs_host.c', 'save_share.c', 'save_zip.c',
|
||||
*plat)), '-o', str(exe), *libs], env=env)
|
||||
bad = 0
|
||||
|
||||
def check(ok, what):
|
||||
nonlocal bad
|
||||
if not ok:
|
||||
bad += 1
|
||||
print('FAIL', what)
|
||||
|
||||
def run(*args, log=None):
|
||||
e = dict(env, SAVE_SHARE_CHECK_LOG=str(log)) if log else env
|
||||
r = subprocess.run([str(exe), *map(str, args)], env=e, capture_output=True, text=True)
|
||||
if r.returncode:
|
||||
print(r.stdout, r.stderr)
|
||||
check(r.returncode == 0, f'{args[0]} exited with {r.returncode}')
|
||||
return r.stdout.strip()
|
||||
|
||||
profile = work / 'profile'
|
||||
acct = profile / 'save' / ACCOUNT
|
||||
mine = {'account.sav': b'mine a', 'option.sav': b'mine o', 'slot_00/progress.sav': b'mine 0' * 500,
|
||||
'slot_00/caption.sav': b'c0'}
|
||||
newer = {'account.sav': b'newer a', 'option.sav': b'newer o', 'slot_01/progress.sav': b'newer 1' * 300}
|
||||
|
||||
# export: nothing yet, then the finished commit before the committed save
|
||||
out = work / 'out'
|
||||
check(run('export', profile, out).startswith('result 1 '), 'export with no save refused')
|
||||
for name, data in mine.items():
|
||||
(acct / name).parent.mkdir(parents=True, exist_ok=True)
|
||||
(acct / name).write_bytes(data)
|
||||
ready = profile / 'save-tx' / 'ready-0000000007'
|
||||
for name, data in newer.items():
|
||||
(ready / name).parent.mkdir(parents=True, exist_ok=True)
|
||||
(ready / name).write_bytes(data)
|
||||
(ready / 'prod.keys').write_bytes(b'secret') # a stray key file in a save folder never goes into a zip
|
||||
(work / 'nx2navi.ini').write_bytes(b'[graphics]\nfps_limit = 60\n')
|
||||
(work / 'game.log').write_bytes(b'version: NX2navi 9.9.9-check\n')
|
||||
line = run('export', profile, out, 'bug', log=work / 'game.log')
|
||||
m = re.match(r'result 0 path (.+)$', line)
|
||||
check(m is not None, f'export: {line}')
|
||||
if m:
|
||||
path = Path(m.group(1))
|
||||
check(re.fullmatch(r'NX2navi-save-\d{4}-\d\d-\d\d_\d\d-\d\d-\d\d-bug-report\.zip', path.name) is not None,
|
||||
f'export name {path.name}')
|
||||
with zipfile.ZipFile(path) as z:
|
||||
check(z.testzip() is None, 'export zip CRCs')
|
||||
got = {n[len('save/'):]: z.read(n) for n in z.namelist() if n.startswith('save/')}
|
||||
# the start moved the finished commit in (swap_in) before the export read it
|
||||
check(got == newer, f'export holds the newest save: {sorted(got)}')
|
||||
check(z.read('bug-report/nx2navi.log').startswith(b'version: NX2navi') and
|
||||
z.read('bug-report/nx2navi.ini').startswith(b'[graphics]'), 'bug report files')
|
||||
check(not any(b'secret' in z.read(n) for n in z.namelist()), 'no key file')
|
||||
man = json.loads(z.read('manifest.json'))
|
||||
saves.check_manifest(man)
|
||||
check((man['exported_by'], man['game_version'], man['nx2navi'], man['files'], man['bug_report']) ==
|
||||
('game', '1.1.1', '9.9.9-check', len(newer), True), f'manifest {man}')
|
||||
check(re.fullmatch(r'\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ', man['exported']) is not None, 'export time')
|
||||
plain = re.match(r'result 0 path (.+)$', run('export', profile, out))
|
||||
check(plain is not None and Path(plain.group(1)).name.endswith('.zip') and
|
||||
'bug-report' not in Path(plain.group(1)).name, 'export without a bug report')
|
||||
if plain:
|
||||
with zipfile.ZipFile(plain.group(1)) as z:
|
||||
check(not [n for n in z.namelist() if n.startswith('bug-report/')], 'no bug report files')
|
||||
check(tree(acct) == dict(newer, **{'prod.keys': b'secret'}), 'the start swapped the finished commit in')
|
||||
check([tree(d) for d in (profile / 'save-backup').iterdir()] == [mine], 'and kept the save it replaced')
|
||||
|
||||
# import: a zip the launcher exports, from another game folder
|
||||
other = work / 'other-game'
|
||||
oacct = saves.account_dir(other)
|
||||
shared_save = {'account.sav': b'theirs a', 'option.sav': b'theirs o', 'slot_02/progress.sav': b'theirs 2' * 999,
|
||||
'slot_02/caption.sav': b'cap'}
|
||||
for name, data in shared_save.items():
|
||||
(oacct / name).parent.mkdir(parents=True, exist_ok=True)
|
||||
(oacct / name).write_bytes(data)
|
||||
shared = saves.export_save(other, work / 'shared', bug_report=True, version='0.6.2', log=lambda m: None)['path']
|
||||
before = tree(acct)
|
||||
line = run('import', profile, shared)
|
||||
check(re.match(r'result 0 detail 0\.6\.2, \d{4}-\d\d-\d\d waiting 1$', line) is not None, f'import: {line}')
|
||||
check(tree(acct) == before, 'the save is unchanged until the next start')
|
||||
check(tree(profile / 'save-import') == shared_save, 'the import waits complete in profile/save-import')
|
||||
# a finished commit made after the import (the game saved before the restart) is backed up and replaced too
|
||||
late = profile / 'save-tx' / 'ready-0000000009'
|
||||
(late / 'option.sav').parent.mkdir(parents=True, exist_ok=True)
|
||||
(late / 'option.sav').write_bytes(b'saved after the import')
|
||||
line = run('start', profile)
|
||||
m = re.match(r'done (.+) waiting 0$', line)
|
||||
check(m is not None and Path(m.group(1)).parent == work / 'saves', f'start: {line}')
|
||||
check(tree(acct) == shared_save, f'the import is the save: {sorted(tree(acct) or {})}')
|
||||
if m:
|
||||
backup = Path(m.group(1))
|
||||
check(re.fullmatch(r'backup \d{4}-\d\d-\d\d \d\d\.\d\d\.\d\d', backup.name) is not None, backup.name)
|
||||
check(tree(backup) == {'option.sav': b'saved after the import'}, f'the backup is the save the game would '
|
||||
f'have loaded: {sorted(tree(backup) or {})}')
|
||||
check(not (profile / 'save-import').exists() and not list((profile / 'save-tx').glob('ready-*')),
|
||||
'nothing waits after the start')
|
||||
check(run('start', profile) == 'done waiting 0', 'a second start imports nothing')
|
||||
check(tree(acct) == shared_save, 'and keeps the imported save')
|
||||
|
||||
# refusals and Cancel
|
||||
def zip_of(name, members, manifest=True):
|
||||
path = work / name
|
||||
with zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED) as z:
|
||||
if manifest:
|
||||
z.writestr('manifest.json', json.dumps(dict({'format': 'nx2navi-save', 'format_version': 1,
|
||||
'title_id': '0400486024dec000'},
|
||||
**(manifest if isinstance(manifest, dict) else {}))))
|
||||
for n, b in members.items():
|
||||
z.writestr(n, b)
|
||||
return path
|
||||
good = {'save/option.sav': b'x', 'save/slot_00/progress.sav': b'y' * 400}
|
||||
cases = {
|
||||
4: zip_of('nomanifest.zip', good, manifest=False),
|
||||
5: zip_of('othergame.zip', good, {'title_id': '0100000000010000'}),
|
||||
6: zip_of('newer.zip', good, {'format_version': 2}),
|
||||
8: zip_of('outside.zip', dict(good, **{'save/../escape.sav': b'z'})),
|
||||
}
|
||||
damaged = bytearray(zip_of('damaged.zip', good).read_bytes())
|
||||
at = damaged.index(b'save/slot_00/progress.sav') + len('save/slot_00/progress.sav') + 3
|
||||
damaged[at] ^= 0x40
|
||||
(work / 'damaged.zip').write_bytes(damaged)
|
||||
cases[7] = work / 'damaged.zip'
|
||||
cases[4.5] = zip_of('nosave.zip', {'save/save.dat': b'q'})
|
||||
for want, path in cases.items():
|
||||
line = run('import', profile, path)
|
||||
check(line.startswith(f'result {int(want)} ') and line.endswith('waiting 0'), f'{path.name}: {line}')
|
||||
check(not (work / 'escape.sav').exists() and tree(acct) == shared_save, 'a refused zip changes nothing')
|
||||
check(run('import', profile, shared).endswith('waiting 1'), 'import again')
|
||||
check(run('cancel', profile) == 'result 0 waiting 0', 'cancel')
|
||||
check(run('start', profile) == 'done waiting 0' and tree(acct) == shared_save, 'a cancelled import stays out')
|
||||
|
||||
print('save_share_check:', 'FAILED' if bad else 'ok')
|
||||
return 1 if bad else 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
184
tests/save_zip_check.c
Normal file
184
tests/save_zip_check.c
Normal file
@@ -0,0 +1,184 @@
|
||||
// runtime/save_zip.c on its own, driven by tests/save_zip_check.py:
|
||||
// save_zip_check selftest the manifest's JSON keys, round trips in memory, damaged input
|
||||
// save_zip_check inflate IN SIZE OUT raw deflate IN (zlib's) into SIZE bytes at OUT; exit 3 when refused
|
||||
// save_zip_check deflate IN OUT raw deflate of IN into OUT, for zlib to read back
|
||||
// save_zip_check zip OUT NAME FILE [NAME FILE] a zip of the files under those names
|
||||
// save_zip_check unzip ZIP DIR every member into DIR/<index>, a line "name|crc|size|dir|link" each;
|
||||
// exit 3 with the SZ_E_* code when a member or the zip is refused
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "save_zip.h"
|
||||
|
||||
static int failures;
|
||||
#define CHECK(c, ...) \
|
||||
do { \
|
||||
if (!(c)) { \
|
||||
failures++; \
|
||||
fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
fputc('\n', stderr); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static uint8_t *read_all(const char *path, size_t *n) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return NULL;
|
||||
fseek(f, 0, SEEK_END);
|
||||
long len = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
uint8_t *buf = malloc(len > 0 ? (size_t)len : 1);
|
||||
*n = buf && fread(buf, 1, (size_t)len, f) == (size_t)len ? (size_t)len : 0;
|
||||
fclose(f);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int write_all(const char *path, const void *data, size_t n) {
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f) return 0;
|
||||
int ok = fwrite(data, 1, n, f) == n;
|
||||
return fclose(f) == 0 && ok;
|
||||
}
|
||||
|
||||
static void selftest_json(void) {
|
||||
const char *j = "{\n \"format\": \"nx2navi-save\", \"nested\": {\"format\": \"inner\", \"list\": [1, \"]\", {}]},\n"
|
||||
" \"format_version\": 1, \"neg\": -12, \"float\": 1.5, \"esc\": \"a\\\"b\\\\c\\u0041\\u00e9\",\n"
|
||||
" \"t\": true, \"title_id\": \"0400486024DEC000\"}";
|
||||
char s[64];
|
||||
long long v = 0;
|
||||
CHECK(sz_json_str(j, "format", s, sizeof s) && !strcmp(s, "nx2navi-save"), "format: %s", s);
|
||||
CHECK(sz_json_int(j, "format_version", &v) && v == 1, "format_version: %lld", v);
|
||||
CHECK(sz_json_int(j, "neg", &v) && v == -12, "neg: %lld", v);
|
||||
CHECK(!sz_json_int(j, "float", &v), "a float is no integer");
|
||||
CHECK(!sz_json_int(j, "format", &v), "a string is no integer");
|
||||
CHECK(!sz_json_str(j, "format_version", s, sizeof s), "an integer is no string");
|
||||
CHECK(sz_json_str(j, "esc", s, sizeof s) && !strcmp(s, "a\"b\\cA?"), "escapes: %s", s);
|
||||
CHECK(sz_json_str(j, "title_id", s, sizeof s) && !strcmp(s, "0400486024DEC000"), "after a literal: %s", s);
|
||||
CHECK(!sz_json_str(j, "list", s, sizeof s) && !sz_json_str(j, "missing", s, sizeof s), "not at the top level");
|
||||
CHECK(sz_json_str(j, "format", s, 5) && !strcmp(s, "nx2n"), "cut: %s", s);
|
||||
CHECK(!sz_json_str("[\"format\"]", "format", s, sizeof s) && !sz_json_str("{\"format\" \"x\"}", "format", s, sizeof s) &&
|
||||
!sz_json_str("", "format", s, sizeof s) && !sz_json_str("{\"format\": \"open", "format", s, sizeof s),
|
||||
"malformed JSON");
|
||||
char e[32];
|
||||
CHECK(!strcmp(sz_json_escape("a\"b\\\n", e, sizeof e), "a\\\"b\\\\\\u000a"), "escape: %s", e);
|
||||
CHECK(!strcmp(sz_json_escape("0123456789", e, 5), "0123"), "escape cut: %s", e);
|
||||
}
|
||||
|
||||
static void selftest_codec(void) {
|
||||
// text with repeats, then bytes of every value: literals of both fixed code lengths, long and far matches
|
||||
size_t n = 200000;
|
||||
uint8_t *src = malloc(n), *back = malloc(n);
|
||||
for (size_t i = 0; i < n; i++) src[i] = i < 120000 ? "fs commit save: 14 files, 2 changed\n"[i % 36] : (uint8_t)(i * 2654435761u >> 13);
|
||||
size_t dn = 0;
|
||||
uint8_t *d = sz_deflate(src, n, &dn);
|
||||
CHECK(d && dn < n, "deflate: %zu bytes of %zu", dn, n);
|
||||
CHECK(d && !sz_inflate(d, dn, back, n) && !memcmp(back, src, n), "inflate of deflate");
|
||||
CHECK(d && sz_inflate(d, dn, back, n - 1), "one byte short of room");
|
||||
CHECK(d && sz_inflate(d, dn - 1, back, n), "the data cut short");
|
||||
uint8_t *empty = sz_deflate(src, 0, &dn);
|
||||
CHECK(empty && !sz_inflate(empty, dn, back, 0), "empty input");
|
||||
free(empty), free(d);
|
||||
CHECK(sz_crc32(0, "123456789", 9) == 0xcbf43926u, "crc32 check value");
|
||||
CHECK(sz_crc32(sz_crc32(0, "1234", 4), "56789", 5) == 0xcbf43926u, "crc32 in parts");
|
||||
|
||||
SzWriter w;
|
||||
sz_writer_init(&w, 2026, 11, 9, 17, 30, 5);
|
||||
sz_writer_add(&w, "manifest.json", "{}", 2);
|
||||
sz_writer_add(&w, "save/slot_00/progress.sav", src, n);
|
||||
sz_writer_add(&w, "save/empty.sav", "", 0);
|
||||
size_t zn = 0;
|
||||
uint8_t *zip = sz_writer_finish(&w, &zn);
|
||||
CHECK(zip != NULL, "writer");
|
||||
SzEntry *e = NULL;
|
||||
int count = 0;
|
||||
CHECK(zip && sz_read_dir(zip, zn, &e, &count) == SZ_OK && count == 3, "read_dir: %d members", count);
|
||||
if (count == 3) {
|
||||
int err;
|
||||
CHECK(!strcmp(e[1].name, "save/slot_00/progress.sav") && e[1].method == 8 && e[0].method == 0, "methods");
|
||||
uint8_t *got = sz_extract(zip, zn, &e[1], &err);
|
||||
CHECK(got && !memcmp(got, src, n) && err == SZ_OK, "extract");
|
||||
free(got);
|
||||
got = sz_extract(zip, zn, &e[2], &err);
|
||||
CHECK(got && got[0] == 0 && e[2].usize == 0, "empty member");
|
||||
free(got);
|
||||
zip[e[1].data + 100] ^= 0x55; // damaged data: refused, by inflate or by the CRC
|
||||
got = sz_extract(zip, zn, &e[1], &err);
|
||||
CHECK(!got && err == SZ_E_DAMAGED, "damaged member read");
|
||||
free(got);
|
||||
zip[e[1].data + 100] ^= 0x55;
|
||||
zip[e[0].data] ^= 1; // a stored member's byte: only the CRC tells
|
||||
got = sz_extract(zip, zn, &e[0], &err);
|
||||
CHECK(!got && err == SZ_E_DAMAGED, "damaged stored member read");
|
||||
free(got);
|
||||
}
|
||||
free(e);
|
||||
for (size_t cut = 0; zip && cut < zn; cut += 997) { // every prefix is refused, never read past its end
|
||||
uint8_t *part = malloc(cut ? cut : 1);
|
||||
memcpy(part, zip, cut);
|
||||
CHECK(sz_read_dir(part, cut, &e, &count) != SZ_OK, "a zip cut at %zu read", cut);
|
||||
free(part);
|
||||
}
|
||||
CHECK(sz_read_dir((const uint8_t *)"PK\5\6", 4, &e, &count) == SZ_E_NOT_ZIP, "too short");
|
||||
free(zip), free(src), free(back);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc >= 2 && !strcmp(argv[1], "selftest")) {
|
||||
selftest_json();
|
||||
selftest_codec();
|
||||
printf("save_zip selftest: %d failures\n", failures);
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
if (argc == 5 && !strcmp(argv[1], "inflate")) {
|
||||
size_t n, want = (size_t)strtoull(argv[3], NULL, 10);
|
||||
uint8_t *in = read_all(argv[2], &n), *out = malloc(want ? want : 1);
|
||||
if (!in || sz_inflate(in, n, out, want)) return 3;
|
||||
return write_all(argv[4], out, want) ? 0 : 2;
|
||||
}
|
||||
if (argc == 4 && !strcmp(argv[1], "deflate")) {
|
||||
size_t n, dn;
|
||||
uint8_t *in = read_all(argv[2], &n), *out = in ? sz_deflate(in, n, &dn) : NULL;
|
||||
return out && write_all(argv[3], out, dn) ? 0 : 2;
|
||||
}
|
||||
if (argc >= 3 && argc % 2 == 1 && !strcmp(argv[1], "zip")) {
|
||||
SzWriter w;
|
||||
sz_writer_init(&w, 2026, 11, 9, 17, 30, 5);
|
||||
for (int i = 3; i + 1 < argc; i += 2) {
|
||||
size_t n;
|
||||
uint8_t *data = read_all(argv[i + 1], &n);
|
||||
if (!data) return 2;
|
||||
sz_writer_add(&w, argv[i], data, n);
|
||||
free(data);
|
||||
}
|
||||
size_t zn;
|
||||
uint8_t *zip = sz_writer_finish(&w, &zn);
|
||||
return zip && write_all(argv[2], zip, zn) ? 0 : 2;
|
||||
}
|
||||
if (argc == 4 && !strcmp(argv[1], "unzip")) {
|
||||
size_t n;
|
||||
uint8_t *zip = read_all(argv[2], &n);
|
||||
SzEntry *e;
|
||||
int count, r = zip ? sz_read_dir(zip, n, &e, &count) : SZ_E_NOT_ZIP;
|
||||
if (r) {
|
||||
printf("refused %d\n", r);
|
||||
return 3;
|
||||
}
|
||||
for (int i = 0; i < count; i++) {
|
||||
int err;
|
||||
uint8_t *data = sz_extract(zip, n, &e[i], &err);
|
||||
if (!data) {
|
||||
printf("refused %d %s\n", err, e[i].name);
|
||||
return 3;
|
||||
}
|
||||
char path[1024];
|
||||
snprintf(path, sizeof path, "%s/%d", argv[3], i);
|
||||
if (!write_all(path, data, (size_t)e[i].usize)) return 2;
|
||||
printf("%s|%08x|%llu|%d|%d\n", e[i].name, e[i].crc, (unsigned long long)e[i].usize, e[i].is_dir, e[i].is_link);
|
||||
free(data);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
fprintf(stderr, "usage: see the top of tests/save_zip_check.c\n");
|
||||
return 2;
|
||||
}
|
||||
220
tests/save_zip_check.py
Normal file
220
tests/save_zip_check.py
Normal file
@@ -0,0 +1,220 @@
|
||||
"""Build and run tests/save_zip_check.c: runtime/save_zip.c, the zip of a shared save as the game's menu writes and
|
||||
reads it (runtime/save_share.c), against Python's zlib and zipfile, which the launcher uses for the same zip
|
||||
(launcher/saves.py).
|
||||
|
||||
- the C program's selftest: the manifest's JSON keys, round trips in memory, damaged and cut input
|
||||
- inflate: raw deflate streams zlib makes at every level and strategy (stored, fixed and dynamic blocks) inflate to
|
||||
the same bytes, and a damaged one is refused
|
||||
- deflate: what the C deflate makes, zlib inflates to the same bytes
|
||||
- zip: a zip the C writer makes passes zipfile's testzip with the same members; zips zipfile makes (stored, deflated,
|
||||
with data descriptors, the launcher's export) unzip in C to the same members with their CRCs; a link is marked
|
||||
- the launcher imports a zip the C writer made (launcher/saves.py import_zip)
|
||||
|
||||
Usage: save_zip_check.py (no game files)
|
||||
"""
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import zipfile
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
|
||||
|
||||
|
||||
def compiler():
|
||||
"""$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn)."""
|
||||
if os.environ.get('CC'):
|
||||
return os.environ['CC']
|
||||
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
|
||||
return str(MSYS_BIN / 'clang.exe')
|
||||
return shutil.which('clang') or 'clang'
|
||||
|
||||
|
||||
class NoSeek(io.RawIOBase):
|
||||
"""A write-only stream zipfile cannot seek in: it writes data descriptors after the members."""
|
||||
def __init__(self):
|
||||
self.buf = bytearray()
|
||||
|
||||
def writable(self):
|
||||
return True
|
||||
|
||||
def write(self, b):
|
||||
self.buf += b
|
||||
return len(b)
|
||||
|
||||
|
||||
def samples():
|
||||
rnd = random.Random(7)
|
||||
log = ''.join(f'[{i:06d}] fs commit save: {rnd.randint(1, 20)} files, {rnd.randint(0, 9)} changed\n'
|
||||
for i in range(4000)).encode()
|
||||
return {
|
||||
'empty': b'',
|
||||
'one': b'x',
|
||||
'text': log,
|
||||
'random': bytes(rnd.getrandbits(8) for _ in range(70000)),
|
||||
'runs': b'\0' * 100000 + b'\xff' * 3 + b'ab' * 40000,
|
||||
'save': bytes(rnd.choice(b'\0\0\0\1\2\x80\xff') for _ in range(150000)),
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
work = ROOT / 'build' / 'test' / 'save_zip'
|
||||
shutil.rmtree(work, ignore_errors=True)
|
||||
work.mkdir(parents=True)
|
||||
exe = work / ('save_zip_check.exe' if os.name == 'nt' else 'save_zip_check')
|
||||
env = dict(os.environ)
|
||||
if os.name == 'nt' and MSYS_BIN.exists():
|
||||
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
|
||||
subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wextra', '-Werror', '-I', str(ROOT / 'runtime'),
|
||||
str(ROOT / 'tests' / 'save_zip_check.c'), str(ROOT / 'runtime' / 'save_zip.c'), '-o',
|
||||
str(exe)], env=env)
|
||||
run = lambda *a: subprocess.run([str(exe), *map(str, a)], env=env, capture_output=True, text=True) # noqa: E731
|
||||
bad = 0
|
||||
|
||||
def check(ok, what):
|
||||
nonlocal bad
|
||||
if not ok:
|
||||
bad += 1
|
||||
print('FAIL', what)
|
||||
|
||||
r = run('selftest')
|
||||
print(r.stdout.strip())
|
||||
check(r.returncode == 0, 'selftest: ' + r.stderr.strip())
|
||||
|
||||
data = samples()
|
||||
for name, raw in data.items():
|
||||
src = work / f'{name}.bin'
|
||||
src.write_bytes(raw)
|
||||
for level in (0, 1, 6, 9):
|
||||
for strategy in (zlib.Z_DEFAULT_STRATEGY, zlib.Z_FIXED, zlib.Z_HUFFMAN_ONLY, zlib.Z_RLE):
|
||||
c = zlib.compressobj(level, zlib.DEFLATED, -15, 9, strategy)
|
||||
packed = work / f'{name}-{level}-{strategy}.deflate'
|
||||
packed.write_bytes(c.compress(raw) + c.flush())
|
||||
out = work / 'out.bin'
|
||||
r = run('inflate', packed, len(raw), out)
|
||||
check(r.returncode == 0 and out.read_bytes() == raw, f'inflate {name} level {level} strategy {strategy}')
|
||||
out = work / f'{name}.c-deflate'
|
||||
r = run('deflate', src, out)
|
||||
try:
|
||||
got = zlib.decompress(out.read_bytes(), -15) if r.returncode == 0 else None
|
||||
except zlib.error as e:
|
||||
got = e
|
||||
check(got == raw, f'C deflate of {name} read by zlib: {got!r:.80}')
|
||||
if raw:
|
||||
print(f' {name}: {len(raw)} bytes, C deflate {out.stat().st_size}, zlib -9 '
|
||||
f'{len(zlib.compress(raw, 9)) - 6}')
|
||||
# a damaged stream: a flipped bit in a dynamic block, and one cut short
|
||||
packed = zlib.compressobj(9, zlib.DEFLATED, -15)
|
||||
stream = bytearray(packed.compress(data['text']) + packed.flush())
|
||||
for mutate in ('flip', 'cut'):
|
||||
s = bytearray(stream)
|
||||
if mutate == 'flip':
|
||||
s[len(s) // 2] ^= 0x10
|
||||
else:
|
||||
del s[len(s) * 2 // 3:]
|
||||
(work / 'bad.deflate').write_bytes(s)
|
||||
r = run('inflate', work / 'bad.deflate', len(data['text']), work / 'out.bin')
|
||||
check(r.returncode != 0 or (work / 'out.bin').read_bytes() != data['text'], f'damaged stream ({mutate}) read')
|
||||
|
||||
# C writes, zipfile reads
|
||||
members = {'manifest.json': b'{"format": "nx2navi-save"}', 'save/option.sav': data['save'][:3000],
|
||||
'save/slot_00/progress.sav': data['save'], 'bug-report/nx2navi.log': data['text'],
|
||||
'save/empty.sav': b''}
|
||||
args = []
|
||||
for i, (n, b) in enumerate(members.items()):
|
||||
(work / f'm{i}').write_bytes(b)
|
||||
args += [n, work / f'm{i}']
|
||||
cz = work / 'c.zip'
|
||||
check(run('zip', cz, *args).returncode == 0, 'C zip')
|
||||
with zipfile.ZipFile(cz) as z:
|
||||
check(z.testzip() is None, 'zipfile testzip of the C zip')
|
||||
check({i.filename: z.read(i) for i in z.infolist()} == members, 'C zip members')
|
||||
check(z.getinfo('save/slot_00/progress.sav').compress_type == zipfile.ZIP_DEFLATED, 'deflated member')
|
||||
check(z.getinfo('manifest.json').compress_type == zipfile.ZIP_STORED, 'small member stored')
|
||||
|
||||
# zipfile writes, C reads
|
||||
def unzip(path):
|
||||
out = work / 'unz'
|
||||
shutil.rmtree(out, ignore_errors=True)
|
||||
out.mkdir()
|
||||
r = run('unzip', path, out)
|
||||
if r.returncode:
|
||||
return r.stdout.strip()
|
||||
got = {}
|
||||
for i, line in enumerate(r.stdout.splitlines()):
|
||||
name, crc, size, is_dir, is_link = line.rsplit('|', 4)
|
||||
got[name] = ((out / str(i)).read_bytes(), int(crc, 16), int(size), is_dir == '1', is_link == '1')
|
||||
return got
|
||||
|
||||
for how in ('stored', 'deflated', 'descriptors'):
|
||||
if how == 'descriptors':
|
||||
stream = NoSeek()
|
||||
with zipfile.ZipFile(stream, 'w', zipfile.ZIP_DEFLATED) as z:
|
||||
for n, b in members.items():
|
||||
with z.open(n, 'w') as f:
|
||||
f.write(b)
|
||||
path = work / 'py-desc.zip'
|
||||
path.write_bytes(bytes(stream.buf))
|
||||
else:
|
||||
path = work / f'py-{how}.zip'
|
||||
with zipfile.ZipFile(path, 'w', zipfile.ZIP_STORED if how == 'stored' else zipfile.ZIP_DEFLATED) as z:
|
||||
for n, b in members.items():
|
||||
z.writestr(n, b)
|
||||
z.writestr('save/folder/', b'')
|
||||
z.comment = b'a comment the end record carries'
|
||||
got = unzip(path)
|
||||
ok = isinstance(got, dict) and all(got[n][0] == b and got[n][1] == zlib.crc32(b) for n, b in members.items())
|
||||
check(ok, f'C unzip of a zipfile zip ({how}): {str(got)[:200]}')
|
||||
if how != 'descriptors':
|
||||
check(isinstance(got, dict) and got['save/folder/'][3], f'folder entry ({how})')
|
||||
# a link member, as a Unix zip tool stores one
|
||||
path = work / 'link.zip'
|
||||
with zipfile.ZipFile(path, 'w') as z:
|
||||
info = zipfile.ZipInfo('save/link.sav')
|
||||
info.create_system = 3
|
||||
info.external_attr = (0o120777) << 16
|
||||
z.writestr(info, '../../outside')
|
||||
got = unzip(path)
|
||||
check(isinstance(got, dict) and got['save/link.sav'][4], 'link marked')
|
||||
# a damaged member: refused by CRC or inflate
|
||||
raw = bytearray((work / 'py-deflated.zip').read_bytes())
|
||||
at = raw.index(b'save/slot_00/progress.sav') + 40
|
||||
raw[at] ^= 0x21
|
||||
(work / 'bad.zip').write_bytes(raw)
|
||||
got = unzip(work / 'bad.zip')
|
||||
check(isinstance(got, str) and got.splitlines()[-1] == 'refused 2 save/slot_00/progress.sav',
|
||||
f'damaged zip read: {str(got)[:120]}')
|
||||
|
||||
# the launcher imports what the game's menu writes
|
||||
from launcher import saves
|
||||
manifest = json.dumps({'format': 'nx2navi-save', 'format_version': 1, 'title_id': '0400486024dec000',
|
||||
'nx2navi': '0.6.1', 'exported': '2026-11-09T17:30:05Z', 'exported_by': 'game'}).encode()
|
||||
(work / 'manifest').write_bytes(manifest)
|
||||
(work / 'opt').write_bytes(b'option')
|
||||
(work / 'prog').write_bytes(data['save'])
|
||||
gz = work / 'game.zip'
|
||||
check(run('zip', gz, 'manifest.json', work / 'manifest', 'save/option.sav', work / 'opt',
|
||||
'save/slot_03/progress.sav', work / 'prog').returncode == 0, 'C zip of a save')
|
||||
game = Path(tempfile.mkdtemp(prefix='nx2navi-zipcheck-'))
|
||||
try:
|
||||
r = saves.import_save(game, gz, log=lambda m: None)
|
||||
acct = saves.account_dir(game)
|
||||
check(r['manifest']['exported_by'] == 'game' and (acct / 'slot_03' / 'progress.sav').read_bytes() ==
|
||||
data['save'], 'launcher import of the C zip')
|
||||
finally:
|
||||
shutil.rmtree(game, ignore_errors=True)
|
||||
|
||||
print('save_zip_check:', 'FAILED' if bad else 'ok')
|
||||
return 1 if bad else 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user