aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/sonix/test_sonix_beacon.py
blob: ec93dfb3758d22ad1533b4bd76855df03e8a6d92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python3
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Run: /usr/bin/python3 desktop/modules/sonix/test_sonix_beacon.py
# Starts the relay on a test port, sends a junk datagram and a beacon, and
# checks that only the beacon comes out.
import os
import select
import socket
import subprocess
import sys

PORT = 47801
here = os.path.dirname(os.path.abspath(__file__))
relay = subprocess.Popen([sys.executable, "-u", os.path.join(here, "sonix-beacon.py"), str(PORT)],
                         stdout=subprocess.PIPE, text=True)
try:
    out = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    for _ in range(50):  # until the relay is bound
        out.sendto(b"not a beacon", ("127.0.0.1", PORT))
        out.sendto(b"SONIXLINK1 10.0.0.5 7800 HiBy R1", ("127.0.0.1", PORT))
        if select.select([relay.stdout], [], [], 0.1)[0]:
            break
    line = relay.stdout.readline().strip()
    assert line == "SONIXLINK1 10.0.0.5 7800 HiBy R1", line
finally:
    relay.kill()
print("ok")