File: isotp_native_socket.uts

package info (click to toggle)
scapy 2.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,956 kB
  • sloc: python: 163,618; sh: 90; makefile: 11
file content (690 lines) | stat: -rw-r--r-- 23,336 bytes parent folder | download | duplicates (2)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
% Regression tests for ISOTPNativeSocket
~ automotive_comm

+ Configuration
~ conf

= Imports

with open(scapy_path("test/contrib/automotive/interface_mockup.py")) as f:
    exec(f.read())

= Definition of constants, utility functions and mock classes

# hexadecimal to bytes convenience function
dhex = bytes.fromhex

+ Compatibility with can-isotp linux kernel modules

= Compatibility with isotpsend
exit_if_no_isotp_module()

message = "01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14"

with new_can_socket0() as isocan, ISOTPSoftSocket(isocan, tx_id=0x642, rx_id=0x242) as s:
    p = subprocess.Popen(["isotpsend", "-s", "242", "-d", "642", iface0], stdin=subprocess.PIPE, universal_newlines=True)
    p.communicate(message)
    r = p.returncode
    assert r == 0
    isotp = s.recv()
    assert isotp.data == dhex(message)


= Compatibility with isotpsend - extended addresses
exit_if_no_isotp_module()
message = "01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14"

with new_can_socket0() as isocan, ISOTPSoftSocket(isocan, tx_id=0x644, rx_id=0x244, ext_address=0xaa, rx_ext_address=0xee) as s:
    p = subprocess.Popen(["isotpsend", "-s", "244", "-d", "644", "-x", "ee:aa", iface0], stdin=subprocess.PIPE, universal_newlines=True)
    p.communicate(message)
    r = p.returncode
    assert r == 0
    isotp = s.recv()
    assert isotp.data == dhex(message)


= Compatibility with isotprecv
exit_if_no_isotp_module()

isotp = ISOTP(data=bytearray(range(1,20)))
p = subprocess.Popen(["isotprecv", "-s", "243", "-d", "643", "-b", "3", iface0], stdout=subprocess.PIPE)
time.sleep(0.1)
with new_can_socket0() as isocan, ISOTPSoftSocket(isocan, tx_id=0x643, rx_id=0x243) as s:
    s.send(isotp)

timer = threading.Timer(1, lambda: p.terminate() if p.poll() else p.wait())
timer.start()  # Timeout the receiver after 1 second
r = p.wait()
assert 0 == r

result = None
for i in range(10):
    time.sleep(0.1)
    if p.poll() is not None:
        result = p.stdout.readline().decode().strip()
        break

assert result is not None
result_data = dhex(result)
assert result_data == isotp.data

timer.join(5)
assert not timer.is_alive()


= Compatibility with isotprecv - extended addresses
exit_if_no_isotp_module()
isotp = ISOTP(data=bytearray(range(1,20)))
cmd = ["isotprecv", "-s245", "-d645", "-b3", "-x", "ee:aa", iface0]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
time.sleep(0.1)  # Give some time for starting reception
with new_can_socket0() as isocan, ISOTPSoftSocket(isocan, tx_id=0x645, rx_id=0x245, ext_address=0xaa, rx_ext_address=0xee) as s:
    s.send(isotp)

timer = threading.Timer(1, lambda: p.terminate() if p.poll() else p.wait())
timer.start()  # Timeout the receiver after 1 second
r = p.wait()
assert 0 == r

result = None
for i in range(10):
    time.sleep(0.1)
    if p.poll() is not None:
        result = p.stdout.readline().decode().strip()
        break

assert result is not None
result_data = dhex(result)
assert result_data == isotp.data

timer.join(5)
assert not timer.is_alive()

= Compatibility ISOTPSoftSocket ISOTPNativeSocket various configs
exit_if_no_isotp_module()

message = "01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14" * 5

