"""Stage 3: does the station survive garbage on the link? A seeded random generator sends frames the station must refuse or ignore: valid CRC around random link messages, random fragment flags/offsets/totals, truncated and corrupted frames, noise, oversize length fields. After every burst the station must still answer STATUS_REQUEST, must not have reset (uptime keeps growing, no ROM banner, no panic text), and must not have lost heap. Safety rules, so a fuzz run can never put anything on the air or change what the board stores: * it starts from a freshly reset, UNCONFIGURED station, and never sends a STATION_CONFIGURE that decodes (valid bodies are 6 or 16 octets with small field values: every body here is the wrong length or has a field out of range), so the radio never starts and BTP_DATA_REQUEST can only answer not_configured; * it never sends CREDENTIALS_PROVISION / CREDENTIALS_ERASE, the PKI requests (they create keys and write NVS) or the trust bootstrap/update. A failure prints the seed and the last frames sent (hex), so it can be replayed with --seed. """ import collections import random import struct import time import linklib as ll from bench_common import FAIL, INFO, PASS, WARN S = "fuzz" # opcodes the generator may use: harmless without a configured radio, or unknown SAFE_KNOWN = [ll.POTI_UPDATE, ll.BTP_DATA_REQUEST, ll.SF_IDCHANGE_SUBSCRIBE, ll.SF_IDCHANGE_UNSUBSCRIBE, ll.SF_IDCHANGE_EVENT_RESPONSE, ll.SF_IDCHANGE_TRIGGER, ll.SF_ID_LOCK, ll.SF_ID_UNLOCK, ll.STATUS_REQUEST, ll.RAW_CAPTURE, ll.PKI_STATUS] UNKNOWN = [o for o in range(0x00, 0x100) if o not in (0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D) and not 0x0E <= o <= 0x17] # everything PKI is excluded, known or not def bad_configure_body(rnd): """A STATION_CONFIGURE body the station must refuse: wrong length, or a field out of range.""" kind = rnd.randrange(4) if kind == 0: n = rnd.choice([n for n in range(0, 40) if n not in (6, 16)]) return bytes(rnd.getrandbits(8) for _ in range(n)) if kind == 1: return struct.pack("= 4: # fragment header with believable numbers total = rnd.choice([n, n + 10, 600, 1536, 1537, 4000, 65535, 0]) body = struct.pack(" boots0: R.add(S, "station reset during fuzz", FAIL, "burst %d, seed %d, uptime %d; last frames:\n%s" % (bursts, seed, s["uptime_ms"], _dump(ring))) failed = True if s["configured"]: R.add(S, "fuzz managed to configure the station", FAIL, "burst %d, seed %d; last frames:\n%s" % (bursts, seed, _dump(ring))) failed = True worst_heap = min(worst_heap, s["heap_free"]) last_heap = s["heap_free"] s0 = dict(s0, uptime_ms=s["uptime_ms"]) if len(L.panics) > panics0: R.add(S, "panic or error text on the console", FAIL, "%s; seed %d; last frames:\n%s" % (L.panics[panics0:][:3], seed, _dump(ring))) failed = True L.log_status(L.status(), S) if not failed: R.add(S, "fuzz run", PASS, "%d frames in %d bursts, no reset, no panic, always answered" % (sent, bursts)) R.ok(S, "heap after fuzz is within 8 KB of the start", abs(last_heap - start_heap) <= 8192, "start %d, end %d, lowest %d" % (start_heap, last_heap, worst_heap), WARN) R.metric(S, "frames_sent", sent) R.metric(S, "device_counts", {k: v for k, v in L.counts.items() if k.startswith(("frames_", "frag_", "confirm"))}) # leave the board in a known state L.reboot() def _dump(ring): return "\n".join(" " + w.hex() for w in list(ring)[-12:])