test_util.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Copyright (C) 2009-2011 Wander Lairson Costa
  2. #
  3. # The following terms apply to all files associated
  4. # with the software unless explicitly disclaimed in individual files.
  5. #
  6. # The authors hereby grant permission to use, copy, modify, distribute,
  7. # and license this software and its documentation for any purpose, provided
  8. # that existing copyright notices are retained in all copies and that this
  9. # notice is included verbatim in any distributions. No written agreement,
  10. # license, or royalty fee is required for any of the authorized uses.
  11. # Modifications to this software may be copyrighted by their authors
  12. # and need not follow the licensing terms described here, provided that
  13. # the new terms are clearly indicated on the first page of each file where
  14. # they apply.
  15. #
  16. # IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
  17. # FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  18. # ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
  19. # DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
  20. # POSSIBILITY OF SUCH DAMAGE.
  21. #
  22. # THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
  23. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
  25. # IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
  26. # NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
  27. # MODIFICATIONS.
  28. import utils
  29. import unittest
  30. from usb.util import *
  31. from devinfo import *
  32. import usb.backend
  33. class _ConfigurationDescriptor(object):
  34. def __init__(self, bConfigurationValue):
  35. self.bLength = 18
  36. self.bDescriptorType = DESC_TYPE_CONFIG
  37. self.wTotalLength = 18
  38. self.bNumInterfaces = 0
  39. self.bConfigurationValue = bConfigurationValue
  40. self.iConfiguration = 0
  41. self.bmAttributes = 0xc0
  42. self.bMaxPower = 50
  43. class _DeviceDescriptor(object):
  44. def __init__(self):
  45. self.configurations = (_ConfigurationDescriptor(1), _ConfigurationDescriptor(2))
  46. self.bLength = 18
  47. self.bDescriptorType = usb.util.DESC_TYPE_DEVICE
  48. self.bcdUSB = 0x0200
  49. self.idVendor = ID_VENDOR
  50. self.idProduct = ID_PRODUCT
  51. self.bcdDevice = 0x0001
  52. self.iManufacturer = 0
  53. self.iProduct = 0
  54. self.iSerialNumber = 0
  55. self.bNumConfigurations = len(self.configurations)
  56. self.bMaxPacketSize0 = 64
  57. self.bDeviceClass = 0xff
  58. self.bDeviceSubClass = 0xff
  59. self.bDeviceProtocol = 0xff
  60. class FindDescriptorTest(unittest.TestCase):
  61. def runTest(self):
  62. d = usb.core.find(idVendor=ID_VENDOR)
  63. if d is None:
  64. return
  65. self.assertEqual(find_descriptor(d, bConfigurationValue=10), None)
  66. self.assertNotEqual(find_descriptor(d, bConfigurationValue=1), None)
  67. self.assertEqual(len(find_descriptor(d, find_all=True, bConfigurationValue=10)), 0)
  68. self.assertEqual(len(find_descriptor(d, find_all=True, bConfigurationValue=1)), 1)
  69. self.assertEqual(len(find_descriptor(d, find_all=True)), d.bNumConfigurations)
  70. self.assertEqual(find_descriptor(d, custom_match = lambda c: c.bConfigurationValue == 10), None)
  71. self.assertNotEqual(find_descriptor(d, custom_match = lambda c: c.bConfigurationValue == 1), None)
  72. self.assertEqual(len(find_descriptor(d, find_all=True, custom_match = lambda c: c.bConfigurationValue == 10)), 0)
  73. self.assertEqual(len(find_descriptor(d, find_all=True, custom_match = lambda c: c.bConfigurationValue == 1)), 1)
  74. self.assertEqual(find_descriptor(d, custom_match = lambda c: c.bConfigurationValue == 10, bLength=9), None)
  75. self.assertNotEqual(find_descriptor(d, custom_match = lambda c: c.bConfigurationValue == 1, bLength=9), None)
  76. cfg = find_descriptor(d)
  77. self.assertTrue(isinstance(cfg, usb.core.Configuration))
  78. intf = find_descriptor(cfg)
  79. self.assertTrue(isinstance(intf, usb.core.Interface))
  80. class UtilTest(unittest.TestCase):
  81. def test_endpoint_address(self):
  82. self.assertEqual(endpoint_address(EP_BULK_OUT), 0x01)
  83. self.assertEqual(endpoint_address(EP_BULK_IN), 0x01)
  84. def test_endpoint_direction(self):
  85. self.assertEqual(endpoint_direction(EP_BULK_OUT), ENDPOINT_OUT)
  86. self.assertEqual(endpoint_direction(EP_BULK_IN), ENDPOINT_IN)
  87. def test_endpoint_type(self):
  88. self.assertEqual(endpoint_type(ENDPOINT_TYPE_CTRL), ENDPOINT_TYPE_CTRL)
  89. self.assertEqual(endpoint_type(ENDPOINT_TYPE_ISO), ENDPOINT_TYPE_ISO)
  90. self.assertEqual(endpoint_type(ENDPOINT_TYPE_INTR), ENDPOINT_TYPE_INTR)
  91. self.assertEqual(endpoint_type(ENDPOINT_TYPE_BULK), ENDPOINT_TYPE_BULK)
  92. def test_ctrl_direction(self):
  93. self.assertEqual(ctrl_direction(CTRL_OUT), CTRL_OUT)
  94. self.assertEqual(ctrl_direction(CTRL_IN), CTRL_IN)
  95. def test_build_request_type(self):
  96. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_STANDARD, CTRL_RECIPIENT_DEVICE), 0x00)
  97. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_STANDARD, CTRL_RECIPIENT_INTERFACE), 0x01)
  98. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_STANDARD, CTRL_RECIPIENT_ENDPOINT), 0x02)
  99. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_STANDARD, CTRL_RECIPIENT_OTHER), 0x03)
  100. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_CLASS, CTRL_RECIPIENT_DEVICE), 0x20)
  101. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_CLASS, CTRL_RECIPIENT_INTERFACE), 0x21)
  102. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_CLASS, CTRL_RECIPIENT_ENDPOINT), 0x22)
  103. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_CLASS, CTRL_RECIPIENT_OTHER), 0x23)
  104. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_VENDOR, CTRL_RECIPIENT_DEVICE), 0x40)
  105. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_VENDOR, CTRL_RECIPIENT_INTERFACE), 0x41)
  106. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_VENDOR, CTRL_RECIPIENT_ENDPOINT), 0x42)
  107. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_VENDOR, CTRL_RECIPIENT_OTHER), 0x43)
  108. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_RESERVED, CTRL_RECIPIENT_DEVICE), 0x60)
  109. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_RESERVED, CTRL_RECIPIENT_INTERFACE), 0x61)
  110. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_RESERVED, CTRL_RECIPIENT_ENDPOINT), 0x62)
  111. self.assertEqual(build_request_type(CTRL_OUT, CTRL_TYPE_RESERVED, CTRL_RECIPIENT_OTHER), 0x63)
  112. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_STANDARD, CTRL_RECIPIENT_DEVICE), 0x80)
  113. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_STANDARD, CTRL_RECIPIENT_INTERFACE), 0x81)
  114. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_STANDARD, CTRL_RECIPIENT_ENDPOINT), 0x82)
  115. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_STANDARD, CTRL_RECIPIENT_OTHER), 0x83)
  116. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_CLASS, CTRL_RECIPIENT_DEVICE), 0xa0)
  117. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_CLASS, CTRL_RECIPIENT_INTERFACE), 0xa1)
  118. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_CLASS, CTRL_RECIPIENT_ENDPOINT), 0xa2)
  119. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_CLASS, CTRL_RECIPIENT_OTHER), 0xa3)
  120. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_VENDOR, CTRL_RECIPIENT_DEVICE), 0xc0)
  121. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_VENDOR, CTRL_RECIPIENT_INTERFACE), 0xc1)
  122. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_VENDOR, CTRL_RECIPIENT_ENDPOINT), 0xc2)
  123. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_VENDOR, CTRL_RECIPIENT_OTHER), 0xc3)
  124. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_RESERVED, CTRL_RECIPIENT_DEVICE), 0xe0)
  125. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_RESERVED, CTRL_RECIPIENT_INTERFACE), 0xe1)
  126. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_RESERVED, CTRL_RECIPIENT_ENDPOINT), 0xe2)
  127. self.assertEqual(build_request_type(CTRL_IN, CTRL_TYPE_RESERVED, CTRL_RECIPIENT_OTHER), 0xe3)
  128. def get_suite():
  129. suite = unittest.TestSuite()
  130. suite.addTest(FindDescriptorTest())
  131. suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(UtilTest))
  132. return suite
  133. if __name__ == '__main__':
  134. utils.run_tests(get_suite())