test_integration.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. # Integration tests
  29. import utils
  30. import unittest
  31. import usb.core
  32. import devinfo
  33. import usb.util
  34. import usb.backend.libusb01 as libusb01
  35. import usb.backend.libusb10 as libusb10
  36. import usb.backend.openusb as openusb
  37. data_list = (utils.get_array_data1(),
  38. utils.get_array_data2(),
  39. utils.get_list_data1(),
  40. utils.get_list_data2(),
  41. utils.get_str_data1(),
  42. utils.get_str_data1())
  43. class DeviceTest(unittest.TestCase):
  44. def __init__(self, dev):
  45. unittest.TestCase.__init__(self)
  46. self.dev = dev
  47. def runTest(self):
  48. try:
  49. self.test_attributes()
  50. self.test_timeout()
  51. self.test_set_configuration()
  52. self.test_set_interface_altsetting()
  53. self.test_write_read()
  54. self.test_ctrl_transfer()
  55. #self.test_reset()
  56. finally:
  57. usb.util.dispose_resources(self.dev)
  58. def test_attributes(self):
  59. self.assertEqual(self.dev.bLength, 18)
  60. self.assertEqual(self.dev.bDescriptorType, usb.util.DESC_TYPE_DEVICE)
  61. self.assertEqual(self.dev.bcdUSB, 0x0200)
  62. self.assertEqual(self.dev.idVendor, devinfo.ID_VENDOR)
  63. self.assertEqual(self.dev.idProduct, devinfo.ID_PRODUCT)
  64. self.assertEqual(self.dev.bcdDevice, 0x0001)
  65. self.assertEqual(self.dev.iManufacturer, 0x01)
  66. self.assertEqual(self.dev.iProduct, 0x02)
  67. self.assertEqual(self.dev.iSerialNumber, 0x03)
  68. self.assertEqual(self.dev.bNumConfigurations, 0x01)
  69. self.assertEqual(self.dev.bMaxPacketSize0, 16)
  70. self.assertEqual(self.dev.bDeviceClass, 0x00)
  71. self.assertEqual(self.dev.bDeviceSubClass, 0x00)
  72. self.assertEqual(self.dev.bDeviceProtocol, 0x00)
  73. def test_timeout(self):
  74. def set_invalid_timeout():
  75. self.dev.default_timeout = -1
  76. tmo = self.dev.default_timeout
  77. self.dev.default_timeout = 1
  78. self.assertEqual(self.dev.default_timeout, 1)
  79. self.dev.default_timeout = tmo
  80. self.assertEqual(self.dev.default_timeout, tmo)
  81. self.assertRaises(ValueError, set_invalid_timeout)
  82. self.assertEqual(self.dev.default_timeout, tmo)
  83. def test_set_configuration(self):
  84. cfg = self.dev[0].bConfigurationValue
  85. self.dev.set_configuration(0)
  86. self.dev.set_configuration(cfg)
  87. self.dev.set_configuration()
  88. self.assertEqual(cfg, self.dev.get_active_configuration().bConfigurationValue)
  89. self.dev.set_configuration(0)
  90. self.assertRaises(usb.core.USBError, self.dev.get_active_configuration)
  91. self.dev.set_configuration()
  92. def test_set_interface_altsetting(self):
  93. intf = self.dev.get_active_configuration()[(0,0)]
  94. self.dev.set_interface_altsetting(intf.bInterfaceNumber, intf.bAlternateSetting)
  95. self.dev.set_interface_altsetting()
  96. def test_reset(self):
  97. self.dev.reset()
  98. utils.delay_after_reset()
  99. def test_write_read(self):
  100. ep_list = ((devinfo.EP_BULK_OUT, devinfo.EP_BULK_IN),
  101. (devinfo.EP_INTR_OUT, devinfo.EP_INTR_IN))
  102. for ep in ep_list:
  103. for data in data_list:
  104. adata = utils.to_array(data)
  105. length = utils.data_len(data)
  106. ret = self.dev.write(ep[0], data)
  107. self.assertEqual(ret,
  108. length,
  109. 'Failed to write data: ' + \
  110. str(data) + ', in EP = ' + \
  111. str(ep[0])
  112. )
  113. ret = self.dev.read(ep[1], length)
  114. self.assertTrue(utils.array_equals(ret, adata),
  115. str(ret) + ' != ' + \
  116. str(adata) + ', in EP = ' + \
  117. str(ep[1])
  118. )
  119. def test_ctrl_transfer(self):
  120. for data in data_list:
  121. length = utils.data_len(data)
  122. adata = utils.to_array(data)
  123. ret = self.dev.ctrl_transfer(
  124. 0x40,
  125. devinfo.CTRL_LOOPBACK_WRITE,
  126. 0,
  127. 0,
  128. data
  129. )
  130. self.assertEqual(ret,
  131. length,
  132. 'Failed to write data: ' + str(data))
  133. ret = utils.to_array(self.dev.ctrl_transfer(
  134. 0xC0,
  135. devinfo.CTRL_LOOPBACK_READ,
  136. 0,
  137. 0,
  138. length
  139. ))
  140. self.assertTrue(utils.array_equals(ret, adata),
  141. str(ret) + ' != ' + str(adata))
  142. class ConfigurationTest(unittest.TestCase):
  143. def __init__(self, dev):
  144. unittest.TestCase.__init__(self)
  145. self.cfg = dev[0]
  146. def runTest(self):
  147. try:
  148. self.test_attributes()
  149. self.test_set()
  150. finally:
  151. usb.util.dispose_resources(self.cfg.device)
  152. def test_attributes(self):
  153. self.assertEqual(self.cfg.bLength, 9)
  154. self.assertEqual(self.cfg.bDescriptorType, usb.util.DESC_TYPE_CONFIG)
  155. self.assertEqual(self.cfg.wTotalLength, 46)
  156. self.assertEqual(self.cfg.bNumInterfaces, 0x01)
  157. self.assertEqual(self.cfg.bConfigurationValue, 0x01)
  158. self.assertEqual(self.cfg.iConfiguration, 0x00)
  159. self.assertEqual(self.cfg.bmAttributes, 0xC0)
  160. self.assertEqual(self.cfg.bMaxPower, 50)
  161. def test_set(self):
  162. self.cfg.set()
  163. class InterfaceTest(unittest.TestCase):
  164. def __init__(self, dev):
  165. unittest.TestCase.__init__(self)
  166. self.dev = dev
  167. self.intf = dev[0][(0,0)]
  168. def runTest(self):
  169. try:
  170. self.dev.set_configuration()
  171. self.test_attributes()
  172. self.test_set_altsetting()
  173. finally:
  174. usb.util.dispose_resources(self.intf.device)
  175. def test_attributes(self):
  176. self.assertEqual(self.intf.bLength, 9)
  177. self.assertEqual(self.intf.bDescriptorType, usb.util.DESC_TYPE_INTERFACE)
  178. self.assertEqual(self.intf.bInterfaceNumber, 0)
  179. self.assertEqual(self.intf.bAlternateSetting, 0)
  180. self.assertEqual(self.intf.bNumEndpoints, 4)
  181. self.assertEqual(self.intf.bInterfaceClass, 0xFF)
  182. self.assertEqual(self.intf.bInterfaceSubClass, 0xFF)
  183. self.assertEqual(self.intf.bInterfaceProtocol, 0xFF)
  184. self.assertEqual(self.intf.iInterface, 0x00)
  185. def test_set_altsetting(self):
  186. self.intf.set_altsetting()
  187. class EndpointTest(unittest.TestCase):
  188. def __init__(self, dev):
  189. unittest.TestCase.__init__(self)
  190. self.dev = dev
  191. intf = dev[0][(0,0)]
  192. self.ep_out = usb.util.find_descriptor(intf, bEndpointAddress=0x01)
  193. self.ep_in = usb.util.find_descriptor(intf, bEndpointAddress=0x81)
  194. def runTest(self):
  195. try:
  196. self.dev.set_configuration()
  197. self.test_attributes()
  198. self.test_write_read()
  199. finally:
  200. usb.util.dispose_resources(self.dev)
  201. def test_attributes(self):
  202. self.assertEqual(self.ep_out.bLength, 7)
  203. self.assertEqual(self.ep_out.bDescriptorType, usb.util.DESC_TYPE_ENDPOINT)
  204. self.assertEqual(self.ep_out.bEndpointAddress, 0x01)
  205. self.assertEqual(self.ep_out.bmAttributes, 0x02)
  206. self.assertEqual(self.ep_out.wMaxPacketSize, 64)
  207. self.assertEqual(self.ep_out.bInterval, 32)
  208. def test_write_read(self):
  209. for data in data_list:
  210. adata = utils.to_array(data)
  211. ret = self.ep_out.write(data)
  212. length = utils.data_len(data)
  213. self.assertEqual(ret, length, 'Failed to write data: ' + str(data))
  214. ret = self.ep_in.read(length)
  215. self.assertTrue(utils.array_equals(ret, adata), str(ret) + ' != ' + str(adata))
  216. def get_suite():
  217. suite = unittest.TestSuite()
  218. test_cases = (DeviceTest, ConfigurationTest, InterfaceTest, EndpointTest)
  219. for m in (libusb10, libusb01, openusb):
  220. b = m.get_backend()
  221. if b is None:
  222. continue
  223. dev = utils.find_my_device(b)
  224. if dev is None:
  225. utils.logger.warning('Test hardware not found for backend %s', m.__name__)
  226. continue
  227. for ObjectTestCase in test_cases:
  228. utils.logger.info('Adding %s(%s) to test suite...', ObjectTestCase.__name__, m.__name__)
  229. suite.addTest(ObjectTestCase(dev))
  230. return suite
  231. if __name__ == '__main__':
  232. utils.run_tests(get_suite())