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.

552 lines
20 KiB

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