Skip to content

The agent that grows with you

test(windows): host facts are arguments, so no test pretends to be on Windows

Teknium31870dda2c52
diff --git a/hermes_cli/gateway_runtime.py b/hermes_cli/gateway_runtime.py
index 862cd090e15..c6eae5236d7 100644
--- a/hermes_cli/gateway_runtime.py
+++ b/hermes_cli/gateway_runtime.py
@@ -251,11 +251,11 @@ def _owner_pid(home: Path) -> int | None:
return None
-def _refused(home: Path) -> GatewayDiscovery:
- """The owner's control channel refused this same-user client. On Windows name the boundary
- (an elevated owner, or one in another logon session) with its remedy instead of the generic
- ``authorization`` that reads as a credential failure (#139845, #140252)."""
- if _NATIVE_WINDOWS and (pid := _owner_pid(home)) is not None:
+def _refused(home: Path, *, windows: bool = _NATIVE_WINDOWS) -> GatewayDiscovery:
+ """The owner's control channel refused this same-user client. On Windows (*windows*, the host
+ fact) name the boundary (an elevated owner, or one in another logon session) with its remedy
+ instead of the generic ``authorization`` that reads as a credential failure (#139845, #140252)."""
+ if windows and (pid := _owner_pid(home)) is not None:
from gateway.runtime_bootstrap_windows import probe_refused_owner
try:
named = probe_refused_owner(pid)
diff --git a/hermes_cli/update_cmd_maint.py b/hermes_cli/update_cmd_maint.py
index 35a8c66b619..26e6b1db851 100644
--- a/hermes_cli/update_cmd_maint.py
+++ b/hermes_cli/update_cmd_maint.py
@@ -21,7 +21,7 @@ from datetime import UTC
logger = logging.getLogger("hermes_cli.update_cmd")
-def _repair_windows_home_acl() -> None:
+def _repair_windows_home_acl(*, elevated: bool | None = None) -> None:
"""Restore access for the filtered UAC token after an elevated update.
An elevated process can create home-tree entries whose DACL only grants
@@ -32,11 +32,11 @@ def _repair_windows_home_acl() -> None:
(``hermes_dir_acl.grant_tree_user_access``): ``icacls /T`` follows junctions, so a
same-user junction inside the home would have turned the elevated grant into full
control of e.g. ``C:\\Program Files\\X``.
+
+ *elevated* is the host fact (default: probed; False on every host but an elevated Windows one).
"""
- if sys.platform != "win32":
- return
from hermes_dir_acl import grant_tree_user_access, process_is_elevated
- if not process_is_elevated():
+ if not (process_is_elevated() if elevated is None else elevated):
# Only an elevated update leaves Administrators-only entries, and only an elevated token
# can rewrite them; a recursive grant over the whole home on every update costs minutes.
return
@@ -45,7 +45,7 @@ def _repair_windows_home_acl() -> None:
home = get_hermes_home()
# The home itself is the operator's choice and may be a link: pin its real directory and walk
# below it (links beneath are still never followed).
- granted, skipped, failed = grant_tree_user_access(home, pin_root=True)
+ granted, skipped, failed = grant_tree_user_access(home, pin_root=True, elevated=True)
if failed and not granted:
path, reason = failed[0]
raise RuntimeError(f"could not restore user access to {home}: {path}: {reason}")
diff --git a/hermes_dir_acl.py b/hermes_dir_acl.py
index 954ac4a7d0a..42087b2f810 100644
--- a/hermes_dir_acl.py
+++ b/hermes_dir_acl.py
@@ -232,13 +232,19 @@ def _warn_refused(path: Path, exc: BaseException) -> None:
"point HERMES_HOME (or the link) at the real directory.", path, exc)
-def grant_user_access(path: str | Path) -> bool:
+def _elevated(elevated: bool | None) -> bool:
+ """The host fact every grant decision takes: *elevated* as passed, else probed (an elevated
+ Windows process; :func:`process_is_elevated` is False on every other host)."""
+ return process_is_elevated() if elevated is None else elevated
+
+
+def grant_user_access(path: str | Path, *, elevated: bool | None = None) -> bool:
"""Give the token's user an inheriting full-control ACE on *path* when this process is elevated.
True when a grant was attempted and did not fail; False off Windows, when not elevated, when
*path* is a junction/symlink/reparse point (refused, one warning), or when the grant failed
- (logged; the directory itself still exists)."""
- if sys.platform != "win32" or not process_is_elevated():
+ (logged; the directory itself still exists). *elevated* defaults to the probed host fact."""
+ if not _elevated(elevated):
return False
target = Path(path)
try:
@@ -285,7 +291,8 @@ def iter_tree_without_reparse_points(root: str | Path, skipped: list[Path]) -> I
yield child, False
-def grant_tree_user_access(root: str | Path, *, pin_root: bool = False) -> tuple[int, list[Path], list[tuple[Path, str]]]:
+def grant_tree_user_access(root: str | Path, *, pin_root: bool = False,
+ elevated: bool | None = None) -> tuple[int, list[Path], list[tuple[Path, str]]]:
"""Recursive :func:`grant_user_access` (the elevated post-update repair): grant the token's user
full control on *root* and every directory and file under it, inheriting on directories.
@@ -295,8 +302,8 @@ def grant_tree_user_access(root: str | Path, *, pin_root: bool = False) -> tuple
Returns ``(granted, skipped, failed)``: *skipped* are the reparse points (and hard-linked files)
that were neither granted nor entered, *failed* the ``(path, reason)`` grants that raised. A
- no-op ``(0, [], [])`` off Windows or when not elevated."""
- if sys.platform != "win32" or not process_is_elevated():
+ no-op ``(0, [], [])`` off Windows or when not elevated (*elevated* defaults to the probe)."""
+ if not _elevated(elevated):
return 0, [], []
root = Path(os.path.realpath(root) if pin_root else os.path.abspath(root))
real_root = os.path.realpath(root)
@@ -332,7 +339,8 @@ def _identity(path: Path) -> tuple[Path, int, int] | None:
return path, st.st_dev, st.st_ino
-def make_user_dir(path: str | Path, mode: int = 0o777, *, parents: bool = False, exist_ok: bool = False) -> Path:
+def make_user_dir(path: str | Path, mode: int = 0o777, *, parents: bool = False, exist_ok: bool = False,
+ elevated: bool | None = None) -> Path:
"""``Path.mkdir`` (same arguments and defaults) that keeps an elevated Windows creation usable
by the same user unelevated.
@@ -340,9 +348,10 @@ def make_user_dir(path: str | Path, mode: int = 0o777, *, parents: bool = False,
this call created (missing parents included) is granted the user's inheriting full-control ACE,
and an existing leaf (the repair case) once per directory object per process, except that a
component that is a junction/symlink/reparse point stops the grants with one warning. An
- existing leaf without ``exist_ok`` is still granted before the ``FileExistsError``."""
+ existing leaf without ``exist_ok`` is still granted before the ``FileExistsError``. *elevated*
+ is the host fact (default: probed); production passes nothing."""
target = Path(path)
- if sys.platform != "win32" or not process_is_elevated():
+ if not _elevated(elevated):
target.mkdir(mode=mode, parents=parents, exist_ok=exist_ok)
return target
created = []
@@ -376,12 +385,13 @@ def _grant_dirs(target: Path, to_grant: list[Path], fresh: set[Path] = frozenset
key = _identity(directory)
if key is None or (directory not in fresh and key in _granted):
continue
- if grant_user_access(directory):
+ if grant_user_access(directory, elevated=True):
_granted.add(key)
-def unusable_dir_remedy(path: str | Path) -> str:
- """The one-line repair for a directory this process may not use (owner from an elevated run)."""
- if os.name == "nt":
+def unusable_dir_remedy(path: str | Path, *, windows: bool | None = None) -> str:
+ """The one-line repair for a directory this process may not use (owner from an elevated run).
+ *windows* is the host fact (default: this host)."""
+ if (os.name == "nt") if windows is None else windows:
return f'from an elevated prompt run: icacls "{path}" /reset /t'
return f'run: sudo chown -R "$(id -un)" "{path}"'
diff --git a/tests/gateway/test_native_media_acl_provenance.py b/tests/gateway/test_native_media_acl_provenance.py
index 64553396de4..49714608c16 100644
--- a/tests/gateway/test_native_media_acl_provenance.py
+++ b/tests/gateway/test_native_media_acl_provenance.py
@@ -2,8 +2,8 @@
``native_inputs_root()`` resolves the store, which erases a link planted below the home
(``cache/documents`` -> elsewhere). An elevated grant on that resolved path landed outside the home
-with no home fence (``expected=None``). Only the elevated-Windows branch selection and the native
-writer are faked; capture, mkdir and the links are real disk operations.
+with no home fence (``expected=None``). Only the elevation fact and the native writer are
+replaced (no host faking); capture, mkdir and the links are real disk operations.
"""
import os
from pathlib import Path
@@ -16,7 +16,6 @@ from gateway import session_ingress_media as media
def _elevated(monkeypatch, home):
monkeypatch.setenv("HERMES_HOME", str(home))
- monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
monkeypatch.setattr(hermes_dir_acl, "_granted", set())
grants = []
diff --git a/tests/gateway/test_scoped_lock_dir_unusable.py b/tests/gateway/test_scoped_lock_dir_unusable.py
index 247f86e91b5..42cdb9657d5 100644
--- a/tests/gateway/test_scoped_lock_dir_unusable.py
+++ b/tests/gateway/test_scoped_lock_dir_unusable.py
@@ -19,8 +19,8 @@ from gateway.platforms.base import BasePlatformAdapter
from gateway.restart import is_global_startup_conflict
from gateway.run_startup import GatewayStartupMixin
-pytestmark = pytest.mark.skipif(os.name == "nt" or os.geteuid() == 0, # windows-footgun: ok — skipped on nt
- reason="POSIX mode bits; root ignores them")
+pytestmark = [pytest.mark.platforms("posix"),
+ pytest.mark.skipif(getattr(os, "geteuid", lambda: 1)() == 0, reason="root ignores mode bits")]
class _StubAdapter(BasePlatformAdapter):
diff --git a/tests/hermes_cli/test_gateway_refused_owner.py b/tests/hermes_cli/test_gateway_refused_owner.py
index 44c584127eb..44b501f186c 100644
--- a/tests/hermes_cli/test_gateway_refused_owner.py
+++ b/tests/hermes_cli/test_gateway_refused_owner.py
@@ -3,7 +3,7 @@
#139845: an elevated gateway's pipe/process refuses the non-elevated Desktop (``OpenProcess`` err=5).
#140252: a session-0 S4U boot-task gateway won the control pipe; the interactive Desktop can never
reach it. Both surfaced as ``Gateway inaccessible (authorization)``, which reads as a credential
-problem. The probe facts are injected here (no Windows host); ``test_refused_owner_windows.py``
+problem. The probe facts and the host fact (``windows=True``) are passed in here, never faked; ``test_refused_owner_windows.py``
runs the real probes on Windows CI.
"""
import json
@@ -63,7 +63,6 @@ def _refusing_owner(monkeypatch, tmp_path: Path, named, owner):
home = tmp_path / "home"
home.mkdir(exist_ok=True)
(home / "gateway.pid").write_text(json.dumps(_record(home, owner.pid)), encoding="utf-8")
- monkeypatch.setattr(runtime, "_NATIVE_WINDOWS", True, raising=False)
seen = []
monkeypatch.setattr(native, "probe_refused_owner", lambda pid: seen.append(pid) or named)
return home, seen
@@ -73,7 +72,7 @@ def test_refusal_names_the_other_session_owner_with_its_remedy(monkeypatch, tmp_
pid = gateway_owner.pid
named = ("gateway_other_session", native.refused_owner_remedy("gateway_other_session", pid, 0))
home, seen = _refusing_owner(monkeypatch, tmp_path, named, gateway_owner)
- result = runtime._refused(home)
+ result = runtime._refused(home, windows=True)
assert seen == [pid]
assert (result.state, result.reason_code) == ("inaccessible", "gateway_other_session")
assert result.detail and f"PID {pid}" in result.detail and "session 0" in result.detail
@@ -83,37 +82,48 @@ def test_refusal_names_the_other_session_owner_with_its_remedy(monkeypatch, tmp_
def test_refusal_names_the_elevated_owner_with_its_remedy(monkeypatch, tmp_path, gateway_owner):
named = ("gateway_elevated", native.refused_owner_remedy("gateway_elevated", gateway_owner.pid, 1))
home, _ = _refusing_owner(monkeypatch, tmp_path, named, gateway_owner)
- result = runtime._refused(home)
+ result = runtime._refused(home, windows=True)
assert result.reason_code == "gateway_elevated"
assert result.detail and "administrator" in result.detail and "hermes gateway stop" in result.detail
def test_unnamed_or_unprobeable_refusal_keeps_authorization(monkeypatch, tmp_path, gateway_owner):
home, _ = _refusing_owner(monkeypatch, tmp_path, None, gateway_owner)
- assert runtime._refused(home).reason_code == "authorization"
+ assert runtime._refused(home, windows=True).reason_code == "authorization"
monkeypatch.setattr(native, "probe_refused_owner", lambda pid: (_ for _ in ()).throw(OSError("x")))
- assert runtime._refused(home).reason_code == "authorization"
+ assert runtime._refused(home, windows=True).reason_code == "authorization"
(home / "gateway.pid").unlink()
- assert runtime._refused(home).reason_code == "authorization"
+ assert runtime._refused(home, windows=True).reason_code == "authorization"
-def test_discovery_maps_a_refused_pipe_through_the_named_state(monkeypatch, tmp_path, gateway_owner):
+def test_discovery_routes_a_refused_control_channel_through_refused(monkeypatch, tmp_path):
"""The real discovery path: ``query_identify`` raising PermissionError is what Windows' pipe
- ``CreateFile`` err=5 / peer-identity refusal produces."""
+ ``CreateFile`` err=5 / peer-identity refusal produces; it reaches ``_refused`` for this home.
+ The Windows naming itself is ``_refused(home, windows=True)`` above."""
from hermes_cli import gateway_runtime_discovery as discovery
- named = ("gateway_elevated", native.refused_owner_remedy("gateway_elevated", gateway_owner.pid, 1))
- home, _ = _refusing_owner(monkeypatch, tmp_path, named, gateway_owner)
+ home = tmp_path / "home"
+ home.mkdir()
+ seen = []
+ monkeypatch.setattr(runtime, "_refused", lambda h: seen.append(Path(h)) or runtime.GatewayDiscovery(
+ "inaccessible", reason_code="gateway_elevated"))
monkeypatch.setattr(discovery, "query_identify",
lambda *_a, **_k: (_ for _ in ()).throw(PermissionError("runtime control pipe inaccessible")))
result = runtime.discover_gateway_endpoint(home, timeout=1)
assert (result.state, result.reason_code) == ("inaccessible", "gateway_elevated")
+ assert [p.resolve() for p in seen] == [home.resolve()]
+
+
+def test_off_windows_a_refusal_stays_authorization_without_probing(monkeypatch, tmp_path, gateway_owner):
+ named = ("gateway_elevated", "x")
+ home, seen = _refusing_owner(monkeypatch, tmp_path, named, gateway_owner)
+ assert runtime._refused(home).reason_code == "authorization" and seen == []
# ── Review-9 (e): never name a PID that is not provably this home's gateway ─────────────────────
def _probe_recorder(monkeypatch):
- monkeypatch.setattr(runtime, "_NATIVE_WINDOWS", True, raising=False)
seen = []
named = lambda pid: seen.append(pid) or ("gateway_elevated", native.refused_owner_remedy("gateway_elevated", pid, 1))
monkeypatch.setattr(native, "probe_refused_owner", named)
@@ -128,7 +138,7 @@ def test_a_recycled_pid_is_never_named(monkeypatch, tmp_path, gateway_owner):
record["start_time"] = (record["start_time"] or 0) + 12345
(home / "gateway.pid").write_text(json.dumps(record), encoding="utf-8")
seen = _probe_recorder(monkeypatch)
- assert runtime._refused(home).reason_code == "authorization" and seen == []
+ assert runtime._refused(home, windows=True).reason_code == "authorization" and seen == []
@pytest.mark.parametrize("shape", ["no start time", "not a gateway", "another home"])
@@ -147,7 +157,7 @@ def test_an_unverifiable_owner_is_never_named(monkeypatch, tmp_path, shape):
record["hermes_home"] = str(tmp_path / "other-profile")
(home / "gateway.pid").write_text(json.dumps(record), encoding="utf-8")
seen = _probe_recorder(monkeypatch)
- assert runtime._refused(home).reason_code == "authorization" and seen == []
+ assert runtime._refused(home, windows=True).reason_code == "authorization" and seen == []
finally:
child.stdin.close()
child.wait(timeout=10)
@@ -161,4 +171,4 @@ def test_a_stale_pid_file_falls_through_to_the_lock_owner(monkeypatch, tmp_path,
(home / "gateway.pid").write_text(json.dumps({"pid": dead.pid, "start_time": 1}), encoding="utf-8")
(home / "gateway.lock").write_text(json.dumps(_record(home, gateway_owner.pid)), encoding="utf-8")
seen = _probe_recorder(monkeypatch)
- assert runtime._refused(home).reason_code == "gateway_elevated" and seen == [gateway_owner.pid]
+ assert runtime._refused(home, windows=True).reason_code == "gateway_elevated" and seen == [gateway_owner.pid]
diff --git a/tests/hermes_cli/test_update_windows_home_acl.py b/tests/hermes_cli/test_update_windows_home_acl.py
index 77a588b8a65..fd76dab9440 100644
--- a/tests/hermes_cli/test_update_windows_home_acl.py
+++ b/tests/hermes_cli/test_update_windows_home_acl.py
@@ -2,15 +2,15 @@
import os
import subprocess
-import sys
+
+import pytest
import hermes_dir_acl
from hermes_cli import update_cmd_maint as maint
-def _fake_elevated_windows(monkeypatch, tmp_path, elevated=True):
+def _fake_elevated(monkeypatch, tmp_path, elevated=True):
granted, runs = [], []
- monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "home"))
sid_row = '"host\\\\user","S-1-5-21-1-2-3-1001"\n' # whoami /user /fo csv /nh
@@ -29,11 +29,11 @@ def test_repair_runs_only_from_an_elevated_update(monkeypatch, tmp_path):
"""A non-elevated update created nothing Administrators-only and could not rewrite it anyway;
the recursive grant over the whole home is skipped there."""
(tmp_path / "home").mkdir()
- granted, runs = _fake_elevated_windows(monkeypatch, tmp_path, elevated=False)
- maint._repair_windows_home_acl()
+ granted, runs = _fake_elevated(monkeypatch, tmp_path, elevated=False)
+ maint._repair_windows_home_acl() # the production call: probed, unelevated
+ maint._repair_windows_home_acl(elevated=False)
assert granted == [] and runs == []
- monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
- maint._repair_windows_home_acl()
+ maint._repair_windows_home_acl(elevated=True)
assert granted == [str(tmp_path / "home")]
@@ -45,17 +45,15 @@ def test_repair_never_recurses_through_a_link_inside_the_home(monkeypatch, tmp_p
outside = tmp_path / "Program Files" / "X"
(outside / "bin").mkdir(parents=True)
(home / "cache" / "images").symlink_to(outside, target_is_directory=True)
- granted, runs = _fake_elevated_windows(monkeypatch, tmp_path)
+ granted, runs = _fake_elevated(monkeypatch, tmp_path)
maint._repair_windows_home_acl()
assert not any(argv and argv[0] == "icacls" and "/T" in argv for argv in runs), runs
assert granted == [str(home), str(home / "cache")]
def test_repair_stays_owed_when_nothing_could_be_granted(monkeypatch, tmp_path):
- import pytest
-
(tmp_path / "home").mkdir()
- _fake_elevated_windows(monkeypatch, tmp_path)
+ _fake_elevated(monkeypatch, tmp_path)
def denied(path, *_a, **_k):
raise OSError("access denied")
@@ -75,16 +73,14 @@ def test_repair_walks_below_an_operator_linked_home(monkeypatch, tmp_path):
outside = tmp_path / "outside"
outside.mkdir()
(real / "jn").symlink_to(outside, target_is_directory=True)
- granted, _runs = _fake_elevated_windows(monkeypatch, tmp_path)
+ granted, _runs = _fake_elevated(monkeypatch, tmp_path)
(tmp_path / "home").symlink_to(real, target_is_directory=True)
maint._repair_windows_home_acl()
assert sorted(granted) == sorted([str(real), str(real / "state"), str(real / "state" / "s.json")])
def test_repair_that_repaired_nothing_stays_owed(monkeypatch, tmp_path):
- import pytest
-
- _fake_elevated_windows(monkeypatch, tmp_path)
+ _fake_elevated(monkeypatch, tmp_path)
(tmp_path / "home").symlink_to(tmp_path / "gone", target_is_directory=True) # dangling
with pytest.raises(RuntimeError, match="nothing was repaired"):
maint._repair_windows_home_acl()
diff --git a/tests/test_hermes_dir_acl.py b/tests/test_hermes_dir_acl.py
index 05f83bb04f3..0a2cae5d5a4 100644
--- a/tests/test_hermes_dir_acl.py
+++ b/tests/test_hermes_dir_acl.py
@@ -1,5 +1,9 @@
"""``make_user_dir`` contract off Windows and its grant selection (the Windows ACL effect itself is
-proved by ``test_hermes_dir_acl_windows.py`` on Windows CI)."""
+proved by ``test_hermes_dir_acl_windows.py`` on Windows CI).
+
+No test pretends to be on Windows: elevation is a host fact the functions take as data (or the
+process_is_elevated probe stands in for it on a production path), and only the native writer
+(``_grant_with_pywin32``) is replaced, to record what an elevated grant would have touched."""
import os
from pathlib import Path
@@ -8,13 +12,13 @@ import pytest
import hermes_dir_acl
def test_off_windows_it_is_plain_mkdir(tmp_path, monkeypatch):
calls = []
- monkeypatch.setattr(hermes_dir_acl, "grant_user_access", lambda p: calls.append(p))
+ monkeypatch.setattr(hermes_dir_acl, "grant_user_access", lambda p, **_k: calls.append(p))
target = hermes_dir_acl.make_user_dir(tmp_path / "a" / "b", 0o700, parents=True)
assert target.is_dir() and calls == []
- if os.name != "nt":
- assert (target.stat().st_mode & 0o777) == 0o700
+ assert (target.stat().st_mode & 0o777) == 0o700
with pytest.raises(FileExistsError):
hermes_dir_acl.make_user_dir(target)
with pytest.raises(FileNotFoundError):
@@ -23,9 +27,8 @@ def test_off_windows_it_is_plain_mkdir(tmp_path, monkeypatch):
def test_elevated_grants_every_created_dir_and_an_existing_leaf_once(tmp_path, monkeypatch):
granted = []
- monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
- monkeypatch.setattr(hermes_dir_acl, "grant_user_access", lambda p: granted.append(p) or True)
+ monkeypatch.setattr(hermes_dir_acl, "grant_user_access", lambda p, **_k: granted.append(p) or True)
monkeypatch.setattr(hermes_dir_acl, "_granted", set())
leaf = hermes_dir_acl.make_user_dir(tmp_path / "state" / "gateway-locks", parents=True, exist_ok=True)
@@ -42,9 +45,8 @@ def test_elevated_grants_every_created_dir_and_an_existing_leaf_once(tmp_path, m
def test_elevated_existing_leaf_without_exist_ok_is_repaired_then_raises(tmp_path, monkeypatch):
"""Path.mkdir contract stays (FileExistsError), but the left-behind dir is granted first."""
granted = []
- monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
- monkeypatch.setattr(hermes_dir_acl, "grant_user_access", lambda p: granted.append(p) or True)
+ monkeypatch.setattr(hermes_dir_acl, "grant_user_access", lambda p, **_k: granted.append(p) or True)
monkeypatch.setattr(hermes_dir_acl, "_granted", set())
left = tmp_path / "state"
left.mkdir()
@@ -56,7 +58,6 @@ def test_elevated_existing_leaf_without_exist_ok_is_repaired_then_raises(tmp_pat
def test_a_failed_grant_never_fails_the_mkdir_and_is_retried(tmp_path, monkeypatch):
- monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
monkeypatch.setattr(hermes_dir_acl, "_granted", set())
attempts = []
@@ -71,11 +72,10 @@ def test_a_failed_grant_never_fails_the_mkdir_and_is_retried(tmp_path, monkeypat
assert target.is_dir() and attempts == [target, target]
-def test_remedy_names_the_platform_repair(monkeypatch):
- monkeypatch.setattr(hermes_dir_acl.os, "name", "nt")
- assert hermes_dir_acl.unusable_dir_remedy("C:\\x") == 'from an elevated prompt run: icacls "C:\\x" /reset /t'
- monkeypatch.setattr(hermes_dir_acl.os, "name", "posix")
- assert "chown" in hermes_dir_acl.unusable_dir_remedy("/x")
+def test_remedy_names_the_platform_repair():
+ remedy = hermes_dir_acl.unusable_dir_remedy
+ assert remedy("C:\\x", windows=True) == 'from an elevated prompt run: icacls "C:\\x" /reset /t'
+ assert "chown" in remedy("/x", windows=False)
# ── P2-5: an elevated grant never follows a junction/symlink out of the Hermes home ──────────────
@@ -90,7 +90,6 @@ def elevated_windows(tmp_path, monkeypatch):
outside.mkdir(parents=True)
(outside / "app.exe").write_text("x")
monkeypatch.setenv("HERMES_HOME", str(home))
- monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
monkeypatch.setattr(hermes_dir_acl, "_granted", set())
touched = []
@@ -139,7 +138,6 @@ def test_make_user_dir_still_grants_real_dirs_under_a_linked_home(tmp_path, monk
linked = tmp_path / "linked-home"
linked.symlink_to(real, target_is_directory=True)
monkeypatch.setenv("HERMES_HOME", str(linked))
- monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
monkeypatch.setattr(hermes_dir_acl, "_granted", set())
calls = []
@@ -197,9 +195,9 @@ def test_tree_grant_never_touches_a_target_outside_the_home(elevated_windows):
def test_tree_grant_is_a_noop_unelevated(tmp_path, monkeypatch):
- monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
- monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: False)
- assert hermes_dir_acl.grant_tree_user_access(tmp_path) == (0, [], [])
+ monkeypatch.setattr(hermes_dir_acl, "_grant_with_pywin32", lambda *_a, **_k: pytest.fail("granted"))
+ assert hermes_dir_acl.grant_tree_user_access(tmp_path, elevated=False) == (0, [], [])
+ assert hermes_dir_acl.grant_tree_user_access(tmp_path) == (0, [], []) # the probe off an elevated Windows host
@pytest.mark.parametrize("shape", ["symlink-only", "symlink-before-a-file", "hardlink-only", "nested-hardlink"])
@@ -243,9 +241,8 @@ def test_a_recreated_dir_is_granted_again(tmp_path, monkeypatch):
"""Review-9 (c): the grant memo was keyed by pathname, so media release deleting a digest dir
and the next capture recreating it at the same path left the new dir without the grant."""
granted = []
- monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
- monkeypatch.setattr(hermes_dir_acl, "grant_user_access", lambda p: granted.append(p) or True)
+ monkeypatch.setattr(hermes_dir_acl, "grant_user_access", lambda p, **_k: granted.append(p) or True)
monkeypatch.setattr(hermes_dir_acl, "_granted", set())
target = tmp_path / "native-inputs" / ("0" * 64)
hermes_dir_acl.make_user_dir(target, 0o700, parents=True, exist_ok=True)
@@ -264,7 +261,6 @@ def test_media_release_then_recapture_grants_the_recreated_digest_dir(tmp_path,
home = tmp_path / "home"
home.mkdir()
monkeypatch.setenv("HERMES_HOME", str(home))
- monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
monkeypatch.setattr(hermes_dir_acl, "_granted", set())
grants = []