Skip to content

The agent that grows with you

test(webapp): gate host-specific Webapp tests with platforms(), not skipif or a faked OS

Tekniumadb1227e42a9
diff --git a/gateway/webapp_listener.py b/gateway/webapp_listener.py
index bb6d12b9014..686d1c8b46e 100644
--- a/gateway/webapp_listener.py
+++ b/gateway/webapp_listener.py
@@ -243,13 +243,13 @@ def _close_to_other_users(folder: Path) -> None:
logger.warning("Could not make %s user-only: %s", folder, exc)
-def _one_use_links_work() -> bool:
+def _one_use_links_work(platform: str = sys.platform) -> bool:
"""Whether a browser the server opens can redeem a one-use launch link here. Redemption needs
proof that the browser's connection belongs to the gateway's OS user
(``web_routers.webapp._peer_is_server_user``) on Linux, or a browser launcher whose arguments other
users cannot read (Windows). macOS and the BSDs offer neither, so there the link is printed for
the user to open by hand and spends without that proof."""
- return sys.platform in ("linux", "win32")
+ return platform in ("linux", "win32")
#: How every missing-bundle reason starts; ``hermes webapp --status`` asks for no restart after it.
diff --git a/hermes_cli/web_routers/webapp.py b/hermes_cli/web_routers/webapp.py
index 2369c1c80e8..6a16110afaf 100644
--- a/hermes_cli/web_routers/webapp.py
+++ b/hermes_cli/web_routers/webapp.py
@@ -128,7 +128,7 @@ def _loopback_peer_uid(client: tuple[str, int], server: tuple[str, int]) -> int
return None
-def _peer_is_server_user(request: Request) -> bool:
+def _peer_is_server_user(request: Request, platform: str = sys.platform) -> bool:
"""Whether this request's connection belongs to the OS user running the server.
Linux authenticates the connection owner. Windows needs no check: a process's command line,
@@ -138,9 +138,9 @@ def _peer_is_server_user(request: Request) -> bool:
be redeemed. Authenticated child-window tickets and the launch tickets ``hermes webapp`` prints for
the user to open by hand (``mint_printed_launch_ticket``) do not use this launcher channel.
"""
- if sys.platform == "win32":
+ if platform == "win32":
return True
- if sys.platform != "linux":
+ if platform != "linux":
return False
client, server = request.client, request.scope.get("server")
if client is None or not server:
diff --git a/tests/gateway/test_webapp_listener.py b/tests/gateway/test_webapp_listener.py
index c28aad9872c..ce815701224 100644
--- a/tests/gateway/test_webapp_listener.py
+++ b/tests/gateway/test_webapp_listener.py
@@ -325,23 +325,26 @@ def test_cancelling_the_gateway_while_the_webapp_listener_starts_cancels_it(monk
_start_runtime_api(monkeypatch, tmp_path, "webapp:\n enabled: true\n", refuse=asyncio.CancelledError())
[email protected]("posix", [True, False])
-def test_only_posix_lets_the_webapps_fixed_port_bind_again_at_once(monkeypatch, posix):
- # A restarted gateway must take the Webapp's port back while the old connections sit in
- # TIME_WAIT. On Windows SO_REUSEADDR would let another process bind the port, so it stays off.
+def _reuse_address(port: int) -> bool:
import socket
from gateway import run_api
- monkeypatch.setattr(run_api, "_REUSE_FIXED_PORT", posix)
- with run_api._listener_socket(socket.AF_INET, socket.SOCK_STREAM, 0, webapp.DEFAULT_PORT) as fixed, \
- run_api._listener_socket(socket.AF_INET, socket.SOCK_STREAM, 0, 0) as ephemeral:
- assert bool(fixed.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)) is posix
- assert not ephemeral.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) # main's private listener
+ with run_api._listener_socket(socket.AF_INET, socket.SOCK_STREAM, 0, port) as listener:
+ return bool(listener.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR))
-def test_the_fixed_port_switch_follows_the_platform():
- from gateway import run_api
- assert run_api._REUSE_FIXED_PORT is (os.name != "nt")
+def test_on_posix_the_webapps_fixed_port_binds_again_at_once():
+ # A restarted gateway must take the Webapp's port back while the old connections sit in TIME_WAIT.
+ assert _reuse_address(webapp.DEFAULT_PORT) is True
+ assert _reuse_address(0) is False # main's private listener
+
+
+def test_on_windows_the_webapps_fixed_port_never_lets_another_process_bind_it():
+ # On Windows SO_REUSEADDR would let another process bind the port, so it stays off.
+ assert _reuse_address(webapp.DEFAULT_PORT) is False
+ assert _reuse_address(0) is False # main's private listener
def test_a_fixed_port_binds_again_while_the_last_gateways_connections_linger():
diff --git a/tests/gateway/test_webapp_listener_fallback.py b/tests/gateway/test_webapp_listener_fallback.py
index f3335f4800f..d5d962074ad 100644
--- a/tests/gateway/test_webapp_listener_fallback.py
+++ b/tests/gateway/test_webapp_listener_fallback.py
@@ -7,7 +7,6 @@ Desktop, CLI and TUI clients still connect, the mount carries none of the Webapp
in a child process (``fixtures/webapp_fallback_peer.py``) with its own ``HERMES_HOME``.
"""
import ipaddress
-import sys
from urllib.parse import urlsplit
import pytest
@@ -61,7 +60,7 @@ def test_a_webapp_that_starts_keeps_the_browser_credential(tmp_path):
assert report["token_rotated"] is False
[email protected](sys.platform != "linux", reason="only Linux lets two SO_REUSEADDR sockets bind one address")
[email protected]("linux") # only Linux lets two SO_REUSEADDR sockets bind one address
def test_the_webapp_owns_its_port_before_mounting_the_dashboard(tmp_path):
# Two processes may both bind the Webapp's port under SO_REUSEADDR; the first to listen owns it.
# The gateway listens as soon as it binds, so a rival that tries while the dashboard mounts loses.
@@ -82,7 +81,7 @@ def test_the_webapp_port_accepts_connections_before_the_dashboard_mounts(tmp_pat
assert report["status"]["state"] == "serving"
[email protected](sys.platform != "win32", reason="SO_EXCLUSIVEADDRUSE is a Windows socket option")
[email protected]("windows") # SO_EXCLUSIVEADDRUSE is a Windows socket option
def test_on_windows_the_webapps_fixed_port_is_held_exclusively():
# Without exclusive use another process may bind the Webapp's port with SO_REUSEADDR (a more
# specific address then takes those connections). Main's private ephemeral listener is unchanged.
diff --git a/tests/hermes_cli/test_webapp_file_urls.py b/tests/hermes_cli/test_webapp_file_urls.py
index 82897c44f51..067cb78e22b 100644
--- a/tests/hermes_cli/test_webapp_file_urls.py
+++ b/tests/hermes_cli/test_webapp_file_urls.py
@@ -34,26 +34,29 @@ def webapp_client(monkeypatch, tmp_path):
client.close()
[email protected](params=["local", "unc"])
-def files(request, tmp_path):
- """A clip, an image and a page, named as the Webapp meets them: by their own path (a drive path on
- Windows) or, on Windows, through a UNC host (the local administrative share, ``\\\\127.0.0.1\\C$``)."""
+def files(tmp_path):
+ """A clip, an image and a page, named as the Webapp meets them by their own path (a drive path on
+ Windows)."""
renders = tmp_path / "renders"
renders.mkdir()
(renders / "clip one.mp4").write_bytes(b"hello")
(renders / "shot one.png").write_bytes(b"\x89PNG\r\n\x1a\nimage")
(renders / "report #1.html").write_text("<p>report</p>", encoding="utf-8")
- if request.param == "unc":
- if os.name != "nt":
- pytest.skip("UNC hosts are Windows paths")
- drive, rest = os.path.splitdrive(str(renders))
- renders = Path(f"\\\\127.0.0.1\\{drive[0]}${rest}")
- if not renders.is_dir():
- pytest.skip("the local administrative share is not reachable")
return renders
-def test_the_webapp_plays_a_file_url_through_its_stream_ticket(webapp_client, files):
+def unc_files(files):
+ """The same files through a UNC host: the local administrative share, ``\\\\127.0.0.1\\C$``."""
+ drive, rest = os.path.splitdrive(str(files))
+ renders = Path(f"\\\\127.0.0.1\\{drive[0]}${rest}")
+ if not renders.is_dir():
+ pytest.skip("the local administrative share is not reachable")
+ return renders
+
+
+def _plays_through_its_stream_ticket(webapp_client, files):
url = (files / "clip one.mp4").as_uri()
minted = webapp_client.post("/api/files/ticket", json={"route": "stream", "path": url, "profile": PROFILE})
assert minted.status_code == 200, (url, minted.text)
@@ -69,7 +72,7 @@ def test_the_webapp_plays_a_file_url_through_its_stream_ticket(webapp_client, fi
assert (stream.status_code, stream.content) == (206, b"ell"), (url, stream.text)
-def test_the_webapp_reads_images_and_previews_by_native_path(webapp_client, files):
+def _reads_images_and_previews_by_native_path(webapp_client, files):
# ``str(path)`` is what the shared parser gives for ``path.as_uri()`` (``apps/shared/src/file-url.ts``).
shot = files / "shot one.png"
read = webapp_client.get("/api/fs/read-data-url", params={"path": str(shot), "profile": PROFILE})
@@ -80,3 +83,23 @@ def test_the_webapp_reads_images_and_previews_by_native_path(webapp_client, file
preview = webapp_client.get("/api/fs/read-text", params={"path": str(page), "profile": PROFILE})
assert preview.status_code == 200, (page, preview.text)
assert preview.json()["text"] == "<p>report</p>"
+
+
[email protected]("linux", "windows")
+def test_the_webapp_plays_a_file_url_through_its_stream_ticket(webapp_client, files):
+ _plays_through_its_stream_ticket(webapp_client, files)
+
+
[email protected]("linux", "windows")
+def test_the_webapp_reads_images_and_previews_by_native_path(webapp_client, files):
+ _reads_images_and_previews_by_native_path(webapp_client, files)
+
+
+def test_the_webapp_plays_a_unc_file_url_through_its_stream_ticket(webapp_client, unc_files):
+ _plays_through_its_stream_ticket(webapp_client, unc_files)
+
+
+def test_the_webapp_reads_unc_images_and_previews_by_native_path(webapp_client, unc_files):
+ _reads_images_and_previews_by_native_path(webapp_client, unc_files)
diff --git a/tests/hermes_cli/test_webapp_launch.py b/tests/hermes_cli/test_webapp_launch.py
index 2454a9fb03a..d3b33fc896d 100644
--- a/tests/hermes_cli/test_webapp_launch.py
+++ b/tests/hermes_cli/test_webapp_launch.py
@@ -111,30 +111,24 @@ def _loopback_request():
@pytest.mark.parametrize("platform", ["freebsd14", "openbsd7", "sunos5"])
-def test_where_no_peer_check_exists_a_launch_ticket_is_never_spent(monkeypatch, platform):
+def test_where_no_peer_check_exists_a_launch_ticket_is_never_spent(platform):
"""Other Unixes show every process's argv to all local users by default and Hermes cannot tell
who owns a loopback connection there. As on macOS, the launcher prints a link for the user to open
by hand rather than passing a launch ticket to a browser launcher, and a launch ticket is never
spent."""
- import sys
-
from gateway import webapp_listener
from hermes_cli.web_routers import webapp
- monkeypatch.setattr(sys, "platform", platform)
- assert webapp._peer_is_server_user(_loopback_request()) is False
- assert webapp_listener._one_use_links_work() is False
+ assert webapp._peer_is_server_user(_loopback_request(), platform=platform) is False
+ assert webapp_listener._one_use_links_work(platform=platform) is False
-def test_windows_spends_a_launch_ticket_without_a_peer_check(monkeypatch):
+def test_windows_spends_a_launch_ticket_without_a_peer_check():
"""Windows does not show a process's command line to other standard users (reading it needs a
handle the process's DACL grants only its own user, SYSTEM and elevated administrators), so the
ticket in the browser launcher's argv stays the user's own, as the stored credential does."""
- import sys
-
from gateway import webapp_listener
from hermes_cli.web_routers import webapp
- monkeypatch.setattr(sys, "platform", "win32")
- assert webapp._peer_is_server_user(_loopback_request()) is True
- assert webapp_listener._one_use_links_work() is True
+ assert webapp._peer_is_server_user(_loopback_request(), platform="win32") is True
+ assert webapp_listener._one_use_links_work(platform="win32") is True