~aleteoryx/muditaos

ref: 7769558634ee3686c5cef6d77fa39e86bf313db0 muditaos/test/pytest/service-desktop/test_filesystem_ops.py -rw-r--r-- 4.2 KiB
77695586 — Marek Niepieklo [CP-832] Merge get/send file tests, add remove file test 4 years 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
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
import pytest
import filecmp

from harness.api.developermode import PhoneModeLock
from harness.api.filesystem import get_file, put_file, FsInitGet, FsGetChunk, FsRemoveFile, FsRenameFile
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"

@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_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)