test_transmit.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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(
  16. ("max_packet_length", "payload"),
  17. ((3, "\x04\x01\x02\x03"), (4, [7, 21, 42, 0, 0, 1, 2, 3])),
  18. )
  19. def test_transmit_exceeding_max_length(transceiver, max_packet_length, payload):
  20. with unittest.mock.patch.object(
  21. transceiver,
  22. "get_packet_length_mode",
  23. return_value=cc1101.options.PacketLengthMode.VARIABLE,
  24. ), unittest.mock.patch.object(
  25. transceiver, "get_packet_length_bytes", return_value=max_packet_length
  26. ):
  27. with pytest.raises(
  28. ValueError, match=r"\bpayload exceeds maximum payload length\b"
  29. ):
  30. transceiver.transmit(payload)
  31. @pytest.mark.parametrize(
  32. ("packet_length", "payload"),
  33. ((3, "\x04\x01\x02\x03"), (4, [7, 21, 42, 0, 0, 1, 2, 3])),
  34. )
  35. def test_transmit_unexpected_payload_len(transceiver, packet_length, payload):
  36. with unittest.mock.patch.object(
  37. transceiver,
  38. "get_packet_length_mode",
  39. return_value=cc1101.options.PacketLengthMode.FIXED,
  40. ), unittest.mock.patch.object(
  41. transceiver, "get_packet_length_bytes", return_value=packet_length
  42. ):
  43. with pytest.raises(ValueError, match=r"\bpayload length\b"):
  44. transceiver.transmit(payload)
  45. @pytest.mark.parametrize("payload", (b"\0", b"\xaa\xbb\xcc", bytes(range(42))))
  46. def test_transmit_fixed(transceiver, payload):
  47. transceiver._spi.xfer.side_effect = lambda v: [15] * len(v)
  48. with unittest.mock.patch.object(
  49. transceiver,
  50. "get_packet_length_mode",
  51. return_value=cc1101.options.PacketLengthMode.FIXED,
  52. ), unittest.mock.patch.object(
  53. transceiver, "get_packet_length_bytes", return_value=len(payload)
  54. ), unittest.mock.patch.object(
  55. transceiver,
  56. "get_main_radio_control_state_machine_state",
  57. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  58. ):
  59. transceiver.transmit(payload)
  60. assert transceiver._spi.xfer.call_args_list == [
  61. unittest.mock.call([0x3B]), # flush
  62. unittest.mock.call([0x3F | 0x40] + list(payload)),
  63. unittest.mock.call([0x35]), # start transmission
  64. ]
  65. @pytest.mark.parametrize(
  66. "payload", (b"\x01\0", b"\x03\xaa\xbb\xcc", b"\x10" + bytes(range(16)))
  67. )
  68. def test_transmit_variable(transceiver, payload):
  69. transceiver._spi.xfer.side_effect = lambda v: [15] * len(v)
  70. with unittest.mock.patch.object(
  71. transceiver,
  72. "get_packet_length_mode",
  73. return_value=cc1101.options.PacketLengthMode.VARIABLE,
  74. ), unittest.mock.patch.object(
  75. transceiver, "get_packet_length_bytes", return_value=255
  76. ), unittest.mock.patch.object(
  77. transceiver,
  78. "get_main_radio_control_state_machine_state",
  79. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  80. ):
  81. transceiver.transmit(payload)
  82. assert transceiver._spi.xfer.call_args_list == [
  83. unittest.mock.call([0x3B]), # flush
  84. unittest.mock.call([0x3F | 0x40] + [len(payload)] + list(payload)),
  85. unittest.mock.call([0x35]), # start transmission
  86. ]