Skip to content

Commit 094f317

Browse files
maciejbocianski0xc0170
authored andcommitted
pyusb_basic test formatting fixes
1 parent 106940d commit 094f317

File tree

1 file changed

+154
-146
lines changed

1 file changed

+154
-146
lines changed

TESTS/host_tests/pyusb_basic.py

Lines changed: 154 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ def get_interface(dev, interface, alternate=0):
5555
VENDOR_TEST_CTRL_OUT_SIZES = 10
5656
VENDOR_TEST_UNSUPPORTED_REQUEST = 32
5757

58-
REQUEST_GET_STATUS = 0 # done
59-
REQUEST_CLEAR_FEATURE = 1 # done
60-
REQUEST_SET_FEATURE = 3 # done
61-
REQUEST_SET_ADDRESS = 5 # ???
58+
REQUEST_GET_STATUS = 0
59+
REQUEST_CLEAR_FEATURE = 1
60+
REQUEST_SET_FEATURE = 3
61+
REQUEST_SET_ADDRESS = 5
6262
REQUEST_GET_DESCRIPTOR = 6
63-
REQUEST_SET_DESCRIPTOR = 7 # done
64-
REQUEST_GET_CONFIGURATION = 8 # done
65-
REQUEST_SET_CONFIGURATION = 9 # done
66-
REQUEST_GET_INTERFACE = 10 # done
67-
REQUEST_SET_INTERFACE = 11 # done
68-
REQUEST_SYNCH_FRAME = 12 # almost done
63+
REQUEST_SET_DESCRIPTOR = 7
64+
REQUEST_GET_CONFIGURATION = 8
65+
REQUEST_SET_CONFIGURATION = 9
66+
REQUEST_GET_INTERFACE = 10
67+
REQUEST_SET_INTERFACE = 11
68+
REQUEST_SYNCH_FRAME = 12
6969

7070
FEATURE_ENDPOINT_HALT = 0
7171
FEATURE_DEVICE_REMOTE_WAKEUP = 1
@@ -269,9 +269,11 @@ def setup(self):
269269

270270
self.register_callback('reset_support', self._callback_reset_support)
271271

272+
272273
def result(self):
273274
return self.__result
274275

276+
275277
def teardown(self):
276278
pass
277279

@@ -292,150 +294,18 @@ def lineno():
292294
"""Returns the current line number in our program."""
293295
return inspect.currentframe().f_back.f_lineno
294296

297+
295298
def raise_if_different(expected, actual, line, text=''):
296299
"""Raise a RuntimeError if actual is different than expected."""
297300
if expected != actual:
298301
raise RuntimeError('[{}]:{}, {} Got {!r}, expected {!r}'.format(__file__, line, text, actual, expected))
299302

303+
300304
def raise_unconditionally(line, text=''):
301305
"""Raise a RuntimeError unconditionally."""
302306
raise RuntimeError('[{}]:{}, {}'.format(__file__, line, text))
303307

304308

