const std = @import("std");
const support = @import("support.zig");
const addManaged = support.addManaged;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const writeFile = support.writeFile;
test "trust: a versioned before-commit is inert until accepted, and an edit revokes consent" {
// The security property the whole design exists for, driven through the real
// binary. Git refuses to let hooks travel at all (hence husky/lefthook);
// fossil lets them travel AND executes them, with a boundary that happens to
// depend on the transport. Splitting declaration from consent gives the
// project its checks and this machine its veto.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "f.txt", "a\n");
try addManaged(gpa, io, tmp.dir, &.{"f.txt"});
var c0 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c0);
try expectExitDiag(c0.term, 0, c0);
// A tracked declaration — the shape that clones with the repository.
try tmp.dir.createDir(io, ".zit-settings", .default_dir);
try writeFile(io, tmp.dir, ".zit-settings/before-commit", "echo ran-the-check\n");
try addManaged(gpa, io, tmp.dir, &.{".zit-settings/before-commit"});
// 1. Untrusted: the commit is REFUSED, not warned about. A warning would
// let an operator believe they were gated when they were not.
try writeFile(io, tmp.dir, "f.txt", "b\n");
var refused = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "two" });
defer freeResult(gpa, &refused);
try expectExit(refused.term, 1);
try std.testing.expect(std.mem.indexOf(u8, refused.stderr, "has not been accepted here") != null);
// The command is shown, because that is what consent would be given TO.
try std.testing.expect(std.mem.indexOf(u8, refused.stderr, "echo ran-the-check") != null);
// 2. Bare `zit trust` SHOWS and grants nothing: consent to something you
// have not been shown is not consent.
var shown = try runZit(gpa, io, tmp.dir, &.{"trust"});
defer freeResult(gpa, &shown);
try expectExitDiag(shown.term, 0, shown);
try std.testing.expect(std.mem.indexOf(u8, shown.stdout, "echo ran-the-check") != null);
try std.testing.expect(std.mem.indexOf(u8, shown.stdout, "NOT trusted") != null);
var still = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "two" });
defer freeResult(gpa, &still);
try expectExit(still.term, 1); // showing did not grant
// 3. Accepted: the check runs and the commit proceeds.
var granted = try runZit(gpa, io, tmp.dir, &.{ "trust", "--yes" });
defer freeResult(gpa, &granted);
try expectExitDiag(granted.term, 0, granted);
var ok = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "two" });
defer freeResult(gpa, &ok);
try expectExitDiag(ok.term, 0, ok);
try std.testing.expect(std.mem.indexOf(u8, ok.stdout, "ran-the-check") != null);
// 4. THE PROPERTY: an edited declaration revokes consent by itself, because
// the record pins CONTENT. This is the pulled-poison case, and the
// replacement command must not execute -- proven by the sentinel file it
// would have written.
try writeFile(io, tmp.dir, ".zit-settings/before-commit", "echo poisoned > pwned.txt\n");
try writeFile(io, tmp.dir, "f.txt", "c\n");
var changed = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "three" });
defer freeResult(gpa, &changed);
try expectExit(changed.term, 1);
try std.testing.expect(std.mem.indexOf(u8, changed.stderr, "CHANGED since you accepted it") != null);
try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "pwned.txt", .{}));
// 5. A failing check aborts the commit and mints nothing.
try writeFile(io, tmp.dir, ".zit-settings/before-commit", "exit 3\n");
var regrant = try runZit(gpa, io, tmp.dir, &.{ "trust", "--yes" });
defer freeResult(gpa, ®rant);
try expectExitDiag(regrant.term, 0, regrant);
var failed = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "four" });
defer freeResult(gpa, &failed);
try expectExit(failed.term, 1);
try std.testing.expect(std.mem.indexOf(u8, failed.stderr, "nothing was committed") != null);
// 6. --no-checks skips, and says which command it skipped. Loud on purpose:
// withholding an escape hatch does not prevent bypass, it makes people
// delete the declaration, which is permanent and invisible.
var skipped = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "four", "--no-checks" });
defer freeResult(gpa, &skipped);
try expectExitDiag(skipped.term, 0, skipped);
try std.testing.expect(std.mem.indexOf(u8, skipped.stderr, "SKIPPING before-commit") != null);
// 7. Revoking puts it back to refusing.
var revoked = try runZit(gpa, io, tmp.dir, &.{ "trust", "--revoke" });
defer freeResult(gpa, &revoked);
try expectExitDiag(revoked.term, 0, revoked);
try writeFile(io, tmp.dir, "f.txt", "d\n");
var after = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "five" });
defer freeResult(gpa, &after);
try expectExit(after.term, 1);
}
test "trust: a locally set before-commit runs without any trust record" {
// Writing the command IS the consent, so the tier decides: a value this
// operator put in their own repository or global file cannot have been
// supplied by a peer, and asking them to trust their own typing would be
// ceremony rather than safety.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "f.txt", "a\n");
try addManaged(gpa, io, tmp.dir, &.{"f.txt"});
var c0 = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "one" });
defer freeResult(gpa, &c0);
try expectExitDiag(c0.term, 0, c0);
var set = try runZit(gpa, io, tmp.dir, &.{ "settings", "before-commit", "echo local-check" });
defer freeResult(gpa, &set);
try expectExitDiag(set.term, 0, set);
var shown = try runZit(gpa, io, tmp.dir, &.{"trust"});
defer freeResult(gpa, &shown);
try expectExitDiag(shown.term, 0, shown);
try std.testing.expect(std.mem.indexOf(u8, shown.stdout, "no trust record needed") != null);
try writeFile(io, tmp.dir, "f.txt", "b\n");
var c = try runCommitEnv(gpa, io, tmp.dir, .{ .user = "u" }, &.{ "commit", "-m", "two" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
try std.testing.expect(std.mem.indexOf(u8, c.stdout, "local-check") != null);
}