-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_transport.py
More file actions
83 lines (59 loc) · 2.63 KB
/
test_transport.py
File metadata and controls
83 lines (59 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# tests/test_transport.py
import os
import sys
from _base_test import BaseTest, MockTestHelper
from _mocks import MockMachine
# Add parent directory to path to import URST
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from urst.transport import URSTTransport
class TestTransport(BaseTest):
def __init__(self):
super().__init__("Transport")
self.helper = MockTestHelper()
def test_basic_transport(self):
"""Test frame types and sequence numbers"""
mock_uart = MockMachine.UART()
transport = URSTTransport(mock_uart)
# Test frame type and sequence number
test_data = b"Test payload"
frame_type = 0x01 # FRAME_DATA
bytes_sent = transport.send_typed_frame(test_data, frame_type)
self.assert_true(bytes_sent > 0, "Typed frame sent")
# Receive and verify
mock_uart.rx_buffer = mock_uart.tx_buffer.copy()
mock_uart.rx_pos = 0
result = transport.recv_typed_frame()
self.assert_true(result is not None, "Typed frame received")
payload, seq_num, recv_type = result
self.assert_equal(payload, test_data, "Payload matches")
self.assert_equal(recv_type, frame_type, "Frame type matches")
self.assert_equal(seq_num, 0, "First sequence number is 0")
# Test sequence number increment
mock_uart.tx_buffer.clear()
mock_uart.rx_buffer.clear()
mock_uart.rx_pos = 0
transport.send_typed_frame(b"Second", 0x01)
mock_uart.rx_buffer = mock_uart.tx_buffer.copy()
mock_uart.rx_pos = 0
result = transport.recv_typed_frame()
_, seq_num2, _ = result
self.assert_equal(seq_num2, 1, "Sequence number increments")
def test_short_frame_returns_none(self):
"""A frame shorter than header size should return None."""
mock_uart = MockMachine.UART()
# Build an encoded frame with only one byte of payload after decode
# Use the codec to construct a valid COBS frame containing a single byte
from urst.codec import URSTCodec
temp_uart = MockMachine.UART()
temp_codec = URSTCodec(temp_uart)
temp_codec.send_frame(b"\x01") # one byte only
# Feed that frame to the transport
mock_uart.rx_buffer = temp_uart.tx_buffer.copy()
mock_uart.rx_pos = 0
transport = URSTTransport(mock_uart)
parsed = transport.recv_typed_frame()
self.assert_none(parsed, "Short decoded payload yields None")
if __name__ == "__main__":
test_suite = TestTransport()
results = test_suite.run_all_tests()
sys.exit(0 if results["failed"] == 0 else 1)