const std = @import("std");
const repeat = @import("zit").repeat;
const logstore = @import("zit").logstore;
const storage = @import("zit").storage;
const zit = @import("zit");
const support = @import("support.zig");
const addManaged = support.addManaged;
const artifactNameByContent = support.artifactNameByContent;
const bareStoreOf = support.bareStoreOf;
const commitHashDup = support.commitHashDup;
const doomed_body = support.doomed_body;
const erasureCloneOf = support.erasureCloneOf;
const erasureOk = support.erasureOk;
const erasureOrigin = support.erasureOrigin;
const erasurePublicHex = support.erasurePublicHex;
const erasure_seed_hex = support.erasure_seed_hex;
const expectContains = support.expectContains;
const expectExitDiag = support.expectExitDiag;
const framesTrailer = support.framesTrailer;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const holdRewriteLock = support.holdRewriteLock;
const initRepo = support.initRepo;
const kept_body = support.kept_body;
const machineHash = support.machineHash;
const postXferCards = support.postXferCards;
const runCommitEnv = support.runCommitEnv;
const runErasureZit = support.runErasureZit;
const runZit = support.runZit;
const runZitStdin = support.runZitStdin;
const shunFixture = support.shunFixture;
const spawnServe = support.spawnServe;
const storeBytesAlloc = support.storeBytesAlloc;
const storeHoldsArtifact = support.storeHoldsArtifact;
const storeShunRowCount = support.storeShunRowCount;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
test "cli shun: refuses without --force leaving the store byte-identical; --force then removes the content" {
// THE DEFAULT IS REFUSAL, and a refusal must be a no-op down to the byte —
// not "no content removed" but "nothing written at all". A run that recorded
// the tombstone while deciding would leave a durable, irreversible mark on a
// repository whose operator explicitly did not say --force.
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 before = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(before);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", doomed });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
// The refusal names what would go, says it is irreversible, and names the
// flag that proceeds. Nothing lands on stdout — a refused destructive verb
// has no success output to compose with.
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "1 hash would be shunned") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "irreversible") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "run 'zit shun --force <HASH>...' to proceed") != null);
// The type diagnostic: a file blob names what the check-ins lose.
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "file content; every check-in listing it loses that file") != null);
}
// THE BYTE-IDENTICAL PIN.
{
const after = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
}
// ...and the content is still readable, so the refusal did not mask anything
// either.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "doomed.txt", root });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings(doomed_body, res.stdout);
}
// Now with --force: the bytes go and the tombstone stays.
{
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", "--reason", "takedown notice 4417", doomed });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings("", res.stderr);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "shunned: 1 hash recorded, 1 artifact removed") != null);
}
try std.testing.expect(!try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
try std.testing.expectEqual(@as(usize, 1), try storeShunRowCount(gpa, io, tmp.dir));
{
// The blob is gone: `cat` reports it missing rather than serving bytes.
var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "doomed.txt", root });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 8, res);
try std.testing.expectEqualStrings("", res.stdout);
}
{
// The neighbour survived, so the rewrite removed exactly what it named.
var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "keep.txt", root });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings(kept_body, res.stdout);
}
{
// A repeat is a no-op: exit 0, no --force needed to say "nothing to do".
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", doomed });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "nothing to do; 1 hash already shunned") != null);
}
}
test "cli shun: the check-in this checkout is on is refused EVEN WITH --force" {
// The one refusal in this verb with no override. Removing the check-in the
// working tree is standing on breaks the tree from under the operator, and
// `zit update` is a cheap way out — so this is the single case where zit
// overrides the operator's explicit --force rather than obeying it.
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 before = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(before);
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", root });
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, "is the check-in this checkout is on; refusing") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "run 'zit update'") != null);
// No flag is offered, because none exists: an operator who reads one out of
// this message would try it and be refused twice.
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "--force") == null);
// Byte-identical, and no tombstone: the guard runs before anything durable.
{
const after = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
}
try std.testing.expectEqual(@as(usize, 0), try storeShunRowCount(gpa, io, tmp.dir));
}
test "cli shun: an invocation naming the current checkout alongside a shunnable blob applies NOTHING" {
// THE PARTIAL-APPLICATION CASE, and the test that pins WHERE the
// current-checkout guard sits rather than merely that it exists. An
// invocation is ONE operation: a run that removed the blob and then reported
// a refusal would have destroyed content irreversibly while telling the
// operator nothing happened, and there is no way back from that half. Its own
// test (not a tail assertion on the single-target case) so the byte-identity
// claim is reached even when the simpler refusal is broken too.
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 before = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(before);
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", doomed, root });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "is the check-in this checkout is on") != null);
const after = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
try std.testing.expectEqual(@as(usize, 0), try storeShunRowCount(gpa, io, tmp.dir));
try std.testing.expect(try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
}
test "cli shun: a hash this repository does not hold is recorded and exits 0; a prefix that matches nothing is refused" {
// The pre-emptive block. sync §3.3 L553-556 filters the phantom scan on the
// shun table, so a name recorded before the artifact ever arrives is never
// requested — recording one is meaningful and costs no rewrite. An
// ABBREVIATED prefix is the opposite case: the table is keyed by complete
// name (§3.3 L544-545), a prefix cannot be completed, and a row keyed on one
// would read as a removal while naming nothing.
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 absent = "dead" ++ repeat("beef", 15); // 64 hex, held by nothing here
{
// Nothing is held, so NO REWRITE RUNS: the report says a row was recorded
// and refuses to claim a removal. A whole-store rewrite here would cost
// O(store) plus transient room for both generations to reclaim nothing, and
// would end in a "shun" whose byte count had grown.
var res = try runCommitEnv(gpa, io, tmp.dir, .{}, &.{ "shun", "--force", "--reason", "pre-emptive block", absent });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings(
"shunned: 1 hash recorded; none is held here, so nothing was removed\n",
res.stdout,
);
}
try std.testing.expectEqual(@as(usize, 1), try storeShunRowCount(gpa, io, tmp.dir));
{
// `--list` shows it, with the recorded date and the reason. The date comes
// from the pinned SOURCE_DATE_EPOCH (1234567890 = 2009-02-13).
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--list" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings(
absent ++ " 2009-02-13 pre-emptive block\n",
res.stdout,
);
}
{
// A prefix matching nothing: refused, and it teaches the way through.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", "dead" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "no artifact matches 'dead'") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "give the complete name") != null);
}
// Still exactly one row: the refused prefix recorded nothing.
try std.testing.expectEqual(@as(usize, 1), try storeShunRowCount(gpa, io, tmp.dir));
{
// A token that is not a hash at all is refused the same way.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", "not-a-hash" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "'not-a-hash' is not an artifact name") != null);
}
{
// An abbreviated prefix that DOES resolve is accepted — resolution to
// exactly one complete name happens before any row is written.
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[0..8] });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "1 hash recorded, 1 artifact removed") != null);
try std.testing.expect(!try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
}
}
test "cli shun --defer: the content stays on disk, the mask is immediate, and a later zit pack reclaims" {
// `--defer` is a name-level mask, not a byte-level one. The bytes remain and
// stay reconstructible through any delta stored against them until a rewrite
// runs — which is exactly why `shun_help` refuses to describe --defer as
// having removed anything.
//
// NOTE on the pinned property: a tombstone is an APPENDED frame, so `.zit`
// cannot come out the same SIZE — it necessarily grows by one frame. What
// --defer promises is that the CONTENT was not reclaimed, and that is what is
// asserted here.
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);
{
// --defer still needs --force: the row it appends is just as irreversible
// as the rewrite's, and the mask takes effect immediately.
//
// THE REFUSAL MUST DESCRIBE THE RUN THAT WAS ASKED FOR. Against the real
// binary: a --defer run removes nothing now, so it must not claim a permanent
// removal, and the command it suggests must carry --defer. An operator who
// pastes a bare `zit shun --force` out of this block escalates a deferred
// tombstone into a whole-store reclaim of every deferred row, named or not.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--defer", doomed });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "1 hash would be shunned") != null);
try std.testing.expect(std.mem.indexOf(
u8,
res.stderr,
"the bytes of 1 artifact stay on disk and stay reconstructible until 'zit pack'",
) != null);
try std.testing.expect(std.mem.indexOf(
u8,
res.stderr,
"run 'zit shun --defer --force <HASH>...' to proceed\n",
) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "permanently removed") == null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "run 'zit shun --force") == null);
}
try std.testing.expectEqual(@as(usize, 0), try storeShunRowCount(gpa, io, tmp.dir));
{
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--defer", "--force", doomed });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings("", res.stderr);
try std.testing.expectEqualStrings(
"shunned: 1 hash recorded; run 'zit pack' to reclaim the space\n",
res.stdout,
);
}
// The row is durable and the CONTENT IS STILL THERE — the whole point of the
// deferred path, and the reason the help text says the bytes stay
// reconstructible.
try std.testing.expectEqual(@as(usize, 1), try storeShunRowCount(gpa, io, tmp.dir));
try std.testing.expect(try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
{
// The mask, however, is immediate: reads answer identically either way.
var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "doomed.txt", root });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 8, res);
}
{
// ...and `zit pack` is what actually removes them.
var res = try runZit(gpa, io, tmp.dir, &.{"pack"});
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "1 shunned reclaimed") != null);
}
try std.testing.expect(!try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
// The tombstone outlived the content, so a later sync will not re-fetch it.
try std.testing.expectEqual(@as(usize, 1), try storeShunRowCount(gpa, io, tmp.dir));
}
test "cli shun: a 40-hex ABBREVIATION of a 64-hex name resolves and removes, rather than tombstoning nothing" {
// THE WIDTH TRAP. 40 hex is a COMPLETE sha1 name and also a legal prefix of a
// 64-hex sha3 one. Deciding by width alone — "40 or 64 hex means complete name,
// skip resolution" — makes this invocation record a row naming nothing, report
// success, rewrite the store for no reason, and leave the content the operator
// was ordered to remove still readable. The `unresolved` refusal exists to stop
// exactly "a row that names nothing"; only asking the store FIRST keeps it in
// the path. 39 and 41 hex always worked; this pins the two widths that did not.
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);
// The fixture is a default (sha3-256) repository, so the name is 64 hex and a
// 40-hex slice of it is an abbreviation, not a name.
try std.testing.expectEqual(@as(usize, 64), doomed.len);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", doomed[0..40] });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "1 hash recorded, 1 artifact removed") != null);
}
try std.testing.expect(!try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
{
// The row names the COMPLETE artifact, not the 40-hex token.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--list" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, doomed) != null);
}
{
// ...and the content is actually gone, which is the claim that used to be
// false while the exit code said 0.
var res = try runZit(gpa, io, tmp.dir, &.{ "cat", "doomed.txt", root });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 8, res);
try std.testing.expectEqualStrings("", res.stdout);
}
}
test "cli shun: the current-checkout stop fires on a 40-hex abbreviation of the check-in too" {
// The same width trap, aimed at the one refusal with no override: a token that
// never resolves cannot be compared against the checkout's name, so a
// width-first rule let `zit shun --force <40 hex of the current check-in>`
// through with "0 artifacts removed" instead of refusing.
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 before = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(before);
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", root[0..40] });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "is the check-in this checkout is on; refusing") != null);
const after = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
try std.testing.expectEqual(@as(usize, 0), try storeShunRowCount(gpa, io, tmp.dir));
}
test "cli shun: one artifact named twice in one invocation is ONE row, not a corruption claim" {
// `packLog`'s row-count gate requires `extra_shuns` rows to be DISTINCT, so an
// undeduplicated repeat used to abort with `zit shun: repository is corrupt` —
// a false and alarming diagnostic on the one verb where an operator can least
// afford to doubt their store. The prefix form is the dangerous one: an operator
// pasting a takedown list beside an abbreviation from `zit log` cannot see that
// the two name the same artifact.
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);
{
// The refusal collapses the duplicate too: listing one artifact twice would
// overstate what the operator is consenting to.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", doomed, doomed[0..12] });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "1 hash would be shunned") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "1 artifact would be permanently removed") != null);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", doomed, doomed[0..12] });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "corrupt") == null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "1 hash recorded, 1 artifact removed") != null);
}
// ONE row, so `recorded` is the count of rows actually written.
try std.testing.expectEqual(@as(usize, 1), try storeShunRowCount(gpa, io, tmp.dir));
try std.testing.expect(!try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
}
test "cli shun --force after --defer RECLAIMS rather than reporting nothing to do" {
// A hash tombstoned by an earlier `--defer` still has its bytes on disk. Folding
// that into "already shunned" made `zit shun --force H` print "nothing to do"
// and exit 0 over content the operator had just been ordered to remove — with
// the reclaim unreachable through this verb at all. So it is its own
// disposition: no new row, and still a rewrite.
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", "--defer", "--force", doomed });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
}
try std.testing.expect(try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
{
// Still refused without --force: this run destroys content, so it is not the
// no-op path even though it records nothing.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", doomed });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "no new hash would be recorded") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "already shunned; content still here and would be removed") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "nothing to do") == null);
}
try std.testing.expect(try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
{
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", doomed });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "0 hashes recorded, 1 artifact removed") != null);
}
try std.testing.expect(!try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
// Exactly one row throughout: the reclaim added none.
try std.testing.expectEqual(@as(usize, 1), try storeShunRowCount(gpa, io, tmp.dir));
{
// NOW it is a no-op, and only now may it say so.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", doomed });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "nothing to do; 1 hash already shunned") != null);
}
}
test "cli shun: a --force run turned away by the temp lock records NO tombstone" {
// TWO PROPERTIES IN ONE, both about the fixed-name temp `pack` and `shun` share.
//
// (1) An overlapping rewrite is turned away with a diagnostic that names what is
// happening. Unguarded, the two runs truncate each other's in-flight new
// generation and both fail the §7.7 reconstruct-and-rehash pass, reported as
// `error.Corrupt` — two healthy processes telling the operator their
// repository is corrupt, while the shun silently does not happen.
//
// (2) THE ONE-DURABLE-TRANSITION RULE, pinned at the RUNNER rather than at
// `rewriteAndSwap`. `runShun` must reach the store only through
// `extra_shuns`; append the rows to `.zit` first and then rewrite — two
// durable transitions — and this aborted run leaves a tombstone behind with
// the content it names still present. Holding the lock from the test is a
// deterministic abort seam inside the rewrite, no race required.
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 before = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(before);
// Stand in for the concurrent rewrite: hold the rewrite lock for the whole
// of the child's run.
var held = try holdRewriteLock(io, tmp.dir);
defer held.release(io);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", doomed });
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, "another repository rewrite is in progress") != null);
// Never the word that used to come out of this collision.
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "corrupt") == null);
}
// NO TOMBSTONE, and `.zit` byte-identical: the rows never touched the live log.
try std.testing.expectEqual(@as(usize, 0), try storeShunRowCount(gpa, io, tmp.dir));
const after = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
try std.testing.expect(try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
}
test "cli shun: the APPEND paths are turned away by the temp lock too, recording nothing" {
// The append paths — `--defer`, and a `--force` run with nothing to reclaim —
// reopen `.zit` and commit on the strength of a snapshot taken earlier. Without
// the same exclusive lock the rewriting verbs take, a `zit pack` (or a
// `zit shun --force`) that renames its new generation over `.zit` after that
// reopen leaves the row landing on an ORPHANED INODE: it is lost with the old
// generation while this run reports success. On the `--defer` path that means the
// deferred shun silently does not exist, having just told the operator to run
// `zit pack` to finish it — the one outcome this verb must never produce.
//
// Holding the lock from the test stands in for the concurrent rewrite, which
// makes this deterministic rather than a race.
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 absent = "dead" ++ repeat("beef", 15); // 64 hex, held by nothing here
const before = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(before);
var held = try holdRewriteLock(io, tmp.dir);
defer held.release(io);
{
// `--defer`: content to leave behind, one row to append.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--defer", "--force", doomed });
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, "another repository rewrite is in progress") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "corrupt") == null);
}
{
// The purely pre-emptive path: nothing to reclaim, so it appends too.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", absent });
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, "another repository rewrite is in progress") != null);
}
// Nothing recorded and `.zit` byte-identical on both paths.
try std.testing.expectEqual(@as(usize, 0), try storeShunRowCount(gpa, io, tmp.dir));
const after = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
try std.testing.expect(try storeHoldsArtifact(gpa, io, tmp.dir, doomed));
}
test "cli shun: a --reason carrying a newline is refused rather than forging a --list row" {
// `--list` is the only view of a durable, irreversible, un-removable record and
// it prints one row per line, so a newline inside a reason manufactures a row
// there indistinguishable from a real one. Reasons arrive from scripts and pasted
// takedown text, not only from typing. NO DRAFT SPEAKS TO THIS: refusing at input
// is a local decision, taken the same way the length gate was — refuse rather
// than mangle a note that may be a legal reference.
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 before = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(before);
var res = try runZit(gpa, io, tmp.dir, &.{
"shun", "--defer", "--force", "--reason",
"legit\ndeadbeefdeadbeef 2020-01-01 forged row", doomed,
});
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, "--reason contains a control byte") != null);
const after = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
}
test "cli shun: the usage surface — no hash, --list with a positional, an unknown option, and --help" {
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 before = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(before);
const Case = struct { argv: []const []const u8, needle: []const u8 };
for ([_]Case{
.{ .argv = &.{"shun"}, .needle = "no hash given" },
.{ .argv = &.{ "shun", "--list", "aabb" }, .needle = "unexpected argument 'aabb'" },
.{ .argv = &.{ "shun", "--force", "-f", "aabb" }, .needle = "unknown option '-f'" },
}) |c| {
var res = try runZit(gpa, io, tmp.dir, c.argv);
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 2, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, c.needle) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "try 'zit shun --help'") != null);
}
{
// `-h` wins over `--force` and over a real hash: help never destroys.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", root, "-h" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "usage: zit shun") != null);
// The two disclosures the help text is REQUIRED to carry: --defer leaves
// the bytes reconstructible, and a running `zit serve` keeps serving them.
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "reconstructible") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "zit serve") != null);
}
{
// `-R` naming a checkout still guards the check-in that checkout is on:
// the override changes where the store is found, not which rules hold.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "-R", ".", "--force", root });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "is the check-in this checkout is on") != null);
}
// Not one of those runs touched the store.
const after = try storeBytesAlloc(gpa, io, tmp.dir);
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
}
test "cli shun -R: a bare store shuns, defers, reclaims and lists, one porcelain line per hash" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "src", .default_dir);
var src = try tmp.dir.openDir(io, "src", .{});
defer src.close(io);
try initRepo(gpa, io, src);
try bareStoreOf(gpa, io, src, tmp.dir, "s.zit");
var bare = try tmp.dir.openDir(io, "s.zit", .{});
defer bare.close(io);
// Two versions of a wiki page and a technote, all recorded through -R.
try writeFile(io, tmp.dir, "v1.txt", "a secret that leaked\n");
try writeFile(io, tmp.dir, "v2.txt", "the redacted page\n");
try writeFile(io, tmp.dir, "note.txt", "a technote to take down\n");
var w1 = try runZitStdin(gpa, io, tmp.dir, "v1.txt", &.{ "wiki", "commit", "Page", "-R", "s.zit", "--user", "u" });
defer freeResult(gpa, &w1);
try expectExitDiag(w1.term, 0, w1);
var w2 = try runZitStdin(gpa, io, tmp.dir, "v2.txt", &.{ "wiki", "commit", "Page", "-R", "s.zit", "--user", "u" });
defer freeResult(gpa, &w2);
try expectExitDiag(w2.term, 0, w2);
var tn = try runZitStdin(gpa, io, tmp.dir, "note.txt", &.{ "technote", "add", "-R", "s.zit", "--user", "u" });
defer freeResult(gpa, &tn);
try expectExitDiag(tn.term, 0, tn);
const v1 = machineHash(w1.stdout);
const v2 = machineHash(w2.stdout);
const note = machineHash(tn.stdout);
try std.testing.expectEqual(@as(usize, 64), v1.len);
try std.testing.expectEqual(@as(usize, 64), note.len);
const preemptive = "dead" ++ repeat("beef", 15); // 64 hex, held by nothing here
// A forge reader's cursor, taken before any shun.
var boot = try runZit(gpa, io, tmp.dir, &.{ "frames", "-R", "s.zit" });
defer freeResult(gpa, &boot);
try expectExitDiag(boot.term, 0, boot);
const t0 = try framesTrailer(gpa, boot.stdout);
defer gpa.free(t0.cursor);
defer gpa.free(t0.token);
const since0 = try std.fmt.allocPrint(gpa, "--since={s}", .{t0.cursor});
defer gpa.free(since0);
const verify0 = try std.fmt.allocPrint(gpa, "--verify={s}", .{t0.token});
defer gpa.free(verify0);
const before = try storeBytesAlloc(gpa, io, bare);
defer gpa.free(before);
{
// Refused without --force, the same as in a checkout: nothing written.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "-R", "s.zit", "--porcelain", v1 });
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, "refusing without --force") != null);
}
{
// An abbreviation that matches nothing cannot be completed: refused.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "-R", "s.zit", "--force", "abcd1234" });
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, "no artifact matches 'abcd1234'") != null);
}
{
// A concurrent rewrite turns the run away, recording nothing.
var held = try holdRewriteLock(io, bare);
defer held.release(io);
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "-R", "s.zit", "--force", v1 });
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, "another repository rewrite is in progress") != null);
}
{
const after = try storeBytesAlloc(gpa, io, bare);
defer gpa.free(after);
try std.testing.expectEqualSlices(u8, before, after);
}
{
// --defer records the row and leaves the bytes: the reader's cursor
// survives, because no generation was swapped in.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "-R", "s.zit", "--porcelain", "--defer", "--force", "--reason", "leaked", note });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
const want = try std.fmt.allocPrint(gpa, "deferred {s}\n", .{note});
defer gpa.free(want);
try std.testing.expectEqualStrings(want, res.stdout);
try std.testing.expect(try storeHoldsArtifact(gpa, io, bare, note));
var poll = try runZit(gpa, io, tmp.dir, &.{ "frames", "-R", "s.zit", since0, verify0 });
defer freeResult(gpa, &poll);
try std.testing.expect(poll.term == .exited and poll.term.exited != 4);
}
{
// The reclaiming run: the named page version is removed, the deferred
// technote is reclaimed, and a hash held by nothing is recorded only.
// One line per named hash, in argument order.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "-R", "s.zit", "--porcelain", "--force", v1, note, preemptive });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
const want = try std.fmt.allocPrint(gpa, "reclaimed {s}\nreclaimed {s}\nrecorded {s}\n", .{ v1, note, preemptive });
defer gpa.free(want);
try std.testing.expectEqualStrings(want, res.stdout);
try std.testing.expect(!try storeHoldsArtifact(gpa, io, bare, v1));
try std.testing.expect(!try storeHoldsArtifact(gpa, io, bare, note));
try std.testing.expect(try storeHoldsArtifact(gpa, io, bare, v2));
}
{
// The rewrite is signalled exactly as `zit pack`'s is: the reader's
// cursor is dead (exit 4) and a re-read from zero recovers.
var poll = try runZit(gpa, io, tmp.dir, &.{ "frames", "-R", "s.zit", since0, verify0 });
defer freeResult(gpa, &poll);
try expectExitDiag(poll.term, 4, poll);
}
{
// Already shunned and already gone: a no-op that needs no --force.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "-R", "s.zit", "--porcelain", v1 });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
const want = try std.fmt.allocPrint(gpa, "gone {s}\n", .{v1});
defer gpa.free(want);
try std.testing.expectEqualStrings(want, res.stdout);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--list", "-R", "s.zit" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, v1) != null);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, preemptive) != null);
const note_row = std.mem.indexOf(u8, res.stdout, note).?;
const row_end = std.mem.indexOfScalarPos(u8, res.stdout, note_row, '\n').?;
try std.testing.expect(std.mem.endsWith(u8, res.stdout[note_row..row_end], " leaked"));
}
{
// --porcelain shapes the recording path's output; --list is a mode of
// its own and already one row per line.
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--list", "--porcelain", "-R", "s.zit" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 2, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "unexpected argument '--porcelain'") != null);
}
// A clone of the shunned store carries the table and lacks the bytes.
// `serve` takes no -R, so it runs from a detached tree whose store is the
// bare one: what it serves is that store itself, not a copy.
try tmp.dir.createDir(io, "front", .default_dir);
var front = try tmp.dir.openDir(io, "front", .{});
defer front.close(io);
var op = try runZit(gpa, io, front, &.{ "open", "--store=../s.zit" });
defer freeResult(gpa, &op);
try expectExitDiag(op.term, 0, op);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, front, 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 cl = try runZit(gpa, io, tmp.dir, &.{ "clone", "--no-open", url, "clone" });
defer freeResult(gpa, &cl);
try expectExitDiag(cl.term, 0, cl);
try std.testing.expect(std.mem.indexOf(u8, cl.stdout, "adopted 3 shun rows") != null);
var clone_dir = try tmp.dir.openDir(io, "clone", .{});
defer clone_dir.close(io);
try std.testing.expect(!try storeHoldsArtifact(gpa, io, clone_dir, v1));
try std.testing.expect(!try storeHoldsArtifact(gpa, io, clone_dir, note));
try std.testing.expect(try storeHoldsArtifact(gpa, io, clone_dir, v2));
var list = try runZit(gpa, io, tmp.dir, &.{ "shun", "--list", "-R", "clone" });
defer freeResult(gpa, &list);
try expectExitDiag(list.term, 0, list);
try std.testing.expect(std.mem.indexOf(u8, list.stdout, v1) != null);
try std.testing.expect(std.mem.indexOf(u8, list.stdout, note) != null);
}
test "cli shun -R: a --defer row on a bare store is reclaimed by the next rewrite" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "src", .default_dir);
var src = try tmp.dir.openDir(io, "src", .{});
defer src.close(io);
try initRepo(gpa, io, src);
try bareStoreOf(gpa, io, src, tmp.dir, "s.zit");
var bare = try tmp.dir.openDir(io, "s.zit", .{});
defer bare.close(io);
try writeFile(io, tmp.dir, "v1.txt", "a secret that leaked\n");
try writeFile(io, tmp.dir, "n1.txt", "a later takedown\n");
var w1 = try runZitStdin(gpa, io, tmp.dir, "v1.txt", &.{ "wiki", "commit", "Page", "-R", "s.zit", "--user", "u" });
defer freeResult(gpa, &w1);
try expectExitDiag(w1.term, 0, w1);
var t1 = try runZitStdin(gpa, io, tmp.dir, "n1.txt", &.{ "technote", "add", "-R", "s.zit", "--user", "u" });
defer freeResult(gpa, &t1);
try expectExitDiag(t1.term, 0, t1);
const v1 = machineHash(w1.stdout);
const n1 = machineHash(t1.stdout);
var sh = try runZit(gpa, io, tmp.dir, &.{ "shun", "-R", "s.zit", "--defer", "--force", v1 });
defer freeResult(gpa, &sh);
try expectExitDiag(sh.term, 0, sh);
try std.testing.expect(std.mem.indexOf(u8, sh.stdout, "run 'zit pack -R s.zit' to reclaim") != null);
try std.testing.expect(try storeHoldsArtifact(gpa, io, bare, v1));
// A later reclaiming shun of something else rewrites the store, and the
// rewrite drops every shunned holding, the deferred one included. Its
// porcelain names only what it was given.
var later = try runZit(gpa, io, tmp.dir, &.{ "shun", "-R", "s.zit", "--porcelain", "--force", n1 });
defer freeResult(gpa, &later);
try expectExitDiag(later.term, 0, later);
const want = try std.fmt.allocPrint(gpa, "reclaimed {s}\n", .{n1});
defer gpa.free(want);
try std.testing.expectEqualStrings(want, later.stdout);
try std.testing.expect(!try storeHoldsArtifact(gpa, io, bare, v1));
try std.testing.expect(!try storeHoldsArtifact(gpa, io, bare, n1));
try std.testing.expectEqual(@as(usize, 2), try storeShunRowCount(gpa, io, bare));
}
test "cli shun: a check-in that is not the current checkout's names its own consequence and can be forced" {
// The manifest arm of the per-type diagnostic, through the real binary: the
// report says a check-in's descendants become unresolvable, and — unlike the
// current checkout — the operator's --force is obeyed.
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);
try writeFile(io, tmp.dir, "keep.txt", "a second revision so the root is not the checkout\n");
const second = try commitHashDup(gpa, io, tmp.dir, "second");
defer gpa.free(second);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", root });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "checkin a check-in; its descendants become unresolvable") != null);
}
{
var res = try runZit(gpa, io, tmp.dir, &.{ "shun", "--force", root });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stdout, "1 hash recorded, 1 artifact removed") != null);
}
try std.testing.expect(!try storeHoldsArtifact(gpa, io, tmp.dir, root));
}
/// Poll `dir` until it no longer holds `name`: a `zit serve` reclaims between
/// connections, after the reply the client has already read. Bounded.
fn waitUntilNotHeld(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, name: []const u8) !void {
const start = std.Io.Clock.now(.awake, io).nanoseconds;
while (try storeHoldsArtifact(gpa, io, dir, name)) {
if (std.Io.Clock.now(.awake, io).nanoseconds - start > 20 * std.time.ns_per_s) return error.StillHeld;
io.sleep(.{ .nanoseconds = 50_000_000 }, .awake) catch {};
}
}
test "cli shun --sign --now: an erasure on A leaves the box, B and an old clone C, and the box refuses C's copy" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const names = try erasureOrigin(gpa, io, tmp.dir);
defer gpa.free(names.secret);
defer gpa.free(names.other);
var a = try tmp.dir.openDir(io, "a", .{});
defer a.close(io);
try erasureCloneOf(gpa, io, tmp.dir, a, &.{"box"});
var box = try tmp.dir.openDir(io, "box", .{});
defer box.close(io);
// The clone pinned the origin's key.
{
var r = try runErasureZit(gpa, io, box, .{}, &.{ "shun", "key", "show" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
const pub_hex = erasurePublicHex();
try std.testing.expectEqualStrings(&pub_hex, std.mem.trim(u8, r.stdout, "\n"));
}
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, box, 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);
try erasureOk(gpa, io, tmp.dir, .{}, &.{ "clone", url, "b" });
try erasureOk(gpa, io, tmp.dir, .{}, &.{ "clone", url, "c" });
var b = try tmp.dir.openDir(io, "b", .{});
defer b.close(io);
var c = try tmp.dir.openDir(io, "c", .{});
defer c.close(io);
// Two hashes in one signed step, one exit status.
try erasureOk(gpa, io, a, .{ .key = erasure_seed_hex }, &.{ "shun", "--force", "--sign", "--now", "--reason", "owner request", names.secret, names.other });
try std.testing.expect(!try storeHoldsArtifact(gpa, io, a, names.secret));
try erasureOk(gpa, io, a, .{}, &.{ "push", url });
// The box took the rows from an anonymous pusher because they verify,
// and reclaims the bytes between connections.
try waitUntilNotHeld(gpa, io, box, names.secret);
try waitUntilNotHeld(gpa, io, box, names.other);
// Two signed rows, and the applied row each erasure left on the box.
try std.testing.expectEqual(@as(usize, 4), try storeShunRowCount(gpa, io, box));
// B's next pull carries the rows and removes the bytes.
try std.testing.expect(try storeHoldsArtifact(gpa, io, b, names.secret));
{
var r = try runErasureZit(gpa, io, b, .{}, &.{"pull"});
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try expectContains(r.stderr, "erased by the repository owner: 2 artifacts removed");
}
try std.testing.expect(!try storeHoldsArtifact(gpa, io, b, names.secret));
{
var r = try runErasureZit(gpa, io, b, .{}, &.{ "cat", "secret.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 8, r);
try expectContains(r.stderr, "was erased by the repository owner");
}
{
var r = try runErasureZit(gpa, io, b, .{}, &.{ "log", "trunk", "secret.txt", "-F", "json" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try expectContains(r.stdout, "\"content\":\"erased\"");
}
// C was offline: it still holds the bytes and offers them; the box keeps
// refusing them, and C's own next session removes them.
try std.testing.expect(try storeHoldsArtifact(gpa, io, c, names.secret));
{
var r = try runErasureZit(gpa, io, c, .{}, &.{"sync"});
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
try std.testing.expect(!try storeHoldsArtifact(gpa, io, c, names.secret));
try std.testing.expect(!try storeHoldsArtifact(gpa, io, box, names.secret));
// The neighbour nobody erased is everywhere.
{
var r = try runErasureZit(gpa, io, c, .{}, &.{ "cat", "keep.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
}
test "cli shun --sign: the default delay keeps the bytes until the clock passes; a cancel inside the window keeps them for good" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const names = try erasureOrigin(gpa, io, tmp.dir);
defer gpa.free(names.secret);
defer gpa.free(names.other);
var a = try tmp.dir.openDir(io, "a", .{});
defer a.close(io);
try erasureCloneOf(gpa, io, tmp.dir, a, &.{"box"});
var box = try tmp.dir.openDir(io, "box", .{});
defer box.close(io);
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, box, 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);
try erasureOk(gpa, io, tmp.dir, .{}, &.{ "clone", url, "b" });
var b = try tmp.dir.openDir(io, "b", .{});
defer b.close(io);
// Signed at a time far past the box's wall clock, so the box keeps both
// erasures pending throughout; B's clock is what the test moves.
const t0 = "4000000000";
const inside = "4000000600";
const after = "4000086400"; // t0 + 24 h
try erasureOk(gpa, io, a, .{ .epoch = t0, .key = erasure_seed_hex }, &.{ "shun", "--force", "--sign", names.secret, names.other });
// Pending: A still reads both.
try erasureOk(gpa, io, a, .{ .epoch = t0 }, &.{ "cat", "secret.txt" });
try erasureOk(gpa, io, a, .{ .epoch = t0 }, &.{ "push", url });
try erasureOk(gpa, io, b, .{ .epoch = t0 }, &.{"pull"});
try std.testing.expectEqual(@as(usize, 2), try storeShunRowCount(gpa, io, b));
try std.testing.expect(try storeHoldsArtifact(gpa, io, b, names.secret));
try erasureOk(gpa, io, b, .{ .epoch = t0 }, &.{ "cat", "secret.txt" });
{
var r = try runErasureZit(gpa, io, b, .{ .epoch = t0 }, &.{ "shun", "--list", "-F", "json" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try expectContains(r.stdout, "\"state\":\"pending\"");
try expectContains(r.stdout, "\"not_before\":4000086400");
try expectContains(r.stdout, "\"signer\":\"verified\"");
}
{
var r = try runErasureZit(gpa, io, b, .{ .epoch = t0 }, &.{"verify"});
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try expectContains(r.stderr, "2 signed erasures pending");
}
// The owner changes their mind about one of them, inside the window.
try erasureOk(gpa, io, a, .{ .epoch = inside, .key = erasure_seed_hex }, &.{ "shun", "cancel", names.other });
try erasureOk(gpa, io, a, .{ .epoch = inside }, &.{ "push", url });
try erasureOk(gpa, io, b, .{ .epoch = inside }, &.{"pull"});
// Past the not-before: B masks the erased one on its next open, and its
// next session removes the bytes; the cancelled one stays.
{
var r = try runErasureZit(gpa, io, b, .{ .epoch = after }, &.{ "cat", "secret.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 8, r);
try expectContains(r.stderr, "was erased by the repository owner");
}
try std.testing.expect(try storeHoldsArtifact(gpa, io, b, names.secret));
try erasureOk(gpa, io, b, .{ .epoch = after }, &.{"pull"});
try std.testing.expect(!try storeHoldsArtifact(gpa, io, b, names.secret));
try std.testing.expect(try storeHoldsArtifact(gpa, io, b, names.other));
try erasureOk(gpa, io, b, .{ .epoch = after }, &.{ "cat", "other.txt" });
// Too late to cancel now.
{
var r = try runErasureZit(gpa, io, a, .{ .epoch = after, .key = erasure_seed_hex }, &.{ "shun", "cancel", names.secret });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 1, r);
try expectContains(r.stderr, "cannot be cancelled");
}
}
test "cli shun --sign: rows that do not verify are ignored by sync and named by verify; the box refuses an unsigned push" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const names = try erasureOrigin(gpa, io, tmp.dir);
defer gpa.free(names.secret);
defer gpa.free(names.other);
var a = try tmp.dir.openDir(io, "a", .{});
defer a.close(io);
try erasureCloneOf(gpa, io, tmp.dir, a, &.{ "box", "b" });
var box = try tmp.dir.openDir(io, "box", .{});
defer box.close(io);
var b = try tmp.dir.openDir(io, "b", .{});
defer b.close(io);
// The box carries a plain shun its operator recorded, and a row signed by
// a key nobody pinned; neither reaches B by sync.
try erasureOk(gpa, io, box, .{}, &.{ "shun", "--force", "--defer", names.other });
const stranger = "2222222222222222222222222222222222222222222222222222222222222222";
{
const strange_pub = zit.erasure.keyHex(try zit.erasure.publicKeyOf(zit.erasure.parseKeyHex(32, stranger).?));
try erasureOk(gpa, io, box, .{}, &.{ "shun", "key", "pin", &strange_pub });
try erasureOk(gpa, io, box, .{ .key = stranger }, &.{ "shun", "--force", "--sign", "--now", names.secret });
const pub_hex = erasurePublicHex();
try erasureOk(gpa, io, box, .{}, &.{ "shun", "key", "pin", &pub_hex });
}
// The plain row, the stranger's signed row, and the applied row that
// erasure left while the stranger's key was the pinned one.
try std.testing.expectEqual(@as(usize, 3), try storeShunRowCount(gpa, io, box));
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, box, 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);
try erasureOk(gpa, io, b, .{}, &.{ "pull", url });
try std.testing.expectEqual(@as(usize, 0), try storeShunRowCount(gpa, io, b));
try erasureOk(gpa, io, b, .{}, &.{ "cat", "secret.txt" });
// The box itself holds the stranger's row as information only.
{
var r = try runErasureZit(gpa, io, box, .{}, &.{"verify"});
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try expectContains(r.stderr, "does not verify against the pinned erasure key");
}
// A pushed unsigned row is refused while a key is pinned.
{
var code_buf: [64]u8 = undefined;
const code = blk: {
var file = try box.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);
var oc = (try log.readConfig(gpa)).?;
defer oc.deinit(gpa);
const pc = oc.config.project_code;
@memcpy(code_buf[0..pc.len], pc);
break :blk code_buf[0..pc.len];
};
var payload_buf: [256]u8 = undefined;
const payload = try std.fmt.bufPrint(&payload_buf, "1800000000 '{s}' scom 'plain takedown'", .{names.other});
const cards = try std.fmt.allocPrint(gpa, "pragma client-version 20000\npush 0000000000000000000000000000000000000000 {s}\nconfig /shun {d}\n{s}\n", .{ code, payload.len, payload });
defer gpa.free(cards);
const reply = try postXferCards(gpa, io, port, cards);
defer gpa.free(reply);
try expectContains(reply, "error not\\sauthorized\\sto\\spush\\sconfiguration");
}
}
test "cli shun key: generate writes a 0600 seed and pins; --print and -F json write nothing; purge undo will not restore an erased name" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDir(io, "a", .default_dir);
var a = try tmp.dir.openDir(io, "a", .{});
defer a.close(io);
try initRepo(gpa, io, a);
try writeFile(io, a, "junk.bin", "swept in by accident\n");
try writeFile(io, a, "keep.txt", "kept\n");
try addManaged(gpa, io, a, &.{ "junk.bin", "keep.txt" });
gpa.free(try commitHashDup(gpa, io, a, "seed"));
const junk = try artifactNameByContent(gpa, io, a, "swept in by accident\n");
defer gpa.free(junk);
try writeFile(io, a, "junk.bin", "the file as it should have been\n");
gpa.free(try commitHashDup(gpa, io, a, "fix"));
const tmp_abs = try tmp.dir.realPathFileAlloc(io, ".", gpa);
defer gpa.free(tmp_abs);
const xdg = try std.fs.path.join(gpa, &.{ tmp_abs, "xdg" });
defer gpa.free(xdg);
{
var r = try runErasureZit(gpa, io, a, .{ .xdg = xdg }, &.{ "shun", "key", "generate", "-F", "json" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try expectContains(r.stdout, "{\"public\":\"");
try expectContains(r.stdout, "\",\"private\":\"");
try std.testing.expectError(error.FileNotFound, tmp.dir.access(io, "xdg", .{}));
var show = try runErasureZit(gpa, io, a, .{}, &.{ "shun", "key", "show" });
defer freeResult(gpa, &show);
try expectExitDiag(show.term, 1, show);
}
try erasureOk(gpa, io, a, .{ .xdg = xdg }, &.{ "shun", "key", "generate" });
{
var keys = try tmp.dir.openDir(io, "xdg/zit/keys", .{ .iterate = true });
defer keys.close(io);
var it = keys.iterate();
const entry = (try it.next(io)).?;
try std.testing.expect(std.mem.endsWith(u8, entry.name, ".erasure"));
const st = try keys.statFile(io, entry.name, .{});
try std.testing.expectEqual(@as(u64, 32), st.size);
try std.testing.expectEqual(@as(std.posix.mode_t, 0o600), st.permissions.toMode() & 0o777);
try std.testing.expect((try it.next(io)) == null);
const dst = try tmp.dir.statFile(io, "xdg/zit/keys", .{});
try std.testing.expectEqual(@as(std.posix.mode_t, 0o700), dst.permissions.toMode() & 0o777);
}
{
var r = try runErasureZit(gpa, io, a, .{ .xdg = xdg }, &.{ "shun", "key", "generate" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 1, r);
try expectContains(r.stderr, "already pins an erasure key");
}
// Purge the junk, then erase it: the bytes are already gone, so no
// rewrite runs and the graveyard still holds them; undo must not bring
// them back.
try erasureOk(gpa, io, a, .{}, &.{ "purge", "artifacts", "--force", junk });
try erasureOk(gpa, io, a, .{ .xdg = xdg }, &.{ "shun", "--force", "--sign", "--now", junk });
var id_buf: [16]u8 = undefined;
const id = blk: {
var r = try runErasureZit(gpa, io, a, .{}, &.{ "purge", "list" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
const tok = std.mem.sliceTo(r.stdout, ' ');
@memcpy(id_buf[0..tok.len], tok);
break :blk id_buf[0..tok.len];
};
{
var r = try runErasureZit(gpa, io, a, .{}, &.{ "purge", "undo", id });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try expectContains(r.stderr, "1 artifact not restored: shunned in this repository");
}
try std.testing.expect(!try storeHoldsArtifact(gpa, io, a, junk));
// A second purge buries keep.txt's first version; an erasure of it beside
// a name still held rewrites the store, and that rewrite empties the
// graveyard of the erased name.
const old_keep = try artifactNameByContent(gpa, io, a, "kept\n");
defer gpa.free(old_keep);
const fixed = try artifactNameByContent(gpa, io, a, "the file as it should have been\n");
defer gpa.free(fixed);
try writeFile(io, a, "keep.txt", "kept, edited\n");
gpa.free(try commitHashDup(gpa, io, a, "edit"));
try erasureOk(gpa, io, a, .{}, &.{ "purge", "artifacts", "--force", old_keep });
{
var r = try runErasureZit(gpa, io, a, .{}, &.{ "purge", "list" });
defer freeResult(gpa, &r);
try std.testing.expect(r.stdout.len != 0);
}
try erasureOk(gpa, io, a, .{ .xdg = xdg }, &.{ "shun", "--force", "--sign", "--now", old_keep, fixed });
{
var r = try runErasureZit(gpa, io, a, .{}, &.{ "purge", "list" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try std.testing.expectEqualStrings("", r.stdout);
}
}
test "cli shun --sign: an applied erasure stays applied after the pinned key changes; a peer still holding the bytes cannot bring them back" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const names = try erasureOrigin(gpa, io, tmp.dir);
defer gpa.free(names.secret);
defer gpa.free(names.other);
var a = try tmp.dir.openDir(io, "a", .{});
defer a.close(io);
// B and the old clone C both start holding the bytes.
try erasureCloneOf(gpa, io, tmp.dir, a, &.{ "b", "c" });
var b = try tmp.dir.openDir(io, "b", .{});
defer b.close(io);
var c = try tmp.dir.openDir(io, "c", .{});
defer c.close(io);
try erasureOk(gpa, io, a, .{ .key = erasure_seed_hex }, &.{ "shun", "--force", "--sign", "--now", names.secret });
{
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, a, 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);
try erasureOk(gpa, io, b, .{}, &.{ "pull", url });
}
try std.testing.expect(!try storeHoldsArtifact(gpa, io, b, names.secret));
// The owner rotates the key on B; the old erasure no longer verifies.
const new_pub = zit.erasure.keyHex(try zit.erasure.publicKeyOf(@splat(0x99)));
try erasureOk(gpa, io, b, .{}, &.{ "shun", "key", "pin", &new_pub });
// C never heard of the erasure and offers the bytes.
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, c, 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);
try erasureOk(gpa, io, b, .{}, &.{ "pull", url });
try std.testing.expect(!try storeHoldsArtifact(gpa, io, b, names.secret));
var r = try runErasureZit(gpa, io, b, .{}, &.{ "cat", "secret.txt" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 8, r);
try expectContains(r.stderr, "was erased by the repository owner");
}
test "cli shun key: a pull, a sync or a config pull carrying another erasure-key never changes the pinned one" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const names = try erasureOrigin(gpa, io, tmp.dir);
defer gpa.free(names.secret);
defer gpa.free(names.other);
var a = try tmp.dir.openDir(io, "a", .{});
defer a.close(io);
try erasureCloneOf(gpa, io, tmp.dir, a, &.{"b"});
var b = try tmp.dir.openDir(io, "b", .{});
defer b.close(io);
const pinned = erasurePublicHex();
// The origin now pins another key.
const other_pub = zit.erasure.keyHex(try zit.erasure.publicKeyOf(@splat(0x77)));
try erasureOk(gpa, io, a, .{}, &.{ "shun", "key", "pin", &other_pub });
const port = try freshLoopbackPort(io);
var server = try spawnServe(gpa, io, a, 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);
for ([_][]const []const u8{ &.{ "pull", url }, &.{ "sync", url }, &.{ "config", "pull", "shun", url } }) |argv| {
try erasureOk(gpa, io, b, .{}, argv);
var r = try runErasureZit(gpa, io, b, .{}, &.{ "shun", "key", "show" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try std.testing.expectEqualStrings(&pinned, std.mem.trim(u8, r.stdout, "\n"));
}
}