2024-11-15 12:55:14 +00:00
|
|
|
# flake8: noqa: F403,F405
|
2024-01-26 11:16:55 +00:00
|
|
|
from common import * # isort:skip
|
2023-06-28 10:58:54 +00:00
|
|
|
|
2019-10-31 15:34:16 +00:00
|
|
|
from storage import device
|
2024-05-24 15:32:04 +00:00
|
|
|
from trezor import config, utils
|
2018-12-29 21:33:19 +00:00
|
|
|
|
|
|
|
class TestConfig(unittest.TestCase):
|
2024-05-24 15:32:04 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2018-12-29 21:33:19 +00:00
|
|
|
config.init()
|
|
|
|
config.wipe()
|
2024-05-24 15:32:04 +00:00
|
|
|
|
|
|
|
def test_u2f_counter(self):
|
2018-12-29 21:33:19 +00:00
|
|
|
for i in range(150):
|
2019-07-01 07:45:30 +00:00
|
|
|
self.assertEqual(device.next_u2f_counter(), i)
|
|
|
|
device.set_u2f_counter(350)
|
2018-12-29 21:33:19 +00:00
|
|
|
for i in range(351, 500):
|
2019-07-01 07:45:30 +00:00
|
|
|
self.assertEqual(device.next_u2f_counter(), i)
|
|
|
|
device.set_u2f_counter(0)
|
|
|
|
self.assertEqual(device.next_u2f_counter(), 1)
|
2018-12-29 21:33:19 +00:00
|
|
|
|
2024-05-24 15:32:04 +00:00
|
|
|
if utils.USE_THP:
|
|
|
|
|
|
|
|
def test_cred_auth_key_counter(self):
|
|
|
|
rounds = 200
|
|
|
|
for i in range(rounds):
|
|
|
|
self.assertEqual(
|
|
|
|
device.get_cred_auth_key_counter(), i.to_bytes(4, "big")
|
|
|
|
)
|
|
|
|
device.increment_cred_auth_key_counter()
|
|
|
|
|
|
|
|
# Test get_cred_auth_key_counter does not change the counter value
|
|
|
|
self.assertEqual(
|
|
|
|
device.get_cred_auth_key_counter(), rounds.to_bytes(4, "big")
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
device.get_cred_auth_key_counter(), rounds.to_bytes(4, "big")
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_cred_auth_key_counter_overflow(self):
|
|
|
|
from storage import common
|
|
|
|
from storage.device import _NAMESPACE, _CRED_AUTH_KEY_COUNTER
|
|
|
|
|
|
|
|
common.set(_NAMESPACE, _CRED_AUTH_KEY_COUNTER, b"\xff\xff\xff\xfe")
|
|
|
|
device.increment_cred_auth_key_counter()
|
|
|
|
self.assertEqual(device.get_cred_auth_key_counter(), b"\xff\xff\xff\xff")
|
|
|
|
with self.assertRaises(AssertionError) as e:
|
|
|
|
device.increment_cred_auth_key_counter()
|
|
|
|
self.assertEqual(e.value.value, "Overflow of cred_auth_key_counter")
|
|
|
|
|
|
|
|
def test_device_secret(self):
|
|
|
|
secret1 = device.get_device_secret()
|
|
|
|
self.assertEqual(len(secret1), 16)
|
|
|
|
secret2 = device.get_device_secret()
|
|
|
|
self.assertEqual(secret1, secret2)
|
|
|
|
config.wipe()
|
|
|
|
secret3 = device.get_device_secret()
|
|
|
|
self.assertEqual(len(secret3), 16)
|
|
|
|
self.assertNotEqual(secret1, secret3)
|
|
|
|
|
2018-12-29 21:33:19 +00:00
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
if __name__ == "__main__":
|
2018-12-29 21:33:19 +00:00
|
|
|
unittest.main()
|