const std = @import("std");
const test_child = @import("test_child");
const logstore = @import("zit").logstore;
const storage = @import("zit").storage;
const net = std.Io.net;
const support = @import("support.zig");
const PushFixture = support.PushFixture;
const addManaged = support.addManaged;
const b_seed_len = support.b_seed_len;
const checkinHash = support.checkinHash;
const collectArtifactDigests = support.collectArtifactDigests;
const commitHashDup = support.commitHashDup;
const dialServe = support.dialServe;
const exchangeWith = support.exchangeWith;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectSameDigestSet = support.expectSameDigestSet;
const expectVerifies = support.expectVerifies;
const freeDigests = support.freeDigests;
const freeResult = support.freeResult;
const freshLoopbackPort = support.freshLoopbackPort;
const initRepo = support.initRepo;
const runCommitEnv = support.runCommitEnv;
const runZit = support.runZit;
const spawnServe = support.spawnServe;
const spawnServeFronted = support.spawnServeFronted;
const tipHashOf = support.tipHashOf;
const waitForListen = support.waitForListen;
const writeFile = support.writeFile;
/// A logging proxy: forwards every request to `tport` and appends one line
/// per request to the log, `login` when it carries any credential (a login
/// card in the decompressed body, a login cookie, or an Authorization
/// header), `-` otherwise. It exits on its own after five idle minutes.
const login_sink_proxy =
\\import re, socket, sys, zlib
\\lport, tport, log = int(sys.argv[1]), int(sys.argv[2]), sys.argv[3]
\\srv = socket.socket()
\\srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
\\srv.bind(("127.0.0.1", lport))
\\srv.listen(16)
\\srv.settimeout(300)
\\def recv_http(c):
\\ data = b""
\\ while b"\r\n\r\n" not in data:
\\ chunk = c.recv(65536)
\\ if not chunk:
\\ return data
\\ data += chunk
\\ head, _, body = data.partition(b"\r\n\r\n")
\\ if re.search(rb"expect:\s*100-continue", head, re.I):
\\ c.sendall(b"HTTP/1.1 100 Continue\r\n\r\n")
\\ m = re.search(rb"content-length:\s*(\d+)", head, re.I)
\\ n = int(m.group(1)) if m else 0
\\ while len(body) < n:
\\ chunk = c.recv(65536)
\\ if not chunk:
\\ break
\\ body += chunk
\\ return head + b"\r\n\r\n" + body
\\while True:
\\ try:
\\ c, _ = srv.accept()
\\ except socket.timeout:
\\ break
\\ c.settimeout(60)
\\ req = recv_http(c)
\\ if not req:
\\ c.close()
\\ continue
\\ head, _, body = req.partition(b"\r\n\r\n")
\\ try:
\\ text = zlib.decompress(body[4:])
\\ except Exception:
\\ text = body
\\ cred = re.search(rb"^login ", text, re.M) or re.search(rb"x-f-l-c=|^authorization:", head, re.I | re.M)
\\ with open(log, "ab") as f:
\\ f.write(b"login\n" if cred else b"-\n")
\\ u = socket.create_connection(("127.0.0.1", tport))
\\ u.sendall(req)
\\ while True:
\\ chunk = u.recv(65536)
\\ if not chunk:
\\ break
\\ c.sendall(chunk)
\\ u.close()
\\ c.close()
\\
;
fn spawnLoginSink(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, lport: u16, tport: u16, log: []const u8) !std.process.Child {
try writeFile(io, dir, "sink.py", login_sink_proxy);
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("PATH", "/usr/bin:/bin");
const l = try std.fmt.allocPrint(gpa, "{d}", .{lport});
defer gpa.free(l);
const t = try std.fmt.allocPrint(gpa, "{d}", .{tport});
defer gpa.free(t);
return test_child.spawn(io, .{
.argv = &.{ "/usr/bin/env", "python3", "sink.py", l, t, log },
.cwd = .{ .dir = dir },
.environ_map = &env,
.stdout = .ignore,
.stderr = .ignore,
});
}
/// How many requests the sink logged, and how many carried a credential.
fn sinkCounts(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, log: []const u8) !struct { requests: usize, logins: usize } {
const bytes = dir.readFileAlloc(io, log, gpa, .limited(1 << 16)) catch |e| switch (e) {
error.FileNotFound => return .{ .requests = 0, .logins = 0 },
else => return e,
};
defer gpa.free(bytes);
return .{ .requests = std.mem.count(u8, bytes, "\n"), .logins = std.mem.count(u8, bytes, "login\n") };
}
/// The stored login's user and the saved remote, read from `dir/.zit`.
fn readLoginAndRemote(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir) !struct { user: []u8, remote: []u8 } {
var file = try dir.openFile(io, ".zit", .{ .mode = .read_only });
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)) orelse return error.TestUnexpectedResult;
defer oc.deinit(gpa);
const user = try gpa.dupe(u8, oc.config.login_user);
errdefer gpa.free(user);
return .{ .user = user, .remote = try gpa.dupe(u8, oc.config.remote_url) };
}
fn expectLoginAndRemote(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, user: []const u8, remote: []const u8) !void {
const got = try readLoginAndRemote(gpa, io, dir);
defer gpa.free(got.user);
defer gpa.free(got.remote);
try std.testing.expectEqualStrings(user, got.user);
try std.testing.expectEqualStrings(remote, got.remote);
}
test "cli push, pull, sync: a saved login is sent only to the saved remote it was made for" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Origin A, and B, a clone of it (same project), each behind a logging proxy.
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "a.txt", "a\n");
try addManaged(gpa, io, tmp_a.dir, &.{"a.txt"});
{
var c = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed", "--no-sync" });
defer freeResult(gpa, &c);
try expectExitDiag(c.term, 0, c);
}
const a_port = try freshLoopbackPort(io);
var a_srv = try spawnServe(gpa, io, tmp_a.dir, a_port);
defer a_srv.kill(io);
try waitForListen(io, a_port);
const a_url_direct = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{a_port});
defer gpa.free(a_url_direct);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
{
var r = try runZit(gpa, io, tmp_b.dir, &.{ "clone", a_url_direct, "b", "--no-open", "--once" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
var b_dir = try tmp_b.dir.openDir(io, "b", .{});
defer b_dir.close(io);
const b_port = try freshLoopbackPort(io);
var b_srv = try spawnServe(gpa, io, b_dir, b_port);
defer b_srv.kill(io);
try waitForListen(io, b_port);
var logs = std.testing.tmpDir(.{});
defer logs.cleanup();
const pa_port = try freshLoopbackPort(io);
var pa = try spawnLoginSink(gpa, io, logs.dir, pa_port, a_port, "a.log");
defer pa.kill(io);
try waitForListen(io, pa_port);
const pb_port = try freshLoopbackPort(io);
var pb = try spawnLoginSink(gpa, io, logs.dir, pb_port, b_port, "b.log");
defer pb.kill(io);
try waitForListen(io, pb_port);
const url_a = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{pa_port});
defer gpa.free(url_a);
const url_b = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{pb_port});
defer gpa.free(url_b);
// The client: cloned with --once, so it starts with no remote and no login.
var tmp_c = std.testing.tmpDir(.{});
defer tmp_c.cleanup();
{
var r = try runZit(gpa, io, tmp_c.dir, &.{ "clone", a_url_direct, "c", "--once", "--user", "alice", "--password", "pw" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
var c_dir = try tmp_c.dir.openDir(io, "c", .{});
defer c_dir.close(io);
try expectLoginAndRemote(gpa, io, c_dir, "", "");
const Step = struct {
fn commit(g: std.mem.Allocator, i: std.Io, d: std.Io.Dir, content: []const u8) !void {
try writeFile(i, d, "a.txt", content);
var c = try runCommitEnv(g, i, d, .{ .user = "u" }, &.{ "commit", "-m", content, "--no-sync" });
defer freeResult(g, &c);
try expectExitDiag(c.term, 0, c);
}
};
// A credentialed push to A, with no remote saved: A becomes the remote
// and the login is saved beside it.
try Step.commit(gpa, io, c_dir, "one\n");
{
var r = try runZit(gpa, io, c_dir, &.{ "push", url_a, "--user", "alice", "--password", "pw" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "login not saved") == null);
}
try expectLoginAndRemote(gpa, io, c_dir, "alice", url_a);
// A bare push goes to the saved remote and signs as the saved login.
try Step.commit(gpa, io, c_dir, "two\n");
{
const before = try sinkCounts(gpa, io, logs.dir, "a.log");
var r = try runZit(gpa, io, c_dir, &.{"push"});
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
const after = try sinkCounts(gpa, io, logs.dir, "a.log");
try std.testing.expect(after.logins > before.logins);
}
// To B, naming no user: no credential reaches B on any verb.
try Step.commit(gpa, io, c_dir, "three\n");
for ([_][]const u8{ "push", "pull", "sync" }) |verb| {
var r = try runZit(gpa, io, c_dir, &.{ verb, url_b });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
{
const b = try sinkCounts(gpa, io, logs.dir, "b.log");
try std.testing.expect(b.requests > 0);
try std.testing.expectEqual(@as(usize, 0), b.logins);
}
// To B as the same user, with no password: refused, not the A secret.
{
var r = try runZit(gpa, io, c_dir, &.{ "push", url_b, "--user", "alice" });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
try std.testing.expectEqual(@as(usize, 0), (try sinkCounts(gpa, io, logs.dir, "b.log")).logins);
}
// To B with a password: authenticated, succeeds, and says the login was
// not saved, because B is not the saved remote.
{
var r = try runZit(gpa, io, c_dir, &.{ "push", url_b, "--user", "bob", "--password", "pw2" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
const want = try std.fmt.allocPrint(gpa, "zit push: login not saved: {s} is not the saved remote ('zit remote {s}' makes it so)\n", .{ url_b, url_b });
defer gpa.free(want);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, want) != null);
try std.testing.expect((try sinkCounts(gpa, io, logs.dir, "b.log")).logins > 0);
}
try expectLoginAndRemote(gpa, io, c_dir, "alice", url_a);
// Re-setting the same remote keeps the login; pointing it elsewhere drops it.
{
var r = try runZit(gpa, io, c_dir, &.{ "remote", url_a });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
try expectLoginAndRemote(gpa, io, c_dir, "alice", url_a);
{
var r = try runZit(gpa, io, c_dir, &.{ "remote", url_b });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
try expectLoginAndRemote(gpa, io, c_dir, "", url_b);
{
const before = (try sinkCounts(gpa, io, logs.dir, "b.log")).logins;
var r = try runZit(gpa, io, c_dir, &.{"pull"});
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try std.testing.expectEqual(before, (try sinkCounts(gpa, io, logs.dir, "b.log")).logins);
}
// --unset drops a login too.
{
var r = try runZit(gpa, io, c_dir, &.{ "push", "--user", "bob", "--password", "pw2" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
try expectLoginAndRemote(gpa, io, c_dir, "bob", url_b);
{
var r = try runZit(gpa, io, c_dir, &.{ "remote", "--unset" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
try expectLoginAndRemote(gpa, io, c_dir, "", "");
// --once authenticates and saves nothing: no login, no remote.
{
var r = try runZit(gpa, io, c_dir, &.{ "push", url_a, "--once", "--user", "alice", "--password", "pw" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
try std.testing.expect(std.mem.indexOf(u8, r.stderr, "login not saved") == null);
}
try expectLoginAndRemote(gpa, io, c_dir, "", "");
for ([_][]const u8{ "pull", "sync" }) |verb| {
var r = try runZit(gpa, io, c_dir, &.{ verb, url_a, "--once", "--user", "alice", "--password", "pw" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
}
try expectLoginAndRemote(gpa, io, c_dir, "", "");
}
/// A redirector: a request for `/` is answered 302 with the Location given,
/// any other path is forwarded to `tport`. One line per request goes to the
/// log: the request path. It exits on its own after five idle minutes.
const redirect_proxy =
\\import re, socket, sys
\\lport, tport, location, log = int(sys.argv[1]), int(sys.argv[2]), sys.argv[3].encode(), sys.argv[4]
\\srv = socket.socket()
\\srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
\\srv.bind(("127.0.0.1", lport))
\\srv.listen(16)
\\srv.settimeout(300)
\\def recv_http(c):
\\ data = b""
\\ while b"\r\n\r\n" not in data:
\\ chunk = c.recv(65536)
\\ if not chunk:
\\ return data
\\ data += chunk
\\ head, _, body = data.partition(b"\r\n\r\n")
\\ m = re.search(rb"content-length:\s*(\d+)", head, re.I)
\\ n = int(m.group(1)) if m else 0
\\ while len(body) < n:
\\ chunk = c.recv(65536)
\\ if not chunk:
\\ break
\\ body += chunk
\\ return head + b"\r\n\r\n" + body
\\while True:
\\ try:
\\ c, _ = srv.accept()
\\ except socket.timeout:
\\ break
\\ c.settimeout(60)
\\ req = recv_http(c)
\\ if not req:
\\ c.close()
\\ continue
\\ path = req.split(b" ", 2)[1]
\\ with open(log, "ab") as f:
\\ f.write(path + b"\n")
\\ if path == b"/":
\\ c.sendall(b"HTTP/1.1 302 Found\r\nLocation: " + location + b"\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")
\\ c.close()
\\ continue
\\ u = socket.create_connection(("127.0.0.1", tport))
\\ u.sendall(req)
\\ while True:
\\ chunk = u.recv(65536)
\\ if not chunk:
\\ break
\\ c.sendall(chunk)
\\ u.close()
\\ c.close()
\\
;
fn spawnRedirector(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, lport: u16, tport: u16, location: []const u8, log: []const u8) !std.process.Child {
try writeFile(io, dir, "redirect.py", redirect_proxy);
var env = std.process.Environ.Map.init(gpa);
defer env.deinit();
try env.put("PATH", "/usr/bin:/bin");
const l = try std.fmt.allocPrint(gpa, "{d}", .{lport});
defer gpa.free(l);
const t = try std.fmt.allocPrint(gpa, "{d}", .{tport});
defer gpa.free(t);
return test_child.spawn(io, .{
.argv = &.{ "/usr/bin/env", "python3", "redirect.py", l, t, location, log },
.cwd = .{ .dir = dir },
.environ_map = &env,
.stdout = .ignore,
.stderr = .ignore,
});
}
fn logLines(gpa: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, log: []const u8) !usize {
const bytes = dir.readFileAlloc(io, log, gpa, .limited(1 << 16)) catch |e| switch (e) {
error.FileNotFound => return 0,
else => return e,
};
defer gpa.free(bytes);
return std.mem.count(u8, bytes, "\n");
}
test "cli push: a redirect to another host is refused, names the target, and sends it nothing" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var fx = try PushFixture.init(gpa, io);
defer fx.deinit(io);
var logs = std.testing.tmpDir(.{});
defer logs.cleanup();
// The second host: localhost, on its own port, logging every request.
const sink_port = try freshLoopbackPort(io);
var sink = try spawnLoginSink(gpa, io, logs.dir, sink_port, fx.port, "sink.log");
defer sink.kill(io);
try waitForListen(io, sink_port);
const location = try std.fmt.allocPrint(gpa, "http://eve:secret@localhost:{d}/", .{sink_port});
defer gpa.free(location);
const redir_port = try freshLoopbackPort(io);
var redir = try spawnRedirector(gpa, io, logs.dir, redir_port, fx.port, location, "redirect.log");
defer redir.kill(io);
try waitForListen(io, redir_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{redir_port});
defer gpa.free(url);
var r = try runZit(gpa, io, fx.dir, &.{ "push", url, "--user", "u", "--password", "pw", "--once" });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
const want = try std.fmt.allocPrint(gpa, "zit push: the peer redirected to http://localhost:{d}/, another host or port; not followed (if you trust it, run the command against that URL directly)\n", .{sink_port});
defer gpa.free(want);
try std.testing.expectEqualStrings(want, r.stderr);
try std.testing.expectEqual(@as(usize, 1), try logLines(gpa, io, logs.dir, "redirect.log"));
try std.testing.expectEqual(@as(usize, 0), (try sinkCounts(gpa, io, logs.dir, "sink.log")).requests);
}
test "cli push: a redirect to the same host and port is followed" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var fx = try PushFixture.init(gpa, io);
defer fx.deinit(io);
var logs = std.testing.tmpDir(.{});
defer logs.cleanup();
const redir_port = try freshLoopbackPort(io);
const location = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/repo/xfer", .{redir_port});
defer gpa.free(location);
var redir = try spawnRedirector(gpa, io, logs.dir, redir_port, fx.port, location, "redirect.log");
defer redir.kill(io);
try waitForListen(io, redir_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{redir_port});
defer gpa.free(url);
var r = try runZit(gpa, io, fx.dir, &.{ "push", url, "--once" });
defer freeResult(gpa, &r);
try expectExitDiag(r.term, 0, r);
// The first request was answered 302; the rest went to /repo.
const bytes = try logs.dir.readFileAlloc(io, "redirect.log", gpa, .limited(1 << 16));
defer gpa.free(bytes);
try std.testing.expect(std.mem.startsWith(u8, bytes, "/\n/repo\n"));
}
test "cli push: with --resolve given, a HOST:PORT without an entry is refused before connecting" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var fx = try PushFixture.init(gpa, io);
defer fx.deinit(io);
var logs = std.testing.tmpDir(.{});
defer logs.cleanup();
const sink_port = try freshLoopbackPort(io);
var sink = try spawnLoginSink(gpa, io, logs.dir, sink_port, fx.port, "sink.log");
defer sink.kill(io);
try waitForListen(io, sink_port);
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/", .{sink_port});
defer gpa.free(url);
const pin = try std.fmt.allocPrint(gpa, "zit-pin-test.invalid:{d}:127.0.0.1", .{sink_port});
defer gpa.free(pin);
var r = try runZit(gpa, io, fx.dir, &.{ "push", url, "--once", "--resolve", pin });
defer freeResult(gpa, &r);
try expectExit(r.term, 1);
const want = try std.fmt.allocPrint(gpa, "zit push: no --resolve entry for 127.0.0.1:{d}; not connecting\n", .{sink_port});
defer gpa.free(want);
try std.testing.expectEqualStrings(want, r.stderr);
try std.testing.expectEqual(@as(usize, 0), (try sinkCounts(gpa, io, logs.dir, "sink.log")).requests);
}
// --- push interop (sync §3.3/§3.7) -----------------------------------------
//
// The push milestone e2e: an outgoing `zit push` that UPLOADS a repo's new
// artifacts into an EXISTING served repo over loopback HTTP. B is cloned from A,
// then B grows a second check-in, then B pushes its new artifacts to A, and A's
// on-disk set must equal B's full set — proving the push client uploaded exactly
// what A lacked and the server persisted it durably (sync §3.3/§3.7), over the
// wire, not from any fossil oracle. This is the mirror of pull: content flows the
// other way, and the durable landing is on the SERVER (A), not the client.
test "cli push: an outgoing push lands a repo's new artifacts on the server over HTTP (sync §3.3/§3.7)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Repo A: init + a committed file — A's first check-in, real artifacts to seed.
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "one.txt", "first artifact over http\n");
try addManaged(gpa, io, tmp_a.dir, &.{"one.txt"});
var commit1 = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "seed" });
defer freeResult(gpa, &commit1);
try expectExit(commit1.term, 0);
// Serve A's seed state and clone it into B; B now shares A's project-code and
// holds A's first-check-in artifacts (the push starts from a real subset).
const clone_port = try freshLoopbackPort(io);
var clone_server = try spawnServe(gpa, io, tmp_a.dir, clone_port);
try waitForListen(io, clone_port);
const clone_url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/xfer", .{clone_port});
defer gpa.free(clone_url);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
var clone = try runZit(gpa, io, tmp_b.dir, &.{ "clone", clone_url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
clone_server.kill(io);
// B grows: a second committed file, so B now holds artifacts A does not.
var b_repo = try tmp_b.dir.openDir(io, "repo", .{});
defer b_repo.close(io);
try writeFile(io, b_repo, "two.txt", "second artifact A has never seen\n");
try addManaged(gpa, io, b_repo, &.{"two.txt"});
var commit2 = try runCommitEnv(gpa, io, b_repo, .{ .user = "u" }, &.{ "commit", "-m", "grow" });
defer freeResult(gpa, &commit2);
try expectExit(commit2.term, 0);
// B's full artifact-digest set is the convergence oracle: after the push, A
// must hold exactly this set. Captured now, before the wire runs.
const b_digests = try collectArtifactDigests(gpa, io, b_repo);
defer freeDigests(gpa, b_digests);
try std.testing.expect(b_digests.len > b_seed_len(gpa, io, tmp_b.dir) - 1);
// Serve A (still only the seed check-in) and push B's content up to it.
const push_port = try freshLoopbackPort(io);
var push_server = try spawnServe(gpa, io, tmp_a.dir, push_port);
defer push_server.kill(io);
try waitForListen(io, push_port);
const push_url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/xfer", .{push_port});
defer gpa.free(push_url);
var push = try runZit(gpa, io, b_repo, &.{ "push", push_url });
defer freeResult(gpa, &push);
try expectExitDiag(push.term, 0, push);
try std.testing.expectEqualStrings("", push.stderr);
// Stop the server so its file handles are released before A is read back — the
// durable landing is on disk, independent of the live process.
push_server.kill(io);
// THE PROOF: A's on-disk artifact set now equals B's full set — A received the
// second check-in's artifacts over the wire and the server persisted them
// durably (sync §3.3/§3.7). A push the server dropped (or only half-stored)
// fails here; the assertion is not weakened.
const a_digests = try collectArtifactDigests(gpa, io, tmp_a.dir);
defer freeDigests(gpa, a_digests);
try expectSameDigestSet(a_digests, b_digests);
}
test "cli push/pull: a served repository refuses another project's push and pull (sync §8.5)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Two separate `zit init`s: two projects, each with its own project code.
var tmp_a = std.testing.tmpDir(.{});
defer tmp_a.cleanup();
try initRepo(gpa, io, tmp_a.dir);
try writeFile(io, tmp_a.dir, "a.txt", "served project\n");
try addManaged(gpa, io, tmp_a.dir, &.{"a.txt"});
var commit_a = try runCommitEnv(gpa, io, tmp_a.dir, .{ .user = "u" }, &.{ "commit", "-m", "a" });
defer freeResult(gpa, &commit_a);
try expectExit(commit_a.term, 0);
var tmp_b = std.testing.tmpDir(.{});
defer tmp_b.cleanup();
try initRepo(gpa, io, tmp_b.dir);
try writeFile(io, tmp_b.dir, "b.txt", "another project\n");
try addManaged(gpa, io, tmp_b.dir, &.{"b.txt"});
var commit_b = try runCommitEnv(gpa, io, tmp_b.dir, .{ .user = "u" }, &.{ "commit", "-m", "b" });
defer freeResult(gpa, &commit_b);
try expectExit(commit_b.term, 0);
const a_before = try collectArtifactDigests(gpa, io, tmp_a.dir);
defer freeDigests(gpa, a_before);
const b_before = try collectArtifactDigests(gpa, io, tmp_b.dir);
defer freeDigests(gpa, b_before);
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}/xfer", .{port});
defer gpa.free(url);
for ([_][]const u8{ "push", "pull" }) |verb| {
var res = try runZit(gpa, io, tmp_b.dir, &.{ verb, url });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 1, res);
const way_out = if (std.mem.eql(u8, verb, "push"))
"clone the peer to work on it"
else
"if the peer is a new empty repository run 'zit push' first, otherwise clone it";
const want = try std.fmt.allocPrint(gpa, "zit {s}: the peer refused: wrong project (this repository and the peer's are different projects; {s})\n", .{ verb, way_out });
defer gpa.free(want);
try std.testing.expectEqualStrings(want, res.stderr);
}
server.kill(io);
// Neither store gained anything from the other project.
const a_after = try collectArtifactDigests(gpa, io, tmp_a.dir);
defer freeDigests(gpa, a_after);
try expectSameDigestSet(a_before, a_after);
const b_after = try collectArtifactDigests(gpa, io, tmp_b.dir);
defer freeDigests(gpa, b_after);
try expectSameDigestSet(b_before, b_after);
}
/// A stand-in for a front such as quarry: listens on loopback, adds
/// `headers` to every request it receives, forwards it to a `zit serve
/// --fronted` on `upstream`, and copies the reply back. One request per
/// connection, as both ends close after a reply. `stop` ends it.
const FrontProxy = struct {
io: std.Io,
listener: net.Server,
port: u16,
upstream: u16,
headers: []const u8,
stopping: std.atomic.Value(bool) = .init(false),
thread: std.Thread = undefined,
fn start(self: *FrontProxy) !void {
self.thread = try std.Thread.spawn(.{}, run, .{self});
}
fn stop(self: *FrontProxy) void {
self.stopping.store(true, .release);
// Unblock the accept so the loop sees the flag.
if (dialServe(self.io, self.port)) |s| s.close(self.io) else |_| {}
self.thread.join();
self.listener.deinit(self.io);
}
fn run(self: *FrontProxy) void {
while (!self.stopping.load(.acquire)) {
const conn = self.listener.accept(self.io) catch return;
defer conn.close(self.io);
if (self.stopping.load(.acquire)) return;
self.relay(conn) catch {};
}
}
fn relay(self: *FrontProxy, conn: net.Stream) !void {
const a = std.heap.page_allocator;
var rbuf: [16 * 1024]u8 = undefined;
var r = conn.reader(self.io, &rbuf);
var head: std.ArrayListUnmanaged(u8) = .empty;
defer head.deinit(a);
var body_len: usize = 0;
while (true) {
const line = try r.interface.takeDelimiterInclusive('\n');
if (std.mem.eql(u8, line, "\r\n")) break;
const colon = std.mem.indexOfScalar(u8, line, ':') orelse 0;
if (colon != 0 and std.ascii.eqlIgnoreCase(line[0..colon], "content-length")) {
body_len = try std.fmt.parseInt(usize, std.mem.trim(u8, line[colon + 1 ..], " \r\n"), 10);
}
try head.appendSlice(a, line);
}
try head.appendSlice(a, self.headers);
try head.appendSlice(a, "\r\n");
const body = try r.interface.readAlloc(a, body_len);
defer a.free(body);
const up = try dialServe(self.io, self.upstream);
defer up.close(self.io);
var ubuf: [4096]u8 = undefined;
var uw = up.writer(self.io, &ubuf);
try uw.interface.writeAll(head.items);
try uw.interface.writeAll(body);
try uw.interface.flush();
var urbuf: [16 * 1024]u8 = undefined;
var ur = up.reader(self.io, &urbuf);
const reply = try ur.interface.allocRemaining(a, .limited(64 << 20));
defer a.free(reply);
var wbuf: [4096]u8 = undefined;
var w = conn.writer(self.io, &wbuf);
try w.interface.writeAll(reply);
try w.interface.flush();
}
};
test "cli push: behind a front that diverts, a commit is refused with the way out, and pushing again prints where it went and pulls it" {
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 writeFile(io, tmp_a.dir, "one.txt", "seed\n");
try addManaged(gpa, io, tmp_a.dir, &.{"one.txt"});
const seed = try commitHashDup(gpa, io, tmp_a.dir, "seed");
defer gpa.free(seed);
const serve_port = try freshLoopbackPort(io);
var server = try spawnServeFronted(gpa, io, tmp_a.dir, serve_port, true);
defer server.kill(io);
try waitForListen(io, serve_port);
const addr = try net.IpAddress.parseIp4("127.0.0.1", 0);
var front = FrontProxy{
.io = io,
.listener = try addr.listen(io, .{}),
.port = 0,
.upstream = serve_port,
.headers = "X-Zit-Protect: trunk\r\nX-Zit-Divert-Login: jan\r\n",
};
front.port = front.listener.socket.address.getPort();
try front.start();
defer front.stop();
const url = try std.fmt.allocPrint(gpa, "http://127.0.0.1:{d}/xfer", .{front.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", url, "repo" });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
var b_repo = try tmp_b.dir.openDir(io, "repo", .{});
defer b_repo.close(io);
// The commit is kept; its autosync push is refused with the way out.
try writeFile(io, b_repo, "two.txt", "grows trunk\n");
try addManaged(gpa, io, b_repo, &.{"two.txt"});
var ci = try runCommitEnv(gpa, io, b_repo, .{ .user = "u" }, &.{ "commit", "-m", "grow" });
defer freeResult(gpa, &ci);
try expectExitDiag(ci.term, 0, ci);
const grow = try gpa.dupe(u8, try checkinHash(ci.stdout));
defer gpa.free(grow);
const attempt = try std.fmt.allocPrint(gpa, "attempt/jan/{s}", .{grow[0..12]});
defer gpa.free(attempt);
try std.testing.expect(std.mem.indexOf(u8, ci.stderr, "the peer refused") != null);
try std.testing.expect(std.mem.indexOf(u8, ci.stderr, attempt) != null);
try std.testing.expect(std.mem.indexOf(u8, ci.stderr, "autosync push failed; continuing locally") != null);
// Pushed again: the server's message is printed, and the move is pulled
// so this repository agrees with the server at once.
var push = try runZit(gpa, io, b_repo, &.{"push"});
defer freeResult(gpa, &push);
try expectExitDiag(push.term, 0, push);
try std.testing.expect(std.mem.indexOf(u8, push.stderr, "server: check-in ") != null);
try std.testing.expect(std.mem.indexOf(u8, push.stderr, attempt) != null);
try std.testing.expect(std.mem.indexOf(u8, push.stdout, "pull from ") != null);
var cur = try runZit(gpa, io, b_repo, &.{ "branch", "current" });
defer freeResult(gpa, &cur);
try expectExitDiag(cur.term, 0, cur);
try std.testing.expectEqualStrings(attempt, std.mem.trimEnd(u8, cur.stdout, "\n"));
const trunk_tip = try tipHashOf(gpa, io, tmp_a.dir, "trunk");
defer gpa.free(trunk_tip);
try std.testing.expectEqualStrings(seed, trunk_tip);
}
fn zitSize(io: std.Io, dir: std.Io.Dir) !u64 {
return (try dir.statFile(io, ".zit", .{})).size;
}
/// Origin A with `files` files (a sub-manifest store when `k` is set), cloned
/// into B and C; B makes five one-file check-ins and pushes them to A, then C
/// pulls them (after one earlier pull has taken A's clusters). Returns the growth of B (sender), A (push receiver) and C (pull
/// receiver) and the bytes the push sent and the pull received on the wire,
/// with `zit verify` clean on all three.
fn receivedGrowth(gpa: std.mem.Allocator, io: std.Io, files: usize, k: ?[]const u8) !struct { sent: u64, pushed: u64, pulled: u64, push_sent: u64, pull_received: u64 } {
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);
if (k) |kv| {
var en = try runZit(gpa, io, a, &.{ "submanifest", "enable", "--k", kv });
defer freeResult(gpa, &en);
try expectExitDiag(en.term, 0, en);
}
var name_buf: [32]u8 = undefined;
for (0..files) |i| {
const name = try std.fmt.bufPrint(&name_buf, "d{d}/f{d:0>4}.txt", .{ i % 50, i });
try a.createDirPath(io, std.fs.path.dirname(name).?);
try writeFile(io, a, name, name);
}
{
var res = try runZit(gpa, io, a, &.{ "add", "." });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
}
const root = try commitHashDup(gpa, io, a, "root");
gpa.free(root);
{
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 u8{ "b", "c" }) |dest| {
var clone = try runZit(gpa, io, tmp.dir, &.{ "clone", url, dest });
defer freeResult(gpa, &clone);
try expectExitDiag(clone.term, 0, clone);
}
}
var b = try tmp.dir.openDir(io, "b", .{});
defer b.close(io);
var c = try tmp.dir.openDir(io, "c", .{});
defer c.close(io);
// The first sync A serves after its large root mints Fossil-style clusters
// over every artifact it holds, and that pull carries them: a one-time
// cost, not storage of the check-ins, so it lands before the baselines.
_ = try exchangeWith(gpa, io, a, c, "pull");
const b0 = try zitSize(io, b);
var content_buf: [32]u8 = undefined;
for (1..6) |n| {
const name = try std.fmt.bufPrint(&name_buf, "d{d}/f{d:0>4}.txt", .{ (n * 131) % files % 50, (n * 131) % files });
try writeFile(io, b, name, try std.fmt.bufPrint(&content_buf, "edit {d}", .{n}));
const h = try commitHashDup(gpa, io, b, "one file");
gpa.free(h);
}
const sent = try zitSize(io, b) - b0;
const a0 = try zitSize(io, a);
const push_wire = try exchangeWith(gpa, io, a, b, "push");
const pushed = try zitSize(io, a) - a0;
const c0 = try zitSize(io, c);
const pull_wire = try exchangeWith(gpa, io, a, c, "pull");
const pulled = try zitSize(io, c) - c0;
for ([_]std.Io.Dir{ a, b, c }) |d| try expectVerifies(gpa, io, d);
return .{ .sent = sent, .pushed = pushed, .pulled = pulled, .push_sent = push_wire.sent, .pull_received = pull_wire.received };
}
test "cli push/pull: check-ins received whole are stored as deltas, near the sender's growth" {
const gpa = std.testing.allocator;
const io = std.testing.io;
const g = try receivedGrowth(gpa, io, 1000, null);
// Stored whole, each received manifest of this flat tree costs ~40 KB; a
// delta, a few hundred bytes. The ratio, not the tree's size, is the test.
try std.testing.expect(g.pushed <= 2 * g.sent);
try std.testing.expect(g.pulled <= 2 * g.sent);
}
test "cli push/pull: a sub-manifest store's received nodes and check-ins store and verify" {
const gpa = std.testing.allocator;
const io = std.testing.io;
const g = try receivedGrowth(gpa, io, 500, "3");
try std.testing.expect(g.pushed <= 2 * g.sent);
try std.testing.expect(g.pulled <= 2 * g.sent);
}
test "cli push/pull: a sender sends its stored deltas, so five one-file check-ins cost little on the wire" {
const gpa = std.testing.allocator;
const io = std.testing.io;
const g = try receivedGrowth(gpa, io, 1000, null);
// Sent whole, the five manifests of this 1,000-file tree are about 80 KB
// each. A push also announces every held artifact (an igot apiece, about
// 70 KB a round on this store), which is most of what remains.
try std.testing.expect(g.push_sent < 200 * 1024);
try std.testing.expect(g.pull_received < 16 * 1024);
}