| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- #!/usr/bin/env python3
- """Tests for the J6 binary921 host/debug protocol helpers."""
- from __future__ import annotations
- import sys
- import unittest
- from pathlib import Path
- sys.dont_write_bytecode = True
- ROOT = Path(__file__).resolve().parents[2]
- sys.path.insert(0, str(ROOT / "tools"))
- from binary921 import ( # noqa: E402
- Binary921FrameError,
- CommandId,
- FrameType,
- build_host_command,
- decode_frame,
- decode_stream,
- encode_frame,
- frame_to_dict,
- profile_id_for_name,
- profile_name_for_id,
- )
- class Binary921ProtocolTests(unittest.TestCase):
- def test_encode_decode_roundtrip_uses_magic_version_type_len_and_crc16(self) -> None:
- payload = bytes([CommandId.SET_PTC, 0x01, 0x0A])
- frame = encode_frame(
- profile_id=1,
- frame_type=FrameType.HOST_CMD,
- seq=0x01020304,
- timestamp_tick=0x11223344,
- payload=payload,
- )
- self.assertEqual(frame[:2], b"\xA5\x5A")
- self.assertEqual(frame[2], 1)
- self.assertEqual(frame[3], 1)
- self.assertEqual(frame[4], FrameType.HOST_CMD)
- self.assertEqual(frame[5:9], b"\x04\x03\x02\x01")
- self.assertEqual(frame[9:13], b"\x44\x33\x22\x11")
- self.assertEqual(frame[13], len(payload))
- decoded = decode_frame(frame)
- self.assertEqual(decoded.profile_id, 1)
- self.assertEqual(decoded.frame_type, FrameType.HOST_CMD)
- self.assertEqual(decoded.seq, 0x01020304)
- self.assertEqual(decoded.timestamp_tick, 0x11223344)
- self.assertEqual(decoded.payload, payload)
- def test_decode_rejects_bad_crc_and_short_frames(self) -> None:
- good = encode_frame(
- profile_id=1,
- frame_type=FrameType.READY,
- seq=1,
- timestamp_tick=2,
- payload=b"",
- )
- bad = bytearray(good)
- bad[-1] ^= 0x01
- with self.assertRaisesRegex(Binary921FrameError, "crc"):
- decode_frame(bytes(bad))
- with self.assertRaisesRegex(Binary921FrameError, "short"):
- decode_frame(good[:7])
- def test_decode_stream_resynchronizes_after_noise(self) -> None:
- first = encode_frame(profile_id=1, frame_type=FrameType.READY, seq=1, timestamp_tick=0, payload=b"")
- second = encode_frame(
- profile_id=1,
- frame_type=FrameType.PWM_STATE,
- seq=2,
- timestamp_tick=20,
- payload=bytes([10, 20, 40, 30]),
- )
- frames, remainder = decode_stream(b"\x00noise" + first + b"\xFF" + second + b"\xA5")
- self.assertEqual([frame.frame_type for frame in frames], [FrameType.READY, FrameType.PWM_STATE])
- self.assertEqual(remainder, b"\xA5")
- def test_decode_stream_accepts_mixed_combined_uart_frames(self) -> None:
- profile_id = 3
- stream = b"".join(
- [
- encode_frame(profile_id=profile_id, frame_type=FrameType.READY, seq=0, timestamp_tick=0, payload=bytes([profile_id, 0, 0, 0])),
- encode_frame(profile_id=profile_id, frame_type=FrameType.HOST_ACK, seq=1, timestamp_tick=1, payload=bytes([1, CommandId.SET_PTC, 0, 0, 0x34, 0x12])),
- encode_frame(profile_id=profile_id, frame_type=FrameType.BMC_PAYLOAD, seq=2, timestamp_tick=2, payload=bytes([48, 48, 0, 1, 0x1E, 0xFF, 0, 0])),
- encode_frame(profile_id=profile_id, frame_type=FrameType.SENSOR_CACHE, seq=3, timestamp_tick=3, payload=bytes([profile_id, 0, 1, 0xFE, 0xEF, 0xFF, 2, 22])),
- ]
- )
- frames, remainder = decode_stream(stream)
- self.assertEqual(remainder, b"")
- self.assertEqual([frame.profile_id for frame in frames], [profile_id] * 4)
- self.assertEqual(
- [frame.frame_type for frame in frames],
- [FrameType.READY, FrameType.HOST_ACK, FrameType.BMC_PAYLOAD, FrameType.SENSOR_CACHE],
- )
- def test_host_command_builder_validates_channels_and_percentages(self) -> None:
- set_ptc = build_host_command(seq=1, command=CommandId.SET_PTC, channel=24, percent=20)
- decoded_ptc = decode_frame(set_ptc)
- self.assertEqual(decoded_ptc.frame_type, FrameType.HOST_CMD)
- self.assertEqual(decoded_ptc.payload, bytes([CommandId.SET_PTC, 24, 20]))
- set_fan = build_host_command(seq=2, command=CommandId.SET_FAN, channel=4, percent=30)
- self.assertEqual(decode_frame(set_fan).payload, bytes([CommandId.SET_FAN, 4, 30]))
- for channel in (0, 25):
- with self.subTest(channel=channel):
- with self.assertRaisesRegex(ValueError, "PTC"):
- build_host_command(seq=3, command=CommandId.SET_PTC, channel=channel, percent=10)
- for channel in (0, 5):
- with self.subTest(channel=channel):
- with self.assertRaisesRegex(ValueError, "FAN"):
- build_host_command(seq=4, command=CommandId.SET_FAN, channel=channel, percent=10)
- with self.assertRaisesRegex(ValueError, "percent"):
- build_host_command(seq=5, command=CommandId.SET_PTC, channel=1, percent=101)
- def test_zero_payload_commands_have_stable_binary_payloads(self) -> None:
- for command in [
- CommandId.GET_SENSOR,
- CommandId.GET_MAP,
- CommandId.GET_STATUS,
- CommandId.DISABLE,
- ]:
- with self.subTest(command=command):
- frame = decode_frame(build_host_command(seq=7, command=command))
- self.assertEqual(frame.payload, bytes([command]))
- def test_frame_to_dict_decodes_ready_host_command_and_ack_payloads(self) -> None:
- ready = decode_frame(
- encode_frame(profile_id=1, frame_type=FrameType.READY, seq=1, timestamp_tick=2, payload=bytes([1, 0, 0, 0]))
- )
- command = decode_frame(build_host_command(seq=9, command=CommandId.SET_PTC, channel=1, percent=10))
- ack = decode_frame(
- encode_frame(
- profile_id=1,
- frame_type=FrameType.HOST_ACK,
- seq=9,
- timestamp_tick=12,
- payload=bytes([1, CommandId.SET_PTC, 0, 0, 0x34, 0x12]),
- )
- )
- self.assertEqual(frame_to_dict(ready)["decoded"], {"kind": "ready", "profile_id_payload": 1})
- self.assertEqual(
- frame_to_dict(command)["decoded"],
- {"kind": "host_cmd", "cmd": "SET_PTC", "cmd_id": 32, "channel": 1, "percent": 10},
- )
- self.assertEqual(
- frame_to_dict(ack)["decoded"],
- {
- "kind": "host_ack",
- "ok": True,
- "cmd": "SET_PTC",
- "cmd_id": 32,
- "status_code": 0,
- "applied_state_hash": 0x1234,
- },
- )
- def test_production_two_uart_profile_has_stable_id_three(self) -> None:
- profile = "production-two-uart-bmc-replay-binary921"
- self.assertEqual(profile_id_for_name(profile), 3)
- self.assertEqual(profile_name_for_id(3), profile)
- frame = decode_frame(
- build_host_command(
- seq=1,
- command=CommandId.SET_PTC,
- channel=1,
- percent=10,
- profile_id=profile_id_for_name(profile),
- )
- )
- self.assertEqual(frame.profile_id, 3)
- self.assertEqual(frame.payload, bytes([CommandId.SET_PTC, 1, 10]))
- def test_module_snapshot_decodes_pwm_pid_and_timeout_summary(self) -> None:
- payload = bytes(
- [
- 3, # profile
- 0, # host_err_count
- 12,
- 0, # command_count_seen
- 0x1E,
- 0xFF,
- 0x08,
- 0x14,
- CommandId.DISABLE,
- 0,
- 0,
- 0,
- 0, # pid requested false
- 1, # timeout active
- 2,
- 22,
- 0,
- 0,
- 30,
- 30,
- 0,
- 0x0F,
- 0,
- 0,
- ]
- )
- frame = decode_frame(
- encode_frame(profile_id=3, frame_type=FrameType.MODULE_SNAPSHOT, seq=9, timestamp_tick=10, payload=payload)
- )
- decoded = frame_to_dict(frame)["decoded"]
- self.assertEqual(decoded["kind"], "module_snapshot")
- self.assertEqual(decoded["profile_id_payload"], 3)
- self.assertEqual(decoded["command_count_seen"], 12)
- self.assertEqual(decoded["last_cmd"], "DISABLE")
- self.assertFalse(decoded["pid_requested"])
- self.assertTrue(decoded["timeout_active"])
- self.assertEqual(decoded["ptc01_percent"], 0)
- self.assertEqual(decoded["ptc24_percent"], 0)
- self.assertEqual(decoded["fan01_percent"], 30)
- self.assertEqual(decoded["fan04_percent"], 30)
- if __name__ == "__main__":
- unittest.main()
|