field_ios

In-the-field app for Radroots on iOS
git clone https://radroots.dev/git/field_ios.git
Log | Files | Refs | README | LICENSE

local-social-fixture.py (100786B)


      1 #!/usr/bin/env python3
      2 """Bounded loopback Nostr and Blossom fixture for iOS simulator tests."""
      3 
      4 from __future__ import annotations
      5 
      6 import argparse
      7 import base64
      8 import hashlib
      9 import http.server
     10 import importlib.metadata
     11 import importlib.util
     12 import ipaddress
     13 import json
     14 import os
     15 import re
     16 import select
     17 import signal
     18 import socket
     19 import socketserver
     20 import stat
     21 import struct
     22 import sys
     23 import threading
     24 import time
     25 import unicodedata
     26 from pathlib import Path
     27 from typing import Any
     28 
     29 from jsonschema import Draft202012Validator
     30 from jsonschema.exceptions import SchemaError, ValidationError
     31 
     32 try:
     33     from scripts.fixture_tool_dispatch import selector_command as run_selector_command
     34     from scripts import fixture_connections as connections
     35 except ModuleNotFoundError as error:
     36     if error.name != "scripts":
     37         raise
     38     from fixture_tool_dispatch import selector_command as run_selector_command
     39     import fixture_connections as connections
     40 
     41 MAX_HTTP_BODY = 16 * 1024 * 1024
     42 MAX_WEBSOCKET_MESSAGE = 2 * 1024 * 1024
     43 MAX_EVENTS = 256
     44 MAX_BLOBS = 16
     45 MAX_JSON_BYTES = 64 * 1024
     46 BUD11_EVENT_KIND = 24_242
     47 BUD11_CONTENT_MAX_BYTES = 4_096
     48 BUD11_AUTHORIZATION_MAX_BYTES = 16 * 1024
     49 BUD11_AUTHORIZATION_ENCODED_MAX_BYTES = 21_846
     50 BUD11_MAX_LIFETIME_SECONDS = 300
     51 BUD11_MAX_CREATED_AGE_SECONDS = 300
     52 BUD11_SERVER_DOMAIN = "127.0.0.1"
     53 BUD11_MUTATION_SCHEMA = "tera.ios.local-social.bud11-mutations.v1"
     54 BUD11_MUTATIONS = (
     55     ("canonical", "none", "http", True),
     56     ("wrong-scheme", "authorization_scheme", "http", False),
     57     ("padded-base64", "padded_base64", "http", False),
     58     ("wrong-kind", "wrong_kind", "http", False),
     59     ("empty-content", "empty_content", "http", False),
     60     ("oversized-content", "oversized_content", "http", False),
     61     ("leading-content-space", "leading_content_space", "http", False),
     62     ("control-content", "control_content", "http", False),
     63     ("missing-action", "missing_action", "http", False),
     64     ("wrong-action", "wrong_action", "http", False),
     65     ("duplicate-action", "duplicate_action", "http", False),
     66     ("wrong-hash", "wrong_hash", "http", False),
     67     ("duplicate-hash", "duplicate_hash", "http", False),
     68     ("missing-server", "missing_server", "http", False),
     69     ("wrong-server", "wrong_server", "http", False),
     70     ("uppercase-server", "uppercase_server", "http", False),
     71     ("duplicate-server", "duplicate_server", "http", False),
     72     ("missing-expiration", "missing_expiration", "http", False),
     73     ("noncanonical-expiration", "noncanonical_expiration", "http", False),
     74     ("expired", "expired", "http", False),
     75     ("created-at-not-past", "created_at_not_past", "http", False),
     76     ("lifetime-too-long", "lifetime_too_long", "http", False),
     77     ("unknown-tag", "unknown_tag", "http", False),
     78     ("event-id-mutation", "event_id", "http", False),
     79     ("signature-mutation", "signature", "http", False),
     80     ("relay-publication", "none", "relay", False),
     81 )
     82 PERSONA_ALIASES = ("P01", "P02", "P03", "P04", "P05")
     83 FLOW_KINDS = {
     84     "Update": 1,
     85     "PhotoUpdate": 1,
     86     "Ask": 1,
     87     "Event": 31923,
     88     "FoodAvailability": 30402,
     89 }
     90 PHOTO_PERSONAS = frozenset(("P01", "P03", "P04"))
     91 PERSONA_CONTROL_SCHEMA = "tera.ios.local-social.persona-control.v1"
     92 PERSONA_ATTEMPT_SCHEMA = "tera.ios.local-social.persona-attempt-evidence.v1"
     93 PERSONA_RESULT_V2_SCHEMA = "tera.ios.local-social.persona-results.v2"
     94 PERSONA_TEST_TARGET = "TeraUITests"
     95 PERSONA_TEST_IDENTIFIER = "TeraUITests/testLocalSocialDeterministicPersonas"
     96 PERSONA_TEST_ACTION = "test"
     97 PERSONA_TEST_CONFIGURATION = "Debug"
     98 PERSONA_XCRESULT_NODE_IDENTIFIER = (
     99     "TeraRemoteQualificationUITests/testLocalSocialDeterministicPersonas()"
    100 )
    101 PERSONA_XCRESULT_NODE_URL = (
    102     "test://com.apple.xcode/Tera/TeraUITests/"
    103     "TeraRemoteQualificationUITests/testLocalSocialDeterministicPersonas"
    104 )
    105 MAX_XCRESULT_JSON_BYTES = 1024 * 1024
    106 MAX_PERSONA_ATTACHMENT_BYTES = 64 * 1024
    107 MAX_PERSONA_ATTACHMENTS_BYTES = 15 * MAX_PERSONA_ATTACHMENT_BYTES
    108 MAX_RESULT_BUNDLE_ENTRIES = 65_536
    109 MAX_RESULT_BUNDLE_FILE_BYTES = 1024 * 1024 * 1024
    110 MAX_RESULT_BUNDLE_BYTES = 8 * 1024 * 1024 * 1024
    111 MAX_RESULT_BUNDLE_RELATIVE_PATH_BYTES = 1024
    112 RESULT_BUNDLE_DIGEST_DOMAIN = b"radroots.ios.persona_result_bundle.v1\0"
    113 VERIFIER_PYTHON = (3, 14, 7)
    114 VERIFIER_JSONSCHEMA = "4.26.0"
    115 PERSONA_ATTACHMENT_NAMES = tuple(
    116     f"radroots-local-social-P{persona:02d}-A{attempt:02d}.json"
    117     for persona in range(1, 6)
    118     for attempt in range(1, 4)
    119 )
    120 PERSONA_XCRESULT_ATTACHMENT_NAME = re.compile(
    121     r"^radroots-local-social-(P0[1-5]-A0[1-3])_0_"
    122     r"[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\.json$"
    123 )
    124 FORBIDDEN_EVIDENCE_KEYS = frozenset(
    125     ("private_key", "secret", "seed", "signed_event", "event_content", "raw_event")
    126 )
    127 
    128 
    129 def strict_object(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
    130     value: dict[str, Any] = {}
    131     for key, item in pairs:
    132         if key in value:
    133             raise ValueError("duplicate JSON member")
    134         value[key] = item
    135     return value
    136 
    137 
    138 def read_json(path: Path, maximum: int = MAX_JSON_BYTES) -> tuple[bytes, Any]:
    139     with path.open("rb") as stream:
    140         raw = stream.read(maximum + 1)
    141     if not raw or len(raw) > maximum:
    142         raise ValueError("JSON input is empty or exceeds its byte bound")
    143     value = json.loads(raw, object_pairs_hook=strict_object)
    144     return raw, value
    145 
    146 
    147 def read_json_bounded(path: Path, maximum: int) -> tuple[bytes, Any]:
    148     return read_json(path, maximum)
    149 
    150 
    151 def exact_keys(value: Any, keys: set[str], name: str) -> dict[str, Any]:
    152     if not isinstance(value, dict) or set(value) != keys:
    153         raise ValueError(f"{name} has an invalid field inventory")
    154     return value
    155 
    156 
    157 def validate_persona_suite(value: Any) -> dict[str, Any]:
    158     root = exact_keys(
    159         value,
    160         {"schema", "schema_version", "locale", "media_fixture_sha256", "personas"},
    161         "persona suite",
    162     )
    163     if (
    164         root["schema"] != "tera.ios.local-social.personas.v1"
    165         or root["schema_version"] != 1
    166         or root["locale"] != "en_US"
    167         or not lowercase_hex(root["media_fixture_sha256"], 64)
    168         or not isinstance(root["personas"], list)
    169         or len(root["personas"]) != 5
    170     ):
    171         raise ValueError("persona suite header is invalid")
    172     expected = (
    173         ("P01", "age_18_27", "experienced_direct", ("Update", "PhotoUpdate", "Ask")),
    174         (
    175             "P02",
    176             "age_18_27",
    177             "novice_progressive_disclosure",
    178             ("Event", "FoodAvailability", "Update"),
    179         ),
    180         (
    181             "P03",
    182             "age_18_27",
    183             "nontechnical_validation_recovery",
    184             ("PhotoUpdate", "Ask", "Event"),
    185         ),
    186         (
    187             "P04",
    188             "adult_other",
    189             "novice_accessibility_keyboard",
    190             ("FoodAvailability", "Update", "PhotoUpdate"),
    191         ),
    192         (
    193             "P05",
    194             "adult_other",
    195             "general_transport_retry_relaunch",
    196             ("Ask", "Event", "FoodAvailability"),
    197         ),
    198     )
    199     attempts: list[dict[str, Any]] = []
    200     for persona_index, (persona_value, expected_value) in enumerate(
    201         zip(root["personas"], expected, strict=True), 1
    202     ):
    203         persona = exact_keys(
    204             persona_value,
    205             {"alias", "synthetic_age_band", "interaction_profile", "attempts"},
    206             "persona",
    207         )
    208         alias, age_band, profile, flows = expected_value
    209         if (
    210             persona["alias"] != alias
    211             or persona["synthetic_age_band"] != age_band
    212             or persona["interaction_profile"] != profile
    213             or not isinstance(persona["attempts"], list)
    214             or len(persona["attempts"]) != 3
    215         ):
    216             raise ValueError("persona matrix is not exact")
    217         for attempt_index, (attempt_value, flow) in enumerate(
    218             zip(persona["attempts"], flows, strict=True), 1
    219         ):
    220             attempt = exact_keys(
    221                 attempt_value,
    222                 {"id", "order", "flow", "marker", "expected_failure"},
    223                 "attempt",
    224             )
    225             expected_id = f"{alias}-A{attempt_index:02d}"
    226             expected_order = (persona_index - 1) * 3 + attempt_index
    227             if (
    228                 attempt["id"] != expected_id
    229                 or attempt["order"] != expected_order
    230                 or attempt["flow"] != flow
    231                 or not isinstance(attempt["marker"], str)
    232                 or not 1 <= len(attempt["marker"].encode("ascii")) <= 24
    233                 or attempt["marker"]
    234                 != f"rr-{alias.lower()}-a{attempt_index:02d}-{flow_marker(flow)}"
    235                 or attempt["expected_failure"]
    236                 not in {"none", "validation_recovery", "transport_retry_relaunch"}
    237             ):
    238                 raise ValueError("persona attempt is not exact")
    239             attempts.append(attempt)
    240     if [item["expected_failure"] for item in attempts].count(
    241         "validation_recovery"
    242     ) != 1:
    243         raise ValueError("persona suite must contain one validation-recovery vector")
    244     if [item["expected_failure"] for item in attempts].count(
    245         "transport_retry_relaunch"
    246     ) != 1:
    247         raise ValueError("persona suite must contain one transport-retry vector")
    248     if any(sum(item["flow"] == flow for item in attempts) != 3 for flow in FLOW_KINDS):
    249         raise ValueError("each Add flow must have exactly three attempts")
    250     return root
    251 
    252 
    253 def validate_bud11_mutation_corpus(value: Any) -> dict[str, Any]:
    254     root = exact_keys(value, {"schema", "schema_version", "vectors"}, "BUD-11 corpus")
    255     if (
    256         root["schema"] != BUD11_MUTATION_SCHEMA
    257         or root["schema_version"] != 1
    258         or not isinstance(root["vectors"], list)
    259     ):
    260         raise ValueError("BUD-11 corpus header is invalid")
    261     observed = []
    262     for value in root["vectors"]:
    263         vector = exact_keys(
    264             value,
    265             {"id", "mutation", "surface", "expected_accepted"},
    266             "BUD-11 vector",
    267         )
    268         if (
    269             not isinstance(vector["id"], str)
    270             or not isinstance(vector["mutation"], str)
    271             or vector["surface"] not in {"http", "relay"}
    272             or type(vector["expected_accepted"]) is not bool
    273         ):
    274             raise ValueError("BUD-11 vector is invalid")
    275         observed.append(
    276             (
    277                 vector["id"],
    278                 vector["mutation"],
    279                 vector["surface"],
    280                 vector["expected_accepted"],
    281             )
    282         )
    283     if tuple(observed) != BUD11_MUTATIONS:
    284         raise ValueError("BUD-11 corpus inventory is invalid")
    285     return root
    286 
    287 
    288 def load_bud11_mutation_corpus(path: Path) -> tuple[bytes, dict[str, Any]]:
    289     raw, value = read_json(path)
    290     corpus = validate_bud11_mutation_corpus(value)
    291     canonical = (
    292         "{\n"
    293         f'  "schema": {json.dumps(value.get("schema"))},\n'
    294         f'  "schema_version": {json.dumps(value.get("schema_version"))},\n'
    295         '  "vectors": [\n'
    296         + ",\n".join(
    297             "    " + json.dumps(vector, ensure_ascii=False)
    298             for vector in value.get("vectors", [])
    299         )
    300         + "\n  ]\n}\n"
    301     ).encode("utf-8")
    302     if raw != canonical:
    303         raise ValueError("BUD-11 corpus is noncanonical")
    304     return raw, corpus
    305 
    306 
    307 def flow_marker(flow: str) -> str:
    308     return {
    309         "Update": "update",
    310         "PhotoUpdate": "photo",
    311         "Ask": "ask",
    312         "Event": "event",
    313         "FoodAvailability": "food",
    314     }[flow]
    315 
    316 
    317 def load_persona_suite(path: Path) -> tuple[bytes, dict[str, Any]]:
    318     raw, value = read_json(path)
    319     return raw, validate_persona_suite(value)
    320 
    321 
    322 def validate_schema_file(path: Path, expected_id: str) -> bytes:
    323     raw, _ = load_schema_file(path, expected_id)
    324     return raw
    325 
    326 
    327 def load_schema_file(path: Path, expected_id: str) -> tuple[bytes, dict[str, Any]]:
    328     raw, value = read_json(path)
    329     if not isinstance(value, dict):
    330         raise ValueError("schema boundary is invalid")
    331     root = exact_keys(value, set(value), "schema")
    332     if (
    333         root.get("$schema") != "https://json-schema.org/draft/2020-12/schema"
    334         or root.get("$id") != expected_id
    335         or root.get("type") != "object"
    336         or root.get("additionalProperties") is not False
    337     ):
    338         raise ValueError("schema boundary is invalid")
    339     if schema_contains_external_reference(root):
    340         raise ValueError("schema contains an external reference")
    341     try:
    342         Draft202012Validator.check_schema(root)
    343     except SchemaError as error:
    344         raise ValueError("schema fails Draft 2020-12 meta-validation") from error
    345     return raw, root
    346 
    347 
    348 def schema_contains_external_reference(value: Any) -> bool:
    349     if isinstance(value, dict):
    350         for key, item in value.items():
    351             if key in {"$ref", "$dynamicRef"} and (
    352                 not isinstance(item, str) or not item.startswith("#/")
    353             ):
    354                 return True
    355             if schema_contains_external_reference(item):
    356                 return True
    357     elif isinstance(value, list):
    358         return any(schema_contains_external_reference(item) for item in value)
    359     return False
    360 
    361 
    362 def validate_schema_instance(schema: dict[str, Any], value: Any, name: str) -> None:
    363     try:
    364         Draft202012Validator(schema).validate(value)
    365     except ValidationError as error:
    366         raise ValueError(f"{name} disagrees with its JSON schema") from error
    367 
    368 
    369 def verify_toolchain_identity() -> None:
    370     if sys.version_info[:3] != VERIFIER_PYTHON:
    371         raise RuntimeError("persona verifier Python identity is unavailable")
    372     try:
    373         schema_version = importlib.metadata.version("jsonschema")
    374     except importlib.metadata.PackageNotFoundError as error:
    375         raise RuntimeError(
    376             "persona verifier schema dependency is unavailable"
    377         ) from error
    378     if schema_version != VERIFIER_JSONSCHEMA:
    379         raise RuntimeError("persona verifier schema dependency is unavailable")
    380 
    381 
    382 def persona_attempts(suite: dict[str, Any]) -> dict[str, dict[str, Any]]:
    383     return {
    384         attempt["id"]: {**attempt, "persona": persona["alias"]}
    385         for persona in suite["personas"]
    386         for attempt in persona["attempts"]
    387     }
    388 
    389 
    390 def read_control(path: Path) -> dict[str, Any] | None:
    391     if not path.is_file():
    392         return None
    393     try:
    394         _, value = read_json(path, 1024)
    395         control = exact_keys(
    396             value,
    397             {"schema", "active_persona", "blossom_enabled"},
    398             "persona control",
    399         )
    400     except (OSError, UnicodeError, ValueError, json.JSONDecodeError):
    401         return None
    402     if (
    403         control["schema"] != PERSONA_CONTROL_SCHEMA
    404         or control["active_persona"] not in PERSONA_ALIASES
    405         or type(control["blossom_enabled"]) is not bool
    406     ):
    407         return None
    408     return control
    409 
    410 
    411 class FixtureState:
    412     def __init__(
    413         self,
    414         evidence: Path,
    415         control: Path,
    416         blossom_port: int,
    417         suite: dict[str, Any] | None = None,
    418     ) -> None:
    419         self._evidence = evidence
    420         self.control = control
    421         self.blossom_port = blossom_port
    422         self._suite = suite
    423         self._attempts = persona_attempts(suite) if suite is not None else {}
    424         self._lock = threading.Lock()
    425         self._events: dict[str, dict[str, Any]] = {}
    426         self._blobs: dict[str, tuple[bytes, str]] = {}
    427         self._upload_attempts = 0
    428         self._accepted_uploads = 0
    429         self._retrievals = 0
    430         self._subscriptions = 0
    431         self._subscriptions_by_persona = {alias: 0 for alias in PERSONA_ALIASES}
    432         self._accepted_attempts: dict[str, dict[str, Any]] = {}
    433         self._identity_by_persona: dict[str, str] = {}
    434         self._persona_by_identity: dict[str, str] = {}
    435         self._accepted_uploads_by_persona = {alias: 0 for alias in PERSONA_ALIASES}
    436         self._uploaded_digests_by_persona = {alias: set() for alias in PERSONA_ALIASES}
    437         self._retrievals_by_persona = {alias: set() for alias in PERSONA_ALIASES}
    438         self._unknown_attempts = 0
    439         self._duplicate_attempts = 0
    440         self._expected_failure_rejections = 0
    441         self._transport_rejected_attempts: set[str] = set()
    442         self._events_accepted_during_expected_failures = 0
    443         self._accepted_connections = 0
    444         self._rejected_connections = 0
    445         self._non_loopback_attempts = 0
    446         self._production_network_contacts = 0
    447         self._unintended_publications = 0
    448         self._write_evidence()
    449 
    450     def observe_connection(self, host: str) -> bool:
    451         try:
    452             permitted = ipaddress.ip_address(host).is_loopback
    453         except ValueError:
    454             permitted = False
    455         with self._lock:
    456             if permitted:
    457                 self._accepted_connections += 1
    458             else:
    459                 self._rejected_connections += 1
    460                 self._non_loopback_attempts += 1
    461                 self._production_network_contacts += 1
    462             self._write_evidence_locked()
    463         return permitted
    464 
    465     def publish(self, event: dict[str, Any]) -> bool | None:
    466         if not valid_nostr_event(event) or not verify_nostr_signature(event):
    467             return False
    468         if event["kind"] == BUD11_EVENT_KIND:
    469             if self._suite is not None:
    470                 with self._lock:
    471                     self._unintended_publications += 1
    472                     self._write_evidence_locked()
    473             return False
    474         if self._suite is not None:
    475             return self._publish_persona_event(event)
    476         event_id = event["id"]
    477         with self._lock:
    478             if event_id not in self._events and len(self._events) >= MAX_EVENTS:
    479                 return False
    480             self._events[event_id] = event
    481             self._write_evidence_locked()
    482         return True
    483 
    484     def _publish_persona_event(self, event: dict[str, Any]) -> bool | None:
    485         attempt = classify_attempt(event, self._attempts)
    486         control = read_control(self.control)
    487         with self._lock:
    488             if attempt is None or control is None:
    489                 self._unknown_attempts += 1
    490                 self._unintended_publications += 1
    491                 self._write_evidence_locked()
    492                 return False
    493             attempt_id = attempt["id"]
    494             persona = attempt["persona"]
    495             if control["active_persona"] != persona:
    496                 self._unknown_attempts += 1
    497                 self._unintended_publications += 1
    498                 self._write_evidence_locked()
    499                 return False
    500             if event["kind"] != FLOW_KINDS[attempt["flow"]]:
    501                 self._unintended_publications += 1
    502                 self._write_evidence_locked()
    503                 return False
    504             public_key = event["pubkey"]
    505             existing_identity = self._identity_by_persona.get(persona)
    506             existing_persona = self._persona_by_identity.get(public_key)
    507             if (existing_identity is not None and existing_identity != public_key) or (
    508                 existing_persona is not None and existing_persona != persona
    509             ):
    510                 self._unintended_publications += 1
    511                 self._write_evidence_locked()
    512                 return False
    513             self._identity_by_persona[persona] = public_key
    514             self._persona_by_identity[public_key] = persona
    515             if (
    516                 attempt["expected_failure"] == "transport_retry_relaunch"
    517                 and attempt_id not in self._transport_rejected_attempts
    518             ):
    519                 self._transport_rejected_attempts.add(attempt_id)
    520                 self._expected_failure_rejections += 1
    521                 self._write_evidence_locked()
    522                 return None
    523             if attempt_id in self._accepted_attempts:
    524                 self._duplicate_attempts += 1
    525                 self._write_evidence_locked()
    526                 return False
    527             if len(self._events) >= MAX_EVENTS:
    528                 return False
    529             self._events[event["id"]] = event
    530             self._accepted_attempts[attempt_id] = {
    531                 "id": attempt_id,
    532                 "flow": attempt["flow"],
    533                 "event_kind": event["kind"],
    534                 "accepted": True,
    535                 "expected_failure_rejections": int(
    536                     attempt_id in self._transport_rejected_attempts
    537                 ),
    538             }
    539             self._write_evidence_locked()
    540         return True
    541 
    542     def query(self, filters: list[dict[str, Any]]) -> list[dict[str, Any]]:
    543         control = read_control(self.control)
    544         with self._lock:
    545             self._subscriptions += 1
    546             if control is not None:
    547                 self._subscriptions_by_persona[control["active_persona"]] += 1
    548             events = list(self._events.values())
    549             self._write_evidence_locked()
    550         selected = [
    551             event for event in events if any(matches(event, item) for item in filters)
    552         ]
    553         selected.sort(key=lambda item: (item.get("created_at", 0), item.get("id", "")))
    554         limits = [
    555             item["limit"]
    556             for item in filters
    557             if isinstance(item, dict) and type(item.get("limit")) is int
    558         ]
    559         count = min(max(max(limits, default=len(selected)), 0), MAX_EVENTS)
    560         return selected[-count:] if count else []
    561 
    562     def upload(
    563         self,
    564         body: bytes,
    565         media_type: str,
    566         expected_hash: str,
    567         authorization: str,
    568     ) -> tuple[bool, dict[str, Any]]:
    569         digest = hashlib.sha256(body).hexdigest()
    570         control = read_control(self.control)
    571         with self._lock:
    572             self._upload_attempts += 1
    573             persona = control["active_persona"] if control is not None else None
    574             if self._suite is None:
    575                 allowed = self.control.is_file() and valid_blossom_authorization(
    576                     authorization, expected_hash, BUD11_SERVER_DOMAIN
    577                 )
    578             else:
    579                 allowed = (
    580                     control is not None
    581                     and control["blossom_enabled"]
    582                     and persona in PHOTO_PERSONAS
    583                     and valid_blossom_authorization(
    584                         authorization, expected_hash, BUD11_SERVER_DOMAIN
    585                     )
    586                 )
    587             capacity = digest in self._blobs or len(self._blobs) < MAX_BLOBS
    588             if allowed and capacity and digest == expected_hash:
    589                 self._blobs[digest] = (body, media_type)
    590                 self._accepted_uploads += 1
    591                 if persona is not None:
    592                     self._accepted_uploads_by_persona[persona] += 1
    593                     self._uploaded_digests_by_persona[persona].add(digest)
    594             self._write_evidence_locked()
    595         descriptor = {
    596             "url": f"http://127.0.0.1:{self.blossom_port}/{digest}.png",
    597             "sha256": digest,
    598             "size": len(body),
    599             "type": media_type,
    600             "uploaded": int(time.time()),
    601         }
    602         return allowed and capacity and digest == expected_hash, descriptor
    603 
    604     def retrieve(self, digest: str) -> tuple[bytes, str] | None:
    605         control = read_control(self.control)
    606         with self._lock:
    607             value = self._blobs.get(digest)
    608             if value is not None:
    609                 if self._suite is None:
    610                     self._retrievals += 1
    611                 elif control is not None:
    612                     persona = control["active_persona"]
    613                     if digest in self._uploaded_digests_by_persona[persona]:
    614                         before = len(self._retrievals_by_persona[persona])
    615                         self._retrievals_by_persona[persona].add(digest)
    616                         self._retrievals += (
    617                             len(self._retrievals_by_persona[persona]) - before
    618                         )
    619                 self._write_evidence_locked()
    620             return value
    621 
    622     def _write_evidence(self) -> None:
    623         with self._lock:
    624             self._write_evidence_locked()
    625 
    626     def _write_evidence_locked(self) -> None:
    627         if self._suite is None:
    628             payload = {
    629                 "schema": "tera-ios-local-social-fixture-evidence-v1",
    630                 "schema_version": 1,
    631                 "accepted_events": len(self._events),
    632                 "event_kinds": sorted(
    633                     event["kind"]
    634                     for event in self._events.values()
    635                     if isinstance(event.get("kind"), int)
    636                 ),
    637                 "subscriptions": self._subscriptions,
    638                 "upload_attempts": self._upload_attempts,
    639                 "accepted_uploads": self._accepted_uploads,
    640                 "retrievals": self._retrievals,
    641                 "accepted_connections": self._accepted_connections,
    642                 "rejected_connections": self._rejected_connections,
    643                 "non_loopback_attempts": self._non_loopback_attempts,
    644                 "production_network_contacts": self._production_network_contacts,
    645             }
    646         else:
    647             payload = self._persona_evidence()
    648         temporary = self._evidence.with_suffix(".tmp")
    649         temporary.write_text(
    650             json.dumps(payload, sort_keys=True) + "\n", encoding="utf-8"
    651         )
    652         os.replace(temporary, self._evidence)
    653 
    654     def _persona_evidence(self) -> dict[str, Any]:
    655         personas = []
    656         for persona in self._suite["personas"]:
    657             alias = persona["alias"]
    658             public_key = self._identity_by_persona.get(alias)
    659             personas.append(
    660                 {
    661                     "alias": alias,
    662                     "identity_sha256": identity_digest(public_key),
    663                     "subscriptions": self._subscriptions_by_persona[alias],
    664                     "accepted_uploads": self._accepted_uploads_by_persona[alias],
    665                     "retrievals": len(self._retrievals_by_persona[alias]),
    666                     "attempts": [
    667                         self._accepted_attempts[attempt["id"]]
    668                         for attempt in persona["attempts"]
    669                         if attempt["id"] in self._accepted_attempts
    670                     ],
    671                 }
    672             )
    673         kind_counts = {
    674             str(kind): sum(event["kind"] == kind for event in self._events.values())
    675             for kind in (1, 31923, 30402)
    676         }
    677         flow_counts = {
    678             flow: sum(item["flow"] == flow for item in self._accepted_attempts.values())
    679             for flow in FLOW_KINDS
    680         }
    681         return {
    682             "schema": "tera.ios.local-social.persona-evidence.v1",
    683             "schema_version": 1,
    684             "personas": personas,
    685             "flow_counts": flow_counts,
    686             "accepted_events": len(self._events),
    687             "event_kind_counts": kind_counts,
    688             "upload_attempts": self._upload_attempts,
    689             "accepted_uploads": self._accepted_uploads,
    690             "retrievals": self._retrievals,
    691             "distinct_identities": len(self._persona_by_identity),
    692             "unknown_attempts": self._unknown_attempts,
    693             "duplicate_attempts": self._duplicate_attempts,
    694             "expected_failure_rejections": self._expected_failure_rejections,
    695             "events_accepted_during_expected_failures": (
    696                 self._events_accepted_during_expected_failures
    697             ),
    698             "accepted_connections": self._accepted_connections,
    699             "rejected_connections": self._rejected_connections,
    700             "non_loopback_attempts": self._non_loopback_attempts,
    701             "production_network_contacts": self._production_network_contacts,
    702             "unintended_publications": self._unintended_publications,
    703             "final_candidate_data_loss": 0,
    704         }
    705 
    706 
    707 def matches(event: dict[str, Any], item: dict[str, Any]) -> bool:
    708     if not isinstance(item, dict):
    709         return False
    710     if isinstance(item.get("ids"), list) and event.get("id") not in item["ids"]:
    711         return False
    712     if (
    713         isinstance(item.get("authors"), list)
    714         and event.get("pubkey") not in item["authors"]
    715     ):
    716         return False
    717     if isinstance(item.get("kinds"), list) and event.get("kind") not in item["kinds"]:
    718         return False
    719     created_at = event.get("created_at")
    720     if not isinstance(created_at, int):
    721         return False
    722     if isinstance(item.get("since"), int) and created_at < item["since"]:
    723         return False
    724     if isinstance(item.get("until"), int) and created_at > item["until"]:
    725         return False
    726     tags = event.get("tags")
    727     if not isinstance(tags, list):
    728         return False
    729     for key, expected in item.items():
    730         if not key.startswith("#") or not isinstance(expected, list):
    731             continue
    732         name = key[1:]
    733         if not any(
    734             isinstance(tag, list)
    735             and len(tag) >= 2
    736             and tag[0] == name
    737             and tag[1] in expected
    738             for tag in tags
    739         ):
    740             return False
    741     return True
    742 
    743 
    744 def valid_nostr_event(event: dict[str, Any]) -> bool:
    745     if not isinstance(event, dict) or set(event) != {
    746         "id",
    747         "pubkey",
    748         "created_at",
    749         "kind",
    750         "tags",
    751         "content",
    752         "sig",
    753     }:
    754         return False
    755     if not lowercase_hex(event["id"], 64) or not lowercase_hex(event["pubkey"], 64):
    756         return False
    757     if not lowercase_hex(event["sig"], 128):
    758         return False
    759     if type(event["created_at"]) is not int or event["created_at"] < 0:
    760         return False
    761     if type(event["kind"]) is not int or not 0 <= event["kind"] <= 0xFFFF_FFFF:
    762         return False
    763     if not isinstance(event["content"], str) or not isinstance(event["tags"], list):
    764         return False
    765     if not all(
    766         isinstance(tag, list) and all(isinstance(value, str) for value in tag)
    767         for tag in event["tags"]
    768     ):
    769         return False
    770     preimage = json.dumps(
    771         [
    772             0,
    773             event["pubkey"],
    774             event["created_at"],
    775             event["kind"],
    776             event["tags"],
    777             event["content"],
    778         ],
    779         ensure_ascii=False,
    780         separators=(",", ":"),
    781     ).encode("utf-8")
    782     return hashlib.sha256(preimage).hexdigest() == event["id"]
    783 
    784 
    785 def lowercase_hex(value: Any, length: int) -> bool:
    786     return (
    787         isinstance(value, str)
    788         and len(value) == length
    789         and all(character in "0123456789abcdef" for character in value)
    790     )
    791 
    792 
    793 def classify_attempt(
    794     event: dict[str, Any], attempts: dict[str, dict[str, Any]]
    795 ) -> dict[str, Any] | None:
    796     values = [event.get("content")]
    797     for tag in event.get("tags", []):
    798         values.extend(tag)
    799     matches = [
    800         attempt
    801         for attempt in attempts.values()
    802         if (
    803             photo_attempt_matches(event, attempt)
    804             if attempt.get("flow") == "PhotoUpdate"
    805             else attempt["marker"] in values
    806         )
    807     ]
    808     return matches[0] if len(matches) == 1 else None
    809 
    810 
    811 def photo_attempt_matches(event: dict[str, Any], attempt: dict[str, Any]) -> bool:
    812     if attempt.get("flow") != "PhotoUpdate":
    813         return False
    814     content = event.get("content")
    815     if not isinstance(content, str):
    816         return False
    817     lines = content.split("\n")
    818     if len(lines) != 2 or lines[0] != attempt.get("marker"):
    819         return False
    820     url = lines[1]
    821     match = re.fullmatch(
    822         r"http://127\.0\.0\.1:([1-9][0-9]{0,4})/([0-9a-f]{64})\.png", url
    823     )
    824     if match is None or int(match.group(1)) > 65535:
    825         return False
    826     digest = match.group(2)
    827     imeta = [
    828         tag
    829         for tag in event.get("tags", [])
    830         if isinstance(tag, list) and tag[:1] == ["imeta"]
    831     ]
    832     return len(imeta) == 1 and all(
    833         field in imeta[0] for field in (f"url {url}", f"x {digest}", "m image/png")
    834     )
    835 
    836 
    837 def identity_digest(public_key: str | None) -> str:
    838     if public_key is None:
    839         return "0" * 64
    840     return hashlib.sha256(
    841         b"radroots.ios.local-social.persona-identity.v1\0" + bytes.fromhex(public_key)
    842     ).hexdigest()
    843 
    844 
    845 SECP256K1_FIELD = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
    846 SECP256K1_ORDER = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
    847 SECP256K1_GENERATOR = (
    848     0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
    849     0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8,
    850 )
    851 
    852 
    853 def point_add(
    854     left: tuple[int, int] | None, right: tuple[int, int] | None
    855 ) -> tuple[int, int] | None:
    856     if left is None:
    857         return right
    858     if right is None:
    859         return left
    860     if left[0] == right[0] and left[1] != right[1]:
    861         return None
    862     if left == right:
    863         if left[1] == 0:
    864             return None
    865         slope = (3 * left[0] * left[0]) * pow(2 * left[1], -1, SECP256K1_FIELD)
    866     else:
    867         slope = (right[1] - left[1]) * pow(right[0] - left[0], -1, SECP256K1_FIELD)
    868     slope %= SECP256K1_FIELD
    869     x = (slope * slope - left[0] - right[0]) % SECP256K1_FIELD
    870     y = (slope * (left[0] - x) - left[1]) % SECP256K1_FIELD
    871     return x, y
    872 
    873 
    874 def point_multiply(value: int, point: tuple[int, int]) -> tuple[int, int] | None:
    875     result = None
    876     current: tuple[int, int] | None = point
    877     while value:
    878         if value & 1:
    879             result = point_add(result, current)
    880         current = point_add(current, current)
    881         value >>= 1
    882     return result
    883 
    884 
    885 def tagged_hash(tag: str, payload: bytes) -> bytes:
    886     tag_hash = hashlib.sha256(tag.encode("ascii")).digest()
    887     return hashlib.sha256(tag_hash + tag_hash + payload).digest()
    888 
    889 
    890 def verify_bip340(public_key: bytes, message: bytes, signature: bytes) -> bool:
    891     if len(public_key) != 32 or len(message) != 32 or len(signature) != 64:
    892         return False
    893     x = int.from_bytes(public_key, "big")
    894     r = int.from_bytes(signature[:32], "big")
    895     s = int.from_bytes(signature[32:], "big")
    896     if x >= SECP256K1_FIELD or r >= SECP256K1_FIELD or s >= SECP256K1_ORDER:
    897         return False
    898     y_squared = (pow(x, 3, SECP256K1_FIELD) + 7) % SECP256K1_FIELD
    899     y = pow(y_squared, (SECP256K1_FIELD + 1) // 4, SECP256K1_FIELD)
    900     if pow(y, 2, SECP256K1_FIELD) != y_squared:
    901         return False
    902     if y & 1:
    903         y = SECP256K1_FIELD - y
    904     challenge = (
    905         int.from_bytes(
    906             tagged_hash("BIP0340/challenge", signature[:32] + public_key + message),
    907             "big",
    908         )
    909         % SECP256K1_ORDER
    910     )
    911     negative = (x, (-y) % SECP256K1_FIELD)
    912     candidate = point_add(
    913         point_multiply(s, SECP256K1_GENERATOR),
    914         point_multiply(challenge, negative),
    915     )
    916     return candidate is not None and candidate[1] % 2 == 0 and candidate[0] == r
    917 
    918 
    919 def verify_nostr_signature(event: dict[str, Any]) -> bool:
    920     try:
    921         return verify_bip340(
    922             bytes.fromhex(event["pubkey"]),
    923             bytes.fromhex(event["id"]),
    924             bytes.fromhex(event["sig"]),
    925         )
    926     except (KeyError, TypeError, ValueError):
    927         return False
    928 
    929 
    930 def valid_bud11_server_domain(value: Any) -> bool:
    931     if (
    932         not isinstance(value, str)
    933         or not value
    934         or len(value) > 253
    935         or not value.isascii()
    936         or value.lower() != value
    937     ):
    938         return False
    939     try:
    940         address = ipaddress.ip_address(value)
    941     except ValueError:
    942         labels = value.split(".")
    943         return not all(label.isdigit() for label in labels) and all(
    944             1 <= len(label) <= 63
    945             and re.fullmatch(r"[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", label) is not None
    946             for label in labels
    947         )
    948     return isinstance(address, ipaddress.IPv4Address) and str(address) == value
    949 
    950 
    951 def valid_bud11_content(value: Any) -> bool:
    952     return (
    953         isinstance(value, str)
    954         and value
    955         and len(value.encode("utf-8")) <= BUD11_CONTENT_MAX_BYTES
    956         and value.strip() == value
    957         and not any(
    958             unicodedata.category(character) == "Cc" and character not in "\t\n\r"
    959             for character in value
    960         )
    961     )
    962 
    963 
    964 def canonical_unsigned_decimal(value: Any) -> int | None:
    965     if (
    966         not isinstance(value, str)
    967         or not value
    968         or len(value) > 20
    969         or any(character not in "0123456789" for character in value)
    970         or (len(value) > 1 and value.startswith("0"))
    971     ):
    972         return None
    973     parsed = int(value)
    974     return parsed if parsed <= 0xFFFF_FFFF_FFFF_FFFF else None
    975 
    976 
    977 def valid_blossom_authorization(
    978     authorization: str,
    979     expected_hash: str,
    980     expected_server: str,
    981     now_unix_s: int | None = None,
    982 ) -> bool:
    983     if not lowercase_hex(expected_hash, 64) or not valid_bud11_server_domain(
    984         expected_server
    985     ):
    986         return False
    987     if not authorization.startswith("Nostr "):
    988         return False
    989     payload = authorization.removeprefix("Nostr ")
    990     if (
    991         not payload
    992         or len(payload) > BUD11_AUTHORIZATION_ENCODED_MAX_BYTES
    993         or any(character.isspace() for character in payload)
    994         or "=" in payload
    995         or re.fullmatch(r"[A-Za-z0-9_-]+", payload) is None
    996     ):
    997         return False
    998     try:
    999         raw = base64.urlsafe_b64decode(payload + "=" * (-len(payload) % 4))
   1000         if len(raw) > BUD11_AUTHORIZATION_MAX_BYTES:
   1001             return False
   1002         if base64.urlsafe_b64encode(raw).rstrip(b"=").decode("ascii") != payload:
   1003             return False
   1004         event = json.loads(raw, object_pairs_hook=strict_object)
   1005     except (ValueError, json.JSONDecodeError):
   1006         return False
   1007     if not valid_nostr_event(event) or not verify_nostr_signature(event):
   1008         return False
   1009     if event["kind"] != BUD11_EVENT_KIND or not valid_bud11_content(event["content"]):
   1010         return False
   1011     if len(event["tags"]) != 4 or event["tags"][0] != ["t", "upload"]:
   1012         return False
   1013     expiration_tag = event["tags"][1]
   1014     if (
   1015         len(expiration_tag) != 2
   1016         or expiration_tag[0] != "expiration"
   1017         or event["tags"][2] != ["x", expected_hash]
   1018         or event["tags"][3] != ["server", expected_server]
   1019     ):
   1020         return False
   1021     expiration = canonical_unsigned_decimal(expiration_tag[1])
   1022     now = int(time.time()) if now_unix_s is None else now_unix_s
   1023     return (
   1024         type(now) is int
   1025         and now >= 0
   1026         and expiration is not None
   1027         and event["created_at"] < now < expiration
   1028         and now - event["created_at"] <= BUD11_MAX_CREATED_AGE_SECONDS
   1029         and 0 < expiration - event["created_at"] <= BUD11_MAX_LIFETIME_SECONDS
   1030     )
   1031 
   1032 
   1033 class ObservableLoopbackServerMixin:
   1034     _fixture_state: FixtureState
   1035 
   1036     def __init__(self, server_address, handler, state):
   1037         self._fixture_state = state
   1038         super().__init__(server_address, handler)
   1039 
   1040     def verify_request(
   1041         self, request: socket.socket, client_address: tuple[str, int]
   1042     ) -> bool:
   1043         del request
   1044         return self._fixture_state.observe_connection(client_address[0])
   1045 
   1046 
   1047 class ReusableThreadingServer(
   1048     ObservableLoopbackServerMixin,
   1049     connections.OwnedConnectionsMixin,
   1050     socketserver.ThreadingTCPServer,
   1051 ):
   1052     allow_reuse_address = True
   1053     daemon_threads = True
   1054 
   1055 
   1056 class ObservableLoopbackHTTPServer(
   1057     ObservableLoopbackServerMixin, connections.OwnedHTTPServer
   1058 ):
   1059     pass
   1060 
   1061 
   1062 class LoopbackConnectionFactory:
   1063     @staticmethod
   1064     def relay(port: int, state: FixtureState) -> ReusableThreadingServer:
   1065         return ReusableThreadingServer(("127.0.0.1", port), RelayHandler, state)
   1066 
   1067     @staticmethod
   1068     def blossom(port: int, state: FixtureState) -> ObservableLoopbackHTTPServer:
   1069         return ObservableLoopbackHTTPServer(("127.0.0.1", port), BlossomHandler, state)
   1070 
   1071 
   1072 class RelayHandler(socketserver.BaseRequestHandler):
   1073     state: FixtureState
   1074 
   1075     def handle(self) -> None:
   1076         self.request.settimeout(15)
   1077         headers = read_until(self.request, b"\r\n\r\n", 16 * 1024)
   1078         lines = headers.decode("ascii").split("\r\n")
   1079         if not lines or lines[0] != "GET / HTTP/1.1":
   1080             return
   1081         fields = {}
   1082         for line in lines[1:]:
   1083             if ":" in line:
   1084                 key, value = line.split(":", 1)
   1085                 fields[key.lower()] = value.strip()
   1086         key = fields.get("sec-websocket-key")
   1087         if not key or fields.get("upgrade", "").lower() != "websocket":
   1088             return
   1089         accept = base64.b64encode(
   1090             hashlib.sha1(
   1091                 (key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").encode()
   1092             ).digest()
   1093         ).decode()
   1094         self.request.sendall(
   1095             (
   1096                 "HTTP/1.1 101 Switching Protocols\r\n"
   1097                 "Upgrade: websocket\r\n"
   1098                 "Connection: Upgrade\r\n"
   1099                 f"Sec-WebSocket-Accept: {accept}\r\n\r\n"
   1100             ).encode("ascii")
   1101         )
   1102         while True:
   1103             frame = read_frame(self.request)
   1104             if frame is None:
   1105                 return
   1106             opcode, payload = frame
   1107             if opcode == 0x8:
   1108                 return
   1109             if opcode == 0x9:
   1110                 send_frame(self.request, 0xA, payload)
   1111                 continue
   1112             if opcode != 0x1:
   1113                 continue
   1114             try:
   1115                 message = json.loads(payload)
   1116             except (UnicodeDecodeError, json.JSONDecodeError):
   1117                 continue
   1118             if not isinstance(message, list) or not message:
   1119                 continue
   1120             if (
   1121                 message[0] == "EVENT"
   1122                 and len(message) == 2
   1123                 and isinstance(message[1], dict)
   1124             ):
   1125                 event_id = message[1].get("id", "")
   1126                 accepted = self.state.publish(message[1])
   1127                 if accepted is None:
   1128                     return
   1129                 send_json(
   1130                     self.request,
   1131                     ["OK", event_id, accepted, "" if accepted else "invalid"],
   1132                 )
   1133             elif (
   1134                 message[0] == "REQ"
   1135                 and len(message) >= 3
   1136                 and isinstance(message[1], str)
   1137             ):
   1138                 subscription = message[1]
   1139                 filters = [value for value in message[2:] if isinstance(value, dict)]
   1140                 for event in self.state.query(filters):
   1141                     send_json(self.request, ["EVENT", subscription, event])
   1142                 send_json(self.request, ["EOSE", subscription])
   1143 
   1144 
   1145 def read_until(stream: socket.socket, marker: bytes, maximum: int) -> bytes:
   1146     return connections.recv_until(stream, marker, maximum)
   1147 
   1148 
   1149 def read_exact(stream: socket.socket, length: int, until=None) -> bytes:
   1150     return connections.recv_exact(stream, length, until)
   1151 
   1152 
   1153 def read_frame(stream: socket.socket) -> tuple[int, bytes] | None:
   1154     try:
   1155         # Idle is not a failure vector; partial frames retain the read deadline.
   1156         while not select.select([stream], [], [], 15)[0]:
   1157             pass
   1158         return read_frame_body(stream, connections.deadline())
   1159     except (ConnectionError, OSError, ValueError):
   1160         return None
   1161 
   1162 
   1163 def read_frame_body(stream: socket.socket, until: float) -> tuple[int, bytes] | None:
   1164     first, second = read_exact(stream, 2, until)
   1165     if first & 0x80 == 0 or second & 0x80 == 0:
   1166         return None
   1167     length = second & 0x7F
   1168     if length == 126:
   1169         length = struct.unpack("!H", read_exact(stream, 2, until))[0]
   1170     elif length == 127:
   1171         length = struct.unpack("!Q", read_exact(stream, 8, until))[0]
   1172     if length > MAX_WEBSOCKET_MESSAGE:
   1173         return None
   1174     mask = read_exact(stream, 4, until)
   1175     payload = bytearray(read_exact(stream, length, until))
   1176     for index in range(length):
   1177         payload[index] ^= mask[index % 4]
   1178     return first & 0x0F, bytes(payload)
   1179 
   1180 
   1181 def send_frame(stream: socket.socket, opcode: int, payload: bytes) -> None:
   1182     length = len(payload)
   1183     header = bytearray([0x80 | opcode])
   1184     if length < 126:
   1185         header.append(length)
   1186     elif length <= 0xFFFF:
   1187         header.append(126)
   1188         header.extend(struct.pack("!H", length))
   1189     else:
   1190         header.append(127)
   1191         header.extend(struct.pack("!Q", length))
   1192     stream.sendall(header + payload)
   1193 
   1194 
   1195 def send_json(stream: socket.socket, value: Any) -> None:
   1196     send_frame(stream, 0x1, json.dumps(value, separators=(",", ":")).encode())
   1197 
   1198 
   1199 class BlossomHandler(http.server.BaseHTTPRequestHandler):
   1200     state: FixtureState
   1201     protocol_version = "HTTP/1.1"
   1202 
   1203     def setup(self) -> None:
   1204         super().setup()
   1205         self.rfile.close()
   1206         self.rfile = connections.http_reader(self.connection)
   1207 
   1208     def do_PUT(self) -> None:  # noqa: N802
   1209         if self.path != "/upload":
   1210             self.send_error(404)
   1211             return
   1212         try:
   1213             length = int(self.headers.get("Content-Length", "-1"))
   1214         except ValueError:
   1215             length = -1
   1216         authorization = self.headers.get("Authorization", "")
   1217         expected_hash = self.headers.get("X-SHA-256", "")
   1218         media_type = self.headers.get("Content-Type", "")
   1219         if (
   1220             length < 1
   1221             or length > MAX_HTTP_BODY
   1222             or not lowercase_hex(expected_hash, 64)
   1223             or not authorization.startswith("Nostr ")
   1224             or media_type != "image/png"
   1225         ):
   1226             self.send_error(400)
   1227             return
   1228         body = self.rfile.read(length)
   1229         if len(body) != length:
   1230             self.send_error(400)
   1231             return
   1232         accepted, descriptor = self.state.upload(
   1233             body, media_type, expected_hash, authorization
   1234         )
   1235         if not accepted:
   1236             self.respond(503, b'{"error":"fixture_retry_required"}', "application/json")
   1237             return
   1238         self.respond(
   1239             200,
   1240             json.dumps(descriptor, separators=(",", ":")).encode(),
   1241             "application/json",
   1242         )
   1243 
   1244     def do_GET(self) -> None:  # noqa: N802
   1245         component = self.path.removeprefix("/")
   1246         if component.endswith(".png") and lowercase_hex(component[:-4], 64):
   1247             value = self.state.retrieve(component[:-4])
   1248             if value is not None:
   1249                 self.respond(200, value[0], value[1])
   1250                 return
   1251         self.respond(404, b'{"error":"not_found"}', "application/json")
   1252 
   1253     def respond(self, status: int, body: bytes, content_type: str) -> None:
   1254         self.send_response(status)
   1255         self.send_header("Content-Type", content_type)
   1256         self.send_header("Content-Length", str(len(body)))
   1257         self.send_header("Connection", "close")
   1258         self.end_headers()
   1259         self.wfile.write(body)
   1260 
   1261     def log_message(self, format: str, *args: Any) -> None:
   1262         return
   1263 
   1264 
   1265 def serve(arguments: argparse.Namespace) -> int:
   1266     evidence = Path(arguments.evidence).resolve()
   1267     ready = Path(arguments.ready).resolve()
   1268     control = Path(arguments.control).resolve()
   1269     for path in (evidence, ready, control):
   1270         path.parent.mkdir(parents=True, exist_ok=True)
   1271     control.unlink(missing_ok=True)
   1272     ready.unlink(missing_ok=True)
   1273     suite = None
   1274     if arguments.persona_fixture is not None:
   1275         _, suite = load_persona_suite(Path(arguments.persona_fixture).resolve())
   1276     state = FixtureState(evidence, control, arguments.blossom_port, suite)
   1277     RelayHandler.state = state
   1278     BlossomHandler.state = state
   1279     relay = LoopbackConnectionFactory.relay(arguments.relay_port, state)
   1280     blossom = LoopbackConnectionFactory.blossom(arguments.blossom_port, state)
   1281     threads = [
   1282         threading.Thread(target=relay.serve_forever, daemon=True),
   1283         threading.Thread(target=blossom.serve_forever, daemon=True),
   1284     ]
   1285     for thread in threads:
   1286         thread.start()
   1287     ready.write_text(
   1288         json.dumps(
   1289             {
   1290                 "schema": "tera-ios-local-social-fixture-ready-v1",
   1291                 "relay": f"ws://127.0.0.1:{arguments.relay_port}",
   1292                 "blossom": f"http://127.0.0.1:{arguments.blossom_port}",
   1293             },
   1294             sort_keys=True,
   1295         )
   1296         + "\n",
   1297         encoding="utf-8",
   1298     )
   1299     stopped = threading.Event()
   1300 
   1301     def stop(_signum: int, _frame: Any) -> None:
   1302         stopped.set()
   1303 
   1304     signal.signal(signal.SIGTERM, stop)
   1305     signal.signal(signal.SIGINT, stop)
   1306     stopped.wait()
   1307     relay.shutdown()
   1308     blossom.shutdown()
   1309     relay.server_close()
   1310     blossom.server_close()
   1311     for thread in threads:
   1312         thread.join(timeout=5)
   1313     return 0
   1314 
   1315 
   1316 def verify(arguments: argparse.Namespace) -> int:
   1317     _, payload = read_json(Path(arguments.evidence).resolve())
   1318     if (
   1319         payload.get("schema") != "tera-ios-local-social-fixture-evidence-v1"
   1320         or payload.get("accepted_events", 0) < 5
   1321         or payload.get("subscriptions", 0) < 1
   1322         or payload.get("upload_attempts", 0) < 1
   1323         or payload.get("accepted_uploads", 0) < 1
   1324         or payload.get("retrievals", 0) < 1
   1325         or payload.get("non_loopback_attempts") != 0
   1326         or payload.get("production_network_contacts") != 0
   1327     ):
   1328         raise SystemExit("local-social fixture evidence is incomplete")
   1329     print("local-social fixture evidence verified")
   1330     return 0
   1331 
   1332 
   1333 def verify_accessibility(arguments: argparse.Namespace) -> int:
   1334     _, payload = read_json(Path(arguments.evidence).resolve())
   1335     if (
   1336         payload.get("schema") != "tera-ios-local-social-fixture-evidence-v1"
   1337         or payload.get("accepted_events") != 0
   1338         or payload.get("upload_attempts") != 0
   1339         or payload.get("accepted_uploads") != 0
   1340         or payload.get("retrievals") != 0
   1341         or payload.get("subscriptions", 0) < 1
   1342     ):
   1343         raise SystemExit("local-social accessibility fixture evidence is invalid")
   1344     print("local-social accessibility fixture evidence verified")
   1345     return 0
   1346 
   1347 
   1348 def verify_persona_fixture(arguments: argparse.Namespace) -> int:
   1349     raw, suite = load_persona_suite(Path(arguments.fixture).resolve())
   1350     fixture_schema, fixture_schema_value = load_schema_file(
   1351         Path(arguments.fixture_schema).resolve(),
   1352         "https://radroots.org/schemas/ios/local-social-personas.v1.schema.json",
   1353     )
   1354     result_schema = validate_schema_file(
   1355         Path(arguments.result_schema).resolve(),
   1356         "https://radroots.org/schemas/ios/local-social-persona-results.v1.schema.json",
   1357     )
   1358     attempt_schema = validate_schema_file(
   1359         Path(arguments.attempt_schema).resolve(),
   1360         "https://radroots.org/schemas/ios/local-social-persona-attempt-evidence.v1.schema.json",
   1361     )
   1362     result_v2_schema = validate_schema_file(
   1363         Path(arguments.result_v2_schema).resolve(),
   1364         "https://radroots.org/schemas/ios/local-social-persona-results.v2.schema.json",
   1365     )
   1366     validate_schema_instance(fixture_schema_value, suite, "persona fixture")
   1367     print(
   1368         "local-social persona fixtures verified: "
   1369         f"fixture={hashlib.sha256(raw).hexdigest()} "
   1370         f"fixture_schema={hashlib.sha256(fixture_schema).hexdigest()} "
   1371         f"result_schema={hashlib.sha256(result_schema).hexdigest()} "
   1372         f"attempt_schema={hashlib.sha256(attempt_schema).hexdigest()} "
   1373         f"result_v2_schema={hashlib.sha256(result_v2_schema).hexdigest()}"
   1374     )
   1375     return 0
   1376 
   1377 
   1378 def verify_bud11_corpus(arguments: argparse.Namespace) -> int:
   1379     raw, corpus = load_bud11_mutation_corpus(Path(arguments.corpus).resolve())
   1380     schema, schema_value = load_schema_file(
   1381         Path(arguments.schema).resolve(),
   1382         "https://radroots.org/schemas/ios/bud11-upload-authorization-mutations.v1.schema.json",
   1383     )
   1384     validate_schema_instance(schema_value, corpus, "BUD-11 corpus")
   1385     print(
   1386         "BUD-11 mutation corpus verified: "
   1387         f"corpus={hashlib.sha256(raw).hexdigest()} "
   1388         f"schema={hashlib.sha256(schema).hexdigest()}"
   1389     )
   1390     return 0
   1391 
   1392 
   1393 def bounded_directory_inventory(
   1394     path: Path,
   1395     maximum_entries: int = MAX_RESULT_BUNDLE_ENTRIES,
   1396     maximum_relative_path_bytes: int = MAX_RESULT_BUNDLE_RELATIVE_PATH_BYTES,
   1397 ) -> list[tuple[int, bytes, Path]]:
   1398     if path.is_symlink() or not path.is_dir():
   1399         raise ValueError("result bundle is not a regular directory")
   1400     pending = [(path, Path())]
   1401     inventory: list[tuple[int, bytes, Path]] = []
   1402     while pending:
   1403         directory, relative_directory = pending.pop()
   1404         with os.scandir(directory) as entries:
   1405             for entry in entries:
   1406                 relative_path = relative_directory / entry.name
   1407                 try:
   1408                     relative = relative_path.as_posix().encode("utf-8")
   1409                 except UnicodeError as error:
   1410                     raise ValueError("result bundle path is not UTF-8") from error
   1411                 if not relative or len(relative) > maximum_relative_path_bytes:
   1412                     raise ValueError("result bundle path exceeds its byte bound")
   1413                 if len(inventory) >= maximum_entries:
   1414                     raise ValueError("result bundle exceeds its entry bound")
   1415                 if entry.is_symlink():
   1416                     raise ValueError("result bundle contains a symbolic link")
   1417                 item = Path(entry.path)
   1418                 if entry.is_dir(follow_symlinks=False):
   1419                     inventory.append((0, relative, item))
   1420                     pending.append((item, relative_path))
   1421                 elif entry.is_file(follow_symlinks=False):
   1422                     inventory.append((1, relative, item))
   1423                 else:
   1424                     raise ValueError("result bundle contains an unsupported entry")
   1425     return sorted(inventory, key=lambda item: item[1])
   1426 
   1427 
   1428 def directory_digest(
   1429     path: Path,
   1430     maximum_entries: int = MAX_RESULT_BUNDLE_ENTRIES,
   1431     maximum_file_bytes: int = MAX_RESULT_BUNDLE_FILE_BYTES,
   1432     maximum_total_bytes: int = MAX_RESULT_BUNDLE_BYTES,
   1433 ) -> str:
   1434     inventory = bounded_directory_inventory(path, maximum_entries)
   1435     digest = hashlib.sha256()
   1436     digest.update(RESULT_BUNDLE_DIGEST_DOMAIN)
   1437     digest.update(len(inventory).to_bytes(4, "big"))
   1438     total_bytes = 0
   1439     for kind, relative, item in inventory:
   1440         digest.update(bytes((kind,)))
   1441         digest.update(len(relative).to_bytes(8, "big"))
   1442         digest.update(relative)
   1443         if kind == 0:
   1444             continue
   1445         descriptor = os.open(
   1446             item,
   1447             os.O_RDONLY | os.O_CLOEXEC | getattr(os, "O_NOFOLLOW", 0),
   1448         )
   1449         try:
   1450             before = os.fstat(descriptor)
   1451             if not stat.S_ISREG(before.st_mode) or before.st_size < 0:
   1452                 raise ValueError("result bundle contains an unsupported entry")
   1453             size = before.st_size
   1454             if size > maximum_file_bytes or total_bytes > maximum_total_bytes - size:
   1455                 raise ValueError("result bundle exceeds its byte bound")
   1456             digest.update(size.to_bytes(8, "big"))
   1457             remaining = size
   1458             with os.fdopen(descriptor, "rb", closefd=False) as stream:
   1459                 while remaining:
   1460                     chunk = stream.read(min(64 * 1024, remaining))
   1461                     if not chunk:
   1462                         raise ValueError("result bundle file changed while hashing")
   1463                     digest.update(chunk)
   1464                     remaining -= len(chunk)
   1465                 if stream.read(1):
   1466                     raise ValueError("result bundle file changed while hashing")
   1467             after = os.fstat(descriptor)
   1468             if (
   1469                 before.st_dev != after.st_dev
   1470                 or before.st_ino != after.st_ino
   1471                 or before.st_size != after.st_size
   1472                 or before.st_mode != after.st_mode
   1473                 or before.st_mtime_ns != after.st_mtime_ns
   1474                 or before.st_ctime_ns != after.st_ctime_ns
   1475             ):
   1476                 raise ValueError("result bundle file changed while hashing")
   1477             total_bytes += size
   1478         finally:
   1479             os.close(descriptor)
   1480     return digest.hexdigest()
   1481 
   1482 
   1483 def simulator_metadata(udid: str, result_bundle: Path) -> dict[str, str]:
   1484     devices = run_json_command_bounded(
   1485         ["xcrun", "simctl", "list", "devices", "--json"],
   1486         MAX_XCRESULT_JSON_BYTES,
   1487     )
   1488     matches = [
   1489         (runtime, device)
   1490         for runtime, values in devices.get("devices", {}).items()
   1491         for device in values
   1492         if device.get("udid") == udid
   1493     ]
   1494     if len(matches) != 1:
   1495         raise ValueError("simulator identity is unavailable")
   1496     runtime, simulator = matches[0]
   1497     runtime_version = runtime.rsplit("iOS-", 1)[-1].replace("-", ".")
   1498     summary = run_json_command_bounded(
   1499         [
   1500             "xcrun",
   1501             "xcresulttool",
   1502             "get",
   1503             "test-results",
   1504             "summary",
   1505             "--path",
   1506             str(result_bundle),
   1507         ],
   1508         MAX_XCRESULT_JSON_BYTES,
   1509     )
   1510     result_devices = [
   1511         row.get("device")
   1512         for row in summary.get("devicesAndConfigurations", [])
   1513         if isinstance(row, dict)
   1514         and isinstance(row.get("device"), dict)
   1515         and isinstance(row["device"].get("deviceId"), str)
   1516         and row["device"]["deviceId"].upper() == udid.upper()
   1517     ]
   1518     if len(result_devices) != 1:
   1519         raise ValueError("result bundle simulator identity is unavailable")
   1520     result_device = result_devices[0]
   1521     version = result_device.get("osVersion")
   1522     architecture = result_device.get("architecture")
   1523     if (
   1524         summary.get("result") != "Passed"
   1525         or summary.get("failedTests") != 0
   1526         or summary.get("passedTests") != 1
   1527         or summary.get("skippedTests") != 0
   1528         or simulator.get("isAvailable") is not True
   1529         or result_device.get("platform") != "iOS Simulator"
   1530         or version != runtime_version
   1531         or not isinstance(version, str)
   1532         or re.fullmatch(r"[1-9][0-9]*(\.[0-9]+){1,2}", version) is None
   1533         or int(version.split(".", 1)[0]) < 18
   1534         or architecture != "arm64"
   1535     ):
   1536         raise ValueError("simulator does not satisfy the development platform contract")
   1537     canonical_version = version if version.count(".") == 2 else f"{version}.0"
   1538     return {
   1539         "udid": udid.upper(),
   1540         "os": f"iOS {canonical_version}",
   1541         "architecture": architecture,
   1542     }
   1543 
   1544 
   1545 def validate_persona_evidence(value: Any, suite: dict[str, Any]) -> dict[str, Any]:
   1546     keys = {
   1547         "schema",
   1548         "schema_version",
   1549         "personas",
   1550         "flow_counts",
   1551         "accepted_events",
   1552         "event_kind_counts",
   1553         "upload_attempts",
   1554         "accepted_uploads",
   1555         "retrievals",
   1556         "distinct_identities",
   1557         "unknown_attempts",
   1558         "duplicate_attempts",
   1559         "expected_failure_rejections",
   1560         "events_accepted_during_expected_failures",
   1561         "accepted_connections",
   1562         "rejected_connections",
   1563         "non_loopback_attempts",
   1564         "production_network_contacts",
   1565         "unintended_publications",
   1566         "final_candidate_data_loss",
   1567     }
   1568     evidence = exact_keys(value, keys, "persona evidence")
   1569     if (
   1570         evidence["schema"] != "tera.ios.local-social.persona-evidence.v1"
   1571         or evidence["schema_version"] != 1
   1572     ):
   1573         raise ValueError("persona evidence header is invalid")
   1574     expected_scalars = {
   1575         "accepted_events": 15,
   1576         "accepted_uploads": 3,
   1577         "retrievals": 3,
   1578         "distinct_identities": 5,
   1579         "unknown_attempts": 0,
   1580         "duplicate_attempts": 0,
   1581         "expected_failure_rejections": 1,
   1582         "events_accepted_during_expected_failures": 0,
   1583         "non_loopback_attempts": 0,
   1584         "production_network_contacts": 0,
   1585         "unintended_publications": 0,
   1586         "final_candidate_data_loss": 0,
   1587     }
   1588     if any(evidence.get(key) != expected for key, expected in expected_scalars.items()):
   1589         raise ValueError("persona evidence totals are invalid")
   1590     if (
   1591         type(evidence["accepted_connections"]) is not int
   1592         or not 1 <= evidence["accepted_connections"] <= 61_440
   1593         or type(evidence["rejected_connections"]) is not int
   1594         or not 0 <= evidence["rejected_connections"] <= 61_440
   1595     ):
   1596         raise ValueError("persona connection evidence is invalid")
   1597     if evidence["flow_counts"] != {flow: 3 for flow in FLOW_KINDS}:
   1598         raise ValueError("persona flow counts are invalid")
   1599     if evidence["event_kind_counts"] != {"1": 9, "31923": 3, "30402": 3}:
   1600         raise ValueError("persona event kind counts are invalid")
   1601     if not isinstance(evidence["personas"], list) or len(evidence["personas"]) != 5:
   1602         raise ValueError("persona evidence inventory is invalid")
   1603     identity_digests = set()
   1604     for persona, expected in zip(evidence["personas"], suite["personas"], strict=True):
   1605         exact_keys(
   1606             persona,
   1607             {
   1608                 "alias",
   1609                 "identity_sha256",
   1610                 "subscriptions",
   1611                 "accepted_uploads",
   1612                 "retrievals",
   1613                 "attempts",
   1614             },
   1615             "persona evidence row",
   1616         )
   1617         if (
   1618             persona["alias"] != expected["alias"]
   1619             or not lowercase_hex(persona["identity_sha256"], 64)
   1620             or persona["identity_sha256"] == "0" * 64
   1621             or type(persona["subscriptions"]) is not int
   1622             or not 1 <= persona["subscriptions"] <= 4096
   1623             or persona["accepted_uploads"] != int(persona["alias"] in PHOTO_PERSONAS)
   1624             or persona["retrievals"] != int(persona["alias"] in PHOTO_PERSONAS)
   1625             or not isinstance(persona["attempts"], list)
   1626             or len(persona["attempts"]) != 3
   1627         ):
   1628             raise ValueError("persona evidence row is invalid")
   1629         identity_digests.add(persona["identity_sha256"])
   1630         for attempt, expected_attempt in zip(
   1631             persona["attempts"], expected["attempts"], strict=True
   1632         ):
   1633             exact_keys(
   1634                 attempt,
   1635                 {"id", "flow", "event_kind", "accepted", "expected_failure_rejections"},
   1636                 "attempt evidence row",
   1637             )
   1638             if (
   1639                 attempt["id"] != expected_attempt["id"]
   1640                 or attempt["flow"] != expected_attempt["flow"]
   1641                 or attempt["event_kind"] != FLOW_KINDS[expected_attempt["flow"]]
   1642                 or attempt["accepted"] is not True
   1643                 or attempt["expected_failure_rejections"]
   1644                 != int(
   1645                     expected_attempt["expected_failure"] == "transport_retry_relaunch"
   1646                 )
   1647             ):
   1648                 raise ValueError("attempt evidence row is invalid")
   1649     if len(identity_digests) != 5:
   1650         raise ValueError("persona identities are not distinct")
   1651     return evidence
   1652 
   1653 
   1654 def valid_ios_runtime(value: Any) -> bool:
   1655     if not isinstance(value, str) or not re.fullmatch(
   1656         r"iOS ([1-9][0-9]*)(\.[0-9]+){1,2}", value
   1657     ):
   1658         return False
   1659     return int(value.split()[1].split(".", 1)[0]) >= 18
   1660 
   1661 
   1662 def canonical_evidence_bytes(value: Any) -> bytes:
   1663     return json.dumps(
   1664         value, ensure_ascii=True, separators=(",", ":"), sort_keys=True
   1665     ).encode("utf-8")
   1666 
   1667 
   1668 def evidence_contains_forbidden_key(value: Any) -> bool:
   1669     if isinstance(value, dict):
   1670         return any(
   1671             key.lower() in FORBIDDEN_EVIDENCE_KEYS
   1672             or evidence_contains_forbidden_key(item)
   1673             for key, item in value.items()
   1674         )
   1675     if isinstance(value, list):
   1676         return any(evidence_contains_forbidden_key(item) for item in value)
   1677     return False
   1678 
   1679 
   1680 def persona_invocation() -> dict[str, str]:
   1681     return {
   1682         "target": PERSONA_TEST_TARGET,
   1683         "identifier": PERSONA_TEST_IDENTIFIER,
   1684         "action": PERSONA_TEST_ACTION,
   1685         "configuration": PERSONA_TEST_CONFIGURATION,
   1686     }
   1687 
   1688 
   1689 def validate_persona_attempt_evidence(
   1690     value: Any,
   1691     suite: dict[str, Any],
   1692     *,
   1693     require_measured_network: bool,
   1694 ) -> dict[str, Any]:
   1695     keys = {
   1696         "schema",
   1697         "schema_version",
   1698         "test_invocation",
   1699         "source",
   1700         "app_build_sha256",
   1701         "simulator",
   1702         "run_id",
   1703         "persona_run_id",
   1704         "persona_alias",
   1705         "attempt_id",
   1706         "attempt_order",
   1707         "flow",
   1708         "expected_failure",
   1709         "public_identity_sha256",
   1710         "endpoint_policy_sha256",
   1711         "ui_observation",
   1712         "network_observation",
   1713         "accessibility",
   1714         "artifact_digests",
   1715     }
   1716     attempt = exact_keys(value, keys, "persona attempt evidence")
   1717     if evidence_contains_forbidden_key(attempt):
   1718         raise ValueError("persona attempt evidence contains a forbidden field")
   1719     if (
   1720         attempt["schema"] != PERSONA_ATTEMPT_SCHEMA
   1721         or attempt["schema_version"] != 1
   1722         or attempt["test_invocation"] != persona_invocation()
   1723         or not lowercase_hex(attempt["app_build_sha256"], 64)
   1724         or not lowercase_hex(attempt["public_identity_sha256"], 64)
   1725         or attempt["public_identity_sha256"] == "0" * 64
   1726         or not lowercase_hex(attempt["endpoint_policy_sha256"], 64)
   1727         or not re.fullmatch(r"[a-z0-9][a-z0-9-]{6,62}[a-z0-9]", attempt["run_id"])
   1728     ):
   1729         raise ValueError("persona attempt evidence header is invalid")
   1730     source = exact_keys(attempt["source"], {"commit", "tree"}, "attempt source")
   1731     if not lowercase_hex(source["commit"], 40) or not lowercase_hex(source["tree"], 40):
   1732         raise ValueError("persona attempt source identity is invalid")
   1733     simulator = exact_keys(
   1734         attempt["simulator"], {"udid", "os", "architecture"}, "attempt simulator"
   1735     )
   1736     if (
   1737         not isinstance(simulator["udid"], str)
   1738         or re.fullmatch(r"[A-F0-9-]{36}", simulator["udid"]) is None
   1739         or not valid_ios_runtime(simulator["os"])
   1740         or simulator["architecture"] != "arm64"
   1741     ):
   1742         raise ValueError("persona attempt simulator is invalid")
   1743     expected_attempts = {
   1744         candidate["id"]: (persona, candidate)
   1745         for persona in suite["personas"]
   1746         for candidate in persona["attempts"]
   1747     }
   1748     expected = expected_attempts.get(attempt["attempt_id"])
   1749     if expected is None:
   1750         raise ValueError("persona attempt identity is unknown")
   1751     expected_persona, expected_attempt = expected
   1752     alias = expected_persona["alias"]
   1753     if (
   1754         attempt["persona_alias"] != alias
   1755         or attempt["attempt_order"] != expected_attempt["order"]
   1756         or attempt["flow"] != expected_attempt["flow"]
   1757         or attempt["expected_failure"] != expected_attempt["expected_failure"]
   1758         or re.fullmatch(
   1759             rf"persona-{alias.lower()}-[0-9a-f]{{24}}", attempt["persona_run_id"]
   1760         )
   1761         is None
   1762     ):
   1763         raise ValueError("persona attempt binding is invalid")
   1764     ui = exact_keys(
   1765         attempt["ui_observation"],
   1766         {
   1767             "validation_attempted",
   1768             "validation_rejected",
   1769             "retry_attempts",
   1770             "relaunches",
   1771             "retention_verified",
   1772             "today_projection_verified",
   1773         },
   1774         "attempt UI observation",
   1775     )
   1776     validation = expected_attempt["expected_failure"] == "validation_recovery"
   1777     retry = expected_attempt["expected_failure"] == "transport_retry_relaunch"
   1778     if (
   1779         ui["validation_attempted"] is not validation
   1780         or ui["validation_rejected"] is not validation
   1781         or ui["retry_attempts"] != int(retry)
   1782         or ui["relaunches"] != int(retry)
   1783         or ui["retention_verified"] is not True
   1784         or ui["today_projection_verified"] is not True
   1785     ):
   1786         raise ValueError("persona attempt UI observation is invalid")
   1787     network = attempt["network_observation"]
   1788     if network == {"state": "pending_step_258"}:
   1789         if require_measured_network:
   1790             raise ValueError("persona attempt network evidence is not measured")
   1791     else:
   1792         network = exact_keys(
   1793             network,
   1794             {
   1795                 "state",
   1796                 "accepted_connections",
   1797                 "rejected_connections",
   1798                 "non_loopback_attempts",
   1799                 "subscriptions",
   1800                 "accepted_events",
   1801                 "accepted_uploads",
   1802                 "retrievals",
   1803                 "unintended_publications",
   1804                 "events_accepted_during_expected_failure",
   1805                 "final_candidate_data_loss",
   1806             },
   1807             "attempt network observation",
   1808         )
   1809         if network["state"] != "measured" or any(
   1810             type(network[key]) is not int or not 0 <= network[key] <= 4096
   1811             for key in network
   1812             if key != "state"
   1813         ):
   1814             raise ValueError("persona attempt network observation is invalid")
   1815         expected_media = int(attempt["flow"] == "PhotoUpdate")
   1816         if (
   1817             network["accepted_events"] != 1
   1818             or network["accepted_uploads"] != expected_media
   1819             or network["retrievals"] != expected_media
   1820             or network["non_loopback_attempts"] != 0
   1821             or network["unintended_publications"] != 0
   1822             or network["events_accepted_during_expected_failure"] != 0
   1823             or network["final_candidate_data_loss"] != 0
   1824         ):
   1825             raise ValueError("persona attempt measured result is invalid")
   1826     accessibility = exact_keys(
   1827         attempt["accessibility"],
   1828         {
   1829             "locale",
   1830             "content_size",
   1831             "reduce_motion",
   1832             "progressive_disclosure",
   1833             "labels_values_traits",
   1834             "keyboard_focus",
   1835             "visible_actions",
   1836             "voiceover_user_observed",
   1837         },
   1838         "attempt accessibility observation",
   1839     )
   1840     if (
   1841         accessibility["locale"] != "en_US"
   1842         or accessibility["content_size"] != "accessibility-extra-extra-extra-large"
   1843         or accessibility["reduce_motion"] is not True
   1844         or any(
   1845             type(accessibility[key]) is not bool
   1846             for key in (
   1847                 "progressive_disclosure",
   1848                 "labels_values_traits",
   1849                 "keyboard_focus",
   1850                 "visible_actions",
   1851                 "voiceover_user_observed",
   1852             )
   1853         )
   1854         or accessibility["visible_actions"] is not True
   1855         or accessibility["voiceover_user_observed"] is not False
   1856         or accessibility["progressive_disclosure"]
   1857         is not (
   1858             expected_persona["interaction_profile"] == "novice_progressive_disclosure"
   1859         )
   1860         or accessibility["keyboard_focus"]
   1861         is not (
   1862             expected_persona["interaction_profile"] == "novice_accessibility_keyboard"
   1863         )
   1864     ):
   1865         raise ValueError("persona attempt accessibility evidence is invalid")
   1866     artifacts = attempt["artifact_digests"]
   1867     if not isinstance(artifacts, list) or len(artifacts) > 4:
   1868         raise ValueError("persona attempt artifact inventory is invalid")
   1869     roles: set[str] = set()
   1870     for artifact in artifacts:
   1871         artifact = exact_keys(artifact, {"role", "sha256"}, "attempt artifact")
   1872         if (
   1873             artifact["role"]
   1874             not in {"media_input", "uploaded_blob", "retrieved_blob", "ui_snapshot"}
   1875             or artifact["role"] in roles
   1876             or not lowercase_hex(artifact["sha256"], 64)
   1877         ):
   1878             raise ValueError("persona attempt artifact is invalid")
   1879         roles.add(artifact["role"])
   1880     return attempt
   1881 
   1882 
   1883 def exact_persona_test_node(value: Any) -> dict[str, Any]:
   1884     if not isinstance(value, dict) or set(value) != {
   1885         "devices",
   1886         "testNodes",
   1887         "testPlanConfigurations",
   1888     }:
   1889         raise ValueError("xcresult test inventory is invalid")
   1890     matches: list[dict[str, Any]] = []
   1891 
   1892     def visit(node: Any) -> None:
   1893         if not isinstance(node, dict):
   1894             raise ValueError("xcresult test node is invalid")
   1895         if node.get("nodeType") == "Test Case" and (
   1896             node.get("nodeIdentifier") == PERSONA_XCRESULT_NODE_IDENTIFIER
   1897             or node.get("nodeIdentifierURL") == PERSONA_XCRESULT_NODE_URL
   1898         ):
   1899             matches.append(node)
   1900         children = node.get("children", [])
   1901         if not isinstance(children, list):
   1902             raise ValueError("xcresult child inventory is invalid")
   1903         for child in children:
   1904             visit(child)
   1905 
   1906     nodes = value["testNodes"]
   1907     if not isinstance(nodes, list):
   1908         raise ValueError("xcresult test inventory is invalid")
   1909     for node in nodes:
   1910         visit(node)
   1911     if len(matches) != 1:
   1912         raise ValueError("xcresult exact persona test is unavailable")
   1913     match = matches[0]
   1914     if (
   1915         match.get("nodeIdentifier") != PERSONA_XCRESULT_NODE_IDENTIFIER
   1916         or match.get("nodeIdentifierURL") != PERSONA_XCRESULT_NODE_URL
   1917         or match.get("name") != "testLocalSocialDeterministicPersonas()"
   1918         or match.get("nodeType") != "Test Case"
   1919         or match.get("result") != "Passed"
   1920     ):
   1921         raise ValueError("xcresult exact persona test did not pass")
   1922     return match
   1923 
   1924 
   1925 def run_json_command_bounded(command: list[str], maximum: int, *, timeout=None) -> Any:
   1926     raw = run_selector_command(command, maximum_stdout=maximum, timeout=timeout).stdout
   1927     if not raw:
   1928         raise ValueError("command JSON output exceeds its byte bound")
   1929     return json.loads(raw, object_pairs_hook=strict_object)
   1930 
   1931 
   1932 def exported_attachment_inventory(path: Path) -> set[str]:
   1933     inventory = bounded_directory_inventory(
   1934         path,
   1935         len(PERSONA_ATTACHMENT_NAMES) + 1,
   1936         255,
   1937     )
   1938     if any(kind != 1 or b"/" in relative for kind, relative, _ in inventory):
   1939         raise ValueError("xcresult attachment export inventory is invalid")
   1940     return {relative.decode("utf-8") for _, relative, _ in inventory}
   1941 
   1942 
   1943 def xcresult_attachment_attempt_id(name: object) -> str | None:
   1944     if not isinstance(name, str):
   1945         return None
   1946     matched = PERSONA_XCRESULT_ATTACHMENT_NAME.fullmatch(name)
   1947     return matched.group(1) if matched is not None else None
   1948 
   1949 
   1950 def load_exported_persona_attachments(
   1951     export_directory: Path,
   1952     suite: dict[str, Any],
   1953     *,
   1954     require_measured_network: bool,
   1955 ) -> list[tuple[bytes, dict[str, Any]]]:
   1956     inventory = exported_attachment_inventory(export_directory)
   1957     manifest_raw, manifest_value = read_json_bounded(
   1958         export_directory / "manifest.json", MAX_XCRESULT_JSON_BYTES
   1959     )
   1960     del manifest_raw
   1961     if not isinstance(manifest_value, list) or len(manifest_value) != 1:
   1962         raise ValueError("xcresult attachment manifest is invalid")
   1963     group = exact_keys(
   1964         manifest_value[0],
   1965         {"testIdentifier", "testIdentifierURL", "attachments"},
   1966         "xcresult attachment group",
   1967     )
   1968     if (
   1969         group["testIdentifier"] != PERSONA_XCRESULT_NODE_IDENTIFIER
   1970         or group["testIdentifierURL"] != PERSONA_XCRESULT_NODE_URL
   1971         or not isinstance(group["attachments"], list)
   1972         or len(group["attachments"]) != 15
   1973     ):
   1974         raise ValueError("xcresult attachment test binding is invalid")
   1975     attachments_by_name: dict[str, tuple[bytes, dict[str, Any]]] = {}
   1976     exported_names: set[str] = set()
   1977     total_bytes = 0
   1978     allowed_manifest_keys = {
   1979         "exportedFileName",
   1980         "suggestedHumanReadableName",
   1981         "isAssociatedWithFailure",
   1982         "configurationName",
   1983         "deviceName",
   1984         "deviceId",
   1985         "timestamp",
   1986         "repetitionNumber",
   1987         "arguments",
   1988     }
   1989     for row_value in group["attachments"]:
   1990         if (
   1991             not isinstance(row_value, dict)
   1992             or not {
   1993                 "exportedFileName",
   1994                 "suggestedHumanReadableName",
   1995                 "isAssociatedWithFailure",
   1996                 "configurationName",
   1997                 "deviceName",
   1998                 "deviceId",
   1999             }.issubset(row_value)
   2000             or not set(row_value).issubset(allowed_manifest_keys)
   2001         ):
   2002             raise ValueError("xcresult attachment row is invalid")
   2003         exported = row_value["exportedFileName"]
   2004         name = row_value["suggestedHumanReadableName"]
   2005         attempt_id = xcresult_attachment_attempt_id(name)
   2006         if attempt_id is None:
   2007             raise ValueError("xcresult attempt attachment identity is invalid")
   2008         canonical_name = f"radroots-local-social-{attempt_id}.json"
   2009         if (
   2010             canonical_name not in PERSONA_ATTACHMENT_NAMES
   2011             or canonical_name in attachments_by_name
   2012             or not isinstance(exported, str)
   2013             or exported in exported_names
   2014             or not 1 <= len(exported.encode("utf-8")) <= 255
   2015             or Path(exported).name != exported
   2016             or row_value["isAssociatedWithFailure"] is not False
   2017             or row_value["configurationName"] != "Test Scheme Action"
   2018             or not isinstance(row_value["deviceName"], str)
   2019             or not row_value["deviceName"]
   2020             or not isinstance(row_value["deviceId"], str)
   2021         ):
   2022             raise ValueError("xcresult attempt attachment identity is invalid")
   2023         exported_names.add(exported)
   2024         path = export_directory / exported
   2025         if path.is_symlink() or not path.is_file():
   2026             raise ValueError("xcresult attempt attachment is not a regular file")
   2027         raw, value = read_json_bounded(path, MAX_PERSONA_ATTACHMENT_BYTES)
   2028         if raw != canonical_evidence_bytes(value):
   2029             raise ValueError("persona attempt attachment is noncanonical")
   2030         attempt = validate_persona_attempt_evidence(
   2031             value, suite, require_measured_network=require_measured_network
   2032         )
   2033         if attempt_id != attempt["attempt_id"]:
   2034             raise ValueError("xcresult attachment name does not bind its attempt")
   2035         total_bytes += len(raw)
   2036         if total_bytes > MAX_PERSONA_ATTACHMENTS_BYTES:
   2037             raise ValueError(
   2038                 "xcresult attempt attachments exceed their aggregate bound"
   2039             )
   2040         attachments_by_name[canonical_name] = (raw, attempt)
   2041     if tuple(sorted(attachments_by_name)) != tuple(sorted(PERSONA_ATTACHMENT_NAMES)):
   2042         raise ValueError("xcresult persona attachment inventory is incomplete")
   2043     if inventory != {"manifest.json", *exported_names}:
   2044         raise ValueError("xcresult attachment export inventory is invalid")
   2045     return [attachments_by_name[name] for name in PERSONA_ATTACHMENT_NAMES]
   2046 
   2047 
   2048 def extract_persona_attempt_attachments(
   2049     result_bundle: Path,
   2050     suite: dict[str, Any],
   2051     *,
   2052     require_measured_network: bool,
   2053 ) -> list[tuple[bytes, dict[str, Any]]]:
   2054     spec = importlib.util.spec_from_file_location(
   2055         "tera_persona_attachment_selection",
   2056         Path(__file__).with_name("persona_attachment_selection.py"),
   2057     )
   2058     assert spec is not None and spec.loader is not None
   2059     module = importlib.util.module_from_spec(spec)
   2060     spec.loader.exec_module(module)
   2061     tools = module.ExtractionTools(
   2062         run_json_command_bounded,
   2063         exact_persona_test_node,
   2064         read_json_bounded,
   2065         bounded_directory_inventory,
   2066         directory_digest,
   2067         load_exported_persona_attachments,
   2068         xcresult_attachment_attempt_id,
   2069         PERSONA_XCRESULT_NODE_IDENTIFIER,
   2070         PERSONA_XCRESULT_NODE_URL,
   2071         PERSONA_ATTACHMENT_NAMES,
   2072         MAX_XCRESULT_JSON_BYTES,
   2073         MAX_PERSONA_ATTACHMENT_BYTES,
   2074         run_selector_command,
   2075     )
   2076     return module.extract(
   2077         result_bundle,
   2078         suite,
   2079         tools,
   2080         require_measured_network=require_measured_network,
   2081     )
   2082 
   2083 
   2084 def reconstruct_persona_result_v2(
   2085     suite: dict[str, Any],
   2086     attachments: list[tuple[bytes, dict[str, Any]]],
   2087     *,
   2088     fixture_sha256: str,
   2089     fixture_schema_sha256: str,
   2090     attempt_schema_sha256: str,
   2091     result_schema_sha256: str,
   2092     result_bundle_sha256: str,
   2093     forward_repairs: list[str],
   2094 ) -> dict[str, Any]:
   2095     if len(attachments) != 15:
   2096         raise ValueError("persona result requires exactly 15 attempt attachments")
   2097     attempts = [
   2098         validate_persona_attempt_evidence(value, suite, require_measured_network=True)
   2099         for _, value in attachments
   2100     ]
   2101     expected_ids = [
   2102         candidate["id"]
   2103         for persona in suite["personas"]
   2104         for candidate in persona["attempts"]
   2105     ]
   2106     if [attempt["attempt_id"] for attempt in attempts] != expected_ids:
   2107         raise ValueError("persona result attempt inventory is not exact")
   2108     first = attempts[0]
   2109     shared_fields = (
   2110         "test_invocation",
   2111         "source",
   2112         "app_build_sha256",
   2113         "simulator",
   2114         "run_id",
   2115         "endpoint_policy_sha256",
   2116     )
   2117     if any(
   2118         attempt[field] != first[field]
   2119         for attempt in attempts[1:]
   2120         for field in shared_fields
   2121     ):
   2122         raise ValueError("persona attempt evidence spans multiple run identities")
   2123     for digest in (
   2124         fixture_sha256,
   2125         fixture_schema_sha256,
   2126         attempt_schema_sha256,
   2127         result_schema_sha256,
   2128         result_bundle_sha256,
   2129     ):
   2130         if not lowercase_hex(digest, 64):
   2131             raise ValueError("persona result digest identity is invalid")
   2132     if (
   2133         not isinstance(forward_repairs, list)
   2134         or len(forward_repairs) > 16
   2135         or len(set(forward_repairs)) != len(forward_repairs)
   2136         or any(not lowercase_hex(revision, 40) for revision in forward_repairs)
   2137     ):
   2138         raise ValueError("persona result forward-repair inventory is invalid")
   2139     identity_by_persona: dict[str, str] = {}
   2140     persona_rows = []
   2141     for persona in suite["personas"]:
   2142         alias = persona["alias"]
   2143         rows = [attempt for attempt in attempts if attempt["persona_alias"] == alias]
   2144         identities = {row["public_identity_sha256"] for row in rows}
   2145         if len(rows) != 3 or len(identities) != 1:
   2146             raise ValueError("persona result identity reuse is invalid")
   2147         identity = identities.pop()
   2148         identity_by_persona[alias] = identity
   2149         subscriptions = sum(row["network_observation"]["subscriptions"] for row in rows)
   2150         if subscriptions < 1:
   2151             raise ValueError("persona result is missing a subscription")
   2152         persona_rows.append(
   2153             {
   2154                 "alias": alias,
   2155                 "public_identity_sha256": identity,
   2156                 "subscriptions": subscriptions,
   2157                 "attempt_ids": [row["attempt_id"] for row in rows],
   2158             }
   2159         )
   2160     if len(set(identity_by_persona.values())) != 5:
   2161         raise ValueError("persona result identities are not distinct")
   2162     network_rows = [attempt["network_observation"] for attempt in attempts]
   2163     accepted_events = sum(row["accepted_events"] for row in network_rows)
   2164     flow_counts = {
   2165         flow: sum(attempt["flow"] == flow for attempt in attempts)
   2166         for flow in FLOW_KINDS
   2167     }
   2168     event_kind_counts = {
   2169         str(kind): sum(
   2170             FLOW_KINDS[attempt["flow"]] == kind
   2171             and attempt["network_observation"]["accepted_events"] == 1
   2172             for attempt in attempts
   2173         )
   2174         for kind in (1, 31923, 30402)
   2175     }
   2176     result = {
   2177         "schema": PERSONA_RESULT_V2_SCHEMA,
   2178         "schema_version": 2,
   2179         "run_id": first["run_id"],
   2180         "test_invocation": first["test_invocation"],
   2181         "source": first["source"],
   2182         "app_build_sha256": first["app_build_sha256"],
   2183         "endpoint_policy_sha256": first["endpoint_policy_sha256"],
   2184         "fixture_sha256": fixture_sha256,
   2185         "fixture_schema_sha256": fixture_schema_sha256,
   2186         "attempt_schema_sha256": attempt_schema_sha256,
   2187         "result_schema_sha256": result_schema_sha256,
   2188         "simulator": first["simulator"],
   2189         "result_bundle_sha256": result_bundle_sha256,
   2190         "attachments": [
   2191             {
   2192                 "attempt_id": attempt["attempt_id"],
   2193                 "sha256": hashlib.sha256(raw).hexdigest(),
   2194             }
   2195             for raw, attempt in attachments
   2196         ],
   2197         "personas": persona_rows,
   2198         "flow_counts": flow_counts,
   2199         "accepted_events": accepted_events,
   2200         "event_kind_counts": event_kind_counts,
   2201         "accepted_uploads": sum(row["accepted_uploads"] for row in network_rows),
   2202         "retrievals": sum(row["retrievals"] for row in network_rows),
   2203         "distinct_identities": len(set(identity_by_persona.values())),
   2204         "unknown_attempts": len(
   2205             set(expected_ids) - {row["attempt_id"] for row in attempts}
   2206         ),
   2207         "duplicate_attempts": len(attempts)
   2208         - len({row["attempt_id"] for row in attempts}),
   2209         "expected_failure_rejections": sum(
   2210             attempt["ui_observation"]["validation_rejected"]
   2211             or attempt["ui_observation"]["retry_attempts"] > 0
   2212             for attempt in attempts
   2213         ),
   2214         "events_accepted_during_expected_failures": sum(
   2215             row["events_accepted_during_expected_failure"] for row in network_rows
   2216         ),
   2217         "accepted_connections": sum(
   2218             row["accepted_connections"] for row in network_rows
   2219         ),
   2220         "rejected_connections": sum(
   2221             row["rejected_connections"] for row in network_rows
   2222         ),
   2223         "non_loopback_attempts": sum(
   2224             row["non_loopback_attempts"] for row in network_rows
   2225         ),
   2226         "unintended_publications": sum(
   2227             row["unintended_publications"] for row in network_rows
   2228         ),
   2229         "final_candidate_data_loss": sum(
   2230             row["final_candidate_data_loss"] for row in network_rows
   2231         ),
   2232         "accessibility": {
   2233             "locale": "en_US",
   2234             "content_size": "accessibility-extra-extra-extra-large",
   2235             "reduce_motion": True,
   2236             "semantic_attempts": sum(
   2237                 attempt["accessibility"]["labels_values_traits"] for attempt in attempts
   2238             ),
   2239             "keyboard_focus_attempts": sum(
   2240                 attempt["accessibility"]["keyboard_focus"] for attempt in attempts
   2241             ),
   2242             "voiceover_user_observed": False,
   2243         },
   2244         "forward_repairs": forward_repairs,
   2245         "complete_matrix_rerun": True,
   2246     }
   2247     if (
   2248         result["flow_counts"] != {flow: 3 for flow in FLOW_KINDS}
   2249         or result["accepted_events"] != 15
   2250         or result["event_kind_counts"] != {"1": 9, "31923": 3, "30402": 3}
   2251         or result["accepted_uploads"] != 3
   2252         or result["retrievals"] != 3
   2253         or result["distinct_identities"] != 5
   2254         or result["unknown_attempts"] != 0
   2255         or result["duplicate_attempts"] != 0
   2256         or result["expected_failure_rejections"] != 2
   2257         or result["events_accepted_during_expected_failures"] != 0
   2258         or result["accepted_connections"] < 1
   2259         or result["non_loopback_attempts"] != 0
   2260         or result["unintended_publications"] != 0
   2261         or result["final_candidate_data_loss"] != 0
   2262         or result["accessibility"]["semantic_attempts"] < 3
   2263         or result["accessibility"]["keyboard_focus_attempts"] != 3
   2264     ):
   2265         raise ValueError("persona result reconstructed outcome is invalid")
   2266     return result
   2267 
   2268 
   2269 def validate_persona_result(
   2270     value: Any,
   2271     suite: dict[str, Any],
   2272     fixture_sha256: str,
   2273     fixture_schema_sha256: str,
   2274     result_schema_sha256: str,
   2275 ) -> dict[str, Any]:
   2276     keys = {
   2277         "schema",
   2278         "schema_version",
   2279         "run_id",
   2280         "source_commit",
   2281         "source_tree",
   2282         "fixture_sha256",
   2283         "fixture_schema_sha256",
   2284         "result_schema_sha256",
   2285         "simulator",
   2286         "result_bundle_sha256",
   2287         "evidence_sha256",
   2288         "personas",
   2289         "flow_counts",
   2290         "accepted_events",
   2291         "event_kind_counts",
   2292         "accepted_uploads",
   2293         "retrievals",
   2294         "distinct_identities",
   2295         "unknown_attempts",
   2296         "duplicate_attempts",
   2297         "expected_failure_rejections",
   2298         "events_accepted_during_expected_failures",
   2299         "production_network_contacts",
   2300         "unintended_publications",
   2301         "final_candidate_data_loss",
   2302         "accessibility",
   2303         "forward_repairs",
   2304         "complete_matrix_rerun",
   2305     }
   2306     result = exact_keys(value, keys, "persona result")
   2307     if (
   2308         result["schema"] != "tera.ios.local-social.persona-results.v1"
   2309         or result["schema_version"] != 1
   2310         or not re.fullmatch(r"[a-z0-9][a-z0-9-]{6,62}[a-z0-9]", result["run_id"])
   2311         or not lowercase_hex(result["source_commit"], 40)
   2312         or not lowercase_hex(result["source_tree"], 40)
   2313     ):
   2314         raise ValueError("persona result header is invalid")
   2315     expected_digests = {
   2316         "fixture_sha256": fixture_sha256,
   2317         "fixture_schema_sha256": fixture_schema_sha256,
   2318         "result_schema_sha256": result_schema_sha256,
   2319     }
   2320     if any(result[key] != digest for key, digest in expected_digests.items()):
   2321         raise ValueError("persona result contract digest is invalid")
   2322     if not lowercase_hex(result["result_bundle_sha256"], 64) or not lowercase_hex(
   2323         result["evidence_sha256"], 64
   2324     ):
   2325         raise ValueError("persona result evidence digest is invalid")
   2326     simulator = exact_keys(
   2327         result["simulator"], {"udid", "os", "architecture"}, "simulator"
   2328     )
   2329     if (
   2330         not isinstance(simulator["udid"], str)
   2331         or re.fullmatch(r"[A-F0-9-]{36}", simulator["udid"]) is None
   2332         or not valid_ios_runtime(simulator["os"])
   2333         or simulator["architecture"] != "arm64"
   2334     ):
   2335         raise ValueError("persona result simulator is invalid")
   2336     accessibility = exact_keys(
   2337         result["accessibility"],
   2338         {
   2339             "locale",
   2340             "content_size",
   2341             "reduce_motion",
   2342             "semantic_audit",
   2343             "voiceover_user_observed",
   2344         },
   2345         "accessibility result",
   2346     )
   2347     if accessibility != {
   2348         "locale": "en_US",
   2349         "content_size": "accessibility-extra-extra-extra-large",
   2350         "reduce_motion": True,
   2351         "semantic_audit": "passed",
   2352         "voiceover_user_observed": False,
   2353     }:
   2354         raise ValueError("persona accessibility result is invalid")
   2355     repairs = result["forward_repairs"]
   2356     if (
   2357         not isinstance(repairs, list)
   2358         or len(repairs) > 16
   2359         or len(set(repairs)) != len(repairs)
   2360         or any(not lowercase_hex(commit, 40) for commit in repairs)
   2361         or result["complete_matrix_rerun"] is not True
   2362     ):
   2363         raise ValueError("persona rerun evidence is invalid")
   2364     expected_scalars = {
   2365         "accepted_events": 15,
   2366         "accepted_uploads": 3,
   2367         "retrievals": 3,
   2368         "distinct_identities": 5,
   2369         "unknown_attempts": 0,
   2370         "duplicate_attempts": 0,
   2371         "expected_failure_rejections": 1,
   2372         "events_accepted_during_expected_failures": 0,
   2373         "production_network_contacts": 0,
   2374         "unintended_publications": 0,
   2375         "final_candidate_data_loss": 0,
   2376     }
   2377     if any(result.get(key) != expected for key, expected in expected_scalars.items()):
   2378         raise ValueError("persona result totals are invalid")
   2379     if result["flow_counts"] != {flow: 3 for flow in FLOW_KINDS} or result[
   2380         "event_kind_counts"
   2381     ] != {"1": 9, "31923": 3, "30402": 3}:
   2382         raise ValueError("persona result event inventory is invalid")
   2383     if not isinstance(result["personas"], list) or len(result["personas"]) != 5:
   2384         raise ValueError("persona result inventory is invalid")
   2385     identities = set()
   2386     for persona, expected in zip(result["personas"], suite["personas"], strict=True):
   2387         exact_keys(
   2388             persona,
   2389             {"alias", "identity_sha256", "subscriptions", "attempts"},
   2390             "persona result row",
   2391         )
   2392         if (
   2393             persona["alias"] != expected["alias"]
   2394             or not lowercase_hex(persona["identity_sha256"], 64)
   2395             or persona["identity_sha256"] == "0" * 64
   2396             or type(persona["subscriptions"]) is not int
   2397             or not 1 <= persona["subscriptions"] <= 4096
   2398             or not isinstance(persona["attempts"], list)
   2399             or len(persona["attempts"]) != 3
   2400         ):
   2401             raise ValueError("persona result row is invalid")
   2402         identities.add(persona["identity_sha256"])
   2403         for attempt, expected_attempt in zip(
   2404             persona["attempts"], expected["attempts"], strict=True
   2405         ):
   2406             exact_keys(
   2407                 attempt,
   2408                 {
   2409                     "id",
   2410                     "flow",
   2411                     "event_kind",
   2412                     "accepted",
   2413                     "expected_failure_rejections",
   2414                 },
   2415                 "attempt result row",
   2416             )
   2417             if (
   2418                 attempt["id"] != expected_attempt["id"]
   2419                 or attempt["flow"] != expected_attempt["flow"]
   2420                 or attempt["event_kind"] != FLOW_KINDS[expected_attempt["flow"]]
   2421                 or attempt["accepted"] is not True
   2422                 or attempt["expected_failure_rejections"]
   2423                 != int(
   2424                     expected_attempt["expected_failure"] == "transport_retry_relaunch"
   2425                 )
   2426             ):
   2427                 raise ValueError("attempt result row is invalid")
   2428     if len(identities) != 5:
   2429         raise ValueError("persona result identities are not distinct")
   2430     return result
   2431 
   2432 
   2433 def verify_persona(arguments: argparse.Namespace) -> int:
   2434     fixture_raw, suite = load_persona_suite(Path(arguments.fixture).resolve())
   2435     fixture_schema_raw, fixture_schema = load_schema_file(
   2436         Path(arguments.fixture_schema).resolve(),
   2437         "https://radroots.org/schemas/ios/local-social-personas.v1.schema.json",
   2438     )
   2439     attempt_schema_raw, attempt_schema = load_schema_file(
   2440         Path(arguments.attempt_schema).resolve(),
   2441         "https://radroots.org/schemas/ios/local-social-persona-attempt-evidence.v1.schema.json",
   2442     )
   2443     result_schema_raw, result_schema = load_schema_file(
   2444         Path(arguments.result_v2_schema).resolve(),
   2445         "https://radroots.org/schemas/ios/local-social-persona-results.v2.schema.json",
   2446     )
   2447     validate_schema_instance(fixture_schema, suite, "persona fixture")
   2448     evidence_path = Path(arguments.evidence).resolve()
   2449     _, evidence_value = read_json(evidence_path)
   2450     evidence = validate_persona_evidence(evidence_value, suite)
   2451     result_bundle = Path(arguments.result_bundle).resolve()
   2452     if not result_bundle.is_dir():
   2453         raise ValueError("XCUITest result bundle is unavailable")
   2454     if not lowercase_hex(arguments.source_commit, 40) or not lowercase_hex(
   2455         arguments.source_tree, 40
   2456     ):
   2457         raise ValueError("source identity is invalid")
   2458     attachments = extract_persona_attempt_attachments(
   2459         result_bundle, suite, require_measured_network=True
   2460     )
   2461     for _, attempt in attachments:
   2462         validate_schema_instance(attempt_schema, attempt, "persona attempt evidence")
   2463     simulator = simulator_metadata(arguments.simulator_id, result_bundle)
   2464     result = reconstruct_persona_result_v2(
   2465         suite,
   2466         attachments,
   2467         fixture_sha256=hashlib.sha256(fixture_raw).hexdigest(),
   2468         fixture_schema_sha256=hashlib.sha256(fixture_schema_raw).hexdigest(),
   2469         attempt_schema_sha256=hashlib.sha256(attempt_schema_raw).hexdigest(),
   2470         result_schema_sha256=hashlib.sha256(result_schema_raw).hexdigest(),
   2471         result_bundle_sha256=directory_digest(result_bundle),
   2472         forward_repairs=arguments.forward_repair_commit,
   2473     )
   2474     validate_schema_instance(result_schema, result, "persona v2 result")
   2475     if (
   2476         result["run_id"] != arguments.run_id
   2477         or result["source"]
   2478         != {"commit": arguments.source_commit, "tree": arguments.source_tree}
   2479         or result["simulator"] != simulator
   2480         or result["accepted_events"] != evidence["accepted_events"]
   2481         or result["accepted_uploads"] != evidence["accepted_uploads"]
   2482         or result["retrievals"] != evidence["retrievals"]
   2483         or result["non_loopback_attempts"] != evidence["non_loopback_attempts"]
   2484         or result["unintended_publications"] != evidence["unintended_publications"]
   2485         or result["final_candidate_data_loss"] != evidence["final_candidate_data_loss"]
   2486         or result["accepted_connections"] > evidence["accepted_connections"]
   2487         or result["rejected_connections"] > evidence["rejected_connections"]
   2488     ):
   2489         raise ValueError("persona aggregate does not match fixture evidence")
   2490     output = Path(arguments.output).resolve()
   2491     output.write_text(json.dumps(result, indent=2) + "\n", encoding="utf-8")
   2492     raw, reloaded = read_json(output)
   2493     if (
   2494         raw != (json.dumps(reloaded, indent=2) + "\n").encode("utf-8")
   2495         or reloaded != result
   2496     ):
   2497         raise ValueError("persona v2 result is noncanonical")
   2498     print(
   2499         "local-social measured persona result verified: "
   2500         f"{hashlib.sha256(raw).hexdigest()}"
   2501     )
   2502     return 0
   2503 
   2504 
   2505 def verify_persona_result_file(
   2506     path: Path,
   2507     suite: dict[str, Any],
   2508     fixture_sha256: str,
   2509     fixture_schema_sha256: str,
   2510     result_schema_sha256: str,
   2511 ) -> dict[str, Any]:
   2512     raw, value = read_json(path)
   2513     canonical = (json.dumps(value, indent=2) + "\n").encode("utf-8")
   2514     if raw != canonical:
   2515         raise ValueError("persona result is noncanonical")
   2516     return validate_persona_result(
   2517         value,
   2518         suite,
   2519         fixture_sha256,
   2520         fixture_schema_sha256,
   2521         result_schema_sha256,
   2522     )
   2523 
   2524 
   2525 def verify_persona_result(arguments: argparse.Namespace) -> int:
   2526     fixture_raw, suite = load_persona_suite(Path(arguments.fixture).resolve())
   2527     fixture_schema_raw, fixture_schema = load_schema_file(
   2528         Path(arguments.fixture_schema).resolve(),
   2529         "https://radroots.org/schemas/ios/local-social-personas.v1.schema.json",
   2530     )
   2531     result_schema_raw, result_schema = load_schema_file(
   2532         Path(arguments.result_schema).resolve(),
   2533         "https://radroots.org/schemas/ios/local-social-persona-results.v1.schema.json",
   2534     )
   2535     validate_schema_instance(fixture_schema, suite, "persona fixture")
   2536     result = verify_persona_result_file(
   2537         Path(arguments.result).resolve(),
   2538         suite,
   2539         hashlib.sha256(fixture_raw).hexdigest(),
   2540         hashlib.sha256(fixture_schema_raw).hexdigest(),
   2541         hashlib.sha256(result_schema_raw).hexdigest(),
   2542     )
   2543     validate_schema_instance(result_schema, result, "persona v1 result")
   2544     print("local-social persona result contract verified")
   2545     return 0
   2546 
   2547 
   2548 def parser() -> argparse.ArgumentParser:
   2549     root = argparse.ArgumentParser()
   2550     commands = root.add_subparsers(dest="command", required=True)
   2551     serve_command = commands.add_parser("serve")
   2552     serve_command.add_argument("--relay-port", type=int, required=True)
   2553     serve_command.add_argument("--blossom-port", type=int, required=True)
   2554     serve_command.add_argument("--evidence", required=True)
   2555     serve_command.add_argument("--ready", required=True)
   2556     serve_command.add_argument("--control", required=True)
   2557     serve_command.add_argument("--persona-fixture")
   2558     verify_command = commands.add_parser("verify")
   2559     verify_command.add_argument("--evidence", required=True)
   2560     accessibility_command = commands.add_parser("verify-accessibility")
   2561     accessibility_command.add_argument("--evidence", required=True)
   2562     fixture_command = commands.add_parser("verify-persona-fixture")
   2563     fixture_command.add_argument("--fixture", required=True)
   2564     fixture_command.add_argument("--fixture-schema", required=True)
   2565     fixture_command.add_argument("--result-schema", required=True)
   2566     fixture_command.add_argument("--attempt-schema", required=True)
   2567     fixture_command.add_argument("--result-v2-schema", required=True)
   2568     bud11_command = commands.add_parser("verify-bud11-corpus")
   2569     bud11_command.add_argument("--corpus", required=True)
   2570     bud11_command.add_argument("--schema", required=True)
   2571     persona_command = commands.add_parser("verify-persona")
   2572     persona_command.add_argument("--fixture", required=True)
   2573     persona_command.add_argument("--fixture-schema", required=True)
   2574     persona_command.add_argument("--result-schema", required=True)
   2575     persona_command.add_argument("--attempt-schema", required=True)
   2576     persona_command.add_argument("--result-v2-schema", required=True)
   2577     persona_command.add_argument("--evidence", required=True)
   2578     persona_command.add_argument("--result-bundle", required=True)
   2579     persona_command.add_argument("--output", required=True)
   2580     persona_command.add_argument("--source-commit", required=True)
   2581     persona_command.add_argument("--source-tree", required=True)
   2582     persona_command.add_argument("--run-id", required=True)
   2583     persona_command.add_argument("--simulator-id", required=True)
   2584     persona_command.add_argument("--forward-repair-commit", action="append", default=[])
   2585     result_command = commands.add_parser("verify-persona-result")
   2586     result_command.add_argument("--fixture", required=True)
   2587     result_command.add_argument("--fixture-schema", required=True)
   2588     result_command.add_argument("--result-schema", required=True)
   2589     result_command.add_argument("--result", required=True)
   2590     return root
   2591 
   2592 
   2593 def main() -> int:
   2594     verify_toolchain_identity()
   2595     arguments = parser().parse_args()
   2596     if arguments.command == "serve":
   2597         return serve(arguments)
   2598     if arguments.command == "verify":
   2599         return verify(arguments)
   2600     if arguments.command == "verify-accessibility":
   2601         return verify_accessibility(arguments)
   2602     if arguments.command == "verify-persona-fixture":
   2603         return verify_persona_fixture(arguments)
   2604     if arguments.command == "verify-bud11-corpus":
   2605         return verify_bud11_corpus(arguments)
   2606     if arguments.command == "verify-persona-result":
   2607         return verify_persona_result(arguments)
   2608     return verify_persona(arguments)
   2609 
   2610 
   2611 if __name__ == "__main__":
   2612     raise SystemExit(main())