Lines 1-131
Link Here
|
1 |
This was part of a pull request that has been merged upstream. Most likely |
|
|
2 |
this patch can be removed on the next release of python-fido2. |
3 |
|
4 |
See https://github.com/Yubico/python-fido2/pull/64 and |
5 |
https://github.com/Yubico/python-fido2/commit/19c86d5459931b8a76d1adc76420a8a1e0c0cf2e |
6 |
|
7 |
--- test/_pyu2f/freebsd_test.py.orig 2019-09-13 11:01:05 UTC |
8 |
+++ test/_pyu2f/freebsd_test.py |
9 |
@@ -0,0 +1,122 @@ |
10 |
+# Copyright 2016 Google Inc. All Rights Reserved. |
11 |
+# |
12 |
+# Licensed under the Apache License, Version 2.0 (the "License"); |
13 |
+# you may not use this file except in compliance with the License. |
14 |
+# You may obtain a copy of the License at |
15 |
+# |
16 |
+# http://www.apache.org/licenses/LICENSE-2.0 |
17 |
+# |
18 |
+# Unless required by applicable law or agreed to in writing, software |
19 |
+# distributed under the License is distributed on an "AS IS" BASIS, |
20 |
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
21 |
+# See the License for the specific language governing permissions and |
22 |
+# limitations under the License. |
23 |
+ |
24 |
+"""Tests for _pyu2f.hid.freebsd.""" |
25 |
+ |
26 |
+import base64 |
27 |
+import os |
28 |
+import sys |
29 |
+ |
30 |
+import six |
31 |
+from six.moves import builtins |
32 |
+from mock import patch |
33 |
+ |
34 |
+if sys.platform.startswith('freebsd'): |
35 |
+ from fido2._pyu2f import freebsd |
36 |
+ |
37 |
+if sys.version_info[:2] < (2, 7): |
38 |
+ import unittest2 as unittest # pylint: disable=g-import-not-at-top |
39 |
+else: |
40 |
+ import unittest # pylint: disable=g-import-not-at-top |
41 |
+ |
42 |
+ |
43 |
+# These are base64 encoded report descriptors of a Yubico token |
44 |
+# and a Logitech keyboard. |
45 |
+YUBICO_RD = 'BtDxCQGhAQkgFQAm/wB1CJVAgQIJIRUAJv8AdQiVQJECwA==' |
46 |
+KEYBOARD_RD = ( |
47 |
+ 'BQEJAqEBCQGhAAUJGQEpBRUAJQGVBXUBgQKVAXUDgQEFAQkwCTEJOBWBJX91CJUDgQbAwA==') |
48 |
+ |
49 |
+ |
50 |
+class FakeUhidFreeBSDModule(): |
51 |
+ def enumerate(self): |
52 |
+ return [{'device': 'uhid0', |
53 |
+ 'path': '/dev/uhid0', |
54 |
+ 'vendor_id': 0x046d, |
55 |
+ 'product_id': 0xc31c, |
56 |
+ 'product_desc': 'Logitech Keyboard'}, |
57 |
+ {'device': 'uhid1', |
58 |
+ 'path': '/dev/uhid1', |
59 |
+ 'vendor_id': 0x1050, |
60 |
+ 'product_id': 0x0407, |
61 |
+ 'product_desc': 'Yubico U2F'}] |
62 |
+ |
63 |
+ def get_report_data(self, fd, unused_report_id): |
64 |
+ if fd: |
65 |
+ return base64.b64decode(YUBICO_RD) |
66 |
+ else: |
67 |
+ return base64.b64decode(KEYBOARD_RD) |
68 |
+ |
69 |
+ |
70 |
+class FakeOsModule(): |
71 |
+ O_RDONLY = os.O_RDONLY |
72 |
+ O_RDWR = os.O_RDWR |
73 |
+ path = os.path |
74 |
+ |
75 |
+ data_written = None |
76 |
+ data_to_return = None |
77 |
+ |
78 |
+ def open(self, path, unused_opts): # pylint: disable=invalid-name |
79 |
+ if path.find('uhid1') >= 0: |
80 |
+ return 1 # fd == 1 => magic number to return Yubikey RD below |
81 |
+ else: |
82 |
+ return 0 |
83 |
+ |
84 |
+ def write(self, unused_dev, data): # pylint: disable=invalid-name |
85 |
+ self.data_written = data |
86 |
+ |
87 |
+ def read(self, unused_dev, unused_length): # pylint: disable=invalid-name |
88 |
+ return self.data_to_return |
89 |
+ |
90 |
+ def close(self, unused_dev): # pylint: disable=invalid-name |
91 |
+ pass |
92 |
+ |
93 |
+ |
94 |
+@unittest.skipIf(not sys.platform.startswith('freebsd'), |
95 |
+ 'FreeBSD specific test case') |
96 |
+class FreeBSDTest(unittest.TestCase): |
97 |
+ @patch('fido2._pyu2f.freebsd.os', FakeOsModule()) |
98 |
+ @patch('fido2._pyu2f.freebsd.uhid_freebsd', FakeUhidFreeBSDModule()) |
99 |
+ def testCallEnumerate(self): |
100 |
+ devs = list(freebsd.FreeBSDHidDevice.Enumerate()) |
101 |
+ devs = sorted(devs, key=lambda k: k['vendor_id']) |
102 |
+ |
103 |
+ self.assertEqual(len(devs), 2) |
104 |
+ self.assertEqual(devs[0]['vendor_id'], 0x046d) |
105 |
+ self.assertEqual(devs[0]['product_id'], 0xc31c) |
106 |
+ self.assertEqual(devs[1]['vendor_id'], 0x1050) |
107 |
+ self.assertEqual(devs[1]['product_id'], 0x0407) |
108 |
+ self.assertEqual(devs[1]['usage_page'], 0xf1d0) |
109 |
+ self.assertEqual(devs[1]['usage'], 1) |
110 |
+ |
111 |
+ @patch('fido2._pyu2f.freebsd.uhid_freebsd', FakeUhidFreeBSDModule()) |
112 |
+ def testCallOpen(self): |
113 |
+ fake_os = FakeOsModule() |
114 |
+ with patch('fido2._pyu2f.linux.os', fake_os): |
115 |
+ with patch('fido2._pyu2f.freebsd.os', fake_os): |
116 |
+ dev = freebsd.FreeBSDHidDevice('/dev/uhid1') |
117 |
+ self.assertEqual(dev.GetInReportDataLength(), 64) |
118 |
+ self.assertEqual(dev.GetOutReportDataLength(), 64) |
119 |
+ |
120 |
+ dev.Write(list(range(0, 64))) |
121 |
+ # The HidDevice implementation prepends one zero-byte-packet |
122 |
+ # (64 bytes) representing the report ID + padding |
123 |
+ self.assertEqual(list(six.iterbytes(fake_os.data_written)), |
124 |
+ [0]*64 + list(range(0, 64))) |
125 |
+ |
126 |
+ fake_os.data_to_return = b'x' * 64 |
127 |
+ self.assertEqual(dev.Read(), [120] * 64) # chr(120) = 'x' |
128 |
+ |
129 |
+ |
130 |
+if __name__ == '__main__': |
131 |
+ unittest.main() |