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

tests: fix test suite for softlock

This commit is contained in:
matejcik 2020-04-27 13:15:25 +02:00 committed by matejcik
parent b68cc5abda
commit 4771d2c233
8 changed files with 82 additions and 197 deletions

View File

@ -21,14 +21,16 @@ import pytest
from trezorlib import btc, device, messages as proto
EXPECTED_RESPONSES_NOPIN = [proto.ButtonRequest(), proto.Success(), proto.Features()]
EXPECTED_RESPONSES_PIN = [proto.PinMatrixRequest()] + EXPECTED_RESPONSES_NOPIN
EXPECTED_RESPONSES_PIN_T1 = [proto.PinMatrixRequest()] + EXPECTED_RESPONSES_NOPIN
EXPECTED_RESPONSES_PIN_TT = [proto.ButtonRequest()] + EXPECTED_RESPONSES_NOPIN
def _set_expected_responses(client):
if client.features.model == "1":
client.set_expected_responses(EXPECTED_RESPONSES_PIN)
client.set_expected_responses(EXPECTED_RESPONSES_PIN_T1)
else:
client.set_expected_responses(EXPECTED_RESPONSES_NOPIN)
client.use_pin_sequence(["1234"])
client.set_expected_responses(EXPECTED_RESPONSES_PIN_TT)
@pytest.mark.setup_client(pin=True)

View File

