Home | History | Annotate | Download | only in usb_gadget
      1 #!/usr/bin/python
      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.
      5 
      6 import unittest
      7 
      8 import hid_constants
      9 import usb_descriptors
     10 
     11 
     12 class DescriptorWithField(usb_descriptors.Descriptor):
     13   pass
     14 
     15 DescriptorWithField.AddField('bField', 'B')
     16 
     17 
     18 class DescriptorWithDefault(usb_descriptors.Descriptor):
     19   pass
     20 
     21 DescriptorWithDefault.AddField('bDefault', 'B', default=42)
     22 
     23 
     24 class DescriptorWithFixed(usb_descriptors.Descriptor):
     25   pass
     26 
     27 DescriptorWithFixed.AddFixedField('bFixed', 'B', 42)
     28 
     29 
     30 class DescriptorWithComputed(usb_descriptors.Descriptor):
     31 
     32   @property
     33   def foo(self):
     34     return 42
     35 
     36 DescriptorWithComputed.AddComputedField('bComputed', 'B', 'foo')
     37 
     38 
     39 class DescriptorWithDescriptors(usb_descriptors.DescriptorContainer):
     40   pass
     41 
     42 DescriptorWithDescriptors.AddField('bType', 'B')
     43 
     44 
     45 class DescriptorTest(unittest.TestCase):
     46 
     47   def test_default(self):
     48     obj = DescriptorWithDefault()
     49     self.assertEquals(obj.bDefault, 42)
     50 
     51   def test_change_default(self):
     52     obj = DescriptorWithDefault()
     53     obj.bDefault = 1
     54     self.assertEquals(obj.bDefault, 1)
     55 
     56   def test_override_default(self):
     57     obj = DescriptorWithDefault(bDefault=56)
     58     self.assertEquals(obj.bDefault, 56)
     59 
     60   def test_fixed(self):
     61     obj = DescriptorWithFixed()
     62     self.assertEquals(obj.bFixed, 42)
     63 
     64   def test_set_fixed(self):
     65     with self.assertRaises(RuntimeError):
     66       DescriptorWithFixed(bFixed=1)
     67 
     68   def test_modify_fixed(self):
     69     obj = DescriptorWithFixed()
     70     with self.assertRaises(RuntimeError):
     71       obj.bFixed = 1
     72 
     73   def test_computed(self):
     74     obj = DescriptorWithComputed()
     75     self.assertEquals(obj.bComputed, 42)
     76 
     77   def test_set_computed(self):
     78     with self.assertRaises(RuntimeError):
     79       DescriptorWithComputed(bComputed=1)
     80 
     81   def test_modify_computed(self):
     82     obj = DescriptorWithComputed()
     83     with self.assertRaises(RuntimeError):
     84       obj.bComputed = 1
     85 
     86   def test_unexpected(self):
     87     with self.assertRaisesRegexp(TypeError, 'Unexpected'):
     88       DescriptorWithField(bUnexpected=1)
     89 
     90   def test_missing(self):
     91     with self.assertRaisesRegexp(TypeError, 'Missing'):
     92       DescriptorWithField()
     93 
     94   def test_size(self):
     95     obj = DescriptorWithField(bField=42)
     96     self.assertEquals(obj.struct_size, 1)
     97     self.assertEquals(obj.total_size, 1)
     98 
     99   def test_encode(self):
    100     obj = DescriptorWithField(bField=0xff)
    101     self.assertEquals(obj.Encode(), '\xff')
    102 
    103   def test_string(self):
    104     obj = DescriptorWithField(bField=42)
    105     string = str(obj)
    106     self.assertIn('bField', string)
    107     self.assertIn('42', string)
    108 
    109   def test_container(self):
    110     parent = DescriptorWithDescriptors(bType=0)
    111     child1 = DescriptorWithField(bField=1)
    112     parent.Add(child1)
    113     child2 = DescriptorWithField(bField=2)
    114     parent.Add(child2)
    115     self.assertEquals(parent.total_size, 3)
    116     self.assertEquals(parent.Encode(), '\x00\x01\x02')
    117     string = str(parent)
    118     self.assertIn('bType', string)
    119     self.assertIn('bField', string)
    120 
    121 
    122 class TestUsbDescriptors(unittest.TestCase):
    123 
    124   def test_device_descriptor(self):
    125     device_desc = usb_descriptors.DeviceDescriptor(
    126         idVendor=0xDEAD,
    127         idProduct=0xBEEF,
    128         bcdDevice=0x0100,
    129         bNumConfigurations=1)
    130     self.assertEquals(
    131         device_desc.Encode(),
    132         '\x12\x01\x00\x02\x00\x00\x00\x40\xAD\xDE\xEF\xBE\x00\x01\x00\x00\x00'
    133         '\x01')
    134 
    135   def test_unique_interfaces(self):
    136     interface_desc1 = usb_descriptors.InterfaceDescriptor(bInterfaceNumber=1)
    137     interface_desc2 = usb_descriptors.InterfaceDescriptor(bInterfaceNumber=1,
    138                                                           bAlternateSetting=1)
    139     interface_desc3 = usb_descriptors.InterfaceDescriptor(bInterfaceNumber=1)
    140 
    141     configuration_desc = usb_descriptors.ConfigurationDescriptor(
    142         bmAttributes=0xC0,
    143         MaxPower=100)
    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)
    148 
    149   def test_unique_endpoints(self):
    150     endpoint_desc1 = usb_descriptors.EndpointDescriptor(
    151         bEndpointAddress=0x01,
    152         bmAttributes=0x02,
    153         wMaxPacketSize=64,
    154         bInterval=1)
    155     endpoint_desc2 = usb_descriptors.EndpointDescriptor(
    156         bEndpointAddress=0x81,
    157         bmAttributes=0x02,
    158         wMaxPacketSize=64,
    159         bInterval=1)
    160     endpoint_desc3 = usb_descriptors.EndpointDescriptor(
    161         bEndpointAddress=0x01,
    162         bmAttributes=0x01,
    163         wMaxPacketSize=32,
    164         bInterval=10)
    165 
    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)
    171 
    172   def test_configuration_descriptor(self):
    173     endpoint_desc = usb_descriptors.EndpointDescriptor(
    174         bEndpointAddress=0x01,
    175         bmAttributes=0x02,
    176         wMaxPacketSize=64,
    177         bInterval=1)
    178     encoded_endpoint = '\x07\x05\x01\x02\x40\x00\x01'
    179     self.assertEquals(endpoint_desc.Encode(), encoded_endpoint)
    180 
    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' +
    185                          encoded_endpoint)
    186     self.assertEquals(interface_desc.Encode(), encoded_interface)
    187 
    188     configuration_desc = usb_descriptors.ConfigurationDescriptor(
    189         bmAttributes=0xC0,
    190         MaxPower=100)
    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' +
    194                              encoded_interface)
    195     self.assertEquals(configuration_desc.Encode(), encoded_configuration)
    196 
    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)
    203 
    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)
    211 
    212 
    213 if __name__ == '__main__':
    214   unittest.main()
    215