test_local_social_fixture.py (50382B)
1 import base64 2 import copy 3 import hashlib 4 import http.client 5 import importlib.util 6 import json 7 import re 8 import secrets 9 import tempfile 10 import time 11 import unittest 12 from pathlib import Path 13 from unittest import mock 14 15 from scripts.local_social_test_support import blossom_server 16 17 18 SCRIPT = Path(__file__).with_name("local-social-fixture.py") 19 SPEC = importlib.util.spec_from_file_location("local_social_fixture", SCRIPT) 20 assert SPEC is not None and SPEC.loader is not None 21 fixture = importlib.util.module_from_spec(SPEC) 22 SPEC.loader.exec_module(fixture) 23 24 25 def sign_bud11_event(event: dict) -> dict: 26 secret = secrets.randbelow(fixture.SECP256K1_ORDER - 1) + 1 27 public_point = fixture.point_multiply(secret, fixture.SECP256K1_GENERATOR) 28 assert public_point is not None 29 if public_point[1] & 1: 30 secret = fixture.SECP256K1_ORDER - secret 31 public_key = public_point[0].to_bytes(32, "big") 32 signed = copy.deepcopy(event) 33 signed["pubkey"] = public_key.hex() 34 preimage = json.dumps( 35 [ 36 0, 37 signed["pubkey"], 38 signed["created_at"], 39 signed["kind"], 40 signed["tags"], 41 signed["content"], 42 ], 43 ensure_ascii=False, 44 separators=(",", ":"), 45 ).encode("utf-8") 46 message = hashlib.sha256(preimage).digest() 47 signed["id"] = message.hex() 48 auxiliary = secrets.token_bytes(32) 49 masked_secret = bytes( 50 left ^ right 51 for left, right in zip( 52 secret.to_bytes(32, "big"), 53 fixture.tagged_hash("BIP0340/aux", auxiliary), 54 strict=True, 55 ) 56 ) 57 nonce = ( 58 int.from_bytes( 59 fixture.tagged_hash("BIP0340/nonce", masked_secret + public_key + message), 60 "big", 61 ) 62 % fixture.SECP256K1_ORDER 63 ) 64 assert nonce != 0 65 nonce_point = fixture.point_multiply(nonce, fixture.SECP256K1_GENERATOR) 66 assert nonce_point is not None 67 if nonce_point[1] & 1: 68 nonce = fixture.SECP256K1_ORDER - nonce 69 challenge = ( 70 int.from_bytes( 71 fixture.tagged_hash( 72 "BIP0340/challenge", 73 nonce_point[0].to_bytes(32, "big") + public_key + message, 74 ), 75 "big", 76 ) 77 % fixture.SECP256K1_ORDER 78 ) 79 signature = nonce_point[0].to_bytes(32, "big") + ( 80 (nonce + challenge * secret) % fixture.SECP256K1_ORDER 81 ).to_bytes(32, "big") 82 signed["sig"] = signature.hex() 83 assert fixture.valid_nostr_event(signed) 84 assert fixture.verify_nostr_signature(signed) 85 return signed 86 87 88 def bud11_event(now_unix_s: int, digest: str) -> dict: 89 return { 90 "created_at": now_unix_s - 5, 91 "kind": fixture.BUD11_EVENT_KIND, 92 "tags": [ 93 ["t", "upload"], 94 ["expiration", str(now_unix_s + 295)], 95 ["x", digest], 96 ["server", fixture.BUD11_SERVER_DOMAIN], 97 ], 98 "content": "Upload exact Tera image", 99 } 100 101 102 def mutate_bud11_event(event: dict, mutation: str, now_unix_s: int) -> tuple[dict, str]: 103 changed = copy.deepcopy(event) 104 if mutation == "none": 105 pass 106 elif mutation == "wrong_kind": 107 changed["kind"] = 1 108 elif mutation == "empty_content": 109 changed["content"] = "" 110 elif mutation == "oversized_content": 111 changed["content"] = "x" * (fixture.BUD11_CONTENT_MAX_BYTES + 1) 112 elif mutation == "leading_content_space": 113 changed["content"] = " Upload exact Tera image" 114 elif mutation == "control_content": 115 changed["content"] = "Upload\0image" 116 elif mutation == "missing_action": 117 changed["tags"].pop(0) 118 elif mutation == "wrong_action": 119 changed["tags"][0][1] = "delete" 120 elif mutation == "duplicate_action": 121 changed["tags"].insert(1, ["t", "upload"]) 122 elif mutation == "wrong_hash": 123 changed["tags"][2][1] = "f" * 64 124 elif mutation == "duplicate_hash": 125 changed["tags"].insert(3, copy.deepcopy(changed["tags"][2])) 126 elif mutation == "missing_server": 127 changed["tags"].pop(3) 128 elif mutation == "wrong_server": 129 changed["tags"][3][1] = "media.example" 130 elif mutation == "uppercase_server": 131 changed["tags"][3][1] = "Media.Example" 132 elif mutation == "duplicate_server": 133 changed["tags"].append(copy.deepcopy(changed["tags"][3])) 134 elif mutation == "missing_expiration": 135 changed["tags"].pop(1) 136 elif mutation == "noncanonical_expiration": 137 changed["tags"][1][1] = "0" + changed["tags"][1][1] 138 elif mutation == "expired": 139 changed["tags"][1][1] = str(now_unix_s) 140 elif mutation == "created_at_not_past": 141 changed["created_at"] = now_unix_s + 60 142 changed["tags"][1][1] = str(now_unix_s + 360) 143 elif mutation == "lifetime_too_long": 144 changed["tags"][1][1] = str(changed["created_at"] + 301) 145 elif mutation == "unknown_tag": 146 changed["tags"].append(["client", "radroots"]) 147 elif mutation in {"event_id", "signature", "authorization_scheme", "padded_base64"}: 148 pass 149 else: 150 raise AssertionError(f"unsupported mutation: {mutation}") 151 signed = sign_bud11_event(changed) 152 if mutation == "event_id": 153 signed["id"] = ("0" if signed["id"][0] != "0" else "1") + signed["id"][1:] 154 elif mutation == "signature": 155 signed["sig"] = ("0" if signed["sig"][0] != "0" else "1") + signed["sig"][1:] 156 raw = json.dumps(signed, ensure_ascii=False, separators=(",", ":")).encode("utf-8") 157 header = "Nostr " + base64.urlsafe_b64encode(raw).rstrip(b"=").decode("ascii") 158 if mutation == "authorization_scheme": 159 header = "Bearer " + header.removeprefix("Nostr ") 160 elif mutation == "padded_base64": 161 header += "=" 162 return signed, header 163 164 165 class LocalSocialFixtureTests(unittest.TestCase): 166 def persona_result(self) -> tuple[dict, dict]: 167 _, suite = fixture.load_persona_suite( 168 Path("test-fixtures/local-social-personas.v1.json") 169 ) 170 personas = [] 171 for persona in suite["personas"]: 172 personas.append( 173 { 174 "alias": persona["alias"], 175 "identity_sha256": hashlib.sha256( 176 persona["alias"].encode("ascii") 177 ).hexdigest(), 178 "subscriptions": 1, 179 "attempts": [ 180 { 181 "id": attempt["id"], 182 "flow": attempt["flow"], 183 "event_kind": fixture.FLOW_KINDS[attempt["flow"]], 184 "accepted": True, 185 "expected_failure_rejections": int( 186 attempt["expected_failure"] 187 == "transport_retry_relaunch" 188 ), 189 } 190 for attempt in persona["attempts"] 191 ], 192 } 193 ) 194 result = { 195 "schema": "tera.ios.local-social.persona-results.v1", 196 "schema_version": 1, 197 "run_id": "persona-result-test-001", 198 "source_commit": "1" * 40, 199 "source_tree": "2" * 40, 200 "fixture_sha256": "3" * 64, 201 "fixture_schema_sha256": "4" * 64, 202 "result_schema_sha256": "5" * 64, 203 "simulator": { 204 "udid": "11111111-2222-3333-4444-555555555555", 205 "os": "iOS 26.5", 206 "architecture": "arm64", 207 }, 208 "result_bundle_sha256": "6" * 64, 209 "evidence_sha256": "7" * 64, 210 "personas": personas, 211 "flow_counts": {flow: 3 for flow in fixture.FLOW_KINDS}, 212 "accepted_events": 15, 213 "event_kind_counts": {"1": 9, "31923": 3, "30402": 3}, 214 "accepted_uploads": 3, 215 "retrievals": 3, 216 "distinct_identities": 5, 217 "unknown_attempts": 0, 218 "duplicate_attempts": 0, 219 "expected_failure_rejections": 1, 220 "events_accepted_during_expected_failures": 0, 221 "production_network_contacts": 0, 222 "unintended_publications": 0, 223 "final_candidate_data_loss": 0, 224 "accessibility": { 225 "locale": "en_US", 226 "content_size": "accessibility-extra-extra-extra-large", 227 "reduce_motion": True, 228 "semantic_audit": "passed", 229 "voiceover_user_observed": False, 230 }, 231 "forward_repairs": [], 232 "complete_matrix_rerun": True, 233 } 234 return suite, result 235 236 def persona_attempt_attachments( 237 self, 238 *, 239 measured: bool = True, 240 ) -> tuple[dict, list[tuple[bytes, dict]]]: 241 _, suite = fixture.load_persona_suite( 242 Path("test-fixtures/local-social-personas.v1.json") 243 ) 244 attachments = [] 245 for persona in suite["personas"]: 246 identity = hashlib.sha256(persona["alias"].encode("ascii")).hexdigest() 247 for index, attempt in enumerate(persona["attempts"]): 248 validation = attempt["expected_failure"] == "validation_recovery" 249 retry = attempt["expected_failure"] == "transport_retry_relaunch" 250 network = {"state": "pending_step_258"} 251 if measured: 252 network = { 253 "state": "measured", 254 "accepted_connections": 1, 255 "rejected_connections": int(retry), 256 "non_loopback_attempts": 0, 257 "subscriptions": int(index == 0), 258 "accepted_events": 1, 259 "accepted_uploads": int(attempt["flow"] == "PhotoUpdate"), 260 "retrievals": int(attempt["flow"] == "PhotoUpdate"), 261 "unintended_publications": 0, 262 "events_accepted_during_expected_failure": 0, 263 "final_candidate_data_loss": 0, 264 } 265 value = { 266 "schema": fixture.PERSONA_ATTEMPT_SCHEMA, 267 "schema_version": 1, 268 "test_invocation": fixture.persona_invocation(), 269 "source": {"commit": "1" * 40, "tree": "2" * 40}, 270 "app_build_sha256": "3" * 64, 271 "simulator": { 272 "udid": "11111111-2222-3333-4444-555555555555", 273 "os": "iOS 26.5", 274 "architecture": "arm64", 275 }, 276 "run_id": "persona-result-test-001", 277 "persona_run_id": ( 278 f"persona-{persona['alias'].lower()}-" + "a" * 24 279 ), 280 "persona_alias": persona["alias"], 281 "attempt_id": attempt["id"], 282 "attempt_order": attempt["order"], 283 "flow": attempt["flow"], 284 "expected_failure": attempt["expected_failure"], 285 "public_identity_sha256": identity, 286 "endpoint_policy_sha256": "4" * 64, 287 "ui_observation": { 288 "validation_attempted": validation, 289 "validation_rejected": validation, 290 "retry_attempts": int(retry), 291 "relaunches": int(retry), 292 "retention_verified": True, 293 "today_projection_verified": True, 294 }, 295 "network_observation": network, 296 "accessibility": { 297 "locale": "en_US", 298 "content_size": "accessibility-extra-extra-extra-large", 299 "reduce_motion": True, 300 "progressive_disclosure": persona["interaction_profile"] 301 == "novice_progressive_disclosure", 302 "labels_values_traits": persona["interaction_profile"] 303 == "novice_accessibility_keyboard", 304 "keyboard_focus": persona["interaction_profile"] 305 == "novice_accessibility_keyboard", 306 "visible_actions": True, 307 "voiceover_user_observed": False, 308 }, 309 "artifact_digests": [], 310 } 311 raw = fixture.canonical_evidence_bytes(value) 312 attachments.append((raw, value)) 313 return suite, attachments 314 315 def test_bip340_reference_signature_and_mutation(self) -> None: 316 public_key = bytes.fromhex( 317 "f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9" 318 ) 319 message = bytes(32) 320 signature = bytes.fromhex( 321 "e907831f80848d1069a5371b402410364bdf1c5f8307b0084c55f1ce2dca8215" 322 "25f66a4a85ea8b71e482a74f382d2ce5ebeee8fdb2172f477df4900d310536c0" 323 ) 324 self.assertTrue(fixture.verify_bip340(public_key, message, signature)) 325 self.assertFalse( 326 fixture.verify_bip340(public_key, message, signature[:-1] + b"\x00") 327 ) 328 329 def test_bud11_corpus_is_exact_bounded_and_contains_no_sensitive_evidence( 330 self, 331 ) -> None: 332 path = Path("test-fixtures/bud11-upload-authorization-mutations.v1.json") 333 raw, corpus = fixture.load_bud11_mutation_corpus(path) 334 self.assertLessEqual(len(raw), fixture.MAX_JSON_BYTES) 335 self.assertEqual(len(corpus["vectors"]), len(fixture.BUD11_MUTATIONS)) 336 self.assertNotRegex( 337 raw.decode("utf-8"), 338 r'"(?:private_key|secret|seed|authorization|event|sig|pubkey)"\s*:', 339 ) 340 341 def test_bud11_corpus_rejects_unknown_duplicate_and_missing_vectors(self) -> None: 342 path = Path("test-fixtures/bud11-upload-authorization-mutations.v1.json") 343 value = json.loads(path.read_text(encoding="utf-8")) 344 value["unknown"] = True 345 with self.assertRaisesRegex(ValueError, "field inventory"): 346 fixture.validate_bud11_mutation_corpus(value) 347 value = json.loads(path.read_text(encoding="utf-8")) 348 value["vectors"].pop() 349 with self.assertRaisesRegex(ValueError, "inventory"): 350 fixture.validate_bud11_mutation_corpus(value) 351 with tempfile.TemporaryDirectory() as directory: 352 duplicate = Path(directory, "duplicate.json") 353 duplicate.write_text( 354 '{"schema":"x","schema":"x","schema_version":1,"vectors":[]}\n', 355 encoding="utf-8", 356 ) 357 with self.assertRaisesRegex(ValueError, "duplicate JSON member"): 358 fixture.load_bud11_mutation_corpus(duplicate) 359 360 def test_bud11_scalar_bounds_and_server_domain_grammar_are_fail_closed( 361 self, 362 ) -> None: 363 self.assertEqual(fixture.canonical_unsigned_decimal("0"), 0) 364 self.assertEqual( 365 fixture.canonical_unsigned_decimal(str(0xFFFF_FFFF_FFFF_FFFF)), 366 0xFFFF_FFFF_FFFF_FFFF, 367 ) 368 for value in ["", "00", "+1", "١", "18446744073709551616", "1" * 10_000]: 369 self.assertIsNone(fixture.canonical_unsigned_decimal(value)) 370 for value in ["127.0.0.1", "media.example", "a-b.example"]: 371 self.assertTrue(fixture.valid_bud11_server_domain(value), value) 372 for value in [ 373 "127.000.0.1", 374 "123", 375 "Media.Example", 376 "https://media.example", 377 "media.example:443", 378 "-media.example", 379 "media-.example", 380 ]: 381 self.assertFalse(fixture.valid_bud11_server_domain(value), value) 382 oversized = "Nostr " + "a" * (fixture.BUD11_AUTHORIZATION_ENCODED_MAX_BYTES + 1) 383 self.assertFalse( 384 fixture.valid_blossom_authorization( 385 oversized, 386 "a" * 64, 387 fixture.BUD11_SERVER_DOMAIN, 388 1, 389 ) 390 ) 391 392 def test_bud11_mutation_corpus_matches_strict_admission_and_relay_denial( 393 self, 394 ) -> None: 395 _, corpus = fixture.load_bud11_mutation_corpus( 396 Path("test-fixtures/bud11-upload-authorization-mutations.v1.json") 397 ) 398 now = int(time.time()) 399 digest = hashlib.sha256(b"canonical-bud11-photo").hexdigest() 400 for vector in corpus["vectors"]: 401 with self.subTest(vector=vector["id"]): 402 event, header = mutate_bud11_event( 403 bud11_event(now, digest), vector["mutation"], now 404 ) 405 if vector["surface"] == "http": 406 accepted = fixture.valid_blossom_authorization( 407 header, 408 digest, 409 fixture.BUD11_SERVER_DOMAIN, 410 now, 411 ) 412 else: 413 with tempfile.TemporaryDirectory() as directory: 414 root = Path(directory) 415 state = fixture.FixtureState( 416 root / "evidence.json", root / "control", 21100 417 ) 418 accepted = bool(state.publish(event)) 419 self.assertEqual(accepted, vector["expected_accepted"]) 420 421 def test_local_blossom_http_runtime_enforces_the_shared_bud11_corpus(self) -> None: 422 _, corpus = fixture.load_bud11_mutation_corpus( 423 Path("test-fixtures/bud11-upload-authorization-mutations.v1.json") 424 ) 425 body = b"canonical-bud11-photo" 426 digest = hashlib.sha256(body).hexdigest() 427 now = int(time.time()) 428 with tempfile.TemporaryDirectory() as directory: 429 root = Path(directory) 430 control = root / "control" 431 control.touch() 432 state = fixture.FixtureState(root / "evidence.json", control, 0) 433 with blossom_server(fixture.BlossomHandler, state): 434 cases = [ 435 (v, "/upload", digest) 436 for v in corpus["vectors"] 437 if v["surface"] == "http" 438 ] 439 valid = next(v for v, _, _ in cases if v["expected_accepted"]) 440 cases += [ 441 (valid, f"/{digest}.png", digest), 442 (valid, "/upload", ""), 443 (valid, "/upload", digest.upper()), 444 ] 445 for vector, path, supplied_hash in cases: 446 with self.subTest( 447 vector=vector["id"], path=path, supplied_hash=supplied_hash 448 ): 449 _, header = mutate_bud11_event( 450 bud11_event(now, digest), vector["mutation"], now 451 ) 452 connection = http.client.HTTPConnection( 453 "127.0.0.1", state.blossom_port, timeout=5 454 ) 455 connection.request( 456 "PUT", 457 path, 458 body=body, 459 headers={ 460 "Authorization": header, 461 "Content-Type": "image/png", 462 "Content-Length": str(len(body)), 463 "X-SHA-256": supplied_hash, 464 }, 465 ) 466 response = connection.getresponse() 467 response.read() 468 connection.close() 469 self.assertEqual( 470 response.status == 200, 471 vector["expected_accepted"] 472 and path == "/upload" 473 and supplied_hash == digest, 474 ) 475 evidence = json.loads((root / "evidence.json").read_text(encoding="utf-8")) 476 self.assertEqual(evidence["accepted_uploads"], 1) 477 478 def test_fixture_rejects_duplicate_and_unknown_fields(self) -> None: 479 source = Path("test-fixtures/local-social-personas.v1.json") 480 payload = json.loads(source.read_text(encoding="utf-8")) 481 payload["unexpected"] = True 482 with self.assertRaisesRegex(ValueError, "field inventory"): 483 fixture.validate_persona_suite(payload) 484 485 with tempfile.TemporaryDirectory() as directory: 486 duplicate = Path(directory, "duplicate.json") 487 duplicate.write_text('{"schema":1,"schema":1}\n', encoding="utf-8") 488 with self.assertRaisesRegex(ValueError, "duplicate JSON member"): 489 fixture.read_json(duplicate) 490 491 def test_json_reads_enforce_maximum_plus_one_before_decoding(self) -> None: 492 with tempfile.TemporaryDirectory() as directory: 493 path = Path(directory, "bounded.json") 494 path.write_bytes(b'{"x":1}') 495 self.assertEqual(fixture.read_json(path, 7)[1], {"x": 1}) 496 path.write_bytes(b'{"x":1}\n') 497 with self.assertRaisesRegex(ValueError, "byte bound"): 498 fixture.read_json(path, 7) 499 500 def test_schemas_pass_meta_validation_and_match_semantic_corpora(self) -> None: 501 fixture_raw, suite = fixture.load_persona_suite( 502 Path("test-fixtures/local-social-personas.v1.json") 503 ) 504 self.assertTrue(fixture_raw) 505 _, persona_schema = fixture.load_schema_file( 506 Path("test-fixtures/local-social-personas.v1.schema.json"), 507 "https://radroots.org/schemas/ios/local-social-personas.v1.schema.json", 508 ) 509 fixture.validate_schema_instance(persona_schema, suite, "persona fixture") 510 _, corpus = fixture.load_bud11_mutation_corpus( 511 Path("test-fixtures/bud11-upload-authorization-mutations.v1.json") 512 ) 513 _, corpus_schema = fixture.load_schema_file( 514 Path("test-fixtures/bud11-upload-authorization-mutations.v1.schema.json"), 515 "https://radroots.org/schemas/ios/bud11-upload-authorization-mutations.v1.schema.json", 516 ) 517 fixture.validate_schema_instance(corpus_schema, corpus, "BUD-11 corpus") 518 519 changed = copy.deepcopy(suite) 520 changed["unexpected"] = True 521 with self.assertRaisesRegex(ValueError, "disagrees"): 522 fixture.validate_schema_instance(persona_schema, changed, "persona fixture") 523 524 with tempfile.TemporaryDirectory() as directory: 525 invalid = Path(directory, "invalid.schema.json") 526 invalid.write_text( 527 json.dumps( 528 { 529 "$schema": "https://json-schema.org/draft/2020-12/schema", 530 "$id": "https://radroots.org/schemas/ios/invalid.json", 531 "type": "object", 532 "additionalProperties": False, 533 "properties": {"x": {"type": "not-a-json-schema-type"}}, 534 } 535 ), 536 encoding="utf-8", 537 ) 538 with self.assertRaisesRegex(ValueError, "meta-validation"): 539 fixture.load_schema_file( 540 invalid, "https://radroots.org/schemas/ios/invalid.json" 541 ) 542 invalid.write_text( 543 json.dumps( 544 { 545 "$schema": "https://json-schema.org/draft/2020-12/schema", 546 "$id": "https://radroots.org/schemas/ios/invalid.json", 547 "type": "object", 548 "additionalProperties": False, 549 "properties": { 550 "x": {"$ref": "https://example.com/external.json"} 551 }, 552 } 553 ), 554 encoding="utf-8", 555 ) 556 with self.assertRaisesRegex(ValueError, "external reference"): 557 fixture.load_schema_file( 558 invalid, "https://radroots.org/schemas/ios/invalid.json" 559 ) 560 561 def test_result_bundle_digest_is_framed_bounded_and_pinned(self) -> None: 562 with tempfile.TemporaryDirectory() as directory: 563 root = Path(directory) 564 (root / "a.txt").write_bytes(b"A") 565 (root / "nested").mkdir() 566 (root / "nested" / "b.bin").write_bytes(b"\x00B") 567 self.assertEqual( 568 fixture.directory_digest(root, 3, 2, 3), 569 "25fb5f36c3b044de2716ddfbbd95c2e5eb39cf38a5bcf2feb793cd57ce27147a", 570 ) 571 with self.assertRaisesRegex(ValueError, "entry bound"): 572 fixture.directory_digest(root, 2, 2, 3) 573 with self.assertRaisesRegex(ValueError, "byte bound"): 574 fixture.directory_digest(root, 3, 1, 3) 575 with self.assertRaisesRegex(ValueError, "byte bound"): 576 fixture.directory_digest(root, 3, 2, 2) 577 578 alternate = root / "alternate" 579 alternate.mkdir() 580 (alternate / "a").write_bytes(b".txtA") 581 self.assertNotEqual( 582 fixture.directory_digest(root, 5, 8, 16), 583 fixture.directory_digest(alternate, 1, 8, 8), 584 ) 585 586 def test_fixture_freezes_exact_persona_and_flow_matrix(self) -> None: 587 _, suite = fixture.load_persona_suite( 588 Path("test-fixtures/local-social-personas.v1.json") 589 ) 590 attempts = fixture.persona_attempts(suite) 591 self.assertEqual( 592 tuple(persona["alias"] for persona in suite["personas"]), 593 fixture.PERSONA_ALIASES, 594 ) 595 self.assertEqual(len(attempts), 15) 596 self.assertEqual( 597 { 598 flow: sum(item["flow"] == flow for item in attempts.values()) 599 for flow in fixture.FLOW_KINDS 600 }, 601 {flow: 3 for flow in fixture.FLOW_KINDS}, 602 ) 603 604 def test_legacy_fixture_control_remains_file_presence_based(self) -> None: 605 body = b"legacy-photo" 606 digest = fixture.hashlib.sha256(body).hexdigest() 607 with tempfile.TemporaryDirectory() as directory: 608 root = Path(directory) 609 control = root / "control" 610 control.touch() 611 state = fixture.FixtureState(root / "evidence.json", control, 21100) 612 with mock.patch.object( 613 fixture, "valid_blossom_authorization", return_value=True 614 ): 615 accepted, _ = state.upload(body, "image/png", digest, "Nostr valid") 616 self.assertTrue(accepted) 617 self.assertIsNotNone(state.retrieve(digest)) 618 evidence = json.loads((root / "evidence.json").read_text(encoding="utf-8")) 619 self.assertEqual(evidence["accepted_uploads"], 1) 620 self.assertEqual(evidence["retrievals"], 1) 621 622 def test_persona_retrieval_counts_only_its_own_uploaded_digest(self) -> None: 623 body = b"persona-photo" 624 digest = fixture.hashlib.sha256(body).hexdigest() 625 _, suite = fixture.load_persona_suite( 626 Path("test-fixtures/local-social-personas.v1.json") 627 ) 628 with tempfile.TemporaryDirectory() as directory: 629 root = Path(directory) 630 control = root / "control.json" 631 state = fixture.FixtureState(root / "evidence.json", control, 21100, suite) 632 self.write_persona_control(control, "P01") 633 with mock.patch.object( 634 fixture, "valid_blossom_authorization", return_value=True 635 ): 636 accepted, _ = state.upload(body, "image/png", digest, "Nostr valid") 637 self.assertTrue(accepted) 638 self.assertIsNotNone(state.retrieve(digest)) 639 self.write_persona_control(control, "P02") 640 self.assertIsNotNone(state.retrieve(digest)) 641 642 evidence = json.loads((root / "evidence.json").read_text(encoding="utf-8")) 643 self.assertEqual(evidence["retrievals"], 1) 644 self.assertEqual(evidence["personas"][0]["retrievals"], 1) 645 self.assertEqual(evidence["personas"][1]["retrievals"], 0) 646 647 def test_transport_retry_drops_one_response_then_accepts(self) -> None: 648 _, suite = fixture.load_persona_suite( 649 Path("test-fixtures/local-social-personas.v1.json") 650 ) 651 attempt = fixture.persona_attempts(suite)["P05-A01"] 652 event = { 653 "id": "1" * 64, 654 "kind": fixture.FLOW_KINDS[attempt["flow"]], 655 "pubkey": "2" * 64, 656 } 657 with tempfile.TemporaryDirectory() as directory: 658 root = Path(directory) 659 control = root / "control.json" 660 self.write_persona_control(control, "P05") 661 state = fixture.FixtureState(root / "evidence.json", control, 21100, suite) 662 with mock.patch.object(fixture, "classify_attempt", return_value=attempt): 663 self.assertIsNone(state._publish_persona_event(event)) 664 self.assertTrue(state._publish_persona_event(event)) 665 666 evidence = json.loads((root / "evidence.json").read_text(encoding="utf-8")) 667 self.assertEqual(evidence["expected_failure_rejections"], 1) 668 self.assertEqual(evidence["accepted_events"], 1) 669 670 @staticmethod 671 def write_persona_control(path: Path, alias: str) -> None: 672 path.write_text( 673 json.dumps( 674 { 675 "schema": fixture.PERSONA_CONTROL_SCHEMA, 676 "active_persona": alias, 677 "blossom_enabled": True, 678 } 679 ), 680 encoding="utf-8", 681 ) 682 683 def test_result_contract_accepts_future_ios_and_rejects_drift(self) -> None: 684 suite, result = self.persona_result() 685 result_schema = json.loads( 686 Path("test-fixtures/local-social-persona-results.v1.schema.json").read_text( 687 encoding="utf-8" 688 ) 689 ) 690 os_pattern = result_schema["properties"]["simulator"]["properties"]["os"][ 691 "pattern" 692 ] 693 self.assertIsNotNone(re.fullmatch(os_pattern, "iOS 26.5")) 694 self.assertIsNone(re.fullmatch(os_pattern, "iOS 17.7")) 695 self.assertIs( 696 fixture.validate_persona_result( 697 result, suite, "3" * 64, "4" * 64, "5" * 64 698 ), 699 result, 700 ) 701 fixture.validate_schema_instance(result_schema, result, "persona v1 result") 702 for mutation in ( 703 lambda value: value.update({"unexpected": True}), 704 lambda value: value["simulator"].update({"os": "iOS 17.7"}), 705 lambda value: value["accessibility"].update( 706 {"voiceover_user_observed": True} 707 ), 708 ): 709 changed = copy.deepcopy(result) 710 mutation(changed) 711 with self.assertRaises(ValueError): 712 fixture.validate_persona_result( 713 changed, suite, "3" * 64, "4" * 64, "5" * 64 714 ) 715 716 def test_attempt_evidence_is_strict_bound_and_secret_free(self) -> None: 717 suite, attachments = self.persona_attempt_attachments(measured=False) 718 for raw, value in attachments: 719 self.assertLessEqual(len(raw), fixture.MAX_PERSONA_ATTACHMENT_BYTES) 720 self.assertIs( 721 fixture.validate_persona_attempt_evidence( 722 value, suite, require_measured_network=False 723 ), 724 value, 725 ) 726 self.assertNotRegex( 727 raw.decode("utf-8"), 728 r'"(?:private_key|secret|seed|signed_event|event_content|raw_event)"', 729 ) 730 with self.assertRaisesRegex(ValueError, "not measured"): 731 fixture.validate_persona_attempt_evidence( 732 value, suite, require_measured_network=True 733 ) 734 735 def test_attempt_evidence_rejects_one_field_identity_and_outcome_drift( 736 self, 737 ) -> None: 738 suite, attachments = self.persona_attempt_attachments() 739 _, canonical = attachments[0] 740 mutations = ( 741 lambda value: value.update({"unknown": True}), 742 lambda value: value["test_invocation"].update( 743 {"identifier": "TeraUITests/testOther"} 744 ), 745 lambda value: value["source"].update({"tree": "A" * 40}), 746 lambda value: value.update({"app_build_sha256": "0" * 63}), 747 lambda value: value.update({"run_id": "x"}), 748 lambda value: value.update({"persona_alias": "P02"}), 749 lambda value: value.update({"attempt_order": 2}), 750 lambda value: value["ui_observation"].update( 751 {"today_projection_verified": False} 752 ), 753 lambda value: value["network_observation"].update( 754 {"non_loopback_attempts": 1} 755 ), 756 lambda value: value["accessibility"].update( 757 {"voiceover_user_observed": True} 758 ), 759 lambda value: value.update({"private_key": "canary"}), 760 ) 761 for mutation in mutations: 762 changed = copy.deepcopy(canonical) 763 mutation(changed) 764 with self.assertRaises(ValueError): 765 fixture.validate_persona_attempt_evidence( 766 changed, suite, require_measured_network=True 767 ) 768 769 def test_persona_result_v2_is_reconstructed_only_from_measured_attempts( 770 self, 771 ) -> None: 772 suite, attachments = self.persona_attempt_attachments() 773 result = fixture.reconstruct_persona_result_v2( 774 suite, 775 attachments, 776 fixture_sha256="5" * 64, 777 fixture_schema_sha256="6" * 64, 778 attempt_schema_sha256="7" * 64, 779 result_schema_sha256="8" * 64, 780 result_bundle_sha256="9" * 64, 781 forward_repairs=[], 782 ) 783 self.assertEqual(result["schema"], fixture.PERSONA_RESULT_V2_SCHEMA) 784 self.assertEqual(result["accepted_events"], 15) 785 self.assertEqual(result["expected_failure_rejections"], 2) 786 self.assertEqual(result["accepted_uploads"], 3) 787 self.assertEqual(result["retrievals"], 3) 788 self.assertEqual(result["non_loopback_attempts"], 0) 789 self.assertEqual(len(result["attachments"]), 15) 790 _, attempt_schema = fixture.load_schema_file( 791 Path("test-fixtures/local-social-persona-attempt-evidence.v1.schema.json"), 792 "https://radroots.org/schemas/ios/local-social-persona-attempt-evidence.v1.schema.json", 793 ) 794 for _, attempt in attachments: 795 fixture.validate_schema_instance( 796 attempt_schema, attempt, "persona attempt evidence" 797 ) 798 _, result_schema = fixture.load_schema_file( 799 Path("test-fixtures/local-social-persona-results.v2.schema.json"), 800 "https://radroots.org/schemas/ios/local-social-persona-results.v2.schema.json", 801 ) 802 fixture.validate_schema_instance(result_schema, result, "persona v2 result") 803 804 _, pending = self.persona_attempt_attachments(measured=False) 805 with self.assertRaisesRegex(ValueError, "not measured"): 806 fixture.reconstruct_persona_result_v2( 807 suite, 808 pending, 809 fixture_sha256="5" * 64, 810 fixture_schema_sha256="6" * 64, 811 attempt_schema_sha256="7" * 64, 812 result_schema_sha256="8" * 64, 813 result_bundle_sha256="9" * 64, 814 forward_repairs=[], 815 ) 816 817 def test_xcresult_test_identity_and_attachment_inventory_are_exact(self) -> None: 818 suite, attachments = self.persona_attempt_attachments() 819 tests = { 820 "devices": [], 821 "testNodes": [ 822 { 823 "children": [ 824 { 825 "name": "testLocalSocialDeterministicPersonas()", 826 "nodeIdentifier": fixture.PERSONA_XCRESULT_NODE_IDENTIFIER, 827 "nodeIdentifierURL": fixture.PERSONA_XCRESULT_NODE_URL, 828 "nodeType": "Test Case", 829 "result": "Passed", 830 } 831 ], 832 "name": "Tera", 833 "nodeType": "Test Plan", 834 "result": "Passed", 835 } 836 ], 837 "testPlanConfigurations": [], 838 } 839 self.assertEqual(fixture.exact_persona_test_node(tests)["result"], "Passed") 840 for mutation in ( 841 lambda value: value["testNodes"][0]["children"][0].update( 842 {"result": "Failed"} 843 ), 844 lambda value: value["testNodes"][0]["children"][0].update( 845 {"nodeIdentifierURL": "test://wrong"} 846 ), 847 lambda value: value["testNodes"].append( 848 copy.deepcopy(value["testNodes"][0]) 849 ), 850 ): 851 changed = copy.deepcopy(tests) 852 mutation(changed) 853 with self.assertRaises(ValueError): 854 fixture.exact_persona_test_node(changed) 855 856 with tempfile.TemporaryDirectory() as directory: 857 root = Path(directory) 858 rows = [] 859 for index, (name, (raw, _)) in enumerate( 860 zip(fixture.PERSONA_ATTACHMENT_NAMES, attachments, strict=True) 861 ): 862 exported = "exported-" + name 863 suggested = f"{name[:-5]}_0_00000000-0000-0000-0000-{index:012X}.json" 864 (root / exported).write_bytes(raw) 865 rows.append( 866 { 867 "exportedFileName": exported, 868 "suggestedHumanReadableName": suggested, 869 "isAssociatedWithFailure": False, 870 "configurationName": "Test Scheme Action", 871 "deviceName": "iPhone 17 Pro", 872 "deviceId": "11111111-2222-3333-4444-555555555555", 873 } 874 ) 875 manifest = [ 876 { 877 "testIdentifier": fixture.PERSONA_XCRESULT_NODE_IDENTIFIER, 878 "testIdentifierURL": fixture.PERSONA_XCRESULT_NODE_URL, 879 "attachments": rows, 880 } 881 ] 882 (root / "manifest.json").write_text(json.dumps(manifest), encoding="utf-8") 883 loaded = fixture.load_exported_persona_attachments( 884 root, suite, require_measured_network=True 885 ) 886 self.assertEqual(len(loaded), 15) 887 self.assertEqual(loaded[0][1]["attempt_id"], "P01-A01") 888 889 unexpected = root / "unexpected.json" 890 unexpected.write_text("{}", encoding="utf-8") 891 with self.assertRaisesRegex(ValueError, "entry bound"): 892 fixture.load_exported_persona_attachments( 893 root, suite, require_measured_network=True 894 ) 895 unexpected.unlink() 896 897 rows[0]["suggestedHumanReadableName"] = rows[1][ 898 "suggestedHumanReadableName" 899 ] 900 (root / "manifest.json").write_text(json.dumps(manifest), encoding="utf-8") 901 with self.assertRaises(ValueError): 902 fixture.load_exported_persona_attachments( 903 root, suite, require_measured_network=True 904 ) 905 906 rows[0]["suggestedHumanReadableName"] = "radroots-local-social-P01-A01.json" 907 (root / "manifest.json").write_text(json.dumps(manifest), encoding="utf-8") 908 with self.assertRaises(ValueError): 909 fixture.load_exported_persona_attachments( 910 root, suite, require_measured_network=True 911 ) 912 913 def test_xcresult_attachment_read_rejects_maximum_plus_one(self) -> None: 914 suite, attachments = self.persona_attempt_attachments() 915 with tempfile.TemporaryDirectory() as directory: 916 root = Path(directory) 917 name = ( 918 "radroots-local-social-P01-A01_0_" 919 "00000000-0000-0000-0000-000000000000.json" 920 ) 921 exported = "oversized.json" 922 (root / exported).write_bytes( 923 b"{" + b" " * fixture.MAX_PERSONA_ATTACHMENT_BYTES + b"}" 924 ) 925 rows = [ 926 { 927 "exportedFileName": exported, 928 "suggestedHumanReadableName": name, 929 "isAssociatedWithFailure": False, 930 "configurationName": "Test Scheme Action", 931 "deviceName": "iPhone 17 Pro", 932 "deviceId": "11111111-2222-3333-4444-555555555555", 933 } 934 ] 935 for index, canonical_name in enumerate( 936 fixture.PERSONA_ATTACHMENT_NAMES[1:], 1 937 ): 938 other_name = ( 939 f"{canonical_name[:-5]}_0_00000000-0000-0000-0000-{index:012X}.json" 940 ) 941 other_exported = f"exported-{index}.json" 942 (root / other_exported).write_bytes(attachments[index][0]) 943 rows.append( 944 { 945 "exportedFileName": other_exported, 946 "suggestedHumanReadableName": other_name, 947 "isAssociatedWithFailure": False, 948 "configurationName": "Test Scheme Action", 949 "deviceName": "iPhone 17 Pro", 950 "deviceId": "11111111-2222-3333-4444-555555555555", 951 } 952 ) 953 (root / "manifest.json").write_text( 954 json.dumps( 955 [ 956 { 957 "testIdentifier": fixture.PERSONA_XCRESULT_NODE_IDENTIFIER, 958 "testIdentifierURL": fixture.PERSONA_XCRESULT_NODE_URL, 959 "attachments": rows, 960 } 961 ] 962 ), 963 encoding="utf-8", 964 ) 965 with self.assertRaisesRegex(ValueError, "byte bound"): 966 fixture.load_exported_persona_attachments( 967 root, suite, require_measured_network=True 968 ) 969 970 def test_simulator_metadata_uses_exact_result_bundle_device(self) -> None: 971 udid = "11111111-2222-3333-4444-555555555555" 972 result_bundle = Path("result.xcresult") 973 simctl_devices = { 974 "devices": { 975 "com.apple.CoreSimulator.SimRuntime.iOS-26-5": [ 976 { 977 "udid": udid, 978 "isAvailable": True, 979 "name": "iPhone 17 Pro", 980 } 981 ] 982 } 983 } 984 result_summary = { 985 "failedTests": 0, 986 "passedTests": 1, 987 "result": "Passed", 988 "skippedTests": 0, 989 "devicesAndConfigurations": [ 990 { 991 "device": { 992 "architecture": "arm64", 993 "deviceId": udid, 994 "osVersion": "26.5", 995 "platform": "iOS Simulator", 996 } 997 } 998 ], 999 } 1000 1001 with mock.patch.object( 1002 fixture, 1003 "run_json_command_bounded", 1004 side_effect=[simctl_devices, result_summary], 1005 ) as run_json: 1006 self.assertEqual( 1007 fixture.simulator_metadata(udid.lower(), result_bundle), 1008 {"udid": udid, "os": "iOS 26.5.0", "architecture": "arm64"}, 1009 ) 1010 commands = [call.args[0] for call in run_json.call_args_list] 1011 self.assertEqual(commands[0][:3], ["xcrun", "simctl", "list"]) 1012 self.assertEqual(commands[1][:3], ["xcrun", "xcresulttool", "get"]) 1013 self.assertNotIn("spawn", commands[0] + commands[1]) 1014 1015 patch_summary = copy.deepcopy(result_summary) 1016 patch_summary["devicesAndConfigurations"][0]["device"]["osVersion"] = "26.5.1" 1017 patch_devices = { 1018 "devices": { 1019 "com.apple.CoreSimulator.SimRuntime.iOS-26-5-1": [ 1020 simctl_devices["devices"][ 1021 "com.apple.CoreSimulator.SimRuntime.iOS-26-5" 1022 ][0] 1023 ] 1024 } 1025 } 1026 with mock.patch.object( 1027 fixture, 1028 "run_json_command_bounded", 1029 side_effect=[patch_devices, patch_summary], 1030 ): 1031 self.assertEqual( 1032 fixture.simulator_metadata(udid, result_bundle)["os"], "iOS 26.5.1" 1033 ) 1034 1035 for mutation in ( 1036 lambda value: value["devicesAndConfigurations"][0]["device"].update( 1037 {"architecture": "x86_64"} 1038 ), 1039 lambda value: value["devicesAndConfigurations"][0]["device"].update( 1040 {"deviceId": "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"} 1041 ), 1042 lambda value: value["devicesAndConfigurations"][0]["device"].update( 1043 {"osVersion": "17.7"} 1044 ), 1045 lambda value: value["devicesAndConfigurations"][0]["device"].update( 1046 {"platform": "macOS"} 1047 ), 1048 lambda value: value.update({"result": "Failed", "failedTests": 1}), 1049 ): 1050 changed = copy.deepcopy(result_summary) 1051 mutation(changed) 1052 with ( 1053 mock.patch.object( 1054 fixture, 1055 "run_json_command_bounded", 1056 side_effect=[simctl_devices, changed], 1057 ), 1058 self.assertRaises(ValueError), 1059 ): 1060 fixture.simulator_metadata(udid, result_bundle) 1061 1062 def test_subprocess_json_is_bounded_before_decoding(self) -> None: 1063 command = [ 1064 fixture.sys.executable, 1065 "-c", 1066 "import sys; sys.stdout.write('{\\\"x\\\":1}')", 1067 ] 1068 self.assertEqual(fixture.run_json_command_bounded(command, 7), {"x": 1}) 1069 with self.assertRaisesRegex(ValueError, "byte bound"): 1070 fixture.run_json_command_bounded(command, 6) 1071 1072 def test_verifier_toolchain_identity_is_exact(self) -> None: 1073 fixture.verify_toolchain_identity() 1074 with ( 1075 mock.patch.object(fixture.sys, "version_info", (3, 14, 6)), 1076 self.assertRaisesRegex(RuntimeError, "Python identity"), 1077 ): 1078 fixture.verify_toolchain_identity() 1079 with ( 1080 mock.patch.object( 1081 fixture.importlib.metadata, "version", return_value="4.25.1" 1082 ), 1083 self.assertRaisesRegex(RuntimeError, "schema dependency"), 1084 ): 1085 fixture.verify_toolchain_identity() 1086 1087 def test_control_is_bounded_and_deny_unknown(self) -> None: 1088 with tempfile.TemporaryDirectory() as directory: 1089 path = Path(directory, "control.json") 1090 path.write_text( 1091 json.dumps( 1092 { 1093 "schema": fixture.PERSONA_CONTROL_SCHEMA, 1094 "active_persona": "P03", 1095 "blossom_enabled": True, 1096 } 1097 ), 1098 encoding="utf-8", 1099 ) 1100 self.assertEqual(fixture.read_control(path)["active_persona"], "P03") 1101 path.write_text( 1102 json.dumps( 1103 { 1104 "schema": fixture.PERSONA_CONTROL_SCHEMA, 1105 "active_persona": "P03", 1106 "blossom_enabled": True, 1107 "secret": "forbidden", 1108 } 1109 ), 1110 encoding="utf-8", 1111 ) 1112 self.assertIsNone(fixture.read_control(path)) 1113 1114 def test_observable_loopback_factory_counts_and_rejects_connections(self) -> None: 1115 with tempfile.TemporaryDirectory() as directory: 1116 root = Path(directory) 1117 state = fixture.FixtureState(root / "evidence.json", root / "control", 0) 1118 server = fixture.LoopbackConnectionFactory.relay(0, state) 1119 try: 1120 self.assertTrue( 1121 server.verify_request(mock.Mock(), ("127.0.0.1", 20_000)) 1122 ) 1123 self.assertFalse( 1124 server.verify_request(mock.Mock(), ("192.0.2.1", 20_001)) 1125 ) 1126 finally: 1127 server.server_close() 1128 _, evidence = fixture.read_json(root / "evidence.json") 1129 self.assertEqual(evidence["accepted_connections"], 1) 1130 self.assertEqual(evidence["rejected_connections"], 1) 1131 self.assertEqual(evidence["non_loopback_attempts"], 1) 1132 self.assertEqual(evidence["production_network_contacts"], 1) 1133 1134 def test_attempt_classification_accepts_only_exact_photo_wire_shape(self) -> None: 1135 marker = "rr-p01-a02-photo" 1136 digest = "a" * 64 1137 url = f"http://127.0.0.1:21101/{digest}.png" 1138 attempt = {"flow": "PhotoUpdate", "marker": marker} 1139 event = { 1140 "content": f"{marker}\n{url}", 1141 "tags": [ 1142 [ 1143 "imeta", 1144 f"url {url}", 1145 f"x {digest}", 1146 "m image/png", 1147 "dim 1x1", 1148 "size 1", 1149 "alt Local qualification image", 1150 ] 1151 ], 1152 } 1153 1154 self.assertIs(fixture.classify_attempt(event, {"P01-A02": attempt}), attempt) 1155 for mutation in ( 1156 lambda value: value.update({"content": marker}), 1157 lambda value: value.update({"content": f"prefix {marker}\n{url}"}), 1158 lambda value: value.update( 1159 {"content": f"{marker}\nhttps://example.com/{digest}.png"} 1160 ), 1161 lambda value: value["tags"][0].remove(f"x {digest}"), 1162 ): 1163 changed = copy.deepcopy(event) 1164 mutation(changed) 1165 self.assertIsNone(fixture.classify_attempt(changed, {"P01-A02": attempt})) 1166 1167 text_attempt = {"flow": "Update", "marker": "rr-p01-a01-update"} 1168 embedded = {"content": "prefix rr-p01-a01-update", "tags": []} 1169 self.assertIsNone(fixture.classify_attempt(embedded, {"P01-A01": text_attempt})) 1170 1171 1172 if __name__ == "__main__": 1173 unittest.main()