test_6400_canonical_packages.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from __future__ import annotations
  2. import json
  3. import subprocess
  4. import sys
  5. import zipfile
  6. from pathlib import Path
  7. import pytest
  8. from tools import package_6400_canonical as canonical
  9. def test_deterministic_sorted_posix_zip(tmp_path: Path) -> None:
  10. files = {
  11. "z/中文 long name.txt": b"z\n",
  12. "a.txt": b"a\n",
  13. "PACKAGE-MANIFEST.json": b"{}\n",
  14. "SHA256SUMS": b"",
  15. "PACKAGE-SELF-CHECK.py": b"print('PASS')\n",
  16. "START-HERE.zh-CN.md": b"start\n",
  17. "EXCLUSIONS.json": b"{}\n",
  18. }
  19. a = canonical.write_zip(tmp_path / "a.zip", files)
  20. b = canonical.write_zip(tmp_path / "b.zip", files)
  21. assert canonical.sha_file(a) == canonical.sha_file(b)
  22. with zipfile.ZipFile(a) as archive:
  23. assert archive.namelist() == sorted(archive.namelist())
  24. assert not any("\\" in name for name in archive.namelist())
  25. @pytest.mark.parametrize("name", ["../escape", "/absolute", "bad\\member"])
  26. def test_unsafe_member_fails(name: str) -> None:
  27. with pytest.raises(ValueError, match="unsafe ZIP member"):
  28. canonical.safe_name(name)
  29. def test_base_id_is_path_sha_role_not_time() -> None:
  30. files = {"src/common/a.v": b"a", "tools/x.py": b"x"}
  31. assert canonical.fingerprint(files) == canonical.fingerprint(dict(reversed(list(files.items()))))
  32. changed = dict(files)
  33. changed["tools/x.py"] = b"y"
  34. assert canonical.fingerprint(changed) != canonical.fingerprint(files)
  35. def test_common_profile_files_and_tamper_failure(tmp_path: Path) -> None:
  36. payload = canonical.common_payload(
  37. "base", "1" * 40, "2" * 40, {"src/common/a.v": b"module a; endmodule\n"}, {"base_id": "abc"}
  38. )
  39. archive = canonical.write_zip(tmp_path / "base.zip", payload)
  40. canonical.tamper_must_fail(archive)
  41. with zipfile.ZipFile(archive) as z:
  42. assert set(canonical.COMMON).issubset(z.namelist())
  43. manifest = json.loads(z.read("PACKAGE-MANIFEST.json"))
  44. assert manifest["profile"] == "base"
  45. def test_current_selfcheck_explicit_base_required(tmp_path: Path) -> None:
  46. payload = canonical.common_payload(
  47. "current", "1" * 40, "2" * 40, {"BASE-REQUIRED.json": b"{}\n"}, {"base_id": "abc"}
  48. )
  49. archive = canonical.write_zip(tmp_path / "current.zip", payload)
  50. root = tmp_path / "unpacked"
  51. root.mkdir()
  52. canonical.extract_safe(archive, root)
  53. result = subprocess.run(
  54. [sys.executable, str(root / "PACKAGE-SELF-CHECK.py")], cwd=root,
  55. text=True, capture_output=True,
  56. )
  57. assert result.returncode == 3
  58. assert "BASE_REQUIRED" in result.stdout
  59. def test_active_sets_exclude_old_profiles_and_scratch() -> None:
  60. selected = canonical.base_paths() + canonical.current_paths()
  61. assert not any("6400-control" in path or "6400-identification" in path or "6400-sim-debug" in path for path in selected)
  62. assert "ACTIVE_FIX_LOCK.yaml" not in selected
  63. assert "QUICK_PROGRESS.md" not in selected
  64. assert "tools/target_registry.py" in canonical.base_paths()
  65. assert "docs/generated/6400-channel-map.md" in canonical.current_paths()