| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- from __future__ import annotations
- import json
- import sys
- import zipfile
- from pathlib import Path
- TOOLS = Path(__file__).resolve().parents[3] / "tools"
- sys.path.insert(0, str(TOOLS))
- from package_hth_review import ( # noqa: E402
- review_readme,
- should_skip,
- snapshot_evidence,
- write_zip_file,
- zip_file_mode,
- )
- from verify_review_package import verify_6400_contract # noqa: E402
- from zip_unix_modes import extract_zip_with_unix_modes # noqa: E402
- HOST = Path(__file__).resolve().parents[3]
- def test_zip_file_mode_marks_scripts_executable(tmp_path):
- sh = tmp_path / "run.sh"
- sh.write_text("#!/bin/sh\n", encoding="utf-8")
- py = tmp_path / "tool.py"
- py.write_bytes(b"#!/usr/bin/env python3\nprint(1)\n")
- txt = tmp_path / "note.txt"
- txt.write_text("x\n", encoding="utf-8")
- assert zip_file_mode(sh) == 0o755
- assert zip_file_mode(py) == 0o755
- assert zip_file_mode(txt) == 0o644
- def test_extract_restores_unix_modes(tmp_path):
- zpath = tmp_path / "m.zip"
- with zipfile.ZipFile(zpath, "w") as zf:
- write_zip_file(zf, "bin/run.sh", b"#!/bin/sh\necho ok\n", 0o755)
- write_zip_file(zf, "readme.txt", b"hello\n", 0o644)
- dest = tmp_path / "out"
- extract_zip_with_unix_modes(zpath, dest)
- sh = dest / "bin" / "run.sh"
- txt = dest / "readme.txt"
- assert sh.is_file()
- assert txt.is_file()
- if sys.platform != "win32":
- assert sh.stat().st_mode & 0o777 == 0o755
- assert txt.stat().st_mode & 0o777 == 0o644
- def test_6400_review_readme_describes_independent_ack_contract():
- text = review_readme(stamp="2026-01-01", head="a" * 40, short="a" * 7, run_id="test")
- for frame_type in ("0x81", "0x82", "0x42", "0x83"):
- assert frame_type in text
- assert "no ACK" not in text
- assert "无 ACK" not in text
- def test_6400_snapshot_evidence_excludes_legacy_profile_and_contract():
- names = set(snapshot_evidence(HOST))
- assert names == {
- "evidence/6400/host-contract.json",
- "evidence/6400/SHA256SUMS",
- "evidence/6400/import-source.json",
- "evidence/6400/profiles/6400-control.json",
- "evidence/6400/profiles/6400-identification.json",
- "evidence/hth.proto",
- "evidence/SHA256SUMS",
- }
- assert not any("prod2u-door-temp" in name or "legacy" in name.lower() for name in names)
- def test_6400_package_filter_keeps_only_canonical_release_profiles():
- assert should_skip(HOST / "RELEASE.zh-CN.md", HOST)
- assert should_skip(HOST / "REQUIREMENTS_TRACEABILITY.md", HOST)
- assert should_skip(HOST / "contracts" / "evidence" / "host-contract.json", HOST)
- assert should_skip(HOST / "profiles" / "prod2u-door-temp" / "profile.json", HOST)
- assert should_skip(HOST / "profiles" / "6400-sim-debug" / "profile.json", HOST)
- assert not should_skip(HOST / "profiles" / "6400-control" / "profile.json", HOST)
- assert not should_skip(HOST / "profiles" / "6400-identification" / "profile.json", HOST)
- def test_6400_package_contract_verifier_accepts_bound_evidence(tmp_path):
- root = tmp_path / "hth-platform-r6"
- host = root / "host"
- def copy(source: Path, target: Path) -> None:
- target.parent.mkdir(parents=True, exist_ok=True)
- target.write_bytes(source.read_bytes())
- for name, data in snapshot_evidence(HOST).items():
- target = root / name
- target.parent.mkdir(parents=True, exist_ok=True)
- target.write_bytes(data)
- copy(HOST / "contracts" / "6400" / "host-contract.json", host / "contracts" / "6400" / "host-contract.json")
- copy(HOST / "profiles" / "6400-control" / "profile.json", host / "profiles" / "6400-control" / "profile.json")
- copy(
- HOST / "profiles" / "6400-identification" / "profile.json",
- host / "profiles" / "6400-identification" / "profile.json",
- )
- contract = json.loads((HOST / "contracts" / "6400" / "host-contract.json").read_text(encoding="utf-8"))
- (root / "PACKAGE_IDENTITY.json").write_text(
- json.dumps(
- {
- "package": "hth-platform-6400-engineering-review-v1",
- "product": "6400-front-rear-v1",
- "protocol_contract_sha256": contract["contract_sha256"],
- "profile_ids": ["6400-control", "6400-identification"],
- }
- ),
- encoding="utf-8",
- )
- assert verify_6400_contract(root, host) == []
|