* security state: fetch has the engine generate it, every launch gives the core its engine, and CI launches with a fixed ready directory The core replaced the intermediates list with the security state (intermediates, OneCRL and the CRLite filters, generated by the engine and copied into every profile). The CLI's fetch calls update_security_state(engine=<the binary it installed>); server.py's one write_user_js passes engine=<the executable of the launch> (no engine_argv: a generation is its own headless run, the core's default line). STEALTHFOX_SECURITY_STATE replaces STEALTHFOX_INTERMEDIATES in every workflow that launches the browser (scripts/ci_lab_security_state.py, ci/security-state/ with a result and no data, pinned by sha256); the unit tests' conftest patches ensure_security_state. Tests: tests/test_cli.py (fetch), tests/test_a_launch_gives_the_core_its_engine_for_the_security_state.py, tests/test_ci_lab_security_state.py, tests/test_ci_workflows_set_the_security_state.py, each red on the code of main. * e2e: the engine's security state - generation, a normal launch, a certificate revoked in a CRLite delta refused at the first navigation, a launch with no state wrapper.e2e.securitystate, both legs, in the candidate's wrapper-e2e job: the engine launched directly in its generate mode (exit, the contract's keys, the hashes are the sha256 of the files, the bytes of security_state/) and through the core's generate; a normal launch writes no security-state pref and re-imports nothing; with Mozilla's CRLite test filters and a CA on Mozilla's default test key, a leaf with the serial and SCTs of revoked-in-delta.example.com is refused with SEC_ERROR_REVOKED_CERTIFICATE at the first navigation, a valid one loads, and the revoked one loads with no state; a launch with no or an empty state starts. Red on firefox-67 (no generate mode); the last test is a guard. * ci: the required list only grows, so the two test files of the intermediates lab keep their names and guard that the old road stays gone The judge of main refused the list of the branch: wrapper.unit.all.* lost tests/test_ci_lab_intermediates.py and tests/test_ci_workflows_set_the_intermediates_list.py. Both files come back with tests that the lab's files and steps are gone and that every leg that had the step sets the security state (red on main, 5 of 5). The unit suites count what the candidate collected (2183 and 2184) plus these 5: 2188 on Linux, 2189 on Windows. * ci: the unit rows count what the candidate's gate collected (2168 linux, 2169 windows) * Release 0.58.0: invisible-core 68.80.0 on firefox-68 (the security state of a retail profile) * test: the frames of a goto are counted once all are announced, within the goto's own deadline With Fission the innermost frame of a-b-a lives in a third process and its announcement may reach the client after the top document's load (2 of 3 frames 0.22 s after the goto, run 44888497316). A frame that never comes is still red.
323 lines
13 KiB
Python
323 lines
13 KiB
Python
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from invisible_playwright import cli
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _fetch_does_not_download_the_security_state(monkeypatch):
|
|
"""`fetch` also looks for a newer security state (`update_security_state`), which reaches Mozilla and runs the engine: a test that is not about it does not."""
|
|
monkeypatch.setattr("invisible_playwright.cli.update_security_state", lambda *a, **k: (None, False))
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_version_subcommand():
|
|
r = subprocess.run(
|
|
[sys.executable, "-m", "invisible_playwright", "version"],
|
|
capture_output=True, text=True, check=True,
|
|
)
|
|
assert "firefox-" in r.stdout
|
|
assert "invisible_playwright" in r.stdout.lower()
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_help_subcommand():
|
|
r = subprocess.run(
|
|
[sys.executable, "-m", "invisible_playwright", "--help"],
|
|
capture_output=True, text=True,
|
|
)
|
|
assert r.returncode == 0
|
|
assert "fetch" in r.stdout
|
|
assert "version" in r.stdout
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_the_cli_offers_exactly_two_commands():
|
|
"""Six subcommands became two, and this is what keeps it there.
|
|
|
|
`path`, `clear-cache`, `doctor` and `fetch --force` were each a step somebody
|
|
had to know to take, in a package whose promise is that the browser handles
|
|
itself. Their behaviour did not go: `fetch` checks every cached tree against
|
|
the seal on every run - which is what `doctor` did, and it was the thing most
|
|
worth doing and least likely to be typed - and prints the path as its last
|
|
line, which is what `path` did with the added guarantee that the thing it
|
|
names exists.
|
|
|
|
`clear-cache` is the one deliberately NOT folded in: the cache root belongs
|
|
to invisible_core, so pruning trees no seal points at would delete an engine
|
|
this package did not put there. It was written for invisible_firefox, whose
|
|
repository was deleted on 2026-08-18 while the package stayed on the index;
|
|
the reason holds for anything else built on the core. `version` prints the
|
|
location instead.
|
|
|
|
Asserted as an exact set, not a subset. A subset check passes while the
|
|
surface grows back one convenience at a time, which is how it got to six.
|
|
"""
|
|
import argparse
|
|
|
|
parser = cli.build_parser()
|
|
actions = [a for a in parser._actions if isinstance(a, argparse._SubParsersAction)]
|
|
assert len(actions) == 1, actions
|
|
assert set(actions[0].choices) == {"fetch", "version"}, sorted(actions[0].choices)
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_fetch_takes_no_arguments():
|
|
"""The tag went with the commands. The seal decides which engine this build
|
|
runs and `verify_engine` refuses anything else, so a tag on the command line
|
|
could only ever name something that would then be rejected - and `--force` is
|
|
unnecessary once every run verifies: a tree is replaced because it does not
|
|
match, not because a flag was passed."""
|
|
import argparse
|
|
|
|
parser = cli.build_parser()
|
|
sub = next(a for a in parser._actions if isinstance(a, argparse._SubParsersAction))
|
|
fetch = sub.choices["fetch"]
|
|
optionals = {o for a in fetch._actions for o in a.option_strings}
|
|
assert optionals == {"-h", "--help"}, optionals
|
|
positionals = [a.dest for a in fetch._actions if not a.option_strings]
|
|
assert not positionals, positionals
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_fetch_prints_the_path_as_its_last_line(tmp_path, monkeypatch, capsys):
|
|
"""`$(invisible-playwright fetch)` has to be a path.
|
|
|
|
That is the whole replacement for the `path` subcommand, so the path must be
|
|
the last line of stdout and nothing else may follow it. The mismatch report
|
|
goes to stderr for exactly this reason.
|
|
"""
|
|
fake = tmp_path / "firefox.exe"
|
|
fake.write_text("x")
|
|
monkeypatch.setattr("invisible_playwright.cli.ensure_binary", lambda: fake)
|
|
monkeypatch.setattr("invisible_core.download.iter_cached_engines", lambda: [])
|
|
|
|
rc = cli.main(["fetch"])
|
|
|
|
out = capsys.readouterr().out
|
|
assert rc == 0
|
|
assert out.strip().splitlines()[-1] == str(fake)
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_fetch_looks_for_a_newer_security_state_after_the_engine_and_for_that_engine(tmp_path, monkeypatch, capsys):
|
|
"""The known-bad is a `fetch` that still calls the removed `update_intermediates` (or none at all): a user who runs `fetch` asking for what is current keeps a stale
|
|
state, or the engine is never asked to generate one. The engine is made present, then `update_security_state` is called with THAT engine (and
|
|
`update_intermediates` is not), in that order, and the path of the engine is still the last line."""
|
|
fake = tmp_path / "firefox.exe"
|
|
fake.write_text("x")
|
|
calls = []
|
|
monkeypatch.setattr("invisible_playwright.cli.ensure_binary", lambda: calls.append("engine") or fake)
|
|
monkeypatch.setattr("invisible_playwright.cli.update_security_state", lambda *a, **k: calls.append(("update", a, k)) or (None, False))
|
|
monkeypatch.setattr("invisible_core.update_security_state", lambda *a, **k: calls.append(("core", a, k)) or (None, False))
|
|
monkeypatch.setattr("invisible_core.update_intermediates", lambda *a, **k: calls.append("intermediates"), raising=False)
|
|
monkeypatch.setattr("invisible_playwright.cli.update_intermediates", lambda *a, **k: calls.append("intermediates"), raising=False)
|
|
monkeypatch.setattr("invisible_core.download.iter_cached_engines", lambda: [])
|
|
|
|
rc = cli.main(["fetch"])
|
|
|
|
assert rc == 0
|
|
assert calls == ["engine", ("update", (), {"engine": fake})]
|
|
assert capsys.readouterr().out.strip().splitlines()[-1] == str(fake)
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_a_failing_security_state_is_a_warning_and_changes_neither_the_exit_nor_the_last_line(tmp_path, monkeypatch, capsys):
|
|
fake = tmp_path / "firefox.exe"
|
|
fake.write_text("x")
|
|
|
|
def refuses(*a, **k):
|
|
raise RuntimeError("no route to host")
|
|
|
|
monkeypatch.setattr("invisible_playwright.cli.ensure_binary", lambda: fake)
|
|
monkeypatch.setattr("invisible_playwright.cli.update_security_state", refuses)
|
|
monkeypatch.setattr("invisible_core.download.iter_cached_engines", lambda: [])
|
|
|
|
rc = cli.main(["fetch"])
|
|
|
|
cap = capsys.readouterr()
|
|
assert rc == 0
|
|
assert "no route to host" in cap.err and "security state" in cap.err
|
|
assert cap.out.strip().splitlines()[-1] == str(fake)
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_a_mismatching_cached_tree_is_reported_but_does_not_stop_the_fetch(
|
|
tmp_path, monkeypatch, capsys,
|
|
):
|
|
"""This is what `doctor` used to do, and why it is not fatal.
|
|
|
|
A tree left over from an older seal is the ordinary case, not a fault:
|
|
`ensure_binary` refuses it by construction and fetches the sealed one, so
|
|
reporting it and carrying on IS the repair. It goes to stderr so it cannot
|
|
end up inside a captured path.
|
|
"""
|
|
fake = tmp_path / "firefox.exe"
|
|
fake.write_text("x")
|
|
stale = tmp_path / "firefox-14"
|
|
monkeypatch.setattr("invisible_playwright.cli.ensure_binary", lambda: fake)
|
|
monkeypatch.setattr("invisible_core.download.iter_cached_engines",
|
|
lambda: [(stale, object())])
|
|
monkeypatch.setattr("invisible_core.seal.engine_problems",
|
|
lambda ident, seal: ["application.ini Version '150.0.1' != '151.0'"])
|
|
|
|
rc = cli.main(["fetch"])
|
|
|
|
cap = capsys.readouterr()
|
|
assert rc == 0
|
|
assert "firefox-14" in cap.err and "150.0.1" in cap.err
|
|
assert "firefox-14" not in cap.out
|
|
assert cap.out.strip().splitlines()[-1] == str(fake)
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_no_subcommand_errors():
|
|
with pytest.raises(SystemExit) as exc:
|
|
cli.main([])
|
|
assert exc.value.code != 0
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_unknown_subcommand_errors():
|
|
with pytest.raises(SystemExit) as exc:
|
|
cli.main(["bogus"])
|
|
assert exc.value.code != 0
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_a_removed_subcommand_is_refused_rather_than_silently_ignored():
|
|
"""`doctor`, `path` and `clear-cache` were real commands people scripted.
|
|
|
|
They must fail loudly now, not be swallowed. argparse gives that for free
|
|
with a subparser set, and this is what stops somebody re-adding a permissive
|
|
fallback that turns an unknown command into a no-op exit 0.
|
|
"""
|
|
for gone in ("doctor", "path", "clear-cache"):
|
|
with pytest.raises(SystemExit) as exc:
|
|
cli.main([gone])
|
|
assert exc.value.code != 0, gone
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_no_test_invokes_the_cli_with_arguments_it_rejects():
|
|
"""A test that calls a removed flag is a red workflow with a green suite.
|
|
|
|
Measured 2026-08-04. `test_release_e2e.py` still ran `fetch --force` three
|
|
days after `--force` went with four of the six subcommands in 0.5.0. Every
|
|
run of that file exited 2 on "unrecognized arguments" - and the DEFAULT suite
|
|
stayed green the whole time, because that test is `e2e`-marked and therefore
|
|
deselected. One stale argument, three red workflows: `e2e` on main, and
|
|
`user-install` on two release tags.
|
|
|
|
A sibling guard already forbade `--force` in the engine-mismatch MESSAGE
|
|
(`test_seal_wire_version.py`). It checked what the package TELLS a user to
|
|
run and never what the tests themselves run, so the removed flag survived in
|
|
the one place that actually executes it.
|
|
|
|
This validates every invocation against the real parser rather than against a
|
|
list of removed names. A list would have to be maintained by the same person
|
|
who forgot to update the call site.
|
|
"""
|
|
import argparse
|
|
import pathlib
|
|
import re
|
|
|
|
parser = cli.build_parser()
|
|
tests_dir = pathlib.Path(__file__).resolve().parent
|
|
# Matches a subprocess argv of the form -m invisible_playwright <args>.
|
|
# No example of one is written here: the first version put a sample
|
|
# invocation in a comment on this line, and the scan flagged its own comment.
|
|
# That is the defect `18-gate-inventory.md` records about the RFC 5737 gate,
|
|
# which flagged its own docstring the day it was written.
|
|
call = re.compile(
|
|
r'"-m",\s*"invisible_playwright"\s*,\s*((?:"[^"]*"\s*,?\s*)+)')
|
|
|
|
problems = []
|
|
for path in sorted(tests_dir.rglob("test_*.py")):
|
|
if "playwright-upstream" in path.parts:
|
|
continue
|
|
# Comments stripped first, for the reason above.
|
|
source = "\n".join(
|
|
line.split("#", 1)[0]
|
|
for line in path.read_text(encoding="utf-8", errors="replace").splitlines())
|
|
for match in call.finditer(source):
|
|
args = re.findall(r'"([^"]*)"', match.group(1))
|
|
args = [a for a in args if a]
|
|
if not args:
|
|
continue
|
|
line = source[:match.start()].count("\n") + 1
|
|
try:
|
|
parser.parse_args(args)
|
|
except SystemExit as exit_:
|
|
# argparse exits 0 for --help and --version, which are valid
|
|
# invocations, and 2 for a parse error. Only the second is a
|
|
# defect. The first version of this gate treated both alike and
|
|
# reported `--help` as broken.
|
|
if exit_.code:
|
|
problems.append(f"{path.name}:{line} -> {args}")
|
|
|
|
assert not problems, (
|
|
"these tests invoke the CLI with arguments its own parser refuses, so "
|
|
"they exit 2 wherever they actually run:\n " + "\n ".join(problems)
|
|
+ "\nThe surface is `fetch` and `version`, and `fetch` takes nothing.")
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_no_workflow_invokes_the_cli_with_arguments_it_rejects():
|
|
"""A manual workflow is still executable code, even when normal CI is green.
|
|
|
|
Measured 2026-09-04: two workflows still invoked the removed ``path``
|
|
subcommand a month after the CLI became ``fetch`` + ``version``. The
|
|
Windows launch matrix failed in all three cells before reaching its launch
|
|
probe; the manual WebRTC gate had not run since the removal. The existing
|
|
sibling scans Python tests only, so neither call site was in its perimeter.
|
|
|
|
Parse literal workflow invocations against the real parser. Shell syntax
|
|
starts at the first punctuation token and is not part of the CLI argv.
|
|
Comment-only lines are excluded so this explanation cannot flag itself.
|
|
"""
|
|
import re
|
|
import shlex
|
|
|
|
parser = cli.build_parser()
|
|
root = Path(__file__).resolve().parents[1]
|
|
workflows = root / ".github" / "workflows"
|
|
call = re.compile(r"python\s+-m\s+invisible_playwright\s+(.+)")
|
|
shell_punctuation = {"|", "||", "&", "&&", ";", ")", ">", ">>"}
|
|
|
|
problems = []
|
|
for path in sorted(workflows.glob("*.y*ml")):
|
|
for line_number, line in enumerate(
|
|
path.read_text(encoding="utf-8", errors="replace").splitlines(), 1
|
|
):
|
|
if line.lstrip().startswith("#"):
|
|
continue
|
|
match = call.search(line)
|
|
if match is None:
|
|
continue
|
|
lexer = shlex.shlex(
|
|
match.group(1), posix=True, punctuation_chars="|&;()<>")
|
|
lexer.whitespace_split = True
|
|
args = []
|
|
for token in lexer:
|
|
if token in shell_punctuation:
|
|
break
|
|
args.append(token)
|
|
try:
|
|
parser.parse_args(args)
|
|
except SystemExit as exit_:
|
|
if exit_.code:
|
|
problems.append(
|
|
f"{path.name}:{line_number} -> {args}")
|
|
|
|
assert not problems, (
|
|
"these workflows invoke the CLI with arguments its own parser refuses, "
|
|
"so the jobs fail before reaching the assertion they exist to run:\n "
|
|
+ "\n ".join(problems)
|
|
+ "\nThe surface is `fetch` and `version`, and `fetch` takes nothing.")
|