@ -16,7 +16,8 @@
import pytest
from trezorlib import device, messages
from trezorlib import btc, device, messages
from trezorlib.client import PASSPHRASE_TEST_PATH
from trezorlib.exceptions import Cancelled, TrezorFailure
PIN4 = "1234"
@ -26,91 +27,41 @@ WIPE_CODE6 = "456789"
pytestmark = pytest.mark.skip_t1
def _input_flow_set_pin(debug, pin):
yield # do you want to set a new pin?
print("set pin?")
debug.press_yes()
yield # enter new pin
print(f"enter pin {pin}")
debug.input(pin)
yield # enter new pin again
print(f"reenter pin {pin}")
debug.input(pin)
yield # success
print("success")
debug.press_yes()
def _input_flow_change_pin(debug, old_pin, new_pin):
yield # do you want to change pin?
debug.press_yes()
yield # enter current pin
debug.input(old_pin)
yield # enter new pin
debug.input(new_pin)
yield # enter new pin again
debug.input(new_pin)
yield # success
debug.press_yes()
def _input_flow_clear_pin(debug, old_pin):
yield # do you want to remove pin?
debug.press_yes()
yield # enter current pin
debug.input(old_pin)
yield # success
debug.press_yes()
def _input_flow_set_wipe_code(debug, pin, wipe_code):
yield # do you want to set/change the wipe_code?
debug.press_yes()
if pin is not None:
yield # enter current pin
debug.input(pin)
yield # enter new wipe code
debug.input(wipe_code)
yield # enter new wipe code again
debug.input(wipe_code)
yield # success
debug.press_yes()
def _input_flow_remove_wipe_code(debug, pin):
yield # do you want to remove wipe code?
debug.press_yes()
yield # enter current pin
debug.input(pin)
yield # success
debug.press_yes()
def _check_wipe_code(client, pin, wipe_code):
client.clear_session()
client.init_device()
assert client.features.wipe_code_protection is True
# Try to change the PIN to the current wipe code value. The operation should fail.
with client, pytest.raises(TrezorFailure):
client.use_pin_sequence([pin, wipe_code, wipe_code])
client.set_expected_responses(
[messages.ButtonRequest()] * 5
+ [messages.Failure(code=messages.FailureType.PinInvalid)]
)
client.set_input_flow(_input_flow_change_pin(client.debug, pin, wipe_code))
device.change_pin(client)
def _ensure_unlocked(client, pin):
with client:
client.use_pin_sequence([pin])
btc.get_address(client, "Testnet", PASSPHRASE_TEST_PATH)
client.init_device()
@pytest.mark.setup_client(pin=PIN4)
def test_set_remove_wipe_code(client):
# Test set wipe code.
assert client.features.wipe_code_protection is None
_ensure_unlocked(client, PIN4)
assert client.features.wipe_code_protection is False
with client:
client.set_expected_responses(
[messages.ButtonRequest()] * 5 + [messages.Success(), messages.Features()]
)
client.set_input_flow(_input_flow_set_wipe_code(client.debug, PIN4, WIPE_CODE4))
client.use_pin_sequence([PIN4, WIPE_CODE4, WIPE_CODE4])
device.change_wipe_code(client)
client.init_device()
@ -122,8 +73,7 @@ def test_set_remove_wipe_code(client):
client.set_expected_responses(
[messages.ButtonRequest()] * 5 + [messages.Success(), messages.Features()]
)
client.set_input_flow(_input_flow_set_wipe_code(client.debug, PIN4, WIPE_CODE6))
client.use_pin_sequence([PIN4, WIPE_CODE6, WIPE_CODE6])
device.change_wipe_code(client)
client.init_device()
@ -135,8 +85,7 @@ def test_set_remove_wipe_code(client):
client.set_expected_responses(
[messages.ButtonRequest()] * 3 + [messages.Success(), messages.Features()]
)
client.set_input_flow(_input_flow_clear_pin(client.debug, PIN4))
client.use_pin_sequence([PIN4])
device.change_wipe_code(client, remove=True)
client.init_device()
@ -172,28 +121,13 @@ def test_set_wipe_code_mismatch(client):
@pytest.mark.setup_client(pin=PIN4)
def test_set_wipe_code_to_pin(client):
def input_flow():
yield # do you want to set the wipe code?
client.debug.press_yes()
yield # enter current pin
client.debug.input(PIN4)
yield # enter new wipe code (same as PIN)
client.debug.input(PIN4)
# failed retry
yield # enter new wipe code
client.debug.input(WIPE_CODE4)
yield # enter new wipe code again
client.debug.input(WIPE_CODE4)
yield # success
client.debug.press_yes()
_ensure_unlocked(client, PIN4)
with client:
client.set_expected_responses(
[messages.ButtonRequest()] * 6 + [messages.Success(), messages.Features()]
)
client.set_input_flow(input_flow)
client.use_pin_sequence([PIN4, PIN4, WIPE_CODE4, WIPE_CODE4])
device.change_wipe_code(client)
client.init_device()
@ -207,8 +141,7 @@ def test_set_pin_to_wipe_code(client):
client.set_expected_responses(
[messages.ButtonRequest()] * 4 + [messages.Success(), messages.Features()]
)
client.set_input_flow(_input_flow_set_wipe_code(client.debug, None, WIPE_CODE4))
client.use_pin_sequence([WIPE_CODE4, WIPE_CODE4])
device.change_wipe_code(client)
# Try to set the PIN to the current wipe code value.
@ -217,5 +150,5 @@ def test_set_pin_to_wipe_code(client):
[messages.ButtonRequest()] * 4
+ [messages.Failure(code=messages.FailureType.PinInvalid)]
)
client.set_input_flow(_input_flow_set_pin(client.debug, WIPE_CODE4))
client.use_pin_sequence([WIPE_CODE4, WIPE_CODE4])
device.change_pin(client)

View File

