|
|
|
@ -18,7 +18,7 @@ import time
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from trezorlib import messages as proto
|
|
|
|
|
from trezorlib import btc, messages as proto
|
|
|
|
|
from trezorlib.exceptions import PinException
|
|
|
|
|
|
|
|
|
|
# FIXME TODO Add passphrase tests
|
|
|
|
@ -26,84 +26,45 @@ from trezorlib.exceptions import PinException
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip_t2
|
|
|
|
|
class TestProtectCall:
|
|
|
|
|
def _some_protected_call(self, client, button, pin, passphrase):
|
|
|
|
|
def _some_protected_call(self, client):
|
|
|
|
|
# This method perform any call which have protection in the device
|
|
|
|
|
res = client.ping(
|
|
|
|
|
"random data",
|
|
|
|
|
button_protection=button,
|
|
|
|
|
pin_protection=pin,
|
|
|
|
|
passphrase_protection=passphrase,
|
|
|
|
|
)
|
|
|
|
|
assert res == "random data"
|
|
|
|
|
res = btc.get_address(client, "Testnet", [0])
|
|
|
|
|
assert res == "mndoQDWatQhfeQbprzZxD43mZ75Z94D6vz"
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin="1234", passphrase=True)
|
|
|
|
|
def test_expected_responses(self, client):
|
|
|
|
|
# This is low-level test of set_expected_responses()
|
|
|
|
|
# feature of debugging client
|
|
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError), client:
|
|
|
|
|
# Scenario 1 - Received unexpected message
|
|
|
|
|
client.set_expected_responses([])
|
|
|
|
|
self._some_protected_call(client, True, True, True)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError), client:
|
|
|
|
|
# Scenario 2 - Received other than expected message
|
|
|
|
|
client.set_expected_responses([proto.Success()])
|
|
|
|
|
self._some_protected_call(client, True, True, True)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError), client:
|
|
|
|
|
# Scenario 3 - Not received expected message
|
|
|
|
|
client.set_expected_responses(
|
|
|
|
|
[proto.ButtonRequest(), proto.Success(), proto.Success()]
|
|
|
|
|
) # This is expected, but not received
|
|
|
|
|
self._some_protected_call(client, True, False, False)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError), client:
|
|
|
|
|
with client:
|
|
|
|
|
# Scenario 4 - Received what expected
|
|
|
|
|
client.set_expected_responses(
|
|
|
|
|
[
|
|
|
|
|
proto.ButtonRequest(),
|
|
|
|
|
proto.PinMatrixRequest(),
|
|
|
|
|
proto.PassphraseRequest(),
|
|
|
|
|
proto.Success(message="random data"),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
self._some_protected_call(client, True, True, True)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError), client:
|
|
|
|
|
# Scenario 5 - Failed message by field filter
|
|
|
|
|
client.set_expected_responses(
|
|
|
|
|
[proto.ButtonRequest(), proto.Success(message="wrong data")]
|
|
|
|
|
[proto.PinMatrixRequest(), proto.PassphraseRequest(), proto.Address()]
|
|
|
|
|
)
|
|
|
|
|
self._some_protected_call(client, True, True, True)
|
|
|
|
|
self._some_protected_call(client)
|
|
|
|
|
|
|
|
|
|
def test_no_protection(self, client):
|
|
|
|
|
with client:
|
|
|
|
|
assert client.debug.read_pin()[0] is None
|
|
|
|
|
client.set_expected_responses([proto.Success()])
|
|
|
|
|
self._some_protected_call(client, False, True, True)
|
|
|
|
|
client.set_expected_responses([proto.Address()])
|
|
|
|
|
self._some_protected_call(client)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin="1234", passphrase=True)
|
|
|
|
|
@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)
|
|
|
|
|
client.set_expected_responses(
|
|
|
|
|
[proto.ButtonRequest(), proto.PinMatrixRequest(), proto.Success()]
|
|
|
|
|
)
|
|
|
|
|
self._some_protected_call(client, True, True, False)
|
|
|
|
|
client.set_expected_responses([proto.PinMatrixRequest(), proto.Address()])
|
|
|
|
|
self._some_protected_call(client)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin="1234", passphrase=True)
|
|
|
|
|
@pytest.mark.setup_client(pin="1234")
|
|
|
|
|
def test_incorrect_pin(self, client):
|
|
|
|
|
client.setup_debuglink(button=True, pin_correct=False)
|
|
|
|
|
with pytest.raises(PinException):
|
|
|
|
|
self._some_protected_call(client, False, True, False)
|
|
|
|
|
self._some_protected_call(client)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin="1234", passphrase=True)
|
|
|
|
|
@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):
|
|
|
|
|
self._some_protected_call(client, False, True, False)
|
|
|
|
|
self._some_protected_call(client)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin="1234", passphrase=True)
|
|
|
|
|
def test_exponential_backoff_with_reboot(self, client):
|
|
|
|
@ -126,5 +87,5 @@ class TestProtectCall:
|
|
|
|
|
for attempt in range(1, 4):
|
|
|
|
|
start = time.time()
|
|
|
|
|
with pytest.raises(PinException):
|
|
|
|
|
self._some_protected_call(client, False, True, False)
|
|
|
|
|
self._some_protected_call(client)
|
|
|
|
|
test_backoff(attempt, start)
|
|
|
|
|