From 3fa99c0c6a60721e3f1aa7626c96531304f98532 Mon Sep 17 00:00:00 2001 From: matejcik Date: Thu, 30 Jan 2020 15:58:12 +0100 Subject: [PATCH] core/tests: add storage.cache tests --- core/tests/test_storage.cache.py | 99 ++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 core/tests/test_storage.cache.py diff --git a/core/tests/test_storage.cache.py b/core/tests/test_storage.cache.py new file mode 100644 index 0000000000..af6c16a3d9 --- /dev/null +++ b/core/tests/test_storage.cache.py @@ -0,0 +1,99 @@ +from common import * +from mock import patch +from mock_storage import mock_storage + +import storage +from storage import cache +from trezor.messages.Initialize import Initialize +from trezor.messages.ClearSession import ClearSession +from trezor.wire import DUMMY_CONTEXT + +from apps.homescreen import handle_Initialize, handle_ClearSession + +KEY = 99 + + +class TestStorageCache(unittest.TestCase): + def test_session_id(self): + session_id_a = cache.get_session_id() + self.assertIsNotNone(session_id_a) + session_id_b = cache.get_session_id() + self.assertEqual(session_id_a, session_id_b) + + cache.clear() + session_id_c = cache.get_session_id() + self.assertIsNotNone(session_id_c) + self.assertNotEqual(session_id_a, session_id_c) + + def test_get_set(self): + value = cache.get(KEY) + self.assertIsNone(value) + + cache.set(KEY, "hello") + value = cache.get(KEY) + self.assertEqual(value, "hello") + + cache.clear() + value = cache.get(KEY) + self.assertIsNone(value) + + @mock_storage + def test_Initialize(self): + def call_Initialize(**kwargs): + msg = Initialize(**kwargs) + return await_result(handle_Initialize(DUMMY_CONTEXT, msg)) + + # calling Initialize without an ID allocates a new one + session_id = cache.get_session_id() + features = call_Initialize() + new_session_id = cache.get_session_id() + self.assertNotEqual(session_id, new_session_id) + self.assertEqual(new_session_id, features.session_id) + + # calling Initialize with the current ID does not allocate a new one + features = call_Initialize(session_id=new_session_id) + same_session_id = cache.get_session_id() + self.assertEqual(new_session_id, same_session_id) + self.assertEqual(same_session_id, features.session_id) + + call_Initialize() + # calling Initialize with a non-current ID returns a different one + features = call_Initialize(session_id=new_session_id) + self.assertNotEqual(new_session_id, features.session_id) + + # allocating a new session ID clears the cache + cache.set(KEY, "hello") + features = call_Initialize() + self.assertIsNone(cache.get(KEY)) + + # resuming a session does not clear the cache + cache.set(KEY, "hello") + call_Initialize(session_id=features.session_id) + self.assertEqual(cache.get(KEY), "hello") + + # supplying a different session ID clears the cache + self.assertNotEqual(new_session_id, features.session_id) + call_Initialize(session_id=new_session_id) + self.assertIsNone(cache.get(KEY)) + + @mock_storage + def test_ClearSession(self): + def call_Initialize(**kwargs): + msg = Initialize(**kwargs) + return await_result(handle_Initialize(DUMMY_CONTEXT, msg)) + + def call_ClearSession(): + return await_result(handle_ClearSession(DUMMY_CONTEXT, ClearSession())) + + session_id = call_Initialize().session_id + cache.set(KEY, "hello") + self.assertEqual(cache.get(KEY), "hello") + + call_ClearSession() + self.assertIsNone(cache.get(KEY)) + new_session_id = cache.get_session_id() + self.assertNotEqual(session_id, new_session_id) + + +if __name__ == "__main__": + unittest.main()