summaryrefslogtreecommitdiff
path: root/test/process/root_ctl.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/process/root_ctl.zig')
-rw-r--r--test/process/root_ctl.zig183
1 files changed, 183 insertions, 0 deletions
diff --git a/test/process/root_ctl.zig b/test/process/root_ctl.zig
new file mode 100644
index 00000000..008cc882
--- /dev/null
+++ b/test/process/root_ctl.zig
@@ -0,0 +1,183 @@
+//! docs/typ/reference.typ, The root ctl <root-ctl>, through a real
+//! session's socket: it exits, keeps its own clock and saves.
+const std = @import("std");
+const libc = std.c;
+const process = @import("../process.zig");
+const eh = @import("../e2e_harness.zig");
+const expect = std.testing.expect;
+const expectEqualStrings = std.testing.expectEqualStrings;
+const Conn = process.Conn;
+const has = process.has;
+const check = process.check;
+
+pub const cases = .{
+ .{ "root-ctl: Exit is answered before the editor quits", exitAnswers },
+ .{ "root-ctl: with nothing attached a message leaves after MessageLinger on its own clock", messagesExpire },
+ .{ "root-ctl: Putall with one pane's file changed on disk fails, answered once the other panes' saves have landed", putallAfterSaves },
+ .{ "root-ctl: long and odd paths where a path goes (name, look, DumpDir, Dump, Save) are refused or said, never a crash; a bad ThemeFile fails the write", oddPaths },
+ .{ "root-ctl: Restore asks once about unsaved text, is answered before it cuts every connection, and the dump's panes come back under their serials", restore },
+};
+
+fn exitAnswers(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "exit", .{});
+ defer d.deinit();
+ try d.files.write("/ctl", "Exit\n");
+ if (d.exited(5_000) == null) return error.StillRunning;
+}
+
+/// Nothing attached, no key, no click, no other request comes to move it along.
+fn messagesExpire(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "msg", .{});
+ defer d.deinit();
+ try d.files.write("/pane/1/ctl", "Msg lingering-words\n");
+ try expect(has(try d.files.screen(), "lingering-words"));
+ // Reading /screen is itself a request, so wait without asking.
+ process.sleep(2500);
+ try expect(!has(try d.files.screen(), "lingering-words"));
+}
+
+fn putallAfterSaves(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "putall", .{});
+ defer d.deinit();
+ const f = &d.files;
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const ar = arena.allocator();
+ const c = try process.Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ try d.put("a.txt", "a\n");
+ try d.put("b.txt", "b\n");
+ const a = try c.look(ar, f.at("{s}/a.txt", .{d.root()}), null);
+ const b = try c.look(ar, f.at("{s}/b.txt", .{d.root()}), null);
+ for ([_]u32{ a, b }) |serial| try c.write(f.at("/pane/{d}/body", .{serial}), "edited\n");
+ try d.put("b.txt", "changed on disk\n");
+ const deadline = eh.nowMs() + 10_000;
+ while (!has(try c.read(ar, "/log"), f.at("changed {d}", .{b}))) {
+ if (eh.nowMs() > deadline) return error.NeverHeard;
+ process.sleep(50);
+ }
+ try std.testing.expectError(error.Refused, c.write("/ctl", "Putall\n"));
+ try expect(has(c.said, "modified on disk"));
+ try expectEqualStrings("a\nedited\n", try d.get(ar, "a.txt"));
+ try expectEqualStrings("changed on disk\n", try d.get(ar, "b.txt"));
+}
+
+/// A part over 255 bytes (ENAMETOOLONG), a loop of links (ELOOP), a file
+/// taken for a directory (ENOTDIR), one not ours (EACCES).
+fn oddPaths(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "odd", .{});
+ defer d.deinit();
+ const f = &d.files;
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const c = try process.Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const root = d.root();
+ const loop = try std.fmt.allocPrintSentinel(a, "{s}/loop", .{root}, 0);
+ try expect(libc.symlink(loop, loop) == 0);
+ try d.put("plain", "x\n");
+ const long: [300]u8 = @splat('x');
+ const odd = [_][]const u8{
+ f.at("/tmp/{s}/f.txt", .{&long}), f.at("{s}/{s}", .{ root, &long }), f.at("{s}/loop/f", .{root}),
+ f.at("{s}/plain/f", .{root}), "/root/nope/f", f.at("{s}/a/../b.txt", .{root}),
+ };
+ const serial = try c.newPane();
+ try c.write(f.at("/pane/{d}/body", .{serial}), "text\n");
+ for (odd) |path| {
+ for ([_][2][]const u8{
+ .{ f.at("/pane/{d}/name", .{serial}), path }, .{ "/look", path }, .{ "/ctl", f.at("DumpDir {s}", .{path}) },
+ .{ "/ctl", "Dump" }, .{ f.at("/pane/{d}/ctl", .{serial}), "Save" },
+ }) |w| {
+ c.write(w[0], f.at("{s}\n", .{w[1]})) catch {};
+ try expect((try c.read(a, "/index")).len > 0);
+ }
+ }
+ try c.write("/ctl", "DumpDir\n");
+ // A theme file that is not there, or no theme, fails the write.
+ try d.put("bad.zon", ".{ .nonsense = 1 }");
+ for ([_][2][]const u8{
+ .{ f.at("ThemeFile {s}/missing.zon\n", .{root}), "theme file" },
+ .{ f.at("ThemeFile {s}/bad.zon\n", .{root}), "theme file" },
+ .{ "ThemeFile notes.txt\n", "a theme file is a .zon" },
+ }) |t| {
+ try std.testing.expectError(error.Refused, c.write("/ctl", t[0]));
+ try expect(has(c.said, t[1]));
+ }
+}
+
+/// A connection to `path`, waiting up to 5 s for it to answer.
+fn redial(gpa: std.mem.Allocator, path: []const u8) !*Conn {
+ const deadline = process.nowMs() + 5_000;
+ while (true) {
+ if (Conn.dial(gpa, path)) |c| return c else |err| if (process.nowMs() > deadline) return err;
+ process.nap(20);
+ }
+}
+
+fn restore(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ for ([_]bool{ false, true }) |tty| {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const name = if (tty) "restore tty space" else "restore detached space";
+ var d = try process.Detached.start(gpa, exe, if (tty) "restore-root" else name, .{});
+ defer d.deinit();
+ var term: ?process.Child = null;
+ defer if (term) |*t| t.deinit();
+ if (tty) term = try d.startTty(exe, name, &.{});
+ const address = d.abs(try std.fmt.allocPrint(a, "pardes-9p-{s}.sock", .{name}));
+ const old = try Conn.dial(gpa, address);
+ defer old.deinit(gpa);
+ try old.replace("/pane/1/body", "dumped state\n");
+ const control = try old.newPane();
+ try old.execRefused(control, try std.fmt.allocPrint(a, "Restore {s}\n", .{d.abs("missing dump.zon")}), "no such file");
+ try std.testing.expectEqualStrings("dumped state\n", try old.read(a, "/pane/1/body"));
+ try old.exec(control, try std.fmt.allocPrint(a, "Mount own {s}\n", .{address}));
+ try old.exec(control, "Dump\n");
+ const saved_rel = try std.fmt.allocPrint(a, "{s}.dump.zon", .{name});
+ const saved = d.abs(saved_rel);
+ const deadline = process.nowMs() + 5_000;
+ while (!d.exists(saved_rel)) {
+ if (process.nowMs() > deadline) return error.NoDump;
+ process.nap(5);
+ }
+ const held_body = try old.hold("/pane/1/body", 0);
+ const held_screen = try old.hold("/screen", 0);
+ const old_ids = try old.serials(a);
+ try std.testing.expectEqualStrings("dumped state\n", try old.readFid(held_body, 0, 65536));
+ try std.testing.expectEqualStrings("{", try old.readFid(held_screen, 0, 1));
+ try old.replace("/pane/1/body", "changed after dump\n");
+ // Unsaved text is asked about once, as Exit asks; the second Restore
+ // goes ahead, and is answered before the hang-up.
+ const line = try std.fmt.allocPrint(a, "Restore {s}\n", .{saved});
+ if (old.exec(control, line)) |_| {} else |err| {
+ if (err != error.Refused) return err;
+ try check(has(old.said, "Modified"), "Restore refused: {s}", .{old.said});
+ }
+ try std.testing.expectEqualStrings("changed after dump\n", try old.read(a, "/pane/1/body"));
+ // Answered before the hang-up: the write's reply comes; the clunk
+ // after it may meet the connection already cut.
+ const restore_fid = try old.hold(try std.fmt.allocPrint(a, "/pane/{d}/exec", .{control}), 1);
+ _ = try old.writeFid(restore_fid, 0, line);
+ old.clunk(restore_fid);
+ // Refused while the connections are cut, or cut already.
+ if (old.readFid(held_body, 0, 65536)) |_| return error.OldFidUsable else |_| {}
+ const restored = try redial(gpa, address);
+ defer restored.deinit(gpa);
+ // The new log names its panes, then where they came from.
+ const log = try restored.read(a, "/log");
+ try check(has(log, try std.fmt.allocPrint(a, "\nrestore {s}\n", .{saved})), "no restore logged: {s}", .{log});
+ // Its panes are the dump's, their serials too.
+ const ids = try restored.serials(a);
+ try check(ids.len == 2 and std.mem.eql(u32, ids, old_ids), "restored {any}, dumped {any}", .{ ids, old_ids });
+ try std.testing.expectEqualStrings("dumped state\n", try restored.pane(a, ids[0], "body"));
+ try check((try restored.pane(a, ids[1], "body")).len == 0, "commands ran through exec, not the body", .{});
+ var screen: process.Files = .init(gpa, address);
+ defer screen.deinit();
+ const width = try std.unicode.utf8CountCodepoints(std.mem.sliceTo(try screen.screen(), '\n'));
+ try std.testing.expectEqual(@as(usize, if (tty) 80 else 160), width); // a detached one is 160x50
+ const opened = try restored.look(a, try std.fmt.allocPrint(a, "/n/own/os{s}", .{d.abs(try std.fmt.allocPrint(a, "{s}.txt", .{name}))}), ids[1]);
+ try std.testing.expectEqualStrings("initial\n", try restored.pane(a, opened, "body"));
+ }
+}