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.zig178
1 files changed, 178 insertions, 0 deletions
diff --git a/test/process/tree.zig b/test/process/tree.zig
new file mode 100644
index 00000000..c7b9e657
--- /dev/null
+++ b/test/process/tree.zig
@@ -0,0 +1,178 @@
+//! docs/typ/reference.typ, The tree <tree>, through a real session's
+//! socket: what a client on the wire sees, and /os on the disk.
+const std = @import("std");
+const process = @import("../process.zig");
+const Conn = process.Conn;
+const has = process.has;
+const check = process.check;
+
+pub const cases = .{
+ .{ "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: a ctl line with no newline is held whatever its write's size, and runs at the close, its failure in the log", heldLine },
+ .{ "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 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 (names) |n| try check(std.mem.startsWith(u8, n, "entry-"), "listed {s}", .{n});
+ 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 heldLine(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, "held", .{});
+ defer d.deinit();
+ // 64 KiB frames: a line with no newline is held whatever the size of its
+ // write (a mount cuts lines anywhere), and runs at the close.
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const ctl = try c.hold("/pane/1/ctl", 1);
+ const line = try a.alloc(u8, 5 + 20000);
+ @memcpy(line[0..5], "bogus");
+ @memset(line[5..], 'x');
+ _ = try c.writeFid(ctl, 0, line);
+ try check(!has(try c.read(a, "/log"), "ctl: unknown control message"), "ran before the close", .{});
+ c.clunk(ctl);
+ try check(has(try c.read(a, "/log"), "ctl: unknown control message"), "never ran", .{});
+}
+
+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});
+}