<s> import utils <EOL> import unittest <EOL> from usb . util import * <EOL> from devinfo import * <EOL> from usb . _debug import methodtrace <EOL> import usb . backend <EOL> class _ConfigurationDescriptor ( object ) : <EOL> def __init__ ( self , bConfigurationValue ) : <EOL> self . bLength = <NUM_LIT:9> <EOL> self . bDescriptorType = DESC_TYPE_CONFIG <EOL> self . wTotalLength = <NUM_LIT> <EOL> self . bNumInterfaces = <NUM_LIT:0> <EOL> self . bConfigurationValue = bConfigurationValue <EOL> self . iConfiguration = <NUM_LIT:0> <EOL> self . bmAttributes = <NUM_LIT> <EOL> self . bMaxPower = <NUM_LIT:50> <EOL> class _DeviceDescriptor ( object ) : <EOL> def __init__ ( self ) : <EOL> self . configurations = ( _ConfigurationDescriptor ( <NUM_LIT:1> ) , _ConfigurationDescriptor ( <NUM_LIT:2> ) ) <EOL> self . bLength = <NUM_LIT> <EOL> self . bDescriptorType = usb . util . DESC_TYPE_DEVICE <EOL> self . bcdUSB = <NUM_LIT> <EOL> self . idVendor = ID_VENDOR <EOL> self . idProduct = ID_PRODUCT <EOL> self . bcdDevice = <NUM_LIT> <EOL> self . iManufacturer = <NUM_LIT:0> <EOL> self . iProduct = <NUM_LIT:0> <EOL> self . iSerialNumber = <NUM_LIT:0> <EOL> self . bNumConfigurations = len ( self . configurations ) <EOL> self . bMaxPacketSize0 = <NUM_LIT:64> <EOL> self . bDeviceClass = <NUM_LIT> <EOL> self . bDeviceSubClass = <NUM_LIT> <EOL> self . bDeviceProtocol = <NUM_LIT> <EOL> class FindDescriptorTest ( unittest . TestCase ) : <EOL> @ methodtrace ( utils . logger ) <EOL> def runTest ( self ) : <EOL> d = usb . core . find ( idVendor = ID_VENDOR ) <EOL> if d is None : <EOL> return <EOL> self . assertEqual ( find_descriptor ( d , bConfigurationValue = <NUM_LIT:10> ) , None ) <EOL> self . assertNotEqual ( find_descriptor ( d , bConfigurationValue = <NUM_LIT:1> ) , None ) <EOL> self . assertEqual ( len ( list ( find_descriptor ( d , find_all = True , bConfigurationValue = <NUM_LIT:10> ) ) ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( list ( find_descriptor ( d , find_all = True , bConfigurationValue = <NUM_LIT:1> ) ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( list ( find_descriptor ( d , find_all = True ) ) ) , d . bNumConfigurations ) <EOL> self . assertEqual ( find_descriptor ( d , custom_match = lambda c : c . bConfigurationValue == <NUM_LIT:10> ) , None ) <EOL> self . assertNotEqual ( find_descriptor ( d , custom_match = lambda c : c . bConfigurationValue == <NUM_LIT:1> ) , None ) <EOL> self . assertEqual ( len ( list ( find_descriptor ( d , find_all = True , custom_match = lambda c : c . bConfigurationValue == <NUM_LIT:10> ) ) ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( list ( find_descriptor ( d , find_all = True , custom_match = lambda c : c . bConfigurationValue == <NUM_LIT:1> ) ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( find_descriptor ( d , custom_match = lambda c : c . bConfigurationValue == <NUM_LIT:10> , bLength = <NUM_LIT:9> ) , None ) <EOL> self . assertNotEqual ( find_descriptor ( d , custom_match = lambda c : c . bConfigurationValue == <NUM_LIT:1> , bLength = <NUM_LIT:9> ) , None ) <EOL> cfg = find_descriptor ( d ) <EOL> self . assertTrue ( isinstance ( cfg , usb . core . Configuration ) ) <EOL> intf = find_descriptor ( cfg ) <EOL> self . assertTrue ( isinstance ( intf , usb . core . Interface ) ) <EOL> class UtilTest ( unittest . TestCase ) : <EOL> @ methodtrace ( utils . logger ) <EOL> def test_endpoint_address ( self ) : <EOL> self . assertEqual ( endpoint_address ( <NUM_LIT> ) , <NUM_LIT> ) <EOL> self . assertEqual ( endpoint_address ( <NUM_LIT> ) , <NUM_LIT> ) <EOL> @ methodtrace ( utils . logger ) <EOL> def test_endpoint_direction ( self ) : <EOL> self . assertEqual ( endpoint_direction ( <NUM_LIT> ) , ENDPOINT_OUT ) <EOL> self . assertEqual ( endpoint_direction ( <NUM_LIT> ) , ENDPOINT_IN ) <EOL> @ methodtrace ( utils . logger ) <EOL> def test_endpoint_type ( self ) : <EOL> self . assertEqual ( endpoint_type ( ENDPOINT_TYPE_CTRL ) , ENDPOINT_TYPE_CTRL ) <EOL> self . assertEqual ( endpoint_type ( ENDPOINT_TYPE_ISO ) , ENDPOINT_TYPE_ISO ) <EOL> self . assertEqual ( endpoint_type ( ENDPOINT_TYPE_INTR ) , ENDPOINT_TYPE_INTR ) <EOL> self . assertEqual ( endpoint_type ( ENDPOINT_TYPE_BULK ) , ENDPOINT_TYPE_BULK ) <EOL> @ methodtrace ( utils . logger ) <EOL> def test_ctrl_direction ( self ) : <EOL> self . assertEqual ( ctrl_direction ( CTRL_OUT ) , CTRL_OUT ) <EOL> self . assertEqual ( ctrl_direction ( CTRL_IN ) , CTRL_IN ) <EOL> @ methodtrace ( utils . logger ) <EOL> def test_build_request_type ( self ) : <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_STANDARD , CTRL_RECIPIENT_DEVICE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_STANDARD , CTRL_RECIPIENT_INTERFACE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_STANDARD , CTRL_RECIPIENT_ENDPOINT ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_STANDARD , CTRL_RECIPIENT_OTHER ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_CLASS , CTRL_RECIPIENT_DEVICE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_CLASS , CTRL_RECIPIENT_INTERFACE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_CLASS , CTRL_RECIPIENT_ENDPOINT ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_CLASS , CTRL_RECIPIENT_OTHER ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_VENDOR , CTRL_RECIPIENT_DEVICE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_VENDOR , CTRL_RECIPIENT_INTERFACE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_VENDOR , CTRL_RECIPIENT_ENDPOINT ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_VENDOR , CTRL_RECIPIENT_OTHER ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_RESERVED , CTRL_RECIPIENT_DEVICE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_RESERVED , CTRL_RECIPIENT_INTERFACE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_RESERVED , CTRL_RECIPIENT_ENDPOINT ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_OUT , CTRL_TYPE_RESERVED , CTRL_RECIPIENT_OTHER ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_STANDARD , CTRL_RECIPIENT_DEVICE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_STANDARD , CTRL_RECIPIENT_INTERFACE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_STANDARD , CTRL_RECIPIENT_ENDPOINT ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_STANDARD , CTRL_RECIPIENT_OTHER ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_CLASS , CTRL_RECIPIENT_DEVICE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_CLASS , CTRL_RECIPIENT_INTERFACE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_CLASS , CTRL_RECIPIENT_ENDPOINT ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_CLASS , CTRL_RECIPIENT_OTHER ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_VENDOR , CTRL_RECIPIENT_DEVICE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_VENDOR , CTRL_RECIPIENT_INTERFACE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_VENDOR , CTRL_RECIPIENT_ENDPOINT ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_VENDOR , CTRL_RECIPIENT_OTHER ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_RESERVED , CTRL_RECIPIENT_DEVICE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_RESERVED , CTRL_RECIPIENT_INTERFACE ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_RESERVED , CTRL_RECIPIENT_ENDPOINT ) , <NUM_LIT> ) <EOL> self . assertEqual ( build_request_type ( CTRL_IN , CTRL_TYPE_RESERVED , CTRL_RECIPIENT_OTHER ) , <NUM_LIT> ) <EOL> def get_suite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> suite . addTest ( FindDescriptorTest ( ) ) <EOL> suite . addTest ( unittest . defaultTestLoader . loadTestsFromTestCase ( UtilTest ) ) <EOL> return suite <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> utils . run_tests ( get_suite ( ) ) </s>
