field_ios

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

commit bf8278855e7ed1425dfbb1dbddc9cadfe5c53772
parent 9e01de02a315a6ec62fe5fc853292afd0781e163
Author: triesap <tyson@radroots.org>
Date:   Fri,  9 Oct 2026 10:49:59 +0000

tera: Repair fixture ownership and qualify the current tooling source

- Enforce complete authored inventories and fixed maintainability anchors.
- Bound selector capture and fixture connection lifetimes with real cleanup.
- Execute all 348 tests once while retaining standalone release checks.
- Qualify current native artifacts and obtain fresh independent acceptance.

Diffstat:
MREADME.md | 8++++++--
MTeraFFI/provenance.json | 26+++++++++++++-------------
MTeraFFI/source.lock | 4++--
MTeraFFI/source/aarch64-apple-darwin.json | 168++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
MTeraFFI/source/aarch64-apple-ios-sim.json | 168++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
MTeraFFI/source/aarch64-apple-ios.json | 168++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Mrelease/provenance.json | 4++--
Ascripts/authored_source_inventory.py | 104+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/fixture_connections.py | 144+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/fixture_test_policy.py | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/fixture_test_policy.v1.json | 23+++++++++++++++++++++++
Ascripts/fixture_tool_dispatch.py | 395+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mscripts/local-social-fixture.py | 260+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mscripts/local_social_test_support.py | 7+++++--
Mscripts/maintainability_ratchet.py | 89++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
Mscripts/persona_attachment_selection.py | 5++---
Mscripts/swift-quality.sh | 40+++-------------------------------------
Ascripts/test_authored_source_inventory.py | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/test_fixture_lifetimes.py | 339+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/test_fixture_test_policy.py | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/test_fixture_tool_dispatch.py | 322+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mscripts/test_local_social_fixture.py | 143+++++++++++++++++++++++++++++++++++++------------------------------------------
Mscripts/test_local_social_relay.py | 2+-
Mscripts/test_maintainability_ratchet.py | 125+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mscripts/test_persona_attachment_selection.py | 2+-
Mscripts/test_release_evidence.py | 360+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Mscripts/test_unittest_report.py | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Ascripts/test_unittest_report_bootstrap.py | 270+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/test_unittest_report_nested.py | 789+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mscripts/test_xcode_selection.py | 444+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Mscripts/unittest_report.py | 780+++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Ascripts/unittest_report_origin.py | 393+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mscripts/verify-package-contract.sh | 23-----------------------
Mtest-fixtures/legacy-identifiers.v1.json | 14++++++++++++++
34 files changed, 5145 insertions(+), 836 deletions(-)

