Files
NODEDC_MISSION_CORE/tests/fleet/test_heartbeat_encoding.py
T
DCCONSTRUCTIONS e06ff9a214 feat(fleet): add shared WireGuard profiles and bounded rover transport
Add portable shared WireGuard profiles while retaining Tailscale and Worker boundaries. Bound rover command/telemetry delivery, recovery and diagnostic capture across Core and Node.
2026-10-29 09:10:01 +03:00

67 lines
2.6 KiB
Python

"""Compression changes framing, never identity, acknowledgements or body limits."""
import gzip
import http.client
import json
import threading
from http.server import ThreadingHTTPServer
from types import SimpleNamespace
import pytest
from k1link.fleet.heartbeat_encoding import MAX_HEARTBEAT_BYTES, decode_heartbeat
from k1link.fleet.transport import NodeChannelHandler
def test_encoding_rejects_truncation_concatenation_expansion_and_other_channels():
body = b'{"snapshot":"fixture"}'
encoded = gzip.compress(body)
assert decode_heartbeat(body, "", "/v1/node/heartbeat") == body
assert decode_heartbeat(encoded, "gzip", "/v1/node/heartbeat") == body
for data, encoding, path in (
(encoded[:-2], "gzip", "/v1/node/heartbeat"),
(encoded + encoded, "gzip", "/v1/node/heartbeat"),
(encoded + b"extra", "gzip", "/v1/node/heartbeat"),
(gzip.compress(b"x" * (MAX_HEARTBEAT_BYTES + 1)), "gzip", "/v1/node/heartbeat"),
(encoded, "deflate", "/v1/node/heartbeat"),
(encoded, "gzip", "/v1/node/rover"),
(encoded, "gzip", "/v1/node/liveness"),
):
with pytest.raises(ValueError):
decode_heartbeat(data, encoding, path)
def test_http_decodes_before_existing_authentication_and_rejects_bad_body():
calls = []
class Handler(NodeChannelHandler):
def setup(self):
super().setup()
self.connection = SimpleNamespace(getpeercert=lambda **_: b"fixture-cert")
def receive(cert, path, value, *, address):
calls.append((cert, path, value))
return 403, {"error": "Fixture binding rejected"}
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
server.address = "127.0.0.1"
server.registry = SimpleNamespace(receive=receive)
worker = threading.Thread(target=server.serve_forever, daemon=True)
worker.start()
try:
for raw, expected in ((gzip.compress(b'{"node_id":"fixture"}'), 403), (b"invalid", 400)):
client = http.client.HTTPConnection(*server.server_address, timeout=2)
try:
client.request("POST", "/v1/node/heartbeat", raw,
{"Content-Type": "application/json", "Content-Encoding": "gzip"})
response = client.getresponse()
assert response.status == expected
assert "error" in json.loads(response.read())
finally:
client.close()
assert calls == [(b"fixture-cert", "/v1/node/heartbeat", {"node_id": "fixture"})]
finally:
server.shutdown()
server.server_close()
worker.join(timeout=2)