# SPDX-License-Identifier: MIT """Synthetic fixtures only: no customer archives, logs or game installation.""" from contextlib import contextmanager from pathlib import Path import stat import tempfile import unittest from unittest.mock import patch import warnings import zipfile from mc_backup_preflight import inspect_archive @contextmanager def fixture(members): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "fixture.zip" with zipfile.ZipFile(path, "w", compression=zipfile.ZIP_STORED) as archive: with warnings.catch_warnings(): warnings.simplefilter("ignore", UserWarning) for name, contents in members: archive.writestr(name, contents) yield path class ArchiveChecks(unittest.TestCase): def test_complete_crc_does_not_change_archive(self): # Placeholder level.dat is intentionally NOT valid Minecraft NBT. with fixture([("server/world/level.dat", b"synthetic"), ("server/world/region/r.0.0.mca", b"synthetic region")]) as path: original = path.read_bytes() report = inspect_archive(path, verify=True) self.assertEqual(report["crc"], "complete") self.assertEqual(report["exit_code"], 0) self.assertEqual(report["world_candidates"], ["server/world"]) self.assertEqual(path.read_bytes(), original) self.assertEqual(list(path.parent.iterdir()), [path]) def test_untrusted_paths_are_flagged_without_extraction(self): for name in ("../outside", "/absolute", "C:/drive", "world\\file"): stored_name = name.replace("\\", "/") with self.subTest(name=name), fixture([(stored_name, b"x")]) as path: # On Windows ZipInfo normalizes backslashes when writing. Patch # both ZIP name fields to exercise an actual external archive. if "\\" in name: path.write_bytes(path.read_bytes().replace(stored_name.encode(), name.encode())) report = inspect_archive(path, verify=True) self.assertIn("unsafe_path", [issue["code"] for issue in report["issues"]]) self.assertEqual(report["crc"], "not_run_metadata_errors") self.assertEqual(list(path.parent.iterdir()), [path]) def test_duplicate_and_symlink_are_flagged(self): link = zipfile.ZipInfo("world/link") link.create_system = 3 link.external_attr = (stat.S_IFLNK | 0o777) << 16 with fixture([("world/level.dat", b"x"), ("world/level.dat", b"y"), (link, b"/etc/passwd")]) as path: codes = {issue["code"] for issue in inspect_archive(path)["issues"]} self.assertTrue({"duplicate_name", "symlink"}.issubset(codes)) def test_byte_limit_means_not_checked(self): with fixture([("world/level.dat", b"synthetic")]) as path: report = inspect_archive(path, verify=True, max_verify_bytes=2) self.assertEqual(report["crc"], "not_run_byte_limit") self.assertEqual(report["crc_bytes_read"], 0) self.assertEqual(report["exit_code"], 1) def test_entry_limit_and_missing_world(self): with fixture([("a", b"x"), ("b", b"y")]) as path: self.assertEqual(inspect_archive(path, max_entries=1)["exit_code"], 2) report = inspect_archive(path) self.assertEqual(report["exit_code"], 1) self.assertIn("no_level_dat", [item["code"] for item in report["issues"]]) def test_corrupt_payload_fails_crc(self): payload = b"unique synthetic payload" with fixture([("world/level.dat", payload)]) as path: original = path.read_bytes() path.write_bytes(original.replace(payload, b"!" + payload[1:], 1)) report = inspect_archive(path, verify=True) self.assertEqual(report["crc"], "failed") self.assertEqual(report["exit_code"], 2) def test_not_a_zip(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "not.zip" path.write_bytes(b"not a zip") self.assertEqual(inspect_archive(path)["exit_code"], 2) def test_soft_timeout_is_reported_as_incomplete(self): with fixture([("world/level.dat", b"synthetic")]) as path: with patch("mc_backup_preflight.time.monotonic", side_effect=[0.0, 46.0]): report = inspect_archive(path, verify=True, max_seconds=45.0) self.assertEqual(report["crc"], "incomplete_time_limit") self.assertEqual(report["crc_bytes_read"], 0) self.assertEqual(report["exit_code"], 1) if __name__ == "__main__": unittest.main()