diff options
Diffstat (limited to 'test/process.zig')
| -rw-r--r-- | test/process.zig | 126 |
1 files changed, 106 insertions, 20 deletions
diff --git a/test/process.zig b/test/process.zig index 3d74c72b..fd423985 100644 --- a/test/process.zig +++ b/test/process.zig @@ -183,6 +183,33 @@ 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 } }); + } + // fsb: /// `dial`, versioned at `size` (a client of small frames, at least @@ -226,26 +253,77 @@ pub const Conn = struct { return c.cl.msize; } - /// `data` written to `path` truncated first (`>`), in as many writes - /// as it takes. - pub fn replace(c: *Conn, path: []const u8, data: []const u8) !void { - const fid = try c.hold(path, 1 | 16); - var off: usize = 0; - while (off < data.len) { - const n = c.writeFid(fid, off, data[off..@min(data.len, off + c.cl.maxWrite())]) catch |err| { - c.clunk(fid); - return err; - }; - off += n; + + /// 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); + } + + + /// 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)); } - _ = try c.call(.{ .clunk = .{ .fid = fid } }); + return tag; } - /// A new pane, made by opening /pane/new; its serial. - pub fn newPane(c: *Conn, gpa: std.mem.Allocator) !u32 { - const got = try c.read(gpa, "/pane/new"); - defer gpa.free(got); - return std.fmt.parseInt(u32, std.mem.trim(u8, got, "\n"), 10); + /// 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)]); + } } /// The serials /index lists, in its order. @@ -261,15 +339,15 @@ pub const Conn = struct { /// A Look at `path` from pane `source`, or from a scratch pane removed /// after; the one pane it answers (test/fs.py's look). pub fn look(c: *Conn, gpa: std.mem.Allocator, path: []const u8, source: ?u32) !u32 { - const from = source orelse try c.newPane(gpa); + const from = source orelse try c.newPane(); var buf: [64]u8 = undefined; const line = try std.fmt.allocPrint(gpa, "{s}\n", .{path}); defer gpa.free(line); try c.write(try std.fmt.bufPrint(&buf, "/pane/{d}/look", .{from}), line); const got = try c.read(gpa, "/look"); defer gpa.free(got); - const answer = std.mem.trim(u8, got, "\n"); - const opened = std.fmt.parseInt(u32, answer, 10) catch { + const said_back = std.mem.trim(u8, got, "\n"); + const opened = std.fmt.parseInt(u32, said_back, 10) catch { std.debug.print("look {s}: answered {s}\n", .{ path, got }); return error.NotOneLook; }; @@ -649,6 +727,14 @@ fn sleepMs(ms: u32) void { _ = 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); +} + // fsb: extern "c" fn readdir(dir: *libc.DIR) ?*libc.dirent; |
