mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-04-21 01:29:02 +00:00
fix: properly return Failure.ActionCancelled in Pairing flow, add tests
This commit is contained in:
parent
7a798e11f0
commit
8085a32343
@ -27,7 +27,7 @@ from trezor.messages import (
|
||||
)
|
||||
from trezor.wire import message_handler
|
||||
from trezor.wire.context import UnexpectedMessageException
|
||||
from trezor.wire.errors import SilentError, UnexpectedMessage
|
||||
from trezor.wire.errors import ActionCancelled, SilentError, UnexpectedMessage
|
||||
from trezor.wire.thp import ChannelState, ThpError, crypto, get_enabled_pairing_methods
|
||||
from trezor.wire.thp.pairing_context import PairingContext
|
||||
|
||||
@ -90,6 +90,9 @@ async def handle_pairing_request(
|
||||
ctx: PairingContext, message: protobuf.MessageType
|
||||
) -> ThpEndResponse:
|
||||
|
||||
if Cancel.is_type_of(message):
|
||||
raise ActionCancelled()
|
||||
|
||||
if not ThpPairingRequest.is_type_of(message):
|
||||
raise UnexpectedMessage("Unexpected message")
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
import os
|
||||
import time
|
||||
import typing as t
|
||||
from hashlib import sha256
|
||||
|
||||
@ -12,6 +13,9 @@ from trezorlib.debuglink import TrezorClientDebugLink as Client
|
||||
from trezorlib.messages import (
|
||||
ButtonAck,
|
||||
ButtonRequest,
|
||||
Cancel,
|
||||
Failure,
|
||||
FailureType,
|
||||
ThpCodeEntryChallenge,
|
||||
ThpCodeEntryCommitment,
|
||||
ThpCodeEntryCpaceHostTag,
|
||||
@ -25,6 +29,7 @@ from trezorlib.messages import (
|
||||
ThpNfcTagTrezor,
|
||||
ThpPairingMethod,
|
||||
ThpPairingPreparationsFinished,
|
||||
ThpPairingRequest,
|
||||
ThpQrCodeSecret,
|
||||
ThpQrCodeTag,
|
||||
ThpSelectMethod,
|
||||
@ -32,6 +37,7 @@ from trezorlib.messages import (
|
||||
from trezorlib.transport.thp import curve25519
|
||||
from trezorlib.transport.thp.cpace import Cpace
|
||||
|
||||
|
||||
from .connect import (
|
||||
get_encrypted_transport_protocol,
|
||||
handle_pairing_request,
|
||||
@ -145,6 +151,34 @@ def test_pairing_code_entry(client: Client) -> None:
|
||||
protocol._has_valid_channel = True
|
||||
|
||||
|
||||
def test_pairing_cancel_1(client: Client) -> None:
|
||||
protocol = prepare_protocol_for_pairing(client)
|
||||
|
||||
protocol._send_message(ThpPairingRequest(host_name="TestTrezor Cancel 1"))
|
||||
button_req = protocol._read_message(ButtonRequest)
|
||||
assert button_req.name == "thp_pairing_request"
|
||||
|
||||
protocol._send_message(ButtonAck())
|
||||
time.sleep(1)
|
||||
protocol._send_message(Cancel())
|
||||
|
||||
resp = protocol._read_message(Failure)
|
||||
assert resp.code == FailureType.ActionCancelled
|
||||
|
||||
|
||||
def test_pairing_cancel_2(client: Client) -> None:
|
||||
protocol = prepare_protocol_for_pairing(client)
|
||||
|
||||
protocol._send_message(ThpPairingRequest(host_name="TestTrezor Cancel 2"))
|
||||
button_req = protocol._read_message(ButtonRequest)
|
||||
assert button_req.name == "thp_pairing_request"
|
||||
|
||||
protocol._send_message(ButtonAck())
|
||||
client.debug.press_no()
|
||||
resp = protocol._read_message(Failure)
|
||||
assert resp.code == FailureType.ActionCancelled
|
||||
|
||||
|
||||
def test_pairing_nfc(client: Client) -> None:
|
||||
protocol = prepare_protocol_for_pairing(client)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user