summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qusb.py
blob: df1248da43832e034320a9af21391a8d2a96f0fe (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
import sh
import unittest
from test.helpers.usb import USBDevices


class Qusb(unittest.TestCase):
    params = None
    __resultlist = None  # resultlist is a python list of python dictionaries

    def __init__(self, testname, testfunc, varlist):
        self.params = varlist
        super(Qusb, self).__init__(testfunc)
        self._testMethodDoc = testname
        if "repetitions" in varlist:
            self.__repetitions = varlist["repetitions"]
        else:
            raise Exception('repetitions param inside Qusb must be defined')
        self.__resultlist = []

    def execute(self):
        # get usb device
        dev_obj = USBDevices(self.params["xml"])
        if dev_obj.getMassStorage():
            device = dev_obj.getMassStorage()['disk'] + "1"
        else:
            self.fail("failed: No USB memory found.")
        # check if the device is mounted, and umount it
        try:
            p = sh.findmnt("-n", device)
            if p.exit_code == 0:
                sh.umount(device)
        except sh.ErrorReturnCode as e:
            # error = 1 means "no found"
            pass
        # mount the device
        sh.mkdir("-p", "/mnt/pendrive")
        p = sh.mount(device, "/mnt/pendrive")
        if p.exit_code != 0:
            self.fail("failed: Unable to mount the USB memory device.")
        # execute test
        for i in range(self.__repetitions):
            # copy files
            try:
                p = sh.cp("/var/lib/hwtest-files/usbdatatest.bin",
                          "/var/lib/hwtest-files/usbdatatest.bin.md5",
                          "/mnt/pendrive")
            except sh.ErrorReturnCode as e:
                sh.umount("/mnt/pendrive")
                sh.rmdir("/mnt/pendrive")
                self.fail("failed: Unable to copy files to the USB memory device.")
            # check MD5
            try:
                p = sh.md5sum("/mnt/pendrive/usbdatatest.bin")
            except sh.ErrorReturnCode as e:
                sh.umount("/mnt/pendrive")
                sh.rmdir("/mnt/pendrive")
                self.fail("failed: Unable to calculate MD5 of the copied file.")
            newmd5 = p.stdout.decode().split(" ")[0]
            with open('/mnt/pendrive/usbdatatest.bin.md5', 'r') as outfile:
                oldmd5 = outfile.read()
            outfile.close()
            if newmd5 != oldmd5:
                sh.umount("/mnt/pendrive")
                sh.rmdir("/mnt/pendrive")
                self.fail("failed: MD5 check failed.")
            # delete copied files
            sh.rm("-f", "/mnt/pendrive/usbdatatest.bin", "/mnt/pendrive/usbdatatest.bin.md5")
        # Finish
        sh.umount("/mnt/pendrive")
        sh.rmdir("/mnt/pendrive")

    def getresults(self):
        return self.__resultlist

    def gettextresult(self):
        return ""