~aleteoryx/muditaos

ref: 25a5d90f4e12ee5d33f4202170f18b59e16a9564 muditaos/test/pytest/service-desktop/test_security.py -rw-r--r-- 8.3 KiB
25a5d90f — rrandomsky [CP-2156] Fixed no response when editing a contact to have the same number as another 2 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
import logging

import pytest
import time

from harness.interface.defs import status
from harness.api.developermode import PhoneModeLock
from harness.api.security import GetPhoneLockStatus, GetPhoneLockTime, SetPhoneLockOff
from harness.request import TransactionError


class SecurityTester:
    def __init__(self, harness):
        self.harness = harness

    def confirm_phone_is_unlocked(self):
        try:
            GetPhoneLockStatus().run(self.harness)
        except TransactionError:
            return False
        else:
            return True

    def confirm_phone_is_locked(self):
        try:
            GetPhoneLockStatus().run(self.harness)
        except TransactionError as error:
            return error.status == status["Forbidden"]
        else:
            return False

    def check_if_passcode_format_validation_works(self, passcode: list):
        try:
            SetPhoneLockOff(passcode).run(self.harness)
        except TransactionError as error:
            return error.status == status["BadRequest"]
        else:
            return False

    def confirm_no_time_lock(self):
        try:
            GetPhoneLockTime().run(self.harness)
        except TransactionError as error:
            return error.status == status["UnprocessableEntity"]
        else:
            return False

    def unlock_phone(self, passcode: list = None):
        try:
            SetPhoneLockOff(passcode).run(self.harness)
            time.sleep(0.1)
        except TransactionError:
            return False
        else:
            return True


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
def test_security_phone_unlocked(harness):
    security_tester = SecurityTester(harness)
    assert security_tester.confirm_phone_is_unlocked(), "Phone is locked, but should be unlocked!"


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_locked")
def test_security_phone_locked_with_code(harness):
    security_tester = SecurityTester(harness)
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_locked")
def test_all_other_endpoints_phone_locked_with_code(harness):
    security_tester = SecurityTester(harness)
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"

    body = {}
    endpoints_list = ["deviceInfo",
                      "update",
                      "filesystem",
                      "backup",
                      "restore",
                      "factory",
                      "contacts",
                      "messages",
                      "calllog",
                      "events",
                      "bluetooth"]
    for endpoint_name in endpoints_list:
        ret = harness.endpoint_request(endpoint_name, "get", body)
        assert ret["status"] == status["Forbidden"]

        ret = harness.endpoint_request(endpoint_name, "post", body)
        assert ret["status"] == status["Forbidden"]

        ret = harness.endpoint_request(endpoint_name, "put", body)
        assert ret["status"] == status["Forbidden"]

        ret = harness.endpoint_request(endpoint_name, "del", body)
        assert ret["status"] == status["Forbidden"]

    body = {"getInfo": "simState"}
    ret = harness.endpoint_request("developerMode", "get", body)
    assert ret["status"] == status["OK"]


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_locked")
def test_security_phone_locked_without_code(harness):
    security_tester = SecurityTester(harness)
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"

    PhoneModeLock(False).run(harness)
    assert not harness.phone_mode_lock

    assert security_tester.confirm_phone_is_unlocked(), "Phone is locked, but should be unlocked!"

    PhoneModeLock(True).run(harness)
    assert harness.phone_mode_lock

    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_locked")
def test_security_unlock_phone(harness):
    """
    Attempt unlocking with wrong passcode: 1111
    Assuming 1111 is not the actual passcode :-)
    """
    passcode = [1, 1, 1, 1]
    security_tester = SecurityTester(harness)
    assert security_tester.unlock_phone(passcode), "Failed to unlock the phone!"
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"

    passcode = ['a', 'a', 'a', 'a']
    assert security_tester.check_if_passcode_format_validation_works(
        passcode), "Passcode with incorrect format was accepted!"
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"

    """
    Attempt unlocking with too short passcode: 1
    """
    passcode = [1]
    assert security_tester.check_if_passcode_format_validation_works(
        passcode), "Passcode with incorrect format was accepted!"
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"

    """
    Attempt unlocking with the default passcode.
    Assuming the default passcode is the actual passcode :-)
    """
    assert security_tester.unlock_phone(), "Failed to unlock the phone!"
    assert security_tester.confirm_phone_is_unlocked(), "Phone is locked, but should be unlocked!"

    harness.lock_phone()
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_locked")
def test_security_time_lock(harness):
    """
    First attempt unlocking with wrong passcode: 1111
    """
    security_tester = SecurityTester(harness)
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"

    passcode = [1, 1, 1, 1]
    assert security_tester.unlock_phone(passcode), "Failed to unlock the phone!"
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"

    assert security_tester.confirm_no_time_lock(), "Phone is time locked unexpectedly!"

    """
    Second attempt unlocking with wrong passcode: 1111
    """
    passcode = [1, 1, 1, 1]
    assert security_tester.unlock_phone(passcode), "Failed to unlock the phone!"
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"

    assert security_tester.confirm_no_time_lock(), "Phone is time locked unexpectedly!"

    """
    Third attempt unlocking with wrong passcode: 1111
    """
    passcode = [1, 1, 1, 1]
    assert security_tester.unlock_phone(passcode), "Failed to unlock the phone!"
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"

    assert time.time() < GetPhoneLockTime().run(harness).phoneLockTime <= time.time() + 30
    assert GetPhoneLockTime().run(harness).timeLeftToNextAttempt <= 15

    """
    Attempt unlocking with default (correct) passcode while phone is time locked
    """
    assert security_tester.unlock_phone(), "Failed to unlock the phone!"
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"

    """
    Waiting 16 seconds to make sure time lock is no longer active
    """
    time.sleep(16)

    """
    Check if time lock is no longer active
    """
    assert security_tester.confirm_no_time_lock(), "Phone is time locked unexpectedly!"

    """
    Fourth attempt unlocking with wrong passcode: 1111
    """
    passcode = [1, 1, 1, 1]
    assert security_tester.unlock_phone(passcode), "Failed to unlock the phone!"
    assert security_tester.confirm_phone_is_locked(), "Phone is unlocked, but should be locked!"

    assert time.time() < GetPhoneLockTime().run(harness).phoneLockTime <= time.time() + 30

    """
    Waiting 31 seconds to make sure time lock is no longer active
    """
    time.sleep(31)

    """
    Check if time lock is no longer active
    """
    assert security_tester.confirm_no_time_lock(), "Phone is time locked unexpectedly!"

    """
    Attempt unlocking when time lock is no longer active
    """
    assert security_tester.unlock_phone(), "Failed to unlock the phone!"
    assert security_tester.confirm_phone_is_unlocked(), "Phone is locked, but should be unlocked!"