| 1 | import argparse |
| 2 | import hashlib |
| 3 | import json |
| 4 | import pathlib |
| 5 | import re |
| 6 | import stat |
| 7 | import sys |
| 8 | import tomllib |
| 9 | |
| 10 | BASE = pathlib.Path(__file__).resolve().parent |
| 11 | ROOT = BASE.parents[1] |
| 12 | SOURCE = ROOT / "experiments/rust-gzip-file-job" |
| 13 | HEAD = "4c6ae4b3a7ab12a992598e94e7b02f3a167fbbd4" |
| 14 | TREE = "9a01a37623b163c35ca4ff25963b2e11c61a0c35" |
| 15 | SOURCE_MANIFEST = "ef32ad5306d2d98751836846f631f66c562e4b55caf5d3422e120305aa78b37f" |
| 16 | MODULES = ("success", "invalid", "limits", "control", "namespace", "concurrency") |
| 17 | FILES = { |
| 18 | "Cargo.toml", "Cargo.lock", "main.rs", "support.rs", "fixtures.rs", |
| 19 | "success.rs", "invalid.rs", "limits.rs", "control.rs", "namespace.rs", "concurrency.rs", |
| 20 | "source-pins.json", "contract.json", "run-plan.json", "static_check.py", |
| 21 | "static_check_helper_tests.py", |
| 22 | } |
| 23 | GUARD = "최적화 모드에서는 정적 검사를 실행할 수 없음" |
| 24 | |
| 25 | |
| 26 | def require(condition, message): |
| 27 | if not condition: |
| 28 | raise RuntimeError(message) |
| 29 | |
| 30 | |
| 31 | def digest(raw): |
| 32 | return hashlib.sha256(raw).hexdigest() |
| 33 | |
| 34 | |
| 35 | def parse_manifest(text, expected_paths): |
| 36 | rows = {} |
| 37 | for line in text.splitlines(): |
| 38 | match = re.fullmatch(r"([0-9a-f]{64}) ([A-Za-z0-9_./-]+)", line) |
| 39 | require(match is not None, "잘못된 manifest 행") |
| 40 | value, name = match.groups() |
| 41 | path = pathlib.PurePosixPath(name) |
| 42 | require(not path.is_absolute() and ".." not in path.parts |
| 43 | and str(path) == name and name not in rows, "중복 또는 잘못된 경로") |
| 44 | rows[name] = value |
| 45 | require(set(rows) == set(expected_paths), "manifest 경로 불일치") |
| 46 | return rows |
| 47 | |
| 48 | |
| 49 | def hashes(): |
| 50 | result = {} |
| 51 | for name in sorted(FILES | {"SHA256SUMS"}): |
| 52 | path = BASE / name |
| 53 | require(path.resolve() == path and stat.S_ISREG(path.lstat().st_mode), "일반 시험 파일 필요") |
| 54 | require(not path.stat().st_mode & 0o111, "시험 파일 mode 불일치") |
| 55 | result[name] = digest(path.read_bytes()) |
| 56 | return result |
| 57 | |
| 58 | |
| 59 | def verify_sources(): |
| 60 | pins = json.loads((BASE / "source-pins.json").read_text()) |
| 61 | require(pins["head"] == HEAD and pins["tree"] == TREE and len(pins["files"]) == 39, "고정 source 기준 불일치") |
| 62 | require(len({row["path"] for row in pins["files"]}) == 39, "source 경로 중복") |
| 63 | roots = ("experiments/rust-gzip-file-job/", "experiments/rust-file-snapshot/", |
| 64 | "experiments/rust-file-publication/", "experiments/rust-memory-gzip-reader/") |
| 65 | result = {} |
| 66 | for row in pins["files"]: |
| 67 | path = ROOT / row["path"] |
| 68 | require(row["path"].startswith(roots) and path.resolve() == path, "source 경로 이탈") |
| 69 | info = path.lstat() |
| 70 | require(stat.S_ISREG(info.st_mode) and not info.st_mode & 0o111, "source mode 불일치") |
| 71 | raw = path.read_bytes() |
| 72 | require(digest(raw) == row["sha256"], "source SHA256 불일치") |
| 73 | blob = hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest() |
| 74 | require(blob == row["git_blob"] and row["mode"] == "100644", "source blob 불일치") |
| 75 | result[row["path"]] = digest(raw) |
| 76 | require(digest((SOURCE / "SHA256SUMS").read_bytes()) == SOURCE_MANIFEST, "source manifest 불일치") |
| 77 | return result |
| 78 | |
| 79 | |
| 80 | def main(): |
| 81 | if sys.flags.optimize: |
| 82 | raise RuntimeError(GUARD) |
| 83 | parser = argparse.ArgumentParser() |
| 84 | parser.add_argument("--manifest-sha256", required=True) |
| 85 | parser.add_argument("--metadata", type=pathlib.Path) |
| 86 | args = parser.parse_args() |
| 87 | before = hashes() |
| 88 | require(before["SHA256SUMS"] == args.manifest_sha256, "시험 manifest 불일치") |
| 89 | rows = parse_manifest((BASE / "SHA256SUMS").read_text(), FILES) |
| 90 | require(all(before[name] == value for name, value in rows.items()), "시험 파일 변경") |
| 91 | sources = verify_sources() |
| 92 | contract = json.loads((BASE / "contract.json").read_text()) |
| 93 | require(contract["source_head"] == HEAD and contract["source_tree"] == TREE, "계약 head 불일치") |
| 94 | require(contract["source_manifest_sha256"] == SOURCE_MANIFEST, "계약 manifest 불일치") |
| 95 | require(set(contract["deliverables"]) == FILES | {"SHA256SUMS"}, "배포 파일 집합 불일치") |
| 96 | names = [] |
| 97 | for module in MODULES: |
| 98 | text = (BASE / (module + ".rs")).read_text() |
| 99 | found = re.findall(r"#\[test\]\s*fn ([a-z0-9_]+)\(", text) |
| 100 | require(len(found) == text.count("#[test]"), "시험 선언 분석 실패") |
| 101 | names.extend(module + "::" + name for name in found) |
| 102 | require(len(names) == len(set(names)) == contract["declared_test_count"] == 62, "선언 수 불일치") |
| 103 | require(sorted(names) == contract["test_names"] == sorted(contract["evidence_classes"]), "이름 집합 불일치") |
| 104 | for name in FILES: |
| 105 | raw = (BASE / name).read_bytes() |
| 106 | require(b"\xc2\xb7" not in raw, "가운뎃점 금지") |
| 107 | for path in BASE.glob("*.rs"): |
| 108 | text = path.read_text() |
| 109 | require("#[ignore" not in text and "#[should_panic" not in text, "시험 제외 금지") |
| 110 | require(not re.search(r"include!|#\[path|Command::|execveat|\bchmod\b|\bchown\b|\bmount\b", text), "범위 밖 Rust 경계") |
| 111 | support = (BASE / "support.rs").read_text() |
| 112 | require("impl Drop for Sandbox" not in support, "실패 증거 자동 정리 금지") |
| 113 | require("owned_descriptors(&self.root).is_empty()" in support, "소유 fd 점검 누락") |
| 114 | main = (BASE / "main.rs").read_text() |
| 115 | require(main.startswith('#![cfg(target_os = "linux")]'), "native Linux 경계 누락") |
| 116 | require(set(re.findall(r"mod ([a-z_]+);", main)) == set(MODULES) | {"support", "fixtures"}, "모듈 집합 불일치") |
| 117 | plan = json.loads((BASE / "run-plan.json").read_text()) |
| 118 | require(plan["target"]["exact_names"] == sorted(names) and plan["target"]["count"] == 62, "실행 계획 이름 불일치") |
| 119 | require(plan["target"]["filtered_per_case"] == 61, "filtered 수 불일치") |
| 120 | require(plan["bounds"]["per_case_seconds"] == 30 and plan["bounds"]["channel_wait_seconds"] == 5, "시간 상한 변경") |
| 121 | require(plan["bounds"]["compile_seconds"] == 120 and plan["bounds"]["per_command_log_bytes"] == 8388608, "실행 상한 변경") |
| 122 | manifest = tomllib.loads((BASE / "Cargo.toml").read_text()) |
| 123 | require(manifest["package"]["autotests"] is False, "자동 시험 발견 금지") |
| 124 | require(manifest["test"] == [{"name": "file_job_public_api", "path": "main.rs"}], "target 불일치") |
| 125 | require(manifest["dependencies"] == { |
| 126 | "job": {"package": "zipelo-gzip-file-job", "path": "../../experiments/rust-gzip-file-job"}, |
| 127 | "libc": {"version": "=0.2.191", "default-features": False, "features": ["std"]}, |
| 128 | "sha2": {"version": "=0.10.9", "default-features": False, "features": ["std", "force-soft"]}, |
| 129 | }, "직접 의존성 변경") |
| 130 | normal = lambda rows: sorted(rows, key=lambda row: (row["name"], row["version"])) |
| 131 | original = tomllib.loads((SOURCE / "Cargo.lock").read_text())["package"] |
| 132 | locked = tomllib.loads((BASE / "Cargo.lock").read_text())["package"] |
| 133 | package = {"name": "zipelo-gzip-file-job-regressions", "version": "0.1.0", |
| 134 | "dependencies": ["libc", "sha2", "zipelo-gzip-file-job"]} |
| 135 | require(normal(locked) == normal(original + [package]), "기존 lock graph 변경") |
| 136 | require(len([p for p in locked if "source" in p]) == 11, "registry 수 변경") |
| 137 | if args.metadata: |
| 138 | require(args.metadata.resolve().is_relative_to(BASE), "metadata 위치 이탈") |
| 139 | metadata = json.loads(args.metadata.read_text()) |
| 140 | packages = {p["id"]: p for p in metadata["packages"]} |
| 141 | root = packages[metadata["resolve"]["root"]] |
| 142 | require(root["name"] == package["name"], "metadata root 불일치") |
| 143 | require(len(root["targets"]) == 1 and root["targets"][0]["kind"] == ["test"], "target 추가") |
| 144 | require(pathlib.Path(root["targets"][0]["src_path"]) == BASE / "main.rs", "시험 경로 이탈") |
| 145 | for node in metadata["resolve"]["nodes"]: |
| 146 | name = packages[node["id"]]["name"] |
| 147 | if name == "sha2": |
| 148 | require(sorted(node["features"]) == ["force-soft", "std"], "sha2 feature 변경") |
| 149 | if name == "libc": |
| 150 | require(sorted(node["features"]) == ["std"], "libc feature 변경") |
| 151 | require(verify_sources() == sources and hashes() == before, "검사 중 입력 변경") |
| 152 | print(json.dumps({"head": HEAD, "source_files": 39, "declared": len(names), |
| 153 | "rust_compiled": False, "rust_executed": 0, "inputs_unchanged": True})) |
| 154 | |
| 155 | |
| 156 | if __name__ == "__main__": |
| 157 | main() |