test_6400_v2_package_replay.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/env python3
  2. """Validation and replay verification for 6400 V2.0 DEFLATE 9 packages."""
  3. from __future__ import annotations
  4. import hashlib
  5. import json
  6. import subprocess
  7. import sys
  8. import tempfile
  9. import zipfile
  10. from pathlib import Path
  11. ROOT = Path(__file__).resolve().parents[2]
  12. def sha256_file(path: Path) -> str:
  13. digest = hashlib.sha256()
  14. with path.open("rb") as stream:
  15. for chunk in iter(lambda: stream.read(1024 * 1024), b""):
  16. digest.update(chunk)
  17. return digest.hexdigest()
  18. def test_firmware_package_deflate9_and_contents() -> None:
  19. firmware_zips = sorted(ROOT.glob("dist/prod2u-6400-v2-firmware-*.zip"))
  20. assert firmware_zips, "No 6400-v2 firmware package found in dist/"
  21. fw_zip = firmware_zips[-1]
  22. with zipfile.ZipFile(fw_zip, "r") as zf:
  23. infolist = zf.infolist()
  24. assert len(infolist) > 0
  25. for info in infolist:
  26. if info.file_size > 0:
  27. assert info.compress_type == zipfile.ZIP_DEFLATED, f"{info.filename} not ZIP_DEFLATED"
  28. names = set(zf.namelist())
  29. assert "manifest.json" in names
  30. assert "SHA256SUMS" in names
  31. assert "prod2u_6400_v2.bit" in names
  32. assert "TD-BUILD-RECEIPT.json" in names
  33. assert "FLASH-INSTRUCTIONS.zh-CN.txt" in names
  34. manifest = json.loads(zf.read("manifest.json").decode("utf-8"))
  35. receipt = json.loads(zf.read("TD-BUILD-RECEIPT.json").decode("utf-8"))
  36. assert manifest["profile"] == "6400-v2"
  37. assert manifest["deployment_status"] == "TD_QUALIFIED_HARDWARE_NOT_RUN"
  38. assert manifest["hardware_validation"] == "NOT RUN"
  39. assert manifest["bitstream_sha256"] == receipt["bitstream_sha256"]
  40. # Check checksums in SHA256SUMS
  41. sums_text = zf.read("SHA256SUMS").decode("utf-8")
  42. for line in sums_text.splitlines():
  43. if not line.strip():
  44. continue
  45. expected_hash, fname = line.split(" ", 1)
  46. actual_hash = hashlib.sha256(zf.read(fname)).hexdigest()
  47. assert actual_hash == expected_hash, f"Hash mismatch for {fname} in {fw_zip.name}"
  48. def test_source_and_review_packages_deflate9() -> None:
  49. for kind in ("source", "review"):
  50. zips = sorted(ROOT.glob(f"dist/prod2u-6400-v2-{kind}-*.zip"))
  51. assert zips, f"No 6400-v2 {kind} package found in dist/"
  52. pkg = zips[-1]
  53. with zipfile.ZipFile(pkg, "r") as zf:
  54. infolist = zf.infolist()
  55. assert len(infolist) > 0
  56. for info in infolist:
  57. if info.file_size > 0:
  58. assert info.compress_type == zipfile.ZIP_DEFLATED, f"{info.filename} not ZIP_DEFLATED"
  59. manifest = json.loads(zf.read("manifest.json").decode("utf-8"))
  60. assert manifest["profile"] == "6400-v2"
  61. assert manifest["kind"] == kind
  62. sums_text = zf.read("SHA256SUMS").decode("utf-8")
  63. for line in sums_text.splitlines():
  64. if not line.strip():
  65. continue
  66. expected_hash, fname = line.split(" ", 1)
  67. actual_hash = hashlib.sha256(zf.read(fname)).hexdigest()
  68. assert actual_hash == expected_hash, f"Hash mismatch for {fname} in {pkg.name}"
  69. def test_delivery_package_deflate9_and_self_check() -> None:
  70. td_receipt_path = Path("/workspace/td-runs/6400-v2-production/td-receipt.json")
  71. if td_receipt_path.is_file():
  72. receipt = json.loads(td_receipt_path.read_text(encoding="utf-8"))
  73. commit_8 = receipt["commit"][:8]
  74. else:
  75. commit_8 = subprocess.check_output(["git", "rev-parse", "--short=8", "HEAD"], cwd=ROOT, text=True).strip()
  76. delivery_zip = ROOT / f"dist/Prod2U-6400-V2.0-Delivery-{commit_8}.zip"
  77. assert delivery_zip.is_file(), f"Delivery package not found: {delivery_zip}"
  78. with zipfile.ZipFile(delivery_zip, "r") as zf:
  79. infolist = zf.infolist()
  80. for info in infolist:
  81. if info.file_size > 0:
  82. assert info.compress_type == zipfile.ZIP_DEFLATED, f"{info.filename} not ZIP_DEFLATED"
  83. manifest = json.loads(zf.read("PACKAGE-MANIFEST.json").decode("utf-8"))
  84. assert manifest["product"] == "6400-v2"
  85. assert manifest["hardware"] == "NOT_RUN"
  86. identity = json.loads(zf.read("RELEASE-IDENTITY.json").decode("utf-8"))
  87. assert identity["product"] == "6400-v2"
  88. assert identity["board"] == "FCB V2.0"
  89. assert identity["hardware"] == "NOT_RUN"
  90. sums_text = zf.read("SHA256SUMS").decode("utf-8")
  91. for line in sums_text.splitlines():
  92. if not line.strip():
  93. continue
  94. expected_hash, fname = line.split(" ", 1)
  95. actual_hash = hashlib.sha256(zf.read(fname)).hexdigest()
  96. assert actual_hash == expected_hash, f"Hash mismatch for {fname} in {delivery_zip.name}"
  97. res = subprocess.run(
  98. [sys.executable, str(ROOT / "tools/package_prod2u.py"), "verify", str(delivery_zip)],
  99. capture_output=True,
  100. text=True,
  101. )
  102. assert res.returncode == 0, f"Delivery package verify failed: {res.stderr}\n{res.stdout}"
  103. def test_isolated_source_replay() -> None:
  104. """Extract source package in clean temporary dir and verify RTL compile & simulation."""
  105. td_receipt_path = Path("/workspace/td-runs/6400-v2-production/td-receipt.json")
  106. if td_receipt_path.is_file():
  107. receipt = json.loads(td_receipt_path.read_text(encoding="utf-8"))
  108. commit_12 = receipt["commit"][:12]
  109. else:
  110. commit_12 = subprocess.check_output(["git", "rev-parse", "--short=12", "HEAD"], cwd=ROOT, text=True).strip()
  111. source_zips = sorted(ROOT.glob(f"dist/prod2u-6400-v2-source-{commit_12}*.zip"))
  112. assert source_zips, f"No source zip found for commit {commit_12}"
  113. source_zip = source_zips[-1]
  114. with tempfile.TemporaryDirectory() as tmpdir:
  115. tmp = Path(tmpdir)
  116. with zipfile.ZipFile(source_zip, "r") as zf:
  117. zf.extractall(tmp)
  118. # Check all 24 source closure files are present
  119. closure = json.loads((tmp / "source-closure-expanded.json").read_text(encoding="utf-8"))
  120. assert len(closure["sources"]) == 24
  121. for src in closure["sources"]:
  122. assert (tmp / src).is_file(), f"Missing source file in unpacked package: {src}"
  123. assert (tmp / "project.json").is_file()
  124. assert (tmp / "generated/6400/prod2u_6400_v1_params.vh").is_file()
  125. # Copy testbench to temporary directory and run iverilog compilation
  126. tb_src = ROOT / "tests/sim/prod2u_6400_v2_tb.v"
  127. sh_out = tmp / "top_test.vvp"
  128. src_files = [str(tmp / s) for s in closure["sources"]]
  129. res = subprocess.run(
  130. ["iverilog", "-g2012", f"-I{tmp / 'generated/6400'}", "-o", str(sh_out), str(tb_src), *src_files],
  131. capture_output=True,
  132. text=True,
  133. )
  134. assert res.returncode == 0, f"iverilog compilation failed in isolated replay:\n{res.stderr}"
  135. # Execute simulation in the isolated environment
  136. sim_res = subprocess.run(["vvp", str(sh_out)], capture_output=True, text=True)
  137. assert sim_res.returncode == 0, f"vvp simulation failed:\n{sim_res.stderr}\n{sim_res.stdout}"
  138. assert "prod2u_6400_v2_tb PASS" in sim_res.stdout