|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +from u2flib_host import hid_transport |
| 4 | +from u2flib_host import exc |
| 5 | +from u2flib_host.yubicommon.compat import byte2int, int2byte |
| 6 | + |
| 7 | +try: |
| 8 | + from unittest.mock import patch |
| 9 | +except ImportError: |
| 10 | + from mock import patch |
| 11 | + |
| 12 | +class TestHIDDevice(object): |
| 13 | + def write(self, payload): |
| 14 | + self.cid = payload[1:5] |
| 15 | + self.cmd = payload[5] ^ hid_transport.TYPE_INIT |
| 16 | + self.size = (payload[6] << 8) + payload[7] |
| 17 | + self.data = list(map(int2byte, payload[8:(8 + self.size)])) |
| 18 | + |
| 19 | + def read(self, size): |
| 20 | + self.response += [0] * (hid_transport.HID_RPT_SIZE - len(self.response) + 1) |
| 21 | + types = list(map(type, self.response)) |
| 22 | + return self.response |
| 23 | + |
| 24 | + def close(self): |
| 25 | + return None |
| 26 | + |
| 27 | + |
| 28 | +class HIDDeviceTest(unittest.TestCase): |
| 29 | + @classmethod |
| 30 | + def build_response(cls, cid, cmd, data): |
| 31 | + size = len(data) |
| 32 | + size_low = size & 0xff |
| 33 | + size_high = (size >> 8) & 0xff |
| 34 | + response = list(map(byte2int, cid)) + list(map(byte2int, cmd)) + [size_high, size_low] |
| 35 | + response += list(map(byte2int, data)) |
| 36 | + return response |
| 37 | + |
| 38 | + |
| 39 | + def test_init(self): |
| 40 | + with patch.object(os, 'urandom', return_value=(b'\xab'*8)) as mock_method: |
| 41 | + hid_device = TestHIDDevice() |
| 42 | + hid_device.response = HIDDeviceTest.build_response( |
| 43 | + b'\xff'*4, |
| 44 | + b'\x86', |
| 45 | + b'\xab'*8 + b'\x01\x02\x03\x04' + b'\x01\x02\x03\x04\x05' |
| 46 | + ) |
| 47 | + |
| 48 | + dev = hid_transport.HIDDevice('/dev/null') |
| 49 | + dev.handle = hid_device |
| 50 | + dev.init() |
| 51 | + self.assertEqual(dev.capabilities, 0x05) |
| 52 | + |
| 53 | + def test_init_invalid_nonce(self): |
| 54 | + with patch.object(os, 'urandom', return_value=(b'\xab'*8)) as mock_method: |
| 55 | + hid_device = TestHIDDevice() |
| 56 | + hid_device.response = HIDDeviceTest.build_response( |
| 57 | + b'\xff'*4, |
| 58 | + b'\x86', |
| 59 | + b'\x00'*8 + b'\x01\x02\x03\x04' + b'\x01\x02\x03\x04\x05' |
| 60 | + ) |
| 61 | + |
| 62 | + dev = hid_transport.HIDDevice('/dev/null') |
| 63 | + dev.handle = hid_device |
| 64 | + with self.assertRaises(exc.DeviceError) as context: |
| 65 | + dev.init() |
| 66 | + self.assertTrue('Wrong INIT response from device' in context.exception) |
| 67 | + |
| 68 | + def test_init_invalid_length(self): |
| 69 | + with patch.object(os, 'urandom', return_value=(b'\xab'*8)) as mock_method: |
| 70 | + hid_device = TestHIDDevice() |
| 71 | + hid_device.response = HIDDeviceTest.build_response( |
| 72 | + b'\xff'*4, |
| 73 | + b'\x86', |
| 74 | + b'\xab'*8 + b'\x01\x02\x03\x04' + b'\x01\x02\x03\x04' |
| 75 | + ) |
| 76 | + |
| 77 | + dev = hid_transport.HIDDevice('/dev/null') |
| 78 | + dev.handle = hid_device |
| 79 | + |
| 80 | + with self.assertRaises(exc.DeviceError) as context: |
| 81 | + dev.init() |
| 82 | + self.assertTrue('Wrong INIT response from device' in context.exception) |
| 83 | + |
| 84 | + def test_ctap2_enabled(self): |
| 85 | + dev = hid_transport.HIDDevice('/dev/null') |
| 86 | + dev.capabilities = 0x01 |
| 87 | + self.assertFalse(dev.ctap2_enabled()) |
| 88 | + dev.capabilities = 0x04 |
| 89 | + self.assertTrue(dev.ctap2_enabled()) |
0 commit comments