1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-29 19:08:12 +00:00
trezor-firmware/tests/device_tests/test_msg_changepin_t1.py

221 lines
7.1 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>.
import pytest
2017-01-03 18:40:05 +00:00
2020-04-27 14:37:16 +00:00
from trezorlib import device, messages
from trezorlib.client import MAX_PIN_LENGTH
2024-11-15 16:31:22 +00:00
from trezorlib.debuglink import SessionDebugWrapper as Session
2020-04-27 14:37:16 +00:00
from trezorlib.exceptions import TrezorFailure
2017-06-23 19:31:42 +00:00
from ..common import get_test_address
2019-09-11 12:29:39 +00:00
PIN4 = "1234"
PIN6 = "789456"
PIN_MAX = "".join(chr((i % 9) + ord("1")) for i in range(MAX_PIN_LENGTH))
PIN_TOO_LONG = PIN_MAX + "1"
2014-02-21 03:46:33 +00:00
pytestmark = pytest.mark.models("legacy")
2020-04-27 14:37:16 +00:00
2024-11-15 16:31:22 +00:00
def _check_pin(session: Session, pin):
session.lock()
with session, session.client as client:
2020-04-27 14:37:16 +00:00
client.use_pin_sequence([pin])
2024-11-15 16:31:22 +00:00
session.set_expected_responses([messages.PinMatrixRequest, messages.Address])
get_test_address(session)
2020-04-27 14:37:16 +00:00
2024-11-15 16:31:22 +00:00
def _check_no_pin(session: Session):
session.lock()
with session:
session.set_expected_responses([messages.Address])
get_test_address(session)
2020-04-27 14:37:16 +00:00
2024-11-15 16:31:22 +00:00
def test_set_pin(session: Session):
assert session.features.pin_protection is False
2020-04-27 14:37:16 +00:00
# Check that there's no PIN protection
2024-11-15 16:31:22 +00:00
_check_no_pin(session)
2020-04-27 14:37:16 +00:00
# Let's set new PIN
2024-11-15 16:31:22 +00:00
with session, session.client as client:
client.use_pin_sequence([PIN_MAX, PIN_MAX])
2024-11-15 16:31:22 +00:00
session.set_expected_responses(
2020-04-27 14:37:16 +00:00
[
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
messages.PinMatrixRequest,
messages.PinMatrixRequest,
messages.Success,
messages.Features,
2020-04-27 14:37:16 +00:00
]
)
2024-11-15 16:31:22 +00:00
device.change_pin(session)
2020-04-27 14:37:16 +00:00
# Check that there's PIN protection now
2024-11-15 16:31:22 +00:00
assert session.features.pin_protection is True
2020-04-27 14:37:16 +00:00
# Check that the PIN is correct
2024-11-15 16:31:22 +00:00
_check_pin(session, PIN_MAX)
2020-04-27 14:37:16 +00:00
@pytest.mark.setup_client(pin=PIN4)
2024-11-15 16:31:22 +00:00
def test_change_pin(session: Session):
assert session.features.pin_protection is True
2020-04-27 14:37:16 +00:00
# Check that there's PIN protection
2024-11-15 16:31:22 +00:00
_check_pin(session, PIN4)
2020-04-27 14:37:16 +00:00
# Let's change PIN
2024-11-15 16:31:22 +00:00
with session, session.client as client:
client.use_pin_sequence([PIN4, PIN_MAX, PIN_MAX])
2024-11-15 16:31:22 +00:00
session.set_expected_responses(
2020-04-27 14:37:16 +00:00
[
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
messages.PinMatrixRequest,
messages.PinMatrixRequest,
messages.PinMatrixRequest,
messages.Success,
messages.Features,
2020-04-27 14:37:16 +00:00
]
)
2024-11-15 16:31:22 +00:00
device.change_pin(session)
2020-04-27 14:37:16 +00:00
# Check that there's still PIN protection now
2024-11-15 16:31:22 +00:00
assert session.features.pin_protection is True
2020-04-27 14:37:16 +00:00
# Check that the PIN is correct
2024-11-15 16:31:22 +00:00
_check_pin(session, PIN_MAX)
2020-04-27 14:37:16 +00:00
@pytest.mark.setup_client(pin=PIN4)
2024-11-15 16:31:22 +00:00
def test_remove_pin(session: Session):
assert session.features.pin_protection is True
2020-04-27 14:37:16 +00:00
# Check that there's PIN protection
2024-11-15 16:31:22 +00:00
_check_pin(session, PIN4)
2020-04-27 14:37:16 +00:00
# Let's remove PIN
2024-11-15 16:31:22 +00:00
with session, session.client as client:
2020-04-27 14:37:16 +00:00
client.use_pin_sequence([PIN4])
2024-11-15 16:31:22 +00:00
session.set_expected_responses(
2020-04-27 14:37:16 +00:00
[
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
messages.PinMatrixRequest,
messages.Success,
messages.Features,
2020-04-27 14:37:16 +00:00
]
)
2024-11-15 16:31:22 +00:00
device.change_pin(session, remove=True)
2020-04-27 14:37:16 +00:00
# Check that there's no PIN protection now
2024-11-15 16:31:22 +00:00
assert session.features.pin_protection is False
_check_no_pin(session)
2020-04-27 14:37:16 +00:00
2024-11-15 16:31:22 +00:00
def test_set_mismatch(session: Session):
assert session.features.pin_protection is False
2020-04-27 14:37:16 +00:00
# Check that there's no PIN protection
2024-11-15 16:31:22 +00:00
_check_no_pin(session)
2020-04-27 14:37:16 +00:00
# Let's set new PIN
2024-11-15 16:31:22 +00:00
with session, session.client as client, pytest.raises(
TrezorFailure, match="PIN mismatch"
):
2020-04-27 14:37:16 +00:00
# use different PINs for first and second attempt. This will fail.
client.use_pin_sequence([PIN4, PIN_MAX])
2024-11-15 16:31:22 +00:00
session.set_expected_responses(
2020-04-27 14:37:16 +00:00
[
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
messages.PinMatrixRequest,
messages.PinMatrixRequest,
messages.Failure,
2020-04-27 14:37:16 +00:00
]
)
2024-11-15 16:31:22 +00:00
device.change_pin(session)
2020-04-27 14:37:16 +00:00
# Check that there's still no PIN protection now
2024-11-15 16:31:22 +00:00
# TODO change session.init_device()
session.refresh_features()
assert session.features.pin_protection is False
_check_no_pin(session)
2020-04-27 14:37:16 +00:00
@pytest.mark.setup_client(pin=PIN4)
2024-11-15 16:31:22 +00:00
def test_change_mismatch(session: Session):
assert session.features.pin_protection is True
2020-04-27 14:37:16 +00:00
# Let's set new PIN
2024-11-15 16:31:22 +00:00
with session, session.client as client, pytest.raises(
TrezorFailure, match="PIN mismatch"
):
2020-04-27 14:37:16 +00:00
client.use_pin_sequence([PIN4, PIN6, PIN6 + "3"])
2024-11-15 16:31:22 +00:00
session.set_expected_responses(
2020-04-27 14:37:16 +00:00
[
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
messages.PinMatrixRequest,
messages.PinMatrixRequest,
messages.PinMatrixRequest,
messages.Failure,
2020-04-27 14:37:16 +00:00
]
)
2024-11-15 16:31:22 +00:00
device.change_pin(session)
2020-04-27 14:37:16 +00:00
# Check that there's still old PIN protection
2024-11-15 16:31:22 +00:00
# TODO change session.init_device()
session.refresh_features()
assert session.features.pin_protection is True
_check_pin(session, PIN4)
2020-04-27 14:37:16 +00:00
@pytest.mark.parametrize("invalid_pin", ("1204", "", PIN_TOO_LONG))
2024-11-15 16:31:22 +00:00
def test_set_invalid(session: Session, invalid_pin):
assert session.features.pin_protection is False
2020-04-27 14:37:16 +00:00
# Let's set an invalid PIN
2024-11-15 16:31:22 +00:00
ret = session.call_raw(messages.ChangePin())
2020-04-27 14:37:16 +00:00
assert isinstance(ret, messages.ButtonRequest)
# Press button
2024-11-15 16:31:22 +00:00
session.client.debug.press_yes()
ret = session.call_raw(messages.ButtonAck())
2020-04-27 14:37:16 +00:00
# Send a PIN containing an invalid digit
assert isinstance(ret, messages.PinMatrixRequest)
2024-11-15 16:31:22 +00:00
ret = session.call_raw(messages.PinMatrixAck(pin=invalid_pin))
2020-04-27 14:37:16 +00:00
# Ensure the invalid PIN is detected
assert isinstance(ret, messages.Failure)
# Check that there's still no PIN protection now
2024-11-15 16:31:22 +00:00
# TODO change session.init_device()
session.refresh_features()
assert session.features.pin_protection is False
_check_no_pin(session)
2020-04-27 14:37:16 +00:00
@pytest.mark.parametrize("invalid_pin", ("1204", "", PIN_TOO_LONG))
2020-04-27 14:37:16 +00:00
@pytest.mark.setup_client(pin=PIN4)
2024-11-15 16:31:22 +00:00
def test_enter_invalid(session: Session, invalid_pin):
assert session.features.pin_protection is True
2020-04-27 14:37:16 +00:00
# use an invalid PIN
2024-11-15 16:31:22 +00:00
ret = session.call_raw(messages.GetAddress())
2020-04-27 14:37:16 +00:00
# Send a PIN containing an invalid digit
assert isinstance(ret, messages.PinMatrixRequest)
2024-11-15 16:31:22 +00:00
ret = session.call_raw(messages.PinMatrixAck(pin=invalid_pin))
2020-04-27 14:37:16 +00:00
# Ensure the invalid PIN is detected
assert isinstance(ret, messages.Failure)