tests: streamline TrezorFailure exception usage

pull/919/head
matejcik 4 years ago
parent a40f1e00ed
commit 1b50877545

@ -22,8 +22,9 @@ class TrezorException(Exception):
class TrezorFailure(TrezorException):
def __init__(self, failure):
self.failure = failure
# TODO: this is backwards compatibility with tests. it should be changed
super().__init__(self.failure.code, self.failure.message)
self.code = failure.code
self.message = failure.message
super().__init__(self.code, self.message, self.failure)
def __str__(self):
from .messages import FailureType
@ -33,8 +34,8 @@ class TrezorFailure(TrezorException):
for name in dir(FailureType)
if not name.startswith("_")
}
if self.failure.message is not None:
return "{}: {}".format(types[self.failure.code], self.failure.message)
if self.message is not None:
return "{}: {}".format(types[self.code], self.message)
else:
return types[self.failure.code]

@ -227,7 +227,5 @@ def test_cardano_sign_tx_validation(
with client:
client.set_expected_responses(expected_responses)
with pytest.raises(TrezorFailure) as exc:
with pytest.raises(TrezorFailure, match=expected_error_message):
cardano.sign_tx(client, inputs, outputs, transactions, protocol_magic)
assert exc.value.args[1] == expected_error_message

@ -98,16 +98,16 @@ def test_dry_run(client):
@pytest.mark.setup_client(mnemonic=MNEMONIC12)
def test_seed_mismatch(client):
with pytest.raises(exceptions.TrezorFailure) as exc:
with pytest.raises(
exceptions.TrezorFailure, match="does not match the one in the device"
):
do_recover(client, ["all"] * 12)
assert "does not match the one in the device" in exc.value.failure.message
@pytest.mark.skip_t2
def test_invalid_seed_t1(client):
with pytest.raises(exceptions.TrezorFailure) as exc:
with pytest.raises(exceptions.TrezorFailure, match="Invalid seed"):
do_recover(client, ["stick"] * 12)
assert "Invalid seed" in exc.value.failure.message
@pytest.mark.skip_t1
@ -163,9 +163,8 @@ def test_invalid_seed_core(client):
@pytest.mark.setup_client(uninitialized=True)
def test_uninitialized(client):
with pytest.raises(exceptions.TrezorFailure) as exc:
with pytest.raises(exceptions.TrezorFailure, match="not initialized"):
do_recover(client, ["all"] * 12)
assert "not initialized" in exc.value.failure.message
DRY_RUN_ALLOWED_FIELDS = ("dry_run", "word_count", "enforce_wordlist", "type")
@ -200,6 +199,7 @@ def test_bad_parameters(client, field_name, field_value):
type=messages.RecoveryDeviceType.ScrambledWords,
)
setattr(msg, field_name, field_value)
with pytest.raises(exceptions.TrezorFailure) as exc:
with pytest.raises(
exceptions.TrezorFailure, match="Forbidden field set in dry-run"
):
client.call(msg)
assert "Forbidden field set in dry-run" in exc.value.failure.message

@ -16,7 +16,7 @@
import pytest
from trezorlib import messages, ripple
from trezorlib import ripple
from trezorlib.exceptions import TrezorFailure
from trezorlib.tools import parse_path
@ -106,9 +106,8 @@ class TestMsgRippleSignTx:
"Sequence": 1,
}
)
with pytest.raises(TrezorFailure) as exc:
with pytest.raises(
TrezorFailure,
match="ProcessError: Fee must be in the range of 10 to 10,000 drops",
):
ripple.sign_tx(client, parse_path("m/44'/144'/0'/0/2"), msg)
assert exc.value.args[0] == messages.FailureType.ProcessError
assert exc.value.args[1].endswith(
"Fee must be in the range of 10 to 10,000 drops"
)

