| 1 |
% Regression tests for enumerators |
| 2 |
~ linux |
| 3 |
|
| 4 |
+ Load general modules |
| 5 |
|
| 6 |
= Load contribution layer |
| 7 |
|
| 8 |
from scapy.contrib.automotive.scanner.enumerator import _AutomotiveTestCaseScanResult, ServiceEnumerator, StateGenerator, StateGeneratingServiceEnumerator |
| 9 |
from scapy.contrib.automotive.scanner.test_case import TestCaseGenerator, AutomotiveTestCase |
| 10 |
from scapy.contrib.automotive.scanner.executor import AutomotiveTestCaseExecutor |
| 11 |
from scapy.contrib.isotp import ISOTP |
| 12 |
from scapy.contrib.automotive.uds import * |
| 13 |
from scapy.contrib.automotive.scanner.staged_test_case import StagedAutomotiveTestCase |
| 14 |
from scapy.utils import SingleConversationSocket |
| 15 |
from scapy.contrib.automotive.ecu import EcuState, EcuResponse |
| 16 |
from scapy.contrib.automotive.uds_ecu_states import * |
| 17 |
import copy |
| 18 |
|
| 19 |
+ Basic checks |
| 20 |
= ServiceEnumerator basecls checks |
| 21 |
|
| 22 |
pkts = [ |
| 23 |
_AutomotiveTestCaseScanResult(EcuState(session=1), UDS(b"\x20abcd"), UDS(b"\x60abcd"), 1.0, 1.9), |
| 24 |
_AutomotiveTestCaseScanResult(EcuState(session=2), UDS(b"\x20abcd"), None, 2.0, None), |
| 25 |
_AutomotiveTestCaseScanResult(EcuState(session=1), UDS(b"\x21abcd"), UDS(b"\x7fabcd"), 3.0, 3.1), |
| 26 |
_AutomotiveTestCaseScanResult(EcuState(session=2), UDS(b"\x21abcd"), UDS(b"\x7fa\x10cd"), 4.0, 4.5), |
| 27 |
] |
| 28 |
|
| 29 |
class MyTestCase(ServiceEnumerator): |
| 30 |
_supported_kwargs = copy.copy(ServiceEnumerator._supported_kwargs) |
| 31 |
_supported_kwargs.update({ |
| 32 |
'local_kwarg': ((int, str), None), |
| 33 |
'verbose': (bool, None), |
| 34 |
'global_arg': (str, None) |
| 35 |
}) |
| 36 |
def _get_initial_requests(self, **kwargs): |
| 37 |
# type: (Any) -> Iterable[Packet] |
| 38 |
return UDS(service=range(1, 11)) |
| 39 |
def _get_table_entry_y(self, tup): |
| 40 |
# type: (_AutomotiveTestCaseScanResult) -> str |
| 41 |
return "0x%02x: %s" % (tup[1].service, tup[1].sprintf("%UDS.service%")) |
| 42 |
def _get_table_entry_z(self, tup): |
| 43 |
# type: (_AutomotiveTestCaseScanResult) -> str |
| 44 |
return self._get_label(tup[2], "PR: Supported") |
| 45 |
@staticmethod |
| 46 |
def _get_negative_response_label(response): |
| 47 |
# type: (Packet) -> str |
| 48 |
return response.sprintf("NR: %UDS_NR.negativeResponseCode%") |
| 49 |
@staticmethod |
| 50 |
def _get_negative_response_code(resp): |
| 51 |
# type: (Packet) -> int |
| 52 |
return resp.negativeResponseCode |
| 53 |
@staticmethod |
| 54 |
def _get_negative_response_desc(nrc): |
| 55 |
# type: (int) -> str |
| 56 |
return UDS_NR(negativeResponseCode=nrc).sprintf( |
| 57 |
"%UDS_NR.negativeResponseCode%") |
| 58 |
|
| 59 |
|
| 60 |
e = MyTestCase() |
| 61 |
for p in pkts: |
| 62 |
p.req.time = p.req_ts |
| 63 |
p.req.sent_time = p.req_ts |
| 64 |
if p.resp is not None: |
| 65 |
p.resp.time = p.resp_ts |
| 66 |
e._store_result(p.state, p.req, p.resp) |
| 67 |
|
| 68 |
|
| 69 |
= ServiceEnumerator not completed check |
| 70 |
|
| 71 |
assert e.completed == False |
| 72 |
|
| 73 |
= ServiceEnumerator completed |
| 74 |
|
| 75 |
e._state_completed[EcuState(session=1)] = True |
| 76 |
e._state_completed[EcuState(session=2)] = True |
| 77 |
|
| 78 |
assert e.completed |
| 79 |
|
| 80 |
= ServiceEnumerator stats check |
| 81 |
|
| 82 |
stat_list = e._compute_statistics() |
| 83 |
|
| 84 |
stats = {label: value for state, label, value in stat_list if state == "all"} |
| 85 |
print(stats) |
| 86 |
|
| 87 |
assert stats["num_answered"] == '3' |
| 88 |
assert stats["num_unanswered"] == '1' |
| 89 |
assert stats["answertime_max"] == '0.9' |
| 90 |
assert stats["answertime_min"] == '0.1' |
| 91 |
assert stats["answertime_avg"] == '0.5' |
| 92 |
assert stats["num_negative_resps"] == '2' |
| 93 |
|
| 94 |
= ServiceEnumerator scanned states |
| 95 |
|
| 96 |
assert len(e.scanned_states) == 2 |
| 97 |
assert {EcuState(session=1), EcuState(session=2)} == e.scanned_states |
| 98 |
|
| 99 |
= ServiceEnumerator scanned results |
| 100 |
|
| 101 |
assert len(e.results_with_positive_response) == 1 |
| 102 |
assert len(e.results_with_negative_response) == 2 |
| 103 |
assert len(e.results_without_response) == 1 |
| 104 |
assert len(e.results_with_response) == 3 |
| 105 |
|
| 106 |
= ServiceEnumerator get_label |
| 107 |
assert e._get_label(pkts[0].resp) == "PR: PositiveResponse" |
| 108 |
assert e._get_label(pkts[0].resp, lambda _: "positive") == "positive" |
| 109 |
assert e._get_label(pkts[0].resp, lambda _: "positive" + hex(pkts[0].req.service)) == "positive" + "0x20" |
| 110 |
assert e._get_label(pkts[1].resp) == "Timeout" |
| 111 |
assert e._get_label(pkts[2].resp) == "NR: 98" |
| 112 |
assert e._get_label(pkts[3].resp) == "NR: generalReject" |
| 113 |
|
| 114 |
= ServiceEnumerator show |
| 115 |
|
| 116 |
e.show(filtered=False) |
| 117 |
|
| 118 |
dump = e.show(dump=True, filtered=False) |
| 119 |
assert "NR: 98" in dump |
| 120 |
assert "NR: generalReject" in dump |
| 121 |
assert "PR: Supported" in dump |
| 122 |
assert "Timeout" in dump |
| 123 |
assert "session1" in dump |
| 124 |
assert "session2" in dump |
| 125 |
assert "0x20" in dump |
| 126 |
assert "0x21" in dump |
| 127 |
|
| 128 |
= ServiceEnumerator filtered results before show |
| 129 |
|
| 130 |
print(len(e.filtered_results)) |
| 131 |
assert len(e.filtered_results) == 2 |
| 132 |
assert e.filtered_results[0] == pkts[0] |
| 133 |
assert e.filtered_results[1] == pkts[2] |
| 134 |
|
| 135 |
= ServiceEnumerator show filtered |
| 136 |
|
| 137 |
e.show(filtered=True) |
| 138 |
|
| 139 |
dump = e.show(dump=True, filtered=True) |
| 140 |
assert "NR: 98" in dump |
| 141 |
assert "NR: generalReject" in dump |
| 142 |
assert "PR: Supported" in dump |
| 143 |
assert "Timeout" not in dump |
| 144 |
assert "session1" in dump |
| 145 |
assert "session2" in dump |
| 146 |
assert "all" in dump |
| 147 |
assert "0x20" in dump |
| 148 |
assert "0x21" in dump |
| 149 |
assert "The following negative response codes are blacklisted: ['serviceNotSupported']" in dump |
| 150 |
|
| 151 |
= ServiceEnumerator filtered results after show |
| 152 |
|
| 153 |
assert len(e.filtered_results) == 3 |
| 154 |
assert e.filtered_results[0] == pkts[0] |
| 155 |
assert e.filtered_results[1] == pkts[2] |
| 156 |
|
| 157 |
= ServiceEnumerator supported responses |
| 158 |
|
| 159 |
assert len(e.supported_responses) == 3 |
| 160 |
|
| 161 |
= ServiceEnumerator evaluate response |
| 162 |
|
| 163 |
conf = {} |
| 164 |
|
| 165 |
assert False == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), None, **conf) |
| 166 |
assert False == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), UDS(b"\x7f\x10\x10"), **conf) |
| 167 |
conf = {"exit_if_service_not_supported": True, "retry_if_busy_returncode": False} |
| 168 |
assert False == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), UDS(b"\x7f\x10\x21"), **conf) |
| 169 |
assert False == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), UDS(b"\x7f\x10\x10"), **conf) |
| 170 |
assert True == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), UDS(b"\x7f\x10\x11"), **conf) |
| 171 |
assert True == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), UDS(b"\x7f\x10\x7f"), **conf) |
| 172 |
conf = {"exit_if_service_not_supported": False, "retry_if_busy_returncode": True} |
| 173 |
assert not e._retry_pkt[EcuState(session=1)] |
| 174 |
assert False == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), UDS(b"\x7f\x10\x10"), **conf) |
| 175 |
assert False == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), UDS(b"\x7f\x10\x11"), **conf) |
| 176 |
assert False == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), UDS(b"\x7f\x10\x7f"), **conf) |
| 177 |
assert not e._retry_pkt[EcuState(session=1)] |
| 178 |
assert True == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), UDS(b"\x7f\x10\x21"), **conf) |
| 179 |
assert e._retry_pkt[EcuState(session=1)] == UDS(b"\x10\x03abcd") |
| 180 |
assert False == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), UDS(b"\x7f\x10\x21"), **conf) |
| 181 |
assert not e._retry_pkt[EcuState(session=1)] |
| 182 |
|
| 183 |
assert True == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), UDS(b"\x50\x03\x00"), **conf) |
| 184 |
assert False == e._evaluate_response(EcuState(session=1), UDS(b"\x11\x03abcd"), UDS(b"\x51\x03\x00"), **conf) |
| 185 |
conf = {"retry_if_none_received": True} |
| 186 |
assert True == e._evaluate_response(EcuState(session=1), UDS(b"\x10\x03abcd"), None, **conf) |
| 187 |
assert e._retry_pkt[EcuState(session=1)] |
| 188 |
|
| 189 |
|
| 190 |
= ServiceEnumerator execute |
| 191 |
|
| 192 |
from queue import Queue |
| 193 |
from scapy.supersocket import SuperSocket |
| 194 |
|
| 195 |
class MockISOTPSocket(SuperSocket): |
| 196 |
nonblocking_socket = True |
| 197 |
@property |
| 198 |
def closed(self): |
| 199 |
return False |
| 200 |
@closed.setter |
| 201 |
def closed(self, var): |
| 202 |
pass |
| 203 |
def __init__(self, rcvd_queue=None): |
| 204 |
self.rcvd_queue = Queue() |
| 205 |
self.sent_queue = Queue() |
| 206 |
if rcvd_queue is not None: |
| 207 |
for c in rcvd_queue: |
| 208 |
self.rcvd_queue.put(c) |
| 209 |
def recv_raw(self, x=MTU): |
| 210 |
pkt = bytes(self.rcvd_queue.get(True, 0.01)) |
| 211 |
return UDS, pkt, 10.0 |
| 212 |
def send(self, x): |
| 213 |
sx = raw(x) |
| 214 |
try: |
| 215 |
x.sent_time = 9.0 |
| 216 |
except AttributeError: |
| 217 |
pass |
| 218 |
self.sent_queue.put(sx) |
| 219 |
return len(sx) |
| 220 |
@staticmethod |
| 221 |
def select(sockets, remain=None): |
| 222 |
time.sleep(0) |
| 223 |
return sockets |
| 224 |
def sr(self, *args, **kargs): |
| 225 |
from scapy import sendrecv |
| 226 |
return sendrecv.sndrcv(self, *args, threaded=False, **kargs) |
| 227 |
def sr1(self, *args, **kargs): |
| 228 |
from scapy import sendrecv |
| 229 |
ans = sendrecv.sndrcv(self, *args, threaded=False, **kargs)[0] # type: SndRcvList |
| 230 |
if len(ans) > 0: |
| 231 |
pkt = ans[0][1] # type: Packet |
| 232 |
return pkt |
| 233 |
else: |
| 234 |
return None |
| 235 |
|
| 236 |
sock = MockISOTPSocket() |
| 237 |
sock.rcvd_queue.put(b"\x41") |
| 238 |
sock.rcvd_queue.put(b"\x42") |
| 239 |
sock.rcvd_queue.put(b"\x43") |
| 240 |
sock.rcvd_queue.put(b"\x44") |
| 241 |
sock.rcvd_queue.put(b"\x45") |
| 242 |
sock.rcvd_queue.put(b"\x46") |
| 243 |
sock.rcvd_queue.put(b"\x47") |
| 244 |
sock.rcvd_queue.put(b"\x48") |
| 245 |
sock.rcvd_queue.put(b"\x49") |
| 246 |
sock.rcvd_queue.put(b"\x4A") |
| 247 |
|
| 248 |
e = MyTestCase() |
| 249 |
|
| 250 |
e.execute(sock, EcuState(session=1)) |
| 251 |
|
| 252 |
assert len(e.filtered_results) == 10 |
| 253 |
assert len(e.results_with_response) == 10 |
| 254 |
assert len(e.results_without_response) == 0 |
| 255 |
|
| 256 |
assert e.has_completed(EcuState(session=1)) |
| 257 |
assert e.completed |
| 258 |
|
| 259 |
e.execute(sock, EcuState(session=2), timeout=0.01) |
| 260 |
|
| 261 |
assert len(e.filtered_results) == 10 |
| 262 |
assert len(e.results_with_response) == 10 |
| 263 |
assert len(e.results_without_response) == 10 |
| 264 |
|
| 265 |
assert e.has_completed(EcuState(session=2)) |
| 266 |
|
| 267 |
e.execute(sock, EcuState(session=3), timeout=0.01, exit_if_no_answer_received=True) |
| 268 |
|
| 269 |
assert not e.has_completed(EcuState(session=3)) |
| 270 |
assert not e.completed |
| 271 |
assert len(e.scanned_states) == 3 |
| 272 |
|
| 273 |
e.execute(sock, EcuState(session=42), state_block_list=[EcuState(session=42)]) |
| 274 |
|
| 275 |
assert e.has_completed(EcuState(session=42)) |
| 276 |
assert len(e.scanned_states) == 3 |
| 277 |
|
| 278 |
e.execute(sock, EcuState(session=13), state_block_list=EcuState(session=13)) |
| 279 |
|
| 280 |
assert e.has_completed(EcuState(session=13)) |
| 281 |
assert len(e.scanned_states) == 3 |
| 282 |
|
| 283 |
e.execute(sock, EcuState(session=41), state_allow_list=[EcuState(session=42)]) |
| 284 |
|
| 285 |
assert e.has_completed(EcuState(session=41)) |
| 286 |
assert len(e.scanned_states) == 3 |
| 287 |
|
| 288 |
e.execute(sock, EcuState(session=12), state_allow_list=EcuState(session=13)) |
| 289 |
|
| 290 |
assert e.has_completed(EcuState(session=12)) |
| 291 |
assert len(e.scanned_states) == 3 |
| 292 |
|
| 293 |
= Test negative response code service not supported |
| 294 |
|
| 295 |
sock.rcvd_queue.put(b"\x7f\x01\x11") |
| 296 |
sock.rcvd_queue.put(b"\x7f\x01\x7f") |
| 297 |
|
| 298 |
e = MyTestCase() |
| 299 |
|
| 300 |
e.execute(sock, EcuState(session=1), exit_if_service_not_supported=True) |
| 301 |
|
| 302 |
assert not e._retry_pkt[EcuState(session=1)] |
| 303 |
assert len(e.results_with_response) == 1 |
| 304 |
assert len(e.results_with_negative_response) == 1 |
| 305 |
assert e.completed |
| 306 |
|
| 307 |
e.execute(sock, EcuState(session=2), exit_if_service_not_supported=True) |
| 308 |
|
| 309 |
assert not e._retry_pkt[EcuState(session=2)] |
| 310 |
assert len(e.results_with_response) == 2 |
| 311 |
assert len(e.results_with_negative_response) == 2 |
| 312 |
assert e.completed |
| 313 |
|
| 314 |
= Test negative response code retry if busy |
| 315 |
|
| 316 |
sock.rcvd_queue.put(b"\x7f\x01\x21") |
| 317 |
sock.rcvd_queue.put(b"\x7f\x01\x10") |
| 318 |
|
| 319 |
e = MyTestCase() |
| 320 |
|
| 321 |
e.execute(sock, EcuState(session=1)) |
| 322 |
|
| 323 |
assert e._retry_pkt[EcuState(session=1)] |
| 324 |
assert len(e.results_with_response) == 1 |
| 325 |
assert len(e.results_with_negative_response) == 1 |
| 326 |
assert len(e.results_without_response) == 0 |
| 327 |
assert not e.completed |
| 328 |
|
| 329 |
e.execute(sock, EcuState(session=1)) |
| 330 |
|
| 331 |
assert not e._retry_pkt[EcuState(session=1)] |
| 332 |
assert len(e.results_with_response) == 2 |
| 333 |
assert len(e.results_with_negative_response) == 2 |
| 334 |
assert len(e.results_without_response) == 9 |
| 335 |
assert e.completed |
| 336 |
assert e.has_completed(EcuState(session=1)) |
| 337 |
|
| 338 |
= Test negative response code don't retry if busy |
| 339 |
|
| 340 |
sock.rcvd_queue.put(b"\x7f\x01\x21") |
| 341 |
|
| 342 |
e = MyTestCase() |
| 343 |
|
| 344 |
e.execute(sock, EcuState(session=1), retry_if_busy_returncode=False) |
| 345 |
|
| 346 |
assert not e._retry_pkt[EcuState(session=1)] |
| 347 |
assert len(e.results_with_response) == 1 |
| 348 |
assert len(e.results_with_negative_response) == 1 |
| 349 |
assert len(e.results_without_response) == 9 |
| 350 |
assert e.completed |
| 351 |
assert e.has_completed(EcuState(session=1)) |
| 352 |
|
| 353 |
= Test execution time |
| 354 |
|
| 355 |
sock.rcvd_queue.put(b"\x7f\x01\x10") |
| 356 |
|
| 357 |
e = MyTestCase() |
| 358 |
|
| 359 |
e.execute(sock, EcuState(session=1), execution_time=-1) |
| 360 |
|
| 361 |
assert not e._retry_pkt[EcuState(session=1)] |
| 362 |
assert len(e.results_with_response) == 1 |
| 363 |
assert len(e.results_with_negative_response) == 1 |
| 364 |
assert len(e.results_without_response) == 0 |
| 365 |
assert not e.completed |
| 366 |
assert not e.has_completed(EcuState(session=1)) |
| 367 |
|
| 368 |
|
| 369 |
+ AutomotiveTestCaseExecutorConfiguration tests |
| 370 |
|
| 371 |
= Definitions |
| 372 |
|
| 373 |
class MockSock(object): |
| 374 |
closed = False |
| 375 |
def sr1(self, *args, **kwargs): |
| 376 |
raise OSError |
| 377 |
|
| 378 |
class TestCase1(MyTestCase): |
| 379 |
pass |
| 380 |
|
| 381 |
class TestCase2(MyTestCase): |
| 382 |
pass |
| 383 |
|
| 384 |
class Scanner(AutomotiveTestCaseExecutor): |
| 385 |
@property |
| 386 |
def default_test_case_clss(self): |
| 387 |
# type: () -> List[Type[AutomotiveTestCaseABC]] |
| 388 |
return [MyTestCase] |
| 389 |
|
| 390 |
= Basic tests |
| 391 |
|
| 392 |
tce = Scanner(MockSock(), test_cases=[TestCase1, TestCase2, MyTestCase], |
| 393 |
verbose=True, debug=True, |
| 394 |
global_arg="Whatever", TestCase1_kwargs={"local_kwarg": 42}) |
| 395 |
|
| 396 |
config = tce.configuration # type: AutomotiveTestCaseExecutorConfiguration |
| 397 |
assert config.verbose |
| 398 |
assert config.debug |
| 399 |
assert len(config.test_cases) == 3 |
| 400 |
assert len(config.stages) == 0 |
| 401 |
assert len(config.staged_test_cases) == 0 |
| 402 |
assert len(config.test_case_clss) == 3 |
| 403 |
assert len(config.TestCase1.items()) == 5 |
| 404 |
assert len(config.TestCase2.items()) == 4 |
| 405 |
assert len(config["TestCase1"].items()) == 5 |
| 406 |
assert len(config.MyTestCase.items()) == 4 |
| 407 |
assert config.TestCase1["verbose"] |
| 408 |
assert config.TestCase1["debug"] |
| 409 |
assert config.TestCase1["local_kwarg"] == 42 |
| 410 |
assert config.TestCase1["global_arg"] == "Whatever" |
| 411 |
assert config.TestCase2["global_arg"] == "Whatever" |
| 412 |
assert config.MyTestCase["global_arg"] == "Whatever" |
| 413 |
assert isinstance(tce.socket, SingleConversationSocket) |
| 414 |
|
| 415 |
|
| 416 |
= Basic tests with default values |
| 417 |
|
| 418 |
tce = Scanner(MockSock()) |
| 419 |
|
| 420 |
config = tce.configuration # type: AutomotiveTestCaseExecutorConfiguration |
| 421 |
assert not config.verbose |
| 422 |
assert not config.debug |
| 423 |
assert len(config.test_cases) == 1 |
| 424 |
assert len(config.MyTestCase.items()) == 1 |
| 425 |
assert isinstance(tce.socket, SingleConversationSocket) |
| 426 |
|
| 427 |
|
| 428 |
= Basic test with stages |
| 429 |
|
| 430 |
def connector(testcase1, _): |
| 431 |
scan_range = len(testcase1.results) |
| 432 |
return {"verbose": True, "scan_range": scan_range} |
| 433 |
|
| 434 |
tc1 = TestCase1() |
| 435 |
tc2 = TestCase2() |
| 436 |
|
| 437 |
pipeline = StagedAutomotiveTestCase([tc1, tc2], [None, connector]) |
| 438 |
|
| 439 |
tce = Scanner(MockSock(), test_cases=[pipeline]) |
| 440 |
|
| 441 |
config = tce.configuration # type: AutomotiveTestCaseExecutorConfiguration |
| 442 |
assert not config.verbose |
| 443 |
assert not config.debug |
| 444 |
assert len(config.test_cases) == 1 |
| 445 |
assert len(config.stages) == 1 |
| 446 |
assert len(config.staged_test_cases) == 2 |
| 447 |
assert len(config.test_case_clss) == 3 |
| 448 |
assert len(config.StagedAutomotiveTestCase.items()) == 1 |
| 449 |
assert isinstance(tce.socket, SingleConversationSocket) |
| 450 |
|
| 451 |
= Basic tests with two stages |
| 452 |
|
| 453 |
def connector(testcase1, testcase2): |
| 454 |
scan_range = len(testcase1.results) |
| 455 |
return {"verbose": True, "scan_range": scan_range} |
| 456 |
|
| 457 |
tc1 = TestCase1() |
| 458 |
tc2 = TestCase2() |
| 459 |
|
| 460 |
pipeline = StagedAutomotiveTestCase([tc1, tc2], [None, connector]) |
| 461 |
|
| 462 |
class StagedTest(StagedAutomotiveTestCase): |
| 463 |
pass |
| 464 |
|
| 465 |
pipeline2 = StagedTest([MyTestCase(), MyTestCase()]) |
| 466 |
|
| 467 |
tce = Scanner(MockSock(), test_cases=[pipeline, pipeline2], verbose=True) |
| 468 |
|
| 469 |
config = tce.configuration # type: AutomotiveTestCaseExecutorConfiguration |
| 470 |
assert config.verbose |
| 471 |
assert not config.debug |
| 472 |
assert len(config.test_cases) == 2 |
| 473 |
assert len(config.stages) == 2 |
| 474 |
assert len(config.staged_test_cases) == 4 |
| 475 |
assert len(config.test_case_clss) == 5 |
| 476 |
assert len(config.StagedAutomotiveTestCase.items()) == 2 |
| 477 |
assert len(config.StagedTest.items()) == 2 |
| 478 |
assert len(config.TestCase1.items()) == 2 |
| 479 |
assert len(config.TestCase2.items()) == 2 |
| 480 |
assert len(config.MyTestCase.items()) == 2 |
| 481 |
|
| 482 |
assert isinstance(tce.socket, SingleConversationSocket) |
| 483 |
|
| 484 |
assert len(tce.state_paths) == 1 |
| 485 |
assert len(tce.final_states) == 1 |
| 486 |
|
| 487 |
tce.state_graph.add_edge((tce.final_states[0], EcuState(session=2))) |
| 488 |
|
| 489 |
assert len(tce.state_paths) == 2 |
| 490 |
assert len(tce.final_states) == 2 |
| 491 |
|
| 492 |
assert not tce.scan_completed |
| 493 |
|
| 494 |
|
| 495 |
= Reset Handler tests |
| 496 |
|
| 497 |
reset_flag = False |
| 498 |
|
| 499 |
def reset_func(): |
| 500 |
global reset_flag |
| 501 |
reset_flag = True |
| 502 |
|
| 503 |
tce = Scanner(MockSock(), reset_handler=reset_func) |
| 504 |
tce.target_state = EcuState(session=2) |
| 505 |
tce.reset_target() |
| 506 |
|
| 507 |
assert reset_flag |
| 508 |
assert tce.target_state == EcuState(session=1) |
| 509 |
|
| 510 |
= Reset Handler tests 2 |
| 511 |
|
| 512 |
tce = Scanner(MockSock()) |
| 513 |
tce.target_state = EcuState(session=2) |
| 514 |
tce.reset_target() |
| 515 |
|
| 516 |
assert tce.target_state == EcuState(session=1) |
| 517 |
|
| 518 |
= Reconnect Handler tests |
| 519 |
|
| 520 |
class MockSocket2: |
| 521 |
closed = False |
| 522 |
|
| 523 |
def reconnect_func(): |
| 524 |
return MockSocket2() |
| 525 |
|
| 526 |
tce = Scanner(MockSock(), reconnect_handler=reconnect_func) |
| 527 |
|
| 528 |
print(tce.socket) |
| 529 |
print(repr(tce.socket)) |
| 530 |
assert isinstance(tce.socket._inner, MockSock) |
| 531 |
tce.reconnect() |
| 532 |
assert isinstance(tce.socket._inner, MockSocket2) |
| 533 |
|
| 534 |
= Reconnect Handler tests 2 |
| 535 |
|
| 536 |
closed = False |
| 537 |
|
| 538 |
class MockSocket1: |
| 539 |
closed = False |
| 540 |
def close(self): |
| 541 |
global closed |
| 542 |
closed = True |
| 543 |
|
| 544 |
class MockSocket2: |
| 545 |
closed = False |
| 546 |
|
| 547 |
def reconnect_func(): |
| 548 |
return MockSocket2() |
| 549 |
|
| 550 |
tce = Scanner(MockSocket1(), reconnect_handler=reconnect_func) |
| 551 |
|
| 552 |
print(tce.socket) |
| 553 |
print(repr(tce.socket)) |
| 554 |
assert isinstance(tce.socket._inner, MockSocket1) |
| 555 |
tce.reconnect() |
| 556 |
assert isinstance(tce.socket._inner, MockSocket2) |
| 557 |
assert closed |
| 558 |
|
| 559 |
= TestCase execute |
| 560 |
|
| 561 |
pre_exec = False |
| 562 |
execute = False |
| 563 |
post_exec = False |
| 564 |
|
| 565 |
class TestCase42(MyTestCase): |
| 566 |
def pre_execute(self, |
| 567 |
socket, # type: _SocketUnion |
| 568 |
state, # type: EcuState |
| 569 |
global_configuration # type: AutomotiveTestCaseExecutorConfiguration # noqa: E501 |
| 570 |
): # type: (...) -> None |
| 571 |
global pre_exec |
| 572 |
assert state == EcuState(session=1) |
| 573 |
assert global_configuration.TestCase42["local_kwarg"] == 42 |
| 574 |
assert global_configuration.TestCase42["verbose"] |
| 575 |
assert global_configuration.TestCase42["debug"] |
| 576 |
global_configuration.TestCase42["local_kwarg"] = 1 |
| 577 |
pre_exec = True |
| 578 |
def execute(self, socket, state, local_kwarg, verbose, debug, **kwargs): |
| 579 |
global execute |
| 580 |
assert verbose |
| 581 |
assert debug |
| 582 |
assert local_kwarg == 1 |
| 583 |
execute = True |
| 584 |
def post_execute(self, |
| 585 |
socket, # type: _SocketUnion |
| 586 |
state, # type: EcuState |
| 587 |
global_configuration # type: AutomotiveTestCaseExecutorConfiguration # noqa: E501 |
| 588 |
): # type: (...) -> None |
| 589 |
global post_exec |
| 590 |
assert global_configuration.TestCase42["local_kwarg"] == 1 |
| 591 |
assert global_configuration.TestCase42["verbose"] |
| 592 |
assert global_configuration.TestCase42["debug"] |
| 593 |
post_exec = True |
| 594 |
|
| 595 |
|
| 596 |
tce = Scanner(MockSock(), test_cases=[TestCase42], |
| 597 |
verbose=True, debug=True, |
| 598 |
TestCase42_kwargs={"local_kwarg": 42}) |
| 599 |
|
| 600 |
tce.execute_test_case(TestCase42()) |
| 601 |
assert pre_exec == execute == post_exec == True |
| 602 |
|
| 603 |
|
| 604 |
= TestCase execute StateGenerator |
| 605 |
|
| 606 |
transition_done = False |
| 607 |
|
| 608 |
def transition_func(sock, conf, kwargs): |
| 609 |
assert kwargs["arg42"] == "hello" |
| 610 |
assert conf.TestCase43["local_kwarg"] == "world" |
| 611 |
global transition_done |
| 612 |
transition_done = True |
| 613 |
return True |
| 614 |
|
| 615 |
class TestCase43(MyTestCase, StateGenerator): |
| 616 |
def get_new_edge(self, socket, config): |
| 617 |
assert config.TestCase43["local_kwarg"] == "world" |
| 618 |
return EcuState(session=1), EcuState(session=2) |
| 619 |
def get_transition_function(self, socket, edge): |
| 620 |
assert edge[0] == EcuState(session=1) |
| 621 |
assert edge[1] == EcuState(session=2) |
| 622 |
return transition_func, {"arg42": "hello"}, None |
| 623 |
def execute(self, socket, state, **kwargs): |
| 624 |
return True |
| 625 |
|
| 626 |
|
| 627 |
tce = Scanner(MockSock(), test_cases=[TestCase43], |
| 628 |
TestCase43_kwargs={"local_kwarg": "world"}) |
| 629 |
|
| 630 |
assert len(tce.final_states) == 1 |
| 631 |
|
| 632 |
tce.execute_test_case(TestCase43()) |
| 633 |
|
| 634 |
assert len(tce.final_states) == 2 |
| 635 |
assert EcuState(session=1) in tce.final_states and EcuState(session=2) in tce.final_states |
| 636 |
assert tce.enter_state(EcuState(session=1), EcuState(session=2)) |
| 637 |
assert transition_done |
| 638 |
|
| 639 |
|
| 640 |
= TestCase execute StateGenerator no edge |
| 641 |
|
| 642 |
class TestCase43(MyTestCase, StateGenerator): |
| 643 |
def get_new_edge(self, socket, config): |
| 644 |
assert config.TestCase43["local_kwarg"] == "world" |
| 645 |
return None |
| 646 |
def execute(self, socket, state, **kwargs): |
| 647 |
return True |
| 648 |
def get_transition_function(self, socket, edge): |
| 649 |
raise NotImplementedError() |
| 650 |
|
| 651 |
tce = Scanner(MockSock(), test_cases=[TestCase43], |
| 652 |
TestCase43_kwargs={"local_kwarg": "world"}) |
| 653 |
|
| 654 |
assert len(tce.final_states) == 1 |
| 655 |
|
| 656 |
tce.execute_test_case(TestCase43()) |
| 657 |
|
| 658 |
assert len(tce.final_states) == 1 |
| 659 |
assert EcuState(session=1) in tce.final_states |
| 660 |
assert not tce.enter_state(EcuState(session=1), EcuState(session=2)) |
| 661 |
|
| 662 |
|
| 663 |
= TestCase execute StateGenerator with cleanupfunc |
| 664 |
|
| 665 |
transition_done = False |
| 666 |
cleanup_done = False |
| 667 |
|
| 668 |
def transition_func(sock, conf, kwargs): |
| 669 |
assert kwargs["arg42"] == "hello" |
| 670 |
assert conf.TestCase43["local_kwarg"] == "world" |
| 671 |
global transition_done |
| 672 |
transition_done = True |
| 673 |
return True |
| 674 |
|
| 675 |
def cleanup_func(sock, conf): |
| 676 |
assert conf.TestCase43["local_kwarg"] == "world" |
| 677 |
global cleanup_done |
| 678 |
cleanup_done = True |
| 679 |
return True |
| 680 |
|
| 681 |
class TestCase43(MyTestCase, StateGenerator): |
| 682 |
def get_new_edge(self, socket, config): |
| 683 |
assert config.TestCase43["local_kwarg"] == "world" |
| 684 |
return EcuState(session=1), EcuState(session=2) |
| 685 |
def get_transition_function(self, socket, edge): |
| 686 |
assert edge[0] == EcuState(session=1) |
| 687 |
assert edge[1] == EcuState(session=2) |
| 688 |
return transition_func, {"arg42": "hello"}, cleanup_func |
| 689 |
def execute(self, socket, state, **kwargs): |
| 690 |
return True |
| 691 |
|
| 692 |
|
| 693 |
tce = Scanner(MockSock(), test_cases=[TestCase43], |
| 694 |
TestCase43_kwargs={"local_kwarg": "world"}) |
| 695 |
|
| 696 |
assert len(tce.final_states) == 1 |
| 697 |
|
| 698 |
tce.execute_test_case(TestCase43()) |
| 699 |
|
| 700 |
assert len(tce.final_states) == 2 |
| 701 |
assert EcuState(session=1) in tce.final_states and EcuState(session=2) in tce.final_states |
| 702 |
assert not len(tce.cleanup_functions) |
| 703 |
assert tce.enter_state(EcuState(session=1), EcuState(session=2)) |
| 704 |
assert transition_done |
| 705 |
assert len(tce.cleanup_functions) |
| 706 |
tce.cleanup_state() |
| 707 |
assert not len(tce.cleanup_functions) |
| 708 |
assert cleanup_done |
| 709 |
|
| 710 |
|
| 711 |
= TestCase execute StateGenerator with not callable cleanupfunc |
| 712 |
|
| 713 |
transition_done = False |
| 714 |
|
| 715 |
def transition_func(sock, conf, kwargs): |
| 716 |
assert kwargs["arg42"] == "hello" |
| 717 |
assert conf.TestCase43["local_kwarg"] == "world" |
| 718 |
global transition_done |
| 719 |
transition_done = True |
| 720 |
return True |
| 721 |
|
| 722 |
class TestCase43(MyTestCase, StateGenerator): |
| 723 |
def get_new_edge(self, socket, config): |
| 724 |
assert config.TestCase43["local_kwarg"] == "world" |
| 725 |
return EcuState(session=1), EcuState(session=2) |
| 726 |
def get_transition_function(self, socket, edge): |
| 727 |
assert edge[0] == EcuState(session=1) |
| 728 |
assert edge[1] == EcuState(session=2) |
| 729 |
return transition_func, {"arg42": "hello"}, "fake" |
| 730 |
def execute(self, socket, state, **kwargs): |
| 731 |
return True |
| 732 |
|
| 733 |
|
| 734 |
tce = Scanner(MockSock(), test_cases=[TestCase43], |
| 735 |
TestCase43_kwargs={"local_kwarg": "world"}) |
| 736 |
|
| 737 |
assert len(tce.final_states) == 1 |
| 738 |
|
| 739 |
tce.execute_test_case(TestCase43()) |
| 740 |
|
| 741 |
assert len(tce.final_states) == 2 |
| 742 |
assert EcuState(session=1) in tce.final_states and EcuState(session=2) in tce.final_states |
| 743 |
assert not len(tce.cleanup_functions) |
| 744 |
assert tce.enter_state(EcuState(session=1), EcuState(session=2)) |
| 745 |
assert transition_done |
| 746 |
assert len(tce.cleanup_functions) |
| 747 |
tce.cleanup_state() |
| 748 |
assert not len(tce.cleanup_functions) |
| 749 |
|
| 750 |
= TestCase execute StateGenerator with cleanupfunc negative return |
| 751 |
|
| 752 |
transition_done = False |
| 753 |
cleanup_done = False |
| 754 |
|
| 755 |
def transition_func(sock, conf, kwargs): |
| 756 |
assert kwargs["arg42"] == "hello" |
| 757 |
assert conf.TestCase43["local_kwarg"] == "world" |
| 758 |
global transition_done |
| 759 |
transition_done = True |
| 760 |
return True |
| 761 |
|
| 762 |
def cleanup_func(sock, conf): |
| 763 |
assert conf.TestCase43["local_kwarg"] == "world" |
| 764 |
global cleanup_done |
| 765 |
cleanup_done = True |
| 766 |
return False |
| 767 |
|
| 768 |
class TestCase43(MyTestCase, StateGenerator): |
| 769 |
def get_new_edge(self, socket, config): |
| 770 |
assert config.TestCase43["local_kwarg"] == "world" |
| 771 |
return EcuState(session=1), EcuState(session=2) |
| 772 |
def get_transition_function(self, socket, edge): |
| 773 |
assert edge[0] == EcuState(session=1) |
| 774 |
assert edge[1] == EcuState(session=2) |
| 775 |
return transition_func, {"arg42": "hello"}, cleanup_func |
| 776 |
def execute(self, socket, state, **kwargs): |
| 777 |
return True |
| 778 |
|
| 779 |
|
| 780 |
tce = Scanner(MockSock(), test_cases=[TestCase43], |
| 781 |
TestCase43_kwargs={"local_kwarg": "world"}) |
| 782 |
|
| 783 |
assert len(tce.final_states) == 1 |
| 784 |
|
| 785 |
tce.execute_test_case(TestCase43()) |
| 786 |
|
| 787 |
assert len(tce.final_states) == 2 |
| 788 |
assert EcuState(session=1) in tce.final_states and EcuState(session=2) in tce.final_states |
| 789 |
assert not len(tce.cleanup_functions) |
| 790 |
assert tce.enter_state(EcuState(session=1), EcuState(session=2)) |
| 791 |
assert transition_done |
| 792 |
assert len(tce.cleanup_functions) |
| 793 |
tce.cleanup_state() |
| 794 |
assert not len(tce.cleanup_functions) |
| 795 |
assert cleanup_done |
| 796 |
|
| 797 |
|
| 798 |
= TestCase execute StateGenerator with cleanupfunc and path |
| 799 |
|
| 800 |
transition_done1 = False |
| 801 |
cleanup_done1 = False |
| 802 |
transition_done2 = False |
| 803 |
cleanup_done2 = False |
| 804 |
|
| 805 |
transition_error = False |
| 806 |
|
| 807 |
|
| 808 |
def transition_func1(sock, conf, kwargs): |
| 809 |
global transition_done1 |
| 810 |
transition_done1 = True |
| 811 |
return True |
| 812 |
|
| 813 |
def cleanup_func1(sock, conf): |
| 814 |
global cleanup_done1 |
| 815 |
cleanup_done1 = True |
| 816 |
return True |
| 817 |
|
| 818 |
def transition_func2(sock, conf, kwargs): |
| 819 |
global transition_done2 |
| 820 |
transition_done2 = True |
| 821 |
return not transition_error |
| 822 |
|
| 823 |
def cleanup_func2(sock, conf): |
| 824 |
global cleanup_done2 |
| 825 |
cleanup_done2 = True |
| 826 |
return True |
| 827 |
|
| 828 |
class TestCase43(MyTestCase, StateGenerator): |
| 829 |
def get_new_edge(self, socket, config): |
| 830 |
return EcuState(session=1), EcuState(session=2) |
| 831 |
def get_transition_function(self, socket, edge): |
| 832 |
return transition_func1, {"arg42": "hello"}, cleanup_func1 |
| 833 |
def execute(self, socket, state, **kwargs): |
| 834 |
return True |
| 835 |
|
| 836 |
class TestCase44(MyTestCase, StateGenerator): |
| 837 |
def get_new_edge(self, socket, config): |
| 838 |
return EcuState(session=2), EcuState(session=3) |
| 839 |
def get_transition_function(self, socket, edge): |
| 840 |
return transition_func2, None, cleanup_func2 |
| 841 |
def execute(self, socket, state, **kwargs): |
| 842 |
return True |
| 843 |
|
| 844 |
reset_done = False |
| 845 |
|
| 846 |
def reset_func(): |
| 847 |
global reset_done |
| 848 |
reset_done = True |
| 849 |
|
| 850 |
reconnect_done = False |
| 851 |
|
| 852 |
def reconnect_func(): |
| 853 |
global reconnect_done |
| 854 |
reconnect_done = True |
| 855 |
return MockSock() |
| 856 |
|
| 857 |
tce = Scanner(MockSock(), test_cases=[TestCase43, TestCase44], |
| 858 |
reset_handler=reset_func, reconnect_handler=reconnect_func) |
| 859 |
|
| 860 |
assert len(tce.final_states) == 1 |
| 861 |
|
| 862 |
tce.execute_test_case(TestCase43()) |
| 863 |
|
| 864 |
assert len(tce.final_states) == 2 |
| 865 |
assert EcuState(session=1) in tce.final_states and EcuState(session=2) in tce.final_states |
| 866 |
tce.execute_test_case(TestCase44()) |
| 867 |
assert len(tce.final_states) == 3 |
| 868 |
assert EcuState(session=3) in tce.final_states and EcuState(session=2) in tce.final_states |
| 869 |
|
| 870 |
assert not len(tce.cleanup_functions) |
| 871 |
assert tce.enter_state_path([EcuState(session=1), EcuState(session=2), EcuState(session=3)]) |
| 872 |
assert transition_done1 |
| 873 |
assert transition_done2 |
| 874 |
assert len(tce.cleanup_functions) == 2 |
| 875 |
assert reconnect_done |
| 876 |
assert reset_done |
| 877 |
tce.cleanup_state() |
| 878 |
assert cleanup_done1 |
| 879 |
assert cleanup_done2 |
| 880 |
|
| 881 |
try: |
| 882 |
tce.enter_state_path([EcuState(session=3)]) |
| 883 |
assert False |
| 884 |
except Scapy_Exception: |
| 885 |
assert True |
| 886 |
|
| 887 |
= Test downrate edge |
| 888 |
|
| 889 |
transition_done1 = False |
| 890 |
cleanup_done1 = False |
| 891 |
|
| 892 |
tce = Scanner(MockSock(), test_cases=[TestCase43, TestCase44], |
| 893 |
reset_handler=reset_func, reconnect_handler=reconnect_func) |
| 894 |
|
| 895 |
assert len(tce.final_states) == 1 |
| 896 |
tce.execute_test_case(TestCase43()) |
| 897 |
assert len(tce.final_states) == 2 |
| 898 |
assert EcuState(session=1) in tce.final_states and EcuState(session=2) in tce.final_states |
| 899 |
tce.execute_test_case(TestCase44()) |
| 900 |
assert len(tce.final_states) == 3 |
| 901 |
assert EcuState(session=3) in tce.final_states and EcuState(session=2) in tce.final_states |
| 902 |
|
| 903 |
assert not len(tce.cleanup_functions) |
| 904 |
transition_error = True |
| 905 |
assert not tce.enter_state_path([EcuState(session=1), EcuState(session=2), EcuState(session=3)]) |
| 906 |
assert transition_done1 |
| 907 |
assert cleanup_done1 |
| 908 |
assert len(tce.cleanup_functions) == 0 |
| 909 |
assert tce.state_graph.weights[(EcuState(session=1), EcuState(session=2))] == 1 |
| 910 |
assert tce.state_graph.weights[(EcuState(session=2), EcuState(session=3))] == 2 |
| 911 |
|
| 912 |
|
| 913 |
= TestCase execute TestCaseGenerator |
| 914 |
|
| 915 |
tc_executed = False |
| 916 |
|
| 917 |
class GeneratedTestCase(MyTestCase): |
| 918 |
def execute(self, socket, state, **kwargs): |
| 919 |
assert kwargs["local_kwarg"] == "world" |
| 920 |
global tc_executed |
| 921 |
tc_executed = True |
| 922 |
return True |
| 923 |
|
| 924 |
|
| 925 |
class TestCase43(MyTestCase, TestCaseGenerator): |
| 926 |
def execute(self, socket, state, **kwargs): |
| 927 |
return True |
| 928 |
def get_generated_test_case(self): |
| 929 |
return GeneratedTestCase() |
| 930 |
|
| 931 |
|
| 932 |
tce = Scanner(MockSock(), test_cases=[TestCase43], |
| 933 |
GeneratedTestCase_kwargs={"local_kwarg": "world"}) |
| 934 |
|
| 935 |
assert len(tce.final_states) == 1 |
| 936 |
assert len(tce.configuration.test_cases) == 1 |
| 937 |
|
| 938 |
tce.execute_test_case(tce.configuration.test_cases[0]) |
| 939 |
|
| 940 |
assert len(tce.configuration.test_cases) == 2 |
| 941 |
|
| 942 |
tce.execute_test_case(tce.configuration.test_cases[1]) |
| 943 |
|
| 944 |
assert tc_executed |
| 945 |
|
| 946 |
= TestCase scan timeout |
| 947 |
|
| 948 |
tc_executed = False |
| 949 |
|
| 950 |
class GeneratedTestCase(MyTestCase): |
| 951 |
def execute(self, socket, state, **kwargs): |
| 952 |
assert kwargs["local_kwarg"] == "world" |
| 953 |
global tc_executed |
| 954 |
tc_executed = True |
| 955 |
return True |
| 956 |
|
| 957 |
|
| 958 |
tce = Scanner(MockSock(), test_cases=[GeneratedTestCase], |
| 959 |
GeneratedTestCase_kwargs={"local_kwarg": "world"}) |
| 960 |
|
| 961 |
assert len(tce.final_states) == 1 |
| 962 |
assert len(tce.configuration.test_cases) == 1 |
| 963 |
|
| 964 |
tce.scan(-1) |
| 965 |
|
| 966 |
assert not tc_executed |
| 967 |
|
| 968 |
|
| 969 |
= TestCase scan |
| 970 |
|
| 971 |
tc_executed = False |
| 972 |
|
| 973 |
class GeneratedTestCase(MyTestCase): |
| 974 |
def execute(self, socket, state, **kwargs): |
| 975 |
assert kwargs["local_kwarg"] == "world" |
| 976 |
global tc_executed |
| 977 |
tc_executed = True |
| 978 |
self._state_completed[state] = True |
| 979 |
return True |
| 980 |
|
| 981 |
|
| 982 |
class TestCase43(MyTestCase, TestCaseGenerator): |
| 983 |
def execute(self, socket, state, **kwargs): |
| 984 |
self._state_completed[state] = True |
| 985 |
return True |
| 986 |
def get_generated_test_case(self): |
| 987 |
return GeneratedTestCase() |
| 988 |
|
| 989 |
|
| 990 |
tce = Scanner(MockSock(), test_cases=[TestCase43], |
| 991 |
GeneratedTestCase_kwargs={"local_kwarg": "world"}) |
| 992 |
|
| 993 |
assert len(tce.final_states) == 1 |
| 994 |
assert len(tce.configuration.test_cases) == 1 |
| 995 |
|
| 996 |
tce.scan() |
| 997 |
|
| 998 |
assert len(tce.configuration.test_cases) == 2 |
| 999 |
assert tc_executed |
| 1000 |
assert tce.scan_completed |
| 1001 |
|
| 1002 |
= Test supported responses |
| 1003 |
|
| 1004 |
class MyTestCase1(AutomotiveTestCase): |
| 1005 |
_description = "MyTestCase1" |
| 1006 |
_supported_kwargs = copy.copy(AutomotiveTestCase._supported_kwargs) |
| 1007 |
_supported_kwargs.update({ |
| 1008 |
'stop_event': (threading.Event, None), # type: ignore |
| 1009 |
}) |
| 1010 |
@property |
| 1011 |
def supported_responses(self): |
| 1012 |
return [EcuResponse(EcuState(session=2), responses=UDS() / UDS_RDBIPR(dataIdentifier=2) / Raw(b"de")), |
| 1013 |
EcuResponse([EcuState(session=2), EcuState(security_level=6)], responses=UDS() / UDS_RDBIPR(dataIdentifier=3) / Raw(b"dea2")), |
| 1014 |
EcuResponse(EcuState(session=range(0,255)), responses=UDS() / UDS_NR(negativeResponseCode=0x7f, requestServiceId=0x13))] |
| 1015 |
|
| 1016 |
|
| 1017 |
class MyTestCase2(AutomotiveTestCase): |
| 1018 |
_description = "MyTestCase2" |
| 1019 |
_supported_kwargs = copy.copy(AutomotiveTestCase._supported_kwargs) |
| 1020 |
_supported_kwargs.update({ |
| 1021 |
'stop_event': (threading.Event, None), # type: ignore |
| 1022 |
}) |
| 1023 |
@property |
| 1024 |
def supported_responses(self): |
| 1025 |
return [EcuResponse(EcuState(session=2), responses=UDS() / UDS_RDBIPR(dataIdentifier=5) / Raw(b"deadbeef1")), |
| 1026 |
EcuResponse(EcuState(session=2), responses=UDS() / UDS_RDBIPR(dataIdentifier=6) / Raw(b"deadbeef2")), |
| 1027 |
EcuResponse(EcuState(session=range(0,255)), responses=UDS() / UDS_NR(negativeResponseCode=0x10, requestServiceId=0x11))] |
| 1028 |
|
| 1029 |
|
| 1030 |
tce = Scanner(MockSock(), test_cases=[MyTestCase1(), MyTestCase2()]) |
| 1031 |
|
| 1032 |
resps = tce.supported_responses |
| 1033 |
|
| 1034 |
assert len(resps) == 6 |
| 1035 |
assert resps[0].responses[0].service != 0x7f |
| 1036 |
assert resps[1].responses[0].service != 0x7f |
| 1037 |
assert resps[2].responses[0].service != 0x7f |
| 1038 |
assert resps[3].responses[0].service != 0x7f |
| 1039 |
|
| 1040 |
assert resps[4].responses[0].service == 0x7f |
| 1041 |
assert resps[5].responses[0].service == 0x7f |
| 1042 |
|
| 1043 |
assert resps[0].responses[0].load == b"dea2" |
| 1044 |
assert resps[1].responses[0].load == b"deadbeef1" |
| 1045 |
assert resps[2].responses[0].load == b"deadbeef2" |
| 1046 |
assert resps[3].responses[0].load == b"de" |
| 1047 |
assert resps[4].responses[0].requestServiceId == 0x13 |
| 1048 |
assert resps[5].responses[0].requestServiceId == 0x11 |
| 1049 |
|
| 1050 |
= Test show testcases |
| 1051 |
|
| 1052 |
try: |
| 1053 |
tce.show_testcases() |
| 1054 |
assert True |
| 1055 |
except Exception: |
| 1056 |
assert False |
| 1057 |
|
| 1058 |
|
| 1059 |
try: |
| 1060 |
tce.show_testcases_status() |
| 1061 |
assert True |
| 1062 |
except Exception: |
| 1063 |
assert False |
| 1064 |
|
| 1065 |
= Test StateGeneratingServiceEnumerator |
| 1066 |
|
| 1067 |
class TestCase43(MyTestCase, StateGeneratingServiceEnumerator): |
| 1068 |
def execute(self, socket, state, **kwargs): |
| 1069 |
return True |
| 1070 |
@property |
| 1071 |
def results(self): # type: () -> List[_AutomotiveTestCaseScanResult] |
| 1072 |
return [_AutomotiveTestCaseScanResult(EcuState(session=1), UDS()/UDS_DSC(b"\x03"), UDS()/UDS_DSCPR(b"\x03"), 1.1, 1.2)] |
| 1073 |
|
| 1074 |
tce = Scanner(MockSock(), test_cases=[TestCase43]) |
| 1075 |
|
| 1076 |
assert len(tce.final_states) == 1 |
| 1077 |
|
| 1078 |
tce.execute_test_case(TestCase43()) |
| 1079 |
|
| 1080 |
assert len(tce.final_states) == 2 |
| 1081 |
assert EcuState(session=1) in tce.final_states and EcuState(session=3) in tce.final_states |
| 1082 |
|
| 1083 |
tf, args, cf = tce.state_graph.get_transition_tuple_for_edge((EcuState(session=1), EcuState(session=3))) |
| 1084 |
|
| 1085 |
assert cf is None |
| 1086 |
assert tf is not None |
| 1087 |
assert len(args) == 2 |
| 1088 |
assert args["req"] == UDS()/UDS_DSC(b"\x03") |
| 1089 |
assert "diagnosticSessionType" in args["desc"] and "extendedDiagnosticSession" in args["desc"] |
| 1090 |
|
| 1091 |
assert not tce.enter_state(EcuState(session=1), EcuState(session=3)) |