summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/behaviour/plumber.zig41
-rw-r--r--test/monkey9p.zig25
-rw-r--r--test/replicas.zig47
3 files changed, 106 insertions, 7 deletions
diff --git a/test/behaviour/plumber.zig b/test/behaviour/plumber.zig
index 28451222..4144901b 100644
--- a/test/behaviour/plumber.zig
+++ b/test/behaviour/plumber.zig
@@ -63,3 +63,44 @@ test "plumber: plumb/send takes a message in its six lines and data, written in
try s.write("/plumb/send", s.at("test\n\n{s}\ntext\n\n{d}\nplumbed.txt:2", .{ dir, "plumbed.txt:2".len }));
try std.testing.expect(has(try s.read("/index"), "/plumbed.txt"));
}
+
+test "plumber: a port's file is read by programs: each open made before a send gets its own copy, one made after none, and a client rule's message waits for the port's first reader" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "seen.txt", .data = "one\ntwo\n" });
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ // A file for each port the rules name, read-only, as plumber(4)'s.
+ try std.testing.expectEqual(@as(u16, 0o444), (try s.stat("/plumb/web")).mode & 0o777);
+ try std.testing.expectEqual(@as(u16, 0o444), (try s.stat("/plumb/edit")).mode & 0o777);
+ // Two readers of edit, then a look: each reads the look's message,
+ // and the editor opens the file all the same.
+ const a = try s.hold("/plumb/edit", 0);
+ const b = try s.hold("/plumb/edit", 0);
+ try s.write("/look", s.at("{s}/seen.txt\n", .{dir}));
+ const want = s.at("pardes\nedit\n{s}\ntext\n\n{d}\n{s}/seen.txt", .{ "/", dir.len + "/seen.txt".len, dir });
+ try std.testing.expectEqualStrings(want, try s.readFid(a));
+ try std.testing.expectEqualStrings(want, try s.readFid(b));
+ try std.testing.expect(has(try s.read("/index"), "/seen.txt"));
+ // An open made after the send has nothing: its read waits.
+ const late = try s.hold("/plumb/edit", 0);
+ try std.testing.expectError(error.Refused, s.readFid(late));
+ s.clunk(late);
+ s.clunk(a);
+ s.clunk(b);
+ // A port only a program reads, its rule a client: the message waits
+ // for the port's first reader, the program started read nothing.
+ try s.write("/plumb/rules", "type is text\ndata matches 'letter-[0-9]+'\nplumb to seemail\nplumb client true\n\n");
+ try s.write("/plumb/send", "test\n\n/\ntext\n\n9\nletter-42");
+ const first = try s.hold("/plumb/seemail", 0);
+ try std.testing.expectEqualStrings("test\nseemail\n/\ntext\n\n9\nletter-42", try s.readFid(first));
+ try std.testing.expectError(error.Refused, s.readFid(first));
+ // Once read, a port's message goes to its readers, no program started.
+ try s.write("/plumb/send", "test\n\n/\ntext\n\n9\nletter-43");
+ try std.testing.expectEqualStrings("test\nseemail\n/\ntext\n\n9\nletter-43", try s.readFid(first));
+ s.clunk(first);
+ // Not to be written.
+ try std.testing.expectError(error.Refused, s.hold("/plumb/seemail", 1));
+}
diff --git a/test/monkey9p.zig b/test/monkey9p.zig
index c9dd5083..a4bf92a3 100644
--- a/test/monkey9p.zig
+++ b/test/monkey9p.zig
@@ -92,7 +92,7 @@ const builtins = [_][]const u8{
"Theme", "Get", "Put", "Undo", "Redo", "Zerox", "Snarf", "Paste", "Cut", "Sort", "Font", "Local",
};
const pane_files = [_][]const u8{ "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", "sel", "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", "pty", "pty/run", "pty/data", "pty/ctl", "pty/status" };
-const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "" };
+const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "plumb", "plumb/rules", "plumb/send", "plumb/edit", "plumb/web", "plumb/seemail", "" };
const long_x: [300]u8 = @splat('x');
const col_files = [_][]const u8{ "tag", "ctl", "exec" };
const writable = [_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "body", "data", "xdata", "addr", "dot", "limit", "name", "sel", "dirty", "mark", "scroll", "errors", "event", "pty/run", "pty/data", "pty/ctl", "log" };
@@ -199,7 +199,7 @@ fn pickCol(r: Random, a: Allocator) []const u8 {
fn randomPath(r: Random, a: Allocator, write: bool) []const u8 {
const x = chance(r);
if (x < 0.6) return print(a, "/pane/{s}/{s}", .{ pickSerial(r, a), if (write) pick(r, &writable) else pick(r, &pane_files) });
- if (x < 0.75) return cat(a, &.{ "/", if (write) pick(r, &[_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "log" }) else pick(r, &root_files) });
+ if (x < 0.75) return cat(a, &.{ "/", if (write) pick(r, &[_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "log", "plumb/rules", "plumb/send", "plumb/edit" }) else pick(r, &root_files) });
if (x < 0.85) return print(a, "/col/{s}/{s}", .{ pickCol(r, a), pick(r, &col_files) });
if (x < 0.93) return print(a, "/pane/{s}/{s}", .{ pickSerial(r, a), weirdName(r, a) });
var out: []const u8 = "";
@@ -230,6 +230,10 @@ fn dataFor(r: Random, a: Allocator, path: []const u8) []const u8 {
return cat(a, &.{ shellSafe(a, pick(r, &[_][]const u8{ "alpha", "alpha.txt", "alpha.txt:2", "utf8.txt:#3", "nothere", "bad.txt", "crlf.txt:1", "gamma", "beta" })), "\n" });
if (is(b, &.{ "dirty", "mark", "scroll" })) return pick(r, &[_][]const u8{ "0", "1", "0\n", "1\n", "2\n", "", "yes\n" });
if (std.mem.eql(u8, b, "log")) return pick(r, &[_][]const u8{ "follow\n", "follow new\n", "follow", "nope\n" });
+ // The plumber's: rule sets that start no program, half ones, bad ones;
+ // messages whole, short of their data, malformed.
+ if (std.mem.eql(u8, b, "rules")) return pick(r, &[_][]const u8{ "type is text\ndata matches 'letter-[0-9]+'\nplumb to seemail\n\n", "include basic\n", "type is text\narg isfile $data\nplumb to edit\n", "data isnt x\n", "plumb to seemail\n\n", "x='[a-z]+'\n", "" });
+ if (std.mem.eql(u8, b, "send")) return pick(r, &[_][]const u8{ "monkey\n\n/\ntext\n\n9\nletter-42", "monkey\n\n/\ntext\n\n9\nalpha.txt", "monkey\nseemail\n/\ntext\n\n1\nx", "monkey\n\n/\ntext\n\n20\nshort", "a\nb\n" });
return payload(r, a, 200);
}
@@ -704,7 +708,8 @@ fn hostPath(path: []const u8) bool {
fn isHeld(path: []const u8) bool {
return std.mem.eql(u8, std.mem.trimEnd(u8, path, "/"), "/log") or std.mem.endsWith(u8, path, "/event") or
- std.mem.endsWith(u8, path, "/pty/data") or std.mem.endsWith(u8, path, "/pty/run");
+ std.mem.endsWith(u8, path, "/pty/data") or std.mem.endsWith(u8, path, "/pty/run") or
+ (std.mem.startsWith(u8, path, "/plumb/") and !std.mem.endsWith(u8, path, "/rules") and !std.mem.endsWith(u8, path, "/send"));
}
/// An open the rule names: pane/new's (it makes a pane) and a truncation
@@ -931,7 +936,7 @@ fn exec(c: *Ctx, a: Allocator, res: *Res, op: Op) anyerror!void {
}
const closed = try clunk(c, a, res, &f, o.path);
// What the open holds when it closes: the last line written, if unended.
- if (opened and o.mode & 3 != 0 and sent.len > 0 and !std.mem.endsWith(u8, sent, "\n"))
+ if (opened and o.mode & 3 != 0 and restAtClose(o.path, sent))
res.add(a, .{ .kind = .close_runs, .path = o.path, .ok = closed });
},
.read => |o| {
@@ -1014,7 +1019,7 @@ fn exec(c: *Ctx, a: Allocator, res: *Res, op: Op) anyerror!void {
}
var h = c.handles.orderedRemove(i % c.handles.items.len);
const ok = try clunk(c, a, res, &h.fid, h.path);
- if (h.fid.mode & 3 != 0 and h.last.len > 0 and !std.mem.endsWith(u8, h.last, "\n"))
+ if (h.fid.mode & 3 != 0 and restAtClose(h.path, h.last))
res.add(a, .{ .kind = .close_runs, .path = h.path, .ok = ok });
},
.restore => |data| {
@@ -1322,7 +1327,15 @@ fn oneFailureRule(c: *Ctx, a: Allocator, res: *Res) ?[]const u8 {
return null;
}
-const line_files = [_][]const u8{ "look", "exec", "tagexec", "ctl" };
+const line_files = [_][]const u8{ "look", "exec", "tagexec", "ctl", "rules" };
+
+/// What an open that wrote `sent` last leaves to its close: a line with no
+/// newline yet, or of plumb/rules' text a rule set with no blank line
+/// after it (plumber(4)'s, read at the close).
+fn restAtClose(path: []const u8, sent: []const u8) bool {
+ if (sent.len == 0) return false;
+ return !std.mem.endsWith(u8, sent, if (std.mem.endsWith(u8, path, "plumb/rules")) "\n\n" else "\n");
+}
/// docs/typ/reference.typ: 'A write of command lines -- to look, exec,
/// tagexec, a ctl of the root, a pane or a column, or a column's exec --
diff --git a/test/replicas.zig b/test/replicas.zig
index 0f23d5a8..5e49a3ac 100644
--- a/test/replicas.zig
+++ b/test/replicas.zig
@@ -608,7 +608,52 @@ fn plumbCase(dir: []const u8) !bool {
ok = false;
}
};
- std.debug.print("plumb: a URL and a commit hash looked at by the authority and as a replica's own input: {s}\n", .{if (ok) "the same session" else "FAILED"});
+ // A rule to a port a program reads (plumb/seemail), of a URL, which a
+ // look reads no disk for: whether the port is read is the authority's
+ // 9P state, asked through the tape. A replica's own look waits for the
+ // first answer, then guesses the last; the second look speculates whole
+ // and is confirmed with no rollback.
+ {
+ // The rule from the session's plumbing file, read at boot.
+ var pbuf: [256]u8 = undefined;
+ try writeText(try std.fmt.bufPrintSentinel(&pbuf, "{s}/plumbing", .{dir}, 0), "type is text\ndata matches 'https://letters/.*'\nplumb to seemail\n\ninclude basic\n");
+ const a = try replica.Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty, .config_dir = dir }, host.host(), true);
+ defer a.deinit();
+ const first = try gpa.dupe(u8, try a.unit(0, 0));
+ defer gpa.free(first);
+ const rp = try replica.Replica.init(gpa, 1, first, true);
+ defer rp.deinit();
+ rp.confirm(try a.local(.{ .command = open }));
+ rp.settle();
+ if (pardes.Plumb.portIndex(rp.core(), "seemail") == null) {
+ std.debug.print("plumb: a look to a port: the plumbing file's port is not the replica's\n", .{});
+ ok = false;
+ }
+ for (0..2) |n| {
+ const before = rp.stats;
+ const msg = try gpa.dupe(u8, try rp.input(.{ .command = "Look https://letters/42" }));
+ defer gpa.free(msg);
+ if (n == 1 and (rp.stats.stopped != before.stopped or rp.stats.speculated == before.speculated)) {
+ std.debug.print("plumb: a look to a port: the replica's second look did not speculate whole ({d} stopped, {d} applied)\n", .{ rp.stats.stopped - before.stopped, rp.stats.speculated - before.speculated });
+ ok = false;
+ }
+ if (try a.pending()) |batch| rp.confirm(batch);
+ rp.confirm(try a.take(msg));
+ rp.settle();
+ }
+ if (rp.diverged) |why| {
+ std.debug.print("plumb: a look to a port: the replica diverged: {s}\n", .{why});
+ ok = false;
+ } else if (!rp.quiet() or rp.digest() != replica.digestOf(a)) {
+ std.debug.print("plumb: a look to a port: the replica's digest is not the authority's\n", .{});
+ ok = false;
+ }
+ if (rp.stats.mispredicted + rp.stats.foreign != 0) {
+ std.debug.print("plumb: a look to a port: {d} rollbacks\n", .{rp.stats.mispredicted + rp.stats.foreign});
+ ok = false;
+ }
+ }
+ std.debug.print("plumb: a URL, a commit hash and a look to a port read by programs, by the authority and as a replica's own input: {s}\n", .{if (ok) "the same session" else "FAILED"});
return ok;
}