Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.9 KB
Raw Blame
"""Process-policy checks with all resource and ctypes calls mocked."""

import ctypes
import importlib.util
import sys
from pathlib import Path
from unittest.mock import Mock, call

import pytest

from local_deep_research.security import process_dump_policy as policy
from local_deep_research.security.process_dump_policy import (
    ProcessDumpPolicyError,
)


@pytest.fixture(autouse=True)
def mocked_system(monkeypatch, isolate_server_process_dump_policy):
    resource = Mock(RLIMIT_CORE=4)
    resource.getrlimit.return_value = (0, 0)
    prctl = Mock(return_value=0)
    library = Mock(prctl=prctl)
    loader = Mock(return_value=library)
    monkeypatch.setattr(policy, "_resource", resource)
    monkeypatch.setattr(policy, "_os_name", "posix")
    monkeypatch.setattr(policy, "_platform", "linux")
    # The helper imports ctypes lazily, so patch the module it will get.
    monkeypatch.setattr(ctypes, "CDLL", loader)
    # Restore the real helper only after every OS boundary is a fake.
    monkeypatch.setattr(
        policy,
        "enforce_process_dump_policy",
        isolate_server_process_dump_policy,
    )
    return resource, prctl, loader


def test_linux_policy_sets_and_reads_back_both_controls(mocked_system):
    resource, prctl, loader = mocked_system

    assert policy.enforce_process_dump_policy() is True

    resource.setrlimit.assert_called_once_with(resource.RLIMIT_CORE, (0, 0))
    resource.getrlimit.assert_called_once_with(resource.RLIMIT_CORE)
    loader.assert_called_once_with(None, use_errno=True)
    assert prctl.call_args_list == [
        call(policy._PR_SET_DUMPABLE, 0, 0, 0, 0),
        call(policy._PR_GET_DUMPABLE, 0, 0, 0, 0),
    ]
    assert prctl.argtypes == [ctypes.c_int] + [ctypes.c_ulong] * 4
    assert prctl.restype is ctypes.c_int


def test_each_startup_reapplies_policy_instead_of_caching(mocked_system):
    resource, prctl, _loader = mocked_system

    policy.enforce_process_dump_policy()
    policy.enforce_process_dump_policy()

    assert resource.setrlimit.call_count == 2
    assert prctl.call_count == 4


def test_other_posix_platform_uses_core_limits(monkeypatch, mocked_system):
    resource, _prctl, loader = mocked_system
    monkeypatch.setattr(policy, "_platform", "darwin")

    assert policy.enforce_process_dump_policy() is True

    resource.getrlimit.assert_called_once_with(resource.RLIMIT_CORE)
    loader.assert_not_called()


def test_unsupported_platform_reports_scope(monkeypatch, mocked_system):
    resource, _prctl, loader = mocked_system
    monkeypatch.setattr(policy, "_os_name", "nt")

    with pytest.warns(RuntimeWarning, match="operating system"):
        assert policy.enforce_process_dump_policy() is False

    resource.setrlimit.assert_not_called()
    loader.assert_not_called()


def test_posix_missing_resource_module_stops_startup(monkeypatch):
    monkeypatch.setattr(policy, "_resource", None)

    with pytest.raises(ProcessDumpPolicyError, match="unavailable"):
        policy.enforce_process_dump_policy()


@pytest.mark.parametrize("operation", ["setrlimit", "getrlimit"])
def test_resource_failure_stops_before_linux_control(operation, mocked_system):
    resource, _prctl, loader = mocked_system
    getattr(resource, operation).side_effect = OSError("synthetic refusal")

    with pytest.raises(ProcessDumpPolicyError, match="core-file limits"):
        policy.enforce_process_dump_policy()

    loader.assert_not_called()


@pytest.mark.parametrize("limits", [(1, 1), (0, 1), (1, 0)])
def test_resource_readback_must_confirm_both_zero(limits, mocked_system):
    resource, _prctl, loader = mocked_system
    resource.getrlimit.return_value = limits

    with pytest.raises(ProcessDumpPolicyError, match="remain enabled"):
        policy.enforce_process_dump_policy()

    loader.assert_not_called()


def test_unavailable_linux_control_stops_startup(mocked_system):
    _resource, _prctl, loader = mocked_system
    loader.side_effect = OSError("synthetic unavailable library")

    with pytest.raises(ProcessDumpPolicyError, match="unavailable"):
        policy.enforce_process_dump_policy()


def test_missing_ctypes_module_stops_startup(monkeypatch, mocked_system):
    _resource, _prctl, loader = mocked_system
    # A None entry makes ``import ctypes`` raise ImportError, as on an
    # interpreter built without _ctypes.
    monkeypatch.setitem(sys.modules, "ctypes", None)

    with pytest.raises(ProcessDumpPolicyError, match="unavailable"):
        policy.enforce_process_dump_policy()

    loader.assert_not_called()


def test_non_linux_posix_does_not_need_ctypes(monkeypatch, mocked_system):
    resource, _prctl, _loader = mocked_system
    monkeypatch.setattr(policy, "_platform", "darwin")
    monkeypatch.setitem(sys.modules, "ctypes", None)

    assert policy.enforce_process_dump_policy() is True

    resource.setrlimit.assert_called_once_with(resource.RLIMIT_CORE, (0, 0))


def test_missing_prctl_symbol_stops_startup(mocked_system):
    _resource, _prctl, loader = mocked_system
    loader.return_value = Mock(spec=[])

    with pytest.raises(ProcessDumpPolicyError, match="unavailable"):
        policy.enforce_process_dump_policy()


@pytest.mark.parametrize("results", [(-1, 0), (0, -1), (0, 1)])
def test_linux_set_and_readback_must_succeed(results, mocked_system):
    _resource, prctl, _loader = mocked_system
    prctl.side_effect = results

    with pytest.raises(ProcessDumpPolicyError, match="could not be disabled"):
        policy.enforce_process_dump_policy()


def test_import_does_not_change_process_policy(monkeypatch, mocked_system):
    resource, _prctl, loader = mocked_system
    monkeypatch.setitem(sys.modules, "resource", resource)
    spec = importlib.util.spec_from_file_location(
        "import_only_process_policy", Path(policy.__file__)
    )
    module = importlib.util.module_from_spec(spec)

    spec.loader.exec_module(module)

    assert callable(module.enforce_process_dump_policy)
    resource.setrlimit.assert_not_called()
    resource.getrlimit.assert_not_called()
    loader.assert_not_called()