| # 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_experimental_isolation.py`.""" | |
| from __future__ import annotations | |
| import os | |
| from pathlib import Path | |
| import sys | |
| import tempfile | |
| import unittest | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| import check_experimental_isolation as cei | |
| LICENSE_HASH = ( | |
| "# Copyright 2026 The Fuchsia Authors\n" | |
| "# Licensed under a BSD-style license <LICENSE-BSD>, Apache License, " | |
| "Version 2.0\n" | |
| ) | |
| LICENSE_SLASH = ( | |
| "// Copyright 2026 The Fuchsia Authors\n" | |
| "// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, " | |
| "Version 2.0\n" | |
| ) | |
| LICENSE_MD = ( | |
| "<!--\n" | |
| "Copyright 2026 The Fuchsia Authors\n" | |
| "Licensed under a BSD-style license <LICENSE-BSD>, Apache License, " | |
| "Version 2.0\n" | |
| "-->\n" | |
| ) | |
| SERDE_SHA = "a" * 64 | |
| FORGED_SHA = "b" * 64 | |
| def _write(path: Path, text: str) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(text, encoding="utf-8") | |
| def _make_base_repo(root: Path) -> None: | |
| _write( | |
| root / "Cargo.toml", | |
| '[workspace]\nresolver = "2"\nmembers = []\nexclude = ["experimental"]\n', | |
| ) | |
| _write( | |
| root / "Cargo.lock", | |
| 'version = 4\n\n' | |
| '[[package]]\nname = "anneal-audit"\nversion = "0.1.0"\n\n' | |
| '[[package]]\nname = "serde"\nversion = "1.0.219"\n' | |
| 'source = "registry+https://github.com/rust-lang/crates.io-index"\n' | |
| f'checksum = "{SERDE_SHA}"\n', | |
| ) | |
| _write( | |
| root / "experimental/README.md", | |
| LICENSE_MD + "\n# Experimental\n", | |
| ) | |
| def _add_valid_experiment(root: Path, slug: str = "demo-exp") -> Path: | |
| exp = root / "experimental" / slug | |
| _write( | |
| exp / "EXPERIMENT.toml", | |
| LICENSE_HASH | |
| + 'owners = ["@joshlf"]\n' | |
| + 'tracking_issue = "https://github.com/google/anneal/issues/123"\n' | |
| + 'expires_on = "2027-01-15"\n' | |
| + 'ci_command = ["./ci.sh"]\n', | |
| ) | |
| _write( | |
| exp / "README.md", | |
| LICENSE_MD | |
| + "\n> [!NOTE]\n" | |
| + "> **Experimental incubator — not audited, not shipped.**\n", | |
| ) | |
| _write(exp / "ci.sh", "#!/usr/bin/env bash\n" + LICENSE_HASH + "echo ok\n") | |
| return exp | |
| class CheckExperimentalIsolationTests(unittest.TestCase): | |
| def test_empty_and_valid_experiment_pass(self): | |
| with tempfile.TemporaryDirectory(prefix="anneal-exp-ok-") as tmp: | |
| root = Path(tmp) | |
| _make_base_repo(root) | |
| self.assertEqual(cei.check_repo(root), []) | |
| exp = _add_valid_experiment(root) | |
| _write( | |
| exp / "Cargo.toml", | |
| LICENSE_HASH | |
| + '[package]\nname = "demo-exp"\nversion = "0.1.0"\n' | |
| + 'edition = "2024"\npublish = false\n' | |
| + '[dependencies]\nserde = "1.0.219"\n', | |
| ) | |
| _write( | |
| exp / "Cargo.lock", | |
| 'version = 4\n\n[[package]]\nname = "demo-exp"\nversion = "0.1.0"\n\n' | |
| '[[package]]\nname = "serde"\nversion = "1.0.219"\n' | |
| 'source = "registry+https://github.com/rust-lang/crates.io-index"\n' | |
| f'checksum = "{SERDE_SHA}"\n', | |
| ) | |
| # Intra-slug relative path (`../helper.rs` from `src/sub/a.rs`) is | |
| # allowed. | |
| _write( | |
| exp / "src/sub/a.rs", | |
| LICENSE_SLASH + '#[path = "../helper.rs"]\nmod helper;\n', | |
| ) | |
| self.assertEqual(cei.check_repo(root), []) | |
| def test_inbound_symlinks_imports_scripts_and_sideways_edges_rejected(self): | |
| with tempfile.TemporaryDirectory(prefix="anneal-exp-edge-") as tmp: | |
| root = Path(tmp) | |
| _make_base_repo(root) | |
| _add_valid_experiment(root, "alpha") | |
| _add_valid_experiment(root, "beta") | |
| # Cargo path dep. | |
| _write( | |
| root / "tools/demo/Cargo.toml", | |
| '[dependencies]\nalpha = { path = "../../experimental/alpha" }\n', | |
| ) | |
| errs = cei.check_repo(root) | |
| self.assertTrue(any("forbidden inbound edge" in e for e in errs), errs) | |
| (root / "tools/demo/Cargo.toml").unlink() | |
| # Direct repo-root shell reference, subdir-relative shell call, | |
| # `${ROOT}/experimental/...`, and Python `os.path.join` / `Path` / | |
| # namespace `from experimental import ...`. | |
| for bad_script in ( | |
| "#!/bin/sh\nbash experimental/alpha/ci.sh\n", | |
| "#!/bin/sh\nbash sub/../experimental/alpha/ci.sh\n", | |
| "#!/bin/sh\nbash ${ROOT}/experimental/alpha/ci.sh\n", | |
| "import os\nos.path.join(root, 'experimental', 'alpha')\n", | |
| "from pathlib import Path\nPath('experimental') / 'alpha'\n", | |
| "from experimental import alpha\n", | |
| ): | |
| _write(root / "tools/demo/run.py", bad_script) | |
| errs = cei.check_repo(root) | |
| self.assertTrue( | |
| any("forbidden inbound edge" in e for e in errs), | |
| (bad_script, errs), | |
| ) | |
| (root / "tools/demo/run.py").unlink() | |
| # Rust raw-string `#[path]` / `include!` with leading subdirs. | |
| _write( | |
| root / "tools/demo/lib.rs", | |
| '#[path = r"sub/../../../../experimental/alpha/mod.rs"]\nmod a;\n', | |
| ) | |
| errs = cei.check_repo(root) | |
| self.assertTrue(any("forbidden inbound edge" in e for e in errs), errs) | |
| (root / "tools/demo/lib.rs").unlink() | |
| # Capitalized Lean import and Lean 4.35 `import all`. | |
| for bad_lean in ( | |
| "import Experimental.Alpha\n", | |
| "import all Experimental.Alpha\n", | |
| ): | |
| _write(root / "theory/Foo.lean", bad_lean) | |
| errs = cei.check_repo(root) | |
| self.assertTrue(any("Lean import" in e for e in errs), errs) | |
| (root / "theory/Foo.lean").unlink() | |
| # Inbound symlink outside `experimental/`. | |
| (root / "tests").mkdir(parents=True, exist_ok=True) | |
| os.symlink("../experimental/alpha/ci.sh", root / "tests/link.sh") | |
| errs = cei.check_repo(root) | |
| self.assertTrue(any("inbound symlink" in e for e in errs), errs) | |
| (root / "tests/link.sh").unlink() | |
| # Sideways explicit and relative `../alpha/`, `./../alpha/`, | |
| # `src/../../alpha/`, and Markdown `[a](./../alpha/README.md)`. | |
| for bad_rs in ( | |
| '#[path = "../alpha/mod.rs"]\nmod a;\n', | |
| '#[path = "./../alpha/mod.rs"]\nmod a;\n', | |
| '#[path = "src/../../alpha/mod.rs"]\nmod a;\n', | |
| 'const S: &str = include_str!("./../../trusted/audit/src/lib.rs");\n', | |
| ): | |
| _write(root / "experimental/beta/lib.rs", LICENSE_SLASH + bad_rs) | |
| errs = cei.check_repo(root) | |
| self.assertTrue( | |
| any("escaping/sideways relative path" in e for e in errs), | |
| (bad_rs, errs), | |
| ) | |
| for bad_sh in ( | |
| "bash ./../alpha/ci.sh\n", | |
| "bash sub/../../alpha/ci.sh\n", | |
| ): | |
| _write(root / "experimental/beta/run.sh", LICENSE_HASH + bad_sh) | |
| errs = cei.check_repo(root) | |
| self.assertTrue( | |
| any("escaping/sideways relative path" in e for e in errs), | |
| (bad_sh, errs), | |
| ) | |
| (root / "experimental/beta/run.sh").unlink() | |
| _write( | |
| root / "experimental/beta/NOTES.md", | |
| LICENSE_MD + "[a](./../alpha/README.md)\n", | |
| ) | |
| errs = cei.check_repo(root) | |
| self.assertTrue( | |
| any("sideways relative link" in e for e in errs), errs | |
| ) | |
| (root / "experimental/beta/NOTES.md").unlink() | |
| _write( | |
| root / "experimental/beta/lib.rs", | |
| LICENSE_SLASH + "// see experimental/alpha/lib.rs\n", | |
| ) | |
| errs = cei.check_repo(root) | |
| self.assertTrue(any("sideways reference" in e for e in errs), errs) | |
| def test_cargo_manifest_lock_build_dir_and_case_hardening(self): | |
| with tempfile.TemporaryDirectory(prefix="anneal-exp-hard-") as tmp: | |
| root = Path(tmp) | |
| _make_base_repo(root) | |
| exp = _add_valid_experiment(root, "hard-exp") | |
| # 1. Manifest hardening: `[workspace.dependencies]`, `**/../alpha` | |
| # glob escape, underscore tables, `publish = false`, | |
| # `[package].build`, `[package].workspace`, | |
| # `[lib] crate-type = ["proc-macro"]`, target `path` escape, | |
| # and missing `Cargo.lock`. | |
| _write( | |
| exp / "Cargo.toml", | |
| LICENSE_HASH | |
| + '[workspace]\nmembers = ["**/../alpha"]\n' | |
| + '[workspace.dependencies]\nrogue-ws = "0.1"\n' | |
| + '[package]\nname = "hard-exp"\nversion = "0.1.0"\n' | |
| + 'build = "custom_build.rs"\n' | |
| + 'workspace = "./../.."\n' | |
| + '[lib]\ncrate-type = ["proc-macro"]\npath = "../alpha/lib.rs"\n' | |
| + '[dependencies]\nserde = "1.0.219"\n' | |
| + '[dev_dependencies]\nrogue-dev = "0.2"\n' | |
| + '[build_dependencies]\nrogue-bld = "0.3"\n', | |
| ) | |
| errs = "\n".join(cei.check_repo(root)) | |
| self.assertIn("[workspace].members", errs) | |
| self.assertIn("rogue-ws", errs) | |
| self.assertIn("publish = false", errs) | |
| self.assertIn("[package].build", errs) | |
| self.assertIn("[package].workspace", errs) | |
| self.assertIn("proc-macro", errs) | |
| self.assertIn("[lib] path `../alpha/lib.rs` escapes", errs) | |
| self.assertIn("requires a checked-in Cargo.lock", errs) | |
| self.assertIn("rogue-dev", errs) | |
| self.assertIn("rogue-bld", errs) | |
| # Virtual workspace (no local `[package]`) still rejects source-less | |
| # `[[package]]` entries in `Cargo.lock`. | |
| _write( | |
| exp / "Cargo.toml", | |
| LICENSE_HASH + '[workspace]\nresolver = "2"\nmembers = []\n', | |
| ) | |
| _write( | |
| exp / "Cargo.lock", | |
| 'version = 4\n\n[[package]]\nname = "rogue-local"\nversion = "0.1.0"\n', | |
| ) | |
| errs_virt = "\n".join(cei.check_repo(root)) | |
| self.assertIn("rogue-local", errs_virt) | |
| # 2. `Cargo.lock` provenance: rejects git source, forged checksum, | |
| # and source-less workspace crate name (`anneal-audit`). | |
| _write( | |
| exp / "Cargo.toml", | |
| LICENSE_HASH | |
| + '[package]\nname = "hard-exp"\nversion = "0.1.0"\npublish = false\n' | |
| + '[dependencies]\nserde = "1.0.219"\n', | |
| ) | |
| _write( | |
| exp / "Cargo.lock", | |
| 'version = 4\n\n' | |
| '[[package]]\nname = "hard-exp"\nversion = "0.1.0"\n\n' | |
| '[[package]]\nname = "serde"\nversion = "1.0.219"\n' | |
| 'source = "git+https://attacker.example/serde.git#deadbeef"\n' | |
| f'checksum = "{FORGED_SHA}"\n\n' | |
| '[[package]]\nname = "anneal-audit"\nversion = "0.1.0"\n' | |
| 'source = "registry+https://github.com/rust-lang/crates.io-index"\n' | |
| f'checksum = "{SERDE_SHA}"\n', | |
| ) | |
| errs_lock = "\n".join(cei.check_repo(root)) | |
| self.assertIn("forbidden source", errs_lock) | |
| self.assertIn("anneal-audit", errs_lock) | |
| # Forged checksum with valid crates.io source also fails. | |
| _write( | |
| exp / "Cargo.lock", | |
| 'version = 4\n\n' | |
| '[[package]]\nname = "hard-exp"\nversion = "0.1.0"\n\n' | |
| '[[package]]\nname = "serde"\nversion = "1.0.219"\n' | |
| 'source = "registry+https://github.com/rust-lang/crates.io-index"\n' | |
| f'checksum = "{FORGED_SHA}"\n', | |
| ) | |
| errs_sha = "\n".join(cei.check_repo(root)) | |
| self.assertIn("(source/checksum) is not present", errs_sha) | |
| # 3. `.build/` is forbidden (never skipped), and case-variant | |
| # filenames/dirs fail closed. | |
| _write(exp / ".build/hidden.rs", LICENSE_SLASH + "fn x() {}\n") | |
| _write(exp / "BUILD.RS", LICENSE_SLASH + "fn main() {}\n") | |
| _write(exp / "Agents.md", LICENSE_MD + "# bad\n") | |
| _write(exp / ".CARGO/config.toml", LICENSE_HASH + "[net]\n") | |
| _write(exp / "Rust-Toolchain.toml", LICENSE_HASH + "[toolchain]\n") | |
| _write( | |
| exp / "sub/cargo.toml", | |
| LICENSE_HASH + '[package]\nname = "s"\nversion = "0.1.0"\n', | |
| ) | |
| errs_case = "\n".join(cei.check_repo(root)) | |
| self.assertIn(".build: forbidden directory", errs_case) | |
| self.assertIn("BUILD.RS: forbidden file", errs_case) | |
| self.assertIn("Agents.md: forbidden file", errs_case) | |
| self.assertIn(".CARGO: forbidden directory", errs_case) | |
| self.assertIn("Rust-Toolchain.toml: forbidden file", errs_case) | |
| self.assertIn("non-canonical casing for Cargo.toml", errs_case) | |
| def test_ci_command_and_forbidden_files_rejected(self): | |
| with tempfile.TemporaryDirectory(prefix="anneal-exp-bad-") as tmp: | |
| root = Path(tmp) | |
| _make_base_repo(root) | |
| exp = _add_valid_experiment(root, "bad-exp") | |
| _write( | |
| exp / "EXPERIMENT.toml", | |
| LICENSE_HASH | |
| + 'owners = ["@joshlf"]\n' | |
| + 'tracking_issue = "https://github.com/google/anneal/issues/123"\n' | |
| + 'expires_on = "2027-01-15"\n' | |
| + 'ci_command = ["./missing.sh", "../../secret"]\n', | |
| ) | |
| _write(exp / "AGENTS.md", LICENSE_MD + "# bad\n") | |
| _write(exp / "build.rs", LICENSE_SLASH + "fn main() {}\n") | |
| _write( | |
| exp / "notes.md", | |
| LICENSE_MD + "see https://host.internal/secret and \u202ebidi\n", | |
| ) | |
| os.symlink(exp / "README.md", exp / "link.md") | |
| errs = "\n".join(cei.check_repo(root)) | |
| self.assertIn("ci_command[1]", errs) | |
| self.assertIn("does not exist as a regular file", errs) | |
| self.assertIn("AGENTS.md", errs) | |
| self.assertIn("build.rs", errs) | |
| self.assertIn("bidi control", errs) | |
| self.assertIn("private corporate hostname", errs) | |
| self.assertIn("symlinks are forbidden", errs) | |
| # `lakefile.*`, `.cargo` file, and invisible format characters are | |
| # also rejected, and live CI/release/manifest files are never in | |
| # `INBOUND_SCAN_EXEMPT_FILES`. | |
| _write(exp / "lakefile.custom", LICENSE_HASH + "x\n") | |
| _write(exp / ".cargo", LICENSE_HASH + "x\n") | |
| _write(exp / "zwsp.md", LICENSE_MD + "zero\u200bwidth\n") | |
| errs_extra = "\n".join(cei.check_repo(root)) | |
| self.assertIn("lakefile.custom: forbidden file", errs_extra) | |
| self.assertIn(".cargo: forbidden file", errs_extra) | |
| self.assertIn("U+200B", errs_extra) | |
| for must_scan in ( | |
| "Cargo.toml", | |
| "ci/run_anneal_check.sh", | |
| "ci/checks.toml", | |
| "ci/pack_omnibus_archive.sh", | |
| "ci/alpha_release.py", | |
| ".github/release_notes.py", | |
| "trusted/audit/src/cargo_boundary.rs", | |
| "tests/audit/tests/common/mod.rs", | |
| ): | |
| self.assertNotIn(must_scan, cei.INBOUND_SCAN_EXEMPT_FILES) | |
| self.assertEqual(cei.INBOUND_SCAN_EXEMPT_PREFIXES, ()) | |
| def test_manifest_escape_readme_and_inbound_hardening(self): | |
| with tempfile.TemporaryDirectory(prefix="anneal-exp-ext-") as tmp: | |
| root = Path(tmp) | |
| _make_base_repo(root) | |
| _add_valid_experiment(root, "alpha") | |
| exp = _add_valid_experiment(root, "beta") | |
| # 1. `experimental/README.md`, `[link](../README.md)`, and | |
| # `.local`/`.internal` field accesses are allowed inside slugs. | |
| _write( | |
| exp / "NOTES.md", | |
| LICENSE_MD + "See [charter](../README.md) and experimental/README.md.\n", | |
| ) | |
| _write( | |
| exp / "src/lib.rs", | |
| LICENSE_SLASH | |
| + "// See experimental/README.md for charter.\n" | |
| + "// Access `place.local` and `features.internal`.\n" | |
| + "pub fn f() -> u32 { 0 }\n", | |
| ) | |
| self.assertEqual(cei.check_repo(root), []) | |
| # 2. `[project]` alias, non-`[lib]` (`[[bin]]`) `proc-macro`, | |
| # mismatched dep version (`serde = "2.0.0"` vs locked `1.0.219`), | |
| # and unlinked sub-crate (`sub/Cargo.toml` when root is not a | |
| # workspace) all fail closed. | |
| _write( | |
| exp / "Cargo.toml", | |
| LICENSE_HASH | |
| + '[project]\nname = "beta"\nversion = "0.1.0"\n' | |
| + '[[bin]]\nname = "m"\ncrate-type = ["proc-macro"]\n' | |
| + '[dependencies]\nserde = "2.0.0"\n', | |
| ) | |
| _write( | |
| exp / "Cargo.lock", | |
| 'version = 4\n\n[[package]]\nname = "beta"\nversion = "0.1.0"\n\n' | |
| '[[package]]\nname = "serde"\nversion = "1.0.219"\n' | |
| 'source = "registry+https://github.com/rust-lang/crates.io-index"\n' | |
| f'checksum = "{SERDE_SHA}"\n', | |
| ) | |
| _write( | |
| exp / "sub/Cargo.toml", | |
| LICENSE_HASH | |
| + '[package]\nname = "sub"\nversion = "0.1.0"\npublish = false\n' | |
| + '[dependencies]\nserde = "1.0.219"\n', | |
| ) | |
| errs_m = "\n".join(cei.check_repo(root)) | |
| self.assertIn("[project]", errs_m) | |
| self.assertIn("publish = false", errs_m) | |
| self.assertIn("[[bin]][0] proc-macro targets are forbidden", errs_m) | |
| self.assertIn("does not match locked version(s)", errs_m) | |
| self.assertIn( | |
| "sub/Cargo.toml: [dependencies] external dependency `serde` " | |
| "requires a checked-in Cargo.lock", | |
| errs_m, | |
| ) | |
| (exp / "sub/Cargo.toml").unlink() | |
| (exp / "sub").rmdir() | |
| (exp / "Cargo.toml").unlink() | |
| (exp / "Cargo.lock").unlink() | |
| # 3. `ci_command` flag values (`--manifest-path=...`, | |
| # `--cfg=/etc/passwd`, backslashes) and quoted flag/command | |
| # relative escapes in source/scripts fail closed. | |
| for bad_arg in ( | |
| "--manifest-path=../alpha/Cargo.toml", | |
| "--cfg=/etc/passwd", | |
| ".\\\\ci.sh", | |
| ): | |
| _write( | |
| exp / "EXPERIMENT.toml", | |
| LICENSE_HASH | |
| + 'owners = ["@joshlf"]\n' | |
| + 'tracking_issue = "https://github.com/google/anneal/issues/1"\n' | |
| + 'expires_on = "2027-01-15"\n' | |
| + f'ci_command = ["./ci.sh", "{bad_arg}"]\n', | |
| ) | |
| errs_cmd = "\n".join(cei.check_repo(root)) | |
| self.assertIn("ci_command[1]", errs_cmd, (bad_arg, errs_cmd)) | |
| _add_valid_experiment(root, "beta") | |
| for bad_line in ( | |
| 'const P: &str = "--manifest-path=../../trusted/audit/Cargo.toml";\n', | |
| 'const Q: &str = "bash ../../alpha/ci.sh";\n', | |
| 'const R: &str = "..\\\\..\\\\trusted\\\\audit";\n', | |
| ): | |
| _write(exp / "src/escape.rs", LICENSE_SLASH + bad_line) | |
| errs_esc = "\n".join(cei.check_repo(root)) | |
| self.assertIn( | |
| "forbidden escaping/sideways relative path", | |
| errs_esc, | |
| (bad_line, errs_esc), | |
| ) | |
| (exp / "src/escape.rs").unlink() | |
| for bad_root_line in ( | |
| 'cmd="--manifest-path=../alpha/Cargo.toml"\n', | |
| 'cmd="bash ../alpha/ci.sh"\n', | |
| 'cmd="..\\\\alpha\\\\ci.sh"\n', | |
| ): | |
| _write(exp / "run.sh", LICENSE_HASH + bad_root_line) | |
| errs_root = "\n".join(cei.check_repo(root)) | |
| self.assertIn( | |
| "forbidden escaping/sideways relative path", | |
| errs_root, | |
| (bad_root_line, errs_root), | |
| ) | |
| (exp / "run.sh").unlink() | |
| # 4. Inbound scan catches `tests/unit/target/bad.rs`, TOML-escaped | |
| # path deps in `tests/unregistered/Cargo.toml`, and Lean block | |
| # comment / guillemet imports. | |
| _write( | |
| root / "tests/unit/target/bad.rs", | |
| LICENSE_SLASH + '#[path = "../../../experimental/alpha/src/lib.rs"]\nmod a;\n', | |
| ) | |
| _write( | |
| root / "tests/unregistered/Cargo.toml", | |
| LICENSE_HASH | |
| + '[package]\nname = "unreg"\nversion = "0.1.0"\n' | |
| + '[dependencies]\nalpha = { "\\u0070ath" = "../../\\u0065xperimental/alpha" }\n', | |
| ) | |
| _write( | |
| root / "theory/Bad1.lean", | |
| "import /- comment -/ Experimental.Alpha\n", | |
| ) | |
| _write( | |
| root / "theory/Bad2.lean", | |
| "import «Experimental».Alpha\n", | |
| ) | |
| errs_in = "\n".join(cei.check_repo(root)) | |
| self.assertIn("tests/unit/target/bad.rs", errs_in) | |
| self.assertIn("tests/unregistered/Cargo.toml", errs_in) | |
| self.assertIn("theory/Bad1.lean", errs_in) | |
| self.assertIn("theory/Bad2.lean", errs_in) | |
| for p in ( | |
| root / "tests/unit/target/bad.rs", | |
| root / "tests/unregistered/Cargo.toml", | |
| root / "theory/Bad1.lean", | |
| root / "theory/Bad2.lean", | |
| ): | |
| p.unlink() | |
| # 5. Nested `[workspace]` in sub-crate cannot inherit parent | |
| # `Cargo.lock`, and a decoy member-local `Cargo.lock` cannot mask | |
| # a missing workspace-root `Cargo.lock`; partial SemVer `>`/`!=` | |
| # and operator-prefixed `*` fail closed; `crates/*/src/target/` | |
| # and `tools/*/src/target/` are scanned; recursive Lean block | |
| # comments (`/- outer /- inner -/ -/`), `--` line comments, | |
| # `private`/`meta` modifiers, and intra-experimental sideways | |
| # `import Experimental.Alpha` fail closed; `ci_command` splits on | |
| # redirections/separators (`cat</etc/passwd`); and `__pycache__`, | |
| # `.claude`, `.agents`, `.codex`, `Makefile`, `Justfile`, `.nix`, | |
| # `.yml`, `lake-manifest.json`, `.gitattributes`, `.envrc`, | |
| # `CLAUDE.md`, `GEMINI.md`, and `.mcp.json` are forbidden inside | |
| # slugs. | |
| _write( | |
| exp / "Cargo.toml", | |
| LICENSE_HASH | |
| + '[workspace]\nmembers = ["sub"]\n' | |
| + '[package]\nname = "beta"\nversion = "0.1.0"\npublish = false\n', | |
| ) | |
| _write( | |
| exp / "Cargo.lock", | |
| 'version = 4\n\n[[package]]\nname = "beta"\nversion = "0.1.0"\n\n' | |
| '[[package]]\nname = "serde"\nversion = "1.0.219"\n' | |
| 'source = "registry+https://github.com/rust-lang/crates.io-index"\n' | |
| f'checksum = "{SERDE_SHA}"\n', | |
| ) | |
| _write( | |
| exp / "sub/Cargo.toml", | |
| LICENSE_HASH | |
| + '[workspace]\n' | |
| + '[package]\nname = "sub"\nversion = "0.1.0"\npublish = false\n' | |
| + '[dependencies]\nserde = "1.0.219"\n', | |
| ) | |
| errs_ws = "\n".join(cei.check_repo(root)) | |
| self.assertIn( | |
| "sub/Cargo.toml: [dependencies] external dependency `serde` " | |
| "requires a checked-in Cargo.lock", | |
| errs_ws, | |
| ) | |
| # Member crate with decoy member-local Cargo.lock when workspace | |
| # root Cargo.lock is missing still fails closed. | |
| (exp / "Cargo.lock").unlink() | |
| _write( | |
| exp / "sub/Cargo.toml", | |
| LICENSE_HASH | |
| + '[package]\nname = "sub"\nversion = "0.1.0"\npublish = false\n' | |
| + '[dependencies]\nserde = "1.0.219"\n', | |
| ) | |
| _write( | |
| exp / "sub/Cargo.lock", | |
| 'version = 4\n\n[[package]]\nname = "sub"\nversion = "0.1.0"\n\n' | |
| '[[package]]\nname = "serde"\nversion = "1.0.219"\n' | |
| 'source = "registry+https://github.com/rust-lang/crates.io-index"\n' | |
| f'checksum = "{SERDE_SHA}"\n', | |
| ) | |
| errs_decoy = "\n".join(cei.check_repo(root)) | |
| self.assertIn( | |
| "sub/Cargo.toml: [dependencies] external dependency `serde` " | |
| "requires a checked-in Cargo.lock", | |
| errs_decoy, | |
| ) | |
| (exp / "sub/Cargo.lock").unlink() | |
| (exp / "sub/Cargo.toml").unlink() | |
| (exp / "sub").rmdir() | |
| (exp / "Cargo.toml").unlink() | |
| for bad_req in (">1", ">1.0", "!=1", "!=1.0", ">=1.*", "!=1.*"): | |
| self.assertFalse( | |
| cei._version_req_matches(bad_req, "1.0.219"), bad_req | |
| ) | |
| for ok_req in ("=1", "=1.0", "=1.0.219", ">0.9", "!=2", "!=1.1", "1.*", "1.0.*"): | |
| self.assertTrue( | |
| cei._version_req_matches(ok_req, "1.0.219"), ok_req | |
| ) | |
| _write( | |
| exp / "EXPERIMENT.toml", | |
| LICENSE_HASH | |
| + 'owners = ["@joshlf"]\n' | |
| + 'tracking_issue = "https://github.com/google/anneal/issues/1"\n' | |
| + 'expires_on = "2027-01-15"\n' | |
| + 'ci_command = ["./ci.sh", "cat</etc/passwd"]\n', | |
| ) | |
| self.assertIn("ci_command[1]", "\n".join(cei.check_repo(root))) | |
| _add_valid_experiment(root, "beta") | |
| _write( | |
| exp / "Sideways.lean", | |
| "-- Copyright 2026 The Fuchsia Authors\n" | |
| "-- Licensed under a BSD-style license <LICENSE-BSD>, Apache " | |
| "License, Version 2.0\n" | |
| "prelude import Experimental.Alpha\n", | |
| ) | |
| _write( | |
| root / "crates/exocrate/src/target/mod.rs", | |
| LICENSE_SLASH | |
| + '#[path = "../../../../experimental/alpha/src/lib.rs"]\nmod a;\n', | |
| ) | |
| _write( | |
| root / "tools/spec/src/target/mod.rs", | |
| LICENSE_SLASH | |
| + '#[path = "../../../../experimental/alpha/src/lib.rs"]\nmod a;\n', | |
| ) | |
| _write( | |
| root / "tests/unit/node_modules/mod.rs", | |
| LICENSE_SLASH | |
| + '#[path = "../../../experimental/alpha/src/lib.rs"]\nmod a;\n', | |
| ) | |
| _write( | |
| root / "theory/NestedComment.lean", | |
| "import /- outer /- inner -/ outer -/ Experimental.Alpha\n", | |
| ) | |
| _write( | |
| root / "theory/LineComment.lean", | |
| "import -- note\n Experimental.Alpha\n", | |
| ) | |
| _write( | |
| root / "theory/PrivateMeta.lean", | |
| "public meta import all Experimental.Alpha\n", | |
| ) | |
| _write( | |
| root / "theory/PrivateOnly.lean", | |
| "private import all Experimental.Alpha\n", | |
| ) | |
| _write( | |
| root / "theory/ChainedHeader.lean", | |
| "module import Init.Core import Experimental.Alpha\n", | |
| ) | |
| _write(exp / "__pycache__/x.txt", LICENSE_HASH + "x\n") | |
| _write(exp / ".claude/settings.json", LICENSE_HASH + "{}\n") | |
| _write(exp / ".agents/rule.md", LICENSE_MD + "# bad\n") | |
| _write(exp / ".codex/cfg.toml", LICENSE_HASH + "x = 1\n") | |
| _write(exp / "Makefile", LICENSE_HASH + "all:\n") | |
| _write(exp / "Justfile", LICENSE_HASH + "all:\n") | |
| _write(exp / "shell.nix", LICENSE_HASH + "{}\n") | |
| _write(exp / "wf.yml", LICENSE_HASH + "name: x\n") | |
| _write(exp / "lake-manifest.json", "{}\n") | |
| _write(exp / ".gitattributes", LICENSE_HASH + "* text=auto\n") | |
| _write(exp / ".envrc", LICENSE_HASH + "dotenv\n") | |
| _write(exp / "CLAUDE.md", LICENSE_MD + "# bad\n") | |
| _write(exp / "GEMINI.md", LICENSE_MD + "# bad\n") | |
| _write(exp / ".mcp.json", LICENSE_HASH + "{}\n") | |
| errs_ext = "\n".join(cei.check_repo(root)) | |
| self.assertIn("Sideways.lean: forbidden sideways Lean import", errs_ext) | |
| self.assertIn("crates/exocrate/src/target/mod.rs", errs_ext) | |
| self.assertIn("tools/spec/src/target/mod.rs", errs_ext) | |
| self.assertIn("tests/unit/node_modules/mod.rs", errs_ext) | |
| self.assertIn("theory/NestedComment.lean", errs_ext) | |
| self.assertIn("theory/LineComment.lean", errs_ext) | |
| self.assertIn("theory/PrivateMeta.lean", errs_ext) | |
| self.assertIn("theory/PrivateOnly.lean", errs_ext) | |
| self.assertIn("theory/ChainedHeader.lean", errs_ext) | |
| self.assertIn("__pycache__: forbidden directory", errs_ext) | |
| self.assertIn(".claude: forbidden directory", errs_ext) | |
| self.assertIn(".agents: forbidden directory", errs_ext) | |
| self.assertIn(".codex: forbidden directory", errs_ext) | |
| self.assertIn("Makefile: forbidden file", errs_ext) | |
| self.assertIn("Justfile: forbidden file", errs_ext) | |
| self.assertIn("shell.nix: forbidden file", errs_ext) | |
| self.assertIn("wf.yml: forbidden file", errs_ext) | |
| self.assertIn("lake-manifest.json: forbidden file", errs_ext) | |
| self.assertIn(".gitattributes: forbidden file", errs_ext) | |
| self.assertIn(".envrc: forbidden file", errs_ext) | |
| self.assertIn("CLAUDE.md: forbidden file", errs_ext) | |
| self.assertIn("GEMINI.md: forbidden file", errs_ext) | |
| self.assertIn(".mcp.json: forbidden file", errs_ext) | |
| # Kebab-case Lean segment matching (`«demo-exp»` vs `«demo-other»`) | |
| # and full-file binary NUL detection past 8 KiB. | |
| self.assertTrue(cei._lean_segment_matches_slug("demo-exp", "demo-exp")) | |
| self.assertTrue(cei._lean_segment_matches_slug("demo_exp", "demo-exp")) | |
| self.assertTrue(cei._lean_segment_matches_slug("DemoExp", "demo-exp")) | |
| self.assertFalse(cei._lean_segment_matches_slug("demo-other", "demo")) | |
| self.assertFalse(cei._lean_segment_matches_slug("alpha-beta", "alpha")) | |
| self.assertFalse(cei._lean_segment_matches_slug("alpha-beta", "alphabeta")) | |
| self.assertTrue(cei._is_binary_bytes(b"a" * 9000 + b"\x00")) | |
| if __name__ == "__main__": | |
| unittest.main() |