120 lines
4.5 KiB
Python
Executable File
120 lines
4.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: MIT
|
|
"""Smoke test of a FIRMWARE room server (simple_room_server) over the air.
|
|
|
|
Two MeshCore companions log in as guests; each posts once and the room must
|
|
push that post to the OTHER one (signed message from the room, author prefix
|
|
in the signature), never back to its author. That is the room's whole job --
|
|
login, store, push -- end to end, on real radios.
|
|
|
|
tools/fw_room_smoke.py --room <pubkey64> --password <guest pw> \\
|
|
--a 127.0.0.1:47001 --b 127.0.0.1:47003
|
|
|
|
--a/--b are companions reachable as meshcore TCP (a companion on USB elsewhere
|
|
can be exposed with a TCP<->serial bridge; the frames are the same). The room
|
|
is added to both as a contact with a flood path, so no advert is needed.
|
|
|
|
Run it on the bench preset only (all three on 866.5): it transmits logins and
|
|
posts. Exit 0 = every step passed.
|
|
|
|
Not the openHop room (tth-room): that one has its own bench,
|
|
openhop-repeater/tools/bench/room_delivery.py.
|
|
"""
|
|
|
|
import argparse
|
|
import asyncio
|
|
import sys
|
|
import time
|
|
|
|
if __name__ == "__main__" and any(a in ("-h", "--help") for a in sys.argv[1:]):
|
|
print(__doc__)
|
|
sys.exit(0)
|
|
|
|
from meshcore import EventType, MeshCore # noqa: E402
|
|
|
|
ROOM_TYPE = 3
|
|
|
|
|
|
def room_contact(pubkey, name="room-under-test"):
|
|
return {
|
|
"public_key": pubkey, "type": ROOM_TYPE, "flags": 0,
|
|
"out_path_len": -1, "out_path": "", "out_path_hash_mode": 0,
|
|
"adv_name": name, "last_advert": 0, "adv_lat": 0.0, "adv_lon": 0.0,
|
|
}
|
|
|
|
|
|
async def connect(hostport):
|
|
host, port = hostport.rsplit(":", 1)
|
|
mc = await MeshCore.create_tcp(host, int(port))
|
|
return mc
|
|
|
|
|
|
async def wait_push(mc, room_prefix, needle, timeout):
|
|
"""Wait for a message from the room whose text contains `needle`."""
|
|
deadline = time.monotonic() + timeout
|
|
while time.monotonic() < deadline:
|
|
ev = await mc.dispatcher.wait_for_event(EventType.CONTACT_MSG_RECV,
|
|
timeout=max(0.1, deadline - time.monotonic()))
|
|
if ev is None:
|
|
break
|
|
p = ev.payload
|
|
if p.get("pubkey_prefix", "").lower() == room_prefix and needle in p.get("text", ""):
|
|
return p
|
|
return None
|
|
|
|
|
|
async def main():
|
|
ap = argparse.ArgumentParser(description="firmware room server smoke test")
|
|
ap.add_argument("--room", required=True, help="room server public key (64 hex)")
|
|
ap.add_argument("--password", required=True, help="guest password")
|
|
ap.add_argument("--a", required=True, help="companion A host:port")
|
|
ap.add_argument("--b", required=True, help="companion B host:port")
|
|
ap.add_argument("--push-timeout", type=float, default=120)
|
|
args = ap.parse_args()
|
|
|
|
room = args.room.lower()
|
|
prefix = room[:12]
|
|
ok = True
|
|
a, b = await connect(args.a), await connect(args.b)
|
|
names = {id(a): a.self_info.get("name"), id(b): b.self_info.get("name")}
|
|
for mc in (a, b):
|
|
await mc.commands.add_contact(room_contact(room))
|
|
await mc.ensure_contacts()
|
|
await mc.start_auto_message_fetching()
|
|
|
|
for mc in (a, b):
|
|
t0 = time.monotonic()
|
|
ev = await mc.commands.send_login_sync(room, args.password, min_timeout=20)
|
|
good = ev is not None and ev.type == EventType.LOGIN_SUCCESS
|
|
print(f"login {names[id(mc)]}: {'OK' if good else 'FAIL'} ({time.monotonic() - t0:.1f} s)")
|
|
ok &= good
|
|
if not good:
|
|
break
|
|
|
|
if ok:
|
|
for src, dst in ((a, b), (b, a)):
|
|
tag = f"smoke-{int(time.time())}-{names[id(src)]}"
|
|
res = await src.commands.send_msg_with_retry(room, tag, max_attempts=3)
|
|
sent = res is not None and res.type != EventType.ERROR
|
|
print(f"post by {names[id(src)]}: {'acked' if sent else 'NOT acked'}")
|
|
t0 = time.monotonic()
|
|
got = await wait_push(dst, prefix, tag, args.push_timeout)
|
|
if got:
|
|
print(f"push to {names[id(dst)]}: OK after {time.monotonic() - t0:.1f} s "
|
|
f"(author sig {got.get('signature')}, path_len {got.get('path_len')})")
|
|
else:
|
|
print(f"push to {names[id(dst)]}: FAIL (nothing in {args.push_timeout:.0f} s)")
|
|
echo = await wait_push(src, prefix, tag, 5)
|
|
if echo:
|
|
print(f"author {names[id(src)]} got its own post back: FAIL")
|
|
ok &= sent and got is not None and echo is None
|
|
|
|
for mc in (a, b):
|
|
await mc.disconnect()
|
|
print("RESULT:", "PASS" if ok else "FAIL")
|
|
return 0 if ok else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(asyncio.run(main()))
|