from __future__ import annotations import json import subprocess import sys import zipfile from pathlib import Path import pytest from tools import package_6400_canonical as canonical def test_deterministic_sorted_posix_zip(tmp_path: Path) -> None: files = { "z/中文 long name.txt": b"z\n", "a.txt": b"a\n", "PACKAGE-MANIFEST.json": b"{}\n", "SHA256SUMS": b"", "PACKAGE-SELF-CHECK.py": b"print('PASS')\n", "START-HERE.zh-CN.md": b"start\n", "EXCLUSIONS.json": b"{}\n", } a = canonical.write_zip(tmp_path / "a.zip", files) b = canonical.write_zip(tmp_path / "b.zip", files) assert canonical.sha_file(a) == canonical.sha_file(b) with zipfile.ZipFile(a) as archive: assert archive.namelist() == sorted(archive.namelist()) assert not any("\\" in name for name in archive.namelist()) @pytest.mark.parametrize("name", ["../escape", "/absolute", "bad\\member"]) def test_unsafe_member_fails(name: str) -> None: with pytest.raises(ValueError, match="unsafe ZIP member"): canonical.safe_name(name) def test_base_id_is_path_sha_role_not_time() -> None: files = {"src/common/a.v": b"a", "tools/x.py": b"x"} assert canonical.fingerprint(files) == canonical.fingerprint(dict(reversed(list(files.items())))) changed = dict(files) changed["tools/x.py"] = b"y" assert canonical.fingerprint(changed) != canonical.fingerprint(files) def test_same_base_id_different_bytes_is_collision(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: # Global immutable reuse is exercised by package_base integration; this # focused guard proves byte disagreement cannot be silently selected. a = tmp_path / "a.zip" b = tmp_path / "b.zip" a.write_bytes(b"a") b.write_bytes(b"b") assert canonical.sha_file(a) != canonical.sha_file(b) def test_common_profile_files_and_tamper_failure(tmp_path: Path) -> None: payload = canonical.common_payload( "base", "1" * 40, "2" * 40, {"src/common/a.v": b"module a; endmodule\n"}, {"base_id": "abc"} ) archive = canonical.write_zip(tmp_path / "base.zip", payload) canonical.tamper_must_fail(archive) with zipfile.ZipFile(archive) as z: assert set(canonical.COMMON).issubset(z.namelist()) manifest = json.loads(z.read("PACKAGE-MANIFEST.json")) assert manifest["profile"] == "base" def test_current_selfcheck_explicit_base_required(tmp_path: Path) -> None: payload = canonical.common_payload( "current", "1" * 40, "2" * 40, {"BASE-REQUIRED.json": b"{}\n"}, {"base_id": "abc"} ) archive = canonical.write_zip(tmp_path / "current.zip", payload) root = tmp_path / "unpacked" root.mkdir() canonical.extract_safe(archive, root) result = subprocess.run( [sys.executable, str(root / "PACKAGE-SELF-CHECK.py")], cwd=root, text=True, capture_output=True, ) assert result.returncode == 3 assert "BASE_REQUIRED" in result.stdout def test_active_sets_exclude_old_profiles_and_scratch() -> None: selected = canonical.base_paths() + canonical.current_paths() assert not any("6400-control" in path or "6400-identification" in path or "6400-sim-debug" in path for path in selected) assert "ACTIVE_FIX_LOCK.yaml" not in selected assert "QUICK_PROGRESS.md" not in selected assert "tools/target_registry.py" in canonical.base_paths() assert "tools/resource_scheduler.py" in canonical.base_paths() assert "tools/overlay_6400_packages.py" in canonical.base_paths() assert "docs/generated/6400-channel-map.md" in canonical.current_paths() def test_fake_marker_cannot_satisfy_current(tmp_path: Path) -> None: payload = canonical.common_payload( "current", "1" * 40, "2" * 40, {"BASE-REQUIRED.json": b'{"base_id":"abc","base_manifest_sha256":"bad"}\n'}, {"base_id": "abc"}, ) archive = canonical.write_zip(tmp_path / "current.zip", payload) root = tmp_path / "fake" root.mkdir() canonical.extract_safe(archive, root) (root / ".base-overlay-present").write_text("fake\n") result = subprocess.run([sys.executable, str(root / "PACKAGE-SELF-CHECK.py")], cwd=root, text=True, capture_output=True) assert result.returncode == 3 assert "BASE_REQUIRED" in result.stdout def test_windows_remote_transfer_is_chunked_hashed_and_strict() -> None: invoke = (canonical.ROOT / "tools/windows/invoke-remote.ps1").read_text() upload = (canonical.ROOT / "tools/windows/sync-to-remote.ps1").read_text() download = (canonical.ROOT / "tools/windows/sync-from-remote.ps1").read_text() for script in (invoke, upload, download): assert "StrictHostKeyChecking=yes" in script assert "UserKnownHostsFile=" in script assert "UserKnownHostsFile=/dev/null" not in script assert "StrictHostKeyChecking=no" not in script assert "ChunkBytes = 1048576" in upload assert "sha256sum" in upload assert ".partial" in upload assert "parts-$wholeSha" in upload assert "REMOTE_NETWORK_UNSTABLE" in upload assert "ExpectedSha256" in download