A clone of: https://github.com/nutechsoftware/alarmdecoder This is requires as they dropped support for older firmware releases w/o building in backward compatibility code, and they had previously hardcoded pyserial to a python2 only version.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
13 KiB

  1. from unittest import TestCase
  2. from mock import Mock, MagicMock, patch
  3. from serial import Serial, SerialException
  4. from pyftdi.pyftdi.ftdi import Ftdi, FtdiError
  5. from usb.core import USBError, Device as USBCoreDevice
  6. import socket
  7. import time
  8. from OpenSSL import SSL, crypto
  9. from ..devices import USBDevice, SerialDevice, SocketDevice
  10. from ..util import NoDeviceError, CommError, TimeoutError
  11. class TestUSBDevice(TestCase):
  12. def setUp(self):
  13. self._device = USBDevice()
  14. self._device._device = Mock(spec=Ftdi)
  15. self._device._device.usb_dev = Mock(spec=USBCoreDevice)
  16. self._device._device.usb_dev.bus = 0
  17. self._device._device.usb_dev.address = 0
  18. self._attached = False
  19. self._detached = False
  20. def tearDown(self):
  21. self._device.close()
  22. def attached_event(self, sender, *args, **kwargs):
  23. self._attached = True
  24. def detached_event(self, sender, *args, **kwargs):
  25. self._detached = True
  26. def test_find_default_param(self):
  27. with patch.object(Ftdi, 'find_all', return_value=[(0, 0, 'AD2', 1, 'AD2')]):
  28. device = USBDevice.find()
  29. self.assertEquals(device.interface, 'AD2')
  30. def test_find_with_param(self):
  31. with patch.object(Ftdi, 'find_all', return_value=[(0, 0, 'AD2-1', 1, 'AD2'), (0, 0, 'AD2-2', 1, 'AD2')]):
  32. device = USBDevice.find((0, 0, 'AD2-1', 1, 'AD2'))
  33. self.assertEquals(device.interface, 'AD2-1')
  34. device = USBDevice.find((0, 0, 'AD2-2', 1, 'AD2'))
  35. self.assertEquals(device.interface, 'AD2-2')
  36. def test_events(self):
  37. self.assertEquals(self._attached, False)
  38. self.assertEquals(self._detached, False)
  39. # this is ugly, but it works.
  40. with patch.object(USBDevice, 'find_all', return_value=[(0, 0, 'AD2-1', 1, 'AD2'), (0, 0, 'AD2-2', 1, 'AD2')]):
  41. USBDevice.start_detection(on_attached=self.attached_event, on_detached=self.detached_event)
  42. with patch.object(USBDevice, 'find_all', return_value=[(0, 0, 'AD2-2', 1, 'AD2')]):
  43. USBDevice.find_all()
  44. time.sleep(1)
  45. USBDevice.stop_detection()
  46. self.assertEquals(self._attached, True)
  47. self.assertEquals(self._detached, True)
  48. def test_find_all(self):
  49. with patch.object(USBDevice, 'find_all', return_value=[]) as mock:
  50. devices = USBDevice.find_all()
  51. self.assertEquals(devices, [])
  52. def test_find_all_exception(self):
  53. with patch.object(Ftdi, 'find_all', side_effect=[USBError('testing'), FtdiError]) as mock:
  54. with self.assertRaises(CommError):
  55. devices = USBDevice.find_all()
  56. with self.assertRaises(CommError):
  57. devices = USBDevice.find_all()
  58. def test_interface_serial_number(self):
  59. self._device.interface = 'AD2USB'
  60. self.assertEquals(self._device.interface, 'AD2USB')
  61. self.assertEquals(self._device.serial_number, 'AD2USB')
  62. self.assertEquals(self._device._device_number, 0)
  63. def test_interface_index(self):
  64. self._device.interface = 1
  65. self.assertEquals(self._device.interface, 1)
  66. self.assertEquals(self._device.serial_number, None)
  67. self.assertEquals(self._device._device_number, 1)
  68. def test_open(self):
  69. self._device.interface = 'AD2USB'
  70. with patch.object(self._device._device, 'open') as mock:
  71. self._device.open(no_reader_thread=True)
  72. mock.assert_any_calls()
  73. def test_open_failed(self):
  74. self._device.interface = 'AD2USB'
  75. with patch.object(self._device._device, 'open', side_effect=[USBError('testing'), FtdiError]):
  76. with self.assertRaises(NoDeviceError):
  77. self._device.open(no_reader_thread=True)
  78. with self.assertRaises(NoDeviceError):
  79. self._device.open(no_reader_thread=True)
  80. def test_write(self):
  81. self._device.interface = 'AD2USB'
  82. self._device.open(no_reader_thread=True)
  83. with patch.object(self._device._device, 'write_data') as mock:
  84. self._device.write('test')
  85. mock.assert_called_with('test')
  86. def test_write_exception(self):
  87. with patch.object(self._device._device, 'write_data', side_effect=FtdiError):
  88. with self.assertRaises(CommError):
  89. self._device.write('test')
  90. def test_read(self):
  91. self._device.interface = 'AD2USB'
  92. self._device.open(no_reader_thread=True)
  93. with patch.object(self._device._device, 'read_data') as mock:
  94. self._device.read()
  95. mock.assert_called_with(1)
  96. def test_read_exception(self):
  97. with patch.object(self._device._device, 'read_data', side_effect=[USBError('testing'), FtdiError]):
  98. with self.assertRaises(CommError):
  99. self._device.read()
  100. with self.assertRaises(CommError):
  101. self._device.read()
  102. def test_read_line(self):
  103. with patch.object(self._device._device, 'read_data', side_effect=list("testing\r\n")):
  104. ret = None
  105. try:
  106. ret = self._device.read_line()
  107. except StopIteration:
  108. pass
  109. self.assertEquals(ret, "testing")
  110. def test_read_line_timeout(self):
  111. with patch.object(self._device._device, 'read_data', return_value='a') as mock:
  112. with self.assertRaises(TimeoutError):
  113. self._device.read_line(timeout=0.1)
  114. self.assertIn('a', self._device._buffer)
  115. def test_read_line_exception(self):
  116. with patch.object(self._device._device, 'read_data', side_effect=[USBError('testing'), FtdiError]):
  117. with self.assertRaises(CommError):
  118. self._device.read_line()
  119. with self.assertRaises(CommError):
  120. self._device.read_line()
  121. class TestSerialDevice(TestCase):
  122. def setUp(self):
  123. self._device = SerialDevice()
  124. self._device._device = Mock(spec=Serial)
  125. self._device._device.open = Mock()
  126. def tearDown(self):
  127. self._device.close()
  128. def test_open(self):
  129. self._device.interface = '/dev/ttyS0'
  130. with patch.object(self._device._device, 'open') as mock:
  131. self._device.open(no_reader_thread=True)
  132. mock.assert_called_with()
  133. def test_open_no_interface(self):
  134. with self.assertRaises(NoDeviceError):
  135. self._device.open(no_reader_thread=True)
  136. self.assertFalse(self._device._running)
  137. def test_open_failed(self):
  138. self._device.interface = '/dev/ttyS0'
  139. with patch.object(self._device._device, 'open', side_effect=[SerialException, ValueError]):
  140. with self.assertRaises(NoDeviceError):
  141. self._device.open(no_reader_thread=True)
  142. with self.assertRaises(NoDeviceError):
  143. self._device.open(no_reader_thread=True)
  144. def test_write(self):
  145. self._device.interface = '/dev/ttyS0'
  146. self._device.open(no_reader_thread=True)
  147. with patch.object(self._device._device, 'write') as mock:
  148. self._device.write('test')
  149. mock.assert_called_with('test')
  150. def test_write_exception(self):
  151. with patch.object(self._device._device, 'write', side_effect=SerialException):
  152. with self.assertRaises(CommError):
  153. self._device.write('test')
  154. def test_read(self):
  155. self._device.interface = '/dev/ttyS0'
  156. self._device.open(no_reader_thread=True)
  157. with patch.object(self._device._device, 'read') as mock:
  158. self._device.read()
  159. mock.assert_called_with(1)
  160. def test_read_exception(self):
  161. with patch.object(self._device._device, 'read', side_effect=SerialException):
  162. with self.assertRaises(CommError):
  163. self._device.read()
  164. def test_read_line(self):
  165. with patch.object(self._device._device, 'read', side_effect=list("testing\r\n")):
  166. ret = None
  167. try:
  168. ret = self._device.read_line()
  169. except StopIteration:
  170. pass
  171. self.assertEquals(ret, "testing")
  172. def test_read_line_timeout(self):
  173. with patch.object(self._device._device, 'read', return_value='a') as mock:
  174. with self.assertRaises(TimeoutError):
  175. self._device.read_line(timeout=0.1)
  176. self.assertIn('a', self._device._buffer)
  177. def test_read_line_exception(self):
  178. with patch.object(self._device._device, 'read', side_effect=[OSError, SerialException]):
  179. with self.assertRaises(CommError):
  180. self._device.read_line()
  181. with self.assertRaises(CommError):
  182. self._device.read_line()
  183. class TestSocketDevice(TestCase):
  184. def setUp(self):
  185. self._device = SocketDevice()
  186. self._device._device = Mock(spec=socket.socket)
  187. def tearDown(self):
  188. self._device.close()
  189. def test_open(self):
  190. with patch.object(socket.socket, '__init__', return_value=None):
  191. with patch.object(socket.socket, 'connect', return_value=None) as mock:
  192. self._device.open(no_reader_thread=True)
  193. mock.assert_called_with(self._device.interface)
  194. def test_open_no_interface(self):
  195. with self.assertRaises(NoDeviceError):
  196. self._device.open(no_reader_thread=True)
  197. self.assertFalse(self._device._running)
  198. def test_open_failed(self):
  199. with patch.object(self._device._device, 'connect', side_effect=socket.error):
  200. with self.assertRaises(NoDeviceError):
  201. self._device.open(no_reader_thread=True)
  202. def test_write(self):
  203. with patch.object(socket.socket, '__init__', return_value=None):
  204. with patch.object(socket.socket, 'connect', return_value=None):
  205. self._device.open(no_reader_thread=True)
  206. with patch.object(socket.socket, 'send') as mock:
  207. self._device.write('test')
  208. mock.assert_called_with('test')
  209. def test_write_exception(self):
  210. with patch.object(self._device._device, 'send', side_effect=[SSL.Error, socket.error]):
  211. with self.assertRaises(CommError):
  212. self._device.write('test')
  213. def test_read(self):
  214. with patch.object(socket.socket, '__init__', return_value=None):
  215. with patch.object(socket.socket, 'connect', return_value=None):
  216. self._device.open(no_reader_thread=True)
  217. with patch.object(socket.socket, 'recv') as mock:
  218. self._device.read()
  219. mock.assert_called_with(1)
  220. def test_read_exception(self):
  221. with patch.object(self._device._device, 'recv', side_effect=socket.error):
  222. with self.assertRaises(CommError):
  223. self._device.read()
  224. def test_read_line(self):
  225. with patch.object(self._device._device, 'recv', side_effect=list("testing\r\n")):
  226. ret = None
  227. try:
  228. ret = self._device.read_line()
  229. except StopIteration:
  230. pass
  231. self.assertEquals(ret, "testing")
  232. def test_read_line_timeout(self):
  233. with patch.object(self._device._device, 'recv', return_value='a') as mock:
  234. with self.assertRaises(TimeoutError):
  235. self._device.read_line(timeout=0.1)
  236. self.assertIn('a', self._device._buffer)
  237. def test_read_line_exception(self):
  238. with patch.object(self._device._device, 'recv', side_effect=socket.error):
  239. with self.assertRaises(CommError):
  240. self._device.read_line()
  241. with self.assertRaises(CommError):
  242. self._device.read_line()
  243. def test_ssl(self):
  244. ssl_key = crypto.PKey()
  245. ssl_key.generate_key(crypto.TYPE_RSA, 2048)
  246. ssl_cert = crypto.X509()
  247. ssl_cert.set_pubkey(ssl_key)
  248. ssl_ca_key = crypto.PKey()
  249. ssl_ca_key.generate_key(crypto.TYPE_RSA, 2048)
  250. ssl_ca_cert = crypto.X509()
  251. ssl_ca_cert.set_pubkey(ssl_ca_key)
  252. self._device.ssl = True
  253. self._device.ssl_key = ssl_key
  254. self._device.ssl_certificate = ssl_cert
  255. self._device.ssl_ca = ssl_ca_cert
  256. # ..there has to be a better way..
  257. with patch.object(socket.socket, '__init__', return_value=None):
  258. with patch.object(socket.socket, 'connect', return_value=None) as mock:
  259. with patch.object(socket.socket, '_sock'):
  260. with patch.object(socket.socket, 'fileno', return_value=1):
  261. self._device.open(no_reader_thread=True)
  262. mock.assert_called_with(self._device.interface)
  263. self.assertIsInstance(self._device._device, SSL.Connection)
  264. def test_ssl_exception(self):
  265. self._device.ssl = True
  266. self._device.ssl_key = 'None'
  267. self._device.ssl_certificate = 'None'
  268. self._device.ssl_ca = 'None'
  269. # ..there has to be a better way..
  270. with patch.object(socket.socket, '__init__', return_value=None):
  271. with patch.object(socket.socket, 'connect', return_value=None) as mock:
  272. with patch.object(socket.socket, '_sock'):
  273. with patch.object(socket.socket, 'fileno', return_value=1):
  274. with self.assertRaises(CommError):
  275. self._device.open(no_reader_thread=True)