kwargs = [({"tx_id": 0x242, "rx_id": 0x642, "bs": 0, "stmin": 0, "padding": False, "ext_address": None, "rx_ext_address": None},
           {"tx_id": 0x642, "rx_id": 0x242, "bs": 0, "stmin": 0, "padding": False, "ext_address": None, "rx_ext_address": None}),
          ({"tx_id": 0x242, "rx_id": 0x642, "bs": 2, "stmin": 0, "padding": False, "ext_address": None, "rx_ext_address": None},
           {"tx_id": 0x642, "rx_id": 0x242, "bs": 0, "stmin": 0, "padding": False, "ext_address": None, "rx_ext_address": None}),
          ({"tx_id": 0x242, "rx_id": 0x642, "bs": 5, "stmin": 0, "padding": False, "ext_address": None, "rx_ext_address": None},
           {"tx_id": 0x642, "rx_id": 0x242, "bs": 0, "stmin": 0, "padding": False, "ext_address": None, "rx_ext_address": None}),
          ({"tx_id": 0x242, "rx_id": 0x642, "bs": 0, "stmin": 5, "padding": False, "ext_address": None, "rx_ext_address": None},
           {"tx_id": 0x642, "rx_id": 0x242, "bs": 0, "stmin": 0, "padding": False, "ext_address": None, "rx_ext_address": None}),
          ({"tx_id": 0x242, "rx_id": 0x642, "bs": 0, "stmin": 10, "padding": False, "ext_address": None, "rx_ext_address": None},
           {"tx_id": 0x642, "rx_id": 0x242, "bs": 0, "stmin": 0, "padding": False, "ext_address": None, "rx_ext_address": None}),
          ({"tx_id": 0x242, "rx_id": 0x642, "bs": 4, "stmin": 130, "padding": False, "ext_address": None, "rx_ext_address": None},
           {"tx_id": 0x642, "rx_id": 0x242, "bs": 0, "stmin": 0, "padding": False, "ext_address": None, "rx_ext_address": None}),
          ({"tx_id": 0x242, "rx_id": 0x642, "bs": 3, "stmin": 0, "padding": True, "ext_address": None, "rx_ext_address": None},
           {"tx_id": 0x642, "rx_id": 0x242, "bs": 0, "stmin": 0, "padding": True, "ext_address": None, "rx_ext_address": None}),
          ({"tx_id": 0x242, "rx_id": 0x642, "bs": 0, "stmin": 0, "padding": False, "ext_address": 0xfe, "rx_ext_address": None},
           {"tx_id": 0x642, "rx_id": 0x242, "bs": 0, "stmin": 0, "padding": False, "ext_address": 0xfe, "rx_ext_address": None}),
          ({"tx_id": 0x242, "rx_id": 0x642, "bs": 0, "stmin": 0, "padding": False, "ext_address": 0xfe, "rx_ext_address": 0xef},
           {"tx_id": 0x642, "rx_id": 0x242, "bs": 0, "stmin": 0, "padding": False, "ext_address": 0xef, "rx_ext_address": 0xfe}),
          ({"tx_id": 0x242, "rx_id": 0x642, "bs": 6, "stmin": 10, "padding": True, "ext_address": 0x12, "rx_ext_address": 0x23},
           {"tx_id": 0x642, "rx_id": 0x242, "bs": 6, "stmin": 5, "padding": True, "ext_address": 0x23, "rx_ext_address": 0x12}),
          ({"tx_id": 0x242, "rx_id": 0x642, "bs": 0, "stmin": 0, "padding": True, "ext_address": 0x45, "rx_ext_address": None},
           {"tx_id": 0x642, "rx_id": 0x242, "bs": 0, "stmin": 40, "padding": True, "ext_address": 0x45, "rx_ext_address": None}),
          ({"tx_id": 0x123, "rx_id": 0x642, "bs": 1, "stmin": 1, "padding": False, "ext_address": None, "rx_ext_address": None},
           {"tx_id": 0x642, "rx_id": 0x123, "bs": 1, "stmin": 1, "padding": False, "ext_address": None, "rx_ext_address": None}),]

