Repository navigation
Expand file tree
/
Copy pathtest_protocol_legacy.py
More file actions
147 lines (102 loc) · 3.01 KB
/
Copy pathtest_protocol_legacy.py
File metadata and controls
147 lines (102 loc) · 3.01 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import random
from obd.protocols import *
from obd.protocols.protocol import Message
from obd import debug
debug.console = True
LEGACY_PROTOCOLS = [
SAE_J1850_PWM,
SAE_J1850_VPW,
ISO_9141_2,
ISO_14230_4_5baud,
ISO_14230_4_fast
]
def check_message(m, num_frames, tx_id, data_bytes):
""" generic test for correct message values """
assert len(m.frames) == num_frames
assert m.tx_id == tx_id
assert m.data_bytes == data_bytes
def test_single_frame():
for protocol in LEGACY_PROTOCOLS:
p = protocol()
# minimum valid length
r = p(["48 6B 10 41 00 FF"])
assert len(r) == 1
check_message(r[0], 1, 0x10, [])
# maximum valid length
r = p(["48 6B 10 41 00 00 01 02 03 04 FF"])
assert len(r) == 1
check_message(r[0], 1, 0x10, list(range(5)))
# to short
r = p(["48 6B 10 41 FF"])
assert len(r) == 0
# to long
r = p(["48 6B 10 41 00 00 01 02 03 04 05 FF"])
assert len(r) == 0
def test_hex_straining():
for protocol in LEGACY_PROTOCOLS:
p = protocol()
r = p(["NO DATA"])
assert len(r) == 0
r = p(["TOTALLY NOT HEX"])
assert len(r) == 0
r = p(["NO DATA", "NO DATA"])
assert len(r) == 0
r = p(["NO DATA", "48 6B 10 41 00 00 01 02 03 FF"])
assert len(r) == 1
check_message(r[0], 1, 0x10, list(range(4)))
def test_multi_ecu():
for protocol in LEGACY_PROTOCOLS:
p = protocol()
test_case = [
"48 6B 13 41 00 00 01 02 03 FF",
"48 6B 10 41 00 00 01 02 03 FF",
"48 6B 11 41 00 00 01 02 03 FF",
]
correct_data = list(range(4))
# seperate ECUs, single frames each
r = p(test_case)
assert len(r) == len(test_case)
# messages are returned in ECU order
check_message(r[0], 1, 0x10, correct_data)
check_message(r[1], 1, 0x11, correct_data)
check_message(r[2], 1, 0x13, correct_data)
def test_multi_line():
for protocol in LEGACY_PROTOCOLS:
p = protocol()
test_case = [
"48 6B 10 49 02 01 00 01 02 03 FF",
"48 6B 10 49 02 02 04 05 06 07 FF",
"48 6B 10 49 02 03 08 09 0A 0B FF",
]
correct_data = list(range(12))
# in-order
r = p(test_case)
assert len(r) == 1
check_message(r[0], len(test_case), 0x10, correct_data)
# test a few out-of-order cases
for n in range(4):
random.shuffle(test_case) # mix up the frame strings
r = p(test_case)
assert len(r) == 1
check_message(r[0], len(test_case), 0x10, correct_data)
# missing frames in a multi-frame message should drop the message
# (tests the contiguity check, and data length byte)
test_case = [
"48 6B 10 49 02 01 00 01 02 03 FF",
"48 6B 10 49 02 02 04 05 06 07 FF",
"48 6B 10 49 02 03 08 09 0A 0B FF",
]
for n in range(len(test_case) - 1):
sub_test = list(test_case)
del sub_test[n]
r = p(sub_test)
assert len(r) == 0
# MODE 03 COMMANDS (GET_DTC) RETURN NO PID BYTE
test_case = [
"48 6B 10 43 00 01 02 03 04 05 FF",
"48 6B 10 43 06 07 08 09 0A 0B FF",
]
correct_data = list(range(12)) # data is stitched in order recieved
r = p(test_case)
assert len(r) == 1
check_message(r[0], len(test_case), 0x10, correct_data)