Files
MicrOBU/obu-firmware/test/bench/bench_conformance.py
T
Ashin Walpola 462e1898ff Run the overnight bench test on the spare board, and document it
Track the bench scripts (run_overnight.py and its stages, the link and ITS
client libraries, the phone-vs-node comparison) with the first reports.

Changes for the spare XIAO (no cell):
- run_overnight.py picks the single Espressif board by default and refuses to
  test a build that is not this checkout's HEAD.
- The unsigned soak stops as "transmit path wedged" after 40 resource_limit
  refusals in a row and tries whether a repeated STATION_CONFIGURE clears it.
- "Refused requests put nothing on the radio" counts only own transmissions;
  frames heard from other stations are copied by RAW_CAPTURE too.
- start_overnight.ps1: optional build and flash, host C tests in the
  background, Windows kept awake. setup_workstation.ps1 checks and installs
  git, Python 3.11, pyserial, asn1tools, MSYS2 gcc and make, ESP-IDF 6.0.2.
- obu-firmware/docs/overnight-bench-test.tex (and PDF) describe the test, the
  workstation set-up and the findings of the 2026-11-25 runs.
2026-11-25 14:43:34 +01:00

301 lines
19 KiB
Python

"""Stage 1: does the station answer the link protocol the way docs/station-link.tex and main/link_service.cpp say?
Every expectation below is taken from the spec text or from the firmware source (cited in the check's name), not
from observed behaviour. What the source leaves open is reported as INFO with the observed value.
The first half runs on a freshly reset, unconfigured station, so nothing in it can reach the radio. The second half
configures it (security 0, low power) and, unless --no-tx, puts a few frames on air whose payload no ITS receiver
accepts as a message (see linklib.test_payload).
"""
import random
import struct
import time
import itslib
import linklib as ll
from bench_common import FAIL, INFO, PASS, SKIP, WARN
S = "conformance"
def expect_code(ctx, name, reply, code):
got = reply.name if reply else "no reply"
return ctx.res.ok(S, name, reply is not None and reply.code == code, "expected %s, got %s" % (ll.CODES[code], got))
def run(ctx):
L, R = ctx.link, ctx.res
ctx.phase = S
L.reboot()
s0 = L.wait_status(20)
if not R.ok(S, "station answers STATUS_REQUEST after a reset", s0 is not None, "no CONFIRM within 20 s"):
return
R.ok(S, "STATUS detail is the 131-octet snapshot (spec: Status confirmation payload)", s0["raw_len"] == 131, "got %d octets" % s0["raw_len"])
R.ok(S, "a reset station is unconfigured", s0["configured"] == 0)
R.ok(S, "uptime restarted at the reset", s0["uptime_ms"] < 30000, "uptime %d ms" % s0["uptime_ms"])
time.sleep(1.5)
s1 = L.status()
if s1:
d = (s1["uptime_ms"] - s0["uptime_ms"]) / 1000.0
R.ok(S, "STATUS uptime runs at real time", 1.0 < d < 3.5, "%.2f s of uptime over ~2 s" % d)
R.metric(S, "heap_free_after_boot", s1["heap_free"])
R.metric(S, "heap_largest_after_boot", s1["heap_largest"])
R.add(S, "battery reading with no cell", INFO, "battery_mv %d (0 = not measured, no shutdown on it)" % s1["battery_mv"])
crc0 = s1["link_crc_errors"] if s1 else 0
# ---- unknown and malformed (no station needed)
for op in (0x00, 0x40, 0x7E, 0x80, 0x85, 0x90, 0xFF):
expect_code(ctx, "opcode 0x%02x is answered unknown_opcode (link_service.cpp default)" % op, L.request(op, b""), ll.UNKNOWN_OPCODE)
expect_code(ctx, "STATUS_REQUEST with a body is malformed", L.request(ll.STATUS_REQUEST, b"\x00"), ll.MALFORMED)
for n in (0, 1, 5, 7, 15, 17):
expect_code(ctx, "STATION_CONFIGURE of %d octets is malformed (spec: 6 or 16)" % n, L.request(ll.STATION_CONFIGURE, bytes(n)), ll.MALFORMED)
expect_code(ctx, "STATION_CONFIGURE security=2 is malformed", L.request(ll.STATION_CONFIGURE, struct.pack("<BHBBB", 2, 180, 5, 2, 0)), ll.MALFORMED)
expect_code(ctx, "STATION_CONFIGURE beaconing=2 is malformed", L.request(ll.STATION_CONFIGURE, struct.pack("<BHBBB", 2, 180, 5, 0, 2)), ll.MALFORMED)
expect_code(ctx, "16-octet STATION_CONFIGURE radio=3 is malformed",
L.request(ll.STATION_CONFIGURE, ll.configure_body(mac=ctx.next_mac(), radio=3)), ll.MALFORMED)
expect_code(ctx, "POTI_UPDATE of 5 octets is malformed", L.request(ll.POTI_UPDATE, bytes(5)), ll.MALFORMED)
expect_code(ctx, "RAW_CAPTURE with a 2-octet body is malformed", L.request(ll.RAW_CAPTURE, b"\x01\x01"), ll.MALFORMED)
expect_code(ctx, "RAW_CAPTURE mode with a high nibble is invalid_argument (raw_capture_policy.hpp)", L.request(ll.RAW_CAPTURE, b"\xF0"), ll.INVALID_ARGUMENT)
expect_code(ctx, "RAW_CAPTURE on before STATION_CONFIGURE is not_configured", L.raw_capture(1), ll.NOT_CONFIGURED)
valid = ll.btp_body(ll.test_payload(ctx.run_id, 0, 43))
expect_code(ctx, "BTP_DATA_REQUEST before STATION_CONFIGURE is not_configured", L.request(ll.BTP_DATA_REQUEST, valid), ll.NOT_CONFIGURED)
expect_code(ctx, "BTP_DATA_REQUEST btp_type=2 is malformed", L.request(ll.BTP_DATA_REQUEST, ll.btp_body(b"x" * 30, btp_type=2)), ll.MALFORMED)
expect_code(ctx, "BTP_DATA_REQUEST with 32 permission octets is malformed", L.request(ll.BTP_DATA_REQUEST, ll.btp_body(b"x" * 30, permissions=bytes(32))), ll.MALFORMED)
expect_code(ctx, "BTP_DATA_REQUEST with 3 stray trailing octets is malformed", L.request(ll.BTP_DATA_REQUEST, valid + b"abc"), ll.MALFORMED)
expect_code(ctx, "BTP_DATA_REQUEST cut short is malformed", L.request(ll.BTP_DATA_REQUEST, valid[:-1]), ll.MALFORMED)
expect_code(ctx, "BTP_DATA_REQUEST with the optional generated_at_ms decodes (then not_configured)",
L.request(ll.BTP_DATA_REQUEST, ll.btp_body(b"x" * 30, generated_at_ms=ll.timestamp_its())), ll.NOT_CONFIGURED)
# ---- sequence numbers
for seq in (65534, 65535, 0, 1):
r = L.request(ll.STATUS_REQUEST, b"", seq=seq)
R.ok(S, "CONFIRM echoes sequence %d (counters wrap 65535 -> 0)" % seq, r is not None and r.code == 0, "no CONFIRM" if r is None else "")
# ---- request fragmentation (spec: Fragmented messages; request_reassembly.hpp). Unconfigured: reaching
# not_configured proves the whole body was joined and decoded, and nothing can go on air.
full = ll.btp_body(ll.test_payload(ctx.run_id, 1, ll.max_btp_payload()))
R.ok(S, "test request is exactly 1536 octets (the request maximum)", len(full) == ll.REQUEST_MAX, "%d" % len(full))
for cap in (240, 504):
expect_code(ctx, "1536-octet request in fragments of %d octets is joined and decoded (-> not_configured)" % cap,
L.request_fragmented(ll.BTP_DATA_REQUEST, full, 3.0, cap=cap), ll.NOT_CONFIGURED)
r = _expect_reply(L, 4242, lambda: L.write(ll.frame(ll.LINK, ll.link_message(ll.BTP_DATA_REQUEST, 4242, valid + bytes(560)))), 1.0)
R.add(S, "an unfragmented link message over 512 octets", INFO, "reply: %s (the spec says to fragment; link_service.cpp drops it before dispatch)" % (r.name if r else "none"))
over = ll.btp_body(b"y" * 500)
seq = L.next_seq()
reply = _expect_reply(L, seq, lambda: L.send_fragmented(ll.BTP_DATA_REQUEST, over, seq=seq, cap=240, total_override=1537), 2.0)
expect_code(ctx, "fragmented request declaring 1537 octets is resource_limit", reply, ll.RESOURCE_LIMIT)
seq = L.next_seq()
reply = _expect_reply(L, seq, lambda: _gap(L, full, seq), 1.2)
R.ok(S, "a fragment that leaves a gap is dropped without a reply", reply is None, "got %s" % (reply.name if reply else ""))
seq = L.next_seq()
reply = _expect_reply(L, seq, lambda: _expire(L, full, seq), 1.0)
R.ok(S, "an unfinished request is discarded after the 2 s idle timeout (its late last fragment gets no reply)", reply is None,
"got %s" % (reply.name if reply else ""))
got = _nine_streams(ctx, L)
R.ok(S, "at most 8 incomplete requests are held (spec: pending maximum 8)", got == 8, "%d of 9 interleaved requests were answered" % got)
# ---- the USB framing
s_before = L.status()
L.write(ll.frame(ll.LINK, ll.link_message(ll.STATUS_REQUEST, 7), bad_crc=True))
time.sleep(0.4)
s_after = L.status()
if s_before and s_after:
R.ok(S, "a frame with a wrong CRC is counted in link_crc_errors and ignored",
s_after["link_crc_errors"] == s_before["link_crc_errors"] + 1,
"%d -> %d" % (s_before["link_crc_errors"], s_after["link_crc_errors"]))
L.write(bytes(random.Random(5).choice([b for b in range(256) if b != 0xAA]) for _ in range(60)))
R.ok(S, "a valid frame after 60 octets of noise is answered (resync)", L.status() is not None)
L.write(b"\xAA\xAA" + ll.frame(ll.LINK, ll.link_message(ll.STATUS_REQUEST, 8)))
time.sleep(0.3)
R.ok(S, "a valid frame after a doubled 0xAA is answered", L.status() is not None)
for n in (1537, 4000, 65535):
L.write(b"\xAA\x55\x10" + struct.pack("<H", n)) # an untrustworthy length: the decoder resyncs at once
R.ok(S, "a valid frame after a header declaring %d payload octets is answered (resync)" % n, L.status() is not None)
L.write(b"\xAA\x55\x10\x0a\x00" + b"\x01\x02\x03\x04" + b"\x00" * 8) # cut short, then padded to its declared length
time.sleep(0.3)
R.ok(S, "a frame cut short and padded out is dropped and the next valid frame is answered", L.status() is not None)
L.write(ll.frame(0x42, b"unknown frame type"))
time.sleep(0.2)
R.ok(S, "a frame of unknown type does not disturb the link", L.status() is not None)
# ---- the half that configures the station
r = L.configure(secured=False, power=5)
if not expect_code(ctx, "STATION_CONFIGURE (6 octets, unsecured, 5 dBm) is accepted", r, ll.ACCEPTED):
return
R.ok(S, "STATION_CONFIGURE detail is 18 octets (spec: gn_address, identifier, credentials_loaded, tickets)", len(r.detail) == 18, "%d" % len(r.detail))
expect_code(ctx, "RAW_CAPTURE mode 0x05 (on + own TX) after configure is accepted", L.raw_capture(5), ll.ACCEPTED)
expect_code(ctx, "BTP_DATA_REQUEST before the first POTI_UPDATE is rejected (no ITS time, no fix)",
L.request(ll.BTP_DATA_REQUEST, valid), ll.REJECTED)
expect_code(ctx, "POTI_UPDATE with latitude 91 degrees is invalid_argument",
L.request(ll.POTI_UPDATE, ll.poti_body(lat=910000000)), ll.INVALID_ARGUMENT)
p0 = L.status()
reply = _expect_reply(L, 0, lambda: L.send(ll.POTI_UPDATE, ll.poti_body(), seq=0), 0.5)
R.ok(S, "an accepted POTI_UPDATE is silent (spec: errors return CONFIRM)", reply is None, "got %s" % (reply.name if reply else ""))
time.sleep(0.3)
p1 = L.status()
if p0 and p1:
R.ok(S, "an accepted POTI_UPDATE counts in poti_updates", p1["poti_updates"] == p0["poti_updates"] + 1, "%d -> %d" % (p0["poti_updates"], p1["poti_updates"]))
r = L.request(ll.POTI_UPDATE, ll.poti_body(offset_ms=-8000))
R.add(S, "POTI_UPDATE 8 s behind the station clock", INFO, "reply: %s" % (r.name if r else "none (accepted silently)"))
L.poti()
# ---- unsecured transmission
L.raw_frames.clear()
sent = []
if ctx.tx_enabled:
for i, size in enumerate((24, 43, 300)):
payload = ll.test_payload(ctx.run_id, 100 + i, size)
r = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(payload))
expect_code(ctx, "unsecured BTP_DATA_REQUEST of %d octets is accepted" % size, r, ll.ACCEPTED)
sent.append(payload)
time.sleep(0.15)
time.sleep(1.0)
got = [itslib.unwrap(f) for _, fl, _, f in L.raw_frames if fl & 1]
got = [g for g in got if g]
seen = {g["payload"] for g in got}
R.ok(S, "every accepted request leaves the radio, byte for byte (own-TX RAW_FRAME copy, unwrapped independently)",
all(p in seen for p in sent), "%d of %d payloads captured" % (sum(p in seen for p in sent), len(sent)))
R.ok(S, "unsecured frames carry no security envelope", all(not g["signed"] for g in got))
macs = {g["src"] for g in got}
R.add(S, "802.11 source of own unsecured frames", INFO, ", ".join(sorted(macs)))
else:
R.add(S, "on-air checks", SKIP, "--no-tx")
# Only the station's own transmissions count: with RAW_CAPTURE on, frames heard from other stations arrive here too
# (the bench has a V2X node and a simulated car on the air; a quiet room does not).
own_frames = lambda: sum(1 for _, fl, _, _ in list(L.raw_frames) if fl & 1)
n_frames = own_frames()
for name, body, code in (
("transport type 2 (TSB) unsecured is unsupported (station_unsecured: geonet.c builds SHB only)", ll.btp_body(b"z" * 30, transport=2), ll.UNSUPPORTED),
("btp_type 2 is malformed", ll.btp_body(b"z" * 30, btp_type=2), ll.MALFORMED),
):
expect_code(ctx, name, L.request(ll.BTP_DATA_REQUEST, body), code)
time.sleep(0.8)
R.ok(S, "refused requests put nothing on the radio", own_frames() == n_frames, "%d new own frames" % (own_frames() - n_frames))
# ---- secured transmission (configure with security on: the reply says whether the board holds a ticket)
mac2 = ctx.next_mac()
r = L.request(ll.STATION_CONFIGURE, ll.configure_body(secured=True, power=5, mac=mac2), 8.0)
if r and r.code == 0 and len(r.detail) == 18:
ctx.credentials = r.detail[16] == 1 and r.detail[17] > 0
R.add(S, "credentials on this board (configure with security on)", INFO, "credentials_loaded %d, tickets %d" % (r.detail[16], r.detail[17]))
else:
R.add(S, "STATION_CONFIGURE with security on", INFO, "reply: %s" % (r.name if r else "none"))
L.poti()
L.raw_capture(5)
if not ctx.credentials:
n_frames = len(L.raw_frames)
r = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(ll.test_payload(ctx.run_id, 200, 43), secured=True))
R.add(S, "secured request on a board without a ticket", INFO, "reply: %s" % (r.name if r else "none"))
time.sleep(1.5)
leaked = [itslib.unwrap(f) for _, fl, _, f in list(L.raw_frames)[n_frames:] if fl & 1]
R.ok(S, "a secured request without a ticket never goes out unsigned", not any(leaked), "%d frames appeared" % len([g for g in leaked if g]))
st = L.status()
if st:
R.add(S, "counters after the unsignable request", INFO, "refused_no_ticket %d, requests_refused %d, sign_failed %d" %
(st["refused_no_ticket"], st["requests_refused"], st["sign_failed"]))
elif ctx.tx_enabled:
L.raw_frames.clear()
payload = ll.test_payload(ctx.run_id, 201, 100)
r = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(payload, secured=True, generated_at_ms=ll.timestamp_its()))
expect_code(ctx, "signed BTP_DATA_REQUEST is accepted", r, ll.ACCEPTED)
time.sleep(2.0)
got = [itslib.unwrap(f) for _, fl, _, f in L.raw_frames if fl & 1]
got = [g for g in got if g and g["payload"] == payload]
R.ok(S, "the signed request leaves the radio inside a signed envelope (IEEE 1609.2 signedData)", len(got) == 1 and got[0]["signed"],
"%d matching frames, signed=%s" % (len(got), [g["signed"] for g in got]))
R.ok(S, "signed frames use the MAC the phone named", len(got) == 1 and got[0]["src"] == mac2.hex(":"), "%s want %s" % ([g["src"] for g in got], mac2.hex(":")))
L.raw_frames.clear()
stale = ll.test_payload(ctx.run_id, 202, 100)
r = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(stale, secured=True, generated_at_ms=ll.timestamp_its() - 5000))
expect_code(ctx, "a signed request generated 5 s ago is rejected as stale (tx_policy::is_stale)", r, ll.REJECTED)
time.sleep(1.0)
R.ok(S, "a stale request is never transmitted", not any(itslib.unwrap(f) for _, fl, _, f in L.raw_frames if fl & 1))
# How large a signed payload still leaves the radio: Vanetza's itsGnMaxSduSize is 1398 octets; the CONFIRM only says "queued"
for size in (1398, 1399, 1400):
L.poti()
st0 = L.status()
L.raw_frames.clear()
payload = ll.test_payload(ctx.run_id, 210 + size, size)
r = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(payload, secured=True, generated_at_ms=ll.timestamp_its()), 3.0)
time.sleep(1.5)
out = any((itslib.unwrap(f) or {}).get("payload") == payload for _, fl, _, f in L.raw_frames if fl & 1)
st1 = L.status()
R.add(S, "signed payload of %d octets" % size, INFO, "CONFIRM %s, transmitted %s, requests_refused %+d, sign_failed %+d" %
(r.name if r else "none", "yes" if out else "NO", (st1["requests_refused"] - st0["requests_refused"]) if st0 and st1 else 0,
(st1["sign_failed"] - st0["sign_failed"]) if st0 and st1 else 0))
else:
R.add(S, "signed transmission", SKIP, "--no-tx")
# ---- reconfigure with a named MAC
mac = ctx.next_mac()
r = L.request(ll.STATION_CONFIGURE, ll.configure_body(mac=mac, power=5))
expect_code(ctx, "16-octet STATION_CONFIGURE naming a MAC is accepted", r, ll.ACCEPTED)
if r and len(r.detail) == 18:
R.add(S, "GN address after naming MAC %s" % mac.hex(":"), INFO, r.detail[:8].hex(":"))
if ctx.tx_enabled and r and r.code == 0:
L.raw_frames.clear()
L.poti()
r2 = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(ll.test_payload(ctx.run_id, 300, 43)))
time.sleep(1.0)
macs = {itslib.unwrap(f)["src"] for _, fl, _, f in L.raw_frames if fl & 1 and itslib.unwrap(f)}
R.ok(S, "frames use the MAC the phone named (spec: MicrOBU 16-octet STATION_CONFIGURE)", macs == {mac.hex(":")}, "on air: %s" % sorted(macs))
L.raw_capture(0)
end = L.status()
if end:
R.add(S, "after the stage", INFO, "uptime %d ms, heap_free %d, heap_min %d, radio failed %d, resets seen %d" %
(end["uptime_ms"], end["heap_free"], end["heap_min_free"], end["radio_failed"], L.boots))
R.ok(S, "the station did not reset during the stage", end["uptime_ms"] > s0["uptime_ms"])
def _expect_reply(L, seq, action, wait):
"""Run `action` (which sends with this sequence) and wait up to `wait` s for a CONFIRM to it."""
import threading
ev = [threading.Event(), None, time.time()]
with L.lock:
L.pending[seq] = ev
try:
action()
ev[0].wait(wait)
return ev[1]
finally:
with L.lock:
L.pending.pop(seq, None)
def _gap(L, body, seq):
cap = 240
L.write(ll.frame(ll.LINK, ll.link_message(ll.BTP_DATA_REQUEST, seq, struct.pack("<HH", len(body), 0) + body[:cap], 1)))
L.write(ll.frame(ll.LINK, ll.link_message(ll.BTP_DATA_REQUEST, seq, struct.pack("<HH", len(body), 2 * cap) + body[2 * cap:3 * cap], 2)))
L.write(ll.frame(ll.LINK, ll.link_message(ll.BTP_DATA_REQUEST, seq, struct.pack("<HH", len(body), len(body) - 100) + body[-100:], 3)))
def _expire(L, body, seq):
cap = 240
L.write(ll.frame(ll.LINK, ll.link_message(ll.BTP_DATA_REQUEST, seq, struct.pack("<HH", len(body), 0) + body[:cap], 1)))
time.sleep(2.6)
L.write(ll.frame(ll.LINK, ll.link_message(ll.BTP_DATA_REQUEST, seq, struct.pack("<HH", len(body), cap) + body[cap:], 3)))
def _nine_streams(ctx, L):
"""Nine interleaved fragmented requests; the station holds eight. Returns how many were answered."""
import threading
body = ll.btp_body(ll.test_payload(ctx.run_id, 2, 574)) # 600 octets in two fragments of 300
cap = 300
seqs = [L.next_seq() for _ in range(9)]
evs = {}
for s in seqs:
evs[s] = [threading.Event(), None, time.time()]
with L.lock:
L.pending.update(evs)
try:
for s in seqs:
L.write(ll.frame(ll.LINK, ll.link_message(ll.BTP_DATA_REQUEST, s, struct.pack("<HH", len(body), 0) + body[:cap], 1)))
for s in seqs:
L.write(ll.frame(ll.LINK, ll.link_message(ll.BTP_DATA_REQUEST, s, struct.pack("<HH", len(body), cap) + body[cap:], 3)))
time.sleep(1.5)
return sum(1 for s in seqs if evs[s][1] is not None)
finally:
with L.lock:
for s in seqs:
L.pending.pop(s, None)