Files
mentor-pdf-exporter/tests/test_engine.py
T

365 lines
15 KiB
Python

import sys
import threading
from pathlib import Path
import pytest
from mentor_pdf_export.engine import run_engine, discover_engine
def test_capture_exit_and_logs(tmp_path):
rc = run_engine([sys.executable, "-c", "import sys; print('hello'); print('bad',file=sys.stderr); sys.exit(3)"], tmp_path, tmp_path / "log", threading.Event(), 10)
assert rc == 3
assert b"hello" in (tmp_path / "log/stdout.log").read_bytes()
assert b"bad" in (tmp_path / "log/stderr.log").read_bytes()
def test_engine_timeout(tmp_path):
with pytest.raises(TimeoutError):
run_engine([sys.executable, "-c", "import time; time.sleep(20)"], tmp_path, tmp_path / "logs", threading.Event(), .3)
def test_pre_cancel_does_not_launch(tmp_path):
cancel = threading.Event(); cancel.set()
with pytest.raises(InterruptedError):
run_engine(["must-not-run.exe"], tmp_path, tmp_path / "logs", cancel, 10)
def test_cancel_running_process(tmp_path):
cancel = threading.Event()
timer = threading.Timer(.3, cancel.set); timer.start()
try:
with pytest.raises(InterruptedError):
run_engine([sys.executable, "-c", "import time; time.sleep(20)"], tmp_path, tmp_path / "logs", cancel, 10)
finally:
timer.cancel()
def test_explicit_invalid_engine_never_falls_back(tmp_path):
with pytest.raises(FileNotFoundError):
discover_engine(tmp_path / "missing.exe")
@pytest.fixture
def owned_process_handles():
"""Retain handles, not recyclable PIDs; clean even assertion failures."""
import os
if os.name != "nt":
pytest.skip("Windows Job Object test")
import win32api
import win32con
import win32event
handles = []
def capture(pid):
handle = win32api.OpenProcess(
win32con.SYNCHRONIZE | win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_TERMINATE,
False, pid,
)
handles.append(handle)
return handle
yield capture
for handle in reversed(handles):
try:
if win32event.WaitForSingleObject(handle, 0) == win32event.WAIT_TIMEOUT:
win32api.TerminateProcess(handle, 1)
assert win32event.WaitForSingleObject(handle, 5000) == win32event.WAIT_OBJECT_0
finally:
handle.Close()
def child_tree_command(pid_file):
child = "import time; time.sleep(30)"
code = (
"import subprocess,time,pathlib; "
f"p=subprocess.Popen([{sys.executable!r},'-B','-c',{child!r}],"
"stdin=subprocess.DEVNULL,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL); "
f"pathlib.Path({str(pid_file)!r}).write_text(str(p.pid)); time.sleep(30)"
)
return [sys.executable, "-B", "-c", code]
def capture_child(pid_file, capture):
import time
deadline = time.monotonic() + 5
while time.monotonic() < deadline:
try:
return capture(int(pid_file.read_text()))
except (FileNotFoundError, ValueError):
time.sleep(.01)
pytest.fail("Synthetic parent did not publish its child PID")
def test_timeout_terminates_owned_child_tree(tmp_path, monkeypatch, owned_process_handles):
import win32event
from mentor_pdf_export import engine
original = engine._create_job
child_handles = []
pid_file = tmp_path / "child.pid"
def assign(process):
owned_process_handles(process.pid)
return original(process)
def started(message):
if "PID=" in message:
child_handles.append(capture_child(pid_file, owned_process_handles))
monkeypatch.setattr(engine, "_create_job", assign)
with pytest.raises(TimeoutError):
run_engine(child_tree_command(pid_file), tmp_path, tmp_path / "logs", threading.Event(), 1.2, started)
assert len(child_handles) == 1
assert win32event.WaitForSingleObject(child_handles[0], 5000) == win32event.WAIT_OBJECT_0
@pytest.mark.parametrize("stop", ["timeout", "cancel"])
def test_delayed_job_binding_cannot_allow_early_child_escape(tmp_path, monkeypatch, owned_process_handles, stop):
import time
import win32event
from mentor_pdf_export import engine
original = engine._create_job
pid_file = tmp_path / "early-child.pid"
ran_before_binding = []
child_handles = []
cancel = threading.Event()
def delayed_assign(process):
owned_process_handles(process.pid)
# Old launch is running: wait for its early child. Fixed launch must
# remain suspended for the entire delay, without executing user code.
deadline = time.monotonic() + .8
while not pid_file.exists() and time.monotonic() < deadline:
time.sleep(.01)
ran_before_binding.append(pid_file.exists())
return original(process)
def started(message):
if "PID=" in message:
child_handles.append(capture_child(pid_file, owned_process_handles))
if stop == "cancel":
cancel.set()
monkeypatch.setattr(engine, "_create_job", delayed_assign)
expected = TimeoutError if stop == "timeout" else InterruptedError
with pytest.raises(expected):
run_engine(child_tree_command(pid_file), tmp_path, tmp_path / "logs", cancel, 2.2, started)
assert len(child_handles) == 1
child_stopped = win32event.WaitForSingleObject(child_handles[0], 1000) == win32event.WAIT_OBJECT_0
assert (ran_before_binding, child_stopped) == ([False], True), "Engine ran before Job binding or its early child survived stop"
def test_windows_native_launch_streams_flags_and_handle_allowlist(tmp_path, monkeypatch, owned_process_handles):
import _winapi
import win32api
import win32con
import win32event
from mentor_pdf_export import engine
original = _winapi.CreateProcess
launches = []
extra = win32event.CreateEvent(None, True, False, None)
win32api.SetHandleInformation(extra, win32con.HANDLE_FLAG_INHERIT, win32con.HANDLE_FLAG_INHERIT)
def create(*args):
flags, info = args[5], args[8]
inherited = info.lpAttributeList.get("handle_list", []) if info.lpAttributeList else []
launches.append((flags, set(inherited), {info.hStdInput, info.hStdOutput, info.hStdError}))
return original(*args)
monkeypatch.setattr(_winapi, "CreateProcess", create)
cwd = tmp_path / "working folder"
cwd.mkdir()
event_code = f"try:\n win32event.SetEvent({int(extra)})\nexcept pywintypes.error:\n pass"
code = (
"import os,sys,win32event,pywintypes; "
"assert sys.stdin.buffer.read()==b''; "
"print(os.getcwd()); print('stdout marker'); print('stderr marker',file=sys.stderr); "
f"exec({event_code!r}); "
"sys.exit(7)"
)
try:
assert engine.run_engine([sys.executable, "-B", "-c", code], cwd, tmp_path / "logs", threading.Event(), 10) == 7
assert len(launches) == 1
flags, inherited, standard = launches[0]
assert flags & 0x00000004, "CREATE_SUSPENDED is required"
assert flags & _winapi.CREATE_NO_WINDOW
assert len(inherited) == 3 and inherited == standard
assert win32event.WaitForSingleObject(extra, 0) == win32event.WAIT_TIMEOUT
assert str(cwd).encode() in (tmp_path / "logs/stdout.log").read_bytes()
assert b"stdout marker" in (tmp_path / "logs/stdout.log").read_bytes()
assert b"stderr marker" in (tmp_path / "logs/stderr.log").read_bytes()
finally:
extra.Close()
def process_handle_count():
import ctypes
from ctypes import wintypes
kernel = ctypes.WinDLL("kernel32", use_last_error=True)
kernel.GetCurrentProcess.restype = wintypes.HANDLE
kernel.GetProcessHandleCount.argtypes = [wintypes.HANDLE, ctypes.POINTER(wintypes.DWORD)]
kernel.GetProcessHandleCount.restype = wintypes.BOOL
count = wintypes.DWORD()
assert kernel.GetProcessHandleCount(kernel.GetCurrentProcess(), ctypes.byref(count))
return count.value
@pytest.mark.parametrize("failure", ["create", "job_create", "job_configure", "assign", "resume", "cancel"])
def test_windows_startup_failures_do_not_execute_or_leak(tmp_path, monkeypatch, owned_process_handles, failure):
import gc
import _winapi
import msvcrt
import win32job
import win32process
import win32event
import mentor_pdf_export.workspace
from mentor_pdf_export import engine
original_create = _winapi.CreateProcess
original_assign = win32job.AssignProcessToJobObject
cancel = threading.Event()
parents = []
marker = tmp_path / "must-not-execute.txt"
def create(*args):
if failure == "create":
raise OSError("injected create failure")
result = original_create(*args)
parents.append(owned_process_handles(result[2]))
return result
def assign(*args):
if failure == "assign":
raise OSError("injected assignment failure")
result = original_assign(*args)
if failure == "cancel":
cancel.set()
return result
def resume(*args):
raise OSError("injected resume failure")
monkeypatch.setattr(_winapi, "CreateProcess", create)
monkeypatch.setattr(win32job, "AssignProcessToJobObject", assign)
if failure == "resume":
monkeypatch.setattr(win32process, "ResumeThread", resume)
if failure in {"job_create", "job_configure"}:
def fail_job(*args):
raise OSError("injected job setup failure")
target = "CreateJobObject" if failure == "job_create" else "SetInformationJobObject"
monkeypatch.setattr(win32job, target, fail_job)
gc.collect()
baseline = process_handle_count()
expected = InterruptedError if failure == "cancel" else OSError
with pytest.raises(expected):
run_engine([sys.executable, "-B", "-c", f"from pathlib import Path; Path({str(marker)!r}).write_text('unsafe')"], tmp_path, tmp_path / "logs", cancel, 10)
assert not marker.exists(), "Suspended process must not execute on setup failure/cancellation"
for handle in parents:
assert win32event.WaitForSingleObject(handle, 5000) == win32event.WAIT_OBJECT_0
gc.collect()
assert process_handle_count() == baseline + len(parents), "Leaked process/thread/job/stdio handle"
@pytest.mark.parametrize("fail_at", [1, 2, 3])
def test_windows_partial_stdio_setup_closes_handles(tmp_path, monkeypatch, owned_process_handles, fail_at):
import _winapi
import msvcrt
import mentor_pdf_export.workspace
from mentor_pdf_export import engine
original = _winapi.DuplicateHandle
calls = []
def duplicate(*args):
calls.append(args)
if len(calls) == fail_at:
raise OSError("injected stdio duplication failure")
return original(*args)
def must_not_launch(*args):
pytest.fail("Incomplete stdio must not create a process")
monkeypatch.setattr(_winapi, "DuplicateHandle", duplicate)
monkeypatch.setattr(_winapi, "CreateProcess", must_not_launch)
baseline = process_handle_count()
with pytest.raises(OSError, match="stdio duplication"):
engine.run_engine([sys.executable, "-B", "-c", "pass"], tmp_path, tmp_path / "logs", threading.Event(), 10)
assert process_handle_count() == baseline
def test_windows_exit_code_259_is_not_mistaken_for_running(tmp_path, owned_process_handles):
assert run_engine([sys.executable, "-B", "-c", "import sys; sys.exit(259)"], tmp_path, tmp_path / "logs", threading.Event(), 10) == 259
def test_callback_error_stops_owned_tree_but_not_other_process(tmp_path, monkeypatch, owned_process_handles):
import subprocess
import win32event
from mentor_pdf_export import engine
original = engine._create_job
pid_file = tmp_path / "callback-child.pid"
children = []
other = subprocess.Popen(
[sys.executable, "-B", "-c", "import time; time.sleep(30)"],
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
creationflags=subprocess.CREATE_NO_WINDOW,
)
try:
other_handle = owned_process_handles(other.pid)
def assign(process):
owned_process_handles(process.pid)
return original(process)
def started(message):
if "PID=" in message:
children.append(capture_child(pid_file, owned_process_handles))
raise RuntimeError("injected event callback failure")
monkeypatch.setattr(engine, "_create_job", assign)
with pytest.raises(RuntimeError, match="event callback failure"):
run_engine(child_tree_command(pid_file), tmp_path, tmp_path / "logs", threading.Event(), 10, started)
assert len(children) == 1
assert win32event.WaitForSingleObject(children[0], 5000) == win32event.WAIT_OBJECT_0
assert win32event.WaitForSingleObject(other_handle, 0) == win32event.WAIT_TIMEOUT
finally:
if other.poll() is None:
other.kill()
other.wait(timeout=10)
def test_success_does_not_kill_background_database_flush(tmp_path,owned_process_handles):
import subprocess
import win32event
marker=tmp_path/'flush-complete.txt';pid_file=tmp_path/'flush.pid'
child=f"import time,pathlib;time.sleep(.6);pathlib.Path({str(marker)!r}).write_text('flushed')"
parent=("import subprocess,pathlib;"
f"p=subprocess.Popen([{sys.executable!r},'-B','-c',{child!r}],stdin=subprocess.DEVNULL,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL);"
f"pathlib.Path({str(pid_file)!r}).write_text(str(p.pid))")
children=[]
def started(message):
if 'PID=' in message:children.append(capture_child(pid_file,owned_process_handles))
assert run_engine([sys.executable,'-B','-c',parent],tmp_path,tmp_path/'logs',threading.Event(),10,started)==0
assert win32event.WaitForSingleObject(children[0],5000)==win32event.WAIT_OBJECT_0
assert marker.read_text()=='flushed'
def test_cancel_after_parent_success_does_not_abort_completed_native_writeback(tmp_path,monkeypatch,owned_process_handles):
import win32event
from mentor_pdf_export import engine
marker=tmp_path/'flush-late-stop.txt';pid_file=tmp_path/'late-stop.pid'
child=f"import time,pathlib;time.sleep(.6);pathlib.Path({str(marker)!r}).write_text('flushed')"
parent=("import subprocess,pathlib;"
f"p=subprocess.Popen([{sys.executable!r},'-B','-c',{child!r}],stdin=subprocess.DEVNULL,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL);"
f"pathlib.Path({str(pid_file)!r}).write_text(str(p.pid))")
children=[];parents=[];original=engine._create_job;cancel=threading.Event()
def bind(process):
parents.append(owned_process_handles(process.pid));return original(process)
def started(message):
if 'PID=' in message:
children.append(capture_child(pid_file,owned_process_handles))
assert win32event.WaitForSingleObject(parents[0],5000)==win32event.WAIT_OBJECT_0
cancel.set()
monkeypatch.setattr(engine,'_create_job',bind)
with pytest.raises(InterruptedError):
run_engine([sys.executable,'-B','-c',parent],tmp_path,tmp_path/'logs',cancel,10,started)
assert win32event.WaitForSingleObject(children[0],5000)==win32event.WAIT_OBJECT_0
assert marker.read_text()=='flushed'