1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
//! 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: /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 (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});
}
|