for kwargs1, kwargs2 in kwargs:
    print("Testing config %s, %s" % (kwargs1, kwargs2))
    with NativeCANSocket(iface0) as cs:
        cs.sniff(timeout=0.01)
        with ISOTPSoftSocket(iface0, **kwargs1) as s, ISOTPNativeSocket(iface0, **kwargs2) as ns:
            ns.send(ISOTP(bytes.fromhex(message)))
            isotp = s.recv()
            assert isotp.data == dhex(message)
            ns.send(ISOTP(bytes.fromhex("00 11 22")))
            isotp = s.recv()
            assert (isotp.data == dhex("00 11 22"))
            pks1 = cs.sniff(timeout=0.01)
        with ISOTPNativeSocket(iface0, **kwargs1) as s, ISOTPSoftSocket(iface0, **kwargs2) as ns:
            ns.send(ISOTP(bytes.fromhex(message)))
            isotp = s.recv()
            assert isotp.data == dhex(message)
            ns.send(ISOTP(bytes.fromhex("00 11 22")))
            isotp = s.recv()
            assert (isotp.data == dhex("00 11 22"))
            pks2 = cs.sniff(timeout=0.01)
        assert len(pks1) == len(pks2) and len(pks2) > 0
        for p1, p2 in zip(pks1, pks2):
            assert bytes(p1) == bytes(p2)


+ ISOTPNativeSocket tests

= Create ISOTP socket
exit_if_no_isotp_module()
s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241)

= Send single frame ISOTP message
exit_if_no_isotp_module()

with new_can_socket(iface0) as cans:
    s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241)
    s.send(ISOTP(data=dhex("01 02 03 04 05")))
    can = cans.sniff(timeout=1, count=1)[0]
    assert can.identifier == 0x641
    assert can.data == dhex("05 01 02 03 04 05")


= Send single frame ISOTP message Test init with CANSocket
exit_if_no_isotp_module()
cans = CANSocket(iface0)
s = ISOTPNativeSocket(cans, tx_id=0x641, rx_id=0x241)
s.send(ISOTP(data=dhex("01 02 03 04 05")))
can = cans.sniff(timeout=1, count=1)[0]
assert can.identifier == 0x641
assert can.data == dhex("05 01 02 03 04 05")
cans.close()


= Test init with wrong type
exit_if_no_isotp_module()
exception_catched = False
try:
    s = ISOTPNativeSocket(42, tx_id=0x641, rx_id=0x241)
except Scapy_Exception:
    exception_catched = True

assert exception_catched

= Send two-frame ISOTP message
exit_if_no_isotp_module()

evt = threading.Event()
def acker():
    with new_can_socket(iface0) as cans:
        evt.set()
        can = cans.sniff(timeout=1, count=1)[0]
        cans.send(CAN(identifier = 0x241, data=dhex("30 00 00")))


with new_can_socket(iface0) as cans:
    t = Thread(target=acker)
    t.start()
    s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241)
    evt.wait(timeout=5)
    s.send(ISOTP(data=dhex("01 02 03 04 05 06 07 08")))
    can = cans.sniff(timeout=1, count=1)[0]
    assert can.identifier == 0x641
    assert can.data == dhex("10 08 01 02 03 04 05 06")
    can = cans.sniff(timeout=1, count=1)[0]
    assert can.identifier == 0x241
    assert can.data == dhex("30 00 00")
    can = cans.sniff(timeout=1, count=1)[0]
    assert can.identifier == 0x641
    assert can.data == dhex("21 07 08")
    t.join(timeout=5)
    assert not t.is_alive()

= Send a single frame ISOTP message with padding
exit_if_no_isotp_module()
s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241, padding=True)

with new_can_socket(iface0) as cans:
    s.send(ISOTP(data=dhex("01")))
    can = cans.sniff(timeout=1, count=1)[0]
    assert can.length == 8


= Send a two-frame ISOTP message with padding
exit_if_no_isotp_module()

acker_ready = threading.Event()
def acker():
    with new_can_socket(iface0) as acks:
        acker_ready.set()
        can = acks.sniff(timeout=1, count=1)[0]
        acks.send(CAN(identifier = 0x241, data=dhex("30 00 00")))

