def test_msd(test_info, msd): """MSD endpoint tests""" # Simple read mbr = msd.scsi_read10(0, 1) test_info.info("MBR[0:16]: %s" % mbr[0:16]) # Test FAT filesystem fat = Fat(msd) print(fat.mbr) # Grab entries in the root directory root_dir = fat.root_dir for entry in root_dir: if entry["DIR_Name"][0] != "\0": print(entry) # Trigger a remount dir_idx = root_dir.find_free_entry_index() root_dir[dir_idx]["DIR_Name"] = "REFRESH ACT" root_dir_data = root_dir.pack() msd.scsi_write10(root_dir.sector, root_dir_data) test_info.info("Added file to root directory") start = time.time() while time.time() - start < DISMOUNT_TIME_S: try: msd.scsi_read10(0, 1) except msd.SCSIError: test_info.info("Dismount detected") break else: test_info.failure("Device failed to dismount") start = time.time() while time.time() - start < DISMOUNT_TIME_S: try: msd.scsi_read10(0, 1) test_info.info("Mount detected") break except msd.SCSIError: pass else: test_info.failure("Device failed to mount")
def test_all(test_info, cdc, hid, msd): """Test all endpoints in parallel""" mutex = threading.RLock() terminate = False error_msg_list = [] def _safe_print(message): """Thread safe wrapper to print messages""" with mutex: print(message) def _test_msd(): """MSD thread entry point for parallel testing""" try: _safe_print("msd started") msd_data = msd.scsi_read10(100, 1) while not terminate: #msd_data = 'x' * 1024 * 16 # 16KB msd.scsi_write10(100, msd_data) _safe_print("msd end") except: error_msg_list.append("MSD test failed") raise def _test_cdc(): """CDC thread entry point for parallel testing""" try: _safe_print("cdc started") while not terminate: cdc.set_line_coding(115200) cdc.get_line_coding() cdc.send_break(cdc.SEND_BREAK_ON) cdc.send_break(cdc.SEND_BREAK_OFF) cdc.read(1024) cdc.write("Hello world") cdc.read(1024) _safe_print("cdc end") except: error_msg_list.append("CDC test failed") raise def _test_hid(): """HID thread entry point for parallel testing""" try: _safe_print("hid started") data = bytearray(64) data[0] = 0x80 while not terminate: hid.set_report(data) resp = hid.get_report(64) assert resp[0] == 0x80 _safe_print("hid end") except: error_msg_list.append("HID test failed") raise thread_list = [] for function in (_test_msd, _test_cdc, _test_hid): thread = threading.Thread(target=function) thread.start() thread_list.append(thread) time.sleep(10) terminate = True for thread in thread_list: thread.join() for error in error_msg_list: test_info.failure(error)
def test_msd_stall(test_info, msd): """Test stalls coming at various times in the middle of MSD xfers""" fat = Fat(msd) root_dir = fat.root_dir dir_idx = root_dir.find_free_entry_index() root_dir[dir_idx]["DIR_Name"] = "REFRESH ACT" root_dir_data = root_dir.pack() # Test that a write fails if media is removed after the CBW # stage but before the CSW stage msd.scsi_write10(root_dir.sector, root_dir_data) msd.delay_cbw_to_data = 1.0 retval = msd.CSW_STATUS_PASSED try: msd.scsi_write10(0, bytearray(512)) test_info.failure("Device failed to stall data stage") except msd.SCSIError as error: retval = error.value msd.delay_cbw_to_data = 0 # Make sure device still works as expected time.sleep(3) msd.scsi_read10(0, 1) msd.scsi_write10(0, bytearray(512)) if retval == msd.CSW_STATUS_FAILED: test_info.info("Test CBW,Stall,Data OUT - Pass") else: test_info.failure("Device returned wrong status") # Test that a write succeeds even if media is removed # after the OUT stage but before the CSW stage msd.scsi_write10(root_dir.sector, root_dir_data) msd.delay_data_to_csw = 1.0 msd.scsi_write10(0, bytearray(512)) msd.delay_data_to_csw = 0 # Make sure device still works as expected time.sleep(3) msd.scsi_read10(0, 1) msd.scsi_write10(0, bytearray(512)) test_info.info("Test DATA OUT,Stall,CSW - Pass") # Test that a read succeeds even if media is removed # after the IN stage but before the CSW stage msd.scsi_write10(root_dir.sector, root_dir_data) msd.delay_data_to_csw = 1.0 resp = msd.scsi_read10(0, 1) assert len(resp) == 512 msd.delay_data_to_csw = 0 # Make sure device still works as expected time.sleep(3) msd.scsi_read10(0, 1) msd.scsi_write10(0, bytearray(512)) test_info.info("Test DATA IN,Stall,CSW - Pass") # Test that a test unit ready succeeds even if media is removed # after the CBW stage but before the CSW stage msd.scsi_write10(root_dir.sector, root_dir_data) msd.delay_data_to_csw = 1.0 resp = msd.scsi_test_unit_ready() msd.delay_data_to_csw = 0 # Make sure device still works as expected time.sleep(3) msd.scsi_read10(0, 1) msd.scsi_write10(0, bytearray(512)) if resp == msd.CSW_STATUS_PASSED: test_info.info("Test CBW,Stall,CSW - Pass") else: test_info.failure("Test CBW,Stall,CSW - Failed") # Test that a test unit ready succeeds even if media is removed # after the CBW stage but before the CSW stage msd.scsi_write10(root_dir.sector, root_dir_data) time.sleep(1.0) resp = msd.scsi_test_unit_ready() # Make sure device still works as expected time.sleep(3) msd.scsi_read10(0, 1) msd.scsi_write10(0, bytearray(512)) if resp == msd.CSW_STATUS_FAILED: test_info.info("Test CBW,Stall,CSW - Pass") else: test_info.failure("Test CBW,Stall,CSW - Failed")