summaryrefslogtreecommitdiff
path: root/test/differential/session.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-14 14:10:28 -0300
committerGabriel Schneider <[email protected]>2026-09-14 14:20:25 -0300
commit5f24c4a2284a0af84fb9d116f7a39f6a58e42ae9 (patch)
tree8679763439492361fe99ea01f5190e5204fa590c /test/differential/session.zig
downloadcloud9-5f24c4a2284a0af84fb9d116f7a39f6a58e42ae9.tar.gz
cloud9-5f24c4a2284a0af84fb9d116f7a39f6a58e42ae9.zip
Implement base 9P2000 sessions, shared transports, and conformance probes
Diffstat (limited to 'test/differential/session.zig')
-rw-r--r--test/differential/session.zig82
1 files changed, 82 insertions, 0 deletions
diff --git a/test/differential/session.zig b/test/differential/session.zig
new file mode 100644
index 0000000..3c5d2de
--- /dev/null
+++ b/test/differential/session.zig
@@ -0,0 +1,82 @@
+//! Local interoperability client. The parent connects stdio to a reference server.
+const std = @import("std");
+const c9 = @import("cloud9");
+const libc = std.c;
+const Session = struct {
+ client: c9.Client,
+ requests: usize = 0,
+ bytes_read: usize = 0,
+ bytes_written: usize = 0,
+ read_calls: usize = 0,
+ write_calls: usize = 0,
+
+ fn ask(s: *Session, request: c9.Client.Request) !c9.Client.Result {
+ const tag = try s.client.submit(request);
+ s.requests += 1;
+ while (s.client.output().len != 0) {
+ const output = s.client.output();
+ s.write_calls += 1;
+ // Fragment requests across their header and body fields.
+ const n = libc.write(1, output.ptr, @min(output.len, 3));
+ if (n <= 0) return error.Write;
+ s.bytes_written += @intCast(n);
+ s.client.wrote(@intCast(n));
+ }
+ while (true) {
+ if (s.client.take()) |done| {
+ if (done.tag != tag) return error.Tag;
+ if (done.result == .fail) {
+ std.debug.print("remote error: {s}\n", .{done.result.fail});
+ return error.Remote;
+ }
+ return done.result;
+ }
+ if (s.client.dead) return error.Protocol;
+ var buffer: [7]u8 = undefined;
+ s.read_calls += 1;
+ const n = libc.read(0, &buffer, buffer.len);
+ if (n <= 0) return error.Read;
+ s.bytes_read += @intCast(n);
+ if (s.client.push(buffer[0..@intCast(n)]) != n) return error.InputFull;
+ }
+ }
+};
+
+pub fn main(init: std.process.Init) !void {
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
+ if (args.len != 3) return error.Arguments;
+ const seed = try std.fmt.parseInt(u64, args[1], 10);
+ const rounds = try std.fmt.parseInt(usize, args[2], 10);
+ if (rounds == 0 or rounds > 10000) return error.Arguments;
+ var random: std.Random.DefaultPrng = .init(seed);
+ var input: [8192]u8 = undefined;
+ var output: [8192]u8 = undefined;
+ var session: Session = .{ .client = .init(.{ .in = &input, .out = &output }) };
+ const version = (try session.ask(.{ .version = .{} })).version;
+ if (version.msize > input.len or !std.mem.eql(u8, version.version, "9P2000")) return error.Version;
+ _ = try session.ask(.{ .attach = .{ .fid = 0, .uname = "user" } });
+ var expected: [4096]u8 = @splat(0);
+ var length: usize = 0;
+ for (0..rounds) |_| {
+ const walk = (try session.ask(.{ .walk = .{ .fid = 0, .newfid = 1, .names = &.{"file"} } })).walk;
+ if (walk.nwqid != 1 or walk.wqid[0].type & c9.qtdir != 0) return error.Walk;
+ _ = try session.ask(.{ .open = .{ .fid = 1, .mode = c9.ordwr } });
+ const offset = random.random().uintLessThan(usize, 2048);
+ const count = random.random().uintLessThan(usize, 1024) + 1;
+ var data: [1024]u8 = undefined;
+ random.random().bytes(data[0..count]);
+ const written = (try session.ask(.{ .write = .{ .fid = 1, .offset = offset, .data = data[0..count] } })).write;
+ if (written != count) return error.WriteCount;
+ @memcpy(expected[offset..][0..count], data[0..count]);
+ length = @max(length, offset + count);
+ const result = (try session.ask(.{ .read = .{ .fid = 1, .offset = 0, .count = expected.len } })).read;
+ if (!std.mem.eql(u8, result, expected[0..length])) return error.Contents;
+ const stat = (try session.ask(.{ .stat = .{ .fid = 1 } })).stat;
+ if (stat.length != length or !std.mem.eql(u8, stat.name, "file")) return error.Stat;
+ const eof = (try session.ask(.{ .read = .{ .fid = 1, .offset = length, .count = 1 } })).read;
+ if (eof.len != 0) return error.Eof;
+ _ = try session.ask(.{ .clunk = .{ .fid = 1 } });
+ }
+ _ = try session.ask(.{ .clunk = .{ .fid = 0 } });
+ std.debug.print("{{\"requests\":{d},\"bytes_read\":{d},\"bytes_written\":{d},\"read_calls\":{d},\"write_calls\":{d},\"seed\":{d},\"rounds\":{d}}}\n", .{ session.requests, session.bytes_read, session.bytes_written, session.read_calls, session.write_calls, seed, rounds });
+}