summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qusbdual.py
blob: fcbb9aa1d7f5edfdbefc8e24fa51717653517dc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sh
import unittest
import os.path
import time


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.")
        # wait to detect the new device
        time.sleep(3)
        # find the mass storage device
        try:
            p = sh.grep(sh.lsblk("-So", "NAME,VENDOR"), "Linux")
        except sh.ErrorReturnCode:
            self.fail("failed: could not find any mass storage gadget")
        device = p.stdout.decode().split(" ")[0]
        # mount the mass storage gadget
        sh.mkdir("-p", "/mnt/station_ramdisk/hdd_gadget")
        p = sh.mount("-o", "ro", "/dev/" + device, "/mnt/station_ramdisk/hdd_gadget")
        if p.exit_code != 0:
            self.fail("failed: Unable to mount the mass storage gadget.")
        # execute test
        for i in range(int(self.__repetitions)):
            # copy files
            try:
                p = sh.cp("/mnt/station_ramdisk/hdd_gadget/usb-test.bin",
                          "/mnt/station_ramdisk/hdd_gadget/usb-test.bin.md5",
                          "/mnt/station_nfsdisk/")
            except sh.ErrorReturnCode as e:
                sh.umount("/mnt/station_ramdisk/hdd_gadget")
                sh.rmdir("/mnt/station_ramdisk/hdd_gadget")
                self.fail("failed: Unable to copy files through USB.")
            # check if the device is still mounted
            if not os.path.ismount("/mnt/station_ramdisk/hdd_gadget"):
                sh.rm("/mnt/station_ramdisk/hdd_gadget/*")
                sh.rmdir("/mnt/station_ramdisk/hdd_gadget")
                self.fail("failed: USB device unmounted during/after copying files.")
            # Check md5
            try:
                p = sh.md5sum("/mnt/station_nfsdisk/usb-test.bin")
            except sh.ErrorReturnCode as e:
                sh.umount("/mnt/station_ramdisk/hdd_gadget")
                sh.rmdir("/mnt/station_ramdisk/hdd_gadget")
                self.fail("failed: Unable to calculate MD5 of the copied file.")
            newmd5 = p.stdout.decode().split(" ")[0]
            with open('/mnt/station_ramdisk/hdd_gadget/usb-test.bin.md5', 'r') as outfile:
                oldmd5 = outfile.read().rstrip("\n")
            outfile.close()
            if newmd5 != oldmd5:
                sh.umount("/mnt/station_ramdisk/hdd_gadget")
                sh.rmdir("/mnt/station_ramdisk/hdd_gadget")
                self.fail("failed: MD5 check failed.")
            # delete copied files
            sh.rm("-f", "/mnt/station_nfsdisk/usb-test.bin", "/mnt/station_nfsdisk/usb-test.bin.md5")
        # Finish
        sh.umount("/mnt/station_ramdisk/hdd_gadget")
        sh.rmdir("/mnt/station_ramdisk/hdd_gadget")

    def getresults(self):
        return self.__resultlist

    def gettextresult(self):
        return ""