94 lines
3.4 KiB
Python
94 lines
3.4 KiB
Python
"""B2 token receipts can return through either admitted monitoring connection."""
|
|
|
|
import struct
|
|
import time
|
|
|
|
import pytest
|
|
from test_vps_host import store # noqa: F401
|
|
|
|
|
|
def receipt(token, code=0):
|
|
data = struct.pack("<I", int(time.time())) + bytes((code,)) + token
|
|
return b"[\x01\x09\x05\0" + data + bytes([sum(data) % 256]) + b"]"
|
|
|
|
|
|
@pytest.fixture
|
|
def pending(store): # noqa: F811
|
|
from ndc_vps.commands import Commands
|
|
|
|
commands = Commands(store)
|
|
imei = "123456789012345"
|
|
other = "123456789012346"
|
|
store.set_devices(
|
|
[
|
|
{"imei": imei, "instance_id": "a" * 32},
|
|
{"imei": other, "instance_id": "b" * 32},
|
|
]
|
|
)
|
|
commands.sessions[imei] = {"primary", "secondary"}
|
|
commands.sessions[other] = {"another-tracker"}
|
|
body = {
|
|
"id": "c" * 32,
|
|
"imei": imei,
|
|
"kind": "request-position",
|
|
"model": 231,
|
|
"firmware": "0.01",
|
|
}
|
|
commands.submit(body)
|
|
raw = commands.take(imei, "primary")
|
|
return commands, imei, other, body, raw[5:9]
|
|
|
|
|
|
def test_second_stream_receipt_confirms_once_without_resending(pending):
|
|
commands, imei, _, body, token = pending
|
|
commands.receive(imei, receipt(token), "secondary")
|
|
confirmed = commands.get(body["id"])
|
|
assert confirmed["state"] == "device_confirmed" and confirmed["device_code"] == 0
|
|
assert commands.take(imei, "secondary") is None
|
|
commands.receive(imei, receipt(token, 5), "primary")
|
|
assert commands.get(body["id"]) == confirmed
|
|
assert type(commands)(commands.store).get(body["id"]) == confirmed
|
|
|
|
|
|
def test_ack_is_not_completion_and_first_acceptance_survives_duplicate(pending):
|
|
commands, imei, _, body, token = pending
|
|
commands.receive(imei, receipt(token, 6), "secondary")
|
|
accepted = commands.get(body["id"])
|
|
assert accepted["state"] == "dispatched" and accepted["device_code"] == 6
|
|
assert accepted["device_accepted_at"] > 0
|
|
commands.receive(imei, receipt(token, 6), "primary")
|
|
assert commands.get(body["id"])["device_accepted_at"] == accepted["device_accepted_at"]
|
|
commands.receive(imei, receipt(token, 5), "secondary")
|
|
assert commands.get(body["id"])["state"] == "device_rejected"
|
|
|
|
|
|
@pytest.mark.parametrize("source", ["unknown-session", "wrong-token", "other-tracker", "closed"])
|
|
def test_unrelated_receipt_never_completes_command(pending, source):
|
|
commands, imei, other, body, token = pending
|
|
session = "secondary"
|
|
if source == "unknown-session":
|
|
session = "unadmitted"
|
|
elif source == "wrong-token":
|
|
token = bytes(value ^ 255 for value in token)
|
|
elif source == "other-tracker":
|
|
imei, session = other, "another-tracker"
|
|
else:
|
|
commands.sessions[imei].remove(session)
|
|
commands.receive(imei, receipt(token), session)
|
|
assert commands.get(body["id"])["state"] == "dispatched"
|
|
|
|
|
|
def test_new_connection_can_deliver_exact_receipt_without_replay(pending):
|
|
commands, imei, _, body, token = pending
|
|
commands.sessions[imei] = {"reconnected"}
|
|
commands.receive(imei, receipt(token), "reconnected")
|
|
assert commands.get(body["id"])["state"] == "device_confirmed"
|
|
assert commands.take(imei, "reconnected") is None
|
|
|
|
|
|
def test_revoked_device_receipt_is_not_accepted(pending):
|
|
commands, imei, _, body, token = pending
|
|
commands.store.set_devices([])
|
|
commands.receive(imei, receipt(token), "secondary")
|
|
assert commands.get(body["id"])["state"] == "dispatched"
|