import sh import unittest import os.path class Qusbdual(unittest.TestCase): params = None __resultlist = None # resultlist is a python list of python dictionaries def __init__(self, testname, testfunc, varlist): self.params = varlist super(Qusbdual, self).__init__(testfunc) self._testMethodDoc = testname if "repetitions" in varlist: self.__repetitions = varlist["repetitions"] else: raise Exception('repetitions param inside Qusbdual must be defined') self.__resultlist = [] def execute(self): # check if file-as-filesystem exists if not os.path.isfile('/var/lib/hwtest-files/mass-otg-test.img'): self.fail("failed: Unable to find file-as-filesystem image.") # generate mass storage gadget p = sh.modprobe("g_mass_storage", "file=/var/lib/hwtest-files/mass-otg-test.img") if p.exit_code != 0: self.fail("failed: Unable to create a mass storage gadget.") # find the mass storage device try: p = sh.grep(sh.lsblk("-So", "NAME,VENDOR"), "Linux") except sh.ErrorReturnCode as e: self.fail("failed: could not find any mass storage gadget") device = p.stdout.decode().split(" ")[0] # mount the mass storage gadget sh.mkdir("-p", "/tmp/station/hdd_gadget") p = sh.mount("-o", "ro", "/dev/" + device, "/tmp/station/hdd_gadget") if p.exit_code != 0: self.fail("failed: Unable to mount the mass storage gadget.") # execute test for i in range(self.__repetitions): # copy files try: p = sh.cp("/tmp/station/hdd_gadget/usb-test.bin", "/tmp/station/hdd_gadget/usb-test.bin.md5", "/tmp/stationnfs") except sh.ErrorReturnCode as e: sh.umount("/tmp/station/hdd_gadget") sh.rmdir("/tmp/station/hdd_gadget") self.fail("failed: Unable to copy files through USB.") # Check md5 try: p = sh.md5sum("/tmp/stationnfs/usb-test.bin") except sh.ErrorReturnCode as e: sh.umount("/tmp/station/hdd_gadget") sh.rmdir("/tmp/station/hdd_gadget") self.fail("failed: Unable to calculate MD5 of the copied file.") newmd5 = p.stdout.decode().split(" ")[0] with open('/tmp/station/hdd_gadget/usb-test.bin.md5', 'r') as outfile: oldmd5 = outfile.read() outfile.close() if newmd5 != oldmd5: sh.umount("/tmp/station/hdd_gadget") sh.rmdir("/tmp/station/hdd_gadget") self.fail("failed: MD5 check failed.") # delete copied files sh.rm("-f", "/tmp/stationnfs/usb-test.bin", "/tmp/stationnfs/usb-test.bin.md5") # Finish sh.umount("/tmp/station/hdd_gadget") sh.rmdir("/tmp/station/hdd_gadget") def getresults(self): return self.__resultlist def gettextresult(self): return ""