1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-29 17:48:10 +00:00
trezor-firmware/tests/device_tests/test_msg_changepin.py

271 lines
9.6 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
from trezorlib import messages as proto
2017-06-23 19:31:42 +00:00
2019-09-11 12:29:39 +00:00
PIN4 = "1234"
PIN6 = "789456"
2018-08-13 16:21:24 +00:00
2014-02-21 03:46:33 +00:00
@pytest.mark.skip_t2
2019-09-11 12:29:39 +00:00
class TestMsgChangepin:
def test_set_pin(self, client):
features = client.call_raw(proto.Initialize())
assert features.pin_protection is False
2014-02-21 03:46:33 +00:00
# Check that there's no PIN protection
2020-01-21 09:05:48 +00:00
ret = client.call_raw(proto.GetAddress())
assert isinstance(ret, proto.Address)
2014-02-21 03:46:33 +00:00
# Let's set new PIN
ret = client.call_raw(proto.ChangePin())
assert isinstance(ret, proto.ButtonRequest)
2014-02-21 03:46:33 +00:00
# Press button
client.debug.press_yes()
ret = client.call_raw(proto.ButtonAck())
2014-02-21 03:46:33 +00:00
# Send the PIN for first time
assert isinstance(ret, proto.PinMatrixRequest)
2019-09-11 12:29:39 +00:00
pin_encoded = client.debug.encode_pin(PIN6)
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
2014-02-21 03:46:33 +00:00
# Send the PIN for second time
assert isinstance(ret, proto.PinMatrixRequest)
2019-09-11 12:29:39 +00:00
pin_encoded = client.debug.encode_pin(PIN6)
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
2014-02-21 03:46:33 +00:00
# Now we're done
assert isinstance(ret, proto.Success)
2014-02-21 03:46:33 +00:00
# Check that there's PIN protection now
features = client.call_raw(proto.Initialize())
assert features.pin_protection is True
2014-02-21 03:46:33 +00:00
# Check that the PIN is correct
2019-09-11 12:29:39 +00:00
self.check_pin(client, PIN6)
2014-02-21 03:46:33 +00:00
@pytest.mark.setup_client(pin=True)
def test_change_pin(self, client):
features = client.call_raw(proto.Initialize())
assert features.pin_protection is True
2014-02-21 03:46:33 +00:00
# Check that there's PIN protection
2020-01-21 09:05:48 +00:00
ret = client.call_raw(proto.GetAddress())
assert isinstance(ret, proto.PinMatrixRequest)
client.call_raw(proto.Cancel())
2014-02-21 03:46:33 +00:00
# Check current PIN value
2019-09-11 12:29:39 +00:00
self.check_pin(client, PIN4)
2014-02-21 03:46:33 +00:00
# Let's change PIN
ret = client.call_raw(proto.ChangePin())
assert isinstance(ret, proto.ButtonRequest)
2014-02-21 03:46:33 +00:00
# Press button
client.debug.press_yes()
ret = client.call_raw(proto.ButtonAck())
2014-02-21 03:46:33 +00:00
# Send current PIN
assert isinstance(ret, proto.PinMatrixRequest)
pin_encoded = client.debug.read_pin_encoded()
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
2014-02-21 03:46:33 +00:00
# Send new PIN for first time
assert isinstance(ret, proto.PinMatrixRequest)
2019-09-11 12:29:39 +00:00
pin_encoded = client.debug.encode_pin(PIN6)
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
2014-02-21 03:46:33 +00:00
# Send the PIN for second time
assert isinstance(ret, proto.PinMatrixRequest)
2019-09-11 12:29:39 +00:00
pin_encoded = client.debug.encode_pin(PIN6)
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
2014-02-21 03:46:33 +00:00
# Now we're done
assert isinstance(ret, proto.Success)
2014-02-21 03:46:33 +00:00
# Check that there's still PIN protection now
features = client.call_raw(proto.Initialize())
assert features.pin_protection is True
2014-02-21 03:46:33 +00:00
# Check that the PIN is correct
2019-09-11 12:29:39 +00:00
self.check_pin(client, PIN6)
2014-02-21 03:46:33 +00:00
@pytest.mark.setup_client(pin=True)
def test_remove_pin(self, client):
features = client.call_raw(proto.Initialize())
assert features.pin_protection is True
2014-02-21 03:46:33 +00:00
# Check that there's PIN protection
2020-01-21 09:05:48 +00:00
ret = client.call_raw(proto.GetAddress())
assert isinstance(ret, proto.PinMatrixRequest)
client.call_raw(proto.Cancel())
2014-02-21 03:46:33 +00:00
# Let's remove PIN
ret = client.call_raw(proto.ChangePin(remove=True))
assert isinstance(ret, proto.ButtonRequest)
2014-02-21 03:46:33 +00:00
# Press button
client.debug.press_yes()
ret = client.call_raw(proto.ButtonAck())
2014-02-21 03:46:33 +00:00
# Send current PIN
assert isinstance(ret, proto.PinMatrixRequest)
pin_encoded = client.debug.read_pin_encoded()
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
2014-02-21 03:46:33 +00:00
# Now we're done
assert isinstance(ret, proto.Success)
2014-02-21 03:46:33 +00:00
# Check that there's no PIN protection now
features = client.call_raw(proto.Initialize())
assert features.pin_protection is False
2020-01-21 09:05:48 +00:00
ret = client.call_raw(proto.GetAddress())
assert isinstance(ret, proto.Address)
2020-03-13 14:34:07 +00:00
def test_set_mismatch(self, client):
features = client.call_raw(proto.Initialize())
assert features.pin_protection is False
2014-02-21 21:22:19 +00:00
# Check that there's no PIN protection
2020-01-21 09:05:48 +00:00
ret = client.call_raw(proto.GetAddress())
assert isinstance(ret, proto.Address)
2014-02-21 21:22:19 +00:00
# Let's set new PIN
ret = client.call_raw(proto.ChangePin())
assert isinstance(ret, proto.ButtonRequest)
2014-02-21 21:22:19 +00:00
# Press button
client.debug.press_yes()
ret = client.call_raw(proto.ButtonAck())
2014-02-21 21:22:19 +00:00
# Send the PIN for first time
assert isinstance(ret, proto.PinMatrixRequest)
2019-09-11 12:29:39 +00:00
pin_encoded = client.debug.encode_pin(PIN6)
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
2014-02-21 21:22:19 +00:00
# Send the PIN for second time, but with typo
assert isinstance(ret, proto.PinMatrixRequest)
2019-09-11 12:29:39 +00:00
pin_encoded = client.debug.encode_pin(PIN4)
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
2014-02-21 21:22:19 +00:00
# Now it should fail, because pins are different
assert isinstance(ret, proto.Failure)
2014-02-21 21:22:19 +00:00
# Check that there's still no PIN protection now
features = client.call_raw(proto.Initialize())
assert features.pin_protection is False
2020-01-21 09:05:48 +00:00
ret = client.call_raw(proto.GetAddress())
assert isinstance(ret, proto.Address)
2014-02-21 21:22:19 +00:00
@pytest.mark.setup_client(pin=True)
2020-03-13 14:34:07 +00:00
def test_change_mismatch(self, client):
features = client.call_raw(proto.Initialize())
assert features.pin_protection is True
# Let's set new PIN
ret = client.call_raw(proto.ChangePin())
assert isinstance(ret, proto.ButtonRequest)
# Press button
client.debug.press_yes()
ret = client.call_raw(proto.ButtonAck())
# Send current PIN
assert isinstance(ret, proto.PinMatrixRequest)
pin_encoded = client.debug.read_pin_encoded()
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
# Send the PIN for first time
assert isinstance(ret, proto.PinMatrixRequest)
2019-09-11 12:29:39 +00:00
pin_encoded = client.debug.encode_pin(PIN6)
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
# Send the PIN for second time, but with typo
assert isinstance(ret, proto.PinMatrixRequest)
2019-09-11 12:29:39 +00:00
pin_encoded = client.debug.encode_pin(PIN6 + "3")
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
# Now it should fail, because pins are different
assert isinstance(ret, proto.Failure)
# Check that there's still old PIN protection
features = client.call_raw(proto.Initialize())
assert features.pin_protection is True
2019-09-11 12:29:39 +00:00
self.check_pin(client, PIN4)
2020-03-13 14:34:07 +00:00
@pytest.mark.parametrize("invalid_pin", ("1204", "", "1234567891"))
def test_set_invalid(self, client, invalid_pin):
features = client.call_raw(proto.Initialize())
assert features.pin_protection is False
# Let's set an invalid PIN
ret = client.call_raw(proto.ChangePin())
assert isinstance(ret, proto.ButtonRequest)
# Press button
client.debug.press_yes()
ret = client.call_raw(proto.ButtonAck())
# Send a PIN containing an invalid digit
assert isinstance(ret, proto.PinMatrixRequest)
ret = client.call_raw(proto.PinMatrixAck(pin=invalid_pin))
# Ensure the invalid PIN is detected
assert isinstance(ret, proto.Failure)
# Check that there's still no PIN protection now
features = client.call_raw(proto.Initialize())
assert features.pin_protection is False
ret = client.call_raw(proto.GetAddress())
assert isinstance(ret, proto.Address)
@pytest.mark.parametrize("invalid_pin", ("1204", "", "1234567891"))
@pytest.mark.setup_client(pin=True)
def test_remove_invalid(self, client, invalid_pin):
features = client.call_raw(proto.Initialize())
assert features.pin_protection is True
# Let's change the PIN
ret = client.call_raw(proto.ChangePin(remove=True))
assert isinstance(ret, proto.ButtonRequest)
# Press button
client.debug.press_yes()
ret = client.call_raw(proto.ButtonAck())
# Instead of the old PIN, send a PIN containing an invalid digit
assert isinstance(ret, proto.PinMatrixRequest)
ret = client.call_raw(proto.PinMatrixAck(pin=invalid_pin))
# Ensure the invalid PIN is detected
assert isinstance(ret, proto.Failure)
# Check that there's still old PIN protection
features = client.call_raw(proto.Initialize())
assert features.pin_protection is True
self.check_pin(client, PIN4)
def check_pin(self, client, pin):
client.clear_session()
2020-01-21 09:05:48 +00:00
ret = client.call_raw(proto.GetAddress())
assert isinstance(ret, proto.PinMatrixRequest)
pin_encoded = client.debug.encode_pin(pin)
ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded))
2020-01-21 09:05:48 +00:00
assert isinstance(ret, proto.Address)