101 lines
4.2 KiB
Python
101 lines
4.2 KiB
Python
"""Bounded HTTP clock exchange over real loopback streams; no physical B2."""
|
|
|
|
import asyncio
|
|
import json
|
|
import tempfile
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from ndc_vps.commands import frame
|
|
from ndc_vps.service import Service
|
|
from ndc_vps.store import Store
|
|
from ndc_vps.web_protocol import time_reply
|
|
|
|
|
|
async def qualify():
|
|
with tempfile.TemporaryDirectory(prefix="ndc-vps-clock-") as directory:
|
|
root = Path(directory)
|
|
(root / "host.json").write_text(
|
|
json.dumps(
|
|
{"host_id": "vps_" + "a" * 64, "tracker": {"host": "example.org", "port": 9922}}
|
|
)
|
|
)
|
|
store = Store(root)
|
|
imei = "123456789012345"
|
|
store.set_devices([{"imei": imei, "instance_id": "c" * 32}])
|
|
service = Service(store)
|
|
server = await asyncio.start_server(service.tracker, "127.0.0.1", 0)
|
|
writers = []
|
|
try:
|
|
port = server.sockets[0].getsockname()[1]
|
|
monitor, writer = await asyncio.open_connection("127.0.0.1", port)
|
|
writers.append(writer)
|
|
writer.write(b"\xff\x23" + int(imei).to_bytes(8, "little"))
|
|
await writer.drain()
|
|
assert (await monitor.readexactly(9))[0] == 0x7B
|
|
reader, writer = await asyncio.open_connection("127.0.0.1", port)
|
|
writers.append(writer)
|
|
request = (
|
|
b"[\x01\x00\x0f"
|
|
+ int(imei).to_bytes(8, "little")
|
|
+ bytes.fromhex("0001e7020000075d")
|
|
)
|
|
for _ in range(2):
|
|
before = int(time.time())
|
|
writer.write(b"POST /c/index.php HTTP/1.1\r\nContent-Length: 20\r\n\r\n" + request)
|
|
await writer.drain()
|
|
header = await reader.readuntil(b"\r\n\r\n")
|
|
assert header.startswith(b"HTTP/1.1 200 OK\r\n")
|
|
assert b"Content-Length: 10\r\n" in header
|
|
body = await reader.readexactly(10)
|
|
seconds = int.from_bytes(body[5:9], "big")
|
|
assert before <= seconds <= int(time.time())
|
|
assert body == time_reply(seconds)
|
|
with store.db() as db:
|
|
assert db.execute("SELECT COUNT(*) FROM commands").fetchone()[0] == 0
|
|
session = next(iter(service.commands.sessions[imei]))
|
|
service.commands.session_activity[imei, session] = time.monotonic() - 20
|
|
original = service.commands.submit(
|
|
{"id": "d" * 32, "imei": imei, "kind": "read-settings", "password": "123456"}
|
|
)
|
|
service.notify(imei)
|
|
await asyncio.sleep(0.1)
|
|
assert service.commands.get("d" * 32)["state"] == "queued"
|
|
# A real incoming telemetry frame wakes the waiting sender.
|
|
data = int(time.time()).to_bytes(4, "little") + b"\x63\0\0\0\0"
|
|
writers[0].write(b"[\x01\x01\x05\0" + data + bytes([sum(data) % 256]) + b"]")
|
|
await writers[0].drain()
|
|
expected = frame("123456*GETP")
|
|
received = await monitor.readexactly(len(expected) + 4)
|
|
assert received in (b"{\x00\x01}" + expected, expected + b"{\x00\x01}")
|
|
receipt = service.commands.get("d" * 32)
|
|
assert receipt["state"] == "dispatched"
|
|
assert receipt["expires_at"] == original["expires_at"]
|
|
assert service.commands.take(imei, session) is None
|
|
finally:
|
|
server.close()
|
|
for writer in writers:
|
|
writer.close()
|
|
await asyncio.gather(
|
|
*(writer.wait_closed() for writer in writers), return_exceptions=True
|
|
)
|
|
for _ in range(30):
|
|
if service.connections == 0:
|
|
break
|
|
await asyncio.sleep(0.01)
|
|
assert service.connections == 0 and not service.wake
|
|
await server.wait_closed()
|
|
return {
|
|
"ok": True,
|
|
"clock_replies": 2,
|
|
"same_http_connection": True,
|
|
"clock_commands_created": 0,
|
|
"stale_configuration_deferred": True,
|
|
"fresh_packet_dispatched_once": True,
|
|
"physical_devices_used": False,
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(json.dumps(asyncio.run(asyncio.wait_for(qualify(), 8)), sort_keys=True))
|