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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
//! A small tree served by `serve.Runner`, for the tests of Fsys.zig and
//! verbs.zig: a README in the house shape, a ctl that refuses in words, a
//! file longer than a message, one that answers a few bytes a read, one
//! that answers what was written to it, an event file whose reads are
//! held until the test feeds them, a directory, and room for one created
//! file.
const std = @import("std");
const Io = std.Io;
const testing = std.testing;
const fs = @import("fs.zig");
const serve = @import("serve.zig");
const transport = @import("transport.zig");
const post = @import("post.zig");
const TestTree = @This();
pub const Req = fs.Req;
pub const Reply = fs.Reply;
pub const features: fs.Features = .{ .create = true, .remove = true };
pub const Runner = serve.Runner(TestTree, .{ .fid_capacity = 32, .slot_capacity = 4 }, .{ .msize = 8192, .connections = 2, .listeners = 3 });
pub const readme =
\\A tree for tests.
\\
\\README this file
\\ctl write ok; anything else is refused in words
\\echo write a line, read it back heard
\\event reads wait for news
\\sub/ a directory
\\ of one file
\\sub/deep the file in it
\\
\\ cat ctl an example, not a file
\\
;
pub const big_len = 200_000;
pub fn bigByte(i: usize) u8 {
return @intCast('a' + i % 23);
}
const Node = struct { name: []const u8, parent: u64, dir: bool = false, mode: u16 = 0o600 };
const nodes = [_]Node{
.{ .name = "/", .parent = 1, .dir = true, .mode = 0o700 },
.{ .name = "README", .parent = 1, .mode = 0o400 },
.{ .name = "ctl", .parent = 1 },
.{ .name = "big", .parent = 1, .mode = 0o400 },
.{ .name = "short", .parent = 1, .mode = 0o400 },
.{ .name = "echo", .parent = 1 },
.{ .name = "event", .parent = 1 },
.{ .name = "sub", .parent = 1, .dir = true, .mode = 0o700 },
.{ .name = "deep", .parent = 8 },
.{ .name = "new", .parent = 1 },
};
const root = 1;
const readme_node = 2;
const ctl_node = 3;
const big_node = 4;
const short_node = 5;
const echo_node = 6;
const event_node = 7;
const new_node = 10;
const short_text = "a short answer, three bytes a read";
mutex: Io.Mutex = .init,
/// Whether `new` exists (made by a create, gone with a remove).
made: bool = false,
ctl: [64]u8 = undefined,
ctl_len: usize = 0,
heard: [128]u8 = undefined,
heard_len: usize = 0,
words: [fs.errmax]u8 = undefined,
/// The read of `event` held, until `feed` answers it.
held: ?struct { *Runner.Conn, fs.Ticket } = null,
dir: testing.TmpDir = undefined,
path_buf: [transport.sun_path_len]u8 = undefined,
/// The listener's socket and the TCP address, once `start` returns.
unix: [:0]const u8 = undefined,
tcp: Io.net.IpAddress = undefined,
runner: Runner = undefined,
/// Serves the tree on a Unix socket in a temporary directory and on TCP
/// at the loopback address.
pub fn start(t: *TestTree) !void {
t.* = .{};
t.dir = testing.tmpDir(.{});
errdefer t.dir.cleanup();
var real: [std.fs.max_path_bytes]u8 = undefined;
const len = try t.dir.dir.realPath(testing.io, &real);
t.unix = try std.fmt.bufPrintSentinel(&t.path_buf, "{s}/9p", .{real[0..len]}, 0);
t.runner.init(.{ .io = testing.io, .root = root, .handler = .{ .ctx = t, .serve = serveFn } });
_ = try t.runner.listen(.{ .unix = t.unix }, 4);
t.tcp = try t.runner.listen(.{ .tcp = .{ .ip4 = .loopback(0) } }, 4);
}
pub fn stop(t: *TestTree) void {
t.runner.stop();
t.dir.cleanup();
}
/// Answers the held read of `event` with `chunk` once it is held (an empty
/// chunk is the end of the file), or with null hangs its connection up.
pub fn feed(t: *TestTree, chunk: ?[]const u8) !void {
var tries: usize = 0;
while (true) : (tries += 1) {
if (tries == 5000) return error.Timeout;
t.mutex.lockUncancelable(testing.io);
const got = t.held;
t.held = null;
t.mutex.unlock(testing.io);
const conn, const ticket = got orelse {
try testing.io.sleep(.fromMilliseconds(1), .awake);
continue;
};
const bytes = chunk orelse return conn.close();
const Make = struct {
fn make(b: *const []const u8) ?Runner.Conn.Answer {
return .{ .reply = .{ .tag = 0 }, .bytes = b.* };
}
};
_ = conn.answerWith(ticket, &bytes, Make.make);
return;
}
}
fn attrOf(t: *const TestTree, node: u64) fs.Attr {
const n = nodes[node - 1];
const size: u64 = switch (node) {
readme_node => readme.len,
big_node => big_len,
ctl_node => t.ctl_len,
else => 0,
};
return .{ .name = n.name, .node = node, .dir = n.dir, .mode = n.mode, .size = size };
}
fn serveFn(ctx: ?*anyopaque, conn: *Runner.Conn, req: fs.Req) void {
const t: *TestTree = @ptrCast(@alignCast(ctx.?));
if (req.op == .read and req.node == event_node) {
const ticket = conn.hold(&.{ .tag = req.tag, .status = .again }) orelse return;
t.mutex.lockUncancelable(testing.io);
defer t.mutex.unlock(testing.io);
t.held = .{ conn, ticket };
return;
}
var buf: [8192]u8 = undefined;
var bytes: []const u8 = "";
const reply = t.answer(req, &buf, &bytes);
conn.reply(&reply, bytes);
}
fn answer(t: *TestTree, req: fs.Req, buf: []u8, bytes: *[]const u8) fs.Reply {
const ok: fs.Reply = .{ .tag = req.tag };
switch (req.op) {
.lookup => {
if (std.mem.eql(u8, req.data, "..")) return .{ .tag = req.tag, .attr = t.attrOf(nodes[req.node - 1].parent) };
for (nodes, 1..) |n, node| {
if (node == new_node and !t.made) continue;
if (node != root and n.parent == req.node and std.mem.eql(u8, n.name, req.data))
return .{ .tag = req.tag, .attr = t.attrOf(node) };
}
return .fail(req.tag, fs.E.NOENT);
},
.getattr, .setattr => return .{ .tag = req.tag, .attr = t.attrOf(req.node) },
.open => {
if (!req.create) return ok;
if (req.node != root or !std.mem.eql(u8, req.data, "new") or t.made) return .fail(req.tag, fs.E.EXIST);
t.made = true;
return .{ .tag = req.tag, .attr = t.attrOf(new_node) };
},
.release => {
if (req.remove) {
if (req.node != new_node) return .fail(req.tag, fs.E.PERM);
t.made = false;
}
return ok;
},
.readdir => {
var w: Io.Writer = .fixed(buf);
var index: u64 = 0;
for (nodes, 1..) |n, node| {
if (node == root or n.parent != req.node or (node == new_node and !t.made)) continue;
defer index += 1;
if (index < req.off) continue;
w.writeInt(u64, node, .little) catch break;
w.writeByte(@intFromBool(n.dir)) catch break;
w.writeByte(@intCast(n.name.len)) catch break;
w.writeAll(n.name) catch break;
}
bytes.* = w.buffered();
return ok;
},
.read => {
const off: usize = @intCast(req.off);
bytes.* = switch (req.node) {
readme_node => readme[@min(off, readme.len)..],
ctl_node => t.ctl[@min(off, t.ctl_len)..t.ctl_len],
// A few bytes a read: a short read is not the end.
short_node => short_text[@min(off, short_text.len)..][0..@min(3, short_text.len -| off)],
echo_node => t.heard[@min(off, t.heard_len)..t.heard_len],
big_node => blk: {
const n = @min(big_len -| off, buf.len);
for (buf[0..n], off..) |*b, i| b.* = bigByte(i);
break :blk buf[0..n];
},
else => "",
};
bytes.* = bytes.*[0..@min(bytes.len, req.size)];
return ok;
},
.write => switch (req.node) {
ctl_node => {
const line = std.mem.trimEnd(u8, req.data, "\n");
if (!std.mem.eql(u8, line, "ok")) {
const words = std.fmt.bufPrint(&t.words, "unknown command: {s}", .{line}) catch &t.words;
return .{ .tag = req.tag, .status = .err, .errno = fs.E.INVAL, .ename = words };
}
t.ctl_len = @min(req.data.len, t.ctl.len);
@memcpy(t.ctl[0..t.ctl_len], req.data[0..t.ctl_len]);
return .{ .tag = req.tag, .written = @intCast(req.data.len) };
},
echo_node => {
const heard = std.fmt.bufPrint(&t.heard, "heard {s}", .{req.data}) catch return .fail(req.tag, fs.E.NOSPC);
t.heard_len = heard.len;
return .{ .tag = req.tag, .written = @intCast(req.data.len) };
},
event_node, new_node => return .{ .tag = req.tag, .written = @intCast(req.data.len) },
else => return .fail(req.tag, fs.E.PERM),
},
}
}
|