test_find.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 usb.backend
  30. from usb.core import find
  31. import usb.util
  32. import unittest
  33. import devinfo
  34. class _DeviceDescriptor(object):
  35. def __init__(self, idVendor, idProduct):
  36. self.bLength = 18
  37. self.bDescriptorType = usb.util.DESC_TYPE_DEVICE
  38. self.bcdUSB = 0x0200
  39. self.idVendor = idVendor
  40. self.idProduct = idProduct
  41. self.bcdDevice = 0x0001
  42. self.iManufacturer = 0
  43. self.iProduct = 0
  44. self.iSerialNumber = 0
  45. self.bNumConfigurations = 0
  46. self.bMaxPacketSize0 = 64
  47. self.bDeviceClass = 0xff
  48. self.bDeviceSubClass = 0xff
  49. self.bDeviceProtocol = 0xff
  50. # We are only interested in test usb.find() function, we don't need
  51. # to implement all IBackend stuff
  52. class _MyBackend(usb.backend.IBackend):
  53. def __init__(self):
  54. self.devices = [_DeviceDescriptor(devinfo.ID_VENDOR, p) for p in range(4)]
  55. def enumerate_devices(self):
  56. return range(len(self.devices))
  57. def get_device_descriptor(self, dev):
  58. return self.devices[dev]
  59. class FindTest(unittest.TestCase):
  60. def test_find(self):
  61. b = _MyBackend()
  62. self.assertEqual(find(backend=b, idVendor=1), None)
  63. self.assertNotEqual(find(backend=b, idProduct=1), None)
  64. self.assertEqual(len(find(find_all=True, backend=b)), len(b.devices))
  65. self.assertEqual(len(find(find_all=True, backend=b, idProduct=1)), 1)
  66. self.assertEqual(len(find(find_all=True, backend=b, idVendor=1)), 0)
  67. self.assertEqual(
  68. len(
  69. find(
  70. find_all=True,
  71. backend=b,
  72. custom_match = lambda d: d.idProduct==1
  73. ),
  74. ),
  75. 1
  76. )
  77. self.assertEqual(
  78. len(
  79. find(
  80. find_all=True,
  81. backend=b,
  82. custom_match = lambda d: d.idVendor==devinfo.ID_VENDOR,
  83. idProduct=1
  84. ),
  85. ),
  86. 1
  87. )
  88. def get_suite():
  89. suite = unittest.TestSuite()
  90. suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(FindTest))
  91. return suite
  92. if __name__ == '__main__':
  93. utils.run_tests(get_suite())