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
|
|
|
|
|
2020-04-27 11:15:25 +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
|
2021-02-13 00:10:10 +00:00
|
|
|
from trezorlib.exceptions import Cancelled, TrezorFailure
|
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
|
|
|
|
|
|
|
pytestmark = pytest.mark.skip_t1
|
|
|
|
|
|
|
|
|
|
|
|
def _check_pin(client, pin):
|
2020-08-25 14:28:32 +00:00
|
|
|
client.lock()
|
2019-09-18 14:13:58 +00:00
|
|
|
assert client.features.pin_protection is True
|
2020-06-19 14:04:24 +00:00
|
|
|
assert client.features.unlocked is False
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
with client:
|
2020-04-27 11:15:25 +00:00
|
|
|
client.use_pin_sequence([pin])
|
2020-09-15 11:06:41 +00:00
|
|
|
client.set_expected_responses([messages.ButtonRequest, messages.Address])
|
2020-04-27 11:15:25 +00:00
|
|
|
btc.get_address(client, "Testnet", PASSPHRASE_TEST_PATH)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _check_no_pin(client):
|
2020-08-25 14:28:32 +00:00
|
|
|
client.lock()
|
2019-09-18 14:13:58 +00:00
|
|
|
assert client.features.pin_protection is False
|
|
|
|
|
|
|
|
with client:
|
2020-09-15 11:06:41 +00:00
|
|
|
client.set_expected_responses([messages.Address])
|
2020-04-27 11:15:25 +00:00
|
|
|
btc.get_address(client, "Testnet", PASSPHRASE_TEST_PATH)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_set_pin(client):
|
|
|
|
assert client.features.pin_protection is False
|
|
|
|
|
|
|
|
# Check that there's no PIN protection
|
|
|
|
_check_no_pin(client)
|
|
|
|
|
|
|
|
# Let's set new PIN
|
|
|
|
with client:
|
2020-08-14 09:03:49 +00:00
|
|
|
client.use_pin_sequence([PIN_MAX, PIN_MAX])
|
2019-09-18 14:13:58 +00:00
|
|
|
client.set_expected_responses(
|
2020-09-15 11:06:41 +00:00
|
|
|
[messages.ButtonRequest] * 4 + [messages.Success, messages.Features]
|
2019-09-18 14:13:58 +00:00
|
|
|
)
|
|
|
|
device.change_pin(client)
|
|
|
|
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.pin_protection is True
|
2020-08-14 09:03:49 +00:00
|
|
|
_check_pin(client, PIN_MAX)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin=PIN4)
|
|
|
|
def test_change_pin(client):
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
|
|
|
|
# Check current PIN value
|
|
|
|
_check_pin(client, PIN4)
|
|
|
|
|
|
|
|
# Let's change PIN
|
|
|
|
with client:
|
2020-08-14 09:03:49 +00:00
|
|
|
client.use_pin_sequence([PIN4, PIN_MAX, PIN_MAX])
|
2019-09-18 14:13:58 +00:00
|
|
|
client.set_expected_responses(
|
2020-09-15 11:06:41 +00:00
|
|
|
[messages.ButtonRequest] * 5 + [messages.Success, messages.Features]
|
2019-09-18 14:13:58 +00:00
|
|
|
)
|
|
|
|
device.change_pin(client)
|
|
|
|
|
|
|
|
# Check that there's still PIN protection now
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
# Check that the PIN is correct
|
2020-08-14 09:03:49 +00:00
|
|
|
_check_pin(client, PIN_MAX)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin=PIN4)
|
|
|
|
def test_remove_pin(client):
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
|
|
|
|
# Check current PIN value
|
|
|
|
_check_pin(client, PIN4)
|
|
|
|
|
|
|
|
# Let's remove PIN
|
|
|
|
with client:
|
2020-04-27 11:15:25 +00:00
|
|
|
client.use_pin_sequence([PIN4])
|
2019-09-18 14:13:58 +00:00
|
|
|
client.set_expected_responses(
|
2020-09-15 11:06:41 +00:00
|
|
|
[messages.ButtonRequest] * 3 + [messages.Success, messages.Features]
|
2019-09-18 14:13:58 +00:00
|
|
|
)
|
|
|
|
device.change_pin(client, remove=True)
|
|
|
|
|
|
|
|
# Check that there's no PIN protection now
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.pin_protection is False
|
|
|
|
_check_no_pin(client)
|
|
|
|
|
|
|
|
|
|
|
|
def test_set_failed(client):
|
|
|
|
assert client.features.pin_protection is False
|
|
|
|
|
|
|
|
# Check that there's no PIN protection
|
|
|
|
_check_no_pin(client)
|
|
|
|
|
|
|
|
# Let's set new PIN
|
|
|
|
def input_flow():
|
|
|
|
yield # do you want to set pin?
|
|
|
|
client.debug.press_yes()
|
|
|
|
yield # enter new pin
|
|
|
|
client.debug.input(PIN4)
|
|
|
|
yield # enter new pin again (but different)
|
2020-08-14 09:03:49 +00:00
|
|
|
client.debug.input(PIN60)
|
2019-09-18 14:13:58 +00:00
|
|
|
|
|
|
|
# failed retry
|
|
|
|
yield # enter new pin
|
|
|
|
client.cancel()
|
|
|
|
|
|
|
|
with client, pytest.raises(Cancelled):
|
2020-09-15 11:06:41 +00:00
|
|
|
client.set_expected_responses([messages.ButtonRequest] * 4 + [messages.Failure])
|
2019-09-18 14:13:58 +00:00
|
|
|
client.set_input_flow(input_flow)
|
|
|
|
|
|
|
|
device.change_pin(client)
|
|
|
|
|
|
|
|
# Check that there's still no PIN protection now
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.pin_protection is False
|
|
|
|
_check_no_pin(client)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin=PIN4)
|
|
|
|
def test_change_failed(client):
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
|
|
|
|
# Check current PIN value
|
|
|
|
_check_pin(client, PIN4)
|
|
|
|
|
|
|
|
# Let's set new PIN
|
|
|
|
def input_flow():
|
|
|
|
yield # do you want to change pin?
|
|
|
|
client.debug.press_yes()
|
|
|
|
yield # enter current pin
|
|
|
|
client.debug.input(PIN4)
|
|
|
|
yield # enter new pin
|
|
|
|
client.debug.input("457891")
|
|
|
|
yield # enter new pin again (but different)
|
|
|
|
client.debug.input("381847")
|
|
|
|
|
|
|
|
# failed retry
|
|
|
|
yield # enter current pin again
|
|
|
|
client.cancel()
|
|
|
|
|
|
|
|
with client, pytest.raises(Cancelled):
|
2020-09-15 11:06:41 +00:00
|
|
|
client.set_expected_responses([messages.ButtonRequest] * 5 + [messages.Failure])
|
2019-09-18 14:13:58 +00:00
|
|
|
client.set_input_flow(input_flow)
|
|
|
|
|
|
|
|
device.change_pin(client)
|
|
|
|
|
|
|
|
# Check that there's still old PIN protection
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
_check_pin(client, PIN4)
|
2021-02-13 00:10:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(pin=PIN4)
|
|
|
|
def test_change_invalid_current(client):
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
|
|
|
|
# Check current PIN value
|
|
|
|
_check_pin(client, PIN4)
|
|
|
|
|
|
|
|
# Let's set new PIN
|
|
|
|
def input_flow():
|
|
|
|
yield # do you want to change pin?
|
|
|
|
client.debug.press_yes()
|
|
|
|
yield # enter wrong current pin
|
|
|
|
client.debug.input(PIN60)
|
|
|
|
yield
|
|
|
|
client.debug.press_no()
|
|
|
|
|
|
|
|
with client, pytest.raises(TrezorFailure):
|
|
|
|
client.set_expected_responses([messages.ButtonRequest] * 3 + [messages.Failure])
|
|
|
|
client.set_input_flow(input_flow)
|
|
|
|
|
|
|
|
device.change_pin(client)
|
|
|
|
|
|
|
|
# Check that there's still old PIN protection
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.pin_protection is True
|
|
|
|
_check_pin(client, PIN4)
|