1
0

test_transmit.py 3.8 KB

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