From bcd68aecd904ce287ae75c5e7cd06d9928ca5431 Mon Sep 17 00:00:00 2001 From: M1nd3r Date: Tue, 19 Nov 2024 14:57:11 +0100 Subject: [PATCH] fixup: fix unit tests --- core/tests/test_apps.bitcoin.approver.py | 11 +++++- core/tests/test_apps.bitcoin.authorization.py | 14 +++++-- core/tests/test_apps.bitcoin.keychain.py | 37 +++++++++++++------ core/tests/test_apps.common.keychain.py | 17 ++++----- core/tests/test_apps.ethereum.keychain.py | 15 +++----- core/tests/test_storage.cache.py | 31 +++++++--------- 6 files changed, 71 insertions(+), 54 deletions(-) diff --git a/core/tests/test_apps.bitcoin.approver.py b/core/tests/test_apps.bitcoin.approver.py index a15345eba8..c5c10a3362 100644 --- a/core/tests/test_apps.bitcoin.approver.py +++ b/core/tests/test_apps.bitcoin.approver.py @@ -29,8 +29,15 @@ else: class TestApprover(unittest.TestCase): if utils.USE_THP: - def setUpClass(self): - context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) + def setUpClass(self): + if __debug__: + thp_common.suppres_debug_log() + thp_common.prepare_context() + + else: + + def setUpClass(self): + context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) def tearDownClass(self): context.CURRENT_CONTEXT = None diff --git a/core/tests/test_apps.bitcoin.authorization.py b/core/tests/test_apps.bitcoin.authorization.py index 5604ae7c06..4faf202989 100644 --- a/core/tests/test_apps.bitcoin.authorization.py +++ b/core/tests/test_apps.bitcoin.authorization.py @@ -18,12 +18,20 @@ else: class TestAuthorization(unittest.TestCase): - if utils.USE_THP: coin = coins.by_name("Bitcoin") - def setUpClass(self): - context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) + if utils.USE_THP: + + def setUpClass(self): + if __debug__: + thp_common.suppres_debug_log() + thp_common.prepare_context() + + else: + + def setUpClass(self): + context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) def tearDownClass(self): context.CURRENT_CONTEXT = None diff --git a/core/tests/test_apps.bitcoin.keychain.py b/core/tests/test_apps.bitcoin.keychain.py index 681e058ce2..9fe07e6220 100644 --- a/core/tests/test_apps.bitcoin.keychain.py +++ b/core/tests/test_apps.bitcoin.keychain.py @@ -15,13 +15,13 @@ else: class TestBitcoinKeychain(unittest.TestCase): + if utils.USE_THP: - def setUpClass(self): - context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) - - def tearDownClass(self): - context.CURRENT_CONTEXT = None + def setUpClass(self): + if __debug__: + thp_common.suppres_debug_log() + thp_common.prepare_context() def setUp(self): seed = bip39.seed(" ".join(["all"] * 12), "") @@ -29,15 +29,17 @@ class TestBitcoinKeychain(unittest.TestCase): else: - def __init__(self): + def setUpClass(self): context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) - super().__init__() def setUp(self): cache_codec.start_session() seed = bip39.seed(" ".join(["all"] * 12), "") cache_codec.get_active_session().set(cache_common.APP_COMMON_SEED, seed) + def tearDownClass(self): + context.CURRENT_CONTEXT = None + def test_bitcoin(self): coin = _get_coin_by_name("Bitcoin") keychain = await_result(_get_keychain_for_coin(coin)) @@ -113,19 +115,30 @@ class TestBitcoinKeychain(unittest.TestCase): @unittest.skipUnless(not utils.BITCOIN_ONLY, "altcoin") class TestAltcoinKeychains(unittest.TestCase): - if not utils.USE_THP: + if utils.USE_THP: - def setUpClass(self): - context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) + def setUpClass(self): + if __debug__: + thp_common.suppres_debug_log() + thp_common.prepare_context() - def tearDownClass(self): - context.CURRENT_CONTEXT = None + def setUp(self): + seed = bip39.seed(" ".join(["all"] * 12), "") + context.cache_set(cache_common.APP_COMMON_SEED, seed) + + else: + + def setUpClass(self): + context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) def setUp(self): cache_codec.start_session() seed = bip39.seed(" ".join(["all"] * 12), "") cache_codec.get_active_session().set(cache_common.APP_COMMON_SEED, seed) + def tearDownClass(self): + context.CURRENT_CONTEXT = None + def test_bcash(self): coin = _get_coin_by_name("Bcash") keychain = await_result(_get_keychain_for_coin(coin)) diff --git a/core/tests/test_apps.common.keychain.py b/core/tests/test_apps.common.keychain.py index a1b5e7f0fd..8c3482b7b6 100644 --- a/core/tests/test_apps.common.keychain.py +++ b/core/tests/test_apps.common.keychain.py @@ -2,7 +2,7 @@ from common import * # isort:skip from mock_storage import mock_storage from storage import cache, cache_common -from trezor import utils, wire +from trezor import wire from trezor.crypto import bip39 from trezor.enums import SafetyCheckLevel from trezor.wire import context @@ -20,27 +20,24 @@ if not utils.USE_THP: class TestKeychain(unittest.TestCase): - def setUpClass(self): - context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) + if utils.USE_THP: - def tearDownClass(self): - context.CURRENT_CONTEXT = None - - def __init__(self): + def setUpClass(self): if __debug__: thp_common.suppres_debug_log() thp_common.prepare_context() - super().__init__() else: - def __init__(self): + def setUpClass(self): context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) - super().__init__() def setUp(self): cache_codec.start_session() + def tearDownClass(self): + context.CURRENT_CONTEXT = None + def tearDown(self): cache.clear_all() diff --git a/core/tests/test_apps.ethereum.keychain.py b/core/tests/test_apps.ethereum.keychain.py index a4b5023075..09e88510ce 100644 --- a/core/tests/test_apps.ethereum.keychain.py +++ b/core/tests/test_apps.ethereum.keychain.py @@ -79,17 +79,12 @@ class TestEthereumKeychain(unittest.TestCase): addr, ) - def setUpClass(self): - context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) + if utils.USE_THP: - def tearDownClass(self): - context.CURRENT_CONTEXT = None - - def __init__(self): + def setUpClass(self): if __debug__: thp_common.suppres_debug_log() thp_common.prepare_context() - super().__init__() def setUp(self): seed = bip39.seed(" ".join(["all"] * 12), "") @@ -97,15 +92,17 @@ class TestEthereumKeychain(unittest.TestCase): else: - def __init__(self): + def setUpClass(self): context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) - super().__init__() def setUp(self): cache_codec.start_session() seed = bip39.seed(" ".join(["all"] * 12), "") cache_codec.get_active_session().set(cache_common.APP_COMMON_SEED, seed) + def tearDownClass(self): + context.CURRENT_CONTEXT = None + def from_address_n(self, address_n): slip44 = _slip44_from_address_n(address_n) network = make_network(slip44=slip44) diff --git a/core/tests/test_storage.cache.py b/core/tests/test_storage.cache.py index 8ffc05bf28..40c22619ad 100644 --- a/core/tests/test_storage.cache.py +++ b/core/tests/test_storage.cache.py @@ -29,21 +29,11 @@ else: class TestStorageCache(unittest.TestCase): - def setUpClass(self): - context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) - - def tearDownClass(self): - context.CURRENT_CONTEXT = None - - def setUp(self): - cache.clear_all() - if utils.USE_THP: - def __init__(self): - thp_common.suppres_debug_log() - # xthp_common.prepare_context() - # config.init() + def setUpClass(self): + if __debug__: + thp_common.suppres_debug_log() super().__init__() def setUp(self): @@ -316,14 +306,19 @@ class TestStorageCache(unittest.TestCase): else: - def __init__(self): - # Context is needed to test decorators and handleInitialize - # It allows access to codec cache from different parts of the code - from trezor.wire import context + def setUpClass(self): from trezor.wire.codec.codec_context import CodecContext + from trezor.wire import context context.CURRENT_CONTEXT = CodecContext(None, bytearray(64)) - super().__init__() + + def tearDownClass(self): + from trezor.wire import context + + context.CURRENT_CONTEXT = None + + def setUp(self): + cache.clear_all() def test_start_session(self): session_id_a = cache_codec.start_session()