with new_can_socket(iface0) as cans:
    thread = Thread(target=acker)
    thread.start()
    acker_ready.wait(timeout=5)
    s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241, padding=True)
    s.send(ISOTP(data=dhex("01 02 03 04 05 06 07 08")))
    can = cans.sniff(timeout=1, count=1)[0]
    assert can.identifier == 0x641
    assert can.data == dhex("10 08 01 02 03 04 05 06")
    can = cans.sniff(timeout=1, count=1)[0]
    assert can.identifier == 0x241
    assert can.data == dhex("30 00 00")
    can = cans.sniff(timeout=1, count=1)[0]
    assert can.identifier == 0x641
    assert can.data == dhex("21 07 08 CC CC CC CC CC")
    thread.join(5)
    assert not thread.is_alive()


= Receive a padded single frame ISOTP message with padding disabled
exit_if_no_isotp_module()
s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241, padding=False)
with new_can_socket(iface0) as cans:
    cans.send(CAN(identifier=0x241, data=dhex("02 05 06 00 00 00 00 00")))
    res = s.recv()
    assert res.data == dhex("05 06")


= Receive a padded single frame ISOTP message with padding enabled
exit_if_no_isotp_module()
s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241, padding=True)
with new_can_socket(iface0) as cans:
    cans.send(CAN(identifier=0x241, data=dhex("02 05 06 00 00 00 00 00")))
    res = s.recv()
    assert res.data == dhex("05 06")


= Receive a non-padded single frame ISOTP message with padding enabled
exit_if_no_isotp_module()
s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241, padding=True)
with new_can_socket(iface0) as cans:
    cans.send(CAN(identifier=0x241, data=dhex("02 05 06")))
    res = s.recv()
    assert res.data == dhex("05 06")


= Receive a padded two-frame ISOTP message with padding enabled
exit_if_no_isotp_module()
s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241, padding=True)
with new_can_socket(iface0) as cans:
    cans.send(CAN(identifier=0x241, data=dhex("10 09 01 02 03 04 05 06")))
    cans.send(CAN(identifier=0x241, data=dhex("21 07 08 09 00 00 00 00")))
    res = s.recv()
    assert res.data == dhex("01 02 03 04 05 06 07 08 09")


= Receive a padded two-frame ISOTP message with padding disabled
exit_if_no_isotp_module()
s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241, padding=False)
with new_can_socket(iface0) as cans:
    cans.send(CAN(identifier=0x241, data=dhex("10 09 01 02 03 04 05 06")))
    cans.send(CAN(identifier=0x241, data=dhex("21 07 08 09 00 00 00 00")))
    res = s.recv()
    assert res.data == dhex("01 02 03 04 05 06 07 08 09")


= Receive a single frame ISOTP message
exit_if_no_isotp_module()
s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241)
with new_can_socket(iface0) as cans:
    cans.send(CAN(identifier = 0x241, data = dhex("05 01 02 03 04 05")))
    isotp = s.recv()
    assert isotp.data == dhex("01 02 03 04 05")
    assert isotp.tx_id == 0x641
    assert isotp.rx_id == 0x241
    assert isotp.ext_address == None
    assert isotp.rx_ext_address == None


= Receive a single frame ISOTP message, with extended addressing
exit_if_no_isotp_module()
s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241, ext_address=0xc0, rx_ext_address=0xea)
with new_can_socket(iface0) as cans:
    cans.send(CAN(identifier = 0x241, data = dhex("EA 05 01 02 03 04 05")))
    isotp = s.recv()
    assert isotp.data == dhex("01 02 03 04 05")
    assert isotp.tx_id == 0x641
    assert isotp.rx_id == 0x241
    assert isotp.ext_address == 0xc0
    assert isotp.rx_ext_address == 0xea


= Receive a two-frame ISOTP message
exit_if_no_isotp_module()
s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241)
with new_can_socket(iface0) as cans:
    cans.send(CAN(identifier = 0x241, data = dhex("10 0B 01 02 03 04 05 06")))
    cans.send(CAN(identifier = 0x241, data = dhex("21 07 08 09 10 11")))
    isotp = s.recv()
    assert isotp.data == dhex("01 02 03 04 05 06 07 08 09 10 11")

