summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qusb.py
blob: b5d152ea4f98afa60189ea8733d60aa6dda677fa (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
84
85
86
87
88
89
90
91
92
93
94
import sh
import unittest
from test.helpers.usb import USBDevices
import os.path


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", "/tmp/station/pendrive")
        p = sh.mount(device, "/tmp/station/pendrive")
        if p.exit_code != 0:
            self.fail("failed: Unable to mount the USB memory device.")
        # execute test
        for i in range(int(self.__repetitions)):
            # copy files
            try:
                p = sh.cp("/var/lib/hwtest-files/usbdatatest.bin",
                          "/var/lib/hwtest-files/usbdatatest.bin.md5",
                          "/tmp/station/pendrive")
            except sh.ErrorReturnCode as e:
                try:
                    sh.umount("/tmp/station/pendrive")
                except sh.ErrorReturnCode:
                    pass
                sh.rmdir("/tmp/station/pendrive")
                self.fail("failed: Unable to copy files to the USB memory device.")
            # check if the device is still mounted
            if not os.path.ismount("/tmp/station/pendrive"):
                sh.rm("/tmp/station/pendrive/*")
                sh.rmdir("/tmp/station/pendrive")
                self.fail("failed: USB device unmounted during/after copying files.")
            # check MD5
            try:
                p = sh.md5sum("/tmp/station/pendrive/usbdatatest.bin")
            except sh.ErrorReturnCode as e:
                try:
                    sh.umount("/tmp/station/pendrive")
                except sh.ErrorReturnCode:
                    pass
                sh.rmdir("/tmp/station/pendrive")
                self.fail("failed: Unable to calculate MD5 of the copied file.")
            newmd5 = p.stdout.decode().split(" ")[0]
            with open('/tmp/station/pendrive/usbdatatest.bin.md5', 'r') as outfile:
                oldmd5 = outfile.read().rstrip("\n")
            outfile.close()
            if newmd5 != oldmd5:
                try:
                    sh.umount("/tmp/station/pendrive")
                except sh.ErrorReturnCode:
                    pass
                sh.rmdir("/tmp/station/pendrive")
                self.fail("failed: MD5 check failed.")
            # delete copied files
            sh.rm("-f", "/tmp/station/pendrive/usbdatatest.bin", "/tmp/station/pendrive/usbdatatest.bin.md5")
        # Finish
        try:
            sh.umount("/tmp/station/pendrive")
        except sh.ErrorReturnCode:
            pass
        sh.rmdir("/tmp/station/pendrive")

    def getresults(self):
        return self.__resultlist

    def gettextresult(self):
        return ""