test_prod2u_p4_temperature_contract.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python3
  2. """Wire-contract tests for the on-demand CT7428 temperature response."""
  3. from __future__ import annotations
  4. import pytest
  5. from tools import prod2u_p4_protocol as protocol
  6. GOLDEN_REQUEST = bytes.fromhex(
  7. "A55A020405000000000000000004280000009E0C"
  8. )
  9. GOLDEN_RESPONSE = bytes.fromhex(
  10. "A55A0204101B"
  11. "000102030405060708090A0B0C0D0E0F101112131415161718191A"
  12. "1FC9"
  13. )
  14. ALL_FF_RESPONSE = bytes.fromhex(
  15. "A55A0204101B"
  16. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  17. "1BC4"
  18. )
  19. def test_get_temperatures_request_matches_the_frozen_wire_frame() -> None:
  20. assert protocol.CommandId.GET_TEMPERATURES == 0x28
  21. assert protocol.build_get_temperatures() == GOLDEN_REQUEST
  22. decoded = protocol.decode_frame(GOLDEN_REQUEST)
  23. assert decoded.frame_type == protocol.FrameType.HOST_CMD
  24. assert decoded.payload == bytes.fromhex("28000000")
  25. def test_temperature_response_matches_the_frozen_35_byte_frame() -> None:
  26. values = bytes(range(27))
  27. response = protocol.build_temperature_response(values)
  28. assert response == GOLDEN_RESPONSE
  29. assert len(response) == 35
  30. assert response[:6] == bytes.fromhex("A55A0204101B")
  31. assert response[6:33] == values
  32. assert response[-2:] == protocol.crc16_ccitt(response[2:-2]).to_bytes(
  33. 2, "little"
  34. )
  35. def test_temperature_response_all_ff_frame_matches_the_frozen_crc() -> None:
  36. response = protocol.build_temperature_response(bytes([0xFF] * 27))
  37. assert response == ALL_FF_RESPONSE
  38. assert response[-2:] == bytes.fromhex("1BC4")
  39. def test_temperature_bus_error_ack_decodes_as_status_0x0a() -> None:
  40. wire = protocol.encode_frame(
  41. frame_type=protocol.FrameType.HOST_ACK,
  42. seq=0,
  43. timestamp_tick=0,
  44. payload=bytes.fromhex("00280A0000000000"),
  45. )
  46. ack = protocol.decode_host_ack(protocol.decode_frame(wire))
  47. assert ack.command == protocol.CommandId.GET_TEMPERATURES
  48. assert ack.status == protocol.StatusCode.TEMPERATURE_BUS_ERROR
  49. @pytest.mark.parametrize(
  50. "values",
  51. [
  52. bytes(26),
  53. bytes(28),
  54. [0] * 26 + [256],
  55. [0] * 26 + [-1],
  56. ],
  57. )
  58. def test_temperature_response_rejects_non_27_byte_payloads(values: object) -> None:
  59. with pytest.raises(ValueError):
  60. protocol.build_temperature_response(values)