test_control.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 struct
  31. import usb.util
  32. import usb.core
  33. import usb.control
  34. import usb.backend.libusb01 as libusb01
  35. import usb.backend.libusb10 as libusb10
  36. import usb.backend.openusb as openusb
  37. class ControlTest(unittest.TestCase):
  38. def __init__(self, dev):
  39. unittest.TestCase.__init__(self)
  40. self.dev = dev
  41. def runTest(self):
  42. try:
  43. self.test_getset_configuration()
  44. self.test_get_status()
  45. self.test_clearset_feature()
  46. self.test_getset_descriptor()
  47. self.test_getset_interface()
  48. self.test_get_string()
  49. finally:
  50. usb.util.dispose_resources(self.dev)
  51. def test_get_status(self):
  52. self.assertEqual(usb.control.get_status(self.dev), 1)
  53. self.assertEqual(usb.control.get_status(self.dev, self.dev[0][0,0]), 0)
  54. self.assertEqual(usb.control.get_status(self.dev, self.dev[0][0,0][0]), 0)
  55. self.assertRaises(ValueError, usb.control.get_status, (self.dev, 0), 0)
  56. def test_clearset_feature(self):
  57. e = self.dev[0][0,0][0]
  58. self.assertEqual(usb.control.get_status(self.dev, e), 0)
  59. usb.control.set_feature(self.dev, usb.control.ENDPOINT_HALT, e)
  60. self.assertEqual(usb.control.get_status(self.dev, e), 1)
  61. usb.control.clear_feature(self.dev, usb.control.ENDPOINT_HALT, e)
  62. self.assertEqual(usb.control.get_status(self.dev, e), 0)
  63. def test_getset_descriptor(self):
  64. # TODO: test set_descriptor
  65. dev_fmt = 'BBHBBBBHHHBBBB'
  66. dev_descr = (self.dev.bLength,
  67. self.dev.bDescriptorType,
  68. self.dev.bcdUSB,
  69. self.dev.bDeviceClass,
  70. self.dev.bDeviceSubClass,
  71. self.dev.bDeviceProtocol,
  72. self.dev.bMaxPacketSize0,
  73. self.dev.idVendor,
  74. self.dev.idProduct,
  75. self.dev.bcdDevice,
  76. self.dev.iManufacturer,
  77. self.dev.iProduct,
  78. self.dev.iSerialNumber,
  79. self.dev.bNumConfigurations)
  80. ret = usb.control.get_descriptor(
  81. self.dev,
  82. struct.calcsize(dev_fmt),
  83. self.dev.bDescriptorType,
  84. 0
  85. )
  86. self.assertEqual(struct.unpack(dev_fmt, ret.tostring()), dev_descr)
  87. def test_getset_configuration(self):
  88. usb.control.set_configuration(self.dev, 1)
  89. self.assertEqual(usb.control.get_configuration(self.dev), 1)
  90. usb.control.set_configuration(self.dev, 0)
  91. self.assertEqual(usb.control.get_configuration(self.dev), 0)
  92. usb.control.set_configuration(self.dev, 1)
  93. self.assertEqual(usb.control.get_configuration(self.dev), 1)
  94. def test_getset_interface(self):
  95. i = self.dev[0][0,0]
  96. usb.control.set_interface(
  97. self.dev,
  98. i.bInterfaceNumber,
  99. i.bAlternateSetting
  100. )
  101. self.assertEqual(usb.control.get_interface(
  102. self.dev,
  103. i.bInterfaceNumber),
  104. i.bAlternateSetting
  105. )
  106. # Although get_string is implemented in the util module,
  107. # we test it here for convenience
  108. def test_get_string(self):
  109. manufacturer_str = 'Mxyzp7lk'.encode('utf-16-le').decode('utf-16-le')
  110. product_str = 'PyUSB'.encode('utf-16-le').decode('utf-16-le')
  111. self.assertEqual(usb.util.get_string(self.dev, len(manufacturer_str), self.dev.iManufacturer), manufacturer_str)
  112. self.assertEqual(usb.util.get_string(self.dev, len(product_str), self.dev.iProduct), product_str)
  113. def get_suite():
  114. suite = unittest.TestSuite()
  115. for m in (libusb10, libusb01, openusb):
  116. b = m.get_backend()
  117. if b is None:
  118. continue
  119. dev = utils.find_my_device(b)
  120. if dev is None:
  121. utils.logger.warning('Test hardware not found for backend %s', m.__name__)
  122. continue
  123. suite.addTest(ControlTest(dev))
  124. return suite
  125. if __name__ == '__main__':
  126. utils.run_tests(get_suite())