= Receive a two-frame ISOTP message and test python with statement
exit_if_no_isotp_module()
with ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241) as s:
    with new_can_socket(iface0) as cans:
        cans.send(CAN(identifier = 0x241, data = dhex("10 0B 01 02 03 04 05 06")))
        cans.send(CAN(identifier = 0x241, data = dhex("21 07 08 09 10 11")))
    isotp = s.recv()
    assert isotp.data == dhex("01 02 03 04 05 06 07 08 09 10 11")


= Send single CANFD frame ISOTP message
exit_if_no_isotp_module()

with new_can_socket(iface0, fd=True) as cans:
    s = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241, fd=True)
    s.send(ISOTP(data=dhex("01 02 03 04 05 06 07 08 09")))
    can = cans.sniff(timeout=1, count=1)[0]
    assert can.identifier == 0x641
    assert can.data == dhex("09 01 02 03 04 05 06 07 08 09")


= ISOTP Socket sr1 test
exit_if_no_isotp_module()

txSock = ISOTPNativeSocket(iface0, tx_id=0x123, rx_id=0x321, basecls=ISOTP)
txmsg = ISOTP(b'\x11\x22\x33')
rx2 = None

receiver_up = Event()

def sender():
    global receiver_up
    receiver_up.wait(timeout=5)
    global txmsg
    global rx2
    rx2 = txSock.sr1(txmsg, timeout=1, verbose=True)

def receiver():
    global receiver_up
    with new_can_socket(iface0) as cans:
        rx = cans.sniff(timeout=1, count=1, started_callback=receiver_up.set)[0]
        cans.send(CAN(identifier=0x321, length=4, data=b'\x03\x7f\x22\x33'))
    expectedrx = CAN(identifier=0x123, length=4, data=b'\x03\x11\x22\x33')
    assert rx.length == expectedrx.length
    assert rx.data == expectedrx.data
    assert rx.identifier == expectedrx.identifier

txThread = threading.Thread(target=sender)
txThread.start()
receiver()
txThread.join(timeout=5)
assert not txThread.is_alive()

assert rx2 is not None
assert rx2 == ISOTP(b'\x7f\x22\x33')
assert rx2.answers(txmsg)

= ISOTP Socket sr1 and ISOTP test
exit_if_no_isotp_module()
txSock = ISOTPNativeSocket(iface0, 0x123, 0x321, basecls=ISOTP)
rxSock = ISOTPNativeSocket(iface0, 0x321, 0x123, basecls=ISOTP)
msg = ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33')
rx2 = None

receiver_up = Event()

def sender():
    receiver_up.wait(timeout=5)
    global rx2
    rx2 = txSock.sr1(msg, timeout=1, verbose=True)

def receiver():
    global rx
    receiver_up.set()
    rx = rxSock.recv()
    rxSock.send(msg)

txThread = threading.Thread(target=sender)
txThread.start()
receiver()
txThread.join(timeout=5)
assert not txThread.is_alive()

assert rx == msg
assert rxSock.send(msg)
assert rx2 is not None
assert rx2 == msg

= ISOTP Socket sr1 and ISOTP test vice versa
exit_if_no_isotp_module()

rxSock = ISOTPNativeSocket(iface0, 0x321, 0x123, basecls=ISOTP)
txSock = ISOTPNativeSocket(iface0, 0x123, 0x321, basecls=ISOTP)

msg = ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33')

receiver_up = Event()

def receiver():
    global rx2, sent
    rx2 = rxSock.sniff(count=1, timeout=1, started_callback=receiver_up.set)
    sent = rxSock.send(msg)

def sender():
    global rx
    receiver_up.wait(timeout=5)
    rx = txSock.sr1(msg, timeout=1,verbose=True)

rx2 = None
sent = False
rxThread = threading.Thread(target=receiver)
rxThread.start()
sender()
rxThread.join(timeout=5)
assert not rxThread.is_alive()

