| # Copyright 2026 The Fuchsia Authors | |
| # | |
| # Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| # <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| # license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| # This file may not be copied, modified, or distributed except according to | |
| # those terms. | |
| """Unit tests for `ci/check_cartridges_lock.py`.""" | |
| from __future__ import annotations | |
| import copy | |
| import json | |
| import os | |
| from pathlib import Path | |
| import sys | |
| import tempfile | |
| import unittest | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| import check_cartridges_lock as ccl | |
| def _base_lock() -> dict: | |
| return { | |
| "schema_version": 1, | |
| "reference_lean_id": "v4.34.1", | |
| "polarized_bridge_rustc_commit": "0" * 40, | |
| "cartridges": [ | |
| { | |
| "id": "Trusted.Core", | |
| "namespace_prefix": "Trusted.Framework", | |
| "stability": "vetted", | |
| "parents": [], | |
| "stage_l_required": False, | |
| }, | |
| { | |
| "id": "C-MIR", | |
| "namespace_prefix": "Trusted.MirSemantics", | |
| "stability": "vetted", | |
| "parents": ["Trusted.Core"], | |
| "stage_l_required": True, | |
| "required_semantic_epoch": 1, | |
| }, | |
| { | |
| "id": "C-MIR-Alloc", | |
| "namespace_prefix": "Trusted.MirSemantics.Alloc", | |
| "stability": "candidate:mir_alloc", | |
| "parents": ["C-MIR"], | |
| "incompatible_with": ["C-MIR-AllocAlt"], | |
| "stage_l_required": True, | |
| "required_semantic_epoch": 1, | |
| }, | |
| { | |
| "id": "C-MIR-AllocAlt", | |
| "namespace_prefix": "Trusted.MirSemantics.AllocAlt", | |
| "stability": "candidate:mir_alloc_alt", | |
| "parents": ["C-MIR"], | |
| "incompatible_with": ["C-MIR-Alloc"], | |
| "stage_l_required": True, | |
| "required_semantic_epoch": 1, | |
| }, | |
| { | |
| "id": "C-Cpp", | |
| "namespace_prefix": "Trusted.CppSemantics", | |
| "stability": "candidate:cpp", | |
| "parents": ["Trusted.Core"], | |
| "stage_l_required": False, | |
| }, | |
| ], | |
| } | |
| class CheckCartridgesLockTests(unittest.TestCase): | |
| def test_valid_lock_and_candidate_derivation(self): | |
| lock = _base_lock() | |
| self.assertEqual(ccl.validate_cartridges_lock(lock), []) | |
| carts, feats = ccl.derive_observed_candidates( | |
| lock, ["Trusted.MirSemantics.evalStmt"] | |
| ) | |
| self.assertEqual(carts, ["C-MIR", "Trusted.Core"]) | |
| self.assertEqual(feats, []) | |
| carts, feats = ccl.derive_observed_candidates( | |
| lock, ["Trusted.MirSemantics.Alloc.allocBytes"] | |
| ) | |
| self.assertEqual(carts, ["C-MIR", "C-MIR-Alloc", "Trusted.Core"]) | |
| self.assertEqual(feats, ["mir_alloc"]) | |
| def test_incompatible_cartridges_rejected_at_derivation(self): | |
| lock = _base_lock() | |
| with self.assertRaisesRegex(ValueError, "incompatible cartridges"): | |
| ccl.derive_observed_candidates( | |
| lock, | |
| [ | |
| "Trusted.MirSemantics.Alloc.a", | |
| "Trusted.MirSemantics.AllocAlt.b", | |
| ], | |
| ) | |
| def test_vetted_cannot_depend_on_candidate_and_core_must_be_vetted(self): | |
| lock = _base_lock() | |
| lock["cartridges"][1]["parents"].append("C-Cpp") | |
| errs = ccl.validate_cartridges_lock(lock) | |
| self.assertTrue( | |
| any("vetted cartridge `C-MIR` may not depend on candidate" in e for e in errs), | |
| errs, | |
| ) | |
| lock2 = copy.deepcopy(_base_lock()) | |
| lock2["cartridges"][0]["stability"] = "candidate:core_exp" | |
| errs2 = ccl.validate_cartridges_lock(lock2) | |
| self.assertTrue(any("Trusted.Core" in e for e in errs2), errs2) | |
| # A candidate cartridge under a different ID still cannot claim | |
| # `Trusted.Core` or root `Trusted` as its `namespace_prefix`. | |
| lock3 = copy.deepcopy(_base_lock()) | |
| lock3["cartridges"].append({ | |
| "id": "C-SneakyCore", | |
| "namespace_prefix": "Trusted.Core", | |
| "stability": "candidate:sneaky", | |
| "parents": [], | |
| "stage_l_required": False, | |
| }) | |
| errs3 = ccl.validate_cartridges_lock(lock3) | |
| self.assertTrue(any("C-SneakyCore" in e for e in errs3), errs3) | |
| def test_cycles_cross_domain_refinements_and_asymmetry_rejected(self): | |
| lock = _base_lock() | |
| lock["cartridges"][2]["parents"].append("C-Cpp") | |
| errs = ccl.validate_cartridges_lock(lock) | |
| self.assertTrue(any("cross-domain parent" in e for e in errs), errs) | |
| lock2 = _base_lock() | |
| lock2["cartridges"][2]["incompatible_with"] = [] | |
| errs2 = ccl.validate_cartridges_lock(lock2) | |
| self.assertTrue(any("asymmetric" in e for e in errs2), errs2) | |
| lock3 = _base_lock() | |
| lock3["cartridges"][2]["parents"] = ["C-MIR-AllocAlt"] | |
| lock3["cartridges"][3]["parents"] = ["C-MIR-Alloc"] | |
| errs3 = ccl.validate_cartridges_lock(lock3) | |
| self.assertTrue(any("cycle detected" in e for e in errs3), errs3) | |
| def test_root_trusted_prefix_and_three_level_hierarchy_enforced(self): | |
| # Even when `Trusted.Core` owns root prefix `Trusted`, nested | |
| # refinements must still declare their immediate enclosing domain | |
| # (`C-MIR`) and cannot take cross-domain parents (`C-Cpp`). | |
| lock = _base_lock() | |
| lock["cartridges"][0]["namespace_prefix"] = "Trusted" | |
| self.assertEqual(ccl.validate_cartridges_lock(lock), []) | |
| lock_cross = copy.deepcopy(lock) | |
| lock_cross["cartridges"][2]["parents"].append("C-Cpp") | |
| errs_cross = ccl.validate_cartridges_lock(lock_cross) | |
| self.assertTrue(any("cross-domain parent" in e for e in errs_cross), errs_cross) | |
| lock_skip = copy.deepcopy(lock) | |
| lock_skip["cartridges"][2]["parents"] = ["Trusted.Core"] | |
| errs_skip = ccl.validate_cartridges_lock(lock_skip) | |
| self.assertTrue( | |
| any("enclosing domain `C-MIR`" in e for e in errs_skip), errs_skip | |
| ) | |
| # 3-level hierarchy: `C-MIR` -> `C-MIR-Alloc` -> `C-MIR-Alloc-Sub`. | |
| lock_3 = copy.deepcopy(lock) | |
| lock_3["cartridges"].append({ | |
| "id": "C-MIR-Alloc-Sub", | |
| "namespace_prefix": "Trusted.MirSemantics.Alloc.Sub", | |
| "stability": "candidate:mir_alloc_sub", | |
| "parents": ["C-MIR"], | |
| "stage_l_required": True, | |
| "required_semantic_epoch": 1, | |
| }) | |
| errs_3 = ccl.validate_cartridges_lock(lock_3) | |
| self.assertTrue( | |
| any("enclosing domain `C-MIR-Alloc`" in e for e in errs_3), errs_3 | |
| ) | |
| lock_3["cartridges"][-1]["parents"] = ["C-MIR-Alloc"] | |
| self.assertEqual(ccl.validate_cartridges_lock(lock_3), []) | |
| def test_bool_schema_duplicate_keys_incompatible_ancestors_and_symlinks(self): | |
| lock_bool = _base_lock() | |
| lock_bool["schema_version"] = True | |
| errs_bool = ccl.validate_cartridges_lock(lock_bool) | |
| self.assertTrue(any("schema_version" in e for e in errs_bool), errs_bool) | |
| # Float `1.0` must be rejected (schema requires integer 1). | |
| lock_float = _base_lock() | |
| lock_float["schema_version"] = 1.0 | |
| errs_float = ccl.validate_cartridges_lock(lock_float) | |
| self.assertTrue(any("schema_version" in e for e in errs_float), errs_float) | |
| # Vetted cartridge nested inside a candidate namespace must be rejected. | |
| lock_nest = _base_lock() | |
| lock_nest["cartridges"].append({ | |
| "id": "C-Cpp-Vetted", | |
| "namespace_prefix": "Trusted.CppSemantics.Sub", | |
| "stability": "vetted", | |
| "parents": ["Trusted.Core"], | |
| "stage_l_required": False, | |
| }) | |
| errs_nest = ccl.validate_cartridges_lock(lock_nest) | |
| self.assertTrue( | |
| any("may not nest inside candidate cartridge" in e for e in errs_nest), | |
| errs_nest, | |
| ) | |
| # Mutually incompatible ancestors in a single cartridge's closure fail. | |
| lock_inc = _base_lock() | |
| lock_inc["cartridges"].append({ | |
| "id": "C-MIR-Both", | |
| "namespace_prefix": "Trusted.MirSemantics.Both", | |
| "stability": "candidate:mir_both", | |
| "parents": ["C-MIR-Alloc", "C-MIR-AllocAlt"], | |
| "stage_l_required": True, | |
| "required_semantic_epoch": 1, | |
| }) | |
| errs_inc = ccl.validate_cartridges_lock(lock_inc) | |
| self.assertTrue( | |
| any("mutually incompatible ancestors" in e for e in errs_inc), | |
| errs_inc, | |
| ) | |
| with tempfile.TemporaryDirectory(prefix="anneal-cart-lock-") as tmp: | |
| root = Path(tmp) | |
| # Dangling symlink must fail closed rather than returning OK. | |
| dangling = root / "dangling.json" | |
| dangling.symlink_to(root / "does-not-exist.json") | |
| errs_sym = ccl.check_file(dangling, repo_root=root) | |
| self.assertTrue(any("symlinks are forbidden" in e for e in errs_sym), errs_sym) | |
| # Symlinked parent directory must also fail closed. | |
| real_sub = root / "real_sub" | |
| real_sub.mkdir() | |
| (real_sub / "cartridges.lock.json").write_text( | |
| json.dumps(_base_lock()), encoding="utf-8" | |
| ) | |
| link_sub = root / "link_sub" | |
| link_sub.symlink_to(real_sub) | |
| errs_psym = ccl.check_file( | |
| link_sub / "cartridges.lock.json", repo_root=root | |
| ) | |
| self.assertTrue( | |
| any("symlinks are forbidden" in e for e in errs_psym), errs_psym | |
| ) | |
| # `--require-exists` rejects missing lock file. | |
| missing = root / "missing.json" | |
| self.assertEqual( | |
| ccl.check_file(missing, require_exists=False, repo_root=root), [] | |
| ) | |
| errs_req = ccl.check_file(missing, require_exists=True, repo_root=root) | |
| self.assertTrue(any("missing" in e for e in errs_req), errs_req) | |
| # Duplicate JSON keys are rejected. | |
| dup = root / "dup.json" | |
| dup.write_text('{"schema_version": 1, "schema_version": 2}\n', encoding="utf-8") | |
| errs_dup = ccl.check_file(dup, repo_root=root) | |
| self.assertTrue(any("duplicate JSON key" in e for e in errs_dup), errs_dup) | |
| def test_symlinked_host_ancestor_and_relative_lock_containment(self): | |
| with tempfile.TemporaryDirectory(prefix="anneal-cart-host-") as tmp: | |
| base = Path(tmp) | |
| real_host = base / "private_var" | |
| real_repo = real_host / "repo" | |
| (real_repo / "trusted/locks").mkdir(parents=True) | |
| lock_file = real_repo / "trusted/locks/cartridges.lock.json" | |
| lock_file.write_text(json.dumps(_base_lock()), encoding="utf-8") | |
| # Simulate macOS `/var -> private/var` or a symlinked `TMPDIR`. | |
| sym_host = base / "var_alias" | |
| sym_host.symlink_to(real_host) | |
| sym_repo = sym_host / "repo" | |
| sym_lock = sym_repo / "trusted/locks/cartridges.lock.json" | |
| sym_missing = sym_repo / "trusted/locks/missing.json" | |
| # External / unbounded `repo_root=None` beneath a symlinked host | |
| # ancestor must succeed on regular/missing files and reject a leaf | |
| # symlink. | |
| self.assertEqual(ccl.check_file(sym_lock), []) | |
| self.assertEqual(ccl.check_file(sym_missing, require_exists=False), []) | |
| errs_req = ccl.check_file(sym_missing, require_exists=True) | |
| self.assertTrue(any("missing" in e for e in errs_req), errs_req) | |
| leaf_link = real_repo / "trusted/locks/leaf_link.json" | |
| leaf_link.symlink_to(lock_file) | |
| errs_leaf = ccl.check_file(sym_repo / "trusted/locks/leaf_link.json") | |
| self.assertTrue( | |
| any("symlinks are forbidden" in e for e in errs_leaf), errs_leaf | |
| ) | |
| # Passing either the symlinked or resolved repo root allows regular | |
| # files beneath the host symlink while still rejecting repo-internal | |
| # symlinks and missing paths that escape `repo_root`. | |
| self.assertEqual(ccl.check_file(sym_lock, repo_root=sym_repo), []) | |
| self.assertEqual(ccl.check_file(sym_lock, repo_root=real_repo), []) | |
| self.assertEqual( | |
| ccl.check_file(sym_missing, require_exists=False, repo_root=real_repo), | |
| [], | |
| ) | |
| errs_in_sym = ccl.check_file( | |
| sym_repo / "trusted/locks/leaf_link.json", repo_root=real_repo | |
| ) | |
| self.assertTrue( | |
| any("symlinks are forbidden" in e for e in errs_in_sym), errs_in_sym | |
| ) | |
| errs_esc = ccl.check_file( | |
| sym_repo / "../outside.json", repo_root=real_repo | |
| ) | |
| self.assertTrue( | |
| any("escapes repository root" in e for e in errs_esc), errs_esc | |
| ) | |
| # External symlink directory jumping into a repo subdir must fail | |
| # closed when checked against `repo_root`. | |
| jump_dir = base / "jump_locks" | |
| jump_dir.symlink_to(real_repo / "trusted/locks") | |
| errs_jump = ccl.check_file( | |
| jump_dir / "cartridges.lock.json", repo_root=real_repo | |
| ) | |
| self.assertTrue( | |
| any("symlinks are forbidden" in e for e in errs_jump), errs_jump | |
| ) | |
| # Relative `--lock` paths inside the repo infer `repo_root` (even | |
| # when CWD is accessed via a symlinked host ancestor) and enforce | |
| # repo-internal symlink / containment rules. | |
| outside_dir = base / "outside" | |
| outside_dir.mkdir() | |
| outside_lock = outside_dir / "ext.json" | |
| outside_lock.write_text(json.dumps(_base_lock()), encoding="utf-8") | |
| escape_dir = real_repo / "trusted/escape_dir" | |
| escape_dir.symlink_to(outside_dir) | |
| prev_cwd = os.getcwd() | |
| try: | |
| os.chdir(sym_repo) | |
| rel_ok = Path("trusted/locks/cartridges.lock.json") | |
| self.assertEqual(ccl._infer_repo_root(rel_ok, real_repo), real_repo) | |
| self.assertEqual(ccl.check_file(rel_ok, repo_root=real_repo), []) | |
| rel_esc = Path("trusted/escape_dir/ext.json") | |
| self.assertEqual(ccl._infer_repo_root(rel_esc, real_repo), real_repo) | |
| errs_rel_esc = ccl.check_file(rel_esc, repo_root=real_repo) | |
| self.assertTrue( | |
| any("symlinks are forbidden" in e for e in errs_rel_esc), | |
| errs_rel_esc, | |
| ) | |
| # External `--lock` path outside the repo (even via symlinked | |
| # host ancestor) infers `None` and validates cleanly. | |
| sym_outside = base / "var_outside" | |
| sym_outside.symlink_to(outside_dir) | |
| ext_via_sym = sym_outside / "ext.json" | |
| self.assertIsNone(ccl._infer_repo_root(ext_via_sym, real_repo)) | |
| self.assertEqual(ccl.check_file(ext_via_sym, repo_root=None), []) | |
| finally: | |
| os.chdir(prev_cwd) | |
| # CLI `main()` succeeds on an external lock under a symlinked | |
| # ancestor and succeeds on a relative lock path inside the repo. | |
| sym_outside_lock = (base / "var_outside") / "ext.json" | |
| self.assertEqual(ccl.main(["--lock", str(sym_outside_lock)]), 0) | |
| repo_root = Path(__file__).resolve().parent.parent | |
| prev_cwd = os.getcwd() | |
| try: | |
| os.chdir(repo_root) | |
| self.assertEqual( | |
| ccl._infer_repo_root( | |
| Path("trusted/locks/cartridges.lock.json"), repo_root | |
| ), | |
| repo_root, | |
| ) | |
| self.assertEqual( | |
| ccl.main(["--lock", "trusted/locks/cartridges.lock.json"]), 0 | |
| ) | |
| finally: | |
| os.chdir(prev_cwd) | |
| def test_json_schema_aligns_with_validator(self): | |
| schema_path = Path(__file__).resolve().parent / "schemas/cartridges.lock.schema.json" | |
| schema = json.loads(schema_path.read_text(encoding="utf-8")) | |
| self.assertEqual(set(schema["properties"].keys()), set(ccl.ALLOWED_TOP_KEYS)) | |
| item_schema = schema["properties"]["cartridges"]["items"] | |
| self.assertEqual(set(item_schema["properties"].keys()), set(ccl.ALLOWED_CARTRIDGE_KEYS)) | |
| self.assertIn("pattern", schema["properties"]["reference_lean_id"]) | |
| self.assertIn("if", item_schema) | |
| self.assertIn("then", item_schema) | |
| if __name__ == "__main__": | |
| unittest.main() |