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_provenance.py (13806B)


      1 from __future__ import annotations
      2 
      3 import copy
      4 import os
      5 import subprocess
      6 import sys
      7 import tempfile
      8 import unittest
      9 from pathlib import Path
     10 from unittest.mock import patch
     11 
     12 SCRIPTS = Path(__file__).resolve().parent
     13 if str(SCRIPTS) not in sys.path:
     14     sys.path.insert(0, str(SCRIPTS))
     15 
     16 import ffi_build as builder  # noqa: E402
     17 import ffi_provenance as provenance  # noqa: E402
     18 import ffi_source as source  # noqa: E402
     19 import package_contract as contract  # noqa: E402
     20 
     21 
     22 class ProducerSourceTests(unittest.TestCase):
     23     def setUp(self) -> None:
     24         self.temporary = tempfile.TemporaryDirectory()
     25         self.addCleanup(self.temporary.cleanup)
     26         self.root = Path(self.temporary.name).resolve()
     27         self.git("init", "--quiet")
     28         (self.root / "core").mkdir()
     29         (self.root / "core/lib.rs").write_text("pub fn value() -> u8 { 1 }\n")
     30         (self.root / "Cargo.lock").write_text("# synthetic source-capture fixture\n")
     31         self.git("add", "core", "Cargo.lock")
     32         self.inputs = ["core", "Cargo.lock"]
     33 
     34     def git(self, *args: str) -> str:
     35         return subprocess.check_output(
     36             ["git", *args], cwd=self.root, text=True, stderr=subprocess.DEVNULL
     37         ).strip()
     38 
     39     def snapshot(self) -> dict:
     40         return source.source_snapshot(self.root, self.inputs)
     41 
     42     def test_producer_build_accepts_positive_epochs(self) -> None:
     43         build = source.producer_contract(SCRIPTS.parent)["build"]
     44         for epoch in (1787871027, 12345, 1):
     45             with self.subTest(epoch=epoch):
     46                 source.validate_build({**build, "source_date_epoch": epoch})
     47 
     48     def test_producer_build_rejects_absent_and_invalid_epoch(self) -> None:
     49         build = source.producer_contract(SCRIPTS.parent)["build"]
     50         absent = {
     51             key: value for key, value in build.items() if key != "source_date_epoch"
     52         }
     53         with self.assertRaises(contract.PackageContractError):
     54             source.validate_build(absent)
     55         for epoch in (None, "1787871027", 0, -1, True):
     56             with self.subTest(epoch=epoch):
     57                 with self.assertRaises(source.ProvenanceError):
     58                     source.validate_build({**build, "source_date_epoch": epoch})
     59 
     60     def test_tree_is_real_and_deterministic_without_an_introducing_commit(self) -> None:
     61         first = self.snapshot()
     62         self.assertEqual(first, self.snapshot())
     63         self.assertEqual(self.git("write-tree"), first["tree"])
     64         self.assertEqual(self.git("cat-file", "-t", first["tree"]), "tree")
     65         # Generated evidence never changes its own source tree identity.
     66         (self.root / "provenance.json").write_bytes(provenance.encoded(first))
     67         self.git("add", "provenance.json")
     68         self.assertEqual(first, self.snapshot())
     69 
     70     def test_worktree_change_requires_an_explicit_staged_transaction(self) -> None:
     71         first = self.snapshot()
     72         (self.root / "core/lib.rs").write_text("pub fn value() -> u8 { 2 }\n")
     73         with self.assertRaisesRegex(source.ProvenanceError, "differs from staged"):
     74             self.snapshot()
     75         self.git("add", "core/lib.rs")
     76         self.assertNotEqual(first["tree"], self.snapshot()["tree"])
     77 
     78     def test_intent_to_add_empty_and_nonempty_inputs_reject(self) -> None:
     79         path = self.root / "core/intent.rs"
     80         for content in (b"", b"// intent-to-add input\n"):
     81             with self.subTest(empty=not content):
     82                 path.write_bytes(content)
     83                 self.git("add", "--intent-to-add", "core/intent.rs")
     84                 with self.assertRaises(source.ProvenanceError):
     85                     self.snapshot()
     86                 self.git("update-index", "--force-remove", "core/intent.rs")
     87                 path.unlink()
     88 
     89     def test_coherent_staging_of_empty_and_nonempty_inputs_matches_git_tree(
     90         self,
     91     ) -> None:
     92         path = self.root / "core/intent.rs"
     93         for content in (b"", b"// staged input\n"):
     94             with self.subTest(empty=not content):
     95                 path.write_bytes(content)
     96                 self.git("add", "--intent-to-add", "core/intent.rs")
     97                 self.git("add", "core/intent.rs")
     98                 self.assertEqual(self.snapshot()["tree"], self.git("write-tree"))
     99 
    100     def test_untracked_and_missing_source_fail_closed(self) -> None:
    101         (self.root / "core/extra.rs").write_text("// untracked input\n")
    102         with self.assertRaisesRegex(source.ProvenanceError, "untracked"):
    103             self.snapshot()
    104         (self.root / "core/extra.rs").unlink()
    105         (self.root / "Cargo.lock").unlink()
    106         self.git("add", "-u", "Cargo.lock")
    107         with self.assertRaisesRegex(source.ProvenanceError, "missing from index"):
    108             self.snapshot()
    109 
    110     def test_symlink_and_file_mode_changes_are_rejected(self) -> None:
    111         path = self.root / "core/lib.rs"
    112         path.chmod(0o755)
    113         with self.assertRaisesRegex(source.ProvenanceError, "differs from staged"):
    114             self.snapshot()
    115         path.unlink()
    116         path.symlink_to("../Cargo.lock")
    117         with self.assertRaisesRegex(source.ProvenanceError, "symlink"):
    118             self.snapshot()
    119         self.git("add", "core/lib.rs")
    120         with self.assertRaisesRegex(source.ProvenanceError, "unsupported mode"):
    121             self.snapshot()
    122 
    123     def test_source_byte_bound_prevents_unbounded_capture(self) -> None:
    124         (self.root / "core/lib.rs").write_bytes(b"x" * (source.MAX_BYTES + 1))
    125         with self.assertRaisesRegex(contract.PackageContractError, "byte limit"):
    126             self.snapshot()
    127 
    128     def test_gitignore_cannot_hide_a_producer_input(self) -> None:
    129         (self.root / ".gitignore").write_text("core/hidden.rs\n")
    130         (self.root / "core/hidden.rs").write_text("// ignored source input\n")
    131         with self.assertRaisesRegex(source.ProvenanceError, "ignored source"):
    132             self.snapshot()
    133 
    134     def test_provenance_rejects_every_source_or_build_tuple_mutation(self) -> None:
    135         expected = {
    136             "source": self.snapshot(),
    137             "cargo_lock_sha256": "a" * 64,
    138             "foundation": {"revision": "b" * 40},
    139             "build": {
    140                 "target": "aarch64-apple-ios",
    141                 "rustc": "rustc 1.99.0",
    142                 "features": [],
    143                 "package_rust_flags": [],
    144                 "feature_graph": ["tera_core|mobile-social"],
    145             },
    146         }
    147         provenance.verify_record(provenance.encoded(expected), expected)
    148         for section, key, value in (
    149             ("source", "tree", "c" * 40),
    150             ("foundation", "revision", "d" * 40),
    151             ("build", "target", "aarch64-apple-ios-sim"),
    152             ("build", "rustc", "rustc 1.96.0"),
    153             ("build", "features", ["extra"]),
    154             ("build", "package_rust_flags", ["-Copt-level=0"]),
    155             ("build", "feature_graph", []),
    156         ):
    157             with self.subTest(section=section, key=key):
    158                 changed = copy.deepcopy(expected)
    159                 changed[section][key] = value
    160                 with self.assertRaisesRegex(
    161                     source.ProvenanceError, "exact source/build tuple"
    162                 ):
    163                     provenance.verify_record(provenance.encoded(changed), expected)
    164         changed = {**expected, "cargo_lock_sha256": "f" * 64}
    165         with self.assertRaises(source.ProvenanceError):
    166             provenance.verify_record(provenance.encoded(changed), expected)
    167 
    168     def test_output_rejects_repository_paths_escapes_and_symlinks(self) -> None:
    169         external = self.root / "external"
    170         external.mkdir()
    171         repository = self.root / "repository"
    172         repository.mkdir()
    173         with patch.dict(
    174             os.environ,
    175             {
    176                 "EXT_BUILD_RUN_ACTIVE": "1",
    177                 "EXT_BUILD_PROJECT_DIR": str(external),
    178             },
    179         ):
    180             for output in (repository / "evidence.json", external / "../escape.json"):
    181                 with self.assertRaises(source.ProvenanceError):
    182                     provenance.output_path(repository, "aarch64-apple-ios", str(output))
    183             (external / "link").symlink_to(repository, target_is_directory=True)
    184             with self.assertRaises(source.ProvenanceError):
    185                 provenance.output_path(
    186                     repository, "aarch64-apple-ios", str(external / "link/value.json")
    187                 )
    188 
    189     def test_ungoverned_build_flags_are_rejected(self) -> None:
    190         for key in (
    191             "RUSTFLAGS",
    192             "RUSTC_WRAPPER",
    193             "CARGO_BUILD_RUSTFLAGS",
    194             "CARGO_TARGET_AARCH64_APPLE_IOS_LINKER",
    195         ):
    196             with self.subTest(key=key), patch.dict(os.environ, {key: "synthetic"}):
    197                 with self.assertRaisesRegex(source.ProvenanceError, "ungoverned"):
    198                     source.reject_build_overrides()
    199 
    200     def test_inherited_native_inputs_are_rejected_before_capture(self) -> None:
    201         for key, value in (
    202             ("LIBSQLITE3_FLAGS", "-USQLITE_ENABLE_API_ARMOR"),
    203             ("LIBSQLITE3_SYS_USE_PKG_CONFIG", ""),
    204             ("CC_aarch64-apple-ios", "unrecorded-clang"),
    205             ("HOST_CFLAGS", "-DUNRECORDED_NATIVE_INPUT=1"),
    206             ("SDKROOT", "/unrecorded-sdk"),
    207         ):
    208             with self.subTest(key=key), patch.dict(os.environ, {key: value}):
    209                 with self.assertRaisesRegex(source.ProvenanceError, "ungoverned"):
    210                     source.reject_build_overrides()
    211 
    212     def test_local_cargo_build_configuration_cannot_escape_source_identity(
    213         self,
    214     ) -> None:
    215         (self.root / ".cargo").mkdir()
    216         config = self.root / ".cargo/config.toml"
    217         config.write_text('[build]\nrustflags = ["--cfg", "unrecorded"]\n')
    218         with self.assertRaisesRegex(source.ProvenanceError, "Cargo configuration"):
    219             source.reject_cargo_configuration(self.root)
    220 
    221     def test_current_contract_separates_foundation_and_application(self) -> None:
    222         config = source.producer_contract(SCRIPTS.parent)
    223         self.assertEqual(config["ffi"]["package"], "tera_ffi")
    224         self.assertEqual(config["repository"], "https://github.com/radrootslabs/tera")
    225         cargo = contract._read_toml(SCRIPTS.parent / "Cargo.toml")
    226         lock = contract._read_toml(SCRIPTS.parent / config["foundation_lock"])
    227         changed = copy.deepcopy(cargo)
    228         changed["workspace"]["dependencies"]["radroots_sdk"]["rev"] = "f" * 40
    229         with self.assertRaisesRegex(
    230             contract.PackageContractError, "foundation Cargo revision"
    231         ):
    232             source.validate_foundation(changed, lock)
    233 
    234     def test_mismatched_foundation_consumer_lock_fails_before_source_capture(
    235         self,
    236     ) -> None:
    237         read_toml = contract._read_toml
    238 
    239         def mismatched(path: Path) -> dict:
    240             value = read_toml(path)
    241             if path.name == "radroots.lib.source-lock.v1.toml":
    242                 value["lockfile_sha256"] = "0" * 64
    243             return value
    244 
    245         with patch.object(contract, "_read_toml", side_effect=mismatched):
    246             with self.assertRaisesRegex(
    247                 contract.PackageContractError, "foundation consumer Cargo lock digest"
    248             ):
    249                 source.producer_contract(SCRIPTS.parent)
    250 
    251     def test_host_install_name_is_fixed_and_scoped_to_the_ffi_library(self) -> None:
    252         config = source.producer_contract(SCRIPTS.parent)
    253         build = config["build"]
    254         flags = [
    255             "-Clink-arg=-Wl,-install_name,@rpath/libtera_ffi.dylib",
    256             "-Clink-arg=-Wl,-reproducible",
    257             "-Clink-arg=-Wl,-oso_prefix,{extbuild_root}",
    258         ]
    259         project = Path("/external/test-producer")
    260         for target in build["targets"]:
    261             with self.subTest(target=target):
    262                 command = builder.library_command(SCRIPTS.parent, target, project)
    263                 self.assertIn("--lib", command)
    264                 self.assertEqual(command[command.index("-p") + 1], "tera_ffi")
    265                 if target == build["host"]:
    266                     self.assertEqual(command[1], "rustc")
    267                     self.assertEqual(
    268                         command[-4:],
    269                         ["--", *(flag.format(extbuild_root=project) for flag in flags)],
    270                     )
    271                     self.assertEqual(source.library_rust_flags(build, target), flags)
    272                 else:
    273                     self.assertEqual(command[1], "build")
    274                     self.assertNotIn("--", command)
    275                     self.assertEqual(source.library_rust_flags(build, target), [])
    276         self.assertFalse(any("install_name" in flag for flag in build["rust_flags"]))
    277         for install_name in (
    278             "/tmp/libtera_ffi.dylib",
    279             "libtera_ffi.dylib",
    280             "@rpath/other.dylib",
    281         ):
    282             changed = copy.deepcopy(build)
    283             changed["host_dylib_install_name"] = install_name
    284             with (
    285                 self.subTest(install_name=install_name),
    286                 self.assertRaises(contract.PackageContractError),
    287             ):
    288                 source.validate_build(changed)
    289         for prefix in ("/tmp", "{producer_root}", ""):
    290             changed = copy.deepcopy(build)
    291             changed["host_oso_prefix"] = prefix
    292             with (
    293                 self.subTest(prefix=prefix),
    294                 self.assertRaises(contract.PackageContractError),
    295             ):
    296                 source.validate_build(changed)
    297         for reproducible in (False, 1, "true"):
    298             changed = copy.deepcopy(build)
    299             changed["host_linker_reproducible"] = reproducible
    300             with (
    301                 self.subTest(reproducible=reproducible),
    302                 self.assertRaises(contract.PackageContractError),
    303             ):
    304                 source.validate_build(changed)
    305 
    306 
    307 if __name__ == "__main__":
    308     unittest.main()