forked from brendan-w/python-OBD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_protocol_can.py
More file actions
195 lines (145 loc) · 4.97 KB
/
test_protocol_can.py
File metadata and controls
195 lines (145 loc) · 4.97 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import random
from obd.protocols import *
CAN_11_PROTOCOLS = [
ISO_15765_4_11bit_500k,
ISO_15765_4_11bit_250k,
]
CAN_29_PROTOCOLS = [
ISO_15765_4_29bit_500k,
ISO_15765_4_29bit_250k,
SAE_J1939
]
def check_message(m, num_frames, tx_id, data):
""" generic test for correct message values """
assert len(m.frames) == num_frames
assert m.tx_id == tx_id
assert m.data == bytearray(data)
def test_single_frame():
for protocol_ in CAN_11_PROTOCOLS:
p = protocol_([])
r = p(["7E8 06 41 00 00 01 02 03"])
assert len(r) == 1
check_message(r[0], 1, 0x0, [0x41, 0x00, 0x00, 0x01, 0x02, 0x03])
# minimum valid length
r = p(["7E8 01 41"])
assert len(r) == 1
check_message(r[0], 1, 0x0, [0x41])
# maximum valid length
r = p(["7E8 07 41 00 00 01 02 03 04"])
assert len(r) == 1
check_message(r[0], 1, 0x0, [0x41, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04])
# to short
r = p(["7E8 01"])
assert len(r) == 0
# to long
r = p(["7E8 08 41 00 00 01 02 03 04 05"])
assert len(r) == 0
# drop frames with zero data
r = p(["7E8 00"])
assert len(r) == 0
# drop odd-sized frames (post padding)
r = p(["7E8 08 41 00 00 01 02 03 04 0"])
assert len(r) == 0
def test_hex_straining():
"""
If non-hex values are sent, they should be marked as ECU.UNKNOWN
"""
for protocol_ in CAN_11_PROTOCOLS:
p = protocol_([])
# single non-hex message
r = p(["12.8 Volts"])
assert len(r) == 1
assert r[0].ecu == ECU.UNKNOWN
assert len(r[0].frames) == 1
# multiple non-hex message
r = p(["12.8 Volts", "NO DATA"])
assert len(r) == 2
for m in r:
assert m.ecu == ECU.UNKNOWN
assert len(m.frames) == 1
# mixed hex and non-hex
r = p(["NO DATA", "7E8 06 41 00 00 01 02 03"])
assert len(r) == 2
# first message should be the valid, parsable hex message
# NOTE: the parser happens to process the valid one's first
check_message(r[0], 1, 0x0, [0x41, 0x00, 0x00, 0x01, 0x02, 0x03])
# second message: invalid, non-parsable non-hex
assert r[1].ecu == ECU.UNKNOWN
assert len(r[1].frames) == 1
assert len(r[1].data) == 0 # no data
def test_multi_ecu():
for protocol_ in CAN_11_PROTOCOLS:
p = protocol_([])
test_case = [
"7E8 06 41 00 00 01 02 03",
"7EB 06 41 00 00 01 02 03",
"7EA 06 41 00 00 01 02 03",
]
correct_data = [0x41, 0x00, 0x00, 0x01, 0x02, 0x03]
# separate ECUs, single frames each
r = p(test_case)
assert len(r) == 3
# messages are returned in ECU order
check_message(r[0], 1, 0x0, correct_data)
check_message(r[1], 1, 0x2, correct_data)
check_message(r[2], 1, 0x3, correct_data)
def test_multi_line():
"""
Tests that valid multiline messages are recombined into single
messages.
"""
for protocol_ in CAN_11_PROTOCOLS:
p = protocol_([])
test_case = [
"7E8 10 20 49 04 00 01 02 03",
"7E8 21 04 05 06 07 08 09 0A",
"7E8 22 0B 0C 0D 0E 0F 10 11",
"7E8 23 12 13 14 15 16 17 18"
]
correct_data = [0x49, 0x04] + list(range(25))
# in-order
r = p(test_case)
assert len(r) == 1
check_message(r[0], len(test_case), 0x0, 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), 0x0, correct_data)
def test_multi_line_missing_frames():
"""
Missing frames in a multi-frame message should drop the message.
Tests the contiguity check, and data length byte
"""
for protocol_ in CAN_11_PROTOCOLS:
p = protocol_([])
test_case = [
"7E8 10 20 49 04 00 01 02 03",
"7E8 21 04 05 06 07 08 09 0A",
"7E8 22 0B 0C 0D 0E 0F 10 11",
"7E8 23 12 13 14 15 16 17 18"
]
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
def test_multi_line_mode_03():
"""
Tests the special handling of mode 3 commands.
Namely, Mode 03 commands have a DTC count byte that is accounted for
in the protocol layer.
"""
for protocol_ in CAN_11_PROTOCOLS:
p = protocol_([])
test_case = [
"7E8 10 20 43 04 00 01 02 03",
"7E8 21 04 05 06 07 08 09 0A",
]
correct_data = [0x43, 0x04] + list(range(8))
r = p(test_case)
assert len(r) == 1
check_message(r[0], len(test_case), 0, correct_data)
def test_can_29():
pass