aien-receipts
Append-only AIEN verification receipts, indexed by repository and exact source commit. Read verdicts, signatures and retained logs before making a claim.
Brought by drakestapleton
aien-receipts / audit / tests / test_audit.py
| 1 | """Tests for the org audit tools. Run: python3 -m unittest discover -s audit/tests -v |
| 2 | |
| 3 | Every test builds its own git repository and, where a scanner is involved, a fake |
| 4 | scanner script with a chosen version and exit code, so the tests need no network |
| 5 | and no installed scanners. Each guard has a negative control: the same input with |
| 6 | the guard's condition flipped must change the outcome. |
| 7 | """ |
| 8 | import hashlib, json, os, shutil, subprocess, sys, tempfile, unittest |
| 9 | |
| 10 | TOOLS = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tools") |
| 11 | sys.path.insert(0, TOOLS) |
| 12 | import checks # noqa: E402 |
| 13 | import loc # noqa: E402 |
| 14 | import stamp # noqa: E402 |
| 15 | import status # noqa: E402 |
| 16 | import verify # noqa: E402 |
| 17 | |
| 18 | GIT_ENV = {"GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@example.invalid", "GIT_COMMITTER_NAME": "t", |
| 19 | "GIT_COMMITTER_EMAIL": "t@example.invalid", "GIT_AUTHOR_DATE": "2026-01-01T00:00:00Z", |
| 20 | "GIT_COMMITTER_DATE": "2026-01-01T00:00:00Z"} |
| 21 | |
| 22 | |
| 23 | def sh(*cmd, cwd=None): |
| 24 | return subprocess.run(cmd, cwd=cwd, check=True, capture_output=True, env={**os.environ, **GIT_ENV}).stdout.decode().strip() |
| 25 | |
| 26 | |
| 27 | def make_repo(root, files): |
| 28 | os.makedirs(root, exist_ok=True) |
| 29 | sh("git", "init", "-q", "-b", "main", root) |
| 30 | return commit(root, files) |
| 31 | |
| 32 | |
| 33 | def commit(root, files, msg="c"): |
| 34 | for p, data in files.items(): |
| 35 | full = os.path.join(root, p) |
| 36 | os.makedirs(os.path.dirname(full), exist_ok=True) |
| 37 | with open(full, "wb") as fh: |
| 38 | fh.write(data if isinstance(data, bytes) else data.encode()) |
| 39 | sh("git", "add", "-A", cwd=root) |
| 40 | sh("git", "commit", "-q", "-m", msg, cwd=root) |
| 41 | return sh("git", "rev-parse", "HEAD", cwd=root) |
| 42 | |
| 43 | |
| 44 | def fake_tool(path, version, body): |
| 45 | os.makedirs(os.path.dirname(path), exist_ok=True) |
| 46 | with open(path, "w") as fh: |
| 47 | fh.write("#!/bin/sh\ncase \"$1\" in --version|-version|version) echo '%s'; exit 0;; esac\n%s\n" % (version, body)) |
| 48 | os.chmod(path, 0o755) |
| 49 | |
| 50 | |
| 51 | class Base(unittest.TestCase): |
| 52 | def setUp(self): |
| 53 | self.tmp = tempfile.mkdtemp(prefix="aien-audit-test-") |
| 54 | self.addCleanup(shutil.rmtree, self.tmp) |
| 55 | self.tools = os.path.join(self.tmp, "tools") |
| 56 | os.makedirs(self.tools) |
| 57 | self.lock = {"schema": "aien-audit-tools-lock/1", "tools": { |
| 58 | "osv-scanner": {"version": "2.7.0", "version_cmd": ["--version"]}, |
| 59 | "venv/bin/semgrep": {"version": "1.182.1", "version_cmd": ["--version"]}}, |
| 60 | "semgrep_rules": {"config_sha256": ""}} |
| 61 | self.lock_path = os.path.join(self.tmp, "tools.lock.json") |
| 62 | self.write_lock() |
| 63 | |
| 64 | def write_lock(self): |
| 65 | with open(self.lock_path, "w") as fh: |
| 66 | json.dump(self.lock, fh) |
| 67 | |
| 68 | def run_checks(self, repo, sha, only, name="r"): |
| 69 | out = os.path.join(self.tmp, "out-" + only.replace(",", "_")) |
| 70 | doc = checks.run_all(repo, sha, name, out, tools=self.tools, only=only, lock_path=self.lock_path) |
| 71 | return doc, {c["check"]: c for c in doc["checks"]}, out |
| 72 | |
| 73 | |
| 74 | class TestCheckerHardening(Base): |
| 75 | def test_output_hash_covers_written_bytes(self): |
| 76 | repo = os.path.join(self.tmp, "repo") |
| 77 | sha = make_repo(repo, {"LICENSE": "MIT License\n", "a.py": "print(1)\n"}) |
| 78 | doc, by, out = self.run_checks(repo, sha, "C2") |
| 79 | c = by["C2-license"] |
| 80 | with open(os.path.join(out, "C2-license.json"), "rb") as fh: |
| 81 | self.assertEqual(hashlib.sha256(fh.read()).hexdigest(), c["output_sha256"]) |
| 82 | |
| 83 | def test_fork_root_license_outside_diff_is_seen(self): |
| 84 | repo = os.path.join(self.tmp, "fork") |
| 85 | base = make_repo(repo, {"License.txt": "Apache License, Version 2.0\n", "up.c": "int x;\n"}) |
| 86 | head = commit(repo, {"ours.c": "int y;\n"}) |
| 87 | doc = checks.run_all(repo, head, "f", os.path.join(self.tmp, "out-fork"), fork_base=base, |
| 88 | tools=self.tools, only="C2", lock_path=self.lock_path) |
| 89 | c2 = {c["check"]: c for c in doc["checks"]}["C2-license"] |
| 90 | self.assertEqual(c2["summary"]["root"], {"License.txt": "Apache-2.0"}) |
| 91 | self.assertEqual(c2["status"], "PASS") |
| 92 | # negative control: no licence anywhere in the tree is still flagged |
| 93 | bare = os.path.join(self.tmp, "bare") |
| 94 | sha = make_repo(bare, {"a.c": "int z;\n"}) |
| 95 | doc, by, out = self.run_checks(bare, sha, "C2") |
| 96 | self.assertEqual(by["C2-license"]["status"], "FLAGGED") |
| 97 | |
| 98 | def test_license_signature_survives_reflow(self): |
| 99 | self.assertEqual(checks.ident("Redistribution and use in source\n and binary forms"), "BSD") |
| 100 | self.assertEqual(checks.ident("See docs/license.rst"), "UNRECOGNIZED") # negative control |
| 101 | |
| 102 | def test_license_ident_takes_spdx_and_earliest_signature(self): |
| 103 | dual = ("x is dual-licensed\nSPDX-License-Identifier: MIT OR GPL-2.0-only\n\nMIT License\n" |
| 104 | "Permission is hereby granted, free of charge\nGNU GENERAL PUBLIC LICENSE\n" |
| 105 | "use the GNU Lesser General Public License instead\n") |
| 106 | self.assertEqual(checks.ident(dual), "MIT OR GPL-2.0-only") |
| 107 | gpl = "GNU GENERAL PUBLIC LICENSE\nVersion 2\n... use the GNU Lesser General Public License instead.\n" |
| 108 | self.assertEqual(checks.ident(gpl), "GPL") # list order would have said LGPL |
| 109 | self.assertEqual(checks.ident("Apache License\nVersion 2.0, January 2004\n"), "Apache-2.0") |
| 110 | |
| 111 | def test_spdx_tag_quoted_in_prose_is_not_a_header(self): |
| 112 | prose = "They carry SPDX-License-Identifier: GPL-2.0-only and remain under\nGPL-2.0-only.\n" |
| 113 | self.assertEqual(checks.SPDX_RE.findall(prose), []) |
| 114 | self.assertEqual(checks.SPDX_RE.findall("/* SPDX-License-Identifier: GPL-2.0-only */\n"), |
| 115 | ["GPL-2.0-only"]) # negative control: a real header still parses |
| 116 | self.assertEqual(checks.SPDX_RE.findall("# SPDX-License-Identifier: (MIT OR Apache-2.0) AND BSD-3-Clause\n"), |
| 117 | ["(MIT OR Apache-2.0) AND BSD-3-Clause"]) |
| 118 | self.assertEqual(checks.SPDX_RE.findall("// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"), |
| 119 | ["Apache-2.0 WITH LLVM-exception"]) |
| 120 | |
| 121 | def test_custom_license_wrapping_apache_text_is_unrecognized(self): |
| 122 | wrapper = ("Sovereign Resource Commons License 1.0 (SRCL-1.0)\nAll rights reserved.\n" |
| 123 | "Licensed under the Apache License, Version 2.0 subject to conditions precedent.\n") |
| 124 | self.assertEqual(checks.ident(wrapper), "UNRECOGNIZED") |
| 125 | self.assertEqual(checks.ident("MIT License\nCopyright (c) 2026 X\nPermission is hereby granted, free of charge"), |
| 126 | "MIT") # negative control: an unversioned title before the signature is not a wrapper |
| 127 | repo = os.path.join(self.tmp, "wrapper") |
| 128 | sha = make_repo(repo, {"LICENSE": wrapper, "License-History.txt": "old terms\n", |
| 129 | "Cargo.toml": '[package]\nlicense = "SRCL-1.0"\n'}) |
| 130 | doc, by, out = self.run_checks(repo, sha, "C2") |
| 131 | with open(os.path.join(out, "C2-license.json")) as fh: |
| 132 | kinds = [i["kind"] for i in json.load(fh)["items"]] |
| 133 | self.assertEqual(kinds, ["unrecognized-root-license", "unrecognized-root-license", "manifest-differs-from-root"]) |
| 134 | |
| 135 | def test_nested_license_governs_its_subtree(self): |
| 136 | agpl = "GNU AFFERO GENERAL PUBLIC LICENSE\n" |
| 137 | repo = os.path.join(self.tmp, "nested") |
| 138 | sha = make_repo(repo, {"LICENSE": "Apache License, Version 2.0\n", |
| 139 | "adapters/a/LICENSE": agpl, |
| 140 | "adapters/a/Cargo.toml": '[package]\nlicense = "AGPL-3.0-or-later"\n', |
| 141 | "tools/b/Cargo.toml": '[package]\nlicense = "AGPL-3.0-or-later"\n'}) |
| 142 | doc, by, out = self.run_checks(repo, sha, "C2") |
| 143 | with open(os.path.join(out, "C2-license.json")) as fh: |
| 144 | items = json.load(fh)["items"] |
| 145 | # adapters/a is covered by its own LICENSE; tools/b is the negative control |
| 146 | self.assertEqual(items, [{"kind": "manifest-differs-from-root", "license": "AGPL-3.0-or-later", |
| 147 | "files": ["tools/b/Cargo.toml"]}]) |
| 148 | |
| 149 | def test_fork_link_to_unchanged_upstream_file_resolves(self): |
| 150 | repo = os.path.join(self.tmp, "fork") |
| 151 | base = make_repo(repo, {"docs/guide.md": "# g\n"}) |
| 152 | head = commit(repo, {"NOTES.md": "[g](docs/guide.md) [gone](docs/missing.md) `alloc[T](n)`\n" |
| 153 | "See [`docs/also-gone.md`](docs/also-gone.md).\n"}) |
| 154 | doc = checks.run_all(repo, head, "f", os.path.join(self.tmp, "out-fork4"), fork_base=base, |
| 155 | tools=self.tools, only="C4", lock_path=self.lock_path) |
| 156 | c4 = {c["check"]: c for c in doc["checks"]}["C4-internal-links"] |
| 157 | with open(os.path.join(self.tmp, "out-fork4", "C4-internal-links.json")) as fh: |
| 158 | targets = [i["target"] for i in json.load(fh)] |
| 159 | self.assertEqual(targets, ["docs/missing.md", "docs/also-gone.md"]) # missing links are the negative control |
| 160 | self.assertEqual(c4["count"], 2) |
| 161 | |
| 162 | def test_missing_scanner_is_not_run(self): |
| 163 | repo = os.path.join(self.tmp, "repo") |
| 164 | sha = make_repo(repo, {"Cargo.lock": "# lock\n"}) |
| 165 | old = os.environ["PATH"] |
| 166 | gitbin = os.path.join(self.tmp, "gitonly") |
| 167 | os.makedirs(gitbin) |
| 168 | os.symlink(shutil.which("git"), os.path.join(gitbin, "git")) |
| 169 | os.environ["PATH"] = gitbin # git, and no osv-scanner anywhere |
| 170 | try: |
| 171 | _, by, _ = self.run_checks(repo, sha, "C11") |
| 172 | finally: |
| 173 | os.environ["PATH"] = old |
| 174 | self.assertEqual(by["C11-dep-advisories"]["status"], "NOT_RUN") |
| 175 | |
| 176 | def test_unpinned_version_is_not_run(self): |
| 177 | repo = os.path.join(self.tmp, "repo") |
| 178 | sha = make_repo(repo, {"Cargo.lock": "# lock\n"}) |
| 179 | fake_tool(os.path.join(self.tools, "osv-scanner"), "osv-scanner version: 2.7.01", "echo '{\"results\":[]}'") |
| 180 | _, by, _ = self.run_checks(repo, sha, "C11") |
| 181 | self.assertEqual(by["C11-dep-advisories"]["status"], "NOT_RUN") |
| 182 | self.assertIn("pins 2.7.0", by["C11-dep-advisories"]["note"]) |
| 183 | # negative control: the pinned version runs |
| 184 | fake_tool(os.path.join(self.tools, "osv-scanner"), "osv-scanner version: 2.7.0", "echo '{\"results\":[]}'") |
| 185 | _, by, _ = self.run_checks(repo, sha, "C11") |
| 186 | self.assertEqual(by["C11-dep-advisories"]["status"], "PASS") |
| 187 | |
| 188 | def test_osv_exit_codes(self): |
| 189 | repo = os.path.join(self.tmp, "repo") |
| 190 | sha = make_repo(repo, {"Cargo.lock": "# lock\n"}) |
| 191 | tool = os.path.join(self.tools, "osv-scanner") |
| 192 | vuln = ('{"results":[{"source":{"path":"\'"$6"\'/Cargo.lock"},"packages":[{"package":{"name":"bincode",' |
| 193 | '"version":"1.3.3"},"vulnerabilities":[{"id":"RUSTSEC-2025-0141"}]}]}]}') |
| 194 | cases = [("echo '{\"results\":[]}'; exit 0", "PASS"), |
| 195 | ("echo '%s'; exit 1" % vuln, "FLAGGED"), |
| 196 | ("echo '{\"results\":[]}'; exit 1", "NOT_RUN"), # says vulnerable, lists none |
| 197 | ("echo 'no packages' >&2; exit 128", "NOT_RUN"), |
| 198 | ("exit 2", "NOT_RUN")] |
| 199 | for body, want in cases: |
| 200 | with self.subTest(body=body): |
| 201 | fake_tool(tool, "osv-scanner version: 2.7.0", body) |
| 202 | _, by, _ = self.run_checks(repo, sha, "C11") |
| 203 | self.assertEqual(by["C11-dep-advisories"]["status"], want) |
| 204 | |
| 205 | def test_semgrep_list_error_type_is_incomplete(self): |
| 206 | self.assertEqual(checks.semgrep_error_type({"type": ["PartialParsing", [{"line": 1}]]}), "PartialParsing") |
| 207 | self.assertEqual(checks.semgrep_error_type({"type": "Timeout"}), "Timeout") |
| 208 | repo = os.path.join(self.tmp, "repo") |
| 209 | sha = make_repo(repo, {"a.py": "print(1)\n"}) |
| 210 | cfg = b"rules: []\n" |
| 211 | with open(os.path.join(self.tools, "semgrep-config.yaml"), "wb") as fh: |
| 212 | fh.write(cfg) |
| 213 | self.lock["semgrep_rules"]["config_sha256"] = hashlib.sha256(cfg).hexdigest() |
| 214 | self.write_lock() |
| 215 | sg = os.path.join(self.tools, "venv", "bin", "semgrep") |
| 216 | write_out = ('while [ "$1" != "--output" ]; do shift; done; out=$2; echo \'%s\' > "$out"; exit 0') |
| 217 | err = '{"results":[],"errors":[{"type":["PartialParsing",[{"line":1}]]}],"paths":{"skipped":[]}}' |
| 218 | fake_tool(sg, "1.182.1", write_out % err) |
| 219 | _, by, _ = self.run_checks(repo, sha, "C5") |
| 220 | self.assertEqual(by["C5-semgrep"]["status"], "INCOMPLETE") |
| 221 | # negative control: no errors, no skips -> PASS |
| 222 | fake_tool(sg, "1.182.1", write_out % '{"results":[],"errors":[],"paths":{"skipped":[]}}') |
| 223 | _, by, _ = self.run_checks(repo, sha, "C5") |
| 224 | self.assertEqual(by["C5-semgrep"]["status"], "PASS") |
| 225 | # a config that is not the pinned one is refused |
| 226 | with open(os.path.join(self.tools, "semgrep-config.yaml"), "wb") as fh: |
| 227 | fh.write(b"rules: [] # edited\n") |
| 228 | _, by, _ = self.run_checks(repo, sha, "C5") |
| 229 | self.assertEqual(by["C5-semgrep"]["status"], "NOT_RUN") |
| 230 | |
| 231 | def test_crashed_check_is_not_run(self): |
| 232 | repo = os.path.join(self.tmp, "repo") |
| 233 | sha = make_repo(repo, {"a.py": "print(1)\n"}) |
| 234 | orig = checks.CHECKS["C2"] |
| 235 | checks.CHECKS["C2"] = lambda ctx: 1 / 0 |
| 236 | try: |
| 237 | _, by, _ = self.run_checks(repo, sha, "C2") |
| 238 | finally: |
| 239 | checks.CHECKS["C2"] = orig |
| 240 | self.assertEqual(by["C2-license"]["status"], "NOT_RUN") |
| 241 | self.assertIn("ZeroDivisionError", by["C2-license"]["note"]) |
| 242 | |
| 243 | |
| 244 | class TestForkLoc(Base): |
| 245 | def test_modified_upstream_file_counts_added_lines_only(self): |
| 246 | repo = os.path.join(self.tmp, "fork") |
| 247 | base = make_repo(repo, {"up.py": "".join("x%d = %d\n" % (i, i) for i in range(100))}) |
| 248 | head = commit(repo, {"up.py": "".join("x%d = %d\n" % (i, i) for i in range(98)) + "y = 1\n\nz = 2\n", |
| 249 | "ours.py": "a = 1\nb = 2\n"}) |
| 250 | scc = shutil.which("scc") or os.path.join(os.environ.get("AUDIT_TOOLS", ""), "scc") |
| 251 | if not os.path.exists(scc): |
| 252 | self.skipTest("scc not installed (NOT_RUN)") |
| 253 | doc = loc.count(repo, head, "fork", base, scc, []) |
| 254 | by = {f["path"]: f for f in doc["files"]} |
| 255 | self.assertEqual(by["up.py"]["code"], 2) # y, z; the two deleted lines are not subtracted |
| 256 | self.assertTrue(by["up.py"]["fork_modified"]) |
| 257 | self.assertEqual(by["ours.py"]["code"], 2) |
| 258 | self.assertNotIn("fork_modified", by["ours.py"]) |
| 259 | |
| 260 | def test_org_totals_dedupe_identical_blobs(self): |
| 261 | f = {"category": "first_party", "language": "Python", "executable": True, "code": 10, "blob": "b1"} |
| 262 | docs = [{"repository": "a", "files": [dict(f, path="x.py")]}, {"repository": "b", "files": [dict(f, path="y.py")]}] |
| 263 | t = loc.org_totals(docs) |
| 264 | self.assertEqual(t["first_party_executable_code"], 20) |
| 265 | self.assertEqual(t["distinct_first_party_executable_code"], 10) |
| 266 | |
| 267 | |
| 268 | def write_stamp(root, ident, st, files): |
| 269 | d = os.path.join(root, ident, st["subject"]["commit"]) |
| 270 | os.makedirs(d, exist_ok=True) |
| 271 | for p, data in files.items(): |
| 272 | os.makedirs(os.path.dirname(os.path.join(d, p)), exist_ok=True) |
| 273 | with open(os.path.join(d, p), "wb") as fh: |
| 274 | fh.write(data) |
| 275 | with open(os.path.join(d, "stamp.json"), "w") as fh: |
| 276 | json.dump(st, fh, indent=1) |
| 277 | return d |
| 278 | |
| 279 | |
| 280 | class TestStamp(Base): |
| 281 | def setUp(self): |
| 282 | super().setUp() |
| 283 | self.repo = os.path.join(self.tmp, "repo") |
| 284 | self.sha = make_repo(self.repo, {"LICENSE": "MIT License\n", "a.py": "print(1)\n"}) |
| 285 | self.doc, by, out = self.run_checks(self.repo, self.sha, "C2") |
| 286 | with open(os.path.join(out, "C2-license.json"), "rb") as fh: |
| 287 | self.c2 = fh.read() |
| 288 | self.report = b"# review\nno findings\n" |
| 289 | self.st = { |
| 290 | "schema": "aien-audit-stamp/1", |
| 291 | "subject": {"visibility": "public", "repository": "repo", "kind": "FIRST-PARTY", "commit": self.sha, |
| 292 | "tree": sh("git", "rev-parse", self.sha + "^{tree}", cwd=self.repo)}, |
| 293 | "deterministic_checked": {"mode": "full-tree", "fork_base": None, "checker": self.doc["checker"], |
| 294 | "tools_lock_sha256": self.doc["tools_lock_sha256"], |
| 295 | # every FLAGGED item counted as triaged, as a reviewer would record it |
| 296 | "checks": [dict(c, triaged=c["count"] or 0) for c in self.doc["checks"]]}, |
| 297 | "model_reviewed": [{"agent_role": "reviewer", "model": "m", "effort": "high", "files_covered": 2, |
| 298 | "files_total": 2, "report": "reviews/r.md", |
| 299 | "report_sha256": hashlib.sha256(self.report).hexdigest(), "complete": True}], |
| 300 | "findings": [], "not_run": [], "verdict": "PASS", "self_reported": False} |
| 301 | self.stamps = os.path.join(self.tmp, "stamps") |
| 302 | |
| 303 | def put(self, st=None, files=None, ident="repo"): |
| 304 | return write_stamp(self.stamps, ident, st or self.st, |
| 305 | files or {"checks/C2-license.json": self.c2, "reviews/r.md": self.report}) |
| 306 | |
| 307 | def test_valid_stamp(self): |
| 308 | self.assertEqual(stamp.validate(self.put()), []) |
| 309 | |
| 310 | def test_tampered_check_output(self): |
| 311 | self.assertTrue(any("does not match" in e for e in |
| 312 | stamp.validate(self.put(files={"checks/C2-license.json": self.c2 + b" ", |
| 313 | "reviews/r.md": self.report})))) |
| 314 | |
| 315 | def test_verdict_better_than_contents(self): |
| 316 | self.st["not_run"] = [{"item": "C1-secrets", "reason": "gitleaks missing"}] |
| 317 | self.assertTrue(any("better than the contents allow" in e for e in stamp.validate(self.put()))) |
| 318 | self.st["verdict"] = "PASS-WITH-NOTES" # negative control |
| 319 | self.assertEqual(stamp.validate(self.put()), []) |
| 320 | |
| 321 | def test_open_high_finding_forces_fail(self): |
| 322 | self.st["verdict"] = "PASS-WITH-NOTES" |
| 323 | self.st["findings"] = [{"id": "F1", "source": "MODEL", "severity": "high", "status": "OPEN"}] |
| 324 | self.assertTrue(any("FAIL" in e for e in stamp.validate(self.put()))) |
| 325 | |
| 326 | def test_incomplete_review_cannot_claim_complete(self): |
| 327 | self.st["model_reviewed"][0]["files_covered"] = 1 |
| 328 | self.assertTrue(any("marked complete" in e for e in stamp.validate(self.put()))) |
| 329 | |
| 330 | def test_forbidden_words(self): |
| 331 | for w in ("pristine", "Approved", "certified"): |
| 332 | with self.subTest(w=w): |
| 333 | self.st["note"] = "this repo is %s" % w |
| 334 | self.assertTrue(any("forbidden word" in e for e in stamp.validate(self.put()))) |
| 335 | |
| 336 | def test_fork_requires_fork_diff(self): |
| 337 | self.st["subject"]["kind"] = "UPSTREAM-FORK" |
| 338 | self.st["subject"]["upstream"] = {"repository": "up/repo", "base_sha": "a" * 40} |
| 339 | self.assertTrue(any("fork-diff" in e for e in stamp.validate(self.put()))) |
| 340 | |
| 341 | def test_private_stamp_must_not_leak_name(self): |
| 342 | salts = {"private-01": {"name": "secret-thing", "salt": "ab" * 32}} |
| 343 | salts["private-01"]["commitment"] = stamp.commitment(salts["private-01"]["salt"], "secret-thing") |
| 344 | sp = os.path.join(self.tmp, "salts.json") |
| 345 | with open(sp, "w") as fh: |
| 346 | json.dump(salts, fh) |
| 347 | sub = self.st["subject"] |
| 348 | del sub["repository"] |
| 349 | sub.update(visibility="private", anonymous_id="private-01", name_commitment=salts["private-01"]["commitment"]) |
| 350 | self.st["self_reported"] = True |
| 351 | self.assertEqual(stamp.validate(self.put(ident="private-01"), sp), []) |
| 352 | self.st["note"] = "see secret-thing for context" |
| 353 | self.assertTrue(any("private repository name" in e for e in stamp.validate(self.put(ident="private-01"), sp))) |
| 354 | del self.st["note"] |
| 355 | sub["name_commitment"] = "0" * 64 |
| 356 | self.assertTrue(any("name_commitment" in e for e in stamp.validate(self.put(ident="private-01"), sp))) |
| 357 | |
| 358 | def test_rerun_reproduces_and_detects_substitution(self): |
| 359 | d = self.put() |
| 360 | code, msgs = verify.rerun(d, self.repo, "", self.tools, lock_path=self.lock_path) |
| 361 | self.assertEqual((code, msgs), (0, [])) |
| 362 | # negative control: a stamp whose recorded output was swapped (consistently, so the hash |
| 363 | # check alone passes) must fail the rerun |
| 364 | fake = b'{"swapped":true}\n' |
| 365 | self.st["deterministic_checked"]["checks"][0]["output_sha256"] = hashlib.sha256(fake).hexdigest() |
| 366 | d = self.put(files={"checks/C2-license.json": fake, "reviews/r.md": self.report}) |
| 367 | self.assertEqual(stamp.validate(d), []) |
| 368 | code, msgs = verify.rerun(d, self.repo, "", self.tools, lock_path=self.lock_path) |
| 369 | self.assertEqual(code, 1) |
| 370 | |
| 371 | |
| 372 | class TestStatus(Base): |
| 373 | def test_stale_sha_negative_control(self): |
| 374 | repo = os.path.join(self.tmp, "repo") |
| 375 | a = make_repo(repo, {"a.py": "1\n"}) |
| 376 | b = commit(repo, {"a.py": "2\n"}) |
| 377 | stamps = os.path.join(self.tmp, "stamps") |
| 378 | write_stamp(stamps, "repo", {"subject": {"visibility": "public", "repository": "repo", "commit": a}, |
| 379 | "verdict": "PASS-WITH-NOTES", "stamped_at": "2026-10-26"}, {}) |
| 380 | self.assertEqual(status.compute(stamps, "org", {"repo": a})["stamps"][0]["state"], "CURRENT") |
| 381 | self.assertEqual(status.compute(stamps, "org", {"repo": b})["stamps"][0]["state"], "STALE") |
| 382 | self.assertEqual(status.compute(stamps, "org", {})["stamps"][0]["state"], "UNKNOWN") |
| 383 | |
| 384 | def test_private_is_not_checkable_and_older_superseded(self): |
| 385 | stamps = os.path.join(self.tmp, "stamps") |
| 386 | write_stamp(stamps, "private-01", {"subject": {"visibility": "private", "commit": "1" * 40}, |
| 387 | "verdict": "PASS-WITH-NOTES", "stamped_at": "2026-10-26"}, {}) |
| 388 | write_stamp(stamps, "repo", {"subject": {"visibility": "public", "commit": "2" * 40}, "stamped_at": "2026-10-01"}, {}) |
| 389 | write_stamp(stamps, "repo", {"subject": {"visibility": "public", "commit": "3" * 40}, "stamped_at": "2026-10-26"}, {}) |
| 390 | rows = status.compute(stamps, "org", {"repo": "3" * 40})["stamps"] |
| 391 | self.assertEqual([(r["id"], r["state"]) for r in rows], |
| 392 | [("private-01", "NOT_CHECKABLE"), ("repo", "CURRENT"), ("repo", "SUPERSEDED")]) |
| 393 | |
| 394 | |
| 395 | if __name__ == "__main__": |
| 396 | unittest.main() |