diff --git a/README.md b/README.md @@ -113,7 +113,8 @@ This test-only JVM harness is part of `make verify` and does not qualify Android UI or a published release. `make swift-quality` also applies the exact checked SwiftLint complexity -baseline and the repository-owned Swift/Python maintainability ratchet. New +baseline, the repository-owned Swift/Python maintainability ratchet, and the +complete authored Python fixture and verifier test inventory. New Swift files are capped at 600 physical lines, new Python files at 800, and new Python functions at complexity 10. Existing larger files and functions are a closed, non-growing inventory; the newly separated user-message classifier @@ -232,7 +233,10 @@ of the release lane. `make package-contract-check` evaluates the Swift package manifest and parses the TOML, plist, JSON, xcconfig, project-package, and lock inputs as structured, -bounded data. It also runs the locked fixture and verifier unit suites. +bounded data, then verifies the canonical mutation corpus and persona fixtures. +The complete locked fixture and verifier unit suites run in `make swift-quality`, +so package builds and release preflight retain their contract checks without +repeating the full test inventory. Complete qualification requires both lanes. For focused UI iteration, `make ui-test UI_TEST_SELECTOR=TeraAccessibilityUITests/testFormAskAtLargestTextWithReduceMotionAndLocalSave` selects an existing owned class and method through the same artifact, package, diff --git a/TeraFFI/provenance.json b/TeraFFI/provenance.json @@ -92,19 +92,19 @@ "sha256": "e714580021c9aac76a0b84f2bb4719aece857eda99bf98050e1e1f0039055226" }, { - "bytes": 156974, + "bytes": 160029, "path": "source/aarch64-apple-darwin.json", - "sha256": "bdc7d432de293191bce93b7eca2489a41e8880691d36b0284ffc4a1ed9796f21" + "sha256": "140870c865fadd5ab210ead46d050da3b1de413fff9168e243505b59ca791e79" }, { - "bytes": 156899, + "bytes": 159954, "path": "source/aarch64-apple-ios-sim.json", - "sha256": "abf6f430c7d998acd67bb8a10600bf8a72eb3397c976d3c0f827ae7e39b9bd7a" + "sha256": "ec4f2d5fc17fcb272f8f0fb398c097e6686597277b9c962a33f1d17e9637fbad" }, { - "bytes": 156869, + "bytes": 159924, "path": "source/aarch64-apple-ios.json", - "sha256": "3c594f013ca7255d5136ad85842cee800a727297e5c3b9ba176358c44ab42b9c" + "sha256": "c7fd3be77d8aee5476967f830c73497ac438b1d7405c7d7ff8b3958ff9c15105" } ], "language": "swift", @@ -112,7 +112,7 @@ "schema": "radroots.artifact-manifest.v2", "source": { "repository": "https://github.com/radrootslabs/tera", - "tree": "413ea1de23644613b27ac4d066e2f130edac1fb6" + "tree": "92cf9f2dc610f43256df3a0bbdb419228f45cba9" }, "source_records": { "aarch64-apple-darwin": "source/aarch64-apple-darwin.json", @@ -174,19 +174,19 @@ "sha256": "5d74060f97e52dad69ab7567c16d6b6b34acfab846f468450d89e2394a8a69d5" }, { - "bytes": 156974, + "bytes": 160029, "path": "TeraFFI/source/aarch64-apple-darwin.json", - "sha256": "bdc7d432de293191bce93b7eca2489a41e8880691d36b0284ffc4a1ed9796f21" + "sha256": "140870c865fadd5ab210ead46d050da3b1de413fff9168e243505b59ca791e79" }, { - "bytes": 156899, + "bytes": 159954, "path": "TeraFFI/source/aarch64-apple-ios-sim.json", - "sha256": "abf6f430c7d998acd67bb8a10600bf8a72eb3397c976d3c0f827ae7e39b9bd7a" + "sha256": "ec4f2d5fc17fcb272f8f0fb398c097e6686597277b9c962a33f1d17e9637fbad" }, { - "bytes": 156869, + "bytes": 159924, "path": "TeraFFI/source/aarch64-apple-ios.json", - "sha256": "3c594f013ca7255d5136ad85842cee800a727297e5c3b9ba176358c44ab42b9c" + "sha256": "c7fd3be77d8aee5476967f830c73497ac438b1d7405c7d7ff8b3958ff9c15105" } ], "schema": "tera.installed-native-artifacts.v1" diff --git a/TeraFFI/source.lock b/TeraFFI/source.lock @@ -1,7 +1,7 @@ schema = "tera.installed-source.v1" repository = "https://github.com/radrootslabs/tera" -source_tree = "413ea1de23644613b27ac4d066e2f130edac1fb6" -manifest_sha256 = "fb64d51e76dd4ae5e6a68eced98dbb9a029edcebd23dc7e4d69e1226dd9464d8" +source_tree = "92cf9f2dc610f43256df3a0bbdb419228f45cba9" +manifest_sha256 = "247df8635a647a7ded386633f9abb3244f68fbec1c3f9ce797f84ba5aadca800" source_date_epoch = 1787871027 [foundation] diff --git a/TeraFFI/source/aarch64-apple-darwin.json b/TeraFFI/source/aarch64-apple-darwin.json @@ -385,7 +385,7 @@ "generator_profile_overrides": { "CARGO_PROFILE_DEV_DEBUG": "line-tables-only" }, - "path_sha256": "e4bfec31ceb26bfa330c30b81c526b8429ba650cf8a92c77e5c3971924187a16" + "path_sha256": "17c088ebe174bfc657ec5b68db8f513bca6942b5ce8ff5904fd59f44c181bb3d" }, "inspection_limits": { "maximum_inspection_seconds": 120, @@ -618,7 +618,7 @@ "generator_profile_overrides": { "CARGO_PROFILE_DEV_DEBUG": "line-tables-only" }, - "path_sha256": "e4bfec31ceb26bfa330c30b81c526b8429ba650cf8a92c77e5c3971924187a16" + "path_sha256": "17c088ebe174bfc657ec5b68db8f513bca6942b5ce8ff5904fd59f44c181bb3d" }, "inspection_limits": { "maximum_inspection_seconds": 120, @@ -2911,6 +2911,12 @@ "mode": "100644", "sha256": "12c9c0a59af7e79a745dffd036b8446923dc74e2407d96218fd21d4475c173b1" }, + "scripts/authored_source_inventory.py": { + "bytes": 3350, + "git_blob": "028db1cc7900eac390f78349ded8ab9184b12c6d", + "mode": "100644", + "sha256": "9f8009346ac7dcde2678a49c4a506770fa0264b4fcb8677deace675016bd5098" + }, "scripts/ffi-artifacts.sh": { "bytes": 210, "git_blob": "be7f39d8f358ae34087232abe318d13ab867471c", @@ -2959,6 +2965,30 @@ "mode": "100644", "sha256": "debbb8f694204d7d14c85820f894e31b18acf24cffbfeceab4f3563ef1fb0ce6" }, + "scripts/fixture_connections.py": { + "bytes": 4452, + "git_blob": "c8b8358faca2728cdb58b29e87ea324dbd97e598", + "mode": "100644", + "sha256": "c777d8a7aaf683d7942bbc2331e05501c1d962d4ae7e307df6b3071806defbb6" + }, + "scripts/fixture_test_policy.py": { + "bytes": 2053, + "git_blob": "00e61a2090c55a4902c1b1f31a9abd00a9c01f75", + "mode": "100644", + "sha256": "072557a9ab2ae72a759e3eca8a14bacfd4c42e2e9efad384b261ba84dced723a" + }, + "scripts/fixture_test_policy.v1.json": { + "bytes": 647, + "git_blob": "45c355ace834c0320c957104c0be1aa2c0a5b225", + "mode": "100644", + "sha256": "c987cb5b58d2e49fb543cd09005cdf4cba1f984270ffa0ea9f0e5bdfcc1b52ca" + }, + "scripts/fixture_tool_dispatch.py": { + "bytes": 14299, + "git_blob": "bc58617bdaf978f1f54af4abbfc07b4b670c58b2", + "mode": "100644", + "sha256": "fa860540d4792094905321f949e6a472b75ad24581c3ab0208635b490b24c90c" + }, "scripts/generate-project.sh": { "bytes": 2964, "git_blob": "00ea6ec5a05bcd3c6a62f73b18f82758c5ac5e17", @@ -3110,22 +3140,22 @@ "sha256": "087faddef16e99f10bb7ad7b39d649e7e410b558ed1f9f4f4e498ab861eea622" }, "scripts/local-social-fixture.py": { - "bytes": 100982, - "git_blob": "3d8c072afa7878ae3a40b5a9eaff5274b7c5f2f7", + "bytes": 100786, + "git_blob": "9b34ef770e1fba3c200702ef90a9cffa7febad1c", "mode": "100644", - "sha256": "9dbb354f4656e1827143033fcb475afb2f247c6c32263c71bfce3b9b6ede5843" + "sha256": "5c3b6bf1627be05cb10bdb9bce9bd156dc255a9c24e5c8d6e692fd9e7fb53be9" }, "scripts/local_social_test_support.py": { - "bytes": 582, - "git_blob": "8dee707fc3b0e18684ba46a89b46965826493b92", + "bytes": 693, + "git_blob": "45bdc6a37b0b68e28c3bd969ebbdb6c4efeaa39a", "mode": "100644", - "sha256": "1489ad0d008ce655bbebb6c172bf0cd2b82d115ab630b50ceaaa7b5f3f7d3301" + "sha256": "6a4a664aad3ad556caf94d9767c620a277ea6bac0cf6592249879a45e155de71" }, "scripts/maintainability_ratchet.py": { - "bytes": 11520, - "git_blob": "7f012730dcb646027baa084229f2193f7b1f421d", + "bytes": 14389, + "git_blob": "898a9e479a2aeb8500a720489c04ac2916d0e990", "mode": "100644", - "sha256": "fff99e755f35fa9f00708427b1b29290c0ecc2733c0ea070f5807ec7c37f6f11" + "sha256": "6c5a18e20e815272231004e8125d96d92a615ad147cba141fe23a86edb447298" }, "scripts/package_contract.py": { "bytes": 26077, @@ -3158,10 +3188,10 @@ "sha256": "af175b24907863f548d0d93a585ceab8d65a686e5152f70739384dd5b8ef2b96" }, "scripts/persona_attachment_selection.py": { - "bytes": 6266, - "git_blob": "a7daf9b0a35bed60804a64f517870e77cd7aae19", + "bytes": 6251, + "git_blob": "aa77a51254b09f9d0d2dfa58f1c8aaf1ac32b62d", "mode": "100644", - "sha256": "0fd26ba301a301caaba97d3bba314a9fe322c12a26e99f8fce5f272eb2cf39fc" + "sha256": "064824c06c5568c50d5e2a4e1c1f626fe6664c7e62eb5b3574d442477fed2500" }, "scripts/release-evidence.sh": { "bytes": 9305, @@ -3182,10 +3212,10 @@ "sha256": "1803572abdb2e22bedd275724edae5f09c523b132bc59086aacca0f5183905d0" }, "scripts/swift-quality.sh": { - "bytes": 2572, - "git_blob": "3a253f4e3b40a8b3429076898a805d53141df673", + "bytes": 1612, + "git_blob": "6f7d0df19c43308191a95fc82092a31a29ea2a32", "mode": "100755", - "sha256": "f4a6f0c865afc3c193d175de20e1d1a8b587ec1c2b8b99e1dbfa775d86292cfc" + "sha256": "05f756e7be225f4befc22b5c518d93872269b0aaf39dbc4cfe65b464d263eb91" }, "scripts/test_app_dependency_graph.py": { "bytes": 4007, @@ -3199,6 +3229,12 @@ "mode": "100644", "sha256": "168fb27e9412a4a49754cfb8748ae04422cd619f3a4f043e539d0e24dab1cb5c" }, + "scripts/test_authored_source_inventory.py": { + "bytes": 4164, + "git_blob": "cd951f8719d6815aad001d8e6c22138f5ce68caf", + "mode": "100644", + "sha256": "b20b7f74ba8ca48949df8861e0d8d2845a07223ed2a8407160d650e283e40c45" + }, "scripts/test_ffi_artifacts.py": { "bytes": 10184, "git_blob": "a1dd396b883ebe3c9933d19d9c2194103ed907b6", @@ -3229,6 +3265,24 @@ "mode": "100644", "sha256": "d54d1b569aa43460cbdc93e7bc246c1a886954d98cae1f20c81678d47b3427a3" }, + "scripts/test_fixture_lifetimes.py": { + "bytes": 13589, + "git_blob": "cdefd4ebf0749498cd61ad0a9df0922a69b085da", + "mode": "100644", + "sha256": "f678394a62d0f006ac3659a16a83cfaf691c734b39491985a0c6cd1929f07e4b" + }, + "scripts/test_fixture_test_policy.py": { + "bytes": 3881, + "git_blob": "a3732e85f0925ba0fd6383433eaac00a1c6440d8", + "mode": "100644", + "sha256": "2000f73cc33d905506d3a49077a5cc6691dcf61a88e20a7745885e1a37f95ebe" + }, + "scripts/test_fixture_tool_dispatch.py": { + "bytes": 15494, + "git_blob": "9cf703441a24c89bb7eabebb375fe011104bb6f9", + "mode": "100644", + "sha256": "4ab4d92f59e6731a33f1549a3bd3e2a4f3e173bd69faa898dabaf31960d14839" + }, "scripts/test_kotlin_smoke.py": { "bytes": 13269, "git_blob": "eeb4ed3c56d16a0cea55c43645834a12c64c933e", @@ -3248,22 +3302,22 @@ "sha256": "00a1b2412a5054bb5c527fc4a7261d6c4a8de946a10d1821616c79ae054d4d1a" }, "scripts/test_local_social_fixture.py": { - "bytes": 50502, - "git_blob": "8164e4c983bfef50bedcdb32599492158e179881", + "bytes": 50382, + "git_blob": "c6cf04b60d31192495c7761ea0e5898652651dc3", "mode": "100644", - "sha256": "81ff78918d1c7ef642f1ebc598d48ea0d8445ea31be783493a0d75581b0d73b1" + "sha256": "b7e968adc81a2acee590db121854c664e6036fb122b253d3b8ac988dc437a7d1" }, "scripts/test_local_social_relay.py": { - "bytes": 4454, - "git_blob": "1d2ab5387568effe45fcbc9ce2f4c0782272ab1e", + "bytes": 4458, + "git_blob": "eeb60952cdc9ce72a5f7c640616659fa70371285", "mode": "100644", - "sha256": "8e9fad61bc7d1c951502ecc4c1305db7bbf51d426cd5fd4c445443884509e5da" + "sha256": "c7751f390bc2c090e81fa760c1423fb0ca807f649c8df42753847206c9d3404c" }, "scripts/test_maintainability_ratchet.py": { - "bytes": 6308, - "git_blob": "7d1cb1bb3a154895640f2e6d83a6f8a6e29367f1", + "bytes": 10777, + "git_blob": "936b10182e14e3074ac0bd526782f75c4f616f7f", "mode": "100644", - "sha256": "1a5384a6109d495477dd7848a367c2452f87c36e46dbab2c20b98940a4d4fed6" + "sha256": "b1b9b0ca99ff58f4229807e81b86446089763d0cdea15ba96128d6bad020e366" }, "scripts/test_package_contract.py": { "bytes": 11243, @@ -3278,40 +3332,58 @@ "sha256": "651fc803fe3e3dd1636aec75f9ea79d81b9407317f93a02e4074a8369558bc33" }, "scripts/test_persona_attachment_selection.py": { - "bytes": 5355, - "git_blob": "13bd0cece4ea4c73c2807275fad51ebf6582590e", + "bytes": 5361, + "git_blob": "fa1fb5ea72928fd90107119252851146fb2b400a", "mode": "100644", - "sha256": "56797c8dd8b1e1efc3c1f469f86c542baecaf12db60bcc799585f475438379f5" + "sha256": "bc641e36a6b521e5a312a49b396c1dca7f583a1ff8354f3a413e2ea3a083ef6d" }, "scripts/test_release_evidence.py": { - "bytes": 14064, - "git_blob": "e2efe54531f0a7e2995fb61989f8616314d3d618", + "bytes": 27441, + "git_blob": "a95066e9268525e004017bc1a4a141d7054ed5ca", "mode": "100644", - "sha256": "446651dd8c2cd194fb0bba06c11149c56726f9f63ecaaead32073f0c574e8e24" + "sha256": "5ac8727655b13605f56c9dcbeb9f7da906a90a7e2aa5bc0cc79a8321860fdf05" }, "scripts/test_unittest_report.py": { - "bytes": 33055, - "git_blob": "1e2b8a07baa061d13b70221fa725a8872d9be924", + "bytes": 36439, + "git_blob": "059ec79c91f821c28efe738597630316597717d8", "mode": "100644", - "sha256": "a1b1ef1ac7533bad0af80bf66790a8954aa990b279d8f417bd15e040f3bfb521" + "sha256": "f7000503cdff47c06ecb68b1f715c3603dcedf65a14563d3cc143406b92fa0e3" + }, + "scripts/test_unittest_report_bootstrap.py": { + "bytes": 12139, + "git_blob": "fad35c7aef1cdf0de244934d1174e6c1175903a5", + "mode": "100644", + "sha256": "06b48ad25ad8fa3d2279553a76fbde5537634f08b7226c173502baa3cdfd6a20" + }, + "scripts/test_unittest_report_nested.py": { + "bytes": 36175, + "git_blob": "4c8f048e29f8d9350d98e4806bcc77c3bd9f5601", + "mode": "100644", + "sha256": "b9a8b3e8067e8ba0395c7d097b4148dc09a835a03efcb938b3b39647bd793d69" }, "scripts/test_xcode_selection.py": { - "bytes": 17439, - "git_blob": "371ea8d59714eef121871e96721ad11bccf5861d", + "bytes": 33648, + "git_blob": "d86ea61092b9b3cfb0b17a7e9e0d7b531d9f8029", "mode": "100644", - "sha256": "876409c92a48c45c5bd7e25accf9d6de8a754ee4182d03a4ad3fe8f187a1b094" + "sha256": "0cef3c9971d2745032377280fc1ebd588298429851538231d971060e9a037a46" }, "scripts/unittest_report.py": { - "bytes": 20276, - "git_blob": "319eb74779ee70a119e997979ac2d6a65d6b243b", + "bytes": 27817, + "git_blob": "2f9b3602f4619704d88fca6298d69dfe93ef2237", + "mode": "100644", + "sha256": "5cb8a6969c941446dc56a3a0097c858ad4650df1e911d34ef39cfb13ba109116" + }, + "scripts/unittest_report_origin.py": { + "bytes": 13972, + "git_blob": "3a9c7ded37c7b85c4f5643402ecc6fdb26595e8a", "mode": "100644", - "sha256": "c1bbdc3c0ad9df4679eb110f9e145b5e399e7e5f74298a56028f46ff1269ff98" + "sha256": "4b05c27408ebaa894ce7609c3cb26a7575f77f5f9650eb1f0ecd41c74620c376" }, "scripts/verify-package-contract.sh": { - "bytes": 1798, - "git_blob": "05fba60dd9a21de054208a2dde389f509013fc5e", + "bytes": 1066, + "git_blob": "ae52e06daaf95eaee3cb956441d344b8df754bc1", "mode": "100755", - "sha256": "94fea1e33e46f298112072326ff16f1a1c669ddb902c06809536cf71aa6a615e" + "sha256": "a3d6834b3a03a69ddeaa37fecc2c855ff06ffada39cec0aa16e01f611b895796" }, "scripts/xcode.sh": { "bytes": 24697, @@ -3326,13 +3398,13 @@ "sha256": "0164f2b369fdcf734f73bd1df2a2f5b2e05b7bbd94c2d3ae75869fff6d876140" }, "test-fixtures/legacy-identifiers.v1.json": { - "bytes": 197529, - "git_blob": "747843ea60a8f729ea42c216af9425a38683dd19", + "bytes": 197873, + "git_blob": "669dad52033afed14641ad0df6ccb6c08cba7f21", "mode": "100644", - "sha256": "e3349cf19d0dc2abf9ffbfc10b8c775c9cb692d175a5adfb3ca06de82d0b949e" + "sha256": "6024d6004038025a488ae68febad38b47ac56565d99df8618584346849333424" } }, "policy": "staged_inputs", - "tree": "413ea1de23644613b27ac4d066e2f130edac1fb6" + "tree": "92cf9f2dc610f43256df3a0bbdb419228f45cba9" } } diff --git a/TeraFFI/source/aarch64-apple-ios-sim.json b/TeraFFI/source/aarch64-apple-ios-sim.json @@ -387,7 +387,7 @@ "generator_profile_overrides": { "CARGO_PROFILE_DEV_DEBUG": "line-tables-only" }, - "path_sha256": "e4bfec31ceb26bfa330c30b81c526b8429ba650cf8a92c77e5c3971924187a16" + "path_sha256": "17c088ebe174bfc657ec5b68db8f513bca6942b5ce8ff5904fd59f44c181bb3d" }, "inspection_limits": { "maximum_inspection_seconds": 120, @@ -616,7 +616,7 @@ "generator_profile_overrides": { "CARGO_PROFILE_DEV_DEBUG": "line-tables-only" }, - "path_sha256": "e4bfec31ceb26bfa330c30b81c526b8429ba650cf8a92c77e5c3971924187a16" + "path_sha256": "17c088ebe174bfc657ec5b68db8f513bca6942b5ce8ff5904fd59f44c181bb3d" }, "inspection_limits": { "maximum_inspection_seconds": 120, @@ -2909,6 +2909,12 @@ "mode": "100644", "sha256": "12c9c0a59af7e79a745dffd036b8446923dc74e2407d96218fd21d4475c173b1" }, + "scripts/authored_source_inventory.py": { + "bytes": 3350, + "git_blob": "028db1cc7900eac390f78349ded8ab9184b12c6d", + "mode": "100644", + "sha256": "9f8009346ac7dcde2678a49c4a506770fa0264b4fcb8677deace675016bd5098" + }, "scripts/ffi-artifacts.sh": { "bytes": 210, "git_blob": "be7f39d8f358ae34087232abe318d13ab867471c", @@ -2957,6 +2963,30 @@ "mode": "100644", "sha256": "debbb8f694204d7d14c85820f894e31b18acf24cffbfeceab4f3563ef1fb0ce6" }, + "scripts/fixture_connections.py": { + "bytes": 4452, + "git_blob": "c8b8358faca2728cdb58b29e87ea324dbd97e598", + "mode": "100644", + "sha256": "c777d8a7aaf683d7942bbc2331e05501c1d962d4ae7e307df6b3071806defbb6" + }, + "scripts/fixture_test_policy.py": { + "bytes": 2053, + "git_blob": "00e61a2090c55a4902c1b1f31a9abd00a9c01f75", + "mode": "100644", + "sha256": "072557a9ab2ae72a759e3eca8a14bacfd4c42e2e9efad384b261ba84dced723a" + }, + "scripts/fixture_test_policy.v1.json": { + "bytes": 647, + "git_blob": "45c355ace834c0320c957104c0be1aa2c0a5b225", + "mode": "100644", + "sha256": "c987cb5b58d2e49fb543cd09005cdf4cba1f984270ffa0ea9f0e5bdfcc1b52ca" + }, + "scripts/fixture_tool_dispatch.py": { + "bytes": 14299, + "git_blob": "bc58617bdaf978f1f54af4abbfc07b4b670c58b2", + "mode": "100644", + "sha256": "fa860540d4792094905321f949e6a472b75ad24581c3ab0208635b490b24c90c" + }, "scripts/generate-project.sh": { "bytes": 2964, "git_blob": "00ea6ec5a05bcd3c6a62f73b18f82758c5ac5e17", @@ -3108,22 +3138,22 @@ "sha256": "087faddef16e99f10bb7ad7b39d649e7e410b558ed1f9f4f4e498ab861eea622" }, "scripts/local-social-fixture.py": { - "bytes": 100982, - "git_blob": "3d8c072afa7878ae3a40b5a9eaff5274b7c5f2f7", + "bytes": 100786, + "git_blob": "9b34ef770e1fba3c200702ef90a9cffa7febad1c", "mode": "100644", - "sha256": "9dbb354f4656e1827143033fcb475afb2f247c6c32263c71bfce3b9b6ede5843" + "sha256": "5c3b6bf1627be05cb10bdb9bce9bd156dc255a9c24e5c8d6e692fd9e7fb53be9" }, "scripts/local_social_test_support.py": { - "bytes": 582, - "git_blob": "8dee707fc3b0e18684ba46a89b46965826493b92", + "bytes": 693, + "git_blob": "45bdc6a37b0b68e28c3bd969ebbdb6c4efeaa39a", "mode": "100644", - "sha256": "1489ad0d008ce655bbebb6c172bf0cd2b82d115ab630b50ceaaa7b5f3f7d3301" + "sha256": "6a4a664aad3ad556caf94d9767c620a277ea6bac0cf6592249879a45e155de71" }, "scripts/maintainability_ratchet.py": { - "bytes": 11520, - "git_blob": "7f012730dcb646027baa084229f2193f7b1f421d", + "bytes": 14389, + "git_blob": "898a9e479a2aeb8500a720489c04ac2916d0e990", "mode": "100644", - "sha256": "fff99e755f35fa9f00708427b1b29290c0ecc2733c0ea070f5807ec7c37f6f11" + "sha256": "6c5a18e20e815272231004e8125d96d92a615ad147cba141fe23a86edb447298" }, "scripts/package_contract.py": { "bytes": 26077, @@ -3156,10 +3186,10 @@ "sha256": "af175b24907863f548d0d93a585ceab8d65a686e5152f70739384dd5b8ef2b96" }, "scripts/persona_attachment_selection.py": { - "bytes": 6266, - "git_blob": "a7daf9b0a35bed60804a64f517870e77cd7aae19", + "bytes": 6251, + "git_blob": "aa77a51254b09f9d0d2dfa58f1c8aaf1ac32b62d", "mode": "100644", - "sha256": "0fd26ba301a301caaba97d3bba314a9fe322c12a26e99f8fce5f272eb2cf39fc" + "sha256": "064824c06c5568c50d5e2a4e1c1f626fe6664c7e62eb5b3574d442477fed2500" }, "scripts/release-evidence.sh": { "bytes": 9305, @@ -3180,10 +3210,10 @@ "sha256": "1803572abdb2e22bedd275724edae5f09c523b132bc59086aacca0f5183905d0" }, "scripts/swift-quality.sh": { - "bytes": 2572, - "git_blob": "3a253f4e3b40a8b3429076898a805d53141df673", + "bytes": 1612, + "git_blob": "6f7d0df19c43308191a95fc82092a31a29ea2a32", "mode": "100755", - "sha256": "f4a6f0c865afc3c193d175de20e1d1a8b587ec1c2b8b99e1dbfa775d86292cfc" + "sha256": "05f756e7be225f4befc22b5c518d93872269b0aaf39dbc4cfe65b464d263eb91" }, "scripts/test_app_dependency_graph.py": { "bytes": 4007, @@ -3197,6 +3227,12 @@ "mode": "100644", "sha256": "168fb27e9412a4a49754cfb8748ae04422cd619f3a4f043e539d0e24dab1cb5c" }, + "scripts/test_authored_source_inventory.py": { + "bytes": 4164, + "git_blob": "cd951f8719d6815aad001d8e6c22138f5ce68caf", + "mode": "100644", + "sha256": "b20b7f74ba8ca48949df8861e0d8d2845a07223ed2a8407160d650e283e40c45" + }, "scripts/test_ffi_artifacts.py": { "bytes": 10184, "git_blob": "a1dd396b883ebe3c9933d19d9c2194103ed907b6", @@ -3227,6 +3263,24 @@ "mode": "100644", "sha256": "d54d1b569aa43460cbdc93e7bc246c1a886954d98cae1f20c81678d47b3427a3" }, + "scripts/test_fixture_lifetimes.py": { + "bytes": 13589, + "git_blob": "cdefd4ebf0749498cd61ad0a9df0922a69b085da", + "mode": "100644", + "sha256": "f678394a62d0f006ac3659a16a83cfaf691c734b39491985a0c6cd1929f07e4b" + }, + "scripts/test_fixture_test_policy.py": { + "bytes": 3881, + "git_blob": "a3732e85f0925ba0fd6383433eaac00a1c6440d8", + "mode": "100644", + "sha256": "2000f73cc33d905506d3a49077a5cc6691dcf61a88e20a7745885e1a37f95ebe" + }, + "scripts/test_fixture_tool_dispatch.py": { + "bytes": 15494, + "git_blob": "9cf703441a24c89bb7eabebb375fe011104bb6f9", + "mode": "100644", + "sha256": "4ab4d92f59e6731a33f1549a3bd3e2a4f3e173bd69faa898dabaf31960d14839" + }, "scripts/test_kotlin_smoke.py": { "bytes": 13269, "git_blob": "eeb4ed3c56d16a0cea55c43645834a12c64c933e", @@ -3246,22 +3300,22 @@ "sha256": "00a1b2412a5054bb5c527fc4a7261d6c4a8de946a10d1821616c79ae054d4d1a" }, "scripts/test_local_social_fixture.py": { - "bytes": 50502, - "git_blob": "8164e4c983bfef50bedcdb32599492158e179881", + "bytes": 50382, + "git_blob": "c6cf04b60d31192495c7761ea0e5898652651dc3", "mode": "100644", - "sha256": "81ff78918d1c7ef642f1ebc598d48ea0d8445ea31be783493a0d75581b0d73b1" + "sha256": "b7e968adc81a2acee590db121854c664e6036fb122b253d3b8ac988dc437a7d1" }, "scripts/test_local_social_relay.py": { - "bytes": 4454, - "git_blob": "1d2ab5387568effe45fcbc9ce2f4c0782272ab1e", + "bytes": 4458, + "git_blob": "eeb60952cdc9ce72a5f7c640616659fa70371285", "mode": "100644", - "sha256": "8e9fad61bc7d1c951502ecc4c1305db7bbf51d426cd5fd4c445443884509e5da" + "sha256": "c7751f390bc2c090e81fa760c1423fb0ca807f649c8df42753847206c9d3404c" }, "scripts/test_maintainability_ratchet.py": { - "bytes": 6308, - "git_blob": "7d1cb1bb3a154895640f2e6d83a6f8a6e29367f1", + "bytes": 10777, + "git_blob": "936b10182e14e3074ac0bd526782f75c4f616f7f", "mode": "100644", - "sha256": "1a5384a6109d495477dd7848a367c2452f87c36e46dbab2c20b98940a4d4fed6" + "sha256": "b1b9b0ca99ff58f4229807e81b86446089763d0cdea15ba96128d6bad020e366" }, "scripts/test_package_contract.py": { "bytes": 11243, @@ -3276,40 +3330,58 @@ "sha256": "651fc803fe3e3dd1636aec75f9ea79d81b9407317f93a02e4074a8369558bc33" }, "scripts/test_persona_attachment_selection.py": { - "bytes": 5355, - "git_blob": "13bd0cece4ea4c73c2807275fad51ebf6582590e", + "bytes": 5361, + "git_blob": "fa1fb5ea72928fd90107119252851146fb2b400a", "mode": "100644", - "sha256": "56797c8dd8b1e1efc3c1f469f86c542baecaf12db60bcc799585f475438379f5" + "sha256": "bc641e36a6b521e5a312a49b396c1dca7f583a1ff8354f3a413e2ea3a083ef6d" }, "scripts/test_release_evidence.py": { - "bytes": 14064, - "git_blob": "e2efe54531f0a7e2995fb61989f8616314d3d618", + "bytes": 27441, + "git_blob": "a95066e9268525e004017bc1a4a141d7054ed5ca", "mode": "100644", - "sha256": "446651dd8c2cd194fb0bba06c11149c56726f9f63ecaaead32073f0c574e8e24" + "sha256": "5ac8727655b13605f56c9dcbeb9f7da906a90a7e2aa5bc0cc79a8321860fdf05" }, "scripts/test_unittest_report.py": { - "bytes": 33055, - "git_blob": "1e2b8a07baa061d13b70221fa725a8872d9be924", + "bytes": 36439, + "git_blob": "059ec79c91f821c28efe738597630316597717d8", "mode": "100644", - "sha256": "a1b1ef1ac7533bad0af80bf66790a8954aa990b279d8f417bd15e040f3bfb521" + "sha256": "f7000503cdff47c06ecb68b1f715c3603dcedf65a14563d3cc143406b92fa0e3" + }, + "scripts/test_unittest_report_bootstrap.py": { + "bytes": 12139, + "git_blob": "fad35c7aef1cdf0de244934d1174e6c1175903a5", + "mode": "100644", + "sha256": "06b48ad25ad8fa3d2279553a76fbde5537634f08b7226c173502baa3cdfd6a20" + }, + "scripts/test_unittest_report_nested.py": { + "bytes": 36175, + "git_blob": "4c8f048e29f8d9350d98e4806bcc77c3bd9f5601", + "mode": "100644", + "sha256": "b9a8b3e8067e8ba0395c7d097b4148dc09a835a03efcb938b3b39647bd793d69" }, "scripts/test_xcode_selection.py": { - "bytes": 17439, - "git_blob": "371ea8d59714eef121871e96721ad11bccf5861d", + "bytes": 33648, + "git_blob": "d86ea61092b9b3cfb0b17a7e9e0d7b531d9f8029", "mode": "100644", - "sha256": "876409c92a48c45c5bd7e25accf9d6de8a754ee4182d03a4ad3fe8f187a1b094" + "sha256": "0cef3c9971d2745032377280fc1ebd588298429851538231d971060e9a037a46" }, "scripts/unittest_report.py": { - "bytes": 20276, - "git_blob": "319eb74779ee70a119e997979ac2d6a65d6b243b", + "bytes": 27817, + "git_blob": "2f9b3602f4619704d88fca6298d69dfe93ef2237", + "mode": "100644", + "sha256": "5cb8a6969c941446dc56a3a0097c858ad4650df1e911d34ef39cfb13ba109116" + }, + "scripts/unittest_report_origin.py": { + "bytes": 13972, + "git_blob": "3a9c7ded37c7b85c4f5643402ecc6fdb26595e8a", "mode": "100644", - "sha256": "c1bbdc3c0ad9df4679eb110f9e145b5e399e7e5f74298a56028f46ff1269ff98" + "sha256": "4b05c27408ebaa894ce7609c3cb26a7575f77f5f9650eb1f0ecd41c74620c376" }, "scripts/verify-package-contract.sh": { - "bytes": 1798, - "git_blob": "05fba60dd9a21de054208a2dde389f509013fc5e", + "bytes": 1066, + "git_blob": "ae52e06daaf95eaee3cb956441d344b8df754bc1", "mode": "100755", - "sha256": "94fea1e33e46f298112072326ff16f1a1c669ddb902c06809536cf71aa6a615e" + "sha256": "a3d6834b3a03a69ddeaa37fecc2c855ff06ffada39cec0aa16e01f611b895796" }, "scripts/xcode.sh": { "bytes": 24697, @@ -3324,13 +3396,13 @@ "sha256": "0164f2b369fdcf734f73bd1df2a2f5b2e05b7bbd94c2d3ae75869fff6d876140" }, "test-fixtures/legacy-identifiers.v1.json": { - "bytes": 197529, - "git_blob": "747843ea60a8f729ea42c216af9425a38683dd19", + "bytes": 197873, + "git_blob": "669dad52033afed14641ad0df6ccb6c08cba7f21", "mode": "100644", - "sha256": "e3349cf19d0dc2abf9ffbfc10b8c775c9cb692d175a5adfb3ca06de82d0b949e" + "sha256": "6024d6004038025a488ae68febad38b47ac56565d99df8618584346849333424" } }, "policy": "staged_inputs", - "tree": "413ea1de23644613b27ac4d066e2f130edac1fb6" + "tree": "92cf9f2dc610f43256df3a0bbdb419228f45cba9" } } diff --git a/TeraFFI/source/aarch64-apple-ios.json b/TeraFFI/source/aarch64-apple-ios.json @@ -387,7 +387,7 @@ "generator_profile_overrides": { "CARGO_PROFILE_DEV_DEBUG": "line-tables-only" }, - "path_sha256": "e4bfec31ceb26bfa330c30b81c526b8429ba650cf8a92c77e5c3971924187a16" + "path_sha256": "17c088ebe174bfc657ec5b68db8f513bca6942b5ce8ff5904fd59f44c181bb3d" }, "inspection_limits": { "maximum_inspection_seconds": 120, @@ -616,7 +616,7 @@ "generator_profile_overrides": { "CARGO_PROFILE_DEV_DEBUG": "line-tables-only" }, - "path_sha256": "e4bfec31ceb26bfa330c30b81c526b8429ba650cf8a92c77e5c3971924187a16" + "path_sha256": "17c088ebe174bfc657ec5b68db8f513bca6942b5ce8ff5904fd59f44c181bb3d" }, "inspection_limits": { "maximum_inspection_seconds": 120, @@ -2909,6 +2909,12 @@ "mode": "100644", "sha256": "12c9c0a59af7e79a745dffd036b8446923dc74e2407d96218fd21d4475c173b1" }, + "scripts/authored_source_inventory.py": { + "bytes": 3350, + "git_blob": "028db1cc7900eac390f78349ded8ab9184b12c6d", + "mode": "100644", + "sha256": "9f8009346ac7dcde2678a49c4a506770fa0264b4fcb8677deace675016bd5098" + }, "scripts/ffi-artifacts.sh": { "bytes": 210, "git_blob": "be7f39d8f358ae34087232abe318d13ab867471c", @@ -2957,6 +2963,30 @@ "mode": "100644", "sha256": "debbb8f694204d7d14c85820f894e31b18acf24cffbfeceab4f3563ef1fb0ce6" }, + "scripts/fixture_connections.py": { + "bytes": 4452, + "git_blob": "c8b8358faca2728cdb58b29e87ea324dbd97e598", + "mode": "100644", + "sha256": "c777d8a7aaf683d7942bbc2331e05501c1d962d4ae7e307df6b3071806defbb6" + }, + "scripts/fixture_test_policy.py": { + "bytes": 2053, + "git_blob": "00e61a2090c55a4902c1b1f31a9abd00a9c01f75", + "mode": "100644", + "sha256": "072557a9ab2ae72a759e3eca8a14bacfd4c42e2e9efad384b261ba84dced723a" + }, + "scripts/fixture_test_policy.v1.json": { + "bytes": 647, + "git_blob": "45c355ace834c0320c957104c0be1aa2c0a5b225", + "mode": "100644", + "sha256": "c987cb5b58d2e49fb543cd09005cdf4cba1f984270ffa0ea9f0e5bdfcc1b52ca" + }, + "scripts/fixture_tool_dispatch.py": { + "bytes": 14299, + "git_blob": "bc58617bdaf978f1f54af4abbfc07b4b670c58b2", + "mode": "100644", + "sha256": "fa860540d4792094905321f949e6a472b75ad24581c3ab0208635b490b24c90c" + }, "scripts/generate-project.sh": { "bytes": 2964, "git_blob": "00ea6ec5a05bcd3c6a62f73b18f82758c5ac5e17", @@ -3108,22 +3138,22 @@ "sha256": "087faddef16e99f10bb7ad7b39d649e7e410b558ed1f9f4f4e498ab861eea622" }, "scripts/local-social-fixture.py": { - "bytes": 100982, - "git_blob": "3d8c072afa7878ae3a40b5a9eaff5274b7c5f2f7", + "bytes": 100786, + "git_blob": "9b34ef770e1fba3c200702ef90a9cffa7febad1c", "mode": "100644", - "sha256": "9dbb354f4656e1827143033fcb475afb2f247c6c32263c71bfce3b9b6ede5843" + "sha256": "5c3b6bf1627be05cb10bdb9bce9bd156dc255a9c24e5c8d6e692fd9e7fb53be9" }, "scripts/local_social_test_support.py": { - "bytes": 582, - "git_blob": "8dee707fc3b0e18684ba46a89b46965826493b92", + "bytes": 693, + "git_blob": "45bdc6a37b0b68e28c3bd969ebbdb6c4efeaa39a", "mode": "100644", - "sha256": "1489ad0d008ce655bbebb6c172bf0cd2b82d115ab630b50ceaaa7b5f3f7d3301" + "sha256": "6a4a664aad3ad556caf94d9767c620a277ea6bac0cf6592249879a45e155de71" }, "scripts/maintainability_ratchet.py": { - "bytes": 11520, - "git_blob": "7f012730dcb646027baa084229f2193f7b1f421d", + "bytes": 14389, + "git_blob": "898a9e479a2aeb8500a720489c04ac2916d0e990", "mode": "100644", - "sha256": "fff99e755f35fa9f00708427b1b29290c0ecc2733c0ea070f5807ec7c37f6f11" + "sha256": "6c5a18e20e815272231004e8125d96d92a615ad147cba141fe23a86edb447298" }, "scripts/package_contract.py": { "bytes": 26077, @@ -3156,10 +3186,10 @@ "sha256": "af175b24907863f548d0d93a585ceab8d65a686e5152f70739384dd5b8ef2b96" }, "scripts/persona_attachment_selection.py": { - "bytes": 6266, - "git_blob": "a7daf9b0a35bed60804a64f517870e77cd7aae19", + "bytes": 6251, + "git_blob": "aa77a51254b09f9d0d2dfa58f1c8aaf1ac32b62d", "mode": "100644", - "sha256": "0fd26ba301a301caaba97d3bba314a9fe322c12a26e99f8fce5f272eb2cf39fc" + "sha256": "064824c06c5568c50d5e2a4e1c1f626fe6664c7e62eb5b3574d442477fed2500" }, "scripts/release-evidence.sh": { "bytes": 9305, @@ -3180,10 +3210,10 @@ "sha256": "1803572abdb2e22bedd275724edae5f09c523b132bc59086aacca0f5183905d0" }, "scripts/swift-quality.sh": { - "bytes": 2572, - "git_blob": "3a253f4e3b40a8b3429076898a805d53141df673", + "bytes": 1612, + "git_blob": "6f7d0df19c43308191a95fc82092a31a29ea2a32", "mode": "100755", - "sha256": "f4a6f0c865afc3c193d175de20e1d1a8b587ec1c2b8b99e1dbfa775d86292cfc" + "sha256": "05f756e7be225f4befc22b5c518d93872269b0aaf39dbc4cfe65b464d263eb91" }, "scripts/test_app_dependency_graph.py": { "bytes": 4007, @@ -3197,6 +3227,12 @@ "mode": "100644", "sha256": "168fb27e9412a4a49754cfb8748ae04422cd619f3a4f043e539d0e24dab1cb5c" }, + "scripts/test_authored_source_inventory.py": { + "bytes": 4164, + "git_blob": "cd951f8719d6815aad001d8e6c22138f5ce68caf", + "mode": "100644", + "sha256": "b20b7f74ba8ca48949df8861e0d8d2845a07223ed2a8407160d650e283e40c45" + }, "scripts/test_ffi_artifacts.py": { "bytes": 10184, "git_blob": "a1dd396b883ebe3c9933d19d9c2194103ed907b6", @@ -3227,6 +3263,24 @@ "mode": "100644", "sha256": "d54d1b569aa43460cbdc93e7bc246c1a886954d98cae1f20c81678d47b3427a3" }, + "scripts/test_fixture_lifetimes.py": { + "bytes": 13589, + "git_blob": "cdefd4ebf0749498cd61ad0a9df0922a69b085da", + "mode": "100644", + "sha256": "f678394a62d0f006ac3659a16a83cfaf691c734b39491985a0c6cd1929f07e4b" + }, + "scripts/test_fixture_test_policy.py": { + "bytes": 3881, + "git_blob": "a3732e85f0925ba0fd6383433eaac00a1c6440d8", + "mode": "100644", + "sha256": "2000f73cc33d905506d3a49077a5cc6691dcf61a88e20a7745885e1a37f95ebe" + }, + "scripts/test_fixture_tool_dispatch.py": { + "bytes": 15494, + "git_blob": "9cf703441a24c89bb7eabebb375fe011104bb6f9", + "mode": "100644", + "sha256": "4ab4d92f59e6731a33f1549a3bd3e2a4f3e173bd69faa898dabaf31960d14839" + }, "scripts/test_kotlin_smoke.py": { "bytes": 13269, "git_blob": "eeb4ed3c56d16a0cea55c43645834a12c64c933e", @@ -3246,22 +3300,22 @@ "sha256": "00a1b2412a5054bb5c527fc4a7261d6c4a8de946a10d1821616c79ae054d4d1a" }, "scripts/test_local_social_fixture.py": { - "bytes": 50502, - "git_blob": "8164e4c983bfef50bedcdb32599492158e179881", + "bytes": 50382, + "git_blob": "c6cf04b60d31192495c7761ea0e5898652651dc3", "mode": "100644", - "sha256": "81ff78918d1c7ef642f1ebc598d48ea0d8445ea31be783493a0d75581b0d73b1" + "sha256": "b7e968adc81a2acee590db121854c664e6036fb122b253d3b8ac988dc437a7d1" }, "scripts/test_local_social_relay.py": { - "bytes": 4454, - "git_blob": "1d2ab5387568effe45fcbc9ce2f4c0782272ab1e", + "bytes": 4458, + "git_blob": "eeb60952cdc9ce72a5f7c640616659fa70371285", "mode": "100644", - "sha256": "8e9fad61bc7d1c951502ecc4c1305db7bbf51d426cd5fd4c445443884509e5da" + "sha256": "c7751f390bc2c090e81fa760c1423fb0ca807f649c8df42753847206c9d3404c" }, "scripts/test_maintainability_ratchet.py": { - "bytes": 6308, - "git_blob": "7d1cb1bb3a154895640f2e6d83a6f8a6e29367f1", + "bytes": 10777, + "git_blob": "936b10182e14e3074ac0bd526782f75c4f616f7f", "mode": "100644", - "sha256": "1a5384a6109d495477dd7848a367c2452f87c36e46dbab2c20b98940a4d4fed6" + "sha256": "b1b9b0ca99ff58f4229807e81b86446089763d0cdea15ba96128d6bad020e366" }, "scripts/test_package_contract.py": { "bytes": 11243, @@ -3276,40 +3330,58 @@ "sha256": "651fc803fe3e3dd1636aec75f9ea79d81b9407317f93a02e4074a8369558bc33" }, "scripts/test_persona_attachment_selection.py": { - "bytes": 5355, - "git_blob": "13bd0cece4ea4c73c2807275fad51ebf6582590e", + "bytes": 5361, + "git_blob": "fa1fb5ea72928fd90107119252851146fb2b400a", "mode": "100644", - "sha256": "56797c8dd8b1e1efc3c1f469f86c542baecaf12db60bcc799585f475438379f5" + "sha256": "bc641e36a6b521e5a312a49b396c1dca7f583a1ff8354f3a413e2ea3a083ef6d" }, "scripts/test_release_evidence.py": { - "bytes": 14064, - "git_blob": "e2efe54531f0a7e2995fb61989f8616314d3d618", + "bytes": 27441, + "git_blob": "a95066e9268525e004017bc1a4a141d7054ed5ca", "mode": "100644", - "sha256": "446651dd8c2cd194fb0bba06c11149c56726f9f63ecaaead32073f0c574e8e24" + "sha256": "5ac8727655b13605f56c9dcbeb9f7da906a90a7e2aa5bc0cc79a8321860fdf05" }, "scripts/test_unittest_report.py": { - "bytes": 33055, - "git_blob": "1e2b8a07baa061d13b70221fa725a8872d9be924", + "bytes": 36439, + "git_blob": "059ec79c91f821c28efe738597630316597717d8", "mode": "100644", - "sha256": "a1b1ef1ac7533bad0af80bf66790a8954aa990b279d8f417bd15e040f3bfb521" + "sha256": "f7000503cdff47c06ecb68b1f715c3603dcedf65a14563d3cc143406b92fa0e3" + }, + "scripts/test_unittest_report_bootstrap.py": { + "bytes": 12139, + "git_blob": "fad35c7aef1cdf0de244934d1174e6c1175903a5", + "mode": "100644", + "sha256": "06b48ad25ad8fa3d2279553a76fbde5537634f08b7226c173502baa3cdfd6a20" + }, + "scripts/test_unittest_report_nested.py": { + "bytes": 36175, + "git_blob": "4c8f048e29f8d9350d98e4806bcc77c3bd9f5601", + "mode": "100644", + "sha256": "b9a8b3e8067e8ba0395c7d097b4148dc09a835a03efcb938b3b39647bd793d69" }, "scripts/test_xcode_selection.py": { - "bytes": 17439, - "git_blob": "371ea8d59714eef121871e96721ad11bccf5861d", + "bytes": 33648, + "git_blob": "d86ea61092b9b3cfb0b17a7e9e0d7b531d9f8029", "mode": "100644", - "sha256": "876409c92a48c45c5bd7e25accf9d6de8a754ee4182d03a4ad3fe8f187a1b094" + "sha256": "0cef3c9971d2745032377280fc1ebd588298429851538231d971060e9a037a46" }, "scripts/unittest_report.py": { - "bytes": 20276, - "git_blob": "319eb74779ee70a119e997979ac2d6a65d6b243b", + "bytes": 27817, + "git_blob": "2f9b3602f4619704d88fca6298d69dfe93ef2237", + "mode": "100644", + "sha256": "5cb8a6969c941446dc56a3a0097c858ad4650df1e911d34ef39cfb13ba109116" + }, + "scripts/unittest_report_origin.py": { + "bytes": 13972, + "git_blob": "3a9c7ded37c7b85c4f5643402ecc6fdb26595e8a", "mode": "100644", - "sha256": "c1bbdc3c0ad9df4679eb110f9e145b5e399e7e5f74298a56028f46ff1269ff98" + "sha256": "4b05c27408ebaa894ce7609c3cb26a7575f77f5f9650eb1f0ecd41c74620c376" }, "scripts/verify-package-contract.sh": { - "bytes": 1798, - "git_blob": "05fba60dd9a21de054208a2dde389f509013fc5e", + "bytes": 1066, + "git_blob": "ae52e06daaf95eaee3cb956441d344b8df754bc1", "mode": "100755", - "sha256": "94fea1e33e46f298112072326ff16f1a1c669ddb902c06809536cf71aa6a615e" + "sha256": "a3d6834b3a03a69ddeaa37fecc2c855ff06ffada39cec0aa16e01f611b895796" }, "scripts/xcode.sh": { "bytes": 24697, @@ -3324,13 +3396,13 @@ "sha256": "0164f2b369fdcf734f73bd1df2a2f5b2e05b7bbd94c2d3ae75869fff6d876140" }, "test-fixtures/legacy-identifiers.v1.json": { - "bytes": 197529, - "git_blob": "747843ea60a8f729ea42c216af9425a38683dd19", + "bytes": 197873, + "git_blob": "669dad52033afed14641ad0df6ccb6c08cba7f21", "mode": "100644", - "sha256": "e3349cf19d0dc2abf9ffbfc10b8c775c9cb692d175a5adfb3ca06de82d0b949e" + "sha256": "6024d6004038025a488ae68febad38b47ac56565d99df8618584346849333424" } }, "policy": "staged_inputs", - "tree": "413ea1de23644613b27ac4d066e2f130edac1fb6" + "tree": "92cf9f2dc610f43256df3a0bbdb419228f45cba9" } } diff --git a/release/provenance.json b/release/provenance.json @@ -2,7 +2,7 @@ "artifacts": { "app_api_sha256": "020924097c0d7efc33128cb8fd3d3b2026d95f57c44da71880e585aff80f070b", "ffi_api_sha256": "5d74060f97e52dad69ab7567c16d6b6b34acfab846f468450d89e2394a8a69d5", - "ffi_provenance_sha256": "fb64d51e76dd4ae5e6a68eced98dbb9a029edcebd23dc7e4d69e1226dd9464d8", + "ffi_provenance_sha256": "247df8635a647a7ded386633f9abb3244f68fbec1c3f9ce797f84ba5aadca800", "info_plist_sha256": "b55010dd73b34bf5ad910a888be0ea1ae45f2a5c14329fa6a57059a317bdbd42", "privacy_manifest_sha256": "0f2103257ba596fbcaeb1d6a4dffe3ae021e84c010178852c6bf500a8cfb0f19", "sbom_sha256": "f0a15c2c9ad7799539fbc52fd1fdbcf17f4046b6f86cfe0a9b8980adc0079e10", @@ -1258,7 +1258,7 @@ "lib_revision": "189c49b74b4bafc142b00b76b296477931139e72", "source_date_epoch": 1787871027, "swift_package_lock_sha256": "8aec408132602a249111a65903c985cca5898ec316a45916d88ba756f08ea40d", - "tera_ffi_source_tree": "413ea1de23644613b27ac4d066e2f130edac1fb6", + "tera_ffi_source_tree": "92cf9f2dc610f43256df3a0bbdb419228f45cba9", "xcode_package_lock_sha256": "3656728f8251f3ea644aa150d154819766470ca7bd4f85b7c51fdf301d361d16" }, "version": "0.1.0-alpha" diff --git a/scripts/authored_source_inventory.py b/scripts/authored_source_inventory.py @@ -0,0 +1,104 @@ +"""One authored inventory for standalone quality, tests and the size ratchet.""" + +from __future__ import annotations + +import argparse +import os +import sys +from pathlib import Path + +SWIFT_ROOTS = ( + Path("Tera"), + Path("TeraPublicAPITests"), + Path("TeraTests"), + Path("TeraUITests"), + Path("scripts/legacy_fixture_writers"), +) +PYTHON_ROOT = Path("scripts") +GENERATED_SWIFT = Path("Tera/Generated") +IGNORED_DIRECTORIES = frozenset( + ("__pycache__", ".venv", ".pytest_cache", ".ruff_cache", ".mypy_cache") +) + + +class InventoryError(ValueError): + """An authored source root cannot be inventoried safely.""" + + +def _walk_error(error: OSError) -> None: + raise InventoryError("authored source inventory cannot be read") from error + + +def _regular(path: Path) -> Path: + if path.is_symlink() or not path.is_file(): + raise InventoryError("authored source is not a regular file") + return path + + +def _directories(root: Path, current: Path, names: list[str]) -> list[str]: + retained = [] + for name in sorted(names): + path = current / name + if name in IGNORED_DIRECTORIES or path.relative_to(root) == GENERATED_SWIFT: + continue + if path.is_symlink(): + raise InventoryError("authored source directory is a link") + retained.append(name) + return retained + + +def _files(root: Path, relative: Path, suffix: str) -> list[Path]: + directory = root / relative + if directory.is_symlink() or not directory.is_dir(): + raise InventoryError("authored source root is unavailable") + found = [] + for current, directories, filenames in os.walk(directory, onerror=_walk_error): + current = Path(current) + directories[:] = _directories(root, current, directories) + found.extend( + _regular(current / name) + for name in sorted(filenames) + if name.endswith(suffix) + ) + return found + + +def source_files(root: Path) -> tuple[list[Path], list[Path]]: + swift = [_regular(root / "Package.swift")] + for relative in SWIFT_ROOTS: + swift.extend(_files(root, relative, ".swift")) + python = _files(root, PYTHON_ROOT, ".py") + if not python: + raise InventoryError("authored Python source inventory is empty") + return sorted(swift), sorted(python) + + +def command(root: Path, lane: str) -> list[str]: + _, python = source_files(root) + paths = [path.relative_to(root).as_posix() for path in python] + if lane == "test": + tests = [path for path in paths if Path(path).name.startswith("test_")] + if not tests: + raise InventoryError("authored test inventory is empty") + return [sys.executable, "-m", "unittest", *tests] + arguments = ["format", "--check"] if lane == "format" else ["check"] + return ["ruff", *arguments, *paths] + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("lane", choices=("format", "lint", "test")) + arguments = parser.parse_args() + root = Path(__file__).resolve().parent.parent + try: + argv = command(root, arguments.lane) + except (InventoryError, OSError) as error: + print(f"authored inventory: {error}", file=sys.stderr) + return 1 + os.chdir(root) + os.execvp(argv[0], argv) + return 1 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/fixture_connections.py b/scripts/fixture_connections.py @@ -0,0 +1,144 @@ +"""Elapsed read bounds and ownership for the isolated loopback fixture.""" + +from __future__ import annotations + +import http.server +import io +import socket +import threading +import time + +READ_SECONDS = 15 +TEARDOWN_SECONDS = 5 +MAX_HANDLERS = 32 + + +def deadline() -> float: + return time.monotonic() + READ_SECONDS + + +def remaining(until: float) -> float: + value = until - time.monotonic() + if value <= 0: + raise TimeoutError("fixture read deadline exceeded") + return value + + +def recv_exact(stream, length: int, until=None) -> bytes: + until = deadline() if until is None else until + value = bytearray() + while len(value) < length: + stream.settimeout(remaining(until)) + chunk = stream.recv(length - len(value)) + if not chunk: + raise ConnectionError("socket closed") + value.extend(chunk) + return bytes(value) + + +def recv_until(stream, marker: bytes, maximum: int) -> bytes: + until = deadline() + value = bytearray() + while marker not in value: + stream.settimeout(remaining(until)) + chunk = stream.recv(4096) + if not chunk: + raise ConnectionError("socket closed") + value.extend(chunk) + if len(value) > maximum: + raise ValueError("request too large") + return bytes(value) + + +class DeadlineReader(io.RawIOBase): + def __init__(self, stream: socket.socket, until: float): + super().__init__() + self.stream, self.until = stream, until + + def readable(self) -> bool: + return True + + def readinto(self, buffer) -> int: + self.stream.settimeout(remaining(self.until)) + return self.stream.recv_into(buffer) + + +def http_reader(stream: socket.socket) -> io.BufferedReader: + return io.BufferedReader(DeadlineReader(stream, deadline())) + + +class OwnedConnectionsMixin: + """Register work before starting it, then close and join only owned work.""" + + max_handlers = MAX_HANDLERS + + def __init__(self, *args, **kwargs): + self._owned_lock = threading.Lock() + self._owned = {} + self._handler_threads = set() + self._closing = False + super().__init__(*args, **kwargs) + + @property + def active_handlers(self) -> int: + with self._owned_lock: + return len(self._owned) + + def process_request(self, request, client_address): + with self._owned_lock: + if self._closing or len(self._owned) >= self.max_handlers: + self.shutdown_request(request) + return + thread = threading.Thread( + target=self._owned_request, args=(request, client_address), daemon=True + ) + self._owned[request] = thread + self._handler_threads = { + item for item in self._handler_threads if item.is_alive() + } + self._handler_threads.add(thread) + try: + thread.start() + except BaseException: + self._owned.pop(request) + self._handler_threads.remove(thread) + self.shutdown_request(request) + raise + + def _owned_request(self, request, client_address): + try: + try: + self.finish_request(request, client_address) + except (ConnectionError, TimeoutError): + pass + except OSError: + if not self._closing: + self.handle_error(request, client_address) + except Exception: + self.handle_error(request, client_address) + finally: + self.shutdown_request(request) + with self._owned_lock: + self._owned.pop(request, None) + + def server_close(self): + with self._owned_lock: + self._closing = True + owned = list(self._owned.items()) + threads = list(self._handler_threads) + super().server_close() + for request, _ in owned: + try: + request.shutdown(socket.SHUT_RDWR) + except OSError: + pass + request.close() + until = time.monotonic() + TEARDOWN_SECONDS + for thread in threads: + thread.join(max(0, until - time.monotonic())) + if any(thread.is_alive() for thread in threads): + raise RuntimeError("fixture owned handler teardown is incomplete") + + +class OwnedHTTPServer(OwnedConnectionsMixin, http.server.ThreadingHTTPServer): + pass diff --git a/scripts/fixture_test_policy.py b/scripts/fixture_test_policy.py @@ -0,0 +1,60 @@ +"""Standalone fixed fixture budgets; explicit short functional cases stay short.""" + +from __future__ import annotations + +import hashlib +import json +import math +from pathlib import Path + +POLICY_PATH = Path(__file__).with_name("fixture_test_policy.v1.json") + + +def unique_keys(pairs): + result = {} + for key, value in pairs: + if key in result: + raise ValueError("duplicate fixture policy key") + result[key] = value + return result + + +def validate_identity(policy): + if ( + policy.get("schema") != "radroots.tera.fixture-test-policy.v1" + or policy.get("id") != "tera_fixture_busy_workstation_v1" + or policy.get("phase") != "pre_mvp_20261008" + or policy.get("functional_short_intervals_unchanged") is not True + or policy.get("quiet_workstation_required") is not False + ): + raise ValueError("unknown fixture policy or altered functional controls") + + +def valid_budget(value): + return type(value) in (int, float) and math.isfinite(value) and 0 < value <= 900 + + +def load_policy(): + if POLICY_PATH.is_symlink(): + raise ValueError("fixture policy must be a regular source file") + with POLICY_PATH.open("rb") as stream: + raw = stream.read(16385) + if len(raw) > 16384: + raise ValueError("fixture policy exceeds its source bound") + policy = json.loads(raw, object_pairs_hook=unique_keys) + if not isinstance(policy, dict): + raise ValueError("fixture policy must be an object") + validate_identity(policy) + limits = policy["limits"] + if not isinstance(limits, dict) or not limits: + raise ValueError("fixture budgets are missing") + if not all(valid_budget(value) for value in limits.values()): + raise ValueError("fixture budget is outside its finite bound") + if policy.get("caps") != {"stdout": 1048576, "stderr": 65536}: + raise ValueError("fixture output caps changed") + policy["source_sha256"] = hashlib.sha256(raw).hexdigest() + return policy + + +def limit(name): + return load_policy()["limits"][name] diff --git a/scripts/fixture_test_policy.v1.json b/scripts/fixture_test_policy.v1.json @@ -0,0 +1,23 @@ +{ + "schema": "radroots.tera.fixture-test-policy.v1", + "id": "tera_fixture_busy_workstation_v1", + "phase": "pre_mvp_20261008", + "limits": { + "compiler": 30, + "native_helper": 30, + "release_command": 120, + "reporter_child": 120, + "selector_command": 120, + "xcode_readiness": 30, + "xcode_wrapper": 60, + "aggregate": 600, + "partition": 900, + "hostile_parser": 3, + "leaf_cleanup": 2, + "root_cleanup": 4 + }, + "caps": {"stdout": 1048576, "stderr": 65536}, + "clock": "one monotonic deadline before the existing spawn call", + "functional_short_intervals_unchanged": true, + "quiet_workstation_required": false +} diff --git a/scripts/fixture_tool_dispatch.py b/scripts/fixture_tool_dispatch.py @@ -0,0 +1,395 @@ +"""Fresh owned native dispatch for Python test-tool bodies; no product runner.""" + +from __future__ import annotations + +import hashlib +import json +import os +import re +import selectors +import shutil +import signal +import stat +import subprocess +import sys +import time +from pathlib import Path + +if __package__: + from .fixture_test_policy import limit, load_policy +else: + from fixture_test_policy import limit, load_policy + + +class FixtureDispatchError(RuntimeError): + """The owned fixture compiler or declared tool could not be established.""" + + +class CompilerCommand: + """One deadline, bounded pipes and cleanup before releasing the leader PID.""" + + LIMITS = {"stdout": 1024 * 1024, "stderr": 64 * 1024} + + def __init__( + self, + argv: list[str], + cwd: Path, + timeout=None, + *, + env=None, + budget="compiler", + maximum_stdout=1024 * 1024, + ): + policy = load_policy() + timeout = policy["limits"][budget] if timeout is None else timeout + if not 0 < timeout <= policy["limits"][budget]: + raise ValueError("fixture command deadline exceeds its fixed policy budget") + if ( + type(maximum_stdout) is not int + or not 0 < maximum_stdout <= self.LIMITS["stdout"] + ): + raise ValueError("command JSON output exceeds its byte bound") + self.LIMITS = {**self.LIMITS, "stdout": maximum_stdout} + self.argv, self.cwd, self.timeout = argv, cwd, timeout + self.environment = env + self.output = {name: bytearray() for name in self.LIMITS} + self.selector = selectors.DefaultSelector() + self.process = None + self.started = time.monotonic() + self.record = { + "argv": argv, + "cwd": str(cwd), + "timeout": timeout, + "policy_id": policy["id"], + "policy_sha256": policy["source_sha256"], + "budget": budget, + "started_ns": time.time_ns(), + "signals": [], + "wait_reaped": False, + "group_absent": False, + } + + def exited(self) -> bool: + state = os.waitid( + os.P_PID, self.process.pid, os.WEXITED | os.WNOHANG | os.WNOWAIT + ) + return state is not None and state.si_pid == self.process.pid + + def read_ready(self, timeout: float, *, reject_excess: bool) -> None: + if not self.selector.get_map(): + time.sleep(timeout) + return + for key, _ in self.selector.select(timeout): + name = key.data + available = self.LIMITS[name] - len(self.output[name]) + size = min(65536, available + 1) if reject_excess else 65536 + data = os.read(key.fd, size) + if not data: + self.selector.unregister(key.fileobj) + key.fileobj.close() + continue + self.output[name].extend(data[:available]) + if len(data) > available: + self.record.setdefault("output_excess", name) + if reject_excess: + raise FixtureDispatchError( + "fixture compiler capture limit exceeded" + ) + + def capture(self) -> None: + deadline = self.started + self.timeout + while self.selector.get_map() or not self.exited(): + remaining = deadline - time.monotonic() + if remaining <= 0: + raise subprocess.TimeoutExpired(self.argv, self.timeout) + self.read_ready(min(remaining, 0.05), reject_excess=True) + + def group_present(self) -> bool: + try: + os.killpg(self.process.pid, 0) + except ProcessLookupError: + return False + except PermissionError: + return True + return True + + def send(self, number: int) -> None: + try: + os.killpg(self.process.pid, number) + except ProcessLookupError: + return + except PermissionError: + self.record.setdefault("signal_errors", []).append( + {"signal": number, "errno": 1} + ) + if not self.exited(): + raise + return + self.record["signals"].append({"signal": number, "at_ns": time.time_ns()}) + + def drain(self, deadline: float) -> None: + while self.selector.get_map() and time.monotonic() < deadline: + self.read_ready( + min(0.01, max(0, deadline - time.monotonic())), reject_excess=False + ) + + def finish_cleanup(self, deadline: float) -> None: + self.send(signal.SIGKILL) + self.process.wait(timeout=max(0.001, deadline - time.monotonic())) + self.record.update(wait_reaped=True, exit=self.process.returncode) + self.drain(deadline) + while self.group_present() and time.monotonic() < deadline: + time.sleep(0.005) + self.record["group_absent"] = not self.group_present() + if not self.record["group_absent"] or self.selector.get_map(): + raise FixtureDispatchError("fixture compiler cleanup incomplete") + + def settle(self) -> None: + deadline = time.monotonic() + limit("leaf_cleanup") + try: + self.send(signal.SIGTERM) + grace = time.monotonic() + 0.2 + self.drain(grace) + while self.group_present() and time.monotonic() < grace: + time.sleep(0.005) + finally: + self.finish_cleanup(deadline) + + def execute(self) -> subprocess.CompletedProcess: + try: + self.process = subprocess.Popen( + self.argv, + cwd=self.cwd, + env=self.environment, + stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + bufsize=0, + start_new_session=True, + ) + self.record.update(pid=self.process.pid, pgid=self.process.pid) + for name in self.LIMITS: + stream = getattr(self.process, name) + os.set_blocking(stream.fileno(), False) + self.selector.register(stream, selectors.EVENT_READ, name) + self.capture() + except BaseException as error: + self.record["error"] = type(error).__name__ + raise + finally: + try: + if self.process is not None: + self.settle() + except BaseException as error: + self.record["cleanup_error"] = type(error).__name__ + raise + finally: + self.selector.close() + if self.process is not None: + for name in self.LIMITS: + getattr(self.process, name).close() + self.record.update( + elapsed_seconds=time.monotonic() - self.started, + ended_ns=time.time_ns(), + ) + return subprocess.CompletedProcess( + self.argv, + self.process.returncode, + bytes(self.output["stdout"]), + bytes(self.output["stderr"]), + ) + + +def selector_command(argv, *, maximum_stdout=1024 * 1024, timeout=None): + capture = CompilerCommand( + argv, + Path.cwd(), + timeout, + budget="selector_command", + maximum_stdout=maximum_stdout, + ) + try: + result = capture.execute() + except FixtureDispatchError as error: + if capture.record.get("output_excess") == "stdout": + raise ValueError("command JSON output exceeds its byte bound") from error + raise + if result.returncode: + raise subprocess.CalledProcessError(result.returncode, argv) + return result + + +NATIVE_SOURCE = r""" +#include <limits.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#if defined(__APPLE__) +#include <mach-o/dyld.h> +#endif + +static const char *roles[] = {ROLE_LITERALS}; +static const char *python = PYTHON_LITERAL; + +static int executing_image(char *resolved) { + char image[PATH_MAX]; +#if defined(__APPLE__) + uint32_t size = sizeof(image); + if (_NSGetExecutablePath(image, &size) != 0) return 0; +#elif defined(__linux__) + ssize_t size = readlink("/proc/self/exe", image, sizeof(image) - 1); + if (size < 0 || (size_t)size >= sizeof(image) - 1) return 0; + image[size] = '\0'; +#else + return 0; +#endif + return realpath(image, resolved) != NULL; +} + +int main(int argc, char **argv) { + if (argc < 1 || argc > 4096) return 64; + const char *role = strrchr(argv[0], '/'); + role = role ? role + 1 : argv[0]; + size_t count = sizeof(roles) / sizeof(roles[0]); + size_t index; + for (index = 0; index < count; index++) + if (strcmp(role, roles[index]) == 0) break; + if (index == count) return 64; + char image[PATH_MAX]; + if (!executing_image(image)) return 64; + char *basename = strrchr(image, '/'); + if (!basename || strcmp(basename + 1, role) != 0) return 64; + *basename = '\0'; + const char *path = getenv("PATH"); + if (!path || path[0] != '/') return 64; + size_t length = strcspn(path, ":"); + if (!length || length >= PATH_MAX) return 64; + if (strlen(image) != length || strncmp(path, image, length) != 0) return 64; + char body[PATH_MAX]; + int size = snprintf(body, sizeof(body), "%.*s/%s.body.py", (int)length, path, role); + if (size < 0 || (size_t)size >= sizeof(body)) return 64; + if (access(body, R_OK) != 0) return 66; + char **arguments = calloc((size_t)argc + 2, sizeof(char *)); + if (!arguments) return 70; + arguments[0] = (char *)python; + arguments[1] = body; + for (int item = 1; item < argc; item++) arguments[item + 1] = argv[item]; + execv(python, arguments); + perror("fixture-tool-dispatch: execv"); + free(arguments); + return 71; +} +""" + + +def file_identity(path: Path) -> dict: + information = path.stat() + return { + "path": str(path), + "resolved": str(path.resolve()), + "sha256": hashlib.sha256(path.read_bytes()).hexdigest(), + "bytes": information.st_size, + "mode": stat.S_IMODE(information.st_mode), + } + + +def c_literal(value: str) -> str: + return '"' + "".join(f"\\{byte:03o}" for byte in value.encode()) + '"' + + +class FixtureToolDispatcher: + """Compile once per fixture, never execute the target or share tool inodes.""" + + def __init__(self, tools: Path, roles: tuple[str, ...]): + self.tools = tools.resolve() + if not 0 < len(roles) <= 16 or len(set(roles)) != len(roles): + raise FixtureDispatchError("fixture roles must be nonempty and unique") + if any(re.fullmatch(r"[a-z][a-z0-9_]{0,31}", role) is None for role in roles): + raise FixtureDispatchError("fixture role name rejected") + self.roles = roles + self.directory = self.tools / ".fixture-dispatch" + self.directory.mkdir() + self.binary = self.directory / "dispatch" + self.record = { + "interpreter": file_identity(Path(sys.executable)), + "roles": list(roles), + "tools": str(self.tools), + "environment_policy": "compiler inherits existing selected developer environment", + "body_policy": "actual interpreter argv is [interpreter, role.body.py, *original argument suffix]", + "image_policy": "actual canonical image parent equals first PATH; actual basename equals argv0 role", + } + self.compile() + + def compile(self) -> None: + compiler = shutil.which("cc") + if compiler is None: + self.record["error"] = "selected native compiler unavailable" + self.save_record() + raise FixtureDispatchError( + "fixture requires an installed selected native compiler" + ) + source = self.directory / "dispatch.c" + source.write_text( + NATIVE_SOURCE.replace( + "ROLE_LITERALS", ",".join(map(c_literal, self.roles)) + ).replace("PYTHON_LITERAL", c_literal(sys.executable)) + ) + self.record.update( + source=file_identity(source), compiler=file_identity(Path(compiler)) + ) + argv = [ + compiler, + "-Wall", + "-Wextra", + "-Werror", + "-o", + str(self.binary), + str(source), + ] + command = CompilerCommand(argv, self.directory) + try: + result = command.execute() + if result.returncode != 0: + raise FixtureDispatchError("fixture native compilation failed") + if ( + not self.binary.is_file() + or self.binary.stat().st_size > 2 * 1024 * 1024 + ): + raise FixtureDispatchError("fixture compiler output rejected") + self.record["binary"] = file_identity(self.binary) + except BaseException as error: + self.record["error"] = type(error).__name__ + raise + finally: + self.record["command"] = command.record + for name, raw in command.output.items(): + (self.directory / f"compiler.{name}").write_bytes(raw) + self.save_record() + + def save_record(self) -> None: + (self.directory / "compilation.json").write_text( + json.dumps(self.record, indent=2) + "\n" + ) + + def executable(self, name: str, body: str) -> None: + if name not in self.roles or len(body.encode()) > 2 * 1024 * 1024: + raise FixtureDispatchError("undeclared fixture tool or oversized body") + path = self.tools / name + if path.exists() or path.is_symlink(): + path.unlink() + body_path = self.tools / f"{name}.body.py" + body_path.write_text(body) + shutil.copyfile(self.binary, path) + path.chmod(0o700) + record = { + "tool": file_identity(path), + "body": file_identity(body_path), + "interpreter": self.record["interpreter"], + "actual_exec_argv_prefix": [sys.executable, str(body_path)], + "argument_suffix": "original native argv[1:] without transformation", + } + (self.directory / f"{name}.json").write_text( + json.dumps(record, indent=2) + "\n" + ) diff --git a/scripts/local-social-fixture.py b/scripts/local-social-fixture.py @@ -18,7 +18,6 @@ import signal import socket import socketserver import stat -import subprocess import struct import sys import threading @@ -30,6 +29,15 @@ from typing import Any from jsonschema import Draft202012Validator from jsonschema.exceptions import SchemaError, ValidationError +try: + from scripts.fixture_tool_dispatch import selector_command as run_selector_command + from scripts import fixture_connections as connections +except ModuleNotFoundError as error: + if error.name != "scripts": + raise + from fixture_tool_dispatch import selector_command as run_selector_command + import fixture_connections as connections + MAX_HTTP_BODY = 16 * 1024 * 1024 MAX_WEBSOCKET_MESSAGE = 2 * 1024 * 1024 MAX_EVENTS = 256 @@ -229,21 +237,15 @@ def validate_persona_suite(value: Any) -> dict[str, Any]: ): raise ValueError("persona attempt is not exact") attempts.append(attempt) - if ( - [item["expected_failure"] for item in attempts].count("validation_recovery") - != 1 - ): + if [item["expected_failure"] for item in attempts].count( + "validation_recovery" + ) != 1: raise ValueError("persona suite must contain one validation-recovery vector") - if ( - [item["expected_failure"] for item in attempts].count( - "transport_retry_relaunch" - ) - != 1 - ): + if [item["expected_failure"] for item in attempts].count( + "transport_retry_relaunch" + ) != 1: raise ValueError("persona suite must contain one transport-retry vector") - if any( - sum(item["flow"] == flow for item in attempts) != 3 for flow in FLOW_KINDS - ): + if any(sum(item["flow"] == flow for item in attempts) != 3 for flow in FLOW_KINDS): raise ValueError("each Add flow must have exactly three attempts") return root @@ -357,9 +359,7 @@ def schema_contains_external_reference(value: Any) -> bool: return False -def validate_schema_instance( - schema: dict[str, Any], value: Any, name: str -) -> None: +def validate_schema_instance(schema: dict[str, Any], value: Any, name: str) -> None: try: Draft202012Validator(schema).validate(value) except ValidationError as error: @@ -433,9 +433,7 @@ class FixtureState: self._identity_by_persona: dict[str, str] = {} self._persona_by_identity: dict[str, str] = {} self._accepted_uploads_by_persona = {alias: 0 for alias in PERSONA_ALIASES} - self._uploaded_digests_by_persona = { - alias: set() for alias in PERSONA_ALIASES - } + self._uploaded_digests_by_persona = {alias: set() for alias in PERSONA_ALIASES} self._retrievals_by_persona = {alias: set() for alias in PERSONA_ALIASES} self._unknown_attempts = 0 self._duplicate_attempts = 0 @@ -506,9 +504,8 @@ class FixtureState: public_key = event["pubkey"] existing_identity = self._identity_by_persona.get(persona) existing_persona = self._persona_by_identity.get(public_key) - if ( - (existing_identity is not None and existing_identity != public_key) - or (existing_persona is not None and existing_persona != persona) + if (existing_identity is not None and existing_identity != public_key) or ( + existing_persona is not None and existing_persona != persona ): self._unintended_publications += 1 self._write_evidence_locked() @@ -833,8 +830,7 @@ def photo_attempt_matches(event: dict[str, Any], attempt: dict[str, Any]) -> boo if isinstance(tag, list) and tag[:1] == ["imeta"] ] return len(imeta) == 1 and all( - field in imeta[0] - for field in (f"url {url}", f"x {digest}", "m image/png") + field in imeta[0] for field in (f"url {url}", f"x {digest}", "m image/png") ) @@ -842,8 +838,7 @@ def identity_digest(public_key: str | None) -> str: if public_key is None: return "0" * 64 return hashlib.sha256( - b"radroots.ios.local-social.persona-identity.v1\0" - + bytes.fromhex(public_key) + b"radroots.ios.local-social.persona-identity.v1\0" + bytes.fromhex(public_key) ).hexdigest() @@ -869,9 +864,7 @@ def point_add( return None slope = (3 * left[0] * left[0]) * pow(2 * left[1], -1, SECP256K1_FIELD) else: - slope = (right[1] - left[1]) * pow( - right[0] - left[0], -1, SECP256K1_FIELD - ) + slope = (right[1] - left[1]) * pow(right[0] - left[0], -1, SECP256K1_FIELD) slope %= SECP256K1_FIELD x = (slope * slope - left[0] - right[0]) % SECP256K1_FIELD y = (slope * (left[0] - x) - left[1]) % SECP256K1_FIELD @@ -908,10 +901,13 @@ def verify_bip340(public_key: bytes, message: bytes, signature: bytes) -> bool: return False if y & 1: y = SECP256K1_FIELD - y - challenge = int.from_bytes( - tagged_hash("BIP0340/challenge", signature[:32] + public_key + message), - "big", - ) % SECP256K1_ORDER + challenge = ( + int.from_bytes( + tagged_hash("BIP0340/challenge", signature[:32] + public_key + message), + "big", + ) + % SECP256K1_ORDER + ) negative = (x, (-y) % SECP256K1_FIELD) candidate = point_add( point_multiply(s, SECP256K1_GENERATOR), @@ -946,8 +942,7 @@ def valid_bud11_server_domain(value: Any) -> bool: labels = value.split(".") return not all(label.isdigit() for label in labels) and all( 1 <= len(label) <= 63 - and re.fullmatch(r"[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", label) - is not None + and re.fullmatch(r"[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", label) is not None for label in labels ) return isinstance(address, ipaddress.IPv4Address) and str(address) == value @@ -960,8 +955,7 @@ def valid_bud11_content(value: Any) -> bool: and len(value.encode("utf-8")) <= BUD11_CONTENT_MAX_BYTES and value.strip() == value and not any( - unicodedata.category(character) == "Cc" - and character not in "\t\n\r" + unicodedata.category(character) == "Cc" and character not in "\t\n\r" for character in value ) ) @@ -1039,6 +1033,10 @@ def valid_blossom_authorization( class ObservableLoopbackServerMixin: _fixture_state: FixtureState + def __init__(self, server_address, handler, state): + self._fixture_state = state + super().__init__(server_address, handler) + def verify_request( self, request: socket.socket, client_address: tuple[str, int] ) -> bool: @@ -1047,32 +1045,18 @@ class ObservableLoopbackServerMixin: class ReusableThreadingServer( - ObservableLoopbackServerMixin, socketserver.ThreadingTCPServer + ObservableLoopbackServerMixin, + connections.OwnedConnectionsMixin, + socketserver.ThreadingTCPServer, ): allow_reuse_address = True daemon_threads = True - def __init__( - self, - server_address: tuple[str, int], - handler: type[socketserver.BaseRequestHandler], - state: FixtureState, - ) -> None: - self._fixture_state = state - super().__init__(server_address, handler) - class ObservableLoopbackHTTPServer( - ObservableLoopbackServerMixin, http.server.ThreadingHTTPServer + ObservableLoopbackServerMixin, connections.OwnedHTTPServer ): - def __init__( - self, - server_address: tuple[str, int], - handler: type[http.server.BaseHTTPRequestHandler], - state: FixtureState, - ) -> None: - self._fixture_state = state - super().__init__(server_address, handler) + pass class LoopbackConnectionFactory: @@ -1082,9 +1066,7 @@ class LoopbackConnectionFactory: @staticmethod def blossom(port: int, state: FixtureState) -> ObservableLoopbackHTTPServer: - return ObservableLoopbackHTTPServer( - ("127.0.0.1", port), BlossomHandler, state - ) + return ObservableLoopbackHTTPServer(("127.0.0.1", port), BlossomHandler, state) class RelayHandler(socketserver.BaseRequestHandler): @@ -1161,25 +1143,11 @@ class RelayHandler(socketserver.BaseRequestHandler): def read_until(stream: socket.socket, marker: bytes, maximum: int) -> bytes: - value = bytearray() - while marker not in value: - chunk = stream.recv(4096) - if not chunk: - raise ConnectionError("socket closed") - value.extend(chunk) - if len(value) > maximum: - raise ValueError("request too large") - return bytes(value) - - -def read_exact(stream: socket.socket, length: int) -> bytes: - value = bytearray() - while len(value) < length: - chunk = stream.recv(length - len(value)) - if not chunk: - raise ConnectionError("socket closed") - value.extend(chunk) - return bytes(value) + return connections.recv_until(stream, marker, maximum) + + +def read_exact(stream: socket.socket, length: int, until=None) -> bytes: + return connections.recv_exact(stream, length, until) def read_frame(stream: socket.socket) -> tuple[int, bytes] | None: @@ -1187,20 +1155,24 @@ def read_frame(stream: socket.socket) -> tuple[int, bytes] | None: # Idle is not a failure vector; partial frames retain the read deadline. while not select.select([stream], [], [], 15)[0]: pass - first, second = read_exact(stream, 2) - except (ConnectionError, OSError, TimeoutError): + return read_frame_body(stream, connections.deadline()) + except (ConnectionError, OSError, ValueError): return None + + +def read_frame_body(stream: socket.socket, until: float) -> tuple[int, bytes] | None: + first, second = read_exact(stream, 2, until) if first & 0x80 == 0 or second & 0x80 == 0: return None length = second & 0x7F if length == 126: - length = struct.unpack("!H", read_exact(stream, 2))[0] + length = struct.unpack("!H", read_exact(stream, 2, until))[0] elif length == 127: - length = struct.unpack("!Q", read_exact(stream, 8))[0] + length = struct.unpack("!Q", read_exact(stream, 8, until))[0] if length > MAX_WEBSOCKET_MESSAGE: return None - mask = read_exact(stream, 4) - payload = bytearray(read_exact(stream, length)) + mask = read_exact(stream, 4, until) + payload = bytearray(read_exact(stream, length, until)) for index in range(length): payload[index] ^= mask[index % 4] return first & 0x0F, bytes(payload) @@ -1228,6 +1200,11 @@ class BlossomHandler(http.server.BaseHTTPRequestHandler): state: FixtureState protocol_version = "HTTP/1.1" + def setup(self) -> None: + super().setup() + self.rfile.close() + self.rfile = connections.http_reader(self.connection) + def do_PUT(self) -> None: # noqa: N802 if self.path != "/upload": self.send_error(404) @@ -1249,6 +1226,9 @@ class BlossomHandler(http.server.BaseHTTPRequestHandler): self.send_error(400) return body = self.rfile.read(length) + if len(body) != length: + self.send_error(400) + return accepted, descriptor = self.state.upload( body, media_type, expected_hash, authorization ) @@ -1564,11 +1544,23 @@ def simulator_metadata(udid: str, result_bundle: Path) -> dict[str, str]: def validate_persona_evidence(value: Any, suite: dict[str, Any]) -> dict[str, Any]: keys = { - "schema", "schema_version", "personas", "flow_counts", "accepted_events", - "event_kind_counts", "upload_attempts", "accepted_uploads", "retrievals", - "distinct_identities", "unknown_attempts", "duplicate_attempts", - "expected_failure_rejections", "events_accepted_during_expected_failures", - "accepted_connections", "rejected_connections", "non_loopback_attempts", + "schema", + "schema_version", + "personas", + "flow_counts", + "accepted_events", + "event_kind_counts", + "upload_attempts", + "accepted_uploads", + "retrievals", + "distinct_identities", + "unknown_attempts", + "duplicate_attempts", + "expected_failure_rejections", + "events_accepted_during_expected_failures", + "accepted_connections", + "rejected_connections", + "non_loopback_attempts", "production_network_contacts", "unintended_publications", "final_candidate_data_loss", @@ -1650,8 +1642,7 @@ def validate_persona_evidence(value: Any, suite: dict[str, Any]) -> dict[str, An or attempt["accepted"] is not True or attempt["expected_failure_rejections"] != int( - expected_attempt["expected_failure"] - == "transport_retry_relaunch" + expected_attempt["expected_failure"] == "transport_retry_relaunch" ) ): raise ValueError("attempt evidence row is invalid") @@ -1733,15 +1724,11 @@ def validate_persona_attempt_evidence( or not lowercase_hex(attempt["public_identity_sha256"], 64) or attempt["public_identity_sha256"] == "0" * 64 or not lowercase_hex(attempt["endpoint_policy_sha256"], 64) - or not re.fullmatch( - r"[a-z0-9][a-z0-9-]{6,62}[a-z0-9]", attempt["run_id"] - ) + or not re.fullmatch(r"[a-z0-9][a-z0-9-]{6,62}[a-z0-9]", attempt["run_id"]) ): raise ValueError("persona attempt evidence header is invalid") source = exact_keys(attempt["source"], {"commit", "tree"}, "attempt source") - if not lowercase_hex(source["commit"], 40) or not lowercase_hex( - source["tree"], 40 - ): + if not lowercase_hex(source["commit"], 40) or not lowercase_hex(source["tree"], 40): raise ValueError("persona attempt source identity is invalid") simulator = exact_keys( attempt["simulator"], {"udid", "os", "architecture"}, "attempt simulator" @@ -1852,8 +1839,7 @@ def validate_persona_attempt_evidence( ) if ( accessibility["locale"] != "en_US" - or accessibility["content_size"] - != "accessibility-extra-extra-extra-large" + or accessibility["content_size"] != "accessibility-extra-extra-extra-large" or accessibility["reduce_motion"] is not True or any( type(accessibility[key]) is not bool @@ -1868,9 +1854,13 @@ def validate_persona_attempt_evidence( or accessibility["visible_actions"] is not True or accessibility["voiceover_user_observed"] is not False or accessibility["progressive_disclosure"] - is not (expected_persona["interaction_profile"] == "novice_progressive_disclosure") + is not ( + expected_persona["interaction_profile"] == "novice_progressive_disclosure" + ) or accessibility["keyboard_focus"] - is not (expected_persona["interaction_profile"] == "novice_accessibility_keyboard") + is not ( + expected_persona["interaction_profile"] == "novice_accessibility_keyboard" + ) ): raise ValueError("persona attempt accessibility evidence is invalid") artifacts = attempt["artifact_digests"] @@ -1932,18 +1922,8 @@ def exact_persona_test_node(value: Any) -> dict[str, Any]: return match -def run_json_command_bounded(command: list[str], maximum: int) -> Any: - process = subprocess.Popen(command, stdout=subprocess.PIPE) - assert process.stdout is not None - raw = process.stdout.read(maximum + 1) - process.stdout.close() - if len(raw) > maximum: - process.kill() - process.wait() - raise ValueError("command JSON output exceeds its byte bound") - return_code = process.wait() - if return_code != 0: - raise subprocess.CalledProcessError(return_code, command) +def run_json_command_bounded(command: list[str], maximum: int, *, timeout=None) -> Any: + raw = run_selector_command(command, maximum_stdout=maximum, timeout=timeout).stdout if not raw: raise ValueError("command JSON output exceeds its byte bound") return json.loads(raw, object_pairs_hook=strict_object) @@ -2007,14 +1987,18 @@ def load_exported_persona_attachments( "arguments", } for row_value in group["attachments"]: - if not isinstance(row_value, dict) or not { - "exportedFileName", - "suggestedHumanReadableName", - "isAssociatedWithFailure", - "configurationName", - "deviceName", - "deviceId", - }.issubset(row_value) or not set(row_value).issubset(allowed_manifest_keys): + if ( + not isinstance(row_value, dict) + or not { + "exportedFileName", + "suggestedHumanReadableName", + "isAssociatedWithFailure", + "configurationName", + "deviceName", + "deviceId", + }.issubset(row_value) + or not set(row_value).issubset(allowed_manifest_keys) + ): raise ValueError("xcresult attachment row is invalid") exported = row_value["exportedFileName"] name = row_value["suggestedHumanReadableName"] @@ -2050,7 +2034,9 @@ def load_exported_persona_attachments( raise ValueError("xcresult attachment name does not bind its attempt") total_bytes += len(raw) if total_bytes > MAX_PERSONA_ATTACHMENTS_BYTES: - raise ValueError("xcresult attempt attachments exceed their aggregate bound") + raise ValueError( + "xcresult attempt attachments exceed their aggregate bound" + ) attachments_by_name[canonical_name] = (raw, attempt) if tuple(sorted(attachments_by_name)) != tuple(sorted(PERSONA_ATTACHMENT_NAMES)): raise ValueError("xcresult persona attachment inventory is incomplete") @@ -2085,6 +2071,7 @@ def extract_persona_attempt_attachments( PERSONA_ATTACHMENT_NAMES, MAX_XCRESULT_JSON_BYTES, MAX_PERSONA_ATTACHMENT_BYTES, + run_selector_command, ) return module.extract( result_bundle, @@ -2159,9 +2146,7 @@ def reconstruct_persona_result_v2( raise ValueError("persona result identity reuse is invalid") identity = identities.pop() identity_by_persona[alias] = identity - subscriptions = sum( - row["network_observation"]["subscriptions"] for row in rows - ) + subscriptions = sum(row["network_observation"]["subscriptions"] for row in rows) if subscriptions < 1: raise ValueError("persona result is missing a subscription") persona_rows.append( @@ -2219,7 +2204,8 @@ def reconstruct_persona_result_v2( "unknown_attempts": len( set(expected_ids) - {row["attempt_id"] for row in attempts} ), - "duplicate_attempts": len(attempts) - len({row["attempt_id"] for row in attempts}), + "duplicate_attempts": len(attempts) + - len({row["attempt_id"] for row in attempts}), "expected_failure_rejections": sum( attempt["ui_observation"]["validation_rejected"] or attempt["ui_observation"]["retry_attempts"] > 0 @@ -2228,9 +2214,15 @@ def reconstruct_persona_result_v2( "events_accepted_during_expected_failures": sum( row["events_accepted_during_expected_failure"] for row in network_rows ), - "accepted_connections": sum(row["accepted_connections"] for row in network_rows), - "rejected_connections": sum(row["rejected_connections"] for row in network_rows), - "non_loopback_attempts": sum(row["non_loopback_attempts"] for row in network_rows), + "accepted_connections": sum( + row["accepted_connections"] for row in network_rows + ), + "rejected_connections": sum( + row["rejected_connections"] for row in network_rows + ), + "non_loopback_attempts": sum( + row["non_loopback_attempts"] for row in network_rows + ), "unintended_publications": sum( row["unintended_publications"] for row in network_rows ), @@ -2429,8 +2421,7 @@ def validate_persona_result( or attempt["accepted"] is not True or attempt["expected_failure_rejections"] != int( - expected_attempt["expected_failure"] - == "transport_retry_relaunch" + expected_attempt["expected_failure"] == "transport_retry_relaunch" ) ): raise ValueError("attempt result row is invalid") @@ -2499,7 +2490,10 @@ def verify_persona(arguments: argparse.Namespace) -> int: output = Path(arguments.output).resolve() output.write_text(json.dumps(result, indent=2) + "\n", encoding="utf-8") raw, reloaded = read_json(output) - if raw != (json.dumps(reloaded, indent=2) + "\n").encode("utf-8") or reloaded != result: + if ( + raw != (json.dumps(reloaded, indent=2) + "\n").encode("utf-8") + or reloaded != result + ): raise ValueError("persona v2 result is noncanonical") print( "local-social measured persona result verified: " diff --git a/scripts/local_social_test_support.py b/scripts/local_social_test_support.py @@ -1,14 +1,15 @@ """Scoped HTTP server ownership for the real local-social fixture tests.""" from contextlib import contextmanager -from http.server import ThreadingHTTPServer from threading import Thread +from scripts.fixture_connections import OwnedHTTPServer + @contextmanager def blossom_server(handler, state): handler.state = state - server = ThreadingHTTPServer(("127.0.0.1", 0), handler) + server = OwnedHTTPServer(("127.0.0.1", 0), handler) state.blossom_port = server.server_address[1] thread = Thread(target=server.serve_forever, daemon=True) thread.start() @@ -18,3 +19,5 @@ def blossom_server(handler, state): server.shutdown() server.server_close() thread.join(timeout=5) + if thread.is_alive(): + raise RuntimeError("fixture server teardown is incomplete") diff --git a/scripts/maintainability_ratchet.py b/scripts/maintainability_ratchet.py @@ -10,11 +10,63 @@ import sys from pathlib import Path from typing import Any +from authored_source_inventory import InventoryError, source_files + MAX_BASELINE_BYTES = 256 * 1024 BASELINE_PATH = Path("test-fixtures/maintainability-baseline.v1.json") -SWIFT_ROOTS = (Path("Tera"), Path("TeraTests"), Path("TeraUITests")) -PYTHON_ROOT = Path("scripts") -EXCLUDED_SWIFT_ROOT = Path("Tera/Generated") + +# Approved pre-ratchet ceilings. Candidate baseline changes cannot enlarge them. +APPROVED_EXCEPTIONS = { + "swift_file_exception": { + "Tera/Runtime/TeraGeneratedRuntimeBackend.swift": 1188, + "Tera/Runtime/TeraRuntimeClient.swift": 820, + "Tera/Runtime/TeraRuntimeModels.swift": 1154, + "Tera/State/TeraAddStore.swift": 783, + "Tera/State/TeraConfigurationStore.swift": 759, + "TeraTests/TeraAddStoreTests.swift": 1366, + "TeraTests/TeraStateMigrationTests.swift": 718, + "TeraUITests/TeraRemoteQualificationUITests.swift": 1924, + }, + "python_file_exception": { + "scripts/local-social-fixture.py": 2626, + "scripts/test_local_social_fixture.py": 1188, + }, + "python_complexity_exception": { + "scripts/local-social-fixture.py:FixtureState._publish_persona_event": 13, + "scripts/local-social-fixture.py:FixtureState.upload": 14, + "scripts/local-social-fixture.py:RelayHandler.handle": 26, + "scripts/local-social-fixture.py:bounded_directory_inventory": 12, + "scripts/local-social-fixture.py:directory_digest": 16, + "scripts/local-social-fixture.py:exact_persona_test_node": 11, + "scripts/local-social-fixture.py:load_exported_persona_attachments": 31, + "scripts/local-social-fixture.py:matches": 22, + "scripts/local-social-fixture.py:photo_attempt_matches": 12, + "scripts/local-social-fixture.py:reconstruct_persona_result_v2": 66, + "scripts/local-social-fixture.py:simulator_metadata": 23, + "scripts/local-social-fixture.py:valid_blossom_authorization": 29, + "scripts/local-social-fixture.py:valid_bud11_server_domain": 12, + "scripts/local-social-fixture.py:valid_nostr_event": 16, + "scripts/local-social-fixture.py:validate_persona_attempt_evidence": 59, + "scripts/local-social-fixture.py:validate_persona_evidence": 31, + "scripts/local-social-fixture.py:validate_persona_result": 43, + "scripts/local-social-fixture.py:validate_persona_suite": 28, + "scripts/local-social-fixture.py:verify_bip340": 11, + "scripts/local-social-fixture.py:verify_persona": 18, + "scripts/test_local_social_fixture.py:mutate_bud11_event": 29, + }, +} +ORIGINAL_BOUNDED_MODULES = frozenset( + ( + "Tera/Runtime/TeraUserMessageClassifier.swift", + "Tera/Runtime/TeraUserMessages.swift", + "Tera/Views/TeraSupportSettingsSection.swift", + "scripts/maintainability_ratchet.py", + "scripts/package_contract.py", + "scripts/package_privacy.py", + "scripts/test_package_contract.py", + "scripts/test_package_privacy.py", + ) +) class MaintainabilityError(Exception): @@ -145,19 +197,12 @@ def _load_baseline(repo_root: Path) -> dict[str, Any]: def _source_files(repo_root: Path) -> tuple[list[Path], list[Path]]: - swift = sorted( - path - for root in SWIFT_ROOTS - for path in (repo_root / root).rglob("*.swift") - if not path.is_symlink() - and not path.relative_to(repo_root).is_relative_to(EXCLUDED_SWIFT_ROOT) - ) - python = sorted( - path for path in (repo_root / PYTHON_ROOT).glob("*.py") if not path.is_symlink() - ) - if not swift or not python: - raise MaintainabilityError("maintainability source inventory is empty") - return swift, python + try: + return source_files(repo_root) + except InventoryError as error: + raise MaintainabilityError( + "maintainability source inventory is invalid" + ) from error def _line_inventory(repo_root: Path, paths: list[Path]) -> dict[str, int]: @@ -252,6 +297,15 @@ def _thresholds(baseline: dict[str, Any]) -> dict[str, int]: return value +def _verify_exception_ceilings(category: str, exceptions: dict[str, int]) -> None: + approved = APPROVED_EXCEPTIONS[category] + for identity, ceiling in exceptions.items(): + if identity not in approved or ceiling > approved[identity]: + raise MaintainabilityError( + "approved exception inventory or ceiling regressed" + ) + + def _verify_observed_metrics( baseline: dict[str, Any], observed: dict[str, dict[str, int]], @@ -287,6 +341,7 @@ def _verify_observed_metrics( exceptions = _closed_exception_map( baseline[baseline_key], key_name=identity, ceiling_name=ceiling ) + _verify_exception_ceilings(baseline_key, exceptions) _verify_metric(observed[metric], exceptions, thresholds[threshold], label) @@ -298,6 +353,8 @@ def _verify_bounded_modules( modules = baseline["bounded_module"] if not isinstance(modules, list) or modules != sorted(set(modules)): raise MaintainabilityError("bounded module inventory differs") + if not ORIGINAL_BOUNDED_MODULES <= set(modules): + raise MaintainabilityError("original bounded module inventory regressed") all_lines = observed["swift_lines"] | observed["python_lines"] for path in modules: if path not in all_lines: diff --git a/scripts/persona_attachment_selection.py b/scripts/persona_attachment_selection.py @@ -3,7 +3,6 @@ import json from pathlib import Path import re -import subprocess import tempfile from typing import Any, Callable, NamedTuple @@ -40,6 +39,7 @@ class ExtractionTools(NamedTuple): names: tuple[str, ...] maximum_json_bytes: int maximum_attempt_bytes: int + run_export: Callable def filename(value: object) -> str: @@ -160,7 +160,7 @@ def extract( tempfile.TemporaryDirectory() as selected_directory, ): raw, selected = Path(raw_directory), Path(selected_directory) - subprocess.run( + tools.run_export( [ "xcrun", "xcresulttool", @@ -173,7 +173,6 @@ def extract( "--output-path", str(raw), ], - check=True, ) project(raw, selected, tools) # This parser still requires exactly fifteen canonical, bounded, diff --git a/scripts/swift-quality.sh b/scripts/swift-quality.sh @@ -20,41 +20,6 @@ readonly -a MAINTAINABILITY_RULES=( large_tuple type_body_length ) -readonly -a PYTHON_QUALITY_PATHS=( - scripts/app_source.py - scripts/test_app_source.py - scripts/test_release_evidence.py - scripts/xcode_child.py - scripts/ffi_source.py - scripts/ffi_native.py - scripts/test_ffi_native.py - scripts/test_ffi_native_cache.py - scripts/unittest_report.py - scripts/test_unittest_report.py - scripts/ffi_artifacts.py - scripts/ffi_build.py - scripts/ffi_installed.py - scripts/test_ffi_installed.py - scripts/test_ffi_artifacts.py - scripts/ffi_provenance.py - scripts/test_ffi_provenance.py - scripts/maintainability_ratchet.py - scripts/package_contract.py - scripts/package_privacy.py - scripts/test_package_privacy.py - scripts/legacy_identifiers.py - scripts/test_legacy_identifiers.py - scripts/app_dependency_graph.py - scripts/test_app_dependency_graph.py - scripts/test_maintainability_ratchet.py - scripts/test_package_contract.py - scripts/kotlin_smoke.py - scripts/test_kotlin_smoke.py - scripts/test_xcode_selection.py - scripts/legacy_upgrade_fixture_producer.py - scripts/legacy_upgrade_fixture_admission.py - scripts/test_legacy_upgrade_fixture.py -) command -v swiftformat >/dev/null || { echo "swift-quality: swiftformat is unavailable" >&2 @@ -88,6 +53,7 @@ swiftlint lint \ "${metric_arguments[@]}" \ "${SOURCE_PATHS[@]}" -uv run --offline --project scripts/persona-verifier ruff format --check "${PYTHON_QUALITY_PATHS[@]}" -uv run --offline --project scripts/persona-verifier ruff check "${PYTHON_QUALITY_PATHS[@]}" +uv run --offline --frozen --project scripts/persona-verifier python scripts/authored_source_inventory.py format +uv run --offline --frozen --project scripts/persona-verifier python scripts/authored_source_inventory.py lint uv run --offline --project scripts/persona-verifier python scripts/maintainability_ratchet.py verify +uv run --offline --frozen --project scripts/persona-verifier python scripts/authored_source_inventory.py test diff --git a/scripts/test_authored_source_inventory.py b/scripts/test_authored_source_inventory.py @@ -0,0 +1,97 @@ +"""Actual omitted roots, nested source and lane agreement regressions.""" + +import tempfile +import unittest +from pathlib import Path + +from scripts import authored_source_inventory as inventory + + +class AuthoredSourceInventoryTests(unittest.TestCase): + def repository(self, root): + (root / "Package.swift").write_text("// package\n") + for relative in inventory.SWIFT_ROOTS: + (root / relative).mkdir(parents=True, exist_ok=True) + (root / relative / "Included.swift").write_text("// authored\n") + (root / "scripts/nested").mkdir() + (root / "scripts/helper.py").write_text("# source\n") + (root / "scripts/nested/test_added.py").write_text("# test\n") + + def test_all_authored_roots_and_nested_python_are_included(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + self.repository(root) + swift, python = inventory.source_files(root) + self.assertEqual( + set(swift), + { + root / "Package.swift", + *(root / path / "Included.swift" for path in inventory.SWIFT_ROOTS), + }, + ) + self.assertEqual( + python, + [root / "scripts/helper.py", root / "scripts/nested/test_added.py"], + ) + + def test_only_generated_and_cache_outputs_are_excluded(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + self.repository(root) + generated = root / inventory.GENERATED_SWIFT + generated.mkdir() + (generated / "Bindings.swift").write_text("// generated\n") + for name in inventory.IGNORED_DIRECTORIES: + cache = root / "scripts" / name + cache.mkdir() + (cache / "test_external.py").write_text("# external\n") + swift, python = inventory.source_files(root) + self.assertNotIn(generated / "Bindings.swift", swift) + self.assertEqual(len(python), 2) + + def test_authored_file_and_directory_links_are_rejected(self): + for directory_link in [False, True]: + with ( + self.subTest(directory_link=directory_link), + tempfile.TemporaryDirectory() as directory, + ): + root = Path(directory) + self.repository(root) + link = root / "scripts" / ("linked" if directory_link else "linked.py") + target = root / "Tera" if directory_link else root / "scripts/helper.py" + link.symlink_to(target, target_is_directory=directory_link) + with self.assertRaises(inventory.InventoryError): + inventory.source_files(root) + + def test_missing_authored_root_cannot_silently_shrink_inventory(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + self.repository(root) + (root / "TeraPublicAPITests/Included.swift").unlink() + (root / "TeraPublicAPITests").rmdir() + with self.assertRaises(inventory.InventoryError): + inventory.source_files(root) + + def test_current_quality_and_test_lanes_share_complete_inventory(self): + root = Path(__file__).resolve().parent.parent + _, python = inventory.source_files(root) + paths = [path.relative_to(root).as_posix() for path in python] + self.assertEqual(inventory.command(root, "format")[3:], paths) + self.assertEqual(inventory.command(root, "lint")[2:], paths) + self.assertEqual( + inventory.command(root, "test")[3:], + [path for path in paths if Path(path).name.startswith("test_")], + ) + self.assertIn( + "scripts/test_maintainability_ratchet.py", inventory.command(root, "test") + ) + self.assertIn( + "scripts/test_ffi_native_cache.py", inventory.command(root, "test") + ) + self.assertIn( + "scripts/local-social-fixture.py", inventory.command(root, "lint") + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/test_fixture_lifetimes.py b/scripts/test_fixture_lifetimes.py @@ -0,0 +1,339 @@ +"""Real child and socket regressions for fixture elapsed bounds and cleanup.""" + +import hashlib +import http.client +import json +import os +import socket +import subprocess +import sys +import tempfile +import threading +import time +import unittest +from contextlib import contextmanager +from pathlib import Path +from unittest import mock + +from scripts import fixture_connections as connections +from scripts import fixture_tool_dispatch as dispatch +from scripts import test_local_social_fixture as support + +fixture = support.fixture + + +def wait_for(predicate, seconds=5): + until = time.monotonic() + seconds + while not predicate(): + if time.monotonic() >= until: + raise AssertionError("fixture readiness or cleanup did not complete") + time.sleep(0.005) + + +@contextmanager +def http_fixture(root, maximum=32): + control = root / "control" + control.touch() + state = fixture.FixtureState(root / "evidence.json", control, 0) + entered = threading.Event() + + class Handler(fixture.BlossomHandler): + def setup(self): + super().setup() + entered.set() + + Handler.state = state + server = fixture.ObservableLoopbackHTTPServer(("127.0.0.1", 0), Handler, state) + server.max_handlers = maximum + state.blossom_port = server.server_address[1] + thread = threading.Thread( + target=server.serve_forever, kwargs={"poll_interval": 0.01} + ) + thread.start() + try: + yield server, state, entered + finally: + server.shutdown() + server.server_close() + thread.join(5) + assert not thread.is_alive() + assert server.active_handlers == 0 + assert all(not item.is_alive() for item in server._handler_threads) + + +def upload_headers(body): + digest = hashlib.sha256(body).hexdigest() + _, authorization = support.mutate_bud11_event( + support.bud11_event(int(time.time()), digest), "none", int(time.time()) + ) + return { + "Authorization": authorization, + "Content-Type": "image/png", + "Content-Length": str(len(body)), + "X-SHA-256": digest, + } + + +def request_headers(body): + fields = "".join( + f"{key}: {value}\r\n" for key, value in upload_headers(body).items() + ) + return ("PUT /upload HTTP/1.1\r\nHost: 127.0.0.1\r\n" + fields + "\r\n").encode() + + +class SelectorLifetimeTests(unittest.TestCase): + def test_json_success_retains_bytes_and_closed_stdin(self): + command = [ + sys.executable, + "-c", + "import sys; assert sys.stdin.read() == ''; sys.stdout.write('{\"x\":1}')", + ] + self.assertEqual(fixture.run_json_command_bounded(command, 7), {"x": 1}) + with self.assertRaisesRegex(ValueError, "byte bound"): + fixture.run_json_command_bounded(command, 6) + + def test_nonzero_status_and_empty_output_remain_failures(self): + command = [sys.executable, "-c", "raise SystemExit(7)"] + with self.assertRaises(subprocess.CalledProcessError) as caught: + fixture.run_json_command_bounded(command, 128) + self.assertEqual(caught.exception.returncode, 7) + self.assertEqual(caught.exception.cmd, command) + with self.assertRaisesRegex(ValueError, "byte bound"): + fixture.run_json_command_bounded([sys.executable, "-c", "pass"], 128) + + def test_silent_partial_and_closed_pipe_children_expire_and_are_reaped(self): + bodies = ["pass", "os.write(1,b'{\"x\":')", "os.close(1); os.close(2)"] + for body in bodies: + with self.subTest(body=body), tempfile.TemporaryDirectory() as directory: + ready = Path(directory) / "ready" + command = [ + sys.executable, + "-c", + "import os,time; from pathlib import Path; Path(" + + repr(str(ready)) + + ").write_text(str(os.getpid())); " + + body + + "; time.sleep(60)", + ] + started = time.monotonic() + with self.assertRaises(subprocess.TimeoutExpired): + fixture.run_json_command_bounded(command, 128, timeout=3) + self.assertTrue(ready.exists(), "Actual child readiness is required") + self.assertLess(time.monotonic() - started, 6) + with self.assertRaises(ProcessLookupError): + os.kill(int(ready.read_text()), 0) + + def test_ignoring_descendant_is_settled_without_late_write_or_sentinel_loss(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + sentinel = subprocess.Popen( + [sys.executable, "-c", "import time; time.sleep(60)"], + stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + start_new_session=True, + ) + source = """import os,signal,time +from pathlib import Path +root=Path(ROOT_LITERAL) +root.joinpath('leader').write_text(str(os.getpid())) +if os.fork()==0: + signal.signal(signal.SIGTERM,signal.SIG_IGN) + root.joinpath('descendant').write_text(str(os.getpid())) + os.close(1);os.close(2) + time.sleep(10) + root.joinpath('late').write_text('unexpected') + time.sleep(60) +else: + while not root.joinpath('descendant').exists():time.sleep(.005) + os.close(1);os.close(2) + time.sleep(60) +""".replace("ROOT_LITERAL", repr(str(root))) + try: + with self.assertRaises(subprocess.TimeoutExpired): + fixture.run_json_command_bounded( + [sys.executable, "-c", source], 128, timeout=3 + ) + for name in ["leader", "descendant"]: + self.assertTrue((root / name).exists()) + with self.assertRaises(ProcessLookupError): + os.kill(int((root / name).read_text()), 0) + self.assertIsNone(sentinel.poll()) + self.assertFalse((root / "late").exists()) + time.sleep(0.1) + self.assertFalse((root / "late").exists()) + finally: + sentinel.terminate() + sentinel.wait(timeout=5) + + def test_caller_interruption_settles_the_actual_started_child(self): + with tempfile.TemporaryDirectory() as directory: + ready = Path(directory) / "ready" + command = [ + sys.executable, + "-c", + "import os,time; from pathlib import Path; Path(" + + repr(str(ready)) + + ").write_text(str(os.getpid())); time.sleep(60)", + ] + original = dispatch.CompilerCommand.read_ready + delivered = False + + def interrupted(capture, *args, **kwargs): + nonlocal delivered + original(capture, *args, **kwargs) + if ready.exists() and not delivered: + delivered = True + raise KeyboardInterrupt + + with mock.patch.object(dispatch.CompilerCommand, "read_ready", interrupted): + with self.assertRaises(KeyboardInterrupt): + fixture.run_json_command_bounded(command, 128, timeout=3) + self.assertTrue(ready.exists()) + with self.assertRaises(ProcessLookupError): + os.kill(int(ready.read_text()), 0) + + +class SocketLifetimeTests(unittest.TestCase): + def test_partial_frame_drips_share_one_elapsed_deadline(self): + client, server = socket.socketpair() + finished = threading.Event() + values = [] + + def read(): + values.append(fixture.read_frame(server)) + finished.set() + + with mock.patch.object(connections, "READ_SECONDS", 0.25): + thread = threading.Thread(target=read) + thread.start() + try: + client.sendall(b"\x81\x9f") + started = time.monotonic() + while not finished.is_set() and time.monotonic() - started < 1: + client.sendall(b"x") + time.sleep(0.03) + self.assertTrue(finished.wait(0.5)) + self.assertEqual(values, [None]) + self.assertLess(time.monotonic() - started, 1) + finally: + client.close() + thread.join(1) + server.close() + self.assertFalse(thread.is_alive()) + + +class HTTPLifetimeTests(unittest.TestCase): + def test_complete_authorized_body_remains_admitted(self): + body = b"canonical-bud11-photo" + headers = upload_headers(body) + with ( + tempfile.TemporaryDirectory() as directory, + http_fixture(Path(directory)) as (_, state, _), + ): + client = http.client.HTTPConnection( + "127.0.0.1", state.blossom_port, timeout=5 + ) + try: + client.request("PUT", "/upload", body=body, headers=headers) + response = client.getresponse() + self.assertEqual(response.status, 200) + self.assertEqual(json.loads(response.read())["size"], len(body)) + finally: + client.close() + + def test_stalled_and_partial_bodies_close_without_upload(self): + body = b"complete-body" + headers = request_headers(body) + for partial in [b"", body[:1]]: + with ( + self.subTest(partial=partial), + tempfile.TemporaryDirectory() as directory, + mock.patch.object(connections, "READ_SECONDS", 0.25), + http_fixture(Path(directory)) as (server, state, entered), + ): + with socket.create_connection( + server.server_address, timeout=5 + ) as client: + self.assertTrue(entered.wait(5)) + client.sendall(headers + partial) + client.settimeout(2) + self.assertEqual(client.recv(1), b"") + wait_for(lambda: server.active_handlers == 0) + evidence = json.loads((Path(directory) / "evidence.json").read_text()) + self.assertEqual(evidence["upload_attempts"], 0) + + def test_trickling_header_and_body_cannot_restart_deadline(self): + body = b"complete-body" + headers = request_headers(body) + for prefix, drip in [(b"G", b"E"), (headers, b"x")]: + with ( + self.subTest(prefix=prefix[:4]), + tempfile.TemporaryDirectory() as directory, + mock.patch.object(connections, "READ_SECONDS", 0.25), + http_fixture(Path(directory)) as (server, _, entered), + ): + with socket.create_connection( + server.server_address, timeout=5 + ) as client: + self.assertTrue(entered.wait(5)) + client.sendall(prefix) + started = time.monotonic() + while server.active_handlers and time.monotonic() - started < 1: + time.sleep(0.03) + try: + client.sendall(drip) + except OSError: + break + wait_for(lambda: server.active_handlers == 0, 0.5) + self.assertLess(time.monotonic() - started, 1) + + def test_active_teardown_preserves_unrelated_socket(self): + sentinel_a, sentinel_b = socket.socketpair() + client = None + try: + with ( + tempfile.TemporaryDirectory() as directory, + http_fixture(Path(directory)) as (server, _, entered), + ): + client = socket.create_connection(server.server_address, timeout=5) + self.assertTrue(entered.wait(5)) + self.assertEqual(server.active_handlers, 1) + self.assertEqual(server.active_handlers, 0) + client.settimeout(1) + self.assertEqual(client.recv(1), b"") + sentinel_a.sendall(b"alive") + sentinel_b.settimeout(1) + self.assertEqual(sentinel_b.recv(5), b"alive") + finally: + if client is not None: + client.close() + sentinel_a.close() + sentinel_b.close() + + def test_concurrent_admission_is_bounded_and_capacity_is_released(self): + with ( + tempfile.TemporaryDirectory() as directory, + http_fixture(Path(directory), maximum=1) as (server, _, entered), + ): + with socket.create_connection(server.server_address, timeout=5) as first: + self.assertTrue(entered.wait(5)) + with socket.create_connection( + server.server_address, timeout=5 + ) as excess: + self.assertEqual(excess.recv(1), b"") + self.assertEqual(server.active_handlers, 1) + first.shutdown(socket.SHUT_RDWR) + wait_for(lambda: server.active_handlers == 0) + entered.clear() + with socket.create_connection( + server.server_address, timeout=5 + ) as replacement: + self.assertTrue(entered.wait(5)) + self.assertEqual(server.active_handlers, 1) + replacement.sendall(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n") + self.assertTrue(replacement.recv(4096).startswith(b"HTTP/1.1 404")) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/test_fixture_test_policy.py b/scripts/test_fixture_test_policy.py @@ -0,0 +1,98 @@ +"""Approved fixed policy, genuine short controls and one invocation clock.""" + +from __future__ import annotations + +import json +import subprocess +import sys +import tempfile +import time +import unittest +from pathlib import Path +from unittest.mock import patch + +from scripts import fixture_test_policy as policy +from scripts.fixture_tool_dispatch import CompilerCommand + + +class FixtureTestPolicyTests(unittest.TestCase): + def test_fixed_successor_budgets_preserve_functional_and_cleanup_limits(self): + limits = policy.load_policy()["limits"] + self.assertEqual(limits["compiler"], 30) + self.assertEqual(limits["native_helper"], 30) + self.assertEqual(limits["release_command"], 120) + self.assertEqual(limits["reporter_child"], 120) + self.assertEqual(limits["selector_command"], 120) + self.assertEqual(limits["aggregate"], 600) + self.assertEqual(limits["partition"], 900) + self.assertEqual(limits["hostile_parser"], 3) + self.assertEqual(limits["leaf_cleanup"], 2) + self.assertEqual(limits["root_cleanup"], 4) + self.assertEqual(limits["xcode_readiness"], 30) + self.assertEqual(limits["xcode_wrapper"], 60) + + def test_unknown_policy_invalid_budgets_and_quiet_gate_are_rejected(self): + for change in ("id", "negative", "boolean", "infinite", "quiet", "short"): + with ( + self.subTest(change=change), + tempfile.TemporaryDirectory() as directory, + ): + value = policy.load_policy() + value.pop("source_sha256") + if change == "id": + value["id"] = "unapproved" + elif change in ("negative", "boolean", "infinite"): + value["limits"]["compiler"] = { + "negative": -1, + "boolean": True, + "infinite": float("inf"), + }[change] + elif change == "quiet": + value["quiet_workstation_required"] = True + else: + value["functional_short_intervals_unchanged"] = False + path = Path(directory) / "policy.json" + path.write_text(json.dumps(value)) + with ( + patch.object(policy, "POLICY_PATH", path), + self.assertRaises(ValueError), + ): + policy.load_policy() + + def test_duplicate_policy_keys_are_rejected(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "policy.json" + path.write_text('{"id":"first","id":"second"}') + with ( + patch.object(policy, "POLICY_PATH", path), + self.assertRaises(ValueError), + ): + policy.load_policy() + + def test_explicit_short_timeout_counts_spawn_delay_and_settles(self): + with tempfile.TemporaryDirectory() as directory: + command = CompilerCommand( + [sys.executable, "-S", "-c", "import time; time.sleep(30)"], + Path(directory), + 0.1, + budget="native_helper", + ) + actual_spawn = subprocess.Popen + + def delayed_spawn(*args, **kwargs): + time.sleep(0.2) + return actual_spawn(*args, **kwargs) + + with ( + patch.object(subprocess, "Popen", delayed_spawn), + self.assertRaises(subprocess.TimeoutExpired), + ): + command.execute() + self.assertEqual(command.record["timeout"], 0.1) + self.assertTrue(command.record["wait_reaped"]) + self.assertTrue(command.record["group_absent"]) + self.assertLessEqual(command.record["elapsed_seconds"], 2.3) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/test_fixture_tool_dispatch.py b/scripts/test_fixture_tool_dispatch.py @@ -0,0 +1,322 @@ +"""Actual native mock tools, argument fidelity and bounded compiler ownership.""" + +from __future__ import annotations + +import json +import os +import shutil +import signal +import subprocess +import sys +import tempfile +import time +import unittest +from pathlib import Path +from unittest.mock import patch + +from scripts.fixture_tool_dispatch import ( + CompilerCommand, + FixtureDispatchError, + FixtureToolDispatcher, +) +from scripts.fixture_test_policy import limit + + +class FixtureToolDispatchTests(unittest.TestCase): + def setUp(self) -> None: + evidence = os.environ.get("TERA_C138_TEST_EVIDENCE") + if evidence: + self.root = Path(tempfile.mkdtemp(prefix="tool-dispatch-", dir=evidence)) + else: + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + self.root = Path(temporary.name).resolve() + self.tools = self.root / "tools" + self.tools.mkdir() + self.dispatcher = FixtureToolDispatcher( + self.tools, ("uv", "cargo", "install", "xcrun", "xcodebuild", "rg") + ) + self.sequence = 0 + + def command( + self, argv: list[str], *, cwd: Path | None = None, timeout=None + ) -> subprocess.CompletedProcess: + self.sequence += 1 + command = CompilerCommand( + argv, + cwd or self.root, + limit("native_helper") if timeout is None else timeout, + budget="native_helper", + ) + try: + return command.execute() + finally: + prefix = self.root / f"command-{self.sequence:02}" + prefix.with_suffix(".json").write_text(json.dumps(command.record, indent=2)) + for name, raw in command.output.items(): + Path(str(prefix) + f".{name}").write_bytes(raw) + self.assert_settled(command.record) + + def assert_settled(self, record: dict) -> None: + self.assertTrue(record["wait_reaped"], record) + self.assertTrue(record["group_absent"], record) + self.assertLessEqual(record["elapsed_seconds"], record["timeout"] + 2.5) + with self.assertRaises(ProcessLookupError): + os.kill(record["pid"], 0) + with self.assertRaises(ProcessLookupError): + os.killpg(record["pgid"], 0) + + def body(self, role: str, status: int = 0) -> str: + return ( + "import json, os, sys\nfrom pathlib import Path\n" + f"Path({str(self.root / (role + '.json'))!r}).write_text(json.dumps({{" + "'argv': sys.argv, 'file': __file__, 'interpreter': sys.executable, " + "'cwd': os.getcwd(), 'path': os.environ['PATH'], " + "'literal': os.environ.get('FIXTURE_LITERAL'), " + "'stdin_closed': os.read(0, 1) == b'', 'pid': os.getpid(), " + "'pgid': os.getpgrp(), 'sid': os.getsid(0)}))\n" + f"sys.exit({status})\n" + ) + + def environment(self) -> dict: + return {"PATH": str(self.tools), "FIXTURE_LITERAL": "space $literal value"} + + def test_first_copies_preserve_actual_body_arguments_and_process_identity( + self, + ) -> None: + arguments = ["", "space argument", "$literal", "-m", "0644", "--quiet", "é"] + bodies = {} + for role, status in (("uv", 0), ("cargo", 7), ("install", 0)): + body = self.body(role, status) + bodies[role] = body + self.dispatcher.executable(role, body) + with patch.dict(os.environ, self.environment(), clear=True): + result = self.command([str(self.tools / role), *arguments]) + self.assertEqual(result.returncode, status, result.stderr) + observed = json.loads((self.root / f"{role}.json").read_text()) + record = json.loads( + (self.root / f"command-{self.sequence:02}.json").read_text() + ) + self.assertEqual( + observed["argv"], [str(self.tools / f"{role}.body.py"), *arguments] + ) + self.assertEqual(observed["file"], str(self.tools / f"{role}.body.py")) + self.assertEqual(observed["interpreter"], sys.executable) + self.assertEqual(observed["cwd"], str(self.root)) + self.assertEqual(observed["path"], str(self.tools)) + self.assertEqual(observed["literal"], self.environment()["FIXTURE_LITERAL"]) + self.assertTrue(observed["stdin_closed"]) + self.assertEqual( + (observed["pid"], observed["pgid"], observed["sid"]), + (record["pid"],) * 3, + ) + for role, body in bodies.items(): + self.assertEqual( + (self.tools / f"{role}.body.py").read_bytes(), body.encode() + ) + compiler = json.loads( + (self.dispatcher.directory / "compilation.json").read_text() + ) + self.assertEqual(compiler["command"]["timeout"], limit("compiler")) + self.assertEqual(compiler["command"]["exit"], 0) + self.assert_settled(compiler["command"]) + independent_tools = self.root / "independent-tools" + independent_tools.mkdir() + independent = FixtureToolDispatcher(independent_tools, self.dispatcher.roles) + independent.executable("uv", self.body("independent")) + for field in ("source", "binary"): + self.assertEqual( + independent.record[field]["sha256"], compiler[field]["sha256"] + ) + self.assertNotEqual( + independent.record["command"]["pid"], compiler["command"]["pid"] + ) + self.assertNotEqual( + independent.binary.stat().st_ino, self.dispatcher.binary.stat().st_ino + ) + self.assert_settled(independent.record["command"]) + (self.root / "stable-compilations.json").write_text( + json.dumps([compiler, independent.record], indent=2) + "\n" + ) + with patch.dict(os.environ, {"PATH": str(independent_tools)}, clear=True): + result = self.command([str(independent_tools / "uv"), *arguments]) + self.assertEqual(result.returncode, 0, result.stderr) + observed = json.loads((self.root / "independent.json").read_text()) + self.assertEqual( + observed["argv"], [str(independent_tools / "uv.body.py"), *arguments] + ) + record = json.loads( + (self.root / f"command-{self.sequence:02}.json").read_text() + ) + self.assertEqual( + (observed["pid"], observed["pgid"], observed["sid"]), + (record["pid"],) * 3, + ) + self.assertEqual(observed["interpreter"], sys.executable) + self.assertEqual(observed["cwd"], str(self.root)) + self.assertEqual(observed["path"], str(independent_tools)) + self.assertTrue(observed["stdin_closed"]) + + def test_original_shell_forms_and_copy_permission_remain_independent(self) -> None: + for role in ("xcrun", "uv", "xcodebuild", "rg"): + self.dispatcher.executable(role, self.body(role)) + identities = [ + (self.tools / role).stat().st_ino + for role in ("xcrun", "uv", "xcodebuild", "rg") + ] + self.assertEqual(len(set(identities)), 4) + self.assertTrue( + all( + not (self.tools / role).is_symlink() + for role in ("xcrun", "uv", "xcodebuild", "rg") + ) + ) + (self.tools / "xcodebuild").chmod(0o600) + (self.tools / "rg").unlink() + for shell, role in (("/bin/sh", "uv"), ("/bin/bash", "xcrun")): + script = self.root / f"{role}.sh" + script.write_text(f"{role} \"space argument\" '$literal'\n") + with patch.dict(os.environ, self.environment(), clear=True): + result = self.command([shell, str(script)]) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual( + json.loads((self.root / f"{role}.json").read_text())["argv"][1:], + ["space argument", "$literal"], + ) + with patch.dict(os.environ, self.environment(), clear=True): + for role, expected in (("xcodebuild", 126), ("rg", 127)): + with self.subTest(role=role): + result = self.command(["/bin/sh", "-c", role]) + self.assertEqual(result.returncode, expected) + + def test_missing_body_unknown_role_and_foreign_path_do_not_execute(self) -> None: + self.dispatcher.executable("uv", self.body("uv")) + (self.tools / "uv.body.py").unlink() + unknown = self.tools / "unknown" + shutil.copyfile(self.dispatcher.binary, unknown) + unknown.chmod(0o700) + with patch.dict(os.environ, self.environment(), clear=True): + self.assertEqual(self.command([str(self.tools / "uv")]).returncode, 66) + self.assertEqual(self.command([str(unknown)]).returncode, 64) + foreign = self.root / "foreign" + foreign.mkdir() + (foreign / "uv.body.py").write_text(self.body("uv")) + with patch.dict(os.environ, {"PATH": str(foreign)}, clear=True): + self.assertEqual(self.command([str(self.tools / "uv")]).returncode, 64) + self.dispatcher.executable("uv", self.body("uv")) + self.dispatcher.executable("cargo", self.body("cargo")) + foreign_image = foreign / "uv" + shutil.copyfile(self.dispatcher.binary, foreign_image) + foreign_image.chmod(0o700) + for image, argv0 in ( + (foreign_image, self.tools / "uv"), + (self.tools / "uv", self.tools / "cargo"), + ): + with self.subTest(image=str(image), argv0=str(argv0)): + script = self.root / f"exec-image-{self.sequence:02}.py" + intent = script.with_suffix(".json") + native_argv = [str(argv0), "space $literal"] + script.write_text( + "import json, os\nfrom pathlib import Path\n" + f"Path({str(intent)!r}).write_text(json.dumps({{'image': {str(image)!r}, 'argv': {native_argv!r}, 'pid': os.getpid(), 'pgid': os.getpgrp()}}))\n" + f"os.execv({str(image)!r}, {native_argv!r})\n" + ) + with patch.dict(os.environ, self.environment(), clear=True): + result = self.command([sys.executable, str(script)]) + self.assertEqual(result.returncode, 64, result.stderr) + observed = json.loads(intent.read_text()) + record = json.loads( + (self.root / f"command-{self.sequence:02}.json").read_text() + ) + self.assertEqual(observed["pid"], record["pid"]) + self.assertEqual(observed["pgid"], record["pgid"]) + alias = self.root / "tools-alias" + alias.symlink_to(self.tools, target_is_directory=True) + with patch.dict(os.environ, {"PATH": str(alias)}, clear=True): + self.assertEqual(self.command([str(self.tools / "uv")]).returncode, 64) + self.assertFalse((self.root / "uv.json").exists()) + self.assertFalse((self.root / "cargo.json").exists()) + with self.assertRaises(FixtureDispatchError): + self.dispatcher.executable("unknown", "raise AssertionError\n") + + def test_real_handler_signal_is_the_actual_process_status(self) -> None: + self.dispatcher.executable( + "cargo", + "import json, os, signal\nfrom pathlib import Path\n" + f"Path({str(self.root / 'signal.json')!r}).write_text(json.dumps({{'pid': os.getpid(), 'pgid': os.getpgrp()}}))\n" + "os.kill(os.getpid(), signal.SIGTERM)\n", + ) + with patch.dict(os.environ, self.environment(), clear=True): + result = self.command([str(self.tools / "cargo")]) + self.assertEqual(result.returncode, -signal.SIGTERM) + observed = json.loads((self.root / "signal.json").read_text()) + record = json.loads((self.root / "command-01.json").read_text()) + self.assertEqual(observed["pid"], record["pid"]) + self.assertEqual(observed["pgid"], record["pgid"]) + + def test_compiler_absence_and_nonzero_fail_closed_with_exact_raw_record( + self, + ) -> None: + for case, compiler in (("absent", None), ("nonzero", "/usr/bin/false")): + tools = self.root / case + tools.mkdir() + with ( + self.subTest(case=case), + patch( + "scripts.fixture_tool_dispatch.shutil.which", return_value=compiler + ), + ): + with self.assertRaises(FixtureDispatchError): + FixtureToolDispatcher(tools, ("uv",)) + directory = tools / ".fixture-dispatch" + record = json.loads((directory / "compilation.json").read_text()) + self.assertFalse((directory / "dispatch").exists()) + if compiler: + self.assertEqual(record["command"]["exit"], 1) + self.assert_settled(record["command"]) + self.assertEqual((directory / "compiler.stdout").read_bytes(), b"") + self.assertEqual((directory / "compiler.stderr").read_bytes(), b"") + + def test_compiler_timeout_kills_ignoring_descendant_and_prevents_late_write( + self, + ) -> None: + ready, late = self.root / "compiler-child.json", self.root / "late.txt" + script = self.root / "compiler-control.py" + script.write_text( + "import json, os, signal, subprocess, sys, time\nfrom pathlib import Path\n" + "signal.signal(signal.SIGTERM, signal.SIG_IGN)\n" + f"child = subprocess.Popen([sys.executable, '-c', {('import os, signal, time; from pathlib import Path; signal.signal(signal.SIGTERM, signal.SIG_IGN); time.sleep(1); Path(' + repr(str(late)) + ').write_text("late")')!r}])\n" + f"Path({str(ready)!r}).write_text(json.dumps({{'pid': os.getpid(), 'child': child.pid, 'pgid': os.getpgrp(), 'stdin_closed': os.read(0, 1) == b''}}))\n" + "time.sleep(10)\n" + ) + with self.assertRaises(subprocess.TimeoutExpired): + self.command([sys.executable, str(script)], timeout=0.5) + observed = json.loads(ready.read_text()) + self.assertTrue(observed["stdin_closed"]) + with self.assertRaises(ProcessLookupError): + os.kill(observed["child"], 0) + time.sleep(0.5) + self.assertFalse(late.exists()) + record = json.loads((self.root / "command-01.json").read_text()) + self.assertIn(signal.SIGKILL, [row["signal"] for row in record["signals"]]) + + def test_compiler_overflow_caps_retained_bytes_and_settles(self) -> None: + for name, descriptor in (("stdout", 1), ("stderr", 2)): + with self.subTest(stream=name): + script = self.root / f"overflow-{name}.py" + script.write_text( + f"import os\nos.write({descriptor}, b'x' * (2 * 1024 * 1024))\n" + ) + with self.assertRaises(FixtureDispatchError): + self.command([sys.executable, str(script)]) + prefix = self.root / f"command-{self.sequence:02}" + self.assertEqual( + len(Path(str(prefix) + f".{name}").read_bytes()), + CompilerCommand.LIMITS[name], + ) + self.assertEqual( + json.loads(prefix.with_suffix(".json").read_text())[ + "output_excess" + ], + name, + ) diff --git a/scripts/test_local_social_fixture.py b/scripts/test_local_social_fixture.py @@ -54,22 +54,28 @@ def sign_bud11_event(event: dict) -> dict: strict=True, ) ) - nonce = int.from_bytes( - fixture.tagged_hash("BIP0340/nonce", masked_secret + public_key + message), - "big", - ) % fixture.SECP256K1_ORDER + nonce = ( + int.from_bytes( + fixture.tagged_hash("BIP0340/nonce", masked_secret + public_key + message), + "big", + ) + % fixture.SECP256K1_ORDER + ) assert nonce != 0 nonce_point = fixture.point_multiply(nonce, fixture.SECP256K1_GENERATOR) assert nonce_point is not None if nonce_point[1] & 1: nonce = fixture.SECP256K1_ORDER - nonce - challenge = int.from_bytes( - fixture.tagged_hash( - "BIP0340/challenge", - nonce_point[0].to_bytes(32, "big") + public_key + message, - ), - "big", - ) % fixture.SECP256K1_ORDER + challenge = ( + int.from_bytes( + fixture.tagged_hash( + "BIP0340/challenge", + nonce_point[0].to_bytes(32, "big") + public_key + message, + ), + "big", + ) + % fixture.SECP256K1_ORDER + ) signature = nonce_point[0].to_bytes(32, "big") + ( (nonce + challenge * secret) % fixture.SECP256K1_ORDER ).to_bytes(32, "big") @@ -320,7 +326,9 @@ class LocalSocialFixtureTests(unittest.TestCase): fixture.verify_bip340(public_key, message, signature[:-1] + b"\x00") ) - def test_bud11_corpus_is_exact_bounded_and_contains_no_sensitive_evidence(self) -> None: + def test_bud11_corpus_is_exact_bounded_and_contains_no_sensitive_evidence( + self, + ) -> None: path = Path("test-fixtures/bud11-upload-authorization-mutations.v1.json") raw, corpus = fixture.load_bud11_mutation_corpus(path) self.assertLessEqual(len(raw), fixture.MAX_JSON_BYTES) @@ -349,7 +357,9 @@ class LocalSocialFixtureTests(unittest.TestCase): with self.assertRaisesRegex(ValueError, "duplicate JSON member"): fixture.load_bud11_mutation_corpus(duplicate) - def test_bud11_scalar_bounds_and_server_domain_grammar_are_fail_closed(self) -> None: + def test_bud11_scalar_bounds_and_server_domain_grammar_are_fail_closed( + self, + ) -> None: self.assertEqual(fixture.canonical_unsigned_decimal("0"), 0) self.assertEqual( fixture.canonical_unsigned_decimal(str(0xFFFF_FFFF_FFFF_FFFF)), @@ -369,9 +379,7 @@ class LocalSocialFixtureTests(unittest.TestCase): "media-.example", ]: self.assertFalse(fixture.valid_bud11_server_domain(value), value) - oversized = "Nostr " + "a" * ( - fixture.BUD11_AUTHORIZATION_ENCODED_MAX_BYTES + 1 - ) + oversized = "Nostr " + "a" * (fixture.BUD11_AUTHORIZATION_ENCODED_MAX_BYTES + 1) self.assertFalse( fixture.valid_blossom_authorization( oversized, @@ -381,7 +389,9 @@ class LocalSocialFixtureTests(unittest.TestCase): ) ) - def test_bud11_mutation_corpus_matches_strict_admission_and_relay_denial(self) -> None: + def test_bud11_mutation_corpus_matches_strict_admission_and_relay_denial( + self, + ) -> None: _, corpus = fixture.load_bud11_mutation_corpus( Path("test-fixtures/bud11-upload-authorization-mutations.v1.json") ) @@ -421,11 +431,21 @@ class LocalSocialFixtureTests(unittest.TestCase): control.touch() state = fixture.FixtureState(root / "evidence.json", control, 0) with blossom_server(fixture.BlossomHandler, state): - cases = [(v, "/upload", digest) for v in corpus["vectors"] if v["surface"] == "http"] + cases = [ + (v, "/upload", digest) + for v in corpus["vectors"] + if v["surface"] == "http" + ] valid = next(v for v, _, _ in cases if v["expected_accepted"]) - cases += [(valid, f"/{digest}.png", digest), (valid, "/upload", ""), (valid, "/upload", digest.upper())] + cases += [ + (valid, f"/{digest}.png", digest), + (valid, "/upload", ""), + (valid, "/upload", digest.upper()), + ] for vector, path, supplied_hash in cases: - with self.subTest(vector=vector["id"], path=path, supplied_hash=supplied_hash): + with self.subTest( + vector=vector["id"], path=path, supplied_hash=supplied_hash + ): _, header = mutate_bud11_event( bud11_event(now, digest), vector["mutation"], now ) @@ -448,7 +468,9 @@ class LocalSocialFixtureTests(unittest.TestCase): connection.close() self.assertEqual( response.status == 200, - vector["expected_accepted"] and path == "/upload" and supplied_hash == digest, + vector["expected_accepted"] + and path == "/upload" + and supplied_hash == digest, ) evidence = json.loads((root / "evidence.json").read_text(encoding="utf-8")) self.assertEqual(evidence["accepted_uploads"], 1) @@ -489,9 +511,7 @@ class LocalSocialFixtureTests(unittest.TestCase): Path("test-fixtures/bud11-upload-authorization-mutations.v1.json") ) _, corpus_schema = fixture.load_schema_file( - Path( - "test-fixtures/bud11-upload-authorization-mutations.v1.schema.json" - ), + Path("test-fixtures/bud11-upload-authorization-mutations.v1.schema.json"), "https://radroots.org/schemas/ios/bud11-upload-authorization-mutations.v1.schema.json", ) fixture.validate_schema_instance(corpus_schema, corpus, "BUD-11 corpus") @@ -608,9 +628,7 @@ class LocalSocialFixtureTests(unittest.TestCase): with tempfile.TemporaryDirectory() as directory: root = Path(directory) control = root / "control.json" - state = fixture.FixtureState( - root / "evidence.json", control, 21100, suite - ) + state = fixture.FixtureState(root / "evidence.json", control, 21100, suite) self.write_persona_control(control, "P01") with mock.patch.object( fixture, "valid_blossom_authorization", return_value=True @@ -621,9 +639,7 @@ class LocalSocialFixtureTests(unittest.TestCase): self.write_persona_control(control, "P02") self.assertIsNotNone(state.retrieve(digest)) - evidence = json.loads( - (root / "evidence.json").read_text(encoding="utf-8") - ) + evidence = json.loads((root / "evidence.json").read_text(encoding="utf-8")) self.assertEqual(evidence["retrievals"], 1) self.assertEqual(evidence["personas"][0]["retrievals"], 1) self.assertEqual(evidence["personas"][1]["retrievals"], 0) @@ -642,18 +658,12 @@ class LocalSocialFixtureTests(unittest.TestCase): root = Path(directory) control = root / "control.json" self.write_persona_control(control, "P05") - state = fixture.FixtureState( - root / "evidence.json", control, 21100, suite - ) - with mock.patch.object( - fixture, "classify_attempt", return_value=attempt - ): + state = fixture.FixtureState(root / "evidence.json", control, 21100, suite) + with mock.patch.object(fixture, "classify_attempt", return_value=attempt): self.assertIsNone(state._publish_persona_event(event)) self.assertTrue(state._publish_persona_event(event)) - evidence = json.loads( - (root / "evidence.json").read_text(encoding="utf-8") - ) + evidence = json.loads((root / "evidence.json").read_text(encoding="utf-8")) self.assertEqual(evidence["expected_failure_rejections"], 1) self.assertEqual(evidence["accepted_events"], 1) @@ -722,7 +732,9 @@ class LocalSocialFixtureTests(unittest.TestCase): value, suite, require_measured_network=True ) - def test_attempt_evidence_rejects_one_field_identity_and_outcome_drift(self) -> None: + def test_attempt_evidence_rejects_one_field_identity_and_outcome_drift( + self, + ) -> None: suite, attachments = self.persona_attempt_attachments() _, canonical = attachments[0] mutations = ( @@ -754,7 +766,9 @@ class LocalSocialFixtureTests(unittest.TestCase): changed, suite, require_measured_network=True ) - def test_persona_result_v2_is_reconstructed_only_from_measured_attempts(self) -> None: + def test_persona_result_v2_is_reconstructed_only_from_measured_attempts( + self, + ) -> None: suite, attachments = self.persona_attempt_attachments() result = fixture.reconstruct_persona_result_v2( suite, @@ -774,9 +788,7 @@ class LocalSocialFixtureTests(unittest.TestCase): self.assertEqual(result["non_loopback_attempts"], 0) self.assertEqual(len(result["attachments"]), 15) _, attempt_schema = fixture.load_schema_file( - Path( - "test-fixtures/local-social-persona-attempt-evidence.v1.schema.json" - ), + Path("test-fixtures/local-social-persona-attempt-evidence.v1.schema.json"), "https://radroots.org/schemas/ios/local-social-persona-attempt-evidence.v1.schema.json", ) for _, attempt in attachments: @@ -824,9 +836,7 @@ class LocalSocialFixtureTests(unittest.TestCase): ], "testPlanConfigurations": [], } - self.assertEqual( - fixture.exact_persona_test_node(tests)["result"], "Passed" - ) + self.assertEqual(fixture.exact_persona_test_node(tests)["result"], "Passed") for mutation in ( lambda value: value["testNodes"][0]["children"][0].update( {"result": "Failed"} @@ -850,9 +860,7 @@ class LocalSocialFixtureTests(unittest.TestCase): zip(fixture.PERSONA_ATTACHMENT_NAMES, attachments, strict=True) ): exported = "exported-" + name - suggested = ( - f"{name[:-5]}_0_00000000-0000-0000-0000-{index:012X}.json" - ) + suggested = f"{name[:-5]}_0_00000000-0000-0000-0000-{index:012X}.json" (root / exported).write_bytes(raw) rows.append( { @@ -871,9 +879,7 @@ class LocalSocialFixtureTests(unittest.TestCase): "attachments": rows, } ] - (root / "manifest.json").write_text( - json.dumps(manifest), encoding="utf-8" - ) + (root / "manifest.json").write_text(json.dumps(manifest), encoding="utf-8") loaded = fixture.load_exported_persona_attachments( root, suite, require_measured_network=True ) @@ -891,20 +897,14 @@ class LocalSocialFixtureTests(unittest.TestCase): rows[0]["suggestedHumanReadableName"] = rows[1][ "suggestedHumanReadableName" ] - (root / "manifest.json").write_text( - json.dumps(manifest), encoding="utf-8" - ) + (root / "manifest.json").write_text(json.dumps(manifest), encoding="utf-8") with self.assertRaises(ValueError): fixture.load_exported_persona_attachments( root, suite, require_measured_network=True ) - rows[0]["suggestedHumanReadableName"] = ( - "radroots-local-social-P01-A01.json" - ) - (root / "manifest.json").write_text( - json.dumps(manifest), encoding="utf-8" - ) + rows[0]["suggestedHumanReadableName"] = "radroots-local-social-P01-A01.json" + (root / "manifest.json").write_text(json.dumps(manifest), encoding="utf-8") with self.assertRaises(ValueError): fixture.load_exported_persona_attachments( root, suite, require_measured_network=True @@ -936,8 +936,7 @@ class LocalSocialFixtureTests(unittest.TestCase): fixture.PERSONA_ATTACHMENT_NAMES[1:], 1 ): other_name = ( - f"{canonical_name[:-5]}_0_" - f"00000000-0000-0000-0000-{index:012X}.json" + f"{canonical_name[:-5]}_0_00000000-0000-0000-0000-{index:012X}.json" ) other_exported = f"exported-{index}.json" (root / other_exported).write_bytes(attachments[index][0]) @@ -1014,9 +1013,7 @@ class LocalSocialFixtureTests(unittest.TestCase): self.assertNotIn("spawn", commands[0] + commands[1]) patch_summary = copy.deepcopy(result_summary) - patch_summary["devicesAndConfigurations"][0]["device"]["osVersion"] = ( - "26.5.1" - ) + patch_summary["devicesAndConfigurations"][0]["device"]["osVersion"] = "26.5.1" patch_devices = { "devices": { "com.apple.CoreSimulator.SimRuntime.iOS-26-5-1": [ @@ -1154,9 +1151,7 @@ class LocalSocialFixtureTests(unittest.TestCase): ], } - self.assertIs( - fixture.classify_attempt(event, {"P01-A02": attempt}), attempt - ) + self.assertIs(fixture.classify_attempt(event, {"P01-A02": attempt}), attempt) for mutation in ( lambda value: value.update({"content": marker}), lambda value: value.update({"content": f"prefix {marker}\n{url}"}), @@ -1167,15 +1162,11 @@ class LocalSocialFixtureTests(unittest.TestCase): ): changed = copy.deepcopy(event) mutation(changed) - self.assertIsNone( - fixture.classify_attempt(changed, {"P01-A02": attempt}) - ) + self.assertIsNone(fixture.classify_attempt(changed, {"P01-A02": attempt})) text_attempt = {"flow": "Update", "marker": "rr-p01-a01-update"} embedded = {"content": "prefix rr-p01-a01-update", "tags": []} - self.assertIsNone( - fixture.classify_attempt(embedded, {"P01-A01": text_attempt}) - ) + self.assertIsNone(fixture.classify_attempt(embedded, {"P01-A01": text_attempt})) if __name__ == "__main__": diff --git a/scripts/test_local_social_relay.py b/scripts/test_local_social_relay.py @@ -35,7 +35,7 @@ def relay_socket(wait): class ScaledReadSocket: def settimeout(self, value): - assert value == 15 + assert 0 < value <= 15 server.settimeout(0.05) def recv(self, length): diff --git a/scripts/test_maintainability_ratchet.py b/scripts/test_maintainability_ratchet.py @@ -20,8 +20,17 @@ class MaintainabilityRatchetTests(unittest.TestCase): (root / "Tera").mkdir() (root / "TeraTests").mkdir() (root / "TeraUITests").mkdir() + (root / "TeraPublicAPITests").mkdir() (root / "scripts").mkdir() + (root / "scripts/legacy_fixture_writers").mkdir() (root / "test-fixtures").mkdir() + (root / "Package.swift").write_text("// package\n", encoding="utf-8") + for relative in ratchet.ORIGINAL_BOUNDED_MODULES: + path = root / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text( + "// bounded\n" if path.suffix == ".swift" else "# bounded\n" + ) (root / "Tera/App.swift").write_text("struct App {}\n", encoding="utf-8") (root / "scripts/check.py").write_text( "def check():\n return True\n", encoding="utf-8" @@ -42,7 +51,13 @@ class MaintainabilityRatchetTests(unittest.TestCase): "swift_file_exception": [], "python_file_exception": [], "python_complexity_exception": [], - "bounded_module": ["Tera/App.swift", "scripts/check.py"], + "bounded_module": sorted( + { + *ratchet.ORIGINAL_BOUNDED_MODULES, + "Tera/App.swift", + "scripts/check.py", + } + ), } baseline.update(overrides) (root / ratchet.BASELINE_PATH).write_text( @@ -62,9 +77,11 @@ class MaintainabilityRatchetTests(unittest.TestCase): def test_exception_cannot_grow_or_become_stale(self) -> None: temporary, root = self._repository() with temporary: - path = root / "Tera/App.swift" + path = root / "Tera/Runtime/TeraRuntimeClient.swift" path.write_text("x\n" * 602, encoding="utf-8") - exception = [{"path": "Tera/App.swift", "maximum_lines": 601}] + exception = [ + {"path": "Tera/Runtime/TeraRuntimeClient.swift", "maximum_lines": 601} + ] self._write_baseline(root, swift_file_exception=exception) with self.assertRaisesRegex(ratchet.MaintainabilityError, "regressed"): ratchet.verify(root) @@ -119,13 +136,20 @@ class MaintainabilityRatchetTests(unittest.TestCase): def test_bounded_module_must_exist_and_remain_small(self) -> None: temporary, root = self._repository() with temporary: - self._write_baseline(root, bounded_module=["Tera/Missing.swift"]) + self._write_baseline( + root, + bounded_module=sorted( + {*ratchet.ORIGINAL_BOUNDED_MODULES, "Tera/Missing.swift"} + ), + ) with self.assertRaisesRegex(ratchet.MaintainabilityError, "absent"): ratchet.verify(root) - (root / "Tera/App.swift").write_text("x\n" * 601, encoding="utf-8") + path = "Tera/Runtime/TeraRuntimeClient.swift" + (root / path).write_text("x\n" * 601, encoding="utf-8") self._write_baseline( root, - swift_file_exception=[{"path": "Tera/App.swift", "maximum_lines": 601}], + swift_file_exception=[{"path": path, "maximum_lines": 601}], + bounded_module=sorted({*ratchet.ORIGINAL_BOUNDED_MODULES, path}), ) with self.assertRaisesRegex(ratchet.MaintainabilityError, "bounded"): ratchet.verify(root) @@ -149,6 +173,95 @@ class MaintainabilityRatchetTests(unittest.TestCase): rows, key_name="path", ceiling_name="maximum_lines" ) + def test_joint_source_and_baseline_cannot_add_an_exception(self) -> None: + temporary, root = self._repository() + with temporary: + (root / "Tera/New.swift").write_text("x\n" * 601) + self._write_baseline( + root, + swift_file_exception=[{"path": "Tera/New.swift", "maximum_lines": 601}], + ) + with self.assertRaisesRegex( + ratchet.MaintainabilityError, "approved exception" + ): + ratchet.verify(root) + + def test_joint_source_and_baseline_cannot_increase_original_ceilings(self) -> None: + cases = [ + ( + "swift_file_exception", + "Tera/Runtime/TeraRuntimeClient.swift", + 821, + "x\n", + ), + ( + "python_file_exception", + "scripts/local-social-fixture.py", + 2627, + "x = 1\n", + ), + ] + for category, path, ceiling, line in cases: + with self.subTest(category=category): + temporary, root = self._repository() + with temporary: + (root / path).write_text(line * ceiling) + self._write_baseline( + root, **{category: [{"path": path, "maximum_lines": ceiling}]} + ) + with self.assertRaisesRegex( + ratchet.MaintainabilityError, "approved exception" + ): + ratchet.verify(root) + + def test_joint_complexity_and_baseline_increase_is_rejected(self) -> None: + temporary, root = self._repository() + with temporary: + branches = "".join( + f" if value == {index}:\n return {index}\n" + for index in range(16) + ) + (root / "scripts/local-social-fixture.py").write_text( + "def valid_nostr_event(value):\n" + branches + " return -1\n" + ) + self._write_baseline( + root, + python_complexity_exception=[ + { + "function": "scripts/local-social-fixture.py:valid_nostr_event", + "maximum_complexity": 17, + } + ], + ) + with self.assertRaisesRegex( + ratchet.MaintainabilityError, "approved exception" + ): + ratchet.verify(root) + + def test_original_bounded_entry_cannot_be_removed(self) -> None: + temporary, root = self._repository() + with temporary: + original = set(ratchet.ORIGINAL_BOUNDED_MODULES) + original.remove("scripts/package_privacy.py") + self._write_baseline(root, bounded_module=sorted(original)) + with self.assertRaisesRegex( + ratchet.MaintainabilityError, "original bounded" + ): + ratchet.verify(root) + + def test_original_exception_can_decrease_and_disappear(self) -> None: + temporary, root = self._repository() + with temporary: + path = "Tera/Runtime/TeraRuntimeClient.swift" + (root / path).write_text("x\n" * 810) + self._write_baseline( + root, swift_file_exception=[{"path": path, "maximum_lines": 810}] + ) + ratchet.verify(root) + (root / path).write_text("x\n" * 600) + self._write_baseline(root) + ratchet.verify(root) + if __name__ == "__main__": unittest.main() diff --git a/scripts/test_persona_attachment_selection.py b/scripts/test_persona_attachment_selection.py @@ -86,7 +86,7 @@ class PersonaAttachmentExtractionTests(unittest.TestCase): } with ( mock.patch.object(fixture, "run_json_command_bounded", return_value=tests), - mock.patch.object(fixture.subprocess, "run", side_effect=run), + mock.patch.object(fixture, "run_selector_command", side_effect=run), ): return fixture.extract_persona_attempt_attachments( Path("synthetic.xcresult"), self.suite, require_measured_network=True diff --git a/scripts/test_release_evidence.py b/scripts/test_release_evidence.py @@ -4,18 +4,193 @@ from __future__ import annotations import json import os +import selectors +import shlex +import signal import shutil import subprocess import sys import tempfile +import time import unittest from pathlib import Path +from scripts.fixture_tool_dispatch import FixtureToolDispatcher +from scripts.fixture_test_policy import limit as fixture_limit, load_policy + SCRIPTS = Path(__file__).resolve().parent if str(SCRIPTS) not in sys.path: sys.path.insert(0, str(SCRIPTS)) +class ReleaseCommand: + """Bounded capture and cleanup while the unreaped leader reserves its PGID.""" + + LIMITS = {"stdout": 1024 * 1024, "stderr": 64 * 1024} + + def __init__(self, argv: list[str], cwd: Path, environment: dict, timeout: float): + if not 0 < timeout <= fixture_limit("release_command"): + raise ValueError("release fixture deadline must be within 30 seconds") + self.argv, self.cwd, self.environment, self.timeout = ( + argv, + cwd, + environment, + timeout, + ) + self.output = {name: bytearray() for name in self.LIMITS} + self.selector = selectors.DefaultSelector() + self.process = None + self.started = time.monotonic() + self.record = { + "argv": argv, + "cwd": str(cwd), + "timeout": timeout, + "policy_id": load_policy()["id"], + "policy_sha256": load_policy()["source_sha256"], + "started_ns": time.time_ns(), + "signals": [], + "wait_reaped": False, + "group_absent": False, + } + + def execute(self) -> subprocess.CompletedProcess: + try: + self.process = subprocess.Popen( + self.argv, + cwd=self.cwd, + env=self.environment, + stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + bufsize=0, + start_new_session=True, + ) + self.record.update(pid=self.process.pid, pgid=self.process.pid) + for name in self.LIMITS: + stream = getattr(self.process, name) + os.set_blocking(stream.fileno(), False) + self.selector.register(stream, selectors.EVENT_READ, name) + self.capture(self.started + self.timeout) + except BaseException as error: + self.record["error"] = type(error).__name__ + raise + finally: + try: + if self.process is not None: + self.settle() + finally: + self.selector.close() + self.record["elapsed_seconds"] = time.monotonic() - self.started + self.record["ended_ns"] = time.time_ns() + return subprocess.CompletedProcess( + self.argv, + self.process.returncode, + bytes(self.output["stdout"]), + bytes(self.output["stderr"]), + ) + + def exited(self) -> bool: + # WNOWAIT leaves the leader's PID reserved until the last group signal. + state = os.waitid( + os.P_PID, self.process.pid, os.WEXITED | os.WNOHANG | os.WNOWAIT + ) + return state is not None and state.si_pid == self.process.pid + + def capture(self, deadline: float) -> None: + while self.selector.get_map() or not self.exited(): + remaining = deadline - time.monotonic() + if remaining <= 0: + raise subprocess.TimeoutExpired( + self.argv, + self.timeout, + output=bytes(self.output["stdout"]), + stderr=bytes(self.output["stderr"]), + ) + self.read_ready(min(remaining, 0.05), reject_excess=True) + + def read_ready(self, timeout: float, *, reject_excess: bool) -> None: + for key, _ in self.selector.select(timeout): + name = key.data + available = self.LIMITS[name] - len(self.output[name]) + size = min(65536, available + 1) if reject_excess else 65536 + data = os.read(key.fd, size) + if not data: + self.selector.unregister(key.fileobj) + key.fileobj.close() + continue + self.output[name].extend(data[:available]) + if len(data) > available: + self.record.setdefault("output_excess", name) + if reject_excess: + raise RuntimeError(f"release fixture {name} exceeds capture limit") + + def group_present(self) -> bool: + try: + os.killpg(self.process.pid, 0) + except ProcessLookupError: + return False + except PermissionError: + # Darwin can report EPERM for an unreaped, exited group leader. + return True + return True + + def send(self, number: int) -> None: + # No wait/poll/reap is permitted before the final signal: PID reuse is fenced. + try: + os.killpg(self.process.pid, number) + except ProcessLookupError: + return + except PermissionError: + self.record.setdefault("signal_errors", []).append( + {"signal": number, "errno": 1} + ) + if not self.exited(): + raise + return + self.record["signals"].append({"signal": number, "at_ns": time.time_ns()}) + + def drain_until(self, deadline: float) -> None: + while self.selector.get_map() and time.monotonic() < deadline: + self.read_ready( + min(0.01, max(0, deadline - time.monotonic())), reject_excess=False + ) + + def finish_cleanup(self, deadline: float) -> None: + self.send(signal.SIGKILL) + self.process.wait(timeout=max(0.001, deadline - time.monotonic())) + self.record.update(wait_reaped=True, exit=self.process.returncode) + self.drain_until(deadline) + while self.group_present() and time.monotonic() < deadline: + time.sleep(0.005) + self.record["group_absent"] = not self.group_present() + if not self.record["group_absent"] or self.selector.get_map(): + raise RuntimeError( + "release fixture cleanup did not settle its group and pipes" + ) + + def settle(self) -> None: + deadline = time.monotonic() + fixture_limit("leaf_cleanup") + try: + self.send(signal.SIGTERM) + grace = time.monotonic() + 0.2 + self.drain_until(grace) + # Closed pipes do not prove a descendant exited; keep the leader reserved. + while self.group_present() and time.monotonic() < grace: + time.sleep(0.005) + except BaseException as error: + self.record["cleanup_error"] = type(error).__name__ + raise + finally: + try: + self.finish_cleanup(deadline) + except BaseException as error: + self.record["cleanup_error"] = type(error).__name__ + raise + finally: + for name in self.LIMITS: + getattr(self.process, name).close() + + class ReleaseFixture(unittest.TestCase): def setUp(self) -> None: evidence = os.environ.get("TERA_C138_TEST_EVIDENCE") @@ -27,6 +202,9 @@ class ReleaseFixture(unittest.TestCase): self.root = Path(temporary.name).resolve() self.tools = self.root / "tools" self.tools.mkdir() + self.dispatcher = FixtureToolDispatcher( + self.tools, ("cargo", "uv", "install", "rg") + ) (self.root / "scripts").mkdir() for name in ( "release-evidence.sh", @@ -147,11 +325,7 @@ class ReleaseFixture(unittest.TestCase): path.write_text(text) def executable(self, name: str, body: str) -> None: - path = self.tools / name - if path.is_symlink(): - path.unlink() - path.write_text(f"#!{sys.executable}\n" + body) - path.chmod(0o700) + self.dispatcher.executable(name, body) def git(self, *arguments: str) -> str: return subprocess.check_output( @@ -170,30 +344,23 @@ class ReleaseFixture(unittest.TestCase): ), ) - def run_release(self, mode: str) -> subprocess.CompletedProcess: + def run_release(self, mode: str, *, timeout=None) -> subprocess.CompletedProcess: argv = ["/bin/sh", str(self.root / "scripts/release-evidence.sh"), mode] - result = subprocess.run( + command = ReleaseCommand( argv, - cwd=self.root, - env=self.environment, - capture_output=True, - check=False, - timeout=30, + self.root, + self.environment, + fixture_limit("release_command") if timeout is None else timeout, ) self.commands += 1 - self.write( - f"command-{self.commands:02}.json", - json.dumps( - { - "argv": argv, - "cwd": str(self.root), - "exit": result.returncode, - "stdout": result.stdout.decode(), - "stderr": result.stderr.decode(), - } - ), - ) - return result + try: + return command.execute() + finally: + record = command.record + for name, output in command.output.items(): + (self.root / f"command-{self.commands:02}.{name}").write_bytes(output) + record[name] = output.decode(errors="replace") + self.write(f"command-{self.commands:02}.json", json.dumps(record)) def prior(self) -> dict[str, bytes]: return { @@ -358,5 +525,148 @@ class ReleaseScannerTests(ReleaseFixture): self.assert_rejected_unchanged(mode, prior) +class ReleaseProcessTests(ReleaseFixture): + def control(self, body: str) -> None: + self.write( + "control.py", + "import json, os, signal, sys, time\nfrom pathlib import Path\n" + "root = Path(__file__).parent\n" + "(root / 'direct.json').write_text(json.dumps({" + "'pid': os.getpid(), 'pgid': os.getpgrp(), 'sid': os.getsid(0)}))\n" + body, + ) + self.write( + "scripts/release-evidence.sh", + f"exec {shlex.quote(sys.executable)} {shlex.quote(str(self.root / 'control.py'))}\n", + ) + + def settled(self) -> dict: + record = json.loads( + (self.root / f"command-{self.commands:02}.json").read_text() + ) + direct = json.loads((self.root / "direct.json").read_text()) + self.assertEqual( + (direct["pid"], direct["pgid"], direct["sid"]), (record["pid"],) * 3 + ) + self.assertTrue(record["wait_reaped"]) + self.assertTrue(record["group_absent"]) + with self.assertRaises(ProcessLookupError): + os.killpg(record["pgid"], 0) + self.assertLessEqual(record["elapsed_seconds"], record["timeout"] + 2.5) + return record + + def test_normal_and_rejected_commands_preserve_bytes_status_and_deadline( + self, + ) -> None: + for status in (0, 7): + with self.subTest(status=status): + self.control( + f"print('ordinary stdout'); print('ordinary stderr', file=sys.stderr); sys.exit({status})\n" + ) + result = self.run_release("write") + self.assertEqual(result.returncode, status) + self.assertEqual(result.stdout, b"ordinary stdout\n") + self.assertEqual(result.stderr, b"ordinary stderr\n") + self.assertEqual( + self.settled()["timeout"], fixture_limit("release_command") + ) + + def test_stdin_is_closed_even_when_the_caller_has_input(self) -> None: + self.control("print(repr(sys.stdin.buffer.read()))\n") + reader, writer = os.pipe() + original = os.dup(0) + try: + os.write(writer, b"P139R_SYNTHETIC_INHERITED_INPUT") + os.close(writer) + os.dup2(reader, 0) + result = self.run_release("write", timeout=1) + finally: + os.dup2(original, 0) + os.close(original) + os.close(reader) + self.assertEqual(result.returncode, 0) + self.assertEqual(result.stdout, b"b''\n") + self.settled() + + def test_exact_output_limits_are_preserved(self) -> None: + self.control( + "sys.stdout.buffer.write(b'x' * 1048576); sys.stderr.buffer.write(b'y' * 65536)\n" + ) + result = self.run_release("write", timeout=2) + self.assertEqual(result.returncode, 0) + self.assertEqual(result.stdout, b"x" * 1048576) + self.assertEqual(result.stderr, b"y" * 65536) + self.settled() + + def test_output_excess_rejects_and_settles(self) -> None: + for stream, limit in ReleaseCommand.LIMITS.items(): + with self.subTest(stream=stream): + self.control( + f"while True: sys.{stream}.buffer.write(b'x' * {limit + 1}); sys.{stream}.flush()\n" + ) + with self.assertRaisesRegex(RuntimeError, "exceeds capture limit"): + self.run_release("write", timeout=2) + record = self.settled() + self.assertEqual(record["output_excess"], stream) + output = self.root / f"command-{self.commands:02}.{stream}" + self.assertEqual(output.stat().st_size, limit) + + def child_control(self, *, ignore_term: bool, close_pipes: bool) -> None: + handler = "(root / 'term.txt').write_text('received TERM')" + if not ignore_term: + handler += "; sys.exit(0)" + self.control( + "child = os.fork()\n" + "if child == 0:\n" + f" signal.signal(signal.SIGTERM, lambda *_: ({handler.replace('; ', ', ')}))\n" + " (root / 'ready.json').write_text(json.dumps({'pid': os.getpid(), 'pgid': os.getpgrp(), 'sid': os.getsid(0)}))\n" + + (" os.close(1); os.close(2)\n" if close_pipes else "") + + " time.sleep(1.2)\n" + " (root / 'late.txt').write_text('escaped late write')\n" + " while True: time.sleep(1)\n" + "while not (root / 'ready.json').exists(): time.sleep(0.005)\n" + "print('ready child', flush=True)\n" + ) + + def assert_child_settled(self, *, ignore_term: bool) -> dict: + record = self.settled() + child = json.loads((self.root / "ready.json").read_text()) + self.assertEqual(child["pgid"], record["pgid"]) + self.assertEqual(child["sid"], record["pgid"]) + self.assertEqual((self.root / "term.txt").read_text(), "received TERM") + with self.assertRaises(ProcessLookupError): + os.kill(child["pid"], 0) + if ignore_term: + self.assertIn( + signal.SIGKILL, [event["signal"] for event in record["signals"]] + ) + # Ready, actual TERM and absent PID/group establish what the scheduled writer did. + time.sleep(1.25) + self.assertFalse((self.root / "late.txt").exists()) + return record + + def test_timeout_settles_child_retaining_capture_pipes(self) -> None: + self.child_control(ignore_term=False, close_pipes=False) + with self.assertRaises(subprocess.TimeoutExpired) as observed: + self.run_release("write", timeout=0.3) + self.assertEqual(observed.exception.timeout, 0.3) + self.assertEqual(observed.exception.stdout, b"ready child\n") + self.assertEqual( + self.assert_child_settled(ignore_term=False)["error"], "TimeoutExpired" + ) + + def test_timeout_kills_child_ignoring_term(self) -> None: + self.child_control(ignore_term=True, close_pipes=False) + with self.assertRaises(subprocess.TimeoutExpired): + self.run_release("write", timeout=0.3) + self.assert_child_settled(ignore_term=True) + + def test_normal_completion_settles_child_with_closed_capture_pipes(self) -> None: + self.child_control(ignore_term=True, close_pipes=True) + result = self.run_release("write", timeout=2) + self.assertEqual(result.returncode, 0) + self.assertEqual(result.stdout, b"ready child\n") + self.assert_child_settled(ignore_term=True) + + if __name__ == "__main__": unittest.main() diff --git a/scripts/test_unittest_report.py b/scripts/test_unittest_report.py @@ -14,22 +14,60 @@ import unittest from pathlib import Path from unittest.mock import patch +from scripts.fixture_tool_dispatch import CompilerCommand +from scripts.fixture_test_policy import limit + SECRET = "private exception, skip reason, and subtest parameter" SCRIPTS = Path(__file__).resolve().parent +def observer_code_mutations(failure): + for table in ["", "executables", "methods"]: + for restored in [False, True]: + action = ( + "function = type(result).addFailure; original = function.__code__; " + ) + if restored: + action += "self.addCleanup(setattr, function, '__code__', original); " + action += "function.__code__ = (lambda self, test, err: self.addSuccess(test)).__code__; " + if table: + action += ( + "import sys; owner = sys.modules['__main__']; " + "names = ['CALLBACK_EXECUTABLES', 'CALLBACK_METHODS']; " + "previous = {name: getattr(owner, name, None) for name in names}; " + ) + if restored: + action += "self.addCleanup(lambda: [(setattr(owner, name, value) if value is not None else delattr(owner, name)) for name, value in previous.items() if value is not None or hasattr(owner, name)]); " + action += ( + "baseline = {**getattr(owner, 'CALLBACK_METHODS', {}), 'addFailure': function}; " + "owner.CALLBACK_EXECUTABLES = {name: owner.origin.callable_identity(method) for name, method in baseline.items()}; " + ) + if table == "methods": + action += "owner.CALLBACK_METHODS = baseline; " + yield ( + action, + ( + "try: " + failure + "\n except AssertionError:\n" + " Path(__name__ + '.failure.executed').write_text(self.id())\n" + " raise" + ), + ) + + class StandaloneReportTests(unittest.TestCase): def setUp(self): temporary = tempfile.TemporaryDirectory(prefix="tera-unittest-report-") self.addCleanup(temporary.cleanup) self.root = Path(temporary.name).resolve() (self.root / "scripts").mkdir() - subprocess.run( - ["git", "init", "--quiet", str(self.root)], check=True, timeout=10 - ) shutil.copyfile( SCRIPTS / "unittest_report.py", self.root / "scripts/unittest_report.py" ) + if (SCRIPTS / "unittest_report_origin.py").is_file(): + shutil.copyfile( + SCRIPTS / "unittest_report_origin.py", + self.root / "scripts/unittest_report_origin.py", + ) self.output = self.root / "report.json" def module(self, source, name="test_observed"): @@ -38,24 +76,46 @@ class StandaloneReportTests(unittest.TestCase): return "scripts." + name def run_process(self, modules, output=None): - arguments = [ - sys.executable, - "-m", - "scripts.unittest_report", - "--report", - str(output or self.output), - *modules, - ] + entry = getattr(self, "report_entry", ["scripts/unittest_report.py"]) + output_path = str(output or self.output) + arguments = [sys.executable, *entry, "--report", output_path, *modules] record = self.probe_record(arguments, modules) - result = subprocess.run( + command = CompilerCommand( arguments, - cwd=self.root, - capture_output=True, - text=True, - timeout=30, + self.root, + getattr(self, "process_timeout", limit("reporter_child")), env={**os.environ, "PYTHONDONTWRITEBYTECODE": "1"}, + budget="reporter_child", + ) + failure = None + try: + command.execute() + except BaseException as error: + failure = error + process = command.process + if process is None: + raise failure + result = subprocess.CompletedProcess( + arguments, + process.returncode, + bytes(command.output["stdout"]).decode(), + bytes(command.output["stderr"]).decode(), ) if record is not None: + (record / "capture.json").write_text(json.dumps(command.record, indent=2)) + (record / "settlement.json").write_text( + json.dumps( + { + "pid": process.pid, + "reaped": process.poll() is not None, + "timed_out": isinstance(failure, subprocess.TimeoutExpired), + "group_absent": command.record["group_absent"], + "pid_absent": not Path("/proc", str(process.pid)).exists() + if sys.platform == "linux" + else self.pid_absent(process.pid), + } + ) + ) (record / "exit.txt").write_text(str(result.returncode)) (record / "stdout.txt").write_text(result.stdout) (record / "stderr.txt").write_text(result.stderr) @@ -65,8 +125,18 @@ class StandaloneReportTests(unittest.TestCase): else b"" ) (record / "report.json").write_bytes(raw) + if failure: + raise failure return result + @staticmethod + def pid_absent(pid): + try: + os.kill(pid, 0) + except ProcessLookupError: + return True + return False + def probe_record(self, arguments, modules): evidence = os.environ.get("TERA_REPORT_TEST_EVIDENCE") if not evidence: @@ -74,6 +144,7 @@ class StandaloneReportTests(unittest.TestCase): record = Path(tempfile.mkdtemp(prefix="probe-", dir=evidence)) (record / "argv.json").write_text(json.dumps(arguments)) (record / "cwd.txt").write_text(str(self.root)) + (record / "test_id.txt").write_text(self.id()) (record / "producer.py").write_bytes( (self.root / "scripts/unittest_report.py").read_bytes() ) @@ -587,7 +658,7 @@ class OutputIntegrityTests(unittest.TestCase): "schema": self.producer.SCHEMA, "test_ids": inventory, "tests_run": result.testsRun, - "callbacks": result.callbacks, + "callbacks": [dict(event) for event in result.callbacks], } def output(self, name): @@ -716,7 +787,7 @@ class ObserverBoundTests(unittest.TestCase): root = Path(temporary).resolve() (root / "scripts").mkdir() (root / "scripts/test_observed.py").write_text("pass\n") - with patch.object(producer.importlib.util, "find_spec") as find: + with patch.object(importlib.util, "find_spec") as find: find.return_value.origin = str(root / "foreign.py") with self.assertRaises(producer.ReportError): producer.load_modules(root, ["scripts.test_observed"]) diff --git a/scripts/test_unittest_report_bootstrap.py b/scripts/test_unittest_report_bootstrap.py @@ -0,0 +1,270 @@ +"""Run genuine before-package and before-fixture dispatch integrity controls.""" + +from __future__ import annotations + +import shutil +import unittest + +from scripts import test_unittest_report as support + +BODY = """from pathlib import Path +class ActualTests(unittest.TestCase): + def setUp(self): + Path('setup.executed').write_text(self.id()) + def test_authored_body(self): + Path('body.executed').write_text(self.id()) + with self.subTest(index=1): + BODY + def tearDown(self): + Path('teardown.executed').write_text(self.id()) +""" +FORGED_RUN = """import unittest +def forged_run(self, result=None): + result.startTest(self) + result.addSuccess(self) + result.stopTest(self) + return result +unittest.TestCase.run = forged_run +""" +STDLIB_SHADOW = """import sys +from pathlib import Path +actual = Path(sys._stdlib_dir) / 'unittest' +__path__ = [str(actual)] +__package__ = 'unittest' +exec(compile((actual / '__init__.py').read_bytes(), str(actual / '__init__.py'), 'exec'), globals()) +def forged_run(self, result=None): + result.startTest(self) + result.addSuccess(self) + result.stopTest(self) + return result +TestCase.run = forged_run +""" + + +def fixture_source(kind, restored, target="body"): + """Keep replacement/restoration controls on the real fixture lifecycle.""" + source = BODY.replace("BODY", "self.fail('actual authored failure')") + expression = ( + "ActualTests.test_authored_body" + if target == "body" + else "unittest.TestCase._callTestMethod" + ) + action = ( + f"function = {expression}; original = function.__code__; " + "function.__code__ = (lambda self, *args: None).__code__" + ) + if kind == "class": + cleanup = ( + "cls.addClassCleanup(setattr, function, '__code__', original); " + if restored == "cleanup" + else "cls.original = original; " + ) + fixture = " @classmethod\n def setUpClass(cls):\n " + fixture += action.replace( + "function.__code__ =", cleanup + "function.__code__ =" + ) + fixture += "\n" + if restored == "teardown": + fixture += ( + " @classmethod\n def tearDownClass(cls):\n" + f" {expression}.__code__ = cls.original\n" + ) + return source.replace(" def setUp(self):", fixture + " def setUp(self):") + fixture = "\ndef setUpModule():\n global original\n " + action + "\n" + if restored == "cleanup": + fixture += ( + " unittest.addModuleCleanup(setattr, function, '__code__', original)\n" + ) + elif restored == "teardown": + fixture += f"def tearDownModule():\n {expression}.__code__ = original\n" + return source + fixture + + +class BootstrapTests(unittest.TestCase): + setUp = support.StandaloneReportTests.setUp + pid_absent = staticmethod(support.StandaloneReportTests.pid_absent) + run_report = support.StandaloneReportTests.run_report + + def probe_record(self, arguments, modules): + self.last_record = support.StandaloneReportTests.probe_record( + self, arguments, modules + ) + if self.last_record is not None: + shutil.copytree( + self.root / "scripts", + self.last_record / "scripts_before", + symlinks=True, + ) + return self.last_record + + def run_process(self, modules, output=None): + result = support.StandaloneReportTests.run_process(self, modules, output) + if self.last_record is not None: + shutil.copytree( + self.root / "scripts", self.last_record / "scripts_after", symlinks=True + ) + for marker in self.root.glob("*.executed"): + shutil.copy2(marker, self.last_record / marker.name) + return result + + def prepare(self, source, *, nested=False, initializer=None): + self.output.unlink(missing_ok=True) + for marker in self.root.glob("*.executed"): + marker.unlink() + root_init = self.root / "scripts/__init__.py" + root_init.unlink(missing_ok=True) + if initializer is not None: + root_init.write_text(initializer) + name = "scripts.controls.test_actual" if nested else "scripts.test_actual" + path = self.root.joinpath(*name.split(".")).with_suffix(".py") + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("import unittest\n" + source) + return name + + def test_namespace_empty_and_ordinary_root_packages_execute_actual_body(self): + ordinary = ( + "from pathlib import Path\n" + "from . import unittest_report, unittest_report_origin\n" + "Path('initializer.executed').write_text('actual initializer')\n" + ) + for nested in (False, True): + for initializer in (None, "", ordinary): + with self.subTest(nested=nested, initializer=initializer): + module = self.prepare( + BODY.replace("BODY", "self.assertTrue(True)"), + nested=nested, + initializer=initializer, + ) + result, document = self.run_report([module]) + self.assertEqual(result.returncode, 0, result.stderr) + expected = module + ".ActualTests.test_authored_body" + self.assertEqual(document["test_ids"], [expected]) + for name in ("setup", "body", "teardown"): + self.assertEqual( + (self.root / (name + ".executed")).read_text(), expected + ) + if initializer: + self.assertTrue((self.root / "initializer.executed").is_file()) + + def test_root_initializer_self_mutation_and_forged_run_are_rejected(self): + controls = ( + FORGED_RUN, + "from pathlib import Path\nPath(__file__).write_text('# mutated\\n')\n", + "from pathlib import Path\nPath(__file__).chmod(0o600)\n", + ) + for nested in (False, True): + for initializer in controls: + with self.subTest(nested=nested, initializer=initializer): + module = self.prepare( + BODY.replace("BODY", "self.fail('actual failure')"), + nested=nested, + initializer=initializer, + ) + result = self.run_process([module]) + self.assertNotEqual(result.returncode, 0) + self.assertFalse((self.root / "body.executed").exists()) + + def test_class_and_module_fixture_code_cannot_skip_inventoried_body(self): + for nested in (False, True): + for kind in ("class", "module"): + for restored in (None, "cleanup", "teardown"): + for target in ("body", "dispatch"): + with self.subTest( + nested=nested, kind=kind, restored=restored, target=target + ): + module = self.prepare( + fixture_source(kind, restored, target), nested=nested + ) + result, document = self.run_report([module]) + self.assertNotEqual(result.returncode, 0) + self.assertFalse((self.root / "body.executed").exists()) + self.assertFalse( + document + and any( + event["callback"] == "addSuccess" + for event in document["callbacks"] + ) + ) + + def test_genuine_body_failure_keeps_actual_setup_body_and_teardown(self): + for nested in (False, True): + with self.subTest(nested=nested): + module = self.prepare( + BODY.replace("BODY", "self.fail('actual authored failure')"), + nested=nested, + ) + result, document = self.run_report([module]) + self.assertNotEqual(result.returncode, 0) + self.assertTrue((self.root / "body.executed").exists()) + self.assertTrue((self.root / "teardown.executed").exists()) + self.assertIn( + "addSubTest", [e["callback"] for e in document["callbacks"]] + ) + + def test_live_legacy_module_entry_requires_trusted_direct_entry(self): + module = self.prepare(BODY.replace("BODY", "self.assertTrue(True)")) + self.report_entry = ["-m", "scripts.unittest_report"] + result = self.run_process([module]) + self.assertNotEqual(result.returncode, 0, result.stderr) + self.assertFalse((self.root / "body.executed").exists()) + self.assertFalse(self.output.exists() and self.output.stat().st_size) + self.assertNotIn("actual authored failure", result.stderr) + self.assertIn("direct file entry", result.stderr) + + def test_bootstrap_helper_failure_has_static_safe_diagnostics(self): + for source in ( + "P139S_SYNTHETIC_DIAGNOSTIC_MARKER = '\n", + "raise RuntimeError('P139S_SYNTHETIC_DIAGNOSTIC_MARKER')\n", + ): + with self.subTest(source=source): + module = self.prepare(BODY.replace("BODY", "self.assertTrue(True)")) + (self.root / "scripts/unittest_report_origin.py").write_text(source) + result = self.run_process([module]) + self.assertNotEqual(result.returncode, 0) + self.assertEqual(result.stdout, "") + self.assertEqual( + result.stderr, + "unittest-report: bootstrap source or origin rejected\n", + ) + self.assertFalse((self.root / "body.executed").exists()) + self.assertFalse(self.output.exists()) + + def test_local_stdlib_shadow_cannot_skip_actual_authored_failure(self): + for nested in (False, True): + for dependency in ("unittest", "hashlib", "inspect", "json"): + with self.subTest(nested=nested, dependency=dependency): + module = self.prepare( + BODY.replace("BODY", "self.fail('actual authored failure')"), + nested=nested, + ) + (self.root / "scripts" / (dependency + ".py")).write_text( + STDLIB_SHADOW + ) + result, document = self.run_report([module]) + self.assertNotEqual(result.returncode, 0) + self.assertTrue((self.root / "body.executed").is_file()) + self.assertFalse( + any( + e["callback"] == "addSuccess" for e in document["callbacks"] + ) + ) + + def test_library_preload_accepts_real_pyexpat_children_and_rejects_forgery(self): + for forged in (None, "alias", "parent_source"): + with self.subTest(forged=forged): + module = self.prepare(BODY.replace("BODY", "self.assertTrue(True)")) + source = "import xml.parsers.expat, sys, types; " + if forged == "alias": + source += "sys.modules['pyexpat.errors'] = types.ModuleType('pyexpat.errors'); " + elif forged == "parent_source": + source += "sys.modules['pyexpat'].__spec__.origin = 'foreign.py'; " + source += "import scripts.unittest_report" + self.report_entry = ["-c", source] + result = self.run_process([module]) + self.assertEqual(result.returncode == 0, forged is None, result.stderr) + self.assertFalse(self.output.exists()) + self.assertFalse((self.root / "body.executed").exists()) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/test_unittest_report_nested.py b/scripts/test_unittest_report_nested.py @@ -0,0 +1,789 @@ +"""Actual nested unittest execution, owned ancestry and adversarial controls.""" + +from __future__ import annotations + +import json +import os +import shutil +import stat +import tempfile +import unittest +from pathlib import Path +from unittest.mock import patch + +from scripts import test_unittest_report as support + +PASS = """import unittest +from pathlib import Path +class NestedTests(unittest.TestCase): + def test_authored_nested_method_executes(self): + Path(__name__ + '.executed').write_text(self.id()) + with self.subTest(index=1): + self.assertEqual(2 + 2, 4) +""" + + +class NestedReportTests(unittest.TestCase): + run_report = support.StandaloneReportTests.run_report + pid_absent = staticmethod(support.StandaloneReportTests.pid_absent) + + def setUp(self): + support.StandaloneReportTests.setUp(self) + self.authored_sources = {} + + def run_process(self, modules, output=None): + result = support.StandaloneReportTests.run_process(self, modules, output) + record = getattr(self, "last_probe_record", None) + if record: + for marker in self.root.glob("*.executed"): + shutil.copyfile(marker, record / marker.name) + return result + + def module(self, source=PASS, name="controls.test_nested", *, initializers=False): + relative = Path("scripts", *name.split(".")).with_suffix(".py") + path = self.root / relative + path.parent.mkdir(parents=True, exist_ok=True) + if initializers: + for directory in [self.root / "scripts", *path.parents]: + if directory.is_relative_to(self.root / "scripts"): + (directory / "__init__.py").write_bytes(b"") + path.write_text(source) + self.authored_sources[path] = path.read_bytes() + return "scripts." + name + + def probe_record(self, arguments, modules): + del modules + evidence = os.environ.get("TERA_REPORT_TEST_EVIDENCE") + if not evidence: + return None + record = Path(tempfile.mkdtemp(prefix="nested-probe-", dir=evidence)) + self.last_probe_record = record + (record / "argv.json").write_text(json.dumps(arguments)) + (record / "cwd.txt").write_text(str(self.root)) + (record / "test_id.txt").write_text(self.id()) + shapes = [] + for path in (self.root / "scripts").rglob("*.py"): + relative = path.relative_to(self.root) + row = {"path": str(relative), "mode": path.lstat().st_mode} + shapes.append(row) + self.copy_fixture(path, record / relative, row) + (record / "source-shapes.json").write_text(json.dumps(shapes)) + return record + + def copy_fixture(self, path, output, row): + output.parent.mkdir(parents=True, exist_ok=True) + if path.is_symlink(): + row["symlink_target"] = os.readlink(path) + elif stat.S_ISREG(path.lstat().st_mode): + try: + output.write_bytes(path.read_bytes()) + except PermissionError: + row["unreadable"] = True + if path in self.authored_sources: + output.write_bytes(self.authored_sources[path]) + + def assert_execution(self, module, document): + identity = module + ".NestedTests.test_authored_nested_method_executes" + self.assertIn(identity, document["test_ids"]) + self.assertEqual((self.root / (module + ".executed")).read_text(), identity) + + def test_namespace_nested_authored_test_really_executes(self): + module = self.module() + result, document = self.run_report([module]) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(document["schema"], "tera.unittest-callback-report.v2") + self.assert_execution(module, document) + + def test_empty_initializers_and_mixed_flat_nested_selections_execute(self): + modules = [ + self.module(initializers=True, name="controls.deep.test_nested"), + self.module(name="test_flat"), + ] + result, document = self.run_report(modules) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(document["schema"], "tera.unittest-callback-report.v2") + self.assertEqual(document["tests_run"], 2) + for module in modules: + self.assert_execution(module, document) + + def rejected(self, modules): + self.output.unlink(missing_ok=True) + result, document = self.run_report(modules) + self.assertNotEqual(result.returncode, 0) + return document + + def test_flat_only_nested_fixture_retains_v1(self): + module = self.module(name="test_flat") + result, document = self.run_report([module]) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(document["schema"], "tera.unittest-callback-report.v1") + self.assert_execution(module, document) + + def test_nested_selector_shape_bounds_and_duplicates_fail_closed(self): + module = self.module() + for names in [ + [module, module], + [module] * 4097, + ["scripts.controls.test_"], + ["scripts.controls.9bad.test_nested"], + ["scripts..controls.test_nested"], + ["scripts.controls.test_nested.method"], + ["scripts.controls.test_" + "x" * 256], + ["scripts/controls/test_nested.py"], + ]: + with self.subTest(selector=names[0]): + self.rejected(names) + + def test_symlinked_package_directory_is_rejected(self): + module = self.module() + path = self.root / "scripts/controls" + path.rename(self.root / "foreign") + path.symlink_to(self.root / "foreign", target_is_directory=True) + self.rejected([module]) + self.assertFalse((self.root / (module + ".executed")).exists()) + + def test_symlinked_initializer_and_selected_module_are_rejected(self): + module = self.module(initializers=True) + for name in ["__init__.py", "test_nested.py"]: + with self.subTest(name=name): + path = self.root / "scripts/controls" / name + before = path.read_bytes() + foreign = self.root / "foreign.py" + foreign.write_bytes(before) + path.unlink() + path.symlink_to(foreign) + self.rejected([module]) + path.unlink() + path.write_bytes(before) + + def test_missing_unreadable_and_fifo_selected_source_is_rejected(self): + module = self.module() + path = self.root / "scripts/controls/test_nested.py" + before = path.read_bytes() + path.unlink() + self.rejected([module]) + os.mkfifo(path) + self.rejected([module]) + path.unlink() + path.write_bytes(before) + path.chmod(0) + try: + self.rejected([module]) + finally: + path.chmod(0o600) + + def test_initializer_changes_during_import_never_run_selected_test(self): + module = self.module(initializers=True) + path = self.root / "scripts/controls/__init__.py" + for action in [ + "Path(__file__).write_bytes(b'')", + "Path('scripts/controls/test_nested.py').write_text('pass\\n')", + "__path__ = [str(Path.cwd() / 'foreign')]", + "__spec__.origin = str(Path.cwd() / 'foreign.py')", + ]: + with self.subTest(action=action): + self.module(initializers=True) + path.write_text("from pathlib import Path\n" + action + "\n") + self.rejected([module]) + self.assertFalse((self.root / (module + ".executed")).exists()) + + def test_ancestry_source_presence_mode_and_identity_drift_is_rejected(self): + for action in [ + "Path('scripts/controls/__init__.py').write_text('new = True\\n')", + "Path('scripts/controls/__init__.py').chmod(0o400)", + "Path('scripts/controls').rename('moved'); Path('scripts/controls').mkdir()", + "Path(__file__).write_text('pass\\n')", + "sys.modules['scripts.controls'].__path__ = ['foreign']", + "sys.modules['scripts.controls'].__spec__.origin = 'foreign.py'", + "sys.modules[__name__] = None", + "globals()['__name__'] = 'foreign.test_drift'", + "__spec__.name = 'foreign.test_drift'", + "__spec__.loader = None", + ]: + with self.subTest(action=action): + source = "import sys\n" + PASS.replace( + " with self.subTest(index=1):", + " " + action + "\n with self.subTest(index=1):", + ) + module = self.module( + source, name="controls.test_drift", initializers=True + ) + self.rejected([module]) + shutil.rmtree(self.root / "scripts/controls") + + def test_empty_initializer_added_during_execution_is_rejected(self): + module = self.module( + PASS.replace( + " with self.subTest(index=1):", + " Path('scripts/controls/__init__.py').touch()\n with self.subTest(index=1):", + ) + ) + self.rejected([module]) + + def test_owned_initializer_executes_source_instead_of_forged_bytecode(self): + import importlib._bootstrap_external + import importlib.util + + module = self.module(initializers=True) + initializer = self.root / "scripts/controls/__init__.py" + initializer.write_text( + "from pathlib import Path\nPath('initializer-executed').write_text('actual source')\n" + ) + cached = Path(importlib.util.cache_from_source(str(initializer))) + cached.parent.mkdir() + forged = compile("raise SystemExit(0)", str(initializer), "exec") + cached.write_bytes( + importlib._bootstrap_external._code_to_timestamp_pyc( + forged, + int(initializer.stat().st_mtime), + initializer.stat().st_size, + ) + ) + result, document = self.run_report([module]) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual( + (self.root / "initializer-executed").read_text(), "actual source" + ) + self.assert_execution(module, document) + + def test_foreign_package_shadowing_is_rejected_before_foreign_import(self): + module = self.module() + foreign = self.root / "foreign" + foreign.mkdir() + (foreign / "test_other.py").write_text( + "from pathlib import Path\nPath('foreign-executed').touch()\n" + ) + first = self.module( + "import scripts\nfrom pathlib import Path\nscripts.__path__ = [str(Path.cwd() / 'foreign')]\n" + + PASS, + name="test_first", + ) + self.rejected([first, module]) + self.assertFalse((self.root / "foreign-executed").exists()) + + def test_nested_partial_nonpassing_duplicate_and_fabricated_lifecycles_reject(self): + for source in [ + PASS.replace( + "self.assertEqual(2 + 2, 4)", "self.skipTest('private reason')" + ), + PASS.replace("self.assertEqual(2 + 2, 4)", "self.fail('private reason')"), + PASS.replace( + "self.assertEqual(2 + 2, 4)", "self._outcome.result.startTestRun()" + ), + PASS + + "\ndef load_tests(loader, tests, pattern):\n return unittest.TestSuite([NestedTests('test_authored_nested_method_executes')] * 2)\n", + PASS + + "\n def id(self): return 'scripts.controls.test_nested.NestedTests.forged'\n", + PASS + + "\n def run(self, result):\n result.startTest(self)\n result.addSuccess(self)\n result.stopTest(self)\n", + PASS.replace("self.assertEqual(2 + 2, 4)", "self._outcome.result.stop()") + + "\n def test_second(self): pass\n", + ]: + with self.subTest(source=source): + self.rejected([self.module(source)]) + + def test_monkey_patched_stdlib_testcase_identity_is_rejected(self): + module = self.module( + PASS + + "\nunittest.TestCase.id = lambda self: 'scripts.controls.test_nested.NestedTests.forged'\n" + ) + self.rejected([module]) + + def test_root_initializer_cannot_prepatch_a_false_testcase_identity(self): + module = self.module(initializers=True) + (self.root / "scripts/__init__.py").write_text( + "import unittest\nunittest.TestCase.id = lambda self: " + "'scripts.controls.test_nested.NestedTests.forged'\n" + ) + self.rejected([module]) + + def test_monkey_patched_observer_cannot_forge_successful_execution(self): + source = "import sys\n" + PASS.replace( + "self.assertEqual(2 + 2, 4)", + "sys.modules['__main__'].CallbackResult.passing = lambda result: True\n" + " self.fail('private failure')", + ) + self.rejected([self.module(source)]) + + def test_instance_callback_shadows_cannot_convert_real_nonpassing_tests(self): + controls = [ + ( + "result.addFailure = lambda test, err: result.addSuccess(test)", + "self.fail('private failure')", + ), + ( + "result.__dict__['addFailure'] = lambda test, err: result.addSuccess(test)", + "self.fail('private failure')", + ), + ( + "object.__setattr__(result, 'addFailure', lambda test, err: result.addSuccess(test))", + "self.fail('private failure')", + ), + ( + "result.addError = lambda test, err: result.addSuccess(test)", + "raise RuntimeError('private error')", + ), + ( + "result.addSkip = lambda test, reason: result.addSuccess(test)", + "self.skipTest('private skip')", + ), + ( + "result.addExpectedFailure = lambda test, err: result.addSuccess(test)", + "self.fail('private expected failure')", + ), + ( + "result.addUnexpectedSuccess = lambda test: result.addSuccess(test)", + "self.assertTrue(True)", + ), + ("result.passing = lambda: True", "self.fail('private failure')"), + ( + "result.addSubTest = lambda test, subtest, err: result.addSuccess(test)", + "with self.subTest(index=1): self.fail('private subtest failure')", + ), + ] + controls.extend(support.observer_code_mutations("self.fail('private failure')")) + for name in ["test_observer", "controls.test_observer"]: + for action, outcome in controls: + with self.subTest(module=name, action=action): + source = PASS.replace( + " with self.subTest(index=1):\n self.assertEqual(2 + 2, 4)", + " result = self._outcome.result\n " + + action + + "\n " + + outcome, + ) + if ( + "addExpectedFailure" in action + or "addUnexpectedSuccess" in action + ): + source = source.replace( + " def test_authored", + " @unittest.expectedFailure\n def test_authored", + ) + module = self.module(source, name=name) + document = self.rejected([module]) + self.assert_execution(module, document) + if "function = type(result).addFailure" in action: + self.assertEqual( + (self.root / (module + ".failure.executed")).read_text(), + document["test_ids"][0], + ) + self.assertNotIn( + "addSuccess", + [event["callback"] for event in document["callbacks"]], + ) + + def test_restored_instance_callback_shadows_remain_rejected(self): + for name in ["test_observer", "controls.test_observer"]: + for action in [ + "result.addFailure = forged", + "result.__dict__['addFailure'] = forged", + "object.__setattr__(result, 'addFailure', forged)", + ]: + with self.subTest(module=name, action=action): + source = PASS.replace( + " with self.subTest(index=1):\n self.assertEqual(2 + 2, 4)", + """ result = self._outcome.result + original = result.addFailure + def forged(test, err): + result.__dict__.pop('addFailure', None) + result.addFailure = original + result.addSuccess(test) + """ + + action + + "\n self.fail('private failure')", + ) + module = self.module(source, name=name) + document = self.rejected([module]) + self.assert_execution(module, document) + + def test_unused_assignment_restored_before_callback_is_still_rejected(self): + for name in ["test_observer", "controls.test_observer"]: + with self.subTest(module=name): + source = PASS.replace( + " with self.subTest(index=1):", + " result = self._outcome.result\n" + " original = result.addFailure\n" + " result.addFailure = lambda test, err: result.addSuccess(test)\n" + " result.addFailure = original\n" + " result.rejected = False\n" + " with self.subTest(index=1):", + ) + module = self.module(source, name=name) + document = self.rejected([module]) + self.assert_execution(module, document) + + def test_unused_instance_dictionary_callback_shadow_is_rejected(self): + for name in ["test_observer", "controls.test_observer"]: + for method in ["addFailure", "addSkip", "stop", "fixture_id"]: + with self.subTest(module=name, method=method): + source = PASS.replace( + " with self.subTest(index=1):", + " self._outcome.result.__dict__['" + + method + + "'] = lambda *args: None\n" + " with self.subTest(index=1):", + ) + module = self.module(source, name=name) + document = self.rejected([module]) + self.assert_execution(module, document) + + def test_observed_failure_and_callback_journal_cannot_be_erased(self): + resets = [ + "result.nonpassing = False", + "result.__dict__['nonpassing'] = False", + "object.__setattr__(result, 'nonpassing', False)", + ] + edits = [ + "result.callbacks[:] = filtered", + "result.callbacks = filtered", + "result.__dict__['callbacks'] = filtered", + "object.__setattr__(result, 'callbacks', filtered)", + "result.callbacks.clear()", + "result.callbacks.pop(2)", + "del result.callbacks[2]", + "result.callbacks[2]['successful'] = True", + ] + for name in ["test_state", "controls.test_state"]: + for reset in resets: + for edit in edits: + with self.subTest(module=name, reset=reset, edit=edit): + source = PASS.replace( + " with self.subTest(index=1):\n self.assertEqual(2 + 2, 4)", + " result = self._outcome.result\n" + " with self.subTest(index=1): self.fail('private failed subtest')\n" + " " + reset + "\n" + " filtered = [event for event in result.callbacks if event['callback'] != 'addSubTest']\n" + " try:\n " + edit + "\n" + " except (AttributeError, TypeError): pass\n" + " self._outcome.success = True", + ) + module = self.module(source, name=name) + document = self.rejected([module]) + self.assert_execution(module, document) + self.assertTrue( + any( + event["callback"] == "addSubTest" + and not event["successful"] + for event in document["callbacks"] + ) + ) + + def test_observer_descriptor_getter_and_setter_code_are_fenced(self): + for name in ["test_descriptor", "controls.test_descriptor"]: + for mode in ["getter", "setter", "journal_getters"]: + with self.subTest(module=name, mode=mode): + changed = { + "getter": "owner.nonpassing.fget.__code__ = false_getter.__code__", + "setter": "owner.nonpassing.fset.__code__ = reset_setter.__code__", + "journal_getters": "owner.nonpassing.fget.__code__ = false_getter.__code__; owner.callbacks.fget.__code__ = filtered_getter.__code__", + }[mode] + reset = "result.nonpassing = False" if mode == "setter" else "pass" + body = ( + """ result = self._outcome.result + with self.subTest(index=1): self.fail('private failed subtest') + owner = type(result) + def replacements(facts): + def false_getter(result): return False if facts else False + def reset_setter(result, value): facts[result]['nonpassing'] = bool(value) + def filtered_getter(result): + return tuple(event for event in facts[result]['callbacks'] if event['callback'] != 'addSubTest') + return false_getter, reset_setter, filtered_getter + false_getter, reset_setter, filtered_getter = replacements(None) + """ + + changed + + "\n " + + reset + + "\n self._outcome.success = True" + ) + source = PASS.replace( + " with self.subTest(index=1):\n self.assertEqual(2 + 2, 4)", + body, + ) + module = self.module(source, name=name) + document = self.rejected([module]) + self.assert_execution(module, document) + + def test_instance_run_shadow_cannot_fabricate_unexecuted_test_callbacks(self): + for name in ["test_execution", "controls.test_execution"]: + for target in ["case", "suite", "method", "dispatch", "suite_call"]: + with self.subTest(module=name, target=target): + source = ( + PASS + + """ +def load_tests(loader, tests, pattern): + case = NestedTests('test_authored_nested_method_executes') + suite = unittest.TestSuite([case]) + def fabricated(result): + result.startTest(case) + result.addSuccess(case) + result.stopTest(case) + """ + + { + "case": "case.run = fabricated", + "suite": "suite.run = fabricated", + "method": "setattr(case, case._testMethodName, lambda: None)", + "dispatch": "case._callTestMethod = lambda method: None", + "suite_call": "class ForgedSuite(unittest.TestSuite):\n def __call__(self, result): fabricated(result)\n suite = ForgedSuite([case])", + }[target] + + "\n return suite\n" + ) + module = self.module(source, name=name) + self.rejected([module]) + self.assertFalse((self.root / (module + ".executed")).exists()) + + def test_setup_dispatch_shadow_cannot_skip_authored_failure(self): + for name in ["test_dispatch", "controls.test_dispatch"]: + for target, assignment in [ + ("self._callTestMethod", "self._callTestMethod = lambda method: None"), + ( + "self._callTestMethod", + "self.__dict__['_callTestMethod'] = lambda method: None", + ), + ( + "type(self).test_authored_failure.__code__", + "type(self).test_authored_failure.__code__ = (lambda self: None).__code__", + ), + ( + "unittest.TestCase._callTestMethod.__code__", + "unittest.TestCase._callTestMethod.__code__ = (lambda self, method: None).__code__", + ), + ]: + for restored in [False, True]: + with self.subTest( + module=name, assignment=assignment, restored=restored + ): + source = ( + """import unittest +from pathlib import Path +class DispatchTests(unittest.TestCase): + def setUp(self): + Path(__name__ + '.setup.executed').write_text(self.id()) + self.addCleanup(lambda: Path(__name__ + '.cleanup.executed').write_text(self.id())) + self.original = ORIGINAL + ASSIGNMENT + def tearDown(self): + RESTORE + Path(__name__ + '.teardown.executed').write_text(self.id()) + def test_authored_failure(self): + Path(__name__ + '.body.executed').write_text(self.id()) + self.fail('actual authored failure') +""".replace("ORIGINAL", target) + .replace("ASSIGNMENT", assignment) + .replace( + "RESTORE", + target + " = self.original" if restored else "pass", + ) + ) + module = self.module(source, name=name) + document = self.rejected([module]) + identity = module + ".DispatchTests.test_authored_failure" + self.assertEqual( + (self.root / (module + ".setup.executed")).read_text(), + identity, + ) + self.assertEqual( + (self.root / (module + ".cleanup.executed")).read_text(), + identity, + ) + self.assertFalse( + (self.root / (module + ".body.executed")).exists() + ) + self.assertIn( + "addError", + [event["callback"] for event in document["callbacks"]], + ) + self.assertNotIn( + "addSuccess", + [event["callback"] for event in document["callbacks"]], + ) + + def test_normal_setup_body_teardown_and_cleanup_keep_stdlib_outcomes(self): + for name in ["test_fixtures", "controls.test_fixtures"]: + for body in [ + "self.assertTrue(True)", + "self.fail('authored failure')", + "self.skipTest('authored skip')", + "with self.subTest(index=1): self.fail('authored subtest')", + ]: + with self.subTest(module=name, body=body): + self.output.unlink(missing_ok=True) + source = """import unittest +from pathlib import Path +class FixtureTests(unittest.TestCase): + def setUp(self): + Path(__name__ + '.setup.executed').write_text(self.id()) + self.addCleanup(lambda: Path(__name__ + '.cleanup.executed').write_text(self.id())) + def tearDown(self): + Path(__name__ + '.teardown.executed').write_text(self.id()) + def test_authored_body(self): + Path(__name__ + '.body.executed').write_text(self.id()) + BODY +""".replace("BODY", body) + module = self.module(source, name=name) + result, document = self.run_report([module]) + self.assertEqual( + result.returncode == 0, + body == "self.assertTrue(True)", + result.stderr, + ) + identity = module + ".FixtureTests.test_authored_body" + self.assertEqual(document["test_ids"], [identity]) + for part in ["setup", "body", "teardown", "cleanup"]: + self.assertEqual( + ( + self.root / (module + "." + part + ".executed") + ).read_text(), + identity, + ) + + def test_monkey_patched_writer_cannot_export_a_forged_success_report(self): + source = ( + "import sys\n" + + PASS + + """ + owner = sys.modules['__main__'] + original = owner.ReportOutput.write + def changed(output, document): + document['tests_run'] = 0 + original(output, document) + owner.ReportOutput.write = changed +""" + ) + document = self.rejected([self.module(source)]) + self.assertEqual(document["tests_run"], 0) + + def test_actual_nested_report_byte_budget_remains_two_million(self): + prefix = "scripts.controls.test_nested.ManyTests.test_" + for others in range(4): + document = support.StandaloneReportTests.report_shape(self, prefix, 0) + for index in range(others): + other = support.StandaloneReportTests.report_shape( + self, prefix + "a_" + str(index), 0 + ) + document["test_ids"].insert(index, other["test_ids"][0]) + document["callbacks"][1:1] = other["callbacks"][1:-1] + document["tests_run"] += 1 + baseline = len(json.dumps(document, sort_keys=True, separators=(",", ":"))) + padding, remainder = divmod(2_000_000 - baseline, 4) + if remainder == 0: + break + self.assertEqual(remainder, 0) + for extra in [0, 1]: + with self.subTest(extra=extra): + self.output.unlink(missing_ok=True) + source = ( + "import unittest\nclass ManyTests(unittest.TestCase): pass\n" + "for index in range(" + str(others) + "):\n" + " setattr(ManyTests, 'test_a_' + str(index), lambda self: None)\n" + "setattr(ManyTests, 'test_' + 'x' * " + + str(padding + extra) + + ", lambda self: None)\n" + ) + result, document = self.run_report([self.module(source)]) + self.assertEqual(result.returncode == 0, extra == 0, result.stderr) + self.assertLessEqual(len(self.output.read_bytes()), 2_000_000) + if extra == 0: + self.assertEqual(len(self.output.read_bytes()), 2_000_000) + + def test_selected_code_cannot_change_report_budget_constants(self): + for action in [ + "sys.modules['__main__'].MAX_ITEMS = 5000", + "sys.modules['scripts.unittest_report_origin'].MAX_BYTES = 5000000", + "sys.modules['__main__'].ENCODER.sort_keys = False", + ]: + with self.subTest(action=action): + source = "import sys\n" + PASS.replace( + " with self.subTest(index=1):", + " " + action + "\n with self.subTest(index=1):", + ) + self.rejected([self.module(source)]) + + def test_selected_inventory_cannot_omit_empty_or_foreign_nested_modules(self): + first = self.module() + for source in [ + "pass\n", + PASS + "\nNestedTests.__module__ = 'foreign.test_other'\n", + ]: + with self.subTest(source=source): + other = self.module(source, name="other.test_selected") + self.rejected([first, other]) + + def test_nested_fixture_failure_has_actual_redacted_module_identity(self): + module = self.module( + PASS + "\ndef setUpModule(): raise RuntimeError('private fixture error')\n" + ) + document = self.rejected([module]) + event = next( + row for row in document["callbacks"] if row["callback"] == "addError" + ) + self.assertEqual(event["test_id"], "setUpModule (" + module + ")") + self.assertNotIn("private fixture error", json.dumps(document)) + + def test_nested_callback_budget_remains_exact(self): + for extra in [0, 1]: + with self.subTest(extra=extra): + self.output.unlink(missing_ok=True) + module = self.module( + "import unittest\nclass NestedTests(unittest.TestCase):\n" + " def test_many(self):\n for _ in range(" + + str(4091 + extra) + + "):\n with self.subTest(index=1): pass\n" + ) + result, document = self.run_report([module]) + self.assertEqual(result.returncode == 0, extra == 0, result.stderr) + self.assertLessEqual(len(document["callbacks"]), 4096) + + def test_initializer_drift_after_report_write_is_rejected(self): + source = ( + "import sys\n" + + PASS + + """ + owner = sys.modules['__main__'] + original = owner.ReportOutput.write + def changed(output, document): + original(output, document) + Path('scripts/controls/__init__.py').write_bytes(b'changed = True\n') + owner.ReportOutput.write = changed +""".replace("b'changed = True\n'", "b'changed = True\\n'") + ) + self.rejected([self.module(source, initializers=True)]) + + def test_actual_descriptor_read_race_fences_same_inode_source_bytes(self): + from scripts import unittest_report_origin as origin + + module = self.module() + path = self.root.joinpath(*module.split(".")).with_suffix(".py") + original = os.read + calls = [] + + def changed(descriptor, count): + raw = original(descriptor, count) + calls.append(count) + path.write_bytes(b"x" * path.stat().st_size) + return raw + + with patch.object(origin.os, "read", changed): + with self.assertRaises(origin.ReportError): + origin.source_snapshot(self.root, path.relative_to(self.root)) + self.assertTrue(calls) + + def test_actual_hostile_long_testcase_identity_rejects_in_bounded_child(self): + self.process_timeout = 3 + source = PASS + "\nNestedTests.__qualname__ = 'test_a.' * 8000 + '!'\n" + self.rejected([self.module(source)]) + + def test_actual_hostile_long_fixture_identity_rejects_in_bounded_child(self): + self.process_timeout = 3 + source = ( + PASS + + """ +unittest.suite._ErrorHolder.id = lambda self: 'setUpModule (scripts.' + 'test_a.' * 8000 + '!)' +def setUpModule(): raise RuntimeError('synthetic fixture error') +""" + ) + self.rejected([self.module(source)]) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/test_xcode_selection.py b/scripts/test_xcode_selection.py @@ -8,17 +8,212 @@ import subprocess import sys import tempfile import time +import traceback import unittest from pathlib import Path +from scripts.fixture_tool_dispatch import CompilerCommand, FixtureToolDispatcher +from scripts.fixture_test_policy import limit, load_policy + ROOT = Path(__file__).resolve().parent.parent +class OwnedFixtureProcess: + """Reserve a fixture group leader until its descendants have settled.""" + + def __init__(self, argv, *, cwd, env, stdout, stderr, record_path): + self.argv = argv + self.record_path = record_path + self.started = time.monotonic() + self.deadline = self.started + limit("xcode_wrapper") + self.process = subprocess.Popen( + argv, cwd=cwd, env=env, stdout=stdout, stderr=stderr, start_new_session=True + ) + self.pid = self.process.pid + self.returncode = None + self.attempted_cleanup = False + self.settled = False + self.record = { + "argv": argv, + "cwd": str(cwd), + "pid": self.pid, + "pgid": self.pid, + "started_ns": time.time_ns(), + "signals": [], + "observations": [], + "policy_id": load_policy()["id"], + "policy_sha256": load_policy()["source_sha256"], + } + + def poll(self): + if self.settled: + return self.returncode + result = os.waitid(os.P_PID, self.pid, os.WEXITED | os.WNOHANG | os.WNOWAIT) + if result is not None: + self.returncode = ( + result.si_status + if result.si_code == os.CLD_EXITED + else -result.si_status + ) + self.record.setdefault("exit_observed_ns", time.time_ns()) + return self.returncode + + def wait(self, timeout): + deadline = min(self.deadline, time.monotonic() + timeout) + while self.poll() is None: + remaining = deadline - time.monotonic() + if remaining <= 0: + raise subprocess.TimeoutExpired(self.argv, timeout) + time.sleep(min(0.01, remaining)) + return self.returncode + + def send_signal(self, number): + if self.poll() is None: + os.kill(self.pid, number) + self.record["signals"].append( + {"pid": self.pid, "signal": number, "at_ns": time.time_ns()} + ) + + def group_signal(self, number): + try: + os.killpg(self.pid, number) + self.record["signals"].append( + {"pgid": self.pid, "signal": number, "at_ns": time.time_ns()} + ) + except ProcessLookupError: + pass + + def signal_live_group(self, number, deadline): + if self.poll() is not None and not self.members(deadline): + return False + try: + self.group_signal(number) + except PermissionError: + self.record.setdefault("signal_errors", []).append( + { + "signal": number, + "at_ns": time.time_ns(), + "error": traceback.format_exc(), + } + ) + if self.poll() is None or self.members(deadline): + raise + return False + return True + + def capture_members(self, deadline): + remaining = deadline - time.monotonic() + if remaining <= 0.05: + raise AssertionError("owned observer cleanup budget is exhausted") + argv = ["ps", "-axo", "pid=,ppid=,pgid=,uid=,lstart="] + observer = subprocess.Popen( + argv, + stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + start_new_session=True, + ) + command = {"argv": argv, "pid": observer.pid, "pgid": observer.pid} + self.record["observations"].append(command) + try: + try: + stdout, stderr = observer.communicate( + timeout=min(0.25, remaining - 0.05) + ) + except subprocess.TimeoutExpired: + observer.kill() + command["timeout"] = True + stdout, stderr = observer.communicate( + timeout=max(0.001, deadline - time.monotonic()) + ) + except BaseException: + command["cleanup_exception"] = traceback.format_exc() + raise + finally: + command.update( + exit=observer.returncode, + wait_reaped=observer.returncode is not None, + at_ns=time.time_ns(), + ) + try: + os.killpg(observer.pid, 0) + command["group_absent"] = False + except ProcessLookupError: + command["group_absent"] = True + if not command["group_absent"]: + raise AssertionError("owned process observer group remains") + return stdout, stderr, command + + def members(self, deadline): + stdout, stderr, command = self.capture_members(deadline) + prefix = str(self.record_path) + f".ps-{len(self.record['observations']):02}" + Path(prefix + ".stdout").write_bytes(stdout) + Path(prefix + ".stderr").write_bytes(stderr) + if len(stdout) > 2_000_000 or len(stderr) > 65_536 or command["exit"]: + raise AssertionError("owned fixture group observation failed") + rows = [] + for line in stdout.decode().splitlines(): + fields = line.split(None, 4) + if len(fields) == 5 and int(fields[2]) == self.pid: + rows.append(dict(zip(("pid", "ppid", "pgid", "uid", "birth"), fields))) + command["members"] = rows + return [row for row in rows if int(row["pid"]) != self.pid] + + def require_group_absent(self): + try: + os.killpg(self.pid, 0) + except ProcessLookupError: + return + raise AssertionError("owned fixture group remains after leader reaping") + + def close(self): + if self.attempted_cleanup: + if not self.settled: + raise AssertionError("previous owned fixture cleanup failed") + return + self.attempted_cleanup = True + started = time.monotonic() + deadline = started + limit("leaf_cleanup") + self.record["cleanup_started_ns"] = time.time_ns() + try: + if self.signal_live_group(signal.SIGTERM, deadline): + time.sleep(0.1) + self.signal_live_group(signal.SIGKILL, deadline) + while self.members(deadline): + if time.monotonic() >= deadline: + raise AssertionError("owned fixture descendants did not settle") + time.sleep(min(0.01, max(0, deadline - time.monotonic()))) + self.record["descendants_absent_ns"] = time.time_ns() + self.returncode = self.process.wait( + timeout=max(0.001, deadline - time.monotonic()) + ) + self.record.update(exit=self.returncode, wait_reaped=True) + self.require_group_absent() + self.record["group_absent"] = True + if time.monotonic() > deadline: + raise AssertionError("owned fixture cleanup exceeded two seconds") + self.settled = True + except BaseException: + self.record["cleanup_exception"] = traceback.format_exc() + raise + finally: + self.record.update( + settled=self.settled, + cleanup_seconds=time.monotonic() - started, + cleanup_ended_ns=time.time_ns(), + ) + self.record_path.write_text(json.dumps(self.record, indent=2) + "\n") + + class XcodeSelectionTests(unittest.TestCase): def setUp(self) -> None: - self.directory = tempfile.TemporaryDirectory() - self.addCleanup(self.directory.cleanup) - self.root = Path(self.directory.name) + evidence = os.environ.get("TERA_C138_TEST_EVIDENCE") + if evidence: + self.root = Path(tempfile.mkdtemp(prefix="xcode-selector-", dir=evidence)) + else: + self.directory = tempfile.TemporaryDirectory() + self.addCleanup(self.directory.cleanup) + self.root = Path(self.directory.name) (self.root / "scripts").mkdir() shutil.copyfile(ROOT / "scripts/xcode.sh", self.root / "scripts/xcode.sh") (self.root / "TeraUITests").mkdir() @@ -27,14 +222,14 @@ class XcodeSelectionTests(unittest.TestCase): self.root / "TeraUITests/TeraAccessibilityUITests.swift", ) self.arguments = self.root / "arguments.json" - executable = self.root / "xcodebuild" - executable.write_text( - f"#!{sys.executable}\n" + self.selector_sequence = 0 + self.dispatcher = FixtureToolDispatcher(self.root, ("xcodebuild",)) + self.dispatcher.executable( + "xcodebuild", "import json, os, sys\n" "from pathlib import Path\n" - "Path(os.environ['TEST_XCODE_ARGUMENTS']).write_text(json.dumps(sys.argv[1:]))\n" + "Path(os.environ['TEST_XCODE_ARGUMENTS']).write_text(json.dumps(sys.argv[1:]))\n", ) - executable.chmod(0o700) self.environment = { **os.environ, "PATH": f"{self.root}:{os.environ['PATH']}", @@ -45,7 +240,8 @@ class XcodeSelectionTests(unittest.TestCase): } def run_selector(self, target: str, *selector: str) -> subprocess.CompletedProcess: - return subprocess.run( + self.selector_sequence += 1 + command = CompilerCommand( [ "/bin/bash", str(self.root / "scripts/xcode.sh"), @@ -54,10 +250,21 @@ class XcodeSelectionTests(unittest.TestCase): target, *selector, ], + self.root, env=self.environment, - capture_output=True, - text=True, - check=False, + budget="selector_command", + ) + try: + result = command.execute() + finally: + (self.root / f"selector-{self.selector_sequence:02}.json").write_text( + json.dumps(command.record, indent=2) + ) + return subprocess.CompletedProcess( + result.args, + result.returncode, + result.stdout.decode(), + result.stderr.decode(), ) def assert_selection(self, target: str, *selector: str) -> None: @@ -132,6 +339,9 @@ class PhysicalXcodeLifecycleTests(unittest.TestCase): shutil.copyfile(ROOT / "scripts" / name, self.root / "scripts" / name) self.tools = self.root / "tools" self.tools.mkdir() + self.dispatcher = FixtureToolDispatcher( + self.tools, ("xcrun", "uv", "xcodebuild") + ) self.locks = self.root / "locks" self.locks.mkdir() self.sentinel = self.locks / "tera-ios-lock-state.unrelated" @@ -186,14 +396,20 @@ class PhysicalXcodeLifecycleTests(unittest.TestCase): "sys.exit(status)\n", ) self.sequence = 0 + self.active_process = None def executable(self, name: str, body: str) -> None: - path = self.tools / name - path.write_text(f"#!{sys.executable}\n" + body) - path.chmod(0o700) + self.dispatcher.executable(name, body) - def await_file(self, path: Path, process: subprocess.Popen) -> dict: - deadline = time.monotonic() + 5 + def await_file( + self, path: Path, process: subprocess.Popen, *, timeout=None + ) -> dict: + deadline = min( + process.deadline, + process.started + limit("xcode_readiness") + if timeout is None + else time.monotonic() + timeout, + ) while time.monotonic() < deadline: if path.exists(): try: @@ -207,14 +423,24 @@ class PhysicalXcodeLifecycleTests(unittest.TestCase): time.sleep(0.01) self.fail("wrapper fixture readiness deadline expired") - def settle_on_failure(self, process: subprocess.Popen) -> None: - if process.poll() is None: - process.terminate() - try: - process.wait(timeout=3) - except subprocess.TimeoutExpired: - process.kill() - process.wait(timeout=3) + def settle_on_failure(self, process) -> None: + if process is not None: + process.close() + + def check_previous_cleanup(self) -> None: + if self.active_process is not None and not self.active_process.settled: + raise AssertionError("prior physical fixture cleanup is incomplete") + + def restore_role(self, process, mode, xcode, previous_mode, prefix) -> None: + self.settle_on_failure(process) + started = time.time_ns() + if mode == "missing": + (self.tools / "saved-xcode").rename(xcode) + else: + xcode.chmod(previous_mode) + Path(str(prefix) + ".role-restoration.json").write_text( + json.dumps({"started_ns": started, "ended_ns": time.time_ns()}) + "\n" + ) def observe_child(self, process, paths, mode, forwarded): ready, event, release, settled = paths @@ -224,7 +450,7 @@ class PhysicalXcodeLifecycleTests(unittest.TestCase): self.assertTrue(Path(data["lock"]).exists()) if forwarded is not None: process.send_signal(forwarded) - signal_data = self.await_file(event, process) + signal_data = self.await_file(event, process, timeout=5) self.assertEqual(signal_data["pid"], data["pid"]) self.assertEqual(signal_data["signal"], forwarded) self.assertTrue(signal_data["lock_exists"]) @@ -244,6 +470,7 @@ class PhysicalXcodeLifecycleTests(unittest.TestCase): def lifecycle( self, operation: str, mode: str, forwarded: int | None = None ) -> None: + self.check_previous_cleanup() self.sequence += 1 prefix = self.root / f"run-{self.sequence:02}" preflight, ready, release, event, settled = ( @@ -296,14 +523,21 @@ class PhysicalXcodeLifecycleTests(unittest.TestCase): xcode.rename(self.tools / "saved-xcode") elif mode == "not-executable": xcode.chmod(0o600) + process = None try: with ( Path(str(prefix) + ".stdout").open("wb") as stdout, Path(str(prefix) + ".stderr").open("wb") as stderr, ): - process = subprocess.Popen( - argv, cwd=self.root, env=environment, stdout=stdout, stderr=stderr + process = OwnedFixtureProcess( + argv, + cwd=self.root, + env=environment, + stdout=stdout, + stderr=stderr, + record_path=Path(str(prefix) + ".ownership.json"), ) + self.active_process = process self.addCleanup(self.settle_on_failure, process) if mode == "early": uv_log = Path(str(prefix) + ".uv") @@ -349,10 +583,7 @@ class PhysicalXcodeLifecycleTests(unittest.TestCase): self.sentinel.read_bytes(), b"unrelated invocation lock\n" ) finally: - if mode == "missing": - (self.tools / "saved-xcode").rename(xcode) - else: - xcode.chmod(previous_mode) + self.restore_role(process, mode, xcode, previous_mode, prefix) def every_branch(self, mode: str, forwarded: int | None = None) -> None: for operation in self.OPERATIONS: @@ -393,5 +624,154 @@ class PhysicalXcodeLifecycleTests(unittest.TestCase): self.every_branch("success") +class XcodeFixtureOwnershipTests(unittest.TestCase): + def setUp(self): + evidence = os.environ.get("TERA_C138_TEST_EVIDENCE") + if evidence: + self.root = Path(tempfile.mkdtemp(prefix="owned-xcode-", dir=evidence)) + else: + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + self.root = Path(temporary.name).resolve() + self.ready = self.root / "ready.json" + self.late = self.root / "late" + self.sentinel = self.root / "unrelated" + self.sentinel.write_bytes(b"unrelated invocation\n") + + def start_owned(self, leader_finish): + child = self.root / "child.py" + child.write_text( + "import json, os, signal, time\nfrom pathlib import Path\n" + "signal.signal(signal.SIGTERM, signal.SIG_IGN)\n" + "signal.signal(signal.SIGINT, signal.SIG_IGN)\n" + f"Path({str(self.ready)!r}).write_text(json.dumps({{'pid': os.getpid(), 'parent': os.getppid(), 'pgid': os.getpgrp()}}))\n" + "os.close(1); os.close(2)\n" + "time.sleep(8)\n" + f"Path({str(self.late)!r}).write_text('actual late writer ran\\n')\n" + ) + leader = self.root / "leader.py" + leader.write_text( + "import os, subprocess, sys, time\nfrom pathlib import Path\n" + f"subprocess.Popen([sys.executable, {str(child)!r}], stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)\n" + f"deadline=time.monotonic()+5\nwhile not Path({str(self.ready)!r}).exists() and time.monotonic()<deadline: time.sleep(.01)\n" + + leader_finish + ) + with ( + (self.root / "stdout").open("wb") as stdout, + (self.root / "stderr").open("wb") as stderr, + ): + process = OwnedFixtureProcess( + [sys.executable, str(leader)], + cwd=self.root, + env=os.environ, + stdout=stdout, + stderr=stderr, + record_path=self.root / "ownership.json", + ) + self.addCleanup(process.close) + deadline = time.monotonic() + 5 + while not self.ready.exists() and time.monotonic() < deadline: + time.sleep(0.01) + data = json.loads(self.ready.read_text()) + self.assertEqual(data["pgid"], process.pid) + return process, data + + def assert_settlement(self, process, data): + with self.assertRaises(ProcessLookupError): + os.kill(data["pid"], 0) + with self.assertRaises(ProcessLookupError): + os.killpg(process.pid, 0) + self.assertFalse(self.late.exists()) + self.assertEqual(self.sentinel.read_bytes(), b"unrelated invocation\n") + record = json.loads((self.root / "ownership.json").read_text()) + self.assertTrue(record["group_absent"]) + self.assertTrue(record["wait_reaped"]) + self.assertLess(record["descendants_absent_ns"], record["cleanup_ended_ns"]) + self.assertLessEqual(record["cleanup_seconds"], 2) + self.assertIn(signal.SIGKILL, [row["signal"] for row in record["signals"]]) + for observer in record["observations"]: + self.assertTrue(observer["wait_reaped"]) + self.assertTrue(observer["group_absent"]) + process.close() + + def test_exited_leader_keeps_identity_until_ignored_term_child_settles(self): + process, data = self.start_owned("sys.exit(7)\n") + self.assertEqual(process.wait(timeout=5), 7) + os.kill(process.pid, 0) + os.kill(data["pid"], 0) + process.close() + self.assertEqual(process.returncode, 7) + self.assert_settlement(process, data) + + def test_closed_stdio_live_leader_and_ignored_term_child_are_owned(self): + process, data = self.start_owned("os.close(1); os.close(2)\ntime.sleep(8)\n") + self.assertIsNone(process.poll()) + process.close() + self.assertEqual(process.returncode, -signal.SIGTERM) + self.assert_settlement(process, data) + + def test_exited_leader_without_descendants_reaps_actual_status(self): + body = self.root / "exit.py" + body.write_text("import sys\nsys.exit(7)\n") + with ( + (self.root / "stdout").open("wb") as stdout, + (self.root / "stderr").open("wb") as stderr, + ): + process = OwnedFixtureProcess( + [sys.executable, str(body)], + cwd=self.root, + env=os.environ, + stdout=stdout, + stderr=stderr, + record_path=self.root / "ownership.json", + ) + self.addCleanup(process.close) + self.assertEqual(process.wait(timeout=5), 7) + held = os.waitid(os.P_PID, process.pid, os.WEXITED | os.WNOHANG | os.WNOWAIT) + self.assertEqual((held.si_pid, held.si_status), (process.pid, 7)) + process.close() + self.assertEqual(process.returncode, 7) + with self.assertRaises(ProcessLookupError): + os.killpg(process.pid, 0) + record = json.loads((self.root / "ownership.json").read_text()) + self.assertTrue(record["wait_reaped"]) + self.assertTrue(record["group_absent"]) + self.assertEqual(record["signals"], []) + self.assertLessEqual(record["cleanup_seconds"], 2) + + def test_timeout_settles_before_missing_role_restore_and_next_subcase(self): + fixture = PhysicalXcodeLifecycleTests() + fixture.setUp() + self.addCleanup(fixture.doCleanups) + original = (fixture.tools / "xcrun.body.py").read_text() + fixture.executable( + "xcrun", + "import json, os, signal, time\nfrom pathlib import Path\n" + "signal.signal(signal.SIGTERM, signal.SIG_IGN)\n" + "signal.signal(signal.SIGINT, signal.SIG_IGN)\n" + "Path(os.environ['MOCK_READY']).write_text(json.dumps({'pid': os.getpid(), 'parent': os.getppid(), 'pgid': os.getpgrp()}))\n" + "time.sleep(8)\n" + "Path(os.environ['MOCK_READY']+'.late').write_text('actual late writer ran\\n')\n" + + original, + ) + with self.assertRaises(subprocess.TimeoutExpired): + fixture.lifecycle("physical-app-build", "missing") + ready = json.loads((fixture.root / "run-01.ready").read_text()) + with self.assertRaises(ProcessLookupError): + os.kill(ready["pid"], 0) + ownership = json.loads((fixture.root / "run-01.ownership.json").read_text()) + restored = json.loads( + (fixture.root / "run-01.role-restoration.json").read_text() + ) + self.assertTrue(ownership["group_absent"]) + self.assertLessEqual(ownership["cleanup_seconds"], 2) + self.assertLessEqual(ownership["cleanup_ended_ns"], restored["started_ns"]) + self.assertTrue(os.access(fixture.tools / "xcodebuild", os.X_OK)) + self.assertFalse((fixture.root / "run-01.ready.late").exists()) + fixture.executable("xcrun", original) + fixture.lifecycle("physical-app-build", "not-executable") + self.assertEqual(fixture.sentinel.read_bytes(), b"unrelated invocation lock\n") + + if __name__ == "__main__": unittest.main() diff --git a/scripts/unittest_report.py b/scripts/unittest_report.py @@ -7,41 +7,133 @@ stderr and never become report fields. No parent contract or pytest is loaded. from __future__ import annotations -import hashlib -import importlib -import importlib.machinery -import importlib.util -import json -import os -import re -import stat import sys -import unittest -from pathlib import Path + +startup_paths = tuple(sys.path) +try: + if not __package__: + sys.path[:] = [sys._stdlib_dir, sys._stdlib_dir + "/lib-dynload"] + import hashlib + import importlib.util + import inspect + import json + import os + import re + import stat + import unittest + import weakref + from pathlib import Path + from types import FunctionType, MappingProxyType +except BaseException: + print("unittest-report: standard library origin rejected", file=sys.stderr) + raise SystemExit(1) from None +finally: + if not __package__: + sys.path[:] = startup_paths[1:] + + +def bootstrap_origin(): + """Load the owned helper source before a direct entry executes any package.""" + if __package__: + from . import unittest_report_origin + + return unittest_report_origin + path = Path(os.path.abspath(__file__)).with_name("unittest_report_origin.py") + + def metadata(value): + return ( + value.st_dev, + value.st_ino, + value.st_mode, + value.st_nlink, + value.st_size, + value.st_mtime_ns, + value.st_ctime_ns, + ) + + directory = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) + try: + descriptor = os.open( + path.name, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=directory + ) + try: + before = os.fstat(descriptor) + if not stat.S_ISREG(before.st_mode) or not 0 < before.st_size <= 2_000_000: + raise ValueError("invalid bootstrap source") + with os.fdopen(os.dup(descriptor), "rb") as source: + raw = source.read(before.st_size + 1) + if len(raw) != before.st_size or metadata(os.fstat(descriptor)) != metadata( + before + ): + raise ValueError("bootstrap source changed") + if metadata( + os.stat(path.name, dir_fd=directory, follow_symlinks=False) + ) != metadata(before): + raise ValueError("bootstrap origin changed") + finally: + os.close(descriptor) + finally: + os.close(directory) + spec = importlib.util.spec_from_file_location( + "scripts.unittest_report_origin", path + ) + module = importlib.util.module_from_spec(spec) + sys.modules[spec.name] = module + exec(compile(raw, str(path), "exec"), module.__dict__) + expected = module.source_metadata(before), hashlib.sha256(raw).hexdigest() + module.require( + module.source_identity(path.parent.parent, Path("scripts") / path.name) + == expected, + "bootstrap helper changed", + ) + producer = sys.modules[__name__] + producer.__spec__ = importlib.util.spec_from_file_location(__name__, __file__) + sys.modules["scripts.unittest_report"] = producer + return module + + +try: + origin = bootstrap_origin() + origin.standard_library_origins() +except BaseException: + print("unittest-report: bootstrap source or origin rejected", file=sys.stderr) + raise SystemExit(1) from None +ReportError = origin.ReportError +identity = origin.identity +load_modules = origin.load_modules +open_directory = origin.open_directory +owned_origin = origin.owned_origin +require = origin.require +selectors = origin.selectors +source_identity = origin.source_identity +source_metadata = origin.source_metadata SCHEMA = "tera.unittest-callback-report.v1" MAX_BYTES = 2_000_000 MAX_ITEMS = 4096 -MODULE = re.compile(r"scripts\.test_[A-Za-z0-9_]+") -TEST_ID = re.compile( - r"scripts\.test_[A-Za-z0-9_]+\.[A-Za-z_][A-Za-z0-9_]*\.[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*" -) +TEST_ID = re.compile(r"scripts(?:\.[A-Za-z_][A-Za-z0-9_]*){3,}") FIXTURE_ID = re.compile( r"(?:setUpClass|tearDownClass|setUpModule|tearDownModule) " - r"\((scripts\.test_[A-Za-z0-9_]+(?:\.[A-Za-z_][A-Za-z0-9_]*)*)\)" + r"\((scripts(?:\.[A-Za-z_][A-Za-z0-9_]*)+)\)" ) +TESTCASE_ID = unittest.TestCase.id +TESTCASE_RUN = unittest.TestCase.run +TESTCASE_CALL = unittest.TestCase.__call__ +TESTCASE_SETUP_CALL = unittest.TestCase._callSetUp +TESTCASE_METHOD_CALL = unittest.TestCase._callTestMethod +SUITE_RUN = unittest.TestSuite.run +SUITE_CALL = unittest.TestSuite.__call__ +RESULT_METHODS = MappingProxyType( + { + name: method + for name, method in vars(unittest.TestResult).items() + if callable(method) and not name.startswith("__") + } +) +CALLBACK_FIELDS = frozenset({"callbacks", "nonpassing", "rejected"}) ENCODER = json.JSONEncoder(sort_keys=True, separators=(",", ":"), ensure_ascii=True) -class ReportError(Exception): - """A stable, secret-free report rejection.""" - - -def require(condition, message): - if not condition: - raise ReportError(message) - - def encoded_size(value): return sum(len(chunk) for chunk in ENCODER.iterencode(value)) @@ -62,59 +154,6 @@ class ReportBudget: destination.append(value) -def selectors(values): - require(0 < len(values) <= MAX_ITEMS, "selected module count is invalid") - seen = set() - for value in values: - require( - isinstance(value, str) and len(value) <= 255, "selected module is invalid" - ) - require(MODULE.fullmatch(value) is not None, "selected module is not canonical") - require(value not in seen, "selected module is duplicated") - seen.add(value) - return values - - -def open_directory(path): - """Walk every component with nofollow; retain the actual directory descriptor.""" - require( - isinstance(path, str) and 0 < len(path) <= 4096 and "\x00" not in path, - "output path is invalid", - ) - absolute = path.startswith("/") - descriptor = os.open("/" if absolute else ".", os.O_RDONLY | os.O_DIRECTORY) - try: - for component in path.split("/"): - if not component or component == ".": - continue - child = os.open( - component, - os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, - dir_fd=descriptor, - ) - os.close(descriptor) - descriptor = child - return descriptor - except BaseException: - os.close(descriptor) - raise - - -def identity(information): - return (information.st_dev, information.st_ino) - - -def source_metadata(information): - return ( - identity(information), - information.st_mode, - information.st_nlink, - information.st_size, - information.st_mtime_ns, - information.st_ctime_ns, - ) - - class ReportOutput: """Exclusive output with bounded byte, position and identity fencing.""" @@ -238,91 +277,78 @@ class ReportOutput: os.close(self.directory) -def source_snapshot(root, relative): - directory = open_directory(str(root / relative.parent)) - try: - descriptor = os.open( - relative.name, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=directory - ) - try: - before = os.fstat(descriptor) - require( - stat.S_ISREG(before.st_mode) and 0 < before.st_size <= MAX_BYTES, - "source origin is not a bounded regular file", - ) - digest = hashlib.sha256() - chunks = [] - remaining = before.st_size - while remaining: - chunk = os.read(descriptor, min(65536, remaining)) - require(bool(chunk), "source origin changed while reading") - digest.update(chunk) - chunks.append(chunk) - remaining -= len(chunk) - after = os.fstat(descriptor) - require( - source_metadata(before) == source_metadata(after), - "source origin changed while reading", - ) - current = os.stat(relative.name, dir_fd=directory, follow_symlinks=False) - require( - source_metadata(current) == source_metadata(before), - "source origin identity changed", - ) - return (source_metadata(before), digest.hexdigest()), b"".join(chunks) - finally: - os.close(descriptor) - finally: - os.close(directory) +def bound_method(instance, name, implementation): + method = getattr(instance, name) + return ( + getattr(method, "__self__", None) is instance + and getattr(method, "__func__", None) is implementation + ) -def source_identity(root, relative): - return source_snapshot(root, relative)[0] +def suite_execution(suite): + return type(suite).__call__ is SUITE_CALL and bound_method(suite, "run", SUITE_RUN) -def owned_origin(module, expected): - require( - getattr(module, "__file__", None) == str(expected), - "loaded module file origin differs", +def authored_method(test): + descriptor = inspect.getattr_static(type(test), test._testMethodName) + if not isinstance(descriptor, (FunctionType, staticmethod, classmethod)): + return False + expected = descriptor.__get__(test, type(test)) + actual = getattr(test, test._testMethodName) + if isinstance(expected, FunctionType): + return actual is expected + return ( + getattr(actual, "__self__", None) is expected.__self__ + and getattr(actual, "__func__", None) is expected.__func__ ) + + +def testcase_execution(test): + return authored_method(test) and all( + bound_method(test, name, implementation) + for name, implementation in ( + ("run", TESTCASE_RUN), + ("__call__", TESTCASE_CALL), + ("_callTestMethod", TESTCASE_METHOD_CALL), + ) + ) + + +def guard_setup(test, verify_execution=None): + """Check dispatch after ordinary setup, inside the original stdlib run.""" require( - getattr(getattr(module, "__spec__", None), "origin", None) == str(expected), - "loaded module specification origin differs", + bound_method(test, "_callSetUp", TESTCASE_SETUP_CALL), + "testcase setup dispatch is overridden", ) + value = canonical_id(test) + method = getattr(test, test._testMethodName) + executables = (getattr(method, "__func__", method), TESTCASE_METHOD_CALL) + before = tuple(origin.callable_identity(item) for item in executables) + def checked_setup(): + try: + TESTCASE_SETUP_CALL(test) + if vars(test).get("_callSetUp") is checked_setup: + del test._callSetUp + if verify_execution is not None: + verify_execution(test) + require( + canonical_id(test) == value + and testcase_execution(test) + and tuple(origin.callable_identity(item) for item in executables) + == before, + "testcase execution changed during setup", + ) + finally: + if vars(test).get("_callSetUp") is checked_setup: + del test._callSetUp -def load_modules(root, names): - modules, sources = [], {} - for name in names: - relative = Path("scripts") / (name.split(".")[1] + ".py") - before, raw = source_snapshot(root, relative) - spec = importlib.util.find_spec(name) - require( - spec is not None and spec.origin == str(root / relative), - "selected module resolution origin differs", - ) - require( - type(spec.loader) is importlib.machinery.SourceFileLoader, - "selected module loader differs", - ) - module = importlib.util.module_from_spec(spec) - require( - source_identity(root, relative) == before, - "selected module changed before import", - ) - sys.modules[name] = module - exec(compile(raw, str(root / relative), "exec"), module.__dict__) - owned_origin(module, root / relative) - require( - source_identity(root, relative) == before, - "selected module changed during import", - ) - sources[relative] = before - modules.append(module) - return modules, sources + test._callSetUp = checked_setup + require(test._callSetUp is checked_setup, "testcase setup guard differs") def iter_tests(suite): + require(suite_execution(suite), "suite execution is overridden") pending = [iter(suite)] visited = set() while pending: @@ -331,7 +357,9 @@ def iter_tests(suite): pending.pop() elif isinstance(current, unittest.TestSuite): require( - id(current) not in visited and len(visited) < MAX_ITEMS, + id(current) not in visited + and len(visited) < MAX_ITEMS + and suite_execution(current), "suite inventory is cyclic or oversized", ) visited.add(id(current)) @@ -345,6 +373,10 @@ def iter_tests(suite): def canonical_id(test): + require( + type(test).id is TESTCASE_ID and unittest.TestCase.id is TESTCASE_ID, + "testcase identity is overridden", + ) value = test.id() require( isinstance(value, str) and len(value) <= MAX_BYTES, @@ -352,7 +384,13 @@ def canonical_id(test): ) require(TEST_ID.fullmatch(value) is not None, "testcase identity is not canonical") require( - value == unittest.TestCase.id(test), + value == TESTCASE_ID(test) + and value + == type(test).__module__ + + "." + + type(test).__qualname__ + + "." + + test._testMethodName, "testcase identity differs from its actual class and method", ) return value @@ -378,160 +416,276 @@ def inventory(suite, names, budget): return values -class CallbackResult(unittest.TestResult): - """Store bounded redacted callback facts, never formatted errors or reprs.""" - - def __init__(self, test_ids, budget, stream): - super().__init__() - self.callbacks = [] - self.inventory = set(test_ids) - self.budget = budget - self.stream = stream - self.active = None - self.started, self.completed, self.succeeded = set(), set(), set() - self.ordinal = 0 - self.success = False - self.nonpassing = False - self.rejected = False - self.run_started = False - self.run_stopped = False - - def event(self, callback, test_id=None, **fields): - value = {"callback": callback, **fields} - if test_id is not None: - value["test_id"] = test_id - try: - self.budget.append(self.callbacks, value) - except ReportError: - self.rejected = True - self.stop() - raise - - def startTestRun(self): - require(not self.run_started, "test run started twice") - self.run_started = True - self.event("startTestRun") - super().startTestRun() - - def stopTestRun(self): - require( - self.run_started and not self.run_stopped, "test run stop order differs" +def execution_identity(test): + implementations = [inspect.getattr_static(type(test), test._testMethodName)] + for name in ("run", "__call__", "_callSetUp", "_callTestMethod"): + method = getattr(test, name) + implementations.append(getattr(method, "__func__", method)) + implementations.extend( + ( + TESTCASE_RUN, + TESTCASE_CALL, + TESTCASE_SETUP_CALL, + TESTCASE_METHOD_CALL, + SUITE_RUN, + SUITE_CALL, ) - self.run_stopped = True - self.event("stopTestRun") - super().stopTestRun() - - def startTest(self, test): - require( - self.run_started and not self.run_stopped, - "testcase is outside run boundaries", - ) - value = canonical_id(test) - require( - value in self.inventory - and value not in self.started - and self.active is None, - "testcase start differs from inventory", - ) - self.budget.add(len(str(self.testsRun + 1)) - len(str(self.testsRun))) - self.event("startTest", value) - super().startTest(test) - self.active, self.ordinal, self.success = value, 0, False - self.started.add(value) + ) + return canonical_id(test), tuple( + origin.callable_identity(item) for item in implementations + ) - def stopTest(self, test): - value = canonical_id(test) - require(value == self.active, "testcase stop order differs") - self.event("stopTest", value) - self.completed.add(value) - self.active = None - super().stopTest(test) - def addSuccess(self, test): - value = canonical_id(test) - require( - value == self.active and not self.success, "testcase success order differs" - ) - self.event("addSuccess", value) - self.success = True - self.succeeded.add(value) - super().addSuccess(test) +def callback_result_type(): + # Selected TestCases can inspect the result, but do not own observed facts. + facts = weakref.WeakKeyDictionary() + + class CallbackResult(unittest.TestResult): + """Store bounded redacted callback facts, never formatted errors or reprs.""" + + def __getattribute__(self, name): + # A callback shadow must never replace the captured observer implementation. + method = methods.get(name) + if method is not None: + if origin.callable_identity(method) != executables[name]: + facts[self]["rejected"] = True + raise ReportError("observer executable identity changed") + namespace = object.__getattribute__(self, "__dict__") + if any(key in namespace for key in methods) or any( + key in namespace for key in CALLBACK_FIELDS + ): + object.__setattr__(self, "rejected", True) + return method.__get__(self, type(self)) + return object.__getattribute__(self, name) + + def __setattr__(self, name, value): + if name in methods: + object.__setattr__(self, "rejected", True) + return + object.__setattr__(self, name, value) + + def __init__(self, test_ids, budget, stream, verify_execution=None): + facts[self] = { + "callbacks": [], + "nonpassing": False, + "rejected": False, + "verify_execution": verify_execution, + } + super().__init__() + self.inventory = set(test_ids) + self.budget = budget + self.stream = stream + self.active = None + self.started, self.completed, self.succeeded = set(), set(), set() + self.ordinal = 0 + self.success = False + self.nonpassing = False + self.rejected = False + self.run_started = False + self.run_stopped = False + + def _get_callbacks(self): + return tuple(MappingProxyType(event) for event in facts[self]["callbacks"]) + + def _set_callbacks(self, value): + del value + facts[self]["rejected"] = True + + callbacks = property(_get_callbacks, _set_callbacks) + del _get_callbacks, _set_callbacks + + def _get_nonpassing(self): + return facts[self]["nonpassing"] + + def _set_nonpassing(self, value): + if facts[self]["nonpassing"] and not value: + facts[self]["rejected"] = True + facts[self]["nonpassing"] |= bool(value) + + nonpassing = property(_get_nonpassing, _set_nonpassing) + del _get_nonpassing, _set_nonpassing + + def _get_rejected(self): + return facts[self]["rejected"] + + def _set_rejected(self, value): + facts[self]["rejected"] |= bool(value) + + rejected = property(_get_rejected, _set_rejected) + del _get_rejected, _set_rejected + + def event(self, callback, test_id=None, **fields): + value = {"callback": callback, **fields} + if test_id is not None: + value["test_id"] = test_id + try: + self.budget.append(facts[self]["callbacks"], value) + except ReportError: + self.rejected = True + self.stop() + raise + + def startTestRun(self): + require(not self.run_started, "test run started twice") + self.run_started = True + self.event("startTestRun") + super().startTestRun() + + def stopTestRun(self): + require( + self.run_started and not self.run_stopped, "test run stop order differs" + ) + self.run_stopped = True + self.event("stopTestRun") + super().stopTestRun() + + def startTest(self, test): + verify_execution = facts[self]["verify_execution"] + if verify_execution is not None: + verify_execution(test) + if type(test).run is TESTCASE_RUN: + require(testcase_execution(test), "testcase execution is overridden") + guard_setup(test, verify_execution) + self.rejected |= ( + type(test).run is not TESTCASE_RUN + or type(test).__call__ is not TESTCASE_CALL + or unittest.TestCase.run is not TESTCASE_RUN + or not testcase_execution(test) + ) + require( + self.run_started and not self.run_stopped, + "testcase is outside run boundaries", + ) + value = canonical_id(test) + require( + value in self.inventory + and value not in self.started + and self.active is None, + "testcase start differs from inventory", + ) + self.budget.add(len(str(self.testsRun + 1)) - len(str(self.testsRun))) + self.event("startTest", value) + super().startTest(test) + self.active, self.ordinal, self.success = value, 0, False + self.started.add(value) + + def stopTest(self, test): + value = canonical_id(test) + require(value == self.active, "testcase stop order differs") + self.event("stopTest", value) + self.completed.add(value) + self.active = None + super().stopTest(test) + + def addSuccess(self, test): + value = canonical_id(test) + require( + value == self.active and not self.success, + "testcase success order differs", + ) + self.event("addSuccess", value) + self.success = True + self.succeeded.add(value) + super().addSuccess(test) + + def addSubTest(self, test, subtest, err): + del subtest + value = canonical_id(test) + require(value == self.active and not self.success, "subtest order differs") + self.ordinal += 1 + self.event( + "addSubTest", value, ordinal=self.ordinal, successful=err is None + ) + if err is not None: + self.nonpassing = True - def addSubTest(self, test, subtest, err): - del subtest - value = canonical_id(test) - require(value == self.active and not self.success, "subtest order differs") - self.ordinal += 1 - self.event("addSubTest", value, ordinal=self.ordinal, successful=err is None) - if err is not None: + def nonpass(self, callback, test): self.nonpassing = True + # _SubTest.id() includes raw parameter repr; its parent is the identity. + parent = getattr(test, "test_case", test) + if isinstance(parent, unittest.TestCase): + value = canonical_id(parent) + elif type(parent) is unittest.suite._ErrorHolder: + value = self.fixture_id(parent) + else: + value = "unittest.redacted_noncase" + self.event(callback, value) + print("unittest-report: " + callback, file=self.stream) + + def fixture_id(self, test): + value = test.id() + require( + isinstance(value, str) and len(value) <= MAX_BYTES, + "fixture identity is invalid", + ) + match = FIXTURE_ID.fullmatch(value) + require(match is not None, "fixture identity is not canonical") + require( + any(item.startswith(match[1] + ".") for item in self.inventory), + "fixture identity is outside inventory", + ) + return value + + def addFailure(self, test, err): + del err + self.nonpass("addFailure", test) + + def addError(self, test, err): + del err + self.nonpass("addError", test) + + def addSkip(self, test, reason): + del reason + self.nonpass("addSkip", test) + + def addExpectedFailure(self, test, err): + del err + self.nonpass("addExpectedFailure", test) + + def addUnexpectedSuccess(self, test): + self.nonpass("addUnexpectedSuccess", test) + + def wasSuccessful(self): + return not self.nonpassing and not self.rejected + + def passing(self): + boundaries = self.callbacks[:1] == ( + {"callback": "startTestRun"}, + ) and self.callbacks[-1:] == ({"callback": "stopTestRun"},) + return ( + boundaries + and self.testsRun > 0 + and self.wasSuccessful() + and not self.shouldStop + and self.active is None + and self.started == self.completed == self.succeeded == self.inventory + and self.testsRun == len(self.inventory) + ) - def nonpass(self, callback, test): - self.nonpassing = True - # _SubTest.id() includes raw parameter repr; its parent is the identity. - parent = getattr(test, "test_case", test) - if isinstance(parent, unittest.TestCase): - value = canonical_id(parent) - elif type(parent) is unittest.suite._ErrorHolder: - value = self.fixture_id(parent) - else: - value = "unittest.redacted_noncase" - self.event(callback, value) - print("unittest-report: " + callback, file=self.stream) - - def fixture_id(self, test): - value = test.id() - require( - isinstance(value, str) and len(value) <= MAX_BYTES, - "fixture identity is invalid", - ) - match = FIXTURE_ID.fullmatch(value) - require(match is not None, "fixture identity is not canonical") - require( - any(item.startswith(match[1] + ".") for item in self.inventory), - "fixture identity is outside inventory", - ) - return value - - def addFailure(self, test, err): - del err - self.nonpass("addFailure", test) - - def addError(self, test, err): - del err - self.nonpass("addError", test) - - def addSkip(self, test, reason): - del reason - self.nonpass("addSkip", test) - - def addExpectedFailure(self, test, err): - del err - self.nonpass("addExpectedFailure", test) - - def addUnexpectedSuccess(self, test): - self.nonpass("addUnexpectedSuccess", test) + methods = MappingProxyType( + { + **RESULT_METHODS, + **{ + name: method + for name, method in vars(CallbackResult).items() + if callable(method) and not name.startswith("__") + }, + } + ) + executables = MappingProxyType( + {name: origin.callable_identity(method) for name, method in methods.items()} + ) + return CallbackResult - def wasSuccessful(self): - return not self.nonpassing and not self.rejected - def passing(self): - boundaries = self.callbacks[:1] == [ - {"callback": "startTestRun"} - ] and self.callbacks[-1:] == [{"callback": "stopTestRun"}] - return ( - boundaries - and self.testsRun > 0 - and self.wasSuccessful() - and not self.shouldStop - and self.active is None - and self.started == self.completed == self.succeeded == self.inventory - and self.testsRun == len(self.inventory) - ) +CallbackResult = callback_result_type() def observe(suite, test_ids, budget, stream): - result = CallbackResult(test_ids, budget, stream) + verify_execution = origin.inventory_execution( + iter_tests(suite), execution_identity, authored_method + ) + result = CallbackResult(test_ids, budget, stream, verify_execution) result.startTestRun() try: suite(result) @@ -548,30 +702,50 @@ def observe(suite, test_ids, budget, stream): def execute(root, names, output, stream): + verify_implementation = origin.runtime_guard([origin, sys.modules[__name__]]) + verify_framework = origin.runtime_guard( + [unittest.case, unittest.suite, unittest.result] + ) producer = Path("scripts/unittest_report.py") initial = source_identity(root, producer) owned_origin(sys.modules[__name__], root / producer) - modules, sources = load_modules(root, names) - sources[producer] = initial + helper = Path("scripts/unittest_report_origin.py") + helper_initial = source_identity(root, helper) + owned_origin(origin, root / helper) + modules, fence = load_modules(root, names) + verify_implementation() + verify_framework() loader = unittest.TestLoader() suite = unittest.TestSuite(loader.loadTestsFromModule(module) for module in modules) require(not loader.errors, "testcase discovery failed") + verify_framework() budget = ReportBudget() test_ids = inventory(suite, names, budget) result, passing = observe(suite, test_ids, budget, stream) - for relative, before in sources.items(): + fence.verify() + for relative, before in [(producer, initial), (helper, helper_initial)]: require( source_identity(root, relative) == before, "source origin changed during execution", ) output.write( { - "schema": SCHEMA, + "schema": SCHEMA + if all(name.count(".") == 1 for name in names) + else "tera.unittest-callback-report.v2", "test_ids": test_ids, "tests_run": result.testsRun, - "callbacks": result.callbacks, + "callbacks": [dict(event) for event in result.callbacks], } ) + fence.verify() + for relative, before in [(producer, initial), (helper, helper_initial)]: + require( + source_identity(root, relative) == before, + "source changed during report output", + ) + verify_implementation() + verify_framework() return 0 if passing else 1 @@ -581,6 +755,12 @@ def main(arguments=None): status = 1 try: require(sys.version_info[:3] == (3, 14, 7), "frozen Python version differs") + if __spec__.name == "scripts.unittest_report": + print( + "unittest-report: use the direct file entry before package initialization", + file=sys.stderr, + ) + return 1 require( len(arguments) >= 3 and arguments[0] == "--report", "expected one explicit report output and selected modules", diff --git a/scripts/unittest_report_origin.py b/scripts/unittest_report_origin.py @@ -0,0 +1,393 @@ +"""Standalone nofollow source reads and canonical module ancestry admission.""" + +from __future__ import annotations + +import hashlib +import importlib.machinery +import importlib.util +import os +import re +import stat +import sys +import types +from pathlib import Path + +MAX_BYTES = 2_000_000 +MAX_ITEMS = 4096 +MODULE = re.compile(r"scripts(?:\.[A-Za-z_][A-Za-z0-9_]*)*\.test_[A-Za-z0-9_]+") + + +class ReportError(Exception): + """A stable, secret-free report rejection.""" + + +def require(condition, message): + if not condition: + raise ReportError(message) + + +def selectors(values): + require(0 < len(values) <= MAX_ITEMS, "selected module count is invalid") + seen = set() + for value in values: + require( + isinstance(value, str) and len(value) <= 255, "selected module is invalid" + ) + require(MODULE.fullmatch(value) is not None, "selected module is not canonical") + require(value not in seen, "selected module is duplicated") + seen.add(value) + return values + + +def open_directory(path): + """Walk every component with nofollow; retain the actual directory descriptor.""" + require( + isinstance(path, str) and 0 < len(path) <= 4096 and "\x00" not in path, + "output path is invalid", + ) + absolute = path.startswith("/") + descriptor = os.open("/" if absolute else ".", os.O_RDONLY | os.O_DIRECTORY) + try: + for component in path.split("/"): + if not component or component == ".": + continue + child = os.open( + component, + os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, + dir_fd=descriptor, + ) + os.close(descriptor) + descriptor = child + return descriptor + except BaseException: + os.close(descriptor) + raise + + +def identity(information): + return (information.st_dev, information.st_ino) + + +def source_metadata(information): + return ( + identity(information), + information.st_mode, + information.st_nlink, + information.st_size, + information.st_mtime_ns, + information.st_ctime_ns, + ) + + +def source_snapshot(root, relative, *, allow_empty=False): + directory = open_directory(str(root / relative.parent)) + try: + descriptor = os.open( + relative.name, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=directory + ) + try: + before = os.fstat(descriptor) + require( + stat.S_ISREG(before.st_mode) + and int(not allow_empty) <= before.st_size <= MAX_BYTES, + "source origin is not a bounded regular file", + ) + digest = hashlib.sha256() + chunks = [] + remaining = before.st_size + while remaining: + chunk = os.read(descriptor, min(65536, remaining)) + require(bool(chunk), "source origin changed while reading") + digest.update(chunk) + chunks.append(chunk) + remaining -= len(chunk) + after = os.fstat(descriptor) + require( + source_metadata(before) == source_metadata(after), + "source origin changed while reading", + ) + current = os.stat(relative.name, dir_fd=directory, follow_symlinks=False) + require( + source_metadata(current) == source_metadata(before), + "source origin identity changed", + ) + return (source_metadata(before), digest.hexdigest()), b"".join(chunks) + finally: + os.close(descriptor) + finally: + os.close(directory) + + +def source_identity(root, relative): + return source_snapshot(root, relative, allow_empty=relative.name == "__init__.py")[ + 0 + ] + + +def owned_origin(module, expected): + require( + getattr(module, "__file__", None) == str(expected), + "loaded module file origin differs", + ) + require( + getattr(getattr(module, "__spec__", None), "origin", None) == str(expected), + "loaded module specification origin differs", + ) + + +def directory_identity(root, relative): + descriptor = open_directory(str(root / relative)) + try: + information = os.fstat(descriptor) + return identity(information), information.st_mode + finally: + os.close(descriptor) + + +def initializer_snapshot(root, directory): + relative = directory / "__init__.py" + descriptor = open_directory(str(root / directory)) + try: + try: + os.stat(relative.name, dir_fd=descriptor, follow_symlinks=False) + except FileNotFoundError: + return None + return source_snapshot(root, relative, allow_empty=True)[0] + finally: + os.close(descriptor) + + +class SourceFence: + """Fence all selected bytes and ancestry before any selected code executes.""" + + def __init__(self, root, names): + self.root = root + self.sources, self.directories, self.initializers, self.loaded = {}, {}, {}, {} + for name in names: + components = name.split(".") + for length in range(1, len(components)): + directory = Path(*components[:length]) + self.directories[directory] = directory_identity(root, directory) + self.initializers[directory] = initializer_snapshot(root, directory) + relative = Path(*components).with_suffix(".py") + self.sources[relative] = source_identity(root, relative) + self.verify() + + def verify(self): + for relative, before in self.sources.items(): + require( + source_identity(self.root, relative) == before, + "selected source changed", + ) + for directory, before in self.directories.items(): + require( + directory_identity(self.root, directory) == before, + "package directory changed", + ) + require( + initializer_snapshot(self.root, directory) + == self.initializers[directory], + "package initializer changed", + ) + for name, module in self.loaded.items(): + require(sys.modules.get(name) is module, "loaded module identity changed") + self.module_origin(name, module) + + def module_origin(self, name, module): + directory = Path(*name.split(".")) + if directory not in self.directories: + owned_origin(module, (self.root / directory).with_suffix(".py")) + require( + module.__name__ == name + and module.__spec__.name == name + and type(module.__spec__.loader) + is importlib.machinery.SourceFileLoader, + "selected module identity or loader differs", + ) + return + self.package_spec(name, module.__spec__) + require( + list(getattr(module, "__path__", [])) == [str(self.root / directory)], + "loaded package search path differs", + ) + initializer = self.initializers[directory] + expected = str(self.root / directory / "__init__.py") if initializer else None + require( + getattr(module, "__file__", None) == expected, "loaded package file differs" + ) + + def package_spec(self, name, spec): + directory = Path(*name.split(".")) + initializer = self.initializers[directory] + require(spec is not None and spec.name == name, "package specification differs") + expected = str(self.root / directory / "__init__.py") if initializer else None + require(spec.origin == expected, "package origin differs") + require( + list(spec.submodule_search_locations or []) == [str(self.root / directory)], + "package specification search path differs", + ) + expected_loader = ( + importlib.machinery.SourceFileLoader + if initializer + else importlib.machinery.NamespaceLoader + ) + require(type(spec.loader) is expected_loader, "package loader differs") + + def package(self, name): + directory = Path(*name.split(".")) + self.verify() + if name in sys.modules: + module = sys.modules[name] + self.module_origin(name, module) + else: + spec = importlib.machinery.PathFinder.find_spec( + name, [str(self.root / directory.parent)] + ) + require(spec is not None, "package resolution failed") + module = importlib.util.module_from_spec(spec) + self.package_spec(name, module.__spec__) + sys.modules[name] = module + if name == "scripts": + for child in ("unittest_report", "unittest_report_origin"): + loaded = sys.modules.get(name + "." + child) + if loaded is not None: + owned_origin(loaded, self.root / directory / (child + ".py")) + setattr(module, child, loaded) + initializer = self.initializers[directory] + if initializer: + before, raw = source_snapshot( + self.root, directory / "__init__.py", allow_empty=True + ) + require( + before == initializer, "package initializer changed before import" + ) + exec( + compile(raw, str(self.root / directory / "__init__.py"), "exec"), + module.__dict__, + ) + self.module_origin(name, module) + self.loaded[name] = module + self.verify() + + def selected(self, name): + components = name.split(".") + for length in range(1, len(components)): + self.package(".".join(components[:length])) + relative = Path(*components).with_suffix(".py") + spec = importlib.util.find_spec(name) + require( + spec is not None and spec.origin == str(self.root / relative), + "selected module resolution origin differs", + ) + require( + type(spec.loader) is importlib.machinery.SourceFileLoader, + "selected module loader differs", + ) + module = importlib.util.module_from_spec(spec) + self.verify() + sys.modules[name] = module + before, raw = source_snapshot(self.root, relative) + require( + before == self.sources[relative], "selected source changed before import" + ) + exec(compile(raw, str(self.root / relative), "exec"), module.__dict__) + self.module_origin(name, module) + self.loaded[name] = module + self.verify() + return module + + +def load_modules(root, names): + fence = SourceFence(root, names) + modules = [fence.selected(name) for name in names] + return modules, fence + + +def callable_identity(value): + if isinstance(value, property): + return ( + value, + tuple( + callable_identity(method) + for method in (value.fget, value.fset, value.fdel) + ), + None, + ) + if isinstance(value, (staticmethod, classmethod)): + value = value.__func__ + return value, getattr(value, "__code__", None), getattr(value, "__defaults__", None) + + +def implementation_identity(module): + values = {} + for name, value in vars(module).items(): + if isinstance(value, types.FunctionType): + values[name] = callable_identity(value) + elif isinstance(value, type): + values[name] = ( + value, + {key: callable_identity(method) for key, method in vars(value).items()}, + ) + elif name.isupper(): + values[name] = value, dict(getattr(value, "__dict__", {})) + return values + + +def extension_child_origin(name, module): + """Only genuine spec-less children generated by the verified pyexpat parent.""" + return ( + name + in { + "pyexpat.errors", + "pyexpat.model", + "xml.parsers.expat.errors", + "xml.parsers.expat.model", + } + and module is getattr(sys.modules.get("pyexpat"), name.rsplit(".", 1)[1], None) + and getattr(module, "__spec__", None) is None + and getattr(module, "__file__", None) is None + ) + + +def standard_library_origins(): + """Check interpreter-owned stdlib dependencies before implementation capture.""" + directory = Path(sys._stdlib_dir) + for name, module in tuple(sys.modules.items()): + if name.split(".", 1)[0] in sys.stdlib_module_names: + source = getattr(getattr(module, "__spec__", None), "origin", None) + require( + source in {"built-in", "frozen"} + or (isinstance(source, str) and Path(source).is_relative_to(directory)) + or extension_child_origin(name, module), + "standard library module origin differs", + ) + + +def inventory_execution(tests, capture, authored): + """Retain private executable baselines before ordinary module/class fixtures.""" + check = require + before = {test: capture(test) for test in tests} + + def verify(test): + check( + test in before and authored(test) and capture(test) == before[test], + "inventoried testcase executable changed", + ) + + return verify + + +def runtime_guard(modules): + """Capture callable implementation identities before selected code imports.""" + snapshot, check = implementation_identity, require + before = [(module, snapshot(module)) for module in modules] + + def verify(): + for module, expected in before: + check(sys.modules.get(module.__name__) is module, "producer module changed") + check( + snapshot(module) == expected, + "producer callback or writer implementation changed", + ) + + return verify diff --git a/scripts/verify-package-contract.sh b/scripts/verify-package-contract.sh @@ -9,29 +9,6 @@ uv run --project "$python_project" --offline --frozen \ sh "$repo_root/scripts/ffi-provenance.sh" contract-check -( - cd "$repo_root" - uv run --project "$python_project" --offline --frozen \ - python -m unittest \ - scripts/test_app_source.py \ - scripts/test_release_evidence.py \ - scripts/test_package_contract.py \ - scripts/test_package_privacy.py \ - scripts/test_legacy_identifiers.py \ - scripts/test_app_dependency_graph.py \ - scripts/test_ffi_provenance.py \ - scripts/test_ffi_native.py \ - scripts/test_unittest_report.py \ - scripts/test_ffi_artifacts.py \ - scripts/test_ffi_installed.py \ - scripts/test_kotlin_smoke.py \ - scripts/test_xcode_selection.py \ - scripts/test_local_social_fixture.py \ - scripts/test_persona_attachment_selection.py \ - scripts/test_local_social_relay.py \ - scripts/test_legacy_upgrade_fixture.py -) - sh "$repo_root/scripts/persona-verifier.sh" verify-bud11-corpus \ --corpus "$repo_root/test-fixtures/bud11-upload-authorization-mutations.v1.json" \ --schema "$repo_root/test-fixtures/bud11-upload-authorization-mutations.v1.schema.json" diff --git a/test-fixtures/legacy-identifiers.v1.json b/test-fixtures/legacy-identifiers.v1.json @@ -59,6 +59,20 @@ }, "entries": [ { + "identifier": "radroots.tera.fixture-test-policy.v1", + "category": "verification_compatibility", + "occurrences": [ + { + "path": "scripts/fixture_test_policy.py", + "count": 1 + }, + { + "path": "scripts/fixture_test_policy.v1.json", + "count": 1 + } + ] + }, + { "identifier": "RADROOTS", "category": "public_namespace", "occurrences": [