2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
12 class DescriptorWithField(usb_descriptors
.Descriptor
):
15 DescriptorWithField
.AddField('bField', 'B')
18 class DescriptorWithDefault(usb_descriptors
.Descriptor
):
21 DescriptorWithDefault
.AddField('bDefault', 'B', default
=42)
24 class DescriptorWithFixed(usb_descriptors
.Descriptor
):
27 DescriptorWithFixed
.AddFixedField('bFixed', 'B', 42)
30 class DescriptorWithComputed(usb_descriptors
.Descriptor
):
36 DescriptorWithComputed
.AddComputedField('bComputed', 'B', 'foo')
39 class DescriptorWithDescriptors(usb_descriptors
.DescriptorContainer
):
42 DescriptorWithDescriptors
.AddField('bType', 'B')
45 class DescriptorTest(unittest
.TestCase
):
47 def test_default(self
):
48 obj
= DescriptorWithDefault()
49 self
.assertEquals(obj
.bDefault
, 42)
51 def test_change_default(self
):
52 obj
= DescriptorWithDefault()
54 self
.assertEquals(obj
.bDefault
, 1)
56 def test_override_default(self
):
57 obj
= DescriptorWithDefault(bDefault
=56)
58 self
.assertEquals(obj
.bDefault
, 56)
61 obj
= DescriptorWithFixed()
62 self
.assertEquals(obj
.bFixed
, 42)
64 def test_set_fixed(self
):
65 with self
.assertRaises(RuntimeError):
66 DescriptorWithFixed(bFixed
=1)
68 def test_modify_fixed(self
):
69 obj
= DescriptorWithFixed()
70 with self
.assertRaises(RuntimeError):
73 def test_computed(self
):
74 obj
= DescriptorWithComputed()
75 self
.assertEquals(obj
.bComputed
, 42)
77 def test_set_computed(self
):
78 with self
.assertRaises(RuntimeError):
79 DescriptorWithComputed(bComputed
=1)
81 def test_modify_computed(self
):
82 obj
= DescriptorWithComputed()
83 with self
.assertRaises(RuntimeError):
86 def test_unexpected(self
):
87 with self
.assertRaisesRegexp(TypeError, 'Unexpected'):
88 DescriptorWithField(bUnexpected
=1)
90 def test_missing(self
):
91 with self
.assertRaisesRegexp(TypeError, 'Missing'):
95 obj
= DescriptorWithField(bField
=42)
96 self
.assertEquals(obj
.struct_size
, 1)
97 self
.assertEquals(obj
.total_size
, 1)
99 def test_encode(self
):
100 obj
= DescriptorWithField(bField
=0xff)
101 self
.assertEquals(obj
.Encode(), '\xff')
103 def test_string(self
):
104 obj
= DescriptorWithField(bField
=42)
106 self
.assertIn('bField', string
)
107 self
.assertIn('42', string
)
109 def test_container(self
):
110 parent
= DescriptorWithDescriptors(bType
=0)
111 child1
= DescriptorWithField(bField
=1)
113 child2
= DescriptorWithField(bField
=2)
115 self
.assertEquals(parent
.total_size
, 3)
116 self
.assertEquals(parent
.Encode(), '\x00\x01\x02')
118 self
.assertIn('bType', string
)
119 self
.assertIn('bField', string
)
122 class TestUsbDescriptors(unittest
.TestCase
):
124 def test_device_descriptor(self
):
125 device_desc
= usb_descriptors
.DeviceDescriptor(
129 bNumConfigurations
=1)
131 device_desc
.Encode(),
132 '\x12\x01\x00\x02\x00\x00\x00\x40\xAD\xDE\xEF\xBE\x00\x01\x00\x00\x00'
135 def test_unique_interfaces(self
):
136 interface_desc1
= usb_descriptors
.InterfaceDescriptor(bInterfaceNumber
=1)
137 interface_desc2
= usb_descriptors
.InterfaceDescriptor(bInterfaceNumber
=1,
139 interface_desc3
= usb_descriptors
.InterfaceDescriptor(bInterfaceNumber
=1)
141 configuration_desc
= usb_descriptors
.ConfigurationDescriptor(
144 configuration_desc
.AddInterface(interface_desc1
)
145 configuration_desc
.AddInterface(interface_desc2
)
146 with self
.assertRaisesRegexp(RuntimeError, r
'Interface 1 \(alternate 0\)'):
147 configuration_desc
.AddInterface(interface_desc3
)
149 def test_unique_endpoints(self
):
150 endpoint_desc1
= usb_descriptors
.EndpointDescriptor(
151 bEndpointAddress
=0x01,
155 endpoint_desc2
= usb_descriptors
.EndpointDescriptor(
156 bEndpointAddress
=0x81,
160 endpoint_desc3
= usb_descriptors
.EndpointDescriptor(
161 bEndpointAddress
=0x01,
166 interface_desc
= usb_descriptors
.InterfaceDescriptor(bInterfaceNumber
=1)
167 interface_desc
.AddEndpoint(endpoint_desc1
)
168 interface_desc
.AddEndpoint(endpoint_desc2
)
169 with self
.assertRaisesRegexp(RuntimeError, 'Endpoint 0x01 already defined'):
170 interface_desc
.AddEndpoint(endpoint_desc3
)
172 def test_configuration_descriptor(self
):
173 endpoint_desc
= usb_descriptors
.EndpointDescriptor(
174 bEndpointAddress
=0x01,
178 encoded_endpoint
= '\x07\x05\x01\x02\x40\x00\x01'
179 self
.assertEquals(endpoint_desc
.Encode(), encoded_endpoint
)
181 interface_desc
= usb_descriptors
.InterfaceDescriptor(bInterfaceNumber
=1)
182 interface_desc
.AddEndpoint(endpoint_desc
)
183 self
.assertEquals([endpoint_desc
], interface_desc
.GetEndpoints())
184 encoded_interface
= ('\x09\x04\x01\x00\x01\xFF\xFF\xFF\x00' +
186 self
.assertEquals(interface_desc
.Encode(), encoded_interface
)
188 configuration_desc
= usb_descriptors
.ConfigurationDescriptor(
191 configuration_desc
.AddInterface(interface_desc
)
192 self
.assertEquals([interface_desc
], configuration_desc
.GetInterfaces())
193 encoded_configuration
= ('\x09\x02\x19\x00\x01\x01\x00\xC0\x64' +
195 self
.assertEquals(configuration_desc
.Encode(), encoded_configuration
)
197 def test_encode_hid_descriptor(self
):
198 hid_desc
= usb_descriptors
.HidDescriptor()
199 hid_desc
.AddDescriptor(hid_constants
.DescriptorType
.REPORT
, 0x80)
200 hid_desc
.AddDescriptor(hid_constants
.DescriptorType
.PHYSICAL
, 0x60)
201 encoded_desc
= '\x0C\x21\x11\x01\x00\x02\x22\x80\x00\x23\x60\x00'
202 self
.assertEquals(hid_desc
.Encode(), encoded_desc
)
204 def test_print_hid_descriptor(self
):
205 hid_desc
= usb_descriptors
.HidDescriptor()
206 hid_desc
.AddDescriptor(hid_constants
.DescriptorType
.REPORT
, 0x80)
207 hid_desc
.AddDescriptor(hid_constants
.DescriptorType
.PHYSICAL
, 0x60)
208 string
= str(hid_desc
)
209 self
.assertIn('0x22', string
)
210 self
.assertIn('0x23', string
)
213 if __name__
== '__main__':