Discover Ethernet routers under the board, map physical SIM slots, expose vendor controls and real telemetry, and provide packaged VPS speed tests with a live monochrome gauge. Enforce compact typography across Core and Node UI.
130 lines
4.4 KiB
Python
130 lines
4.4 KiB
Python
import json
|
|
import secrets
|
|
import time
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from k1link.fleet.librespeed import LibreSpeed
|
|
from k1link.fleet.trust import PairingError
|
|
|
|
|
|
def invitation():
|
|
return json.dumps(
|
|
dict(
|
|
schema="missioncore.librespeed-connect/v1",
|
|
server_id="ls_" + "a" * 32,
|
|
endpoint="https://server.example.com:19445",
|
|
fingerprint="b" * 64,
|
|
bootstrap=secrets.token_urlsafe(32),
|
|
expires_at=time.time() + 3600,
|
|
)
|
|
)
|
|
|
|
|
|
def test_lost_claim_response_reuses_private_identity_and_public_projection_redacts(
|
|
tmp_path, monkeypatch
|
|
):
|
|
monkeypatch.setattr(
|
|
"k1link.fleet.vps_transport.destination",
|
|
lambda _: ("server.example.com", 19445, "130.49.172.182"),
|
|
)
|
|
transport = Mock(
|
|
side_effect=[
|
|
OSError("reply lost"),
|
|
{"schema": "missioncore.librespeed/v1", "server_id": "ls_" + "a" * 32},
|
|
]
|
|
)
|
|
monkeypatch.setattr("k1link.fleet.vps_transport.request", transport)
|
|
store = LibreSpeed(tmp_path)
|
|
doc = invitation()
|
|
with pytest.raises(OSError):
|
|
store.pair(doc)
|
|
assert store.listing() == []
|
|
store.pair(doc)
|
|
assert transport.call_args_list[0].args[3] == transport.call_args_list[1].args[3]
|
|
public = json.dumps(store.listing())
|
|
assert "token" not in public and "credential" not in public and "bootstrap" not in public
|
|
assert store.path.stat().st_mode & 0o777 == 0o600
|
|
|
|
|
|
def test_changed_certificate_cannot_reuse_owner_credential(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(
|
|
"k1link.fleet.vps_transport.destination",
|
|
lambda _: ("server.example.com", 19445, "130.49.172.182"),
|
|
)
|
|
transport = Mock(
|
|
return_value={"schema": "missioncore.librespeed/v1", "server_id": "ls_" + "a" * 32}
|
|
)
|
|
monkeypatch.setattr("k1link.fleet.vps_transport.request", transport)
|
|
store = LibreSpeed(tmp_path)
|
|
doc = invitation()
|
|
store.pair(doc)
|
|
altered = json.loads(doc)
|
|
altered["fingerprint"] = "c" * 64
|
|
with pytest.raises(PairingError):
|
|
store.pair(json.dumps(altered))
|
|
assert transport.call_count == 1
|
|
|
|
|
|
def test_lease_validates_server_reply_without_exposing_service_token(tmp_path, monkeypatch):
|
|
store = LibreSpeed(tmp_path)
|
|
store.save(
|
|
dict(
|
|
id="ls_" + "a" * 32,
|
|
endpoint="https://server.example.com:19445",
|
|
fingerprint="b" * 64,
|
|
token="controller-secret",
|
|
state="connected",
|
|
)
|
|
)
|
|
monkeypatch.setattr(
|
|
"k1link.fleet.vps_transport.destination",
|
|
lambda _: ("server.example.com", 19445, "130.49.172.182"),
|
|
)
|
|
reply = dict(
|
|
operation_id="op_" + "c" * 32,
|
|
token=secrets.token_urlsafe(32),
|
|
expires_at=int(time.time()) + 90,
|
|
)
|
|
monkeypatch.setattr("k1link.fleet.vps_transport.request", lambda *args: reply)
|
|
lease = store.lease("ls_" + "a" * 32, reply["operation_id"])
|
|
assert lease["token"] == reply["token"] and "controller-secret" not in json.dumps(lease)
|
|
reply["expires_at"] = time.time() + 10000
|
|
with pytest.raises(PairingError):
|
|
store.lease("ls_" + "a" * 32, reply["operation_id"])
|
|
|
|
|
|
def test_cancel_is_delivered_to_node_before_server_lease_is_released(tmp_path, monkeypatch):
|
|
from threading import RLock
|
|
from types import SimpleNamespace
|
|
|
|
from k1link.fleet.router_speedtest import cancel
|
|
|
|
operation = "op_" + "c" * 32
|
|
entry = dict(
|
|
speedtest_server="ls_" + "a" * 32,
|
|
device_id="router-one",
|
|
runtime_id="b" * 32,
|
|
binding="binding-one",
|
|
payload={"action": "speedtest"},
|
|
public={"state": "applying"},
|
|
)
|
|
fleet = SimpleNamespace(
|
|
root=tmp_path,
|
|
lock=RLock(),
|
|
find=lambda _: {"binding": {"binding_id": "binding-one"}},
|
|
routers=SimpleNamespace(pending={("vehicle", operation): entry}),
|
|
)
|
|
revoke = Mock()
|
|
monkeypatch.setattr(LibreSpeed, "cancel", revoke)
|
|
result = cancel(fleet, "vehicle", operation)
|
|
assert result["state"] == "cancelling"
|
|
revoke.assert_not_called()
|
|
queued = next(e for k, e in fleet.routers.pending.items() if k != ("vehicle", operation))
|
|
assert queued["payload"]["speedtest_cancel"] == operation
|
|
assert queued["payload"]["runtime_id"] == "b" * 32
|
|
assert entry["payload"] is None
|
|
cancel(fleet, "vehicle", operation)
|
|
assert len(fleet.routers.pending) == 2
|