const std = @import("std");
const logstore = @import("zit").logstore;
const storage = @import("zit").storage;
const support = @import("support.zig");
const addManaged = support.addManaged;
const artifactNameByContent = support.artifactNameByContent;
const doomed_body = support.doomed_body;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const shunFixture = support.shunFixture;
const spawnServe = support.spawnServe;
const storeBytesAlloc = support.storeBytesAlloc;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
/// Clear the project code in the config frame, standing in for a repository
/// written before the writer that made it recorded one.
fn clearProjectCode(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !void {
var file = try dir.openFile(io, ".zit", .{ .mode = .read_write });
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.open(gpa, fs.storageFile(), 0);
defer log.deinit(gpa);
var oc = (try log.readConfig(gpa)) orelse return error.TestUnexpectedResult;
defer oc.deinit(gpa);
var next = oc.config;
next.project_code = "";
try log.putConfig(next);
try log.commit();
}
test "verify: a repository with no project code says peers will refuse it, without calling it damage" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
const note = "no project code, so sync peers and forges will refuse it";
var healthy = try runZit(gpa, io, tmp.dir, &.{"verify"});
defer freeResult(gpa, &healthy);
try expectExitDiag(healthy.term, 0, healthy);
try std.testing.expect(std.mem.indexOf(u8, healthy.stderr, note) == null);
try clearProjectCode(gpa, io, tmp.dir);
var res = try runZit(gpa, io, tmp.dir, &.{"verify"});
defer freeResult(gpa, &res);
// Nothing is damaged: the exit code and the verdict stay clean, and the
// damage advisory does not appear.
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "0 faults") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, note) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "healthy peer") == null);
}
// ---------------------------------------------------------------------------
// `zit verify` — zit's own fsck. No contract doc and no RFC section stand
// behind this verb (it is a local design decision; repo-state §7.3 blesses a
// full rebuild as a fallback but says nothing about a verb's shape), so these
// titles cite neither a `§` nor a `B-` id.
// ---------------------------------------------------------------------------
/// The byte offset of the first PAYLOAD byte of the frame holding `body`.
///
/// THE PITFALL THIS EXISTS TO CLOSE: the two older corruption e2e tests flip at
/// `frame_offset + 10`, which lands inside the HEADER window (the hash), not the
/// payload. A payload-fault fixture that used that offset would pass for the
/// wrong reason — it would be exercising header-checksum detection. The payload
/// begins after `frameHeaderSpan` = 22 + hash_len bytes.
fn artifactPayloadOffset(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, body: []const u8) !u64 {
var file = try dir.openFile(io, ".zit", .{});
defer file.close(io);
const st = try file.stat(io);
var fs = storage.FileStorage.init(io, file, st.size);
var log = try logstore.LogStore.openReadOnly(gpa, fs.storageFile());
defer log.deinit(gpa);
const rids = try log.ridsInPersistedOrder(gpa);
defer gpa.free(rids);
for (rids) |rid| {
const content = (try log.content(gpa, rid)) orelse continue;
defer gpa.free(content);
if (!std.mem.eql(u8, content, body)) continue;
return log.offsetOf(rid) + 22 + @as(u64, log.hashOf(rid).len);
}
return error.NoArtifactHoldsThoseBytes;
}
/// Flips every bit of the byte at `at` in `dir/.zit`.
fn flipStoreByte(io: std.Io, dir: std.Io.Dir, at: u64) !void {
var file = try dir.openFile(io, ".zit", .{ .mode = .read_write });
defer file.close(io);
var byte: [1]u8 = undefined;
try std.testing.expectEqual(@as(usize, 1), try file.readPositionalAll(io, &byte, at));
byte[0] ^= 0xff;
try file.writePositionalAll(io, &byte, at);
}
test "cli verify: a clean repository verifies and exits 0" {
// Two files, so a verb that reported damage on everything could not pass
// this by accident.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root = try shunFixture(gpa, io, tmp.dir);
defer gpa.free(root);
var res = try runZit(gpa, io, tmp.dir, &.{"verify"});
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings("", res.stderr);
try std.testing.expect(std.mem.startsWith(u8, res.stdout, "verified: "));
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "0 faults") != null);
}
test "cli verify: a bitflipped artifact payload is reported by name and exits 1" {
// ACCEPTANCE 1. The fault line's job is to be pasted into `zit artifact` or
// `zit shun`, so the FULL name has to appear — and the reason has to say
// which check failed, or the assertion cannot tell the two verification
// layers apart (see the mutation proof).
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root = try shunFixture(gpa, io, tmp.dir);
defer gpa.free(root);
const doomed = try artifactNameByContent(gpa, io, tmp.dir, doomed_body);
defer gpa.free(doomed);
const at = try artifactPayloadOffset(gpa, io, tmp.dir, doomed_body);
try flipStoreByte(io, tmp.dir, at);
const before = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(before);
var res = try runZit(gpa, io, tmp.dir, &.{"verify"});
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
// Nothing on stdout: a run that found damage has no clean summary to
// compose with.
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, doomed) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "payload checksum mismatch") != null);
// AN FSCK THAT MUTATES IS A BUG. Byte-identical, not same-length.
const after = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
}
test "cli verify: a bitflipped config-record payload is reported and exits 1" {
// THE SEMANTIC MUTATION TARGET. A config frame is never interned by `scan`,
// so `ridsInPersistedOrder` never yields it and the per-artifact pass
// structurally cannot see it. Only the whole-log frame pass catches this.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// Fixture premise, pinned rather than assumed: a fresh `zit init` store is
// the file header followed by exactly one config frame, whose one-byte
// sentinel hash puts its payload at header_len + 22 + 1.
{
const bytes = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(bytes);
try std.testing.expectEqual(@backingInt(logstore.FrameKind.config), bytes[logstore.header_len + 4]);
try std.testing.expectEqual(@as(u8, 1), bytes[logstore.header_len + 5]);
}
try flipStoreByte(io, tmp.dir, logstore.header_len + 23);
var res = try runZit(gpa, io, tmp.dir, &.{"verify"});
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "config record") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "payload checksum mismatch") != null);
}
/// The start offset of the first frame of `kind` in a raw store image, found by
/// walking the frame geometry: payload_len at +0, flags at +4, hash_len at +5,
/// and an 8-aligned span of 26 + hash_len + payload_len.
fn frameStartOfKind(bytes: []const u8, kind: logstore.FrameKind) !usize {
var off: usize = logstore.header_len;
while (off + 22 <= bytes.len) {
const pl: usize = std.mem.readInt(u32, bytes[off..][0..4], .little);
const flags = bytes[off + 4];
const hl: usize = bytes[off + 5];
if (flags == @backingInt(kind)) return off;
off += ((26 + hl + pl) + 7) / 8 * 8;
}
return error.NoFrameOfThatKind;
}
test "cli verify: a store the frame pass finds clean but no open will accept is a fault, not a clean run" {
// THE ANSWER THIS VERB MUST NEVER GIVE. The frame pass walks raw bytes, so
// it comes up clean on a shun row whose checksums are all valid and whose
// payload `scan` still rejects — and the per-artifact pass then cannot open
// the store at all. Reporting "0 faults" and exiting 0 on a repository
// every other verb refuses to open would send an operator away satisfied
// from a broken store, and contradicts verify's own exit-status table.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root = try shunFixture(gpa, io, tmp.dir);
defer gpa.free(root);
const doomed = try artifactNameByContent(gpa, io, tmp.dir, doomed_body);
defer gpa.free(doomed);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", doomed });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
}
{
var file = try tmp.dir.openFile(io, ".zit", .{ .mode = .read_write });
defer file.close(io);
const st = try file.stat(io);
const bytes = try gpa.alloc(u8, @intCast(st.size));
defer gpa.free(bytes);
try std.testing.expectEqual(bytes.len, try file.readPositionalAll(io, bytes, 0));
const start = try frameStartOfKind(bytes, .shun);
const pl: usize = std.mem.readInt(u32, bytes[start..][0..4], .little);
const hl: usize = bytes[start + 5];
// A shun payload is epoch(8) | reason_len(2) | reason. Declaring a
// reason longer than the payload can hold makes `parseShun` reject it,
// while leaving every length the framing itself depends on untouched.
std.mem.writeInt(u16, bytes[start + 22 + hl + 8 ..][0..2], 0xffff, .little);
// Recompute the whole-frame checksum so the frame pass has no complaint:
// the header window is untouched, so only this one has moved.
var fcrc = std.hash.Crc32.init();
fcrc.update(bytes[start .. start + 22 + hl + pl]);
std.mem.writeInt(u32, bytes[start + 22 + hl + pl ..][0..4], fcrc.final(), .little);
try file.writePositionalAll(io, bytes[start .. start + 22 + hl + pl + 4], @intCast(start));
}
var res = try runZit(gpa, io, tmp.dir, &.{"verify"});
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expectEqualStrings("", res.stdout);
// The frame pass genuinely had nothing to say — that is the premise, not an
// accident — so this can only be the second pass refusing the store.
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "payload checksum mismatch") == null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "could not be opened for per-artifact verification") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "bytes checked") != null);
}
test "cli verify: an incomplete final frame is reported but is not a fault (exit 0)" {
// Guards against sending an operator to re-clone over an ordinary crash. A
// torn tail is an interrupted write; the next write truncates it away.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root = try shunFixture(gpa, io, tmp.dir);
defer gpa.free(root);
{
var file = try tmp.dir.openFile(io, ".zit", .{ .mode = .read_write });
defer file.close(io);
const st = try file.stat(io);
try file.setLength(io, st.size - 4);
}
var res = try runZit(gpa, io, tmp.dir, &.{"verify"});
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "incomplete final frame") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "this is not corruption") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "fault") == null);
// `openReadOnly` refuses a torn tail, so the per-artifact pass could not
// run — and the summary has to say so rather than report "0 artifacts",
// which would read as "this store holds none".
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "per-artifact verification did not run") != null);
}
test "cli verify: a corrupt frame header stops the walk and says so" {
// A SHORT WALK MUST NOT READ AS A COMPLETE ONE. Past a damaged header
// window there is no trustworthy frame boundary, so the byte accounting in
// the verdict has to come up short of the file size.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root = try shunFixture(gpa, io, tmp.dir);
defer gpa.free(root);
const at = try artifactPayloadOffset(gpa, io, tmp.dir, doomed_body);
const total = blk: {
const bytes = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(bytes);
break :blk bytes.len;
};
// `at - 10` is past the 10-byte prefix window (so the prefix crc still
// passes) and inside the header crc's coverage — with a 32-byte hash it
// lands in `delta_of`. Which header field it hits does not matter; that it
// is a header-window byte and not a payload byte is the whole point.
try flipStoreByte(io, tmp.dir, at - 10);
var res = try runZit(gpa, io, tmp.dir, &.{"verify"});
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "cannot verify past this point") != null);
var total_buf: [32]u8 = undefined;
const total_str = try std.fmt.bufPrint(&total_buf, " of {d} bytes checked", .{total});
try std.testing.expect(std.mem.indexOf(u8, res.stderr, total_str) != null);
// The bytes-checked figure is short of the total, stated as its own fact.
var full_buf: [64]u8 = undefined;
const full_str = try std.fmt.bufPrint(&full_buf, "{d} of {d} bytes checked", .{ total, total });
try std.testing.expect(std.mem.indexOf(u8, res.stderr, full_str) == null);
}
test "cli verify: --help prints its own help, and an unknown option is a usage error" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
{
var res = try runZit(gpa, io, tmp.dir, &.{ "verify", "--help" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.startsWith(u8, res.stdout, "usage: zit verify"));
try std.testing.expectEqualStrings("", res.stderr);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "verify", "--deep" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 2, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "--deep") != null);
}
}
test "cli verify: a checksum-valid frame whose content does not match its name is caught by the per-artifact pass" {
// THE SECOND LAYER'S OWN COVERAGE. Rewriting the frame's hash field AND
// recomputing both checksums that cover it produces a frame the whole-log
// pass has no complaint about — every checksum is valid — carrying content
// that does not hash to the name the frame gives it. Only reconstructing the
// artifact and re-hashing it finds this, which is why the two passes are not
// redundant with each other.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const root = try shunFixture(gpa, io, tmp.dir);
defer gpa.free(root);
const payload_at = try artifactPayloadOffset(gpa, io, tmp.dir, doomed_body);
var renamed_buf: [2 * logstore.max_hash_len]u8 = undefined;
var renamed_len: usize = 0;
{
var file = try tmp.dir.openFile(io, ".zit", .{ .mode = .read_write });
defer file.close(io);
const st = try file.stat(io);
const bytes = try gpa.alloc(u8, @intCast(st.size));
defer gpa.free(bytes);
try std.testing.expectEqual(bytes.len, try file.readPositionalAll(io, bytes, 0));
// Recover the frame start from the payload offset: the payload begins
// 22 + hash_len bytes into the frame, and hash_len is the sixth byte of
// the frame's prefix window — so the candidate start that agrees with
// its own declared width is the right one.
const start: usize = blk: {
var hl: usize = logstore.min_hash_len;
while (hl <= logstore.max_hash_len) : (hl += 1) {
const cand: usize = @intCast(payload_at - 22 - hl);
if (bytes[cand + 5] == hl) break :blk cand;
}
return error.FrameStartNotRecoverable;
};
const hl: usize = bytes[start + 5];
const pl: usize = std.mem.readInt(u32, bytes[start..][0..4], .little);
try std.testing.expectEqual(@as(usize, @intCast(payload_at)), start + 22 + hl);
// Rename the frame, leaving its payload alone. The prefix window is
// untouched (it does not cover the hash), so only the header checksum
// and the whole-frame checksum have to be recomputed.
bytes[start + 10] ^= 0xff;
var hcrc = std.hash.Crc32.init();
hcrc.update(bytes[start .. start + 18 + hl]);
std.mem.writeInt(u32, bytes[start + 18 + hl ..][0..4], hcrc.final(), .little);
var fcrc = std.hash.Crc32.init();
fcrc.update(bytes[start .. start + 22 + hl + pl]);
std.mem.writeInt(u32, bytes[start + 22 + hl + pl ..][0..4], fcrc.final(), .little);
try file.writePositionalAll(io, bytes[start .. start + 22 + hl + pl + 4], @intCast(start));
const lower = "0123456789abcdef";
for (bytes[start + 10 ..][0..hl], 0..) |b, i| {
renamed_buf[i * 2] = lower[b >> 4];
renamed_buf[i * 2 + 1] = lower[b & 0x0f];
}
renamed_len = hl * 2;
}
const renamed = renamed_buf[0..renamed_len];
var res = try runZit(gpa, io, tmp.dir, &.{"verify"});
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expectEqualStrings("", res.stdout);
// No frame-pass complaint — every checksum is valid — so this line can only
// have come from the per-artifact pass.
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "payload checksum mismatch") == null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, renamed) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "content does not hash to its name") != null);
}
test "cli verify on a sparse store: elision in the summary, damage still damage" {
// Sparse-cone RFC section 5.2: verify reports a complete-within-its-cone
// store as healthy with the elision count in the SUMMARY (never as
// findings), and real in-cone damage still reports exactly as on a dense
// store -- elision must never mask corruption.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try tmp_a.dir.createDir(io, "src", .default_dir);
try tmp_a.dir.createDir(io, "tools", .default_dir);
try writeFile(io, tmp_a.dir, "src/a.zig", "in-cone\n");
try writeFile(io, tmp_a.dir, "tools/t.bin", "out-of-cone\n");
try addManaged(gpa, io, tmp_a.dir, &.{ "src/a.zig", "tools/t.bin" });
var c1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &c1);
try expectExit(c1.term, 0);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, tmp_a.dir, port);
defer server.kill(io);
try waitForListen(io, port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{port});
defer gpa.free(url);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", "--cone=src", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
var b_dir = try tmp_b.dir.openDir(io, "repo", .{});
defer b_dir.close(io);
// Healthy: summary carries the sparse line with the exact count.
var ver = try runZit(gpa, io, b_dir, &.{"verify"});
defer freeResult(gpa, &ver);
try expectExitDiag(ver.term, 0, ver);
try std.testing.expect(std.mem.indexOf(u8, ver.stdout, "sparse store: cone of 1 member; 1 artifact deliberately elided") != null);
try std.testing.expect(std.mem.indexOf(u8, ver.stdout, "0 faults") != null);
try std.testing.expect(std.mem.indexOf(u8, ver.stderr, "missing") == null);
// Negative space: flip one byte mid-store; verify must still find real
// damage -- the sparse summary never absorbs a genuine fault.
{
var f = try b_dir.openFile(io, ".zit", .{ .mode = .read_write });
defer f.close(io);
const st = try f.stat(io);
const mid = st.size / 2;
var one: [1]u8 = undefined;
_ = try f.readPositionalAll(io, &one, mid);
one[0] ^= 0x01;
try f.writePositionalAll(io, &one, mid);
}
var damaged = try runZit(gpa, io, b_dir, &.{"verify"});
defer freeResult(gpa, &damaged);
try expectExit(damaged.term, 1);
try std.testing.expect(std.mem.indexOf(u8, damaged.stderr, "fault") != null);
}