1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 23:08:14 +00:00

tests: move autolock tests to a separate file

This commit is contained in:
matejcik 2020-05-25 09:17:48 +02:00 committed by matejcik
parent 6f53ca0ac6
commit 0c3bbef81b
2 changed files with 116 additions and 60 deletions

View File

@ -0,0 +1,116 @@
#
# This file is part of the Trezor project.
# Copyright (C) 2012-2019 SatoshiLabs and contributors
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import time
import pytest
from trezorlib import device, messages
from ..common import TEST_ADDRESS_N, get_test_address
PIN4 = "1234"
pytestmark = pytest.mark.setup_client(pin=PIN4)
def pin_request(client):
return (
messages.PinMatrixRequest()
if client.features.model == "1"
else messages.ButtonRequest()
)
def set_autolock_delay(client, delay):
with client:
client.use_pin_sequence([PIN4])
client.set_expected_responses(
[
pin_request(client),
messages.ButtonRequest(),
messages.Success(),
messages.Features(),
]
)
device.apply_settings(client, auto_lock_delay_ms=delay)
def test_apply_auto_lock_delay(client):
set_autolock_delay(client, 10 * 1000)
time.sleep(0.1) # sleep less than auto-lock delay
with client:
# No PIN protection is required.
client.set_expected_responses([messages.Address()])
get_test_address(client)
time.sleep(10.1) # sleep more than auto-lock delay
with client:
client.use_pin_sequence([PIN4])
client.set_expected_responses([pin_request(client), messages.Address()])
get_test_address(client)
def test_apply_minimal_auto_lock_delay(client):
"""
Verify that the delay is not below the minimal auto-lock delay (10 secs)
otherwise the device may auto-lock before any user interaction.
"""
set_autolock_delay(client, 1 * 1000)
time.sleep(0.1) # sleep less than auto-lock delay
with client:
# No PIN protection is required.
client.set_expected_responses([messages.Address()])
get_test_address(client)
# sleep more than specified auto-lock delay (1s) but less than minimal allowed (10s)
time.sleep(3)
with client:
# No PIN protection is required.
client.set_expected_responses([messages.Address()])
get_test_address(client)
time.sleep(10.1) # sleep more than the minimal auto-lock delay
with client:
client.use_pin_sequence([PIN4])
client.set_expected_responses([pin_request(client), messages.Address()])
get_test_address(client)
@pytest.mark.skip_t1
def test_autolock_cancels_ui(client):
set_autolock_delay(client, 10 * 1000)
resp = client.call_raw(
messages.GetAddress(
coin_name="Testnet",
address_n=TEST_ADDRESS_N,
show_display=True,
script_type=messages.InputScriptType.SPENDADDRESS,
)
)
assert isinstance(resp, messages.ButtonRequest)
# send an ack, do not read response
client._raw_write(messages.ButtonAck())
# sleep more than auto-lock delay
time.sleep(10.1)
resp = client._raw_read()
assert isinstance(resp, messages.Failure)
assert resp.code == messages.FailureType.ActionCancelled

View File

@ -14,14 +14,10 @@
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import time
import pytest
from trezorlib import device, messages as proto
from ..common import test_address
EXPECTED_RESPONSES_NOPIN = [proto.ButtonRequest(), proto.Success(), proto.Features()]
EXPECTED_RESPONSES_PIN_T1 = [proto.PinMatrixRequest()] + EXPECTED_RESPONSES_NOPIN
EXPECTED_RESPONSES_PIN_TT = [proto.ButtonRequest()] + EXPECTED_RESPONSES_NOPIN
@ -121,59 +117,3 @@ class TestMsgApplysettings:
with client:
_set_expected_responses(client)
device.apply_settings(client, homescreen=img)
def test_apply_auto_lock_delay(self, client):
pin_response = (
proto.PinMatrixRequest()
if client.features.model == "1"
else proto.ButtonRequest()
)
with client:
_set_expected_responses(client)
device.apply_settings(client, auto_lock_delay_ms=int(10e3)) # 10 secs
time.sleep(0.1) # sleep less than auto-lock delay
with client:
# No PIN protection is required.
client.set_expected_responses([proto.Address()])
test_address(client)
time.sleep(10.1) # sleep more than auto-lock delay
with client:
client.use_pin_sequence([PIN4])
client.set_expected_responses([pin_response, proto.Address()])
test_address(client)
def test_apply_minimal_auto_lock_delay(self, client):
"""
Verify that the delay is not below the minimal auto-lock delay (10 secs)
otherwise the device may auto-lock before any user interaction.
"""
pin_response = (
proto.PinMatrixRequest()
if client.features.model == "1"
else proto.ButtonRequest()
)
with client:
_set_expected_responses(client)
# Note: the actual delay will be 10 secs (see above).
device.apply_settings(client, auto_lock_delay_ms=int(1e3))
time.sleep(0.1) # sleep less than auto-lock delay
with client:
# No PIN protection is required.
client.set_expected_responses([proto.Address()])
test_address(client)
time.sleep(2) # sleep less than the minimal auto-lock delay
with client:
# No PIN protection is required.
client.set_expected_responses([proto.Address()])
test_address(client)
time.sleep(10.1) # sleep more than the minimal auto-lock delay
with client:
client.use_pin_sequence([PIN4])
client.set_expected_responses([pin_response, proto.Address()])
test_address(client)