@ -491,7 +491,7 @@ class TestMsgSigntx:
script_type=proto.OutputScriptType.PAYTOADDRESS,
)
with pytest.raises(TrezorFailure) as exc:
with pytest.raises(TrezorFailure, match="NotEnoughFunds"):
check_sign_tx(
client,
"Bitcoin",
@ -500,7 +500,6 @@ class TestMsgSigntx:
failure=proto.FailureType.NotEnoughFunds,
unknown_path=True,
)
assert exc.value.args[0] == proto.FailureType.NotEnoughFunds
@pytest.mark.setup_client(mnemonic=MNEMONIC12)
def test_p2sh(self, client):
@ -606,7 +605,9 @@ class TestMsgSigntx:
# Set up attack processors
client.set_filter(proto.TxAck, attack_processor)
with pytest.raises(TrezorFailure) as exc:
with pytest.raises(
TrezorFailure, match="Transaction has changed during signing"
):
btc.sign_tx(
client,
"Bitcoin",
@ -614,11 +615,6 @@ class TestMsgSigntx:
[out1, out2],
prev_txes=TxCache("Bitcoin"),
)
assert exc.value.args[0] in (
proto.FailureType.ProcessError,
proto.FailureType.DataError,
)
assert exc.value.args[1].endswith("Transaction has changed during signing")
# Ensure that if the change output is modified after the user confirms the
# transaction, then signing fails.
@ -662,17 +658,13 @@ class TestMsgSigntx:
# Set up attack processors
client.set_filter(proto.TxAck, attack_processor)
with pytest.raises(TrezorFailure) as exc:
with pytest.raises(
TrezorFailure, match="Transaction has changed during signing"
):
btc.sign_tx(
client, "Testnet", [inp1], [out1, out2], prev_txes=TxCache("Testnet")
)
assert exc.value.args[0] in (
proto.FailureType.ProcessError,
proto.FailureType.DataError,
)
assert exc.value.args[1].endswith("Transaction has changed during signing")
def test_attack_change_input_address(self, client):
inp1 = proto.TxInputType(
address_n=parse_path("44'/1'/4'/0/0"),

@ -330,15 +330,11 @@ class TestMsgSigntxBch:
]
)
with pytest.raises(TrezorFailure) as exc:
with pytest.raises(
TrezorFailure, match="Transaction has changed during signing"
):
btc.sign_tx(client, "Bcash", [inp1, inp2], [out1], prev_txes=TX_API)
assert exc.value.args[0] in (
proto.FailureType.ProcessError,
proto.FailureType.DataError,
)
assert exc.value.args[1].endswith("Transaction has changed during signing")
def test_attack_change_input(self, client):
inp1 = proto.TxInputType(
address_n=parse_path("44'/145'/10'/0/0"),

@ -49,8 +49,8 @@ class TestMsgStellarGetAddress:
stellar.get_address(client, parse_path("m/0/1"))
if client.features.model == "1":
assert exc.value.args[0] == proto.FailureType.ProcessError
assert exc.value.args[1].endswith("Failed to derive private key")
assert exc.value.code == proto.FailureType.ProcessError
assert exc.value.message.endswith("Failed to derive private key")
else:
assert exc.value.args[0] == proto.FailureType.DataError
assert exc.value.args[1].endswith("Forbidden key path")
assert exc.value.code == proto.FailureType.DataError
assert exc.value.message.endswith("Forbidden key path")

@ -298,8 +298,5 @@ class TestMultisig:
script_type=proto.OutputScriptType.PAYTOADDRESS,
)
with pytest.raises(TrezorFailure) as exc:
with pytest.raises(TrezorFailure, match="Pubkey not found in multisig script"):
btc.sign_tx(client, "Bitcoin", [inp1], [out1], prev_txes=TX_API)
assert exc.value.args[0] == proto.FailureType.DataError
assert exc.value.args[1].endswith("Pubkey not found in multisig script")

@ -173,10 +173,10 @@ class TestOpReturn:
btc.sign_tx(client, "Bitcoin", [inp1], [out1], prev_txes=TX_API)
if client.features.model == "1":
assert exc.value.args[0] == proto.FailureType.ProcessError
assert exc.value.args[1].endswith("Failed to compile output")
assert exc.value.code == proto.FailureType.ProcessError
assert exc.value.message.endswith("Failed to compile output")
else:
assert exc.value.args[0] == proto.FailureType.DataError
assert exc.value.args[1].endswith(
assert exc.value.code == proto.FailureType.DataError
assert exc.value.message.endswith(
"OP_RETURN output with non-zero amount"
)

@ -42,7 +42,7 @@ def test_sd_no_format(client):
client.set_input_flow(input_flow)
device.sd_protect(client, Op.ENABLE)
assert e.value.failure.code == messages.FailureType.ProcessError
assert e.value.code == messages.FailureType.ProcessError
@pytest.mark.sd_card
@ -109,4 +109,4 @@ def test_sd_protect_unlock(client):
client.set_input_flow(input_flow_change_pin_format)
device.change_pin(client)
assert e.value.failure.code == messages.FailureType.ProcessError
assert e.value.code == messages.FailureType.ProcessError

Loading…
Cancel
Save