Files
meshcore-repeater-observer/tests/test_observer.py
T
tthandClaude Opus 5.5 0da25dd15b
ci / test (push) Successful in 1m15s
meshcore-repeater-observer 0.1.0
Reads a MeshCore repeater's console over BLE or USB and publishes what it
hears: the `pktlog` raw-frame feed in the meshcoretomqtt format (community
analyzer with a JWT signed by the node's key, audience = broker hostname, or
an own broker without auth) and optionally stats, `nodes`, `rxlog` to a local
MQTT broker. Needs the observer build of meshcore-tth.

Tested on a XIAO with the observer build over USB and over BLE: feed in the
meshcoretomqtt format, packet hashes matching those of other observers.
Unit tests for the parsers, the packet transform, the Ed25519 signature
(checked against `cryptography`), the JWT and paho-mqtt 1.x/2.x; README
(Czech), AGENTS.md, CI, Renovate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 00:20:03 +01:00

168 lines
6.7 KiB
Python

"""Parsers, the meshcoretomqtt transform and the JWT signature -- no radio, no broker."""
import hashlib
import importlib
import json
import os
import subprocess
import sys
from base64 import urlsafe_b64decode
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
def load(env=None):
"""Import the module with a clean MC_* environment (it reads env at import)."""
for k in [k for k in os.environ if k.startswith("MC_")]:
del os.environ[k]
os.environ.update(env or {})
import meshcore_repeater_observer as mod
return importlib.reload(mod)
@pytest.fixture
def ob():
return load()
def test_parse_pktlog(ob):
text = " -> P,7,1793600000,40,-92,1104AABB\r\nP,8,1793600001,-12,-110,15\r\nE,8,3,8\r\n"
recs, last, oldest, newest = ob.parse_pktlog(text)
assert [r["seq"] for r in recs] == [7, 8] and (last, oldest, newest) == (8, 3, 8)
assert recs[0]["snr"] == 40 and recs[0]["rssi"] == -92 and recs[0]["raw"] == "1104AABB"
def test_parse_rxlog(ob):
recs, last, oldest, newest = ob.parse_rxlog("R,5,1793600000,abcd,11,24,-95,5361\nE,5,1,5\n")
assert recs == [
{"seq": 5, "when": 1793600000, "pkthash": "abcd", "hdr": 0x11, "snr": 6.0, "rssi": -95, "path": "5361"}
]
assert (last, oldest, newest) == (5, 1, 5)
def test_parse_nodes_name_with_commas(ob):
rows, nxt, total = ob.parse_nodes("N,ab12,2,50100000,14200000,20,-90,30,deadbeef,5361,name, with, commas\nE,1,1\n")
assert rows[0]["name"] == "name, with, commas" and rows[0]["lat"] == 50.1 and rows[0]["snr"] == 5.0
assert (nxt, total) == (1, 1)
def test_paged_done(ob):
assert ob._paged_done(bytearray(b"P,1,2,3,4,ab\r\nE,1,1,1\r\n"))
assert ob._paged_done(bytearray(b" -> E,0,0,0\r\n"))
assert not ob._paged_done(bytearray(b"P,1,2,3,4,ab\r\n"))
def test_clock_both_formats(ob):
assert ob.parse_clock("12:34 - 7/8/2026 UTC (epoch 1786095587)") == (1786095587, True)
from datetime import datetime, timezone
node, exact = ob.parse_clock("12:34 - 7/8/2026 UTC")
assert not exact and node == int(datetime(2026, 8, 7, 12, 34, tzinfo=timezone.utc).timestamp())
assert ob.parse_clock("garbage") is None
def test_frame_and_hash(ob):
payload = bytes(range(40))
flood_advert = bytes([(4 << 2) | 1, 0]) + payload # route F, type 4, no path
route, ptype, plb, pl = ob.parse_raw_frame(flood_advert)
assert (route, ptype, plb, pl) == (1, 4, 0, payload)
assert ob.meshcore_packet_hash(4, 0, payload) == hashlib.sha256(bytes([4]) + payload).digest()[:8].hex().upper()
# 2-byte path hashes, two hops: (2-1)<<6 | 2
with_path = bytes([(4 << 2) | 1, 0x42]) + b"\x53\x61\x77\x88" + payload
assert ob.parse_raw_frame(with_path)[3] == payload
assert ob.parse_raw_frame(b"\x11") is None
def test_packet_json(ob):
ob.OBS.name, ob.OBS.pub = "my-rpt", "AB" * 32
raw = (bytes([(4 << 2) | 1, 0]) + bytes(40)).hex()
msg = ob.obs_packet_json({"seq": 1, "when": 1793600000, "snr": 41, "rssi": -100, "raw": raw})
assert msg["origin"] == "my-rpt" and msg["route"] == "F" and msg["packet_type"] == "4"
assert msg["SNR"] == "10" and msg["RSSI"] == "-100" and msg["raw"] == raw.upper() and len(msg["hash"]) == 16
assert ob.obs_packet_json({"seq": 1, "when": 0, "snr": 0, "rssi": 0, "raw": "zz"}) is None
def orlp_keypair(seed: bytes, ob):
"""orlp/ed25519 keypair layout: prv64 = clamped scalar || prefix (lib/ed25519/keypair.c)."""
h = bytearray(hashlib.sha512(seed).digest())
h[0] &= 248
h[31] &= 63
h[31] |= 64
a = int.from_bytes(h[:32], "little")
pub = ob._ed_compress(ob._ed_mul(ob._ED_B, a))
return pub, bytes(h)
def test_signature_verifies_with_cryptography(ob):
ed = pytest.importorskip("cryptography.hazmat.primitives.asymmetric.ed25519")
seed = bytes(range(32))
pub, prv = orlp_keypair(seed, ob)
# the same key derived the standard way (RFC 8032 seed) must give the same public key
std = ed.Ed25519PrivateKey.from_private_bytes(seed).public_key()
from cryptography.hazmat.primitives import serialization
assert std.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw) == pub
sig = ob.ed25519_sign_orlp(b"hello", pub, prv)
std.verify(sig, b"hello") # raises on a bad signature
def test_jwt_audience_is_broker_host():
ob = load({"MC_OBS_ENABLE": "1", "MC_OBS_IATA": "PRG"})
pub, prv = orlp_keypair(b"\x01" * 32, ob)
tok = ob.make_auth_token(pub.hex(), prv.hex())
head, body, sig = tok.split(".")
payload = json.loads(urlsafe_b64decode(body + "=" * (-len(body) % 4)))
assert json.loads(urlsafe_b64decode(head + "==")) == {"alg": "Ed25519", "typ": "JWT"}
assert payload["aud"] == "mqtt1.meshcore.cz" == ob.OBS_HOST
assert payload["publicKey"] == pub.hex().upper() and payload["exp"] - payload["iat"] == ob.OBS_TOKEN_TTL
assert len(sig) == 128 and sig == sig.upper() # uppercase hex, not base64url
ob2 = load({"MC_OBS_MQTT_HOST": "mqtt2.meshcore.website"})
assert ob2.OBS_AUDIENCE == "mqtt2.meshcore.website"
@pytest.mark.parametrize(
"env,expect",
[
({}, "MC_SERIAL_PORT"),
({"MC_SERIAL_PORT": "/dev/x", "MC_OBS_ENABLE": "1"}, "MC_OBS_IATA"),
({"MC_SERIAL_PORT": "/dev/x"}, "nothing to publish"),
({"MC_SERIAL_PORT": "/dev/x", "MC_OBS_ENABLE": "1", "MC_OBS_IATA": "PRG"}, None),
({"MC_BLE_ADDR": "AA:BB:CC:DD:EE:FF", "MC_MQTT_HOST": "localhost"}, None),
],
)
def test_config_error(env, expect):
err = load(env).config_error()
assert (err is None) if expect is None else (expect in err)
def test_local_mqtt_optional():
ob = load({"MC_SERIAL_PORT": "/dev/x", "MC_OBS_ENABLE": "1", "MC_OBS_IATA": "PRG"})
assert isinstance(ob.make_mqtt(), ob.NullMQTT)
assert ob.MQTT_TOPIC == "repeater-observer/repeater"
def test_help_without_dependencies():
code = (
"import sys, builtins; real = builtins.__import__\n"
"def imp(n, *a, **k):\n"
" if n.split('.')[0] in ('bleak', 'paho', 'serial'): raise ImportError(n)\n"
" return real(n, *a, **k)\n"
"builtins.__import__ = imp; sys.argv = ['x', '--help']\n"
"import runpy; runpy.run_path('meshcore_repeater_observer.py', run_name='__main__')"
)
r = subprocess.run([sys.executable, "-c", code], cwd=ROOT, capture_output=True, text=True)
assert r.returncode == 0 and "MC_SERIAL_PORT" in r.stdout
def test_mqtt_client_with_installed_paho(ob):
# paho 2.x refuses Client() without callback_api_version; 1.x has no such argument
c = ob.mqtt_client(client_id="t", clean_session=True, transport="websockets")
c.on_connect = lambda cl, ud, fl, rc: None
c.on_disconnect = lambda cl, ud, rc: None
c.username_pw_set("v1_AB", "token")