fix(k1): restore canonical local connection lifecycle
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
@@ -13,9 +15,14 @@ TEST_PROFILE_ID = "fixture.quick-connect.v1"
|
||||
TEST_CREDENTIAL_SOURCE_ID = "fixture.firmware-provider.v1"
|
||||
|
||||
|
||||
def _helper(tmp_path: Path) -> Path:
|
||||
def _helper(tmp_path: Path, *, seed_compiled_cache: bool = True) -> Path:
|
||||
helper = tmp_path / "associate_wifi.swift"
|
||||
helper.write_text("// offline fixture\n", encoding="utf-8")
|
||||
if seed_compiled_cache:
|
||||
executable = wifi._compiled_macos_helper_path(helper)
|
||||
executable.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
executable.write_bytes(b"offline compiled fixture\n")
|
||||
executable.chmod(0o700)
|
||||
return helper
|
||||
|
||||
|
||||
@@ -42,8 +49,9 @@ def test_association_exposes_only_profile_id_and_expected_ssid_to_platform_helpe
|
||||
stderr=b"",
|
||||
)
|
||||
|
||||
helper = _helper(tmp_path)
|
||||
result = wifi.associate_with_wifi_profile_once(
|
||||
_helper(tmp_path),
|
||||
helper,
|
||||
TEST_PROFILE_ID,
|
||||
"XGR-OFFLINE",
|
||||
runner=fake_runner,
|
||||
@@ -61,7 +69,7 @@ def test_association_exposes_only_profile_id_and_expected_ssid_to_platform_helpe
|
||||
}
|
||||
assert len(calls) == 1
|
||||
call = calls[0]
|
||||
assert call["argv"][:2] == ["/usr/bin/xcrun", "swift"]
|
||||
assert call["argv"] == [str(wifi._compiled_macos_helper_path(helper))]
|
||||
request = json.loads(bytes(call["input"]).decode("utf-8"))
|
||||
assert request == {
|
||||
"action": "associate",
|
||||
@@ -290,6 +298,8 @@ def test_association_reports_operator_timeout_separately_from_missing_helper(
|
||||
)
|
||||
|
||||
assert raised.value.reason_code == "host-wifi-operation-timeout"
|
||||
assert raised.value.helper_stage is None
|
||||
assert raised.value.helper_elapsed_ms is None
|
||||
|
||||
|
||||
def test_association_reports_an_unavailable_helper_separately_from_timeout(
|
||||
@@ -317,6 +327,271 @@ def test_association_reports_an_unavailable_helper_separately_from_timeout(
|
||||
assert raised.value.reason_code == "host-wifi-helper-unavailable"
|
||||
|
||||
|
||||
def test_cold_helper_build_uses_source_hash_and_separate_timeout(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
monkeypatch.setattr(wifi.sys, "platform", "darwin")
|
||||
helper = _helper(tmp_path, seed_compiled_cache=False)
|
||||
executable = wifi._compiled_macos_helper_path(helper)
|
||||
calls: list[dict[str, object]] = []
|
||||
|
||||
def fake_runner(argv: list[str], **kwargs: object) -> subprocess.CompletedProcess[bytes]:
|
||||
calls.append({"argv": argv, **kwargs})
|
||||
if argv[:2] == ["/usr/bin/xcrun", "swiftc"]:
|
||||
assert argv[2] == str(helper.resolve())
|
||||
assert argv[3] == "-o"
|
||||
assert kwargs["stdin"] == subprocess.DEVNULL
|
||||
assert "input" not in kwargs
|
||||
assert 0 < float(kwargs["timeout"]) <= wifi.DEFAULT_HELPER_BUILD_TIMEOUT_SECONDS
|
||||
staging = Path(argv[4])
|
||||
staging.write_bytes(b"compiled fixture\n")
|
||||
staging.chmod(0o700)
|
||||
return subprocess.CompletedProcess(argv, 0, stdout=b"", stderr=b"")
|
||||
|
||||
assert argv == [str(executable)]
|
||||
assert kwargs["timeout"] == 30.0
|
||||
return subprocess.CompletedProcess(
|
||||
argv,
|
||||
0,
|
||||
stdout=(
|
||||
b'{"ok":true,"adapter":"macOS Keychain",'
|
||||
b'"profile_available":true}'
|
||||
),
|
||||
stderr=b"",
|
||||
)
|
||||
|
||||
result = wifi.check_wifi_profile(
|
||||
helper,
|
||||
TEST_PROFILE_ID,
|
||||
"XGR-OFFLINE",
|
||||
runner=fake_runner,
|
||||
)
|
||||
|
||||
assert result["available"] is True
|
||||
assert len(calls) == 2
|
||||
assert calls[0]["argv"][:2] == ["/usr/bin/xcrun", "swiftc"]
|
||||
assert calls[1]["argv"] == [str(executable)]
|
||||
assert executable.is_file()
|
||||
assert executable.stat().st_mode & 0o111
|
||||
assert executable.name.endswith(hashlib.sha256(helper.read_bytes()).hexdigest())
|
||||
assert not list(executable.parent.glob(f".{executable.name}.*.tmp"))
|
||||
|
||||
|
||||
def test_plugin_helper_cache_is_stable_under_repository_runtime(tmp_path: Path) -> None:
|
||||
helper = tmp_path / "repo" / "plugins" / "xgrids-k1" / "macos" / "associate_wifi.swift"
|
||||
helper.parent.mkdir(parents=True)
|
||||
helper.write_text("// offline fixture\n", encoding="utf-8")
|
||||
|
||||
assert wifi._helper_cache_directory(helper) == (
|
||||
tmp_path / "repo" / ".runtime" / "mission-core" / "helpers"
|
||||
)
|
||||
|
||||
|
||||
def test_helper_lock_and_compile_share_one_build_deadline(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
monkeypatch.setattr(wifi.sys, "platform", "darwin")
|
||||
helper = _helper(tmp_path, seed_compiled_cache=False)
|
||||
monotonic_values = iter((100.0, 100.0, 104.0))
|
||||
monkeypatch.setattr(wifi.time, "monotonic", lambda: next(monotonic_values))
|
||||
monkeypatch.setattr(wifi.time, "monotonic_ns", lambda: 0)
|
||||
lock_timeouts: list[float] = []
|
||||
compile_timeouts: list[float] = []
|
||||
|
||||
class FakeLock:
|
||||
def __enter__(self) -> None:
|
||||
return None
|
||||
|
||||
def __exit__(self, *_args: object) -> None:
|
||||
return None
|
||||
|
||||
def fake_lock(_path: Path, *, timeout_seconds: float) -> FakeLock:
|
||||
lock_timeouts.append(timeout_seconds)
|
||||
return FakeLock()
|
||||
|
||||
def fake_runner(argv: list[str], **kwargs: object) -> subprocess.CompletedProcess[bytes]:
|
||||
if argv[:2] == ["/usr/bin/xcrun", "swiftc"]:
|
||||
compile_timeouts.append(float(kwargs["timeout"]))
|
||||
staging = Path(argv[4])
|
||||
staging.write_bytes(b"compiled fixture\n")
|
||||
staging.chmod(0o700)
|
||||
return subprocess.CompletedProcess(argv, 0, stdout=b"", stderr=b"")
|
||||
return subprocess.CompletedProcess(
|
||||
argv,
|
||||
0,
|
||||
stdout=b'{"ok":true,"adapter":"macOS Keychain","profile_available":true}',
|
||||
stderr=b"",
|
||||
)
|
||||
|
||||
monkeypatch.setattr(wifi, "_exclusive_helper_build_lock", fake_lock)
|
||||
result = wifi._run_macos_helper(
|
||||
helper,
|
||||
{"action": "check-profile"},
|
||||
timeout_seconds=3.0,
|
||||
build_timeout_seconds=10.0,
|
||||
runner=fake_runner,
|
||||
)
|
||||
|
||||
assert result["ok"] is True
|
||||
assert lock_timeouts == [10.0]
|
||||
assert compile_timeouts == [6.0]
|
||||
|
||||
|
||||
def test_compiled_helper_is_reused_without_recompiling(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
monkeypatch.setattr(wifi.sys, "platform", "darwin")
|
||||
helper = _helper(tmp_path, seed_compiled_cache=False)
|
||||
executable = wifi._compiled_macos_helper_path(helper)
|
||||
compile_count = 0
|
||||
runtime_count = 0
|
||||
|
||||
def fake_runner(argv: list[str], **_: object) -> subprocess.CompletedProcess[bytes]:
|
||||
nonlocal compile_count, runtime_count
|
||||
if argv[:2] == ["/usr/bin/xcrun", "swiftc"]:
|
||||
compile_count += 1
|
||||
staging = Path(argv[4])
|
||||
staging.write_bytes(b"compiled fixture\n")
|
||||
staging.chmod(0o700)
|
||||
return subprocess.CompletedProcess(argv, 0, stdout=b"", stderr=b"")
|
||||
runtime_count += 1
|
||||
assert argv == [str(executable)]
|
||||
return subprocess.CompletedProcess(
|
||||
argv,
|
||||
0,
|
||||
stdout=(
|
||||
b'{"ok":true,"adapter":"macOS Keychain",'
|
||||
b'"profile_available":true}'
|
||||
),
|
||||
stderr=b"",
|
||||
)
|
||||
|
||||
for _ in range(2):
|
||||
wifi.check_wifi_profile(
|
||||
helper,
|
||||
TEST_PROFILE_ID,
|
||||
"XGR-OFFLINE",
|
||||
runner=fake_runner,
|
||||
)
|
||||
|
||||
assert compile_count == 1
|
||||
assert runtime_count == 2
|
||||
|
||||
|
||||
def test_helper_build_timeout_is_separate_from_operation_timeout(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
monkeypatch.setattr(wifi.sys, "platform", "darwin")
|
||||
helper = _helper(tmp_path, seed_compiled_cache=False)
|
||||
seen_timeout: float | None = None
|
||||
monotonic_values = iter((1_000_000_000, 1_123_000_000))
|
||||
monkeypatch.setattr(wifi.time, "monotonic_ns", lambda: next(monotonic_values))
|
||||
|
||||
def timed_out_compiler(
|
||||
argv: list[str], **kwargs: object
|
||||
) -> subprocess.CompletedProcess[bytes]:
|
||||
nonlocal seen_timeout
|
||||
assert argv[:2] == ["/usr/bin/xcrun", "swiftc"]
|
||||
seen_timeout = float(kwargs["timeout"])
|
||||
raise subprocess.TimeoutExpired(argv, timeout=seen_timeout)
|
||||
|
||||
with pytest.raises(
|
||||
wifi.HostWifiProfileError,
|
||||
match="host-wifi-helper-build-timeout",
|
||||
) as raised:
|
||||
wifi._run_macos_helper(
|
||||
helper,
|
||||
{"action": "check-profile"},
|
||||
timeout_seconds=3.0,
|
||||
build_timeout_seconds=7.5,
|
||||
runner=timed_out_compiler,
|
||||
)
|
||||
|
||||
assert raised.value.reason_code == "host-wifi-helper-build-timeout"
|
||||
assert raised.value.helper_stage == "compile"
|
||||
assert raised.value.helper_elapsed_ms == 123
|
||||
assert seen_timeout is not None
|
||||
assert 0 < seen_timeout <= 7.5
|
||||
assert not wifi._compiled_macos_helper_path(helper).exists()
|
||||
|
||||
|
||||
def test_helper_build_lock_contention_is_bounded(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
fcntl = pytest.importorskip("fcntl")
|
||||
lock_path = tmp_path / "helper.lock"
|
||||
descriptor = os.open(lock_path, os.O_RDWR | os.O_CREAT, 0o600)
|
||||
fcntl.flock(descriptor, fcntl.LOCK_EX)
|
||||
monotonic_values = iter((100.0, 100.02))
|
||||
monkeypatch.setattr(wifi.time, "monotonic", lambda: next(monotonic_values))
|
||||
|
||||
try:
|
||||
with (
|
||||
pytest.raises(wifi.HostWifiProfileError) as raised,
|
||||
wifi._exclusive_helper_build_lock(
|
||||
lock_path,
|
||||
timeout_seconds=0.01,
|
||||
),
|
||||
):
|
||||
raise AssertionError("contended lock must not be acquired")
|
||||
finally:
|
||||
fcntl.flock(descriptor, fcntl.LOCK_UN)
|
||||
os.close(descriptor)
|
||||
|
||||
assert raised.value.reason_code == "host-wifi-helper-build-timeout"
|
||||
assert raised.value.helper_stage == "compile-lock"
|
||||
assert raised.value.helper_elapsed_ms == 19
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("compiler_failure", "expected_reason_code"),
|
||||
[
|
||||
("exit", "host-wifi-helper-build-failed"),
|
||||
("unavailable", "host-wifi-helper-compiler-unavailable"),
|
||||
],
|
||||
)
|
||||
def test_helper_build_errors_have_sanitized_build_taxonomy(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
compiler_failure: str,
|
||||
expected_reason_code: str,
|
||||
) -> None:
|
||||
monkeypatch.setattr(wifi.sys, "platform", "darwin")
|
||||
helper = _helper(tmp_path, seed_compiled_cache=False)
|
||||
|
||||
def failing_compiler(
|
||||
argv: list[str], **_: object
|
||||
) -> subprocess.CompletedProcess[bytes]:
|
||||
assert argv[:2] == ["/usr/bin/xcrun", "swiftc"]
|
||||
if compiler_failure == "unavailable":
|
||||
raise OSError(f"private diagnostic {TEST_PASSWORD}")
|
||||
return subprocess.CompletedProcess(
|
||||
argv,
|
||||
1,
|
||||
stdout=b"",
|
||||
stderr=f"private diagnostic {TEST_PASSWORD}".encode(),
|
||||
)
|
||||
|
||||
with pytest.raises(wifi.HostWifiProfileError) as raised:
|
||||
wifi.check_wifi_profile(
|
||||
helper,
|
||||
TEST_PROFILE_ID,
|
||||
"XGR-OFFLINE",
|
||||
runner=failing_compiler,
|
||||
)
|
||||
|
||||
assert raised.value.reason_code == expected_reason_code
|
||||
assert raised.value.helper_stage == "compile"
|
||||
assert isinstance(raised.value.helper_elapsed_ms, int)
|
||||
assert raised.value.helper_elapsed_ms >= 0
|
||||
assert TEST_PASSWORD not in str(raised.value)
|
||||
|
||||
|
||||
def test_association_rejects_an_uninstalled_platform(
|
||||
tmp_path: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
|
||||
Reference in New Issue
Block a user