182 lines
5.7 KiB
Python
182 lines
5.7 KiB
Python
"""Durability, identity admission, decoder and shared VPS assignment acceptance."""
|
|
|
|
import base64
|
|
import hashlib
|
|
import struct
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from k1link.fleet.vps_hosts import VPSHosts
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def package(timestamp=None, lat=55.1, lon=37.2):
|
|
tags = (
|
|
b"\x03"
|
|
+ struct.pack("<f", lat)
|
|
+ b"\x04"
|
|
+ struct.pack("<f", lon)
|
|
+ b"\x05\x0a\x19\x88\x02"
|
|
)
|
|
data = struct.pack("<I", int(timestamp or time.time())) + tags
|
|
return b"[\x01\x01" + struct.pack("<H", len(tags)) + data + bytes([sum(data) % 256]) + b"]"
|
|
|
|
|
|
def test_core_cursor_and_position_order_are_one_transaction(tmp_path):
|
|
class Equipment:
|
|
vehicle = "rover-a"
|
|
|
|
def listing(self, vehicle=None):
|
|
return [{"id": "c" * 32, "name": "B2", "vehicle_id": self.vehicle}]
|
|
|
|
eq = Equipment()
|
|
hosts = VPSHosts(tmp_path, "core", eq)
|
|
row = {
|
|
"id": "vps_test",
|
|
"name": "VPS",
|
|
"state": "paired",
|
|
"cursor": 0,
|
|
"vehicle_ids": ["rover-a", "rover-b"],
|
|
"last_sync_at": time.time(),
|
|
}
|
|
hosts.save(row)
|
|
now = int(time.time())
|
|
|
|
def item(seq, measured):
|
|
raw = package(measured)
|
|
return {
|
|
"id": seq,
|
|
"instance_id": "c" * 32,
|
|
"received_at": time.time(),
|
|
"raw": base64.b64encode(raw).decode(),
|
|
"digest": hashlib.sha256(raw).hexdigest(),
|
|
}
|
|
|
|
hosts.ingest(row, [item(1, now), item(2, now - 86400)], [{"id": "c" * 32}])
|
|
assert hosts.rows()[0]["cursor"] == 2
|
|
|
|
status = hosts.telemetry()[0]
|
|
assert status["online"] and status["position_fresh"]
|
|
assert status["position"]["measured_at"] == now
|
|
eq.vehicle = "rover-b"
|
|
assert hosts.telemetry()[0]["position"] is not None # one shared host, reassigned tracker
|
|
eq.vehicle = "rover-c"
|
|
assert hosts.telemetry()[0]["position"] is None
|
|
with pytest.raises(ValueError):
|
|
hosts.ingest(row, [item(3, now), {**item(4, now), "raw": "!!"}], [{"id": "c" * 32}])
|
|
assert hosts.rows()[0]["cursor"] == 2
|
|
|
|
eq.vehicle = "rover-a"
|
|
row["state"] = "replaced"
|
|
hosts.save(row)
|
|
replaced_status = hosts.telemetry()[0]
|
|
assert replaced_status["position"] is None
|
|
assert not replaced_status["online"]
|
|
|
|
|
|
def test_tls_identity_checked_before_credentials(monkeypatch):
|
|
from k1link.fleet import vps_transport
|
|
from k1link.fleet.trust import PairingError
|
|
|
|
requests = []
|
|
|
|
class Socket:
|
|
def getpeercert(self, binary_form=False):
|
|
return b"wrong certificate"
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
class Context:
|
|
def wrap_socket(self, raw, **kwargs):
|
|
return raw
|
|
|
|
class Connection:
|
|
def __init__(self, *args, **kwargs):
|
|
self.sock = None
|
|
|
|
def request(self, *args, **kwargs):
|
|
requests.append(args)
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
monkeypatch.setattr(vps_transport, "destination", lambda _: ("example.org", 19443, "1.1.1.1"))
|
|
monkeypatch.setattr(vps_transport, "connect", lambda *args: Socket())
|
|
monkeypatch.setattr(vps_transport.ssl, "SSLContext", lambda _: Context())
|
|
monkeypatch.setattr(vps_transport.http.client, "HTTPConnection", Connection)
|
|
with pytest.raises(PairingError, match="Идентичность"):
|
|
vps_transport.request(
|
|
{
|
|
"endpoint": "https://example.org:19443",
|
|
"fingerprint": "a" * 64,
|
|
"token": "synthetic",
|
|
},
|
|
"POST",
|
|
"/v1/pair",
|
|
{"secret": "synthetic"},
|
|
)
|
|
assert requests == []
|
|
|
|
|
|
def test_reinstalled_host_requires_explicit_replacement_and_keeps_history(tmp_path, monkeypatch):
|
|
from k1link.fleet import vps_hosts
|
|
from k1link.fleet.trust import PairingError
|
|
|
|
hosts = VPSHosts(tmp_path, "core", None)
|
|
old = {
|
|
"id": "vps_old",
|
|
"endpoint": "https://example.org:19443",
|
|
"state": "paired",
|
|
"revision": 1,
|
|
"name": "old",
|
|
"vehicle_ids": ["rover-a"],
|
|
}
|
|
hosts.save(old)
|
|
monkeypatch.setattr(
|
|
vps_hosts,
|
|
"invitation",
|
|
lambda _: {"host_id": "vps_new", "endpoint": old["endpoint"], "secret": "synthetic"},
|
|
)
|
|
monkeypatch.setattr(
|
|
vps_hosts,
|
|
"request",
|
|
lambda *args: {"host_id": "vps_new", "schema": "missioncore.vps-host/v1"},
|
|
)
|
|
with pytest.raises(PairingError, match="переустановлен"):
|
|
hosts.pair("synthetic", "replacement", ["rover-a"])
|
|
assert hosts.listing()[0]["id"] == "vps_old"
|
|
result = hosts.pair("synthetic", "replacement", ["rover-a"], replaces_id="vps_old")
|
|
assert result["state"] == "paired"
|
|
assert len(hosts.listing()) == 1
|
|
assert len(hosts.rows()) == 2
|
|
assert hosts.rows()[0]["state"] == "replaced"
|
|
|
|
|
|
def test_core_admits_only_independently_observed_numeric_id(tmp_path, monkeypatch):
|
|
from k1link.fleet import vps_hosts
|
|
|
|
hosts = VPSHosts(tmp_path, "core", None)
|
|
devices = [
|
|
{"id": "a" * 32, "identity_key": "123456789012345", "observed": {"device_id": "74565"}},
|
|
{"id": "b" * 32, "identity_key": "223456789012345", "observed": {}},
|
|
]
|
|
monkeypatch.setattr(hosts, "devices", lambda row: devices)
|
|
monkeypatch.setattr(hosts, "ingest", lambda *args: 0)
|
|
monkeypatch.setattr(hosts, "save", lambda row: None)
|
|
calls = []
|
|
|
|
def call(host, method, path, body=None):
|
|
calls.append((path, body))
|
|
return {"items": []}
|
|
|
|
monkeypatch.setattr(vps_hosts, "request", call)
|
|
hosts.sync({"cursor": 0})
|
|
items = calls[0][1]["items"]
|
|
assert items[0]["device_id"] == 74565
|
|
assert "device_id" not in items[1]
|
|
assert set(items[0]) == {"imei", "instance_id", "device_id"}
|