| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 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-SELF-CHECK.py": b"print('PASS')\n",
- "START-HERE.zh-CN.md": b"start\n",
- "EXCLUSIONS.json": b"{}\n",
- }
- files, _ = canonical.package_archive.finalize(files, {"profile": "fixture"})
- 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 "generated/6400/channel-map.json" in canonical.current_paths()
- mapping=json.loads((canonical.ROOT/"generated/6400/channel-map.json").read_text(encoding="utf-8"))
- assert mapping["contract_sha256"]==(canonical.ROOT/"generated/6400/contract-sha256.txt").read_text().strip()
- assert len(mapping["channels"])==24
- assert "src/top/prod2u_mrdimm_rs232_only_top.v" in selected
- assert "products/6400-front-rear/analysis-replay-closure.json" not in selected
- 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_adapters_share_verified_python_implementation() -> None:
- upload = (canonical.ROOT / "tools/windows/sync-to-remote.ps1").read_text(encoding="utf-8-sig")
- download = (canonical.ROOT / "tools/windows/sync-from-remote.ps1").read_text(encoding="utf-8-sig")
- for script in (upload, download):
- assert "remote_transfer.py" in script and "--remote-worker" in script
- assert "268435456" in script and "--retries" in script
- assert "scp " not in script
- assert "ExpectedSha256" in download and "REMOTE_PARTIAL_SUFFIX_REQUIRED" in upload
|