1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-25 07:48:10 +00:00
trezor-firmware/tests/device_tests/test_protect_call.py

92 lines
3.3 KiB
Python
Raw Normal View History

# This file is part of the Trezor project.
2017-01-03 18:40:05 +00:00
#
2019-05-29 16:44:09 +00:00
# Copyright (C) 2012-2019 SatoshiLabs and contributors
2017-01-03 18:40:05 +00:00
#
# 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.
2017-01-03 18:40:05 +00:00
#
# 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>.
2017-01-03 18:40:05 +00:00
import time
2018-08-13 16:21:24 +00:00
import pytest
2020-01-21 09:05:48 +00:00
from trezorlib import btc, messages as proto
2018-11-14 13:44:05 +00:00
from trezorlib.exceptions import PinException
2014-02-03 23:32:10 +00:00
# FIXME TODO Add passphrase tests
2013-01-14 13:44:11 +00:00
2017-06-23 19:31:42 +00:00
@pytest.mark.skip_t2
2019-09-11 12:29:39 +00:00
class TestProtectCall:
2020-01-21 09:05:48 +00:00
def _some_protected_call(self, client):
2013-01-14 16:05:38 +00:00
# This method perform any call which have protection in the device
2020-01-21 09:05:48 +00:00
res = btc.get_address(client, "Testnet", [0])
assert res == "mndoQDWatQhfeQbprzZxD43mZ75Z94D6vz"
2019-09-11 12:29:39 +00:00
@pytest.mark.setup_client(pin="1234", passphrase=True)
def test_expected_responses(self, client):
2020-01-21 09:05:48 +00:00
with client:
# Scenario 4 - Received what expected
2019-09-11 12:29:39 +00:00
client.set_expected_responses(
2020-01-21 09:05:48 +00:00
[proto.PinMatrixRequest(), proto.PassphraseRequest(), proto.Address()]
2019-09-11 12:29:39 +00:00
)
2020-01-21 09:05:48 +00:00
self._some_protected_call(client)
2014-02-15 19:31:34 +00:00
def test_no_protection(self, client):
with client:
assert client.debug.read_pin()[0] is None
2020-01-21 09:05:48 +00:00
client.set_expected_responses([proto.Address()])
self._some_protected_call(client)
2020-01-21 09:05:48 +00:00
@pytest.mark.setup_client(pin="1234")
def test_pin(self, client):
with client:
assert client.debug.read_pin()[0] == "1234"
client.setup_debuglink(button=True, pin_correct=True)
2020-01-21 09:05:48 +00:00
client.set_expected_responses([proto.PinMatrixRequest(), proto.Address()])
self._some_protected_call(client)
2020-01-21 09:05:48 +00:00
@pytest.mark.setup_client(pin="1234")
def test_incorrect_pin(self, client):
client.setup_debuglink(button=True, pin_correct=False)
with pytest.raises(PinException):
2020-01-21 09:05:48 +00:00
self._some_protected_call(client)
2013-10-10 15:18:02 +00:00
2020-01-21 09:05:48 +00:00
@pytest.mark.setup_client(pin="1234")
def test_cancelled_pin(self, client):
client.setup_debuglink(button=True, pin_correct=False) # PIN cancel
with pytest.raises(PinException):
2020-01-21 09:05:48 +00:00
self._some_protected_call(client)
2014-02-17 00:54:54 +00:00
@pytest.mark.setup_client(pin="1234", passphrase=True)
def test_exponential_backoff_with_reboot(self, client):
client.setup_debuglink(button=True, pin_correct=False)
2014-02-17 00:54:54 +00:00
def test_backoff(attempts, start):
2016-05-20 15:20:11 +00:00
if attempts <= 1:
expected = 0
2016-05-20 15:20:11 +00:00
else:
2017-12-16 21:37:21 +00:00
expected = (2 ** (attempts - 1)) - 1
got = round(time.time() - start, 2)
2018-08-13 16:21:24 +00:00
msg = "Pin delay expected to be at least %s seconds, got %s" % (
expected,
got,
)
2016-05-05 01:16:17 +00:00
print(msg)
assert got >= expected
for attempt in range(1, 4):
start = time.time()
with pytest.raises(PinException):
2020-01-21 09:05:48 +00:00
self._some_protected_call(client)
test_backoff(attempt, start)