zapretgui/tests/test_platforms_engine.py
loop-uh 692e36fc08
All checks were successful
Zapret source guards / validate (push) Successful in 9s
Linux engine: root helper (nftables + nfqws2), Linux runner, unit and polkit files
* platforms/linux/helper: a small root service. The window (normal user)
  asks over a Unix socket (/run/zapretgui/helper.sock, root:zapretgui 0660,
  caller checked with SO_PEERCRED); the helper translates the preset's
  --wf-* options with capture_rules, refuses what Linux cannot do, starts
  nfqws2 (which drops to the caller's uid after opening its queue) and only
  then installs the fixed table "inet zapretgui". On the way out the rules go
  first, the engine second. Leftovers of a crashed helper (table, engine,
  sysctl) are removed at the next start; the unit file adds the same
  "nft delete table" before and after as a second net.
* platforms/linux/runner.py: the runner the program drives on Linux (same
  method names as the Windows runner): reads the preset, rewrites its
  relative bin/lua/lists/user paths to the real trees of the XDG layout,
  validates early (unsupported options are named before anything stops),
  hands the arguments to the helper.
* packaging/linux: systemd unit and polkit rule; scripts/linux_helper_smoke.sh
  runs the whole chain for real (root + nftables + nfqws2, crash recovery
  included); prepare_dev_root.py now also copies the capture filters.
* tests: policy, lifecycle order and failure cases with fake system calls,
  socket round trips, runner, packaging names. The test session keeps the
  shared Windows runner factory for the Windows runtime tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 15:16:33 -05:00

78 lines
3.2 KiB
Python

"""The ``engine`` interface: Windows and the stub hand out the runner of the shared factory; Linux has its own."""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from platforms.linux import PLATFORM as LINUX
from platforms.stub import PLATFORM as STUB
from platforms.windows import PLATFORM as WINDOWS
def test_every_backend_has_an_engine() -> None:
for backend in (WINDOWS, LINUX, STUB):
for name in ("runner", "current_runner", "reset_runner", "invalidate_runner"):
assert callable(getattr(backend.engine, name))
def test_engine_calls_reach_the_factory_at_call_time() -> None:
runner = MagicMock()
for backend in (WINDOWS, STUB):
with (
patch("winws_runtime.runners.runner_factory.get_strategy_runner", return_value=runner) as get,
patch("winws_runtime.runners.runner_factory.get_current_runner", return_value=runner) as current,
patch("winws_runtime.runners.runner_factory.reset_strategy_runner") as reset,
patch("winws_runtime.runners.runner_factory.invalidate_strategy_runner") as invalidate,
):
assert backend.engine.runner("C:/z/winws2.exe") is runner
assert backend.engine.current_runner() is runner
backend.engine.reset_runner()
backend.engine.invalidate_runner()
get.assert_called_once_with("C:/z/winws2.exe")
current.assert_called_once_with()
reset.assert_called_once_with()
invalidate.assert_called_once_with()
def test_neutral_runtime_code_asks_the_platform_for_the_runner() -> None:
import inspect
from winws_runtime.health import post_mortem
from winws_runtime.runtime import control_workers, direct_network, preset_launch_service, sync_shutdown
for module in (post_mortem, control_workers, direct_network, preset_launch_service, sync_shutdown):
source = inspect.getsource(module)
assert "runner_factory" not in source, module.__name__
assert "engine." in source, module.__name__
def test_direct_network_works_without_a_runner() -> None:
from winws_runtime.runtime import direct_network
fake = MagicMock()
fake.engine.current_runner.return_value = None
with patch("platforms.current", return_value=fake):
assert direct_network.direct_network_pause_needed() is False
@pytest.mark.real_engine
def test_linux_engine_keeps_one_runner_and_forgets_it_on_reset() -> None:
from platforms.linux.engine import LinuxEngine
from platforms.linux.runner import LinuxStrategyRunner
engine = LinuxEngine()
assert engine.current_runner() is None
runner = engine.runner("/opt/zapretgui/exe/winws2.exe")
assert isinstance(runner, LinuxStrategyRunner) and engine.runner("anything") is runner
assert engine.current_runner() is runner
with patch.object(runner, "stop") as stop:
engine.reset_runner()
stop.assert_called_once_with()
assert engine.current_runner() is None
runner = engine.runner("x")
with patch.object(runner, "stop_background_watchers") as watchers:
engine.invalidate_runner()
watchers.assert_called_once_with()
assert engine.current_runner() is None