| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- 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_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 "docs/generated/6400-channel-map.md" in canonical.current_paths()
|