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