assert rx == msg
assert rx2[0] == msg
assert sent

= ISOTP Socket sniff
exit_if_no_isotp_module()

rxSock = ISOTPNativeSocket(iface0, 0x321, 0x123, basecls=ISOTP)
txSock = ISOTPNativeSocket(iface0, 0x123, 0x321, basecls=ISOTP)
succ = False

receiver_up = Event()

def receiver():
    rx = rxSock.sniff(count=5, timeout=1, started_callback=receiver_up.set)
    msg = ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33')
    msg.data += b'0'
    assert rx[0] == msg
    msg.data += b'1'
    assert rx[1] == msg
    msg.data += b'2'
    assert rx[2] == msg
    msg.data += b'3'
    assert rx[3] == msg
    msg.data += b'4'
    assert rx[4] == msg
    global succ
    succ = True

def sender():
    receiver_up.wait(timeout=5)
    msg = ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33')
    msg.data += b'0'
    assert txSock.send(msg)
    msg.data += b'1'
    assert txSock.send(msg)
    msg.data += b'2'
    assert txSock.send(msg)
    msg.data += b'3'
    assert txSock.send(msg)
    msg.data += b'4'
    assert txSock.send(msg)

rxThread = threading.Thread(target=receiver)
rxThread.start()
sender()
rxThread.join(timeout=5)
assert not rxThread.is_alive()

assert succ

+ ISOTPNativeSocket MITM attack tests
~ vcan_socket needs_root linux

= bridge and sniff with isotp native sockets set up vcan0 and vcan1 for package forwarding vcan1
exit_if_no_isotp_module()

isoTpSocket0 = ISOTPNativeSocket(iface0, tx_id=0x241, rx_id=0x641)
isoTpSocket1 = ISOTPNativeSocket(iface1, tx_id=0x641, rx_id=0x241)
bSocket0 = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241)
bSocket1 = ISOTPNativeSocket(iface1, tx_id=0x241, rx_id=0x641)

bridgeStarted = threading.Event()
def bridge():
    global bridgeStarted
    def forwarding(pkt):
        return pkt
    bridge_and_sniff(if1=bSocket0, if2=bSocket1, xfrm12=forwarding, xfrm21=forwarding, timeout=2, count=1, started_callback=bridgeStarted.set)
    bSocket0.close()
    bSocket1.close()
    global bSucc
    bSucc = True

bSucc = False

threadBridge = threading.Thread(target=bridge)
threadBridge.start()
bridgeStarted.wait(timeout=5)
isoTpSocket0.send(ISOTP(b'Request'))
packetsVCan1 = isoTpSocket1.sniff(timeout=0.5, count=1)

assert len(packetsVCan1) == 1

isoTpSocket0.close()
isoTpSocket1.close()

threadBridge.join(timeout=5)
assert not threadBridge.is_alive()

assert bSucc

= bridge and sniff with isotp native sockets set up vcan0 and vcan1 for package change to vcan1
exit_if_no_isotp_module()

isoTpSocket0 = ISOTPNativeSocket(iface0, tx_id=0x241, rx_id=0x641)
isoTpSocket1 = ISOTPNativeSocket(iface1, tx_id=0x641, rx_id=0x241)
bSocket0 = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241)
bSocket1 = ISOTPNativeSocket(iface1, tx_id=0x241, rx_id=0x641)

bSucc = False

bridgeStarted = threading.Event()
def bridge():
    global bridgeStarted
    global bSucc
    def forwarding(pkt):
        pkt.data = 'changed'
        return pkt
    bridge_and_sniff(if1=bSocket0, if2=bSocket1, xfrm12=forwarding, xfrm21=forwarding, timeout=0.5, started_callback=bridgeStarted.set)
    bSocket0.close()
    bSocket1.close()
    bSucc = True

threadBridge = threading.Thread(target=bridge)
threadBridge.start()
bridgeStarted.wait(timeout=5)
isoTpSocket0.send(ISOTP(b'Request'))
packetsVCan1 = isoTpSocket1.sniff(timeout=0.5, count=1)

