test_gpio.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # python-cc1101 - Python Library to Transmit RF Signals via CC1101 Transceivers
  2. #
  3. # Copyright (C) 2021 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 ctypes
  18. import ctypes.util
  19. import datetime
  20. import errno
  21. import re
  22. import unittest.mock
  23. import pytest
  24. import cc1101._gpio
  25. # pylint: disable=protected-access
  26. def test__load_libgpiod():
  27. with unittest.mock.patch(
  28. "ctypes.util.find_library", return_value=ctypes.util.find_library("c")
  29. ) as find_library_mock:
  30. assert isinstance(cc1101._gpio._load_libgpiod(), ctypes.CDLL)
  31. find_library_mock.assert_called_once_with("gpiod")
  32. def test_load_libgpiod_not_found():
  33. cc1101._gpio._load_libgpiod.cache_clear()
  34. with unittest.mock.patch(
  35. "ctypes.util.find_library", return_value=None
  36. ), pytest.raises(FileNotFoundError):
  37. cc1101._gpio._load_libgpiod()
  38. @pytest.mark.parametrize("name", ("GPIO24", "GPIO25"))
  39. def test_line_find_permission_denied(libgpiod_mock, name):
  40. libgpiod_mock.gpiod_line_find.return_value = 0
  41. ctypes.set_errno(errno.EACCES)
  42. with pytest.raises(
  43. PermissionError, match=f"^Failed to access GPIO line {re.escape(name)!r}\\.\\n"
  44. ):
  45. cc1101._gpio.GPIOLine.find(name.encode())
  46. @pytest.mark.parametrize("name", ("GPIO24", "GPIO25"))
  47. def test_line_find_non_existing(libgpiod_mock, name):
  48. libgpiod_mock.gpiod_line_find.return_value = 0
  49. ctypes.set_errno(errno.ENOENT)
  50. with pytest.raises(
  51. FileNotFoundError, match=f"^GPIO line {re.escape(name)!r} does not exist\\.\\n"
  52. ):
  53. cc1101._gpio.GPIOLine.find(name.encode())
  54. @pytest.mark.parametrize("name", ("GPIO24", "GPIO25"))
  55. def test_line_find_unknown_error(libgpiod_mock, name):
  56. libgpiod_mock.gpiod_line_find.return_value = 0
  57. ctypes.set_errno(errno.ENOANO)
  58. with pytest.raises(
  59. OSError, match=f"^Failed to open GPIO line {re.escape(name)!r}: ENOANO$"
  60. ):
  61. cc1101._gpio.GPIOLine.find(name.encode())
  62. def test_line_find(libgpiod_mock):
  63. libgpiod_mock.gpiod_line_find.return_value = 21
  64. line = cc1101._gpio.GPIOLine.find(b"GPIO24")
  65. libgpiod_mock.gpiod_line_find.assert_called_once_with(b"GPIO24")
  66. assert isinstance(line, cc1101._gpio.GPIOLine)
  67. assert line._pointer.value == 21
  68. def test_line_release(libgpiod_mock):
  69. line = cc1101._gpio.GPIOLine(42)
  70. del line
  71. libgpiod_mock.gpiod_line_close_chip.assert_called_once_with(42)
  72. @pytest.mark.parametrize("consumer", (b"CC1101 GDO0", b"test"))
  73. @pytest.mark.parametrize("timeout_seconds", (21.42, 0.1234))
  74. @pytest.mark.parametrize("reached_timeout", (False, True))
  75. def test_line_wait_for_rising_edge(
  76. libgpiod_mock, consumer: bytes, timeout_seconds: float, reached_timeout: bool
  77. ):
  78. pointer = ctypes.c_void_p(1234)
  79. line = cc1101._gpio.GPIOLine(pointer=pointer)
  80. libgpiod_mock.gpiod_line_request_rising_edge_events.return_value = 0
  81. libgpiod_mock.gpiod_line_event_wait.return_value = 0 if reached_timeout else 1
  82. event_occured = line.wait_for_rising_edge(
  83. consumer=consumer, timeout=datetime.timedelta(seconds=timeout_seconds)
  84. )
  85. assert event_occured is not reached_timeout
  86. libgpiod_mock.gpiod_line_request_rising_edge_events.assert_called_once_with(
  87. pointer, consumer
  88. )
  89. libgpiod_mock.gpiod_line_event_wait.assert_called_once()
  90. wait_args, wait_kwargs = libgpiod_mock.gpiod_line_event_wait.call_args
  91. assert wait_args[0] == pointer
  92. assert (
  93. wait_args[1].contents.tv_sec + wait_args[1].contents.tv_nsec / 10**9
  94. ) == pytest.approx(timeout_seconds, abs=1e-10)
  95. assert not wait_args[2:]
  96. assert not wait_kwargs
  97. libgpiod_mock.gpiod_line_release.assert_called_once_with(pointer)
  98. @pytest.mark.parametrize("consumer", (b"CC1101 GDO0",))
  99. @pytest.mark.parametrize("timeout_seconds", (21.42,))
  100. def test_line_wait_for_rising_edge_busy(
  101. libgpiod_mock, consumer: bytes, timeout_seconds: float
  102. ):
  103. pointer = ctypes.c_void_p(1234)
  104. line = cc1101._gpio.GPIOLine(pointer=pointer)
  105. libgpiod_mock.gpiod_line_request_rising_edge_events.return_value = -1
  106. ctypes.set_errno(errno.EBUSY)
  107. with pytest.raises(
  108. OSError,
  109. match=r"^Request for rising edge event notifications failed \(EBUSY\)."
  110. r"\nBlocked by another process\?$",
  111. ):
  112. line.wait_for_rising_edge(
  113. consumer=consumer, timeout=datetime.timedelta(seconds=timeout_seconds)
  114. )
  115. def test_line_wait_for_rising_edge_wait_failed(libgpiod_mock) -> None:
  116. line = cc1101._gpio.GPIOLine(pointer=ctypes.c_void_p(42 // 2))
  117. libgpiod_mock.gpiod_line_request_rising_edge_events.return_value = 0
  118. # "0 if wait timed out, -1 if an error occurred, 1 if an event occurred."
  119. libgpiod_mock.gpiod_line_event_wait.return_value = -1
  120. with pytest.raises(
  121. OSError, match="^Failed to wait for rising edge event notification.$"
  122. ):
  123. line.wait_for_rising_edge(
  124. consumer=b"test", timeout=datetime.timedelta(seconds=1)
  125. )