test_transmit.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import unittest.mock
  2. import pytest
  3. import cc1101.options
  4. # pylint: disable=protected-access
  5. def test_transmit_empty_payload(transceiver):
  6. with unittest.mock.patch.object(
  7. transceiver,
  8. "get_packet_length_mode",
  9. return_value=cc1101.options.PacketLengthMode.VARIABLE,
  10. ), unittest.mock.patch.object(
  11. transceiver, "get_packet_length_bytes", return_value=21
  12. ):
  13. with pytest.raises(ValueError, match=r"\bempty\b"):
  14. transceiver.transmit([])
  15. @pytest.mark.parametrize("payload", (b"\0\x01\x02", [0, 127]))
  16. def test_transmit_first_null(transceiver, payload):
  17. with unittest.mock.patch.object(
  18. transceiver,
  19. "get_packet_length_mode",
  20. return_value=cc1101.options.PacketLengthMode.VARIABLE,
  21. ), unittest.mock.patch.object(
  22. transceiver, "get_packet_length_bytes", return_value=21
  23. ):
  24. with pytest.raises(ValueError, match=r"\bfirst byte\b.*\bmust not be null\b"):
  25. transceiver.transmit(payload)
  26. @pytest.mark.parametrize(
  27. ("max_packet_length", "payload"),
  28. ((3, "\x04\x01\x02\x03"), (4, [7, 21, 42, 0, 0, 1, 2, 3])),
  29. )
  30. def test_transmit_exceeding_max_length(transceiver, max_packet_length, payload):
  31. with unittest.mock.patch.object(
  32. transceiver,
  33. "get_packet_length_mode",
  34. return_value=cc1101.options.PacketLengthMode.VARIABLE,
  35. ), unittest.mock.patch.object(
  36. transceiver, "get_packet_length_bytes", return_value=max_packet_length
  37. ):
  38. with pytest.raises(
  39. ValueError, match=r"\bpayload exceeds maximum payload length\b"
  40. ):
  41. transceiver.transmit(payload)
  42. @pytest.mark.parametrize(
  43. ("packet_length", "payload"),
  44. ((3, "\x04\x01\x02\x03"), (4, [7, 21, 42, 0, 0, 1, 2, 3])),
  45. )
  46. def test_transmit_unexpected_payload_len(transceiver, packet_length, payload):
  47. with unittest.mock.patch.object(
  48. transceiver,
  49. "get_packet_length_mode",
  50. return_value=cc1101.options.PacketLengthMode.FIXED,
  51. ), unittest.mock.patch.object(
  52. transceiver, "get_packet_length_bytes", return_value=packet_length
  53. ):
  54. with pytest.raises(ValueError, match=r"\bpayload length\b"):
  55. transceiver.transmit(payload)
  56. @pytest.mark.parametrize("payload", (b"\0", b"\xaa\xbb\xcc", bytes(range(42))))
  57. def test_transmit_fixed(transceiver, payload):
  58. transceiver._spi.xfer.side_effect = lambda v: [15] * len(v)
  59. with unittest.mock.patch.object(
  60. transceiver,
  61. "get_packet_length_mode",
  62. return_value=cc1101.options.PacketLengthMode.FIXED,
  63. ), unittest.mock.patch.object(
  64. transceiver, "get_packet_length_bytes", return_value=len(payload)
  65. ), unittest.mock.patch.object(
  66. transceiver,
  67. "get_main_radio_control_state_machine_state",
  68. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  69. ):
  70. transceiver.transmit(payload)
  71. assert transceiver._spi.xfer.call_args_list == [
  72. unittest.mock.call([0x3B]), # flush
  73. unittest.mock.call([0x3F | 0x40] + list(payload)),
  74. unittest.mock.call([0x35]), # start transmission
  75. ]
  76. @pytest.mark.parametrize(
  77. "payload", (b"\x01\0", b"\x03\xaa\xbb\xcc", b"\x10" + bytes(range(16)))
  78. )
  79. def test_transmit_variable(transceiver, payload):
  80. transceiver._spi.xfer.side_effect = lambda v: [15] * len(v)
  81. with unittest.mock.patch.object(
  82. transceiver,
  83. "get_packet_length_mode",
  84. return_value=cc1101.options.PacketLengthMode.VARIABLE,
  85. ), unittest.mock.patch.object(
  86. transceiver, "get_packet_length_bytes", return_value=255
  87. ), unittest.mock.patch.object(
  88. transceiver,
  89. "get_main_radio_control_state_machine_state",
  90. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  91. ):
  92. transceiver.transmit(payload)
  93. assert transceiver._spi.xfer.call_args_list == [
  94. unittest.mock.call([0x3B]), # flush
  95. unittest.mock.call([0x3F | 0x40] + list(payload)),
  96. unittest.mock.call([0x35]), # start transmission
  97. ]