Skip to content

Commit a34526d

Browse files
fkjagodzinskic1728p9
authored andcommitted
Tests: USB: Generic: Update documentation
1 parent 3a66dab commit a34526d

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

TESTS/host_tests/pyusb_basic.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,11 @@ def control_stress_test(dev, log):
916916

917917

918918
def find_ep_pair(intf, endpoint_type):
919+
"""Find an OUT and IN endpoint pair.
920+
921+
Raise a RuntimeError if any endpoint could not be found
922+
or wMaxPacketSize is not equal for both endpoints.
923+
"""
919924
ep_out = usb.util.find_descriptor(
920925
intf, custom_match=lambda e:
921926
usb.util.endpoint_type(e.bmAttributes) == endpoint_type and
@@ -934,13 +939,24 @@ def find_ep_pair(intf, endpoint_type):
934939

935940

936941
def loopback_ep_test(ep_out, ep_in, payload_size):
942+
"""Send and receive random data using OUT/IN endpoint pair.
943+
944+
Verify that data received from IN endpoint is equal to
945+
data sent to OUT endpoint.
946+
Raise a RuntimeError if data does not match.
947+
"""
937948
payload_out = array.array('B', (random.randint(0x00, 0xff) for _ in range(payload_size)))
938949
ep_out.write(payload_out)
939950
payload_in = ep_in.read(ep_in.wMaxPacketSize)
940951
raise_if_different(payload_out, payload_in, lineno(), 'Payloads mismatch.')
941952

942953

943954
def random_size_loopback_ep_test(ep_out, ep_in, failure, error, seconds, log, min_payload_size=1):
955+
"""Repeat data transfer test for OUT/IN endpoint pair for a given time.
956+
957+
Set a failure Event if OUT/IN data verification fails.
958+
Set an error Event if unexpected USB error occurs.
959+
"""
944960
end_ts = time.time() + seconds
945961
while time.time() < end_ts and not failure.is_set() and not error.is_set():
946962
payload_size = random.randint(min_payload_size, ep_out.wMaxPacketSize)
@@ -958,6 +974,12 @@ def random_size_loopback_ep_test(ep_out, ep_in, failure, error, seconds, log, mi
958974

959975

960976
def halt_ep_test(dev, ep_out, ep_in, ep_to_halt, log):
977+
"""OUT/IN endpoint halt test.
978+
979+
Verify that halting an endpoint at a random point of OUT or IN transfer
980+
raises a USBError.
981+
Raise a RuntimeError if halt fails or any unexpected error occurs.
982+
"""
961983
MIN_HALT_DELAY = 0.01
962984
MAX_HALT_DELAY = 0.1
963985
delay = random.uniform(MIN_HALT_DELAY, MAX_HALT_DELAY)
@@ -969,6 +991,7 @@ def halt_ep_test(dev, ep_out, ep_in, ep_to_halt, log):
969991
ctrl_error = Event()
970992

971993
def timer_handler():
994+
"""Halt an endpoint using a USB control request."""
972995
try:
973996
dev.ctrl_transfer(**ctrl_kwargs)
974997
except Exception as err:
@@ -995,10 +1018,19 @@ def timer_handler():
9951018

9961019
USB_ERROR_FMT = str('Got {0!r} while testing endpoints '
9971020
'{1.bEndpointAddress:#04x}({1.wMaxPacketSize:02}) and '
998-
'{2.bEndpointAddress:#04x}({2.wMaxPacketSize:02}) with a random payload of {3} B.')
1021+
'{2.bEndpointAddress:#04x}({2.wMaxPacketSize:02}) with '
1022+
'a random payload of {3} B.')
9991023

10001024

10011025
def ep_test_data_correctness(dev, log, verbose=False):
1026+
"""Test data correctness for every OUT/IN endpoint pair.
1027+
1028+
Given a USB device with multiple OUT/IN endpoint pairs
1029+
When the host sends random payloads up to wMaxPacketSize in size
1030+
to an OUT endpoint of the device,
1031+
and then the device sends data back to host using an IN endpoint
1032+
Then data sent and received by host is equal for every endpoint pair
1033+
"""
10021034
cfg = dev.get_active_configuration()
10031035
for intf in cfg:
10041036
log('interface {}, alt {} -- '.format(intf.bInterfaceNumber, intf.bAlternateSetting), end='')
@@ -1047,6 +1079,13 @@ def ep_test_data_correctness(dev, log, verbose=False):
10471079

10481080

10491081
def ep_test_halt(dev, log, verbose=False):
1082+
"""Test endpoint halt for every OUT/IN endpoint pair.
1083+
1084+
Given a USB device with multiple OUT/IN endpoint pairs
1085+
When the host issues an endpoint halt control request at a random point
1086+
of OUT or IN transfer
1087+
Then the endpoint is stalled and all further transfers fail
1088+
"""
10501089
cfg = dev.get_active_configuration()
10511090
for intf in cfg:
10521091
log('interface {}, alt {} -- '.format(intf.bInterfaceNumber, intf.bAlternateSetting), end='')
@@ -1092,6 +1131,13 @@ def ep_test_halt(dev, log, verbose=False):
10921131

10931132

10941133
def ep_test_parallel_transfers(dev, log, verbose=False):
1134+
"""Test simultaneous data transfers for multiple OUT/IN endpoint pairs.
1135+
1136+
Given a USB device with multiple OUT/IN endpoint pairs
1137+
When multiple OUT and IN endpoints are used to transfer random test data
1138+
Then all transfers succeed
1139+
and data received equals data sent for every endpoint pair
1140+
"""
10951141
cfg = dev.get_active_configuration()
10961142
for intf in cfg:
10971143
log('interface {}, alt {} -- '.format(intf.bInterfaceNumber, intf.bAlternateSetting), end='')
@@ -1145,6 +1191,14 @@ def ep_test_parallel_transfers(dev, log, verbose=False):
11451191

11461192

11471193
def ep_test_parallel_transfers_ctrl(dev, log, verbose=False):
1194+
"""Test simultaneous data transfers in parallel with control transfers.
1195+
1196+
Given a USB device with multiple OUT/IN endpoint pairs
1197+
When multiple OUT and IN endpoints are used to transfer random data
1198+
and control requests are processed in parallel
1199+
Then all transfers succeed
1200+
and for every endpoint pair, data received by host equals data sent by host
1201+
"""
11481202
cfg = dev.get_active_configuration()
11491203
for intf in cfg:
11501204
log('interface {}, alt {} -- '.format(intf.bInterfaceNumber, intf.bAlternateSetting), end='')
@@ -1201,6 +1255,12 @@ def ep_test_parallel_transfers_ctrl(dev, log, verbose=False):
12011255

12021256

12031257
def ep_test_abort(dev, log, verbose=False):
1258+
"""Test aborting data transfer for every OUT/IN endpoint pair.
1259+
1260+
Given a USB device with multiple OUT/IN endpoint pairs
1261+
When a device aborts an in progress data transfer
1262+
Then no more data is transmitted
1263+
"""
12041264
NUM_PACKETS_UNTIL_ABORT = 2
12051265
NUM_PACKETS_AFTER_ABORT = 8
12061266
cfg = dev.get_active_configuration()

TESTS/usb_device/basic/main.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ void control_stress_test()
200200
}
201201
}
202202

