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.

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