summaryrefslogtreecommitdiff
path: root/test/process/tree.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/process/tree.zig')
-rw-r--r--test/process/tree.zig636
1 files changed, 636 insertions, 0 deletions
diff --git a/test/process/tree.zig b/test/process/tree.zig
new file mode 100644
index 00000000..358e7040
--- /dev/null
+++ b/test/process/tree.zig
@@ -0,0 +1,636 @@
+//! docs/typ/reference.typ, The tree <tree>, through a real session's socket.
+const std = @import("std");
+const libc = std.c;
+const process = @import("../process.zig");
+const c9 = @import("cloud9");
+const eh = @import("../e2e_harness.zig");
+const expect = std.testing.expect;
+const expectEqual = std.testing.expectEqual;
+const expectEqualStrings = std.testing.expectEqualStrings;
+const Conn = process.Conn;
+const has = process.has;
+const check = process.check;
+
+pub const cases = .{
+ .{ "tree: listing, stat and find are inert; new, remove, look, exec, name, sel, log, ctl lock, focus, the ctl split, commands, Save and the slots behave", discovery },
+ .{ "tree: a write bigger than a 9P message runs each line once, whole, an Edit block once it ends; a detached session is 160x50 until told", cutWrites },
+ .{ "tree: the root lists its files and not self; /os reads the disk, `..` taken as a walk takes it, and writes it truncated", rootAndOs },
+ .{ "tree: a client of 256-byte frames lists /os whole, a dangling link left out; one of 64 KiB is agreed", frames },
+ .{ "tree: /screen freezes at its open; a dozen connections in turn each read a body", frozenScreen },
+ .{ "tree: a Look at a relative name finds the disk's file first; /n/self is the session's tree, /n/os the disk", looks },
+ .{ "tree: names up to 255 bytes list, read, write and open through /os and /n/os, and a directory lists them", longNames },
+};
+
+fn contains(names: []const []const u8, name: []const u8) bool {
+ for (names) |n| if (std.mem.eql(u8, n, name)) return true;
+ return false;
+}
+
+/// A write refused, its words holding `why`.
+fn refusedSaying(c: *process.Conn, path: []const u8, data: []const u8, why: []const u8) !void {
+ try std.testing.expectError(error.Refused, c.write(path, data));
+ if (!has(c.said, why)) {
+ std.debug.print("{s} refused saying {s}, not {s}\n", .{ path, c.said, why });
+ return error.WrongWords;
+ }
+}
+
+/// A read on `c` given up after `sec` seconds, so a wait that never ends fails.
+fn timeout(c: *process.Conn, sec: i64) void {
+ const tv: libc.timeval = .{ .sec = sec, .usec = 0 };
+ _ = libc.setsockopt(c.fd, libc.SOL.SOCKET, libc.SO.RCVTIMEO, &tv, @sizeOf(libc.timeval));
+}
+
+/// /index's row for `serial`.
+fn indexRow(index: []const u8, serial: u32) ![]const u8 {
+ var rows = std.mem.tokenizeScalar(u8, index, '\n');
+ while (rows.next()) |row| {
+ const sp = std.mem.indexOfScalar(u8, row, ' ') orelse continue;
+ if ((std.fmt.parseInt(u32, row[0..sp], 10) catch continue) == serial) return row;
+ }
+ return error.NoRow;
+}
+
+/// The serials of /index's rows whose fourth word, the path, is `path`.
+fn panesAt(a: std.mem.Allocator, c: *process.Conn, path: []const u8) ![]const []const u8 {
+ var out: std.ArrayList([]const u8) = .empty;
+ var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
+ while (rows.next()) |row| {
+ var w = std.mem.tokenizeScalar(u8, row, ' ');
+ const serial = w.next().?;
+ _ = w.next();
+ _ = w.next();
+ if (std.mem.eql(u8, w.next() orelse "", path)) try out.append(a, serial);
+ }
+ return out.items;
+}
+
+const Seen = struct { path: []const u8, mode: u32, mtime: u32 };
+
+/// Every entry below `path` stated, as `find` does, without opening one.
+fn walkTree(a: std.mem.Allocator, c: *process.Conn, path: []const u8, seen: *std.ArrayList(Seen)) !void {
+ for (try c.list(a, path)) |name| {
+ const child = try std.fmt.allocPrint(a, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), name });
+ const st = try c.stat(child);
+ try seen.append(a, .{ .path = child, .mode = st.mode, .mtime = st.mtime });
+ if (st.mode & c9.dmdir != 0 and !contains(&.{ "/log", "/os", "/screen" }, child)) try walkTree(a, c, child, seen);
+ }
+}
+
+/// A follower's read on a held /log, which waits for a new record.
+const Follower = struct {
+ conn: *process.Conn,
+ fid: u32,
+ off: u64,
+ buf: [256]u8 = undefined,
+ len: usize = 0,
+
+ fn run(w: *Follower) void {
+ const got = w.conn.readFid(w.fid, w.off, 4096) catch return;
+ w.len = @min(got.len, w.buf.len);
+ @memcpy(w.buf[0..w.len], got[0..w.len]);
+ }
+};
+
+fn discovery(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "discovery", .{});
+ defer d.deinit();
+ const f = &d.files;
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const c = try process.Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const root = d.root();
+
+ const top = try c.list(a, "/");
+ // A set: which files the root holds, not the order a listing gives.
+ const want = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "pane", "col", "os" };
+ var others: usize = 0;
+ for (top) |n| {
+ if (contains(&.{ "src", "shaders" }, n)) continue;
+ others += 1;
+ try expect(contains(&want, n));
+ }
+ try expectEqual(want.len, others);
+ for (want) |n| try expect(contains(top, n));
+ const embedded = contains(top, "src");
+ const before = try c.read(a, "/index");
+ const guide = try c.read(a, "/README");
+ try expect(std.mem.count(u8, guide, "\n") <= 45 and has(guide, "Msg hello") and has(guide, "pane/new"));
+ try expectEqual(guide.len, (try c.stat("/README")).length);
+ // ls/stat/find over the whole tree, without opening, creates nothing.
+ var seen: std.ArrayList(Seen) = .empty;
+ try walkTree(a, c, "/", &seen);
+ var names: std.ArrayList([]const u8) = .empty;
+ for (seen.items) |s| try names.append(a, s.path);
+ for ([_][]const u8{ "/index", "/status", "/look", "/exec", "/log", "/pane/1", "/pane/1/body", "/pane/1/name", "/pane/1/sel", "/pane/1/ctl", "/pane/1/tag", "/pane/1/event", "/pane/1/dot", "/pane/1/limit", "/pane/1/dirty", "/pane/1/mark", "/pane/1/scroll", "/pane/1/look", "/pane/1/exec" }) |n| try expect(contains(names.items, n));
+ try expect(!contains(names.items, "/pane/1/pty")); // the fixture file pane grew no pty/
+ for (seen.items) |s| {
+ try expect(!std.mem.startsWith(u8, s.path, "/self") and !std.mem.endsWith(u8, s.path, "/rdsel") and !std.mem.endsWith(u8, s.path, "/wrsel"));
+ const mode = s.mode & 0o777;
+ try expect(mode == 0o755 or mode == 0o644 or mode == 0o666 or mode == 0o444 or mode == 0o222 or mode == 0o555);
+ try expect(s.mtime > 0);
+ }
+ try expectEqualStrings(before, try c.read(a, "/index")); // browsing created no pane
+ try expect(before.len > 0);
+ try expectEqual(@as(u64, 0), (try c.stat("/index")).length); // a view made by each read stats 0 (reference.typ, Stats)
+ try expectEqual(@as(u32, 0o444), (try c.stat("/index")).mode & 0o777);
+ try expectEqual(@as(u32, 0o444), (try c.stat("/status")).mode & 0o777);
+ try expectEqual(@as(u32, 0o666), (try c.stat("/exec")).mode & 0o777);
+ try expectEqual(@as(u32, 0o666), (try c.stat("/pane/1/body")).mode & 0o777);
+ try expectEqual(@as(u32, 0o755), (try c.stat("/pane")).mode & 0o777);
+ // A range file reads the pair it takes; a flag file reads 0 or 1.
+ try expectEqual(@as(u64, 24), (try c.stat("/pane/1/addr")).length);
+ try expectEqual(@as(u64, 0), (try c.stat("/pane/1/limit")).length);
+ try expectEqualStrings("0\n", try c.read(a, "/pane/1/dirty"));
+ try expectEqualStrings("1\n", try c.read(a, "/pane/1/scroll"));
+ const fixture = try std.fmt.parseInt(u32, before[0..std.mem.indexOfScalar(u8, before, ' ').?], 10);
+ try expect(std.mem.startsWith(u8, before, f.at("{d} text 0 ", .{fixture})));
+ try expect(std.mem.endsWith(u8, before[0..std.mem.lastIndexOfScalar(u8, std.mem.trimEnd(u8, before, "\n"), ' ').?], "/discovery.txt"));
+ // /status reports the editor; /exec has nothing to report yet.
+ var pid: i64 = 0;
+ var version = false;
+ var panes: i64 = 0;
+ var lines = std.mem.tokenizeScalar(u8, try c.read(a, "/status"), '\n');
+ while (lines.next()) |line| {
+ const sp = std.mem.indexOfScalar(u8, line, ' ') orelse continue;
+ const key, const value = .{ line[0..sp], std.mem.trim(u8, line[sp..], " ") };
+ if (std.mem.eql(u8, key, "pid")) pid = try std.fmt.parseInt(i64, value, 10);
+ if (std.mem.eql(u8, key, "version")) version = value.len > 0;
+ if (std.mem.eql(u8, key, "panes")) panes = try std.fmt.parseInt(i64, value, 10);
+ }
+ try expect(pid > 0 and version and panes >= 1);
+ try expectEqualStrings("", try c.read(a, "/exec"));
+ // An open of /pane/new makes a pane and the read names it; each open
+ // makes another. A stat makes none, which is why new can be listed at
+ // all: ls -l stats every name a listing gave it. /log answers the
+ // recent past and ends; `follow` written to the same open makes its
+ // reads past that wait for new records.
+ const log = try c.hold("/log", 2);
+ const frozen = (try c.readFid(log, 0, 1 << 16)).len;
+ _ = try c.writeFid(log, 0, "follow\n");
+ const first = try c.newPane();
+ try expectEqual(first, try c.newest(a));
+ const second = try c.newPane();
+ try expect(first != second and first != fixture);
+ try expect(contains(try c.list(a, "/pane"), "new"));
+ _ = try c.stat("/pane/new");
+ try expectEqual(second, try c.newest(a)); // a stat of new made no pane
+ // What the editor said is in the stream too.
+ const paneEvent = struct {
+ fn next(conn: *process.Conn, al: std.mem.Allocator, fid: u32, off: u64) ![]const u8 {
+ while (true) {
+ const record = try conn.readFid(fid, off, 1 << 16);
+ if (!std.mem.startsWith(u8, record, "msg ") and !std.mem.startsWith(u8, record, "err ")) return al.dupe(u8, record);
+ }
+ }
+ }.next;
+ try expectEqualStrings(f.at("new {d} {s}/+New\n", .{ first, root }), try paneEvent(c, a, log, frozen));
+ try expectEqualStrings(f.at("new {d} {s}/+New\n", .{ second, root }), try paneEvent(c, a, log, frozen));
+ // A follower that is waiting wakes when the editor logs, not when some
+ // unrelated request next happens to wait.
+ {
+ const follower = try process.Conn.dial(gpa, d.socket());
+ defer follower.deinit(gpa);
+ timeout(follower, 5);
+ const waiting = try follower.hold("/log", 2);
+ const start = (try follower.readFid(waiting, 0, 1 << 16)).len;
+ _ = try follower.writeFid(waiting, 0, "follow\n");
+ var w: Follower = .{ .conn = follower, .fid = waiting, .off = start };
+ const reader = try std.Thread.spawn(.{}, Follower.run, .{&w});
+ process.sleep(200);
+ try c.write("/exec", "Msg woken\n");
+ reader.join();
+ try expect(std.mem.endsWith(u8, w.buf[0..w.len], " woken\n"));
+ }
+ // A pane's ctl takes acme's lock: a second open's lock fails at once
+ // with "file in use" until the holder unlocks, and nothing else waits
+ // on it meanwhile.
+ {
+ const other = try process.Conn.dial(gpa, d.socket());
+ defer other.deinit(gpa);
+ const ctl = f.at("/pane/{d}/ctl", .{first});
+ const mine = try c.hold(ctl, 2);
+ _ = try c.writeFid(mine, 0, "lock\n");
+ const theirs = try other.hold(ctl, 1);
+ try std.testing.expectError(error.Refused, other.writeFid(theirs, 0, "lock\n"));
+ try expect(has(other.said, "file in use"));
+ try c.write(f.at("/pane/{d}/addr", .{first}), "#0");
+ _ = try c.writeFid(mine, 0, "unlock\n");
+ _ = try other.writeFid(theirs, 0, "lock\n");
+ c.clunk(mine);
+ other.clunk(theirs);
+ }
+ // /focus names the pane with the keyboard and moves it, as rio's wctl
+ // `current` does; a pane's ctl line ends saying which it is.
+ try c.write("/focus", f.at("{d}\n", .{first}));
+ try expectEqualStrings(f.at("{d}\n", .{first}), try c.read(a, "/focus"));
+ try expect(has(try c.read(a, f.at("/pane/{d}/ctl", .{first})), " current "));
+ try expect(has(try c.read(a, f.at("/pane/{d}/ctl", .{second})), " notcurrent "));
+ try refusedSaying(c, "/focus", "99999\n", "no such pane");
+ // /ctl reads the settings as a write takes them and takes the session's
+ // builtins; a pane's ctl takes the pane's.
+ const settings = try c.read(a, "/ctl");
+ try expect(has(settings, "Verbose ") and !has(settings, "Platform"));
+ try c.write("/ctl", settings);
+ try expectEqualStrings(settings, try c.read(a, "/ctl"));
+ try c.write("/ctl", "Verbose off\n");
+ try expect(has(try c.read(a, "/ctl"), "Verbose off\n"));
+ try refusedSaying(c, "/ctl", "Del\n", "not a session control message \"Del\"");
+ try refusedSaying(c, "/ctl", "Nope 3\n", "unknown control message \"Nope\"");
+ try refusedSaying(c, f.at("/pane/{d}/ctl", .{first}), "Newcol\n", "not a window control message \"Newcol\"");
+ const commands = try c.read(a, "/commands");
+ const row = struct {
+ fn row(text: []const u8, start: []const u8) bool {
+ var rows = std.mem.tokenizeScalar(u8, text, '\n');
+ while (rows.next()) |r| if (std.mem.eql(u8, r, start) or (std.mem.startsWith(u8, r, start) and r.len > start.len and r[start.len] == ' ')) return true;
+ return false;
+ }
+ }.row;
+ try expect(row(commands, "Newcol root") and row(commands, "Save arg pane") and row(commands, "Edit arg both"));
+ // word, `arg` if it takes one, its ctl, a setting's values, then `-- ` what it does
+ var crows = std.mem.tokenizeScalar(u8, commands, '\n');
+ while (crows.next()) |r| {
+ var w = std.mem.tokenizeScalar(u8, r, ' ');
+ _ = w.next();
+ var which = w.next().?;
+ if (std.mem.eql(u8, which, "arg")) which = w.next().?;
+ try expect(contains(&.{ "root", "pane", "both" }, which));
+ }
+ try expect(row(commands, "Placement arg root acme,pardes"));
+ try expect(std.mem.endsWith(u8, commands, "\n"));
+ try expectEqual(commands.len, (try c.stat("/commands")).length);
+ try c.write(f.at("/pane/{d}/ctl", .{first}), "Msg said through ctl\n");
+ try expect(has(try f.screen(), "said through ctl"));
+ const listed = try c.list(a, "/pane");
+ try expectEqual(@as(usize, 4), listed.len);
+ for ([_]u32{ fixture, first, second }) |n| try expect(contains(listed, f.at("{d}", .{n})));
+ try expect(contains(listed, "new"));
+ try c.replace(f.at("/pane/{d}/body", .{first}), "first pane");
+ try expectEqualStrings("first pane", try c.read(a, f.at("/pane/{d}/body", .{first})));
+ try expectEqualStrings("", try c.read(a, f.at("/pane/{d}/body", .{second})));
+ try expectEqual(@as(u64, "first pane".len), (try c.stat(f.at("/pane/{d}/body", .{first}))).length);
+ const index = try c.read(a, "/index");
+ try expect(std.mem.startsWith(u8, try indexRow(index, first), f.at("{d} text 1 ", .{first})));
+ try expect(std.mem.startsWith(u8, try indexRow(index, second), f.at("{d} text 0 ", .{second})));
+ // name: read the file name, write to rename; sel: the selection.
+ const name = f.at("/pane/{d}/name", .{first});
+ try expectEqualStrings(f.at("{s}/+New\n", .{root}), try c.read(a, name));
+ try c.write(name, "notes.txt\n");
+ try expectEqualStrings(f.at("{s}/notes.txt\n", .{root}), try c.read(a, name));
+ try expectEqualStrings(f.at("rename {d} {s}/notes.txt\n", .{ first, root }), try paneEvent(c, a, log, frozen));
+ try expectEqual(@as(u64, f.at("{s}/notes.txt\n", .{root}).len), (try c.stat(name)).length);
+ // Copying addr onto dot selects that range; sel reads and replaces it.
+ const addr = f.at("/pane/{d}/addr", .{first});
+ const dot = f.at("/pane/{d}/dot", .{first});
+ const sel = f.at("/pane/{d}/sel", .{first});
+ try c.write(addr, "#0,#5");
+ try c.write(dot, try c.read(a, addr));
+ try expectEqualStrings(try c.read(a, addr), try c.read(a, dot));
+ try expectEqualStrings("first", try c.read(a, sel));
+ try expectEqual(@as(u64, 5), (try c.stat(sel)).length);
+ try c.write(sel, "FIRST");
+ try expectEqualStrings("FIRST pane", try c.read(a, f.at("/pane/{d}/body", .{first})));
+ try expectEqualStrings("FIRST", try c.read(a, sel)); // what a write to sel put there is selected
+ // exec Msg shows text; exec Save writes the named file; rmdir closes.
+ try c.write(f.at("/pane/{d}/exec", .{first}), "Msg hello from 9P\n");
+ try expect(has(try f.screen(), "hello from 9P"));
+ try expectEqualStrings("1\n", try c.read(a, f.at("/pane/{d}/dirty", .{first})));
+ try c.write(f.at("/pane/{d}/exec", .{first}), "Save\n");
+ try expectEqualStrings("FIRST pane", try d.get(a, "notes.txt"));
+ try expectEqualStrings(f.at("save {d} {s}/notes.txt\n", .{ first, root }), try paneEvent(c, a, log, frozen));
+ try expectEqualStrings(f.at("{d}\n", .{first}), try c.read(a, "/exec"));
+ try expectEqualStrings("0\n", try c.read(a, f.at("/pane/{d}/dirty", .{first})));
+ try c.remove(f.at("/pane/{d}", .{second}));
+ try expectEqualStrings(f.at("del {d} {s}/+New\n", .{ second, root }), try paneEvent(c, a, log, frozen));
+ try expect(!contains(try c.list(a, "/pane"), f.at("{d}", .{second})));
+ // look opens a file; the serial reads back.
+ try d.put("opened.txt", "one\ntwo\nthree\n");
+ try c.write("/look", f.at("{s}/opened.txt:2\n", .{root}));
+ const opened = try std.fmt.parseInt(u32, std.mem.trim(u8, try c.read(a, "/look"), "\n"), 10);
+ try expect(opened != fixture and opened != first and opened != second);
+ try expectEqualStrings("one\ntwo\nthree\n", try c.read(a, f.at("/pane/{d}/body", .{opened})));
+ try expectEqualStrings(f.at("{s}/opened.txt\n", .{root}), try c.read(a, f.at("/pane/{d}/name", .{opened})));
+ try expectEqualStrings(f.at("new {d} {s}/opened.txt\n", .{ opened, root }), try paneEvent(c, a, log, frozen));
+ try c.write(f.at("/pane/{d}/look", .{first}), f.at("{s}/opened.txt:3\n", .{root}));
+ try expectEqualStrings(f.at("{d}\n", .{opened}), try c.read(a, "/look")); // a repeated look targets the open pane
+ for ([_]u32{ fixture, first, opened }) |n| try expect(contains(try c.list(a, "/pane"), f.at("{d}", .{n})));
+ // Writing an event record back still performs the action it names.
+ try c.replace(f.at("/pane/{d}/body", .{first}), "Msg via event");
+ try c.write(f.at("/pane/{d}/event", .{first}), "FX0 13\n");
+ try expect(has(try f.screen(), "via event"));
+ // A command line may not carry control characters, and nothing else in
+ // the tree may be created or removed.
+ if (c.write("/exec", "Msg a\x00b")) |_| return error.Taken else |_| {}
+ if (c.write("/status", "anything\n")) |_| return error.Taken else |_| {}
+ if (c.create("/", "x", c9.dmdir | 0o755)) |_| return error.Taken else |_| {}
+ if (c.create("/pane", "one", c9.dmdir | 0o755)) |_| return error.Taken else |_| {}
+ if (c.create("/pane", "plain-file", 0o644)) |_| return error.Taken else |_| {}
+ if (c.remove("/pane/1/body")) |_| return error.Taken else |_| {}
+ if (c.remove("/index")) |_| return error.Taken else |_| {}
+ c.clunk(log);
+ if (embedded) {
+ try expect(std.mem.startsWith(u8, try c.read(a, "/src/pardes.zig"), "const std"));
+ try expectEqual(@as(u32, 0o444), (try c.stat("/src/pardes.zig")).mode & 0o777);
+ try expect(has(try c.read(a, f.at("/pane/{d}/body", .{try c.look(a, "/virtual/src/pardes.zig", null)})), "pub const Pardes"));
+ }
+ // An open of data with OTRUNC (a shell's >) deletes the addressed range
+ // only, so the write after it replaces that range.
+ const scratch = try c.newPane();
+ try c.write(f.at("/pane/{d}/body", .{scratch}), "hello world\nsecond line\n");
+ try c.write(f.at("/pane/{d}/addr", .{scratch}), "#0,#5");
+ try c.replace(f.at("/pane/{d}/data", .{scratch}), "HOWDY");
+ try expectEqualStrings("HOWDY world\nsecond line\n", try c.read(a, f.at("/pane/{d}/body", .{scratch})));
+ // sam's regexps: ^ at any line; a miss says so and leaves no address,
+ // so the data write after it refuses.
+ try c.write(f.at("/pane/{d}/addr", .{scratch}), "/^second/");
+ try expectEqualStrings(" 12 18 ", try c.read(a, f.at("/pane/{d}/addr", .{scratch})));
+ try refusedSaying(c, f.at("/pane/{d}/addr", .{scratch}), "/nowhere/", "no match for regexp");
+ try refusedSaying(c, f.at("/pane/{d}/data", .{scratch}), "LOST", "no address");
+ try expectEqualStrings("HOWDY world\nsecond line\n", try c.read(a, f.at("/pane/{d}/body", .{scratch})));
+ // Through a mount both read as EINVAL; the log says which and why.
+ try expect(has(try c.read(a, "/log"), f.at("err {d} addr: no match for regexp\n", .{scratch})));
+ try c.remove(f.at("/pane/{d}", .{scratch}));
+ // Real `diff -u` in a command pane: the terminal turns the tab before
+ // each timestamp into blanks, and a look at a changed line still opens
+ // the new file at it.
+ try d.put("diff-old.txt", "one\ntwo\nthree\n");
+ try d.put("diff-new.txt", "one\nTWO\nthree\n");
+ const exits = std.mem.count(u8, try c.read(a, "/log"), "\nexit ");
+ try c.write("/exec", "diff -u diff-old.txt diff-new.txt\n");
+ const deadline = eh.nowMs() + 10_000;
+ while (std.mem.count(u8, try c.read(a, "/log"), "\nexit ") <= exits) {
+ if (eh.nowMs() > deadline) return error.DiffNeverEnded;
+ process.sleep(50);
+ }
+ // A command's pane is kind cmd, not term: no shell to type into.
+ var ran: u32 = 0;
+ var irows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
+ while (irows.next()) |r| {
+ var w = std.mem.tokenizeScalar(u8, r, ' ');
+ const serial = w.next().?;
+ if (std.mem.eql(u8, w.next() orelse "", "cmd")) ran = try std.fmt.parseInt(u32, serial, 10);
+ }
+ const shown = try c.read(a, f.at("/pane/{d}/body", .{ran}));
+ try expect(has(shown, "+++ diff-new.txt") and !has(shown, "\t"));
+ try c.write(f.at("/pane/{d}/look", .{ran}), "+TWO\n");
+ const diffed = try panesAt(a, c, f.at("{s}/diff-new.txt", .{root}));
+ try expectEqual(@as(usize, 1), diffed.len);
+ try c.remove(f.at("/pane/{s}", .{diffed[0]}));
+ try c.remove(f.at("/pane/{d}", .{ran}));
+ // A Save makes the directories its file goes in, whatever names it: the
+ // pane's own name or Save's path. `~` is home (this session's HOME, the
+ // test's root) wherever a path is typed: Save, name, a look, with an
+ // address or not.
+ const tilde = try c.newPane();
+ try c.write(f.at("/pane/{d}/body", .{tilde}), "one\ntwo\nthree\n");
+ try c.write(f.at("/pane/{d}/ctl", .{tilde}), "Save ~/tilde-made/sub/x.txt\n");
+ try expectEqualStrings("one\ntwo\nthree\n", try d.get(a, "tilde-made/sub/x.txt"));
+ try c.write(f.at("/pane/{d}/name", .{tilde}), "~/tilde-named.txt\n");
+ try expectEqualStrings(f.at("{s}/tilde-named.txt\n", .{root}), try c.read(a, f.at("/pane/{d}/name", .{tilde})));
+ try c.remove(f.at("/pane/{d}", .{tilde}));
+ try c.write("/look", "~/tilde-made/sub/x.txt:3\n");
+ const looked = try panesAt(a, c, f.at("{s}/tilde-made/sub/x.txt", .{root}));
+ try expectEqual(@as(usize, 1), looked.len);
+ // At line 3: dot, offset 8, where the look put it (addr is a 9P client's own register).
+ try expect(std.mem.startsWith(u8, std.mem.trimStart(u8, try c.read(a, f.at("/pane/{s}/dot", .{looked[0]})), " "), "8 "));
+ try c.remove(f.at("/pane/{s}", .{looked[0]}));
+ const made_dirs = try c.newPane();
+ try c.write(f.at("/pane/{d}/body", .{made_dirs}), "deep\n");
+ try c.write(f.at("/pane/{d}/ctl", .{made_dirs}), f.at("Save {s}/save-made/a/b/x.txt\n", .{root}));
+ try expectEqualStrings("deep\n", try d.get(a, "save-made/a/b/x.txt"));
+ try c.write(f.at("/pane/{d}/name", .{made_dirs}), f.at("{s}/save-named/c/y.txt\n", .{root}));
+ try c.write(f.at("/pane/{d}/ctl", .{made_dirs}), "Save\n");
+ try expectEqualStrings("deep\n", try d.get(a, "save-named/c/y.txt"));
+ try c.remove(f.at("/pane/{d}", .{made_dirs}));
+ // A save the host cannot do (a directory it may not make) fails the
+ // write that asked for it, and the log's err record names the path and
+ // why.
+ const unsaved = try c.newPane();
+ try c.write(f.at("/pane/{d}/body", .{unsaved}), "unsaved\n");
+ try refusedSaying(c, f.at("/pane/{d}/ctl", .{unsaved}), "Save /nonexistent-pardes-dir/x.txt\n", "Save /nonexistent-pardes-dir/x.txt: permission denied"); // a directory it may not make
+ try expect(has(try c.read(a, "/log"), f.at("err {d} ctl: Save /nonexistent-pardes-dir/x.txt: ", .{unsaved})));
+ try c.remove(f.at("/pane/{d}", .{unsaved}));
+ // Sixteen connections fill the slots; the next is told why.
+ {
+ var extra: [15]*process.Conn = undefined;
+ var n: usize = 0;
+ defer for (extra[0..n]) |e| e.deinit(gpa);
+ while (n < extra.len) : (n += 1) extra[n] = try process.Conn.dial(gpa, d.socket());
+ var words: [256]u8 = undefined;
+ try expect(has(try process.Conn.dialRefusal(gpa, d.socket(), &words), "too many connections"));
+ }
+ const until = eh.nowMs() + 5_000;
+ while (!has(try c.read(a, "/log"), "err - 9p: too many connections")) {
+ if (eh.nowMs() > until) return error.NotLogged;
+ process.sleep(10);
+ }
+}
+
+fn screenSize(f: *process.Files) !struct { i64, i64 } {
+ const json = try std.json.parseFromSliceLeaky(std.json.Value, f.arena.allocator(), try f.read("/screen"), .{});
+ return .{ json.object.get("cols").?.integer, json.object.get("rows").?.integer };
+}
+
+/// A write bigger than a 9P message comes as several, cut anywhere: each
+/// command line runs once, whole, an Edit block once it ends, and a last
+/// line with no newline when the open closes. Never a fragment.
+fn cutWrites(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "cut", .{});
+ defer d.deinit();
+ const f = &d.files;
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const c = try process.Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ // As test/fs.py's client cut them: an 8192-byte msize's writes.
+ const cut = 8192 - 23;
+ var burst: std.ArrayList(u8) = .empty;
+ for (0..1000) |i| try burst.print(a, "Msg burst-{d:0>4}\n", .{i});
+ try expect(burst.items.len > 8192);
+ try c.writeCut("/exec", 1, burst.items, cut);
+ const log = try c.read(a, "/log");
+ var i: usize = 0;
+ var lines = std.mem.tokenizeScalar(u8, log, '\n');
+ while (lines.next()) |line| {
+ if (!std.mem.startsWith(u8, line, "msg ") or !has(line, "burst-")) continue;
+ var w = std.mem.splitScalar(u8, line, ' ');
+ _ = w.next();
+ _ = w.next();
+ try expectEqualStrings(f.at("burst-{d:0>4}", .{i}), w.rest());
+ i += 1;
+ }
+ try expectEqual(@as(usize, 1000), i);
+ try expect(!has(log, "\nrun ") and !has(log, "exit 127"));
+ try c.write("/exec", "Msg no-newline-at-end");
+ const said = try c.read(a, "/log");
+ // An error, not `.?`: a panic would leave the session running.
+ const upto = said[0 .. std.mem.indexOf(u8, said, "no-newline-at-end") orelse return error.NeverRun];
+ try expect(has(upto[upto.len -| 40..], "msg "));
+ const serial = try c.newPane();
+ var block: std.ArrayList(u8) = .empty;
+ try block.appendSlice(a, "Edit ,a\n");
+ for (0..2500) |k| try block.print(a, "line {d:0>5} of the block\n", .{k});
+ try block.appendSlice(a, ".\n");
+ try expect(block.items.len > 50000);
+ try c.writeCut(f.at("/pane/{d}/ctl", .{serial}), 1, block.items, cut);
+ var body = std.mem.splitScalar(u8, std.mem.trimEnd(u8, try c.read(a, f.at("/pane/{d}/body", .{serial})), "\n"), '\n');
+ var k: usize = 0;
+ while (body.next()) |line| : (k += 1) try expectEqualStrings(f.at("line {d:0>5} of the block", .{k}), line);
+ try expectEqual(@as(usize, 2500), k);
+ // A detached session with no frontend is 160x50 until told.
+ const cols, const rows = try screenSize(f);
+ try expect(cols == 160 and rows == 50);
+ try c.write("/ctl", "size 132 40\n");
+ const cols2, const rows2 = try screenSize(f);
+ try expect(cols2 == 132 and rows2 == 40);
+}
+
+fn rootAndOs(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "first", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const root = d.root();
+ const names = try c.list(a, "/");
+ for ([_][]const u8{ "pane", "index", "status", "look", "exec", "log", "os" }) |want| {
+ for (names) |n| {
+ if (std.mem.eql(u8, n, want)) break;
+ } else return error.NotListed;
+ }
+ for (names) |n| try check(!std.mem.eql(u8, n, "self"), "self listed", .{});
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/body"));
+ const os_path = try std.fmt.allocPrint(a, "/os{s}/first.txt", .{root});
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, os_path));
+ const listed = try c.list(a, try std.fmt.allocPrint(a, "/os{s}", .{root}));
+ for (listed) |n| {
+ if (std.mem.eql(u8, n, "first.txt")) break;
+ } else return error.NotListed;
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/../1/body"));
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, try std.fmt.allocPrint(a, "/os{s}/../{s}/first.txt", .{ root, std.fs.path.basename(root) })));
+ try c.replace(os_path, "written through 9P\n");
+ try std.testing.expectEqualStrings("written through 9P\n", try d.get(a, "first.txt"));
+ try std.testing.expectEqualStrings("written through 9P\n", try c.read(a, os_path));
+}
+
+fn frames(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "frames", .{});
+ defer d.deinit();
+ _ = std.c.mkdir(d.abs("listing"), 0o755);
+ _ = symlink(d.abs("listing/missing"), d.abs("listing/broken"));
+ for (0..24) |i| try d.put(try std.fmt.allocPrint(a, "listing/entry-{d:0>2}", .{i}), "");
+ const small = try Conn.dialSized(gpa, d.socket(), 256);
+ defer small.deinit(gpa);
+ const names = try small.list(a, try std.fmt.allocPrint(a, "/os{s}/listing", .{d.root()}));
+ try check(names.len == 24, "listed {d}: {any}", .{ names.len, names });
+ for (0..24) |i| {
+ const want = try std.fmt.allocPrint(a, "entry-{d:0>2}", .{i});
+ for (names) |n| {
+ if (std.mem.eql(u8, n, want)) break;
+ } else return error.NotListed;
+ }
+ const big = try Conn.dial(gpa, d.socket());
+ defer big.deinit(gpa);
+ try std.testing.expectEqual(@as(u32, 65536), big.msizeAgreed());
+}
+
+extern "c" fn symlink(target: [*:0]const u8, path: [*:0]const u8) c_int;
+
+fn frozenScreen(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "frozen", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const frozen = try c.hold("/screen", 0);
+ var before: std.ArrayList(u8) = .empty;
+ try before.appendSlice(a, try c.readFid(frozen, 0, 31));
+ try c.write("/pane/1/body", "screen changed\n");
+ while (true) {
+ const chunk = try c.readFid(frozen, before.items.len, 65536);
+ if (chunk.len == 0) break;
+ try before.appendSlice(a, chunk);
+ }
+ c.clunk(frozen);
+ const json = try std.json.parseFromSliceLeaky(std.json.Value, a, before.items, .{});
+ var old: std.ArrayList(u8) = .empty;
+ for (json.object.get("cells").?.array.items) |cell| try old.appendSlice(a, cell.array.items[0].string);
+ try check(has(old.items, "initial") and !has(old.items, "screen changed"), "the open's screen moved", .{});
+ try check(has(try d.files.screen(), "screen changed"), "a new open shows the old screen", .{});
+ const body = "initial\nscreen changed\n";
+ for (0..12) |i| {
+ const other = try Conn.dial(gpa, d.socket());
+ defer other.deinit(gpa);
+ try check(std.mem.eql(u8, try other.read(a, "/pane/1/body"), body), "connection {d}", .{i});
+ }
+}
+
+fn looks(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "first", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ // A build with its sources (-Dembed-sources) serves them under /src.
+ for (try c.list(a, "/")) |n| if (std.mem.eql(u8, n, "src")) {
+ const source = try c.look(a, "/virtual/src/pardes.zig", null);
+ try check(has(try c.pane(a, source, "body"), "pub const Pardes"), "/virtual/src", .{});
+ };
+ try d.put("index", "OS takes precedence\n");
+ var opened = try c.look(a, "index", null);
+ try std.testing.expectEqualStrings("OS takes precedence\n", try c.pane(a, opened, "body"));
+ opened = try c.look(a, "/n/self/index", null);
+ const index = try c.pane(a, opened, "body");
+ try check(std.mem.startsWith(u8, index, "1 ") and has(index, "first.txt"), "/n/self/index: {s}", .{index});
+ try d.put("explicit.txt", "explicit OS mount\n");
+ opened = try c.look(a, try std.fmt.allocPrint(a, "/n/os{s}", .{d.abs("explicit.txt")}), null);
+ try std.testing.expectEqualStrings("explicit OS mount\n", try c.pane(a, opened, "body"));
+}
+
+fn longNames(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "long-names", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const dir_rel = try a.alloc(u8, 64);
+ @memcpy(dir_rel[0..10], "directory-");
+ @memset(dir_rel[10..], 'd');
+ _ = std.c.mkdir(d.abs(dir_rel), 0o755);
+ const dir = d.abs(dir_rel);
+ for ([_]usize{ 29, 63, 127, 255 }) |length| {
+ const name = try a.alloc(u8, length);
+ @memset(name[0 .. length - 4], 'f');
+ @memcpy(name[length - 4 ..], ".txt");
+ const rel = try std.fmt.allocPrint(a, "{s}/{s}", .{ dir_rel, name });
+ try d.put(rel, "long filename\n");
+ const wire = try std.fmt.allocPrint(a, "/os{s}", .{d.abs(rel)});
+ const listed = try c.list(a, try std.fmt.allocPrint(a, "/os{s}", .{dir}));
+ for (listed) |n| {
+ if (std.mem.eql(u8, n, name)) break;
+ } else return error.NotListed;
+ try std.testing.expectEqualStrings("long filename\n", try c.read(a, wire));
+ try c.replace(wire, "written through long filename\n");
+ try std.testing.expectEqualStrings("written through long filename\n", try d.get(a, rel));
+ const opened = try c.look(a, try std.fmt.allocPrint(a, "/n/os{s}", .{d.abs(rel)}), null);
+ try std.testing.expectEqualStrings("written through long filename\n", try c.pane(a, opened, "body"));
+ try c.removePane(opened); // a long name's tag takes rows
+ }
+ const opened = try c.look(a, try std.fmt.allocPrint(a, "/n/os{s}", .{dir}), null);
+ const listing = try c.pane(a, opened, "body");
+ for (try process.entries(a, dir)) |n| try check(has(listing, try std.fmt.allocPrint(a, "/n/os{s}/{s}", .{ dir, n })), "{s} not listed", .{n});
+}