2013-10-11 01:51:45 +00:00
|
|
|
import time
|
2013-01-14 13:44:11 +00:00
|
|
|
import unittest
|
|
|
|
import common
|
|
|
|
|
2013-09-13 03:28:29 +00:00
|
|
|
from trezorlib.client import CallException, PinException
|
|
|
|
from trezorlib import proto
|
2013-01-14 13:44:11 +00:00
|
|
|
|
2013-09-13 03:28:29 +00:00
|
|
|
class TestProtectCall(common.TrezorTest):
|
2013-01-14 16:05:38 +00:00
|
|
|
def _some_protected_call(self):
|
|
|
|
# This method perform any call which have protection in the device
|
|
|
|
entropy_len = 10
|
2013-09-13 03:28:29 +00:00
|
|
|
entropy = self.client.get_entropy(entropy_len)
|
2013-01-14 16:05:38 +00:00
|
|
|
self.assertEqual(len(entropy), entropy_len)
|
2013-10-11 01:51:45 +00:00
|
|
|
|
2013-01-14 16:05:38 +00:00
|
|
|
def test_no_protection(self):
|
2013-09-13 03:28:29 +00:00
|
|
|
self.client.load_device(seed=self.mnemonic1, pin='')
|
2013-01-14 13:44:11 +00:00
|
|
|
|
2013-09-13 03:28:29 +00:00
|
|
|
self.assertEqual(self.client.debuglink.read_pin()[0], '')
|
2013-01-14 16:05:38 +00:00
|
|
|
self._some_protected_call()
|
|
|
|
|
2013-09-01 01:35:31 +00:00
|
|
|
def test_pin(self):
|
2013-09-13 03:28:29 +00:00
|
|
|
self.client.load_device(seed=self.mnemonic1, pin=self.pin2)
|
2013-01-14 16:05:38 +00:00
|
|
|
|
2013-09-13 03:28:29 +00:00
|
|
|
self.assertEqual(self.client.debuglink.read_pin()[0], self.pin2)
|
2013-01-14 16:05:38 +00:00
|
|
|
self._some_protected_call()
|
2013-01-14 13:44:11 +00:00
|
|
|
|
2013-01-14 16:05:38 +00:00
|
|
|
def test_incorrect_pin(self):
|
2013-09-13 03:28:29 +00:00
|
|
|
self.client.setup_debuglink(button=True, pin_correct=False)
|
2013-01-14 16:05:38 +00:00
|
|
|
self.assertRaises(PinException, self._some_protected_call)
|
2013-10-10 15:18:02 +00:00
|
|
|
|
|
|
|
def test_cancelled_pin(self):
|
|
|
|
self.client.setup_debuglink(button=True, pin_correct=-1) # PIN cancel
|
|
|
|
self.assertRaises(PinException, self._some_protected_call)
|
2013-10-11 01:51:45 +00:00
|
|
|
|
|
|
|
def test_exponential_backoff_with_reboot(self):
|
|
|
|
self.client.setup_debuglink(button=True, pin_correct=False)
|
2013-01-14 16:05:38 +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)
|
|
|
|
|
|
|
|
for attempt in range(1, 6):
|
|
|
|
start = time.time()
|
|
|
|
self.assertRaises(PinException, self._some_protected_call)
|
|
|
|
test_backoff(attempt, start)
|
|
|
|
|
|
|
|
# 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))
|
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()
|