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.

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