2016-11-06 16:04:25 +00:00
|
|
|
from common import *
|
2016-11-06 11:38:33 +00:00
|
|
|
|
|
|
|
from trezor.crypto import random
|
2017-12-16 00:24:40 +00:00
|
|
|
from trezor.pin import pin_to_int
|
2016-11-06 11:38:33 +00:00
|
|
|
|
2016-11-06 16:04:25 +00:00
|
|
|
from trezor import config
|
|
|
|
|
2017-11-10 14:18:43 +00:00
|
|
|
PINAPP = 0x00
|
|
|
|
PINKEY = 0x00
|
|
|
|
|
|
|
|
|
|
|
|
def random_entry():
|
|
|
|
while True:
|
2017-12-14 18:01:41 +00:00
|
|
|
appid, key = 1 + random.uniform(255), random.uniform(256)
|
2017-11-10 14:18:43 +00:00
|
|
|
if appid != PINAPP or key != PINKEY:
|
|
|
|
break
|
|
|
|
return appid, key
|
|
|
|
|
|
|
|
|
2016-11-06 11:38:33 +00:00
|
|
|
class TestConfig(unittest.TestCase):
|
|
|
|
|
2017-06-20 11:24:12 +00:00
|
|
|
def test_init(self):
|
|
|
|
config.init()
|
|
|
|
config.init()
|
|
|
|
config.init()
|
|
|
|
|
2016-11-08 20:34:52 +00:00
|
|
|
def test_wipe(self):
|
2017-06-20 11:24:12 +00:00
|
|
|
config.init()
|
2016-11-08 20:34:52 +00:00
|
|
|
config.wipe()
|
2017-12-16 00:24:40 +00:00
|
|
|
self.assertEqual(config.unlock(pin_to_int(''), None), True)
|
2017-12-14 18:01:41 +00:00
|
|
|
config.set(1, 1, b'hello')
|
|
|
|
config.set(1, 2, b'world')
|
|
|
|
v0 = config.get(1, 1)
|
|
|
|
v1 = config.get(1, 2)
|
2016-11-08 20:34:52 +00:00
|
|
|
self.assertEqual(v0, b'hello')
|
|
|
|
self.assertEqual(v1, b'world')
|
|
|
|
config.wipe()
|
2017-12-14 18:01:41 +00:00
|
|
|
v0 = config.get(1, 1)
|
|
|
|
v1 = config.get(1, 2)
|
2016-11-23 14:05:53 +00:00
|
|
|
self.assertEqual(v0, bytes())
|
|
|
|
self.assertEqual(v1, bytes())
|
2016-11-08 20:34:52 +00:00
|
|
|
|
2017-11-10 14:18:43 +00:00
|
|
|
def test_lock(self):
|
|
|
|
for _ in range(128):
|
|
|
|
config.init()
|
|
|
|
config.wipe()
|
2017-12-16 00:24:40 +00:00
|
|
|
self.assertEqual(config.unlock(pin_to_int(''), None), True)
|
2017-11-10 14:18:43 +00:00
|
|
|
appid, key = random_entry()
|
|
|
|
value = random.bytes(16)
|
|
|
|
config.set(appid, key, value)
|
|
|
|
config.init()
|
|
|
|
self.assertEqual(config.get(appid, key), bytes())
|
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
config.set(appid, key, bytes())
|
|
|
|
config.init()
|
|
|
|
config.wipe()
|
2017-12-16 00:24:40 +00:00
|
|
|
self.assertEqual(config.change_pin(pin_to_int(''), pin_to_int('000'), None), False)
|
2017-11-10 14:18:43 +00:00
|
|
|
|
|
|
|
def test_change_pin(self):
|
|
|
|
config.init()
|
|
|
|
config.wipe()
|
2017-12-16 00:24:40 +00:00
|
|
|
self.assertEqual(config.unlock(pin_to_int(''), None), True)
|
2017-11-10 14:18:43 +00:00
|
|
|
with self.assertRaises(RuntimeError):
|
2017-12-16 00:24:40 +00:00
|
|
|
config.set(PINAPP, PINKEY, b'value')
|
|
|
|
self.assertEqual(config.change_pin(pin_to_int('000'), pin_to_int('666'), None), False)
|
|
|
|
self.assertEqual(config.change_pin(pin_to_int(''), pin_to_int('000'), None), True)
|
2017-11-10 14:18:43 +00:00
|
|
|
self.assertEqual(config.get(PINAPP, PINKEY), bytes())
|
|
|
|
config.set(1, 1, b'value')
|
|
|
|
config.init()
|
2017-12-16 00:24:40 +00:00
|
|
|
self.assertEqual(config.unlock(pin_to_int('000'), None), True)
|
|
|
|
config.change_pin(pin_to_int('000'), pin_to_int(''), None)
|
2017-11-10 14:18:43 +00:00
|
|
|
config.init()
|
2017-12-16 00:24:40 +00:00
|
|
|
self.assertEqual(config.unlock(pin_to_int('000'), None), False)
|
|
|
|
self.assertEqual(config.unlock(pin_to_int(''), None), True)
|
2017-11-10 14:18:43 +00:00
|
|
|
self.assertEqual(config.get(1, 1), b'value')
|
|
|
|
|
2016-11-06 11:38:33 +00:00
|
|
|
def test_set_get(self):
|
2017-06-20 11:24:12 +00:00
|
|
|
config.init()
|
2016-11-06 11:38:33 +00:00
|
|
|
config.wipe()
|
2017-12-16 00:24:40 +00:00
|
|
|
self.assertEqual(config.unlock(pin_to_int(''), None), True)
|
2017-11-10 14:18:43 +00:00
|
|
|
for _ in range(32):
|
|
|
|
appid, key = random_entry()
|
2016-11-23 13:45:58 +00:00
|
|
|
value = random.bytes(128)
|
|
|
|
config.set(appid, key, value)
|
|
|
|
value2 = config.get(appid, key)
|
|
|
|
self.assertEqual(value, value2)
|
2016-11-06 11:38:33 +00:00
|
|
|
|
2017-12-08 15:46:13 +00:00
|
|
|
def test_compact(self):
|
|
|
|
config.init()
|
|
|
|
config.wipe()
|
2017-12-16 00:24:40 +00:00
|
|
|
self.assertEqual(config.unlock(pin_to_int(''), None), True)
|
2017-12-08 15:46:13 +00:00
|
|
|
appid, key = 1, 1
|
|
|
|
for _ in range(259):
|
|
|
|
value = random.bytes(259)
|
|
|
|
config.set(appid, key, value)
|
|
|
|
value2 = config.get(appid, key)
|
|
|
|
self.assertEqual(value, value2)
|
|
|
|
|
2016-11-06 11:38:33 +00:00
|
|
|
def test_get_default(self):
|
2017-06-20 11:24:12 +00:00
|
|
|
config.init()
|
2016-11-06 11:38:33 +00:00
|
|
|
config.wipe()
|
2017-12-16 00:24:40 +00:00
|
|
|
self.assertEqual(config.unlock(pin_to_int(''), None), True)
|
2017-11-10 14:18:43 +00:00
|
|
|
for _ in range(128):
|
|
|
|
appid, key = random_entry()
|
2016-11-23 13:45:58 +00:00
|
|
|
value = config.get(appid, key)
|
|
|
|
self.assertEqual(value, bytes())
|
2016-11-06 11:38:33 +00:00
|
|
|
|
2017-12-16 01:11:26 +00:00
|
|
|
|
2016-11-06 11:38:33 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|