305-
def test_device(serial_number, log=print):
306-
dev = usb.core.find(custom_match=TestMatch(serial_number))
307-
if dev is None:
308-
log("Device not found")
309-
return
310-
311-
## --Control Tests-- ##
312-
#control_basic_test(dev, log)
313-
# Test control IN/OUT/NODATA
314-
control_stall_test(dev, log)
315-
# Invalid control in/out/nodata requests are stalled
316-
# Stall during different points in the control transfer
317-
#control_sizes_test(dev, log)
318-
# Test control requests of various data stage sizes (1,8,16,32,64,255,256,...)
319-
control_stress_test(dev, log)
320-
# normal and delay mode
321-
322-
## --Endpoint test-- ##
323-
#for each endpoint
324-
#-test all allowed wMaxPacketSize sizes and transfer types
325-
#-stall tests
326-
#-set/clear stall control request
327-
#-stall at random points of sending/receiveing data
328-
#-test aborting an in progress transfer
329-
#test as many endpoints at once as possible
330-
#test biggest configuration possible
331-
332-
## --physical test-- ##
333-
#-reset notification/handling
334-
#-connect/disconnect tests - have device disconnect and then reconnect
335-
#-disconnect during various phases of control transfers and endpoint transfers
336-
#-suspend/resume tests (may not be possible to test with current framework)
337-
#-suspend/resume notifications
338-
339-
## -- Stress tests-- ##
340-
#-concurrent tests (all endpoints at once including control)
341-
#-concurrent tests + reset + delay
342-
343-
## -- other tests-- ##
344-
#-report throughput for in/out of control, bulk, interrupt and iso transfers
345-
#-verify that construction/destruction repeatedly works gracefully
346-
347-
intf = get_interface(dev, 0, 0)
348-
349-
# Find endpoints
350-
bulk_in = None
351-
bulk_out = None
352-
int_in = None
353-
int_out = None
354-
355-
for endpoint in intf:
356-
log("Processing endpoint %s" % endpoint)
357-
ep_type = endpoint.bmAttributes & 0x3
358-
if ep_type == 2:
359-
if endpoint.bEndpointAddress & 0x80:
360-
assert bulk_in is None
361-
bulk_in = endpoint
362-
else:
363-
assert bulk_out is None
364-
bulk_out = endpoint
365-
elif ep_type == 3:
366-
if endpoint.bEndpointAddress & 0x80:
367-
assert int_in is None
368-
int_in = endpoint
369-
else:
370-
assert int_out is None
371-
int_out = endpoint
372-
assert bulk_in is not None
373-
assert bulk_out is not None
374-
assert int_in is not None
375-
assert int_out is not None
376-
bulk_out.write("hello" + "x" *256);
377-
int_out.write("world" + "x" *256);
378-
379-
dev.set_interface_altsetting(0, 1)
380-
381-
intf = get_interface(dev, 0, 0)
382-
383-
# Find endpoints
384-
bulk_in = None
385-
bulk_out = None
386-
int_in = None
387-
int_out = None
388-
389-
for endpoint in intf:
390-
log("Processing endpoint %s" % endpoint)
391-
ep_type = endpoint.bmAttributes & 0x3
392-
if ep_type == 2:
393-
if endpoint.bEndpointAddress & 0x80:
394-
assert bulk_in is None
395-
bulk_in = endpoint
396-
else:
397-
assert bulk_out is None
398-
bulk_out = endpoint
399-
elif ep_type == 3:
400-
if endpoint.bEndpointAddress & 0x80:
401-
assert int_in is None
402-
int_in = endpoint
403-
else:
404-
assert int_out is None
405-
int_out = endpoint
406-
assert bulk_in is not None
407-
assert bulk_out is not None
408-
assert int_in is not None
409-
assert int_out is not None
410-
bulk_out.write("hello2" + "x" *256);
411-
int_out.write("world2" + "x" *256);
412-
413-
414-
t = Thread(target=write_data, args=(bulk_out,))
415-
t.start()
416-
417-
for _ in range(10):
418-
request_type = build_request_type(CTRL_OUT, CTRL_TYPE_VENDOR,
419-
CTRL_RECIPIENT_DEVICE)
420-
request = VENDOR_TEST_CTRL_NONE_DELAY
421-
value = 0 # Always 0 for this request
422-
index = 0 # Communication interface
423-
length = 0 # No data
424-
dev.ctrl_transfer(request_type, request, value, index, length, 5000)
425-
426-
t.join()
427-
428-
429-
def write_data(pipe):
430-
print("Write data running")
431-
count = 0
432-
for _ in range(40):
433-
pipe.write("Value is %s" % count)
434-
count += 1
435-
print("Count %s" % count)
436-
time.sleep(0.5)
437-
438-
439309
def control_basic_test(dev, vendor_id, product_id, log):
440310
get_status_test(dev, log)
441311
set_clear_feature_test(dev, log)
@@ -915,7 +785,6 @@ def control_data_test(dev, sizes_list, log):
915785

916786

917787
def control_stress_test(dev, log):
918-
919788
# Test various patterns of control transfers
920789
#
921790
# Some devices have had problems with back-to-back
@@ -1025,13 +894,152 @@ def repeated_construction_destruction_test(log):
1025894
yield
1026895

1027896

