| 1 | import argparse |
| 2 | import hashlib |
| 3 | import json |
| 4 | import pathlib |
| 5 | import re |
| 6 | import sys |
| 7 | import tomllib |
| 8 | |
| 9 | BASE = pathlib.Path(__file__).resolve().parent |
| 10 | ROOT = BASE.parents[1] |
| 11 | HEAD = "b7c3d5575943f8caab8bbdcf465c278c3e7059d1" |
| 12 | SOURCE = ROOT / "experiments/rust-file-snapshot" |
| 13 | SOURCE_MANIFEST = "668b1c3f140a033a6a0768b84ec52f111ee5fbfaf707632d2fc9a5a380758120" |
| 14 | FILES = { |
| 15 | "Cargo.toml", "Cargo.lock", "main.rs", "support.rs", "boundaries.rs", |
| 16 | "paths.rs", "mutations.rs", "control.rs", "source-pins.json", |
| 17 | "contract.json", "static_check.py", "static_check_helper_tests.py", |
| 18 | } |
| 19 | MODULES = ("boundaries", "paths", "mutations", "control") |
| 20 | GUARD = "최적화 모드에서는 정적 검사를 실행할 수 없음" |
| 21 | |
| 22 | |
| 23 | def require(condition, message): |
| 24 | if not condition: |
| 25 | raise RuntimeError(message) |
| 26 | |
| 27 | |
| 28 | def digest(raw): |
| 29 | return hashlib.sha256(raw).hexdigest() |
| 30 | |
| 31 | |
| 32 | def parse_manifest(text, expected_paths): |
| 33 | rows = {} |
| 34 | for line in text.splitlines(): |
| 35 | match = re.fullmatch(r"([0-9a-f]{64}) ([A-Za-z0-9_./-]+)", line) |
| 36 | require(match is not None, "잘못된 manifest 행") |
| 37 | value, name = match.groups() |
| 38 | path = pathlib.PurePosixPath(name) |
| 39 | require(not path.is_absolute() and ".." not in path.parts |
| 40 | and str(path) == name and name not in rows, "중복 또는 잘못된 경로") |
| 41 | rows[name] = value |
| 42 | require(set(rows) == set(expected_paths), "manifest 경로 불일치") |
| 43 | return rows |
| 44 | |
| 45 | |
| 46 | def hashes(): |
| 47 | result = {} |
| 48 | for name in sorted(FILES | {"SHA256SUMS"}): |
| 49 | path = BASE / name |
| 50 | require(not path.is_symlink(), "시험 파일 링크 거부") |
| 51 | result[name] = digest(path.read_bytes()) |
| 52 | return result |
| 53 | |
| 54 | |
| 55 | def verify_sources(): |
| 56 | pins = json.loads((BASE / "source-pins.json").read_text()) |
| 57 | require(pins["head"] == HEAD and len(pins["files"]) == 9, "고정 source 기준 불일치") |
| 58 | names = [row["path"] for row in pins["files"]] |
| 59 | require(len(set(names)) == 9, "source 경로 중복") |
| 60 | result = {} |
| 61 | for row in pins["files"]: |
| 62 | path = ROOT / row["path"] |
| 63 | require(path.resolve().is_relative_to(SOURCE) and not path.is_symlink(), "source 경로 이탈") |
| 64 | raw = path.read_bytes() |
| 65 | require(digest(raw) == row["sha256"], "source SHA256 불일치") |
| 66 | blob = hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest() |
| 67 | require(blob == row["git_blob"] and row["mode"] == "100644", "source blob 불일치") |
| 68 | result[row["path"]] = digest(raw) |
| 69 | manifest = (SOURCE / "SHA256SUMS").read_bytes() |
| 70 | require(digest(manifest) == SOURCE_MANIFEST, "source manifest 불일치") |
| 71 | entries = parse_manifest(manifest.decode(), set(names) - {"experiments/rust-file-snapshot/SHA256SUMS"}) |
| 72 | for name, value in entries.items(): |
| 73 | require(digest((ROOT / name).read_bytes()) == value, "source manifest 파일 불일치") |
| 74 | return result |
| 75 | |
| 76 | |
| 77 | def main(): |
| 78 | if sys.flags.optimize: |
| 79 | raise RuntimeError(GUARD) |
| 80 | parser = argparse.ArgumentParser() |
| 81 | parser.add_argument("--manifest-sha256", required=True) |
| 82 | parser.add_argument("--metadata", type=pathlib.Path) |
| 83 | args = parser.parse_args() |
| 84 | before = hashes() |
| 85 | require(before["SHA256SUMS"] == args.manifest_sha256, "시험 manifest 불일치") |
| 86 | rows = parse_manifest((BASE / "SHA256SUMS").read_text(), FILES) |
| 87 | require(all(before[name] == value for name, value in rows.items()), "시험 파일 변경") |
| 88 | sources = verify_sources() |
| 89 | contract = json.loads((BASE / "contract.json").read_text()) |
| 90 | require(contract["source_head"] == HEAD, "계약 head 불일치") |
| 91 | names = [] |
| 92 | for module in MODULES: |
| 93 | source = (BASE / (module + ".rs")).read_text() |
| 94 | found = re.findall(r"#\[test\]\s*fn ([a-z0-9_]+)\(", source) |
| 95 | require(len(found) == source.count("#[test]"), "시험 선언 분석 실패") |
| 96 | names.extend(module + "::" + name for name in found) |
| 97 | require(len(names) == len(set(names)) == contract["declared_test_count"] == 72, "선언 수 불일치") |
| 98 | require(sorted(names) == contract["test_names"] == sorted(contract["evidence_classes"]), "이름 집합 불일치") |
| 99 | for path in BASE.glob("*.rs"): |
| 100 | source = path.read_text() |
| 101 | require("#[ignore" not in source and "#[should_panic" not in source, "시험 제외 금지") |
| 102 | require(not re.search(r"include!|#\[path|Command::|execveat|\bmknod\b", source), "범위 밖 Rust 경계") |
| 103 | manifest = tomllib.loads((BASE / "Cargo.toml").read_text()) |
| 104 | require(manifest["package"]["autotests"] is False, "자동 시험 발견 금지") |
| 105 | require(manifest["test"] == [{"name": "snapshot_public_api", "path": "main.rs"}], "target 불일치") |
| 106 | require(manifest["dependencies"] == { |
| 107 | "snapshot": {"package": "zipelo-file-snapshot", "path": "../../experiments/rust-file-snapshot"}, |
| 108 | "libc": {"version": "=0.2.191", "default-features": False, "features": ["std"]}, |
| 109 | }, "직접 의존성 변경") |
| 110 | normal = lambda rows: sorted(rows, key=lambda row: (row["name"], row["version"])) |
| 111 | original = tomllib.loads((SOURCE / "Cargo.lock").read_text())["package"] |
| 112 | locked = tomllib.loads((BASE / "Cargo.lock").read_text())["package"] |
| 113 | root = {"name": "zipelo-file-snapshot-regressions", "version": "0.1.0", "dependencies": ["libc", "zipelo-file-snapshot"]} |
| 114 | require(normal(locked) == normal(original + [root]), "기존 lock graph 변경") |
| 115 | if args.metadata: |
| 116 | require(args.metadata.resolve().is_relative_to(BASE), "metadata 위치 이탈") |
| 117 | metadata = json.loads(args.metadata.read_text()) |
| 118 | packages = {p["id"]: p for p in metadata["packages"]} |
| 119 | root_package = packages[metadata["resolve"]["root"]] |
| 120 | require(root_package["name"] == root["name"], "metadata root 불일치") |
| 121 | require(len(root_package["targets"]) == 1 and root_package["targets"][0]["kind"] == ["test"], "target 추가") |
| 122 | registry = {(p["name"], p["version"], p["checksum"]) for p in locked if "source" in p} |
| 123 | expected = {(p["name"], p["version"], p["checksum"]) for p in json.loads((SOURCE / "dependencies.json").read_text())["registry_packages"]} |
| 124 | require(registry == expected, "registry graph 변경") |
| 125 | for node in metadata["resolve"]["nodes"]: |
| 126 | package = packages[node["id"]] |
| 127 | if package["name"] == "sha2": |
| 128 | require(sorted(node["features"]) == ["force-soft", "std"], "sha2 feature 변경") |
| 129 | if package["name"] == "libc": |
| 130 | require(sorted(node["features"]) == ["std"], "libc feature 변경") |
| 131 | require(verify_sources() == sources and hashes() == before, "검사 중 입력 변경") |
| 132 | print(json.dumps({"head": HEAD, "source_files": 9, "declared": len(names), |
| 133 | "rust_compiled": False, "rust_executed": 0, "inputs_unchanged": True})) |
| 134 | |
| 135 | |
| 136 | if __name__ == "__main__": |
| 137 | main() |