~aleteoryx/muditaos

ref: 92e042add14cac3d9d1a6cdbf7a72eaf0749b33c muditaos/test/pytest/service-desktop/test_filesystem_ops.py -rw-r--r-- 4.6 KiB
92e042ad — Lefucjusz [BH-2065] Fix race condition between CMake targets 1 year, 5 months ago
                                                                                
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
import pytest
import filecmp
import re

from harness.api.developermode import PhoneModeLock
from harness.api.filesystem import get_file, put_file, FsInitGet, FsGetChunk, FsRemoveFile, FsRenameFile, FsListFiles
from harness import log
from harness.request import TransactionError
from harness.interface.defs import Status


def setPasscode(harness, flag):
    PhoneModeLock(flag).run(harness)


testFileName = "lorem-ipsum.txt"
osLogFileName = "MuditaOS.log"

sysUserPath = "/sys/user"
sysUserLogsPath = sysUserPath + "/logs/"
sysUserMusicPath = sysUserPath + "/music/"


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.rt1051
def test_send_file(harness):
    """
    Attempt requesting and sending file to Pure
    """
    put_file(harness, testFileName, sysUserPath)


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.rt1051
def test_get_file_list(harness):
    """
    Test listing files
    """
    file_list = FsListFiles(sysUserMusicPath).run(harness)
    for file in file_list:
        assert re.match(r"" + sysUserMusicPath + ".*\.mp3", file["path"])
        assert (type(file["fileSize"]) is int)


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.rt1051
def test_get_file_back(harness):
    """
    Attempt requesting and transfering file data
    """
    loadedFile = "./lorem-ipsum-2.txt"

    get_file(harness, testFileName, "./", sysUserPath, loadedFile)

    assert filecmp.cmp(testFileName, loadedFile, shallow=True)


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.rt1051
def test_rename_file(harness):
    """
    Attempt renaming a file and confirm by getting it back and comparing with original
    """
    sourceFile = "/sys/user/" + testFileName
    renamedFile = "renamed_" + testFileName
    destFile = "/sys/user/" + renamedFile

    FsRenameFile(sourceFile, destFile).run(harness)

    get_file(harness, renamedFile, "./", sysUserPath)

    assert filecmp.cmp(testFileName, renamedFile, shallow=True)


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.rt1051
def test_delete_files(harness):
    """
    Attempt deleting a file and confirm removal by attempting to get it back
    """
    fileToRemove = sysUserPath + "/" + "renamed_" + testFileName
    FsRemoveFile(fileToRemove).run(harness)

    with pytest.raises(TransactionError, match=r".*" + str(Status.NotFound.value) + ".*"):
        get_file(harness, testFileName, "./", sysUserPath)


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.rt1051
def test_get_not_existing_file(harness):
    """
    Attempt requesting not exiting file
    """

    """
    Getting a large file may hit screen auto lock.
    We need to disable pass code for duration of test
    """

    testFileName = "Unknown.file"
    with pytest.raises(TransactionError, match=r".*" + str(Status.NotFound.value) + ".*"):
        get_file(harness, testFileName, "./", sysUserLogsPath)


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.rt1051
def test_get_invalid_chunks(harness):
    """
    Attempts:
    requesting data with invalid chunk numbers, which are:
        - 0 as numbering start with 1
        - chunk count + 1 (or any more) as it exceeds maximum no of chunks
    requesting data with invalid rxID:
       - rxID != current rxID transfer
    """
    ret = FsInitGet(sysUserLogsPath, osLogFileName).run(harness)

    assert ret.fileSize != 0

    totalChunks = int(((ret.fileSize + ret.chunkSize - 1) / ret.chunkSize))
    log.info(f"totalChunks #: {totalChunks}")

    with pytest.raises(TransactionError, match=r".*" + str(Status.BadRequest.value) + ".*"):
        FsGetChunk(ret.rxID, 0).run(harness)

    with pytest.raises(TransactionError, match=r".*" + str(Status.BadRequest.value) + ".*"):
        FsGetChunk(ret.rxID, totalChunks + 1).run(harness)

    with pytest.raises(TransactionError, match=r".*" + str(Status.BadRequest.value) + ".*"):
        FsGetChunk(ret.rxID - 1, totalChunks + 1).run(harness)

    with pytest.raises(TransactionError, match=r".*" + str(Status.BadRequest.value) + ".*"):
        FsGetChunk(ret.rxID + 1, totalChunks + 1).run(harness)


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.rt1051
def test_get_file(harness):
    """
    Get file MuditaOS.log file - whole transfer
    """
    get_file(harness, osLogFileName, "./", sysUserLogsPath)