897+
"""
898+
For documentation purpose until test writing finished
899+
TODO: remove this if not needed anymore
900+
901+
902+
def test_device(serial_number, log=print):
903+
dev = usb.core.find(custom_match=TestMatch(serial_number))
904+
if dev is None:
905+
log("Device not found")
906+
return
907+
908+
## --Control Tests-- ##
909+
#control_basic_test(dev, log)
910+
# Test control IN/OUT/NODATA
911+
control_stall_test(dev, log)
912+
# Invalid control in/out/nodata requests are stalled
913+
# Stall during different points in the control transfer
914+
#control_sizes_test(dev, log)
915+
# Test control requests of various data stage sizes (1,8,16,32,64,255,256,...)
916+
control_stress_test(dev, log)
917+
# normal and delay mode
918+
919+
## --Endpoint test-- ##
920+
#for each endpoint
921+
#-test all allowed wMaxPacketSize sizes and transfer types
922+
#-stall tests
923+
#-set/clear stall control request
924+
#-stall at random points of sending/receiveing data
925+
#-test aborting an in progress transfer
926+
#test as many endpoints at once as possible
927+
#test biggest configuration possible
928+
929+
## --physical test-- ##
930+
#-reset notification/handling
931+
#-connect/disconnect tests - have device disconnect and then reconnect
932+
#-disconnect during various phases of control transfers and endpoint transfers
933+
#-suspend/resume tests (may not be possible to test with current framework)
934+
#-suspend/resume notifications
935+
936+
## -- Stress tests-- ##
937+
#-concurrent tests (all endpoints at once including control)
938+
#-concurrent tests + reset + delay
939+
940+
## -- other tests-- ##
941+
#-report throughput for in/out of control, bulk, interrupt and iso transfers
942+
#-verify that construction/destruction repeatedly works gracefully
943+
944+
intf = get_interface(dev, 0, 0)
945+
946+
# Find endpoints
947+
bulk_in = None
948+
bulk_out = None
949+
int_in = None
950+
int_out = None
951+
952+
for endpoint in intf:
953+
log("Processing endpoint %s" % endpoint)
954+
ep_type = endpoint.bmAttributes & 0x3
955+
if ep_type == 2:
956+
if endpoint.bEndpointAddress & 0x80:
957+
assert bulk_in is None
958+
bulk_in = endpoint
959+
else:
960+
assert bulk_out is None
961+
bulk_out = endpoint
962+
elif ep_type == 3:
963+
if endpoint.bEndpointAddress & 0x80:
964+
assert int_in is None
965+
int_in = endpoint
966+
else:
967+
assert int_out is None
968+
int_out = endpoint
969+
assert bulk_in is not None
970+
assert bulk_out is not None
971+
assert int_in is not None
972+
assert int_out is not None
973+
bulk_out.write("hello" + "x" *256);
974+
int_out.write("world" + "x" *256);
975+
976+
dev.set_interface_altsetting(0, 1)
977+
978+
intf = get_interface(dev, 0, 0)
979+
980+
# Find endpoints
981+
bulk_in = None
982+
bulk_out = None
983+
int_in = None
984+
int_out = None
985+
986+
for endpoint in intf:
987+
log("Processing endpoint %s" % endpoint)
988+
ep_type = endpoint.bmAttributes & 0x3
989+
if ep_type == 2:
990+
if endpoint.bEndpointAddress & 0x80:
991+
assert bulk_in is None
992+
bulk_in = endpoint
993+
else:
994+
assert bulk_out is None
995+
bulk_out = endpoint
996+
elif ep_type == 3:
997+
if endpoint.bEndpointAddress & 0x80:
998+
assert int_in is None
999+
int_in = endpoint
1000+
else:
1001+
assert int_out is None
1002+
int_out = endpoint
1003+
assert bulk_in is not None
1004+
assert bulk_out is not None
1005+
assert int_in is not None
1006+
assert int_out is not None
1007+
bulk_out.write("hello2" + "x" *256);
1008+
int_out.write("world2" + "x" *256);
1009+
1010+
1011+
t = Thread(target=write_data, args=(bulk_out,))
1012+
t.start()
1013+
1014+
for _ in range(10):
1015+
request_type = build_request_type(CTRL_OUT, CTRL_TYPE_VENDOR,
1016+
CTRL_RECIPIENT_DEVICE)
1017+
request = VENDOR_TEST_CTRL_NONE_DELAY
1018+
value = 0 # Always 0 for this request
1019+
index = 0 # Communication interface
1020+
length = 0 # No data
1021+
dev.ctrl_transfer(request_type, request, value, index, length, 5000)
1022+
1023+
t.join()
1024+
1025+
1026+
def write_data(pipe):
1027+
print("Write data running")
1028+
count = 0
1029+
for _ in range(40):
1030+
pipe.write("Value is %s" % count)
1031+
count += 1
1032+
print("Count %s" % count)
1033+
time.sleep(0.5)
1034+
1035+
10281036
def main():
10291037
parser = ArgumentParser(description="USB basic test")
10301038
parser.add_argument('serial', help='USB serial number of DUT')
10311039
args = parser.parse_args()
10321040
ret = test_device(args.serial)
10331041
print("Test %s" % "passed" if ret else "failed")
10341042
1035-
10361043
if __name__ == "__main__":
10371044
main()
1045+
"""

0 commit comments

Comments
 (0)