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

100 lines
3.3 KiB
Python
Raw Normal View History

# 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 messages, models
2024-11-15 16:31:22 +00:00
from trezorlib.debuglink import SessionDebugWrapper as Session
from trezorlib.exceptions import PinException
2023-05-04 12:18:55 +00:00
from ..common import check_pin_backoff_time, get_test_address
from ..input_flows import InputFlowPINBackoff
PIN4 = "1234"
BAD_PIN = "5678"
pytestmark = pytest.mark.setup_client(pin=PIN4)
@pytest.mark.setup_client(pin=None)
2024-11-15 16:31:22 +00:00
def test_no_protection(session: Session):
with session:
session.set_expected_responses([messages.Address])
get_test_address(session)
2024-11-15 16:31:22 +00:00
def test_correct_pin(session: Session):
with session, session.client as client:
client.use_pin_sequence([PIN4])
2024-11-15 16:31:22 +00:00
session.lock() # TODO is the lock here necessary/correctly?
# Expected responses differ between T1 and TT
2024-11-15 16:31:22 +00:00
is_t1 = session.model is models.T1B1
session.set_expected_responses(
[
(is_t1, messages.PinMatrixRequest),
(
not is_t1,
messages.ButtonRequest(code=messages.ButtonRequestType.PinEntry),
),
messages.Address,
]
)
2024-11-15 16:31:22 +00:00
get_test_address(session)
@pytest.mark.models("legacy")
2024-11-15 16:31:22 +00:00
def test_incorrect_pin_t1(session: Session):
with pytest.raises(PinException):
2024-11-15 16:31:22 +00:00
session.client.use_pin_sequence([BAD_PIN])
get_test_address(session)
@pytest.mark.models("core")
2024-11-15 16:31:22 +00:00
def test_incorrect_pin_t2(session: Session):
session.lock() # TODO is the lock here necessary/correctly?
with session, session.client as client:
# After first incorrect attempt, TT will not raise an error, but instead ask for another attempt
client.use_pin_sequence([BAD_PIN, PIN4])
2024-11-15 16:31:22 +00:00
session.set_expected_responses(
[
messages.ButtonRequest(code=messages.ButtonRequestType.PinEntry),
messages.ButtonRequest(code=messages.ButtonRequestType.PinEntry),
messages.Address,
]
)
2024-11-15 16:31:22 +00:00
get_test_address(session)
@pytest.mark.models("legacy")
2024-11-15 16:31:22 +00:00
def test_exponential_backoff_t1(session: Session):
for attempt in range(3):
start = time.time()
2024-11-15 16:31:22 +00:00
with session, session.client as client, pytest.raises(PinException):
client.use_pin_sequence([BAD_PIN])
2024-11-15 16:31:22 +00:00
get_test_address(session)
2023-05-04 12:18:55 +00:00
check_pin_backoff_time(attempt, start)
@pytest.mark.models("core")
2024-11-15 16:31:22 +00:00
def test_exponential_backoff_t2(session: Session):
with session.client as client:
2023-05-04 12:18:55 +00:00
IF = InputFlowPINBackoff(client, BAD_PIN, PIN4)
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
session.lock()
get_test_address(session)