test_remote_scripts.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. #!/usr/bin/env python3
  2. """Remote collector and package tests for the converged CPLD project."""
  3. from __future__ import annotations
  4. import json
  5. import shutil
  6. import subprocess
  7. import tempfile
  8. import unittest
  9. from pathlib import Path
  10. ROOT = Path(__file__).resolve().parents[2]
  11. class RemoteScriptTests(unittest.TestCase):
  12. def test_remote_tools_only_expose_latest_real_fan_ptc_collector(self) -> None:
  13. scripts = sorted(path.name for path in (ROOT / "tools" / "remote").glob("*.ps1"))
  14. self.assertEqual(scripts, ["collect-j6-host-sensor-real-fan-ptc.ps1"])
  15. self.assertFalse((ROOT / "tools" / "serial_debug_capture.py").exists())
  16. self.assertFalse((ROOT / "tools" / "windows_serial_validation.py").exists())
  17. def test_collect_j6_host_sensor_real_fan_ptc_is_static_allowlisted_host_writer_and_debug_reader(self) -> None:
  18. script = (ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1").read_text(
  19. encoding="utf-8"
  20. )
  21. for marker in [
  22. "System.IO.Ports.SerialPort",
  23. "[ValidatePattern('^COM[0-9]+$')]",
  24. "HostPort",
  25. "DebugPort",
  26. 'if (-not $SelfTest -and $HostPort -eq $DebugPort)',
  27. 'if (-not $SelfTest -and $HostPort -ne "COM8")',
  28. 'if (-not $SelfTest -and $DebugPort -ne "COM7")',
  29. "BaudRate = 115200",
  30. "DebugBaud = 921600",
  31. "DtrEnable = $false",
  32. "RtsEnable = $false",
  33. "host-port-raw.bin",
  34. "host-port-escaped.txt",
  35. "debug-uart-raw.bin",
  36. "debug-uart-escaped.txt",
  37. "bmc-raw-replay.bin",
  38. "host-sensor-report.json",
  39. "debug-frame-stream.json",
  40. "host-command-transcript.json",
  41. "j6-host-sensor-real-fan-ptc-capture-manifest.json",
  42. "real-bmc-j6-host-sensor-real-fan-ptc-debug921-v1-receipt-draft.json",
  43. "real-bmc-j6-host-sensor-real-fan-ptc-debug921-v1",
  44. "J6CMD",
  45. "J6RAW",
  46. "J6EVT",
  47. "J6PWM",
  48. "J6MOD",
  49. "J6STAT",
  50. "J6MAP",
  51. "J6TEMP",
  52. "SENS",
  53. "DIMM",
  54. "PTCCH",
  55. "FANCH",
  56. "SET PTC 01 10",
  57. "SET PTC 24 20",
  58. "SET FAN 01 40",
  59. "SET FAN 04 30",
  60. "SET PID ENABLE",
  61. "GET STATUS",
  62. "GET MAP",
  63. "GET SENSOR",
  64. "SET PID DISABLE",
  65. "DISABLE",
  66. "host_port_writes_to_serial",
  67. "debug_port_writes_to_serial",
  68. "driver_install_attempted = $false",
  69. "enumerates_com_ports = $false",
  70. "opens_unknown_com_ports = $false",
  71. "AssumeManualSramProgrammed",
  72. "PROGRAM_SRAM",
  73. "SelfTest",
  74. "startup-preflight.json",
  75. "READY",
  76. "J6READY",
  77. "preflight",
  78. "blocked_waiting_for_ready_epoch",
  79. ]:
  80. self.assertIn(marker, script)
  81. self.assertIn("$hostSerial.Write", script)
  82. self.assertNotIn("$debugSerial.Write", script)
  83. for forbidden in [
  84. "SET PTC 00",
  85. "SET FAN 00",
  86. "SET FAN 05",
  87. "SET FAN 06",
  88. "GetPortNames",
  89. "Win32_SerialPort",
  90. "Get-CimInstance",
  91. "Get-WmiObject",
  92. "pip install",
  93. "pyserial",
  94. "python",
  95. "pnputil",
  96. "devcon",
  97. "install-driver",
  98. "add-windowsdriver",
  99. "driverquery",
  100. "PROGRAM_FLASH",
  101. "ERASE_FLASH",
  102. "PROGRAM_SPIBIN",
  103. "DIRECT_FLASH_WRITE",
  104. "blocked_edit_required",
  105. ]:
  106. self.assertNotIn(forbidden.lower(), script.lower())
  107. def test_collect_j6_host_sensor_real_fan_ptc_has_startup_preflight_selftest(self) -> None:
  108. powershell = shutil.which("powershell")
  109. if powershell is None:
  110. self.skipTest("PowerShell is not available")
  111. script_path = ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1"
  112. result = subprocess.run(
  113. [
  114. powershell,
  115. "-NoProfile",
  116. "-ExecutionPolicy",
  117. "Bypass",
  118. "-File",
  119. str(script_path),
  120. "-SelfTest",
  121. ],
  122. cwd=ROOT,
  123. text=True,
  124. capture_output=True,
  125. check=False,
  126. )
  127. output = result.stderr + result.stdout
  128. self.assertEqual(result.returncode, 0, output)
  129. for marker in [
  130. "selftest=pass",
  131. "ready_line=READY profile=temp-uart-ptc-remap seq=00000000",
  132. "j6ready_line=J6READY profile=temp-uart-ptc-remap",
  133. "epoch_locked=pass",
  134. "pre_sram_noise=OK seq=0000000D cmd=STATUS",
  135. "post_j6ready_raw_replay=pass",
  136. "formal_ack=OK seq=00000001 cmd=PTCCH",
  137. "stale_ack_isolated=pass",
  138. ]:
  139. self.assertIn(marker, output)
  140. def test_collect_j6_host_sensor_real_fan_ptc_accepts_native_bool_argument_shape(self) -> None:
  141. powershell = shutil.which("powershell")
  142. if powershell is None:
  143. self.skipTest("PowerShell is not available")
  144. script_path = ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1"
  145. result = subprocess.run(
  146. [
  147. powershell,
  148. "-NoProfile",
  149. "-ExecutionPolicy",
  150. "Bypass",
  151. "-File",
  152. str(script_path),
  153. "-SelfTest",
  154. "-AssumeManualSramProgrammed",
  155. "$true",
  156. ],
  157. cwd=ROOT,
  158. text=True,
  159. capture_output=True,
  160. check=False,
  161. )
  162. output = result.stderr + result.stdout
  163. self.assertEqual(result.returncode, 0, output)
  164. self.assertIn("selftest=pass", output)
  165. def test_collect_j6_host_sensor_real_fan_ptc_handles_discarded_ack_arrays(self) -> None:
  166. powershell = shutil.which("powershell")
  167. if powershell is None:
  168. self.skipTest("PowerShell is not available")
  169. script_path = ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1"
  170. script = script_path.read_text(encoding="utf-8-sig")
  171. helper_start = script.index("function Read-HostPortAvailableLines")
  172. helper_end = script.index("function Invoke-StartupPreflight")
  173. helpers = script[helper_start:helper_end]
  174. with tempfile.TemporaryDirectory() as tmp:
  175. probe_path = Path(tmp) / "probe.ps1"
  176. probe_path.write_text(
  177. helpers
  178. + """
  179. $hostRaw = New-Object System.Collections.Generic.List[byte]
  180. $hostWindow = New-Object System.Text.StringBuilder
  181. $hostReports = New-Object System.Collections.Generic.List[object]
  182. $queue = New-Object System.Collections.Generic.Queue[object]
  183. $queue.Enqueue([ordered]@{
  184. ack_line = "OK seq=00000001 cmd=STATUS"
  185. raw = "OK seq=00000001 cmd=STATUS"
  186. token = "OK seq=00000001 cmd=STATUS"
  187. class = "ok"
  188. seq = 1
  189. cmd = "STATUS"
  190. ack_offset = 1
  191. response_offset = 1
  192. delimiter = "CR"
  193. timestamp = (Get-Date).ToUniversalTime().ToString("o")
  194. }) | Out-Null
  195. $queue.Enqueue([ordered]@{
  196. ack_line = "OK seq=00000001 cmd=PTCCH"
  197. raw = "OK seq=00000001 cmd=PTCCH"
  198. token = "OK seq=00000001 cmd=PTCCH"
  199. class = "ok"
  200. seq = 1
  201. cmd = "PTCCH"
  202. ack_offset = 10
  203. response_offset = 10
  204. delimiter = "CR"
  205. timestamp = (Get-Date).ToUniversalTime().ToString("o")
  206. }) | Out-Null
  207. $serial = [pscustomobject]@{ BytesToRead = 0 }
  208. $ack = Read-HostAckLine $serial $hostRaw $hostWindow $hostReports $queue 5 200 1 "PTCCH"
  209. if ($ack.ack_line -ne "OK seq=00000001 cmd=PTCCH") { throw "candidate ack not returned: $($ack.ack_line)" }
  210. if ($ack.discarded_acks.Count -ne 1) { throw "discarded ack count was $($ack.discarded_acks.Count)" }
  211. if (-not $ack.discarded_acks[0].stale) { throw "discarded ack was not marked stale" }
  212. """,
  213. encoding="utf-8",
  214. )
  215. result = subprocess.run(
  216. [powershell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(probe_path)],
  217. cwd=ROOT,
  218. text=True,
  219. capture_output=True,
  220. check=False,
  221. )
  222. self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
  223. def test_collect_j6_host_sensor_real_fan_ptc_epoch_lock_discards_pre_sram_ack_noise(self) -> None:
  224. powershell = shutil.which("powershell")
  225. if powershell is None:
  226. self.skipTest("PowerShell is not available")
  227. script_path = ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1"
  228. script = script_path.read_text(encoding="utf-8-sig")
  229. helper_start = script.index("function Read-HostPortAvailableLines")
  230. helper_end = script.index("function Invoke-CollectorSelfTest")
  231. helpers = script[helper_start:helper_end]
  232. with tempfile.TemporaryDirectory() as tmp:
  233. probe_path = Path(tmp) / "probe.ps1"
  234. probe_path.write_text(
  235. helpers
  236. + """
  237. $preflight = New-StartupPreflightRecord
  238. $ackQueue = New-Object System.Collections.Generic.Queue[object]
  239. $ackQueue.Enqueue([ordered]@{
  240. ack_line = "OK seq=0000000D cmd=STATUS"
  241. raw = "OK seq=0000000D cmd=STATUS"
  242. token = "OK seq=0000000D cmd=STATUS"
  243. class = "ok"
  244. seq = 13
  245. cmd = "STATUS"
  246. ack_offset = 25
  247. response_offset = 25
  248. delimiter = "CR"
  249. timestamp = (Get-Date).ToUniversalTime().ToString("o")
  250. }) | Out-Null
  251. $preReadyDiscard = @(Clear-HostAckQueue $ackQueue)
  252. Add-PreSramNoise $preflight "ack" $preReadyDiscard[0]
  253. $hostReady = [ordered]@{ kind = "ready"; raw = "READY profile=temp-uart-ptc-remap seq=00000000"; fields = [ordered]@{} }
  254. $debugReady = [ordered]@{ kind = "j6ready"; raw = "J6READY profile=temp-uart-ptc-remap"; fields = [ordered]@{} }
  255. Update-PreflightEpochReady $preflight $hostReady 100 200
  256. Update-PreflightEpochReady $preflight $debugReady 300 400
  257. Complete-StartupPreflightRecord $preflight
  258. if ($preflight.status -ne "pass") { throw "preflight status was $($preflight.status)" }
  259. if (-not $preflight.epoch_locked) { throw "epoch was not locked" }
  260. if ($preflight.sync_ack_seen) { throw "preflight incorrectly accepted sync ack" }
  261. if ($preflight.pre_sram_noise_count -ne 1) { throw "pre_sram_noise_count was $($preflight.pre_sram_noise_count)" }
  262. if ($preflight.pre_sram_noise[0].ack_line -ne "OK seq=0000000D cmd=STATUS") { throw "wrong pre_sram noise ack" }
  263. if ($preflight.epoch_locked_at_host_offset -ne 100) { throw "wrong host epoch offset" }
  264. if ($preflight.epoch_locked_at_debug_offset -ne 400) { throw "wrong debug epoch offset" }
  265. $ackQueue.Enqueue([ordered]@{
  266. ack_line = "OK seq=00000001 cmd=PTCCH"
  267. raw = "OK seq=00000001 cmd=PTCCH"
  268. token = "OK seq=00000001 cmd=PTCCH"
  269. class = "ok"
  270. seq = 1
  271. cmd = "PTCCH"
  272. ack_offset = 150
  273. response_offset = 150
  274. delimiter = "CR"
  275. timestamp = (Get-Date).ToUniversalTime().ToString("o")
  276. }) | Out-Null
  277. $serial = [pscustomobject]@{ BytesToRead = 0 }
  278. $hostRaw = New-Object System.Collections.Generic.List[byte]
  279. $hostWindow = New-Object System.Text.StringBuilder
  280. $hostReports = New-Object System.Collections.Generic.List[object]
  281. $ack = Read-HostAckLine $serial $hostRaw $hostWindow $hostReports $ackQueue 120 200 1 "PTCCH"
  282. if ($ack.ack_line -ne "OK seq=00000001 cmd=PTCCH") { throw "formal ack not returned: $($ack.ack_line)" }
  283. if (-not $ack.seq_match -or -not $ack.cmd_match) { throw "formal ack mismatch" }
  284. """,
  285. encoding="utf-8",
  286. )
  287. result = subprocess.run(
  288. [powershell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(probe_path)],
  289. cwd=ROOT,
  290. text=True,
  291. capture_output=True,
  292. check=False,
  293. )
  294. self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
  295. def test_collect_j6_host_sensor_real_fan_ptc_marks_clear_queue_results_as_pre_sram_noise(self) -> None:
  296. powershell = shutil.which("powershell")
  297. if powershell is None:
  298. self.skipTest("PowerShell is not available")
  299. script_path = ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1"
  300. script = script_path.read_text(encoding="utf-8-sig")
  301. helper_start = script.index("function Clear-HostAckQueue")
  302. helper_end = script.index("function Update-PreflightEpochReady")
  303. helpers = script[helper_start:helper_end]
  304. with tempfile.TemporaryDirectory() as tmp:
  305. probe_path = Path(tmp) / "probe.ps1"
  306. probe_path.write_text(
  307. helpers
  308. + """
  309. $preflight = New-StartupPreflightRecord
  310. $ackQueue = New-Object System.Collections.Generic.Queue[object]
  311. $ackQueue.Enqueue([ordered]@{
  312. ack_line = "OK seq=0000000D cmd=STATUS"
  313. raw = "OK seq=0000000D cmd=STATUS"
  314. token = "OK seq=0000000D cmd=STATUS"
  315. class = "ok"
  316. seq = 13
  317. cmd = "STATUS"
  318. ack_offset = 25
  319. response_offset = 25
  320. delimiter = "CR"
  321. timestamp = (Get-Date).ToUniversalTime().ToString("o")
  322. }) | Out-Null
  323. $moreStale = @(Clear-HostAckQueue $ackQueue)
  324. foreach ($ack in $moreStale) {
  325. Set-AckMetadata $ack "pre_sram_noise" $true
  326. Add-PreSramNoise $preflight "ack" $ack
  327. }
  328. if ($moreStale.Count -ne 1) { throw "unexpected stale count $($moreStale.Count)" }
  329. if ($moreStale[0] -is [array]) { throw "stale ack was nested array" }
  330. if (-not $moreStale[0].pre_sram_noise) { throw "pre_sram_noise was not set" }
  331. if ($preflight.pre_sram_noise_count -ne 1) { throw "preflight noise count was $($preflight.pre_sram_noise_count)" }
  332. if ($preflight.pre_sram_noise[0].ack_line -ne "OK seq=0000000D cmd=STATUS") { throw "wrong pre_sram noise ack" }
  333. """,
  334. encoding="utf-8",
  335. )
  336. result = subprocess.run(
  337. [powershell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(probe_path)],
  338. cwd=ROOT,
  339. text=True,
  340. capture_output=True,
  341. check=False,
  342. )
  343. self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
  344. def test_collect_j6_host_sensor_real_fan_ptc_handles_timeout_discarded_ack_arrays(self) -> None:
  345. powershell = shutil.which("powershell")
  346. if powershell is None:
  347. self.skipTest("PowerShell is not available")
  348. script_path = ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1"
  349. script = script_path.read_text(encoding="utf-8-sig")
  350. helper_start = script.index("function Read-HostPortAvailableLines")
  351. helper_end = script.index("function Invoke-StartupPreflight")
  352. helpers = script[helper_start:helper_end]
  353. with tempfile.TemporaryDirectory() as tmp:
  354. probe_path = Path(tmp) / "probe.ps1"
  355. probe_path.write_text(
  356. helpers
  357. + """
  358. $hostRaw = New-Object System.Collections.Generic.List[byte]
  359. $hostWindow = New-Object System.Text.StringBuilder
  360. $hostReports = New-Object System.Collections.Generic.List[object]
  361. $queue = New-Object System.Collections.Generic.Queue[object]
  362. $queue.Enqueue([ordered]@{
  363. ack_line = "OK seq=00000001 cmd=STATUS"
  364. raw = "OK seq=00000001 cmd=STATUS"
  365. token = "OK seq=00000001 cmd=STATUS"
  366. class = "ok"
  367. seq = 1
  368. cmd = "STATUS"
  369. ack_offset = 1
  370. response_offset = 1
  371. delimiter = "CR"
  372. timestamp = (Get-Date).ToUniversalTime().ToString("o")
  373. }) | Out-Null
  374. $serial = [pscustomobject]@{ BytesToRead = 0 }
  375. $ack = Read-HostAckLine $serial $hostRaw $hostWindow $hostReports $queue 5 100 1 "PTCCH"
  376. if ($ack.class -ne "timeout") { throw "ack class was $($ack.class)" }
  377. if ($ack.discarded_acks.Count -ne 1) { throw "discarded ack count was $($ack.discarded_acks.Count)" }
  378. if (-not $ack.discarded_acks[0].stale) { throw "discarded ack was not marked stale" }
  379. """,
  380. encoding="utf-8",
  381. )
  382. result = subprocess.run(
  383. [powershell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(probe_path)],
  384. cwd=ROOT,
  385. text=True,
  386. capture_output=True,
  387. check=False,
  388. )
  389. self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
  390. def test_collect_j6_host_sensor_real_fan_ptc_preserves_empty_raw_replay_byte_array(self) -> None:
  391. powershell = shutil.which("powershell")
  392. if powershell is None:
  393. self.skipTest("PowerShell is not available")
  394. script_path = ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1"
  395. script = script_path.read_text(encoding="utf-8")
  396. helper_start = script.index("function Convert-HexToBytes")
  397. helper_end = script.index("$out =")
  398. helpers = script[helper_start:helper_end]
  399. with tempfile.TemporaryDirectory() as tmp:
  400. probe_path = Path(tmp) / "probe.ps1"
  401. out_path = Path(tmp) / "empty-replay.bin"
  402. probe_path.write_text(
  403. helpers
  404. + f"""
  405. $events = New-Object System.Collections.Generic.List[object]
  406. $rawReplayArray = New-BmcRawReplay $events
  407. if ($null -eq $rawReplayArray) {{
  408. throw "raw replay array was null"
  409. }}
  410. if ($rawReplayArray.GetType().FullName -ne "System.Byte[]") {{
  411. throw "raw replay array type was $($rawReplayArray.GetType().FullName)"
  412. }}
  413. if ($rawReplayArray.Length -ne 0) {{
  414. throw "raw replay array length was $($rawReplayArray.Length)"
  415. }}
  416. [System.IO.File]::WriteAllBytes("{out_path}", $rawReplayArray)
  417. """,
  418. encoding="utf-8",
  419. )
  420. result = subprocess.run(
  421. [powershell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(probe_path)],
  422. cwd=ROOT,
  423. text=True,
  424. capture_output=True,
  425. check=False,
  426. )
  427. self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
  428. self.assertTrue(out_path.exists())
  429. self.assertEqual(out_path.stat().st_size, 0)
  430. def test_collect_j6_host_sensor_real_fan_ptc_parses_hex_fields_without_losing_payload_width(self) -> None:
  431. powershell = shutil.which("powershell")
  432. if powershell is None:
  433. self.skipTest("PowerShell is not available")
  434. script_path = ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1"
  435. script = script_path.read_text(encoding="utf-8")
  436. helper_start = script.index("function Convert-TokenValue")
  437. helper_end = script.index("function Convert-BytesToEscapedText")
  438. replay_helper_start = script.index("function Convert-HexToBytes")
  439. replay_helper_end = script.index("function Get-LastFieldOrDefault")
  440. helpers = script[helper_start:helper_end] + script[replay_helper_start:replay_helper_end]
  441. with tempfile.TemporaryDirectory() as tmp:
  442. probe_path = Path(tmp) / "probe.ps1"
  443. probe_path.write_text(
  444. helpers
  445. + """
  446. $events = New-Object System.Collections.Generic.List[object]
  447. $events.Add((Convert-FrameLine "J6PWM seq=0000000A src=hm pid=disabled timeout=1 ptc01=00 ptc24=00 fan01=1E fan04=1E" @("J6PWM"))) | Out-Null
  448. $events.Add((Convert-FrameLine "J6TEMP seq=00000003 status=valid valid=1E missing=00 fault=0 host_report_drop=00000000" @("J6TEMP"))) | Out-Null
  449. $events.Add((Convert-FrameLine "J6RAW seq=00000001 off=00000000 len=02 sum=0031 data=0031" @("J6RAW"))) | Out-Null
  450. $events.Add((Convert-FrameLine "J6RAW s=002A o=00000100 l=04 u=000A d=01020304" @("J6RAW"))) | Out-Null
  451. $events.Add((Convert-FrameLine "J6EVT s=06 cmd=PID_E c=01 p=000 ok=1 e=0 to=0" @("J6EVT"))) | Out-Null
  452. $events.Add((Convert-FrameLine "DIMM i=10 t=00 m=1" @("DIMM"))) | Out-Null
  453. foreach ($probe in @(
  454. @($events[0].fields.fan01, "fan01"),
  455. @($events[0].fields.timeout, "timeout"),
  456. @($events[1].fields.valid, "valid"),
  457. @($events[1].fields.missing, "missing"),
  458. @($events[2].fields.len, "len"),
  459. @($events[2].fields.sum, "sum"),
  460. @($events[3].fields.seq, "compact_seq"),
  461. @($events[3].fields.off, "compact_off"),
  462. @($events[3].fields.len, "compact_len"),
  463. @($events[3].fields.sum, "compact_sum"),
  464. @($events[4].fields.ch, "compact_ch"),
  465. @($events[4].fields.pct, "compact_pct"),
  466. @($events[4].fields.err, "compact_err")
  467. )) {
  468. if ($probe[0].GetType().FullName -ne "System.Int64") {
  469. throw "$($probe[1]) parsed as $($probe[0].GetType().FullName)"
  470. }
  471. }
  472. if ([int64]$events[0].fields.fan01 -ne 30) { throw "fan01 was $($events[0].fields.fan01)" }
  473. if ([int64]$events[0].fields.timeout -ne 1) { throw "timeout was $($events[0].fields.timeout)" }
  474. if ([int64]$events[1].fields.valid -ne 30) { throw "valid was $($events[1].fields.valid)" }
  475. if ([int64]$events[1].fields.missing -ne 0) { throw "missing was $($events[1].fields.missing)" }
  476. if ([int64]$events[2].fields.len -ne 2) { throw "len was $($events[2].fields.len)" }
  477. if ([int64]$events[2].fields.sum -ne 49) { throw "sum was $($events[2].fields.sum)" }
  478. if ([int64]$events[3].fields.seq -ne 42) { throw "compact seq was $($events[3].fields.seq)" }
  479. if ([int64]$events[3].fields.off -ne 256) { throw "compact off was $($events[3].fields.off)" }
  480. if ([int64]$events[3].fields.len -ne 4) { throw "compact len was $($events[3].fields.len)" }
  481. if ([int64]$events[3].fields.sum -ne 10) { throw "compact sum was $($events[3].fields.sum)" }
  482. if ($events[2].fields.data.GetType().FullName -ne "System.String") { throw "data parsed as $($events[2].fields.data.GetType().FullName)" }
  483. if ($events[2].fields.data -ne "0031") { throw "data was $($events[2].fields.data)" }
  484. if ($events[3].fields.data -ne "01020304") { throw "compact data was $($events[3].fields.data)" }
  485. if ([int64]$events[4].fields.ch -ne 1) { throw "compact ch was $($events[4].fields.ch)" }
  486. if ([int64]$events[4].fields.pct -ne 0) { throw "compact pct was $($events[4].fields.pct)" }
  487. if ([int64]$events[4].fields.err -ne 0) { throw "compact err was $($events[4].fields.err)" }
  488. if ([int64]$events[5].fields.id -ne 16) { throw "dimm id was $($events[5].fields.id)" }
  489. $replay = New-BmcRawReplay $events
  490. $replayHex = (($replay | ForEach-Object { $_.ToString("X2") }) -join "")
  491. if ($replayHex -ne "003101020304") { throw "replay was $replayHex" }
  492. """,
  493. encoding="utf-8",
  494. )
  495. result = subprocess.run(
  496. [powershell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(probe_path)],
  497. cwd=ROOT,
  498. text=True,
  499. capture_output=True,
  500. check=False,
  501. )
  502. self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
  503. def test_collect_j6_host_sensor_real_fan_ptc_keeps_command_tokens_as_strings(self) -> None:
  504. powershell = shutil.which("powershell")
  505. if powershell is None:
  506. self.skipTest("PowerShell is not available")
  507. script_path = ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1"
  508. script = script_path.read_text(encoding="utf-8")
  509. helper_start = script.index("function Convert-TokenValue")
  510. helper_end = script.index("function Convert-BytesToEscapedText")
  511. helpers = script[helper_start:helper_end]
  512. with tempfile.TemporaryDirectory() as tmp:
  513. probe_path = Path(tmp) / "probe.ps1"
  514. probe_path.write_text(
  515. helpers
  516. + """
  517. $events = New-Object System.Collections.Generic.List[object]
  518. $events.Add((Convert-FrameLine "J6CMD seq=00000001 cmd=raw text=ipmitool" @("J6CMD"))) | Out-Null
  519. $events.Add((Convert-FrameLine "J6EVT seq=00000002 ok=1 err=0 type=PTCCH ch=01 pct=0A" @("J6EVT"))) | Out-Null
  520. $events.Add((Convert-FrameLine "OK seq=00000003 cmd=FANCH" @("OK"))) | Out-Null
  521. if ($events[0].fields.cmd -ne "raw") { throw "cmd was $($events[0].fields.cmd)" }
  522. if ($events[1].fields.type -ne "PTCCH") { throw "type was $($events[1].fields.type)" }
  523. if ($events[1].fields.ch.GetType().FullName -ne "System.Int64") { throw "ch type was $($events[1].fields.ch.GetType().FullName)" }
  524. if ([int64]$events[1].fields.ch -ne 1) { throw "ch was $($events[1].fields.ch)" }
  525. if ([int64]$events[1].fields.pct -ne 10) { throw "pct was $($events[1].fields.pct)" }
  526. if ($events[2].fields.cmd -ne "FANCH") { throw "ack cmd was $($events[2].fields.cmd)" }
  527. """,
  528. encoding="utf-8",
  529. )
  530. result = subprocess.run(
  531. [powershell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(probe_path)],
  532. cwd=ROOT,
  533. text=True,
  534. capture_output=True,
  535. check=False,
  536. )
  537. self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
  538. def test_j6_host_sensor_real_fan_ptc_package_script_creates_temp_profile_package(self) -> None:
  539. script = (ROOT / "tools" / "package_j6_host_sensor_real_fan_ptc_debug921.ps1").read_text(
  540. encoding="utf-8"
  541. )
  542. for marker in [
  543. "START-HERE.zh-CN.md",
  544. "tools\\remote\\collect-j6-host-sensor-real-fan-ptc.ps1",
  545. "bitstreams",
  546. "td-receipts",
  547. "commands",
  548. "pin_inventory.fcb_fpga_mcu0611.json",
  549. "sha256-inventory.json",
  550. "Compress-Archive",
  551. ".zip.sha256.txt",
  552. "temp-uart-ptc-remap",
  553. "j6-host-sensor-real-fan-ptc-temp-uart-ptc-remap-debug921-v2",
  554. "startup-preflight.json",
  555. "READY profile=temp-uart-ptc-remap",
  556. "collector first, then PROGRAM_SRAM",
  557. "ForceOverwrite",
  558. "SET PTC 01 10",
  559. "SET PTC 24 20",
  560. "SET FAN 01 40",
  561. "SET FAN 04 30",
  562. "excluded_self_referential_file",
  563. ]:
  564. self.assertIn(marker, script)
  565. self.assertNotIn("Set-ExecutionPolicy", script)
  566. self.assertNotIn("-AssumeManualSramProgrammed $true", script)
  567. for marker in [
  568. "formal-uart",
  569. "j6-host-sensor-real-fan-ptc-formal-uart-debug921-v1",
  570. "debug115",
  571. "swapped",
  572. "-ManualSramAttestation",
  573. "$att.status",
  574. "blocked_edit_required",
  575. "collect-com8-host-com7-debug.ps1",
  576. r"-Bitstream .\bitstreams\uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.bit",
  577. "SET PTC 00",
  578. "SET FAN 00",
  579. "SET FAN 05",
  580. "SET FAN 06",
  581. "pip install",
  582. "pyserial",
  583. "python",
  584. ]:
  585. self.assertNotIn(marker.lower(), script.lower())
  586. def test_j6_host_sensor_real_fan_ptc_package_output_contains_temp_profile_artifacts(self) -> None:
  587. powershell = shutil.which("powershell")
  588. if powershell is None:
  589. self.skipTest("PowerShell is not available")
  590. with tempfile.TemporaryDirectory() as tmp:
  591. bitstream = Path(tmp) / "fake.bit"
  592. td_receipt = Path(tmp) / "td-receipt.json"
  593. bitstream.write_bytes(b"fake-sram-bitstream")
  594. td_receipt.write_text(
  595. json.dumps(
  596. {
  597. "schema": "td-build-receipt-v1",
  598. "status": "pass",
  599. "programming_mode": "SRAM-only",
  600. "bitstream": {"sha256": "0" * 64},
  601. }
  602. ),
  603. encoding="utf-8",
  604. )
  605. for profile, package_name in [
  606. ("temp-uart-ptc-remap", "j6-host-sensor-real-fan-ptc-temp-uart-ptc-remap-debug921-v2-test"),
  607. ]:
  608. with self.subTest(profile=profile):
  609. result = subprocess.run(
  610. [
  611. powershell,
  612. "-NoProfile",
  613. "-ExecutionPolicy",
  614. "Bypass",
  615. "-File",
  616. str(ROOT / "tools" / "package_j6_host_sensor_real_fan_ptc_debug921.ps1"),
  617. "-OutputDir",
  618. tmp,
  619. "-PackageName",
  620. package_name,
  621. "-Profile",
  622. profile,
  623. "-Bitstream",
  624. str(bitstream),
  625. "-TdReceipt",
  626. str(td_receipt),
  627. ],
  628. cwd=ROOT,
  629. capture_output=True,
  630. check=False,
  631. )
  632. output = (result.stderr or b"").decode("utf-8", errors="replace") + (
  633. result.stdout or b""
  634. ).decode("utf-8", errors="replace")
  635. self.assertEqual(result.returncode, 0, output)
  636. stage = Path(tmp) / package_name
  637. self.assertTrue((stage / "START-HERE.zh-CN.md").is_file())
  638. self.assertTrue((stage / "collector" / "collect-j6-host-sensor-real-fan-ptc.ps1").is_file())
  639. self.assertTrue((stage / "commands" / "collect-template.ps1").is_file())
  640. self.assertTrue((stage / "bitstreams" / bitstream.name).is_file())
  641. self.assertTrue((stage / "td-receipts" / td_receipt.name).is_file())
  642. self.assertTrue((stage / "config" / "pin_inventory.fcb_fpga_mcu0611.json").is_file())
  643. self.assertTrue((stage / "schemas" / "real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt.schema.json").is_file())
  644. self.assertFalse((stage / "blocked").exists())
  645. self.assertTrue((Path(tmp) / f"{package_name}.zip").is_file())
  646. self.assertTrue((Path(tmp) / f"{package_name}.zip.sha256.txt").is_file())
  647. start_here = (stage / "START-HERE.zh-CN.md").read_text(encoding="utf-8-sig")
  648. self.assertIn("PROGRAM_SRAM", start_here)
  649. self.assertIn("先启动 collector", start_here)
  650. self.assertIn("collector first, then PROGRAM_SRAM", start_here)
  651. self.assertIn(f"-Profile {profile}", start_here)
  652. self.assertIn(f".\\bitstreams\\{bitstream.name}", start_here)
  653. self.assertIn(f".\\td-receipts\\{td_receipt.name}", start_here)
  654. self.assertIn("powershell.exe -NoProfile -ExecutionPolicy Bypass -File", start_here)
  655. self.assertIn("-HostPort COM8 -DebugPort COM7", start_here)
  656. self.assertNotIn("Set-ExecutionPolicy", start_here)
  657. self.assertNotIn("-AssumeManualSramProgrammed", start_here)
  658. self.assertNotIn("先手动刷 SRAM", start_here)
  659. self.assertNotIn("刷完 SRAM 后采集", start_here)
  660. collect_template = (stage / "commands" / "collect-template.ps1").read_text(
  661. encoding="utf-8-sig"
  662. )
  663. self.assertNotIn("Set-ExecutionPolicy", collect_template)
  664. self.assertNotIn("-AssumeManualSramProgrammed", collect_template)
  665. self.assertIn("-NoProfile", collect_template)
  666. self.assertIn("-ExecutionPolicy Bypass", collect_template)
  667. self.assertIn("-File .\\collector\\collect-j6-host-sensor-real-fan-ptc.ps1", collect_template)
  668. self.assertNotIn("blocked_pin_conflict_with_locked_debug_uart", start_here)
  669. manifest = json.loads((stage / "manifest.json").read_text(encoding="utf-8-sig"))
  670. self.assertEqual(manifest["status"], "pass")
  671. self.assertEqual(manifest["pin_profile"], profile)
  672. self.assertEqual(manifest["programming_mode"], "PROGRAM_SRAM")
  673. self.assertIn("host_control_commands", manifest)
  674. self.assertIn("pin_map", manifest)
  675. self.assertNotIn("pin_conflicts", manifest)
  676. inventory = json.loads((stage / "sha256-inventory.json").read_text(encoding="utf-8-sig"))
  677. self.assertEqual(inventory["self_hash_policy"], "excluded_self_referential_file")
  678. inventory_paths = {item["path"] for item in inventory["inventory"]}
  679. self.assertIn("manifest.json", inventory_paths)
  680. self.assertIn("START-HERE.zh-CN.md", inventory_paths)
  681. self.assertIn("collector/collect-j6-host-sensor-real-fan-ptc.ps1", inventory_paths)
  682. self.assertIn("commands/collect-template.ps1", inventory_paths)
  683. self.assertNotIn("sha256-inventory.json", inventory_paths)
  684. def test_j6_host_sensor_real_fan_ptc_runbook_is_chinese_copy_paste_ready_with_english_paths(self) -> None:
  685. runbook = (ROOT / "docs" / "j6-host-sensor-real-fan-ptc-debug921-runbook.zh-CN.md").read_text(
  686. encoding="utf-8"
  687. )
  688. for marker in [
  689. "J6 Host Sensor Real FAN/PTC Debug921",
  690. "START-HERE.zh-CN.md",
  691. "temp-uart-ptc-remap",
  692. "PROGRAM_SRAM",
  693. "collector first, then PROGRAM_SRAM",
  694. "COM8",
  695. "COM7",
  696. "PTC03",
  697. "PTC04",
  698. "PTC17",
  699. "PTC18",
  700. "PTC21",
  701. "PTC22",
  702. "SET PTC 01 10",
  703. "SET PTC 24 20",
  704. "SET FAN 01 40",
  705. "SET FAN 04 30",
  706. "SET PID ENABLE",
  707. "GET SENSOR",
  708. "SET PID DISABLE",
  709. "DISABLE",
  710. "PTCCH",
  711. "FANCH",
  712. "1-based",
  713. "SENS",
  714. "DIMM",
  715. "J6TEMP",
  716. "J6RAW",
  717. "host-port-escaped.txt",
  718. "debug-uart-escaped.txt",
  719. "host-sensor-report.json",
  720. "bmc-raw-replay.bin",
  721. "不安装驱动",
  722. "不枚举未知 COM",
  723. "不写 DebugPort",
  724. "blocked_pin_conflict_with_locked_debug_uart",
  725. ]:
  726. self.assertIn(marker, runbook)
  727. for forbidden in [
  728. "debug115",
  729. "Manual SRAM attestation status must be pass",
  730. "blocked_edit_required",
  731. "当前不要刷机",
  732. "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force",
  733. "先手动刷 SRAM",
  734. "确认 SRAM 下载完成后",
  735. r".\commands\collect-com8-host-com7-debug.ps1",
  736. r"bitstreams\uart_rs232_j6_host_sensor_real_fan_ptc_debug921_primary.bit",
  737. "collector/collect-j6-host-sensor-real-fan-ptc.ps1",
  738. "SET PTC 00",
  739. "SET FAN 00",
  740. "SET FAN 05",
  741. "SET FAN 06",
  742. r".\collector\采集",
  743. ]:
  744. self.assertNotIn(forbidden, runbook)
  745. def test_j6_host_sensor_real_fan_ptc_schema_requires_sensor_pwm_pid_pinmap_and_debug_artifacts(self) -> None:
  746. schema = json.loads(
  747. (ROOT / "schemas" / "real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt.schema.json").read_text(
  748. encoding="utf-8"
  749. )
  750. )
  751. layers = schema["properties"]["layers"]["properties"]
  752. self.assertEqual(schema["properties"]["diagnostic_scope"], {"const": "host_sensor_real_fan_ptc_debug921"})
  753. for layer in [
  754. "host_command_sequence",
  755. "host_control_transcript",
  756. "host_sensor_reports",
  757. "debug_frame_capture",
  758. "bmc_raw_sampling",
  759. "pid_boundary",
  760. "timeout_evidence",
  761. "pin_map",
  762. "real_fan_ptc_hardware_status",
  763. "artifact_hashes",
  764. ]:
  765. self.assertIn(layer, layers)
  766. commands = layers["host_command_sequence"]["properties"]["commands"]["prefixItems"]
  767. self.assertEqual(commands[0], {"const": "SET PTC 01 10"})
  768. self.assertEqual(commands[1], {"const": "SET PTC 24 20"})
  769. self.assertEqual(commands[2], {"const": "SET FAN 01 40"})
  770. self.assertEqual(commands[3], {"const": "SET FAN 04 30"})
  771. self.assertIn("host_sensor_report", layers["host_sensor_reports"]["required"])
  772. self.assertIn("debug_frame_stream", layers["debug_frame_capture"]["required"])
  773. self.assertIn("raw_replay", layers["debug_frame_capture"]["required"])
  774. self.assertEqual(layers["pin_map"]["properties"]["ptc_count"], {"const": 24})
  775. self.assertEqual(layers["pin_map"]["properties"]["fan_count"], {"const": 4})
  776. if __name__ == "__main__":
  777. unittest.main()