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

% CAN unit tests

+ Configuration of kamene

= Load CAN_addon
~ conf command
from kamene.layers.can import CAN, CANSocket, srcan

= Setup string for vcan
~ conf command
bashCommand = "/bin/bash -c 'sudo modprobe vcan; 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

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

+ Basic Packet Tests()
= CAN Packet init

canframe = CAN(id=0x7ff,dlc=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
bytes(canframe) == b'\xff\x07\x00\x00\x08\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08'

= DLC greater than 8
canframe = CAN(id=0x7ff,dlc=9,data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
canframe.dlc = len(canframe.data)
bytes(canframe) == b'\xff\x07\x00\x00\x08\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08'

+ Basic Socket Tests()
= CAN Socket Init

sock1 = CANSocket(iface="vcan0")

= CAN Socket send recv

def sender():
    sleep(0.1)
    sock2 = CANSocket(iface="vcan0")
    sock2.send(CAN(id=0x7ff,dlc=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))

thread = threading.Thread(target=sender)
thread.start()

rx = sock1.recv()
rx == CAN(id=0x7ff,dlc=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08')

+ Advanced Socket Tests()
= CAN Socket sr1

tx = CAN(id=0x7ff,dlc=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08')

= CAN Socket sr1 init time
tx.sent_time == 0

def sender():
    sleep(0.1)
    sock2 = CANSocket(iface="vcan0")
    sock2.send(tx)

thread = threading.Thread(target=sender)
thread.start()
rx = None
rx = sock1.sr1(tx)

= CAN Socket sr1 time check
tx.sent_time < rx.time and tx == rx and rx.time > 0

= srcan

tx = CAN(id=0x7ff,dlc=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08')

= srcan check init time
tx.sent_time == 0

def sender():
    sleep(0.1)
    sock2 = CANSocket(iface="vcan0")
    sock2.send(tx)

thread = threading.Thread(target=sender)
thread.start()
rx = None
rx = srcan(tx, "vcan0", timeout=1)
rx = rx[0][0][1]

= srcan check rx and tx
tx == rx and tx.sent_time > 0 and rx.time > 0 and tx.sent_time < rx.time

= sniff with filtermask 0x7ff

sock1 = CANSocket(iface='vcan0', filter=[{'can_id': 0x200, 'can_mask': 0x7ff}])

def sender():
    sleep(0.1)
    sock2 = CANSocket(iface="vcan0")
    sock2.send(CAN(id=0x200, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x300, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x300, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x200, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x100, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x200, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))

thread = threading.Thread(target=sender)
thread.start()

packets = sock1.sniff(timeout=0.3)
len(packets) == 3

= sniff with filtermask 0x700

sock1 = CANSocket(iface='vcan0', filter=[{'can_id': 0x200, 'can_mask': 0x700}])

def sender():
    sleep(0.1)
    sock2 = CANSocket(iface="vcan0")
    sock2.send(CAN(id=0x212, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x300, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x2ff, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x1ff, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x200, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x2aa, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))

thread = threading.Thread(target=sender)
thread.start()

packets = sock1.sniff(timeout=0.3)
len(packets) == 4

= sniff with filtermask 0x0ff

sock1 = CANSocket(iface='vcan0', filter=[{'can_id': 0x200, 'can_mask': 0x0ff}])

def sender():
    sleep(0.1)
    sock2 = CANSocket(iface="vcan0")
    sock2.send(CAN(id=0x200, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x301, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x300, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x1ff, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x700, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x100, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))

thread = threading.Thread(target=sender)
thread.start()

packets = sock1.sniff(timeout=0.3)
len(packets) == 4

= sniff with multiple filters

sock1 = CANSocket(iface='vcan0', filter=[{'can_id': 0x200, 'can_mask': 0x7ff},
                                                     {'can_id': 0x400, 'can_mask': 0x7ff},
                                                     {'can_id': 0x600, 'can_mask': 0x7ff},
                                                     {'can_id': 0x7ff, 'can_mask': 0x7ff}])

def sender():
    sleep(0.1)
    sock2 = CANSocket(iface="vcan0")
    sock2.send(CAN(id=0x200, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x300, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x400, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x500, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x600, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x700, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x7ff, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))


thread = threading.Thread(target=sender)
thread.start()

packets = sock1.sniff(timeout=0.3)
len(packets) == 4

= sniff with filtermask 0x7ff and inverse filter

sock1 = CANSocket(iface='vcan0', filter=[{'can_id': 0x200 | CAN_INV_FILTER, 'can_mask': 0x7ff}])

def sender():
    sleep(0.1)
    sock2 = CANSocket(iface="vcan0")
    sock2.send(CAN(id=0x200, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x200, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x300, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x200, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x100, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(id=0x200, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))

thread = threading.Thread(target=sender)
thread.start()

packets = sock1.sniff(timeout=0.3)
len(packets) == 2

= sniff with filtermask 0x1FFFFFFF

sock1 = CANSocket(iface='vcan0', filter=[{'can_id': 0x10000000, 'can_mask': 0x1FFFFFFF}])

def sender():
    sleep(0.1)
    sock2 = CANSocket(iface="vcan0")
    sock2.send(CAN(flags='EFF', id=0x10010000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(flags='EFF', id=0x10020000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(flags='EFF', id=0x10000000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(flags='EFF', id=0x10030000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(flags='EFF', id=0x10040000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(flags='EFF', id=0x10000000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))

thread = threading.Thread(target=sender)
thread.start()

packets = sock1.sniff(timeout=0.3)
packets[0].show()
print(len(packets))

len(packets) == 2

= sniff with filtermask 0x1FFFFFFF and inverse filter

sock1 = CANSocket(iface='vcan0', filter=[{'can_id': 0x10000000 | CAN_INV_FILTER, 'can_mask': 0x1FFFFFFF}])

def sender():
    sleep(0.1)
    sock2 = CANSocket(iface="vcan0")
    sock2.send(CAN(flags='EFF', id=0x10010000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(flags='EFF', id=0x10020000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(flags='EFF', id=0x10000000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(flags='EFF', id=0x10030000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(flags='EFF', id=0x10040000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))
    sock2.send(CAN(flags='EFF', id=0x10000000, dlc=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))

thread = threading.Thread(target=sender)
thread.start()

packets = sock1.sniff(timeout=0.3)
packets[0].show()
print(len(packets))

len(packets) == 4

= CAN Socket sr1 with receive own messages

sock1 = CANSocket(iface="vcan0", receive_own_messages=True)
tx = CAN(id=0x7ff,dlc=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
rx = None
rx = sock1.sr1(tx)
tx.sent_time < rx.time and tx == rx and rx.time > 0

+ PCAP CAN Tests()
= Write pcap file

rx = CAN(id=0x7ff,dlc=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08')
wrpcap('/tmp/kamenePcapTest.pcap', rx, append=False)
readPack = rdpcap('/tmp/kamenePcapTest.pcap', 1)
rx == readPack[0]

