diff options
Diffstat (limited to 'test/process.zig')
| -rw-r--r-- | test/process.zig | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/test/process.zig b/test/process.zig index f797c063..56fe12ba 100644 --- a/test/process.zig +++ b/test/process.zig @@ -182,6 +182,119 @@ pub const Conn = struct { } return names.toOwnedSlice(gpa); } + + // fsa: + + /// `data` written to `path` opened OTRUNC (a shell's `>`), the open + /// clunked after; a refusal by a write or the clunk is `error.Refused`. + pub fn replace(c: *Conn, path: []const u8, data: []const u8) !void { + try c.writeCut(path, 1 | 16, data, msize - 24); + } + + /// `data` written to `path` (opened `mode`) in writes of at most `cut` + /// bytes, as a client with a smaller msize cuts them. + pub fn writeCut(c: *Conn, path: []const u8, mode: u8, data: []const u8, cut: usize) !void { + const fid = try c.hold(path, mode); + var at: usize = 0; + while (at < data.len) { + const n = c.writeFid(fid, at, data[at..@min(data.len, at + cut)]) catch |err| { + c.clunk(fid); + return err; + }; + if (n == 0) { + c.clunk(fid); + return error.ShortWrite; + } + at += n; + } + _ = try c.call(.{ .clunk = .{ .fid = fid } }); + } + + /// A file or directory (`perm` with c9.dmdir) made in `dir`. + pub fn create(c: *Conn, dir: []const u8, name: []const u8, perm: u32) !void { + const fid = try c.walk(dir); + defer c.clunk(fid); + _ = try c.call(.{ .create = .{ .fid = fid, .name = name, .perm = perm, .mode = 0 } }); + } + + /// A new pane, made by an open of /pane/new; its serial. + pub fn newPane(c: *Conn) !u32 { + const fid = try c.hold("/pane/new", 0); + defer c.clunk(fid); + return std.fmt.parseInt(u32, std.mem.trim(u8, try c.readFid(fid, 0, 64), "\n"), 10); + } + + /// The newest pane's serial: /index is ordered by serial. + pub fn newest(c: *Conn, gpa: std.mem.Allocator) !u32 { + const index = try c.read(gpa, "/index"); + defer gpa.free(index); + var rows = std.mem.splitBackwardsScalar(u8, std.mem.trimEnd(u8, index, "\n"), '\n'); + const last = rows.first(); + return std.fmt.parseInt(u32, last[0 .. std.mem.indexOfScalar(u8, last, ' ') orelse last.len], 10); + } + + /// A right click on `path` from a fresh scratch, which goes after: the + /// one pane /look names, never the scratch. + pub fn look(c: *Conn, gpa: std.mem.Allocator, path: []const u8) !u32 { + const scratch = try c.newPane(); + var buf: [4200]u8 = undefined; + try c.write(try std.fmt.bufPrint(&buf, "/pane/{d}/look", .{scratch}), try std.fmt.bufPrint(buf[64..], "{s}\n", .{path})); + const got = try c.read(gpa, "/look"); + defer gpa.free(got); + var rows = std.mem.tokenizeAny(u8, got, " \n"); + const opened = try std.fmt.parseInt(u32, rows.next() orelse return error.NothingOpened, 10); + if (rows.next() != null or opened == scratch) return error.NotOnePane; + try c.remove(try std.fmt.bufPrint(&buf, "/pane/{d}", .{scratch})); + return opened; + } + + /// The words a server refuses a new connection's Tversion with (its + /// slots all taken), copied into `buf`; error.NotRefused when it took it. + pub fn dialRefusal(gpa: std.mem.Allocator, path: []const u8, buf: []u8) ![]const u8 { + const c = try gpa.create(Conn); + defer gpa.destroy(c); + c.* = .{ .fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0), .cl = undefined }; + c.cl = c9.Client.init(.{ .in = &c.in, .out = &c.out }); + if (c.fd < 0) return error.NoSocket; + defer _ = libc.close(c.fd); + var addr: libc.sockaddr.un = .{ .path = @splat(0) }; + if (path.len >= addr.path.len) return error.NameTooLong; + @memcpy(addr.path[0..path.len], path); + if (libc.connect(c.fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) != 0) return error.ConnectFailed; + _ = c.call(.{ .version = .{ .msize = msize } }) catch |err| { + if (err != error.Refused) return err; + const n = @min(buf.len, c.said.len); + @memcpy(buf[0..n], c.said[0..n]); + return buf[0..n]; + }; + return error.NotRefused; + } + + /// A request sent without waiting for its answer (several in flight on + /// one connection); its tag. + pub fn send(c: *Conn, req: c9.Client.Request) !u16 { + const tag = c.cl.submit(req) catch return error.Protocol; + while (c.cl.output().len > 0) { + const out = c.cl.output(); + const n = libc.write(c.fd, out.ptr, out.len); + if (n <= 0) return error.Hangup; + c.cl.wrote(@intCast(n)); + } + return tag; + } + + /// The next answer to come, to whichever request `send` sent; a read's + /// bytes valid until the next call. + pub fn answer(c: *Conn) !c9.Client.Done { + while (true) { + if (c.cl.take()) |done| return done; + var buf: [msize]u8 = undefined; + const n = libc.read(c.fd, &buf, buf.len); + if (n <= 0) return error.Hangup; + var at: usize = 0; + while (at < n) at += c.cl.push(buf[at..@intCast(n)]); + } + } }; /// A running pardes's file tree, dialled by its pid or socket path. @@ -482,3 +595,11 @@ fn sleepMs(ms: u32) void { var ts: libc.timespec = .{ .sec = 0, .nsec = @as(isize, ms) * std.time.ns_per_ms }; _ = libc.nanosleep(&ts, null); } + +// fsa: + +/// A wait of `ms` milliseconds, for what a test gives a session time to do. +pub fn sleep(ms: u32) void { + var ts: libc.timespec = .{ .sec = @intCast(ms / 1000), .nsec = @as(isize, ms % 1000) * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); +} |
