Add message integrity test (#766)

* msg integrity test

* check that test works, corrupt address

* Revert "check that test works, corrupt address"
This commit is contained in:
Igor Biletskyy 2021-11-05 13:12:49 -07:00 committed by GitHub
parent 0a98c38b6f
commit 4d57e48fd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import time
import random
import threading
from panda import Panda
from collections import defaultdict
from nose.tools import assert_equal, assert_less, assert_greater
from .helpers import panda_jungle, start_heartbeat_thread, reset_pandas, time_many_sends, test_all_pandas, test_all_gen2_pandas, clear_can_buffers, panda_connect_and_init
@ -250,3 +251,45 @@ def test_bulk_write(p):
# Set back to silent mode
p.set_safety_mode(Panda.SAFETY_SILENT)
@test_all_pandas
@panda_connect_and_init
def test_message_integrity(p):
start_heartbeat_thread(p)
clear_can_buffers(p)
p.set_safety_mode(Panda.SAFETY_ALLOUTPUT)
p.set_power_save(False)
p.set_can_loopback(True)
n = 250
for i in range(n):
sent_msgs = defaultdict(set)
for _ in range(random.randrange(10)):
to_send = []
for __ in range(random.randrange(100)):
bus = random.randrange(3)
addr = random.randrange(1, 1<<29)
dat = bytes([random.getrandbits(8) for _ in range(random.randrange(1, 9))])
sent_msgs[bus].add((addr, dat))
to_send.append([addr, None, dat, bus])
p.can_send_many(to_send, timeout=0)
start_time = time.time()
while time.time() - start_time < 2 and any(len(sent_msgs[bus]) for bus in range(3)):
recvd = p.can_recv()
for msg in recvd:
if msg[3] >= 128:
k = (msg[0], bytes(msg[2]))
assert k in sent_msgs[msg[3]-128], f"message {k} was never sent on bus {bus}"
sent_msgs[msg[3]-128].discard(k)
# if a set isn't empty, messages got dropped
for bus in range(3):
assert not len(sent_msgs[bus]), f"loop {i}: bus {bus} missing {len(sent_msgs[bus])} messages"
# Set back to silent mode
p.set_safety_mode(Panda.SAFETY_SILENT)
print("Got all messages intact")