#!/usr/bin/env python3 """Pure lexical paths: byte oracles, CLI/AIR/package parity and authority.""" import json from pathlib import Path import subprocess import tempfile REPO = Path(__file__).resolve().parents[2] AJI = REPO / "zig-out/bin/aji" CASES = REPO / "tests/conformance/source/path" checks = 0 def cli(*args, code=0, **kwargs): global checks run = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True, **kwargs) assert run.returncode == code, (args, run.returncode, run.stdout, run.stderr) checks += 1 return run.stdout def returned(path, *extra): record = json.loads(cli("run", "--fuel", "2000000", *extra, path)) assert record["status"] == "returned" and record["value"] == 42, record return record example = REPO / "examples/std/path.aji" record = returned(example) canonical = cli("lower", example) assert cli("lower", example) == canonical report = json.loads(cli("report", example)) assert report["entry"]["capabilities"] == [] and report["unsafe"]["operations"] == 0 and report["unsafe"]["trusted_functions"] == [], report assert all(function["allocations"] == 0 for function in report["functions"]), report for name in ("basic", "all_bytes", "canceled_capacity", "disjoint", "malformed_unchanged", "model", "shared", "loan_join"): returned(CASES / (name + ".aji")) for name, expected in (("alias", ["E2101", "E2101"]), ("join_alias", ["E2101", "E2101"]), ("overlap", ["E2101", "E2101"]), ("loan", ["E4101"]), ("wrong_slice", ["E7005"]), ("wrong_input", ["E7005"]), ("wrong_range", ["E7005"]), ("missing_import", ["E7002"])): commands = ("check", "lower", "report", "run", "explore") if expected[0] in ("E7002", "E7005") else ("check", "report", "run", "explore") for command in commands: path = CASES / (name + ".aji") errors = [json.loads(line) for line in cli(command, path, code=1).splitlines()] assert [error["code"] for error in errors] == expected, errors assert all(Path(error["file"]).resolve() == path.resolve() for error in errors), errors returned(CASES / "authority.aji", "--grant", "src=fs.read:/allowed", "--vfs", "/denied/file=secret") trap = json.loads(cli("run", CASES / "range_bounds.aji", code=4)) assert trap["code"] == "T0008" and trap["source_location"]["file"].endswith("range_bounds.aji"), trap with tempfile.TemporaryDirectory(prefix="aji-path-") as directory: root = Path(directory) air = root / "result.air" air.write_text(canonical) cli("check", air) assert returned(air) == record entry, manifest, result = root / "main.aji", root / "aji.toml", root / "hosted.json" entry.write_text(example.read_text()) manifest.write_text('[package]\nname = "path"\nversion = "1"\nentry = "main.aji"\n') assert json.loads(cli("run", "--manifest", manifest)) == record assert cli("lower", "--manifest", manifest) == canonical entry.write_text(example.read_text().replace('import std.path;\nimport std.bytes;', 'import std.bytes;\nimport std.path;')) assert cli("lower", "--manifest", manifest) == canonical entry.write_text(example.read_text().replace('fn main()', 'fn __aji_std_path_join() -> u64 effects [] { return 0; }\nfn main()')) returned(entry) assert "@__aji_std_path_join_1" in cli("lower", entry) # Pinned std sources do not depend on the process working directory. assert json.loads(cli("run", example, cwd=root)) == record cli("run", "--profile", "hosted", "--result-file", result, example) assert json.loads(result.read_text()) == {"status": "returned", "value": 42} # Lexical output is still checked against the entry grant by hosted fs. # The denied file really exists, but no open/read is authorized. denied, allowed = root / "denied", root / "allowed" denied.mkdir(); allowed.mkdir(); (denied / "file").write_text("secret") source = (CASES / "authority.aji").read_text().replace('b"/allowed"', f'b"{allowed}"').replace('b"/denied/file"', f'b"{denied}/file"').replace('[u8;16]', '[u8;128]').replace('[255u8;16]', '[255u8;128]') entry.write_text(source) cli("run", "--profile", "hosted", "--grant", f"src=fs.read:{allowed}", "--result-file", result, entry) assert json.loads(result.read_text())["value"] == 42 # Pure path transforms agree across profiles, including all non-NUL bytes. for name in ("all_bytes", "canceled_capacity", "disjoint", "malformed_unchanged"): cli("run", "--profile", "hosted", "--fuel", "2000000", "--result-file", result, CASES / (name + ".aji")) assert json.loads(result.read_text())["value"] == 42 for seed in (0, 7, 19): assert returned(example, "--seed", str(seed)) == record cli("explore", "--max-runs", "12", CASES / "shared.aji") print(f"source path integration passed ({checks} checks; pure profiles, byte ranges and independent authority)")