- Main Page
- Related Pages
- Data Structures
- Files

- File List
- Globals

# test/usb-tester/usbtest/testctrl.py

    00001 import unittest
    00002 import usbtest
    00003 import usb
    00004
    00005 DESC_DATA_VALUE         = 0x55
    00006 TIMEOUT                 = 1000
    00007
    00008 class TestCase(unittest.TestCase):
    00009         "Test various tricky control transfers"
    00010
    00011         def setUp(self):
    00012                 self.dev = usbtest.dev.open()
    00013                 self.dev.claimInterface(0)
    00014                 self.dev.controlMsg(usb.ENDPOINT_OUT | usb.TYPE_VENDOR
    00015                                 | usb.RECIP_INTERFACE, usbtest.REQ_INIT_DESC,
    00016                                 0, DESC_DATA_VALUE, 0, timeout=TIMEOUT)
    00017
    00018         def tearDown(self):
    00019                 self.dev.releaseInterface()
    00020                 del self.dev
    00021
    00022         def testInvalidInReq(self):
    00023                 try:
    00024                         self.dev.controlMsg(usb.ENDPOINT_IN | usb.TYPE_VENDOR
    00025                                         | usb.RECIP_INTERFACE,
    00026                                         usbtest.REQ_INVALID, 8, 0, 0,
    00027                                         timeout=TIMEOUT)
    00028                 except usb.USBError:
    00029                         pass
    00030                 else:
    00031                         fail("expected a USBError")
    00032
    00033         def testInvalidOutReq(self):
    00034                 try:
    00035                         self.dev.controlMsg(usb.ENDPOINT_OUT | usb.TYPE_VENDOR
    00036                                         | usb.RECIP_INTERFACE,
    00037                                         usbtest.REQ_INVALID, 8, 0, 0,
    00038                                         timeout=TIMEOUT)
    00039                 except usb.USBError:
    00040                         pass
    00041                 else:
    00042                         fail("expected a USBError")
    00043
    00044         def testSetInterface(self):
    00045                 self.dev.setAltInterface(0)
    00046
    00047         def testGetInterface(self):
    00048                 altsetting, = self.dev.controlMsg(usb.ENDPOINT_IN
    00049                                 | usb.TYPE_STANDARD | usb.RECIP_INTERFACE,
    00050                                 usb.REQ_GET_INTERFACE, 1, 0, 0, timeout=TIMEOUT)
    00051                 self.failUnlessEqual(altsetting, 0)
    00052
    00053         def doTestCtrlIn(self, dt, length, desc_len=-1):
    00054                 if desc_len == -1:
    00055                         desc_len = length
    00056                 data_len = min(length, desc_len)
    00057
    00058                 desc = self.dev.controlMsg(usb.ENDPOINT_IN |
    00059                                 usb.TYPE_VENDOR | usb.RECIP_INTERFACE,
    00060                                 usbtest.REQ_GET_DESC, length, dt << 8, 0,
    00061                                 timeout=TIMEOUT)
    00062                 self.failUnlessEqual(len(desc), data_len)
    00063                 self.failUnlessEqual(desc[0], desc_len)
    00064                 self.failUnlessEqual(desc[1], dt)
    00065                 for i in range(2, data_len):
    00066                         self.failUnlessEqual(desc[i], DESC_DATA_VALUE)
    00067
    00068         def testCtrlInSize2(self):
    00069                 self.doTestCtrlIn(usbtest.DT_SIZE2, 2)
    00070
    00071         def testCtrlInSize63(self):
    00072                 self.doTestCtrlIn(usbtest.DT_SIZE63, 63)
    00073
    00074         def testCtrlInSize64(self):
    00075                 self.doTestCtrlIn(usbtest.DT_SIZE64, 64)
    00076
    00077         def testCtrlInSize65(self):
    00078                 self.doTestCtrlIn(usbtest.DT_SIZE65, 65)
    00079
    00080         def testCtrlInSize127(self):
    00081                 self.doTestCtrlIn(usbtest.DT_SIZE127, 127)
    00082
    00083         def testCtrlInSize128(self):
    00084                 self.doTestCtrlIn(usbtest.DT_SIZE128, 128)
    00085
    00086         def testCtrlInSize129(self):
    00087                 self.doTestCtrlIn(usbtest.DT_SIZE129, 129)
    00088
    00089         def testCtrlInTruncated(self):
    00090                 self.doTestCtrlIn(usbtest.DT_SIZE129, 3, 129)
    00091
    00092         def testCtrlInShortZLP(self):
    00093                 self.doTestCtrlIn(usbtest.DT_SIZE128, 256, 128)
    00094
    00095         def testCtrlInShortNoZLP(self):
    00096                 self.doTestCtrlIn(usbtest.DT_SIZE127, 256, 127)
    00097
    00098 def suite():
    00099         return unittest.makeSuite(TestCase, 'test')