2013-10-11 01:51:45 +00:00
|
|
|
import time
|
2013-01-14 13:44:11 +00:00
|
|
|
import unittest
|
|
|
|
import common
|
|
|
|
|
2014-02-06 09:43:26 +00:00
|
|
|
from trezorlib import messages_pb2 as proto
|
|
|
|
from trezorlib import types_pb2 as types
|
2014-02-15 19:31:34 +00:00
|
|
|
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
|
|
|
|
2013-09-13 03:28:29 +00:00
|
|
|
class TestProtectCall(common.TrezorTest):
|
2014-02-13 15:47:28 +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
|
2014-02-13 15:47:28 +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')
|
2013-10-11 01:51:45 +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
|
|
|
|
|
|
|
|
# Scenario 1 - Received unexpected message
|
|
|
|
self.client.set_expected_responses([])
|
|
|
|
self.assertRaises(CallException, self._some_protected_call, True, True, True)
|
|
|
|
|
|
|
|
# Scenario 2 - Received other than expected message
|
|
|
|
self.client.set_expected_responses([proto.Success()])
|
|
|
|
self.assertRaises(CallException, self._some_protected_call, True, True, True)
|
|
|
|
|
|
|
|
# Scenario 3 - Not received expected message
|
|
|
|
self.client.set_expected_responses([proto.ButtonRequest(),
|
|
|
|
proto.Success(),
|
|
|
|
proto.Success()]) # This is expected, but not received
|
|
|
|
self.assertRaises(Exception, self._some_protected_call, True, False, False)
|
|
|
|
|
|
|
|
# Scenario 4 - Received what expected
|
|
|
|
self.client.set_expected_responses([proto.ButtonRequest(),
|
|
|
|
proto.PinMatrixRequest(),
|
2014-02-20 18:15:43 +00:00
|
|
|
proto.PassphraseRequest(),
|
2014-02-15 19:31:34 +00:00
|
|
|
proto.Success(message='random data')])
|
|
|
|
self._some_protected_call(True, True, True)
|
|
|
|
|
|
|
|
# Scenario 5 - Failed message by field filter
|
|
|
|
self.client.set_expected_responses([proto.ButtonRequest(),
|
|
|
|
proto.PinMatrixRequest(),
|
|
|
|
proto.Success(message='wrong data')])
|
|
|
|
self.assertRaises(CallException, self._some_protected_call, True, True, True)
|
|
|
|
|
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()
|
|
|
|
|
2014-02-13 15:47:28 +00:00
|
|
|
self.assertEqual(self.client.debug.read_pin()[0], '')
|
2014-02-15 19:31:34 +00:00
|
|
|
self.client.set_expected_responses([proto.Success()])
|
2014-02-13 15:47:28 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
self.assertEqual(self.client.debug.read_pin()[0], self.pin4)
|
2014-02-13 15:47:28 +00:00
|
|
|
self.client.setup_debuglink(button=True, pin_correct=True)
|
2014-02-15 19:31:34 +00:00
|
|
|
self.client.set_expected_responses([proto.ButtonRequest(),
|
|
|
|
proto.PinMatrixRequest(),
|
|
|
|
proto.Success()])
|
2014-02-13 15:47:28 +00:00
|
|
|
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)
|
2014-02-13 15:47:28 +00:00
|
|
|
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()
|
2014-02-13 15:47:28 +00:00
|
|
|
self.client.setup_debuglink(button=True, pin_correct=False) # PIN cancel
|
|
|
|
self.assertRaises(PinException, self._some_protected_call, False, True, False)
|
2013-10-11 01:51:45 +00:00
|
|
|
|
|
|
|
def test_exponential_backoff_with_reboot(self):
|
2014-02-17 00:54:54 +00:00
|
|
|
self.setup_mnemonic_pin_passphrase()
|
|
|
|
|
2013-10-11 01:51:45 +00:00
|
|
|
self.client.setup_debuglink(button=True, pin_correct=False)
|
2014-02-17 00:54:54 +00:00
|
|
|
|
2013-10-11 01:51:45 +00:00
|
|
|
def test_backoff(attempts, start):
|
|
|
|
expected = 1.8 ** attempts
|
|
|
|
got = time.time() - start
|
|
|
|
|
|
|
|
msg = "Pin delay expected to be at least %s seconds, got %s" % (expected, got)
|
|
|
|
print msg
|
|
|
|
self.assertLessEqual(expected, got, msg)
|
|
|
|
|
2014-02-03 23:32:10 +00:00
|
|
|
for attempt in range(1, 4):
|
2013-10-11 01:51:45 +00:00
|
|
|
start = time.time()
|
2014-02-13 15:47:28 +00:00
|
|
|
self.assertRaises(PinException, self._some_protected_call, False, True, False)
|
2013-10-11 01:51:45 +00:00
|
|
|
test_backoff(attempt, start)
|
2014-02-15 19:31:34 +00:00
|
|
|
'''
|
2013-10-11 01:51:45 +00:00
|
|
|
# Unplug Trezor now
|
|
|
|
self.client.debuglink.stop()
|
|
|
|
self.client.close()
|
|
|
|
|
|
|
|
# Give it some time to reboot (it may take some time on RPi)
|
2013-10-11 04:19:06 +00:00
|
|
|
boot_delay = 20
|
2013-10-11 01:51:45 +00:00
|
|
|
time.sleep(boot_delay)
|
|
|
|
|
|
|
|
# Connect to Trezor again
|
2013-10-11 04:19:06 +00:00
|
|
|
start = time.time()
|
2013-10-11 01:51:45 +00:00
|
|
|
self.setUp()
|
2013-10-11 04:19:06 +00:00
|
|
|
expected = 1.8 ** attempt / 2 # This test isn't accurate, let's expect at least some delay
|
|
|
|
took = time.time() - start
|
|
|
|
print "Expected reboot time at least %s seconds" % expected
|
|
|
|
print "Rebooted in %s seconds" % took
|
|
|
|
self.assertLessEqual(expected, time.time() - start, "Bootup took %s seconds, expected %s seconds or more!" % (took, expected))
|
2014-01-12 15:16:07 +00:00
|
|
|
'''
|
2013-10-11 01:51:45 +00:00
|
|
|
|
2013-01-14 13:44:11 +00:00
|
|
|
if __name__ == '__main__':
|
2013-09-13 03:28:29 +00:00
|
|
|
unittest.main()
|