# isotp unit tests
#
# Type the following command to launch start the tests:
# $ sudo bash test/run_tests -t test/isotp.uts -F

% isotp unit tests

+ Configuration of kamene

= Load isotp socket
~ conf command

from kamene.layers.isotp import ISOTPSocket
from kamene.layers.can import CANSocket, CAN
from kamene.layers.uds import *

= Setup string for vcan
~ conf command
bashCommand = "/bin/bash -c 'sudo modprobe vcan; sudo modprobe can-isotp; sudo ip link add name vcan0 type vcan; sudo ip link set dev vcan0 up'"

= Load os
~ conf command
import os
import threading
from time import sleep
from kamene.layers.isotp import ISOTP, ISOTPSocket
from kamene.layers.can import CAN, CANSocket


= Setup vcan0
~ conf command
0 == os.system(bashCommand)

+ Basic Socket Tests()
= ISOTP Socket simple send

def sender():
    sleep(0.2)
    sock = ISOTPSocket('vcan0', 123, 321)
    sock.send(ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33'))

txThread = threading.Thread(target=sender)

sock = ISOTPSocket('vcan0', 321, 123)
txThread.start()
rx = sock.recv()
rx == ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33')

= ISOTP Socket send with extended addressing

def sender():
    sleep(0.2)
    sock = ISOTPSocket('vcan0', 123, 321, 23, 21)
    sock.send(ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33'))

txThread = threading.Thread(target=sender)

sock = ISOTPSocket('vcan0', 321, 123, 21, 23)
txThread.start()
rx = sock.recv()
rx == ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33')

= ISOTP Socket validate single frame

def sender():
    sleep(0.1)
    sock = ISOTPSocket('vcan0', 0x123, 0x321)
    sock.send(Raw(load=b'\x11\x22\x33'))

txThread = threading.Thread(target=sender)

sock = CANSocket('vcan0')
txThread.start()
rx = sock.recv()
rx == CAN(id=0x321, dlc=4, data=b'\x03\x11\x22\x33')

= ISOTP Socket sr1 test

def sender():
    sleep(0.1)
    sock = ISOTPSocket('vcan0', 0x123, 0x321)
    rx = sock.sr1(Raw(load=b'\x11\x22\x33'), timeout=1)
    rx is not None
    rx == ISOTP(b'\x7f\x22\x33')

txThread = threading.Thread(target=sender)

sock = CANSocket('vcan0')
txThread.start()
rx = sock.recv()
rx == CAN(id=0x321, dlc=4, data=b'\x03\x11\x22\x33')
sock.send(CAN(id=0x123, dlc=4, data=b'\x03\x7f\x22\x33'))

= ISOTP Socket sr1 and ISOTP test

def sender():
    sleep(0.1)
    sock = ISOTPSocket('vcan0', 0x123, 0x321)
    rx = sock.sr1(ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33'), timeout=1)
    rx is not None
    rx == ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33')

txThread = threading.Thread(target=sender)

sock = ISOTPSocket('vcan0', 0x321, 0x123)
txThread.start()
rx = sock.recv()
rx == ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33')
sock.send(ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33'))

= ISOTP Socket sr1 and ISOTP test vice versa

def receiver():
    sock = ISOTPSocket('vcan0', 0x321, 0x123)
    rx = sock.recv()
    rx == ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33')
    sock.send(ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33'))

rxThread = threading.Thread(target=receiver)
rxThread.start()
sleep(0.1)
sock = ISOTPSocket('vcan0', 0x123, 0x321)
rx = sock.sr1(ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33'), timeout=1)
rx == ISOTP(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33')

