diff --git a/python/src/trezorlib/debuglink.py b/python/src/trezorlib/debuglink.py index 052fa3f2cf..889475dc6e 100644 --- a/python/src/trezorlib/debuglink.py +++ b/python/src/trezorlib/debuglink.py @@ -407,6 +407,9 @@ class TrezorClientDebugLink(TrezorClient): Useful for test scenarios with an active malicious actor on the wire. """ + if not self.in_with_statement: + raise RuntimeError("Must be called inside 'with' statement") + self.filters[message_type] = callback def _filter_message(self, msg): @@ -477,6 +480,7 @@ class TrezorClientDebugLink(TrezorClient): self.in_with_statement -= 1 self.ui.clear() + self.filters.clear() self.watch_layout(False) if _type is not None: diff --git a/tests/device_tests/test_msg_signtx.py b/tests/device_tests/test_msg_signtx.py index ddd709b4cf..b073495cab 100644 --- a/tests/device_tests/test_msg_signtx.py +++ b/tests/device_tests/test_msg_signtx.py @@ -826,12 +826,12 @@ class TestMsgSigntx: return msg - # Set up attack processors - client.set_filter(messages.TxAck, attack_processor) - - with pytest.raises( + with client, pytest.raises( TrezorFailure, match="Transaction has changed during signing" ): + # Set up attack processors + client.set_filter(messages.TxAck, attack_processor) + btc.sign_tx( client, "Bitcoin", @@ -879,12 +879,12 @@ class TestMsgSigntx: return msg - # Set up attack processors - client.set_filter(messages.TxAck, attack_processor) - - with pytest.raises( + with client, pytest.raises( TrezorFailure, match="Transaction has changed during signing" ): + # Set up attack processors + client.set_filter(messages.TxAck, attack_processor) + btc.sign_tx( client, "Testnet", [inp1], [out1, out2], prev_txes=TX_CACHE_TESTNET ) @@ -933,9 +933,9 @@ class TestMsgSigntx: return msg - client.set_filter(messages.TxAck, attack_processor) # Now run the attack, must trigger the exception with client: + client.set_filter(messages.TxAck, attack_processor) client.set_expected_responses( [ request_input(0), diff --git a/tests/device_tests/test_msg_signtx_bcash.py b/tests/device_tests/test_msg_signtx_bcash.py index 6444773631..53f9edc9f6 100644 --- a/tests/device_tests/test_msg_signtx_bcash.py +++ b/tests/device_tests/test_msg_signtx_bcash.py @@ -230,9 +230,8 @@ class TestMsgSigntxBch: return msg - client.set_filter(proto.TxAck, attack_processor) - with client: + client.set_filter(proto.TxAck, attack_processor) client.set_expected_responses( [ request_input(0), diff --git a/tests/device_tests/test_msg_signtx_bgold.py b/tests/device_tests/test_msg_signtx_bgold.py index 727d5196cd..47340b0cbf 100644 --- a/tests/device_tests/test_msg_signtx_bgold.py +++ b/tests/device_tests/test_msg_signtx_bgold.py @@ -172,8 +172,8 @@ class TestMsgSigntxBitcoinGold: return msg - client.set_filter(proto.TxAck, attack_processor) with client: + client.set_filter(proto.TxAck, attack_processor) client.set_expected_responses( [ request_input(0), diff --git a/tests/device_tests/test_msg_signtx_segwit.py b/tests/device_tests/test_msg_signtx_segwit.py index f990d8d6d4..e434f0d633 100644 --- a/tests/device_tests/test_msg_signtx_segwit.py +++ b/tests/device_tests/test_msg_signtx_segwit.py @@ -319,8 +319,8 @@ class TestMsgSigntxSegwit: return msg # Now run the attack, must trigger the exception - client.set_filter(proto.TxAck, attack_processor) with client: + client.set_filter(proto.TxAck, attack_processor) client.set_expected_responses( [ request_input(0), diff --git a/tests/device_tests/test_multisig.py b/tests/device_tests/test_multisig.py index 4be213bfcc..d194bcefbf 100644 --- a/tests/device_tests/test_multisig.py +++ b/tests/device_tests/test_multisig.py @@ -294,8 +294,8 @@ class TestMultisig: attack_count -= 1 return msg - client.set_filter(proto.TxAck, attack_processor) with client: + client.set_filter(proto.TxAck, attack_processor) client.set_expected_responses( [ request_input(0),