% Regression tests for automotive scanner staged test_case

+ Load general modules

= Load contribution layer

from scapy.contrib.automotive.scanner.test_case import AutomotiveTestCase
from scapy.contrib.automotive.ecu import EcuState, EcuResponse
from scapy.contrib.automotive.scanner.staged_test_case import StagedAutomotiveTestCase
from scapy.contrib.automotive.uds import UDS, UDS_RDBIPR, UDS_NR
from scapy.packet import Raw

+ Basic checks

= Definition of Test classes


class MyTestCase1(AutomotiveTestCase):
    _description = "MyTestCase1"
    @property
    def supported_responses(self):
        return [EcuResponse(EcuState(session=2),            responses=UDS() / UDS_RDBIPR(dataIdentifier=2) / Raw(b"de")),
                EcuResponse([EcuState(session=2), EcuState(security_level=6)],            responses=UDS() / UDS_RDBIPR(dataIdentifier=3) / Raw(b"dea2")),
                EcuResponse(EcuState(session=range(0,255)), responses=UDS() / UDS_NR(negativeResponseCode=0x7f, requestServiceId=0x13))]


class MyTestCase2(AutomotiveTestCase):
    _description = "MyTestCase2"
    @property
    def supported_responses(self):
        return [EcuResponse(EcuState(session=2),            responses=UDS() / UDS_RDBIPR(dataIdentifier=5) / Raw(b"deadbeef1")),
                EcuResponse(EcuState(session=2),            responses=UDS() / UDS_RDBIPR(dataIdentifier=6) / Raw(b"deadbeef2")),
                EcuResponse(EcuState(session=range(0,255)), responses=UDS() / UDS_NR(negativeResponseCode=0x10, requestServiceId=0x11))]


= Create instance of stage test

tc1 = MyTestCase1()
tc2 = MyTestCase2()

mt = StagedAutomotiveTestCase([tc1, tc2])

assert len(mt.test_cases) == 2
assert mt.current_test_case == tc1
assert mt.current_connector == None
assert mt.previous_test_case == None
assert mt[0] == tc1
assert mt[1] == tc2

= Check completion

tc1 = MyTestCase1()
tc2 = MyTestCase2()

mt = StagedAutomotiveTestCase([tc1, tc2])

tc1._state_completed[EcuState(session=1)] = False
tc2._state_completed[EcuState(session=1)] = False

assert not mt.completed
assert not mt.has_completed(EcuState(session=1))

tc1._state_completed[EcuState(session=1)] = True
assert mt.current_test_case == tc1
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert mt.current_test_case == tc2
assert not mt.completed

tc2._state_completed[EcuState(session=1)] = True
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert mt.completed
assert mt.has_completed(EcuState(session=1))

= Check completion 2

tc1 = MyTestCase1()
tc2 = MyTestCase2()

mt = StagedAutomotiveTestCase([tc1, tc2])

tc1._state_completed[EcuState(session=1)] = False
tc2._state_completed[EcuState(session=1)] = False

assert not mt.completed
assert not mt.has_completed(EcuState(session=1))

tc1._state_completed[EcuState(session=1)] = True
assert mt.current_test_case == tc1
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
tc1._state_completed[EcuState(session=1)] = False
assert not mt.has_completed(EcuState(session=1))
tc1._state_completed[EcuState(session=1)] = True
assert mt.current_test_case == tc1
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))

assert mt.current_test_case == tc2
assert not mt.completed

tc2._state_completed[EcuState(session=1)] = True
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert mt.completed
assert mt.has_completed(EcuState(session=1))

= Check supported responses

tc1 = MyTestCase1()
tc2 = MyTestCase2()
tx = StagedAutomotiveTestCase([tc1, tc2])
resps = tx.supported_responses

assert len(resps) == 6
assert resps[0].responses[0].service != 0x7f
assert resps[1].responses[0].service != 0x7f
assert resps[2].responses[0].service != 0x7f
assert resps[3].responses[0].service != 0x7f

assert resps[4].responses[0].service == 0x7f
assert resps[5].responses[0].service == 0x7f

assert resps[0].responses[0].load == b"dea2"
assert resps[1].responses[0].load == b"deadbeef1"
assert resps[2].responses[0].load == b"deadbeef2"
assert resps[3].responses[0].load == b"de"
assert resps[4].responses[0].requestServiceId == 0x13
assert resps[5].responses[0].requestServiceId == 0x11


= Check connector

test_storage_tc2 = None

class MyTestCase2(AutomotiveTestCase):
    _description = "MyTestCase2"
    def pre_execute(self, socket, state, global_configuration):
        global test_storage_tc2
        print(global_configuration)
        test_storage_tc2 = global_configuration
    def supported_responses(self):
        return []

test_storage_tc3 = None

class MyTestCase3(AutomotiveTestCase):
    _description = "MyTestCase3"
    def pre_execute(self, socket, state, global_configuration):
        global test_storage_tc3
        print(global_configuration)
        test_storage_tc3 = global_configuration
    def supported_responses(self):
        return []

def con1(tc1, tc2):
    assert isinstance(tc1, MyTestCase1)
    assert isinstance(tc2, MyTestCase2)
    return {"tc2_con_config": 42}

def con2(tc2, tc3):
    assert isinstance(tc2, MyTestCase2)
    assert isinstance(tc3, MyTestCase3)
    return {"tc3_con_config": "deadbeef"}

tc1 = MyTestCase1()
tc2 = MyTestCase2()
tc3 = MyTestCase3()

assert test_storage_tc2 is None
assert test_storage_tc3 is None

mt = StagedAutomotiveTestCase([tc1, tc2, tc3], [None, con1, con2])

assert mt.current_test_case == tc1
assert mt.current_connector == None

#Move stage forward
tc1._state_completed[EcuState(session=1)] = True
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))

assert mt.current_test_case == tc2
assert mt.current_connector == con1

mt.pre_execute(None, None, {"MyTestCase2": {"verbose": True, "config": "whatever"}})

assert test_storage_tc2["MyTestCase2"]["verbose"]
assert test_storage_tc2["MyTestCase2"]["tc2_con_config"] == 42
assert test_storage_tc2["MyTestCase2"]["config"] == "whatever"

#Move stage forward
tc2._state_completed[EcuState(session=1)] = True
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))
assert not mt.has_completed(EcuState(session=1))

assert mt.current_test_case == tc3
assert mt.current_connector == con2

mt.pre_execute(None, None, {})

assert test_storage_tc3["MyTestCase3"]["tc3_con_config"] == "deadbeef"

= Check show

dump = mt.show(dump=True)

assert "MyTestCase1" in dump
assert "MyTestCase2" in dump
assert "MyTestCase3" in dump

= Check len

assert len(mt) == 3

= Check generator functions

assert mt.get_generated_test_case() == None
assert mt.get_new_edge(None, None) == None
assert mt.get_transition_function(None, None) == None





