1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-27 01:48:17 +00:00
trezor-firmware/tests/device_tests/test_protect_call.py

135 lines
5.4 KiB
Python
Raw Normal View History

2017-01-03 18:40:05 +00:00
# This file is part of the TREZOR project.
#
# Copyright (C) 2012-2016 Marek Palatinus <slush@satoshilabs.com>
# Copyright (C) 2012-2016 Pavol Rusnak <stick@satoshilabs.com>
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
2016-05-20 20:27:20 +00:00
from __future__ import print_function
import time
2013-01-14 13:44:11 +00:00
import unittest
import common
from trezorlib import messages as proto
from trezorlib.client import PinException, CallException
2014-02-03 23:32:10 +00:00
# FIXME TODO Add passphrase tests
2013-01-14 13:44:11 +00:00
2017-06-23 19:31:42 +00:00
2013-09-13 03:28:29 +00:00
class TestProtectCall(common.TrezorTest):
2014-06-04 15:59:16 +00:00
def _some_protected_call(self, button, pin, passphrase):
2013-01-14 16:05:38 +00:00
# This method perform any call which have protection in the device
2017-06-23 19:31:42 +00:00
res = self.client.ping(
'random data',
button_protection=button,
pin_protection=pin,
passphrase_protection=passphrase
)
2014-02-03 23:32:10 +00:00
self.assertEqual(res, 'random data')
2014-06-04 15:59:16 +00:00
"""
2014-02-15 19:31:34 +00:00
def test_expected_responses(self):
2014-02-17 00:54:54 +00:00
self.setup_mnemonic_pin_passphrase()
2014-02-15 19:31:34 +00:00
# This is low-level test of set_expected_responses()
# feature of debugging client
with self.client:
# Scenario 1 - Received unexpected message
self.client.set_expected_responses([])
self.assertRaises(CallException, self._some_protected_call, True, True, True)
with self.client:
# Scenario 2 - Received other than expected message
self.client.set_expected_responses([proto.Success()])
self.assertRaises(CallException, self._some_protected_call, True, True, True)
def scenario3():
with self.client:
# Scenario 3 - Not received expected message
self.client.set_expected_responses([proto.ButtonRequest(),
proto.Success(),
proto.Success()]) # This is expected, but not received
self._some_protected_call(True, False, False)
self.assertRaises(Exception, scenario3)
with self.client:
# Scenario 4 - Received what expected
self.client.set_expected_responses([proto.ButtonRequest(),
proto.PinMatrixRequest(),
proto.PassphraseRequest(),
proto.Success(message='random data')])
self._some_protected_call(True, True, True)
def scenario5():
with self.client:
# Scenario 5 - Failed message by field filter
self.client.set_expected_responses([proto.ButtonRequest(),
proto.Success(message='wrong data')])
self._some_protected_call(True, True, True)
self.assertRaises(CallException, scenario5)
2014-06-04 15:59:16 +00:00
"""
2014-02-15 19:31:34 +00:00
2013-01-14 16:05:38 +00:00
def test_no_protection(self):
2014-02-17 00:54:54 +00:00
self.setup_mnemonic_nopin_nopassphrase()
with self.client:
self.assertEqual(self.client.debug.read_pin()[0], None)
self.client.set_expected_responses([proto.Success()])
self._some_protected_call(False, True, True)
2013-01-14 16:05:38 +00:00
2013-09-01 01:35:31 +00:00
def test_pin(self):
2014-02-17 00:54:54 +00:00
self.setup_mnemonic_pin_passphrase()
with self.client:
self.assertEqual(self.client.debug.read_pin()[0], self.pin4)
self.client.setup_debuglink(button=True, pin_correct=True)
self.client.set_expected_responses([proto.ButtonRequest(),
proto.PinMatrixRequest(),
proto.Success()])
self._some_protected_call(True, True, False)
2013-01-14 16:05:38 +00:00
def test_incorrect_pin(self):
2014-02-17 00:54:54 +00:00
self.setup_mnemonic_pin_passphrase()
2013-09-13 03:28:29 +00:00
self.client.setup_debuglink(button=True, pin_correct=False)
self.assertRaises(PinException, self._some_protected_call, False, True, False)
2013-10-10 15:18:02 +00:00
def test_cancelled_pin(self):
2014-02-17 00:54:54 +00:00
self.setup_mnemonic_pin_passphrase()
self.client.setup_debuglink(button=True, pin_correct=False) # PIN cancel
self.assertRaises(PinException, self._some_protected_call, False, True, False)
def test_exponential_backoff_with_reboot(self):
2014-02-17 00:54:54 +00:00
self.setup_mnemonic_pin_passphrase()
self.client.setup_debuglink(button=True, pin_correct=False)
2014-02-17 00:54:54 +00:00
def test_backoff(attempts, start):
2016-05-20 15:20:11 +00:00
if attempts <= 1:
expected = 0
2016-05-20 15:20:11 +00:00
else:
expected = (2 ** (attempts - 2))
got = time.time() - start
msg = "Pin delay expected to be at least %s seconds, got %s" % (expected, got)
2016-05-05 01:16:17 +00:00
print(msg)
self.assertLessEqual(expected, got, msg)
2014-06-04 15:59:16 +00:00
for attempt in range(1, 6):
start = time.time()
self.assertRaises(PinException, self._some_protected_call, False, True, False)
test_backoff(attempt, start)