1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-22 12:32:02 +00:00

test(core): fix THP tests failing on non-THP builds

[no changelog]
This commit is contained in:
M1nd3r 2024-07-30 18:01:45 +02:00
parent 65a295425b
commit 5f887f4457
6 changed files with 147 additions and 132 deletions

View File

@ -1,4 +1,4 @@
from common import *
from common import * # isort:skip
from trezor import utils
if utils.USE_THP:

View File

@ -1,11 +1,11 @@
from common import *
from trezor import config, utils
from trezor import log
from common import * # isort:skip
from trezor import config, log, utils
if utils.USE_THP:
from apps.thp import credential_manager
from trezor.messages import ThpCredentialMetadata
from apps.thp import credential_manager
def _issue_credential(host_name: str, host_static_pubkey: bytes) -> bytes:
metadata = ThpCredentialMetadata(host_name=host_name)
return credential_manager.issue_credential(host_static_pubkey, metadata)

View File

@ -1,12 +1,12 @@
from common import *
from common import * # isort:skip
from trezorcrypto import aesgcm, curve25519
import storage
from trezor import utils
from trezor.wire.thp.crypto import IV_1, IV_2, Handshake
from trezorcrypto import aesgcm, curve25519
if utils.USE_THP:
from trezor.wire.thp import crypto
from trezor.wire.thp.crypto import IV_1, IV_2, Handshake
def get_dummy_device_secret():
return b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
@ -14,8 +14,9 @@ def get_dummy_device_secret():
@unittest.skipUnless(utils.USE_THP, "only needed for THP")
class TestTrezorHostProtocolCrypto(unittest.TestCase):
key_1 = b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07"
if utils.USE_THP:
handshake = Handshake()
key_1 = b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07"
# 0:key, 1:nonce, 2:auth_data, 3:plaintext, 4:expected_ciphertext, 5:expected_tag
vectors_enc = [
(

View File

@ -1,6 +1,13 @@
from common import *
from common import * # isort:skip
from storage import cache_thp
from trezor import config, io, log, protobuf, utils
from trezor.crypto.curve import curve25519
from trezor.enums import MessageType
from trezor.loop import wait
from trezor.wire.errors import UnexpectedMessage
from trezor.wire.protocol_common import Message
from apps.thp import pairing
if utils.USE_THP:
from storage.cache_common import (
CHANNEL_HANDSHAKE_HASH,
CHANNEL_KEY_RECEIVE,
@ -8,11 +15,8 @@ from storage.cache_common import (
CHANNEL_NONCE_RECEIVE,
CHANNEL_NONCE_SEND,
)
from trezor.enums import ThpPairingMethod, MessageType
from trezor.wire.errors import UnexpectedMessage
from trezor.wire.protocol_common import Message
from trezor.wire.thp.crypto import Handshake
from trezor.wire.thp.pairing_context import PairingContext
from trezor.crypto import elligator2
from trezor.enums import ThpPairingMethod
from trezor.messages import (
ThpCodeEntryChallenge,
ThpCodeEntryCpaceHost,
@ -21,15 +25,12 @@ from trezor.messages import (
ThpEndRequest,
ThpStartPairingRequest,
)
from trezor import io, config, log, protobuf
from trezor.loop import wait
from trezor.wire import thp_main
from trezor.wire.thp import interface_manager
from storage import cache_thp
from trezor.wire.thp import ChannelState
from trezor.crypto import elligator2
from trezor.crypto.curve import curve25519
from trezor.wire.thp import ChannelState, interface_manager
from trezor.wire.thp.crypto import Handshake
from trezor.wire.thp.pairing_context import PairingContext
from apps.thp import pairing
# Disable log.debug for the test
log.debug = lambda name, msg, *args: None
@ -61,6 +62,7 @@ def get_dummy_key() -> bytes:
return b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x20\x01\x02\x03\x04\x05\x06\x07\x08\x09\x30\x31"
@unittest.skipUnless(utils.USE_THP, "only needed for THP")
class TestTrezorHostProtocol(unittest.TestCase):
def setUp(self):
self.interface = MockHID(0xDEADBEEF)

View File

@ -1,16 +1,9 @@
from common import *
from common import * # isort:skip
from trezor import utils
if utils.USE_THP:
from trezor.wire.thp import writer
from trezor.wire.thp.thp_messages import PacketHeader, ENCRYPTED_TRANSPORT
if __debug__:
# Disable log.debug for the test
from trezor import log
log.debug = lambda name, msg, *args: None
from trezor.wire.thp.thp_messages import ENCRYPTED_TRANSPORT, PacketHeader
class MockHID:
def __init__(self, num):
@ -28,6 +21,12 @@ class MockHID:
return wait(mode | self.num)
if __debug__:
# Disable log.debug for the test
from trezor import log
log.debug = lambda name, msg, *args: None
@unittest.skipUnless(utils.USE_THP, "only needed for THP")
class TestTrezorHostProtocolWriter(unittest.TestCase):
short_payload_expected = b"04123400050700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
@ -86,6 +85,15 @@ class TestTrezorHostProtocolWriter(unittest.TestCase):
def setUp(self):
self.interface = MockHID(0xDEADBEEF)
def test_write_empty_packet(self):
gen = writer.write_packet_to_wire(self.interface, b"")
with self.assertRaises(StopIteration):
gen.send(None)
gen.send(None)
print(self.interface.data[0])
self.assertEqual(len(self.interface.data), 1)
self.assertEqual(self.interface.data[0], b"")
def test_write_empty_payload(self):
header = PacketHeader(ENCRYPTED_TRANSPORT, 4660, 4)
gen = writer.write_payloads_to_wire(self.interface, header, (b"",))

View File

@ -1,20 +1,22 @@
from common import *
from typing import TYPE_CHECKING
from storage.cache_thp import BROADCAST_CHANNEL_ID
import trezor.wire.thp
from trezor.wire.thp import alternating_bit_protocol as ABP
from trezor.wire.thp.writer import PACKET_LENGTH
from ubinascii import hexlify
from common import * # isort:skip
import ustruct
from typing import TYPE_CHECKING
from ubinascii import hexlify
import trezor.wire.thp
from storage.cache_thp import BROADCAST_CHANNEL_ID
from trezor import io, log, utils
from trezor.loop import wait
from trezor.utils import chunks
from trezor.wire import thp_main
from trezor.wire.protocol_common import Message
if utils.USE_THP:
from trezor.wire import thp_main
from trezor.wire.thp import alternating_bit_protocol as ABP
from trezor.wire.thp import checksum
from trezor.wire.thp.checksum import CHECKSUM_LENGTH
from trezor.wire.thp.writer import PACKET_LENGTH
if __debug__:
# Disable log.debug for the test
log.debug = lambda name, msg, *args: None
@ -47,6 +49,7 @@ CONT = 0x80
HEADER_INIT_LENGTH = 5
HEADER_CONT_LENGTH = 3
if utils.USE_THP:
INIT_MESSAGE_DATA_LENGTH = PACKET_LENGTH - HEADER_INIT_LENGTH - _MESSAGE_TYPE_LEN
@ -89,6 +92,7 @@ async def deprecated_write_message(
# This test suite is an adaptation of test_trezor.wire.codec_v1
@unittest.skipUnless(utils.USE_THP, "only needed for THP")
class TestWireTrezorHostProtocolV1(unittest.TestCase):
def setUp(self):
self.interface = MockHID(0xDEADBEEF)