test_backend.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. import devinfo
  31. import usb.util
  32. import usb.backend.libusb01 as libusb01
  33. import usb.backend.libusb10 as libusb10
  34. import usb.backend.openusb as openusb
  35. class BackendTest(unittest.TestCase):
  36. def __init__(self, backend):
  37. unittest.TestCase.__init__(self)
  38. self.backend = backend
  39. def runTest(self):
  40. try:
  41. self.test_enumerate_devices()
  42. self.test_get_device_descriptor()
  43. self.test_get_configuration_descriptor()
  44. self.test_get_interface_descriptor()
  45. self.test_get_endpoint_descriptor()
  46. self.test_open_device()
  47. self.test_set_configuration()
  48. self.test_claim_interface()
  49. self.test_set_interface_altsetting()
  50. self.test_bulk_write_read()
  51. self.test_intr_write_read()
  52. self.test_iso_write_read()
  53. self.test_ctrl_transfer()
  54. except:
  55. # do this to not influence other tests upon error
  56. intf = self.backend.get_interface_descriptor(self.dev, 0, 0, 0)
  57. self.backend.release_interface(self.handle, intf.bInterfaceNumber)
  58. self.backend.close_device(self.handle)
  59. raise
  60. self.test_release_interface()
  61. #self.test_reset_device()
  62. self.test_close_device()
  63. #utils.delay_after_reset()
  64. def test_enumerate_devices(self):
  65. for d in self.backend.enumerate_devices():
  66. desc = self.backend.get_device_descriptor(d)
  67. if desc.idVendor == devinfo.ID_VENDOR and desc.idProduct == devinfo.ID_PRODUCT:
  68. self.dev = d
  69. return
  70. self.fail('PyUSB test device not found')
  71. def test_get_device_descriptor(self):
  72. dsc = self.backend.get_device_descriptor(self.dev)
  73. self.assertEqual(dsc.bLength, 18)
  74. self.assertEqual(dsc.bDescriptorType, usb.util.DESC_TYPE_DEVICE)
  75. self.assertEqual(dsc.bcdUSB, 0x0200)
  76. self.assertEqual(dsc.idVendor, devinfo.ID_VENDOR)
  77. self.assertEqual(dsc.idProduct, devinfo.ID_PRODUCT)
  78. self.assertEqual(dsc.bcdDevice, 0x0001)
  79. self.assertEqual(dsc.iManufacturer, 0x01)
  80. self.assertEqual(dsc.iProduct, 0x02)
  81. self.assertEqual(dsc.iSerialNumber, 0x03)
  82. self.assertEqual(dsc.bNumConfigurations, 0x01)
  83. self.assertEqual(dsc.bMaxPacketSize0, 16)
  84. self.assertEqual(dsc.bDeviceClass, 0x00)
  85. self.assertEqual(dsc.bDeviceSubClass, 0x00)
  86. self.assertEqual(dsc.bDeviceProtocol, 0x00)
  87. def test_get_configuration_descriptor(self):
  88. cfg = self.backend.get_configuration_descriptor(self.dev, 0)
  89. self.assertEqual(cfg.bLength, 9)
  90. self.assertEqual(cfg.bDescriptorType, usb.util.DESC_TYPE_CONFIG)
  91. self.assertEqual(cfg.wTotalLength, 46)
  92. self.assertEqual(cfg.bNumInterfaces, 0x01)
  93. self.assertEqual(cfg.bConfigurationValue, 0x01)
  94. self.assertEqual(cfg.iConfiguration, 0x00)
  95. self.assertEqual(cfg.bmAttributes, 0xC0)
  96. self.assertEqual(cfg.bMaxPower, 50)
  97. def test_get_interface_descriptor(self):
  98. intf = self.backend.get_interface_descriptor(self.dev, 0, 0, 0)
  99. self.assertEqual(intf.bLength, 9)
  100. self.assertEqual(intf.bDescriptorType, usb.util.DESC_TYPE_INTERFACE)
  101. self.assertEqual(intf.bInterfaceNumber, 0)
  102. self.assertEqual(intf.bAlternateSetting, 0)
  103. self.assertEqual(intf.bNumEndpoints, 4)
  104. self.assertEqual(intf.bInterfaceClass, 0xFF)
  105. self.assertEqual(intf.bInterfaceSubClass, 0xFF)
  106. self.assertEqual(intf.bInterfaceProtocol, 0xFF)
  107. self.assertEqual(intf.iInterface, 0x00)
  108. def test_get_endpoint_descriptor(self):
  109. ep = self.backend.get_endpoint_descriptor(self.dev, 0, 0, 0, 0)
  110. self.assertEqual(ep.bLength, 7)
  111. self.assertEqual(ep.bDescriptorType, usb.util.DESC_TYPE_ENDPOINT)
  112. self.assertEqual(ep.bEndpointAddress, 0x01)
  113. self.assertEqual(ep.bmAttributes, 0x02)
  114. self.assertEqual(ep.wMaxPacketSize, 64)
  115. self.assertEqual(ep.bInterval, 32)
  116. def test_open_device(self):
  117. self.handle = self.backend.open_device(self.dev)
  118. def test_close_device(self):
  119. self.backend.close_device(self.handle)
  120. def test_set_configuration(self):
  121. cfg = self.backend.get_configuration_descriptor(self.dev, 0)
  122. self.backend.set_configuration(self.handle, 0)
  123. self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
  124. def test_set_interface_altsetting(self):
  125. intf = self.backend.get_interface_descriptor(self.dev, 0, 0, 0)
  126. self.backend.set_interface_altsetting(self.handle,
  127. intf.bInterfaceNumber,
  128. intf.bAlternateSetting)
  129. def test_claim_interface(self):
  130. intf = self.backend.get_interface_descriptor(self.dev, 0, 0, 0)
  131. self.backend.claim_interface(self.handle, intf.bInterfaceNumber)
  132. def test_release_interface(self):
  133. intf = self.backend.get_interface_descriptor(self.dev, 0, 0, 0)
  134. self.backend.release_interface(self.handle, intf.bInterfaceNumber)
  135. def test_bulk_write_read(self):
  136. self.__write_read(self.backend.bulk_write,
  137. self.backend.bulk_read,
  138. devinfo.EP_BULK_OUT,
  139. devinfo.EP_BULK_IN)
  140. def test_intr_write_read(self):
  141. self.__write_read(self.backend.intr_write,
  142. self.backend.intr_read,
  143. devinfo.EP_INTR_OUT,
  144. devinfo.EP_INTR_IN)
  145. def test_iso_write_read(self):
  146. pass
  147. def test_ctrl_transfer(self):
  148. for data in (utils.get_array_data1(), utils.get_array_data2()):
  149. length = len(data) * data.itemsize
  150. ret = self.backend.ctrl_transfer(self.handle,
  151. 0x40,
  152. devinfo.CTRL_LOOPBACK_WRITE,
  153. 0,
  154. 0,
  155. data,
  156. 1000)
  157. self.assertEqual(ret,
  158. length,
  159. 'Failed to write data: ' + str(data))
  160. ret = self.backend.ctrl_transfer(self.handle,
  161. 0xC0,
  162. devinfo.CTRL_LOOPBACK_READ,
  163. 0,
  164. 0,
  165. length,
  166. 1000)
  167. self.assertEqual(ret,
  168. data,
  169. 'Failed to read data: ' + str(data) + ' != ' + str(ret))
  170. def test_reset_device(self):
  171. self.backend.reset_device(self.handle)
  172. def __write_read(self, write_fn, read_fn, ep_out, ep_in):
  173. intf = self.backend.get_interface_descriptor(self.dev, 0, 0, 0).bInterfaceNumber
  174. for data in (utils.get_array_data1(), utils.get_array_data2()):
  175. length = len(data) * data.itemsize
  176. ret = write_fn(self.handle, ep_out, intf, data, 1000)
  177. self.assertEqual(ret,
  178. length,
  179. 'Failed to write data: ' + \
  180. str(data) + \
  181. ', in EP = ' + \
  182. str(ep_out))
  183. ret = read_fn(self.handle, ep_in, intf, length, 1000)
  184. self.assertEqual(ret,
  185. data,
  186. 'Failed to read data: ' + \
  187. str(data) + \
  188. ', in EP = ' + \
  189. str(ep_in))
  190. def get_suite():
  191. suite = unittest.TestSuite()
  192. for m in (libusb10, libusb01, openusb):
  193. b = m.get_backend()
  194. if b is not None and utils.find_my_device(b):
  195. utils.logger.info('Adding %s(%s) to test suite...', BackendTest.__name__, m.__name__)
  196. suite.addTest(BackendTest(b))
  197. else:
  198. utils.logger.warning('%s(%s) is not available', BackendTest.__name__, m.__name__)
  199. return suite
  200. if __name__ == '__main__':
  201. utils.run_tests(get_suite())