• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1% Regression tests for isotp_scan
2~ scanner
3
4+ Configuration
5~ conf
6
7= Imports
8from scapy.contrib.isotp.isotp_scanner import send_multiple_ext, filter_periodic_packets, scan_extended, scan
9from test.testsocket import TestSocket
10
11with open(scapy_path("test/contrib/automotive/interface_mockup.py")) as f:
12    exec(f.read())
13
14= Test send_multiple_ext()
15
16pkt = ISOTPHeaderEA(identifier=0x100, extended_address=1)/ISOTP_FF(message_size=100, data=b'\x00\x00\x00\x00\x00')
17number_of_packets = 100
18
19with new_can_socket0() as sock1, new_can_socket0() as sock:
20    send_multiple_ext(sock1, 0, pkt, number_of_packets)
21    pkts = sock.sniff(timeout=4, count=number_of_packets)
22
23assert len(pkts) == number_of_packets
24
25= Test filter_periodic_packets() with periodic packets
26pkt = CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
27received_packets = dict()
28for i in range(40):
29    temp_pkt = pkt.copy()
30    temp_pkt.time = i / 1000
31    received_packets[i] = (temp_pkt, temp_pkt.identifier)
32
33filter_periodic_packets(received_packets)
34assert len(received_packets) == 0
35
36
37= Test filter_periodic_packets() with periodic packets and one outlier
38outlier = CAN(identifier=300, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
39outlier.time = 50 / 1000
40
41pkt = CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
42received_packets = dict()
43for i in range(40):
44    temp_pkt = pkt.copy()
45    temp_pkt.time = i / 1000
46    received_packets[i] = (temp_pkt, temp_pkt.identifier)
47
48received_packets[40] = (outlier, outlier.identifier)
49
50filter_periodic_packets(received_packets)
51assert len(received_packets) == 1
52
53= Test filter_periodic_packets() with nonperiodic packets
54pkt = CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
55received_packets = dict()
56for i in range(40):
57    temp_pkt = pkt.copy()
58    temp_pkt.time = (i * i) / 1000
59    received_packets[i] = (temp_pkt, temp_pkt.identifier)
60
61filter_periodic_packets(received_packets)
62assert len(received_packets) == 40
63
64= define helper function
65
66def make_noise(p, t):
67    for _ in range(20):
68        sock_noise.send(p)
69        time.sleep(t)
70
71= test scan
72
73sock_sender = TestSocket(CAN)
74
75sockets = list()
76for idx in range(1, 4):
77    sock_recv = TestSocket(CAN)
78    sock_sender.pair(sock_recv)
79    sockets.append(ISOTPSoftSocket(sock_recv, tx_id=0x700 + idx, rx_id=0x600 + idx))
80
81found_packets = scan(sock_sender, range(0x5ff, 0x604),
82                     noise_ids=[0x701], sniff_time=0.1)
83
84for s in sockets:
85    s.close()
86
87assert len(found_packets) == 2
88assert found_packets[0x602][0].identifier == 0x702
89assert found_packets[0x603][0].identifier == 0x703
90
91= test scan extended
92
93sock_sender = TestSocket(CAN)
94sock_recv = TestSocket(CAN)
95sock_sender.pair(sock_recv)
96
97with ISOTPSoftSocket(sock_recv, tx_id=0x700, rx_id=0x601, ext_address=0xaa, rx_ext_address=0xbb):
98    found_packets = scan_extended(sock_sender, [0x600, 0x601],
99                                  extended_scan_range=range(0xb0, 0xc0),
100                                  sniff_time=0.1)
101
102fpkt = found_packets[list(found_packets.keys())[0]][0]
103rpkt = CAN(flags=0, identifier=0x700, length=4, data=b'\xaa0\x00\x00')
104assert fpkt.length == rpkt.length
105assert fpkt.data == rpkt.data
106assert fpkt.identifier == rpkt.identifier
107
108= scan with text output
109
110sock_sender = TestSocket(CAN)
111sock_recv1 = TestSocket(CAN)
112sock_sender.pair(sock_recv1)
113sock_recv2 = TestSocket(CAN)
114sock_sender.pair(sock_recv2)
115sock_noise = TestSocket(CAN)
116sock_sender.pair(sock_noise)
117
118with ISOTPSoftSocket(sock_recv1, tx_id=0x702, rx_id=0x602), ISOTPSoftSocket(sock_recv2, tx_id=0x703, rx_id=0x603):
119    pkt = CAN(identifier=0x701, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
120    make_noise(pkt, 0.01)
121    result = isotp_scan(sock_sender, range(0x5ff, 0x604 + 1),
122                        output_format="text",
123                        noise_listen_time=0.1,
124                        sniff_time=0.02,
125                        verbose=False)
126
127text = "\nFound 2 ISOTP-FlowControl Packet(s):"
128assert text in result
129assert "0x602" in result
130assert "0x603" in result
131assert "0x702" in result
132assert "0x703" in result
133assert "No Padding" in result
134
135= scan with text output padding
136
137sock_sender = TestSocket(CAN)
138sock_recv1 = TestSocket(CAN)
139sock_sender.pair(sock_recv1)
140sock_recv2 = TestSocket(CAN)
141sock_sender.pair(sock_recv2)
142sock_noise = TestSocket(CAN)
143sock_sender.pair(sock_noise)
144
145with ISOTPSoftSocket(sock_recv1, tx_id=0x702, rx_id=0x602, padding=True), ISOTPSoftSocket(sock_recv2, tx_id=0x703, rx_id=0x603, padding=True):
146    pkt = CAN(identifier=0x701, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
147    make_noise(pkt, 0.01)
148    result = isotp_scan(sock_sender, range(0x5ff, 0x604 + 1),
149                        output_format="text",
150                        noise_listen_time=0.1,
151                        sniff_time=0.02,
152                        verbose=False)
153
154text = "\nFound 2 ISOTP-FlowControl Packet(s):"
155assert text in result
156assert "0x602" in result
157assert "0x603" in result
158assert "0x702" in result
159assert "0x703" in result
160assert "Padding enabled" in result
161
162= scan with text output extended_can id
163
164sock_sender = TestSocket(CAN)
165sock_recv1 = TestSocket(CAN)
166sock_sender.pair(sock_recv1)
167sock_recv2 = TestSocket(CAN)
168sock_sender.pair(sock_recv2)
169sock_noise = TestSocket(CAN)
170sock_sender.pair(sock_noise)
171
172with ISOTPSoftSocket(sock_recv1, tx_id=0x1ffff702, rx_id=0x1ffff602), ISOTPSoftSocket(sock_recv2, tx_id=0x1ffff703, rx_id=0x1ffff603):
173    pkt = CAN(identifier=0x1ffff701, flags="extended", length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
174    make_noise(pkt, 0.01)
175    result = isotp_scan(sock_sender, range(0x1ffff5ff, 0x1ffff604 + 1),
176                        output_format="text",
177                        noise_listen_time=0.1,
178                        sniff_time=0.02,
179                        extended_can_id=True,
180                        verbose=False)
181
182text = "\nFound 2 ISOTP-FlowControl Packet(s):"
183assert text in result
184assert "0x1ffff602" in result
185assert "0x1ffff603" in result
186assert "0x1ffff702" in result
187assert "0x1ffff703" in result
188assert "No Padding" in result
189
190= scan with code output
191
192sock_sender = TestSocket(CAN)
193sock_recv1 = TestSocket(CAN)
194sock_sender.pair(sock_recv1)
195sock_recv2 = TestSocket(CAN)
196sock_sender.pair(sock_recv2)
197sock_noise = TestSocket(CAN)
198sock_sender.pair(sock_noise)
199
200with ISOTPSoftSocket(sock_recv1, tx_id=0x702, rx_id=0x602), ISOTPSoftSocket(sock_recv2, tx_id=0x703, rx_id=0x603):
201    pkt = CAN(identifier=0x701, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
202    make_noise(pkt, 0.01)
203    result = isotp_scan(sock_sender, range(0x5ff, 0x604 + 1),
204                        output_format="code",
205                        noise_listen_time=0.1,
206                        sniff_time=0.02,
207                        can_interface="can0",
208                        verbose=False)
209
210s1 = "ISOTPSocket(can0, tx_id=0x602, rx_id=0x702, " \
211     "padding=False, basecls=ISOTP)\n"
212s2 = "ISOTPSocket(can0, tx_id=0x603, rx_id=0x703, " \
213     "padding=False, basecls=ISOTP)\n"
214assert s1 in result
215assert s2 in result
216
217= scan with json output
218
219sock_sender = TestSocket(CAN)
220sock_recv1 = TestSocket(CAN)
221sock_sender.pair(sock_recv1)
222sock_recv2 = TestSocket(CAN)
223sock_sender.pair(sock_recv2)
224sock_noise = TestSocket(CAN)
225sock_sender.pair(sock_noise)
226
227with ISOTPSoftSocket(sock_recv1, tx_id=0x702, rx_id=0x602), ISOTPSoftSocket(sock_recv2, tx_id=0x703, rx_id=0x603):
228    pkt = CAN(identifier=0x701, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
229    make_noise(pkt, 0.01)
230    result = isotp_scan(sock_sender, range(0x5ff, 0x604 + 1),
231                        output_format="json",
232                        noise_listen_time=0.1,
233                        sniff_time=0.02,
234                        can_interface="can0",
235                        verbose=False)
236
237s1 = "\"iface\": \"can0\", \"tx_id\": 1538, \"rx_id\": 1794, " \
238     "\"padding\": false, \"basecls\": \"ISOTP\""
239s2 = "\"iface\": \"can0\", \"tx_id\": 1539, \"rx_id\": 1795, " \
240     "\"padding\": false, \"basecls\": \"ISOTP\""
241print(result)
242assert s1 in result
243assert s2 in result
244
245= scan with code output noise
246
247sock_sender = TestSocket(CAN)
248sock_recv1 = TestSocket(CAN)
249sock_sender.pair(sock_recv1)
250sock_recv2 = TestSocket(CAN)
251sock_sender.pair(sock_recv2)
252sock_noise = TestSocket(CAN)
253sock_sender.pair(sock_noise)
254
255with ISOTPSoftSocket(sock_recv1, tx_id=0x702, rx_id=0x602), ISOTPSoftSocket(sock_recv2, tx_id=0x703, rx_id=0x603):
256    pkt = CAN(identifier=0x702, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
257    make_noise(pkt, 0.01)
258    result = isotp_scan(sock_sender, range(0x5ff, 0x604 + 1),
259                        output_format="code",
260                        noise_listen_time=0.1,
261                        sniff_time=0.02,
262                        can_interface="can0",
263                        verbose=False)
264
265s1 = "ISOTPSocket(can0, tx_id=0x602, rx_id=0x702, " \
266     "padding=False, basecls=ISOTP)\n"
267s2 = "ISOTPSocket(can0, tx_id=0x603, rx_id=0x703, " \
268     "padding=False, basecls=ISOTP)\n"
269assert s1 not in result
270assert s2 in result
271
272= scan with code output extended_isotp
273
274sock_sender = TestSocket(CAN)
275sock_recv1 = TestSocket(CAN)
276sock_sender.pair(sock_recv1)
277sock_recv2 = TestSocket(CAN)
278sock_sender.pair(sock_recv2)
279sock_noise = TestSocket(CAN)
280sock_sender.pair(sock_noise)
281
282with ISOTPSoftSocket(sock_recv1, tx_id=0x702, rx_id=0x602, ext_address=0x11, rx_ext_address=0x22), ISOTPSoftSocket(sock_recv2, tx_id=0x703, rx_id=0x603, ext_address=0x11, rx_ext_address=0x22):
283    pkt = CAN(identifier=0x701, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
284    make_noise(pkt, 0.01)
285    result = isotp_scan(sock_sender, range(0x5ff, 0x604 + 1),
286                        output_format="code",
287                        noise_listen_time=0.1,
288                        sniff_time=0.05,
289                        extended_scan_range=range(0x20, 0x30),
290                        extended_addressing=True,
291                        can_interface="can0",
292                        verbose=False)
293
294s1 = "ISOTPSocket(can0, tx_id=0x602, rx_id=0x702, padding=False, " \
295         "ext_address=0x22, rx_ext_address=0x11, basecls=ISOTP)"
296s2 = "ISOTPSocket(can0, tx_id=0x603, rx_id=0x703, padding=False, " \
297     "ext_address=0x22, rx_ext_address=0x11, basecls=ISOTP)"
298assert s1 in result
299assert s2 in result
300
301= scan with code output extended_isotp extended can id
302
303sock_sender = TestSocket(CAN)
304sock_recv1 = TestSocket(CAN)
305sock_sender.pair(sock_recv1)
306sock_recv2 = TestSocket(CAN)
307sock_sender.pair(sock_recv2)
308sock_noise = TestSocket(CAN)
309sock_sender.pair(sock_noise)
310
311with ISOTPSoftSocket(sock_recv1, tx_id=0x1ffff702, rx_id=0x1ffff602, ext_address=0x11, rx_ext_address=0x22), ISOTPSoftSocket(sock_recv2, tx_id=0x1ffff703, rx_id=0x1ffff603, ext_address=0x11, rx_ext_address=0x22):
312    pkt = CAN(identifier=0x701, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
313    make_noise(pkt, 0.01)
314    result = isotp_scan(sock_sender, range(0x1ffff5ff, 0x1ffff604 + 1),
315                        output_format="code",
316                        noise_listen_time=0.1,
317                        sniff_time=0.05,
318                        extended_scan_range=range(0x20, 0x30),
319                        extended_addressing=True,
320                        can_interface="can0",
321                        verbose=False)
322
323s1 = "ISOTPSocket(can0, tx_id=0x1ffff602, rx_id=0x1ffff702, padding=False, " \
324     "ext_address=0x22, rx_ext_address=0x11, basecls=ISOTP)"
325s2 = "ISOTPSocket(can0, tx_id=0x1ffff603, rx_id=0x1ffff703, padding=False, " \
326     "ext_address=0x22, rx_ext_address=0x11, basecls=ISOTP)"
327print(result)
328assert s1 in result
329assert s2 in result
330
331= scan default output
332
333sock_sender = TestSocket(CAN)
334sock_recv1 = TestSocket(CAN)
335sock_sender.pair(sock_recv1)
336sock_recv2 = TestSocket(CAN)
337sock_sender.pair(sock_recv2)
338sock_noise = TestSocket(CAN)
339sock_sender.pair(sock_noise)
340
341with ISOTPSoftSocket(sock_recv1, tx_id=0x702, rx_id=0x602), ISOTPSoftSocket(sock_recv2, tx_id=0x703, rx_id=0x603):
342    pkt = CAN(identifier=0x701, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
343    make_noise(pkt, 0.01)
344    result = isotp_scan(sock_sender, range(0x5ff, 0x604 + 1),
345                        noise_listen_time=0.1,
346                        sniff_time=0.02,
347                        can_interface=new_can_socket0(),
348                        verbose=False)
349
350assert 0x602 == result[0].tx_id
351assert 0x702 == result[0].rx_id
352assert 0x603 == result[1].tx_id
353assert 0x703 == result[1].rx_id
354
355for s in result:
356    s.close()
357    del s
358
359= scan default output extended
360
361sock_sender = TestSocket(CAN)
362sock_recv1 = TestSocket(CAN)
363sock_sender.pair(sock_recv1)
364sock_recv2 = TestSocket(CAN)
365sock_sender.pair(sock_recv2)
366sock_noise = TestSocket(CAN)
367sock_sender.pair(sock_noise)
368
369with ISOTPSoftSocket(sock_recv1, tx_id=0x702, rx_id=0x602, ext_address=0x11, rx_ext_address=0x22), ISOTPSoftSocket(sock_recv2, tx_id=0x703, rx_id=0x603, ext_address=0x11, rx_ext_address=0x22):
370    pkt = CAN(identifier=0x701, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
371    make_noise(pkt, 0.01)
372    result = isotp_scan(sock_sender, range(0x5ff, 0x604 + 1),
373                        noise_listen_time=0.1,
374                        sniff_time=0.02,
375                        extended_scan_range=range(0x20, 0x30),
376                        extended_addressing=True,
377                        can_interface=new_can_socket0(),
378                        verbose=False)
379
380assert 0x602 == result[0].tx_id
381assert 0x702 == result[0].rx_id
382assert 0x22 == result[0].ext_address
383assert 0x11 == result[0].rx_ext_address
384assert 0x603 == result[1].tx_id
385assert 0x703 == result[1].rx_id
386assert 0x22 == result[1].ext_address
387assert 0x11 == result[1].rx_ext_address
388
389for s in result:
390    s.close()
391    del s
392
393+ Cleanup
394
395= Delete vcan interfaces
396
397assert cleanup_interfaces()
398
399+ Coverage stability tests
400
401= empty tests
402
403from scapy.contrib.isotp.isotp_scanner import generate_code_output, generate_text_output
404
405assert generate_code_output("", None) == ""
406assert generate_text_output("") == "No packets found."
407
408= get_isotp_fc
409
410from scapy.contrib.isotp.isotp_scanner import get_isotp_fc
411
412# to trigger "noise_ids.append(packet.identifier)"
413a = []
414get_isotp_fc(
415    1, [], a, False,
416    Bunch(
417        flags="extended",
418        identifier=1,
419        data=b"\x00"
420    )
421)
422assert 1 in a
423