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.

411 lines
14 KiB

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