From 9a30a13aff3743bf8b47631b06ae2fe6d003973a Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Thu, 2 Jun 2016 14:39:54 +0200 Subject: [PATCH] fix test_msg, rename to test_wire --- src/tests/{test_msg.py => test_wire.py} | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) rename src/tests/{test_msg.py => test_wire.py} (74%) diff --git a/src/tests/test_msg.py b/src/tests/test_wire.py similarity index 74% rename from src/tests/test_msg.py rename to src/tests/test_wire.py index 3b6c0b2da7..964fda475d 100644 --- a/src/tests/test_msg.py +++ b/src/tests/test_wire.py @@ -5,7 +5,7 @@ import unittest from trezor import loop from trezor import msg -from trezor.msg import read_wire_msg, write_wire_msg +from trezor.wire import read_wire_msg, write_wire_msg def chunks(l, n): @@ -13,33 +13,39 @@ def chunks(l, n): yield l[i:i + n] -class TestMsg(unittest.TestCase): +class TestWire(unittest.TestCase): def test_read_wire_msg(self): + # Reading empty message returns correct type and empty bytes + reader = read_wire_msg() reader.send(None) empty_message = b'\x3f##\xab\xcd\x00\x00\x00\x00' + b'\x00' * 55 try: - reader.send((loop.HID_READ, empty_message)) + reader.send((empty_message,)) except StopIteration as e: restype, resmsg = e.value self.assertEqual(restype, int('0xabcd', 16)) self.assertEqual(resmsg, b'') + # Reading message from one report + reader = read_wire_msg() reader.send(None) content = bytes([x for x in range(0, 55)]) message = b'\x3f##\xab\xcd\x00\x00\x00\x37' + content try: - reader.send((loop.HID_READ, message)) + reader.send((message,)) except StopIteration as e: restype, resmsg = e.value self.assertEqual(restype, int('0xabcd', 16)) self.assertEqual(resmsg, content) + # Reading message spanning multiple reports + reader = read_wire_msg() reader.send(None) @@ -48,7 +54,7 @@ class TestMsg(unittest.TestCase): reports = [b'\x3f' + ch + '\x00' * (63 - len(ch)) for ch in chunks(message, 63)] try: for report in reports: - reader.send((loop.HID_READ, report)) + reader.send((report,)) except StopIteration as e: restype, resmsg = e.value self.assertEqual(restype, int('0xabcd', 16)) @@ -56,8 +62,15 @@ class TestMsg(unittest.TestCase): def test_write_wire_msg(self): + # Writing message spanning multiple reports calls msg.send() with correct data + sent_reps = [] - msg.send = lambda rep: sent_reps.append(bytes(rep)) + + def dummy_send(iface, rep): + sent_reps.append(bytes(rep)) + return len(rep) + + msg.send = dummy_send content = bytes([x for x in range(0, 256)]) message = b'##\xab\xcd\x00\x00\x01\00' + content