1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 23:48:12 +00:00

fix test_msg, rename to test_wire

This commit is contained in:
Jan Pochyla 2016-06-02 14:39:54 +02:00 committed by Pavol Rusnak
parent 154184e4e2
commit 9a30a13aff
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -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