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.

551 lines
20 KiB

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