Files
NODEDC_MISSION_CORE/tests/test_b2_automatic_connectivity.py
T

96 lines
4.0 KiB
Python

"""Card identity, three-field APN overlay and the last network write boundary."""
import json
import pytest
from test_b2_cellular_telemetry import status
from test_b2_sim_identity import CARD_A, CARD_B, IMEI, capture, exchange
from test_b2_sim_network import AT, tags, update
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"]