test_remote_scripts.py 38 KB

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