1
0

test_cli_transmit.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # python-cc1101 - Python Library to Transmit RF Signals via C1101 Transceivers
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import io
  18. import logging
  19. import unittest.mock
  20. import pytest
  21. import cc1101._cli
  22. from cc1101.options import PacketLengthMode, SyncMode
  23. # pylint: disable=protected-access
  24. @pytest.mark.parametrize(
  25. (
  26. "args",
  27. "base_frequency_hertz",
  28. "symbol_rate_baud",
  29. "sync_mode",
  30. "packet_length_mode",
  31. "checksum_disabled",
  32. ),
  33. (
  34. ([""], None, None, None, None, False),
  35. (["", "-f", "433920000"], 433920000, None, None, None, False),
  36. (
  37. ["", "--base-frequency-hertz", "868000000"],
  38. 868000000,
  39. None,
  40. None,
  41. None,
  42. False,
  43. ),
  44. (["", "-r", "10000"], None, 10000, None, None, False),
  45. (["", "--symbol-rate-baud", "12345"], None, 12345, None, None, False),
  46. (
  47. ["", "-s", "no-preamble-and-sync-word"],
  48. None,
  49. None,
  50. SyncMode.NO_PREAMBLE_AND_SYNC_WORD,
  51. None,
  52. False,
  53. ),
  54. (
  55. ["", "--sync-mode", "no-preamble-and-sync-word"],
  56. None,
  57. None,
  58. SyncMode.NO_PREAMBLE_AND_SYNC_WORD,
  59. None,
  60. False,
  61. ),
  62. (
  63. ["", "--sync-mode", "transmit-16-match-15-bits"],
  64. None,
  65. None,
  66. SyncMode.TRANSMIT_16_MATCH_15_BITS,
  67. None,
  68. False,
  69. ),
  70. (["", "-l", "fixed"], None, None, None, PacketLengthMode.FIXED, False),
  71. (
  72. ["", "--packet-length-mode", "fixed"],
  73. None,
  74. None,
  75. None,
  76. PacketLengthMode.FIXED,
  77. False,
  78. ),
  79. (
  80. ["", "--packet-length-mode", "variable"],
  81. None,
  82. None,
  83. None,
  84. PacketLengthMode.VARIABLE,
  85. False,
  86. ),
  87. (["", "--disable-checksum"], None, None, None, None, True),
  88. ),
  89. )
  90. @pytest.mark.parametrize("payload", (b"", b"message"))
  91. def test_configure_device(
  92. args,
  93. base_frequency_hertz,
  94. symbol_rate_baud,
  95. sync_mode,
  96. packet_length_mode,
  97. checksum_disabled,
  98. payload,
  99. ):
  100. # pylint: disable=too-many-arguments
  101. with unittest.mock.patch("cc1101.CC1101") as transceiver_mock:
  102. stdin_mock = unittest.mock.MagicMock()
  103. stdin_mock.buffer = io.BytesIO(payload)
  104. with unittest.mock.patch("sys.stdin", stdin_mock):
  105. with unittest.mock.patch("sys.argv", args):
  106. cc1101._cli._transmit()
  107. transceiver_mock.assert_called_once_with()
  108. if base_frequency_hertz is None:
  109. transceiver_mock().__enter__().set_base_frequency_hertz.assert_not_called()
  110. else:
  111. transceiver_mock().__enter__().set_base_frequency_hertz.assert_called_once_with(
  112. base_frequency_hertz
  113. )
  114. if symbol_rate_baud is None:
  115. transceiver_mock().__enter__().set_symbol_rate_baud.assert_not_called()
  116. else:
  117. transceiver_mock().__enter__().set_symbol_rate_baud.assert_called_once_with(
  118. symbol_rate_baud
  119. )
  120. if sync_mode is None:
  121. transceiver_mock().__enter__().set_sync_mode.assert_not_called()
  122. else:
  123. transceiver_mock().__enter__().set_sync_mode.assert_called_once_with(sync_mode)
  124. if packet_length_mode is None:
  125. transceiver_mock().__enter__().set_packet_length_mode.assert_not_called()
  126. else:
  127. transceiver_mock().__enter__().set_packet_length_mode.assert_called_once_with(
  128. packet_length_mode
  129. )
  130. if packet_length_mode == PacketLengthMode.FIXED:
  131. transceiver_mock().__enter__().set_packet_length_bytes.assert_called_with(
  132. len(payload)
  133. )
  134. else:
  135. transceiver_mock().__enter__().set_packet_length_bytes.assert_not_called()
  136. if checksum_disabled:
  137. transceiver_mock().__enter__().disable_checksum.assert_called_once_with()
  138. else:
  139. transceiver_mock().__enter__().disable_checksum.assert_not_called()
  140. @pytest.mark.parametrize(
  141. ("args", "root_log_level", "log_format"),
  142. (
  143. ([""], logging.INFO, "%(message)s"),
  144. (
  145. ["", "--debug"],
  146. logging.DEBUG,
  147. "%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s",
  148. ),
  149. ),
  150. )
  151. def test_root_log_level(args, root_log_level, log_format):
  152. stdin_mock = unittest.mock.MagicMock()
  153. stdin_mock.buffer = io.BytesIO(b"")
  154. with unittest.mock.patch("cc1101.CC1101"), unittest.mock.patch(
  155. "sys.stdin", stdin_mock
  156. ), unittest.mock.patch("sys.argv", args), unittest.mock.patch(
  157. "logging.basicConfig"
  158. ) as logging_basic_config_mock:
  159. cc1101._cli._transmit()
  160. assert logging_basic_config_mock.call_count == 1
  161. assert logging_basic_config_mock.call_args[1]["level"] == root_log_level
  162. assert logging_basic_config_mock.call_args[1]["format"] == log_format
  163. @pytest.mark.parametrize("payload", (b"", b"message"))
  164. def test_logging(caplog, payload):
  165. # pylint: disable=too-many-arguments
  166. stdin_mock = unittest.mock.MagicMock()
  167. stdin_mock.buffer = io.BytesIO(payload)
  168. with unittest.mock.patch("sys.stdin", stdin_mock), unittest.mock.patch(
  169. "sys.argv", [""]
  170. ), unittest.mock.patch("cc1101.CC1101") as transceiver_mock, caplog.at_level(
  171. logging.DEBUG
  172. ):
  173. transceiver_mock().__enter__().__str__.return_value = "dummy"
  174. cc1101._cli._transmit()
  175. assert caplog.record_tuples == [
  176. (
  177. "cc1101._cli",
  178. logging.DEBUG,
  179. "args=Namespace(base_frequency_hertz=None, debug=False, "
  180. "disable_checksum=False, packet_length_mode=None, symbol_rate_baud=None, "
  181. "sync_mode=None)",
  182. ),
  183. ("cc1101._cli", logging.INFO, "dummy"),
  184. ]