203+
/** Test data correctness for every OUT/IN endpoint pair
204+
*
205+
* Given a USB device with multiple OUT/IN endpoint pairs
206+
* When the host sends random payloads up to wMaxPacketSize in size
207+
* to an OUT endpoint of the device,
208+
* and then the device sends data back to host using an IN endpoint
209+
* Then data sent and received by host is equal for every endpoint pair
210+
*/
203211
void ep_test_data_correctness()
204212
{
205213
uint16_t vendor_id = 0x0d28;
@@ -230,6 +238,13 @@ void ep_test_data_correctness()
230238
}
231239
}
232240

241+
/** Test endpoint halt for every OUT/IN endpoint pair
242+
*
243+
* Given a USB device with multiple OUT/IN endpoint pairs
244+
* When the host issues an endpoint halt control request at a random point
245+
* of OUT or IN transfer
246+
* Then the endpoint is stalled and all further transfers fail
247+
*/
233248
void ep_test_halt()
234249
{
235250
uint16_t vendor_id = 0x0d28;
@@ -260,6 +275,13 @@ void ep_test_halt()
260275
}
261276
}
262277

278+
/** Test simultaneous data transfers for multiple OUT/IN endpoint pairs
279+
*
280+
* Given a USB device with multiple OUT/IN endpoint pairs
281+
* When multiple OUT and IN endpoints are used to transfer random data in parallel
282+
* Then all transfers succeed
283+
* and for every endpoint pair, data received by host equals data sent by host
284+
*/
263285
void ep_test_parallel_transfers()
264286
{
265287
uint16_t vendor_id = 0x0d28;
@@ -290,6 +312,14 @@ void ep_test_parallel_transfers()
290312
}
291313
}
292314

315+
/** Test simultaneous data transfers in parallel with control transfers
316+
*
317+
* Given a USB device with multiple OUT/IN endpoint pairs
318+
* When multiple OUT and IN endpoints are used to transfer random data
319+
* and control requests are processed in parallel
320+
* Then all transfers succeed
321+
* and for every endpoint pair, data received by host equals data sent by host
322+
*/
293323
void ep_test_parallel_transfers_ctrl()
294324
{
295325
uint16_t vendor_id = 0x0d28;
@@ -320,6 +350,12 @@ void ep_test_parallel_transfers_ctrl()
320350
}
321351
}
322352

353+
/** Test aborting data transfer for every OUT/IN endpoint pair
354+
*
355+
* Given a USB device with multiple OUT/IN endpoint pairs
356+
* When a device aborts an in progress data transfer
357+
* Then no more data is transmitted
358+
*/
323359
void ep_test_abort()
324360
{
325361
uint16_t vendor_id = 0x0d28;

0 commit comments

Comments
 (0)