summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/kitty.zig144
1 files changed, 144 insertions, 0 deletions
diff --git a/test/kitty.zig b/test/kitty.zig
index 3f9776fa..e760e8d6 100644
--- a/test/kitty.zig
+++ b/test/kitty.zig
@@ -776,3 +776,147 @@ test "kitty: a placement relative to another (P=, Q=) is drawn at its parent's c
r.feed(0, apc("a=d,d=i,i=80,p=1", ""));
try std.testing.expectEqual(@as(usize, 0), (try r.placesOf(80)).len);
}
+
+// ---- What t=f, t=t and t=s may make pardes open ----
+//
+// A program in a pane names paths on pardes's machine. Which paths ghostty's
+// loader accepts is its policy (kitty's, which the user accepts); what pardes
+// owns is that none of them hangs or crashes it or blows its memory: a FIFO
+// (an open for reading waits for a writer), a device (an open may do
+// something), a file no screen could hold. And t=t deletes temporary files
+// only.
+
+extern "c" fn mkfifo(path: [*:0]const u8, mode: std.c.mode_t) c_int;
+extern "c" fn symlink(target: [*:0]const u8, link: [*:0]const u8) c_int;
+extern "c" fn usleep(us: c_uint) c_int;
+extern "c" fn ftruncate(fd: c_int, len: i64) c_int;
+extern "c" fn getcwd(buf: [*]u8, size: usize) ?[*:0]u8;
+
+const Guard = struct {
+ path: [:0]const u8,
+ done: std.atomic.Value(bool) = .init(false),
+ blocked: std.atomic.Value(bool) = .init(false),
+
+ /// After 1.5 s still running, the feed is stuck in an open of the FIFO:
+ /// open it for writing, which lets that open return.
+ fn run(g: *Guard) void {
+ for (0..150) |_| {
+ if (g.done.load(.acquire)) return;
+ _ = usleep(10_000);
+ }
+ g.blocked.store(true, .release);
+ const fd = std.c.open(g.path, .{ .ACCMODE = .WRONLY }, @as(std.c.mode_t, 0));
+ if (fd >= 0) _ = std.c.close(fd);
+ }
+};
+
+/// Feeds `bytes`; whether the feed blocked opening `fifo`.
+fn feedBlocks(r: *Rig, bytes: []const u8, fifo: [:0]const u8) !bool {
+ var g: Guard = .{ .path = fifo };
+ const t = try std.Thread.spawn(.{}, Guard.run, .{&g});
+ r.feed(0, bytes);
+ g.done.store(true, .release);
+ t.join();
+ return g.blocked.load(.acquire);
+}
+
+test "kitty: a FIFO named by t=f, t=t or t=s never blocks pardes" {
+ var r = try Rig.init(1);
+ defer r.deinit();
+ const arena = r.frame.allocator();
+ const pid = std.c.getpid();
+ const Case = struct { medium: u8, fifo: [:0]const u8, name: []const u8 };
+ const file = try std.fmt.allocPrintSentinel(arena, "/tmp/pardes-kitty-{d}-fifo", .{pid}, 0);
+ const temp = try std.fmt.allocPrintSentinel(arena, "/tmp/tty-graphics-protocol-pardes-{d}-fifo", .{pid}, 0);
+ const shm_path = try std.fmt.allocPrintSentinel(arena, "/dev/shm/pardes-kitty-{d}-fifo", .{pid}, 0);
+ const cases = [_]Case{
+ .{ .medium = 'f', .fifo = file, .name = file },
+ .{ .medium = 't', .fifo = temp, .name = temp },
+ .{ .medium = 's', .fifo = shm_path, .name = shm_path["/dev/shm".len..] },
+ };
+ for (cases) |c| {
+ _ = std.c.unlink(c.fifo);
+ if (mkfifo(c.fifo, 0o600) != 0) return error.MkfifoFailed;
+ defer _ = std.c.unlink(c.fifo);
+ const cmd = try std.fmt.allocPrint(arena, "\x1b_Ga=t,t={c},i=90,f=24,s=1,v=1;{s}\x1b\\", .{ c.medium, try b64(arena, c.name) });
+ const blocked = try feedBlocks(&r, cmd, c.fifo);
+ if (blocked) std.debug.print("t={c}: pardes blocked opening the FIFO {s}\n", .{ c.medium, c.fifo });
+ try std.testing.expect(!blocked);
+ try std.testing.expect(std.mem.startsWith(u8, try r.take(0), "\x1b_Gi=90;E"));
+ try std.testing.expect(!r.hasImage(0, 90));
+ }
+}
+
+test "kitty: t=s through a /dev/shm symlink to a FIFO does not block pardes either" {
+ var r = try Rig.init(1);
+ defer r.deinit();
+ const arena = r.frame.allocator();
+ const pid = std.c.getpid();
+ const fifo = try std.fmt.allocPrintSentinel(arena, "/tmp/pardes-kitty-{d}-shmfifo", .{pid}, 0);
+ _ = std.c.unlink(fifo);
+ if (mkfifo(fifo, 0o600) != 0) return error.MkfifoFailed;
+ defer _ = std.c.unlink(fifo);
+ const link = try std.fmt.allocPrintSentinel(arena, "/dev/shm/pardes-kitty-{d}-link", .{pid}, 0);
+ _ = std.c.unlink(link);
+ if (symlink(fifo, link) != 0) return error.SymlinkFailed;
+ defer _ = std.c.unlink(link);
+ const cmd = try std.fmt.allocPrint(arena, "\x1b_Ga=t,t=s,i=91,f=24,s=1,v=1;{s}\x1b\\", .{try b64(arena, link["/dev/shm".len..])});
+ try std.testing.expect(!try feedBlocks(&r, cmd, fifo));
+ try std.testing.expect(std.mem.startsWith(u8, try r.take(0), "\x1b_Gi=91;E"));
+}
+
+test "kitty: t=f and t=t refuse /proc, /sys, devices and directories" {
+ var r = try Rig.init(1);
+ defer r.deinit();
+ const arena = r.frame.allocator();
+ const refused = [_][]const u8{ "/proc/self/status", "/sys/devices/system/cpu/online", "/dev/null", "/dev/zero", "/dev/urandom", "/tmp", "/" };
+ for ([_]u8{ 'f', 't' }) |medium| for (refused) |path| {
+ // A raw 1x1 pixel needs 3 bytes (S=3): any readable file would do.
+ r.feed(0, try std.fmt.allocPrint(arena, "\x1b_Ga=t,t={c},i=92,f=24,s=1,v=1,S=3;{s}\x1b\\", .{ medium, try b64(arena, path) }));
+ try std.testing.expect(std.mem.startsWith(u8, try r.take(0), "\x1b_Gi=92;E"));
+ try std.testing.expect(!r.hasImage(0, 92));
+ };
+}
+
+test "kitty: t=t deletes only a temporary file: one outside the temp directory, or behind a symlink, stays" {
+ var r = try Rig.init(1);
+ defer r.deinit();
+ const arena = r.frame.allocator();
+ const pid = std.c.getpid();
+ var cwd_buf: [4096]u8 = undefined;
+ const cwd = std.mem.span(getcwd(&cwd_buf, cwd_buf.len) orelse return error.NoCwd);
+ // Outside /tmp, /dev/shm and $TMPDIR: the build's own cache.
+ const dir = try std.fmt.allocPrintSentinel(arena, "{s}/.zig-cache/tmp/pardes-kitty-{d}", .{ cwd, pid }, 0);
+ _ = std.c.mkdir(dir, 0o700);
+ defer _ = std.c.rmdir(dir);
+ const kept = try std.fmt.allocPrintSentinel(arena, "{s}/tty-graphics-protocol-kept.rgb", .{dir}, 0);
+ try writeTemp(kept, &rgb_raw);
+ defer _ = std.c.unlink(kept);
+ const link = try std.fmt.allocPrintSentinel(arena, "/tmp/tty-graphics-protocol-pardes-{d}-link", .{pid}, 0);
+ _ = std.c.unlink(link);
+ if (symlink(kept, link) != 0) return error.SymlinkFailed;
+ defer _ = std.c.unlink(link);
+ for ([_][:0]const u8{ kept, link }) |path| {
+ r.feed(0, try std.fmt.allocPrint(arena, "\x1b_Ga=t,t=t,i=93,f=24,s=4,v=2;{s}\x1b\\", .{try b64(arena, path)}));
+ try std.testing.expect(std.mem.startsWith(u8, try r.take(0), "\x1b_Gi=93;E"));
+ try std.testing.expect(!r.hasImage(0, 93));
+ try std.testing.expect(exists(kept));
+ }
+}
+
+test "kitty: a file bigger than a screen may hold is refused, not read whole" {
+ var r = try Rig.init(1);
+ defer r.deinit();
+ const arena = r.frame.allocator();
+ // 100 MiB, sparse: 5120x5120 RGBA, past the 64 MiB a screen holds.
+ const big = try std.fmt.allocPrintSentinel(arena, "/tmp/pardes-kitty-{d}-big.rgba", .{std.c.getpid()}, 0);
+ const fd = std.c.open(big, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(std.c.mode_t, 0o600));
+ if (fd < 0) return error.OpenFailed;
+ _ = ftruncate(fd, 5120 * 5120 * 4);
+ _ = std.c.close(fd);
+ defer _ = std.c.unlink(big);
+ r.feed(0, try std.fmt.allocPrint(arena, "\x1b_Ga=t,t=f,i=94,f=32,s=5120,v=5120;{s}\x1b\\", .{try b64(arena, big)}));
+ const got = try r.take(0);
+ try std.testing.expect(std.mem.startsWith(u8, got, "\x1b_Gi=94;E"));
+ try std.testing.expect(!r.hasImage(0, 94));
+}