Engine candidate 5 (run 46208549074): ab_oracle, ab_network and ab_host were all red on a BROKEN
CONTROL. The published engine (labelled firefox-68, a tag written in the harness, when firefox-70
was the last published) was launched by the CANDIDATE's core, which no longer declares
zoom.stealth.screen.popup_position, and the engine refuses a launch whose declarations its core does
not emit (rule 7). A pairing no user has.
The control is now the set a user gets: the last invisible-playwright on the index, the
invisible-core it pins exactly, the engine that core's seal pins, in a venv of their own.
- ci/py-lock/published.txt: the set as a hash lock (every file of the two versions, the requirements
each declares); scripts/ci_published_set.py makes it (resolve), compares it with the index (check),
installs it offline over the gate's lock from the run's wheelhouse (install) and verifies what the
venv imports (verify); fetch/tag run BY the set's python: its own core fetches its own engine.
- .github/workflows/published-set.yml: daily and on a pull request that touches the lock, red when
the committed lock is not the index's last release.
- ci_ab_oracle, ci_ab_host, ci_ab_network: the published side runs in the set's python
(INVPW_PUBLISHED_PYTHON) with an environment that carries nothing of the candidate's job
(PYTHONPATH, VIRTUAL_ENV, its seal, its pin switches); the candidate side is unchanged. No tag is
written in a harness file: the label and the file names (dump-<N>.json, ...) come from the seal of
the set. A candidate without a set is refused, never run against its own core.
- ci_poison_host: the by-product files are named for the tag of the set as well.
- tests/test_ci_published_set.py (36, in wrapper.unit.all.linux and .windows): the defect on fake
cores and a fake engine that refuses as rule 7 (red on the old harness with that refusal), the
three seams, the lock on known-bad inputs, the resolver on synthetic index documents, the install
commands, no tag written in a harness file.
Red on the old harness: the defect test fails with "stealth: zoom.stealth.screen.popup_position is
not declared by the core candidate-core (rule 7)" on the scripts of df491f2e. The regression
registry of the wrapper is not on this base: its row follows with the registry.
595 lines
35 KiB
Python
595 lines
35 KiB
Python
"""The WebRTC walk of the network oracle keeps its evidence OUTSIDE the leaves that are compared, and the hunt repeats that walk alone (scripts/ci_ab_network.py), no browser.
|
|
|
|
The A/A of the oracle found the published engine sending 8 or 9 TURN Allocate datagrams to 198.51.100.7:3478 in the WebRTC walk of one seed. The two readings are told apart by
|
|
what each datagram carried and when: a STUN transaction id repeats on a retransmission, and the page's own ICE times and the moments of the stages say where the close fell. This file holds
|
|
that evidence to its rules: it is in ``transcript["evidence"]`` (which ``compare_transcripts`` never reads), what the page returns for the compared fields is what it returned before, the
|
|
transaction id comes from the header of the datagram, the gateway's summary is the one it was, and the hunt (``AB_NETWORK_HUNT=webrtc:<seed>:<n>``, wired into e2e-hunt.yml) writes one file
|
|
per repetition and a summary and decides nothing.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
import sys
|
|
import time
|
|
import types
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
|
|
import ci_ab_network as ab # noqa: E402
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
import _turn_chain # noqa: E402
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
HUNT_WORKFLOW = ROOT / ".github" / "workflows" / "e2e-hunt.yml"
|
|
|
|
COOKIE = b"\x21\x12\xa4\x42"
|
|
TID = bytes(range(1, 13))
|
|
|
|
|
|
def stun(message_type: int, tid: bytes = TID) -> bytes:
|
|
"""A STUN header (RFC 8489): type, length 0, the magic cookie and a 12-byte transaction id."""
|
|
return message_type.to_bytes(2, "big") + b"\x00\x00" + COOKIE + tid
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------------------- the transaction id
|
|
def test_the_transaction_id_is_bytes_8_to_20_of_a_stun_header():
|
|
allocate = stun(0x0003)
|
|
assert len(allocate) == 20
|
|
assert ab.stun_tid(allocate) == TID.hex() == "0102030405060708090a0b0c"
|
|
assert ab.stun_tid(allocate + b"\x00\x06\x00\x04lab\x00") == TID.hex(), "the attributes after the header change nothing"
|
|
assert ab.stun_tid(stun(0x0113, b"\xff" * 12)) == "ff" * 12
|
|
|
|
|
|
@pytest.mark.parametrize("payload", [b"", b"\xc3" + b"\x00" * 40, b"\x40" + b"\x01" * 30, b"\x00\x03\x00\x00" + b"\x00" * 16, stun(0x0003)[:19], b"hello world" * 3])
|
|
def test_a_datagram_that_is_not_stun_has_no_transaction_id(payload):
|
|
assert ab.stun_tid(payload) is None
|
|
|
|
|
|
def test_the_gateway_writes_the_transaction_id_of_each_datagram_and_the_summary_of_the_phase_does_not_change():
|
|
from _doh_servers import FakeDoh
|
|
from _turn_chain import TURN_IP, TURN_PORT
|
|
|
|
assert "%s:%d" % (TURN_IP, TURN_PORT) == ab.TURN_TARGET, "the target the hunt counts is the TURN server of the lab"
|
|
gateway_class, turn_class, _page = ab.make_lab_classes()
|
|
rec = ab.Recorder()
|
|
doh = FakeDoh({})
|
|
turn = turn_class("127.0.0.1")
|
|
gateway = gateway_class(rec, doh, turn, "127.0.0.1", "198.51.100.9")
|
|
try:
|
|
rec.set_phase("webrtc")
|
|
gateway._route(TURN_IP, TURN_PORT, 1, stun(0x0003), 1, None)
|
|
gateway._route(TURN_IP, TURN_PORT, 1, stun(0x0003), 1, None)
|
|
gateway._route(TURN_IP, TURN_PORT, 1, stun(0x0003, b"\x09" * 12), 1, None)
|
|
gateway._route("127.0.0.1", 5555, 1, b"\xc3" + b"\x00" * 20, 1, None)
|
|
finally:
|
|
gateway.close()
|
|
turn.close()
|
|
doh.close()
|
|
events = rec.of("webrtc", "udp")
|
|
assert [(e["target"], e["message"], e["tid"]) for e in events] == [
|
|
(ab.TURN_TARGET, "stun-0x0003", TID.hex()), (ab.TURN_TARGET, "stun-0x0003", TID.hex()), (ab.TURN_TARGET, "stun-0x0003", "09" * 12), ("127.0.0.1:5555", "quic-long", None)]
|
|
norm = ab.Normalizer({})
|
|
# the count is of TRANSACTIONS: the first Allocate was sent twice (one transaction), the second once, and the QUIC datagram has no transaction id (one)
|
|
assert ab.udp_summary(events, norm)["counts"] == {ab.TURN_TARGET: 2, "127.0.0.1:5555": 1}
|
|
without = [{k: v for k, v in e.items() if k != "tid"} for e in events]
|
|
assert ab.udp_summary(without, norm)["counts"] == {ab.TURN_TARGET: 3, "127.0.0.1:5555": 1}, "with no transaction id every datagram counts one"
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------------------- the evidence key
|
|
def lab_with(rec, **webrtc):
|
|
return types.SimpleNamespace(rec=rec, webrtc=webrtc)
|
|
|
|
|
|
def recorded_walk():
|
|
"""A recorder that went through the stages of the walk, with the datagrams a TURN server's client sends: the first Allocate three times, the authenticated one twice, one late."""
|
|
rec = ab.Recorder()
|
|
rec.set_phase("webrtc")
|
|
for target, message, tid in [(ab.TURN_TARGET, "stun-0x0003", "aa" * 12), (ab.TURN_TARGET, "stun-0x0003", "aa" * 12), ("198.51.100.5:19302", "stun-0x0001", "bb" * 12),
|
|
(ab.TURN_TARGET, "stun-0x0003", "cc" * 12), ("127.0.0.1:5555", "quic-long", None)]:
|
|
rec.event("udp", target=target, message=message, tid=tid, opener=None)
|
|
counted = len(rec.of("webrtc", "udp"))
|
|
rec.set_phase("webrtc.end")
|
|
rec.event("udp", target=ab.TURN_TARGET, message="stun-0x0003", tid="cc" * 12, opener=None)
|
|
return rec, counted
|
|
|
|
|
|
def test_the_evidence_of_the_walk_lists_every_datagram_with_its_time_target_message_and_transaction_and_the_moments_of_the_stages():
|
|
rec, counted = recorded_walk()
|
|
page = {"start": 1.0, "offer": 2.0, "candidates": [{"t": 3.0, "type": "host"}], "errors": [{"t": 4.0, "code": 701, "url": "turn:x"}], "gathered": 5.0, "closing": 6.0, "closed": 6.5}
|
|
evidence = ab.webrtc_evidence(lab_with(rec, counted=counted, evaluate={"called": 0.5, "returned": 7.0}, page=page))
|
|
json.dumps(evidence)
|
|
assert set(evidence) == {"phase_t", "evaluate", "page", "datagrams", "turn", "returned", "send_errors", "deallocation"}
|
|
assert evidence["page"] == page and evidence["evaluate"] == {"called": 0.5, "returned": 7.0}
|
|
assert set(evidence["phase_t"]) == {"webrtc", "webrtc.end"} and evidence["phase_t"]["webrtc"] <= evidence["phase_t"]["webrtc.end"]
|
|
datagrams = evidence["datagrams"]
|
|
assert [(d["target"], d["message"], d["tid"], d["phase"], d["counted"]) for d in datagrams] == [
|
|
(ab.TURN_TARGET, "stun-0x0003", "aa" * 12, "webrtc", True), (ab.TURN_TARGET, "stun-0x0003", "aa" * 12, "webrtc", True), ("198.51.100.5:19302", "stun-0x0001", "bb" * 12, "webrtc", True),
|
|
(ab.TURN_TARGET, "stun-0x0003", "cc" * 12, "webrtc", True), ("127.0.0.1:5555", "quic-long", None, "webrtc", True),
|
|
(ab.TURN_TARGET, "stun-0x0003", "cc" * 12, "webrtc.end", False)]
|
|
assert all(isinstance(d["t"], float) for d in datagrams) and [d["t"] for d in datagrams] == sorted(d["t"] for d in datagrams)
|
|
|
|
|
|
def test_the_evidence_of_the_walk_leaves_out_the_datagrams_of_other_stages_and_says_nothing_when_the_walk_never_ran():
|
|
rec = ab.Recorder()
|
|
rec.set_phase("h3")
|
|
rec.event("udp", target="127.0.0.1:1", message="quic-long", tid=None, opener=None)
|
|
empty = ab.webrtc_evidence(lab_with(rec))
|
|
assert empty == {"phase_t": {"webrtc": None, "webrtc.end": None}, "evaluate": None, "page": None, "datagrams": [], "turn": [], "returned": [], "send_errors": [], "deallocation": {"seen": False, "records": []}}
|
|
|
|
|
|
def evidence_transcript(label, datagrams):
|
|
"""A transcript as ``run`` writes it: ``seeds`` (compared) and ``evidence`` (not), with the webrtc evidence of a real recorder in it."""
|
|
rec = ab.Recorder()
|
|
rec.set_phase("webrtc")
|
|
for tid in datagrams:
|
|
rec.event("udp", target=ab.TURN_TARGET, message="stun-0x0003", tid=tid, opener=None)
|
|
rec.set_phase("webrtc.end")
|
|
evidence = ab.webrtc_evidence(lab_with(rec, counted=len(datagrams), page={"gathered": float(len(datagrams))}))
|
|
return {"format": ab.FORMAT, "label": label, "platform": "linux",
|
|
"seeds": {"4": {"webrtc": {"candidates": ["udp comp1 prio1 srflx <exit>"], "candidate_types": ["relay", "srflx"], "gathering": "complete", "udp": {"counts": {ab.TURN_TARGET: 8}}}}},
|
|
"evidence": {"format": ab.EVIDENCE_FORMAT, "seeds": {"4": {"journey": [], "https": {}, "h3": [], "webrtc": evidence}}}}
|
|
|
|
|
|
def test_two_transcripts_that_differ_only_in_the_evidence_of_the_walk_have_no_difference():
|
|
a = evidence_transcript("a", ["aa" * 12] * 8)
|
|
b = evidence_transcript("b", ["aa" * 12] * 7 + ["bb" * 12, "cc" * 12])
|
|
assert a["evidence"] != b["evidence"] and a["seeds"] == b["seeds"]
|
|
report = ab.compare_transcripts(a, b, [], 0, "a", "b")
|
|
assert report["differences"] == [] and report["problems"] == [] and report["verdict"] == "green"
|
|
assert report["leaves"] == ab.compare_transcripts(a, a, [], 0, "a", "a")["leaves"]
|
|
assert ab.flatten(a["seeds"]).keys().isdisjoint({p for p in ab.flatten(a["evidence"]["seeds"], "evidence")}), "no leaf of the evidence is a leaf of the seeds"
|
|
# and the same difference in the compared part IS one: the control of the test above
|
|
c = json.loads(json.dumps(a))
|
|
c["seeds"]["4"]["webrtc"]["udp"]["counts"][ab.TURN_TARGET] = 9
|
|
assert [d["path"] for d in ab.compare_transcripts(a, c, [], 0, "a", "c")["differences"]] == ["seeds/4/webrtc/udp/counts/198.51.100.7:3478"]
|
|
|
|
|
|
def test_a_run_writes_the_evidence_of_the_walk_under_the_evidence_of_its_seed(tmp_path, monkeypatch):
|
|
import _lab_egress
|
|
import ci_crash_evidence as crash
|
|
|
|
def walk_seed(binary, seed, with_idle, patch):
|
|
ab.EVIDENCE[str(seed)] = evidence_transcript("x", ["aa" * 12] * 2)["evidence"]["seeds"]["4"]
|
|
return {"webrtc": {}}
|
|
|
|
monkeypatch.setattr(ab, "walk_seed", walk_seed)
|
|
monkeypatch.setattr(_lab_egress, "install", lambda patch, work: None)
|
|
monkeypatch.setenv(crash.CRASH_DIR_ENV, str(tmp_path / "t.crash"))
|
|
try:
|
|
assert ab.run("/e/firefox", "firefox-68", (4,), tmp_path / "t.json") == 0
|
|
finally:
|
|
crash.stop_probe()
|
|
document = json.loads((tmp_path / "t.json").read_text(encoding="utf-8"))
|
|
assert [d["tid"] for d in document["evidence"]["seeds"]["4"]["webrtc"]["datagrams"]] == ["aa" * 12] * 2
|
|
assert set(document) >= {"seeds", "evidence", "exits"} and "webrtc" not in document["evidence"], "the evidence is under the seed, not beside the seeds"
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------------------- the page's probe
|
|
def test_the_probe_returns_what_it_returned_and_its_times_beside_it():
|
|
js = ab.RTC_JS
|
|
assert "return {candidates: cands, errors, sdp, gathering, times: T};" in js, "the four fields that are compared come first and unchanged; `times` is the only addition"
|
|
assert "cands.push(e.candidate.candidate)" in js and "errors.push({code: e.errorCode, url: e.url})" in js
|
|
for what in ("T.offer = performance.now()", "T.gathered = performance.now()", "T.closing = performance.now()", "T.closed = performance.now()"):
|
|
assert what in js, what
|
|
assert re.search(r"T\.candidates\.push\(\{t: performance\.now\(\), type: ", js), "each icecandidate, with its type"
|
|
assert re.search(r"T\.errors\.push\(\{t: performance\.now\(\), code: e\.errorCode, url: e\.url\}\)", js), "each icecandidateerror, with its code and url"
|
|
assert js.index("T.closing = performance.now()") < js.index("pc.close()") < js.index("T.closed = performance.now()"), "the close is bracketed by its two times"
|
|
|
|
|
|
def test_what_the_page_measures_of_itself_is_in_the_evidence_and_the_summary_of_the_walk_is_the_same_with_any_times():
|
|
class Page:
|
|
def __init__(self, result):
|
|
self.result = result
|
|
|
|
def goto(self, *args, **kwargs):
|
|
pass
|
|
|
|
def evaluate(self, js, arg):
|
|
assert js == ab.RTC_JS and arg == [ab.STUN_URL, ab.TURN_URL, ab.TURN_USERNAME, _turn_chain.PASSWORD]
|
|
return json.loads(json.dumps(self.result))
|
|
|
|
class Browser:
|
|
def __init__(self, result):
|
|
self.page = Page(result)
|
|
|
|
def new_context(self, **kwargs):
|
|
return types.SimpleNamespace(new_page=lambda: self.page, close=lambda: None)
|
|
|
|
base = {"candidates": ["candidate:1 1 udp 100 192.0.2.1 5000 typ host"], "errors": [{"code": 701, "url": "turn:198.51.100.7:3478"}], "sdp": "v=0\r\n", "gathering": "complete"}
|
|
outs, labs = [], []
|
|
for times in ({"start": 1.0, "gathered": 2.0}, {"start": 5.5, "gathered": 9.25, "closed": 11.0}, None):
|
|
result = dict(base) if times is None else dict(base, times=times)
|
|
with ab.Lab(host="127.0.0.1") as lab:
|
|
outs.append(json.dumps(ab.walk_webrtc(Browser(result), lab), sort_keys=True))
|
|
labs.append(lab.webrtc)
|
|
assert outs[0] == outs[1] == outs[2], "the walk's summary does not depend on the page's times"
|
|
assert '"times"' not in outs[0] and "gathered" not in outs[0]
|
|
assert [x["page"] for x in labs] == [{"start": 1.0, "gathered": 2.0}, {"start": 5.5, "gathered": 9.25, "closed": 11.0}, None]
|
|
assert all(x["evaluate"]["called"] <= x["evaluate"]["returned"] for x in labs) and all(isinstance(x["counted"], int) for x in labs)
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------------------- the TURN server's answers and the relay's return path
|
|
def signed_allocate(tid, user=b"lab"):
|
|
"""An Allocate with USERNAME and the MESSAGE-INTEGRITY of the long-term key (the server of the lab checks it)."""
|
|
from _turn_chain import _attr, _message, long_term_key
|
|
|
|
return _message(0x0003, tid, _attr(0x0006, user), long_term_key(user.decode()))
|
|
|
|
|
|
def test_the_turn_server_of_the_lab_writes_every_request_with_what_it_carried_and_every_answer_with_where_it_went_and_answers_as_it_always_did():
|
|
import socket
|
|
|
|
from _turn_chain import RELAYED, TurnServer
|
|
|
|
_gateway, turn_class, _page = ab.make_lab_classes()
|
|
turn = turn_class("127.0.0.1")
|
|
assert isinstance(turn, TurnServer) and isinstance(turn.sock, ab.ExchangeSocket)
|
|
ticks = iter(x / 10 for x in range(1, 100))
|
|
turn.clock = lambda: next(ticks)
|
|
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
client.settimeout(5)
|
|
client.bind(("127.0.0.1", 0))
|
|
first, second = b"\x01" * 12, b"\x02" * 12
|
|
try:
|
|
client.sendto(stun(0x0003, first), turn.address)
|
|
unauthorized, _ = client.recvfrom(2048)
|
|
client.sendto(signed_allocate(second), turn.address)
|
|
success, _ = client.recvfrom(2048)
|
|
client.sendto(b"not stun at all, twenty-odd bytes", turn.address)
|
|
client.sendto(stun(0x0001, b"\x03" * 12), turn.address) # a Binding request: the server of the lab does not answer it
|
|
port = client.getsockname()[1]
|
|
for _ in range(100):
|
|
if len(turn.exchanges) == 3:
|
|
break
|
|
time.sleep(0.05)
|
|
finally:
|
|
client.close()
|
|
turn.close()
|
|
assert int.from_bytes(unauthorized[:2], "big") == 0x0113 and int.from_bytes(success[:2], "big") == 0x0103, "what the server answers is what it answered"
|
|
assert len(turn.seen) == 3 and [a[2] for a in turn.allocations] == ["lab"] and turn.allocations[0][:2] == ("127.0.0.1", port)
|
|
one, two, three = turn.exchanges
|
|
json.dumps(turn.exchanges)
|
|
assert (one["t"], one["src"], one["tid"], one["type"], one["username"], one["integrity"]) == (0.1, "127.0.0.1:%d" % port, first.hex(), "0x0003", False, False)
|
|
assert one["responses"] == [{"t": 0.2, "tid": first.hex(), "type": "0x0113", "to": "127.0.0.1:%d" % port, "relayed": None, "mapped": None, "error": 401}]
|
|
assert (two["tid"], two["type"], two["username"], two["integrity"]) == (second.hex(), "0x0003", True, True)
|
|
(answer,) = two["responses"]
|
|
assert answer["type"] == "0x0103" and answer["to"] == "127.0.0.1:%d" % port and answer["error"] is None
|
|
assert answer["relayed"] == "%s:%d" % RELAYED and answer["mapped"] == "127.0.0.1:%d" % port, "the XOR-RELAYED-ADDRESS and XOR-MAPPED-ADDRESS the server put in, decoded"
|
|
assert (three["type"], three["responses"]) == ("0x0001", []), "a request the server does not answer has no answer in its record"
|
|
|
|
|
|
def test_a_failure_of_the_record_never_reaches_the_server():
|
|
class Owner:
|
|
exchanges = None # append on None: the record fails
|
|
|
|
def clock(self):
|
|
raise RuntimeError("no clock")
|
|
|
|
class Raw:
|
|
def __init__(self):
|
|
self.sent = []
|
|
|
|
def recvfrom(self, size):
|
|
return stun(0x0003), ("127.0.0.1", 9)
|
|
|
|
def sendto(self, data, address):
|
|
self.sent.append((data, address))
|
|
return len(data)
|
|
|
|
def close(self):
|
|
self.closed = True
|
|
|
|
raw = Raw()
|
|
sock = ab.ExchangeSocket(raw, Owner())
|
|
assert sock.recvfrom(10) == (stun(0x0003), ("127.0.0.1", 9))
|
|
assert sock.sendto(stun(0x0113), ("127.0.0.1", 9)) == 20 and raw.sent == [(stun(0x0113), ("127.0.0.1", 9))]
|
|
sock.close()
|
|
assert raw.closed is True
|
|
|
|
|
|
def test_an_xor_address_is_decoded_and_an_answer_with_no_request_has_a_record_of_its_own():
|
|
from _turn_chain import _attr, _xor_addr
|
|
|
|
assert ab.xor_address(_xor_addr("198.51.100.7", 50000)) == "198.51.100.7:50000"
|
|
assert ab.xor_address(None) is None and ab.xor_address(b"\x00\x02" + b"\x00" * 18) is None
|
|
assert ab.stun_attributes(stun(0x0003) + _attr(0x0006, b"lab") + _attr(0x0008, b"\x00" * 20)) == {0x0006: b"lab", 0x0008: b"\x00" * 20}
|
|
assert ab.stun_attributes(stun(0x0003) + b"\x00\x06\x00\x40ab") == {}, "a truncated attribute says nothing"
|
|
owner = types.SimpleNamespace(exchanges=[], clock=lambda: 1.5)
|
|
ab.ExchangeSocket(types.SimpleNamespace(sendto=lambda data, address: 0), owner).sendto(stun(0x0113, b"\x07" * 12), ("127.0.0.1", 5))
|
|
assert owner.exchanges == [{"t": 1.5, "src": None, "tid": "07" * 12, "type": None, "username": None, "integrity": None,
|
|
"responses": [{"t": 1.5, "tid": "07" * 12, "type": "0x0113", "to": "127.0.0.1:5", "relayed": None, "mapped": None, "error": None}]}]
|
|
|
|
|
|
def test_the_relay_writes_each_datagram_it_sends_back_with_the_address_it_claims_and_the_five_tuple_it_goes_to_and_still_sends_it():
|
|
import socket
|
|
import struct
|
|
|
|
from _doh_servers import FakeDoh
|
|
|
|
gateway_class, turn_class, _page = ab.make_lab_classes()
|
|
rec = ab.Recorder()
|
|
doh = FakeDoh({})
|
|
turn = turn_class("127.0.0.1")
|
|
gateway = gateway_class(rec, doh, turn, "127.0.0.1", "198.51.100.9")
|
|
receiver = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
receiver.settimeout(5)
|
|
receiver.bind(("127.0.0.1", 0))
|
|
relay = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
relay.bind(("127.0.0.1", 0))
|
|
try:
|
|
rec.set_phase("webrtc")
|
|
gateway._deliver(relay, receiver.getsockname(), "198.51.100.7", 3478, stun(0x0103, b"\x05" * 12))
|
|
gateway._deliver(relay, receiver.getsockname(), "198.51.100.5", 19302, b"\xc3" + b"\x00" * 5)
|
|
first, _ = receiver.recvfrom(2048)
|
|
second, _ = receiver.recvfrom(2048)
|
|
relay_address = relay.getsockname()
|
|
client_address = receiver.getsockname()
|
|
finally:
|
|
receiver.close()
|
|
relay.close()
|
|
gateway.close()
|
|
turn.close()
|
|
doh.close()
|
|
assert first == b"\x00\x00\x00\x01" + socket.inet_aton("198.51.100.7") + struct.pack("!H", 3478) + stun(0x0103, b"\x05" * 12), "the datagram is sent as it always was"
|
|
assert second.endswith(b"\xc3" + b"\x00" * 5)
|
|
back = rec.of("webrtc", "udp_back")
|
|
assert [(e["claimed"], e["to"], e["relay"], e["message"], e["tid"]) for e in back] == [
|
|
("198.51.100.7:3478", "%s:%d" % client_address, "%s:%d" % relay_address, "stun-0x0103", "05" * 12),
|
|
("198.51.100.5:19302", "%s:%d" % client_address, "%s:%d" % relay_address, "quic-long", None)]
|
|
assert rec.of("webrtc", "udp") == [], "a datagram back is not a datagram of the browser: the compared UDP summary does not see it"
|
|
|
|
|
|
def test_the_evidence_keeps_the_turn_exchanges_of_the_walk_and_the_return_datagrams_and_the_gateways_send_errors():
|
|
rec = ab.Recorder()
|
|
rec.set_phase("webrtc")
|
|
rec.event("udp_back", claimed="198.51.100.7:3478", to="127.0.0.1:40000", relay="127.0.0.1:41000", message="stun-0x0103", tid="aa" * 12)
|
|
rec.set_phase("webrtc.end")
|
|
rec.phase_t["webrtc"] = 5.0 # the stage began at 5 s on the recorder's clock
|
|
before = {"t": 1.0, "src": "x:1", "tid": "00" * 12, "type": "0x0003", "username": False, "integrity": False, "responses": []}
|
|
inside = {"t": 5.5, "src": "127.0.0.1:40000", "tid": "aa" * 12, "type": "0x0003", "username": True, "integrity": True, "responses": []}
|
|
lab = types.SimpleNamespace(rec=rec, webrtc={}, turn=types.SimpleNamespace(exchanges=[before, inside]), gateway=types.SimpleNamespace(send_errors=["the answer to the client: OSError x"]))
|
|
evidence = ab.webrtc_evidence(lab)
|
|
json.dumps(evidence)
|
|
assert evidence["turn"] == [inside], "the exchanges of the walk, not what the server heard before its stage"
|
|
assert [(e["claimed"], e["to"], e["relay"], e["tid"], e["phase"]) for e in evidence["returned"]] == [("198.51.100.7:3478", "127.0.0.1:40000", "127.0.0.1:41000", "aa" * 12, "webrtc")]
|
|
assert evidence["send_errors"] == ["the answer to the client: OSError x"]
|
|
|
|
|
|
def test_a_lab_gives_its_turn_server_the_clock_of_its_recorder():
|
|
with ab.Lab(host="127.0.0.1") as lab:
|
|
assert lab.turn.clock == lab.rec.now and lab.turn.exchanges == []
|
|
|
|
|
|
def test_a_hunt_repetition_launches_the_engine_with_its_own_log_and_the_variables_are_gone_after(tmp_path, monkeypatch):
|
|
import os
|
|
|
|
seen = []
|
|
|
|
def launch(binary, seed, lab, patch):
|
|
seen.append((os.environ.get("MOZ_LOG"), os.environ.get("MOZ_LOG_FILE")))
|
|
raise RuntimeError("stop here")
|
|
|
|
monkeypatch.setattr(ab, "launch", launch)
|
|
monkeypatch.setattr(ab, "begin_launch", lambda seed, name: None)
|
|
monkeypatch.delenv("MOZ_LOG", raising=False)
|
|
monkeypatch.delenv("MOZ_LOG_FILE", raising=False)
|
|
patch = pytest.MonkeyPatch()
|
|
try:
|
|
record = ab.hunt_repetition("/e/firefox", "firefox-68-again", 7, 4, patch, tmp_path)
|
|
assert "MOZ_LOG" not in os.environ and "MOZ_LOG_FILE" not in os.environ, "the variables are the launch's: a repetition leaves none"
|
|
without = ab.hunt_repetition("/e/firefox", "firefox-68", 1, 4, patch)
|
|
finally:
|
|
patch.undo()
|
|
assert seen == [("sync,timestamp,nicer:5,signaling:5", str(tmp_path / "firefox-68-again-7-mozlog")), (None, None)] and ab.HUNT_MOZ_LOG == "sync,timestamp,nicer:5,signaling:5"
|
|
assert record["mozlog"] == "firefox-68-again-7-mozlog" and "mozlog" not in without
|
|
|
|
|
|
def test_the_run_of_a_normal_a_a_does_not_log_the_engine():
|
|
import inspect
|
|
|
|
for function in (ab.run_main, ab.run, ab.run_engine, ab.launch, ab.engine_env):
|
|
assert "HUNT_MOZ_LOG" not in inspect.getsource(function) and "setenv" not in inspect.getsource(function), function.__name__
|
|
assert "MOZ_LOG" in inspect.getsource(ab.engine_env), "an engine's own environment still has the log variables taken out: only the hunt's repetition sets them"
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------------------- the hunt
|
|
def repetition(engine, i, tids, late=0, error=None):
|
|
"""A repetition file as ``hunt_repetition`` writes it: ``tids`` are the transaction ids of the datagrams to the TURN server in order, the first ``late`` of the last ones after the close."""
|
|
datagrams = [{"t": 0.1 * n, "phase": "webrtc.end" if n >= len(tids) - late else "webrtc", "target": ab.TURN_TARGET, "message": "stun-0x0003", "tid": tid, "counted": n < len(tids) - late}
|
|
for n, tid in enumerate(tids)]
|
|
datagrams.append({"t": 0.05, "phase": "webrtc", "target": "198.51.100.5:19302", "message": "stun-0x0001", "tid": "dd" * 12, "counted": True})
|
|
record = {"format": ab.HUNT_FORMAT, "engine": engine, "i": i, "seed": 4, "error": error, "evidence": {"phase_t": {}, "evaluate": None, "page": None, "datagrams": datagrams}}
|
|
record["counts"] = ab.hunt_counts(record["evidence"])
|
|
return record
|
|
|
|
|
|
def test_the_hunt_summary_counts_the_datagrams_to_the_turn_server_and_lists_the_transactions_of_each_target():
|
|
a, b = "aa" * 12, "bb" * 12
|
|
records = [repetition("firefox-68", 1, [a, a, a, b, b]), repetition("firefox-68", 2, [a, a, b, b, b, b], late=1), repetition("firefox-68-again", 1, [a] * 4 + [b] * 4),
|
|
repetition("firefox-68-again", 2, [], error="Error: launch")]
|
|
summary = ab.hunt_summary(records)
|
|
json.dumps(summary)
|
|
assert summary["format"] == ab.HUNT_SUMMARY_FORMAT and summary["target"] == "198.51.100.7:3478"
|
|
first, second, third, failed = summary["repetitions"]
|
|
assert (first["engine"], first["i"], first["count_3478"], first["count_3478_all"]) == ("firefox-68", 1, 5, 5)
|
|
assert first["targets"] == {"198.51.100.5:19302": [["dd" * 12, 1]], ab.TURN_TARGET: [[a, 3], [b, 2]]}
|
|
assert (second["count_3478"], second["count_3478_all"]) == (5, 6), "a datagram after the close is in `all` and not in the count the A/A makes"
|
|
assert second["targets"][ab.TURN_TARGET] == [[a, 2], [b, 4]]
|
|
assert (third["count_3478"], third["targets"][ab.TURN_TARGET]) == (8, [[a, 4], [b, 4]])
|
|
assert failed["error"] == "Error: launch" and failed["count_3478"] == 0
|
|
assert summary["count_3478_by_engine"] == {"firefox-68": {"5": 2}, "firefox-68-again": {"0": 1, "8": 1}}
|
|
|
|
|
|
def test_the_hunt_summary_is_read_from_the_repetition_files_and_does_not_read_itself(tmp_path):
|
|
for record in (repetition("firefox-68-again", 2, ["aa" * 12]), repetition("firefox-68", 10, ["aa" * 12]), repetition("firefox-68", 2, ["aa" * 12] * 2)):
|
|
ab.write_json(tmp_path / ("%s-%d.json" % (record["engine"], record["i"])), record)
|
|
ab.write_json(tmp_path / "summary.json", ab.hunt_summary(ab.read_hunt_records(tmp_path)))
|
|
(tmp_path / "notes.txt").write_text("not json", encoding="utf-8")
|
|
records = ab.read_hunt_records(tmp_path)
|
|
assert [(r["engine"], r["i"]) for r in records] == [("firefox-68", 2), ("firefox-68", 10), ("firefox-68-again", 2)], "by engine, then by number (10 after 2)"
|
|
assert len(ab.hunt_summary(records)["repetitions"]) == 3
|
|
|
|
|
|
@pytest.mark.parametrize("text, expected", [("webrtc:4:20", ("webrtc", 4, 20)), (" webrtc:11:1 ", ("webrtc", 11, 1))])
|
|
def test_the_hunt_spec_names_the_walk_the_seed_and_the_count(text, expected):
|
|
assert ab.parse_hunt(text) == expected
|
|
|
|
|
|
@pytest.mark.parametrize("text", ["", "webrtc", "webrtc:4", "webrtc:four:3", "webrtc:4:0", "webrtc:4:-1", "h3:4:3", "webrtc:4:3:1", "4:3"])
|
|
def test_a_hunt_spec_that_does_not_read_is_refused(text):
|
|
with pytest.raises(ValueError, match=ab.HUNT_ENV):
|
|
ab.parse_hunt(text)
|
|
|
|
|
|
|
|
def test_ab_with_the_hunt_variable_runs_only_the_walk_n_times_for_each_engine_of_the_a_a_and_exits_0(tmp_path, monkeypatch, capsys):
|
|
calls = []
|
|
|
|
def hunt_engine(binary, seal, label, seed, n, out_dir):
|
|
calls.append((binary, seal, label, seed, n))
|
|
for i in range(1, n + 1):
|
|
ab.write_json(out_dir / ("%s-%d.json" % (label, i)), repetition(label, i, ["aa" * 12] * (8 + i % 2)))
|
|
return 0
|
|
|
|
def never(*a, **k):
|
|
raise AssertionError("the A/A, the A/B and the volatile list are not part of a hunt")
|
|
|
|
monkeypatch.setattr(ab, "hunt_engine", hunt_engine)
|
|
monkeypatch.setattr(ab, "run_engine", never)
|
|
monkeypatch.setattr(ab, "compare_files", never)
|
|
monkeypatch.setenv(ab.HUNT_ENV, "webrtc:4:3")
|
|
assert ab.main(["ab", "--a-a", "--engine-tag", "firefox-68", "--out", str(tmp_path / "network"), "--published-binary", "/e/firefox"]) == 0
|
|
binary = str(Path("/e/firefox"))
|
|
assert calls == [(binary, None, "firefox-68", 4, 3), (binary, None, "firefox-68-again", 4, 3)]
|
|
hunt = tmp_path / "network" / "hunt"
|
|
assert sorted(p.name for p in hunt.iterdir()) == sorted(["summary.json"] + ["%s-%d.json" % (e, i) for e in ("firefox-68", "firefox-68-again") for i in (1, 2, 3)])
|
|
summary = json.loads((hunt / "summary.json").read_text(encoding="utf-8"))
|
|
assert [(r["engine"], r["i"], r["count_3478"]) for r in summary["repetitions"]] == [("firefox-68", 1, 9), ("firefox-68", 2, 8), ("firefox-68", 3, 9), ("firefox-68-again", 1, 9), ("firefox-68-again", 2, 8),
|
|
("firefox-68-again", 3, 9)]
|
|
assert summary["repetitions"][0]["targets"][ab.TURN_TARGET] == [["aa" * 12, 9]]
|
|
assert not (tmp_path / "network" / "diff-a-a.json").exists()
|
|
assert "hunt summary written" in capsys.readouterr().out
|
|
|
|
|
|
def test_a_hunt_decides_nothing_even_when_an_engine_run_fails(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(ab, "hunt_engine", lambda *a, **k: 1)
|
|
monkeypatch.setenv(ab.HUNT_ENV, "webrtc:4:2")
|
|
assert ab.main(["ab", "--a-a", "--engine-tag", "firefox-68", "--out", str(tmp_path / "network"), "--published-binary", "/e/firefox"]) == 0
|
|
assert json.loads((tmp_path / "network" / "hunt" / "summary.json").read_text(encoding="utf-8"))["repetitions"] == []
|
|
|
|
|
|
def test_a_hunt_variable_that_does_not_read_is_refused_and_runs_nothing(tmp_path, monkeypatch, capsys):
|
|
monkeypatch.setattr(ab, "hunt_engine", lambda *a, **k: pytest.fail("a spec that does not read runs nothing"))
|
|
monkeypatch.setenv(ab.HUNT_ENV, "webrtc:4")
|
|
assert ab.main(["ab", "--a-a", "--engine-tag", "firefox-68", "--out", str(tmp_path / "network"), "--published-binary", "/e/firefox"]) == 2
|
|
assert "FAIL AB_NETWORK_HUNT" in capsys.readouterr().out
|
|
|
|
|
|
def test_without_the_hunt_variable_ab_is_the_a_a_it_was(tmp_path, monkeypatch):
|
|
monkeypatch.delenv(ab.HUNT_ENV, raising=False)
|
|
monkeypatch.setattr(ab, "hunt_engine", lambda *a, **k: pytest.fail("no hunt without the variable"))
|
|
ran = []
|
|
monkeypatch.setattr(ab, "run_engine", lambda binary, seal, label, seeds, out: ran.append(label) or 1)
|
|
assert ab.main(["ab", "--a-a", "--engine-tag", "firefox-68", "--out", str(tmp_path / "network"), "--published-binary", "/e/firefox", "--seeds", "4"]) == 2
|
|
assert ran == ["firefox-68", "firefox-68-again"], "the A/A runs the engine twice and says so when neither made a transcript: its own red, as before"
|
|
|
|
|
|
def test_the_engine_of_a_hunt_runs_in_a_process_of_its_own_with_its_seal_and_its_own_cache(tmp_path, monkeypatch):
|
|
seen = {}
|
|
|
|
class Done:
|
|
returncode = 0
|
|
|
|
monkeypatch.setattr(ab.subprocess, "run", lambda cmd, env, timeout: seen.update(cmd=cmd, env=env, timeout=timeout) or Done())
|
|
assert ab.hunt_engine("/e/firefox", "/e/seal.json", "firefox-68-again", 4, 20, tmp_path / "hunt") == 0
|
|
cmd = seen["cmd"]
|
|
assert cmd[3:] == ["hunt-run", "--binary", "/e/firefox", "--label", "firefox-68-again", "--seed", "4", "--n", "20", "--out", str(tmp_path / "hunt")]
|
|
assert seen["env"]["INVISIBLE_SEAL_FILE"] == "/e/seal.json" and seen["env"]["INVISIBLE_CORE_AUTOFIX"] == "off"
|
|
assert seen["timeout"] == ab.HUNT_BASE_S + 20 * ab.HUNT_PER_REPETITION_S
|
|
|
|
|
|
def test_a_repetition_is_one_launch_the_walk_alone_and_the_evidence_read_after_the_close(monkeypatch):
|
|
order = []
|
|
|
|
class Session:
|
|
def __enter__(self):
|
|
order.append("open")
|
|
return types.SimpleNamespace(new_context=lambda **k: types.SimpleNamespace(new_page=lambda: order.append("page")))
|
|
|
|
def __exit__(self, *exc):
|
|
order.append("closed")
|
|
|
|
monkeypatch.setattr(ab, "launch", lambda binary, seed, lab, patch: Session())
|
|
monkeypatch.setattr(ab, "begin_launch", lambda seed, name: order.append("begin " + name))
|
|
monkeypatch.setattr(ab, "glean_ready", lambda session, seed, name: order.append("glean") or {})
|
|
monkeypatch.setattr(ab, "walk_webrtc", lambda browser, lab: order.append("walk") or {"udp": {}})
|
|
for walker in ("walk_journey", "walk_h3", "walk_https"):
|
|
monkeypatch.setattr(ab, walker, lambda *a, **k: pytest.fail("a hunt walks nothing but the WebRTC page"))
|
|
monkeypatch.setattr(ab, "webrtc_evidence", lambda lab: order.append("evidence") or {"datagrams": [{"target": ab.TURN_TARGET, "counted": True}, {"target": ab.TURN_TARGET, "counted": False}]})
|
|
record = ab.hunt_repetition("/e/firefox", "firefox-68", 3, 4, object())
|
|
assert order == ["begin hunt", "open", "page", "glean", "walk", "closed", "evidence"]
|
|
assert (record["engine"], record["i"], record["seed"], record["error"], record["counts"]) == ("firefox-68", 3, 4, None, {"counted": 1, "all": 2})
|
|
|
|
|
|
def test_a_repetition_that_fails_is_a_record_with_its_error_and_the_hunt_goes_on(monkeypatch):
|
|
def broken(binary, seed, lab, patch):
|
|
raise RuntimeError("the engine did not start 17")
|
|
|
|
monkeypatch.setattr(ab, "launch", broken)
|
|
monkeypatch.setattr(ab, "begin_launch", lambda seed, name: None)
|
|
record = ab.hunt_repetition("/e/firefox", "firefox-68", 1, 4, object())
|
|
assert record["error"] == "RuntimeError: the engine did not start N" and record["evidence"] is None and record["counts"] == {"counted": 0, "all": 0}
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------------------- e2e-hunt.yml
|
|
def steps_of(text):
|
|
"""The steps of the hunt job: ``{name: block}`` (the blocks of the lines that start with `` - ``)."""
|
|
job = text[text.index("\n hunt:\n"):]
|
|
blocks = re.split(r"\n(?= - )", job)
|
|
return {b.split("name:", 1)[1].split("\n", 1)[0].strip(): b for b in blocks if b.startswith(" - name:")}
|
|
|
|
|
|
def test_the_hunt_workflow_has_the_input_that_chooses_the_walk_and_stays_a_manual_one():
|
|
text = HUNT_WORKFLOW.read_text(encoding="utf-8")
|
|
on = text[text.index("\non:\n"):text.index("\npermissions:")]
|
|
assert " workflow_dispatch:\n" in on and "push:" not in on and "pull_request" not in on and "schedule:" not in on
|
|
inputs = on[on.index(" inputs:\n"):]
|
|
block = inputs[inputs.index(" ab_network_webrtc_seed:\n"):]
|
|
block = block[:block.index("\n os:")]
|
|
assert 'default: ""' in block and "required: false" in block, "empty is today's behaviour"
|
|
assert " runs:\n" in inputs and " os:\n" in inputs, "the inputs it had are still there"
|
|
|
|
|
|
def test_the_step_of_the_walk_runs_the_hunt_mode_only_when_the_input_is_set_and_the_e2e_loop_only_when_it_is_not():
|
|
steps = steps_of(HUNT_WORKFLOW.read_text(encoding="utf-8"))
|
|
walk = next(b for n, b in steps.items() if n.startswith("The network oracle's WebRTC walk of one seed"))
|
|
loop = steps["Loop until it catches one"]
|
|
assert "if: inputs.ab_network_webrtc_seed != ''" in walk and "if: inputs.ab_network_webrtc_seed == ''" in loop
|
|
assert "AB_NETWORK_HUNT: webrtc:${{ inputs.ab_network_webrtc_seed }}:${{ inputs.runs }}" in walk, "the seed and the number of repetitions come from the inputs, through the environment"
|
|
assert "${{ inputs." not in walk.split("run: |")[1], "no input is written into the script text"
|
|
assert "scripts/ci_ab_network.py ab --a-a --out network --published-binary \"$FF\"" in walk
|
|
assert "xvfb-run -a" in walk and 'RUNNER_OS" = "Linux"' in walk
|
|
assert "scripts/run_e2e.py" in loop and "run_e2e" not in walk
|
|
for name in ("The security state (a fixed directory of the repository)", "The GeoIP database (made by the repository, same bytes every run)"):
|
|
assert "if: inputs.ab_network_webrtc_seed != ''" in steps[name], "what only the hunt needs is made only for it"
|
|
names = list(steps)
|
|
assert names.index("The security state (a fixed directory of the repository)") < names.index("Fetch the published firefox binary") < names.index(next(n for n in names if n.startswith("The network oracle's WebRTC walk")))
|
|
|
|
|
|
def test_the_hunt_uploads_network_hunt_as_an_artifact_and_every_action_it_uses_is_pinned_by_sha():
|
|
text = HUNT_WORKFLOW.read_text(encoding="utf-8")
|
|
steps = steps_of(text)
|
|
upload = next(b for n, b in steps.items() if n.startswith("Upload the hunt of the network oracle's WebRTC walk"))
|
|
assert "if: always() && inputs.ab_network_webrtc_seed != ''" in upload and "path: network/hunt" in upload
|
|
assert re.search(r"uses: actions/upload-artifact@[0-9a-f]{40} # v4", upload)
|
|
for line in text.splitlines():
|
|
m = re.match(r"\s*- uses: (\S+)|\s+uses: (\S+)", line)
|
|
if m and not (m.group(1) or m.group(2)).startswith("./"):
|
|
assert re.search(r"@[0-9a-f]{40}\b", line), line
|