packetsVCan1[0].data = b'changed'
assert len(packetsVCan1) == 1

isoTpSocket0.close()
isoTpSocket1.close()

threadBridge.join(timeout=5)
assert not threadBridge.is_alive()

assert bSucc

= bridge and sniff with isotp native sockets set up vcan0 and vcan1 for package forwarding in both directions
exit_if_no_isotp_module()

bSucc = False

isoTpSocket0 = ISOTPNativeSocket(iface0, tx_id=0x241, rx_id=0x641)
isoTpSocket1 = ISOTPNativeSocket(iface1, tx_id=0x641, rx_id=0x241)
bSocket0 = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241)
bSocket1 = ISOTPNativeSocket(iface1, tx_id=0x241, rx_id=0x641)

bridgeStarted = threading.Event()
def bridge():
    global bridgeStarted
    global bSucc
    def forwarding(pkt):
        return pkt
    bridge_and_sniff(if1=bSocket0, if2=bSocket1, xfrm12=forwarding, xfrm21=forwarding, timeout=0.5, started_callback=bridgeStarted.set, count=2)
    bSocket0.close()
    bSocket1.close()
    bSucc = True

threadBridge = threading.Thread(target=bridge)
threadBridge.start()
bridgeStarted.wait(timeout=5)

packetVcan0 = ISOTP(b'RequestVcan0')
packetVcan1 = ISOTP(b'RequestVcan1')
isoTpSocket0.send(packetVcan0)
isoTpSocket1.send(packetVcan1)

packetsVCan0 = isoTpSocket0.sniff(timeout=0.5, count=1)
packetsVCan1 = isoTpSocket1.sniff(timeout=0.5, count=1)

len(packetsVCan0) == 1
len(packetsVCan1) == 1

isoTpSocket0.close()
isoTpSocket1.close()

threadBridge.join(timeout=5)
assert not threadBridge.is_alive()

assert bSucc

= bridge and sniff with isotp native sockets set up vcan0 and vcan1 for package change in both directions
exit_if_no_isotp_module()

bSucc = False

isoTpSocket0 = ISOTPNativeSocket(iface0, tx_id=0x241, rx_id=0x641)
isoTpSocket1 = ISOTPNativeSocket(iface1, tx_id=0x641, rx_id=0x241)
bSocket0 = ISOTPNativeSocket(iface0, tx_id=0x641, rx_id=0x241)
bSocket1 = ISOTPNativeSocket(iface1, tx_id=0x241, rx_id=0x641)

bridgeStarted = threading.Event()
def bridge():
    global bridgeStarted
    global bSucc
    def forwarding(pkt):
        pkt.data = 'changed'
        return pkt
    bridge_and_sniff(if1=bSocket0, if2=bSocket1, xfrm12=forwarding, xfrm21=forwarding, timeout=0.5, started_callback=bridgeStarted.set, count=2)
    bSocket0.close()
    bSocket1.close()
    bSucc = True

threadBridge = threading.Thread(target=bridge)
threadBridge.start()
bridgeStarted.wait(timeout=5)

packetVcan0 = ISOTP(b'RequestVcan0')
packetVcan1 = ISOTP(b'RequestVcan1')
isoTpSocket0.send(packetVcan0)
isoTpSocket1.send(packetVcan1)

packetsVCan0 = isoTpSocket0.sniff(timeout=0.5, count=1)
packetsVCan1 = isoTpSocket1.sniff(timeout=0.5, count=1)

packetsVCan0[0].data = b'changed'
assert len(packetsVCan0) == 1
packetsVCan1[0].data = b'changed'
assert len(packetsVCan1) == 1

isoTpSocket0.close()
isoTpSocket1.close()

threadBridge.join(timeout=5)
assert not threadBridge.is_alive()

assert bSucc

+ Cleanup

= Cleanup reference to ISOTPSoftSocket to let the thread end
s = None

= Delete vcan interfaces

assert cleanup_interfaces()