PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/usb_gadget/hid_gadget_test.py

https://github.com/chromium/chromium
Python | 258 lines | 242 code | 12 blank | 4 comment | 2 complexity | 749973d8238bebe3b05e52f72533247e MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. #!/usr/bin/env python
  2. # Copyright 2014 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import unittest
  6. import mock
  7. import hid_constants
  8. import hid_descriptors
  9. import hid_gadget
  10. import usb_constants
  11. report_desc = hid_descriptors.ReportDescriptor(
  12. hid_descriptors.UsagePage(0xFF00), # Vendor Defined
  13. hid_descriptors.Usage(0x00),
  14. hid_descriptors.Collection(
  15. hid_constants.CollectionType.APPLICATION,
  16. hid_descriptors.LogicalMinimum(0, force_length=1),
  17. hid_descriptors.LogicalMaximum(255, force_length=2),
  18. hid_descriptors.ReportSize(8),
  19. hid_descriptors.ReportCount(8),
  20. hid_descriptors.Input(hid_descriptors.Data,
  21. hid_descriptors.Variable,
  22. hid_descriptors.Absolute,
  23. hid_descriptors.BufferedBytes),
  24. hid_descriptors.Output(hid_descriptors.Data,
  25. hid_descriptors.Variable,
  26. hid_descriptors.Absolute,
  27. hid_descriptors.BufferedBytes),
  28. hid_descriptors.Feature(hid_descriptors.Data,
  29. hid_descriptors.Variable,
  30. hid_descriptors.Absolute,
  31. hid_descriptors.BufferedBytes)
  32. )
  33. )
  34. combo_report_desc = hid_descriptors.ReportDescriptor(
  35. hid_descriptors.ReportID(1),
  36. report_desc,
  37. hid_descriptors.ReportID(2),
  38. report_desc
  39. )
  40. class HidGadgetTest(unittest.TestCase):
  41. def test_bad_intervals(self):
  42. with self.assertRaisesRegexp(ValueError, 'Full speed'):
  43. hid_gadget.HidGadget(report_desc, features={}, interval_ms=50000,
  44. vendor_id=0, product_id=0)
  45. with self.assertRaisesRegexp(ValueError, 'High speed'):
  46. hid_gadget.HidGadget(report_desc, features={}, interval_ms=5000,
  47. vendor_id=0, product_id=0)
  48. def test_get_string_descriptor(self):
  49. g = hid_gadget.HidGadget(report_desc=report_desc, features={},
  50. vendor_id=0, product_id=0)
  51. chip = mock.Mock()
  52. g.Connected(chip, usb_constants.Speed.HIGH)
  53. g.AddStringDescriptor(2, 'HID Gadget')
  54. desc = g.ControlRead(0x80, 6, 0x0302, 0x0409, 255)
  55. self.assertEquals(desc, '\x16\x03H\0I\0D\0 \0G\0a\0d\0g\0e\0t\0')
  56. def test_get_report_descriptor(self):
  57. g = hid_gadget.HidGadget(report_desc=report_desc, features={},
  58. vendor_id=0, product_id=0)
  59. chip = mock.Mock()
  60. g.Connected(chip, usb_constants.Speed.HIGH)
  61. desc = g.ControlRead(0x81, 6, 0x2200, 0, 63)
  62. self.assertEquals(desc, report_desc)
  63. def test_set_idle(self):
  64. g = hid_gadget.HidGadget(report_desc=report_desc, features={},
  65. vendor_id=0, product_id=0)
  66. chip = mock.Mock()
  67. g.Connected(chip, usb_constants.Speed.HIGH)
  68. self.assertTrue(g.ControlWrite(0x21, 0x0A, 0, 0, ''))
  69. def test_class_wrong_target(self):
  70. g = hid_gadget.HidGadget(report_desc=report_desc, features={},
  71. vendor_id=0, product_id=0)
  72. chip = mock.Mock()
  73. g.Connected(chip, usb_constants.Speed.HIGH)
  74. self.assertIsNone(g.ControlRead(0xA0, 0, 0, 0, 0)) # Device
  75. self.assertIsNone(g.ControlRead(0xA1, 0, 0, 1, 0)) # Interface 1
  76. self.assertIsNone(g.ControlWrite(0x20, 0, 0, 0, '')) # Device
  77. self.assertIsNone(g.ControlWrite(0x21, 0, 0, 1, '')) # Interface 1
  78. def test_send_report_zero(self):
  79. g = hid_gadget.HidGadget(report_desc=report_desc, features={},
  80. vendor_id=0, product_id=0)
  81. chip = mock.Mock()
  82. g.Connected(chip, usb_constants.Speed.HIGH)
  83. g.SendReport(0, 'Hello world!')
  84. chip.SendPacket.assert_called_once_with(0x81, 'Hello world!')
  85. def test_send_multiple_reports(self):
  86. g = hid_gadget.HidGadget(report_desc=report_desc, features={},
  87. vendor_id=0, product_id=0)
  88. chip = mock.Mock()
  89. g.Connected(chip, usb_constants.Speed.HIGH)
  90. g.SendReport(1, 'Hello!')
  91. g.SendReport(2, 'World!')
  92. chip.SendPacket.assert_has_calls([
  93. mock.call(0x81, '\x01Hello!'),
  94. mock.call(0x81, '\x02World!'),
  95. ])
  96. class TestFeature(hid_gadget.HidFeature):
  97. def SetInputReport(self, data):
  98. self.input_report = data
  99. return True
  100. def SetOutputReport(self, data):
  101. self.output_report = data
  102. return True
  103. def SetFeatureReport(self, data):
  104. self.feature_report = data
  105. return True
  106. def GetInputReport(self):
  107. return 'Input report.'
  108. def GetOutputReport(self):
  109. return 'Output report.'
  110. def GetFeatureReport(self):
  111. return 'Feature report.'
  112. class HidFeatureTest(unittest.TestCase):
  113. def test_disconnected(self):
  114. feature = TestFeature()
  115. with self.assertRaisesRegexp(RuntimeError, 'not connected'):
  116. feature.SendReport('Hello world!')
  117. def test_send_report(self):
  118. feature = TestFeature()
  119. g = hid_gadget.HidGadget(report_desc, features={1: feature},
  120. vendor_id=0, product_id=0)
  121. chip = mock.Mock()
  122. g.Connected(chip, usb_constants.Speed.HIGH)
  123. feature.SendReport('Hello world!')
  124. chip.SendPacket.assert_called_once_with(0x81, '\x01Hello world!')
  125. g.Disconnected()
  126. def test_get_bad_report(self):
  127. feature = TestFeature()
  128. g = hid_gadget.HidGadget(report_desc, features={1: feature},
  129. vendor_id=0, product_id=0)
  130. chip = mock.Mock()
  131. g.Connected(chip, usb_constants.Speed.HIGH)
  132. self.assertIsNone(g.ControlRead(0xA1, 1, 0x0102, 0, 8))
  133. def test_set_bad_report(self):
  134. feature = TestFeature()
  135. g = hid_gadget.HidGadget(report_desc, features={1: feature},
  136. vendor_id=0, product_id=0)
  137. chip = mock.Mock()
  138. g.Connected(chip, usb_constants.Speed.HIGH)
  139. self.assertIsNone(g.ControlWrite(0x21, 0x09, 0x0102, 0, 'Hello!'))
  140. def test_get_input_report(self):
  141. feature = TestFeature()
  142. g = hid_gadget.HidGadget(report_desc, features={1: feature},
  143. vendor_id=0, product_id=0)
  144. chip = mock.Mock()
  145. g.Connected(chip, usb_constants.Speed.HIGH)
  146. report = g.ControlRead(0xA1, 1, 0x0101, 0, 8)
  147. self.assertEquals(report, 'Input re')
  148. def test_set_input_report(self):
  149. feature = TestFeature()
  150. g = hid_gadget.HidGadget(report_desc, features={1: feature},
  151. vendor_id=0, product_id=0)
  152. chip = mock.Mock()
  153. g.Connected(chip, usb_constants.Speed.HIGH)
  154. self.assertTrue(g.ControlWrite(0x21, 0x09, 0x0101, 0, 'Hello!'))
  155. self.assertEquals(feature.input_report, 'Hello!')
  156. def test_get_output_report(self):
  157. feature = TestFeature()
  158. g = hid_gadget.HidGadget(report_desc, features={1: feature},
  159. vendor_id=0, product_id=0)
  160. chip = mock.Mock()
  161. g.Connected(chip, usb_constants.Speed.HIGH)
  162. report = g.ControlRead(0xA1, 1, 0x0201, 0, 8)
  163. self.assertEquals(report, 'Output r')
  164. def test_set_output_report(self):
  165. feature = TestFeature()
  166. g = hid_gadget.HidGadget(report_desc, features={1: feature},
  167. vendor_id=0, product_id=0)
  168. chip = mock.Mock()
  169. g.Connected(chip, usb_constants.Speed.HIGH)
  170. self.assertTrue(g.ControlWrite(0x21, 0x09, 0x0201, 0, 'Hello!'))
  171. self.assertEquals(feature.output_report, 'Hello!')
  172. def test_receive_interrupt(self):
  173. feature = TestFeature()
  174. g = hid_gadget.HidGadget(report_desc, features={1: feature},
  175. vendor_id=0, product_id=0)
  176. chip = mock.Mock()
  177. g.Connected(chip, usb_constants.Speed.HIGH)
  178. g.SetConfiguration(1)
  179. g.ReceivePacket(0x01, '\x01Hello!')
  180. self.assertFalse(chip.HaltEndpoint.called)
  181. self.assertEquals(feature.output_report, 'Hello!')
  182. def test_receive_interrupt_report_zero(self):
  183. feature = TestFeature()
  184. g = hid_gadget.HidGadget(report_desc, features={0: feature},
  185. vendor_id=0, product_id=0)
  186. chip = mock.Mock()
  187. g.Connected(chip, usb_constants.Speed.HIGH)
  188. g.SetConfiguration(1)
  189. g.ReceivePacket(0x01, 'Hello!')
  190. self.assertFalse(chip.HaltEndpoint.called)
  191. self.assertEquals(feature.output_report, 'Hello!')
  192. def test_receive_bad_interrupt(self):
  193. feature = TestFeature()
  194. g = hid_gadget.HidGadget(report_desc, features={1: feature},
  195. vendor_id=0, product_id=0)
  196. chip = mock.Mock()
  197. g.Connected(chip, usb_constants.Speed.HIGH)
  198. g.SetConfiguration(1)
  199. g.ReceivePacket(0x01, '\x00Hello!')
  200. chip.HaltEndpoint.assert_called_once_with(0x01)
  201. def test_get_feature_report(self):
  202. feature = TestFeature()
  203. g = hid_gadget.HidGadget(report_desc, features={1: feature},
  204. vendor_id=0, product_id=0)
  205. chip = mock.Mock()
  206. g.Connected(chip, usb_constants.Speed.HIGH)
  207. report = g.ControlRead(0xA1, 1, 0x0301, 0, 8)
  208. self.assertEquals(report, 'Feature ')
  209. def test_set_feature_report(self):
  210. feature = TestFeature()
  211. g = hid_gadget.HidGadget(report_desc, features={1: feature},
  212. vendor_id=0, product_id=0)
  213. chip = mock.Mock()
  214. g.Connected(chip, usb_constants.Speed.HIGH)
  215. self.assertTrue(g.ControlWrite(0x21, 0x09, 0x0301, 0, 'Hello!'))
  216. self.assertEquals(feature.feature_report, 'Hello!')
  217. if __name__ == '__main__':
  218. unittest.main()