ci: the host oracle's journey ends with a geolocation step, the candidate must start no Windows location utility process, and stock Firefox is the negative control

This commit is contained in:
feder-cr committed 2026-11-22 05:52:34 +01:00
1 parent d4f4f86d88
commit f7450cfd5b
2 files changed
+454

No files matched your search

+223
View File
@@ -6,6 +6,12 @@ change (a helper that is no longer spawned, a path of the machine it did not tou
TRACE of the run, from the launch to the exit of the browser (the quiet point is the exit of the process, not a wait), after the same short
journey (the first steps of the fake web's P4 walk, tests/fakeweb/profile_p4.py, no internet, driven by the stock Playwright client).
After the four steps of the journey comes a fifth: the geolocation permission is granted to a page of the oracle's own and `getCurrentPosition` is called and awaited
(the callback or the error, an event). On Windows the candidate must start NO utility process of the kind WINDOWS_UTILS (`-sandboxingKind 3`) and load none of the modules of
the platform's location provider (an absolute rule, `geolocation_problems`); the NEGATIVE CONTROL is Playwright's own Firefox on the same runner with the same step
(`run_stock`, command `stock`), which MUST start that process: if it does not the step does not reach the provider and the leg is a broken control (exit 2).
On Linux the same step runs and its footprint is compared like the rest.
Three things are compared, over the whole trace:
* the processes, by KIND, from the command lines of the launch: the parent, `-contentproc` with its type, `-sandboxingKind N` and
@@ -66,7 +72,9 @@ import subprocess
import sys
import tempfile
import time
import threading
import xml.etree.ElementTree as ET
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
HERE = Path(__file__).resolve().parent
@@ -85,6 +93,29 @@ JOURNEY = (("open home", "s1_open_home"),
("hover and click a teaser", "s2_hover_click_teaser"),
("scroll to the bottom, next article", "s3_scroll_next"),
("nav Category", "s7_nav_category"))
#: The step that follows the journey (not a method of the fake web's journey: it has a page of the oracle's own, `start_geo_lab`): the geolocation
#: permission is granted to that page and `navigator.geolocation.getCurrentPosition` is called. It ends with the callback or the error, whichever the
#: engine gives (an event, never a clock): what the page is answered is recorded (`geo`) and never judged, the host is what is judged.
GEO_STEP = "geolocation granted, getCurrentPosition"
GEO_PATH = "/geo"
#: The position the core declares to our engine, made up (the engine's own contract test, juggler/tests/geolocation_windows_provider_never_selected.test.mjs, uses
#: the same); the stock control is given none, so that it has to ask the platform.
GEO_DECLARED = {"latitude": 45.0, "longitude": 9.0, "accuracy": 1500}
#: The page asks once and ends with a position or an error. `timeout` is the acquisition limit of the request (a stock Firefox with no answer from its
#: provider would wait for ever): it is never read by a verdict, the answer is recorded and not judged.
GEO_READ = """() => new Promise(resolve => navigator.geolocation.getCurrentPosition(
position => resolve({answer: 'position'}),
error => resolve({answer: 'error', code: error.code}),
{timeout: %d, maximumAge: 0}))""" % 20000
#: The Windows location provider of Firefox runs in the utility process of the kind WINDOWS_UTILS: `-sandboxingKind 3` (the enum of
#: ipc/glue/UtilityProcessSandboxing.h: GENERIC_UTILITY 0, UTILITY_AUDIO_DECODING_WMF 1, MF_MEDIA_ENGINE_CDM 2, WINDOWS_UTILS 3, WINDOWS_FILE_DIALOG 4).
#: Our engine has no caller of it left (acceptance contract, item 4, of the engine's geolocation_windows_provider_never_selected.test.mjs).
LOCATION_UTILITY_KIND = "3"
#: The modules the provider loads (a Windows trace sees a library as a file opened): the engine's contract names the first four as ABSENT (item 3 of the
#: same test); the WinRT geolocation modules are matched by their prefix.
LOCATION_MODULES = frozenset({"geolocation.dll", "locationframeworkps.dll", "locationapi.dll", "wlanapi.dll", "capabilityaccessmanagerclient.dll"})
LOCATION_MODULE_PREFIXES = ("windows.devices.geolocation",)
#: What a seed's footprint must hold at least, per category of leaf: a footprint that holds less did not see the run.
#: Provisional: the first A/A on both systems gives the real numbers (they are in diff.json, "leaves").
#: There is no floor for `net`: a run that opens no netlink socket and sends nothing to a public address is a run that can exist.
@@ -1019,6 +1050,54 @@ def _by(seed_footprint: dict, leaf: str) -> list:
return list(seed_footprint.get("touched_by" if category == "file" else "spawned_by", {}).get(name, []))
def _windows() -> bool:
return sys.platform == "win32"
def location_utility_kinds(kinds) -> list:
"""The kinds of process (`utility/sandboxingKind=3/notForBrowser`) that are the utility process of the Windows location provider."""
return sorted(k for k in kinds if "sandboxingKind=%s" % LOCATION_UTILITY_KIND in k.split("/"))
def location_modules(paths) -> list:
"""The paths (host paths of a footprint, either separator) of a module the Windows location provider loads."""
out = []
for path in paths:
name = _base(path).lower()
if name in LOCATION_MODULES or name.startswith(LOCATION_MODULE_PREFIXES):
out.append(path)
return sorted(out)
def geolocation_problems(seed_footprint: dict, where: str) -> list:
"""What a candidate's launch did to the host that only the platform's location provider does: a process of the kind `-sandboxingKind 3` and
the modules it loads. Absolute (it does not read the published engine): the candidate answers from the core's declaration and asks nobody."""
problems = []
kinds = location_utility_kinds(seed_footprint.get("kinds", {}))
if kinds:
problems.append("%s side B: the engine started the Windows location utility process (-sandboxingKind %s): %s"
% (where, LOCATION_UTILITY_KIND, ", ".join(kinds)))
modules = location_modules(seed_footprint.get("files", []))
if modules:
problems.append("%s side B: the engine loaded location modules: %s" % (where, ", ".join(modules[:10])))
return problems
def stock_control_problems(stock: dict) -> list:
"""What makes the stock control BROKEN (exit 2, the A/B says nothing): its own trace or step failed, or stock Firefox, given the permission and the
same request, did not start the Windows location utility process, so the step does not reach the platform provider."""
seed = stock.get("stock") or {}
problems = ["stock control: %s" % p for p in seed.get("problems", [])]
problems += ["stock control: step %s (%s) failed: %s" % (st.get("n"), st.get("name"), st.get("error")) for st in seed.get("steps", []) if not st.get("ok")]
if not seed:
problems.append("stock control: the run made no footprint")
elif not location_utility_kinds(seed.get("kinds", {})):
problems.append("stock control: stock Firefox did not start the Windows location utility process (-sandboxingKind %s) after getCurrentPosition with the "
"permission granted: the step does not reach the platform provider, so a candidate without that process proves nothing"
% LOCATION_UTILITY_KIND)
return problems
def judge(a: dict, b: dict, expected: list | None = None, a_a: bool = False, platform: str | None = None,
floors: dict | None = None) -> dict:
"""The verdict on two footprints (the document `run` writes). `expected` is the declared differences, applied only when `a_a` is false.
@@ -1054,6 +1133,9 @@ def judge(a: dict, b: dict, expected: list | None = None, a_a: bool = False, pla
if va != vb:
differences.append({"seed": seed, "category": category_of(leaf), "leaf": leaf, "a": _clip(va), "b": _clip(vb),
"by": {"A": _by(seeds_a[seed], leaf), "B": _by(seeds_b[seed], leaf)}})
if os_name == "windows" and not a_a:
for where, fp in [("seed %s" % seed, seeds_b[seed]) for seed in sorted(seeds_b)] + ([("warm-up", b["warmup"])] if b.get("warmup") else []):
problems += geolocation_problems(fp, where)
stale = []
if not a_a:
for entry in expected or []:
@@ -1153,6 +1235,58 @@ def _context(binary: str, windows: bool, work: Path) -> Context:
return Context("linux", app=app, temp=temp, cache={os.environ.get("XDG_CACHE_HOME", "")})
def start_geo_lab():
"""The oracle's own lab page of the geolocation step: one static page on 127.0.0.1 (a secure context). Returns (origin, stop)."""
body = b"<!doctype html><meta charset=utf-8><title>geo</title><p>geo"
class Handler(BaseHTTPRequestHandler):
timeout = 5
def log_message(self, *args):
pass
def do_GET(self):
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
def stop():
server.shutdown()
server.server_close()
thread.join()
return "http://127.0.0.1:%d" % server.server_address[1], stop
def geolocation_step(browser, origin: str, declared: dict | None, n: int) -> dict:
"""The step after the journey: a context (with the declared position, or none for the stock control), the permission granted to the lab page,
the page opened, `getCurrentPosition` called and awaited. Returns the step as the journey's are recorded, with `geo` (what the page was answered)."""
step = {"n": n, "name": GEO_STEP}
context = None
try:
context = browser.new_context(geolocation=dict(declared)) if declared else browser.new_context()
context.grant_permissions(["geolocation"], origin=origin)
page = context.new_page()
page.goto(origin + GEO_PATH)
step["geo"] = page.evaluate(GEO_READ)
step["ok"] = True
except Exception as exc: # noqa: BLE001 - recorded, and a step that failed is a problem of the run
step.update(ok=False, error=str(exc).strip().splitlines()[0][:300] if str(exc).strip() else repr(exc))
finally:
if context is not None:
try:
context.close()
except Exception: # noqa: BLE001
pass
return step
def run_seed(binary: str, seed: int, work: Path, name: str | None = None) -> dict:
"""One browser, one journey, one close, one trace. Returns the seed's footprint; `name` is the folder of its work (the warm-up has one of its own)."""
from playwright.sync_api import sync_playwright
@@ -1179,6 +1313,7 @@ def run_seed(binary: str, seed: int, work: Path, name: str | None = None) -> dic
exe = _stand_in(binary, seed_work / "engine", log, pidfile, syscalls=STRACE_CALLS, fd_paths=True, network=True)
origin_a, origin_o, stop_site = journey_site.start((0, 0))
geo_origin, stop_geo = start_geo_lab()
server, steps, root_pid = None, [], None
try:
server = launch_server(headless=True, binary_path=exe, seed=seed, locale="en-US", timezone="Europe/Rome",
@@ -1205,6 +1340,7 @@ def run_seed(binary: str, seed: int, work: Path, name: str | None = None) -> dic
steps.append({"n": n, "name": name, "ok": True})
except Exception as exc: # noqa: BLE001 - recorded, and a step that failed is a problem of the run
steps.append({"n": n, "name": name, "ok": False, "error": str(exc).strip().splitlines()[0][:300] if str(exc).strip() else repr(exc)})
steps.append(geolocation_step(browser, geo_origin, GEO_DECLARED, len(JOURNEY) + 1))
context.close()
browser.close()
# The close: the run ends with the exit of the browser, and the trace is read after it.
@@ -1219,6 +1355,7 @@ def run_seed(binary: str, seed: int, work: Path, name: str | None = None) -> dic
"problems": ["the trace of the run could not be taken: %s" % e]}
finally:
stop_site()
stop_geo()
if server is not None:
try:
server.close()
@@ -1315,6 +1452,46 @@ def run_engine(binary: str, leg: str, seeds: list, label: str) -> dict:
"binary": os.path.basename(os.path.dirname(os.path.realpath(binary)))}
def run_stock(leg: str) -> dict:
"""The negative control of the geolocation step, Windows only: Playwright's own Firefox (`p.firefox.launch()`, as the fake web's stock control), with
no declared position, the same step, the same trace. It must start the Windows location utility process (`stock_control_problems`). The tree is the
one of this process (the driver and the browser it launches): only the kinds and the modules of the run are read."""
from playwright.sync_api import sync_playwright
work = Path(tempfile.mkdtemp(prefix="ab-host-stock-"))
trace = WindowsTrace(work)
stop_geo = None
steps = []
def document(result):
return {"format": FORMAT, "engine": "stock", "leg": leg, "platform": sys.platform, "stock": result}
try:
try:
trace.start()
except TraceError as e:
return document({"seed": 0, "steps": [], "kinds": {}, "files": [], "ours": {}, "trace": {"source": "etw+audit", "exited": False},
"problems": ["the trace of a Windows run could not be started (it needs an administrator): %s" % e]})
geo_origin, stop_geo = start_geo_lab()
try:
with sync_playwright() as p:
exe = p.firefox.executable_path
browser = p.firefox.launch(headless=True)
try:
steps.append(geolocation_step(browser, geo_origin, None, len(JOURNEY) + 1))
finally:
browser.close()
return document(_seed_result(exe, 0, work, steps, work / "none.log", trace, os.getpid(), True))
except TraceError as e:
return document({"seed": 0, "steps": steps, "kinds": {}, "files": [], "ours": {}, "trace": {"exited": False},
"problems": ["the trace of the run could not be taken: %s" % e]})
finally:
if stop_geo is not None:
stop_geo()
trace.cleanup()
shutil.rmtree(work, ignore_errors=True)
def write_json(path: Path, document) -> None:
Path(path).write_text(json.dumps(document, sort_keys=True, indent=1), encoding="utf-8")
@@ -1348,6 +1525,39 @@ def run_subprocess(binary: str, seal: Path | None, leg: str, out: Path, label: s
return 124
def run_stock_subprocess(leg: str, out: Path) -> int:
"""`stock` in a process of its own (its tree is the one of that process), with a temp root of its own."""
env = dict(os.environ)
for name in ("MOZ_LOG", "MOZ_LOG_FILE"):
env.pop(name, None)
cmd = [sys.executable, "-u", str(Path(__file__).resolve()), "stock", "--leg", leg, "--out", str(out)]
try:
with tempfile.TemporaryDirectory(prefix="ab-host-tmp-") as temp:
env.update(TMPDIR=temp, TEMP=temp, TMP=temp)
return subprocess.run(cmd, env=env, timeout=RUN_TIMEOUT_S).returncode
except subprocess.TimeoutExpired:
return 124
def run_stock_control(leg: str, out: Path) -> dict | None:
"""The stock run of the geolocation step and its verdict: the footprint, or None when the control is broken (said on the output)."""
print("=== negative control of the geolocation step: Playwright's own Firefox")
sys.stdout.flush()
rc = run_stock_subprocess(leg, out / "footprint-stock.json")
broken = ["the run of the stock control ended with %s" % rc] if rc != 0 or not (out / "footprint-stock.json").exists() else []
stock = None
if not broken:
stock = read_json(out / "footprint-stock.json")
broken = stock_control_problems(stock)
if broken:
print("\n".join("FAIL " + b for b in broken))
print("BROKEN CONTROL: the geolocation step does not reach the platform provider on stock Firefox; the A/B says nothing")
return None
print("GEOLOCATION CONTROL: stock Firefox started %s; modules: %s" % (
", ".join(location_utility_kinds(stock["stock"]["kinds"])), ", ".join(location_modules(stock["stock"]["files"])) or "-"))
return stock
def ab(candidate: str | None, candidate_seal: Path | None, leg: str, out: Path, engine_tag: str, seeds: list,
published: tuple | None = None, expected_file: Path = EXPECTED_FILE) -> int:
"""A/A first (the published engine twice), then, with a candidate, the published engine against it. 0 green, 1 the A/B is red,
@@ -1382,6 +1592,10 @@ def ab(candidate: str | None, candidate_seal: Path | None, leg: str, out: Path,
return 2
if not candidate:
return 0
if _windows():
stock = run_stock_control(leg, out)
if stock is None:
return 2
expected = load_expected(expected_file)
report = judge(first, read_json(out / runs[2][1]), expected=expected, a_a=False)
write_json(out / "diff.json", report)
@@ -1401,6 +1615,9 @@ def main(argv=None) -> int:
r.add_argument("--out", type=Path, required=True)
r.add_argument("--label", default="engine")
r.add_argument("--seeds", default=None, help="comma separated (default: ci/persona-seeds.json 'candidate', else the host gate's)")
k = sub.add_parser("stock", help="the negative control of the geolocation step (Windows): Playwright's own Firefox")
k.add_argument("--leg", required=True)
k.add_argument("--out", type=Path, required=True)
c = sub.add_parser("compare", help="two footprints: the diff and the verdict")
c.add_argument("a", type=Path)
c.add_argument("b", type=Path)
@@ -1426,6 +1643,12 @@ def main(argv=None) -> int:
if report["verdict"] == "green":
return 0
return 2 if args.a_a else 1
if args.cmd == "stock":
if not _windows():
print("the stock control is for Windows: Linux has no Windows location provider to reach")
return 3
write_json(args.out, run_stock(args.leg))
return 0
seeds = [int(s) for s in args.seeds.split(",")] if args.seeds else default_seeds()
if args.cmd == "run":
if sys.platform.startswith("linux") and not shutil.which("strace"):
+231
View File
@@ -1027,3 +1027,234 @@ def test_the_stand_in_traces_the_network_only_when_the_host_oracle_asks(tmp_path
assert "-e trace=%s " % stand_in._SYSCALLS in font and "socket" not in font, "the font tests' default is unchanged"
assert "-e trace=%s,%s " % (host.STRACE_CALLS, stand_in._NET_SYSCALLS) in oracle and " -y" in oracle
assert set(stand_in._NET_SYSCALLS.split(",")) == set(host.NET_CALLS)
# -
# The geolocation step: the candidate starts no Windows location utility process and loads no location module; stock Firefox, the control, must start it.
LOCATION_CMD = '"%s" -contentproc --channel="100.7.8" -sandboxingKind 3 -notForBrowser utility' % FIREFOX
WIN_FILES = ["c:\\windows\\system32\\kernel32.dll", "c:\\windows\\system32\\ntdll.dll", "c:\\windows\\system32\\user32.dll"]
WIN_KINDS = {"parent": 1, "tab/isForBrowser": 2, "utility/sandboxingKind=1/notForBrowser": 1}
LOCATION_KIND = "utility/sandboxingKind=3/notForBrowser"
def windows_footprints(candidate_kinds=None, candidate_files=None, engine="candidate"):
published = footprint(platform="win32", kinds=WIN_KINDS, files=WIN_FILES)
return published, footprint(engine, platform="win32", kinds=candidate_kinds or WIN_KINDS, files=candidate_files or WIN_FILES)
def test_the_location_utility_process_is_the_kind_3_of_a_real_windows_command_line():
events = host.parse_audit_events(audit(18, 108, 100, FIREFOX, LOCATION_CMD))
label = host.windows_processes(events, FIREFOX)[108]["label"]
assert label == LOCATION_KIND
assert host.location_utility_kinds({"parent": 1, label: 1, "utility/sandboxingKind=1/notForBrowser": 1, "utility/sandboxingKind=30/notForBrowser": 1}) == [LOCATION_KIND]
assert host.LOCATION_UTILITY_KIND == "3"
def test_a_candidate_that_starts_the_location_utility_process_is_red_on_windows_and_says_so():
published, candidate = windows_footprints(candidate_kinds=dict(WIN_KINDS, **{LOCATION_KIND: 1}))
report = judge(published, candidate, expected=[])
assert report["verdict"] == "red"
messages = [p for p in report["problems"] if "location utility process (-sandboxingKind 3)" in p]
assert len(messages) == len(SEEDS) and all("side B" in m and LOCATION_KIND in m for m in messages)
def test_the_rule_is_absolute_the_published_engine_having_the_process_too_does_not_excuse_the_candidate():
both = dict(WIN_KINDS, **{LOCATION_KIND: 1})
report = judge(footprint(platform="win32", kinds=both, files=WIN_FILES), footprint("candidate", platform="win32", kinds=both, files=WIN_FILES), expected=[])
assert report["differences"] == [] and report["verdict"] == "red"
def test_a_candidate_that_loads_a_location_module_is_red_whatever_the_spelling_of_the_path():
for path in ("C:\\Windows\\System32\\GeoLocation.dll", "c:\\windows\\system32\\locationframeworkps.dll", "C:\\Windows\\System32\\wlanapi.dll",
"c:\\windows\\system32\\capabilityaccessmanagerclient.dll", "c:\\windows\\system32\\locationapi.dll",
"c:\\windows\\system32\\windows.devices.geolocation.dll", "c:\\windows\\system32\\Windows.Devices.GeolocationExt.dll"):
published, candidate = windows_footprints(candidate_files=WIN_FILES + [path])
published["seeds"] = [dict(s, files=WIN_FILES + [path]) for s in published["seeds"]]
report = judge(published, candidate, expected=[])
assert report["verdict"] == "red" and any("loaded location modules" in p and path.rsplit("\\", 1)[-1] in p for p in report["problems"]), path
def test_a_neighbour_of_the_location_modules_is_not_one():
assert host.location_modules(["c:\\windows\\system32\\geolocation.dll.mui", "c:\\windows\\system32\\location.dll", "c:\\lab\\geolocation.json"]) == []
def test_the_utility_process_of_the_media_decoder_is_not_the_location_one():
published, candidate = windows_footprints()
assert judge(published, candidate, expected=[])["verdict"] == "green"
def test_the_warm_up_launch_of_the_candidate_is_checked_too():
published, candidate = windows_footprints()
candidate["warmup"] = dict(seed_footprint(11, kinds=dict(WIN_KINDS, **{LOCATION_KIND: 1}), files=WIN_FILES), seed=11)
report = judge(published, candidate, expected=[])
assert report["verdict"] == "red" and any(p.startswith("warm-up side B: the engine started the Windows location utility process") for p in report["problems"])
def test_the_rule_is_for_the_candidate_on_windows_not_for_the_a_a_nor_for_linux():
published, candidate = windows_footprints(candidate_kinds=dict(WIN_KINDS, **{LOCATION_KIND: 1}))
a_a = judge(candidate, copy.deepcopy(candidate), a_a=True)
assert not any("location" in p for p in a_a["problems"])
side_a = judge(candidate, published, expected=[])
assert not any("side B: the engine started" in p for p in side_a["problems"])
on_linux = judge(footprint(), footprint("candidate", kinds=dict(KINDS, **{LOCATION_KIND: 1})), expected=[])
assert not any("location" in p for p in on_linux["problems"])
def a_stock(kinds, files=(), steps=None, problems=()):
return {"format": host.FORMAT, "engine": "stock", "platform": "win32",
"stock": {"seed": 0, "steps": steps if steps is not None else [{"n": 5, "name": host.GEO_STEP, "ok": True}], "kinds": kinds, "files": list(files),
"problems": list(problems)}}
def test_stock_that_starts_the_location_utility_process_is_a_working_control():
stock = a_stock(dict(WIN_KINDS, **{LOCATION_KIND: 1}), files=["c:\\windows\\system32\\geolocation.dll"])
assert host.stock_control_problems(stock) == []
def test_stock_that_does_not_start_it_is_a_broken_control_and_says_the_step_does_not_reach_the_provider():
problems = host.stock_control_problems(a_stock(WIN_KINDS))
assert len(problems) == 1 and "did not start the Windows location utility process (-sandboxingKind 3)" in problems[0]
assert "does not reach the platform provider" in problems[0]
def test_a_stock_run_that_failed_or_was_not_traced_is_a_broken_control_too():
kinds = dict(WIN_KINDS, **{LOCATION_KIND: 1})
assert host.stock_control_problems(a_stock(kinds, steps=[{"n": 5, "name": host.GEO_STEP, "ok": False, "error": "timeout"}])) == [
"stock control: step 5 (%s) failed: timeout" % host.GEO_STEP]
assert host.stock_control_problems(a_stock(kinds, problems=["the file trace lost 3 events"])) == ["stock control: the file trace lost 3 events"]
assert host.stock_control_problems({"stock": {}}) == ["stock control: the run made no footprint"]
class FakePage:
def __init__(self, calls):
self.calls = calls
def goto(self, url):
self.calls.append(("goto", url))
def evaluate(self, script):
self.calls.append(("evaluate", script))
return {"answer": "error", "code": 2}
class FakeContext:
def __init__(self, calls, options):
self.calls = calls
calls.append(("new_context", options))
def grant_permissions(self, permissions, origin=None):
self.calls.append(("grant_permissions", list(permissions), origin))
def new_page(self):
self.calls.append(("new_page",))
return FakePage(self.calls)
def close(self):
self.calls.append(("close",))
class FakeBrowser:
def __init__(self):
self.calls = []
def new_context(self, **options):
return FakeContext(self.calls, options)
def test_the_step_grants_the_permission_to_the_lab_page_then_opens_it_then_asks_and_awaits_the_answer():
browser = FakeBrowser()
step = host.geolocation_step(browser, "http://127.0.0.1:9", host.GEO_DECLARED, 5)
assert [c[0] for c in browser.calls] == ["new_context", "grant_permissions", "new_page", "goto", "evaluate", "close"]
assert browser.calls[0][1] == {"geolocation": host.GEO_DECLARED}
assert browser.calls[1][1:] == (["geolocation"], "http://127.0.0.1:9")
assert browser.calls[3][1] == "http://127.0.0.1:9/geo"
assert step == {"n": 5, "name": host.GEO_STEP, "ok": True, "geo": {"answer": "error", "code": 2}}
def test_the_stock_control_declares_no_position_so_that_it_has_to_ask_the_platform():
browser = FakeBrowser()
host.geolocation_step(browser, "http://127.0.0.1:9", None, 5)
assert browser.calls[0] == ("new_context", {})
def test_the_request_ends_with_the_callback_or_the_error_and_no_timer_of_the_page_decides():
assert "getCurrentPosition" in host.GEO_READ and "position =>" in host.GEO_READ and "error =>" in host.GEO_READ
assert "setTimeout" not in host.GEO_READ and "sleep" not in host.GEO_READ.lower()
def test_a_step_that_threw_is_recorded_as_failed_and_closes_its_context():
class Throws(FakePage):
def evaluate(self, script):
raise RuntimeError("page crashed\nmore")
class B(FakeBrowser):
def new_context(self, **options):
context = FakeContext(self.calls, options)
context.new_page = lambda: Throws(self.calls)
return context
browser = B()
step = host.geolocation_step(browser, "http://127.0.0.1:9", None, 5)
assert step["ok"] is False and step["error"] == "page crashed" and browser.calls[-1] == ("close",)
def test_the_journey_has_the_geolocation_step_after_its_own_steps_in_a_run():
source = Path(host.__file__).read_text(encoding="utf-8")
run_seed = source[source.index("def run_seed("):source.index("def _wait_for_tree_exit(")]
assert run_seed.index("for n, (name, method) in enumerate(JOURNEY, 1)") < run_seed.index("geolocation_step(browser, geo_origin, GEO_DECLARED, len(JOURNEY) + 1)")
assert run_seed.index("geolocation_step(") < run_seed.index("browser.close()")
def test_the_lab_page_of_the_step_is_served_by_the_oracle_itself():
import urllib.request
origin, stop = host.start_geo_lab()
try:
assert origin.startswith("http://127.0.0.1:")
with urllib.request.urlopen(origin + host.GEO_PATH, timeout=5) as response:
assert response.status == 200 and b"<title>geo</title>" in response.read()
finally:
stop()
def test_a_windows_ab_runs_the_stock_control_after_the_candidate_and_before_the_verdict_and_a_broken_one_is_exit_2(tmp_path, monkeypatch):
order = []
monkeypatch.setattr(host, "_windows", lambda: True)
monkeypatch.setattr(host.oracle, "fetch_published", lambda tag, work: ("pub-binary", "pub-seal"))
monkeypatch.setattr(host, "print_report", lambda *a, **k: None)
monkeypatch.setattr(host, "load_expected", lambda path: [])
stock = {"doc": a_stock(WIN_KINDS)}
def fake_run(exe, seal, leg, out, label, seeds):
order.append(("run", label))
host.write_json(out, footprint(label, platform="win32"))
return 0
def fake_stock(leg, out):
order.append(("stock", leg))
host.write_json(out, stock["doc"])
return 0
def fake_judge(a, b, **kw):
order.append(("judge", "a-a" if kw.get("a_a") else "a-b"))
return {"verdict": "green", "problems": []}
monkeypatch.setattr(host, "run_subprocess", fake_run)
monkeypatch.setattr(host, "run_stock_subprocess", fake_stock)
monkeypatch.setattr(host, "judge", fake_judge)
assert host.ab("cand", Path("seal"), "windows-x86_64", tmp_path / "o1", "firefox-68", [11]) == 2
assert order == [("run", "firefox-68"), ("run", "firefox-68 again"), ("run", "candidate"), ("judge", "a-a"), ("stock", "windows-x86_64")]
order.clear()
stock["doc"] = a_stock(dict(WIN_KINDS, **{LOCATION_KIND: 1}))
assert host.ab("cand", Path("seal"), "windows-x86_64", tmp_path / "o2", "firefox-68", [11]) == 0
assert order[-2:] == [("stock", "windows-x86_64"), ("judge", "a-b")]
order.clear()
assert host.ab(None, None, "windows-x86_64", tmp_path / "o3", "firefox-68", [11]) == 0
assert ("stock", "windows-x86_64") not in order, "the A/A has no candidate: no control of the geolocation step"
def test_the_stock_command_is_for_windows_only(tmp_path, monkeypatch, capsys):
monkeypatch.setattr(host, "_windows", lambda: False)
assert host.main(["stock", "--leg", "linux-x86_64", "--out", str(tmp_path / "s.json")]) == 3
assert "Linux has no Windows location provider" in capsys.readouterr().out