161 lines
7.0 KiB
Python
161 lines
7.0 KiB
Python
"""Card identity, three-field APN overlay and the last network write boundary."""
|
|
|
|
import json
|
|
import time
|
|
|
|
import pytest
|
|
from test_b2_cellular_telemetry import package, status
|
|
from test_b2_sim_identity import CARD_A, CARD_B, IMEI, capture, exchange
|
|
from test_b2_sim_network import AT, tags, update
|
|
from test_vps_host import full_context, reach_sim_restart, sim_context, store # noqa: F401
|
|
|
|
from k1link.device_plugins.arnavi_b2 import profiles, sim_access
|
|
from k1link.device_plugins.arnavi_b2.sim_network import binding
|
|
from k1link.device_plugins.arnavi_b2.sim_observation_store import SimObservationStore
|
|
from k1link.device_plugins.arnavi_b2.usb_protocol import Configuration, ProtocolError, Setting
|
|
|
|
|
|
def test_identity_survives_normal_telemetry_but_not_removal_search_or_archived_startup():
|
|
value = update(raw=tags(1, CARD_A[1]) + status())
|
|
value = update(value, at=AT + 1000, raw=status())
|
|
assert binding(value, "1", now=AT + 1000) == CARD_A[1]
|
|
assert binding(value, "1", now=AT + 1100) is None
|
|
changed = update(value, at=AT + 1001, raw=status(first=2))
|
|
assert binding(update(changed, at=AT + 1002, raw=status()), "1", now=AT + 1002) is None
|
|
archived = update(raw=tags(1, CARD_A[1]) + status(), received=AT + 1000)
|
|
assert binding(update(archived, at=AT + 1001, raw=status()), "1", now=AT + 1001) is None
|
|
|
|
|
|
def test_verified_access_follows_card_across_slots_and_survives_restart(tmp_path):
|
|
cache = SimObservationStore(tmp_path)
|
|
cache.record(capture(exchange(1, CARD_A) + exchange(2, CARD_B)))
|
|
value = update(raw=tags(1, CARD_A[1]) + tags(2, CARD_B[1]) + status())
|
|
first = sim_access.resolve(value, cache, "1", now=AT)
|
|
assert first["source"] == "carrier_candidate" and first["access"] == (
|
|
"internet.mts.ru",
|
|
"mts",
|
|
"mts",
|
|
)
|
|
cache.remember_access(
|
|
IMEI,
|
|
first["card_ref"],
|
|
("private.apn", "private-user", "private-pass"),
|
|
operation="a" * 32,
|
|
verified_at=AT,
|
|
configuration_sha256="b" * 64,
|
|
)
|
|
swapped = update(value, at=AT + 1, raw=tags(1, CARD_B[1]) + tags(2, CARD_A[1]) + status())
|
|
result = sim_access.resolve(swapped, SimObservationStore(tmp_path), "2", now=AT + 1)
|
|
assert result["source"] == "verified_card" and result["access"][0] == "private.apn"
|
|
assert sim_access.resolve(swapped, cache, "1", now=AT + 1)["source"] == "carrier_candidate"
|
|
assert "private-pass" not in json.dumps(sim_access.public(result))
|
|
assert CARD_A[1] not in json.dumps(sim_access.public(result))
|
|
assert cache.verified_access("other", first["card_ref"]) is None
|
|
cache.remember_access(
|
|
IMEI,
|
|
first["card_ref"],
|
|
("old", "", ""),
|
|
operation="c" * 32,
|
|
verified_at=AT - 1,
|
|
configuration_sha256="d" * 64,
|
|
)
|
|
assert cache.verified_access(IMEI, first["card_ref"])["access"][0] == "private.apn"
|
|
|
|
|
|
def test_unknown_card_cannot_be_guessed_from_serving_network_or_apn(tmp_path):
|
|
value = update(raw=tags(1, CARD_A[1]) + status())
|
|
assert sim_access.resolve(value, SimObservationStore(tmp_path), "1", now=AT) == {
|
|
"state": "unknown_card"
|
|
}
|
|
|
|
|
|
def test_automatic_overlay_replaces_apn_and_credentials_only_for_selected_sim():
|
|
config = Configuration(
|
|
tuple(
|
|
Setting(n, v)
|
|
for n, v in (
|
|
(67, b"one"),
|
|
(68, b"user1"),
|
|
(69, b"pass1"),
|
|
(89, b"two"),
|
|
(90, b"user2"),
|
|
(91, b"pass2"),
|
|
(72, bytes(4)),
|
|
(222, b"unknown\xff"),
|
|
)
|
|
)
|
|
)
|
|
policy = profiles.Policy(
|
|
connectivity={"strategy": "sim", "activate": True, "sim_slot": 2, "automatic": True}
|
|
).model_dump()
|
|
with pytest.raises(ProtocolError, match="sim_access_not_resolved"):
|
|
profiles.prepare(config, policy)
|
|
desired = profiles.prepare(config, policy, automatic_access=("internet", "gdata", "gdata"))
|
|
assert [desired.text(n) for n in (89, 90, 91)] == ["internet", "gdata", "gdata"]
|
|
assert all(desired.value(n) == config.value(n) for n in (67, 68, 69, 222))
|
|
assert desired.integer(72, 4) == 1 << 10
|
|
assert not profiles.portable(desired)["connectivity"]["automatic"]
|
|
|
|
|
|
def guarded(context):
|
|
command, body, _, _, _, now = context
|
|
body["sim_guard"] = {"slot": 2, "sid": CARD_A[1]}
|
|
command.sim_guard.observe(body["imei"], package(int(now[0]), tags(2, CARD_A[1]) + status()))
|
|
return command, body, now
|
|
|
|
|
|
def test_card_swap_after_dispatch_prevents_http_configuration_write(sim_context): # noqa: F811
|
|
command, body, now = guarded(sim_context)
|
|
_, _, before, _, wire, _ = sim_context
|
|
command.submit(body)
|
|
command.session_activity[body["imei"], "live"] = time.monotonic()
|
|
assert command.take(body["imei"], "live")
|
|
command.web_receive(wire(5, before.encode()), "192.0.2.1")
|
|
now[0] += 16
|
|
command.session_activity[body["imei"], "live"] = time.monotonic()
|
|
assert command.take(body["imei"], "live")
|
|
command.sim_guard.observe(body["imei"], package(int(now[0]), tags(2, CARD_B[1]) + status()))
|
|
assert command.web_receive(wire(2), "192.0.2.1") is None
|
|
result = command.get(body["id"])
|
|
assert result["state"] == "rejected" and result["reason"] == "sim_card_changed"
|
|
assert "write_reply_sent_at" not in result and "sim_guard" not in result
|
|
|
|
|
|
def test_gprs_without_new_card_identity_cannot_confirm_automatic_access(sim_context): # noqa: F811
|
|
command, body, now = guarded(sim_context)
|
|
reach_sim_restart(sim_context)
|
|
assert command.take(body["imei"], "live")
|
|
now[0] += 2
|
|
command.session_started[body["imei"], "new"] = time.monotonic()
|
|
now[0] += 1
|
|
command.receive(body["imei"], package(int(now[0]), status()), "new")
|
|
assert command.get(body["id"])["state"] == "dispatched"
|
|
now[0] += 1
|
|
command.receive(body["imei"], package(int(now[0]), tags(2, CARD_A[1]) + status()), "new")
|
|
result = command.get(body["id"])
|
|
assert result["state"] == "sim_selected" and result["sim_guard_verified"] is True
|
|
|
|
|
|
def test_guard_and_waiting_intent_survive_vps_restart(sim_context): # noqa: F811
|
|
from ndc_vps.commands import Commands
|
|
from ndc_vps.operations import Operations
|
|
|
|
command, body, now = guarded(sim_context)
|
|
runtime = Operations(command.store, command)
|
|
runtime.submit(body)
|
|
command.sim_guard.observe(body["imei"], package(int(now[0] + 1), status(second=0)))
|
|
now[0] += 2
|
|
command.session_activity[body["imei"], "live"] = time.monotonic()
|
|
runtime.advance(body["imei"])
|
|
assert runtime.get(body["id"])["runtime_phase"] == "waiting-identity"
|
|
assert runtime.get(body["id"])["attempt_count"] == 0
|
|
restored = Operations(command.store, Commands(command.store))
|
|
restored.commands.sessions[body["imei"]] = {"new"}
|
|
restored.commands.session_activity[body["imei"], "new"] = time.monotonic()
|
|
restored.commands.sim_guard.observe(
|
|
body["imei"], package(int(now[0]), tags(2, CARD_B[1]) + status())
|
|
)
|
|
assert restored.get(body["id"])["state"] == "rejected"
|
|
assert restored.get(body["id"])["reason"] == "sim_card_changed"
|
|
assert restored.get(body["id"])["attempt_count"] == 0
|