Skip to content

Commit

Permalink
Use endpoint numbers instead of index in loopback test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kauwua committed Jul 16, 2024
1 parent 39b6ca2 commit b79ab55
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 161 deletions.
4 changes: 2 additions & 2 deletions docs/Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ Based on Facedancer's stress test.
NOTE : the above USB stress test should replace this loopback test in most cases.

* Compile : compile the tests with `-DBUILD_TESTS=1`
* Run : flash `test_firmware_usb_loopback.bin` or `test_firmware_usb_loopback_separate_usb_stacks.bin` to one board. Run `test_loopback.py`, `test_loopback_randomize_packetsize.py` or `test_loopback_zlp.py`.
* Run : flash `test_firmware_usb_loopback.bin` or `test_firmware_usb_loopback_separate_usb_stacks.bin` to one board. Run `test_loopback.py` or `test_loopback_randomize_packetsize.py`.

`test_loopback.py` will send data to the board via USB, which will send it back. The test will then check the integrity of the transmission. The test is repeated for all 7 IN/OUT endpoints pairs.

Run `test_loopback_randomize_packetsize.py` to send packets with variable sizes, to check if the board can handle various packet sizes.

`test_loopback_zlp.py` sends packets without data (ZLP, Zero-length packets) to test if the device can handle these properly.
`test_loopbackp.py --zlp` sends packets without data (ZLP, Zero-length packets) to test if the device can handle these properly.

The `test_firmware_usb_loopback_separate_usb_stacks.bin` firmware will create one USB3 device using the USB3 lines of the connector and one USB2 device using the USB2 lines of the connector. You can then run the scripts at the same time for both device.

Expand Down
45 changes: 28 additions & 17 deletions tests/scripts/test_loopback.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def check(byte_array, packet_size, reference_array):
"""
Check the received buffers against what has been sent, to check for integrity
"""
for i in range(len(byte_array) // packet_size):
packet_in = byte_array[i * packet_size:i*packet_size + packet_size]
packet_ref = reference_array[i *
packet_size:i*packet_size + packet_size]
for j in range(len(byte_array) // packet_size):
packet_in = byte_array[j * packet_size:j*packet_size + packet_size]
packet_ref = reference_array[j *
packet_size:j*packet_size + packet_size]
if packet_in != packet_ref:
print(f"Error at {i} ")
print(f"Error at {j} ")
print(packet_in)
print(packet_ref)
return False
Expand Down Expand Up @@ -57,8 +57,13 @@ def send_next_packet(head, ep_in, ep_out, endp_max_packet_size, buffer_in, buffe
parser = argparse.ArgumentParser()
parser.add_argument("--device_num", default=0,
help="In case multiple devices are found, select the index of the device that will be used.", type=int)
parser.add_argument(
"--zlp", help="Only send packets of size 0", action='store_true')
args = parser.parse_args()

if args.zlp:
BUFFER_SIZE = 0

# find our device
devs = usb.core.find(idVendor=0x16c0, idProduct=0x27d8, find_all=True)

Expand Down Expand Up @@ -109,9 +114,9 @@ def send_next_packet(head, ep_in, ep_out, endp_max_packet_size, buffer_in, buffe
SUCCESS = True

fails = []
for i in range(len(ep_in)):
for i, (current_ep_in, current_ep_out) in enumerate(zip(ep_in, ep_out)):
TOTAL_TIME_NS = 0
print(f"EP {i+1}")
print(f"EP {current_ep_in.bEndpointAddress & 0x7f}")
try:
endp_max_packet_size = ENDP_BURST_SIZE * ep_out[i].wMaxPacketSize
buffer_out = array.array(
Expand All @@ -122,13 +127,19 @@ def send_next_packet(head, ep_in, ep_out, endp_max_packet_size, buffer_in, buffe
head = 0
num_sent = 0
total_to_send = len(buffer_out)
while head < total_to_send:
try:
head = send_next_packet(
head, ep_in[i], ep_out[i], endp_max_packet_size, buffer_in, buffer_out)
sys.stdout.write(f"\r{100. * head/total_to_send} % sent")
except usb.core.USBTimeoutError: # HydraUSB3 tends to timeout when handling USB3
print("error timeout, retrying ! ")

if BUFFER_SIZE == 0:
send_next_packet(
head, current_ep_in, current_ep_out, endp_max_packet_size, buffer_in, buffer_out)
else:
while head < total_to_send:
try:
head = send_next_packet(
head, current_ep_in, current_ep_out, endp_max_packet_size, buffer_in, buffer_out)
sys.stdout.write(
f"\r{100. * head/total_to_send} % sent")
except usb.core.USBTimeoutError: # HydraUSB3 tends to timeout when handling USB3
print("error timeout, retrying ! ")

STOP = time.time_ns()
sys.stdout.write("\r")
Expand All @@ -139,12 +150,12 @@ def send_next_packet(head, ep_in, ep_out, endp_max_packet_size, buffer_in, buffe
f"Success ! Transfer rate with only transfer {len(buffer_in) / ((TOTAL_TIME_NS) * 1e-9) * 1e-6} MB/s")
else:
print("Error")
fails.append(i)
fails.append(current_ep_in.bEndpointAddress & 0x7f)
except:
fails.append(i)
fails.append(current_ep_in.bEndpointAddress & 0x7f)

print(
f"There have been {len(fails)} fails. Endpoints {[ep + 1 for ep in fails]} failed ")
f"There have been {len(fails)} fails. Endpoints {fails} failed ")

if len(fails) == 0:
print("Test successful ! ")
142 changes: 0 additions & 142 deletions tests/scripts/test_loopback_zlp.py

This file was deleted.

0 comments on commit b79ab55

Please sign in to comment.