@ -16,7 +16,8 @@
import pytest
from trezorlib import device, messages
from trezorlib import btc, device, messages
from trezorlib.client import PASSPHRASE_TEST_PATH
from trezorlib.exceptions import Cancelled
PIN4 = "1234"
@ -25,75 +26,24 @@ PIN6 = "789456"
pytestmark = pytest.mark.skip_t1
def _input_flow_set_pin(debug, pin):
yield # do you want to set a new pin?
print("set pin?")
debug.press_yes()
yield # enter new pin
print(f"enter pin {pin}")
debug.input(pin)
yield # enter new pin again
print(f"reenter pin {pin}")
debug.input(pin)
yield # success
print("success")
debug.press_yes()
def _input_flow_change_pin(debug, old_pin, new_pin):
yield # do you want to change pin?
debug.press_yes()
yield # enter current pin
debug.input(old_pin)
yield # enter new pin
debug.input(new_pin)
yield # enter new pin again
debug.input(new_pin)
yield # success
debug.press_yes()
def _input_flow_clear_pin(debug, old_pin):
yield # do you want to remove pin?
debug.press_yes()
yield # enter current pin
debug.input(old_pin)
yield # success
debug.press_yes()
def _check_pin(client, pin):
client.clear_session()
assert client.features.pin_protection is True
assert client.features.pin_cached is False
with client:
client.set_expected_responses(
[messages.ButtonRequest()] * 5 + [messages.Success(), messages.Features()]
)
client.set_input_flow(_input_flow_change_pin(client.debug, pin, pin))
device.change_pin(client)
client.use_pin_sequence([pin])
client.set_expected_responses([messages.ButtonRequest(), messages.Address()])
btc.get_address(client, "Testnet", PASSPHRASE_TEST_PATH)
def _check_no_pin(client):
client.clear_session()
assert client.features.pin_protection is False
def input_flow():
yield from _input_flow_set_pin(client.debug, PIN4)
yield from _input_flow_clear_pin(client.debug, PIN4)
with client:
client.set_expected_responses(
[messages.ButtonRequest()] * 4
+ [messages.Success(), messages.Features()]
+ [messages.ButtonRequest()] * 3
+ [messages.Success(), messages.Features()]
)
client.set_input_flow(input_flow)
device.change_pin(client)
device.change_pin(client, remove=True)
assert client.features.pin_protection is False
client.set_expected_responses([messages.Address()])
btc.get_address(client, "Testnet", PASSPHRASE_TEST_PATH)
def test_set_pin(client):
@ -104,11 +54,10 @@ def test_set_pin(client):
# Let's set new PIN
with client:
client.use_pin_sequence([PIN6, PIN6])
client.set_expected_responses(
[messages.ButtonRequest()] * 4 + [messages.Success(), messages.Features()]
)
client.set_input_flow(_input_flow_set_pin(client.debug, PIN6))
device.change_pin(client)
client.init_device()
@ -125,11 +74,10 @@ def test_change_pin(client):
# Let's change PIN
with client:
client.use_pin_sequence([PIN4, PIN6, PIN6])
client.set_expected_responses(
[messages.ButtonRequest()] * 5 + [messages.Success(), messages.Features()]
)
client.set_input_flow(_input_flow_change_pin(client.debug, PIN4, PIN6))
device.change_pin(client)
# Check that there's still PIN protection now
@ -148,11 +96,10 @@ def test_remove_pin(client):
# Let's remove PIN
with client:
client.use_pin_sequence([PIN4])
client.set_expected_responses(
[messages.ButtonRequest()] * 3 + [messages.Success(), messages.Features()]
)
client.set_input_flow(_input_flow_clear_pin(client.debug, PIN4))
device.change_pin(client, remove=True)
# Check that there's no PIN protection now

View File

