field_ios

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

test_ffi_native.py (33785B)


      1 """Native input guards and real filesystem/subprocess controls; no artifact claims."""
      2 
      3 from __future__ import annotations
      4 
      5 import copy
      6 import json
      7 import itertools
      8 import os
      9 import shutil
     10 import subprocess
     11 import sys
     12 import tempfile
     13 import unittest
     14 from pathlib import Path
     15 from unittest.mock import patch
     16 
     17 SCRIPTS = Path(__file__).resolve().parent
     18 if str(SCRIPTS) not in sys.path:
     19     sys.path.insert(0, str(SCRIPTS))
     20 
     21 import ffi_build as builder  # noqa: E402
     22 import ffi_native as native  # noqa: E402
     23 import ffi_source as source  # noqa: E402
     24 from fixture_alias_tool import compile_alias_dispatch  # noqa: E402
     25 
     26 
     27 # Literal cc 1.4.0 cargo_env_var_os inputs, independent of the production list.
     28 CARGO_TRIM_OVERRIDES = (
     29     {"CARGO_TRIM_PATHS_SCOPE": ""},
     30     {"CARGO_TRIM_PATHS_SCOPE": "macro"},
     31     {"CARGO_TRIM_PATHS_REMAP": ""},
     32     {"CARGO_TRIM_PATHS_REMAP": "native.c=C137_REMAPPED"},
     33     {"CARGO_TRIM_PATHS_SCOPE": "", "CARGO_TRIM_PATHS_REMAP": ""},
     34     {"CARGO_TRIM_PATHS_SCOPE": "", "CARGO_TRIM_PATHS_REMAP": "native.c=C137_REMAPPED"},
     35     {"CARGO_TRIM_PATHS_SCOPE": "all", "CARGO_TRIM_PATHS_REMAP": ""},
     36     {
     37         "CARGO_TRIM_PATHS_SCOPE": "all",
     38         "CARGO_TRIM_PATHS_REMAP": "native.c=C137_REMAPPED",
     39     },
     40 )
     41 
     42 
     43 class NativePublicPathTests(unittest.TestCase):
     44     """Actual private aliases with the unchanged production public roots."""
     45 
     46     def setUp(self) -> None:
     47         temporary = tempfile.TemporaryDirectory(prefix="C137_PRIVATE_PATH_SENTINEL-")
     48         self.addCleanup(temporary.cleanup)
     49         self.alias = Path(temporary.name).resolve() / "clang"
     50         self.alias.symlink_to("/usr/bin/clang")
     51         self.assertFalse(any(self.alias.is_relative_to(p) for p in native.PUBLIC_ROOTS))
     52         self.assertTrue(
     53             any(self.alias.resolve().is_relative_to(p) for p in native.PUBLIC_ROOTS)
     54         )
     55 
     56     def assert_private_rejection(self, path: Path) -> None:
     57         for helper in (native.public_path, native.public_tool, native.tool_identity):
     58             with self.subTest(helper=helper.__name__):
     59                 with self.assertRaises(source.ProvenanceError) as failure:
     60                     helper(path if helper is native.tool_identity else str(path))
     61                 self.assertNotIn(str(path), str(failure.exception))
     62                 self.assertNotIn("C137_PRIVATE_PATH_SENTINEL", str(failure.exception))
     63 
     64     def test_real_private_alias_to_public_tool_rejects_without_private_diagnostic(
     65         self,
     66     ) -> None:
     67         self.assert_private_rejection(self.alias)
     68 
     69     def test_public_prefix_traversal_to_private_alias_rejects_without_diagnostic(
     70         self,
     71     ) -> None:
     72         path = Path("/usr/..") / self.alias.relative_to("/")
     73         self.assertTrue(path.is_relative_to(Path("/usr")))
     74         normalized = Path(os.path.normpath(path))
     75         self.assertFalse(any(normalized.is_relative_to(p) for p in native.PUBLIC_ROOTS))
     76         self.assert_private_rejection(path)
     77 
     78     def test_traversal_rejects_even_when_resolved_tool_is_public(self) -> None:
     79         self.assert_private_rejection(Path("/usr/bin/../bin/clang"))
     80 
     81     def test_real_public_tools_keep_selected_path_and_public_resolved_identity(
     82         self,
     83     ) -> None:
     84         for name in ("clang", "ranlib"):
     85             with self.subTest(tool=name):
     86                 path = Path("/usr/bin") / name
     87                 self.assertEqual(native.public_tool(str(path)), path)
     88                 identity = native.tool_identity(path)
     89                 self.assertEqual(identity["path"], str(path))
     90                 self.assertEqual(identity["resolved_path"], str(path.resolve()))
     91                 self.assertTrue(
     92                     any(
     93                         Path(identity["resolved_path"]).is_relative_to(p)
     94                         for p in native.PUBLIC_ROOTS
     95                     )
     96                 )
     97 
     98 
     99 class NativeInputTests(unittest.TestCase):
    100     def test_literal_cargo_trim_inputs_reject_source_policy(self) -> None:
    101         for overrides in CARGO_TRIM_OVERRIDES:
    102             with (
    103                 self.subTest(overrides=overrides),
    104                 patch.dict(os.environ, overrides, clear=True),
    105             ):
    106                 with self.assertRaisesRegex(
    107                     source.ProvenanceError, "^ungoverned inherited native build input$"
    108                 ):
    109                     source.reject_build_overrides()
    110 
    111     def test_literal_cargo_trim_inputs_reject_inspection_environment(self) -> None:
    112         for overrides in CARGO_TRIM_OVERRIDES:
    113             with (
    114                 self.subTest(overrides=overrides),
    115                 patch.dict(os.environ, overrides, clear=True),
    116             ):
    117                 with self.assertRaisesRegex(
    118                     source.ProvenanceError, "^ungoverned inherited native build input$"
    119                 ):
    120                     native.inspection_environment()
    121 
    122     def test_literal_cargo_trim_inputs_reject_build_before_config_read(self) -> None:
    123         config = source.producer_contract(SCRIPTS.parent)
    124         for overrides in CARGO_TRIM_OVERRIDES:
    125             with (
    126                 self.subTest(overrides=overrides),
    127                 patch.dict(os.environ, overrides, clear=True),
    128                 patch.object(
    129                     builder.contract, "_read_toml", return_value={"revision": "a" * 40}
    130                 ) as read,
    131             ):
    132                 with self.assertRaisesRegex(
    133                     source.ProvenanceError, "^ungoverned inherited native build input$"
    134                 ):
    135                     builder.build_environment(SCRIPTS.parent, Path("/external"), config)
    136                 read.assert_not_called()
    137 
    138     def test_cc_cargo_trim_names_do_not_invent_host_or_target_variants(self) -> None:
    139         for name in ("CARGO_TRIM_PATHS_SCOPE", "CARGO_TRIM_PATHS_REMAP"):
    140             for variant in (
    141                 "HOST_" + name,
    142                 "TARGET_" + name,
    143                 name + "_aarch64-apple-ios",
    144                 name + "_aarch64_apple_ios",
    145             ):
    146                 with self.subTest(variant=variant):
    147                     native.reject_inherited({variant: "inactive cc input"})
    148 
    149     def test_every_supported_input_variant_rejects_empty_and_nonempty(self) -> None:
    150         for name in sorted(native.inherited_names()):
    151             for value in ("", "unrecorded"):
    152                 with self.subTest(name=name, empty=not value):
    153                     with patch.dict(os.environ, {name: value}, clear=True):
    154                         with self.assertRaisesRegex(
    155                             source.ProvenanceError, "ungoverned"
    156                         ):
    157                             source.reject_build_overrides()
    158 
    159     def test_tool_precedence_and_additive_flag_inputs_are_never_flattened(self) -> None:
    160         for target in native.TARGET_SDKS:
    161             for base in native.TOOL_INPUTS:
    162                 names = (
    163                     base,
    164                     "HOST_" + base,
    165                     "TARGET_" + base,
    166                     base + "_" + target.replace("-", "_"),
    167                     base + "_" + target,
    168                 )
    169                 for winner in names:
    170                     environment = {name: "lower" for name in names}
    171                     environment[winner] = ""
    172                     with self.subTest(target=target, base=base, winner=winner):
    173                         with self.assertRaises(source.ProvenanceError):
    174                             native.reject_inherited(environment)
    175 
    176     def test_inactive_cpp_and_other_target_controls_do_not_invent_inputs(self) -> None:
    177         environment = {
    178             "CXX": "inactive",
    179             "CPPFLAGS": "inactive",
    180             "CC_wasm32_unknown_unknown": "inactive",
    181         }
    182         native.reject_inherited(environment)
    183 
    184     def test_ring_pregeneration_is_rejected_for_every_presence_sensitive_value(
    185         self,
    186     ) -> None:
    187         for value in ("", "0", "1", "malformed"):
    188             with self.subTest(value=value), self.assertRaises(source.ProvenanceError):
    189                 native.reject_inherited({"RING_PREGENERATE_ASM": value})
    190 
    191     def test_uv_fallback_is_bound_by_digest_and_other_loader_controls_reject(
    192         self,
    193     ) -> None:
    194         base = {"PATH": "/public/tools"}
    195         first = native.inherited_identity(base)
    196         second = native.inherited_identity(
    197             {**base, "DYLD_FALLBACK_LIBRARY_PATH": "sentinel"}
    198         )
    199         self.assertNotEqual(first, second)
    200         self.assertNotIn("sentinel", json.dumps(second))
    201         for key in (
    202             "DYLD_LIBRARY_PATH",
    203             "DYLD_INSERT_LIBRARIES",
    204             "DYLD_FRAMEWORK_PATH",
    205         ):
    206             with self.subTest(key=key), self.assertRaises(source.ProvenanceError):
    207                 native.inherited_identity({**base, key: ""})
    208 
    209     def test_missing_or_unbounded_path_is_rejected(self) -> None:
    210         for value in ("", "x" * (source.MAX_BYTES + 1)):
    211             with (
    212                 self.subTest(empty=not value),
    213                 self.assertRaises(source.ProvenanceError),
    214             ):
    215                 native.inherited_identity({"PATH": value})
    216 
    217     def test_extbuild_allowed_generator_debug_policy_is_preserved(self) -> None:
    218         with patch.dict(
    219             os.environ,
    220             {
    221                 "EXT_BUILD_RUN_ACTIVE": "1",
    222                 "CARGO_PROFILE_DEV_DEBUG": "line-tables-only",
    223             },
    224             clear=True,
    225         ):
    226             source.reject_build_overrides()
    227             self.assertEqual(
    228                 source.allowed_profile_overrides(),
    229                 {"CARGO_PROFILE_DEV_DEBUG": "line-tables-only"},
    230             )
    231 
    232 
    233 class NativeBindingTests(unittest.TestCase):
    234     def setUp(self) -> None:
    235         self.alias_compiler = shutil.which("cc")
    236         self.alias_compiler_environment = dict(os.environ)
    237         temporary = tempfile.TemporaryDirectory()
    238         self.addCleanup(temporary.cleanup)
    239         self.root = Path(temporary.name).resolve()
    240         self.tools = self.root / "bin"
    241         self.tools.mkdir()
    242         for name in (
    243             "xcrun",
    244             "clang",
    245             "ld",
    246             "ar",
    247             "libtool",
    248             "swift-frontend",
    249             "xcodebuild",
    250             "swiftformat",
    251             "swift-format",
    252             "swift",
    253         ):
    254             tool = self.tools / name
    255             tool.write_text("synthetic tool identity: " + name)
    256             tool.chmod(0o755)
    257         for alias, target in (
    258             ("ranlib", "libtool"),
    259             ("swiftc", "swift-frontend"),
    260             ("swift-symbolgraph-extract", "swift-frontend"),
    261         ):
    262             (self.tools / alias).symlink_to(target)
    263         self.sdks = {}
    264         self.resources = self.root / "resources"
    265         self.resources.mkdir()
    266         (self.resources / "stddef.h").write_text("/* clang resources */")
    267         for name in native.TARGET_SDKS.values():
    268             sdk = self.root / name
    269             sdk.mkdir()
    270             (sdk / "SDKSettings.json").write_text('{"Version":"26.5"}')
    271             (sdk / "header.h").write_text("/* governed SDK */")
    272             self.sdks[name] = sdk
    273         self.environment = {
    274             "PATH": str(self.tools),
    275             "EXT_BUILD_RUN_ACTIVE": "1",
    276             "CARGO_PROFILE_DEV_DEBUG": "line-tables-only",
    277             "DYLD_FALLBACK_LIBRARY_PATH": "private-sentinel",
    278         }
    279         self.addCleanup(patch.stopall)
    280         patch.dict(os.environ, self.environment, clear=True).start()
    281         patch.object(native, "PUBLIC_ROOTS", (self.root,)).start()
    282         patch.object(source, "command", side_effect=self.query).start()
    283 
    284     def query(self, root: Path, argv: list[str], **kwargs) -> bytes:
    285         self.assertEqual(root, self.root)
    286         self.assertNotIn("DYLD_FALLBACK_LIBRARY_PATH", kwargs["environment"])
    287         if argv[0] == "/usr/bin/xcode-select":
    288             return str(self.root).encode()
    289         if "-print-resource-dir" in argv:
    290             return str(self.resources).encode()
    291         sdk = argv[argv.index("--sdk") + 1]
    292         if "--find" in argv:
    293             return str(self.tools / argv[-1]).encode()
    294         if "--show-sdk-path" in argv:
    295             return str(self.sdks[sdk]).encode()
    296         if "--show-sdk-version" in argv:
    297             return b"26.5"
    298         if "--show-sdk-build-version" in argv:
    299             return b"test-sdk-build"
    300         self.fail("unexpected synthetic query")
    301 
    302     def capture(self, target: str) -> dict:
    303         return native.capture(self.root, target)
    304 
    305     def test_public_fixture_aliases_retain_actual_argv0_dispatch(self) -> None:
    306         # These existing synthetic tools use explicitly substituted fixture roots.
    307         binary, command = compile_alias_dispatch(
    308             self.root, self.alias_compiler, self.alias_compiler_environment
    309         )
    310         self.assertTrue(command["wait_reaped"])
    311         self.assertTrue(command["group_absent"])
    312         for name in ("libtool", "swift-frontend"):
    313             shutil.copyfile(binary, self.tools / name)
    314             (self.tools / name).chmod(0o755)
    315         for name in ("ranlib", "swiftc", "swift-symbolgraph-extract"):
    316             with self.subTest(tool=name):
    317                 alias = self.tools / name
    318                 selected = native.public_tool(str(alias))
    319                 self.assertEqual(selected, alias)
    320                 self.assertNotEqual(selected, alias.resolve())
    321                 identity = native.tool_identity(selected)
    322                 self.assertEqual(identity["path"], str(alias))
    323                 self.assertEqual(identity["resolved_path"], str(alias.resolve()))
    324                 result = subprocess.run(
    325                     [str(selected)],
    326                     check=True,
    327                     capture_output=True,
    328                     text=True,
    329                     timeout=30,
    330                     env=native.inspection_environment(),
    331                 )
    332                 self.assertEqual(result.stdout.strip(), name)
    333 
    334     def test_native_alias_dispatch_does_not_depend_on_python_loader(self) -> None:
    335         with patch.object(
    336             sys, "executable", str(self.root / "unavailable-python-loader")
    337         ):
    338             self.test_public_fixture_aliases_retain_actual_argv0_dispatch()
    339 
    340     def test_literal_cargo_trim_capture_rejects_before_tool_queries(self) -> None:
    341         for overrides in CARGO_TRIM_OVERRIDES:
    342             with self.subTest(overrides=overrides), patch.dict(os.environ, overrides):
    343                 source.command.reset_mock()
    344                 with self.assertRaisesRegex(
    345                     source.ProvenanceError, "^ungoverned inherited native build input$"
    346                 ):
    347                     self.capture(native.HOST)
    348                 source.command.assert_not_called()
    349 
    350     def test_literal_cargo_trim_introduction_rejects_after_initial_capture(
    351         self,
    352     ) -> None:
    353         context = self.capture(native.HOST)
    354         for overrides in CARGO_TRIM_OVERRIDES:
    355             with self.subTest(overrides=overrides), patch.dict(os.environ, overrides):
    356                 source.command.reset_mock()
    357                 with self.assertRaisesRegex(
    358                     source.ProvenanceError, "^ungoverned inherited native build input$"
    359                 ):
    360                     native.verify(self.root, context)
    361                 source.command.assert_not_called()
    362 
    363     def test_literal_cargo_trim_in_execution_dictionary_rejects_with_clean_ambient(
    364         self,
    365     ) -> None:
    366         context = self.capture("aarch64-apple-ios")
    367         host = self.capture(native.HOST)
    368         for overrides in CARGO_TRIM_OVERRIDES:
    369             with self.subTest(overrides=overrides):
    370                 self.assertNotIn("CARGO_TRIM_PATHS_SCOPE", os.environ)
    371                 self.assertNotIn("CARGO_TRIM_PATHS_REMAP", os.environ)
    372                 environment = {
    373                     **self.environment,
    374                     **overrides,
    375                     "CARGO_ENCODED_RUSTFLAGS": "--remap-path-prefix=/fixture=/tera",
    376                     "IPHONEOS_DEPLOYMENT_TARGET": "18.0",
    377                 }
    378                 source.command.reset_mock()
    379                 with self.assertRaisesRegex(
    380                     source.ProvenanceError, "^ungoverned inherited native build input$"
    381                 ):
    382                     native.execution_environment(self.root, environment, context, host)
    383                 source.command.assert_not_called()
    384 
    385     def test_clean_owned_build_environment_retains_governed_flags_and_deployment(
    386         self,
    387     ) -> None:
    388         config = source.producer_contract(SCRIPTS.parent)
    389         environment = builder.build_environment(SCRIPTS.parent, self.root, config)
    390         host = self.capture(native.HOST)
    391         for target in native.TARGET_SDKS:
    392             with self.subTest(target=target):
    393                 context = self.capture(target)
    394                 bound = native.execution_environment(
    395                     self.root, environment, context, host
    396                 )
    397                 self.assertTrue(
    398                     bound["CARGO_ENCODED_RUSTFLAGS"].startswith(
    399                         environment["CARGO_ENCODED_RUSTFLAGS"] + "\x1f"
    400                     )
    401                 )
    402                 self.assertEqual(
    403                     bound["SOURCE_DATE_EPOCH"], environment["SOURCE_DATE_EPOCH"]
    404                 )
    405                 self.assertEqual(bound["CARGO_PROFILE_DEV_DEBUG"], "line-tables-only")
    406                 self.assertNotIn("DYLD_FALLBACK_LIBRARY_PATH", bound)
    407                 self.assertNotIn("MACOSX_DEPLOYMENT_TARGET", bound)
    408                 if target == native.HOST:
    409                     self.assertNotIn("IPHONEOS_DEPLOYMENT_TARGET", bound)
    410                 else:
    411                     self.assertEqual(bound["IPHONEOS_DEPLOYMENT_TARGET"], "18.0")
    412 
    413     def test_all_targets_and_independent_generator_bind_real_alias_and_sdk_bytes(
    414         self,
    415     ) -> None:
    416         host = self.capture(native.HOST)
    417         for target, sdk in native.TARGET_SDKS.items():
    418             with self.subTest(target=target):
    419                 context = self.capture(target)
    420                 self.assertEqual(context, self.capture(target))
    421                 self.assertEqual(context["sdk"]["path"], str(self.sdks[sdk]))
    422                 self.assertEqual(
    423                     context["tools"]["ranlib"]["path"], str(self.tools / "ranlib")
    424                 )
    425                 self.assertEqual(
    426                     context["tools"]["ranlib"]["resolved_path"],
    427                     str(self.tools / "libtool"),
    428                 )
    429                 self.assertEqual(
    430                     context["tools"]["swiftc"]["path"], str(self.tools / "swiftc")
    431                 )
    432                 bound = native.execution_environment(
    433                     self.root, self.environment, context, host
    434                 )
    435                 self.assertEqual(bound["SDKROOT"], str(self.sdks[sdk]))
    436                 self.assertEqual(bound["PATH"], self.environment["PATH"])
    437                 self.assertEqual(bound["CARGO_PROFILE_DEV_DEBUG"], "line-tables-only")
    438                 self.assertNotIn("DYLD_FALLBACK_LIBRARY_PATH", bound)
    439                 self.assertNotIn("MACOSX_DEPLOYMENT_TARGET", bound)
    440                 for selected in (target, native.HOST):
    441                     self.assertEqual(bound["CC_" + selected], str(self.tools / "clang"))
    442                     self.assertEqual(bound["AR_" + selected], str(self.tools / "ar"))
    443                     self.assertEqual(
    444                         bound["RANLIB_" + selected], str(self.tools / "ranlib")
    445                     )
    446                 if target == native.HOST:
    447                     self.assertNotIn("IPHONEOS_DEPLOYMENT_TARGET", bound)
    448                 else:
    449                     self.assertEqual(bound["IPHONEOS_DEPLOYMENT_TARGET"], "18.0")
    450 
    451     def test_changed_tools_sdk_and_selection_are_fenced_before_execution(self) -> None:
    452         context = self.capture("aarch64-apple-ios")
    453         for path in (
    454             self.tools / "clang",
    455             self.tools / "ld",
    456             self.tools / "ar",
    457             self.tools / "libtool",
    458             self.tools / "swift-frontend",
    459             self.tools / "xcodebuild",
    460             self.tools / "swiftformat",
    461             self.sdks["iphoneos"] / "header.h",
    462             self.resources / "stddef.h",
    463         ):
    464             original = path.read_bytes()
    465             path.write_bytes(original + b"changed")
    466             with (
    467                 self.subTest(tool=path.name),
    468                 self.assertRaisesRegex(source.ProvenanceError, "drift"),
    469             ):
    470                 native.verify(self.root, context)
    471             path.write_bytes(original)
    472         changed = copy.deepcopy(context)
    473         changed["sdk"]["path"] = str(self.sdks["macosx"])
    474         with self.assertRaisesRegex(source.ProvenanceError, "drift"):
    475             native.verify(self.root, changed)
    476 
    477     def test_environment_presence_and_path_changes_are_fenced(self) -> None:
    478         context = self.capture(native.HOST)
    479         for overrides in (
    480             {"PATH": str(self.tools) + ":/different"},
    481             {"DYLD_FALLBACK_LIBRARY_PATH": "changed"},
    482             {"LIBSQLITE3_SYS_USE_PKG_CONFIG": ""},
    483             {"DEVELOPER_DIR": ""},
    484             {"CARGO_PROFILE_DEV_DEBUG": "different"},
    485         ):
    486             with (
    487                 self.subTest(keys=sorted(overrides)),
    488                 patch.dict(os.environ, overrides),
    489             ):
    490                 with self.assertRaises(source.ProvenanceError):
    491                     native.verify(self.root, context)
    492 
    493     def test_native_stage_executes_captured_environment_in_a_real_child(self) -> None:
    494         host = self.capture(native.HOST)
    495         for target in (*native.TARGET_SDKS, native.HOST):
    496             context = self.capture(target)
    497             log = builder.run_native(
    498                 self.root,
    499                 self.root,
    500                 "child-" + target,
    501                 [
    502                     sys.executable,
    503                     "-c",
    504                     "import json,os; print(json.dumps(dict(os.environ)))",
    505                 ],
    506                 self.environment,
    507                 context,
    508                 host,
    509             )
    510             observed = json.loads(log.read_text())
    511             self.assertEqual(observed["SDKROOT"], context["sdk"]["path"])
    512             self.assertEqual(
    513                 observed["CC_" + target], context["tools"]["compiler"]["path"]
    514             )
    515             self.assertEqual(
    516                 observed[
    517                     "CARGO_TARGET_" + target.upper().replace("-", "_") + "_LINKER"
    518                 ],
    519                 context["tools"]["compiler"]["path"],
    520             )
    521             self.assertIn(
    522                 context["tools"]["linker"]["path"], observed["CARGO_ENCODED_RUSTFLAGS"]
    523             )
    524 
    525     def test_target_and_host_sdk_changes_bind_both_c_and_rust_cache_inputs(
    526         self,
    527     ) -> None:
    528         target = self.capture("aarch64-apple-ios")
    529         host = self.capture(native.HOST)
    530         original = native.execution_environment(
    531             self.root, self.environment, target, host
    532         )
    533         repeated = native.execution_environment(
    534             self.root, self.environment, target, host
    535         )
    536         self.assertEqual(original, repeated)
    537         for sdk, selected in (("iphoneos", "target"), ("macosx", "host")):
    538             with self.subTest(selected=selected):
    539                 (self.sdks[sdk] / "header.h").write_text("changed SDK context " + sdk)
    540                 target = self.capture("aarch64-apple-ios")
    541                 host = self.capture(native.HOST)
    542                 changed = native.execution_environment(
    543                     self.root, self.environment, target, host
    544                 )
    545                 for key in (
    546                     "CFLAGS_aarch64-apple-ios",
    547                     "CFLAGS_aarch64-apple-darwin",
    548                     "CARGO_ENCODED_RUSTFLAGS",
    549                 ):
    550                     self.assertNotEqual(changed[key], original[key])
    551                 self.assertIn(
    552                     "--no-default-config", changed["CFLAGS_aarch64-apple-ios"]
    553                 )
    554                 self.assertEqual(changed["PATH"], original["PATH"])
    555                 original = changed
    556 
    557     def test_postbuild_sdk_drift_rejects_real_child_result(self) -> None:
    558         context = self.capture(native.HOST)
    559         path = self.sdks["macosx"] / "header.h"
    560         code = (
    561             "from pathlib import Path; Path("
    562             + repr(str(path))
    563             + ").write_text('changed')"
    564         )
    565         with self.assertRaisesRegex(source.ProvenanceError, "drift"):
    566             builder.run_native(
    567                 self.root,
    568                 self.root,
    569                 "postbuild-drift",
    570                 [sys.executable, "-c", code],
    571                 self.environment,
    572                 context,
    573                 context,
    574             )
    575         self.assertTrue((self.root / "postbuild-drift.txt").is_file())
    576 
    577     def test_bad_tool_path_or_permissions_reject_without_echoing_path(self) -> None:
    578         with self.assertRaisesRegex(source.ProvenanceError, "not absolute"):
    579             native.public_path("private-sentinel")
    580         with self.assertRaisesRegex(source.ProvenanceError, "not public"):
    581             native.public_path("/bin/sh")
    582         path = self.tools / "ar"
    583         path.chmod(0o644)
    584         with self.assertRaisesRegex(source.ProvenanceError, "not executable"):
    585             native.tool_identity(path)
    586 
    587     def test_sdk_symlink_escape_and_nonregular_file_reject(self) -> None:
    588         sdk = self.sdks["iphoneos"]
    589         (sdk / "link").symlink_to(self.tools / "clang")
    590         with self.assertRaisesRegex(source.ProvenanceError, "escapes"):
    591             native.sdk_identity(sdk)
    592         (sdk / "link").unlink()
    593         os.mkfifo(sdk / "fifo")
    594         with self.assertRaisesRegex(source.ProvenanceError, "regular"):
    595             native.sdk_identity(sdk)
    596 
    597     def test_sdk_node_and_aggregate_byte_limits_exact_and_one_over(self) -> None:
    598         sdk = self.sdks["iphoneos"]
    599         size = sum(path.stat().st_size for path in sdk.iterdir())
    600         with (
    601             patch.object(native, "MAX_NATIVE_NODES", 2),
    602             patch.object(native, "MAX_NATIVE_BYTES", size),
    603         ):
    604             self.assertEqual(native.sdk_identity(sdk)["nodes"], 2)
    605             self.assertEqual(native.sdk_identity(sdk)["bytes"], size)
    606         with patch.object(native, "MAX_NATIVE_NODES", 1):
    607             with self.assertRaisesRegex(source.ProvenanceError, "inspection bound"):
    608                 native.sdk_identity(sdk)
    609         with patch.object(native, "MAX_NATIVE_BYTES", size - 1):
    610             with self.assertRaisesRegex(source.ProvenanceError, "byte bound"):
    611                 native.sdk_identity(sdk)
    612 
    613     def test_sdk_inspection_deadline_is_bounded(self) -> None:
    614         with patch.object(
    615             native.time,
    616             "monotonic",
    617             side_effect=itertools.chain([0], itertools.repeat(120)),
    618         ):
    619             native.sdk_identity(self.sdks["iphoneos"])
    620         with patch.object(native.time, "monotonic", side_effect=[0, 121]):
    621             with self.assertRaisesRegex(source.ProvenanceError, "inspection bound"):
    622                 native.sdk_identity(self.sdks["iphoneos"])
    623 
    624     def test_long_stream_is_fenced_within_chunks(self) -> None:
    625         path = self.sdks["iphoneos"] / "header.h"
    626         with patch.object(native.time, "monotonic", side_effect=[0, 0, 121]):
    627             with self.assertRaisesRegex(source.ProvenanceError, "inspection bound"):
    628                 native.file_identity(path)
    629 
    630     def test_stream_detects_content_mutation_at_descriptor_settlement(self) -> None:
    631         path = self.sdks["iphoneos"] / "header.h"
    632         stamp = native.file_stamp
    633 
    634         def changed_stamp(selected):
    635             selected.write_bytes(b"changed while reading")
    636             return stamp(selected)
    637 
    638         with patch.object(native, "file_stamp", side_effect=changed_stamp):
    639             with self.assertRaisesRegex(
    640                 source.ProvenanceError, "changed while reading"
    641             ):
    642                 native.file_identity(path)
    643 
    644     def test_failed_sdk_walk_rejects_incomplete_inventory(self) -> None:
    645         with patch.object(
    646             native.os, "scandir", side_effect=PermissionError("private-sentinel")
    647         ):
    648             with self.assertRaisesRegex(source.ProvenanceError, "enumeration failed"):
    649                 native.sdk_identity(self.sdks["iphoneos"])
    650 
    651     def observed_sdk_directory(self) -> tuple[Path, dict, object]:
    652         directory = self.root / "wide-sdk"
    653         directory.mkdir()
    654         for index in range(32):
    655             (directory / f"member{index}").write_bytes(b"synthetic SDK byte")
    656         observed = {"entries": 0}
    657         scanner = native.os.scandir
    658 
    659         class CountingScandir:
    660             def __init__(self, selected):
    661                 self.iterator = scanner(selected)
    662 
    663             def __iter__(self):
    664                 return self
    665 
    666             def __next__(self):
    667                 entry = next(self.iterator)
    668                 observed["entries"] += 1
    669                 return entry
    670 
    671             def __enter__(self):
    672                 return self
    673 
    674             def __exit__(self, *args):
    675                 self.iterator.close()
    676 
    677         return directory, observed, CountingScandir
    678 
    679     def test_sdk_wide_directory_rejects_before_buffering_beyond_node_cap(self) -> None:
    680         directory, observed, scanner = self.observed_sdk_directory()
    681         with (
    682             patch.object(native, "MAX_NATIVE_NODES", 2),
    683             patch.object(native.os, "scandir", scanner),
    684         ):
    685             with self.assertRaisesRegex(source.ProvenanceError, "inspection bound"):
    686                 native.sdk_inventory(directory, float("inf"))
    687         self.assertLessEqual(observed["entries"], 3)
    688 
    689     def test_sdk_deadline_is_checked_during_real_directory_enumeration(self) -> None:
    690         directory, observed, scanner = self.observed_sdk_directory()
    691         with (
    692             patch.object(native.os, "scandir", scanner),
    693             patch.object(
    694                 native.time, "monotonic", side_effect=lambda: observed["entries"]
    695             ),
    696         ):
    697             with self.assertRaisesRegex(source.ProvenanceError, "inspection bound"):
    698                 native.sdk_inventory(directory, 2)
    699         self.assertLessEqual(observed["entries"], 3)
    700 
    701     def test_inventoried_directory_replaced_by_foreign_symlink_is_not_traversed(
    702         self,
    703     ) -> None:
    704         sdk = self.sdks["iphoneos"]
    705         directory = sdk / "child"
    706         directory.mkdir()
    707         (directory / "member").write_text("SDK member")
    708         foreign = self.root / "foreign"
    709         foreign.mkdir()
    710         (foreign / "sentinel").write_text("must not be enumerated")
    711         stamp = native.file_stamp
    712 
    713         def replace_directory(path):
    714             value = stamp(path)
    715             if path == directory and not path.is_symlink():
    716                 path.rename(self.root / "preserved-child")
    717                 path.symlink_to(foreign, target_is_directory=True)
    718             return value
    719 
    720         with patch.object(native, "file_stamp", side_effect=replace_directory):
    721             with self.assertRaises(source.ProvenanceError):
    722                 native.sdk_inventory(sdk, float("inf"))
    723 
    724     def test_tool_query_receives_only_remaining_aggregate_deadline(self) -> None:
    725         with patch.object(native.time, "monotonic", return_value=100):
    726             native.query(self.root, ["/usr/bin/xcode-select", "-p"], {}, 101)
    727         self.assertEqual(source.command.call_args.kwargs["timeout"], 1)
    728         with patch.object(native.time, "monotonic", return_value=102):
    729             with self.assertRaisesRegex(source.ProvenanceError, "inspection bound"):
    730                 native.query(self.root, ["/usr/bin/xcode-select", "-p"], {}, 101)
    731 
    732     def test_legitimate_internal_sdk_links_and_capture_inventory_drift(self) -> None:
    733         sdk = self.sdks["iphoneos"]
    734         (sdk / "alias.h").symlink_to("header.h")
    735         self.assertEqual(native.sdk_identity(sdk)["nodes"], 3)
    736         actual_entry = native.sdk_entry
    737 
    738         def changing_entry(root, path, deadline):
    739             value = actual_entry(root, path, deadline)
    740             if path.name == "header.h":
    741                 (root / "new.h").write_text("inventory drift")
    742             return value
    743 
    744         with patch.object(native, "sdk_entry", side_effect=changing_entry):
    745             with self.assertRaisesRegex(source.ProvenanceError, "inventory changed"):
    746                 native.sdk_identity(sdk)
    747 
    748     def test_sdk_root_and_member_replacement_during_capture_reject(self) -> None:
    749         sdk = self.sdks["iphoneos"]
    750         actual_entry = native.sdk_entry
    751 
    752         def replacing_entry(root, path, deadline):
    753             value = actual_entry(root, path, deadline)
    754             if path.name == "header.h":
    755                 path.unlink()
    756                 path.write_text("/* governed SDK */")
    757             return value
    758 
    759         with patch.object(native, "sdk_entry", side_effect=replacing_entry):
    760             with self.assertRaisesRegex(source.ProvenanceError, "inventory changed"):
    761                 native.sdk_identity(sdk)
    762 
    763     def test_sdk_root_replacement_during_capture_rejects_equal_bytes(self) -> None:
    764         sdk = self.sdks["iphoneos"]
    765         actual_entry = native.sdk_entry
    766 
    767         def replace_root(root, path, deadline):
    768             value = actual_entry(root, path, deadline)
    769             if path.name == "header.h":
    770                 root.rename(root.with_name("preserved-sdk"))
    771                 root.mkdir()
    772                 for original in root.with_name("preserved-sdk").iterdir():
    773                     (root / original.name).write_bytes(original.read_bytes())
    774             return value
    775 
    776         with patch.object(native, "sdk_entry", side_effect=replace_root):
    777             with self.assertRaisesRegex(source.ProvenanceError, "inventory changed"):
    778                 native.sdk_identity(sdk)
    779 
    780     def test_tool_nofollow_identity_rejects_symlink_input(self) -> None:
    781         with self.assertRaises(OSError):
    782             native.file_identity(self.tools / "ranlib")
    783 
    784 
    785 if __name__ == "__main__":
    786     unittest.main()