From 013e0f6d668abc91799f2ed1e17034661ab42fb3 Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 22 May 2018 19:13:59 +0200 Subject: [PATCH] device_tests: get nicer tracebacks by using pytest.raises instead of custom exception code --- .../tests/device_tests/test_msg_signtx.py | 30 ++++++++----------- .../device_tests/test_msg_signtx_bcash.py | 9 ++---- .../device_tests/test_msg_signtx_segwit.py | 13 ++++---- .../test_msg_stellar_verify_message.py | 26 ++++++---------- trezorlib/tests/device_tests/test_multisig.py | 15 +++++----- .../tests/device_tests/test_op_return.py | 14 ++++----- 6 files changed, 44 insertions(+), 63 deletions(-) diff --git a/trezorlib/tests/device_tests/test_msg_signtx.py b/trezorlib/tests/device_tests/test_msg_signtx.py index 7b92325aa..25181fe43 100644 --- a/trezorlib/tests/device_tests/test_msg_signtx.py +++ b/trezorlib/tests/device_tests/test_msg_signtx.py @@ -16,7 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with this library. If not, see . -from .common import * +from binascii import hexlify, unhexlify +import pytest + +from .common import TrezorTest from trezorlib import coins from trezorlib import messages as proto @@ -514,12 +517,9 @@ class TestMsgSigntx(TrezorTest): proto.Failure(code=proto.FailureType.NotEnoughFunds) ]) - try: + with pytest.raises(CallException) as exc: self.client.sign_tx('Bitcoin', [inp1, ], [out1, ]) - except CallException as e: - assert e.args[0] == proto.FailureType.NotEnoughFunds - else: - assert False # types.FailureType.NotEnoughFunds expected + assert exc.value.args[0] == proto.FailureType.NotEnoughFunds def test_p2sh(self): self.setup_mnemonic_nopin_nopassphrase() @@ -623,13 +623,10 @@ class TestMsgSigntx(TrezorTest): assert hexlify(serialized_tx) == b'01000000021c032e5715d1da8115a2fe4f57699e15742fe113b0d2d1ca3b594649d322bec6010000006b483045022100f773c403b2f85a5c1d6c9c4ad69c43de66930fff4b1bc818eb257af98305546a0220443bde4be439f276a6ce793664b463580e210ec6c9255d68354449ac0443c76501210338d78612e990f2eea0c426b5e48a8db70b9d7ed66282b3b26511e0b1c75515a6ffffffff6ea42cd8d9c8e5441c4c5f85bfe50311078730d2881494f11f4d2257777a4958010000006b48304502210090cff1c1911e771605358a8cddd5ae94c7b60cc96e50275908d9bf9d6367c79f02202bfa72e10260a146abd59d0526e1335bacfbb2b4401780e9e3a7441b0480c8da0121038caebd6f753bbbd2bb1f3346a43cd32140648583673a31d62f2dfb56ad0ab9e3ffffffff02a0860100000000001976a9142f4490d5263906e4887ca2996b9e207af3e7824088aca0860100000000001976a914812c13d97f9159e54e326b481b8f88a73df8507a88ac00000000' # Now run the attack, must trigger the exception - try: + with pytest.raises(CallException) as exc: self.client.sign_tx('Bitcoin', [inp1, inp2], [out1, out2], debug_processor=attack_processor) - except CallException as exc: - assert exc.args[0] == proto.FailureType.ProcessError - assert exc.args[1] == 'Transaction has changed during signing' - else: - assert False # exception expected + assert exc.value.args[0] == proto.FailureType.ProcessError + assert exc.value.args[1] == 'Transaction has changed during signing' def test_attack_change_input_address(self): # This unit test attempts to modify input address after the Trezor checked @@ -701,13 +698,10 @@ class TestMsgSigntx(TrezorTest): proto.Failure(code=proto.FailureType.ProcessError), ]) # Now run the attack, must trigger the exception - try: + with pytest.raises(CallException) as exc: self.client.sign_tx('Testnet', [inp1], [out1, out2], debug_processor=attack_processor) - except CallException as exc: - assert exc.args[0] == proto.FailureType.ProcessError - assert exc.args[1] == 'Transaction has changed during signing' - else: - assert False # exception expected + assert exc.value.args[0] == proto.FailureType.ProcessError + assert exc.value.args[1] == 'Transaction has changed during signing' def test_spend_coinbase(self): # 25 TEST generated to m/1 (mfiGQVPcRcaEvQPYDErR34DcCovtxYvUUV) diff --git a/trezorlib/tests/device_tests/test_msg_signtx_bcash.py b/trezorlib/tests/device_tests/test_msg_signtx_bcash.py index f397d5b6a..f54cedb45 100644 --- a/trezorlib/tests/device_tests/test_msg_signtx_bcash.py +++ b/trezorlib/tests/device_tests/test_msg_signtx_bcash.py @@ -222,13 +222,10 @@ class TestMsgSigntxBch(TrezorTest): proto.Failure(code=proto.FailureType.ProcessError), ]) - try: + with pytest.raises(CallException) as exc: self.client.sign_tx('Bcash', [inp1, inp2], [out1], debug_processor=attack_processor) - except CallException as exc: - assert exc.args[0] == proto.FailureType.ProcessError - assert exc.args[1] == 'Transaction has changed during signing' - else: - assert False # exception expected + assert exc.value.args[0] == proto.FailureType.ProcessError + assert exc.value.args[1] == 'Transaction has changed during signing' def test_attack_change_input(self): self.setup_mnemonic_allallall() diff --git a/trezorlib/tests/device_tests/test_msg_signtx_segwit.py b/trezorlib/tests/device_tests/test_msg_signtx_segwit.py index 65136c3a3..edfffe019 100644 --- a/trezorlib/tests/device_tests/test_msg_signtx_segwit.py +++ b/trezorlib/tests/device_tests/test_msg_signtx_segwit.py @@ -15,8 +15,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with this library. If not, see . -from .common import * +from binascii import hexlify, unhexlify +import pytest +from .common import TrezorTest from trezorlib import coins from trezorlib import messages as proto from trezorlib.ckd_public import deserialize @@ -235,10 +237,7 @@ class TestMsgSigntxSegwit(TrezorTest): proto.TxRequest(request_type=proto.RequestType.TXINPUT, details=proto.TxRequestDetailsType(request_index=0)), proto.Failure(code=proto.FailureType.ProcessError), ]) - try: + with pytest.raises(CallException) as exc: self.client.sign_tx('Testnet', [inp1], [out1, out2], debug_processor=attack_processor) - except CallException as exc: - assert exc.args[0] == proto.FailureType.ProcessError - assert exc.args[1] == 'Transaction has changed during signing' - else: - assert False # exception expected + assert exc.value.args[0] == proto.FailureType.ProcessError + assert exc.value.args[1] == 'Transaction has changed during signing' diff --git a/trezorlib/tests/device_tests/test_msg_stellar_verify_message.py b/trezorlib/tests/device_tests/test_msg_stellar_verify_message.py index 30795c56b..f7ea1aa8c 100644 --- a/trezorlib/tests/device_tests/test_msg_stellar_verify_message.py +++ b/trezorlib/tests/device_tests/test_msg_stellar_verify_message.py @@ -13,12 +13,13 @@ # You should have received a copy of the GNU Lesser General Public License # along with this library. If not, see . +from binascii import unhexlify +import pytest + from .common import TrezorTest from .conftest import TREZOR_VERSION -from binascii import unhexlify, hexlify from trezorlib import messages as proto from trezorlib.client import CallException -import pytest @pytest.mark.stellar @@ -44,28 +45,19 @@ class TestMsgStellarVerifyMessage(TrezorTest): msg = 'abc' pubkey = unhexlify('15d648bfe4d36f196cfb5735ffd8ca54cd4b8233f743f22449de7cf301cdb469') signature = unhexlify('0000000000000000') # invalid length - try: + with pytest.raises(CallException) as exc: self.client.stellar_verify_message(pubkey, signature, msg) - except CallException as exc: - assert exc.args[0] == proto.FailureType.DataError - else: - assert False + assert exc.value.args[0] == proto.FailureType.DataError # invalid signature signature = unhexlify('00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000') - try: + with pytest.raises(CallException) as exc: self.client.stellar_verify_message(pubkey, signature, msg) - except CallException as exc: - assert exc.args[0] == proto.FailureType.DataError - else: - assert False + assert exc.value.args[0] == proto.FailureType.DataError # invalid pubkey pubkey = unhexlify('00') signature = unhexlify('00') - try: + with pytest.raises(CallException) as exc: self.client.stellar_verify_message(pubkey, signature, msg) - except CallException as exc: - assert exc.args[0] == proto.FailureType.DataError - else: - assert False + assert exc.value.args[0] == proto.FailureType.DataError diff --git a/trezorlib/tests/device_tests/test_multisig.py b/trezorlib/tests/device_tests/test_multisig.py index 4316a9a71..15d263760 100644 --- a/trezorlib/tests/device_tests/test_multisig.py +++ b/trezorlib/tests/device_tests/test_multisig.py @@ -16,9 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with this library. If not, see . -from __future__ import print_function +from binascii import hexlify, unhexlify +import pytest -from .common import * +from .common import TrezorTest from trezorlib import messages as proto import trezorlib.ckd_public as bip32 from trezorlib.client import CallException @@ -249,10 +250,8 @@ class TestMultisig(TrezorTest): script_type=proto.OutputScriptType.PAYTOADDRESS ) - try: + with pytest.raises(CallException) as exc: self.client.sign_tx('Bitcoin', [inp1, ], [out1, ]) - except CallException as exc: - assert exc.args[0] == proto.FailureType.DataError - assert exc.args[1] == 'Pubkey not found in multisig script' - else: - assert False # exception expected + + assert exc.value.args[0] == proto.FailureType.DataError + assert exc.value.args[1] == 'Pubkey not found in multisig script' diff --git a/trezorlib/tests/device_tests/test_op_return.py b/trezorlib/tests/device_tests/test_op_return.py index e4a38bdc9..1168a128c 100644 --- a/trezorlib/tests/device_tests/test_op_return.py +++ b/trezorlib/tests/device_tests/test_op_return.py @@ -15,8 +15,10 @@ # # You should have received a copy of the GNU Lesser General Public License # along with this library. If not, see . +from binascii import unhexlify, hexlify +import pytest -from .common import * +from .common import TrezorTest from trezorlib import messages as proto from trezorlib.client import CallException @@ -105,10 +107,8 @@ class TestOpReturn(TrezorTest): proto.Failure() ]) - try: + with pytest.raises(CallException) as exc: self.client.sign_tx('Bitcoin', [inp1], [out1]) - except CallException as exc: - assert exc.args[0] == proto.FailureType.DataError - assert exc.args[1] == 'OP_RETURN output with non-zero amount' - else: - assert False # exception expected + + assert exc.value.args[0] == proto.FailureType.DataError + assert exc.value.args[1] == 'OP_RETURN output with non-zero amount'