2019-09-18 14:13:58 +00:00
|
|
|
# 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 pytest
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
from trezorlib import btc, device, messages
|
2020-08-14 09:03:49 +00:00
|
|
|
from trezorlib.client import MAX_PIN_LENGTH, PASSPHRASE_TEST_PATH
|
2024-09-02 11:01:16 +00:00
|
|
|
from trezorlib.debuglink import LayoutType
|
2024-11-15 16:31:22 +00:00
|
|
|
from trezorlib.debuglink import SessionDebugWrapper as Session
|
2021-02-13 00:10:10 +00:00
|
|
|
from trezorlib.exceptions import Cancelled, TrezorFailure
|
2019-09-18 14:13:58 +00:00
|
|
|
|
2024-05-09 16:01:51 +00:00
|
|
|
from .. import buttons
|
2023-05-04 12:18:55 +00:00
|
|
|
from ..input_flows import (
|
|
|
|
InputFlowCodeChangeFail,
|
|
|
|
InputFlowNewCodeMismatch,
|
|
|
|
InputFlowWrongPIN,
|
|
|
|
)
|
|
|
|
|
2019-09-18 14:13:58 +00:00
|
|
|
PIN4 = "1234"
|
2020-08-14 09:03:49 +00:00
|
|
|
PIN60 = "789456" * 10
|
|
|
|
PIN_MAX = "".join(chr((i % 10) + ord("0")) for i in range(MAX_PIN_LENGTH))
|
2019-09-18 14:13:58 +00:00
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
pytestmark = pytest.mark.models("core")
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
def _check_pin(session: Session, pin: str):
|
2019-09-18 14:13:58 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
with session, session.client as client:
|
|
|
|
client.ui.__init__(client.debug)
|
|
|
|
client.use_pin_sequence([pin, pin, pin, pin, pin, pin])
|
|
|
|
session.lock()
|
|
|
|
assert session.features.pin_protection is True
|
|
|
|
assert session.features.unlocked is False
|
|
|
|
session.set_expected_responses([messages.ButtonRequest, messages.Address])
|
|
|
|
btc.get_address(session, "Testnet", PASSPHRASE_TEST_PATH)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
def _check_no_pin(session: Session):
|
|
|
|
session.lock()
|
|
|
|
assert session.features.pin_protection is False
|
2019-09-18 14:13:58 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
with session:
|
|
|
|
session.set_expected_responses([messages.Address])
|
|
|
|
btc.get_address(session, "Testnet", PASSPHRASE_TEST_PATH)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
def test_set_pin(session: Session):
|
|
|
|
assert session.features.pin_protection is False
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
# Check that there's no PIN protection
|
2024-11-15 16:31:22 +00:00
|
|
|
_check_no_pin(session)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
# Let's set new PIN
|
2024-11-15 16:31:22 +00:00
|
|
|
with session, session.client as client:
|
2024-09-02 11:01:16 +00:00
|
|
|
if client.layout_type is LayoutType.TR:
|
|
|
|
br_count = 6
|
|
|
|
else:
|
|
|
|
br_count = 4
|
2020-08-14 09:03:49 +00:00
|
|
|
client.use_pin_sequence([PIN_MAX, PIN_MAX])
|
2024-11-15 16:31:22 +00:00
|
|
|
session.set_expected_responses(
|
|
|
|
[messages.ButtonRequest] * br_count + [messages.Success]
|
2019-09-18 14:13:58 +00:00
|
|
|
)
|
2024-11-15 16:31:22 +00:00
|
|
|
device.change_pin(session)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
assert session.features.pin_protection is True
|
|
|
|
_check_pin(session, PIN_MAX)
|
2019-09-18 14:13:58 +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
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
# Check current PIN value
|
2024-11-15 16:31:22 +00:00
|
|
|
_check_pin(session, PIN4)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
# Let's change PIN
|
2024-11-15 16:31:22 +00:00
|
|
|
with session, session.client as client:
|
2020-08-14 09:03:49 +00:00
|
|
|
client.use_pin_sequence([PIN4, PIN_MAX, PIN_MAX])
|
2024-09-02 11:01:16 +00:00
|
|
|
if client.layout_type is LayoutType.TR:
|
|
|
|
br_count = 6
|
|
|
|
else:
|
|
|
|
br_count = 5
|
2024-11-15 16:31:22 +00:00
|
|
|
session.set_expected_responses(
|
|
|
|
[messages.ButtonRequest] * br_count
|
|
|
|
+ [messages.Success] # , messages.Features]
|
2019-09-18 14:13:58 +00:00
|
|
|
)
|
2024-11-15 16:31:22 +00:00
|
|
|
device.change_pin(session)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
# Check that there's still 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 True
|
2019-09-18 14:13:58 +00:00
|
|
|
# Check that the PIN is correct
|
2024-11-15 16:31:22 +00:00
|
|
|
_check_pin(session, PIN_MAX)
|
2019-09-18 14:13:58 +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
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
# Check current PIN value
|
2024-11-15 16:31:22 +00:00
|
|
|
_check_pin(session, PIN4)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
# Let's remove PIN
|
2024-11-15 16:31:22 +00:00
|
|
|
with session, session.client as client:
|
2020-04-27 11:15:25 +00:00
|
|
|
client.use_pin_sequence([PIN4])
|
2024-11-15 16:31:22 +00:00
|
|
|
session.set_expected_responses(
|
|
|
|
[messages.ButtonRequest] * 3 + [messages.Success]
|
2019-09-18 14:13:58 +00:00
|
|
|
)
|
2024-11-15 16:31:22 +00:00
|
|
|
device.change_pin(session, remove=True)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
# Check that there's 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)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
def test_set_failed(session: Session):
|
|
|
|
assert session.features.pin_protection is False
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
# Check that there's no PIN protection
|
2024-11-15 16:31:22 +00:00
|
|
|
_check_no_pin(session)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
with session, session.client as client, pytest.raises(TrezorFailure):
|
2023-11-21 15:55:46 +00:00
|
|
|
IF = InputFlowNewCodeMismatch(client, PIN4, PIN60, what="pin")
|
2023-05-04 12:18:55 +00:00
|
|
|
client.set_input_flow(IF.get())
|
2019-09-18 14:13:58 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
device.change_pin(session)
|
2019-09-18 14:13:58 +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)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin=PIN4)
|
2024-11-15 16:31:22 +00:00
|
|
|
def test_change_failed(session: Session):
|
|
|
|
assert session.features.pin_protection is True
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
# Check current PIN value
|
2024-11-15 16:31:22 +00:00
|
|
|
_check_pin(session, PIN4)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
with session, session.client as client, pytest.raises(Cancelled):
|
|
|
|
IF = InputFlowCodeChangeFail(session, PIN4, "457891", "381847")
|
2023-05-04 12:18:55 +00:00
|
|
|
client.set_input_flow(IF.get())
|
2019-09-18 14:13:58 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
device.change_pin(session)
|
2019-09-18 14:13:58 +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)
|
2021-02-13 00:10:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin=PIN4)
|
2024-11-15 16:31:22 +00:00
|
|
|
def test_change_invalid_current(session: Session):
|
|
|
|
assert session.features.pin_protection is True
|
2021-02-13 00:10:10 +00:00
|
|
|
|
|
|
|
# Check current PIN value
|
2024-11-15 16:31:22 +00:00
|
|
|
_check_pin(session, PIN4)
|
2021-02-13 00:10:10 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
with session, session.client as client, pytest.raises(TrezorFailure):
|
2023-05-04 12:18:55 +00:00
|
|
|
IF = InputFlowWrongPIN(client, PIN60)
|
|
|
|
client.set_input_flow(IF.get())
|
2021-02-13 00:10:10 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
device.change_pin(session)
|
2021-02-13 00:10:10 +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)
|
2024-05-09 16:01:51 +00:00
|
|
|
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
@pytest.mark.models("mercury")
|
2024-05-09 16:01:51 +00:00
|
|
|
@pytest.mark.setup_client(pin=None)
|
2024-11-15 16:31:22 +00:00
|
|
|
def test_pin_menu_cancel_setup(session: Session):
|
2024-05-09 16:01:51 +00:00
|
|
|
def cancel_pin_setup_input_flow():
|
|
|
|
yield
|
2024-11-15 16:31:22 +00:00
|
|
|
debug = session.client.debug
|
2024-05-09 16:01:51 +00:00
|
|
|
# enter context menu
|
2024-11-15 16:31:22 +00:00
|
|
|
debug.click(buttons.CORNER_BUTTON)
|
|
|
|
debug.synchronize_at("VerticalMenu")
|
2024-05-09 16:01:51 +00:00
|
|
|
# click "Cancel PIN setup"
|
2024-11-15 16:31:22 +00:00
|
|
|
debug.click(buttons.VERTICAL_MENU[0])
|
|
|
|
debug.synchronize_at("Paragraphs")
|
2024-05-09 16:01:51 +00:00
|
|
|
# swipe through info screen
|
2024-11-15 16:31:22 +00:00
|
|
|
debug.swipe_up()
|
|
|
|
debug.synchronize_at("PromptScreen")
|
2024-05-09 16:01:51 +00:00
|
|
|
# tap to confirm
|
2024-11-15 16:31:22 +00:00
|
|
|
debug.click(buttons.TAP_TO_CONFIRM)
|
2024-05-09 16:01:51 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
with session, session.client as client, pytest.raises(Cancelled):
|
2024-05-09 16:01:51 +00:00
|
|
|
client.set_input_flow(cancel_pin_setup_input_flow)
|
2024-11-15 16:31:22 +00:00
|
|
|
session.call(messages.ChangePin())
|
|
|
|
_check_no_pin(session)
|