test_zip_unix_modes.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from __future__ import annotations
  2. import json
  3. import sys
  4. import zipfile
  5. from pathlib import Path
  6. TOOLS = Path(__file__).resolve().parents[3] / "tools"
  7. sys.path.insert(0, str(TOOLS))
  8. from package_hth_review import ( # noqa: E402
  9. review_readme,
  10. should_skip,
  11. snapshot_evidence,
  12. write_zip_file,
  13. zip_file_mode,
  14. )
  15. from verify_review_package import verify_6400_contract # noqa: E402
  16. from zip_unix_modes import extract_zip_with_unix_modes # noqa: E402
  17. HOST = Path(__file__).resolve().parents[3]
  18. def test_zip_file_mode_marks_scripts_executable(tmp_path):
  19. sh = tmp_path / "run.sh"
  20. sh.write_text("#!/bin/sh\n", encoding="utf-8")
  21. py = tmp_path / "tool.py"
  22. py.write_bytes(b"#!/usr/bin/env python3\nprint(1)\n")
  23. txt = tmp_path / "note.txt"
  24. txt.write_text("x\n", encoding="utf-8")
  25. assert zip_file_mode(sh) == 0o755
  26. assert zip_file_mode(py) == 0o755
  27. assert zip_file_mode(txt) == 0o644
  28. def test_extract_restores_unix_modes(tmp_path):
  29. zpath = tmp_path / "m.zip"
  30. with zipfile.ZipFile(zpath, "w") as zf:
  31. write_zip_file(zf, "bin/run.sh", b"#!/bin/sh\necho ok\n", 0o755)
  32. write_zip_file(zf, "readme.txt", b"hello\n", 0o644)
  33. dest = tmp_path / "out"
  34. extract_zip_with_unix_modes(zpath, dest)
  35. sh = dest / "bin" / "run.sh"
  36. txt = dest / "readme.txt"
  37. assert sh.is_file()
  38. assert txt.is_file()
  39. if sys.platform != "win32":
  40. assert sh.stat().st_mode & 0o777 == 0o755
  41. assert txt.stat().st_mode & 0o777 == 0o644
  42. def test_6400_review_readme_describes_independent_ack_contract():
  43. text = review_readme(stamp="2026-01-01", head="a" * 40, short="a" * 7, run_id="test")
  44. for frame_type in ("0x81", "0x82", "0x42", "0x83"):
  45. assert frame_type in text
  46. assert "no ACK" not in text
  47. assert "无 ACK" not in text
  48. def test_6400_snapshot_evidence_excludes_legacy_profile_and_contract():
  49. names = set(snapshot_evidence(HOST))
  50. assert names == {
  51. "evidence/6400/host-contract.json",
  52. "evidence/6400/SHA256SUMS",
  53. "evidence/6400/import-source.json",
  54. "evidence/6400/profiles/6400-control.json",
  55. "evidence/6400/profiles/6400-identification.json",
  56. "evidence/hth.proto",
  57. "evidence/SHA256SUMS",
  58. }
  59. assert not any("prod2u-door-temp" in name or "legacy" in name.lower() for name in names)
  60. def test_6400_package_filter_keeps_only_canonical_release_profiles():
  61. assert should_skip(HOST / "RELEASE.zh-CN.md", HOST)
  62. assert should_skip(HOST / "REQUIREMENTS_TRACEABILITY.md", HOST)
  63. assert should_skip(HOST / "contracts" / "evidence" / "host-contract.json", HOST)
  64. assert should_skip(HOST / "profiles" / "prod2u-door-temp" / "profile.json", HOST)
  65. assert should_skip(HOST / "profiles" / "6400-sim-debug" / "profile.json", HOST)
  66. assert not should_skip(HOST / "profiles" / "6400-control" / "profile.json", HOST)
  67. assert not should_skip(HOST / "profiles" / "6400-identification" / "profile.json", HOST)
  68. def test_6400_package_contract_verifier_accepts_bound_evidence(tmp_path):
  69. root = tmp_path / "hth-platform-r6"
  70. host = root / "host"
  71. def copy(source: Path, target: Path) -> None:
  72. target.parent.mkdir(parents=True, exist_ok=True)
  73. target.write_bytes(source.read_bytes())
  74. for name, data in snapshot_evidence(HOST).items():
  75. target = root / name
  76. target.parent.mkdir(parents=True, exist_ok=True)
  77. target.write_bytes(data)
  78. copy(HOST / "contracts" / "6400" / "host-contract.json", host / "contracts" / "6400" / "host-contract.json")
  79. copy(HOST / "profiles" / "6400-control" / "profile.json", host / "profiles" / "6400-control" / "profile.json")
  80. copy(
  81. HOST / "profiles" / "6400-identification" / "profile.json",
  82. host / "profiles" / "6400-identification" / "profile.json",
  83. )
  84. contract = json.loads((HOST / "contracts" / "6400" / "host-contract.json").read_text(encoding="utf-8"))
  85. (root / "PACKAGE_IDENTITY.json").write_text(
  86. json.dumps(
  87. {
  88. "package": "hth-platform-6400-engineering-review-v1",
  89. "product": "6400-front-rear-v1",
  90. "protocol_contract_sha256": contract["contract_sha256"],
  91. "profile_ids": ["6400-control", "6400-identification"],
  92. }
  93. ),
  94. encoding="utf-8",
  95. )
  96. assert verify_6400_contract(root, host) == []