summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/behaviour/columns_and_tags.zig3
-rw-r--r--test/behaviour/event.zig11
-rw-r--r--test/behaviour/limits.zig11
-rw-r--r--test/behaviour/look_and_exec.zig42
-rw-r--r--test/behaviour/settings.zig34
-rw-r--r--test/behaviour/tree.zig15
-rw-r--r--test/fs.py1991
-rw-r--r--test/process.zig530
-rw-r--r--test/process/builtins.zig52
-rw-r--r--test/process/edit.zig139
-rw-r--r--test/process/flags.zig780
-rw-r--r--test/process/look_and_exec.zig239
-rw-r--r--test/process/mounts.zig212
-rw-r--r--test/process/other_files.zig119
-rw-r--r--test/process/repls.zig79
-rw-r--r--test/process/root_ctl.zig183
-rw-r--r--test/process/terminals.zig245
-rw-r--r--test/process/tree.zig636
-rw-r--r--test/process_test.zig9
-rw-r--r--test/v9fs.py196
-rw-r--r--test/v9fs_driver_test.py26
-rw-r--r--test/v9fs_terminal.py108
22 files changed, 3334 insertions, 2326 deletions
diff --git a/test/behaviour/columns_and_tags.zig b/test/behaviour/columns_and_tags.zig
index b9e3b3f5..22f55774 100644
--- a/test/behaviour/columns_and_tags.zig
+++ b/test/behaviour/columns_and_tags.zig
@@ -133,6 +133,9 @@ test "columns-and-tags: Delcol on the last column leaves the window, drawn and t
try s.write("/ctl", "Newcol\n");
try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "New\n");
try expectEqualStrings(s.at("{d}:{d}", .{ try s.column(0), try s.focused() }), try s.columns());
+ // And 9P's new makes a pane there.
+ const made = try s.newPane();
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("\n{d} ", .{made})) != null);
}
test "columns-and-tags: New is a column tag's word, in no pane's tag nor the workspace's" {
diff --git a/test/behaviour/event.zig b/test/behaviour/event.zig
index 77a80e30..65222175 100644
--- a/test/behaviour/event.zig
+++ b/test/behaviour/event.zig
@@ -69,3 +69,14 @@ test "event: a bad event record says what is wrong with it" {
try std.testing.expect(has(s.said, c[1]));
}
}
+
+test "event: a write to the body is an EI record to the reader holding event" {
+ var s = try Session.initFile(std.testing.allocator, "replaced\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const event = try s.hold(s.at("/pane/{d}/event", .{n}), 0);
+ defer s.clunk(event);
+ try s.write(s.at("/pane/{d}/body", .{n}), "event\n");
+ const record = try s.readFid(event);
+ try std.testing.expect(std.mem.startsWith(u8, record, "EI") and has(record, "event\n"));
+}
diff --git a/test/behaviour/limits.zig b/test/behaviour/limits.zig
index d84d4490..35dde65e 100644
--- a/test/behaviour/limits.zig
+++ b/test/behaviour/limits.zig
@@ -38,3 +38,14 @@ test "limits: a ctl line over 1 MiB is refused once, EINVAL, and its tail is not
// What came after the long line's newline ran.
try s.expect(s.at("/pane/{d}/body", .{n}), "");
}
+
+test "limits: a ctl line with no newline is held whatever its write's size, and runs at the close, its failure in the log" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const ctl = try s.hold(s.at("/pane/{d}/ctl", .{try s.focused()}), 1);
+ const line = "bogus" ++ @as([20000]u8, @splat('x'));
+ try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ctl, line));
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "ctl: unknown control message") == null);
+ s.clunk(ctl);
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "ctl: unknown control message") != null);
+}
diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig
index 0d8056da..c46ca394 100644
--- a/test/behaviour/look_and_exec.zig
+++ b/test/behaviour/look_and_exec.zig
@@ -713,3 +713,45 @@ test "look-and-exec: a pane's tagexec runs a word as a click in its tag, and rea
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "from the tag") != null);
try s.expect(tagexec, s.at("{d}\n", .{n}));
}
+
+test "look-and-exec: a look at a directory opens a pane listing it, as acme's directory window: named with a trailing /, its entries, ctl's isdir 1, get reading it again" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.createDirPath(std.testing.io, "listed/sub");
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "listed/.dot", .data = "" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const dir = s.at("{s}/listed", .{try tmpPath(&s, &tmp)});
+ const scratch = try s.newPane();
+ try s.write(s.at("/pane/{d}/look", .{scratch}), s.at("{s}\n", .{dir}));
+ const serial = try std.fmt.parseInt(u32, std.mem.trim(u8, try s.read("/look"), "\n"), 10);
+ try std.testing.expect(serial != scratch);
+ const words = struct {
+ fn of(text: []const u8, buf: [][]const u8) []const []const u8 {
+ var it = std.mem.tokenizeAny(u8, text, " \n");
+ var n: usize = 0;
+ while (it.next()) |w| : (n += 1) buf[n] = w;
+ return buf[0..n];
+ }
+ }.of;
+ var buf: [16][]const u8 = undefined;
+ var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
+ const row = while (rows.next()) |r| {
+ if (std.mem.startsWith(u8, r, s.at("{d} ", .{serial}))) break words(r, &buf);
+ } else return error.NoRow;
+ try std.testing.expectEqualStrings("text", row[1]);
+ try std.testing.expectEqualStrings("0", row[2]);
+ try std.testing.expectEqualStrings(s.at("{s}/", .{dir}), row[3]);
+ const body = words(try s.read(s.at("/pane/{d}/body", .{serial})), &buf);
+ try std.testing.expectEqual(@as(usize, 2), body.len);
+ try std.testing.expectEqualStrings(".dot", body[0]);
+ try std.testing.expectEqualStrings("sub/", body[1]);
+ try std.testing.expectEqualStrings("1", words(try s.read(s.at("/pane/{d}/ctl", .{serial})), &buf)[3]);
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "listed/later", .data = "" });
+ try s.write(s.at("/pane/{d}/ctl", .{serial}), "get\n");
+ const again = words(try s.read(s.at("/pane/{d}/body", .{serial})), &buf);
+ try std.testing.expectEqual(@as(usize, 3), again.len);
+ try std.testing.expectEqualStrings(".dot", again[0]);
+ try std.testing.expectEqualStrings("later", again[1]);
+ try std.testing.expectEqualStrings("sub/", again[2]);
+}
diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig
index a24a3010..9a14ff50 100644
--- a/test/behaviour/settings.zig
+++ b/test/behaviour/settings.zig
@@ -269,3 +269,37 @@ test "settings: every on/off setting the root ctl reads flips when its word is w
try std.testing.expectEqualStrings("Theme acme", try setting(&s, "Theme"));
try std.testing.expectEqualStrings("Shell /bin/sh", try setting(&s, "Shell"));
}
+
+/// A cell's style (one of `Session.styles`) as its bold flag and ink.
+fn look(s: *Session, style: []const u8) !struct { bool, []const u8 } {
+ const a = s.arena.allocator();
+ const json = try std.json.parseFromSliceLeaky(std.json.Value, a, style, .{});
+ return .{ json.object.get("bold").?.bool, try std.json.Stringify.valueAlloc(a, json.object.get("fg").?, .{}) };
+}
+
+test "settings: a keyword is coloured apart from the text beside it, and not bold (SyntaxBold is off by default), at a long file's end" {
+ var s = try Session.initFile(std.testing.allocator, "initial\n");
+ defer s.deinit();
+ const n = try s.focused();
+ try s.write(s.at("/pane/{d}/name", .{n}), "keywords.zig\n");
+ var source: std.ArrayList(u8) = .empty;
+ for (0..9999) |_| try source.appendSlice(s.arena.allocator(), "// padding\n");
+ try source.appendSlice(s.arena.allocator(), "fn check() void {\n if (true) return;\n}\n");
+ try s.replace(s.at("/pane/{d}/body", .{n}), source.items);
+ const screen = try s.screen();
+ const styles = try s.styles();
+ const cols = std.mem.indexOfScalar(u8, screen, '\n').?;
+ try std.testing.expectEqual(styles.len, cols * (std.mem.count(u8, screen, "\n") + 1));
+ for ([_][]const u8{ "fn", "if" }) |word| {
+ const col, const row = try s.where(s.at("{s} ", .{word}));
+ const at = row * cols + col;
+ _, const ink = try look(&s, styles[at]);
+ for (styles[at..][0..word.len]) |cell| {
+ const bold, const each = try look(&s, cell);
+ try std.testing.expect(!bold);
+ try std.testing.expectEqualStrings(ink, each);
+ }
+ _, const plain = try look(&s, styles[at + word.len]);
+ try std.testing.expect(!std.mem.eql(u8, ink, plain));
+ }
+}
diff --git a/test/behaviour/tree.zig b/test/behaviour/tree.zig
index 7f9b37a4..56806589 100644
--- a/test/behaviour/tree.zig
+++ b/test/behaviour/tree.zig
@@ -42,3 +42,18 @@ test "tree: a terminal is listed as term with its directory as name, which a wri
try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/name", .{n}), "/elsewhere\n"));
try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid rename: a terminal is named"));
}
+
+test "tree: a body takes a 60 KB write whole, a truncating one replaces it, and a pane not there is refused" {
+ var s = try Session.initFile(std.testing.allocator, "initial\n");
+ defer s.deinit();
+ var payload: std.ArrayList(u8) = .empty;
+ for (0..3000) |_| try payload.appendSlice(s.arena.allocator(), "const value = \"λ\";\n");
+ const n = try s.newPane();
+ const body = s.at("/pane/{d}/body", .{n});
+ try s.write(body, payload.items);
+ try s.expect(body, payload.items);
+ try s.replace(body, "replaced\n");
+ try s.expect(body, "replaced\n");
+ try std.testing.expectError(error.Refused, s.read("/pane/4294967295/body"));
+ try s.expect(body, "replaced\n");
+}
diff --git a/test/fs.py b/test/fs.py
index c5dd6234..a2b67cbc 100644
--- a/test/fs.py
+++ b/test/fs.py
@@ -113,11 +113,6 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False
assert not reader.is_alive(), 'terminal reader did not stop'
-def newest(client):
- """The newest pane's serial: /index is ordered by serial."""
- return int(client.read('/index').splitlines()[-1].split()[0])
-
-
def new_pane(client, contents):
"""An open of /pane/new makes a pane; the read names it."""
serial = int(client.read('/pane/new'))
@@ -126,1993 +121,11 @@ def new_pane(client, contents):
return serial
-def look(client, path, source=None):
- """A right click on `path` from a pane (a fresh scratch by default)."""
- serial = source if source is not None else new_pane(client, b'')
- client.write(f'/pane/{serial}/look', f'{path}\n'.encode())
- results = [int(row) for row in client.read('/look').split()]
- assert len(results) == 1 and results[0] != serial, (path, results)
- # The scratch it looked from goes: a column holds only so many panes,
- # each its tag and two rows.
- if source is None:
- client.remove(f'/pane/{serial}')
- return results[0]
-
-
def execute(client, serial, command):
"""A middle click on `command` from a pane."""
client.write(f'/pane/{serial}/exec', f'{command}\n'.encode())
-def execute_refused(client, serial, command, words):
- """A middle click on a builtin that fails: the write fails, saying why."""
- try:
- execute(client, serial, command)
- except OSError as why:
- assert words in str(why), (command, why)
- return
- raise AssertionError(f'{command!r} was taken')
-
-
-def walk_tree(client, path='/', skip=('/log', '/os', '/screen')):
- """Stat every entry below `path`, as `find` does, without opening anything."""
- seen = []
- for name in client.list(path):
- child = (path.rstrip('/') + '/' + name)
- info = client.stat(child)
- seen.append((child, info))
- if info['dir'] and child not in skip:
- seen.extend(walk_tree(client, child, skip))
- return seen
-
-
-def discovery(binary, embedded=False):
- with tempfile.TemporaryDirectory(prefix='pardes-discovery-') as directory:
- root = Path(directory)
- with session(binary, root, 'discovery') as (client, address):
- top = client.list('/')
- # A set: which files the root holds, not the order a listing gives.
- assert set(top) - {'src', 'shaders'} == {'README', 'index', 'status', 'focus', 'ctl', 'commands', 'recent',
- 'look', 'exec', 'pager', 'log', 'screen', 'listeners', 'layout', 'tag', 'tagexec',
- 'pane', 'col', 'os'}, top
- assert 'self' not in top and 'cons' not in top and 'new' not in top, top
- assert ('src' in top) == embedded, (top, embedded)
- before = client.read('/index')
- guide = client.read('/README')
- assert guide.count(b'\n') <= 45 and b'Msg hello' in guide and b'pane/new' in guide, guide
- assert client.stat('/README')['length'] == len(guide)
- # ls/stat/find over the whole tree, without opening, creates nothing.
- seen = walk_tree(client)
- names = {path for path, _ in seen}
- assert {'/index', '/status', '/look', '/exec', '/log', '/pane/1', '/pane/1/body',
- '/pane/1/name', '/pane/1/sel', '/pane/1/ctl', '/pane/1/tag', '/pane/1/event',
- '/pane/1/dot', '/pane/1/limit', '/pane/1/dirty', '/pane/1/mark',
- '/pane/1/scroll', '/pane/1/look', '/pane/1/exec'} <= names, sorted(names)
- assert '/pane/1/pty' not in names, 'the fixture file pane grew a pty/'
- assert not any(name.startswith('/self') or name.endswith('/rdsel') or name.endswith('/wrsel')
- for name in names), sorted(names)
- for path, info in seen:
- assert info['mode'] in (0o755, 0o644, 0o666, 0o444, 0o222, 0o555), (path, info)
- assert info['mtime'] > 0, (path, info)
- assert client.read('/index') == before, 'browsing created a pane'
- assert client.stat('/index')['length'] == len(before) > 0
- index = client.stat('/index')
- assert index['mode'] == 0o444 and client.stat('/status')['mode'] == 0o444
- assert client.stat('/exec')['mode'] == 0o666
- assert client.stat('/pane/1/body')['mode'] == 0o666 and client.stat('/pane')['mode'] == 0o755
- # A range file reads the pair it takes; a flag file reads 0 or 1.
- assert client.stat('/pane/1/addr')['length'] == 24
- assert client.stat('/pane/1/limit')['length'] == 0
- assert client.read('/pane/1/dirty') == b'0\n'
- assert client.read('/pane/1/scroll') == b'1\n'
- fixture = int(before.split()[0])
- assert before.split()[1:3] == [b'text', b'0'] and before.rsplit(b' ', 1)[0].endswith(b'/discovery.txt'), before
- # /status reports the editor; /exec has nothing to report yet.
- status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())
- assert int(status['pid']) > 0 and status['version'] and int(status['panes']) >= 1, status
- assert client.read('/exec') == b''
- # An open of /pane/new makes a pane and the read names it; each
- # open makes another. A stat makes none, which is why new can be
- # listed at all: ls -l stats every name a listing gave it.
- # /log answers the recent past and ends; `follow` written to the
- # same open makes its reads past that wait for new records.
- log = client.open('/log', 2)
- frozen = len(client.read_fid(log))
- client.rpc(118, struct.pack('<IQI', log, 0, 7) + b'follow\n')
- first = int(client.read('/pane/new'))
- assert first == newest(client)
- second = int(client.read('/pane/new'))
- assert first != second and first != fixture, (first, second)
- assert 'new' in client.list('/pane'), 'new should be visible to ls'
- client.stat('/pane/new')
- assert newest(client) == second, 'a stat of new made a pane'
- # What the editor said is in the stream too.
- def pane_event():
- while (record := client.read_fid(log, frozen)).startswith((b'msg ', b'err ')):
- pass
- return record
- assert pane_event() == f'new {first} {root}/+New\n'.encode()
- assert pane_event() == f'new {second} {root}/+New\n'.encode()
- # A follower that is waiting wakes when the editor logs, not when
- # some unrelated request next happens to wait.
- with Client(address) as follower:
- waiting = follower.open('/log', 2)
- start = len(follower.read_fid(waiting))
- follower.rpc(118, struct.pack('<IQI', waiting, 0, 7) + b'follow\n')
- woke = []
- reader = threading.Thread(target=lambda: woke.append(follower.read_fid(waiting, start)), daemon=True)
- reader.start()
- time.sleep(.2)
- client.write('/exec', b'Msg woken\n')
- reader.join(5)
- assert woke and woke[0].endswith(b' woken\n'), woke
- # A pane's ctl takes acme's lock: a second open's lock fails at
- # once with "file in use" until the holder unlocks, and nothing
- # else waits on it meanwhile.
- with Client(address) as other:
- mine = client.open(f'/pane/{first}/ctl', 2)
- client.rpc(118, struct.pack('<IQI', mine, 0, 5) + b'lock\n')
- theirs = other.open(f'/pane/{first}/ctl', 1)
- try:
- other.rpc(118, struct.pack('<IQI', theirs, 0, 5) + b'lock\n')
- raise AssertionError('a second lock was taken')
- except OSError as refused:
- assert 'file in use' in str(refused), refused
- client.write(f'/pane/{first}/addr', b'#0')
- client.rpc(118, struct.pack('<IQI', mine, 0, 7) + b'unlock\n')
- other.rpc(118, struct.pack('<IQI', theirs, 0, 5) + b'lock\n')
- client.close(mine)
- other.close(theirs)
- # /focus names the pane with the keyboard and moves it, as rio's
- # wctl `current` does; a pane's ctl line ends saying which it is.
- client.write('/focus', f'{first}\n'.encode())
- assert client.read('/focus') == f'{first}\n'.encode()
- assert b' current ' in client.read(f'/pane/{first}/ctl')
- assert b' notcurrent ' in client.read(f'/pane/{second}/ctl')
- try:
- client.write('/focus', b'99999\n')
- raise AssertionError('focus took a serial no pane has')
- except OSError as refused:
- assert 'no such pane' in str(refused), refused
- # /ctl reads the settings as a write takes them and takes the
- # session's builtins; a pane's ctl takes the pane's.
- settings = client.read('/ctl')
- assert b'Verbose ' in settings and b'Platform' not in settings, settings
- client.write('/ctl', settings)
- assert client.read('/ctl') == settings
- client.write('/ctl', b'Verbose off\n')
- assert b'Verbose off\n' in client.read('/ctl')
- for path, line, why in (('/ctl', b'Del\n', 'not a session control message "Del"'),
- ('/ctl', b'Nope 3\n', 'unknown control message "Nope 3"'),
- (f'/pane/{first}/ctl', b'Newcol\n', 'not a window control message "Newcol"')):
- try:
- client.write(path, line)
- raise AssertionError(f'{path} took {line!r}')
- except OSError as refused:
- assert why in str(refused), refused
- commands = client.read('/commands').decode().splitlines()
- def row(start):
- return any(r == start or r.startswith(start + ' ') for r in commands)
- assert row('Newcol root') and row('Save arg pane') and row('Edit arg both'), commands
- # word, `arg` if it takes one, its ctl, a setting's values, then `-- ` what it does
- assert all(r.split()[2 if r.split()[1] == 'arg' else 1] in ('root', 'pane', 'both') for r in commands), commands
- assert row('Placement arg root acme,pardes'), commands
- assert client.stat('/commands')['length'] == len('\n'.join(commands).encode()) + 1 # bytes: a description may end in …
- client.write(f'/pane/{first}/ctl', b'Msg said through ctl\n')
- assert 'said through ctl' in ''.join(cell[0] for cell in client.screen()['cells'])
- assert set(client.list('/pane')) == {'new', str(fixture), str(first), str(second)}
- client.write(f'/pane/{first}/body', b'first pane', truncate=True)
- assert client.read(f'/pane/{first}/body') == b'first pane'
- assert client.read(f'/pane/{second}/body') == b''
- assert client.stat(f'/pane/{first}/body')['length'] == len(b'first pane')
- rows = {int(row.split()[0]): row.split() for row in client.read('/index').splitlines()}
- assert rows[first][1:3] == [b'text', b'1'] and rows[second][1:3] == [b'text', b'0'], rows
- # name: read the file name, write to rename; sel: the selection.
- assert client.read(f'/pane/{first}/name') == f'{root}/+New\n'.encode()
- client.write(f'/pane/{first}/name', b'notes.txt\n')
- assert client.read(f'/pane/{first}/name') == f'{root}/notes.txt\n'.encode()
- assert pane_event() == f'rename {first} {root}/notes.txt\n'.encode()
- assert client.stat(f'/pane/{first}/name')['length'] == len(f'{root}/notes.txt\n')
- # Copying addr onto dot selects that range; sel reads and replaces it.
- client.write(f'/pane/{first}/addr', b'#0,#5')
- client.write(f'/pane/{first}/dot', client.read(f'/pane/{first}/addr'))
- assert client.read(f'/pane/{first}/dot') == client.read(f'/pane/{first}/addr')
- assert client.read(f'/pane/{first}/sel') == b'first'
- assert client.stat(f'/pane/{first}/sel')['length'] == 5
- client.write(f'/pane/{first}/sel', b'FIRST')
- assert client.read(f'/pane/{first}/body') == b'FIRST pane'
- assert client.read(f'/pane/{first}/sel') == b''
- # exec Msg shows text; exec Save writes the named file; rmdir closes.
- client.write(f'/pane/{first}/exec', b'Msg hello from 9P\n')
- assert 'hello from 9P' in ''.join(cell[0] for cell in client.screen()['cells'])
- assert client.read(f'/pane/{first}/dirty') == b'1\n'
- client.write(f'/pane/{first}/exec', b'Save\n')
- assert (root / 'notes.txt').read_bytes() == b'FIRST pane'
- assert pane_event() == f'save {first} {root}/notes.txt\n'.encode()
- assert client.read('/exec') == f'{first}\n'.encode()
- assert client.read(f'/pane/{first}/dirty') == b'0\n'
- client.remove(f'/pane/{second}')
- assert pane_event() == f'del {second} {root}/+New\n'.encode()
- assert str(second) not in client.list('/pane')
- # look opens a file; the serial reads back.
- (root / 'opened.txt').write_bytes(b'one\ntwo\nthree\n')
- client.write('/look', f'{root}/opened.txt:2\n'.encode())
- opened = int(client.read('/look'))
- assert opened not in (fixture, first, second), opened
- assert client.read(f'/pane/{opened}/body') == b'one\ntwo\nthree\n'
- assert client.read(f'/pane/{opened}/name') == f'{root}/opened.txt\n'.encode()
- assert pane_event() == f'new {opened} {root}/opened.txt\n'.encode()
- client.write(f'/pane/{first}/look', f'{root}/opened.txt:3\n'.encode())
- assert client.read('/look') == f'{opened}\n'.encode(), 'a repeated look targets the open pane'
- assert {str(fixture), str(first), str(opened)} <= set(client.list('/pane'))
- # Writing an event record back still performs the action it names.
- client.write(f'/pane/{first}/body', b'Msg via event', truncate=True)
- client.write(f'/pane/{first}/event', b'FX0 13\n')
- assert 'via event' in ''.join(cell[0] for cell in client.screen()['cells'])
- # A command line may not carry control characters, and nothing else
- # in the tree may be created or removed.
- refused = [lambda: client.write('/exec', b'Msg a\x00b'),
- lambda: client.write('/status', b'anything\n'),
- lambda: client.mkdir('/', 'x'),
- lambda: client.mkdir('/pane', 'one'),
- lambda: client.create('/pane', 'plain-file'),
- lambda: client.remove('/pane/1/body'),
- lambda: client.remove('/index')]
- for index, attempt in enumerate(refused):
- try:
- attempt()
- except OSError:
- pass
- else:
- raise AssertionError(f'refusal {index} was accepted')
- client.close(log)
- if embedded:
- assert client.read('/src/pardes.zig').startswith(b'const std')
- assert client.stat('/src/pardes.zig')['mode'] == 0o444
- assert b'pub const Pardes' in client.read(f'/pane/{look(client, "/virtual/src/pardes.zig")}/body')
- # An open of data with OTRUNC (a shell's >) deletes the addressed
- # range only, so the write after it replaces that range.
- scratch = new_pane(client, b'hello world\nsecond line\n')
- client.write(f'/pane/{scratch}/addr', b'#0,#5')
- client.write(f'/pane/{scratch}/data', b'HOWDY', truncate=True)
- assert client.read(f'/pane/{scratch}/body') == b'HOWDY world\nsecond line\n'
- # sam's regexps: ^ at any line; a miss says so and leaves no
- # address, so the data write after it refuses.
- client.write(f'/pane/{scratch}/addr', b'/^second/')
- assert client.read(f'/pane/{scratch}/addr') == b' 12 18 '
- for path, data, why in ((f'/pane/{scratch}/addr', b'/nowhere/', 'no match for regexp'),
- (f'/pane/{scratch}/data', b'LOST', 'no address')):
- try:
- client.write(path, data)
- raise AssertionError(f'{path} took {data!r}')
- except OSError as refused:
- assert why in str(refused), refused
- assert client.read(f'/pane/{scratch}/body') == b'HOWDY world\nsecond line\n'
- # Through a mount both read as EINVAL; the log says which and why.
- assert f'err {scratch} addr: no match for regexp\n'.encode() in client.read('/log')
- client.remove(f'/pane/{scratch}')
- # Real `diff -u` in a command pane: the terminal turns the tab
- # before each timestamp into blanks, and a look at a changed line
- # still opens the new file at it.
- (root / 'diff-old.txt').write_text('one\ntwo\nthree\n')
- (root / 'diff-new.txt').write_text('one\nTWO\nthree\n')
- seen = client.read('/log').decode().count('\nexit ')
- client.write('/exec', b'diff -u diff-old.txt diff-new.txt\n')
- deadline = time.monotonic() + 10
- while client.read('/log').decode().count('\nexit ') <= seen:
- assert time.monotonic() < deadline, 'diff -u never ended'
- time.sleep(.05)
- # A command's pane is kind cmd, not term: no shell to type into.
- ran = [row.split()[0] for row in client.read('/index').decode().splitlines() if row.split()[1] == 'cmd']
- shown = client.read(f'/pane/{ran[-1]}/body').decode()
- assert '+++ diff-new.txt' in shown and '\t' not in shown, shown
- client.write(f'/pane/{ran[-1]}/look', b'+TWO\n')
- opened = [row for row in client.read('/index').decode().splitlines() if row.split()[3] == f'{root}/diff-new.txt']
- assert len(opened) == 1, client.read('/index')
- client.remove(f'/pane/{opened[0].split()[0]}')
- client.remove(f'/pane/{ran[-1]}')
- # A Save makes the directories its file goes in, whatever names
- # it: the pane's own name or Save's path.
- # `~` is home (this session's HOME, the test's root) wherever a
- # path is typed: Save, name, a look, with an address or not.
- tilde = new_pane(client, b'one\ntwo\nthree\n')
- client.write(f'/pane/{tilde}/ctl', b'Save ~/tilde-made/sub/x.txt\n')
- assert (root / 'tilde-made/sub/x.txt').read_bytes() == b'one\ntwo\nthree\n'
- client.write(f'/pane/{tilde}/name', b'~/tilde-named.txt\n')
- assert client.read(f'/pane/{tilde}/name') == f'{root}/tilde-named.txt\n'.encode()
- client.remove(f'/pane/{tilde}')
- client.write('/look', b'~/tilde-made/sub/x.txt:3\n')
- looked = [row for row in client.read('/index').decode().splitlines() if row.split()[3] == f'{root}/tilde-made/sub/x.txt']
- assert len(looked) == 1, client.read('/index')
- assert client.read(f'/pane/{looked[0].split()[0]}/addr').startswith(b'8 '), client.read(f'/pane/{looked[0].split()[0]}/addr')
- client.remove(f'/pane/{looked[0].split()[0]}')
- made_dirs = new_pane(client, b'deep\n')
- client.write(f'/pane/{made_dirs}/ctl', f'Save {root}/save-made/a/b/x.txt\n'.encode())
- assert (root / 'save-made/a/b/x.txt').read_bytes() == b'deep\n'
- client.write(f'/pane/{made_dirs}/name', f'{root}/save-named/c/y.txt\n'.encode())
- client.write(f'/pane/{made_dirs}/ctl', b'Save\n')
- assert (root / 'save-named/c/y.txt').read_bytes() == b'deep\n'
- client.remove(f'/pane/{made_dirs}')
- # A save the host cannot do (a directory it may not make) fails
- # the write that asked for it, and the log's err record names
- # the path and why.
- unsaved = new_pane(client, b'unsaved\n')
- try:
- client.write(f'/pane/{unsaved}/ctl', b'Save /nonexistent-pardes-dir/x.txt\n')
- raise AssertionError('a Save into a missing directory succeeded')
- except OSError as refused:
- assert 'Save /nonexistent-pardes-dir/x.txt: no such directory' in str(refused), refused
- assert f'err {unsaved} ctl: Save /nonexistent-pardes-dir/x.txt: '.encode() in client.read('/log'), client.read('/log')
- client.remove(f'/pane/{unsaved}')
-
- # Sixteen connections fill the slots; the next is told why.
- extra = [Client(address) for _ in range(15)]
- try:
- try:
- Client(address).__exit__()
- raise AssertionError('a seventeenth connection was served')
- except OSError as refused:
- assert 'too many connections' in str(refused), refused
- finally:
- for other in extra:
- other.__exit__()
- deadline = time.monotonic() + 5
- while b'err - 9p: too many connections' not in client.read('/log'):
- assert time.monotonic() < deadline, client.read('/log')
- time.sleep(.01)
-
- print('9P discovery: listing/stat/find are inert; new, remove, look, exec, name, sel, log, ctl lock, focus, the ctl split and commands behave')
-
-
-def run_file(binary):
- """pty/run: a line in, how it ended out, on the same open."""
- def run(c, serial, line):
- fid = c.open(f'/pane/{serial}/pty/run', 2)
- try:
- c.rpc(118, struct.pack('<IQI', fid, 0, len(line)) + line)
- return c.read_fid(fid)
- finally:
- c.close(fid)
- with tempfile.TemporaryDirectory() as directory:
- root = Path(directory)
- with session(binary, root, 'run') as (client, address):
- # From a pane on a peer's path no command or terminal starts:
- # refused before any pane is made, no `exit 127` pane left.
- peer = new_pane(client, b'')
- client.write(f'/pane/{peer}/name', b'/n/nosuchpeer/d/f.txt\n')
- count = len(client.read('/index').splitlines())
- execute_refused(client, peer, 'ls', 'no such directory')
- execute_refused(client, peer, 'Tty', 'no such directory')
- assert len(client.read('/index').splitlines()) == count, client.read('/index')
- assert b'exit 127' not in client.read('/log')
- client.remove(f'/pane/{peer}')
- client.write('/exec', b'Shell bash\n')
- client.write('/exec', b'Tty\n')
- term = int(max(r.split()[0] for r in client.read('/index').splitlines() if r.split()[1] == b'term'))
- deadline = time.monotonic() + 10
- while run(client, term, b'true\n') == b'busy\n': # until its first prompt
- assert time.monotonic() < deadline, 'the shell never reached a prompt'
- time.sleep(.05)
- assert run(client, term, b'false\n') == b'exit 1\n'
- assert run(client, term, b'printf "one\\ntwo\\n"\n') == b'exit 0\none\ntwo\n' # then what it printed
- assert run(client, term, b'sh -c "exit 7"\n') == b'exit 7\n'
- assert run(client, term, b'fi\n') == b'error not run\n' # a syntax error runs nothing
- assert run(client, term, b"echo 'open\n") == b'error not run\n' # nor does an open quote
- assert run(client, term, b'true\n') == b'exit 0\n' # ...and the shell is clear after
- # With no frontend attached the pty's size is in pixels too, at a
- # nominal 8x16 cell: a program sizes its kitty graphics by it.
- said = run(client, term, b'python3 -c \'import fcntl,termios,struct;print(*struct.unpack("4H",fcntl.ioctl(0,termios.TIOCGWINSZ,bytes(8))))\'\n')
- rows, cols, xpixel, ypixel = map(int, said.split(b'\n')[1].split())
- assert (xpixel, ypixel) == (cols * 8, rows * 16) and xpixel > 0, said
- # A paging command run through pty/run does not wait in less: the
- # terminal's PAGER is `pardes -`, and its text is a +Pager pane.
- if shutil.which('git'):
- repo = root / 'pagedrepo'
- repo.mkdir()
- git = ['git', '-C', str(repo), '-c', 'user.name=p', '-c', 'user.email=p@x']
- subprocess.run(git + ['init', '-q'], check=True, env=dict(os.environ, HOME=str(root)))
- (repo / 'f').write_text('x\n')
- subprocess.run(git + ['add', 'f'], check=True, env=dict(os.environ, HOME=str(root)))
- subprocess.run(git + ['commit', '-qm', 'paged-commit-subject'], check=True, env=dict(os.environ, HOME=str(root)))
- assert run(client, term, b'git -C pagedrepo log\n').startswith(b'exit 0\n')
- pager = [r.split()[0].decode() for r in client.read('/index').splitlines() if r.split()[3:4] == [f'{repo}/+Pager'.encode()]] # git -C runs it in the repo
- assert len(pager) == 1, client.read('/index')
- assert b'paged-commit-subject' in client.read(f'/pane/{pager[0]}/body')
- assert b'\x1b' not in client.read(f'/pane/{pager[0]}/body')
- client.remove(f'/pane/{pager[0]}')
- # systemd's tools page through their own variable: set too, its
- # secure mode off, the path unquoted where it needs none. Read
- # by the shell: coreutils 9.12's printenv quotes a value with a
- # blank on a terminal.
- systemd_pager = run(client, term, b'printf "%s\\n" "$SYSTEMD_PAGER" "$SYSTEMD_PAGERSECURE"\n')
- assert systemd_pager.startswith(b'exit 0\n') and systemd_pager.rstrip().endswith(b' -\n0'), systemd_pager
- if shutil.which('journalctl'):
- assert run(client, term, b'journalctl -n 3 --no-hostname\n').startswith(b'exit ')
- for row in client.read('/index').splitlines():
- if row.split()[3:4] == [f'{root}/+Pager'.encode()]:
- client.remove(f'/pane/{row.split()[0].decode()}')
- # A pty is not given one row (it loses the prompt's input mark,
- # and the shell would read busy for ever): refused, and said.
- try:
- client.write(f'/pane/{term}/pty/ctl', b'winsize 20 1\n')
- raise AssertionError('winsize 20 1 was taken')
- except OSError as refused:
- assert 'at least 2 rows' in str(refused), refused
- client.write(f'/pane/{term}/pty/ctl', b'winsize 20 2\n')
- time.sleep(.5)
- assert run(client, term, b'true\n') == b'exit 0\n'
- assert client.read(f'/pane/{term}/pty/ctl').split()[2] == b'2', client.read(f'/pane/{term}/pty/ctl')
- client.write(f'/pane/{term}/pty/ctl', b'winsize 80 24\n')
- # A shell started again that cannot start (its directory is
- # there but cannot be entered) fails the write and leaves the
- # running shell be: runs still work.
- locked = root / 'locked'
- locked.mkdir()
- assert run(client, term, f'cd {locked}\n'.encode()) == b'exit 0\n'
- deadline = time.monotonic() + 5
- while str(locked).encode() not in client.read('/index'):
- assert time.monotonic() < deadline, client.read('/index')
- time.sleep(.05)
- locked.chmod(0o600)
- try:
- if os.geteuid() != 0: # root enters any directory
- client.write(f'/pane/{term}/pty/ctl', b'exec\n')
- raise AssertionError('pty/ctl exec of a shell that cannot start was taken')
- except OSError as refused:
- assert 'shell' in str(refused), refused
- finally:
- locked.chmod(0o755)
- kept = run(client, term, b'true\n')
- assert kept == b'exit 0\n', (kept, client.read('/log')[-600:])
- assert run(client, term, f'cd {root}\n'.encode()) == b'exit 0\n'
- with Client(address) as other:
- slow = []
- waiter = threading.Thread(target=lambda: slow.append(run(other, term, b'sleep 1\n')), daemon=True)
- waiter.start()
- time.sleep(.3)
- # The job the other client ran holds the tty, and says so.
- assert run(client, term, b'true\n') == b'busy: sleep is running\n'
- waiter.join(5)
- assert slow == [b'exit 0\n'], slow
- closing = []
- waiter = threading.Thread(target=lambda: closing.append(run(other, term, b'sleep 30\n')), daemon=True)
- waiter.start()
- time.sleep(.3)
- client.write(f'/pane/{term}/exec', b'Del\n')
- waiter.join(5)
- assert closing == [b'error shell gone\n'], closing
-
-
-def exit_answers(binary):
- """Exit written to /ctl is answered before the editor quits."""
- with tempfile.TemporaryDirectory(prefix='pardes-exit-') as directory:
- seen = {}
- with session(binary, Path(directory), 'exit', terminal=seen) as (client, _):
- client.write('/ctl', b'Exit\n')
- deadline = time.monotonic() + 5
- while time.monotonic() < deadline:
- try:
- # a zombie has quit; the session reaps it
- if Path(f'/proc/{seen["pid"]}/stat').read_text().split(') ')[1][0] == 'Z':
- break
- except FileNotFoundError:
- break
- time.sleep(.01)
- else:
- raise AssertionError("Exit did not quit the editor")
-
-
-def unattended_messages_expire(binary):
- """With nothing attached, a message leaves after MessageLinger on its own
- clock: no key, no click, no other request comes to move it along."""
- with tempfile.TemporaryDirectory(prefix='pardes-msg-') as directory:
- with session(binary, Path(directory), 'msg') as (client, _):
- client.write('/pane/1/ctl', b'Msg lingering-words\n')
- def shown():
- screen = json.loads(client.read('/screen'))
- return 'lingering-words' in ''.join(cell[0] for cell in screen['cells'])
- assert shown()
- # Reading /screen is itself a request, so wait without asking.
- time.sleep(2.5)
- assert not shown(), 'an unattended message outlived MessageLinger'
-
-
-def directory_panes(binary):
- """A look at a directory opens a pane listing it, as acme's directory
- window: a text pane named with a trailing /, its body the entries (a
- directory's marked /), ctl's isdir 1, and get reads it again."""
- with tempfile.TemporaryDirectory(prefix='pardes-dirpane-') as directory:
- root = Path(directory).resolve()
- listed = root / 'listed'
- (listed / 'sub').mkdir(parents=True)
- (listed / '.dot').write_bytes(b'')
- with session(binary, root, 'dirpane') as (client, _):
- serial = look(client, str(listed))
- rows = [row.split() for row in client.read('/index').decode().splitlines()]
- row = next(r for r in rows if int(r[0]) == serial)
- assert row[1:4] == ['text', '0', f'{listed}/'], row
- body = client.read(f'/pane/{serial}/body').decode()
- assert body.split() == ['.dot', 'sub/'], body
- assert client.read(f'/pane/{serial}/ctl').split()[3] == b'1'
- (listed / 'later').write_bytes(b'')
- client.write(f'/pane/{serial}/ctl', b'get\n')
- assert client.read(f'/pane/{serial}/body').decode().split() == ['.dot', 'later', 'sub/']
-
-
-def putall_answers_after_its_saves(binary):
- """Putall with one pane refused (its file changed on disk under unsaved
- edits) fails, and is answered only once the other panes' saves have
- landed."""
- with tempfile.TemporaryDirectory(prefix='pardes-putall-') as directory:
- root = Path(directory).resolve()
- (root / 'a.txt').write_bytes(b'a\n')
- (root / 'b.txt').write_bytes(b'b\n')
- with session(binary, root, 'putall') as (client, _):
- a = look(client, str(root / 'a.txt'))
- b = look(client, str(root / 'b.txt'))
- for serial in (a, b):
- client.write(f'/pane/{serial}/body', b'edited\n')
- (root / 'b.txt').write_bytes(b'changed on disk\n')
- deadline = time.monotonic() + 10
- while f'changed {b}' not in client.read('/log').decode():
- assert time.monotonic() < deadline, 'the watch never saw b.txt change'
- time.sleep(.05)
- try:
- client.write('/ctl', b'Putall\n')
- except OSError as why:
- assert 'modified on disk' in str(why), why
- else:
- raise AssertionError('Putall over a file changed on disk was taken')
- assert (root / 'a.txt').read_bytes() == b'a\nedited\n', (root / 'a.txt').read_bytes()
- assert (root / 'b.txt').read_bytes() == b'changed on disk\n'
-
-
-def new_terminals_named_once(binary):
- """A terminal started outside the session's directory is named by that
- directory from its `new` on: no rename to the session's and back."""
- with tempfile.TemporaryDirectory(prefix='pardes-names-') as directory:
- root = Path(directory)
- (root / 'sub').mkdir()
- with session(binary, root, 'names') as (client, _):
- # A look at a directory starts a terminal there (DirLook
- # terminal); DirLook pane, the default, lists it in a pane.
- client.write('/ctl', b'DirLook terminal\n')
- made = [look(client, str(root / 'sub'))]
- before = {int(row.split()[0]) for row in client.read('/index').splitlines()}
- client.write('/pane/1/ctl', b'Tty\n')
- if shutil.which('fish'):
- client.write('/pane/1/ctl', b'Tty fish\n')
- made += [s for s in (int(row.split()[0]) for row in client.read('/index').splitlines()) if s not in before]
- time.sleep(1.5) # their shells start and say where they are
- # A shell started again by pty/ctl exec: named as it was.
- client.write(f'/pane/{made[0]}/pty/ctl', b'exec\n')
- time.sleep(1.5)
- log = client.read('/log').decode()
- for serial in made:
- assert f'\nrename {serial} ' not in '\n' + log, (serial, log)
- assert f'\nnew {serial} {root}' in '\n' + log, (serial, log)
- # A shell that cannot start (its interpreter is not there): Tty
- # fails the write with why and leaves no pane; pty/ctl exec
- # fails with why and keeps its terminal.
- bad = root / 'badsh'
- bad.write_bytes(b'#!/nonexistent/interp\n')
- bad.chmod(0o755)
- panes = client.read('/index')
- for where, line in (('/pane/1/ctl', f'Tty {bad}\n'), ('/ctl', f'Shell {bad}\n')):
- try:
- client.write(where, line.encode())
- raise AssertionError(f'{line} of a shell that cannot start was taken')
- except OSError as refused:
- assert 'interpreter /nonexistent/interp not found' in str(refused), refused
- time.sleep(.3)
- assert client.read('/index').count(b'\n') == panes.count(b'\n'), client.read('/index')
- assert b'shell: shell not found' not in client.read('/log'), client.read('/log')
- assert str(made[0]).encode() in b' '.join(r.split()[0] for r in client.read('/index').splitlines())
-
-
-def recent_survives_restart(binary):
- """A file opened and closed is in /recent closed, and still is in the
- next session: the list is kept under $XDG_STATE_HOME (here HOME's
- .local/state) across restarts."""
- with tempfile.TemporaryDirectory(prefix='pardes-recent-') as directory:
- root = Path(directory)
- kept = root / 'kept.txt'
- kept.write_bytes(b'one\ntwo\n')
- with session(binary, root, 'recent1') as (client, _):
- serial = look(client, f'{kept}:2')
- assert f'open {kept}\n' in client.read('/recent').decode()
- client.write(f'/pane/{serial}/ctl', b'Del\n')
- assert f'closed {kept}\n' in client.read('/recent').decode(), client.read('/recent')
- assert (root / '.local' / 'state' / 'pardes' / 'store').exists()
- with session(binary, root, 'recent2') as (client, _):
- listed = client.read('/recent').decode()
- assert f'closed {kept}\n' in listed, listed
- # Recent fills a query pane with it, its row relative to the
- # pane's directory, and a look at the row opens it again.
- client.write('/pane/1/exec', b'Recent\n')
- rows = int(client.read('/pane/1/exec').split()[-1])
- body = client.read(f'/pane/{rows}/body').decode()
- assert body.startswith('Recent '), body
- row = next(r for r in body.splitlines() if r.startswith('kept.txt:'))
- client.write(f'/pane/{rows}/look', (row.split(' ')[0] + '\n').encode())
- assert f'open {kept}\n' in client.read('/recent').decode()
-
-
-def del_twice(client, serial):
- """Del, and again when the first refused over unsaved text."""
- try:
- client.write(f'/pane/{serial}/ctl', b'Del\n')
- except OSError:
- client.write(f'/pane/{serial}/ctl', b'Del\n')
-
-
-def store_shared_between_windows(binary):
- """Two windows share one store (Store.zig): a file opened in one is in
- the other's /recent at its next read, with no restart; a pane Del'd in
- one comes back where it was from a look at its @p serial there, its text
- with it, and only there."""
- with tempfile.TemporaryDirectory(prefix='pardes-store-') as directory:
- root = Path(directory)
- shared = root / 'shared.txt'
- shared.write_bytes(b'one\ntwo\nthree\n')
- with session(binary, root, 'store1') as (one, _), session(binary, root, 'store2') as (two, _):
- serial = look(one, f'{shared}:3')
- assert f'open {shared}\n' in one.read('/recent').decode()
- assert f'closed {shared}\n' in two.read('/recent').decode(), two.read('/recent')
- one.write(f'/pane/{serial}/ctl', b'Del\n')
- assert f'closed {shared}\n' in one.read('/recent').decode()
- scratch = new_pane(one, b'kept across a Del\n')
- del_twice(one, scratch)
- assert str(scratch) not in [row.split()[0] for row in one.read('/index').decode().splitlines()]
- back = look(one, f'@p{scratch}')
- assert one.read(f'/pane/{back}/body') == b'kept across a Del\n'
- # The other window's session closed no such pane.
- source = new_pane(two, b'')
- try:
- two.write(f'/pane/{source}/look', f'@p{scratch}\n'.encode())
- except OSError:
- pass
- assert b'kept across a Del' not in b''.join(two.read(f'/pane/{row.split()[0]}/body')
- for row in two.read('/index').decode().splitlines())
- assert (root / '.local' / 'state' / 'pardes' / 'store').exists()
-
-
-def workflow_canary(binary):
- """One session driven as the dogfood agents drive it, asserting the log
- and the read-backs at every step: the canary for the main paths (the
- round-18 Grep that never found anything went through every narrower
- test). Look files, Grep and Find hits and misses, a plain word looked
- at, Edit in two files, Save, get, pty/run failing then passing, a
- command pane and Kill, a REPL send, the layout and a column's files,
- ask/answer, and Dump/Restore last."""
- started = time.monotonic()
- with tempfile.TemporaryDirectory(prefix='pardes-flow-') as directory:
- root = Path(directory)
- (root / 'a.txt').write_bytes(b'alpha one\nshared word\n')
- (root / 'b.txt').write_bytes(b'beta two\nshared word\n')
- (root / 's.sh').write_bytes(b'echo from-file\n')
-
- def wait(ready, what, seconds=10):
- deadline = time.monotonic() + seconds
- while not ready():
- assert time.monotonic() < deadline, what
- time.sleep(.05)
-
- with session(binary, root, 'flow') as (client, address):
- def log():
- return client.read('/log').decode('utf-8', 'replace')
-
- def made_by(serial, line):
- """The pane an exec at `serial` made, as exec reads it back."""
- client.write(f'/pane/{serial}/exec', line)
- return int(client.read(f'/pane/{serial}/exec').split()[-1])
-
- # Look opens files, and the log says new.
- a = look(client, str(root / 'a.txt'))
- b = look(client, str(root / 'b.txt'))
- assert client.read(f'/pane/{a}/body') == b'alpha one\nshared word\n'
- assert f'new {a} {root}/a.txt\n' in log() and f'new {b} {root}/b.txt\n' in log()
-
- # Grep and Find: a hit fills its query pane, line 1 the query and
- # the lines under each file's name; a miss fails ENOENT.
- grep = made_by(a, b'Grep shared\n')
- rows = client.read(f'/pane/{grep}/body').decode()
- assert rows == 'Grep shared\na.txt\na.txt:2:1 shared word\nb.txt\nb.txt:2:1 shared word\n', rows
- client.remove(f'/pane/{grep}')
- execute_refused(client, a, 'Grep zzqqnomatch', 'Grep: text not found')
- found = made_by(a, b'Find b.txt\n')
- assert 'b.txt' in client.read(f'/pane/{found}/body').decode()
- client.remove(f'/pane/{found}')
- execute_refused(client, a, 'Find zzqqnomatch', 'Find: name not found')
-
- # A plain word looked at is found in the pane, after its dot.
- client.write(f'/pane/{a}/dot', b' 0 0 ')
- client.write(f'/pane/{a}/look', b'shared\n')
- assert client.read(f'/pane/{a}/dot').split() == [b'10', b'16'], client.read(f'/pane/{a}/dot')
-
- # Edit in two files, then Save each: on disk, and logged.
- for pane in (a, b):
- client.write(f'/pane/{pane}/ctl', b'Edit ,s/shared/SHARED/g\n')
- assert client.read(f'/pane/{b}/body') == b'beta two\nSHARED word\n'
- for pane in (a, b):
- client.write(f'/pane/{pane}/exec', b'Save\n')
- assert (root / 'a.txt').read_bytes() == b'alpha one\nSHARED word\n'
- assert f'save {a} {root}/a.txt\n' in log() and f'save {b} {root}/b.txt\n' in log()
-
- # get reloads what changed on disk.
- (root / 'b.txt').write_bytes(b'from disk\n')
- client.write(f'/pane/{b}/ctl', b'get\n')
- assert client.read(f'/pane/{b}/body') == b'from disk\n'
-
- # A terminal: pty/run failing then passing.
- term = made_by(1, b'Tty\n')
-
- def run(line):
- fid = client.open(f'/pane/{term}/pty/run', 2)
- try:
- client.rpc(118, struct.pack('<IQI', fid, 0, len(line)) + line)
- return client.read_fid(fid)
- finally:
- client.close(fid)
- deadline = time.monotonic() + 10
- while (answer := run(b'true\n')) != b'exit 0\n':
- assert time.monotonic() < deadline, answer
- time.sleep(.05)
- assert run(b'false\n') == b'exit 1\n'
- assert run(b'echo passed\n') == b'exit 0\npassed\n'
- # (A run's answer is its record: the log has no err for these.)
- assert f'err {term} pty/run' not in log(), log()
-
- # A command pane, and Kill: its exit is logged.
- sleeper = made_by(a, b'sleep 30\n')
- assert f'run {sleeper} sleep 30\n' in log()
- client.write('/ctl', b'Kill sleep\n')
- wait(lambda: f'\nexit {sleeper} ' in log(), 'Kill: no exit logged')
-
- # A REPL send: a program in the terminal (cat, which says back
- # what it is sent; a nested shell reads as the shell itself)
- # bound as bash's REPL, an exec on a .sh file's body (its event
- # written back) goes there.
- s = look(client, str(root / 's.sh'))
- client.write(f'/pane/{term}/pty/data', b'cat\r')
- wait(lambda: client.read(f'/pane/{term}/pty/status').split()[-1] == b'1', ('the REPL program did not start', client.read(f'/pane/{term}/body')[-300:]))
- client.write(f'/pane/{term}/ctl', b'Repl bash\n')
- fid = client.open(f'/pane/{s}/event', 2)
- try:
- record = b'MX0 0 0 14 echo from-file\n'
- client.rpc(118, struct.pack('<IQI', fid, 0, len(record)) + record)
- finally:
- client.close(fid)
- wait(lambda: f'\nsend {s} {term} ' in log(), ('no REPL send logged', log()[-800:]))
- wait(lambda: client.read(f'/pane/{term}/body').count(b'echo from-file') >= 2, 'the REPL did not get it')
- client.write(f'/pane/{term}/ctl', b'Repl -\n')
- client.write(f'/pane/{term}/pty/data', b'\x04')
- wait(lambda: run(b'true\n') == b'exit 0\n', 'the terminal did not come back to its shell')
-
- # The layout and a column's files: Newcol, its tag, rmdir.
- assert len(client.read('/layout').splitlines()) >= 2 # a column, then `active`
- client.write('/ctl', b'Newcol\n')
- columns = [row for row in client.read('/layout').splitlines() if not row.startswith(b'active')]
- assert len(columns) == 2, columns
- empty = columns[-1].split()[0].decode()
- assert b'New' in client.read(f'/col/{empty}/tag')
- client.remove(f'/col/{empty}')
- assert len([row for row in client.read('/layout').splitlines() if not row.startswith(b'active')]) == 1
-
- # ask/answer: Save on a terminal asks for a path over 9P.
- client.write(f'/pane/{term}/exec', b'Save\n')
- wait(lambda: f'ask {term} save path\n' in log(), 'no ask logged')
- client.write(f'/pane/{term}/ctl', f'answer {root}/scroll.txt\n'.encode())
- wait(lambda: (root / 'scroll.txt').exists(), 'the answered Save wrote nothing')
- assert f'answer {term} {root}/scroll.txt\n' in log()
-
- # Dump, then Restore it: the new session names its panes.
- client.write('/ctl', b'Dump\n')
- saved = root / 'flow.dump.zon'
- wait(saved.exists, 'Dump wrote nothing')
- assert f'dump {saved}\n' in log()
- try:
- client.write('/ctl', f'Restore {saved}\n'.encode())
- except (EOFError, OSError):
- pass
- time.sleep(.5)
- with Client(address) as restored:
- wait(lambda: f'restore {saved}' in restored.read('/log').decode(), 'Restore not logged')
- names = restored.read('/index').decode()
- assert f'{root}/a.txt' in names and f'{root}/b.txt' in names, names
- # Recent goes on across the Restore, and is kept after it.
- assert f'{root}/a.txt\n' in restored.read('/recent').decode(), restored.read('/recent')
- (root / 'after.txt').write_bytes(b'after\n')
- look(restored, str(root / 'after.txt'))
- assert f'open {root}/after.txt\n' in restored.read('/recent').decode()
- state = root / '.local' / 'state' / 'pardes' / 'store'
- wait(lambda: f'{root}/after.txt'.encode() in state.read_bytes(), 'Recent not kept after Restore')
- assert time.monotonic() - started < 30, 'the canary took too long'
-
-
-def python_repl_runs_one_line(binary):
- """A one-line exec sent to Python's own REPL (3.13's pyrepl, which asks
- for bracketed paste) runs at once, with no second Enter: the Enter
- reaches it after the paste, not inside it. A real pty, a real python3."""
- python = shutil.which('python3')
- if python is None:
- return
- version = subprocess.run([python, '-c', 'import sys; print(sys.version_info >= (3, 13))'],
- capture_output=True, text=True).stdout.strip()
- if version != 'True':
- return
- with tempfile.TemporaryDirectory(prefix='pardes-repl-') as directory:
- with session(binary, Path(directory), 'repl') as (client, _):
- def panes(client):
- return {int(row.split()[0]) for row in client.read('/index').splitlines()}
- before = panes(client)
- client.write('/pane/1/ctl', b'Tty\n')
- deadline = time.monotonic() + 10
- while time.monotonic() < deadline and panes(client) == before:
- time.sleep(.05)
- tty = max(panes(client) - before)
- def body():
- return client.read(f'/pane/{tty}/body').decode('utf-8', 'replace')
- def wait_for(text, seconds=15):
- deadline = time.monotonic() + seconds
- while time.monotonic() < deadline:
- if text in body():
- return True
- time.sleep(.1)
- return False
- assert wait_for('$') or wait_for('>'), body()
- client.write(f'/pane/{tty}/pty/data', python.encode() + b' -q\r')
- assert wait_for('>>>'), body()
- client.write(f'/pane/{tty}/ctl', b'Repl python\n')
- # An exec on the REPL's own body goes to the REPL, as its event
- # record written back does: an empty range acts on its text.
- fid = client.open(f'/pane/{tty}/event', 2)
- try:
- record = b'MX0 0 0 10 print(6*7)\n'
- client.rpc(118, struct.pack('<IQI', fid, 0, len(record)) + record)
- assert wait_for('\n42'), body()
- # After the session has sat idle, the Enter still waits for
- # the paste: core time is as old as the last frame then.
- for n in range(5):
- time.sleep(2)
- line = f'print({1000 + n})'.encode()
- record = b'MX0 0 0 %d %s\n' % (len(line), line)
- client.rpc(118, struct.pack('<IQI', fid, 0, len(record)) + record)
- assert wait_for(f'\n{1000 + n}'), (n, body())
- # More than one line, with and without a newline at the end,
- # and a block with a statement after it: each runs whole,
- # and the REPL is back at its prompt, not at `...`.
- for code, outputs in [(b'print(11)\nprint(22)', ['11', '22']),
- (b'print(33)\nprint(44)\n', ['33', '44']),
- (b'for i in range(2):\n print(55 + i)\nprint(77)\n', ['55', '56', '77'])]:
- record = b'MX0 0 0 %d %s\n' % (len(code), code)
- client.rpc(118, struct.pack('<IQI', fid, 0, len(record)) + record)
- for out in outputs:
- assert wait_for(f'\n{out}\n'), (code, out, body())
- time.sleep(.5)
- assert body().rstrip().endswith('>>>'), (code, body()[-200:])
- finally:
- client.close(fid)
-
-
-def signal_cleans_up(binary):
- """A detached session told to stop by a signal unlinks its sockets. A
- hangup is a terminal's end, not a session's: the daemon keeps it."""
- for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP]:
- with tempfile.TemporaryDirectory(prefix='pardes-sig-') as directory:
- root = Path(directory)
- seen = {}
- with session(binary, root, 'sig', terminal=seen) as (client, address):
- assert address.exists()
- if sig == signal.SIGHUP:
- os.kill(seen['pid'], sig)
- time.sleep(.3)
- assert address.exists() and client.read('/index'), 'a hangup ended a kept session'
- sig = signal.SIGTERM
- os.kill(seen['pid'], sig)
- deadline = time.monotonic() + 5
- while address.exists() and time.monotonic() < deadline:
- time.sleep(.01)
- assert not address.exists(), (sig, list(root.iterdir()))
-
-
-def odd_paths(binary):
- """Long and odd paths written where a path goes -- name, look, DumpDir,
- Dump, Save -- are refused or said, never a crash: a part over 255 bytes
- (ENAMETOOLONG), a loop of links (ELOOP), a file taken for a directory
- (ENOTDIR), one not ours (EACCES)."""
- with tempfile.TemporaryDirectory(prefix='pardes-odd-') as directory:
- root = Path(directory)
- (root / 'loop').symlink_to(root / 'loop')
- (root / 'plain').write_bytes(b'x\n')
- odd = ['/tmp/' + 'x' * 300 + '/f.txt', str(root / ('y' * 300)), str(root / 'loop' / 'f'),
- str(root / 'plain' / 'f'), '/root/nope/f', str(root / 'a' / '..' / 'b.txt')]
- with session(binary, root, 'odd') as (client, _):
- serial = new_pane(client, b'text\n')
- for path in odd:
- for where, line in ((f'/pane/{serial}/name', path), ('/look', path),
- ('/ctl', f'DumpDir {path}'), ('/ctl', 'Dump'), (f'/pane/{serial}/ctl', 'Save')):
- try:
- client.write(where, (line + '\n').encode())
- except OSError:
- pass
- assert client.read('/index'), (where, line)
- client.write('/ctl', b'DumpDir\n')
- # A theme file that is not there, or no theme, fails the write.
- (root / 'bad.zon').write_bytes(b'.{ .nonsense = 1 }')
- for line, why in ((f'ThemeFile {root}/missing.zon', 'theme file'), (f'ThemeFile {root}/bad.zon', 'theme file'),
- ('ThemeFile notes.txt', 'a theme file is a .zon')):
- try:
- client.write('/ctl', (line + '\n').encode())
- raise AssertionError(f'{line} was taken')
- except OSError as refused:
- assert why in str(refused), (line, refused)
-
-
-def delcol_last_keeps_window(binary):
- """Delcol on the last column leaves the window, empty but for the
- workspace tag, as acme does; Newcol and New start again from there."""
- with tempfile.TemporaryDirectory(prefix='pardes-delcol-') as directory:
- with session(binary, Path(directory), 'delcol') as (client, _):
- def columns():
- return [l for l in client.read('/layout').decode().splitlines() if l and not l.startswith('active')]
- while columns():
- client.write(f'/col/{columns()[0].split()[0]}/ctl', b'Delcol\n')
- # Still here, empty.
- assert client.read('/index') == b'', client.read('/index')
- client.write('/ctl', b'Newcol\n')
- assert len(columns()) == 1, columns()
- serial = new_pane(client, b'back\n')
- assert str(serial).encode() in client.read('/index'), client.read('/index')
-
-
-def cut_writes(binary):
- """A write bigger than a 9P message comes as several, cut anywhere: each
- command line runs once, whole, an Edit block once it ends, and a last
- line with no newline when the open closes. Never a fragment."""
- with tempfile.TemporaryDirectory(prefix='pardes-cut-') as directory:
- with session(binary, Path(directory), 'cut') as (client, _):
- burst = b''.join(b'Msg burst-%04d\n' % i for i in range(1000))
- assert len(burst) > client.msize
- client.write('/exec', burst)
- log = client.read('/log').decode()
- said = [line.split(' ', 2)[2] for line in log.splitlines() if line.startswith('msg ') and 'burst-' in line]
- assert said == ['burst-%04d' % i for i in range(1000)], said[:3] + said[-3:]
- assert '\nrun ' not in log and 'exit 127' not in log, log[-400:]
- client.write('/exec', b'Msg no-newline-at-end')
- assert 'msg ' in client.read('/log').decode().split('no-newline-at-end')[0][-40:]
- serial = new_pane(client, b'')
- block = b'Edit ,a\n' + b''.join(b'line %05d of the block\n' % i for i in range(2500)) + b'.\n'
- assert len(block) > 50000
- client.write(f'/pane/{serial}/ctl', block)
- body = client.read(f'/pane/{serial}/body').decode().splitlines()
- assert body == ['line %05d of the block' % i for i in range(2500)], (len(body), body[:2])
- # A detached session with no frontend is 160x50 until told.
- screen = client.screen()
- assert (screen['cols'], screen['rows']) == (160, 50), (screen['cols'], screen['rows'])
- client.write('/ctl', b'size 132 40\n')
- screen = client.screen()
- assert (screen['cols'], screen['rows']) == (132, 40), (screen['cols'], screen['rows'])
-
-
-def edit_commands(binary):
- """Edit's `< | >` run off the loop and answer the write that ran them
- once applied; a failing one changes nothing and fails the write in its
- words; X edits every open file it matches; w writes. The editor answers
- other requests while a slow command runs."""
- with tempfile.TemporaryDirectory(prefix='pardes-edit-') as directory:
- root = Path(directory)
- (root / 'a.txt').write_bytes(b'pear\napple\nfig\n')
- (root / 'b.txt').write_bytes(b'apple pie\n')
- with session(binary, root, 'edit') as (client, address):
- a = look(client, str(root / 'a.txt'))
- b = look(client, str(root / 'b.txt'))
- # The write returns with the text sorted, through the session's
- # shell in the file's directory.
- client.write(f'/pane/{a}/ctl', b'Edit , | sort\n')
- assert client.read(f'/pane/{a}/body') == b'apple\nfig\npear\n'
- client.write(f'/pane/{a}/ctl', b'Edit $ < pwd\n')
- assert client.read(f'/pane/{a}/body') == f'apple\nfig\npear\n{root}\n'.encode()
- client.write(f'/pane/{a}/ctl', b'Undo\n')
- assert client.read(f'/pane/{a}/body') == b'apple\nfig\npear\n'
- # A per-match command, one undo step.
- client.write(f'/pane/{a}/ctl', b'Edit ,x/[a-z]+/ | tr a-z A-Z\n')
- assert client.read(f'/pane/{a}/body') == b'APPLE\nFIG\nPEAR\n'
- # A failing command: nothing changed, the write fails in words,
- # its stderr in +Errors.
- try:
- client.write(f'/pane/{a}/ctl', b'Edit , | echo nope >&2; exit 4\n')
- raise AssertionError('a failing Edit command was taken')
- except OSError as why:
- assert 'exit status 4' in str(why), why
- assert client.read(f'/pane/{a}/body') == b'APPLE\nFIG\nPEAR\n'
- index = client.read('/index').decode()
- errors = [line.split()[0] for line in index.splitlines() if line.endswith(('+Errors', f'{root}/+Errors')) or '+Errors' in line]
- assert errors and any(b'nope' in client.read(f'/pane/{e}/body') for e in errors), index
- # While a slow command runs, the editor answers.
- done = threading.Event()
-
- def slow():
- with Client(address) as other:
- other.write(f'/pane/{b}/ctl', b'Edit , | sleep 1; cat\n')
- done.set()
- worker = threading.Thread(target=slow)
- worker.start()
- time.sleep(.2)
- began = time.monotonic()
- client.read('/index')
- assert time.monotonic() - began < .5 and not done.is_set(), 'the editor waited on the command'
- worker.join(timeout=15)
- assert done.is_set()
- # On the same connection: the Edit's write is held, and a read sent
- # after it is answered first; a Tflush of it stops it, changes
- # nothing, and frees the way for the next Edit.
- def send(kind, tag, payload):
- client.socket.sendall(struct.pack('<IBH', 7 + len(payload), kind, tag) + payload)
-
- def answer():
- size, kind, tag = struct.unpack('<IBH', client.receive(7))
- return kind, tag, client.receive(size - 7)
- ctl = client.open(f'/pane/{a}/ctl', 1)
- status = client.open('/status', 0)
- line = b'Edit , | sleep 1; cat\n'
- began = time.monotonic()
- send(118, 900, struct.pack('<IQI', ctl, 0, len(line)) + line)
- send(116, 901, struct.pack('<IQI', status, 0, 4096))
- first = answer()
- assert first[1] == 901 and first[0] == 117 and time.monotonic() - began < .5, (first[:2], time.monotonic() - began)
- second = answer()
- assert second[1] == 900 and second[0] == 119, second
- line = b'Edit , | sleep 5; tr a-z A-Z\n'
- send(118, 902, struct.pack('<IQI', ctl, 0, len(line)) + line)
- time.sleep(.3)
- send(108, 903, struct.pack('<H', 902))
- tags = sorted(answer()[1] for _ in range(2))
- assert tags == [902, 903], tags
- client.close(ctl)
- client.close(status)
- time.sleep(1)
- client.write(f'/pane/{a}/ctl', b'Edit , | cat\n')
- time.sleep(4.5)
- assert client.read(f'/pane/{a}/body') == b'APPLE\nFIG\nPEAR\n', client.read(f'/pane/{a}/body')
- # X: each open file whose line matches; w writes it.
- client.write(f'/pane/{a}/ctl', b'Edit X/b\\.txt$/ ,s/apple/APPLE/\n')
- assert client.read(f'/pane/{b}/body') == b'APPLE pie\n'
- client.write(f'/pane/{b}/ctl', b'Edit w\n')
- assert (root / 'b.txt').read_bytes() == b'APPLE pie\n'
- client.write(f'/pane/{a}/ctl', b'Edit 1 w first.txt\n')
- assert (root / 'first.txt').read_bytes() == b'APPLE\n'
- # A file changed on disk under unsaved edits: Edit's w refuses
- # once, as Save does, and the next w overwrites it.
- w1 = root / 'w1.txt'
- w1.write_bytes(b'one\n')
- w = look(client, str(w1))
- client.write(f'/pane/{w}/ctl', b'Edit $a/two/\n')
- seen = client.read('/log').decode().count(f'\nchanged {w}')
- with open(w1, 'ab') as f:
- f.write(b'external\n')
- deadline = time.monotonic() + 10
- while client.read('/log').decode().count(f'\nchanged {w}') == seen:
- assert time.monotonic() < deadline, 'the change on disk was not heard'
- time.sleep(.05)
- try:
- client.write(f'/pane/{w}/ctl', b'Edit w\n')
- raise AssertionError('w overwrote a file changed on disk')
- except OSError as why:
- assert 'modified on disk since read (w again to overwrite)' in str(why), why
- assert w1.read_bytes() == b'one\nexternal\n'
- client.write(f'/pane/{w}/ctl', b'Edit w\n')
- assert w1.read_bytes() == b'one\ntwo', w1.read_bytes()
-
-
-def test(binary, quic=False):
- started = time.monotonic()
- cut_writes(binary)
- edit_commands(binary)
- odd_paths(binary)
- delcol_last_keeps_window(binary)
- run_file(binary)
- exit_answers(binary)
- unattended_messages_expire(binary)
- new_terminals_named_once(binary)
- directory_panes(binary)
- putall_answers_after_its_saves(binary)
- workflow_canary(binary)
- recent_survives_restart(binary)
- store_shared_between_windows(binary)
- python_repl_runs_one_line(binary)
- signal_cleans_up(binary)
- for options, message in [
- (['--fs'], b'no such option'),
- (['--fs9'], b'no such option'),
- (['--', 'a', 'b'], b'is the second'),
- (['--9p'], b'--9p needs a socket name'),
- (['--9p='], b'invalid 9P socket name'),
- (['--9p=a/b'], b'invalid 9P socket name'),
- (['--attach=work', '--9p=work'], b'--attach has none'),
- (['--9p-tcp'], b'--9p-tcp needs'),
- (['--9p-tcp='], b'--9p-tcp needs'),
- (['--9p-tcp=tcp!localhost!5640'], b'--9p-tcp needs'),
- (['--9p-tcp=quic!127.0.0.1!5640'], b'--9p-tcp needs'),
- (['--9p-tcp=tcp!127.0.0.1!5640', '--9p-tcp=tcp!127.0.0.1!5641'], b'specified twice'),
- (['--attach=work', '--9p-tcp=tcp!127.0.0.1!5640'], b'--attach has none'),
- (['--9p-quic'], b'--9p-quic needs'),
- (['--9p-quic='], b'--9p-quic needs'),
- (['--9p-quic=tcp!127.0.0.1!5640'], b'--9p-quic needs'),
- (['--mount=os=work'], b'invalid mount name'),
- (['--mount=peer='], b'invalid mount name or dial'),
- (['--mount=peer=one', '--mount=peer=two'], b'duplicate mount'),
- (['--mount=peer=tcp!127.0.0.1!0'], b'invalid mount dial'),
- (['--attach=work', '--mount=peer=one'], b'--attach has none'),
- ]:
- result = subprocess.run([binary, *options], capture_output=True, timeout=5)
- assert result.returncode == 1 and message in result.stderr, (options, result)
- with tempfile.TemporaryDirectory(prefix='pardes-fs-') as directory:
- root = Path(directory)
- with session(binary, root, 'renamed', '--9p=explicit', socket_name='explicit') as (named, named_address):
- assert named_address.name == 'pardes-9p-explicit.sock'
- assert not (root / 'pardes-9p-renamed.sock').exists()
- assert named.read('/pane/1/body') == b'initial\n'
- with session(binary, root, 'first') as (client, address):
- assert {'pane', 'index', 'status', 'look', 'exec', 'log', 'os'} <= set(client.list('/'))
- assert 'self' not in client.list('/')
- assert client.read('/pane/1/body') == b'initial\n'
- assert client.read('/os' + str(root / 'first.txt')) == b'initial\n'
- assert 'first.txt' in client.list('/os' + str(root))
- assert client.read('/pane/1/../1/body') == b'initial\n'
- assert client.read('/os' + str(root) + '/../' + root.name + '/first.txt') == b'initial\n'
-
- listing = root / 'listing'
- listing.mkdir()
- (listing / 'broken').symlink_to(listing / 'missing')
- for number in range(24):
- (listing / f'entry-{number:02}').touch()
- with Client(address, msize=256) as small:
- assert set(small.list('/os' + str(listing))) == {
- f'entry-{number:02}' for number in range(24)}
- # 64 KiB frames: a ctl line with no newline is held whatever the
- # size of its write -- a mount cuts lines anywhere -- and runs at
- # the close, its failure then in the log.
- with Client(address, msize=65536) as big:
- assert big.msize == 65536
- ctl = big.open('/pane/1/ctl', 1)
- line = b'bogus' + b'x' * 20000
- big.rpc(118, struct.pack('<IQI', ctl, 0, len(line)) + line)
- assert b'ctl: unknown control message' not in client.read('/log')
- big.close(ctl)
- assert b'ctl: unknown control message' in client.read('/log')
-
- frozen = client.open('/screen')
- before = bytearray(client.read_fid(frozen, count=31))
- client.write('/pane/1/body', b'screen changed\n')
- while chunk := client.read_fid(frozen, len(before)):
- before.extend(chunk)
- client.close(frozen)
- old_screen = json.loads(before)
- old_text = ''.join(cell[0] for cell in old_screen['cells'])
- assert 'initial' in old_text and 'screen changed' not in old_text
- assert 'screen changed' in ''.join(cell[0] for cell in client.screen()['cells'])
-
- payload = ('const value = "λ";\n' * 3000).encode()
- serial = new_pane(client, payload)
- body = f'/pane/{serial}/body'
- assert client.read(body) == payload
- client.write(body, b'replaced\n', truncate=True)
- assert client.read(body) == b'replaced\n'
-
- event = client.open(f'/pane/{serial}/event')
- client.write(body, b'event\n')
- record = client.read_fid(event)
- assert record.startswith(b'EI') and b'event\n' in record, record
- client.close(event)
-
- os_path = '/os' + str(root / 'first.txt')
- client.write(os_path, b'written through 9P\n', truncate=True)
- assert (root / 'first.txt').read_bytes() == b'written through 9P\n'
- assert client.read(os_path) == b'written through 9P\n'
-
- for index in range(12):
- with Client(address) as other:
- assert other.read(body) == b'replaced\nevent\n', index
-
- missing = False
- try:
- client.read('/pane/4294967295/body')
- except OSError:
- missing = True
- assert missing
- assert client.read(body) == b'replaced\nevent\n'
-
- if 'src' in client.list('/'):
- source = look(client, '/virtual/src/pardes.zig')
- assert b'pub const Pardes' in client.read(f'/pane/{source}/body')
-
- (root / 'index').write_bytes(b'OS takes precedence\n')
- opened = look(client, 'index')
- assert client.read(f'/pane/{opened}/body') == b'OS takes precedence\n'
- opened = look(client, '/n/self/index')
- index = client.read(f'/pane/{opened}/body')
- assert index.split()[0] == b'1' and b'first.txt' in index, index
- (root / 'explicit.txt').write_bytes(b'explicit OS mount\n')
- opened = look(client, '/n/os' + str(root / 'explicit.txt'))
- assert client.read(f'/pane/{opened}/body') == b'explicit OS mount\n'
-
- own = root / 'pardes-9p-second.sock'
- with session(binary, root, 'second', '--mount=peer=' + str(address),
- '--mount=own=' + str(own)) as (remote, _):
- opened = look(remote, '/n/own/pane/1/body')
- assert remote.read(f'/pane/{opened}/body') == b'initial\n'
- opened = look(remote, '/n/peer/pane/' + str(serial) + '/body')
- assert remote.read(f'/pane/{opened}/body') == b'replaced\nevent\n'
- remote.write(f'/pane/{opened}/body', b'saved to peer\n', truncate=True)
- remote.write(f'/pane/{opened}/exec', b'Save\n')
- assert client.read(body) == b'saved to peer\n'
-
- large = b'mounted file contents\n' * 60000
- large_path = root / 'large.txt'
- large_path.write_bytes(large)
- opened = look(remote, '/n/peer/os' + str(large_path))
- assert remote.read(f'/pane/{opened}/body') == large
- edited = large[:-3] + b'edited\n'
- remote.write(f'/pane/{opened}/body', edited, truncate=True)
- remote.write(f'/pane/{opened}/exec', b'Save\n')
- assert large_path.read_bytes() == edited
- assert client.read('/os' + str(large_path)) == edited
- # A path not there under a peer's mount is a miss, as any
- # path to no file is: the write answered, the err logged.
- remote.write('/look', b'/n/peer/os' + str(root / 'no-such-file.txt').encode() + b'\n')
- assert b'no such file' in remote.read('/log')
-
- with session(binary, root, 'runtime') as (remote, own_address):
- control = new_pane(remote, b'')
- execute(remote, control, 'Mount peer ' + str(address))
- opened = look(remote, '/n/peer/pane/1/body', source=control)
- assert remote.read(f'/pane/{opened}/body') == client.read('/pane/1/body')
- # A builtin that fails through exec fails the write (one rule).
- execute_refused(remote, control, 'Mount peer ' + str(own_address), 'already mounted')
- execute_refused(remote, control, 'Unmount peer', 'mount in use') # a pane is open on it
- remote.write(f'/pane/{opened}/body', b'runtime mounted Save\n', truncate=True)
- remote.write(f'/pane/{opened}/exec', b'Save\n')
- assert client.read('/pane/1/body') == b'runtime mounted Save\n'
- assert remote.read('/pane/1/body') == b'initial\n'
- roots = look(remote, '/n', source=control)
- assert b'/n/peer/\n' in remote.read(f'/pane/{roots}/body')
- remote.write(f'/pane/{opened}/exec', b'Del\n')
- execute(remote, control, 'Unmount peer')
- remote.write(f'/pane/{roots}/ctl', b'get\n')
- assert remote.read(f'/pane/{roots}/body') == b'/n/os/\n/n/self/\n'
- execute(remote, control, 'Mount peer ' + str(own_address))
- opened = look(remote, '/n/peer/pane/1/body', source=control)
- assert remote.read(f'/pane/{opened}/body') == b'initial\n'
-
- with session(binary, root, 'mounted-startup', '--mount=peer=' + str(address),
- file='/n/peer/pane/1/body:1') as (remote, _):
- assert remote.read('/pane/1/body') == b'runtime mounted Save\n'
-
- client.write(f'/pane/{serial}/exec', b'Del\n')
- assert str(serial) not in client.list('/pane')
-
- for tty in [False, True]:
- name = 'restore tty space' if tty else 'restore detached space'
- with session(binary, root, name, tty=tty) as (old, address):
- old.write('/pane/1/body', b'dumped state\n', truncate=True)
- control = new_pane(old, b'')
- execute_refused(old, control, 'Restore ' + str(root / 'missing dump.zon'), 'no such file')
- assert old.read('/pane/1/body') == b'dumped state\n'
- execute(old, control, 'Mount own ' + str(address))
- execute(old, control, 'Dump')
- saved = root / (name + '.dump.zon')
- deadline = time.monotonic() + 5
- while not saved.exists():
- assert time.monotonic() < deadline, 'Dump did not write its state'
- time.sleep(.005)
- held_body = old.open('/pane/1/body')
- held_screen = old.open('/screen')
- old_ids = {int(row.split()[0]) for row in old.read('/index').splitlines()}
- assert old.read_fid(held_body) == b'dumped state\n'
- assert old.read_fid(held_screen, count=1) == b'{'
- old.write('/pane/1/body', b'changed after dump\n', truncate=True)
- # Unsaved text is asked about once, as Exit asks; the second
- # Restore goes ahead, and is answered before the hang-up.
- try:
- execute(old, control, 'Restore ' + str(saved))
- except OSError as refused:
- assert 'Modified' in str(refused), refused
- assert old.read('/pane/1/body') == b'changed after dump\n'
- # Answered before the hang-up: the write's reply comes; the
- # clunk after it may meet the connection already cut.
- restore = old.open(f'/pane/{control}/exec', 1)
- line = ('Restore ' + str(saved) + '\n').encode()
- old.rpc(118, struct.pack('<IQI', restore, 0, len(line)) + line)
- try:
- old.close(restore)
- except (EOFError, OSError):
- pass
- # Refused while the connections are cut, or cut already.
- try:
- old.read_fid(held_body)
- except (EOFError, OSError):
- pass
- else:
- raise AssertionError('Restore left an old body fid usable')
- with Client(address) as restored:
- # The new log names its panes, then where they came from.
- log = restored.read('/log')
- assert b'\nrestore ' + str(saved).encode() + b'\n' in log, log
- # Its panes are the dump's, their serials too.
- restored_ids = [int(row.split()[0]) for row in restored.read('/index').splitlines()]
- assert len(restored_ids) == 2 and set(restored_ids) == old_ids, restored_ids
- file_id, control_id = restored_ids
- contents = restored.read(f'/pane/{file_id}/body')
- assert contents == b'dumped state\n', (contents, saved.read_text(), restored.read('/index'))
- assert restored.read(f'/pane/{control_id}/body') == b'', 'commands ran through exec, not the body'
- assert restored.screen()['cols'] == (80 if tty else 160) # a detached one is 160x50
- opened = look(restored, '/n/own/os' + str(root / (name + '.txt')), source=control_id)
- assert restored.read(f'/pane/{opened}/body') == b'initial\n'
-
- with session(binary, root, 'forwarding') as (client, address):
- child_dir = root / 'child-working-directory'
- child_dir.mkdir()
- spaced = child_dir / 'space name.txt'
- spaced.write_bytes(b'first line\nsecond line\n')
- env = os.environ.copy()
- # PARDES_PID is what says "you are inside a pardes"; the socket
- # and the pane only say how to reach it. Without the pid the child
- # starts its own session, so this test used to pass only when the
- # runner itself happened to be running inside one.
- status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())
- env.update(PARDES_9P=str(address), PARDES_PANE='1', PARDES_PID=status['pid'])
- env.pop('PARDES_FORWARD_LOOK', None)
- for word, expected, selected, reuse in [('space name.txt:2:4', spaced.read_bytes(), [14, 14], False),
- ('/n/self/pane/1/body', b'initial\n', None, False),
- ('/virtual/pane/1/body:1:2-4', b'initial\n', [1, 4], True)]:
- before = {int(row.split()[0]) for row in client.read('/index').splitlines()}
- result = subprocess.run([binary, '--tty', word], cwd=child_dir, env=env,
- capture_output=True, timeout=3)
- assert result.returncode == 0, (word, result.stderr)
- after = {int(row.split()[0]) for row in client.read('/index').splitlines()}
- if reuse:
- assert after == before
- else:
- opened, = after - before
- assert client.read(f'/pane/{opened}/body') == expected
- assert client.read('/pane/1/body') == b'initial\n'
- if selected is not None:
- # The selection is dot; reading it takes no command at all.
- actual = list(map(int, client.read(f'/pane/{opened}/dot').split()))
- assert actual == selected, (word, actual, selected)
- addr = client.open('/pane/1/addr', 2)
- limit = client.open('/pane/1/limit', 2)
- for fid, text in [(addr, b'#0,#3'), (limit, b'#0,#3'), (addr, b'#0')]:
- client.rpc(118, struct.pack('<IQI', fid, 0, len(text)) + text)
- try:
- for _ in range(8):
- result = subprocess.run([binary], cwd=child_dir, env=env,
- capture_output=True, timeout=3)
- assert result.returncode == 1 and b'already inside pardes' in result.stderr
- text = b'/tial/'
- try:
- client.rpc(118, struct.pack('<IQI', addr, 0, len(text)) + text)
- except OSError:
- pass
- else:
- raise AssertionError('no-argument child cleared the parent address limit')
- finally:
- client.close(addr)
- client.close(limit)
- client.read('/pane/1/ctl')
-
- control = new_pane(client, b'')
- execute(client, control, 'Dump')
- saved = root / 'forwarding.dump.zon'
- deadline = time.monotonic() + 3
- while not saved.exists():
- assert time.monotonic() < deadline, 'forwarding fixture Dump did not finish'
- time.sleep(.005)
- before = client.read('/index')
- inherited = {key: env[key] for key in ['PARDES_9P', 'PARDES_PANE', 'PARDES_PID']}
- cases = [
- ('mount', ['--mount=peer=' + str(address), '/n/peer/pane/1/body'], None),
- ('name', ['--9p=forwarded-name'], 'forwarded-name'),
- ('tcp', ['--9p-tcp=tcp!127.0.0.1!0'], None),
- ('load', ['-l', str(saved)], None),
- ('shells', ['-n', '3'], None),
- ('toggle', ['--tty-toggle=q'], None),
- ('toggle-arg', ['--tty-toggle', 'q'], None),
- ]
- if quic:
- cases.append(('quic', ['--9p-quic=quic!127.0.0.1!0'], None))
- for name, options, socket_name in cases:
- with session(binary, root, 'forward-' + name, tty=True, socket_name=socket_name,
- launch=[*([] if name == 'shells' else ['--tty']), *options],
- inherited=inherited) as (local, local_address):
- assert local_address != address
- index = local.read('/index').splitlines()
- if name == 'mount':
- assert len(index) == 1 and b'/n/peer/' in index[0], index
- assert local.read('/pane/1/body') == b'initial\n'
- elif name == 'load':
- assert local.read('/index') == before
- assert local.read('/pane/1/body') == b'initial\n'
- else:
- # A bare tty launch is a shell plus the empty text pane
- # the boot puts under it; --shells=3 is the classic
- # three-shell layout and has no scratch.
- assert len(index) == (3 if name == 'shells' else 2), index
- if name in ['tcp', 'quic']:
- assert (name + '!127.0.0.1!').encode() in local.read('/listeners')
- assert client.read('/index') == before
- assert client.read('/pane/1/body') == b'initial\n'
-
- # A session that answers takes the file or says why, as acme's B
- # does: a pane it has not is its refusal and exit 1, never a
- # nested editor drawn inside the pane that asked.
- gone = subprocess.run([binary, '--tty', str(spaced)], cwd=root,
- env=dict(env, PARDES_PANE='4294967295'), stdin=subprocess.DEVNULL,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10)
- assert gone.returncode == 1 and gone.stderr.startswith(b'pardes: '), gone
- assert client.read('/index') == before
- for name, identity, word in [
- ('stale-file', dict(inherited, PARDES_9P=str(root / 'absent.sock')), str(spaced)),
- ('stale-missing', dict(inherited, PARDES_9P=str(root / 'absent.sock')), 'missing-child-file.txt'),
- ('stale-noarg', dict(inherited, PARDES_9P=str(root / 'absent.sock')), None),
- # No pid means "not inside a pardes at all", which is what
- # --nested withholds and what a plain shell has.
- ('not-nested', {k: v for k, v in inherited.items() if k != 'PARDES_PID'}, str(spaced)),
- ]:
- with session(binary, root, name, tty=True, inherited=identity,
- launch=['--tty', *([word] if word else [])]) as (local, local_address):
- assert local_address != address
- if word == str(spaced):
- assert local.read('/pane/1/body') == spaced.read_bytes()
- elif word:
- assert word.encode() in local.read('/index') or word in ''.join(
- cell[0] for cell in local.screen()['cells'])
- assert client.read('/index') == before
-
- # --wait (acme's E): a launch inside a pane lasts until the pane it
- # opened is deleted, as an $EDITOR must; one on a file already open
- # waits on that pane; a session that goes away ends it with 1.
- with session(binary, root, 'wait') as (client, address):
- # A command run from a pane's tag gets acme's $winid, that pane;
- # one from the workspace's tag, none.
- def command_body(node, line):
- client.write(node, line)
- serial = int(client.read(node).split()[-1])
- deadline = time.monotonic() + 10
- while True:
- log = client.read('/log').decode()
- ran = log.rfind(f'\nrun {serial} ')
- if ran >= 0 and f'\nexit {serial} ' in log[ran:]:
- body = client.read(f'/pane/{serial}/body').decode().split('% echo winid')[-1]
- client.remove(f'/pane/{serial}')
- return body
- assert time.monotonic() < deadline, log[-400:]
- time.sleep(.05)
- first_pane = min(int(row.split()[0]) for row in client.read('/index').splitlines())
- assert f'winid={first_pane}\n' in command_body(f'/pane/{first_pane}/tagexec', b'echo winid=$winid\n')
- assert 'winid=\n' in command_body('/tagexec', b'echo winid=$winid\n')
- # A second --detach of a name a live session holds: said, exit 1,
- # no Zig error trace.
- second_env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')}
- second_env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root))
- second = subprocess.run([binary, '--detach=wait'], cwd=root, env=second_env,
- stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, timeout=20)
- assert second.returncode == 1, second
- assert second.stderr == b'pardes: a session named wait is already running\n', second.stderr
- status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())
- env = {k: v for k, v in os.environ.items() if k != 'PARDES_FORWARD_LOOK'}
- env.update(PARDES_9P=str(address), PARDES_PANE='1', PARDES_PID=status['pid'])
- edited = root / 'wait edit.txt'
- edited.write_bytes(b'edit me\n')
-
- def serials():
- return {int(row.split()[0]) for row in client.read('/index').splitlines()}
-
- def launch(flag):
- before = serials()
- child = subprocess.Popen([binary, flag, str(edited)], cwd=root, env=env,
- stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
- deadline = time.monotonic() + 3
- while child.poll() is None and not serials() - before and time.monotonic() < deadline:
- time.sleep(.01)
- return child
-
- first = launch('--wait')
- opened, = serials() - {1}
- second = launch('-w')
- time.sleep(.5)
- assert first.poll() is None and second.poll() is None, (first.stderr.read(), second.stderr.read())
- assert serials() == {1, opened}
- client.write(f'/pane/{opened}/body', b'edited\n', truncate=True)
- execute(client, opened, 'Save')
- client.remove(f'/pane/{opened}')
- deleted = time.monotonic()
- assert first.wait(timeout=5) == 0
- # Woken by the `del` record, not a poll.
- assert time.monotonic() - deleted < .05, time.monotonic() - deleted
- assert second.wait(timeout=5) == 0
- assert edited.read_bytes() == b'edited\n'
- # --wait from outside the session's panes, $PARDES_9P alone (an
- # agent's EDITOR): asked of the session, not a screen of its own.
- outside_env = {k: v for k, v in env.items() if k not in ('PARDES_PANE', 'PARDES_PID')}
- far = root / 'wait far.txt'
- far.write_bytes(b'far\n')
- before = serials()
- waiting = subprocess.Popen([binary, '--wait', str(far)], cwd=root, env=outside_env,
- stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
- deadline = time.monotonic() + 5
- while not serials() - before and time.monotonic() < deadline:
- time.sleep(.01)
- far_pane, = serials() - before
- time.sleep(.3)
- assert waiting.poll() is None, waiting.stderr.read()
- client.remove(f'/pane/{far_pane}')
- assert waiting.wait(timeout=5) == 0
- # --wait with a $PARDES_9P that answers no more: said, exit 1,
- # never a screen of its own drawn in the asker's terminal.
- dead_env = dict(outside_env, PARDES_9P=str(root / 'pardes-9p-gone.sock'))
- gone = subprocess.run([binary, '--wait', str(far)], cwd=root, env=dead_env, capture_output=True, timeout=10)
- assert gone.returncode == 1 and b'does not answer' in gone.stderr, gone
- # --wait with no FILE says it needs one, and waits on nothing.
- bare = subprocess.run([binary, '--wait'], cwd=root, env=env, capture_output=True, timeout=10)
- assert bare.returncode == 1 and b'--wait needs a FILE' in bare.stderr, bare
- # `--` ends the options: a name starting with a dash is a name.
- dash = root / '-dash.txt'
- dash.write_bytes(b'dash\n')
- before = serials()
- assert subprocess.run([binary, '--', dash.name], cwd=root, env=env, capture_output=True, timeout=10).returncode == 0
- dashed, = serials() - before
- assert client.read(f'/pane/{dashed}/name').rstrip(b'\n') == str(dash).encode()
- client.remove(f'/pane/{dashed}')
- # A name under a directory that may not be searched is refused,
- # permission denied, exit 1, no pane: its Save could never be.
- # (Root searches any directory.)
- if os.geteuid() != 0:
- (root / 'noperm').mkdir()
- (root / 'noperm').chmod(0)
- before = serials()
- try:
- denied = subprocess.run([binary, 'noperm/a/b/c'], cwd=root, env=env, capture_output=True, timeout=10)
- finally:
- (root / 'noperm').chmod(0o755)
- assert denied.returncode == 1 and b'permission denied' in denied.stderr, denied
- assert serials() == before
- # --wait waits on the pane its look answered, on the look's own
- # open: a name /index shows escaped (a backslash is `\\` there)
- # is still the pane waited on, not one already gone.
- odd = root / 'wait\\odd.txt'
- odd.write_bytes(b'odd\n')
- before = serials()
- waiting = subprocess.Popen([binary, '--wait', str(odd)], cwd=root, env=env,
- stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
- deadline = time.monotonic() + 3
- while not serials() - before and time.monotonic() < deadline:
- time.sleep(.01)
- odd_pane, = serials() - before
- time.sleep(.5)
- assert waiting.poll() is None, waiting.stderr.read()
- client.remove(f'/pane/{odd_pane}')
- assert waiting.wait(timeout=5) == 0
- # A name not there yet opens a pane named it, as acme's B does,
- # and Save creates it: git's and fish's temporary files are.
- fresh = root / 'wait new.txt'
- before = serials()
- made = subprocess.Popen([binary, '--wait', fresh.name], cwd=root, env=env,
- stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
- # The launch makes the pane (pane/new), then names it: wait for
- # the name, not the pane.
- deadline = time.monotonic() + 3
- while str(fresh).encode() not in client.read('/index'):
- assert made.poll() is None, made.stderr.read()
- assert time.monotonic() < deadline, 'a new name opened no pane named it'
- time.sleep(.01)
- named, = serials() - before
- client.write(f'/pane/{named}/body', b'new\n')
- execute(client, named, 'Save')
- client.remove(f'/pane/{named}')
- code = made.wait(timeout=5)
- assert code == 0, (code, made.stderr.read())
- assert fresh.read_bytes() == b'new\n'
- # ...in a directory not there either: forwarded all the same, and
- # Save makes the directories.
- deep = root / 'wait-missing' / 'sub' / 'new.txt'
- before = serials()
- made = subprocess.Popen([binary, 'wait-missing/sub/new.txt'], cwd=root, env=env,
- stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
- assert made.wait(timeout=5) == 0, made.stderr.read()
- deadline = time.monotonic() + 3
- while str(deep).encode() not in client.read('/index'):
- assert time.monotonic() < deadline, 'a name in a missing directory opened no pane named it'
- time.sleep(.01)
- named, = serials() - before
- client.write(f'/pane/{named}/body', b'deep\n')
- execute(client, named, 'Save')
- client.remove(f'/pane/{named}')
- assert deep.read_bytes() == b'deep\n'
- # `pardes '~/x'`, quoted so the shell left the `~`: home all the
- # same: the launching shell's HOME.
- before = serials()
- quoted = subprocess.run([binary, '~/fwd-tilde.txt'], cwd=root, env=dict(env, HOME=str(root)),
- stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, timeout=10)
- assert quoted.returncode == 0, quoted.stderr
- assert f'{root}/fwd-tilde.txt'.encode() in client.read('/index'), client.read('/index')
- for serial in serials() - before:
- client.remove(f'/pane/{serial}')
- # A name the session refuses: its reason and exit 1, as B's,
- # never a nested editor, and no pane left for it.
- before = serials()
- refused = subprocess.run([binary, 'x' * 300 + '.txt'], cwd=root, env=env,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10)
- assert refused.returncode == 1, refused
- assert b'a component over 255 bytes' in refused.stderr, refused.stderr
- assert serials() == before, (serials(), before)
- # A pane address the session has not is refused in words, exit
- # 1, no pane made for a file of that name; one it has is looked
- # at. A name with a newline is said in words, not an error name.
- # `pardes -` (a terminal's PAGER): stdin, its escapes stripped, in a
- # +Pager pane of the session, clean, and back at once.
- fed = subprocess.run([binary, '-'], cwd=root, env=env, input=b'\x1b[1mbold\x1b[0m text\n',
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10)
- assert fed.returncode == 0, fed.stderr
- made_now, = serials() - before
- assert client.read(f'/pane/{made_now}/name') == f'{root}/+Pager\n'.encode()
- assert client.read(f'/pane/{made_now}/body') == b'bold text\n'
- row = [r for r in client.read('/index').splitlines() if r.split()[0] == str(made_now).encode()][0]
- assert row.split()[2] == b'0', row # clean: nothing to ask at Exit
- # Its `new` record names it as it is: no +New and a rename.
- log_now = client.read('/log').decode()
- assert f'\nnew {made_now} {root}/+Pager\n' in '\n' + log_now, log_now[-400:]
- assert f'\nrename {made_now} ' not in '\n' + log_now
- # Its colours are the pane's, drawn and never read: the body is
- # the plain text, and the screen shows `red` in the theme's red,
- # the rest in the pane's ink. Mapped through the theme, as a
- # terminal's are; Filter shows them as sent, `rgb` in the colour
- # given.
- # Each run is its own +Pager; one there already keeps its page.
- fed = subprocess.run([binary, '-'], cwd=root, env=env,
- input=b'\x1b]0;title\x07\x1b[31mred\x1b[m \x1b[38;2;1;2;3mrgb\x1b[m plain\n',
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10)
- assert fed.returncode == 0, fed.stderr
- coloured, = serials() - before - {made_now}
- assert client.read(f'/pane/{coloured}/name') == f'{root}/+Pager\n'.encode()
- assert client.read(f'/pane/{coloured}/body') == b'red rgb plain\n'
- assert client.read(f'/pane/{made_now}/body') == b'bold text\n'
- def inks():
- screen = client.screen()
- for row in range(screen['rows']):
- cells = screen['cells'][row * screen['cols']:(row + 1) * screen['cols']]
- at = ''.join(cell[0] for cell in cells).find('red rgb plain')
- if at >= 0:
- return [screen['styles'][cells[at + i][1]]['fg'] for i in (0, 4, 8)]
- raise AssertionError(('the paged text is not on the screen', screen))
- ink = inks()
- assert ink[0] != ink[2] and ink[1] != {'rgb': [1, 2, 3]}, ink
- client.write(f'/pane/{coloured}/exec', b'Filter\n')
- ink = inks()
- assert ink[1] == {'rgb': [1, 2, 3]}, ink
- assert ink[0] != ink[2] and ink[0] != ink[1], ink
- # A progress line keeps its last state.
- fed = subprocess.run([binary, '-'], cwd=root, env=env, input=b'again\r\nprogress 10%\rprogress 100%\n',
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10)
- assert fed.returncode == 0, fed.stderr
- again, = serials() - before - {made_now, coloured}
- assert client.read(f'/pane/{again}/body') == b'again\nprogress 100%\n'
- for serial in (made_now, coloured, again):
- client.remove(f'/pane/{serial}')
- # More than one 8 MiB write's worth arrives whole, byte for byte,
- # in its +Pager (each write once replaced the last).
- big = b''.join(b'line %08d of the paged text\n' % i for i in range(400000)) # ~11 MiB
- assert len(big) > 8 * 1024 * 1024
- fed = subprocess.run([binary, '-'], cwd=root, env=env, input=big,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=120)
- assert fed.returncode == 0, fed.stderr
- big_pane, = serials() - before
- assert client.read(f'/pane/{big_pane}/body') == big, 'the paged body is not the text'
- # Past the 256 MiB cap: the first 256 MiB, then the cut note.
- cap = 256 * 1024 * 1024
- huge = subprocess.Popen(['sh', '-c', f'yes 0123456789abcdef | head -c {cap + 4096}'], stdout=subprocess.PIPE)
- fed = subprocess.run([binary, '-'], cwd=root, env=env, stdin=huge.stdout,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=600)
- huge.wait()
- assert fed.returncode == 0, fed.stderr
- client.remove(f'/pane/{big_pane}')
- big_pane, = serials() - before
- got = client.read(f'/pane/{big_pane}/body')
- note = b'\n[pardes: the rest of stdin is cut: a pane holds at most a file\'s limit]\n'
- assert got.endswith(note) and len(got) == cap + len(note), (len(got), got[-120:])
- assert got[:cap] == (b'0123456789abcdef\n' * (cap // 17 + 1))[:cap]
- del got, big
- client.remove(f'/pane/{big_pane}')
- # From a directory whose name could not be a ctl line: the
- # session's directory's +Pager, the text whole, no stray +New.
- odd = root / 'two\nlines'
- odd.mkdir()
- fed = subprocess.run([binary, '-'], cwd=odd, env=env, input=b'odd dir text\n',
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10)
- assert fed.returncode == 0, fed.stderr
- odd_pane, = serials() - before
- assert client.read(f'/pane/{odd_pane}/name') == f'{root}/+Pager\n'.encode()
- assert client.read(f'/pane/{odd_pane}/body') == b'odd dir text\n'
- client.remove(f'/pane/{odd_pane}')
- # Nothing on stdin: no pane, and done.
- fed = subprocess.run([binary, '-'], cwd=root, env=env, input=b'',
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10)
- assert fed.returncode == 0 and serials() == before, (fed, serials())
- # A new name in a directory there but not writable: refused now,
- # not a pane that only fails at Save. (Root writes anywhere.)
- shut = root / 'shut-dir'
- shut.mkdir()
- shut.chmod(0o500)
- try:
- if os.geteuid() != 0:
- refused = subprocess.run([binary, 'shut-dir/new.txt'], cwd=root, env=env,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10)
- assert refused.returncode == 1, refused
- assert b'may not be read or written' in refused.stderr, refused.stderr
- assert serials() == before, (serials(), before)
- finally:
- shut.chmod(0o755)
- for word, said in (('@p999:1', b'pardes: @p999:1: this session has no such pane'),
- ('two\nlines.txt', b'pardes: two\\nlines.txt: a file name is one line')):
- refused = subprocess.run([binary, word], cwd=root, env=env,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10)
- assert refused.returncode == 1, refused
- assert said in refused.stderr, refused.stderr
- assert b'NotOneLine' not in refused.stderr and b'NoSuchPane' not in refused.stderr
- assert serials() == before, (serials(), before)
- looked = subprocess.run([binary, '@p1:1'], cwd=root, env=env,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10)
- assert looked.returncode == 0, looked.stderr
- assert serials() == before, (serials(), before)
- orphan = launch('--wait')
- assert orphan.poll() is None
- assert orphan.wait(timeout=5) == 1
-
- # `pardes -` outside a session: an editor whose first pane is a
- # +Pager of stdin, as `vim -`; here a detached one, to read it.
- outside_env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')}
- outside_env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), PARDES_NOTIME='1')
- boot = subprocess.Popen([binary, '--detach=stdinboot', '-'], cwd=root, env=outside_env,
- stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
- start_new_session=True)
- try:
- boot.stdin.write(b'\x1b[32mfrom stdin\x1b[m\nsecond\n')
- boot.stdin.close()
- sock = root / 'pardes-9p-stdinboot.sock'
- deadline = time.monotonic() + 10
- while not sock.exists():
- assert boot.poll() is None, boot.stderr.read()
- assert time.monotonic() < deadline, 'the stdin boot never listened'
- time.sleep(.05)
- with Client(str(sock)) as booted:
- first = booted.read('/index').split(b'\n')[0].split()
- assert first[3] == f'{root}/+Pager'.encode(), first
- assert booted.read(f'/pane/{first[0].decode()}/body') == b'from stdin\nsecond\n'
- finally:
- boot.terminate()
- boot.wait(timeout=10)
-
- # Killed with SIGTERM or SIGHUP, a tty editor puts its terminal
- # back (main screen, cooked mode) before it dies: a nested one
- # killed in a pane leaves that pane usable.
- # ...and its 9P socket goes with it. One a dead editor left behind
- # (no process has its pid) is swept at the next start; a live
- # pid's, here this test's own, never is.
- def unix_socket_file(path):
- made = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
- made.bind(str(path))
- made.close()
- for sig in (signal.SIGTERM, signal.SIGHUP):
- dead_socket = root / 'pardes-9p-2147483647.sock'
- live_socket = root / f'pardes-9p-{os.getpid()}.sock'
- for path in (dead_socket, live_socket):
- if not path.exists():
- unix_socket_file(path)
- master, slave = os.openpty()
- fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack('HHHH', 24, 80, 0, 0))
- cooked = termios.tcgetattr(slave)
- env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')}
- env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), TERM='xterm-256color', PARDES_NOTIME='1')
- existing = set(root.glob('pardes-9p-*.sock'))
- killed = subprocess.Popen([binary, '--tty', 'x.txt'], cwd=root, env=env,
- stdin=slave, stdout=slave, stderr=subprocess.DEVNULL,
- start_new_session=True,
- preexec_fn=lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0))
- seen = b''
- try:
- deadline = time.monotonic() + 10
- while b'\x1b[?1049h' not in seen:
- assert time.monotonic() < deadline, seen[-200:]
- if select.select([master], [], [], .1)[0]:
- seen += os.read(master, 65536)
- time.sleep(.3)
- # The session's own, named by its pid: forked off the
- # launched process, which stays as its stub.
- own_socket, = set(root.glob('pardes-9p-*.sock')) - existing
- assert not dead_socket.exists()
- assert live_socket.exists()
- killed.send_signal(sig)
- assert killed.wait(timeout=5) == -sig
- if sig == signal.SIGHUP:
- # A hangup is the terminal's end: the session carries on
- # detached (the daemon keeps it) until told to end.
- assert own_socket.exists(), own_socket
- os.kill(int(own_socket.name[len('pardes-9p-'):-len('.sock')]), signal.SIGTERM)
- deadline = time.monotonic() + 5
- while own_socket.exists() and time.monotonic() < deadline:
- time.sleep(.01)
- assert not own_socket.exists(), own_socket
- while select.select([master], [], [], .2)[0]:
- try:
- chunk = os.read(master, 65536)
- except OSError:
- break
- if not chunk:
- break
- seen += chunk
- tail = seen[seen.rindex(b'\x1b[?1049h'):]
- assert b'\x1b[?1049l' in tail, tail[-200:]
- assert termios.tcgetattr(slave)[3] & termios.ICANON == cooked[3] & termios.ICANON
- finally:
- if killed.poll() is None:
- killed.kill()
- killed.wait()
- os.close(master)
- os.close(slave)
-
- # A launch with no terminal to draw on (a pty that is no one's
- # controlling terminal) says so and exits 1, no error trace.
- master, slave = os.openpty()
- try:
- env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')}
- env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), TERM='xterm-256color')
- result = subprocess.run([binary, '--tty', 'x.txt'], cwd=root, env=env, stdin=slave, stdout=slave,
- stderr=subprocess.PIPE, start_new_session=True, timeout=10)
- finally:
- os.close(master)
- os.close(slave)
- assert result.returncode == 1, result
- assert b'pardes: no terminal to draw on' in result.stderr and b'.zig:' not in result.stderr, result.stderr
-
- for tty in [False, True]:
- name = 'lsp-tty' if tty else 'lsp-detached'
- with session(binary, root, name, tty=tty) as (client, _):
- control = new_pane(client, b'')
- execute(client, control, 'Lspinfo')
- deadline = time.monotonic() + 3
- while True:
- matches = [int(row.split()[0]) for row in client.read('/index').splitlines()
- if b'+Lsp' in row]
- if matches:
- break
- assert time.monotonic() < deadline, (name, 'Lspinfo did not complete')
- time.sleep(.005)
- assert len(matches) == 1, matches
- result = client.read(f'/pane/{matches[0]}/body')
- assert result.startswith(b'backend: lsp-client\n'), result
- assert b'protocol servers (lsp-client):' in result, result
- assert client.read('/pane/1/body') == b'initial\n'
- assert '+Lsp' in ''.join(cell[0] for cell in client.screen()['cells'])
-
- with session(binary, root, 'highlight') as (client, _):
- client.write('/pane/1/name', b'keywords.zig\n')
- source = b'// padding\n' * 9999 + b'fn check() void {\n if (true) return;\n}\n'
- client.write('/pane/1/body', source, truncate=True)
- screen = client.screen()
- assert len(screen['cells']) == screen['cols'] * screen['rows']
- found = set()
- for row in range(screen['rows']):
- cells = screen['cells'][row * screen['cols']:(row + 1) * screen['cols']]
- text = ''.join(cell[0] for cell in cells)
- for word in ['fn', 'if']:
- start = text.find(word + ' ')
- if start < 0:
- continue
- styles = [screen['styles'][cell[1]] for cell in cells[start:start + len(word)]]
- # Weight is opt-in (SyntaxBold, off by default); colour is
- # not, so the keyword must differ from the text beside it.
- plain = screen['styles'][cells[start + len(word)][1]]
- assert all(not style['bold'] and style['fg'] == styles[0]['fg'] for style in styles), (word, styles)
- assert styles[0]['fg'] != plain['fg'], (word, styles[0], plain)
- found.add(word)
- assert found == {'fn', 'if'}, screen
-
- with session(binary, root, 'long-names') as (client, _):
- directory = root / ('directory-' + 'd' * 54)
- directory.mkdir()
- for length in [29, 63, 127, 255]:
- name = 'f' * (length - 4) + '.txt'
- path = directory / name
- path.write_bytes(b'long filename\n')
- wire_path = '/os' + str(path)
- assert name in client.list('/os' + str(directory))
- assert client.read(wire_path) == b'long filename\n'
- client.write(wire_path, b'written through long filename\n', truncate=True)
- assert path.read_bytes() == b'written through long filename\n'
- opened = look(client, '/n/os' + str(path))
- assert client.read(f'/pane/{opened}/body') == path.read_bytes()
- client.remove(f'/pane/{opened}') # a long name's tag takes rows
- opened = look(client, '/n/os' + str(directory))
- listing = client.read(f'/pane/{opened}/body')
- for path in directory.iterdir():
- assert ('/n/os' + str(path)).encode() in listing
- if quic:
- quic_test(binary)
- print(f'9P integration: passed ({time.monotonic() - started:.3f}s)')
-
-
-def quic_test(binary):
- for tty in [False, True]:
- for host in ['127.0.0.1', '::1']:
- with tempfile.TemporaryDirectory(prefix='pardes-quic-fs-') as directory:
- root = Path(directory)
- ports = {}
- with session(binary, root, 'server', '--9p-tcp=tcp!' + host + '!0',
- '--9p-quic=quic!' + host + '!0', tty=tty, network=ports) as (unix, address):
- assert set(unix.read('/listeners').decode().splitlines()) == {
- 'unix!' + str(address), 'tcp!' + host + '!' + str(ports['TCP']),
- 'quic!' + host + '!' + str(ports['QUIC'])}
- with Client((host, ports['TCP'])) as tcp:
- unix_body = unix.open('/pane/1/body')
- tcp_body = tcp.open('/pane/1/body')
- with session(binary, root, 'client') as (remote, _):
- control = new_pane(remote, b'')
- dial = 'quic!' + host + '!' + str(ports['QUIC'])
- execute(remote, control, 'Mount peer ' + dial)
- opened = look(remote, '/n/peer/pane/1/body', source=control)
- body = f'/pane/{opened}/body'
- assert remote.read(body) == b'initial\n'
- payload = 'saved café λ\n'.encode() + b'x' * (3 * 8192 + 27) + b'\n'
- remote.write(body, payload, truncate=True)
- remote.write(f'/pane/{opened}/exec', b'Save\n')
- assert unix.read_fid(unix_body) == payload[:unix.msize - 11]
- assert tcp.read('/pane/1/body') == payload
- tcp.write('/pane/1/body', b'TCP to QUIC Get\n', truncate=True)
- remote.write(f'/pane/{opened}/ctl', b'get\n')
- assert remote.read(body) == b'TCP to QUIC Get\n'
- assert unix.read_fid(unix_body) == b'TCP to QUIC Get\n'
- assert tcp.read_fid(tcp_body) == b'TCP to QUIC Get\n'
- screen_pane = look(remote, '/n/peer/screen', source=control)
- screen = json.loads(remote.read(f'/pane/{screen_pane}/body'))
- assert len(screen['cells']) == screen['cols'] * screen['rows']
- # The peer's own screen, whole, over QUIC. Its pane's
- # tag, not its text: where the view sits after the
- # body shrank from a long wrapped line depends on
- # whether a frame was drawn meanwhile (sometimes
- # on the empty last line, ` 2`, text off-screen).
- assert 'server.txt Save' in ''.join(cell[0] for cell in screen['cells'])
- large = ('normal λ line\n' * 20000).encode()
- fixture = root / 'large.txt'
- fixture.write_bytes(large)
- large_pane = look(remote, '/n/peer/os' + str(fixture), source=control)
- assert remote.read(f'/pane/{large_pane}/body') == large
- edited = b'QUIC file saved\n'
- remote.write(f'/pane/{large_pane}/body', edited, truncate=True)
- remote.write(f'/pane/{large_pane}/exec', b'Save\n')
- assert fixture.read_bytes() == edited
- for pane in [opened, screen_pane, large_pane]:
- remote.write(f'/pane/{pane}/exec', b'Del\n')
- execute(remote, control, 'Unmount peer')
- roots = look(remote, '/n', source=control)
- assert remote.read(f'/pane/{roots}/body') == b'/n/os/\n/n/self/\n'
- assert unix.read_fid(unix_body) == b'TCP to QUIC Get\n'
- assert tcp.read_fid(tcp_body) == b'TCP to QUIC Get\n'
- # Not checked: a session Looking at its OWN tree through
- # a QUIC mount ('Mount own quic!...'). QUIC is still
- # answered on the editor's thread (cloud9's QUIC adapter
- # is not std.Io based), so that Look waits on the loop
- # it is running in and opens nothing; features.txt lists
- # it under "Left as they are". Unix and TCP self-mounts
- # are covered by test/selfmount.py.
- unix.close(unix_body)
- tcp.close(tcp_body)
-
-
if __name__ == '__main__':
- if len(sys.argv) in [3, 4] and sys.argv[2] == '--discovery':
- discovery(str(Path(sys.argv[1]).resolve()), embedded=sys.argv[3:] == ['--embedded'])
- raise SystemExit(0)
- if len(sys.argv) not in [2, 3] or (len(sys.argv) == 3 and sys.argv[2] != '--quic'):
- raise SystemExit('usage: fs.py <pardes> [--quic | --discovery]')
- test(str(Path(sys.argv[1]).resolve()), quic=len(sys.argv) == 3)
+ raise SystemExit('fs.py is the helpers the developer tools share (session, new_pane, '
+ 'execute); its tests are Zig now: zig build behaviour-test process-test')
diff --git a/test/process.zig b/test/process.zig
index f797c063..fd423985 100644
--- a/test/process.zig
+++ b/test/process.zig
@@ -182,6 +182,206 @@ 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
+ /// 217, cloud9's least).
+ pub fn dialSized(gpa: std.mem.Allocator, path: []const u8, size: u32) !*Conn {
+ var addr: libc.sockaddr.un = .{ .path = undefined };
+ if (path.len >= addr.path.len) return error.NameTooLong;
+ @memset(&addr.path, 0);
+ @memcpy(addr.path[0..path.len], path);
+ return connectTo(gpa, libc.AF.UNIX, @ptrCast(&addr), @sizeOf(libc.sockaddr.un), size);
+ }
+
+ /// A connection over TCP to `host` (numeric, v4 or v6) at `port`.
+ pub fn dialTcp(gpa: std.mem.Allocator, host: [:0]const u8, port: u16) !*Conn {
+ if (std.mem.indexOfScalar(u8, host, ':') != null) {
+ var addr: libc.sockaddr.in6 = .{ .port = std.mem.nativeToBig(u16, port), .flowinfo = 0, .addr = undefined, .scope_id = 0 };
+ if (inet_pton(libc.AF.INET6, host, &addr.addr) != 1) return error.BadHost;
+ return connectTo(gpa, libc.AF.INET6, @ptrCast(&addr), @sizeOf(libc.sockaddr.in6), msize);
+ }
+ var addr: libc.sockaddr.in = .{ .port = std.mem.nativeToBig(u16, port), .addr = undefined };
+ if (inet_pton(libc.AF.INET, host, &addr.addr) != 1) return error.BadHost;
+ return connectTo(gpa, libc.AF.INET, @ptrCast(&addr), @sizeOf(libc.sockaddr.in), msize);
+ }
+
+ fn connectTo(gpa: std.mem.Allocator, family: c_uint, addr: *const libc.sockaddr, len: libc.socklen_t, size: u32) !*Conn {
+ const c = try gpa.create(Conn);
+ errdefer gpa.destroy(c);
+ c.* = .{ .fd = -1, .cl = undefined };
+ c.cl = c9.Client.init(.{ .in = &c.in, .out = &c.out });
+ c.fd = libc.socket(family, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0);
+ if (c.fd < 0) return error.NoSocket;
+ errdefer _ = libc.close(c.fd);
+ if (libc.connect(c.fd, addr, len) != 0) return error.ConnectFailed;
+ _ = try c.call(.{ .version = .{ .msize = size } });
+ _ = try c.call(.{ .attach = .{ .fid = 0, .uname = "test" } });
+ return c;
+ }
+
+ /// The frame size the server agreed to.
+ pub fn msizeAgreed(c: *const Conn) u32 {
+ return c.cl.msize;
+ }
+
+
+ /// 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));
+ }
+ 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)]);
+ }
+ }
+
+ /// The serials /index lists, in its order.
+ pub fn serials(c: *Conn, gpa: std.mem.Allocator) ![]u32 {
+ const index = try c.read(gpa, "/index");
+ defer gpa.free(index);
+ var out: std.ArrayList(u32) = .empty;
+ var rows = std.mem.tokenizeScalar(u8, index, '\n');
+ while (rows.next()) |row| try out.append(gpa, try std.fmt.parseInt(u32, row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse row.len], 10));
+ return out.toOwnedSlice(gpa);
+ }
+
+ /// 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();
+ 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 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;
+ };
+ if (opened == from) return error.NotOneLook;
+ // A column holds only so many panes, each its tag and two rows.
+ if (source == null) try c.remove(try std.fmt.bufPrint(&buf, "/pane/{d}", .{from}));
+ return opened;
+ }
+
+ /// Pane `n`'s `file` read whole.
+ pub fn pane(c: *Conn, gpa: std.mem.Allocator, n: u32, file: []const u8) ![]u8 {
+ var buf: [64]u8 = undefined;
+ return c.read(gpa, try std.fmt.bufPrint(&buf, "/pane/{d}/{s}", .{ n, file }));
+ }
+
+ /// A line written to pane `n`'s exec, a middle click on it.
+ pub fn exec(c: *Conn, n: u32, line: []const u8) !void {
+ var buf: [64]u8 = undefined;
+ try c.write(try std.fmt.bufPrint(&buf, "/pane/{d}/exec", .{n}), line);
+ }
+
+ /// An exec that fails: refused, its words holding `words`.
+ pub fn execRefused(c: *Conn, n: u32, line: []const u8, words: []const u8) !void {
+ if (c.exec(n, line)) |_| {
+ std.debug.print("{s}: taken\n", .{line});
+ return error.Taken;
+ } else |err| if (err != error.Refused) return err;
+ try check(has(c.said, words), "{s}: refused {s}", .{ line, c.said });
+ }
+
+ pub fn removePane(c: *Conn, n: u32) !void {
+ var buf: [64]u8 = undefined;
+ try c.remove(try std.fmt.bufPrint(&buf, "/pane/{d}", .{n}));
+ }
};
/// A running pardes's file tree, dialled by its pid or socket path.
@@ -276,6 +476,18 @@ pub const Files = struct {
pub fn at(f: *Files, comptime fmt: []const u8, args: anytype) []const u8 {
return std.fmt.allocPrint(f.arena.allocator(), fmt, args) catch @panic("out of memory");
}
+
+ // fsb:
+
+ /// The ink /screen draws at a cell, as its JSON (`{"rgb":[1,2,3]}`).
+ pub fn fg(f: *Files, col: usize, row: usize) ![]const u8 {
+ const a = f.arena.allocator();
+ const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try f.read("/screen"), .{});
+ const cols: usize = @intCast(json.object.get("cols").?.integer);
+ const cell = json.object.get("cells").?.array.items[row * cols + col];
+ const style = json.object.get("styles").?.array.items[@intCast(cell.array.items[1].integer)];
+ return std.json.Stringify.valueAlloc(a, style.object.get("fg").?, .{});
+ }
};
/// The files of the app a pty harness started.
@@ -476,9 +688,327 @@ pub const Detached = struct {
}
d.pid = 0;
}
+
+ // fsb:
+
+ /// Whether `rel` under the root is there (a dangling link is not).
+ pub fn exists(d: *const Detached, rel: []const u8) bool {
+ var buf: [4096]u8 = undefined;
+ const path = std.fmt.bufPrintSentinel(&buf, "{s}/{s}", .{ d.root(), rel }, 0) catch return false;
+ return libc.access(path, 0) == 0;
+ }
+
+ /// `rel` under the root, absolute, kept until deinit.
+ pub fn abs(d: *Detached, rel: []const u8) [:0]const u8 {
+ return std.fmt.allocPrintSentinel(d.files.arena.allocator(), "{s}/{s}", .{ d.root(), rel }, 0) catch @panic("out of memory");
+ }
+
+ /// Another session in this root, in a pty, as fs.py's tty sessions:
+ /// `pardes --tty --9p=<name> <args> <root>/<name>.txt` (holding
+ /// "initial\n"), back once its socket (`abs("pardes-9p-<name>.sock")`)
+ /// is there.
+ pub fn startTty(d: *Detached, exe: [:0]const u8, name: []const u8, args: []const [:0]const u8) !Child {
+ const a = d.files.arena.allocator();
+ try d.put(try std.fmt.allocPrint(a, "{s}.txt", .{name}), "initial\n");
+ var argv: std.ArrayList([:0]const u8) = .empty;
+ try argv.appendSlice(a, &.{ exe, "--tty", try std.fmt.allocPrintSentinel(a, "--9p={s}", .{name}, 0) });
+ try argv.appendSlice(a, args);
+ try argv.append(a, d.abs(try std.fmt.allocPrint(a, "{s}.txt", .{name})));
+ const env = try a.dupe([2][:0]const u8, &.{.{ "PARDES_DUMP", d.abs(try std.fmt.allocPrint(a, "{s}.dump.zon", .{name})) }});
+ var c = try Child.spawn(argv.items, .{ .root = d.root(), .env = env, .pty = .{ .log = d.abs(try std.fmt.allocPrint(a, "{s}.pty", .{name})) } });
+ errdefer c.deinit();
+ try c.waitPath(d.abs(try std.fmt.allocPrint(a, "pardes-9p-{s}.sock", .{name})), 10_000);
+ return c;
+ }
};
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);
+}
+
+// fsb:
+
+extern "c" fn readdir(dir: *libc.DIR) ?*libc.dirent;
+extern "c" fn ioctl(fd: c_int, request: c_ulong, ...) c_int;
+extern "c" fn openpty(master: *c_int, slave: *c_int, name: ?[*]u8, termp: ?*const anyopaque, winp: ?*const std.posix.winsize) c_int;
+extern "c" fn signal(sig: c_int, handler: usize) usize;
+extern "c" var environ: [*:null]?[*:0]u8;
+extern "c" fn inet_pton(family: c_uint, src: [*:0]const u8, dst: *anyopaque) c_int;
+extern "c" fn pipe2(fds: *[2]c_int, flags: c_int) c_int;
+// shortcut: Linux's O_CLOEXEC, TIOCSCTTY and TCGETS, the only host these
+// run on; take them from std for another.
+const o_cloexec: c_int = 0o2000000;
+
+/// A program run as a shell in `root` would run it: the caller's
+/// environment without a session's (PARDES_*, a PAGER), HOME and the
+/// runtime and config directories the root, no daemon (PARDES_DAEMON=off:
+/// the process is the session) and no language servers, unless `env` says
+/// otherwise. Its stderr is kept (`said`).
+pub const Child = struct {
+ pid: libc.pid_t,
+ err_fd: c_int,
+ status: ?c_int = null,
+ /// A pty's ends, when on one, and the process copying its output to
+ /// `Pty.log`.
+ master: c_int = -1,
+ slave: c_int = -1,
+ drain: libc.pid_t = 0,
+
+ pub const Pty = struct {
+ /// Where everything the program draws goes.
+ log: [:0]const u8,
+ /// Made its controlling terminal; else a pty that is no one's.
+ ctty: bool = true,
+ };
+
+ pub const Options = struct {
+ /// HOME and the runtime and config directories; null: the caller's.
+ root: ?[]const u8 = null,
+ /// Where it runs; null: `root`, or here.
+ cwd: ?[]const u8 = null,
+ /// Set after the root's.
+ env: []const [2][:0]const u8 = &.{},
+ /// Unset after that.
+ unset: []const [:0]const u8 = &.{},
+ /// Its stdin, written whole and closed; null: /dev/null.
+ stdin: ?[]const u8 = null,
+ /// Started in a 24x80 pty, its stdin and stdout.
+ pty: ?Pty = null,
+ };
+
+ pub fn spawn(argv: []const [:0]const u8, opts: Options) !Child {
+ // A child gone before its stdin is written is EPIPE, not our end.
+ _ = signal(@intCast(@intFromEnum(libc.SIG.PIPE)), 1);
+ var args: [32:null]?[*:0]const u8 = @splat(null);
+ if (argv.len > args.len) return error.TooManyArgs;
+ for (argv, 0..) |a, i| args[i] = a;
+ var bufs: [4][4096]u8 = undefined;
+ // The build hands the binary over by a relative path, and the child
+ // runs elsewhere.
+ if (std.mem.indexOfScalar(u8, argv[0], '/') != null) args[0] = realpath(argv[0], &bufs[3]) orelse return error.NoBinary;
+ const root_z: ?[:0]const u8 = if (opts.root) |r| try std.fmt.bufPrintSentinel(&bufs[0], "{s}", .{r}, 0) else null;
+ const config = if (opts.root) |r| try std.fmt.bufPrintSentinel(&bufs[1], "{s}/config", .{r}, 0) else "";
+ const cwd: ?[:0]const u8 = if (opts.cwd) |c| try std.fmt.bufPrintSentinel(&bufs[2], "{s}", .{c}, 0) else root_z;
+ // Close-on-exec, so a later child holds none of these.
+ var err_pipe: [2]c_int = undefined;
+ if (pipe2(&err_pipe, o_cloexec) != 0) return error.NoPipe;
+ var in_pipe: [2]c_int = .{ -1, -1 };
+ if (opts.stdin != null and pipe2(&in_pipe, o_cloexec) != 0) return error.NoPipe;
+ var c: Child = .{ .pid = 0, .err_fd = err_pipe[0] };
+ if (opts.pty != null) {
+ var ws: std.posix.winsize = .{ .row = 24, .col = 80, .xpixel = 0, .ypixel = 0 };
+ if (openpty(&c.master, &c.slave, null, null, &ws) != 0) return error.NoPty;
+ for ([_]c_int{ c.master, c.slave }) |fd| _ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1)); // FD_CLOEXEC
+ }
+ const pid = libc.fork();
+ if (pid < 0) return error.ForkFailed;
+ if (pid == 0) {
+ var i: usize = 0;
+ while (environ[i]) |kv| {
+ const entry = std.mem.span(kv);
+ const eq = std.mem.indexOfScalar(u8, entry, '=') orelse entry.len;
+ if (std.mem.startsWith(u8, entry, "PARDES_") or std.mem.indexOf(u8, entry[0..eq], "PAGER") != null) {
+ var name: [256]u8 = undefined;
+ const n = std.fmt.bufPrintSentinel(&name, "{s}", .{entry[0..@min(eq, 255)]}, 0) catch _exit(126);
+ _ = unsetenv(n);
+ i = 0;
+ } else i += 1;
+ }
+ if (root_z) |r| for ([_][2][*:0]const u8{
+ .{ "HOME", r }, .{ "XDG_RUNTIME_DIR", r }, .{ "XDG_CONFIG_HOME", config }, .{ "PARDES_NOTIME", "1" },
+ }) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126);
+ for ([_][2][*:0]const u8{
+ .{ "PARDES_DAEMON", "off" }, .{ "PARDES_LSP_RS", "" }, .{ "PARDES_LSP_C", "" }, .{ "PARDES_LSP_GO", "" },
+ .{ "PARDES_LSP_TS", "" }, .{ "PARDES_LSP_PY", "" },
+ }) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126);
+ if (opts.pty != null and setenv("TERM", "xterm-256color", 1) != 0) _exit(126);
+ for (opts.env) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126);
+ for (opts.unset) |name| _ = unsetenv(name);
+ if (cwd) |d| if (libc.chdir(d) != 0) _exit(126);
+ _ = libc.setsid();
+ if (opts.pty) |p| {
+ if (p.ctty) _ = ioctl(c.slave, 0x540E, @as(c_int, 0));
+ _ = libc.dup2(c.slave, 0);
+ _ = libc.dup2(c.slave, 1);
+ } else {
+ const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR }, @as(libc.mode_t, 0));
+ _ = libc.dup2(if (opts.stdin != null) in_pipe[0] else null_fd, 0);
+ _ = libc.dup2(null_fd, 1);
+ }
+ _ = libc.dup2(err_pipe[1], 2);
+ _ = execvp(args[0].?, &args);
+ _exit(127);
+ }
+ c.pid = pid;
+ _ = libc.close(err_pipe[1]);
+ if (opts.stdin) |bytes| {
+ _ = libc.close(in_pipe[0]);
+ var off: usize = 0;
+ while (off < bytes.len) {
+ const n = libc.write(in_pipe[1], bytes[off..].ptr, @min(bytes.len - off, 1 << 20));
+ if (n <= 0) break;
+ off += @intCast(n);
+ }
+ _ = libc.close(in_pipe[1]);
+ }
+ if (opts.pty) |p| {
+ const drain = libc.fork();
+ if (drain == 0) {
+ const fd = libc.open(p.log, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644));
+ var buf: [16 * 1024]u8 = undefined;
+ while (true) {
+ const n = libc.read(c.master, &buf, buf.len);
+ if (n <= 0) _exit(0);
+ _ = libc.write(fd, &buf, @intCast(n));
+ }
+ }
+ c.drain = drain;
+ }
+ return c;
+ }
+
+ /// Its wait(2) word once it has exited, waiting up to `ms`; null while it runs.
+ pub fn wait(c: *Child, ms: i64) ?c_int {
+ if (c.status) |s| return s;
+ const deadline = eh.nowMs() + ms;
+ while (true) {
+ var status: c_int = 0;
+ if (libc.waitpid(c.pid, &status, std.c.W.NOHANG) == c.pid) {
+ c.status = status;
+ return status;
+ }
+ if (eh.nowMs() >= deadline) return null;
+ sleepMs(1);
+ }
+ }
+
+ /// Everything it wrote to stderr, read to its end (its exit, or every
+ /// process holding it gone).
+ pub fn said(c: *Child, gpa: std.mem.Allocator) ![]u8 {
+ var out: std.ArrayList(u8) = .empty;
+ var buf: [4096]u8 = undefined;
+ while (true) {
+ const n = libc.read(c.err_fd, &buf, buf.len);
+ if (n <= 0) break;
+ try out.appendSlice(gpa, buf[0..@intCast(n)]);
+ }
+ return out.toOwnedSlice(gpa);
+ }
+
+ /// Waits up to `ms` for `path` (its socket) to be there; its stderr
+ /// said if it exits first.
+ pub fn waitPath(c: *Child, path: []const u8, ms: i64) !void {
+ var buf: [4096]u8 = undefined;
+ const z = try std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0);
+ const deadline = eh.nowMs() + ms;
+ while (libc.access(z, 0) != 0) {
+ if (c.wait(0)) |status| {
+ const words = c.said(std.heap.page_allocator) catch "";
+ std.debug.print("exited, status {d}, before {s}: {s}\n", .{ status, path, words });
+ return error.Exited;
+ }
+ if (eh.nowMs() > deadline) return error.Timeout;
+ sleepMs(5);
+ }
+ }
+
+ pub fn kill(c: *const Child, sig: libc.SIG) void {
+ if (c.status == null) _ = libc.kill(c.pid, sig);
+ }
+
+ /// Ended (SIGTERM, SIGKILL after 5 s) and its fds closed.
+ pub fn deinit(c: *Child) void {
+ if (c.wait(0) == null) {
+ _ = libc.kill(c.pid, libc.SIG.TERM);
+ if (c.wait(5_000) == null) {
+ _ = libc.kill(c.pid, libc.SIG.KILL);
+ _ = c.wait(5_000);
+ }
+ }
+ if (c.drain > 0) {
+ _ = libc.kill(c.drain, libc.SIG.KILL);
+ var st: c_int = 0;
+ _ = libc.waitpid(c.drain, &st, 0);
+ }
+ for ([_]c_int{ c.err_fd, c.master, c.slave }) |fd| if (fd >= 0) {
+ _ = libc.close(fd);
+ };
+ }
+};
+
+/// What a run ended with: its exit code (a signal's is minus it, as
+/// Python's returncode) and its stderr.
+pub const Ran = struct { code: c_int, said: []u8 };
+
+/// `argv` run to its end.
+pub fn run(gpa: std.mem.Allocator, argv: []const [:0]const u8, opts: Child.Options) !Ran {
+ var c = try Child.spawn(argv, opts);
+ defer c.deinit();
+ const words = try c.said(gpa);
+ var status: c_int = 0;
+ _ = libc.waitpid(c.pid, &status, 0);
+ c.status = status;
+ return .{ .code = code(status), .said = words };
+}
+
+/// A wait(2) word as an exit code, a signal's as minus it.
+pub fn code(status: c_int) c_int {
+ const sig = status & 0x7f;
+ return if (sig != 0) -sig else (status >> 8) & 0xff;
+}
+
+/// The names in directory `dir`, kept in `gpa`.
+pub fn entries(gpa: std.mem.Allocator, dir: []const u8) ![]const []const u8 {
+ var buf: [4096]u8 = undefined;
+ const d = libc.opendir(try std.fmt.bufPrintSentinel(&buf, "{s}", .{dir}, 0)) orelse return error.NoDir;
+ defer _ = libc.closedir(d);
+ var names: std.ArrayList([]const u8) = .empty;
+ while (readdir(d)) |e| {
+ const name = std.mem.sliceTo(&e.name, 0);
+ if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) continue;
+ try names.append(gpa, try gpa.dupe(u8, name));
+ }
+ return names.toOwnedSlice(gpa);
+}
+
+/// Whether the line discipline of the terminal at `fd` is canonical.
+pub fn canonical(fd: c_int) !bool {
+ var t: std.os.linux.termios = undefined;
+ if (ioctl(fd, 0x5401, &t) != 0) return error.NoTermios; // TCGETS
+ return t.lflag.ICANON;
+}
+
+/// Monotonic milliseconds.
+pub const nowMs = eh.nowMs;
+
+pub fn nap(ms: u32) void {
+ sleepMs(ms);
+}
+
+pub fn has(hay: []const u8, needle: []const u8) bool {
+ return std.mem.indexOf(u8, hay, needle) != null;
+}
+
+/// `ok`, or what is wrong said and the test failed.
+pub fn check(ok: bool, comptime what: []const u8, args: anytype) !void {
+ if (ok) return;
+ std.debug.print(what ++ "\n", args);
+ return error.TestUnexpectedResult;
+}
+
+/// Whether `exe` was built with QUIC (-Dquic=true): refused for that, or
+/// for --attach's own reason, which comes after.
+pub fn quicIncluded(gpa: std.mem.Allocator, exe: [:0]const u8) !bool {
+ const r = try run(gpa, &.{ exe, "--attach=work", "--9p-quic=quic!127.0.0.1!0" }, .{});
+ defer gpa.free(r.said);
+ return !has(r.said, "QUIC is not included");
+}
diff --git a/test/process/builtins.zig b/test/process/builtins.zig
new file mode 100644
index 00000000..410a3f54
--- /dev/null
+++ b/test/process/builtins.zig
@@ -0,0 +1,52 @@
+//! docs/typ/reference.typ, Builtins <builtins> a real session runs, detached
+//! and in a pty.
+const std = @import("std");
+const process = @import("../process.zig");
+const Conn = process.Conn;
+const has = process.has;
+const check = process.check;
+
+pub const cases = .{
+ .{ "builtins: Lspinfo opens one +Lsp pane saying the backend and its servers, detached and in a pty", lspinfo },
+};
+
+fn lspinfo(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) "lsp-tty" else "lsp-detached";
+ var d = try process.Detached.start(gpa, exe, if (tty) "lsp-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 c = try Conn.dial(gpa, address);
+ defer c.deinit(gpa);
+ const control = try c.newPane();
+ try c.exec(control, "Lspinfo\n");
+ const deadline = process.nowMs() + 3_000;
+ const lsp = while (true) {
+ var found: ?u32 = null;
+ var count: usize = 0;
+ var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
+ while (rows.next()) |row| if (has(row, "+Lsp")) {
+ count += 1;
+ found = try std.fmt.parseInt(u32, row[0..std.mem.indexOfScalar(u8, row, ' ').?], 10);
+ };
+ if (found) |n| {
+ try check(count == 1, "{s}: {d} +Lsp panes", .{ name, count });
+ break n;
+ }
+ if (process.nowMs() > deadline) return error.LspinfoNeverDone;
+ process.nap(5);
+ };
+ const body = try c.pane(a, lsp, "body");
+ try check(std.mem.startsWith(u8, body, "backend: lsp-client\n") and has(body, "protocol servers (lsp-client):"), "{s}", .{body});
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/body"));
+ var f: process.Files = .init(gpa, address);
+ defer f.deinit();
+ try check(has(try f.screen(), "+Lsp"), "+Lsp not drawn", .{});
+ }
+}
diff --git a/test/process/edit.zig b/test/process/edit.zig
new file mode 100644
index 00000000..30477552
--- /dev/null
+++ b/test/process/edit.zig
@@ -0,0 +1,139 @@
+//! docs/typ/reference.typ, Edit <edit>, through a real session's socket:
+//! its commands run in the session's shell, and its w writes.
+const std = @import("std");
+const process = @import("../process.zig");
+const eh = @import("../e2e_harness.zig");
+const expect = std.testing.expect;
+const expectEqual = std.testing.expectEqual;
+const expectEqualStrings = std.testing.expectEqualStrings;
+
+pub const cases = .{
+ .{ "edit: < | > run off the loop and answer their write once applied, a failing one changing nothing; a held Edit write is flushed; X edits every open file it matches, w writes", editCommands },
+};
+
+fn has(hay: []const u8, needle: []const u8) bool {
+ return std.mem.indexOf(u8, hay, needle) != null;
+}
+
+fn refusedSaying(c: *process.Conn, path: []const u8, data: []const u8, why: []const u8) !void {
+ try std.testing.expectError(error.Refused, c.write(path, data));
+ if (!has(c.said, why)) {
+ std.debug.print("{s} refused saying {s}, not {s}\n", .{ path, c.said, why });
+ return error.WrongWords;
+ }
+}
+
+/// An Edit written on another connection, in a thread.
+const Slow = struct {
+ conn: *process.Conn,
+ path: []const u8,
+ done: std.atomic.Value(bool) = .init(false),
+
+ fn go(s: *Slow) void {
+ s.conn.write(s.path, "Edit , | sleep 1; cat\n") catch {};
+ s.done.store(true, .release);
+ }
+};
+
+fn editCommands(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "edit", .{});
+ 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);
+ const root = d.root();
+ try d.put("a.txt", "pear\napple\nfig\n");
+ try d.put("b.txt", "apple pie\n");
+ const a = try c.look(ar, f.at("{s}/a.txt", .{root}), null);
+ const b = try c.look(ar, f.at("{s}/b.txt", .{root}), null);
+ const actl = f.at("/pane/{d}/ctl", .{a});
+ const abody = f.at("/pane/{d}/body", .{a});
+ // The write returns with the text sorted, through the session's shell
+ // in the file's directory.
+ try c.write(actl, "Edit , | sort\n");
+ try expectEqualStrings("apple\nfig\npear\n", try c.read(ar, abody));
+ try c.write(actl, "Edit $ < pwd\n");
+ try expectEqualStrings(f.at("apple\nfig\npear\n{s}\n", .{root}), try c.read(ar, abody));
+ try c.write(actl, "Undo\n");
+ try expectEqualStrings("apple\nfig\npear\n", try c.read(ar, abody));
+ // A per-match command, one undo step.
+ try c.write(actl, "Edit ,x/[a-z]+/ | tr a-z A-Z\n");
+ try expectEqualStrings("APPLE\nFIG\nPEAR\n", try c.read(ar, abody));
+ // A failing command: nothing changed, the write fails in words, its
+ // stderr in +Errors.
+ try refusedSaying(c, actl, "Edit , | echo nope >&2; exit 4\n", "exit status 4");
+ try expectEqualStrings("APPLE\nFIG\nPEAR\n", try c.read(ar, abody));
+ var said = false;
+ var rows = std.mem.tokenizeScalar(u8, try c.read(ar, "/index"), '\n');
+ while (rows.next()) |row| {
+ if (!has(row, "+Errors")) continue;
+ said = said or has(try c.read(ar, f.at("/pane/{s}/body", .{row[0..std.mem.indexOfScalar(u8, row, ' ').?]})), "nope");
+ }
+ try expect(said);
+ // While a slow command runs, the editor answers.
+ {
+ const other = try process.Conn.dial(gpa, d.socket());
+ defer other.deinit(gpa);
+ var slow: Slow = .{ .conn = other, .path = f.at("/pane/{d}/ctl", .{b}) };
+ const worker = try std.Thread.spawn(.{}, Slow.go, .{&slow});
+ process.sleep(200);
+ const began = eh.nowMs();
+ _ = try c.read(ar, "/index");
+ const quick = eh.nowMs() - began < 500 and !slow.done.load(.acquire);
+ worker.join();
+ try expect(quick); // the editor did not wait on the command
+ try expect(slow.done.load(.acquire));
+ }
+ // On the same connection: the Edit's write is held, and a read sent
+ // after it is answered first; a Tflush of it stops it, changes nothing,
+ // and frees the way for the next Edit.
+ const ctl = try c.hold(actl, 1);
+ const status = try c.hold("/status", 0);
+ const began = eh.nowMs();
+ const held = try c.send(.{ .write = .{ .fid = ctl, .offset = 0, .data = "Edit , | sleep 1; cat\n" } });
+ const read = try c.send(.{ .read = .{ .fid = status, .offset = 0, .count = 4096 } });
+ const first = try c.answer();
+ try expect(first.tag == read and first.result == .read and eh.nowMs() - began < 500);
+ const second = try c.answer();
+ try expect(second.tag == held and second.result == .write);
+ const flushed = try c.send(.{ .write = .{ .fid = ctl, .offset = 0, .data = "Edit , | sleep 5; tr a-z A-Z\n" } });
+ process.sleep(300);
+ const flush = try c.send(.{ .flush = .{ .oldtag = flushed } });
+ const one = (try c.answer()).tag;
+ const two = (try c.answer()).tag;
+ try expect((one == flushed and two == flush) or (one == flush and two == flushed));
+ c.clunk(ctl);
+ c.clunk(status);
+ process.sleep(1000);
+ try c.write(actl, "Edit , | cat\n");
+ process.sleep(4500);
+ try expectEqualStrings("APPLE\nFIG\nPEAR\n", try c.read(ar, abody));
+ // X: each open file whose line matches; w writes it.
+ try c.write(actl, "Edit X/b\\.txt$/ ,s/apple/APPLE/\n");
+ try expectEqualStrings("APPLE pie\n", try c.read(ar, f.at("/pane/{d}/body", .{b})));
+ try c.write(f.at("/pane/{d}/ctl", .{b}), "Edit w\n");
+ try expectEqualStrings("APPLE pie\n", try d.get(ar, "b.txt"));
+ try c.write(actl, "Edit 1 w first.txt\n");
+ try expectEqualStrings("APPLE\n", try d.get(ar, "first.txt"));
+ // A file changed on disk under unsaved edits: Edit's w refuses once, as
+ // Save does, and the next w overwrites it.
+ try d.put("w1.txt", "one\n");
+ const w = try c.look(ar, f.at("{s}/w1.txt", .{root}), null);
+ const wctl = f.at("/pane/{d}/ctl", .{w});
+ try c.write(wctl, "Edit $a/two/\n");
+ const changed = f.at("\nchanged {d}", .{w});
+ const seen = std.mem.count(u8, try c.read(ar, "/log"), changed);
+ try eh.appendFile(try std.fmt.allocPrintSentinel(ar, "{s}/w1.txt", .{root}, 0), "external\n");
+ const deadline = eh.nowMs() + 10_000;
+ while (std.mem.count(u8, try c.read(ar, "/log"), changed) == seen) {
+ if (eh.nowMs() > deadline) return error.NeverHeard;
+ process.sleep(50);
+ }
+ try refusedSaying(c, wctl, "Edit w\n", "modified on disk since read (w again to overwrite)");
+ try expectEqualStrings("one\nexternal\n", try d.get(ar, "w1.txt"));
+ try c.write(wctl, "Edit w\n");
+ try expectEqualStrings("one\ntwo", try d.get(ar, "w1.txt"));
+}
diff --git a/test/process/flags.zig b/test/process/flags.zig
new file mode 100644
index 00000000..fffc2867
--- /dev/null
+++ b/test/process/flags.zig
@@ -0,0 +1,780 @@
+//! docs/typ/reference.typ, Flags <flags>: a real pardes launched with
+//! them, inside a session and out (test/fs.py's inline checks).
+const std = @import("std");
+const libc = std.c;
+const process = @import("../process.zig");
+const Conn = process.Conn;
+const Child = process.Child;
+
+pub const cases = .{
+ .{ "flags: a bad option, socket name, dial or mount is refused in words, exit 1", refusals },
+ .{ "flags: --9p=<name> names the socket, not --detach's name", explicitName },
+ .{ "flags: a launch inside a session forwards its file there, refuses what it cannot, and one with flags of a session's own starts its own", forwarding },
+ .{ "flags: --wait lasts until its pane goes; a forwarded name is opened, made or refused in words; pardes - pages stdin into a +Pager", waitAndPager },
+ .{ "flags: a command from a pane's tag gets $winid, one from the workspace's tag none", winid },
+ .{ "flags: pardes - outside a session boots on stdin's +Pager; a launch with no terminal to draw on says so", outside },
+ .{ "flags: a tty editor killed by SIGTERM or SIGHUP puts its terminal back and its socket goes; a dead editor's socket is swept", killedTty },
+ .{ "flags: a detached session told to stop by a signal unlinks its socket; a hangup does not stop it", signalCleansUp },
+};
+
+extern "c" fn realpath(path: [*:0]const u8, resolved: [*]u8) ?[*:0]const u8;
+
+const long_name: [:0]const u8 = blk: {
+ var name: [304:0]u8 = @splat('x');
+ @memcpy(name[300..], ".txt");
+ const done = name;
+ break :blk &done;
+};
+
+const has = process.has;
+const check = process.check;
+
+fn z(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) [:0]const u8 {
+ return std.fmt.allocPrintSentinel(a, fmt, args, 0) catch @panic("out of memory");
+}
+
+fn contains(set: []const u32, n: u32) bool {
+ return std.mem.indexOfScalar(u32, set, n) != null;
+}
+
+/// The serials in `after` not in `before`.
+fn added(a: std.mem.Allocator, before: []const u32, after: []const u32) []const u32 {
+ var out: std.ArrayList(u32) = .empty;
+ for (after) |n| if (!contains(before, n)) out.append(a, n) catch @panic("out of memory");
+ return out.items;
+}
+
+/// The one pane made since `before`.
+fn madeOne(a: std.mem.Allocator, c: *Conn, before: []const u32) !u32 {
+ const new = added(a, before, try c.serials(a));
+ try check(new.len == 1, "made {any}, not one pane", .{new});
+ return new[0];
+}
+
+fn same(a: []const u32, b: []const u32) bool {
+ return std.mem.eql(u32, a, b);
+}
+
+/// The session's pid, from /status.
+fn statusPid(a: std.mem.Allocator, c: *Conn) ![:0]const u8 {
+ var lines = std.mem.tokenizeScalar(u8, try c.read(a, "/status"), '\n');
+ while (lines.next()) |line| if (std.mem.startsWith(u8, line, "pid ")) return z(a, "{s}", .{line[4..]});
+ return error.NoPid;
+}
+
+/// Waits up to `ms` for the session's log to hold `text`.
+fn waitLog(a: std.mem.Allocator, c: *Conn, text: []const u8, ms: i64) !void {
+ const deadline = process.nowMs() + ms;
+ while (!has(try c.read(a, "/log"), text)) {
+ if (process.nowMs() > deadline) {
+ std.debug.print("not in the log: {s}\n", .{text});
+ return error.Timeout;
+ }
+ process.nap(20);
+ }
+}
+
+fn isSocketName(name: []const u8) bool {
+ return std.mem.startsWith(u8, name, "pardes-9p-") and std.mem.endsWith(u8, name, ".sock");
+}
+
+/// The socket a launched session made in `root`, none of `existing`: one
+/// named by its pid, as fs.py's glob found it.
+fn newSocket(a: std.mem.Allocator, root: []const u8, existing: []const []const u8, child: *Child) ![]const u8 {
+ const deadline = process.nowMs() + 10_000;
+ while (true) {
+ for (try process.entries(a, root)) |name| {
+ if (!isSocketName(name)) continue;
+ for (existing) |old| {
+ if (std.mem.eql(u8, old, name)) break;
+ } else return std.fmt.allocPrint(a, "{s}/{s}", .{ root, name });
+ }
+ if (child.wait(0)) |status| {
+ std.debug.print("exited {d} before its socket: {s}\n", .{ status, child.said(a) catch "" });
+ return error.Exited;
+ }
+ if (process.nowMs() > deadline) return error.NoSocket;
+ process.nap(5);
+ }
+}
+
+fn refusals(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const Row = struct { []const [:0]const u8, []const u8 };
+ for ([_]Row{
+ .{ &.{"--fs"}, "no such option" },
+ .{ &.{"--fs9"}, "no such option" },
+ .{ &.{ "--", "a", "b" }, "is the second" },
+ .{ &.{"--9p"}, "--9p needs a socket name" },
+ .{ &.{"--9p="}, "invalid 9P socket name" },
+ .{ &.{"--9p=a/b"}, "invalid 9P socket name" },
+ .{ &.{ "--attach=work", "--9p=work" }, "--attach has none" },
+ .{ &.{"--9p-tcp"}, "--9p-tcp needs" },
+ .{ &.{"--9p-tcp="}, "--9p-tcp needs" },
+ .{ &.{"--9p-tcp=tcp!localhost!5640"}, "--9p-tcp needs" },
+ .{ &.{"--9p-tcp=quic!127.0.0.1!5640"}, "--9p-tcp needs" },
+ .{ &.{ "--9p-tcp=tcp!127.0.0.1!5640", "--9p-tcp=tcp!127.0.0.1!5641" }, "specified twice" },
+ .{ &.{ "--attach=work", "--9p-tcp=tcp!127.0.0.1!5640" }, "--attach has none" },
+ .{ &.{"--9p-quic"}, "--9p-quic needs" },
+ .{ &.{"--9p-quic="}, "--9p-quic needs" },
+ .{ &.{"--9p-quic=tcp!127.0.0.1!5640"}, "--9p-quic needs" },
+ .{ &.{"--mount=os=work"}, "invalid mount name" },
+ .{ &.{"--mount=peer="}, "invalid mount name or dial" },
+ .{ &.{ "--mount=peer=one", "--mount=peer=two" }, "duplicate mount" },
+ .{ &.{"--mount=peer=tcp!127.0.0.1!0"}, "invalid mount dial" },
+ .{ &.{ "--attach=work", "--mount=peer=one" }, "--attach has none" },
+ }) |row| {
+ var argv: [4][:0]const u8 = undefined;
+ argv[0] = exe;
+ for (row[0], 1..) |arg, i| argv[i] = arg;
+ const r = try process.run(a, argv[0 .. 1 + row[0].len], .{});
+ try check(r.code == 1 and has(r.said, row[1]), "{any}: exit {d}: {s}", .{ row[0], r.code, r.said });
+ }
+}
+
+fn explicitName(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "renamed", .{ .args = &.{"--9p=explicit"}, .socket = "explicit" });
+ defer d.deinit();
+ try std.testing.expect(d.exists("pardes-9p-explicit.sock"));
+ try std.testing.expect(!d.exists("pardes-9p-renamed.sock"));
+ try d.files.expect("/pane/1/body", "initial\n");
+}
+
+fn forwarding(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "forwarding", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const address = d.abs("pardes-9p-forwarding.sock");
+ _ = libc.mkdir(d.abs("child-working-directory"), 0o755);
+ const child_dir = d.abs("child-working-directory");
+ const spaced_text = "first line\nsecond line\n";
+ try d.put("child-working-directory/space name.txt", spaced_text);
+ const spaced = d.abs("child-working-directory/space name.txt");
+ // PARDES_PID says "you are inside a pardes"; the socket and the pane
+ // only say how to reach it.
+ const pid = try statusPid(a, c);
+ const inherited = [_][2][:0]const u8{ .{ "PARDES_9P", address }, .{ "PARDES_PANE", "1" }, .{ "PARDES_PID", pid } };
+ const opts: Child.Options = .{ .root = d.root(), .cwd = child_dir, .env = &inherited };
+ var opened: u32 = 0;
+ const Word = struct { [:0]const u8, []const u8, ?[2]u32, bool };
+ for ([_]Word{
+ .{ "space name.txt:2:4", spaced_text, .{ 14, 14 }, false },
+ .{ "/n/self/pane/1/body", "initial\n", null, false },
+ .{ "/virtual/pane/1/body:1:2-4", "initial\n", .{ 1, 4 }, true },
+ }) |row| {
+ const before = try c.serials(a);
+ const r = try process.run(a, &.{ exe, "--tty", row[0] }, opts);
+ try check(r.code == 0, "{s}: exit {d}: {s}", .{ row[0], r.code, r.said });
+ if (row[3]) {
+ try check(same(before, try c.serials(a)), "{s} made a pane", .{row[0]});
+ } else opened = try madeOne(a, c, before);
+ try std.testing.expectEqualStrings(row[1], try c.pane(a, opened, "body"));
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/body"));
+ // The selection is dot; reading it takes no command at all.
+ if (row[2]) |sel| {
+ var it = std.mem.tokenizeScalar(u8, try c.pane(a, opened, "dot"), ' ');
+ const got = [2]u32{ try std.fmt.parseInt(u32, it.next().?, 10), try std.fmt.parseInt(u32, std.mem.trim(u8, it.next().?, " \n"), 10) };
+ try check(std.mem.eql(u32, &got, &sel), "{s}: dot {any}, not {any}", .{ row[0], got, sel });
+ }
+ }
+ // A launch with no file inside a session is refused, and never clears
+ // the address limit its parent's pane holds.
+ const addr = try c.hold("/pane/1/addr", 2);
+ const limit = try c.hold("/pane/1/limit", 2);
+ _ = try c.writeFid(addr, 0, "#0,#3");
+ _ = try c.writeFid(limit, 0, "#0,#3");
+ _ = try c.writeFid(addr, 0, "#0");
+ for (0..8) |_| {
+ const r = try process.run(a, &.{exe}, opts);
+ try check(r.code == 1 and has(r.said, "already inside pardes"), "no-argument child: exit {d}: {s}", .{ r.code, r.said });
+ if (c.writeFid(addr, 0, "/tial/")) |_| {
+ return error.LimitCleared;
+ } else |_| {}
+ }
+ c.clunk(addr);
+ c.clunk(limit);
+ _ = try c.read(a, "/pane/1/ctl");
+
+ const control = try c.newPane();
+ try c.exec(control, "Dump\n");
+ const deadline = process.nowMs() + 3_000;
+ while (!d.exists("forwarding.dump.zon")) {
+ if (process.nowMs() > deadline) return error.NoDump;
+ process.nap(5);
+ }
+ const saved = d.abs("forwarding.dump.zon");
+ const before = try c.read(a, "/index");
+ // Flags of a session's own start one, never forwarded.
+ const Launch = struct { []const u8, []const [:0]const u8, ?[]const u8 };
+ for ([_]Launch{
+ .{ "mount", &.{ "--tty", z(a, "--mount=peer={s}", .{address}), "/n/peer/pane/1/body" }, null },
+ .{ "name", &.{ "--tty", "--9p=forwarded-name" }, "forwarded-name" },
+ .{ "tcp", &.{ "--tty", "--9p-tcp=tcp!127.0.0.1!0" }, null },
+ .{ "load", &.{ "--tty", "-l", saved }, null },
+ .{ "shells", &.{ "-n", "3" }, null },
+ .{ "toggle", &.{ "--tty", "--tty-toggle=q" }, null },
+ .{ "toggle-arg", &.{ "--tty", "--tty-toggle", "q" }, null },
+ .{ "quic", &.{ "--tty", "--9p-quic=quic!127.0.0.1!0" }, null },
+ }) |row| {
+ if (std.mem.eql(u8, row[0], "quic") and !try process.quicIncluded(a, exe)) continue;
+ var argv: [8][:0]const u8 = undefined;
+ argv[0] = exe;
+ for (row[1], 1..) |arg, i| argv[i] = arg;
+ const existing = try process.entries(a, d.root());
+ var child = try Child.spawn(argv[0 .. 1 + row[1].len], .{ .root = d.root(), .env = &inherited, .pty = .{ .log = d.abs(try std.fmt.allocPrint(a, "forward-{s}.pty", .{row[0]})) } });
+ defer child.deinit();
+ const sock = if (row[2]) |name| blk: {
+ const p = try std.fmt.allocPrint(a, "{s}/pardes-9p-{s}.sock", .{ d.root(), name });
+ try child.waitPath(p, 10_000);
+ break :blk p;
+ } else try newSocket(a, d.root(), existing, &child);
+ try check(!std.mem.eql(u8, sock, address), "{s} forwarded", .{row[0]});
+ var local: process.Files = .init(gpa, sock);
+ defer local.deinit();
+ const index = try local.read("/index");
+ const rows = std.mem.count(u8, index, "\n");
+ if (std.mem.eql(u8, row[0], "mount")) {
+ try check(rows == 1 and has(index, "/n/peer/"), "mount: {s}", .{index});
+ try local.expect("/pane/1/body", "initial\n");
+ } else if (std.mem.eql(u8, row[0], "load")) {
+ try std.testing.expectEqualStrings(before, index);
+ try local.expect("/pane/1/body", "initial\n");
+ } else {
+ // A bare tty launch is a shell plus the empty text pane the
+ // boot puts under it; --shells=3 is three shells, no scratch.
+ try check(rows == @as(usize, if (std.mem.eql(u8, row[0], "shells")) 3 else 2), "{s}: {s}", .{ row[0], index });
+ }
+ if (std.mem.eql(u8, row[0], "tcp") or std.mem.eql(u8, row[0], "quic"))
+ try check(has(try local.read("/listeners"), try std.fmt.allocPrint(a, "{s}!127.0.0.1!", .{row[0]})), "{s}: no listener", .{row[0]});
+ try std.testing.expectEqualStrings(before, try c.read(a, "/index"));
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/body"));
+ }
+
+ // A session that answers takes the file or says why, as acme's B does:
+ // a pane it has not is its refusal and exit 1, never a nested editor.
+ const gone = try process.run(a, &.{ exe, "--tty", spaced }, .{ .root = d.root(), .env = &.{ inherited[0], .{ "PARDES_PANE", "4294967295" }, inherited[2] } });
+ try check(gone.code == 1 and std.mem.startsWith(u8, gone.said, "pardes: "), "gone pane: exit {d}: {s}", .{ gone.code, gone.said });
+ try std.testing.expectEqualStrings(before, try c.read(a, "/index"));
+ const absent = d.abs("absent.sock");
+ const stale = [_][2][:0]const u8{ .{ "PARDES_9P", absent }, inherited[1], inherited[2] };
+ const Stale = struct { []const u8, []const [2][:0]const u8, ?[:0]const u8 };
+ for ([_]Stale{
+ .{ "stale-file", &stale, spaced },
+ .{ "stale-missing", &stale, "missing-child-file.txt" },
+ .{ "stale-noarg", &stale, null },
+ // No pid is "not inside a pardes at all", as a plain shell has.
+ .{ "not-nested", inherited[0..2], spaced },
+ }) |row| {
+ const existing = try process.entries(a, d.root());
+ var argv: [3][:0]const u8 = .{ exe, "--tty", undefined };
+ if (row[2]) |w| argv[2] = w;
+ var child = try Child.spawn(argv[0..if (row[2] != null) 3 else 2], .{ .root = d.root(), .env = row[1], .pty = .{ .log = d.abs(try std.fmt.allocPrint(a, "{s}.pty", .{row[0]})) } });
+ defer child.deinit();
+ const sock = try newSocket(a, d.root(), existing, &child);
+ try check(!std.mem.eql(u8, sock, address), "{s} forwarded", .{row[0]});
+ var local: process.Files = .init(gpa, sock);
+ defer local.deinit();
+ if (row[2]) |w| {
+ if (std.mem.eql(u8, w, spaced)) {
+ try local.expect("/pane/1/body", spaced_text);
+ } else try check(has(try local.read("/index"), w) or has(try local.screen(), w), "{s}: {s} not shown", .{ row[0], w });
+ }
+ try std.testing.expectEqualStrings(before, try c.read(a, "/index"));
+ }
+}
+
+/// `pardes <flag> <file>` started, back once it has made a pane or exited.
+fn launch(a: std.mem.Allocator, c: *Conn, argv: []const [:0]const u8, opts: Child.Options) !Child {
+ const before = try c.serials(a);
+ var child = try Child.spawn(argv, opts);
+ const deadline = process.nowMs() + 3_000;
+ while (child.wait(0) == null and same(before, try c.serials(a)) and process.nowMs() < deadline) process.nap(10);
+ return child;
+}
+
+fn running(a: std.mem.Allocator, child: *Child) !void {
+ if (child.wait(0)) |status| {
+ std.debug.print("exited {d}: {s}\n", .{ status, child.said(a) catch "" });
+ return error.Exited;
+ }
+}
+
+fn exitsWith(child: *Child, want: c_int, ms: i64) !void {
+ const status = child.wait(ms) orelse return error.StillRunning;
+ try std.testing.expectEqual(want, process.code(status));
+}
+
+fn waitAndPager(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "wait", .{});
+ var ended = false;
+ defer if (!ended) d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const root = d.root();
+ // A second --detach of a name a live session holds: said, exit 1, no
+ // Zig error trace.
+ const second_detach = try process.run(a, &.{ exe, "--detach=wait" }, .{ .root = root });
+ try std.testing.expectEqual(@as(c_int, 1), second_detach.code);
+ try std.testing.expectEqualStrings("pardes: a session named wait is already running\n", second_detach.said);
+ const address = d.abs("pardes-9p-wait.sock");
+ const env = [_][2][:0]const u8{ .{ "PARDES_9P", address }, .{ "PARDES_PANE", "1" }, .{ "PARDES_PID", try statusPid(a, c) } };
+ const opts: Child.Options = .{ .root = root, .env = &env };
+ try d.put("wait edit.txt", "edit me\n");
+ const edited = d.abs("wait edit.txt");
+
+ // --wait (acme's E): a launch inside a pane lasts until the pane it
+ // opened is deleted; one on a file already open waits on that pane.
+ var first = try launch(a, c, &.{ exe, "--wait", edited }, opts);
+ defer first.deinit();
+ const opened = blk: {
+ const now = try c.serials(a);
+ try check(now.len == 2 and now[0] == 1, "--wait: {any}", .{now});
+ break :blk now[1];
+ };
+ var second = try launch(a, c, &.{ exe, "-w", edited }, opts);
+ defer second.deinit();
+ process.nap(500);
+ try running(a, &first);
+ try running(a, &second);
+ try check(same(try c.serials(a), &.{ 1, opened }), "-w made a pane", .{});
+ try c.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), "edited\n");
+ try c.exec(opened, "Save\n");
+ try c.removePane(opened);
+ const deleted = process.nowMs();
+ try exitsWith(&first, 0, 5_000);
+ // Woken by the `del` record, not a poll.
+ try check(process.nowMs() - deleted < 50, "--wait woke {d} ms after the del", .{process.nowMs() - deleted});
+ try exitsWith(&second, 0, 5_000);
+ try std.testing.expectEqualStrings("edited\n", try d.get(a, "wait edit.txt"));
+
+ // --wait from outside the session's panes, $PARDES_9P alone (an
+ // agent's EDITOR): asked of the session, not a screen of its own.
+ const outside_env = env[0..1];
+ try d.put("wait far.txt", "far\n");
+ const far = d.abs("wait far.txt");
+ {
+ const before = try c.serials(a);
+ var waiting = try Child.spawn(&.{ exe, "--wait", far }, .{ .root = root, .env = outside_env });
+ defer waiting.deinit();
+ const deadline = process.nowMs() + 5_000;
+ while (same(before, try c.serials(a)) and process.nowMs() < deadline) process.nap(10);
+ const far_pane = try madeOne(a, c, before);
+ process.nap(300);
+ try running(a, &waiting);
+ try c.removePane(far_pane);
+ try exitsWith(&waiting, 0, 5_000);
+ }
+ // --wait with a $PARDES_9P that answers no more: said, exit 1.
+ const dead = try process.run(a, &.{ exe, "--wait", far }, .{ .root = root, .env = &.{.{ "PARDES_9P", d.abs("pardes-9p-gone.sock") }} });
+ try check(dead.code == 1 and has(dead.said, "does not answer"), "dead: exit {d}: {s}", .{ dead.code, dead.said });
+ // --wait with no FILE says it needs one, and waits on nothing.
+ const bare = try process.run(a, &.{ exe, "--wait" }, opts);
+ try check(bare.code == 1 and has(bare.said, "--wait needs a FILE"), "bare: exit {d}: {s}", .{ bare.code, bare.said });
+ // `--` ends the options: a name starting with a dash is a name.
+ try d.put("-dash.txt", "dash\n");
+ {
+ const before = try c.serials(a);
+ const r = try process.run(a, &.{ exe, "--", "-dash.txt" }, opts);
+ try check(r.code == 0, "--: exit {d}: {s}", .{ r.code, r.said });
+ const dashed = try madeOne(a, c, before);
+ try std.testing.expectEqualStrings(d.abs("-dash.txt"), std.mem.trimEnd(u8, try c.pane(a, dashed, "name"), "\n"));
+ try c.removePane(dashed);
+ }
+ // A name under a directory that may not be searched is refused,
+ // permission denied, exit 1, no pane. (Root searches any directory.)
+ if (libc.geteuid() != 0) {
+ _ = libc.mkdir(d.abs("noperm"), 0);
+ const before = try c.serials(a);
+ const denied = process.run(a, &.{ exe, "noperm/a/b/c" }, opts);
+ _ = libc.chmod(d.abs("noperm"), 0o755);
+ const r = try denied;
+ try check(r.code == 1 and has(r.said, "permission denied"), "noperm: exit {d}: {s}", .{ r.code, r.said });
+ try check(same(before, try c.serials(a)), "noperm made a pane", .{});
+ }
+ // --wait waits on the pane its look answered: a name /index shows
+ // escaped (a backslash is `\\` there) is still the pane waited on.
+ try d.put("wait\\odd.txt", "odd\n");
+ {
+ const before = try c.serials(a);
+ var waiting = try launch(a, c, &.{ exe, "--wait", d.abs("wait\\odd.txt") }, opts);
+ defer waiting.deinit();
+ const odd_pane = try madeOne(a, c, before);
+ process.nap(500);
+ try running(a, &waiting);
+ try c.removePane(odd_pane);
+ try exitsWith(&waiting, 0, 5_000);
+ }
+ // A name not there yet opens a pane named it, as acme's B does, and
+ // Save creates it: git's and fish's temporary files are.
+ {
+ const before = try c.serials(a);
+ var made = try Child.spawn(&.{ exe, "--wait", "wait new.txt" }, opts);
+ defer made.deinit();
+ // The launch makes the pane, then names it: wait for the name.
+ const deadline = process.nowMs() + 3_000;
+ while (!has(try c.read(a, "/index"), d.abs("wait new.txt"))) {
+ try running(a, &made);
+ if (process.nowMs() > deadline) return error.NoPaneNamed;
+ process.nap(10);
+ }
+ const named = try madeOne(a, c, before);
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{named}), "new\n");
+ try c.exec(named, "Save\n");
+ try c.removePane(named);
+ try exitsWith(&made, 0, 5_000);
+ try std.testing.expectEqualStrings("new\n", try d.get(a, "wait new.txt"));
+ }
+ // ...in a directory not there either: forwarded all the same, and Save
+ // makes the directories.
+ {
+ const before = try c.serials(a);
+ const r = try process.run(a, &.{ exe, "wait-missing/sub/new.txt" }, opts);
+ try check(r.code == 0, "deep: exit {d}: {s}", .{ r.code, r.said });
+ const deadline = process.nowMs() + 3_000;
+ while (!has(try c.read(a, "/index"), d.abs("wait-missing/sub/new.txt"))) {
+ if (process.nowMs() > deadline) return error.NoPaneNamed;
+ process.nap(10);
+ }
+ const named = try madeOne(a, c, before);
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{named}), "deep\n");
+ try c.exec(named, "Save\n");
+ try c.removePane(named);
+ try std.testing.expectEqualStrings("deep\n", try d.get(a, "wait-missing/sub/new.txt"));
+ }
+ // `pardes '~/x'`, quoted so the shell left the `~`: the launching
+ // shell's HOME all the same.
+ {
+ const before = try c.serials(a);
+ const r = try process.run(a, &.{ exe, "~/fwd-tilde.txt" }, opts);
+ try check(r.code == 0, "tilde: exit {d}: {s}", .{ r.code, r.said });
+ try check(has(try c.read(a, "/index"), d.abs("fwd-tilde.txt")), "no ~ pane", .{});
+ for (added(a, before, try c.serials(a))) |n| try c.removePane(n);
+ }
+ // A name the session refuses: its reason and exit 1, no pane left.
+ const before = try c.serials(a);
+ {
+ const r = try process.run(a, &.{ exe, long_name }, opts);
+ try check(r.code == 1 and has(r.said, "a component over 255 bytes"), "long: exit {d}: {s}", .{ r.code, r.said });
+ try check(same(before, try c.serials(a)), "a long name made a pane", .{});
+ }
+ // `pardes -` (a terminal's PAGER): stdin, its escapes stripped, in a
+ // +Pager pane of the session, clean, and back at once.
+ const pager_name = try std.fmt.allocPrint(a, "{s}/+Pager\n", .{root});
+ const made_now = blk: {
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "\x1b[1mbold\x1b[0m text\n" });
+ try check(r.code == 0, "pager: exit {d}: {s}", .{ r.code, r.said });
+ break :blk try madeOne(a, c, before);
+ };
+ try std.testing.expectEqualStrings(pager_name, try c.pane(a, made_now, "name"));
+ try std.testing.expectEqualStrings("bold text\n", try c.pane(a, made_now, "body"));
+ {
+ var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
+ const row = while (rows.next()) |r| {
+ if (std.mem.startsWith(u8, r, try std.fmt.allocPrint(a, "{d} ", .{made_now}))) break r;
+ } else return error.NotListed;
+ var words = std.mem.tokenizeScalar(u8, row, ' ');
+ _ = words.next();
+ _ = words.next();
+ try check(std.mem.eql(u8, words.next().?, "0"), "a +Pager is dirty: {s}", .{row}); // clean: nothing to ask at Exit
+ // Its `new` record names it as it is: no +New and a rename.
+ const log = try std.fmt.allocPrint(a, "\n{s}", .{try c.read(a, "/log")});
+ try check(has(log, try std.fmt.allocPrint(a, "\nnew {d} {s}/+Pager\n", .{ made_now, root })), "no new record: {s}", .{log});
+ try check(!has(log, try std.fmt.allocPrint(a, "\nrename {d} ", .{made_now})), "a +Pager renamed", .{});
+ }
+ // Its colours are the pane's, drawn and never read: mapped through the
+ // theme, as a terminal's are; Filter shows them as sent. Each run is its
+ // own +Pager; one there already keeps its page.
+ const coloured = blk: {
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "\x1b]0;title\x07\x1b[31mred\x1b[m \x1b[38;2;1;2;3mrgb\x1b[m plain\n" });
+ try check(r.code == 0, "colours: exit {d}: {s}", .{ r.code, r.said });
+ const new = added(a, before, try c.serials(a));
+ try check(new.len == 2 and new[0] == made_now, "colours: {any}", .{new});
+ break :blk new[1];
+ };
+ try std.testing.expectEqualStrings(pager_name, try c.pane(a, coloured, "name"));
+ try std.testing.expectEqualStrings("red rgb plain\n", try c.pane(a, coloured, "body"));
+ try std.testing.expectEqualStrings("bold text\n", try c.pane(a, made_now, "body"));
+ const rgb = "{\"rgb\":[1,2,3]}";
+ {
+ const ink = try inks(&d.files);
+ try check(!std.mem.eql(u8, ink[0], ink[2]) and !std.mem.eql(u8, ink[1], rgb), "drawn inks {s} {s} {s}", .{ ink[0], ink[1], ink[2] });
+ }
+ try c.exec(coloured, "Filter\n");
+ {
+ const ink = try inks(&d.files);
+ try std.testing.expectEqualStrings(rgb, ink[1]);
+ try check(!std.mem.eql(u8, ink[0], ink[2]) and !std.mem.eql(u8, ink[0], ink[1]), "filtered inks {s} {s} {s}", .{ ink[0], ink[1], ink[2] });
+ }
+ // A progress line keeps its last state.
+ const again = blk: {
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "again\r\nprogress 10%\rprogress 100%\n" });
+ try check(r.code == 0, "progress: exit {d}: {s}", .{ r.code, r.said });
+ const new = added(a, before, try c.serials(a));
+ try check(new.len == 3, "progress: {any}", .{new});
+ break :blk new[2];
+ };
+ try std.testing.expectEqualStrings("again\nprogress 100%\n", try c.pane(a, again, "body"));
+ for ([_]u32{ made_now, coloured, again }) |n| try c.removePane(n);
+ // More than one 8 MiB write's worth arrives whole, byte for byte.
+ {
+ var big: std.ArrayList(u8) = .empty;
+ defer big.deinit(gpa);
+ for (0..400_000) |i| try big.print(gpa, "line {d:0>8} of the paged text\n", .{i}); // ~11 MiB
+ try std.testing.expect(big.items.len > 8 * 1024 * 1024);
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = big.items });
+ try check(r.code == 0, "big: exit {d}: {s}", .{ r.code, r.said });
+ const big_pane = try madeOne(a, c, before);
+ const got = try c.read(gpa, try std.fmt.allocPrint(a, "/pane/{d}/body", .{big_pane}));
+ defer gpa.free(got);
+ try check(std.mem.eql(u8, got, big.items), "the paged body is not the text", .{});
+ // Past the 256 MiB cap: the first 256 MiB, then the cut note.
+ const cap = 256 * 1024 * 1024;
+ var exe_buf: [4096]u8 = undefined;
+ const huge = try process.run(a, &.{ "sh", "-c", z(a, "yes 0123456789abcdef | head -c {d} | \"$0\" -", .{cap + 4096}), std.mem.span(realpath(exe, &exe_buf) orelse return error.NoBinary) }, .{ .root = root, .env = &env });
+ try check(huge.code == 0, "huge: exit {d}: {s}", .{ huge.code, huge.said });
+ try c.removePane(big_pane);
+ }
+ {
+ const cap = 256 * 1024 * 1024;
+ const big_pane = try madeOne(a, c, before);
+ const got = try c.read(gpa, try std.fmt.allocPrint(a, "/pane/{d}/body", .{big_pane}));
+ defer gpa.free(got);
+ const note = "\n[pardes: the rest of stdin is cut: a pane holds at most a file's limit]\n";
+ try check(std.mem.endsWith(u8, got, note) and got.len == cap + note.len, "huge: {d} bytes", .{got.len});
+ const line = "0123456789abcdef\n";
+ var i: usize = 0;
+ while (i < cap) : (i += line.len) {
+ const n = @min(line.len, cap - i);
+ if (!std.mem.eql(u8, got[i..][0..n], line[0..n])) return error.NotTheText;
+ }
+ try c.removePane(big_pane);
+ }
+ // From a directory whose name could not be a ctl line: the session's
+ // directory's +Pager, the text whole, no stray +New.
+ _ = libc.mkdir(d.abs("two\nlines"), 0o755);
+ {
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .cwd = d.abs("two\nlines"), .env = &env, .stdin = "odd dir text\n" });
+ try check(r.code == 0, "odd dir: exit {d}: {s}", .{ r.code, r.said });
+ const odd_pane = try madeOne(a, c, before);
+ try std.testing.expectEqualStrings(pager_name, try c.pane(a, odd_pane, "name"));
+ try std.testing.expectEqualStrings("odd dir text\n", try c.pane(a, odd_pane, "body"));
+ try c.removePane(odd_pane);
+ }
+ // Nothing on stdin: no pane, and done.
+ {
+ const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "" });
+ try check(r.code == 0 and same(before, try c.serials(a)), "empty stdin: exit {d}: {s}", .{ r.code, r.said });
+ }
+ // A new name in a directory there but not writable: refused now, not a
+ // pane that only fails at Save. (Root writes anywhere.)
+ _ = libc.mkdir(d.abs("shut-dir"), 0o500);
+ if (libc.geteuid() != 0) {
+ const r = try process.run(a, &.{ exe, "shut-dir/new.txt" }, opts);
+ try check(r.code == 1 and has(r.said, "may not be read or written"), "shut-dir: exit {d}: {s}", .{ r.code, r.said });
+ try check(same(before, try c.serials(a)), "shut-dir made a pane", .{});
+ }
+ _ = libc.chmod(d.abs("shut-dir"), 0o755);
+ // A pane address the session has not is refused in words, exit 1, no
+ // pane made for a file of that name; one it has is looked at. A name
+ // with a newline is said in words, not an error name.
+ for ([_][2][:0]const u8{
+ .{ "@p999:1", "pardes: @p999:1: this session has no such pane" },
+ .{ "two\nlines.txt", "pardes: two\\nlines.txt: a file name is one line" },
+ }) |row| {
+ const r = try process.run(a, &.{ exe, row[0] }, opts);
+ try check(r.code == 1 and has(r.said, row[1]), "{s}: exit {d}: {s}", .{ row[0], r.code, r.said });
+ try check(!has(r.said, "NotOneLine") and !has(r.said, "NoSuchPane"), "an error name: {s}", .{r.said});
+ try check(same(before, try c.serials(a)), "{s} made a pane", .{row[0]});
+ }
+ const looked = try process.run(a, &.{ exe, "@p1:1" }, opts);
+ try check(looked.code == 0, "@p1:1: exit {d}: {s}", .{ looked.code, looked.said });
+ try check(same(before, try c.serials(a)), "@p1:1 made a pane", .{});
+ // A session that goes away ends a --wait on it with 1.
+ var orphan = try launch(a, c, &.{ exe, "--wait", edited }, opts);
+ defer orphan.deinit();
+ try running(a, &orphan);
+ d.deinit();
+ ended = true;
+ try exitsWith(&orphan, 1, 5_000);
+}
+
+/// The inks of `red rgb plain`'s three words, as /screen draws them.
+fn inks(f: *process.Files) ![3][]const u8 {
+ const col, const row = try f.where("red rgb plain");
+ return .{ try f.fg(col, row), try f.fg(col + 4, row), try f.fg(col + 8, row) };
+}
+
+fn winid(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "winid", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const first = std.mem.min(u32, try c.serials(a));
+ try check(has(try commandBody(a, c, try std.fmt.allocPrint(a, "/pane/{d}/tagexec", .{first})), try std.fmt.allocPrint(a, "winid={d}\n", .{first})), "a pane's tag: no $winid", .{});
+ try check(has(try commandBody(a, c, "/tagexec"), "winid=\n"), "the workspace's tag: a $winid", .{});
+}
+
+/// What `echo winid=$winid` written to `node` printed, its pane removed.
+fn commandBody(a: std.mem.Allocator, c: *Conn, node: []const u8) ![]const u8 {
+ try c.write(node, "echo winid=$winid\n");
+ var words = std.mem.tokenizeAny(u8, try c.read(a, node), " \n");
+ var last: []const u8 = "";
+ while (words.next()) |w| last = w;
+ const serial = try std.fmt.parseInt(u32, last, 10);
+ const deadline = process.nowMs() + 10_000;
+ while (true) {
+ const log = try c.read(a, "/log");
+ if (std.mem.lastIndexOf(u8, log, try std.fmt.allocPrint(a, "\nrun {d} ", .{serial}))) |ran| {
+ if (has(log[ran..], try std.fmt.allocPrint(a, "\nexit {d} ", .{serial}))) {
+ const body = try c.pane(a, serial, "body");
+ var parts = std.mem.splitSequence(u8, body, "% echo winid");
+ var tail: []const u8 = "";
+ while (parts.next()) |p| tail = p;
+ try c.removePane(serial);
+ return tail;
+ }
+ }
+ if (process.nowMs() > deadline) return error.NeverExited;
+ process.nap(50);
+ }
+}
+
+fn outside(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ // A session for its root alone.
+ var d = try process.Detached.start(gpa, exe, "outside", .{});
+ defer d.deinit();
+ // `pardes -` outside a session: an editor whose first pane is a +Pager
+ // of stdin, as `vim -`; here a detached one, to read it.
+ {
+ var boot = try Child.spawn(&.{ exe, "--detach=stdinboot", "-" }, .{ .root = d.root(), .stdin = "\x1b[32mfrom stdin\x1b[m\nsecond\n" });
+ defer boot.deinit();
+ const sock = d.abs("pardes-9p-stdinboot.sock");
+ try boot.waitPath(sock, 10_000);
+ var booted: process.Files = .init(gpa, sock);
+ defer booted.deinit();
+ var first = std.mem.tokenizeScalar(u8, std.mem.sliceTo(try booted.read("/index"), '\n'), ' ');
+ const serial = first.next().?;
+ _ = first.next();
+ _ = first.next();
+ try std.testing.expectEqualStrings(d.abs("+Pager"), first.next().?);
+ try booted.expect(booted.at("/pane/{s}/body", .{serial}), "from stdin\nsecond\n");
+ }
+ // A launch with no terminal to draw on (a pty that is no one's
+ // controlling terminal) says so and exits 1, no error trace.
+ var tty = try Child.spawn(&.{ exe, "--tty", "x.txt" }, .{ .root = d.root(), .pty = .{ .log = d.abs("noctty.pty"), .ctty = false } });
+ defer tty.deinit();
+ const words = try tty.said(a);
+ try exitsWith(&tty, 1, 10_000);
+ try check(has(words, "pardes: no terminal to draw on") and !has(words, ".zig:"), "no terminal: {s}", .{words});
+}
+
+/// A unix socket's file at `path`, bound and closed, as a dead editor
+/// leaves one.
+fn socketFile(path: []const u8) !void {
+ const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM, 0);
+ if (fd < 0) return error.NoSocket;
+ defer _ = libc.close(fd);
+ var addr: libc.sockaddr.un = .{ .path = undefined };
+ @memset(&addr.path, 0);
+ @memcpy(addr.path[0..path.len], path);
+ if (libc.bind(fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) != 0) return error.NoBind;
+}
+
+fn killedTty(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "killed", .{});
+ defer d.deinit();
+ // Before the deinit, a failure's way out too: else the root comes back.
+ defer daemonGone(&d);
+ // Killed with SIGTERM or SIGHUP, a tty editor puts its terminal back
+ // (main screen, cooked mode) before it dies, and its 9P socket goes
+ // with it. One a dead editor left (no process has its pid) is swept at
+ // the next start; a live pid's, here this test's own, never is.
+ for ([_]libc.SIG{ .TERM, .HUP }) |sig| {
+ const dead = "pardes-9p-2147483647.sock";
+ const live = try std.fmt.allocPrint(a, "pardes-9p-{d}.sock", .{libc.getpid()});
+ for ([_][]const u8{ dead, live }) |name| if (!d.exists(name)) try socketFile(d.abs(name));
+ const existing = try process.entries(a, d.root());
+ const log = d.abs(try std.fmt.allocPrint(a, "killed-{t}.pty", .{sig}));
+ // The daemon's, as a user's editor has: a hangup leaves the session.
+ var killed = try Child.spawn(&.{ exe, "--tty", "x.txt" }, .{ .root = d.root(), .env = &.{.{ "PARDES_DAEMON", "on" }}, .pty = .{ .log = log } });
+ defer killed.deinit();
+ const deadline = process.nowMs() + 10_000;
+ while (!has(d.get(a, std.fs.path.basename(log)) catch "", "\x1b[?1049h")) {
+ if (process.nowMs() > deadline) return error.NeverDrew;
+ process.nap(20);
+ }
+ process.nap(300);
+ // The session's own, named by its pid: forked off the launched
+ // process, which stays as its stub.
+ const own = try newSocket(a, d.root(), existing, &killed);
+ try check(!d.exists(dead), "a dead editor's socket kept", .{});
+ try check(d.exists(live), "a live pid's socket swept", .{});
+ killed.kill(sig);
+ try exitsWith(&killed, -@as(c_int, @intCast(@intFromEnum(sig))), 5_000);
+ const own_z = z(a, "{s}", .{own});
+ if (sig == .HUP) {
+ // A hangup is the terminal's end: the session carries on
+ // detached (the daemon keeps it) until told to end.
+ try check(libc.access(own_z, 0) == 0, "a hangup ended the session", .{});
+ const name = std.fs.path.basename(own);
+ _ = libc.kill(try std.fmt.parseInt(libc.pid_t, name["pardes-9p-".len .. name.len - ".sock".len], 10), .TERM);
+ }
+ const gone_by = process.nowMs() + 5_000;
+ while (libc.access(own_z, 0) == 0 and process.nowMs() < gone_by) process.nap(10);
+ try check(libc.access(own_z, 0) != 0, "{s} left behind", .{own});
+ process.nap(200);
+ const seen = try d.get(a, std.fs.path.basename(log));
+ const tail = seen[std.mem.lastIndexOf(u8, seen, "\x1b[?1049h").?..];
+ try check(has(tail, "\x1b[?1049l"), "the main screen not put back", .{});
+ try check(try process.canonical(killed.slave), "the terminal left raw", .{});
+ daemonGone(&d);
+ }
+}
+
+/// The daemon a session had (PARDES_DAEMON=on) outlives it a little and,
+/// going, makes its runtime directory (the root) again: waited for.
+fn daemonGone(d: *const process.Detached) void {
+ const deadline = process.nowMs() + 5_000;
+ while (d.exists("pardes-daemon.sock") and process.nowMs() < deadline) process.nap(20);
+ process.nap(100);
+}
+
+fn signalCleansUp(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ // A detached session told to stop by a signal unlinks its sockets. A
+ // hangup is a terminal's end, not a session's: the daemon keeps it.
+ for ([_]libc.SIG{ .TERM, .INT, .HUP }) |first| {
+ var d = try process.Detached.start(gpa, exe, "sig", .{ .env = &.{.{ "PARDES_DAEMON", "on" }} });
+ defer d.deinit();
+ defer daemonGone(&d);
+ try std.testing.expect(d.exists("pardes-9p-sig.sock"));
+ var sig = first;
+ if (sig == .HUP) {
+ _ = libc.kill(d.pid, sig);
+ process.nap(300);
+ try check(d.exists("pardes-9p-sig.sock") and (try d.files.read("/index")).len > 0, "a hangup ended a kept session", .{});
+ sig = .TERM;
+ }
+ _ = libc.kill(d.pid, sig);
+ const deadline = process.nowMs() + 5_000;
+ while (d.exists("pardes-9p-sig.sock") and process.nowMs() < deadline) process.nap(10);
+ try check(!d.exists("pardes-9p-sig.sock"), "{t}: the socket left", .{first});
+ }
+}
diff --git a/test/process/look_and_exec.zig b/test/process/look_and_exec.zig
new file mode 100644
index 00000000..2a0266f8
--- /dev/null
+++ b/test/process/look_and_exec.zig
@@ -0,0 +1,239 @@
+//! docs/typ/reference.typ, look and exec <look-and-exec>: one session
+//! driven as the dogfood agents drive it (test/fs.py's workflow_canary).
+const std = @import("std");
+const process = @import("../process.zig");
+const Conn = process.Conn;
+const has = process.has;
+const check = process.check;
+
+pub const cases = .{
+ .{ "look-and-exec: the canary: Look, Grep and Find hit and miss, a word looked at, Edit and Save, get, pty/run, Kill, a REPL send, columns, ask and answer, Dump and Restore, in under 30 s", canary },
+};
+
+const Flow = struct {
+ a: std.mem.Allocator,
+ c: *Conn,
+
+ fn log(f: Flow) ![]const u8 {
+ return f.c.read(f.a, "/log");
+ }
+
+ /// Waits up to 10 s for the log to hold `text`.
+ fn waitLog(f: Flow, text: []const u8) !void {
+ const deadline = process.nowMs() + 10_000;
+ while (!has(try f.log(), text)) {
+ if (process.nowMs() > deadline) {
+ std.debug.print("not in the log: {s}\n", .{text});
+ return error.Timeout;
+ }
+ process.nap(50);
+ }
+ }
+
+ /// The pane an exec at `serial` made, as exec reads it back.
+ fn madeBy(f: Flow, serial: u32, line: []const u8) !u32 {
+ try f.c.exec(serial, line);
+ var words = std.mem.tokenizeAny(u8, try f.c.pane(f.a, serial, "exec"), " \n");
+ var last: []const u8 = "";
+ while (words.next()) |w| last = w;
+ return std.fmt.parseInt(u32, last, 10);
+ }
+
+ /// A line run in terminal `term` through pty/run: its answer.
+ fn run(f: Flow, term: u32, line: []const u8) ![]const u8 {
+ const fid = try f.c.hold(try std.fmt.allocPrint(f.a, "/pane/{d}/pty/run", .{term}), 2);
+ defer f.c.clunk(fid);
+ _ = try f.c.writeFid(fid, 0, line);
+ return f.a.dupe(u8, try f.c.readFid(fid, 0, 65536));
+ }
+
+ /// An event record written back on pane `n`'s event, as a reader acts.
+ fn event(f: Flow, n: u32, record: []const u8) !void {
+ const fid = try f.c.hold(try std.fmt.allocPrint(f.a, "/pane/{d}/event", .{n}), 2);
+ defer f.c.clunk(fid);
+ _ = try f.c.writeFid(fid, 0, record);
+ }
+
+ fn columns(f: Flow) !usize {
+ var n: usize = 0;
+ var rows = std.mem.tokenizeScalar(u8, try f.c.read(f.a, "/layout"), '\n');
+ while (rows.next()) |row| n += @intFromBool(!std.mem.startsWith(u8, row, "active"));
+ return n;
+ }
+};
+
+fn canary(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ const started = process.nowMs();
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "flow", .{});
+ defer d.deinit();
+ try d.put("a.txt", "alpha one\nshared word\n");
+ try d.put("b.txt", "beta two\nshared word\n");
+ try d.put("s.sh", "echo from-file\n");
+ const root = d.root();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const f: Flow = .{ .a = a, .c = c };
+
+ // Look opens files, and the log says new.
+ const pa = try c.look(a, d.abs("a.txt"), null);
+ const pb = try c.look(a, d.abs("b.txt"), null);
+ try std.testing.expectEqualStrings("alpha one\nshared word\n", try c.pane(a, pa, "body"));
+ try check(has(try f.log(), try std.fmt.allocPrint(a, "new {d} {s}/a.txt\n", .{ pa, root })) and
+ has(try f.log(), try std.fmt.allocPrint(a, "new {d} {s}/b.txt\n", .{ pb, root })), "no new records", .{});
+
+ // Grep and Find: a hit fills its query pane, line 1 the query and the
+ // lines under each file's name; a miss fails ENOENT.
+ const grep = try f.madeBy(pa, "Grep shared\n");
+ try std.testing.expectEqualStrings("Grep shared\na.txt\na.txt:2:1 shared word\nb.txt\nb.txt:2:1 shared word\n", try c.pane(a, grep, "body"));
+ try c.removePane(grep);
+ try c.execRefused(pa, "Grep zzqqnomatch\n", "Grep: text not found");
+ const found = try f.madeBy(pa, "Find b.txt\n");
+ try check(has(try c.pane(a, found, "body"), "b.txt"), "Find found nothing", .{});
+ try c.removePane(found);
+ try c.execRefused(pa, "Find zzqqnomatch\n", "Find: name not found");
+
+ // A plain word looked at is found in the pane, after its dot.
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/dot", .{pa}), " 0 0 ");
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/look", .{pa}), "shared\n");
+ {
+ var it = std.mem.tokenizeAny(u8, try c.pane(a, pa, "dot"), " \n");
+ try check(std.mem.eql(u8, it.next().?, "10") and std.mem.eql(u8, it.next().?, "16"), "the word not found after dot", .{});
+ }
+
+ // Edit in two files, then Save each: on disk, and logged.
+ for ([_]u32{ pa, pb }) |n| try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{n}), "Edit ,s/shared/SHARED/g\n");
+ try std.testing.expectEqualStrings("beta two\nSHARED word\n", try c.pane(a, pb, "body"));
+ for ([_]u32{ pa, pb }) |n| try c.exec(n, "Save\n");
+ try std.testing.expectEqualStrings("alpha one\nSHARED word\n", try d.get(a, "a.txt"));
+ try check(has(try f.log(), try std.fmt.allocPrint(a, "save {d} {s}/a.txt\n", .{ pa, root })) and
+ has(try f.log(), try std.fmt.allocPrint(a, "save {d} {s}/b.txt\n", .{ pb, root })), "no save records", .{});
+
+ // get reloads what changed on disk.
+ try d.put("b.txt", "from disk\n");
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{pb}), "get\n");
+ try std.testing.expectEqualStrings("from disk\n", try c.pane(a, pb, "body"));
+
+ // A terminal: pty/run failing then passing.
+ const term = try f.madeBy(1, "Tty\n");
+ {
+ const deadline = process.nowMs() + 10_000;
+ while (true) {
+ const answer = try f.run(term, "true\n");
+ if (std.mem.eql(u8, answer, "exit 0\n")) break;
+ if (process.nowMs() > deadline) {
+ std.debug.print("pty/run answered {s}\n", .{answer});
+ return error.Timeout;
+ }
+ process.nap(50);
+ }
+ }
+ try std.testing.expectEqualStrings("exit 1\n", try f.run(term, "false\n"));
+ try std.testing.expectEqualStrings("exit 0\npassed\n", try f.run(term, "echo passed\n"));
+ // (A run's answer is its record: the log has no err for these.)
+ try check(!has(try f.log(), try std.fmt.allocPrint(a, "err {d} pty/run", .{term})), "pty/run logged an err", .{});
+
+ // A command pane, and Kill: its exit is logged.
+ const sleeper = try f.madeBy(pa, "sleep 30\n");
+ try check(has(try f.log(), try std.fmt.allocPrint(a, "run {d} sleep 30\n", .{sleeper})), "no run record", .{});
+ try c.write("/ctl", "Kill sleep\n");
+ try f.waitLog(try std.fmt.allocPrint(a, "\nexit {d} ", .{sleeper}));
+
+ // A REPL send: a program in the terminal (cat, which says back what it
+ // is sent) bound as bash's REPL; an exec on a .sh file's body (its event
+ // written back) goes there.
+ const s = try c.look(a, d.abs("s.sh"), null);
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/pty/data", .{term}), "cat\r");
+ {
+ const deadline = process.nowMs() + 10_000;
+ while (true) {
+ const status = std.mem.trimEnd(u8, try c.pane(a, term, "pty/status"), " \n");
+ if (std.mem.endsWith(u8, status, " 1") or std.mem.eql(u8, status, "1")) break;
+ if (process.nowMs() > deadline) return error.ReplNeverStarted;
+ process.nap(50);
+ }
+ }
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{term}), "Repl bash\n");
+ try f.event(s, "MX0 0 0 14 echo from-file\n");
+ try f.waitLog(try std.fmt.allocPrint(a, "\nsend {d} {d} ", .{ s, term }));
+ {
+ const deadline = process.nowMs() + 10_000;
+ while (std.mem.count(u8, try c.pane(a, term, "body"), "echo from-file") < 2) {
+ if (process.nowMs() > deadline) return error.ReplNeverGotIt;
+ process.nap(50);
+ }
+ }
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{term}), "Repl -\n");
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/pty/data", .{term}), "\x04");
+ {
+ const deadline = process.nowMs() + 10_000;
+ while (!std.mem.eql(u8, try f.run(term, "true\n"), "exit 0\n")) {
+ if (process.nowMs() > deadline) return error.ShellNeverBack;
+ process.nap(50);
+ }
+ }
+
+ // The layout and a column's files: Newcol, its tag, rmdir.
+ try check(std.mem.count(u8, try c.read(a, "/layout"), "\n") >= 2, "no column in /layout", .{}); // a column, then `active`
+ try c.write("/ctl", "Newcol\n");
+ try std.testing.expectEqual(@as(usize, 2), try f.columns());
+ const empty = blk: {
+ var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/layout"), '\n');
+ var last: []const u8 = "";
+ while (rows.next()) |row| if (!std.mem.startsWith(u8, row, "active")) {
+ last = row;
+ };
+ break :blk std.mem.sliceTo(last, ' ');
+ };
+ try check(has(try c.read(a, try std.fmt.allocPrint(a, "/col/{s}/tag", .{empty})), "New"), "a column's tag has no New", .{});
+ try c.remove(try std.fmt.allocPrint(a, "/col/{s}", .{empty}));
+ try std.testing.expectEqual(@as(usize, 1), try f.columns());
+
+ // ask/answer: Save on a terminal asks for a path over 9P.
+ try c.exec(term, "Save\n");
+ try f.waitLog(try std.fmt.allocPrint(a, "ask {d} save path\n", .{term}));
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{term}), try std.fmt.allocPrint(a, "answer {s}/scroll.txt\n", .{root}));
+ {
+ const deadline = process.nowMs() + 10_000;
+ while (!d.exists("scroll.txt")) {
+ if (process.nowMs() > deadline) return error.AnsweredSaveWroteNothing;
+ process.nap(50);
+ }
+ }
+ try check(has(try f.log(), try std.fmt.allocPrint(a, "answer {d} {s}/scroll.txt\n", .{ term, root })), "no answer record", .{});
+
+ // Dump, then Restore it: the new session names its panes.
+ try c.write("/ctl", "Dump\n");
+ const saved = d.abs("flow.dump.zon");
+ {
+ const deadline = process.nowMs() + 10_000;
+ while (!d.exists("flow.dump.zon")) {
+ if (process.nowMs() > deadline) return error.DumpWroteNothing;
+ process.nap(50);
+ }
+ }
+ try check(has(try f.log(), try std.fmt.allocPrint(a, "dump {s}\n", .{saved})), "no dump record", .{});
+ c.write("/ctl", try std.fmt.allocPrint(a, "Restore {s}\n", .{saved})) catch {};
+ process.nap(500);
+ const restored = try Conn.dial(gpa, d.socket());
+ defer restored.deinit(gpa);
+ const r: Flow = .{ .a = a, .c = restored };
+ try r.waitLog(try std.fmt.allocPrint(a, "restore {s}", .{saved}));
+ const names = try restored.read(a, "/index");
+ try check(has(names, d.abs("a.txt")) and has(names, d.abs("b.txt")), "restored {s}", .{names});
+ // Recent goes on across the Restore, and is kept after it.
+ try check(has(try restored.read(a, "/recent"), try std.fmt.allocPrint(a, "{s}/a.txt\n", .{root})), "Recent lost", .{});
+ try d.put("after.txt", "after\n");
+ _ = try restored.look(a, d.abs("after.txt"), null);
+ try check(has(try restored.read(a, "/recent"), try std.fmt.allocPrint(a, "open {s}/after.txt\n", .{root})), "no open in Recent", .{});
+ {
+ const deadline = process.nowMs() + 10_000;
+ while (!has(d.get(a, ".local/state/pardes/store") catch "", d.abs("after.txt"))) {
+ if (process.nowMs() > deadline) return error.RecentNotKept;
+ process.nap(50);
+ }
+ }
+ try check(process.nowMs() - started < 30_000, "the canary took {d} ms", .{process.nowMs() - started});
+}
diff --git a/test/process/mounts.zig b/test/process/mounts.zig
new file mode 100644
index 00000000..10dc7b5a
--- /dev/null
+++ b/test/process/mounts.zig
@@ -0,0 +1,212 @@
+//! docs/typ/reference.typ, Mounts <mounts>: sessions mounting each other's
+//! trees over their sockets, at startup and at run time.
+const std = @import("std");
+const process = @import("../process.zig");
+const Conn = process.Conn;
+const Detached = process.Detached;
+const has = process.has;
+const check = process.check;
+
+pub const cases = .{
+ .{ "mounts: --mount at startup reads a peer's panes and its disk, a Save through it lands there, and a path not there is a miss in the log", startup },
+ .{ "mounts: a session listening on TCP and QUIC too (-Dquic=true) is mounted over QUIC, saved and got through, its screen read, and unmounted, v4 and v6, detached and in a pty", quic },
+ .{ "mounts: Mount and Unmount at run time, refused in words when mounted already or in use; a session's /n lists its mounts; a file through a mount opens at startup", runtime },
+};
+
+/// A session whose pane `serial` holds "replaced\nevent\n", as fs.py's
+/// `first` had one when its peers mounted it.
+fn peer(gpa: std.mem.Allocator, exe: [:0]const u8, a: std.mem.Allocator) !struct { Detached, *Conn, u32 } {
+ var d = try Detached.start(gpa, exe, "first", .{});
+ errdefer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ const serial = try c.newPane();
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{serial}), "replaced\nevent\n");
+ return .{ d, c, serial };
+}
+
+fn startup(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var first, const client, const serial = try peer(gpa, exe, a);
+ defer first.deinit();
+ defer client.deinit(gpa);
+ const address = first.abs("pardes-9p-first.sock");
+ const own = first.abs("pardes-9p-second.sock");
+ var second = try Detached.start(gpa, exe, "second", .{
+ .root = first.root(),
+ .args = &.{ try std.fmt.allocPrintSentinel(a, "--mount=peer={s}", .{address}, 0), try std.fmt.allocPrintSentinel(a, "--mount=own={s}", .{own}, 0) },
+ });
+ defer second.deinit();
+ const remote = try Conn.dial(gpa, second.socket());
+ defer remote.deinit(gpa);
+ var opened = try remote.look(a, "/n/own/pane/1/body", null);
+ try std.testing.expectEqualStrings("initial\n", try remote.pane(a, opened, "body"));
+ opened = try remote.look(a, try std.fmt.allocPrint(a, "/n/peer/pane/{d}/body", .{serial}), null);
+ try std.testing.expectEqualStrings("replaced\nevent\n", try remote.pane(a, opened, "body"));
+ try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), "saved to peer\n");
+ try remote.exec(opened, "Save\n");
+ try std.testing.expectEqualStrings("saved to peer\n", try client.pane(a, serial, "body"));
+
+ var large: std.ArrayList(u8) = .empty;
+ for (0..60000) |_| try large.appendSlice(a, "mounted file contents\n");
+ try first.put("large.txt", large.items);
+ opened = try remote.look(a, try std.fmt.allocPrint(a, "/n/peer/os{s}", .{first.abs("large.txt")}), null);
+ try check(std.mem.eql(u8, try remote.pane(a, opened, "body"), large.items), "the mounted file is not whole", .{});
+ const edited = try std.fmt.allocPrint(a, "{s}edited\n", .{large.items[0 .. large.items.len - 3]});
+ try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), edited);
+ try remote.exec(opened, "Save\n");
+ try check(std.mem.eql(u8, try first.get(a, "large.txt"), edited), "the Save through the mount is not on disk", .{});
+ try check(std.mem.eql(u8, try client.read(a, try std.fmt.allocPrint(a, "/os{s}", .{first.abs("large.txt")})), edited), "the peer's /os disagrees", .{});
+ // A path not there under a peer's mount is a miss, as any path to no
+ // file is: the write answered, the err logged.
+ try remote.write("/look", try std.fmt.allocPrint(a, "/n/peer/os{s}\n", .{first.abs("no-such-file.txt")}));
+ try check(has(try remote.read(a, "/log"), "no such file"), "no miss logged", .{});
+ try client.exec(serial, "Del\n");
+ for (try client.list(a, "/pane")) |n| try check(!std.mem.eql(u8, n, try std.fmt.allocPrint(a, "{d}", .{serial})), "Del left the pane", .{});
+}
+
+fn runtime(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var first, const client, _ = try peer(gpa, exe, a);
+ defer first.deinit();
+ defer client.deinit(gpa);
+ const address = first.abs("pardes-9p-first.sock");
+ {
+ var d = try Detached.start(gpa, exe, "runtime", .{ .root = first.root() });
+ defer d.deinit();
+ const remote = try Conn.dial(gpa, d.socket());
+ defer remote.deinit(gpa);
+ const own_address = first.abs("pardes-9p-runtime.sock");
+ const control = try remote.newPane();
+ try remote.exec(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{address}));
+ var opened = try remote.look(a, "/n/peer/pane/1/body", control);
+ try std.testing.expectEqualStrings(try client.read(a, "/pane/1/body"), try remote.pane(a, opened, "body"));
+ // A builtin that fails through exec fails the write (one rule).
+ try remote.execRefused(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{own_address}), "already mounted");
+ try remote.execRefused(control, "Unmount peer\n", "mount in use"); // a pane is open on it
+ try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), "runtime mounted Save\n");
+ try remote.exec(opened, "Save\n");
+ try std.testing.expectEqualStrings("runtime mounted Save\n", try client.read(a, "/pane/1/body"));
+ try std.testing.expectEqualStrings("initial\n", try remote.read(a, "/pane/1/body"));
+ const roots = try remote.look(a, "/n", control);
+ try check(has(try remote.pane(a, roots, "body"), "/n/peer/\n"), "/n lists no peer", .{});
+ try remote.exec(opened, "Del\n");
+ try remote.exec(control, "Unmount peer\n");
+ try remote.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{roots}), "get\n");
+ try std.testing.expectEqualStrings("/n/os/\n/n/self/\n", try remote.pane(a, roots, "body"));
+ try remote.exec(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{own_address}));
+ opened = try remote.look(a, "/n/peer/pane/1/body", control);
+ try std.testing.expectEqualStrings("initial\n", try remote.pane(a, opened, "body"));
+ }
+ var d = try Detached.start(gpa, exe, "mounted-startup", .{
+ .root = first.root(),
+ .args = &.{try std.fmt.allocPrintSentinel(a, "--mount=peer={s}", .{address}, 0)},
+ .file = "/n/peer/pane/1/body:1",
+ });
+ defer d.deinit();
+ try d.files.expect("/pane/1/body", "runtime mounted Save\n");
+}
+
+/// The port of /listeners' `<net>!<host>!<port>` line.
+fn port(listeners: []const u8, net: []const u8) !u16 {
+ var rows = std.mem.tokenizeScalar(u8, listeners, '\n');
+ while (rows.next()) |row| {
+ var parts = std.mem.splitScalar(u8, row, '!');
+ if (!std.mem.eql(u8, parts.first(), net)) continue;
+ _ = parts.next() orelse return error.BadListener;
+ const p = try std.fmt.parseInt(u16, parts.next() orelse return error.BadListener, 10);
+ try check(parts.next() == null and p > 0, "listener {s}", .{row});
+ return p;
+ }
+ return error.NoListener;
+}
+
+fn quic(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ if (!try process.quicIncluded(gpa, exe)) {
+ std.debug.print("SKIP a build without QUIC (-Dquic=true)\n", .{});
+ return;
+ }
+ for ([_]bool{ false, true }) |tty| for ([_][:0]const u8{ "127.0.0.1", "::1" }) |host| {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const args = [_][:0]const u8{
+ try std.fmt.allocPrintSentinel(a, "--9p-tcp=tcp!{s}!0", .{host}, 0),
+ try std.fmt.allocPrintSentinel(a, "--9p-quic=quic!{s}!0", .{host}, 0),
+ };
+ var d = try Detached.start(gpa, exe, if (tty) "quic-root" else "server", .{ .args = if (tty) &.{} else &args });
+ defer d.deinit();
+ var term: ?process.Child = null;
+ defer if (term) |*t| t.deinit();
+ if (tty) term = try d.startTty(exe, "server", &args);
+ const address = d.abs("pardes-9p-server.sock");
+ const unix = try Conn.dial(gpa, address);
+ defer unix.deinit(gpa);
+ const listeners = try unix.read(a, "/listeners");
+ const tcp_port = try port(listeners, "tcp");
+ const quic_port = try port(listeners, "quic");
+ try std.testing.expectEqual(@as(usize, 3), std.mem.count(u8, listeners, "\n"));
+ for ([_][]const u8{
+ try std.fmt.allocPrint(a, "unix!{s}\n", .{address}),
+ try std.fmt.allocPrint(a, "tcp!{s}!{d}\n", .{ host, tcp_port }),
+ try std.fmt.allocPrint(a, "quic!{s}!{d}\n", .{ host, quic_port }),
+ }) |row| try check(has(listeners, row), "no {s} in {s}", .{ row, listeners });
+ const tcp = try Conn.dialTcp(gpa, host, tcp_port);
+ defer tcp.deinit(gpa);
+ const unix_body = try unix.hold("/pane/1/body", 0);
+ defer unix.clunk(unix_body);
+ const tcp_body = try tcp.hold("/pane/1/body", 0);
+ defer tcp.clunk(tcp_body);
+ var client = try Detached.start(gpa, exe, "client", .{ .root = d.root() });
+ defer client.deinit();
+ const remote = try Conn.dial(gpa, client.socket());
+ defer remote.deinit(gpa);
+ const control = try remote.newPane();
+ try remote.exec(control, try std.fmt.allocPrint(a, "Mount peer quic!{s}!{d}\n", .{ host, quic_port }));
+ const opened = try remote.look(a, "/n/peer/pane/1/body", control);
+ const body = try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened});
+ try std.testing.expectEqualStrings("initial\n", try remote.read(a, body));
+ var payload: std.ArrayList(u8) = .empty;
+ try payload.appendSlice(a, "saved café λ\n");
+ try payload.appendNTimes(a, 'x', 3 * 8192 + 27);
+ try payload.append(a, '\n');
+ try remote.replace(body, payload.items);
+ try remote.exec(opened, "Save\n");
+ try std.testing.expectEqualStrings(payload.items[0..@min(payload.items.len, unix.cl.maxRead())], try unix.readFid(unix_body, 0, 65536));
+ try check(std.mem.eql(u8, try tcp.read(a, "/pane/1/body"), payload.items), "TCP reads another body", .{});
+ try tcp.replace("/pane/1/body", "TCP to QUIC Get\n");
+ try remote.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{opened}), "get\n");
+ try std.testing.expectEqualStrings("TCP to QUIC Get\n", try remote.read(a, body));
+ try std.testing.expectEqualStrings("TCP to QUIC Get\n", try unix.readFid(unix_body, 0, 65536));
+ try std.testing.expectEqualStrings("TCP to QUIC Get\n", try tcp.readFid(tcp_body, 0, 65536));
+ // The peer's own screen, whole, over QUIC: its pane's tag, not its
+ // text, whose place after the body shrank depends on a frame drawn.
+ const screen_pane = try remote.look(a, "/n/peer/screen", control);
+ const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try remote.pane(a, screen_pane, "body"), .{});
+ const cells = json.object.get("cells").?.array.items;
+ try std.testing.expectEqual(@as(usize, @intCast(json.object.get("cols").?.integer * json.object.get("rows").?.integer)), cells.len);
+ var text: std.ArrayList(u8) = .empty;
+ for (cells) |cell| try text.appendSlice(a, cell.array.items[0].string);
+ try check(has(text.items, "server.txt Save"), "the peer's screen has no tag", .{});
+ var large: std.ArrayList(u8) = .empty;
+ for (0..20000) |_| try large.appendSlice(a, "normal λ line\n");
+ try d.put("large.txt", large.items);
+ const large_pane = try remote.look(a, try std.fmt.allocPrint(a, "/n/peer/os{s}", .{d.abs("large.txt")}), control);
+ try check(std.mem.eql(u8, try remote.pane(a, large_pane, "body"), large.items), "the large file is not whole", .{});
+ try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{large_pane}), "QUIC file saved\n");
+ try remote.exec(large_pane, "Save\n");
+ try std.testing.expectEqualStrings("QUIC file saved\n", try d.get(a, "large.txt"));
+ for ([_]u32{ opened, screen_pane, large_pane }) |n| try remote.exec(n, "Del\n");
+ try remote.exec(control, "Unmount peer\n");
+ const roots = try remote.look(a, "/n", control);
+ try std.testing.expectEqualStrings("/n/os/\n/n/self/\n", try remote.pane(a, roots, "body"));
+ try std.testing.expectEqualStrings("TCP to QUIC Get\n", try unix.readFid(unix_body, 0, 65536));
+ try std.testing.expectEqualStrings("TCP to QUIC Get\n", try tcp.readFid(tcp_body, 0, 65536));
+ // Not checked: a session Looking at its OWN tree through a QUIC
+ // mount: QUIC is answered on the editor's thread, so that Look waits
+ // on the loop it runs in (features.txt, "Left as they are").
+ };
+}
diff --git a/test/process/other_files.zig b/test/process/other_files.zig
index d03d6b36..ad58dd10 100644
--- a/test/process/other_files.zig
+++ b/test/process/other_files.zig
@@ -6,8 +6,33 @@ const process = @import("../process.zig");
pub const cases = .{
.{ "other-files: kbdin and mousein drive a detached session as the user would, each write done before it is answered", keysAndMouse },
.{ "other-files: a held connection lists a directory, stats a file, writes and reads, is refused in words and removes a pane", heldConnection },
+ .{ "other-files: a file opened and closed is in /recent closed, and still is in the next session; Recent's row opens it again", recentSurvivesRestart },
+ .{ "other-files: two sessions share one store: a file opened in one is in the other's /recent, a Del'd pane comes back from @p only where it was", storeShared },
};
+extern "c" fn system(cmd: [*:0]const u8) c_int;
+
+fn has(hay: []const u8, needle: []const u8) bool {
+ return std.mem.indexOf(u8, hay, needle) != null;
+}
+
+/// A session's root kept past it, removed at the test's end.
+fn keepRoot(d: *process.Detached, buf: *[4096]u8) []const u8 {
+ d.owns_root = false;
+ @memcpy(buf[0..d.root().len], d.root());
+ return buf[0..d.root().len];
+}
+
+fn removeRoot(root: []const u8) void {
+ var cmd: [4200]u8 = undefined;
+ _ = system(std.fmt.bufPrintSentinel(&cmd, "rm -rf -- '{s}'", .{root}, 0) catch return);
+}
+
+fn storeExists(root: []const u8) bool {
+ var buf: [4200]u8 = undefined;
+ return std.c.access(std.fmt.bufPrintSentinel(&buf, "{s}/.local/state/pardes/store", .{root}, 0) catch return false, 0) == 0;
+}
+
/// A script types and clicks as the user does: no sleep or poll between a
/// write and the read that checks it; where to click is read off /screen.
/// (test/fs.py's keys_and_mouse, the first case written through the files
@@ -64,3 +89,97 @@ fn heldConnection(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try c.remove(try std.fmt.bufPrint(&path, "/pane/{d}", .{serial}));
try std.testing.expectError(error.NotFound, c.walk(body));
}
+
+/// The list is kept under $XDG_STATE_HOME (here HOME's .local/state) across
+/// restarts.
+fn recentSurvivesRestart(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var root_buf: [4096]u8 = undefined;
+ var one = try process.Detached.start(gpa, exe, "recent1", .{});
+ const root = keepRoot(&one, &root_buf);
+ defer removeRoot(root);
+ const kept = try std.fmt.allocPrint(a, "{s}/kept.txt", .{root});
+ {
+ defer one.deinit();
+ try one.put("kept.txt", "one\ntwo\n");
+ const c = try process.Conn.dial(gpa, one.socket());
+ defer c.deinit(gpa);
+ const serial = try c.look(a, try std.fmt.allocPrint(a, "{s}:2", .{kept}), null);
+ try std.testing.expect(has(try c.read(a, "/recent"), try std.fmt.allocPrint(a, "open {s}\n", .{kept})));
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{serial}), "Del\n");
+ try std.testing.expect(has(try c.read(a, "/recent"), try std.fmt.allocPrint(a, "closed {s}\n", .{kept})));
+ }
+ try std.testing.expect(storeExists(root));
+ var two = try process.Detached.start(gpa, exe, "recent2", .{ .root = root });
+ defer two.deinit();
+ const c = try process.Conn.dial(gpa, two.socket());
+ defer c.deinit(gpa);
+ try std.testing.expect(has(try c.read(a, "/recent"), try std.fmt.allocPrint(a, "closed {s}\n", .{kept})));
+ // Recent fills a query pane with it, its row relative to the pane's
+ // directory, and a look at the row opens it again.
+ try c.write("/pane/1/exec", "Recent\n");
+ var answer = std.mem.tokenizeAny(u8, try c.read(a, "/pane/1/exec"), " \n");
+ var last: []const u8 = "";
+ while (answer.next()) |w| last = w;
+ const rows = try std.fmt.parseInt(u32, last, 10);
+ const body = try c.read(a, try std.fmt.allocPrint(a, "/pane/{d}/body", .{rows}));
+ try std.testing.expect(std.mem.startsWith(u8, body, "Recent "));
+ var lines = std.mem.tokenizeScalar(u8, body, '\n');
+ const row = while (lines.next()) |l| {
+ if (std.mem.startsWith(u8, l, "kept.txt:")) break l;
+ } else return error.NoRow;
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/look", .{rows}), try std.fmt.allocPrint(a, "{s}\n", .{row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse row.len]}));
+ try std.testing.expect(has(try c.read(a, "/recent"), try std.fmt.allocPrint(a, "open {s}\n", .{kept})));
+}
+
+/// Del, and again when the first refused over unsaved text.
+fn delTwice(c: *process.Conn, ctl: []const u8) !void {
+ c.write(ctl, "Del\n") catch try c.write(ctl, "Del\n");
+}
+
+/// The store (Store.zig) shared with no restart; a pane Del'd in one
+/// comes back where it was from a look at its @p serial there, its text
+/// with it, and only there.
+fn storeShared(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var root_buf: [4096]u8 = undefined;
+ var one = try process.Detached.start(gpa, exe, "store1", .{});
+ const root = keepRoot(&one, &root_buf);
+ defer removeRoot(root);
+ {
+ defer one.deinit();
+ var two = try process.Detached.start(gpa, exe, "store2", .{ .root = root });
+ defer two.deinit();
+ const c1 = try process.Conn.dial(gpa, one.socket());
+ defer c1.deinit(gpa);
+ const c2 = try process.Conn.dial(gpa, two.socket());
+ defer c2.deinit(gpa);
+ try one.put("shared.txt", "one\ntwo\nthree\n");
+ const shared = try std.fmt.allocPrint(a, "{s}/shared.txt", .{root});
+ const serial = try c1.look(a, try std.fmt.allocPrint(a, "{s}:3", .{shared}), null);
+ try std.testing.expect(has(try c1.read(a, "/recent"), try std.fmt.allocPrint(a, "open {s}\n", .{shared})));
+ try std.testing.expect(has(try c2.read(a, "/recent"), try std.fmt.allocPrint(a, "closed {s}\n", .{shared})));
+ try c1.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{serial}), "Del\n");
+ try std.testing.expect(has(try c1.read(a, "/recent"), try std.fmt.allocPrint(a, "closed {s}\n", .{shared})));
+ const scratch = try c1.newPane();
+ try c1.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{scratch}), "kept across a Del\n");
+ try delTwice(c1, try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{scratch}));
+ var rows = std.mem.tokenizeScalar(u8, try c1.read(a, "/index"), '\n');
+ while (rows.next()) |r| try std.testing.expect(!std.mem.startsWith(u8, r, try std.fmt.allocPrint(a, "{d} ", .{scratch})));
+ const back = try c1.look(a, try std.fmt.allocPrint(a, "@p{d}", .{scratch}), null);
+ try std.testing.expectEqualStrings("kept across a Del\n", try c1.read(a, try std.fmt.allocPrint(a, "/pane/{d}/body", .{back})));
+ // The other window's session closed no such pane.
+ const source = try c2.newPane();
+ c2.write(try std.fmt.allocPrint(a, "/pane/{d}/look", .{source}), try std.fmt.allocPrint(a, "@p{d}\n", .{scratch})) catch {};
+ var others = std.mem.tokenizeScalar(u8, try c2.read(a, "/index"), '\n');
+ while (others.next()) |r| {
+ const body = try c2.read(a, try std.fmt.allocPrint(a, "/pane/{s}/body", .{r[0..std.mem.indexOfScalar(u8, r, ' ').?]}));
+ try std.testing.expect(!has(body, "kept across a Del"));
+ }
+ }
+ try std.testing.expect(storeExists(root));
+}
diff --git a/test/process/repls.zig b/test/process/repls.zig
new file mode 100644
index 00000000..09e25e96
--- /dev/null
+++ b/test/process/repls.zig
@@ -0,0 +1,79 @@
+//! docs/typ/reference.typ, REPLs <repls>: an exec sent to a real REPL in
+//! a real pty.
+const std = @import("std");
+const process = @import("../process.zig");
+const Conn = process.Conn;
+const has = process.has;
+const check = process.check;
+
+pub const cases = .{
+ .{ "repls: a one-line exec sent to Python's own REPL (pyrepl, bracketed paste) runs at once, after idling too, and a block runs whole", pythonRepl },
+};
+
+/// Waits up to `ms` for the terminal's body to hold `text`.
+fn waitFor(a: std.mem.Allocator, c: *Conn, tty: u32, text: []const u8, ms: i64) !bool {
+ const deadline = process.nowMs() + ms;
+ while (process.nowMs() < deadline) {
+ if (has(try c.pane(a, tty, "body"), text)) return true;
+ process.nap(100);
+ }
+ return false;
+}
+
+fn pythonRepl(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ // 3.13's pyrepl asks for bracketed paste; an older one or none is no test.
+ const version = try process.run(a, &.{ "python3", "-c", "import sys; sys.exit(sys.version_info < (3, 13))" }, .{});
+ if (version.code != 0) {
+ std.debug.print("SKIP no python3 3.13 or later\n", .{});
+ return;
+ }
+ var d = try process.Detached.start(gpa, exe, "repl", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const before = try c.serials(a);
+ try c.write("/pane/1/ctl", "Tty\n");
+ const deadline = process.nowMs() + 10_000;
+ var tty: u32 = 0;
+ while (tty == 0 and process.nowMs() < deadline) : (process.nap(50)) {
+ for (try c.serials(a)) |n| if (std.mem.indexOfScalar(u32, before, n) == null) {
+ tty = @max(tty, n);
+ };
+ }
+ try check(tty != 0, "Tty made no pane", .{});
+ try check(try waitFor(a, c, tty, "$", 15_000) or try waitFor(a, c, tty, ">", 15_000), "no prompt: {s}", .{try c.pane(a, tty, "body")});
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/pty/data", .{tty}), "python3 -q\r");
+ try check(try waitFor(a, c, tty, ">>>", 15_000), "no >>>: {s}", .{try c.pane(a, tty, "body")});
+ try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{tty}), "Repl python\n");
+ // An exec on the REPL's own body goes to the REPL, as its event record
+ // written back does: an empty range acts on its text.
+ const fid = try c.hold(try std.fmt.allocPrint(a, "/pane/{d}/event", .{tty}), 2);
+ defer c.clunk(fid);
+ _ = try c.writeFid(fid, 0, "MX0 0 0 10 print(6*7)\n");
+ try check(try waitFor(a, c, tty, "\n42", 15_000), "no 42: {s}", .{try c.pane(a, tty, "body")});
+ // After the session has sat idle, the Enter still waits for the paste:
+ // core time is as old as the last frame then.
+ for (0..5) |n| {
+ process.nap(2_000);
+ const line = try std.fmt.allocPrint(a, "print({d})", .{1000 + n});
+ _ = try c.writeFid(fid, 0, try std.fmt.allocPrint(a, "MX0 0 0 {d} {s}\n", .{ line.len, line }));
+ try check(try waitFor(a, c, tty, try std.fmt.allocPrint(a, "\n{d}", .{1000 + n}), 15_000), "{d}: {s}", .{ n, try c.pane(a, tty, "body") });
+ }
+ // More than one line, with and without a newline at the end, and a block
+ // with a statement after it: each runs whole, and the REPL is back at
+ // its prompt, not at `...`.
+ for ([_]struct { []const u8, []const []const u8 }{
+ .{ "print(11)\nprint(22)", &.{ "11", "22" } },
+ .{ "print(33)\nprint(44)\n", &.{ "33", "44" } },
+ .{ "for i in range(2):\n print(55 + i)\nprint(77)\n", &.{ "55", "56", "77" } },
+ }) |row| {
+ _ = try c.writeFid(fid, 0, try std.fmt.allocPrint(a, "MX0 0 0 {d} {s}\n", .{ row[0].len, row[0] }));
+ for (row[1]) |out| try check(try waitFor(a, c, tty, try std.fmt.allocPrint(a, "\n{s}\n", .{out}), 15_000), "{s}: no {s}: {s}", .{ row[0], out, try c.pane(a, tty, "body") });
+ process.nap(500);
+ const body = try c.pane(a, tty, "body");
+ try check(std.mem.endsWith(u8, std.mem.trimEnd(u8, body, " \t\r\n"), ">>>"), "{s}: not at its prompt: {s}", .{ row[0], body[body.len -| 200..] });
+ }
+}
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"));
+ }
+}
diff --git a/test/process/terminals.zig b/test/process/terminals.zig
index ba73a64e..e810ceb1 100644
--- a/test/process/terminals.zig
+++ b/test/process/terminals.zig
@@ -1,17 +1,248 @@
-//! docs/typ/guide.typ, Terminals <terminals>: an interactive program in a
-//! real session's shell, driven through its pty files as an agent drives
-//! one (test/agent_session_test.py's interaction checks, which went through
-//! test/agent_session.py).
+//! docs/typ/guide.typ, Terminals <terminals>, through a real session's
+//! socket: its shells run.
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 expectEqual = std.testing.expectEqual;
+const expectEqualStrings = std.testing.expectEqualStrings;
+
+extern "c" fn system(cmd: [*:0]const u8) c_int;
pub const cases = .{
+ .{ "terminals: pty/run takes a line and answers how it ended, then what it printed, on the same open; busy while a job holds the tty, gone with its shell", runFile },
+ .{ "terminals: a terminal started outside the session's directory is named by it from its new on, and a shell that cannot start leaves no pane", namedOnce },
.{ "terminals: a program's prompt shows and holds still, input sent unsubmitted shows, and ^U takes the screen back to what it was", prompt },
.{ "terminals: what a program prints past a screenful stays in the terminal's body, and its prompt after it shows", history },
.{ "terminals: a control byte through pty/data reaches a program in raw mode, which then carries on", rawKey },
};
+fn has(hay: []const u8, needle: []const u8) bool {
+ return std.mem.indexOf(u8, hay, needle) != null;
+}
+
+fn refusedSaying(c: *process.Conn, path: []const u8, data: []const u8, why: []const u8) !void {
+ try std.testing.expectError(error.Refused, c.write(path, data));
+ if (!has(c.said, why)) {
+ std.debug.print("{s} refused saying {s}, not {s}\n", .{ path, c.said, why });
+ return error.WrongWords;
+ }
+}
+
+/// A line written to a terminal's pty/run and the answer read on the same open.
+fn run(a: std.mem.Allocator, c: *process.Conn, serial: u32, line: []const u8) ![]const u8 {
+ var buf: [64]u8 = undefined;
+ const fid = try c.hold(try std.fmt.bufPrint(&buf, "/pane/{d}/pty/run", .{serial}), 2);
+ defer c.clunk(fid);
+ _ = try c.writeFid(fid, 0, line);
+ return a.dupe(u8, try c.readFid(fid, 0, 1 << 16));
+}
+
+/// The serials of /index's rows whose fourth word, the path, is `path`.
+fn panesAt(a: std.mem.Allocator, c: *process.Conn, path: []const u8) ![]const []const u8 {
+ var out: std.ArrayList([]const u8) = .empty;
+ var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
+ while (rows.next()) |row| {
+ var w = std.mem.tokenizeScalar(u8, row, ' ');
+ const serial = w.next().?;
+ _ = w.next();
+ _ = w.next();
+ if (std.mem.eql(u8, w.next() orelse "", path)) try out.append(a, serial);
+ }
+ return out.items;
+}
+
+/// The serials /index lists.
+fn serials(a: std.mem.Allocator, c: *process.Conn) ![]const u32 {
+ var out: std.ArrayList(u32) = .empty;
+ var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
+ while (rows.next()) |row| try out.append(a, try std.fmt.parseInt(u32, row[0..std.mem.indexOfScalar(u8, row, ' ').?], 10));
+ return out.items;
+}
+
+fn shell(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) c_int {
+ return system(std.fmt.allocPrintSentinel(a, fmt, args, 0) catch @panic("out of memory"));
+}
+
+/// A run on another connection, as another client's, in a thread.
+const Other = struct {
+ a: std.mem.Allocator,
+ conn: *process.Conn,
+ serial: u32,
+ line: []const u8,
+ got: []const u8 = "",
+
+ fn go(o: *Other) void {
+ o.got = run(o.a, o.conn, o.serial, o.line) catch "";
+ }
+};
+
+fn runFile(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "run", .{});
+ 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();
+ // From a pane on a peer's path no command or terminal starts: refused
+ // before any pane is made, no `exit 127` pane left.
+ const peer = try c.newPane();
+ try c.write(f.at("/pane/{d}/name", .{peer}), "/n/nosuchpeer/d/f.txt\n");
+ const count = std.mem.count(u8, try c.read(a, "/index"), "\n");
+ try refusedSaying(c, f.at("/pane/{d}/exec", .{peer}), "ls\n", "no such directory");
+ try refusedSaying(c, f.at("/pane/{d}/exec", .{peer}), "Tty\n", "no such directory");
+ try expectEqual(count, std.mem.count(u8, try c.read(a, "/index"), "\n"));
+ try expect(!has(try c.read(a, "/log"), "exit 127"));
+ try c.remove(f.at("/pane/{d}", .{peer}));
+ try c.write("/exec", "Shell bash\n");
+ try c.write("/exec", "Tty\n");
+ var term: u32 = 0;
+ var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
+ while (rows.next()) |r| {
+ var w = std.mem.tokenizeScalar(u8, r, ' ');
+ const serial = try std.fmt.parseInt(u32, w.next().?, 10);
+ if (std.mem.eql(u8, w.next().?, "term")) term = @max(term, serial);
+ }
+ const deadline = eh.nowMs() + 10_000;
+ while (std.mem.eql(u8, try run(a, c, term, "true\n"), "busy\n")) { // deadline its first prompt
+ if (eh.nowMs() > deadline) return error.NoPrompt;
+ process.sleep(50);
+ }
+ try expectEqualStrings("exit 1\n", try run(a, c, term, "false\n"));
+ try expectEqualStrings("exit 0\none\ntwo\n", try run(a, c, term, "printf \"one\\ntwo\\n\"\n")); // then what it printed
+ try expectEqualStrings("exit 7\n", try run(a, c, term, "sh -c \"exit 7\"\n"));
+ try expectEqualStrings("error not run\n", try run(a, c, term, "fi\n")); // a syntax error runs nothing
+ try expectEqualStrings("error not run\n", try run(a, c, term, "echo 'open\n")); // nor does an open quote
+ try expectEqualStrings("exit 0\n", try run(a, c, term, "true\n")); // ...and the shell is clear after
+ // With no frontend attached the pty's size is in pixels too, at a
+ // nominal 8x16 cell: a program sizes its kitty graphics by it.
+ const said = try run(a, c, term, "python3 -c 'import fcntl,termios,struct;print(*struct.unpack(\"4H\",fcntl.ioctl(0,termios.TIOCGWINSZ,bytes(8))))'\n");
+ var lines = std.mem.splitScalar(u8, said, '\n');
+ _ = lines.next();
+ var size = std.mem.tokenizeScalar(u8, lines.next() orelse "", ' ');
+ var n: [4]u32 = undefined;
+ for (&n) |*v| v.* = try std.fmt.parseInt(u32, size.next() orelse return error.NoSize, 10);
+ try expect(n[2] == n[1] * 8 and n[3] == n[0] * 16 and n[2] > 0);
+ // A paging command run through pty/run does not wait in less: the
+ // terminal's PAGER is `pardes -`, and its text is a +Pager pane.
+ if (shell(a, "command -v git >/dev/null", .{}) == 0) {
+ const git = f.at("HOME='{s}' git -C '{s}/pagedrepo' -c user.name=p -c user.email=p@x", .{ root, root });
+ try expectEqual(@as(c_int, 0), shell(a, "mkdir '{s}/pagedrepo' && {s} init -q && echo x > '{s}/pagedrepo/f' && {s} add f && {s} commit -qm paged-commit-subject", .{ root, git, root, git, git }));
+ try expect(std.mem.startsWith(u8, try run(a, c, term, "git -C pagedrepo log\n"), "exit 0\n"));
+ const pager = try panesAt(a, c, f.at("{s}/pagedrepo/+Pager", .{root})); // git -C runs it in the repo
+ try expectEqual(@as(usize, 1), pager.len);
+ const body = try c.read(a, f.at("/pane/{s}/body", .{pager[0]}));
+ try expect(has(body, "paged-commit-subject") and !has(body, "\x1b"));
+ try c.remove(f.at("/pane/{s}", .{pager[0]}));
+ }
+ // systemd's tools page through their own variable: set too, its secure
+ // mode off, the path unquoted where it needs none. Read by the shell:
+ // coreutils 9.12's printenv quotes a value with a blank on a terminal.
+ const systemd = try run(a, c, term, "printf \"%s\\n\" \"$SYSTEMD_PAGER\" \"$SYSTEMD_PAGERSECURE\"\n");
+ try expect(std.mem.startsWith(u8, systemd, "exit 0\n") and std.mem.endsWith(u8, std.mem.trimEnd(u8, systemd, " \n"), " -\n0"));
+ if (shell(a, "command -v journalctl >/dev/null", .{}) == 0) {
+ try expect(std.mem.startsWith(u8, try run(a, c, term, "journalctl -n 3 --no-hostname\n"), "exit "));
+ for (try panesAt(a, c, f.at("{s}/+Pager", .{root}))) |p| try c.remove(f.at("/pane/{s}", .{p}));
+ }
+ // A pty is not given one row (it loses the prompt's input mark, and the
+ // shell would read busy for ever): refused, and said.
+ const ptyctl = f.at("/pane/{d}/pty/ctl", .{term});
+ try refusedSaying(c, ptyctl, "winsize 20 1\n", "at least 2 rows");
+ try c.write(ptyctl, "winsize 20 2\n");
+ process.sleep(500);
+ try expectEqualStrings("exit 0\n", try run(a, c, term, "true\n"));
+ var words = std.mem.tokenizeAny(u8, try c.read(a, ptyctl), " \n");
+ _ = words.next();
+ _ = words.next();
+ try expectEqualStrings("2", words.next() orelse ""); // not `.?`: a panic leaves the session running
+ try c.write(ptyctl, "winsize 80 24\n");
+ // A shell started again that cannot start (its directory is there but
+ // cannot be entered) fails the write and leaves the running shell be:
+ // runs still work.
+ const locked = try std.fmt.allocPrintSentinel(a, "{s}/locked", .{root}, 0);
+ try expectEqual(@as(c_int, 0), libc.mkdir(locked, 0o755));
+ try expectEqualStrings("exit 0\n", try run(a, c, term, f.at("cd {s}\n", .{locked})));
+ const moved_by = eh.nowMs() + 5_000;
+ while (!has(try c.read(a, "/index"), locked)) {
+ if (eh.nowMs() > moved_by) return error.NeverMoved;
+ process.sleep(50);
+ }
+ _ = libc.chmod(locked, 0o600);
+ const refused = if (libc.geteuid() != 0) refusedSaying(c, ptyctl, "exec\n", "shell") else {}; // root enters any directory
+ _ = libc.chmod(locked, 0o755);
+ try refused;
+ try expectEqualStrings("exit 0\n", try run(a, c, term, "true\n"));
+ try expectEqualStrings("exit 0\n", try run(a, c, term, f.at("cd {s}\n", .{root})));
+ const other = try process.Conn.dial(gpa, d.socket());
+ defer other.deinit(gpa);
+ const tv: libc.timeval = .{ .sec = 10, .usec = 0 };
+ _ = libc.setsockopt(other.fd, libc.SOL.SOCKET, libc.SO.RCVTIMEO, &tv, @sizeOf(libc.timeval));
+ var slow: Other = .{ .a = a, .conn = other, .serial = term, .line = "sleep 1\n" };
+ var waiter = try std.Thread.spawn(.{}, Other.go, .{&slow});
+ process.sleep(300);
+ // The job the other client ran holds the tty, and says so.
+ const busy = run(a, c, term, "true\n");
+ waiter.join();
+ try expectEqualStrings("busy: sleep is running\n", try busy);
+ try expectEqualStrings("exit 0\n", slow.got);
+ var closing: Other = .{ .a = a, .conn = other, .serial = term, .line = "sleep 30\n" };
+ waiter = try std.Thread.spawn(.{}, Other.go, .{&closing});
+ process.sleep(300);
+ const del = c.write(f.at("/pane/{d}/exec", .{term}), "Del\n");
+ waiter.join();
+ try del;
+ try expectEqualStrings("error shell gone\n", closing.got);
+}
+
+fn namedOnce(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "names", .{});
+ 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();
+ try expectEqual(@as(c_int, 0), libc.mkdir(try std.fmt.allocPrintSentinel(a, "{s}/sub", .{root}, 0), 0o755));
+ // A look at a directory starts a terminal there (DirLook terminal);
+ // DirLook pane, the default, lists it in a pane.
+ try c.write("/ctl", "DirLook terminal\n");
+ var made: std.ArrayList(u32) = .empty;
+ try made.append(a, try c.look(a, f.at("{s}/sub", .{root}), null));
+ const before = try serials(a, c);
+ try c.write("/pane/1/ctl", "Tty\n");
+ if (shell(a, "command -v fish >/dev/null", .{}) == 0) try c.write("/pane/1/ctl", "Tty fish\n");
+ for (try serials(a, c)) |s| if (std.mem.indexOfScalar(u32, before, s) == null) try made.append(a, s);
+ try expect(made.items.len >= 2);
+ process.sleep(1500); // their shells start and say where they are
+ // A shell started again by pty/ctl exec: named as it was.
+ try c.write(f.at("/pane/{d}/pty/ctl", .{made.items[0]}), "exec\n");
+ process.sleep(1500);
+ const log = f.at("\n{s}", .{try c.read(a, "/log")});
+ for (made.items) |s| {
+ try expect(!has(log, f.at("\nrename {d} ", .{s})));
+ try expect(has(log, f.at("\nnew {d} {s}", .{ s, root })));
+ }
+ // A shell that cannot start (its interpreter is not there): Tty fails
+ // the write with why and leaves no pane; pty/ctl exec fails with why
+ // and keeps its terminal.
+ try d.put("badsh", "#!/nonexistent/interp\n");
+ const bad = f.at("{s}/badsh", .{root});
+ _ = libc.chmod(try std.fmt.allocPrintSentinel(a, "{s}", .{bad}, 0), 0o755);
+ const panes = std.mem.count(u8, try c.read(a, "/index"), "\n");
+ try refusedSaying(c, "/pane/1/ctl", f.at("Tty {s}\n", .{bad}), "interpreter /nonexistent/interp not found");
+ try refusedSaying(c, "/ctl", f.at("Shell {s}\n", .{bad}), "interpreter /nonexistent/interp not found");
+ process.sleep(300);
+ try expectEqual(panes, std.mem.count(u8, try c.read(a, "/index"), "\n"));
+ try expect(!has(try c.read(a, "/log"), "shell: shell not found"));
+ try expect(std.mem.indexOfScalar(u32, try serials(a, c), made.items[0]) != null);
+}
+
const ready = "AGENT_READY> ";
fn prompt(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
@@ -53,11 +284,11 @@ fn drive(gpa: std.mem.Allocator, exe: [:0]const u8, script: []const u8, key: ?u8
_ = try settled(f);
}
const deadline = eh.nowMs() + 10_000;
- while (lines(try f.read(body)) < rows) {
+ while (lineCount(try f.read(body)) < rows) {
if (eh.nowMs() > deadline) return error.ShortHistory;
sleepMs(50);
}
- if (rows == 0) try std.testing.expect(lines(try f.read(body)) < 1000);
+ if (rows == 0) try std.testing.expect(lineCount(try f.read(body)) < 1000);
const before = try settled(f);
const probe = "pq7c0ffee2b9d";
try std.testing.expect(std.mem.indexOf(u8, before, probe) == null);
@@ -111,7 +342,7 @@ fn unwrapped(f: *process.Files) ![]const u8 {
return out[0..n];
}
-fn lines(text: []const u8) usize {
+fn lineCount(text: []const u8) usize {
return std.mem.count(u8, text, "\n") + @intFromBool(text.len > 0 and text[text.len - 1] != '\n');
}
diff --git a/test/process/tree.zig b/test/process/tree.zig
new file mode 100644
index 00000000..358e7040
--- /dev/null
+++ b/test/process/tree.zig
@@ -0,0 +1,636 @@
+//! docs/typ/reference.typ, The tree <tree>, through a real session's socket.
+const std = @import("std");
+const libc = std.c;
+const process = @import("../process.zig");
+const c9 = @import("cloud9");
+const eh = @import("../e2e_harness.zig");
+const expect = std.testing.expect;
+const expectEqual = std.testing.expectEqual;
+const expectEqualStrings = std.testing.expectEqualStrings;
+const Conn = process.Conn;
+const has = process.has;
+const check = process.check;
+
+pub const cases = .{
+ .{ "tree: listing, stat and find are inert; new, remove, look, exec, name, sel, log, ctl lock, focus, the ctl split, commands, Save and the slots behave", discovery },
+ .{ "tree: a write bigger than a 9P message runs each line once, whole, an Edit block once it ends; a detached session is 160x50 until told", cutWrites },
+ .{ "tree: the root lists its files and not self; /os reads the disk, `..` taken as a walk takes it, and writes it truncated", rootAndOs },
+ .{ "tree: a client of 256-byte frames lists /os whole, a dangling link left out; one of 64 KiB is agreed", frames },
+ .{ "tree: /screen freezes at its open; a dozen connections in turn each read a body", frozenScreen },
+ .{ "tree: a Look at a relative name finds the disk's file first; /n/self is the session's tree, /n/os the disk", looks },
+ .{ "tree: names up to 255 bytes list, read, write and open through /os and /n/os, and a directory lists them", longNames },
+};
+
+fn contains(names: []const []const u8, name: []const u8) bool {
+ for (names) |n| if (std.mem.eql(u8, n, name)) return true;
+ return false;
+}
+
+/// A write refused, its words holding `why`.
+fn refusedSaying(c: *process.Conn, path: []const u8, data: []const u8, why: []const u8) !void {
+ try std.testing.expectError(error.Refused, c.write(path, data));
+ if (!has(c.said, why)) {
+ std.debug.print("{s} refused saying {s}, not {s}\n", .{ path, c.said, why });
+ return error.WrongWords;
+ }
+}
+
+/// A read on `c` given up after `sec` seconds, so a wait that never ends fails.
+fn timeout(c: *process.Conn, sec: i64) void {
+ const tv: libc.timeval = .{ .sec = sec, .usec = 0 };
+ _ = libc.setsockopt(c.fd, libc.SOL.SOCKET, libc.SO.RCVTIMEO, &tv, @sizeOf(libc.timeval));
+}
+
+/// /index's row for `serial`.
+fn indexRow(index: []const u8, serial: u32) ![]const u8 {
+ var rows = std.mem.tokenizeScalar(u8, index, '\n');
+ while (rows.next()) |row| {
+ const sp = std.mem.indexOfScalar(u8, row, ' ') orelse continue;
+ if ((std.fmt.parseInt(u32, row[0..sp], 10) catch continue) == serial) return row;
+ }
+ return error.NoRow;
+}
+
+/// The serials of /index's rows whose fourth word, the path, is `path`.
+fn panesAt(a: std.mem.Allocator, c: *process.Conn, path: []const u8) ![]const []const u8 {
+ var out: std.ArrayList([]const u8) = .empty;
+ var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
+ while (rows.next()) |row| {
+ var w = std.mem.tokenizeScalar(u8, row, ' ');
+ const serial = w.next().?;
+ _ = w.next();
+ _ = w.next();
+ if (std.mem.eql(u8, w.next() orelse "", path)) try out.append(a, serial);
+ }
+ return out.items;
+}
+
+const Seen = struct { path: []const u8, mode: u32, mtime: u32 };
+
+/// Every entry below `path` stated, as `find` does, without opening one.
+fn walkTree(a: std.mem.Allocator, c: *process.Conn, path: []const u8, seen: *std.ArrayList(Seen)) !void {
+ for (try c.list(a, path)) |name| {
+ const child = try std.fmt.allocPrint(a, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), name });
+ const st = try c.stat(child);
+ try seen.append(a, .{ .path = child, .mode = st.mode, .mtime = st.mtime });
+ if (st.mode & c9.dmdir != 0 and !contains(&.{ "/log", "/os", "/screen" }, child)) try walkTree(a, c, child, seen);
+ }
+}
+
+/// A follower's read on a held /log, which waits for a new record.
+const Follower = struct {
+ conn: *process.Conn,
+ fid: u32,
+ off: u64,
+ buf: [256]u8 = undefined,
+ len: usize = 0,
+
+ fn run(w: *Follower) void {
+ const got = w.conn.readFid(w.fid, w.off, 4096) catch return;
+ w.len = @min(got.len, w.buf.len);
+ @memcpy(w.buf[0..w.len], got[0..w.len]);
+ }
+};
+
+fn discovery(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "discovery", .{});
+ 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 top = try c.list(a, "/");
+ // A set: which files the root holds, not the order a listing gives.
+ const want = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "pane", "col", "os" };
+ var others: usize = 0;
+ for (top) |n| {
+ if (contains(&.{ "src", "shaders" }, n)) continue;
+ others += 1;
+ try expect(contains(&want, n));
+ }
+ try expectEqual(want.len, others);
+ for (want) |n| try expect(contains(top, n));
+ const embedded = contains(top, "src");
+ const before = try c.read(a, "/index");
+ const guide = try c.read(a, "/README");
+ try expect(std.mem.count(u8, guide, "\n") <= 45 and has(guide, "Msg hello") and has(guide, "pane/new"));
+ try expectEqual(guide.len, (try c.stat("/README")).length);
+ // ls/stat/find over the whole tree, without opening, creates nothing.
+ var seen: std.ArrayList(Seen) = .empty;
+ try walkTree(a, c, "/", &seen);
+ var names: std.ArrayList([]const u8) = .empty;
+ for (seen.items) |s| try names.append(a, s.path);
+ for ([_][]const u8{ "/index", "/status", "/look", "/exec", "/log", "/pane/1", "/pane/1/body", "/pane/1/name", "/pane/1/sel", "/pane/1/ctl", "/pane/1/tag", "/pane/1/event", "/pane/1/dot", "/pane/1/limit", "/pane/1/dirty", "/pane/1/mark", "/pane/1/scroll", "/pane/1/look", "/pane/1/exec" }) |n| try expect(contains(names.items, n));
+ try expect(!contains(names.items, "/pane/1/pty")); // the fixture file pane grew no pty/
+ for (seen.items) |s| {
+ try expect(!std.mem.startsWith(u8, s.path, "/self") and !std.mem.endsWith(u8, s.path, "/rdsel") and !std.mem.endsWith(u8, s.path, "/wrsel"));
+ const mode = s.mode & 0o777;
+ try expect(mode == 0o755 or mode == 0o644 or mode == 0o666 or mode == 0o444 or mode == 0o222 or mode == 0o555);
+ try expect(s.mtime > 0);
+ }
+ try expectEqualStrings(before, try c.read(a, "/index")); // browsing created no pane
+ try expect(before.len > 0);
+ try expectEqual(@as(u64, 0), (try c.stat("/index")).length); // a view made by each read stats 0 (reference.typ, Stats)
+ try expectEqual(@as(u32, 0o444), (try c.stat("/index")).mode & 0o777);
+ try expectEqual(@as(u32, 0o444), (try c.stat("/status")).mode & 0o777);
+ try expectEqual(@as(u32, 0o666), (try c.stat("/exec")).mode & 0o777);
+ try expectEqual(@as(u32, 0o666), (try c.stat("/pane/1/body")).mode & 0o777);
+ try expectEqual(@as(u32, 0o755), (try c.stat("/pane")).mode & 0o777);
+ // A range file reads the pair it takes; a flag file reads 0 or 1.
+ try expectEqual(@as(u64, 24), (try c.stat("/pane/1/addr")).length);
+ try expectEqual(@as(u64, 0), (try c.stat("/pane/1/limit")).length);
+ try expectEqualStrings("0\n", try c.read(a, "/pane/1/dirty"));
+ try expectEqualStrings("1\n", try c.read(a, "/pane/1/scroll"));
+ const fixture = try std.fmt.parseInt(u32, before[0..std.mem.indexOfScalar(u8, before, ' ').?], 10);
+ try expect(std.mem.startsWith(u8, before, f.at("{d} text 0 ", .{fixture})));
+ try expect(std.mem.endsWith(u8, before[0..std.mem.lastIndexOfScalar(u8, std.mem.trimEnd(u8, before, "\n"), ' ').?], "/discovery.txt"));
+ // /status reports the editor; /exec has nothing to report yet.
+ var pid: i64 = 0;
+ var version = false;
+ var panes: i64 = 0;
+ var lines = std.mem.tokenizeScalar(u8, try c.read(a, "/status"), '\n');
+ while (lines.next()) |line| {
+ const sp = std.mem.indexOfScalar(u8, line, ' ') orelse continue;
+ const key, const value = .{ line[0..sp], std.mem.trim(u8, line[sp..], " ") };
+ if (std.mem.eql(u8, key, "pid")) pid = try std.fmt.parseInt(i64, value, 10);
+ if (std.mem.eql(u8, key, "version")) version = value.len > 0;
+ if (std.mem.eql(u8, key, "panes")) panes = try std.fmt.parseInt(i64, value, 10);
+ }
+ try expect(pid > 0 and version and panes >= 1);
+ try expectEqualStrings("", try c.read(a, "/exec"));
+ // An open of /pane/new makes a pane and the read names it; each open
+ // makes another. A stat makes none, which is why new can be listed at
+ // all: ls -l stats every name a listing gave it. /log answers the
+ // recent past and ends; `follow` written to the same open makes its
+ // reads past that wait for new records.
+ const log = try c.hold("/log", 2);
+ const frozen = (try c.readFid(log, 0, 1 << 16)).len;
+ _ = try c.writeFid(log, 0, "follow\n");
+ const first = try c.newPane();
+ try expectEqual(first, try c.newest(a));
+ const second = try c.newPane();
+ try expect(first != second and first != fixture);
+ try expect(contains(try c.list(a, "/pane"), "new"));
+ _ = try c.stat("/pane/new");
+ try expectEqual(second, try c.newest(a)); // a stat of new made no pane
+ // What the editor said is in the stream too.
+ const paneEvent = struct {
+ fn next(conn: *process.Conn, al: std.mem.Allocator, fid: u32, off: u64) ![]const u8 {
+ while (true) {
+ const record = try conn.readFid(fid, off, 1 << 16);
+ if (!std.mem.startsWith(u8, record, "msg ") and !std.mem.startsWith(u8, record, "err ")) return al.dupe(u8, record);
+ }
+ }
+ }.next;
+ try expectEqualStrings(f.at("new {d} {s}/+New\n", .{ first, root }), try paneEvent(c, a, log, frozen));
+ try expectEqualStrings(f.at("new {d} {s}/+New\n", .{ second, root }), try paneEvent(c, a, log, frozen));
+ // A follower that is waiting wakes when the editor logs, not when some
+ // unrelated request next happens to wait.
+ {
+ const follower = try process.Conn.dial(gpa, d.socket());
+ defer follower.deinit(gpa);
+ timeout(follower, 5);
+ const waiting = try follower.hold("/log", 2);
+ const start = (try follower.readFid(waiting, 0, 1 << 16)).len;
+ _ = try follower.writeFid(waiting, 0, "follow\n");
+ var w: Follower = .{ .conn = follower, .fid = waiting, .off = start };
+ const reader = try std.Thread.spawn(.{}, Follower.run, .{&w});
+ process.sleep(200);
+ try c.write("/exec", "Msg woken\n");
+ reader.join();
+ try expect(std.mem.endsWith(u8, w.buf[0..w.len], " woken\n"));
+ }
+ // A pane's ctl takes acme's lock: a second open's lock fails at once
+ // with "file in use" until the holder unlocks, and nothing else waits
+ // on it meanwhile.
+ {
+ const other = try process.Conn.dial(gpa, d.socket());
+ defer other.deinit(gpa);
+ const ctl = f.at("/pane/{d}/ctl", .{first});
+ const mine = try c.hold(ctl, 2);
+ _ = try c.writeFid(mine, 0, "lock\n");
+ const theirs = try other.hold(ctl, 1);
+ try std.testing.expectError(error.Refused, other.writeFid(theirs, 0, "lock\n"));
+ try expect(has(other.said, "file in use"));
+ try c.write(f.at("/pane/{d}/addr", .{first}), "#0");
+ _ = try c.writeFid(mine, 0, "unlock\n");
+ _ = try other.writeFid(theirs, 0, "lock\n");
+ c.clunk(mine);
+ other.clunk(theirs);
+ }
+ // /focus names the pane with the keyboard and moves it, as rio's wctl
+ // `current` does; a pane's ctl line ends saying which it is.
+ try c.write("/focus", f.at("{d}\n", .{first}));
+ try expectEqualStrings(f.at("{d}\n", .{first}), try c.read(a, "/focus"));
+ try expect(has(try c.read(a, f.at("/pane/{d}/ctl", .{first})), " current "));
+ try expect(has(try c.read(a, f.at("/pane/{d}/ctl", .{second})), " notcurrent "));
+ try refusedSaying(c, "/focus", "99999\n", "no such pane");
+ // /ctl reads the settings as a write takes them and takes the session's
+ // builtins; a pane's ctl takes the pane's.
+ const settings = try c.read(a, "/ctl");
+ try expect(has(settings, "Verbose ") and !has(settings, "Platform"));
+ try c.write("/ctl", settings);
+ try expectEqualStrings(settings, try c.read(a, "/ctl"));
+ try c.write("/ctl", "Verbose off\n");
+ try expect(has(try c.read(a, "/ctl"), "Verbose off\n"));
+ try refusedSaying(c, "/ctl", "Del\n", "not a session control message \"Del\"");
+ try refusedSaying(c, "/ctl", "Nope 3\n", "unknown control message \"Nope\"");
+ try refusedSaying(c, f.at("/pane/{d}/ctl", .{first}), "Newcol\n", "not a window control message \"Newcol\"");
+ const commands = try c.read(a, "/commands");
+ const row = struct {
+ fn row(text: []const u8, start: []const u8) bool {
+ var rows = std.mem.tokenizeScalar(u8, text, '\n');
+ while (rows.next()) |r| if (std.mem.eql(u8, r, start) or (std.mem.startsWith(u8, r, start) and r.len > start.len and r[start.len] == ' ')) return true;
+ return false;
+ }
+ }.row;
+ try expect(row(commands, "Newcol root") and row(commands, "Save arg pane") and row(commands, "Edit arg both"));
+ // word, `arg` if it takes one, its ctl, a setting's values, then `-- ` what it does
+ var crows = std.mem.tokenizeScalar(u8, commands, '\n');
+ while (crows.next()) |r| {
+ var w = std.mem.tokenizeScalar(u8, r, ' ');
+ _ = w.next();
+ var which = w.next().?;
+ if (std.mem.eql(u8, which, "arg")) which = w.next().?;
+ try expect(contains(&.{ "root", "pane", "both" }, which));
+ }
+ try expect(row(commands, "Placement arg root acme,pardes"));
+ try expect(std.mem.endsWith(u8, commands, "\n"));
+ try expectEqual(commands.len, (try c.stat("/commands")).length);
+ try c.write(f.at("/pane/{d}/ctl", .{first}), "Msg said through ctl\n");
+ try expect(has(try f.screen(), "said through ctl"));
+ const listed = try c.list(a, "/pane");
+ try expectEqual(@as(usize, 4), listed.len);
+ for ([_]u32{ fixture, first, second }) |n| try expect(contains(listed, f.at("{d}", .{n})));
+ try expect(contains(listed, "new"));
+ try c.replace(f.at("/pane/{d}/body", .{first}), "first pane");
+ try expectEqualStrings("first pane", try c.read(a, f.at("/pane/{d}/body", .{first})));
+ try expectEqualStrings("", try c.read(a, f.at("/pane/{d}/body", .{second})));
+ try expectEqual(@as(u64, "first pane".len), (try c.stat(f.at("/pane/{d}/body", .{first}))).length);
+ const index = try c.read(a, "/index");
+ try expect(std.mem.startsWith(u8, try indexRow(index, first), f.at("{d} text 1 ", .{first})));
+ try expect(std.mem.startsWith(u8, try indexRow(index, second), f.at("{d} text 0 ", .{second})));
+ // name: read the file name, write to rename; sel: the selection.
+ const name = f.at("/pane/{d}/name", .{first});
+ try expectEqualStrings(f.at("{s}/+New\n", .{root}), try c.read(a, name));
+ try c.write(name, "notes.txt\n");
+ try expectEqualStrings(f.at("{s}/notes.txt\n", .{root}), try c.read(a, name));
+ try expectEqualStrings(f.at("rename {d} {s}/notes.txt\n", .{ first, root }), try paneEvent(c, a, log, frozen));
+ try expectEqual(@as(u64, f.at("{s}/notes.txt\n", .{root}).len), (try c.stat(name)).length);
+ // Copying addr onto dot selects that range; sel reads and replaces it.
+ const addr = f.at("/pane/{d}/addr", .{first});
+ const dot = f.at("/pane/{d}/dot", .{first});
+ const sel = f.at("/pane/{d}/sel", .{first});
+ try c.write(addr, "#0,#5");
+ try c.write(dot, try c.read(a, addr));
+ try expectEqualStrings(try c.read(a, addr), try c.read(a, dot));
+ try expectEqualStrings("first", try c.read(a, sel));
+ try expectEqual(@as(u64, 5), (try c.stat(sel)).length);
+ try c.write(sel, "FIRST");
+ try expectEqualStrings("FIRST pane", try c.read(a, f.at("/pane/{d}/body", .{first})));
+ try expectEqualStrings("FIRST", try c.read(a, sel)); // what a write to sel put there is selected
+ // exec Msg shows text; exec Save writes the named file; rmdir closes.
+ try c.write(f.at("/pane/{d}/exec", .{first}), "Msg hello from 9P\n");
+ try expect(has(try f.screen(), "hello from 9P"));
+ try expectEqualStrings("1\n", try c.read(a, f.at("/pane/{d}/dirty", .{first})));
+ try c.write(f.at("/pane/{d}/exec", .{first}), "Save\n");
+ try expectEqualStrings("FIRST pane", try d.get(a, "notes.txt"));
+ try expectEqualStrings(f.at("save {d} {s}/notes.txt\n", .{ first, root }), try paneEvent(c, a, log, frozen));
+ try expectEqualStrings(f.at("{d}\n", .{first}), try c.read(a, "/exec"));
+ try expectEqualStrings("0\n", try c.read(a, f.at("/pane/{d}/dirty", .{first})));
+ try c.remove(f.at("/pane/{d}", .{second}));
+ try expectEqualStrings(f.at("del {d} {s}/+New\n", .{ second, root }), try paneEvent(c, a, log, frozen));
+ try expect(!contains(try c.list(a, "/pane"), f.at("{d}", .{second})));
+ // look opens a file; the serial reads back.
+ try d.put("opened.txt", "one\ntwo\nthree\n");
+ try c.write("/look", f.at("{s}/opened.txt:2\n", .{root}));
+ const opened = try std.fmt.parseInt(u32, std.mem.trim(u8, try c.read(a, "/look"), "\n"), 10);
+ try expect(opened != fixture and opened != first and opened != second);
+ try expectEqualStrings("one\ntwo\nthree\n", try c.read(a, f.at("/pane/{d}/body", .{opened})));
+ try expectEqualStrings(f.at("{s}/opened.txt\n", .{root}), try c.read(a, f.at("/pane/{d}/name", .{opened})));
+ try expectEqualStrings(f.at("new {d} {s}/opened.txt\n", .{ opened, root }), try paneEvent(c, a, log, frozen));
+ try c.write(f.at("/pane/{d}/look", .{first}), f.at("{s}/opened.txt:3\n", .{root}));
+ try expectEqualStrings(f.at("{d}\n", .{opened}), try c.read(a, "/look")); // a repeated look targets the open pane
+ for ([_]u32{ fixture, first, opened }) |n| try expect(contains(try c.list(a, "/pane"), f.at("{d}", .{n})));
+ // Writing an event record back still performs the action it names.
+ try c.replace(f.at("/pane/{d}/body", .{first}), "Msg via event");
+ try c.write(f.at("/pane/{d}/event", .{first}), "FX0 13\n");
+ try expect(has(try f.screen(), "via event"));
+ // A command line may not carry control characters, and nothing else in
+ // the tree may be created or removed.
+ if (c.write("/exec", "Msg a\x00b")) |_| return error.Taken else |_| {}
+ if (c.write("/status", "anything\n")) |_| return error.Taken else |_| {}
+ if (c.create("/", "x", c9.dmdir | 0o755)) |_| return error.Taken else |_| {}
+ if (c.create("/pane", "one", c9.dmdir | 0o755)) |_| return error.Taken else |_| {}
+ if (c.create("/pane", "plain-file", 0o644)) |_| return error.Taken else |_| {}
+ if (c.remove("/pane/1/body")) |_| return error.Taken else |_| {}
+ if (c.remove("/index")) |_| return error.Taken else |_| {}
+ c.clunk(log);
+ if (embedded) {
+ try expect(std.mem.startsWith(u8, try c.read(a, "/src/pardes.zig"), "const std"));
+ try expectEqual(@as(u32, 0o444), (try c.stat("/src/pardes.zig")).mode & 0o777);
+ try expect(has(try c.read(a, f.at("/pane/{d}/body", .{try c.look(a, "/virtual/src/pardes.zig", null)})), "pub const Pardes"));
+ }
+ // An open of data with OTRUNC (a shell's >) deletes the addressed range
+ // only, so the write after it replaces that range.
+ const scratch = try c.newPane();
+ try c.write(f.at("/pane/{d}/body", .{scratch}), "hello world\nsecond line\n");
+ try c.write(f.at("/pane/{d}/addr", .{scratch}), "#0,#5");
+ try c.replace(f.at("/pane/{d}/data", .{scratch}), "HOWDY");
+ try expectEqualStrings("HOWDY world\nsecond line\n", try c.read(a, f.at("/pane/{d}/body", .{scratch})));
+ // sam's regexps: ^ at any line; a miss says so and leaves no address,
+ // so the data write after it refuses.
+ try c.write(f.at("/pane/{d}/addr", .{scratch}), "/^second/");
+ try expectEqualStrings(" 12 18 ", try c.read(a, f.at("/pane/{d}/addr", .{scratch})));
+ try refusedSaying(c, f.at("/pane/{d}/addr", .{scratch}), "/nowhere/", "no match for regexp");
+ try refusedSaying(c, f.at("/pane/{d}/data", .{scratch}), "LOST", "no address");
+ try expectEqualStrings("HOWDY world\nsecond line\n", try c.read(a, f.at("/pane/{d}/body", .{scratch})));
+ // Through a mount both read as EINVAL; the log says which and why.
+ try expect(has(try c.read(a, "/log"), f.at("err {d} addr: no match for regexp\n", .{scratch})));
+ try c.remove(f.at("/pane/{d}", .{scratch}));
+ // Real `diff -u` in a command pane: the terminal turns the tab before
+ // each timestamp into blanks, and a look at a changed line still opens
+ // the new file at it.
+ try d.put("diff-old.txt", "one\ntwo\nthree\n");
+ try d.put("diff-new.txt", "one\nTWO\nthree\n");
+ const exits = std.mem.count(u8, try c.read(a, "/log"), "\nexit ");
+ try c.write("/exec", "diff -u diff-old.txt diff-new.txt\n");
+ const deadline = eh.nowMs() + 10_000;
+ while (std.mem.count(u8, try c.read(a, "/log"), "\nexit ") <= exits) {
+ if (eh.nowMs() > deadline) return error.DiffNeverEnded;
+ process.sleep(50);
+ }
+ // A command's pane is kind cmd, not term: no shell to type into.
+ var ran: u32 = 0;
+ var irows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
+ while (irows.next()) |r| {
+ var w = std.mem.tokenizeScalar(u8, r, ' ');
+ const serial = w.next().?;
+ if (std.mem.eql(u8, w.next() orelse "", "cmd")) ran = try std.fmt.parseInt(u32, serial, 10);
+ }
+ const shown = try c.read(a, f.at("/pane/{d}/body", .{ran}));
+ try expect(has(shown, "+++ diff-new.txt") and !has(shown, "\t"));
+ try c.write(f.at("/pane/{d}/look", .{ran}), "+TWO\n");
+ const diffed = try panesAt(a, c, f.at("{s}/diff-new.txt", .{root}));
+ try expectEqual(@as(usize, 1), diffed.len);
+ try c.remove(f.at("/pane/{s}", .{diffed[0]}));
+ try c.remove(f.at("/pane/{d}", .{ran}));
+ // A Save makes the directories its file goes in, whatever names it: the
+ // pane's own name or Save's path. `~` is home (this session's HOME, the
+ // test's root) wherever a path is typed: Save, name, a look, with an
+ // address or not.
+ const tilde = try c.newPane();
+ try c.write(f.at("/pane/{d}/body", .{tilde}), "one\ntwo\nthree\n");
+ try c.write(f.at("/pane/{d}/ctl", .{tilde}), "Save ~/tilde-made/sub/x.txt\n");
+ try expectEqualStrings("one\ntwo\nthree\n", try d.get(a, "tilde-made/sub/x.txt"));
+ try c.write(f.at("/pane/{d}/name", .{tilde}), "~/tilde-named.txt\n");
+ try expectEqualStrings(f.at("{s}/tilde-named.txt\n", .{root}), try c.read(a, f.at("/pane/{d}/name", .{tilde})));
+ try c.remove(f.at("/pane/{d}", .{tilde}));
+ try c.write("/look", "~/tilde-made/sub/x.txt:3\n");
+ const looked = try panesAt(a, c, f.at("{s}/tilde-made/sub/x.txt", .{root}));
+ try expectEqual(@as(usize, 1), looked.len);
+ // At line 3: dot, offset 8, where the look put it (addr is a 9P client's own register).
+ try expect(std.mem.startsWith(u8, std.mem.trimStart(u8, try c.read(a, f.at("/pane/{s}/dot", .{looked[0]})), " "), "8 "));
+ try c.remove(f.at("/pane/{s}", .{looked[0]}));
+ const made_dirs = try c.newPane();
+ try c.write(f.at("/pane/{d}/body", .{made_dirs}), "deep\n");
+ try c.write(f.at("/pane/{d}/ctl", .{made_dirs}), f.at("Save {s}/save-made/a/b/x.txt\n", .{root}));
+ try expectEqualStrings("deep\n", try d.get(a, "save-made/a/b/x.txt"));
+ try c.write(f.at("/pane/{d}/name", .{made_dirs}), f.at("{s}/save-named/c/y.txt\n", .{root}));
+ try c.write(f.at("/pane/{d}/ctl", .{made_dirs}), "Save\n");
+ try expectEqualStrings("deep\n", try d.get(a, "save-named/c/y.txt"));
+ try c.remove(f.at("/pane/{d}", .{made_dirs}));
+ // A save the host cannot do (a directory it may not make) fails the
+ // write that asked for it, and the log's err record names the path and
+ // why.
+ const unsaved = try c.newPane();
+ try c.write(f.at("/pane/{d}/body", .{unsaved}), "unsaved\n");
+ try refusedSaying(c, f.at("/pane/{d}/ctl", .{unsaved}), "Save /nonexistent-pardes-dir/x.txt\n", "Save /nonexistent-pardes-dir/x.txt: permission denied"); // a directory it may not make
+ try expect(has(try c.read(a, "/log"), f.at("err {d} ctl: Save /nonexistent-pardes-dir/x.txt: ", .{unsaved})));
+ try c.remove(f.at("/pane/{d}", .{unsaved}));
+ // Sixteen connections fill the slots; the next is told why.
+ {
+ var extra: [15]*process.Conn = undefined;
+ var n: usize = 0;
+ defer for (extra[0..n]) |e| e.deinit(gpa);
+ while (n < extra.len) : (n += 1) extra[n] = try process.Conn.dial(gpa, d.socket());
+ var words: [256]u8 = undefined;
+ try expect(has(try process.Conn.dialRefusal(gpa, d.socket(), &words), "too many connections"));
+ }
+ const until = eh.nowMs() + 5_000;
+ while (!has(try c.read(a, "/log"), "err - 9p: too many connections")) {
+ if (eh.nowMs() > until) return error.NotLogged;
+ process.sleep(10);
+ }
+}
+
+fn screenSize(f: *process.Files) !struct { i64, i64 } {
+ const json = try std.json.parseFromSliceLeaky(std.json.Value, f.arena.allocator(), try f.read("/screen"), .{});
+ return .{ json.object.get("cols").?.integer, json.object.get("rows").?.integer };
+}
+
+/// A write bigger than a 9P message comes as several, cut anywhere: each
+/// command line runs once, whole, an Edit block once it ends, and a last
+/// line with no newline when the open closes. Never a fragment.
+fn cutWrites(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var d = try process.Detached.start(gpa, exe, "cut", .{});
+ 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);
+ // As test/fs.py's client cut them: an 8192-byte msize's writes.
+ const cut = 8192 - 23;
+ var burst: std.ArrayList(u8) = .empty;
+ for (0..1000) |i| try burst.print(a, "Msg burst-{d:0>4}\n", .{i});
+ try expect(burst.items.len > 8192);
+ try c.writeCut("/exec", 1, burst.items, cut);
+ const log = try c.read(a, "/log");
+ var i: usize = 0;
+ var lines = std.mem.tokenizeScalar(u8, log, '\n');
+ while (lines.next()) |line| {
+ if (!std.mem.startsWith(u8, line, "msg ") or !has(line, "burst-")) continue;
+ var w = std.mem.splitScalar(u8, line, ' ');
+ _ = w.next();
+ _ = w.next();
+ try expectEqualStrings(f.at("burst-{d:0>4}", .{i}), w.rest());
+ i += 1;
+ }
+ try expectEqual(@as(usize, 1000), i);
+ try expect(!has(log, "\nrun ") and !has(log, "exit 127"));
+ try c.write("/exec", "Msg no-newline-at-end");
+ const said = try c.read(a, "/log");
+ // An error, not `.?`: a panic would leave the session running.
+ const upto = said[0 .. std.mem.indexOf(u8, said, "no-newline-at-end") orelse return error.NeverRun];
+ try expect(has(upto[upto.len -| 40..], "msg "));
+ const serial = try c.newPane();
+ var block: std.ArrayList(u8) = .empty;
+ try block.appendSlice(a, "Edit ,a\n");
+ for (0..2500) |k| try block.print(a, "line {d:0>5} of the block\n", .{k});
+ try block.appendSlice(a, ".\n");
+ try expect(block.items.len > 50000);
+ try c.writeCut(f.at("/pane/{d}/ctl", .{serial}), 1, block.items, cut);
+ var body = std.mem.splitScalar(u8, std.mem.trimEnd(u8, try c.read(a, f.at("/pane/{d}/body", .{serial})), "\n"), '\n');
+ var k: usize = 0;
+ while (body.next()) |line| : (k += 1) try expectEqualStrings(f.at("line {d:0>5} of the block", .{k}), line);
+ try expectEqual(@as(usize, 2500), k);
+ // A detached session with no frontend is 160x50 until told.
+ const cols, const rows = try screenSize(f);
+ try expect(cols == 160 and rows == 50);
+ try c.write("/ctl", "size 132 40\n");
+ const cols2, const rows2 = try screenSize(f);
+ try expect(cols2 == 132 and rows2 == 40);
+}
+
+fn rootAndOs(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "first", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const root = d.root();
+ const names = try c.list(a, "/");
+ for ([_][]const u8{ "pane", "index", "status", "look", "exec", "log", "os" }) |want| {
+ for (names) |n| {
+ if (std.mem.eql(u8, n, want)) break;
+ } else return error.NotListed;
+ }
+ for (names) |n| try check(!std.mem.eql(u8, n, "self"), "self listed", .{});
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/body"));
+ const os_path = try std.fmt.allocPrint(a, "/os{s}/first.txt", .{root});
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, os_path));
+ const listed = try c.list(a, try std.fmt.allocPrint(a, "/os{s}", .{root}));
+ for (listed) |n| {
+ if (std.mem.eql(u8, n, "first.txt")) break;
+ } else return error.NotListed;
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/../1/body"));
+ try std.testing.expectEqualStrings("initial\n", try c.read(a, try std.fmt.allocPrint(a, "/os{s}/../{s}/first.txt", .{ root, std.fs.path.basename(root) })));
+ try c.replace(os_path, "written through 9P\n");
+ try std.testing.expectEqualStrings("written through 9P\n", try d.get(a, "first.txt"));
+ try std.testing.expectEqualStrings("written through 9P\n", try c.read(a, os_path));
+}
+
+fn frames(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "frames", .{});
+ defer d.deinit();
+ _ = std.c.mkdir(d.abs("listing"), 0o755);
+ _ = symlink(d.abs("listing/missing"), d.abs("listing/broken"));
+ for (0..24) |i| try d.put(try std.fmt.allocPrint(a, "listing/entry-{d:0>2}", .{i}), "");
+ const small = try Conn.dialSized(gpa, d.socket(), 256);
+ defer small.deinit(gpa);
+ const names = try small.list(a, try std.fmt.allocPrint(a, "/os{s}/listing", .{d.root()}));
+ try check(names.len == 24, "listed {d}: {any}", .{ names.len, names });
+ for (0..24) |i| {
+ const want = try std.fmt.allocPrint(a, "entry-{d:0>2}", .{i});
+ for (names) |n| {
+ if (std.mem.eql(u8, n, want)) break;
+ } else return error.NotListed;
+ }
+ const big = try Conn.dial(gpa, d.socket());
+ defer big.deinit(gpa);
+ try std.testing.expectEqual(@as(u32, 65536), big.msizeAgreed());
+}
+
+extern "c" fn symlink(target: [*:0]const u8, path: [*:0]const u8) c_int;
+
+fn frozenScreen(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "frozen", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const frozen = try c.hold("/screen", 0);
+ var before: std.ArrayList(u8) = .empty;
+ try before.appendSlice(a, try c.readFid(frozen, 0, 31));
+ try c.write("/pane/1/body", "screen changed\n");
+ while (true) {
+ const chunk = try c.readFid(frozen, before.items.len, 65536);
+ if (chunk.len == 0) break;
+ try before.appendSlice(a, chunk);
+ }
+ c.clunk(frozen);
+ const json = try std.json.parseFromSliceLeaky(std.json.Value, a, before.items, .{});
+ var old: std.ArrayList(u8) = .empty;
+ for (json.object.get("cells").?.array.items) |cell| try old.appendSlice(a, cell.array.items[0].string);
+ try check(has(old.items, "initial") and !has(old.items, "screen changed"), "the open's screen moved", .{});
+ try check(has(try d.files.screen(), "screen changed"), "a new open shows the old screen", .{});
+ const body = "initial\nscreen changed\n";
+ for (0..12) |i| {
+ const other = try Conn.dial(gpa, d.socket());
+ defer other.deinit(gpa);
+ try check(std.mem.eql(u8, try other.read(a, "/pane/1/body"), body), "connection {d}", .{i});
+ }
+}
+
+fn looks(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "first", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ // A build with its sources (-Dembed-sources) serves them under /src.
+ for (try c.list(a, "/")) |n| if (std.mem.eql(u8, n, "src")) {
+ const source = try c.look(a, "/virtual/src/pardes.zig", null);
+ try check(has(try c.pane(a, source, "body"), "pub const Pardes"), "/virtual/src", .{});
+ };
+ try d.put("index", "OS takes precedence\n");
+ var opened = try c.look(a, "index", null);
+ try std.testing.expectEqualStrings("OS takes precedence\n", try c.pane(a, opened, "body"));
+ opened = try c.look(a, "/n/self/index", null);
+ const index = try c.pane(a, opened, "body");
+ try check(std.mem.startsWith(u8, index, "1 ") and has(index, "first.txt"), "/n/self/index: {s}", .{index});
+ try d.put("explicit.txt", "explicit OS mount\n");
+ opened = try c.look(a, try std.fmt.allocPrint(a, "/n/os{s}", .{d.abs("explicit.txt")}), null);
+ try std.testing.expectEqualStrings("explicit OS mount\n", try c.pane(a, opened, "body"));
+}
+
+fn longNames(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var d = try process.Detached.start(gpa, exe, "long-names", .{});
+ defer d.deinit();
+ const c = try Conn.dial(gpa, d.socket());
+ defer c.deinit(gpa);
+ const dir_rel = try a.alloc(u8, 64);
+ @memcpy(dir_rel[0..10], "directory-");
+ @memset(dir_rel[10..], 'd');
+ _ = std.c.mkdir(d.abs(dir_rel), 0o755);
+ const dir = d.abs(dir_rel);
+ for ([_]usize{ 29, 63, 127, 255 }) |length| {
+ const name = try a.alloc(u8, length);
+ @memset(name[0 .. length - 4], 'f');
+ @memcpy(name[length - 4 ..], ".txt");
+ const rel = try std.fmt.allocPrint(a, "{s}/{s}", .{ dir_rel, name });
+ try d.put(rel, "long filename\n");
+ const wire = try std.fmt.allocPrint(a, "/os{s}", .{d.abs(rel)});
+ const listed = try c.list(a, try std.fmt.allocPrint(a, "/os{s}", .{dir}));
+ for (listed) |n| {
+ if (std.mem.eql(u8, n, name)) break;
+ } else return error.NotListed;
+ try std.testing.expectEqualStrings("long filename\n", try c.read(a, wire));
+ try c.replace(wire, "written through long filename\n");
+ try std.testing.expectEqualStrings("written through long filename\n", try d.get(a, rel));
+ const opened = try c.look(a, try std.fmt.allocPrint(a, "/n/os{s}", .{d.abs(rel)}), null);
+ try std.testing.expectEqualStrings("written through long filename\n", try c.pane(a, opened, "body"));
+ try c.removePane(opened); // a long name's tag takes rows
+ }
+ const opened = try c.look(a, try std.fmt.allocPrint(a, "/n/os{s}", .{dir}), null);
+ const listing = try c.pane(a, opened, "body");
+ for (try process.entries(a, dir)) |n| try check(has(listing, try std.fmt.allocPrint(a, "/n/os{s}/{s}", .{ dir, n })), "{s} not listed", .{n});
+}
diff --git a/test/process_test.zig b/test/process_test.zig
index 5f2fcb83..2f1b5791 100644
--- a/test/process_test.zig
+++ b/test/process_test.zig
@@ -6,8 +6,16 @@
const std = @import("std");
const files = .{
+ @import("process/builtins.zig"),
+ @import("process/edit.zig"),
+ @import("process/flags.zig"),
+ @import("process/look_and_exec.zig"),
+ @import("process/mounts.zig"),
@import("process/other_files.zig"),
+ @import("process/repls.zig"),
+ @import("process/root_ctl.zig"),
@import("process/terminals.zig"),
+ @import("process/tree.zig"),
};
pub fn main(init: std.process.Init) !void {
@@ -25,6 +33,7 @@ pub fn main(init: std.process.Init) !void {
std.debug.print("PASS {s}\n", .{case[0]});
} else |err| {
failed += 1;
+ if (@errorReturnTrace()) |t| std.debug.dumpErrorReturnTrace(t);
std.debug.print("FAIL {s}: {t}\n", .{ case[0], err });
}
}
diff --git a/test/v9fs.py b/test/v9fs.py
deleted file mode 100644
index 30efe9fb..00000000
--- a/test/v9fs.py
+++ /dev/null
@@ -1,196 +0,0 @@
-#!/usr/bin/env python3
-"""Opt-in Linux kernel-mount probe; the editor always runs unprivileged.
-
-Run after `sudo -v` with a native Pardes binary and the runtime v9fs helper.
-The helper alone runs through sudo, creates a private mount namespace, mounts
-9P, drops privileges, and execs this file's worker. No FUSE or global mount.
-"""
-import argparse
-import json
-import os
-import pwd
-from pathlib import Path
-import subprocess
-import sys
-import tempfile
-import time
-import traceback
-
-from fs import session
-from ninep import Client
-
-
-def write_existing(path, data, *, truncate=False):
- flags = os.O_WRONLY | (os.O_TRUNC if truncate else 0)
- fd = os.open(path, flags)
- try:
- assert os.write(fd, data) == len(data), str(path)
- finally:
- os.close(fd)
-
-
-def worker(mountpoint, socket, uid, gid, original_namespace):
- assert os.getresuid() == (uid, uid, uid), os.getresuid()
- assert os.getresgid() == (gid, gid, gid), os.getresgid()
- assert set(os.getgroups()) == set(os.getgrouplist(pwd.getpwuid(uid).pw_name, gid)), os.getgroups()
- assert os.readlink('/proc/self/ns/mnt') != original_namespace
- status = dict(line.split(':', 1) for line in Path('/proc/self/status').read_text().splitlines())
- for field in ('CapEff', 'CapPrm', 'CapAmb'):
- assert int(status[field].strip(), 16) == 0, (field, status[field])
- entries = Path('/proc/self/mountinfo').read_text().splitlines()
- mounted = [line for line in entries if line.split()[4] == str(mountpoint)]
- assert len(mounted) == 1 and ' - 9p ' in mounted[0], mounted
- assert not any(field.startswith(('shared:', 'master:')) for field in mounted[0].split()[6:])
-
- tree = mountpoint
- assert {'os', 'index', 'pane', 'status', 'look', 'exec', 'log', 'screen', 'README'} <= set(os.listdir(tree))
- assert 'self' not in os.listdir(tree) and 'new' not in os.listdir(tree)
- assert 'new' not in os.listdir(tree / 'pane'), 'a listing would make a pane per stat'
- # A direct connection provides independent evidence for VFS reads/writes.
- with Client(socket) as client:
- assert (tree / 'index').read_bytes() == client.read('/index')
- assert (tree / 'pane/1/body').read_bytes() == b'initial\n'
-
- before = client.read('/index')
- subprocess.run(['ls', '-l', str(tree), str(tree / 'pane' / '1')], check=True, capture_output=True, timeout=5)
- subprocess.run(['find', str(tree / 'pane'), '-ls'], check=True, capture_output=True, timeout=5)
- assert (tree / 'README').read_bytes() == client.read('/README')
- assert client.read('/index') == before, 'browsing created panes'
- # Opening pane/new makes a pane and the read names it, so no trip
- # through the index. Whether a repeated path reaches the server at all
- # is the kernel's dentry cache's business, so the second pane comes
- # over the wire, where the open is exact.
- def serials():
- return {int(row.split()[0]) for row in (tree / 'index').read_bytes().splitlines()}
-
- def mkpane():
- known = serials()
- made = int((tree / 'pane' / 'new').read_bytes())
- assert made not in known, 'the open of new made no pane'
- return made
-
- serial = mkpane()
- another = int(client.read('/pane/new'))
- assert serial != another, 'a second open of new reused a pane'
- (tree / 'pane' / str(another)).rmdir()
- assert another not in serials()
- pane = tree / 'pane' / str(serial)
- wire = f'/pane/{serial}'
- assert str(serial) in os.listdir(tree / 'pane')
- assert serial in [int(row.split()[0]) for row in (tree / 'index').read_bytes().splitlines()]
-
- write_existing(pane / 'body', b'kernel body\n', truncate=True)
- assert client.read(wire + '/body') == b'kernel body\n'
- # Reopen must observe changes made through another 9P connection.
- client.write(wire + '/body', b'wire update\n', truncate=True)
- assert (pane / 'body').read_bytes() == b'wire update\n'
- write_existing(pane / 'body', b'appended\n')
- assert client.read(wire + '/body') == b'wire update\nappended\n'
- write_existing(pane / 'addr', b'#0,#4')
- write_existing(pane / 'data', b'v9fs')
- assert client.read(wire + '/body') == b'v9fs update\nappended\n'
-
- # Exercise an actual shell redirection, including its O_TRUNC open.
- subprocess.run(['/bin/sh', '-c', 'printf "kernel-probe\\n" > "$1/name"',
- 'v9fs-probe', str(pane)], check=True, timeout=5)
- tag = client.read(wire + '/tag')
- assert tag.split(maxsplit=1)[0] == str(socket.parent / 'kernel-probe').encode(), tag
- # Children inherit this single mount and can use ordinary tools.
- copied = subprocess.run(['/bin/cat', str(pane / 'body')],
- check=True, capture_output=True, timeout=5).stdout
- assert copied == b'v9fs update\nappended\n'
-
- command = b'Msg kernel-v9fs-ready'
- write_existing(pane / 'body', command, truncate=True)
- write_existing(pane / 'event', f'MX0 {len(command)}\n'.encode())
- # Event writes acknowledge dispatch, so reopen screen until rendered.
- deadline = time.monotonic() + 5
- while True:
- screen = json.loads((tree / 'screen').read_bytes())
- if 'kernel-v9fs-ready' in ''.join(cell[0] for cell in screen['cells']):
- break
- assert time.monotonic() < deadline, 'Exec result was not rendered'
- time.sleep(.01)
-
- # Reading OS files through the exported tree does not recurse through
- # the mount: the core still lives in the supervisor's namespace.
- assert (mountpoint / 'os' / str(socket.parent).lstrip('/') / 'kernel.txt').read_bytes() == b'initial\n'
- (tree / 'pane' / str(serial)).rmdir()
- assert serial not in [int(row.split()[0]) for row in (tree / 'index').read_bytes().splitlines()]
-
- print('v9fs: namespace isolation, privilege drop, inherited mount, directory refresh, '
- 'text edits, control writes, Exec and screen checks passed', flush=True)
-
-
-def run_helper(command, timeout=30):
- # Keep the caller's controlling terminal: sudo credentials are commonly
- # scoped to it. setsid/start_new_session makes an earlier sudo -v useless.
- # The elevated helper itself creates the separate *mount* namespace.
- child = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
- text=True)
- try:
- stdout, stderr = child.communicate(timeout=timeout)
- except subprocess.TimeoutExpired:
- # sudo forwards signals to its command. Keep the server alive while
- # stopping the mount user, then let session() clean up.
- child.terminate()
- try:
- child.communicate(timeout=5)
- except subprocess.TimeoutExpired:
- child.kill()
- child.communicate(timeout=5)
- raise RuntimeError('kernel probe timed out; no compatibility result')
- if child.returncode:
- raise RuntimeError(f'kernel probe failed ({child.returncode})\n{stdout}{stderr}')
- return stdout
-
-
-def run(binary, helper):
- if sys.platform != 'linux':
- raise RuntimeError('this probe requires Linux v9fs')
- if os.getuid() == 0:
- raise RuntimeError('run the driver as your normal user; only the mount helper uses sudo')
- # Never prompt from a build step. An unavailable prerequisite is a failure,
- # not a skipped test that might be mistaken for mounted-filesystem coverage.
- available = subprocess.run(['sudo', '-n', '-v'], capture_output=True, text=True, timeout=5)
- if available.returncode:
- raise RuntimeError('mount authorization unavailable: run sudo -v in your terminal, then retry\n'
- + available.stderr.strip())
- namespace = os.readlink('/proc/self/ns/mnt')
- with tempfile.TemporaryDirectory(prefix='pardes-v9fs-') as directory:
- root = Path(directory)
- target = root / 'mount'
- target.mkdir()
- with session(str(binary), root, 'kernel') as (client, address):
- command = ['sudo', '-n', '--', str(helper), str(address), str(target),
- str(os.getuid()), str(os.getgid()), '--', sys.executable, '-B',
- str(Path(__file__).resolve()), '--worker', str(target), str(address),
- str(os.getuid()), str(os.getgid()), namespace]
- print(run_helper(command), end='')
- assert os.readlink('/proc/self/ns/mnt') == namespace
- assert list(target.iterdir()) == [], 'mount escaped its private namespace'
- assert client.read('/pane/1/body') == b'initial\n', 'core stopped serving after probe exit'
- print('v9fs: supervisor namespace unchanged and session cleanup passed')
-
-
-def main():
- if len(sys.argv) > 1 and sys.argv[1] == '--worker':
- if len(sys.argv) != 7:
- raise RuntimeError('invalid internal worker arguments')
- worker(Path(sys.argv[2]), Path(sys.argv[3]), int(sys.argv[4]), int(sys.argv[5]), sys.argv[6])
- return
- parser = argparse.ArgumentParser(description=__doc__)
- parser.add_argument('binary', type=lambda text: Path(text).resolve(strict=True))
- parser.add_argument('helper', type=lambda text: Path(text).resolve(strict=True))
- args = parser.parse_args()
- run(args.binary, args.helper)
-
-
-if __name__ == '__main__':
- try:
- main()
- except (AssertionError, OSError, RuntimeError, subprocess.SubprocessError) as error:
- if len(sys.argv) > 1 and sys.argv[1] == '--worker':
- traceback.print_exc()
- print(f'v9fs: {error}', file=sys.stderr)
- sys.exit(1)
diff --git a/test/v9fs_driver_test.py b/test/v9fs_driver_test.py
deleted file mode 100644
index 9781c4fe..00000000
--- a/test/v9fs_driver_test.py
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/usr/bin/env python3
-"""Unprivileged regression checks for the v9fs helper launcher."""
-import os
-import sys
-import unittest
-
-from v9fs import run_helper
-
-
-class HelperLaunchTests(unittest.TestCase):
- def test_preserves_session_for_terminal_scoped_sudo_credentials(self):
- output = run_helper([sys.executable, '-c', 'import os; print(os.getsid(0))'])
- self.assertEqual(int(output), os.getsid(0))
-
- def test_failure_preserves_diagnostics(self):
- with self.assertRaisesRegex(RuntimeError, r'(?s)failed \(7\).*mount refused'):
- run_helper([sys.executable, '-c',
- 'import sys; print("mount refused", file=sys.stderr); sys.exit(7)'])
-
- def test_timeout_is_not_a_passing_probe(self):
- with self.assertRaisesRegex(RuntimeError, 'timed out; no compatibility result'):
- run_helper([sys.executable, '-c', 'import time; time.sleep(60)'], timeout=.1)
-
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/v9fs_terminal.py b/test/v9fs_terminal.py
deleted file mode 100644
index b49246d1..00000000
--- a/test/v9fs_terminal.py
+++ /dev/null
@@ -1,108 +0,0 @@
-#!/usr/bin/env python3
-"""Exercise Tty9p and its real launcher with a nonprivileged sudo stand-in.
-
-This checks PTY routing and lifecycle, not kernel-mount compatibility (v9fs.py).
-"""
-import json
-import os
-from pathlib import Path
-import signal
-import shutil
-import sys
-import tempfile
-import time
-
-from fs import execute, new_pane, session
-
-
-def until(probe, description):
- deadline = time.monotonic() + 8
- while time.monotonic() < deadline:
- result = probe()
- if result:
- return result
- time.sleep(.02)
- raise AssertionError(description)
-
-
-def test(binary, helper):
- with tempfile.TemporaryDirectory(prefix='pardes-v9fs-terminal-') as directory:
- root = Path(directory)
- # Exercise shell quoting with a real executable path containing both
- # whitespace and an apostrophe, under bash and fish where available.
- quoted_helper = root / "helper's quoted path"
- shutil.copy2(helper, quoted_helper)
- helper = quoted_helper
- sudo = root / 'sudo'
- # A fresh PATH in the owned fixture selects this stand-in. It never
- # invokes real sudo, mounts anything, or handles a real password.
- sudo.write_text(f'#!{sys.executable}\n' + '''
-import json, os, signal, sys, termios
-from pathlib import Path
-tty = os.open('/dev/tty', os.O_RDWR)
-assert os.isatty(tty)
-report = dict(argv=sys.argv[1:], pane=os.environ['PARDES_PANE'],
- socket=os.environ['PARDES_9P'], mount=os.environ['PARDES_MOUNT'],
- path=os.environ['PARDES_V9FS_PATH'], launcher=os.getppid(), pid=os.getpid())
-parent_status = Path(f"/proc/{report['launcher']}/status").read_text().splitlines()
-shell_pid = next(line.split()[1] for line in parent_status if line.startswith('PPid:'))
-report['shell'] = os.readlink(f'/proc/{shell_pid}/exe')
-Path(os.environ['V9FS_REPORT']).write_text(json.dumps(report))
-before = termios.tcgetattr(tty)
-hidden = termios.tcgetattr(tty)
-hidden[3] &= ~termios.ECHO
-termios.tcsetattr(tty, termios.TCSANOW, hidden)
-os.write(tty, b'V9FS_AUTH_READY: ')
-response = os.read(tty, 100)
-termios.tcsetattr(tty, termios.TCSANOW, before)
-os.write(tty, b'V9FS_INPUT_RECEIVED\\n')
-os.write(tty, b'sudo stand-in: authentication refused\\n')
-sys.exit(1)
-''')
- sudo.chmod(0o700)
- shells = [shutil.which('bash') or '/bin/sh', shutil.which('fish') or '/bin/sh']
- for shell, interrupted in zip(shells, (False, True)):
- report_path = root / ('interrupted.json' if interrupted else 'normal.json')
- path = str(root) + ':' + os.environ.get('PATH', '/usr/bin:/bin')
- name = 'interrupted' if interrupted else 'normal'
- with session(str(binary), root, name, inherited={
- 'PARDES_V9FS_HELPER': str(helper), 'PATH': path,
- 'V9FS_REPORT': str(report_path), 'SHELL': shell,
- }) as (client, address):
- control = new_pane(client, b'')
- execute(client, control, 'Shell ' + shell)
- execute(client, control, 'Tty9p')
- until(report_path.exists, 'Tty9p did not reach the sudo stand-in')
- report = json.loads(report_path.read_text())
- pane = f"/pane/{report['pane']}"
- until(lambda: b'V9FS_AUTH_READY' in client.read(pane + '/body'), 'prompt not visible')
- assert report['socket'] == str(address)
- assert report['argv'][:2] == ['-E', '--'], report
- assert report['argv'][2] == str(helper), report
- assert report['argv'][3] == str(address), report
- assert report['argv'][4] == report['mount'], report
- assert report['argv'][5:8] == [str(os.getuid()), str(os.getgid()), '--'], report
- assert report['path'] == path
- assert report['shell'] == str(Path(shell).resolve()), report
- # The detached core remains responsive during authentication.
- assert client.read('/pane/1/body') == b'initial\n'
- target = Path(report['mount'])
- assert target.is_dir() and list(target.iterdir()) == []
- if interrupted:
- os.kill(report['launcher'], signal.SIGTERM)
- else:
- client.write(pane + '/pty/data', b'probe-response\r')
- until(lambda: b'V9FS_INPUT_RECEIVED' in client.read(pane + '/body'), 'input did not reach new PTY')
- assert b'probe-response' not in client.read(pane + '/body'), 'password input was echoed'
- until(lambda: not target.exists(), 'launcher left its temporary mountpoint')
- # Failure/cancellation returns to the original interactive
- # shell, so the pane is useful and its errors remain visible.
- client.write(pane + '/pty/data', b'printf "OUTER_READY:%s\\n" "$PARDES_PANE"\r')
- expected = ('OUTER_READY:' + report['pane']).encode()
- until(lambda: expected in client.read(pane + '/body'), 'original shell did not remain usable')
- assert client.read('/pane/1/body') == b'initial\n'
- print('Tty9p: shell-first startup, quoted paths, hidden password input, failure recovery and cleanup passed')
-
-
-if __name__ == '__main__':
- test(Path(sys.argv[1]).resolve(), Path(sys.argv[2]).resolve())