@ -23,18 +23,22 @@ from trezorlib.tools import parse_path
ADDRESS_N = parse_path("44'/0'/0'")
XPUB = "xpub6BiVtCpG9fQPxnPmHXG8PhtzQdWC2Su4qWu6XW9tpWFYhxydCLJGrWBJZ5H6qTAHdPQ7pQhtpjiYZVZARo14qHiay2fvrX996oEP42u8wZy"
PIN4 = "1234"
@pytest.mark.skip_ui
@pytest.mark.setup_client(pin=True, passphrase=True)
@pytest.mark.setup_client(pin=PIN4, passphrase=True)
def test_clear_session(client):
if client.features.model == "1":
init_responses = [messages.PinMatrixRequest(), messages.PassphraseRequest()]
else:
init_responses = [messages.PassphraseRequest()]
is_trezor1 = client.features.model == "1"
init_responses = [
messages.PinMatrixRequest() if is_trezor1 else messages.ButtonRequest(),
messages.PassphraseRequest(),
]
cached_responses = [messages.PublicKey()]
with client:
client.use_pin_sequence([PIN4])
client.set_expected_responses(init_responses + cached_responses)
assert get_public_node(client, ADDRESS_N).xpub == XPUB
@ -47,6 +51,7 @@ def test_clear_session(client):
# session cache is cleared
with client:
client.use_pin_sequence([PIN4])
client.set_expected_responses(init_responses + cached_responses)
assert get_public_node(client, ADDRESS_N).xpub == XPUB

View File

@ -16,24 +16,22 @@
import pytest
from trezorlib import messages as proto
from trezorlib import messages
class TestMsgPing:
@pytest.mark.skip_ui
@pytest.mark.setup_client(pin=True, passphrase=True)
def test_ping(self, client):
with client:
client.set_expected_responses([proto.Success()])
res = client.ping("random data")
assert res == "random data"
@pytest.mark.skip_ui
def test_ping(client):
with client:
client.set_expected_responses([messages.Success()])
res = client.ping("random data")
assert res == "random data"
with client:
client.set_expected_responses(
[
proto.ButtonRequest(code=proto.ButtonRequestType.ProtectCall),
proto.Success(),
]
)
res = client.ping("random data", button_protection=True)
assert res == "random data"
with client:
client.set_expected_responses(
[
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
messages.Success(),
]
)
res = client.ping("random data", button_protection=True)
assert res == "random data"

View File

@ -41,12 +41,12 @@ class TestMsgRecoverydeviceT2:
ret = client.call_raw(proto.ButtonAck())
# Enter PIN for first time
assert ret == proto.ButtonRequest(code=proto.ButtonRequestType.Other)
assert ret == proto.ButtonRequest(code=proto.ButtonRequestType.PinEntry)
client.debug.input("654")
ret = client.call_raw(proto.ButtonAck())
# Enter PIN for second time
assert ret == proto.ButtonRequest(code=proto.ButtonRequestType.Other)
assert ret == proto.ButtonRequest(code=proto.ButtonRequestType.PinEntry)
client.debug.input("654")
ret = client.call_raw(proto.ButtonAck())

View File

@ -16,23 +16,19 @@
import pytest
from trezorlib import device, messages as proto
from trezorlib import device
class TestMsgWipedevice:
@pytest.mark.setup_client(pin=True, passphrase=True)
def test_wipe_device(self, client):
features = client.call_raw(proto.Initialize())
@pytest.mark.setup_client(passphrase=True)
def test_wipe_device(client):
assert client.features.initialized is True
assert client.features.label == "test"
assert client.features.passphrase_protection is True
device_id = client.get_device_id()
assert features.initialized is True
assert features.pin_protection is True
assert features.passphrase_protection is True
device_id = features.device_id
device.wipe(client)
device.wipe(client)
features = client.call_raw(proto.Initialize())
assert features.initialized is False
assert features.pin_protection is False
assert features.passphrase_protection is False
assert features.device_id != device_id
assert client.features.initialized is False
assert client.features.label is None
assert client.features.passphrase_protection is False
assert client.get_device_id() != device_id

View File

@ -49,6 +49,10 @@ def test_sd_no_format(client):
@pytest.mark.setup_client(pin="1234")
def test_sd_protect_unlock(client):
def input_flow_enable_sd_protect():
yield # Enter PIN to unlock device
assert "PinDialog" == client.debug.wait_layout().text
client.debug.input("1234")
yield # do you really want to enable SD protection
assert "SD card protection" in client.debug.wait_layout().text
client.debug.press_yes()