summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/agent_session_test.py13
-rw-r--r--test/behaviour/columns_and_tags.zig3
-rw-r--r--test/behaviour/look_and_exec.zig42
-rw-r--r--test/fs.py869
-rw-r--r--test/monkey9p.py2010
-rw-r--r--test/monkey9p.zig1680
-rw-r--r--test/process.zig126
-rw-r--r--test/process/builtins.zig2
-rw-r--r--test/process/edit.zig139
-rw-r--r--test/process/flags.zig2
-rw-r--r--test/process/mounts.zig6
-rw-r--r--test/process/other_files.zig119
-rw-r--r--test/process/root_ctl.zig102
-rw-r--r--test/process/terminals.zig352
-rw-r--r--test/process/tree.zig478
-rw-r--r--test/process_test.zig3
-rw-r--r--test/session.zig71
-rw-r--r--test/v9fs.py196
-rw-r--r--test/v9fs_driver_test.py26
-rw-r--r--test/v9fs_terminal.py108
20 files changed, 3107 insertions, 3240 deletions
diff --git a/test/agent_session_test.py b/test/agent_session_test.py
index ba20b5a9..6dc385ab 100644
--- a/test/agent_session_test.py
+++ b/test/agent_session_test.py
@@ -71,6 +71,12 @@ def check(binary, options, command, expected, phase=None, cleanup_failure=False)
assert time.monotonic() - started < 8, 'timeout did not bound cleanup'
+# What pardes does for the driver (a prompt that settles, history past a
+# screenful, a raw-mode key, a probe and ^U) is test/process/terminals.zig;
+# what is left here is the driver's own: its arguments, its JSON, its
+# timeouts and its cleanup.
+
+
def main():
parser = argparse.ArgumentParser()
parser.add_argument('binary', type=Path)
@@ -93,16 +99,11 @@ def main():
]:
check(binary, options, argv, 2)
check(binary, ready + ['--timeout', '10'], command, 0)
- check(binary, ready + ['--timeout', '10', '--min-rows', '1000'],
- ['/bin/sh', '-c', 'printf "AGENT_READY> "; sleep .3; seq 1 3000; printf "AGENT_READY> "; read answer'], 0)
- check(binary, ready + ['--timeout', '10', '--min-rows', '1000', '--prepare-key', '0f'],
- ['/bin/sh', '-c', 'stty raw -echo; printf "AGENT_READY> "; dd bs=1 count=1 of=/dev/null 2>/dev/null; '
- 'stty sane; seq 1 3000; printf "AGENT_READY> "; read answer'], 0)
check(binary, ready + ['--timeout', '2', '--min-rows', '1000'], command, 1, 'history')
check(binary, ready + ['--timeout', '1'],
['/bin/sh', '-c', ': AGENT_READY; sleep 30'], 1, 'readiness')
check(binary, ready + ['--timeout', '10'], command, 1, 'files-cleanup', cleanup_failure=True)
- print('agent-session: 16 functional argument, interaction, timeout and cleanup checks passed')
+ print('agent-session: 14 argument, output, timeout and cleanup checks of the driver passed')
if __name__ == '__main__':
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/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/fs.py b/test/fs.py
index fa3388fe..a2b67cbc 100644
--- a/test/fs.py
+++ b/test/fs.py
@@ -4,6 +4,9 @@ import fcntl
import json
import os
import shutil
+import select
+import signal
+import socket
from pathlib import Path
import subprocess
import struct
@@ -110,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'))
@@ -123,868 +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 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)
- recent_survives_restart(binary)
- store_shared_between_windows(binary)
- print(f'9P integration: passed ({time.monotonic() - started:.3f}s)')
-
-
-
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/monkey9p.py b/test/monkey9p.py
deleted file mode 100644
index 7899f25b..00000000
--- a/test/monkey9p.py
+++ /dev/null
@@ -1,2010 +0,0 @@
-#!/usr/bin/env python3
-"""pardes 9P monkey: random 9P operations against a throwaway detached session.
-
- zig build monkey-9p -Dplatform=tty --prefix <scratch> -- [--seed N] [--steps M]
- zig build monkey-9p -Dplatform=tty --prefix <scratch> -- --replay FILE
- zig build monkey-9p -Dplatform=tty --prefix <scratch> -- --shrink FILE
- python3 test/monkey9p.py <pardes> [same flags]
-
-A run starts `pardes --detach` with its own HOME, XDG dirs and a short
-runtime dir in /tmp, speaks raw 9P to it (test/ninep.py), and for each
-step asks a GENERATOR for one concrete operation, runs it, then runs every
-INVARIANT. Everything random comes from --seed; every operation it ran is
-written to <out>/runs/seed-N/ops.jsonl, concrete but for pane and column
-references ({p3}: the 4th serial of /index, mod its length, when the op
-runs, so a shrunk sequence still names live panes), and `--replay` runs
-the same requests again without the generator. A failure writes a bug record
-(seed, step, the replayable operations since that session began) to
-<out>/bugs/, shrinks it by delta debugging over replays unless
---no-shrink, and the run carries on in a fresh session.
-
-Plug-in points: a generator is a function `(ctx, rng) -> op dict` under
-@generator(weight); an op kind is a function `(sess, op) -> Result` under
-@op_kind(name); an invariant is a function `(ctx, res) -> str | None`
-under @invariant, returning why it failed.
-
-Safety: the session never sees PARDES_*, NINE_MOUNT or NAMESPACE; every
-payload that could reach a shell is drawn from an alphabet with no path
-or shell metacharacters (shell_safe), Edit text from a grammar without
-sam's w/e/r/f/b/B/D/n/!/</>/| commands, and nothing is ever written,
-created or removed under /os or /src (the host filesystem). This process
-makes itself a child subreaper, so every shell the session starts is its
-descendant even after pardes dies, and teardown kills them all.
-"""
-import argparse
-import collections
-import ctypes
-import hashlib
-import json
-import os
-import random
-import re
-import select
-import shutil
-import signal
-import socket
-import struct
-import subprocess
-import sys
-import tempfile
-import threading
-import time
-
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
-from ninep import Client, string # noqa: E402
-
-REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
-DEFAULT_OUT = os.path.join(os.path.dirname(REPO), '.scratch', 'monkey9p')
-
-# 9P message types.
-TWALK, TOPEN, TCREATE, TREAD, TWRITE, TCLUNK, TREMOVE, TSTAT, TFLUSH = 110, 112, 114, 116, 118, 120, 122, 124, 108
-RERROR = 107
-OREAD, OWRITE, ORDWR, OTRUNC = 0, 1, 2, 16
-DMDIR = 0x80000000
-
-TIMEOUT = 5.0 # every reply, unless the op is a documented slow one
-SLOW_TIMEOUT = 30.0 # Grep, Find, Dump, Restore, Tty, huge writes
-HELD_WAIT = 0.1 # how long a read of a held file is let wait before Tflush
-MSIZE = 65536 + 24
-
-
-class Hang(Exception):
- """A 9P reply did not come within its timeout."""
-
-
-class Dropped(Exception):
- """The server closed the connection (or reset it)."""
-
-
-class Ended(Exception):
- """pardes quit cleanly: exit 0, no crash file. Closing the session's last
- pane quits it (docs/typ/reference.typ: 'closing the session's last pane quits pardes'),
- and a terminal whose shell exits closes its pane, so this can follow any
- step; the run starts a fresh session and carries on."""
-
-
-# --------------------------------------------------------------------------
-# Wire: raw 9P with per-request timeouts and Tflush for held reads.
-
-class Wire(Client):
- def __init__(self, address, msize=MSIZE):
- super().__init__(address, timeout=TIMEOUT, msize=msize)
- self.next_fid = 100
-
- def newfid(self):
- self.next_fid += 1
- return self.next_fid
-
- def _send(self, kind, payload):
- self.tag = (self.tag + 1) % 65535
- packet = struct.pack('<IBH', 7 + len(payload), kind, self.tag) + payload
- if len(packet) > self.msize:
- raise ValueError(f'harness bug: a {len(packet)}-byte request exceeds msize {self.msize}')
- try:
- self.socket.sendall(packet)
- except socket.timeout:
- raise Hang('send did not drain within the timeout')
- except OSError as why:
- raise Dropped(f'send: {why}')
- return self.tag
-
- def _recv(self, timeout):
- self.socket.settimeout(timeout)
- try:
- size, kind, tag = struct.unpack('<IBH', self.receive(7))
- body = self.receive(size - 7)
- except socket.timeout:
- raise Hang(f'no reply within {timeout:g}s')
- except (EOFError, ConnectionResetError, BrokenPipeError) as why:
- raise Dropped(str(why))
- except OSError as why:
- if isinstance(why, socket.timeout):
- raise Hang(f'no reply within {timeout:g}s')
- raise Dropped(str(why))
- return kind, tag, body
-
- def call(self, kind, payload, timeout=TIMEOUT):
- """(ok, body_or_error_text)."""
- tag = self._send(kind, payload)
- while True:
- rkind, rtag, body = self._recv(timeout)
- if rtag != tag:
- continue # a late reply to a flushed request
- if rkind == RERROR:
- n = struct.unpack_from('<H', body)[0]
- return False, body[2:2 + n].decode(errors='replace')
- if rkind != kind + 1:
- raise Dropped(f'expected reply {kind + 1}, received {rkind}')
- return True, body
-
- def call_held(self, kind, payload, wait=HELD_WAIT):
- """A request the server may hold: after `wait`, flush it. Returns
- (ok, body) for an answer, or (None, 'flushed')."""
- tag = self._send(kind, payload)
- self.socket.settimeout(None)
- ready, _, _ = select.select([self.socket], [], [], wait)
- if ready:
- rkind, rtag, body = self._recv(TIMEOUT)
- if rtag == tag:
- if rkind == RERROR:
- n = struct.unpack_from('<H', body)[0]
- return False, body[2:2 + n].decode(errors='replace')
- return True, body
- ftag = self._send(TFLUSH, struct.pack('<H', tag))
- answer = (None, 'flushed')
- while True:
- rkind, rtag, body = self._recv(TIMEOUT)
- if rtag == tag:
- if rkind == RERROR:
- n = struct.unpack_from('<H', body)[0]
- answer = (False, body[2:2 + n].decode(errors='replace'))
- else:
- answer = (True, body)
- elif rtag == ftag:
- return answer
-
- # Convenience over call().
- def walk_names(self, names, timeout=TIMEOUT):
- """(fid or None, error)."""
- fid = self.newfid()
- old = 1
- if not names:
- ok, body = self.call(TWALK, struct.pack('<IIH', old, fid, 0), timeout)
- return (fid, None) if ok else (None, body)
- for start in range(0, len(names), 16):
- part = names[start:start + 16]
- ok, body = self.call(TWALK, struct.pack('<IIH', old, fid, len(part)) +
- b''.join(string(n) for n in part), timeout)
- if not ok:
- if old != 1:
- self.call(TCLUNK, struct.pack('<I', fid))
- return None, body
- if struct.unpack_from('<H', body)[0] != len(part):
- if old != 1:
- self.call(TCLUNK, struct.pack('<I', fid))
- return None, 'walk stopped short'
- old = fid
- return fid, None
-
- def clunk(self, fid):
- return self.call(TCLUNK, struct.pack('<I', fid))
-
- def read_all(self, fid, count=None, limit=1 << 22):
- count = min(count or (self.msize - 11), self.msize - 11)
- data = bytearray()
- while len(data) < limit:
- ok, body = self.call(TREAD, struct.pack('<IQI', fid, len(data), count))
- if not ok:
- raise OSError(body)
- chunk = body[4:]
- if not chunk:
- break
- data.extend(chunk)
- return bytes(data)
-
- def read_path(self, path):
- fid, err = self.walk_names(split(path))
- if fid is None:
- raise FileNotFoundError(f'{path}: {err}')
- try:
- ok, body = self.call(TOPEN, struct.pack('<IB', fid, OREAD))
- if not ok:
- raise OSError(f'{path}: {body}')
- return self.read_all(fid)
- finally:
- self.clunk(fid)
-
- def write_path(self, path, data):
- """(ok, error) for one open, one write, one clunk."""
- fid, err = self.walk_names(split(path))
- if fid is None:
- return False, err
- try:
- ok, body = self.call(TOPEN, struct.pack('<IB', fid, OWRITE))
- if not ok:
- return False, body
- ok, body = self.call(TWRITE, struct.pack('<IQI', fid, 0, len(data)) + data)
- return (True, None) if ok else (False, body)
- finally:
- self.clunk(fid)
-
-
-def split(path):
- return [p.encode('latin-1') if isinstance(p, str) else p for p in path.split('/') if p]
-
-
-def dir_names(data):
- names, offset = [], 0
- while offset + 2 <= len(data):
- size = struct.unpack_from('<H', data, offset)[0]
- n = struct.unpack_from('<H', data, offset + 41)[0]
- names.append(data[offset + 43:offset + 43 + n].decode('latin-1'))
- offset += size + 2
- return names
-
-
-# --------------------------------------------------------------------------
-# Session lifecycle.
-
-def _libc():
- try:
- return ctypes.CDLL(None, use_errno=True)
- except OSError:
- return None
-
-
-def become_subreaper():
- """Orphaned shells re-parent to us, not init, so teardown finds them."""
- libc = _libc()
- if libc is not None:
- libc.prctl(36, 1, 0, 0, 0) # PR_SET_CHILD_SUBREAPER
-
-
-def descendants(root):
- parent = {}
- for entry in os.listdir('/proc'):
- if not entry.isdigit():
- continue
- try:
- with open(f'/proc/{entry}/stat', 'rb') as f:
- stat = f.read()
- ppid = int(stat[stat.rindex(b')') + 2:].split()[1])
- except (OSError, ValueError):
- continue
- parent.setdefault(ppid, []).append(int(entry))
- out, stack = [], [root]
- while stack:
- for child in parent.get(stack.pop(), []):
- out.append(child)
- stack.append(child)
- return out
-
-
-def tokened(token):
- """Our own processes whose environment carries this session's token."""
- found, needle = [], f'M9P_TOKEN={token}'.encode()
- for entry in os.listdir('/proc'):
- if not entry.isdigit():
- continue
- try:
- with open(f'/proc/{entry}/environ', 'rb') as f:
- if needle in f.read().split(b'\0'):
- found.append(int(entry))
- except OSError:
- continue
- return found
-
-
-def sweep(prefix):
- """Kill every process whose M9P_TOKEN starts with `prefix`, until none."""
- needle = f'M9P_TOKEN={prefix}'.encode()
- for _ in range(100):
- found = []
- for entry in os.listdir('/proc'):
- if not entry.isdigit() or int(entry) == os.getpid():
- continue
- try:
- with open(f'/proc/{entry}/environ', 'rb') as f:
- if any(v.startswith(needle) for v in f.read().split(b'\0')):
- found.append(int(entry))
- except OSError:
- continue
- if not found:
- return
- for pid in found:
- try:
- os.kill(pid, signal.SIGKILL)
- except ProcessLookupError:
- pass
- reap_zombies()
- time.sleep(0.02)
-
-
-def reap_zombies():
- while True:
- try:
- pid, _ = os.waitpid(-1, os.WNOHANG)
- except ChildProcessError:
- return
- if pid == 0:
- return
-
-
-RUNTIME_DIRS = [] # removed at exit, whatever ended the run
-
-
-class Session:
- """One throwaway `pardes --detach`, its dirs, its connection(s)."""
-
- counter = 0
-
- def __init__(self, binary, run_dir, follower):
- Session.counter += 1
- self.binary = binary
- self.follower_mode = follower
- self.token = f'{os.getpid()}-{Session.counter}-{random.SystemRandom().randrange(1 << 30)}'
- self.dir = os.path.join(run_dir, f'sess{Session.counter}')
- self.home = os.path.join(self.dir, 'home')
- self.runtime = tempfile.mkdtemp(prefix='m9p.', dir='/tmp')
- RUNTIME_DIRS.append(self.runtime)
- self.proc = None
- self.wire = None
- self.follower = None
- self.handles = {}
- self.dump_written = False
- self.marker = None
- self.window_base = 0
- self.start()
-
- def env(self):
- env = {k: v for k, v in os.environ.items()
- if not k.startswith('PARDES_') and k not in ('NINE_MOUNT', 'NAMESPACE')}
- env.update(HOME=self.home, XDG_RUNTIME_DIR=self.runtime,
- XDG_CONFIG_HOME=os.path.join(self.home, 'config'),
- XDG_STATE_HOME=os.path.join(self.home, 'state'),
- XDG_DATA_HOME=os.path.join(self.home, 'data'),
- XDG_CACHE_HOME=os.path.join(self.home, 'cache'),
- SHELL='/bin/sh', PARDES_NOTIME='1', M9P_TOKEN=self.token,
- PARDES_DUMP=os.path.join(self.home, 'm9p.dump.zon'))
- for language in ['RS', 'C', 'GO', 'TS', 'PY', 'ZIG']:
- env['PARDES_LSP_' + language] = ''
- return env
-
- def start(self):
- os.makedirs(self.home, exist_ok=True)
- for name, text in FIXTURES.items():
- with open(os.path.join(self.home, name), 'wb') as f:
- f.write(text)
- self.stderr = open(os.path.join(self.dir, 'stderr'), 'wb')
- self.proc = subprocess.Popen([self.binary, '--detach=m9p', 'alpha.txt'], cwd=self.home,
- env=self.env(), stdin=subprocess.DEVNULL,
- stdout=subprocess.DEVNULL, stderr=self.stderr,
- start_new_session=True)
- self.address = os.path.join(self.runtime, 'pardes-9p-m9p.sock')
- self.connect(first=True)
- # Two more panes, so one close does not end the session.
- for name in (b'utf8.txt\n', b'crlf.txt\n'):
- self.wire.write_path('/look', name)
-
- def connect(self, first=False):
- deadline = time.monotonic() + (15 if first else 10)
- last = None
- while time.monotonic() < deadline:
- if self.proc.poll() is not None:
- raise Dropped(f'pardes exited {self.proc.returncode} before its socket answered')
- if os.path.exists(self.address):
- try:
- self.wire = Wire(self.address)
- break
- except OSError as why:
- last = why
- time.sleep(0.01)
- else:
- raise Hang(f'9P socket did not answer: {last}')
- self.handles = {}
- self.marker = None
- if self.follower_mode:
- self.follower = Follower(self.address)
-
- def reconnect(self):
- self.close_wires()
- # A Restore hangs up and serves again: let the old socket go.
- time.sleep(0.05)
- self.connect()
-
- def close_wires(self):
- if self.follower is not None:
- self.follower.stop()
- self.follower = None
- if self.wire is not None:
- try:
- self.wire.socket.close()
- except OSError:
- pass
- self.wire = None
-
- def alive(self):
- return self.proc is not None and self.proc.poll() is None
-
- def crash_text(self):
- """The crash file's text (src/crash.zig) and stderr's tail."""
- out = []
- for base, _, files in os.walk(self.home):
- if 'crashes' in files:
- with open(os.path.join(base, 'crashes'), 'rb') as f:
- out.append(f.read().decode(errors='replace'))
- try:
- self.stderr.flush()
- with open(os.path.join(self.dir, 'stderr'), 'rb') as f:
- text = f.read()
- # The panic line leads; the frames under it can run long.
- tail = (text if len(text) <= 6000 else text[:3000] + b'\n...\n' + text[-2000:]).decode(errors='replace')
- if tail.strip():
- out.append('stderr: ' + tail)
- except OSError:
- pass
- return '\n'.join(out)
-
- def has_crash_file(self):
- for base, _, files in os.walk(os.path.join(self.home, 'config')):
- if 'crashes' in files:
- return True
- return False
-
- def close(self, keep=False):
- """Kill pardes and everything it started; verify nothing survives."""
- self.close_wires()
- if self.proc is not None and self.proc.poll() is None:
- self.proc.terminate()
- try:
- self.proc.wait(timeout=3)
- except subprocess.TimeoutExpired:
- self.proc.kill()
- self.proc.wait()
- survivors = []
- for _ in range(100):
- reap_zombies()
- pids = set(tokened(self.token)) | set(descendants(os.getpid()))
- pids.discard(os.getpid())
- if not pids:
- break
- for pid in pids:
- try:
- os.kill(pid, signal.SIGKILL)
- except ProcessLookupError:
- pass
- survivors = pids
- time.sleep(0.02)
- else:
- raise RuntimeError(f'processes survived teardown: {sorted(survivors)}')
- reap_zombies()
- self.stderr.close()
- shutil.rmtree(self.runtime, ignore_errors=True)
- if not keep:
- shutil.rmtree(self.dir, ignore_errors=True)
-
-
-class Follower:
- """A second connection holding `follow new` on /log, reading records."""
-
- def __init__(self, address):
- self.lines = []
- self.cond = threading.Condition()
- self.done = False
- self.wire = Wire(address)
- fid, err = self.wire.walk_names([b'log'])
- ok, body = self.wire.call(TOPEN, struct.pack('<IB', fid, ORDWR))
- ok, body = self.wire.call(TWRITE, struct.pack('<IQI', fid, 0, 11) + b'follow new\n')
- if not ok:
- raise OSError(f'follow new: {body}')
- self.fid = fid
- self.thread = threading.Thread(target=self.run, daemon=True)
- self.thread.start()
-
- def run(self):
- offset = 0
- try:
- while not self.done:
- tag = self.wire._send(TREAD, struct.pack('<IQI', self.fid, offset, 8192))
- while True:
- self.wire.socket.settimeout(None)
- kind, rtag, body = self.wire._recv(None)
- if rtag == tag:
- break
- if kind == RERROR:
- break
- chunk = body[4:]
- if not chunk:
- break
- offset += len(chunk)
- with self.cond:
- self.lines.extend(chunk.decode(errors='replace').splitlines())
- self.cond.notify_all()
- except (Hang, Dropped, OSError, struct.error):
- pass
- with self.cond:
- self.done = True
- self.cond.notify_all()
-
- def wait_for(self, needle, timeout=TIMEOUT):
- deadline = time.monotonic() + timeout
- with self.cond:
- while True:
- for i in range(len(self.lines) - 1, -1, -1):
- if needle in self.lines[i]:
- return i
- left = deadline - time.monotonic()
- if left <= 0 or self.done:
- return None
- self.cond.wait(left)
-
- def stop(self):
- self.done = True
- try:
- self.wire.socket.shutdown(socket.SHUT_RDWR)
- except OSError:
- pass
- self.wire.socket.close()
- self.thread.join(timeout=2)
-
-
-FIXTURES = {
- 'alpha.txt': b'alpha beta gamma\nsecond line\n\nthird after a blank\n',
- 'utf8.txt': 'naïve café 日本語 é \U0001f600\nline two\n'.encode(),
- 'crlf.txt': b'one\r\ntwo\r\nthree\r\n',
- 'bad.txt': b'hello \xff\xfe world\nlatin-1 caf\xe9\ntruncated \xe6\x97 here\n',
- 'empty.txt': b'',
- 'noeol.txt': b'no newline at the end',
-}
-
-
-# --------------------------------------------------------------------------
-# Results, registries.
-
-class Result:
- def __init__(self):
- self.requests = [] # (kind, path, ok, why)
- self.hung_up = False # the op expected the server to hang up (Restore)
- self.note = ''
-
- def add(self, kind, path, ok, why=None):
- self.requests.append((kind, path, ok, why))
-
- def writes(self):
- return [r for r in self.requests if r[0] in ('write', 'clunk-write', 'open-new', 'trunc')]
-
-
-GENERATORS = [] # (weight, fn)
-OPS = {} # name -> fn(sess, op) -> Result
-INVARIANTS = [] # fn(ctx, res) -> str | None
-
-
-def generator(weight):
- def wrap(fn):
- GENERATORS.append((weight, fn))
- return fn
- return wrap
-
-
-def op_kind(name):
- def wrap(fn):
- OPS[name] = fn
- return fn
- return wrap
-
-
-def invariant(fn):
- INVARIANTS.append(fn)
- return fn
-
-
-def host_path(path):
- """Guard: nothing mutating ever touches the served host tree."""
- parts = split(path)
- return bool(parts) and parts[0] in (b'os', b'src')
-
-
-def is_held(path):
- return (path.rstrip('/') == '/log' or path.endswith('/event') or
- path.endswith('/pty/data') or path.endswith('/pty/run'))
-
-
-def slow(data):
- return len(data) > 65536 or any(w in data for w in (b'Grep', b'Find', b'Dump', b'Restore', b'Tty', b'Edit'))
-
-
-# --------------------------------------------------------------------------
-# Op kinds: each takes a concrete op (what the ops log holds) and runs it.
-
-def dec(s):
- return s.encode('latin-1')
-
-
-def enc(b):
- return b.decode('latin-1')
-
-
-@op_kind('write')
-def op_write(sess, op):
- """open(path, mode); write data in the given chunk sizes; clunk."""
- res = Result()
- path, data, mode = op['path'], dec(op['data']), op.get('mode', OWRITE)
- if host_path(path):
- res.note = 'refused by guard: host tree'
- return res
- w = sess.wire
- fid, err = w.walk_names(split(path))
- if fid is None:
- res.add('walk', path, False, err)
- return res
- timeout = SLOW_TIMEOUT if slow(data) else TIMEOUT
- ok, body = w.call(TOPEN, struct.pack('<IB', fid, mode), timeout)
- res.add(open_kind(path, mode), path, ok, None if ok else body)
- sizes = chunking(op.get('chunks', 'whole'), len(data), w.msize - 23)
- opened = ok
- sent = data
- if ok:
- offset = 0
- took = []
- for size in sizes:
- piece = data[offset:offset + size]
- ok, body = w.call(TWRITE, struct.pack('<IQI', fid, op.get('offset', offset), len(piece)) + piece, timeout)
- # A write on a fid opened for reading is 9P misuse, not a write
- # the tree refused: the rule does not name it.
- res.add('write' if mode & 3 else 'write-badfid', path, ok, None if ok else body)
- offset += size
- sent = data[:offset]
- if ok:
- took.append(size)
- if not ok and op.get('stop_on_error', True):
- break
- # Misses are allowed the Twrites that succeeded: a line written a
- # byte a Twrite is a look a byte, each its own write.
- allowances(res, path, data[:sum(took)], took, w.msize - 24)
- ok, body = w.call(TCLUNK, struct.pack('<I', fid), timeout)
- res.add('clunk-write' if mode & 3 else 'clunk', path, ok, None if ok else body)
- # What the open holds when it closes: the last line written, if unended.
- if opened and mode & 3 and sent and not sent.endswith(b'\n'):
- res.add('close-runs', path, ok)
- return res
-
-
-def served_lines(data, sizes, room):
- """The lines a lines file runs for `data` written in pieces of `sizes`
- (docs/typ/reference.typ, tree.zig write): each ended line, and the unended tail of a piece
- shorter than a Twrite holds (`room`, msize less 24), which is whole in
- itself, as acme takes each write; a piece that fills its Twrite may go
- on, and its tail waits for the next, and so does one of a multiple of
- 4096 bytes, where a writer's buffer filled. A line written a byte a
- Twrite is so as many lines as bytes."""
- lines, pending, at = [], b'', 0
- for size in sizes:
- piece = data[at:at + size]
- at += size
- pending += piece
- *done, pending = pending.split(b'\n')
- lines += done
- if pending and len(piece) < room and len(piece) % 4096 != 0:
- lines.append(pending)
- pending = b''
- if pending:
- lines.append(pending)
- return lines
-
-
-def allowances(res, path, data, sizes, room):
- """Errs a successful write may log (docs/typ/reference.typ): a look that finds nothing logs
- one, per line, whether written to look, written back as an event record
- (acme's xfid.c:842) or run as the Look builtin; an Edit block whose text
- never ended runs, and fails, at the close."""
- base = path.rsplit('/', 1)[-1]
- served = served_lines(data, sizes, room)
- lines = [line for line in served if line.strip()]
- if base == 'look':
- # A look's line is its text, blanks and all: one of blanks alone
- # is a look too (docs/typ/reference.typ); only an empty one (or a lone \r) is none.
- n = len([line for line in served if line.rstrip(b'\r')])
- elif base == 'event':
- n = len(lines)
- elif base in ('exec', 'ctl', 'tagexec'):
- n = sum(1 for line in lines if line.split()[:1] == [b'Look'])
- if b'Edit' in data and len(lines) > 1:
- n += 1
- else:
- return
- if n:
- res.add('allow', path, True, n)
-
-
-def open_kind(path, mode):
- """An open the rule names: pane/new's (it makes a pane) and a truncation
- ('every write or truncation the tree refused'); others are 'open'."""
- if path.rstrip('/') == '/pane/new' and not mode & 3:
- return 'open-new'
- if mode & OTRUNC and mode & 3:
- return 'trunc'
- return 'open'
-
-
-def check_served_lines():
- assert served_lines(b'a\nb\n', [4], 8000) == [b'a', b'b']
- assert served_lines(b'ab\n', [1, 1, 1], 8000) == [b'a', b'b', b'']
- assert served_lines(b'abcd', [2, 2], 2) == [b'abcd']
- assert served_lines(b'ab\ncd', [3, 2], 8000) == [b'ab', b'cd']
-
-
-def chunking(spec, n, cap):
- if n == 0:
- return [0]
- if spec == 'bytes':
- return [1] * n
- if isinstance(spec, list):
- out, total = [], 0
- for s in spec:
- s = max(1, min(s, cap, n - total))
- out.append(s)
- total += s
- if total >= n:
- return out
- while total < n:
- s = min(cap, n - total)
- out.append(s)
- total += s
- return out
- out, total = [], 0
- while total < n:
- s = min(cap, n - total)
- out.append(s)
- total += s
- return out
-
-
-@op_kind('read')
-def op_read(sess, op):
- """open(path, OREAD); read from offset in chunks of count until EOF or cap."""
- res = Result()
- path, count, offset = op['path'], op.get('count', 8192), op.get('offset', 0)
- w = sess.wire
- fid, err = w.walk_names(split(path))
- if fid is None:
- res.add('walk', path, False, err)
- return res
- mode = op.get('mode', OREAD)
- ok, body = w.call(TOPEN, struct.pack('<IB', fid, mode))
- res.add(open_kind(path, mode), path, ok, None if ok else body)
- if ok:
- for _ in range(op.get('reads', 4)):
- payload = struct.pack('<IQI', fid, offset, min(count, w.msize - 11))
- if is_held(path):
- ok, body = w.call_held(TREAD, payload)
- else:
- ok, body = w.call(TREAD, payload)
- res.add('read', path, ok, body if ok is False else None)
- if not ok or len(body) <= 4:
- break
- offset += len(body) - 4
- ok, body = w.call(TCLUNK, struct.pack('<I', fid))
- res.add('clunk', path, ok, None if ok else body)
- return res
-
-
-@op_kind('stat')
-def op_stat(sess, op):
- res = Result()
- fid, err = sess.wire.walk_names(split(op['path']))
- res.add('walk', op['path'], fid is not None, err)
- if fid is not None:
- ok, body = sess.wire.call(TSTAT, struct.pack('<I', fid))
- res.add('stat', op['path'], ok, None if ok else body)
- sess.wire.clunk(fid)
- return res
-
-
-@op_kind('remove')
-def op_remove(sess, op):
- res = Result()
- if host_path(op['path']):
- res.note = 'refused by guard: host tree'
- return res
- fid, err = sess.wire.walk_names(split(op['path']))
- res.add('walk', op['path'], fid is not None, err)
- if fid is not None:
- ok, body = sess.wire.call(TREMOVE, struct.pack('<I', fid), SLOW_TIMEOUT)
- res.add('remove', op['path'], ok, None if ok else body)
- return res
-
-
-@op_kind('create')
-def op_create(sess, op):
- res = Result()
- if host_path(op['path']):
- res.note = 'refused by guard: host tree'
- return res
- fid, err = sess.wire.walk_names(split(op['path']))
- res.add('walk', op['path'], fid is not None, err)
- if fid is not None:
- ok, body = sess.wire.call(TCREATE, struct.pack('<I', fid) + string(dec(op['name'])) +
- struct.pack('<IB', op.get('perm', 0o666), op.get('mode', OREAD)))
- res.add('create', op['path'] + '/' + op['name'], ok, None if ok else body)
- sess.wire.clunk(fid)
- return res
-
-
-@op_kind('hold')
-def op_hold(sess, op):
- """Open and keep a fid across later ops as handle op['h']."""
- res = Result()
- path, mode = op['path'], op.get('mode', OREAD)
- if host_path(path) and mode & 3:
- res.note = 'refused by guard: host tree'
- return res
- fid, err = sess.wire.walk_names(split(path))
- if fid is None:
- res.add('walk', path, False, err)
- return res
- ok, body = sess.wire.call(TOPEN, struct.pack('<IB', fid, mode))
- res.add(open_kind(path, mode), path, ok, None if ok else body)
- if ok:
- sess.handles[op['h']] = [fid, path, mode, [0], b'']
- else:
- sess.wire.clunk(fid)
- return res
-
-
-@op_kind('hwrite')
-def op_hwrite(sess, op):
- res = Result()
- h = sess.handles.get(op['h'])
- if h is None:
- res.note = 'no such handle'
- return res
- fid, path, mode, off, _ = h
- data = dec(op['data'])[:sess.wire.msize - 23] # one Twrite
- h[4] = data
- ok, body = sess.wire.call(TWRITE, struct.pack('<IQI', fid, off[0], len(data)) + data,
- SLOW_TIMEOUT if slow(data) else TIMEOUT)
- res.add('write' if mode & 3 else 'write-badfid', path, ok, None if ok else body)
- if ok:
- allowances(res, path, data, [len(data)], sess.wire.msize - 24)
- if ok:
- off[0] += len(data)
- return res
-
-
-@op_kind('hread')
-def op_hread(sess, op):
- res = Result()
- h = sess.handles.get(op['h'])
- if h is None:
- res.note = 'no such handle'
- return res
- fid, path, _, off, _ = h
- offset = op['offset'] if op.get('offset') is not None else off[0]
- payload = struct.pack('<IQI', fid, offset, min(op.get('count', 8192), sess.wire.msize - 11))
- ok, body = sess.wire.call_held(TREAD, payload) if is_held(path) else sess.wire.call(TREAD, payload)
- res.add('read', path, ok, body if ok is False else None)
- if ok:
- off[0] = offset + len(body) - 4
- return res
-
-
-@op_kind('clunk')
-def op_clunk(sess, op):
- res = Result()
- h = sess.handles.pop(op['h'], None)
- if h is None:
- res.note = 'no such handle'
- return res
- ok, body = sess.wire.call(TCLUNK, struct.pack('<I', h[0]))
- res.add('clunk-write' if h[2] & 3 else 'clunk', h[1], ok, None if ok else body)
- if h[2] & 3 and h[4] and not h[4].endswith(b'\n'):
- res.add('close-runs', h[1], ok)
- return res
-
-
-@op_kind('restore')
-def op_restore(sess, op):
- """Write Restore [f] to /ctl: refused with an Rerror, or the server
- hangs up and serves the dump's panes again (docs/typ/reference.typ: a Restore hangs its
- connection up)."""
- res = Result()
- data = dec(op['data'])
- try:
- ok, why = sess.wire.write_path('/ctl', data)
- res.add('write', '/ctl', ok, why)
- if ok:
- # Taken: the hangup follows the reply.
- try:
- sess.wire.call(TSTAT, struct.pack('<I', 1), 2.0)
- except (Dropped, Hang):
- pass
- res.hung_up = True
- except Dropped:
- res.add('write', '/ctl', True, None)
- res.hung_up = True
- if res.hung_up:
- sess.reconnect()
- return res
-
-
-@op_kind('host')
-def op_host(sess, op):
- """A change on disk under the session's HOME: write or delete a file."""
- res = Result()
- name = op['name']
- assert '/' not in name and name not in ('.', '..')
- path = os.path.join(sess.home, name)
- if op['action'] == 'rm':
- try:
- os.remove(path)
- except OSError:
- pass
- else:
- with open(path, 'wb') as f:
- f.write(dec(op['data']))
- return res
-
-
-@op_kind('seq')
-def op_seq(sess, op):
- """Several ops as one step (e.g. clearing the window)."""
- res = Result()
- for sub in op['ops']:
- r = OPS[sub['op']](sess, sub)
- res.requests.extend(r.requests)
- res.hung_up |= r.hung_up
- return res
-
-
-# --------------------------------------------------------------------------
-# Input: data pools and generators (deliberately simple; swap freely).
-
-SAFE = (b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '
- b'-_=+,:#^@%()\n\r\t\0')
-UNSAFE = set(b'/~$`;|&<>.\\*?![]{}\'"')
-
-
-def shell_safe(data):
- """No path or shell metacharacters, whatever else it holds."""
- return bytes(b for b in data if b not in UNSAFE)
-
-
-RUNES = ['é', 'é', '日本', '\U0001f600', 'Δ', ' ', '​', '\u0080', '\u009f']
-ODD = [b'', b'\n', b'\r\n', b'\0', b'\xff', b'\xfe\xff', b'\xe6\x97', b'\xc0\x80', b'\x1b[2J', b'\x7f', b'\t',
- b'\n\n\n', b' ', b' \n']
-WORDS = [b'alpha', b'beta', b'foo', b'bar', b'x', b'hello', b'zz', b'0', b'1', b'-1', b'99999999999',
- b'on', b'off', b'follow', b'follow new', b'lock', b'unlock', b'clean', b'dirty', b'show',
- b'dot=addr', b'addr=dot', b'limit=addr', b'nomark', b'mark', b'get', b'put', b'del', b'delete',
- b'name', b'cleartag', b'answer', b'answer -', b'size 80 24', b'size 20 6', b'size 5000 5000',
- b'winsize 80 24', b'winsize 0 0', b'sig INT', b'sig TERM', b'sig HUP', b'sig KILL', b'sig BOGUS',
- b'exec', b'Placement acme', b'Placement pardes', b'Wrap on', b'Wrap off']
-SHELL_LINES = [b'true', b'false', b'echo hi', b'echo m9p', b'sleep 1', b'ls', b'cat nonexist',
- b'printf abc', b'exit 3', b'seq 1 2000', b'yes | head -n 3000', b'date', b'pwd', b'env | wc']
-DENY = {'Exit', 'Attach', 'Detach', 'Mount', 'Unmount', 'Tty9p', 'ClipYank', 'ClipYankMain', 'ClipPaste',
- 'ClipPasteBefore', 'ClipReplace', 'Shell', 'ThemeFile', 'EffectCode', 'Help', 'Tutor', 'Changelog',
- 'Rename', 'Hover', 'CodeAction', 'SelectRefs', 'Symbols', 'Diagnostics', 'WsDiagnostics', 'Callers',
- 'Callees', 'Supertypes', 'Subtypes', 'WsSymbols', 'Lspinfo', 'Lspwhy', 'Restore'}
-BUILTINS = ['Look', 'Exec', 'Kill', 'Dump', 'Msg', 'NextColor', 'Themes', 'DumpThemes', 'TreeContext', 'PdfFit',
- 'PdfTint', 'PdfSections', 'Petscii', 'Palette', 'Ascii', 'Save', 'New', 'Newcol', 'Del', 'Filter',
- 'Mode', 'Togglettymode', 'Edit', 'Undo', 'Redo', 'Collapse', 'Delcol', 'DelAbove', 'DelBelow', 'Tty',
- 'Repl', 'Joincol', 'Config', 'LocationsConfig', 'Messages', 'Mini', 'Find', 'Grep', 'Left', 'Down',
- 'Up', 'Right', 'Back', 'Forward', 'Last', 'Recent', 'Jumplist', 'Colors', 'Wrap', 'Tagbottom', 'Debug',
- 'FocusTint', 'Verbose', 'Motion', 'InactiveDim', 'DumpDir', 'Theme', 'Placement', 'BootShell',
- 'LookWord', 'Get', 'Put', 'Undo', 'Redo', 'Zerox', 'Snarf', 'Paste', 'Cut', 'Sort', 'Font', 'Local']
-PANE_FILES = ['name', 'body', 'tag', 'ctl', 'addr', 'dot', 'limit', 'data', 'xdata', 'sel', 'dirty', 'mark',
- 'scroll', 'errors', 'event', 'look', 'exec', 'tagexec', 'pty', 'pty/run', 'pty/data', 'pty/ctl',
- 'pty/status']
-ROOT_FILES = ['README', 'index', 'status', 'focus', 'ctl', 'commands', 'recent', 'look', 'exec', 'log', 'screen',
- 'listeners', 'layout', 'tag', 'tagexec', 'pane', 'pane/new', 'col', '']
-COL_FILES = ['tag', 'ctl', 'exec']
-WRITABLE = ['ctl', 'exec', 'look', 'tag', 'tagexec', 'focus', 'body', 'data', 'xdata', 'addr', 'dot', 'limit',
- 'name', 'sel', 'dirty', 'mark', 'scroll', 'errors', 'event', 'pty/run', 'pty/data', 'pty/ctl', 'log']
-
-
-class Ctx:
- """What the generators and invariants see."""
-
- def __init__(self, seed):
- self.seed = seed
- self.rng = random.Random(seed)
- self.sess = None
- self.step = 0
- self.serials = []
- self.cols = []
- self.gone = [] # serials seen and since closed (stale)
- self.next_h = 0
- self.last_index = b''
- self.verbose = False
-
-
-def pick_serial(ctx, rng):
- """A live pane as a token, {pN}: the Nth serial of /index (mod its
- length) when the op runs, so a shrunk replay still names a pane that
- exists; or a stale or impossible serial, literally."""
- r = rng.random()
- if ctx.serials and r < 0.85:
- return '{p%d}' % rng.randrange(64)
- if ctx.gone and r < 0.95:
- return rng.choice(ctx.gone)
- return rng.choice([0, 1, 999, 4294967295, 4294967296, -1])
-
-
-def pick_col(ctx, rng):
- if ctx.cols and rng.random() < 0.85:
- return '{c%d}' % rng.randrange(16)
- return rng.choice([0, 99, 4294967295])
-
-
-def odd_bytes(rng, n):
- out = bytearray()
- while len(out) < n:
- r = rng.random()
- if r < 0.5:
- out += rng.choice(WORDS)
- elif r < 0.7:
- out += rng.choice(RUNES).encode()
- elif r < 0.9:
- out += rng.choice(ODD)
- else:
- out.append(rng.randrange(256))
- if rng.random() < 0.5:
- out += b' '
- return bytes(out[:n])
-
-
-def payload(rng, maxlen=64):
- """Plain or odd bytes, sometimes huge, with or without a newline."""
- r = rng.random()
- if r < 0.35:
- data = rng.choice(WORDS)
- elif r < 0.55:
- data = odd_bytes(rng, rng.randrange(0, maxlen))
- elif r < 0.62:
- data = rng.choice(ODD)
- elif r < 0.66:
- data = (rng.choice(WORDS) + b' ') * rng.randrange(100, 20000) # huge
- elif r < 0.7:
- data = b'x' * rng.choice([255, 256, 1023, 1024, 1025, 65535, 65536, 70000])
- else:
- data = rng.choice(WORDS) + b' ' + odd_bytes(rng, rng.randrange(0, 12))
- if rng.random() < 0.6 and not data.endswith(b'\n'):
- data += rng.choice([b'\n', b'\n', b'\r\n', b'\n\n'])
- return shell_safe(data)
-
-
-def weird_name(rng):
- r = rng.random()
- if r < 0.3:
- return 'x' * rng.choice([255, 256, 300, 1000])
- if r < 0.5:
- return rng.choice(['..', '.', '', ' ', 'new', 'NEW', '01', '1 ', '\n', 'a\0b'])
- if r < 0.7:
- return enc(rng.choice(RUNES).encode())
- return enc(bytes(rng.randrange(1, 256) for _ in range(rng.randrange(1, 20))).replace(b'/', b'_'))
-
-
-def random_path(ctx, rng, writable=False):
- r = rng.random()
- if r < 0.6:
- f = rng.choice(WRITABLE if writable else PANE_FILES)
- return f'/pane/{pick_serial(ctx, rng)}/{f}'
- if r < 0.75:
- f = rng.choice(['ctl', 'exec', 'look', 'tag', 'tagexec', 'focus', 'log'] if writable else ROOT_FILES)
- return '/' + f
- if r < 0.85:
- return f'/col/{pick_col(ctx, rng)}/{rng.choice(COL_FILES)}'
- if r < 0.93:
- return f'/pane/{pick_serial(ctx, rng)}/{weird_name(rng)}'
- return '/' + '/'.join(weird_name(rng) for _ in range(rng.randrange(1, 4)))
-
-
-def data_for(ctx, rng, path):
- """Payload a file is likely to take, often mutated."""
- base = path.rsplit('/', 1)[-1]
- r = rng.random()
- if r < 0.25:
- return payload(rng)
- if base in ('addr', 'dot', 'limit', 'sel'):
- return address(rng)
- if base == 'ctl' and path.endswith('pty/ctl'):
- return rng.choice([b'sig INT\n', b'sig TERM\n', b'sig HUP\n', b'sig QUIT\n', b'sig KILL\n',
- b'winsize 80 24\n', b'winsize 1 1\n', b'winsize 99999 3\n', b'exec\n', b'sig\n'])
- if base in ('ctl', 'exec', 'tagexec'):
- return builtin_line(ctx, rng, path)
- if base == 'run':
- return shell_safe(rng.choice(SHELL_LINES)) + b'\n'
- if base == 'data' and '/pty/' in path:
- return shell_safe(rng.choice(SHELL_LINES)) + rng.choice([b'\r', b'\n', b'', b'\x03', b'\x04'])
- if base == 'event':
- return event_record(rng)
- if base == 'focus':
- return str(pick_serial(ctx, rng)).encode() + rng.choice([b'\n', b''])
- if base == 'look':
- return shell_safe(rng.choice([b'alpha', b'alpha.txt', b'alpha.txt:2', b'utf8.txt:#3', b'nothere',
- b'bad.txt', b'crlf.txt:1', b'gamma', b'beta'])) + b'\n'
- if base in ('dirty', 'mark', 'scroll'):
- return rng.choice([b'0', b'1', b'0\n', b'1\n', b'2\n', b'', b'yes\n'])
- if base == 'log':
- return rng.choice([b'follow\n', b'follow new\n', b'follow', b'nope\n'])
- return payload(rng, 200)
-
-
-def mutate(rng, data, rounds=None):
- data = bytearray(data)
- for _ in range(rounds or rng.randrange(1, 4)):
- if not data:
- data += rng.choice(WORDS)
- continue
- i = rng.randrange(len(data))
- r = rng.random()
- if r < 0.3:
- del data[i]
- elif r < 0.55:
- data[i:i] = shell_safe(rng.choice(ODD) or b'\0')
- elif r < 0.75:
- j = rng.randrange(i, len(data) + 1)
- data[i:i] = data[i:j]
- else:
- data[i:i] = rng.choice(RUNES).encode()
- return bytes(data)
-
-
-def builtin_line(ctx, rng, path):
- word = rng.choice(BUILTINS).encode()
- r = rng.random()
- if r < 0.4:
- line = word
- elif r < 0.7:
- line = word + b' ' + rng.choice(WORDS)
- elif r < 0.8:
- line = b'Edit ' + edit_command(rng)
- return line + b'\n' # Edit text keeps its / delimiters
- else:
- line = mutate(rng, word + b' ' + rng.choice(WORDS))
- if rng.random() < 0.1:
- line = shell_safe(rng.choice(SHELL_LINES))
- if rng.random() < 0.8:
- line += b'\n'
- return shell_safe(line)
-
-
-# sam's commands without the ones that reach files or shells.
-EDIT_TEXT = b'abcdghijklmopqstuvxyz0123456789 ACEGHIJKLMNOPQRSTUVWXYZ_-'
-
-
-def edit_text(rng):
- s = bytes(rng.choice(EDIT_TEXT) for _ in range(rng.randrange(0, 8)))
- if rng.random() < 0.3:
- s += rng.choice(RUNES).encode()
- return s
-
-
-def regex(rng):
- parts = [edit_text(rng) or b'a']
- for _ in range(rng.randrange(0, 4)):
- parts.append(rng.choice([b'(a|b)', b'^', b'$', b'[a-z]', b'[^ ]', b'.*', b'\\n',
- '[é日]'.encode(), b'x+', b'(', b'[', b'\\', b'a**',
- b'(((a)))', b'[z-a]', (b'a|' * rng.randrange(1, 300)) + b'b']))
- parts.append(edit_text(rng))
- return b''.join(parts)
-
-
-def address(rng):
- """sam addresses: valid, near-valid and garbage."""
- simple = [b'0', b'$', b'.', b',', b';', b'#0', b'#3', b'#99999', b'1', b'2', b'3:2', b'1:1', b'0:0',
- b'#1,#2', b'1,$', b'2,1', b'-', b'+', b'-1', b'+2', b'#-1', b'99999', b'1:99', b'?a?']
- r = rng.random()
- if r < 0.4:
- a = rng.choice(simple)
- elif r < 0.7:
- a = b'/' + regex(rng) + b'/'
- elif r < 0.85:
- a = rng.choice(simple) + rng.choice([b',', b';', b'+', b'-']) + rng.choice(simple + [b'/a/'])
- else:
- a = mutate(rng, rng.choice(simple) + b'/' + regex(rng) + b'/')
- return a + rng.choice([b'', b'\n'])
-
-
-def edit_command(rng):
- t = lambda: edit_text(rng) # noqa: E731
- cmds = [lambda: b',x/' + regex(rng) + b'/c/' + t() + b'/',
- lambda: b's/' + regex(rng) + b'/' + t() + b'/g',
- lambda: b'a/' + t() + b'/',
- lambda: b'i/' + t() + b'/',
- lambda: b'c/' + t() + b'/',
- lambda: b'd',
- lambda: b',d',
- lambda: b',y/' + regex(rng) + b'/d',
- lambda: b',x/' + regex(rng) + b'/g/' + regex(rng) + b'/d',
- lambda: b',x/' + regex(rng) + b'/v/' + regex(rng) + b'/c/' + t() + b'/',
- lambda: b'1,2m$',
- lambda: b'1t0',
- lambda: b'k',
- lambda: b'p',
- lambda: b'=',
- lambda: b'u',
- lambda: b'{\na/' + t() + b'/\ni/' + t() + b'/\n}',
- lambda: b',x/\\n/a/' + t() + b'/',
- lambda: b'0,$s/' + regex(rng) + b'/&&/g']
- cmd = rng.choice(cmds)()
- if rng.random() < 0.4:
- cmd = address(rng).rstrip(b'\n') + cmd
- if rng.random() < 0.2:
- cmd = mutate(rng, cmd)
- # Mutation never introduces a banned command letter or shell char.
- cmd = bytes(b for b in cmd if b not in b'werfbBDn!<>|')
- return cmd
-
-
-def event_record(rng):
- origin = rng.choice(b'EFKMZ')
- action = rng.choice(b'xXlLiIdDZ')
- q0, q1 = rng.choice([0, 1, 5, 10]), rng.choice([0, 2, 7, 1 << 40, -1])
- rec = bytes([origin, action]) + f'{q0} {q1} {rng.choice([0, 1, 2, 8])} '.encode()
- text = shell_safe(edit_text(rng))
- rec += str(len(text)).encode() + b' ' + text + b'\n'
- if rng.random() < 0.3:
- rec = mutate(rng, rec)
- return shell_safe(rec)
-
-
-def new_handle(ctx):
- ctx.next_h += 1
- return f'h{ctx.next_h}'
-
-
-@generator(30)
-def gen_write(ctx, rng):
- path = random_path(ctx, rng, writable=True)
- data = data_for(ctx, rng, path)
- mode = rng.choice([OWRITE, OWRITE, ORDWR, OWRITE | OTRUNC, ORDWR | OTRUNC, OREAD])
- r = rng.random()
- chunks = 'bytes' if r < 0.08 and len(data) <= 64 else (
- [rng.randrange(1, 16) for _ in range(4)] if r < 0.15 else 'whole')
- op = {'op': 'write', 'path': path, 'data': enc(data), 'mode': mode, 'chunks': chunks}
- if rng.random() < 0.05:
- op['offset'] = rng.choice([1, 100, 1 << 40])
- return op
-
-
-@generator(14)
-def gen_read(ctx, rng):
- path = random_path(ctx, rng)
- return {'op': 'read', 'path': path, 'count': rng.choice([1, 2, 7, 64, 200, 8192, 1 << 20]),
- 'offset': rng.choice([0, 0, 0, 1, 3, 100, 1 << 33]), 'reads': rng.choice([1, 2, 8])}
-
-
-@generator(4)
-def gen_stat(ctx, rng):
- path = random_path(ctx, rng)
- if rng.random() < 0.2:
- path = '/os/' + rng.choice(['etc', 'etc/hosts', 'nonexistent', 'x' * 300])
- return {'op': 'stat', 'path': path}
-
-
-@generator(5)
-def gen_newpane(ctx, rng):
- if rng.random() < 0.7:
- return {'op': 'read', 'path': '/pane/new', 'count': 64, 'reads': 1}
- return {'op': 'hold', 'h': new_handle(ctx), 'path': '/pane/new', 'mode': rng.choice([OREAD, ORDWR])}
-
-
-@generator(2)
-def gen_remove(ctx, rng):
- r = rng.random()
- if r < 0.6:
- path = f'/pane/{pick_serial(ctx, rng)}'
- elif r < 0.85:
- path = f'/col/{pick_col(ctx, rng)}'
- else:
- path = random_path(ctx, rng)
- return {'op': 'remove', 'path': path}
-
-
-@generator(2)
-def gen_create(ctx, rng):
- where = rng.choice(['/col', '/pane', '/', f'/pane/{pick_serial(ctx, rng)}'])
- return {'op': 'create', 'path': where, 'name': weird_name(rng) if rng.random() < 0.5 else 'new',
- 'perm': rng.choice([0o666, DMDIR | 0o777]), 'mode': rng.choice([OREAD, OWRITE])}
-
-
-@generator(2)
-def gen_clear_window(ctx, rng):
- """Down to the empty window (every pane closed), sometimes rebuilt."""
- keep = rng.choice(ctx.serials) if ctx.serials and rng.random() < 0.8 else None
- ops = [{'op': 'remove', 'path': f'/pane/{s}'} for s in ctx.serials if s != keep]
- if rng.random() < 0.5:
- ops += [{'op': 'remove', 'path': f'/col/{c}'} for c in ctx.cols]
- if rng.random() < 0.6:
- ops.append({'op': 'read', 'path': '/pane/new', 'count': 64, 'reads': 1})
- return {'op': 'seq', 'ops': ops}
-
-
-@generator(6)
-def gen_hold(ctx, rng):
- held = [h for h in (ctx.sess.handles if ctx.sess else {})]
- r = rng.random()
- if not held or r < 0.35:
- path = random_path(ctx, rng)
- if rng.random() < 0.3:
- path = rng.choice(['/log', f'/pane/{pick_serial(ctx, rng)}/event', '/screen', '/index',
- f'/pane/{pick_serial(ctx, rng)}/ctl'])
- return {'op': 'hold', 'h': new_handle(ctx), 'path': path, 'mode': rng.choice([OREAD, ORDWR, OWRITE])}
- h = rng.choice(held)
- path = ctx.sess.handles[h][1]
- if r < 0.6:
- return {'op': 'hwrite', 'h': h, 'data': enc(data_for(ctx, rng, path))}
- if r < 0.85:
- return {'op': 'hread', 'h': h, 'count': rng.choice([1, 16, 8192]),
- 'offset': rng.choice([None, None, 0, 5])}
- return {'op': 'clunk', 'h': h}
-
-
-@generator(6)
-def gen_shell(ctx, rng):
- s = pick_serial(ctx, rng)
- line = shell_safe(rng.choice(SHELL_LINES))
- r = rng.random()
- if r < 0.25:
- return {'op': 'write', 'path': f'/pane/{s}/ctl', 'data': enc(b'Tty\n'), 'mode': OWRITE}
- if r < 0.45:
- return {'op': 'write', 'path': f'/pane/{s}/pty/run', 'data': enc(line + b'\n'), 'mode': ORDWR}
- if r < 0.6:
- return {'op': 'write', 'path': f'/pane/{s}/pty/data', 'data': enc(line + b'\r'), 'mode': OWRITE}
- if r < 0.75:
- return {'op': 'write', 'path': f'/pane/{s}/pty/ctl',
- 'data': enc(rng.choice([b'sig INT\n', b'sig TERM\n', b'sig KILL\n', b'sig HUP\n'])), 'mode': OWRITE}
- return {'op': 'write', 'path': rng.choice(['/exec', f'/pane/{s}/exec']), 'data': enc(line + b'\n'),
- 'mode': OWRITE}
-
-
-@generator(6)
-def gen_edit(ctx, rng):
- s = pick_serial(ctx, rng)
- path = rng.choice([f'/pane/{s}/ctl', f'/pane/{s}/exec', '/exec'])
- return {'op': 'write', 'path': path, 'data': enc(b'Edit ' + edit_command(rng) + b'\n'), 'mode': OWRITE}
-
-
-@generator(8)
-def gen_addr(ctx, rng):
- s = pick_serial(ctx, rng)
- ops = [{'op': 'write', 'path': f'/pane/{s}/addr', 'data': enc(address(rng)), 'mode': OWRITE}]
- r = rng.random()
- if r < 0.4:
- ops.append({'op': 'read', 'path': f'/pane/{s}/{rng.choice(["xdata", "data", "addr", "dot"])}',
- 'count': 8192, 'reads': 2})
- elif r < 0.7:
- ops.append({'op': 'write', 'path': f'/pane/{s}/{rng.choice(["data", "xdata"])}',
- 'data': enc(payload(rng, 20)), 'mode': rng.choice([OWRITE, OWRITE | OTRUNC])})
- return {'op': 'seq', 'ops': ops}
-
-
-@generator(3)
-def gen_tag(ctx, rng):
- path = rng.choice([f'/pane/{pick_serial(ctx, rng)}/tag', '/tag', f'/col/{pick_col(ctx, rng)}/tag'])
- return {'op': 'write', 'path': path, 'data': enc(payload(rng, 40)),
- 'mode': rng.choice([OWRITE, OWRITE | OTRUNC])}
-
-
-@generator(3)
-def gen_event(ctx, rng):
- s = pick_serial(ctx, rng)
- return {'op': 'hold', 'h': new_handle(ctx), 'path': f'/pane/{s}/event', 'mode': ORDWR}
-
-
-@generator(2)
-def gen_dump_restore(ctx, rng):
- r = rng.random()
- if r < 0.5:
- return {'op': 'write', 'path': '/ctl', 'data': enc(rng.choice([b'Dump\n', b'Dump', b'Dump m9pdump\n'])),
- 'mode': OWRITE}
- return {'op': 'restore', 'data': enc(rng.choice([b'Restore\n', b'Restore\n', b'Restore m9pdump\n',
- b'Restore nothere\n']))}
-
-
-@generator(3)
-def gen_host(ctx, rng):
- name = rng.choice(list(FIXTURES) + ['fresh.txt'])
- if rng.random() < 0.5:
- return {'op': 'host', 'action': 'rm', 'name': name}
- return {'op': 'host', 'action': 'write', 'name': name, 'data': enc(payload(rng, 100))}
-
-
-@generator(3)
-def gen_look_fixture(ctx, rng):
- name = rng.choice(list(FIXTURES) + ['fresh.txt'])
- suffix = rng.choice([b'', b':2', b':#4', b':1:3', b':/beta/'])
- return {'op': 'write', 'path': rng.choice(['/look', f'/pane/{pick_serial(ctx, rng)}/look']),
- 'data': enc(name.encode() + suffix + b'\n'), 'mode': OWRITE}
-
-
-# --------------------------------------------------------------------------
-# Log window: the records one step caused, between two unique markers.
-
-MARK = re.compile(r'unknown control message "(m9p-mark-\d+)"')
-
-
-def write_marker(sess, step):
- """An unknown ctl word logs `err - ctl: unknown control message "<word>"`."""
- word = f'm9p-mark-{step}'
- ok, why = sess.wire.write_path('/ctl', word.encode() + b'\n')
- if ok:
- return None, 'marker write was taken'
- prev, sess.marker = sess.marker, word
- if sess.follower is not None:
- end = sess.follower.wait_for(f'"{word}"')
- if end is None:
- return None, 'marker record never reached the follower'
- lines = sess.follower.lines[:end]
- start = 0
- if prev is not None:
- for i in range(len(lines) - 1, -1, -1):
- if f'"{prev}"' in lines[i]:
- start = i + 1
- break
- else:
- return None, None
- window = lines[start:]
- del sess.follower.lines[:end]
- return window, None
- lines = sess.wire.read_path('/log').decode(errors='replace').splitlines()
- end = next((i for i in range(len(lines) - 1, -1, -1) if f'"{word}"' in lines[i]), None)
- if end is None:
- return None, 'marker record missing from /log'
- if prev is None:
- return None, None
- start = next((i for i in range(end - 1, -1, -1) if f'"{prev}"' in lines[i]), None)
- if start is None:
- return None, None # the ring moved past it
- return lines[start + 1:end], None
-
-
-COUNT = re.compile(r'^(.*) \(x(\d+)\)$')
-
-
-def occurrences(lines, kind):
- """Records of `kind` (err|msg), counting `(xN)` repeats (docs/typ/reference.typ: a record said
- again is counted; a follower sees each count as a line of its own)."""
- out, last_text, last_n = [], None, 0
- for line in lines:
- if not line.startswith(kind + ' '):
- last_text = None
- continue
- m = COUNT.match(line)
- text, n = (m.group(1), int(m.group(2))) if m else (line, 1)
- if text == last_text:
- new = n - last_n
- else:
- new = n
- out.extend([text] * max(new, 0))
- last_text, last_n = text, n
- return out
-
-
-# --------------------------------------------------------------------------
-# Invariants: small, cheap, each with its doc citation.
-
-@invariant
-def alive(ctx, res):
- """Never crashes or panics (src/crash.zig: a panic leaves its crash file)."""
- if not ctx.sess.alive():
- return f'pardes died (exit {ctx.sess.proc.returncode})'
- if ctx.sess.has_crash_file():
- return 'crash file written: ' + ctx.sess.crash_text()[-2000:]
- return None
-
-
-@invariant
-def one_failure_rule(ctx, res):
- """docs/typ/reference.typ 'One rule for what fails': a write fails whenever what it asked
- for fails ... and logs its reason exactly once, as `err <serial|-> <file>:
- <why>`, with no `msg` for it. What is not a failure: a look that finds
- nothing answers nothing and logs one `err`, the write succeeding."""
- if res.window is None or res.hung_up:
- return None
- writes = res.writes()
- # The rule is a write's: a refused open (pane/new's and an OTRUNC one
- # too), create or remove logs no err, nor does a write to pane/new
- # (`permission denied`: it is only read).
- failed = [w for w in writes if w[2] is False and w[0] in ('write', 'clunk-write')
- and w[1].rstrip('/') != '/pane/new']
- errs = occurrences(res.window, 'err')
- msgs = occurrences(res.window, 'msg')
- # Allowed only the Twrites that succeeded (allowances): a failed one
- # logs its one err.
- allowed = sum(r[3] for r in res.requests if r[0] == 'allow')
- # docs/typ/reference.typ 'A write of command lines ... what is left when it closes runs at
- # the close ... and its failure is in the log alone'.
- closes = [r for r in res.requests if r[0] == 'close-runs']
- lo, hi = len(failed), len(failed) + allowed + len(closes)
- if not lo <= len(errs) <= hi:
- return (f'{len(failed)} failed write(s) logged {len(errs)} err record(s) '
- f'(allowed {lo}..{hi}); failed={[(w[0], w[1], w[3]) for w in failed]} window={res.window}')
- # A msg is a failed write's only when no Twrite of the op succeeded: a
- # line cut across Twrites runs a piece a Twrite, and a piece that ran
- # (a Save) says so while a later one is refused.
- succeeded = [w for w in writes if w[2] is True and w[0] == 'write']
- # A file watch's notice (`<file> deleted on disk ...`, `changed on
- # disk`, a stamped `reloaded <file>` or `reloaded theme`) is the host's
- # news, said whenever it arrives: no write's.
- msgs = [m for m in msgs if ' deleted on disk (' not in m and ' changed on disk (' not in m and ' reloaded ' not in m]
- if failed and msgs and not succeeded:
- return f'a failed write also logged msg: {msgs}; failed={[(w[1], w[3]) for w in failed]}'
- return None
-
-
-LINE_FILES = ('look', 'exec', 'tagexec', 'ctl')
-
-
-@invariant
-def close_runs_only_line_files(ctx, res):
- """docs/typ/reference.typ: 'A write of command lines -- to look, exec, tagexec, a ctl of the
- root, a pane or a column, or a column's exec -- runs each line once it is
- whole ... what is left when it closes runs at the close'. Another file
- that fails only at its close breaks 'a write fails whenever what it asked
- for fails' (or the list is short)."""
- if res.window is None:
- return None
- closes = [r[1] for r in res.requests if r[0] == 'close-runs']
- failed_files = {w[1].rsplit('/', 1)[-1] for w in res.writes() if w[2] is False}
- for line in occurrences(res.window, 'err'):
- m = re.match(r'err \S+ ([^:]+):', line)
- if not m:
- continue
- name = m.group(1).rsplit('/', 1)[-1]
- if name in LINE_FILES or name in failed_files:
- continue
- for path in closes:
- if path.rsplit('/', 1)[-1] == name:
- return f'a write to {name} with no newline was answered, then failed at the close: {line}'
- return None
-
-
-@invariant
-def index_matches_pane_dirs(ctx, res):
- """docs/typ/reference.typ /index: one line per pane; /pane/<n>/ each pane's directory."""
- w = ctx.sess.wire
- first = w.read_path('/index')
- fid, err = w.walk_names([b'pane'])
- if fid is None:
- return f'walking /pane failed: {err}'
- ok, _ = w.call(TOPEN, struct.pack('<IB', fid, OREAD))
- names = dir_names(w.read_all(fid))
- w.clunk(fid)
- second = w.read_path('/index')
- ctx.last_index = second
- serials = []
- for line in second.decode(errors='replace').splitlines():
- try:
- serials.append(int(line.split()[0]))
- except (IndexError, ValueError):
- return f'/index line unparsable: {line!r}'
- old = set(ctx.serials)
- ctx.serials = serials
- ctx.gone = sorted(set(ctx.gone) | (old - set(serials)))[-20:]
- if first != second:
- return None # it changed between the reads; no claim
- dirs = sorted(int(n) for n in names if n.isdigit())
- extra = [n for n in names if not n.isdigit() and n != 'new']
- if extra:
- return f'/pane lists non-serial entries {extra}'
- if sorted(serials) != dirs:
- return f'/index serials {sorted(serials)} != /pane dirs {dirs}'
- return None
-
-
-LISTINGS = ['/index', '/layout', '/', '/pane', '/col', '/pane/{s}', '/status', '/commands', '/pane/{s}/tag',
- '/recent', '/README']
-
-
-@invariant
-def chunked_equals_whole(ctx, res):
- """9P read(5): reads at successive offsets see the one file; a listing read
- in chunks equals one read whole within the same open."""
- w = ctx.sess.wire
- path = LISTINGS[ctx.step % len(LISTINGS)]
- if '{s}' in path:
- if not ctx.serials:
- return None
- path = path.format(s=ctx.serials[ctx.step % len(ctx.serials)])
- fid, err = w.walk_names(split(path))
- if fid is None:
- return None # gone since
- try:
- ok, body = w.call(TOPEN, struct.pack('<IB', fid, OREAD))
- if not ok:
- return None
- whole = w.read_all(fid)
- isdir = path in ('/', '/pane', '/col') or path.count('/') == 2 and path.startswith('/pane/')
- size = 128 + (ctx.step * 37) % 900 if isdir else 1 + (ctx.step * 7) % 50
- chunked = w.read_all(fid, count=size)
- if isdir:
- whole_n, chunk_n = dir_names(whole), dir_names(chunked)
- if whole_n != chunk_n:
- return f'{path}: whole {whole_n} != chunked({size}) {chunk_n}'
- elif whole != chunked and w.read_all(fid) != chunked:
- # A live file (a tag says `running`, then `exit 0`) may change
- # between the two reads: only chunks that no whole read matches,
- # read again, are a chunking fault.
- return f'{path}: whole {whole[:200]!r} != chunked({size}) {chunked[:200]!r}'
- finally:
- w.clunk(fid)
- return None
-
-
-# --------------------------------------------------------------------------
-# Runner.
-
-class Failure(Exception):
- def __init__(self, kind, why, step):
- super().__init__(f'{kind}: {why}')
- self.kind, self.why, self.step = kind, why, step
-
- def signature(self):
- """What makes two failures the same bug: the kind, and the message with
- numbers, quoted words and pane paths blurred."""
- why = re.sub(r'"[^"]*"', '""', self.why)
- why = re.sub(r"'[^']*'", "''", why) if 'window=' not in why else why
- why = re.sub(r'/home/\S*', 'PATH', why)
- return self.kind + ':' + re.sub(r'\d+', 'N', why)[:300]
-
-
-def check_death(sess, step, what):
- """After an error: did pardes quit cleanly (Ended), or die (Failure)?"""
- deadline = time.monotonic() + 1.0
- while sess.alive() and time.monotonic() < deadline:
- time.sleep(0.02)
- if sess.alive():
- return
- if sess.proc.returncode == 0 and not sess.has_crash_file():
- raise Ended(what)
- raise Failure('crash', f'{what}: pardes exit {sess.proc.returncode} ' + sess.crash_text()[:3000], step)
-
-
-TOKEN = re.compile(r'\{([pc])(\d+)\}')
-
-
-def resolve(ctx, op):
- """Replace {pN}/{cN} in an op's paths with live serials."""
- def sub(m):
- live = ctx.serials if m.group(1) == 'p' else ctx.cols
- return str(live[int(m.group(2)) % len(live)]) if live else '0'
- out = dict(op)
- if 'path' in out:
- out['path'] = TOKEN.sub(sub, out['path'])
- if 'ops' in out:
- out['ops'] = [resolve(ctx, o) for o in out['ops']]
- if 'data' in out and '/focus' in out.get('path', ''):
- out['data'] = TOKEN.sub(sub, out['data'])
- return out
-
-
-def run_step(ctx, op):
- """Run one op and every invariant. Raises Failure or Ended."""
- sess = ctx.sess
- op = resolve(ctx, op)
- ctx.last_op = op
- what = f'{op["op"]} {op.get("path", "")}'.strip()
- try:
- res = OPS[op['op']](sess, op)
- except Hang as why:
- check_death(sess, ctx.step, what)
- raise Failure('hang', f'{what}: {why}', ctx.step)
- except Dropped as why:
- check_death(sess, ctx.step, what)
- raise Failure('dropped', f'{what}: {why}', ctx.step)
- res.window = None
- try:
- if not sess.alive():
- check_death(sess, ctx.step, what)
- # Liveness: a trivial request answers within the timeout.
- ok, _ = sess.wire.call(TSTAT, struct.pack('<I', 1))
- window, why = write_marker(sess, ctx.step)
- if why:
- raise Failure('log', why, ctx.step)
- res.window = None if res.hung_up else window
- if ctx.verbose:
- print(f' op {ctx.step}: {json.dumps(op)[:300]}')
- for r in res.requests:
- print(f' {r[0]} {r[1]!r} -> {"ok" if r[2] else ("held" if r[2] is None else "Rerror")}'
- f'{": " + str(r[3]) if r[3] else ""}')
- for line in window or []:
- print(f' log: {line}')
- for check in INVARIANTS:
- problem = check(ctx, res)
- if problem:
- raise Failure(check.__name__, problem, ctx.step)
- # The column list, for the generators.
- fid, err = sess.wire.walk_names([b'col'])
- if fid is not None:
- sess.wire.call(TOPEN, struct.pack('<IB', fid, OREAD))
- ctx.cols = [int(n) for n in dir_names(sess.wire.read_all(fid)) if n.isdigit()]
- sess.wire.clunk(fid)
- except Hang as why:
- check_death(sess, ctx.step, what)
- raise Failure('hang', f'after {what}: {why}', ctx.step)
- except Dropped as why:
- check_death(sess, ctx.step, what)
- raise Failure('dropped', f'after {what}: {why}', ctx.step)
- except Failure as f:
- # An invariant that failed because pardes was quitting is not one.
- check_death(sess, ctx.step, what)
- raise f
- except OSError as why:
- check_death(sess, ctx.step, what)
- raise Failure('checker', f'after {what}: {why}', ctx.step)
- return res
-
-
-class Runner:
- def __init__(self, args):
- self.args = args
- self.out = args.out
- os.makedirs(os.path.join(self.out, 'bugs'), exist_ok=True)
- os.makedirs(os.path.join(self.out, 'runs'), exist_ok=True)
-
- def new_session(self, ctx, follower, run_dir):
- if ctx.sess is not None:
- ctx.sess.close()
- ctx.sess = Session(self.args.binary, run_dir, follower)
- ctx.serials, ctx.cols, ctx.gone = [], [], []
- idx = ctx.sess.wire.read_path('/index')
- ctx.serials = [int(line.split()[0]) for line in idx.decode(errors='replace').splitlines() if line]
-
- def execute(self, ops, run_dir, ctx=None, log=None, stop_on_failure=True, verbose=False):
- """Run concrete ops (a replay). Returns the first Failure or None."""
- ctx = ctx or Ctx(0)
- ctx.verbose = verbose
- failure = None
- try:
- for i, op in enumerate(ops):
- ctx.step = i
- if op['op'] == 'fresh':
- self.new_session(ctx, op.get('follower', False), run_dir)
- continue
- if ctx.sess is None:
- self.new_session(ctx, False, run_dir)
- try:
- run_step(ctx, op)
- except Ended:
- ctx.sess.close()
- ctx.sess = None
- except Failure as f:
- failure = f
- if verbose:
- print(f'step {i}: {f}')
- if stop_on_failure:
- break
- finally:
- if ctx.sess is not None:
- ctx.sess.close(keep=False)
- ctx.sess = None
- return failure
-
- def fuzz(self, seed, steps):
- name = f'seed-{seed}'
- run_dir = os.path.join(self.out, 'runs', name)
- shutil.rmtree(run_dir, ignore_errors=True)
- os.makedirs(run_dir)
- ctx = Ctx(seed)
- rng = ctx.rng
- log_path = os.path.join(run_dir, 'ops.jsonl')
- segment = []
- found = collections.OrderedDict()
- t0 = time.monotonic()
- weights = [w for w, _ in GENERATORS]
- ended = 0
- with open(log_path, 'w') as log:
- log.write(json.dumps({'seed': seed, 'steps': steps, 'binary': self.args.binary}) + '\n')
-
- def fresh():
- op = {'op': 'fresh', 'follower': rng.random() < 0.5}
- log.write(json.dumps(op) + '\n')
- segment.clear()
- segment.append(op)
- self.new_session(ctx, op['follower'], run_dir)
-
- fresh()
- for step in range(steps):
- ctx.step = step
- gen = rng.choices(GENERATORS, weights)[0][1]
- op = gen(ctx, rng)
- log.write(json.dumps(op) + '\n')
- log.flush()
- segment.append(op)
- f = None
- try:
- run_step(ctx, op)
- except Ended:
- ended += 1
- fresh()
- continue
- except Failure as failure:
- f = failure
- except Exception as why: # a harness bug: say so, carry on
- import traceback
- f = Failure('harness', f'{type(why).__name__}: {why} ' +
- traceback.format_exc()[-1500:], step)
- if f is not None:
- sig = f.signature()
- first = sig not in found
- found.setdefault(sig, []).append(step)
- print(f'[seed {seed} step {step}] FAIL {f.kind}: {f.why[:400]}', flush=True)
- if first:
- crash = ctx.sess.crash_text()
- ctx.sess.close(keep=True)
- ctx.sess = None
- self.record(seed, step, f, list(segment), crash)
- fresh()
- if self.args.progress and step and step % self.args.progress == 0:
- rate = step / (time.monotonic() - t0)
- print(f'[seed {seed}] step {step}/{steps} {rate:.0f} steps/s, {len(found)} distinct failure(s)',
- flush=True)
- if ctx.sess is not None:
- ctx.sess.close()
- dt = time.monotonic() - t0
- print(f'[seed {seed}] {steps} steps in {dt:.0f}s ({steps / dt:.0f}/s), {ended} clean quit(s); '
- f'ops log {log_path}')
- for sig, at in found.items():
- print(f' {len(at)}x {sig[:200]} (first at step {at[0]})')
- return found
-
- def record(self, seed, step, f, ops, crash):
- digest = hashlib.sha1(f.signature().encode()).hexdigest()[:8]
- path = os.path.join(self.out, 'bugs', f'{f.kind}-{digest}-seed{seed}-step{step}.jsonl')
- header = {'seed': seed, 'step': step, 'kind': f.kind, 'why': f.why, 'signature': f.signature(),
- 'crash': crash[-4000:], 'binary': self.args.binary}
- write_ops(path, header, ops)
- print(f' bug record {path}', flush=True)
- if not self.args.no_shrink:
- self.shrink(path)
-
- def shrink(self, path):
- header, ops = read_ops(path)
- # The first replay pins the signature (this code's, which may have
- # changed since the record was written); later ones must match it.
- sig = None
- kind = header.get('kind')
- run_dir = os.path.join(self.out, 'runs', f'shrink-{os.getpid()}')
- os.makedirs(run_dir, exist_ok=True)
- follower = ops[0].get('follower', False) if ops and ops[0]['op'] == 'fresh' else False
- body = [op for op in ops if op['op'] != 'fresh']
- budget = [self.args.shrink_budget]
- deadline = time.monotonic() + self.args.shrink_seconds
-
- def fails(candidate):
- if budget[0] <= 0 or time.monotonic() > deadline:
- return False
- budget[0] -= 1
- nonlocal sig
- f = self.execute([{'op': 'fresh', 'follower': follower}] + candidate, run_dir)
- if f is None:
- return False
- if sig is None:
- if kind is not None and f.kind != kind:
- return False
- sig = f.signature()
- return True
- return f.signature() == sig
-
- if not fails(body):
- # One more try with the other log mode, then give up.
- follower = not follower
- if not fails(body):
- header['shrink'] = 'did not reproduce on replay'
- write_ops(path, header, ops)
- print(f' not reproduced on replay: {path}', flush=True)
- return
- small = ddmin(body, fails)
- # Then shrink each op's payload.
- small = shrink_payloads(small, fails)
- header['shrink'] = f'{len(body)} -> {len(small)} ops'
- out = path.replace('.jsonl', '.min.jsonl')
- write_ops(out, header, [{'op': 'fresh', 'follower': follower}] + small)
- print(f' shrunk {len(body)} -> {len(small)} ops: {out}', flush=True)
-
-
-def ddmin(items, fails):
- """Zeller's delta debugging: a 1-minimal failing subsequence."""
- n = 2
- # Trailing ops after the failure never matter; the failing op is last.
- while len(items) >= 2:
- size = max(1, len(items) // n)
- chunks = [items[i:i + size] for i in range(0, len(items), size)]
- reduced = False
- for i in range(len(chunks)):
- complement = [op for j, c in enumerate(chunks) if j != i for op in c]
- if complement and fails(complement):
- items, n, reduced = complement, max(n - 1, 2), True
- break
- if not reduced:
- if n >= len(items):
- break
- n = min(len(items), n * 2)
- return items
-
-
-def shrink_payloads(ops, fails):
- """Halve long data payloads while the failure stays."""
- ops = [dict(op) for op in ops]
- for i, op in enumerate(ops):
- while 'data' in op and len(op['data']) > 8:
- half = dict(op, data=op['data'][:len(op['data']) // 2])
- trial = ops[:i] + [half] + ops[i + 1:]
- if fails(trial):
- ops[i] = op = half
- else:
- break
- if op.get('chunks') not in (None, 'whole'):
- whole = dict(op, chunks='whole')
- if fails(ops[:i] + [whole] + ops[i + 1:]):
- ops[i] = whole
- return ops
-
-
-def write_ops(path, header, ops):
- with open(path, 'w') as f:
- f.write(json.dumps(header) + '\n')
- for op in ops:
- f.write(json.dumps(op) + '\n')
-
-
-def read_ops(path):
- with open(path) as f:
- lines = [json.loads(line) for line in f if line.strip()]
- return lines[0], lines[1:]
-
-
-def main():
- ap = argparse.ArgumentParser(description=__doc__.split('\n\n')[0])
- ap.add_argument('binary')
- ap.add_argument('--seed', type=int, action='append', help='seed (repeatable); default 1')
- ap.add_argument('--steps', type=int, default=1000)
- ap.add_argument('--replay', help='run an ops log (or bug record) exactly')
- ap.add_argument('--shrink', help='shrink a bug record by delta debugging over replays')
- ap.add_argument('--no-shrink', action='store_true', help='record failures without shrinking them')
- ap.add_argument('--shrink-budget', type=int, default=300, help='replays a shrink may spend')
- ap.add_argument('--shrink-seconds', type=float, default=600)
- ap.add_argument('--smoke', action='store_true', help='fixed seed, few steps, fail on any failure')
- ap.add_argument('--progress', type=int, default=500)
- ap.add_argument('--verbose', '-v', action='store_true', help='replay: print each request and log record')
- ap.add_argument('--out', default=DEFAULT_OUT)
- args = ap.parse_args()
- check_served_lines()
- args.binary = os.path.abspath(args.binary)
- for key in [k for k in os.environ if k.startswith('PARDES_') or k in ('NINE_MOUNT', 'NAMESPACE')]:
- del os.environ[key]
- become_subreaper()
- signal.signal(signal.SIGTERM, lambda *_: sys.exit(143))
- runner = Runner(args)
- try:
- return run_main(runner, args)
- finally:
- sweep(f'{os.getpid()}-')
- for d in RUNTIME_DIRS:
- shutil.rmtree(d, ignore_errors=True)
-
-
-def run_main(runner, args):
- if args.replay:
- header, ops = read_ops(args.replay)
- run_dir = os.path.join(args.out, 'runs', f'replay-{os.getpid()}')
- os.makedirs(run_dir, exist_ok=True)
- f = runner.execute(ops, run_dir, verbose=args.verbose)
- print(f'replay {args.replay}: ' + (f'FAIL at op {f.step}: {f.kind}: {f.why}' if f else 'passed'))
- return 1 if f else 0
- if args.shrink:
- runner.shrink(args.shrink)
- return 0
- if args.smoke:
- args.no_shrink = True
- found = runner.fuzz(20260929, args.steps if args.steps != 1000 else 800)
- return 1 if found else 0
- total = 0
- for seed in args.seed or [1]:
- total += len(runner.fuzz(seed, args.steps))
- return 1 if total else 0
-
-
-if __name__ == '__main__':
- sys.exit(main())
diff --git a/test/monkey9p.zig b/test/monkey9p.zig
new file mode 100644
index 00000000..45c5d12f
--- /dev/null
+++ b/test/monkey9p.zig
@@ -0,0 +1,1680 @@
+//! The 9P monkey at T1 (docs/next-tests.md): random 9P requests against an
+//! in-process session (test/session.zig), and after each step the
+//! documented rules checked against what the requests answered and what
+//! /log said meanwhile. It was test/monkey9p.py, which drove a real
+//! `pardes --detach` over its socket.
+//!
+//! zig build monkey-session -Dplatform=tty -- [--seed N]... [--steps M] [--only i,j,k] [--no-shrink] [-v]
+//! zig build monkey-session-smoke -Dplatform=tty (part of behaviour-test)
+//!
+//! A step's op is drawn from the seed alone: the panes, columns and held
+//! files it names are tokens (`{p3}`: /index's 4th serial, mod its length,
+//! when the op runs), so `--only` replays any subset of a seed's steps, and
+//! a failure shrinks by delta debugging over them. Each run is a child of
+//! this program (itself again) in a throwaway root that is its HOME, XDG
+//! directories and working directory, so a panic or a hang is a failure
+//! the parent reports and shrinks like any other.
+//!
+//! What T1 leaves out: Session performs no effects (no shell runs, nothing
+//! is saved or dumped, a Restore hangs nothing up), and no 9P engine stands
+//! in front of the core, so the engine's own refusals (a write on an open
+//! for reading, a directory opened to write, a mode bit missing) and
+//! OTRUNC's truncation first are made here, as cloud9's fs.zig makes them.
+const std = @import("std");
+const Session = @import("session").Session;
+const libc = std.c;
+const Allocator = std.mem.Allocator;
+const Random = std.Random;
+
+extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8;
+extern "c" fn execve(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8) c_int;
+extern "c" fn system(cmd: [*:0]const u8) c_int;
+extern "c" fn _exit(status: c_int) noreturn;
+extern "c" var environ: [*:null]?[*:0]u8;
+
+const OREAD: u8 = 0;
+const OWRITE: u8 = 1;
+const ORDWR: u8 = 2;
+const OTRUNC: u8 = 16;
+const DMDIR: u32 = 0x80000000;
+// As the Python client asked: a Twrite carries msize less 23, a Tread msize less 11.
+const msize = 65536 + 24;
+const read_max = msize - 11;
+const write_max = msize - 23;
+const room = msize - 24;
+/// A step that has not finished in this long is a hang.
+const hang_ms = 30_000;
+
+const fixtures = [_][2][]const u8{
+ .{ "alpha.txt", "alpha beta gamma\nsecond line\n\nthird after a blank\n" },
+ .{ "utf8.txt", "na\xc3\xafve caf\xc3\xa9 \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e e\xcc\x81 \xf0\x9f\x98\x80\nline two\n" },
+ .{ "crlf.txt", "one\r\ntwo\r\nthree\r\n" },
+ .{ "bad.txt", "hello \xff\xfe world\nlatin-1 caf\xe9\ntruncated \xe6\x97 here\n" },
+ .{ "empty.txt", "" },
+ .{ "noeol.txt", "no newline at the end" },
+};
+const fixture_names = [_][]const u8{ "alpha.txt", "utf8.txt", "crlf.txt", "bad.txt", "empty.txt", "noeol.txt", "fresh.txt" };
+
+// ---------------------------------------------------------------------------
+// Input pools.
+
+const unsafe = "/~$`;|&<>.\\*?![]{}'\"";
+const runes = [_][]const u8{ "\xc3\xa9", "e\xcc\x81", "\xe6\x97\xa5\xe6\x9c\xac", "\xf0\x9f\x98\x80", "\xce\x94", "\xc2\xa0", "\xe2\x80\x8b", "\xc2\x80", "\xc2\x9f" };
+const odd = [_][]const u8{ "", "\n", "\r\n", "\x00", "\xff", "\xfe\xff", "\xe6\x97", "\xc0\x80", "\x1b[2J", "\x7f", "\t", "\n\n\n", " ", " \n" };
+const words = [_][]const u8{
+ "alpha", "beta", "foo", "bar", "x", "hello", "zz", "0", "1", "-1", "99999999999",
+ "on", "off", "follow", "follow new", "lock", "unlock", "clean", "dirty", "show", "dot=addr", "addr=dot",
+ "limit=addr", "nomark", "mark", "get", "put", "del", "delete", "name", "cleartag", "answer", "answer -",
+ "size 80 24", "size 20 6", "size 5000 5000", "winsize 80 24", "winsize 0 0", "sig INT", "sig TERM", "sig HUP", "sig KILL", "sig BOGUS", "exec",
+ "Placement acme", "Placement pardes", "Wrap on", "Wrap off",
+};
+const shell_lines = [_][]const u8{ "true", "false", "echo hi", "echo m9p", "sleep 1", "ls", "cat nonexist", "printf abc", "exit 3", "seq 1 2000", "yes | head -n 3000", "date", "pwd", "env | wc" };
+const builtins = [_][]const u8{
+ "Look", "Exec", "Kill", "Dump", "Msg", "NextColor", "Themes", "DumpThemes", "TreeContext", "PdfFit", "PdfTint", "PdfSections", "Petscii",
+ "Palette", "Ascii", "Save", "New", "Newcol", "Del", "Filter", "Mode", "Togglettymode", "Edit", "Undo", "Redo", "Collapse",
+ "Delcol", "DelAbove", "DelBelow", "Tty", "Repl", "Joincol", "Config", "LocationsConfig", "Messages", "Mini", "Find", "Grep", "Left",
+ "Down", "Up", "Right", "Back", "Forward", "Last", "Recent", "Jumplist", "Colors", "Wrap", "Tagbottom", "Debug", "FocusTint",
+ "Verbose", "Motion", "InactiveDim", "DumpDir", "Theme", "Placement", "BootShell", "LookWord", "Get", "Put", "Undo", "Redo", "Zerox",
+ "Snarf", "Paste", "Cut", "Sort", "Font", "Local",
+};
+const pane_files = [_][]const u8{ "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", "sel", "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", "pty", "pty/run", "pty/data", "pty/ctl", "pty/status" };
+const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "" };
+const long_x: [300]u8 = @splat('x');
+const col_files = [_][]const u8{ "tag", "ctl", "exec" };
+const writable = [_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "body", "data", "xdata", "addr", "dot", "limit", "name", "sel", "dirty", "mark", "scroll", "errors", "event", "pty/run", "pty/data", "pty/ctl", "log" };
+// sam's commands without the ones that reach files or shells.
+const edit_chars = "abcdghijklmopqstuvxyz0123456789 ACEGHIJKLMNOPQRSTUVWXYZ_-";
+
+fn oom() noreturn {
+ @panic("out of memory");
+}
+fn cat(a: Allocator, parts: []const []const u8) []u8 {
+ return std.mem.concat(a, u8, parts) catch oom();
+}
+fn print(a: Allocator, comptime f: []const u8, args: anytype) []u8 {
+ return std.fmt.allocPrint(a, f, args) catch oom();
+}
+fn chance(r: Random) f64 {
+ return r.float(f64);
+}
+fn below(r: Random, n: usize) usize {
+ return r.uintLessThan(usize, n);
+}
+fn range(r: Random, lo: usize, hi: usize) usize {
+ return r.intRangeLessThan(usize, lo, hi);
+}
+fn pick(r: Random, xs: anytype) @TypeOf(xs[0]) {
+ return xs[below(r, xs.len)];
+}
+fn repeat(a: Allocator, unit: []const u8, n: usize) []u8 {
+ const out = a.alloc(u8, unit.len * n) catch oom();
+ for (0..n) |i| @memcpy(out[i * unit.len ..][0..unit.len], unit);
+ return out;
+}
+fn base(path: []const u8) []const u8 {
+ return path[if (std.mem.lastIndexOfScalar(u8, path, '/')) |i| i + 1 else 0..];
+}
+
+/// No path or shell metacharacters, whatever else it holds.
+fn shellSafe(a: Allocator, data: []const u8) []u8 {
+ var out: std.ArrayList(u8) = .empty;
+ for (data) |b| if (std.mem.indexOfScalar(u8, unsafe, b) == null) out.append(a, b) catch oom();
+ return out.items;
+}
+
+fn oddBytes(r: Random, a: Allocator, n: usize) []u8 {
+ var out: std.ArrayList(u8) = .empty;
+ while (out.items.len < n) {
+ const x = chance(r);
+ if (x < 0.5) {
+ out.appendSlice(a, pick(r, &words)) catch oom();
+ } else if (x < 0.7) {
+ out.appendSlice(a, pick(r, &runes)) catch oom();
+ } else if (x < 0.9) {
+ out.appendSlice(a, pick(r, &odd)) catch oom();
+ } else out.append(a, r.int(u8)) catch oom();
+ if (chance(r) < 0.5) out.append(a, ' ') catch oom();
+ }
+ return out.items[0..n];
+}
+
+/// Plain or odd bytes, sometimes huge, with or without a newline.
+fn payload(r: Random, a: Allocator, maxlen: usize) []u8 {
+ const x = chance(r);
+ var data: []const u8 = if (x < 0.35)
+ pick(r, &words)
+ else if (x < 0.55)
+ oddBytes(r, a, range(r, 0, maxlen))
+ else if (x < 0.62)
+ pick(r, &odd)
+ else if (x < 0.66)
+ repeat(a, cat(a, &.{ pick(r, &words), " " }), range(r, 100, 20000))
+ else if (x < 0.7)
+ repeat(a, "x", pick(r, &[_]usize{ 255, 256, 1023, 1024, 1025, 65535, 65536, 70000 }))
+ else
+ cat(a, &.{ pick(r, &words), " ", oddBytes(r, a, range(r, 0, 12)) });
+ if (chance(r) < 0.6 and !std.mem.endsWith(u8, data, "\n")) data = cat(a, &.{ data, pick(r, &[_][]const u8{ "\n", "\n", "\r\n", "\n\n" }) });
+ return shellSafe(a, data);
+}
+
+fn weirdName(r: Random, a: Allocator) []const u8 {
+ const x = chance(r);
+ if (x < 0.3) return repeat(a, "x", pick(r, &[_]usize{ 255, 256, 300, 1000 }));
+ if (x < 0.5) return pick(r, &[_][]const u8{ "..", ".", "", " ", "new", "NEW", "01", "1 ", "\n", "a\x00b" });
+ if (x < 0.7) return pick(r, &runes);
+ const out = a.alloc(u8, range(r, 1, 20)) catch oom();
+ for (out) |*b| b.* = @intCast(range(r, 1, 256));
+ std.mem.replaceScalar(u8, out, '/', '_');
+ return out;
+}
+
+/// A live pane as a token (`{pN}`), a stale one (`{gN}`: a serial seen and
+/// since closed), or an impossible serial, literally.
+fn pickSerial(r: Random, a: Allocator) []const u8 {
+ const x = chance(r);
+ if (x < 0.85) return print(a, "{{p{d}}}", .{below(r, 64)});
+ if (x < 0.95) return print(a, "{{g{d}}}", .{below(r, 64)});
+ return pick(r, &[_][]const u8{ "0", "1", "999", "4294967295", "4294967296", "-1" });
+}
+
+fn pickCol(r: Random, a: Allocator) []const u8 {
+ if (chance(r) < 0.85) return print(a, "{{c{d}}}", .{below(r, 16)});
+ return pick(r, &[_][]const u8{ "0", "99", "4294967295" });
+}
+
+fn randomPath(r: Random, a: Allocator, write: bool) []const u8 {
+ const x = chance(r);
+ if (x < 0.6) return print(a, "/pane/{s}/{s}", .{ pickSerial(r, a), if (write) pick(r, &writable) else pick(r, &pane_files) });
+ if (x < 0.75) return cat(a, &.{ "/", if (write) pick(r, &[_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "log" }) else pick(r, &root_files) });
+ if (x < 0.85) return print(a, "/col/{s}/{s}", .{ pickCol(r, a), pick(r, &col_files) });
+ if (x < 0.93) return print(a, "/pane/{s}/{s}", .{ pickSerial(r, a), weirdName(r, a) });
+ var out: []const u8 = "";
+ for (0..range(r, 1, 4)) |_| out = cat(a, &.{ out, "/", weirdName(r, a) });
+ return out;
+}
+
+/// The payload a file is likely to take, often mutated.
+fn dataFor(r: Random, a: Allocator, path: []const u8) []const u8 {
+ const b = base(path);
+ const is = struct {
+ fn any(name: []const u8, set: []const []const u8) bool {
+ for (set) |s| if (std.mem.eql(u8, name, s)) return true;
+ return false;
+ }
+ }.any;
+ if (chance(r) < 0.25) return payload(r, a, 64);
+ if (is(b, &.{ "addr", "dot", "limit", "sel" })) return address(r, a);
+ if (std.mem.eql(u8, b, "ctl") and std.mem.endsWith(u8, path, "pty/ctl"))
+ return pick(r, &[_][]const u8{ "sig INT\n", "sig TERM\n", "sig HUP\n", "sig QUIT\n", "sig KILL\n", "winsize 80 24\n", "winsize 1 1\n", "winsize 99999 3\n", "exec\n", "sig\n" });
+ if (is(b, &.{ "ctl", "exec", "tagexec" })) return builtinLine(r, a);
+ if (std.mem.eql(u8, b, "run")) return cat(a, &.{ shellSafe(a, pick(r, &shell_lines)), "\n" });
+ if (std.mem.eql(u8, b, "data") and std.mem.indexOf(u8, path, "/pty/") != null)
+ return cat(a, &.{ shellSafe(a, pick(r, &shell_lines)), pick(r, &[_][]const u8{ "\r", "\n", "", "\x03", "\x04" }) });
+ if (std.mem.eql(u8, b, "event")) return eventRecord(r, a);
+ if (std.mem.eql(u8, b, "focus")) return cat(a, &.{ pickSerial(r, a), pick(r, &[_][]const u8{ "\n", "" }) });
+ if (std.mem.eql(u8, b, "look"))
+ return cat(a, &.{ shellSafe(a, pick(r, &[_][]const u8{ "alpha", "alpha.txt", "alpha.txt:2", "utf8.txt:#3", "nothere", "bad.txt", "crlf.txt:1", "gamma", "beta" })), "\n" });
+ if (is(b, &.{ "dirty", "mark", "scroll" })) return pick(r, &[_][]const u8{ "0", "1", "0\n", "1\n", "2\n", "", "yes\n" });
+ if (std.mem.eql(u8, b, "log")) return pick(r, &[_][]const u8{ "follow\n", "follow new\n", "follow", "nope\n" });
+ return payload(r, a, 200);
+}
+
+fn mutate(r: Random, a: Allocator, data: []const u8) []u8 {
+ var d: std.ArrayList(u8) = .empty;
+ d.appendSlice(a, data) catch oom();
+ for (0..range(r, 1, 4)) |_| {
+ if (d.items.len == 0) {
+ d.appendSlice(a, pick(r, &words)) catch oom();
+ continue;
+ }
+ const i = below(r, d.items.len);
+ const x = chance(r);
+ if (x < 0.3) {
+ _ = d.orderedRemove(i);
+ } else if (x < 0.55) {
+ const o = pick(r, &odd);
+ d.insertSlice(a, i, shellSafe(a, if (o.len == 0) "\x00" else o)) catch oom();
+ } else if (x < 0.75) {
+ const j = range(r, i, d.items.len + 1);
+ d.insertSlice(a, i, a.dupe(u8, d.items[i..j]) catch oom()) catch oom();
+ } else d.insertSlice(a, i, pick(r, &runes)) catch oom();
+ }
+ return d.items;
+}
+
+fn builtinLine(r: Random, a: Allocator) []const u8 {
+ const word = pick(r, &builtins);
+ const x = chance(r);
+ var line: []const u8 = if (x < 0.4)
+ word
+ else if (x < 0.7)
+ cat(a, &.{ word, " ", pick(r, &words) })
+ else if (x < 0.8)
+ return cat(a, &.{ "Edit ", editCommand(r, a), "\n" }) // Edit text keeps its / delimiters
+ else
+ mutate(r, a, cat(a, &.{ word, " ", pick(r, &words) }));
+ if (chance(r) < 0.1) line = shellSafe(a, pick(r, &shell_lines));
+ if (chance(r) < 0.8) line = cat(a, &.{ line, "\n" });
+ return shellSafe(a, line);
+}
+
+fn editText(r: Random, a: Allocator) []const u8 {
+ const out = a.alloc(u8, range(r, 0, 8)) catch oom();
+ for (out) |*b| b.* = pick(r, edit_chars);
+ return if (chance(r) < 0.3) cat(a, &.{ out, pick(r, &runes) }) else out;
+}
+
+fn regex(r: Random, a: Allocator) []const u8 {
+ const first = editText(r, a);
+ var out: []const u8 = if (first.len == 0) "a" else first;
+ for (0..range(r, 0, 4)) |_| {
+ const alts = [_][]const u8{ "(a|b)", "^", "$", "[a-z]", "[^ ]", ".*", "\\n", "[\xc3\xa9\xe6\x97\xa5]", "x+", "(", "[", "\\", "a**", "(((a)))", "[z-a]", "" };
+ const k = below(r, alts.len);
+ const alt = if (k == alts.len - 1) cat(a, &.{ repeat(a, "a|", range(r, 1, 300)), "b" }) else alts[k];
+ out = cat(a, &.{ out, alt, editText(r, a) });
+ }
+ return out;
+}
+
+/// sam addresses: valid, near-valid and garbage.
+fn address(r: Random, a: Allocator) []const u8 {
+ const simple = [_][]const u8{ "0", "$", ".", ",", ";", "#0", "#3", "#99999", "1", "2", "3:2", "1:1", "0:0", "#1,#2", "1,$", "2,1", "-", "+", "-1", "+2", "#-1", "99999", "1:99", "?a?" };
+ const x = chance(r);
+ const addr: []const u8 = if (x < 0.4)
+ pick(r, &simple)
+ else if (x < 0.7)
+ cat(a, &.{ "/", regex(r, a), "/" })
+ else if (x < 0.85)
+ cat(a, &.{ pick(r, &simple), pick(r, &[_][]const u8{ ",", ";", "+", "-" }), pick(r, &(simple ++ [_][]const u8{"/a/"})) })
+ else
+ mutate(r, a, cat(a, &.{ pick(r, &simple), "/", regex(r, a), "/" }));
+ return cat(a, &.{ addr, pick(r, &[_][]const u8{ "", "\n" }) });
+}
+
+fn editCommand(r: Random, a: Allocator) []const u8 {
+ var cmd: []const u8 = switch (below(r, 19)) {
+ 0 => cat(a, &.{ ",x/", regex(r, a), "/c/", editText(r, a), "/" }),
+ 1 => cat(a, &.{ "s/", regex(r, a), "/", editText(r, a), "/g" }),
+ 2 => cat(a, &.{ "a/", editText(r, a), "/" }),
+ 3 => cat(a, &.{ "i/", editText(r, a), "/" }),
+ 4 => cat(a, &.{ "c/", editText(r, a), "/" }),
+ 5 => "d",
+ 6 => ",d",
+ 7 => cat(a, &.{ ",y/", regex(r, a), "/d" }),
+ 8 => cat(a, &.{ ",x/", regex(r, a), "/g/", regex(r, a), "/d" }),
+ 9 => cat(a, &.{ ",x/", regex(r, a), "/v/", regex(r, a), "/c/", editText(r, a), "/" }),
+ 10 => "1,2m$",
+ 11 => "1t0",
+ 12 => "k",
+ 13 => "p",
+ 14 => "=",
+ 15 => "u",
+ 16 => cat(a, &.{ "{\na/", editText(r, a), "/\ni/", editText(r, a), "/\n}" }),
+ 17 => cat(a, &.{ ",x/\\n/a/", editText(r, a), "/" }),
+ else => cat(a, &.{ "0,$s/", regex(r, a), "/&&/g" }),
+ };
+ if (chance(r) < 0.4) cmd = cat(a, &.{ std.mem.trimEnd(u8, address(r, a), "\n"), cmd });
+ if (chance(r) < 0.2) {
+ // Mutation never brings in a banned command letter or shell character.
+ var out: std.ArrayList(u8) = .empty;
+ for (mutate(r, a, cmd)) |b| if (std.mem.indexOfScalar(u8, "werfbBDn!<>|", b) == null) out.append(a, b) catch oom();
+ cmd = out.items;
+ }
+ return cmd;
+}
+
+fn eventRecord(r: Random, a: Allocator) []const u8 {
+ const text = shellSafe(a, editText(r, a));
+ var rec: []const u8 = print(a, "{c}{c}{d} {d} {d} {d} {s}\n", .{
+ pick(r, "EFKMZ"), pick(r, "xXlLiIdDZ"),
+ pick(r, &[_]i64{ 0, 1, 5, 10 }), pick(r, &[_]i64{ 0, 2, 7, 1 << 40, -1 }),
+ pick(r, &[_]u8{ 0, 1, 2, 8 }), text.len,
+ text,
+ });
+ if (chance(r) < 0.3) rec = mutate(r, a, rec);
+ return shellSafe(a, rec);
+}
+
+// ---------------------------------------------------------------------------
+// Ops: what a step does, drawn from the seed alone.
+
+const Chunks = union(enum) { whole, bytes, sizes: [4]usize };
+const Op = union(enum) {
+ /// open(path, mode); write data in the given chunk sizes; clunk.
+ write: struct { path: []const u8, data: []const u8, mode: u8 = OWRITE, chunks: Chunks = .whole, offset: ?u64 = null },
+ /// open(path, mode); read from offset in chunks of count until EOF or `reads` reads.
+ read: struct { path: []const u8, count: u32 = 8192, offset: u64 = 0, reads: u8 = 4, mode: u8 = OREAD },
+ stat: []const u8,
+ remove: []const u8,
+ create: struct { path: []const u8, name: []const u8, perm: u32, mode: u8 },
+ /// An open kept across later ops; `hwrite`, `hread` and `clunk` name
+ /// the held ones by index, mod how many are held.
+ hold: struct { path: []const u8, mode: u8 },
+ /// Its data drawn, when it runs, from `seed` and the held file's name.
+ hwrite: struct { h: usize, seed: u64 },
+ hread: struct { h: usize, count: u32, offset: ?u64 },
+ clunk: usize,
+ /// Restore [f] written to /ctl: refused, or (a real session) the server
+ /// hangs up and serves the dump's panes again.
+ restore: []const u8,
+ /// A change on disk in the root: a file written, or deleted (null).
+ host: struct { name: []const u8, data: ?[]const u8 },
+ seq: []const Op,
+ /// Down to the empty window (every pane closed but `keep`, the columns
+ /// too), sometimes rebuilt with a new pane.
+ clear: struct { keep: ?usize, cols: bool, rebuild: bool },
+};
+
+fn seq(a: Allocator, ops: []const Op) Op {
+ return .{ .seq = a.dupe(Op, ops) catch oom() };
+}
+
+fn gen(r: Random, a: Allocator) Op {
+ const weights = [_]u8{ 30, 14, 4, 5, 2, 2, 2, 6, 6, 6, 8, 3, 3, 2, 3, 3 };
+ var w = below(r, 99);
+ var k: usize = 0;
+ while (w >= weights[k]) : (k += 1) w -= weights[k];
+ switch (k) {
+ 0 => {
+ const path = randomPath(r, a, true);
+ const data = dataFor(r, a, path);
+ const mode = pick(r, &[_]u8{ OWRITE, OWRITE, ORDWR, OWRITE | OTRUNC, ORDWR | OTRUNC, OREAD });
+ const x = chance(r);
+ const chunks: Chunks = if (x < 0.08 and data.len <= 64) .bytes else if (x < 0.15) .{ .sizes = .{ range(r, 1, 16), range(r, 1, 16), range(r, 1, 16), range(r, 1, 16) } } else .whole;
+ const offset: ?u64 = if (chance(r) < 0.05) pick(r, &[_]u64{ 1, 100, 1 << 40 }) else null;
+ return .{ .write = .{ .path = path, .data = data, .mode = mode, .chunks = chunks, .offset = offset } };
+ },
+ 1 => return .{ .read = .{
+ .path = randomPath(r, a, false),
+ .count = pick(r, &[_]u32{ 1, 2, 7, 64, 200, 8192, 1 << 20 }),
+ .offset = pick(r, &[_]u64{ 0, 0, 0, 1, 3, 100, 1 << 33 }),
+ .reads = pick(r, &[_]u8{ 1, 2, 8 }),
+ } },
+ 2 => {
+ var path = randomPath(r, a, false);
+ if (chance(r) < 0.2) path = cat(a, &.{ "/os/", pick(r, &[_][]const u8{ "etc", "etc/hosts", "nonexistent", &long_x }) });
+ return .{ .stat = path };
+ },
+ 3 => {
+ if (chance(r) < 0.7) return .{ .read = .{ .path = "/pane/new", .count = 64, .reads = 1 } };
+ return .{ .hold = .{ .path = "/pane/new", .mode = pick(r, &[_]u8{ OREAD, ORDWR }) } };
+ },
+ 4 => {
+ const x = chance(r);
+ return .{ .remove = if (x < 0.6) print(a, "/pane/{s}", .{pickSerial(r, a)}) else if (x < 0.85) print(a, "/col/{s}", .{pickCol(r, a)}) else randomPath(r, a, false) };
+ },
+ 5 => return .{ .create = .{
+ .path = pick(r, &[_][]const u8{ "/col", "/pane", "/", print(a, "/pane/{s}", .{pickSerial(r, a)}) }),
+ .name = if (chance(r) < 0.5) weirdName(r, a) else "new",
+ .perm = pick(r, &[_]u32{ 0o666, DMDIR | 0o777 }),
+ .mode = pick(r, &[_]u8{ OREAD, OWRITE }),
+ } },
+ 6 => return .{ .clear = .{ .keep = if (chance(r) < 0.8) below(r, 64) else null, .cols = chance(r) < 0.5, .rebuild = chance(r) < 0.6 } },
+ 7 => {
+ const x = chance(r);
+ if (x < 0.35) {
+ var path = randomPath(r, a, false);
+ if (chance(r) < 0.3) path = pick(r, &[_][]const u8{ "/log", print(a, "/pane/{s}/event", .{pickSerial(r, a)}), "/screen", "/index", print(a, "/pane/{s}/ctl", .{pickSerial(r, a)}) });
+ return .{ .hold = .{ .path = path, .mode = pick(r, &[_]u8{ OREAD, ORDWR, OWRITE }) } };
+ }
+ const h = below(r, 64);
+ if (x < 0.6) return .{ .hwrite = .{ .h = h, .seed = r.int(u64) } };
+ if (x < 0.85) return .{ .hread = .{ .h = h, .count = pick(r, &[_]u32{ 1, 16, 8192 }), .offset = pick(r, &[_]?u64{ null, null, 0, 5 }) } };
+ return .{ .clunk = h };
+ },
+ 8 => {
+ const s = pickSerial(r, a);
+ const line = shellSafe(a, pick(r, &shell_lines));
+ const x = chance(r);
+ if (x < 0.25) return .{ .write = .{ .path = print(a, "/pane/{s}/ctl", .{s}), .data = "Tty\n" } };
+ if (x < 0.45) return .{ .write = .{ .path = print(a, "/pane/{s}/pty/run", .{s}), .data = cat(a, &.{ line, "\n" }), .mode = ORDWR } };
+ if (x < 0.6) return .{ .write = .{ .path = print(a, "/pane/{s}/pty/data", .{s}), .data = cat(a, &.{ line, "\r" }) } };
+ if (x < 0.75) return .{ .write = .{ .path = print(a, "/pane/{s}/pty/ctl", .{s}), .data = pick(r, &[_][]const u8{ "sig INT\n", "sig TERM\n", "sig KILL\n", "sig HUP\n" }) } };
+ return .{ .write = .{ .path = pick(r, &[_][]const u8{ "/exec", print(a, "/pane/{s}/exec", .{s}) }), .data = cat(a, &.{ line, "\n" }) } };
+ },
+ 9 => {
+ const s = pickSerial(r, a);
+ const path = pick(r, &[_][]const u8{ print(a, "/pane/{s}/ctl", .{s}), print(a, "/pane/{s}/exec", .{s}), "/exec" });
+ return .{ .write = .{ .path = path, .data = cat(a, &.{ "Edit ", editCommand(r, a), "\n" }) } };
+ },
+ 10 => {
+ const s = pickSerial(r, a);
+ const first: Op = .{ .write = .{ .path = print(a, "/pane/{s}/addr", .{s}), .data = address(r, a) } };
+ const x = chance(r);
+ if (x < 0.4) return seq(a, &.{ first, .{ .read = .{ .path = print(a, "/pane/{s}/{s}", .{ s, pick(r, &[_][]const u8{ "xdata", "data", "addr", "dot" }) }), .reads = 2 } } });
+ if (x < 0.7) return seq(a, &.{ first, .{ .write = .{
+ .path = print(a, "/pane/{s}/{s}", .{ s, pick(r, &[_][]const u8{ "data", "xdata" }) }),
+ .data = payload(r, a, 20),
+ .mode = pick(r, &[_]u8{ OWRITE, OWRITE | OTRUNC }),
+ } } });
+ return seq(a, &.{first});
+ },
+ 11 => return .{ .write = .{
+ .path = pick(r, &[_][]const u8{ print(a, "/pane/{s}/tag", .{pickSerial(r, a)}), "/tag", print(a, "/col/{s}/tag", .{pickCol(r, a)}) }),
+ .data = payload(r, a, 40),
+ .mode = pick(r, &[_]u8{ OWRITE, OWRITE | OTRUNC }),
+ } },
+ 12 => return .{ .hold = .{ .path = print(a, "/pane/{s}/event", .{pickSerial(r, a)}), .mode = ORDWR } },
+ 13 => {
+ if (chance(r) < 0.5) return .{ .write = .{ .path = "/ctl", .data = pick(r, &[_][]const u8{ "Dump\n", "Dump", "Dump m9pdump\n" }) } };
+ return .{ .restore = pick(r, &[_][]const u8{ "Restore\n", "Restore\n", "Restore m9pdump\n", "Restore nothere\n" }) };
+ },
+ 14 => {
+ const name = pick(r, &fixture_names);
+ if (chance(r) < 0.5) return .{ .host = .{ .name = name, .data = null } };
+ return .{ .host = .{ .name = name, .data = payload(r, a, 100) } };
+ },
+ else => {
+ const name = pick(r, &fixture_names);
+ const suffix = pick(r, &[_][]const u8{ "", ":2", ":#4", ":1:3", ":/beta/" });
+ return .{ .write = .{ .path = pick(r, &[_][]const u8{ "/look", print(a, "/pane/{s}/look", .{pickSerial(r, a)}) }), .data = cat(a, &.{ name, suffix, "\n" }) } };
+ },
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Running an op: the requests it made, as the rules see them.
+
+const Kind = enum { walk, open, open_new, trunc, write, write_badfid, read, clunk, clunk_write, stat, remove, create, close_runs, allow };
+/// `ok` null: held (a read parked, as a read of the log with nothing new is).
+const Rec = struct { kind: Kind, path: []const u8, ok: ?bool, why: []const u8 = "", n: usize = 0 };
+const Res = struct {
+ recs: std.ArrayList(Rec) = .empty,
+ /// The op expected the server to hang up (a Restore taken).
+ hung_up: bool = false,
+ note: []const u8 = "",
+ window: ?[]const []const u8 = null,
+
+ fn add(res: *Res, a: Allocator, rec: Rec) void {
+ res.recs.append(a, rec) catch oom();
+ }
+
+ fn isWrite(rec: Rec) bool {
+ return rec.kind == .write or rec.kind == .clunk_write or rec.kind == .open_new or rec.kind == .trunc;
+ }
+};
+
+/// An open of the fuzzer's: the session's walked fid, its mode, and where
+/// a directory's reading has got to (an engine's diroff, in entries).
+const Fid = struct { w: Session.Walked, mode: u8 = 0, pos: u64 = 0, index: u64 = 0 };
+const Handle = struct { fid: Fid, path: []const u8, off: u64 = 0, last: []const u8 = "" };
+
+const Fail = error{ Fail, Ended };
+
+const Ctx = struct {
+ gpa: Allocator,
+ seed: u64,
+ root: []const u8,
+ verbose: bool,
+ sess: ?Session = null,
+ /// What lasts as long as a session.
+ life: std.heap.ArenaAllocator,
+ serials: std.ArrayList(u32) = .empty,
+ cols: std.ArrayList(u32) = .empty,
+ /// Serials seen and since closed, the last 20.
+ gone: std.ArrayList(u32) = .empty,
+ handles: std.ArrayList(Handle) = .empty,
+ /// A second open holding `follow new` on /log, and the lines it read.
+ follower: ?Session.Walked = null,
+ lines: std.ArrayList([]const u8) = .empty,
+ marker: ?usize = null,
+ step: usize = 0,
+ fail_kind: []const u8 = "",
+ fail_why: []const u8 = "",
+
+ fn s(c: *Ctx) *Session {
+ return &c.sess.?;
+ }
+
+ fn fail(c: *Ctx, a: Allocator, kind: []const u8, comptime f: []const u8, args: anytype) error{Fail} {
+ c.fail_kind = kind;
+ c.fail_why = print(a, f, args);
+ return error.Fail;
+ }
+
+ /// A request answered `.again` that nothing at T1 would answer later
+ /// is a request the real server would hold until its timeout.
+ fn hang(c: *Ctx, a: Allocator, what: []const u8, path: []const u8) error{Fail} {
+ return c.fail(a, "hang", "{s} {s}: held, never answered", .{ what, path });
+ }
+
+ /// A new session: the fixtures on disk, three of them looked at (so one
+ /// close does not end it), the session's own first pane removed, and
+ /// half the time a follower of the log.
+ fn fresh(c: *Ctx, at_step: usize) !void {
+ if (c.sess) |*old| old.deinit();
+ c.sess = null;
+ _ = c.life.reset(.free_all);
+ c.serials = .empty;
+ c.cols = .empty;
+ c.gone = .empty;
+ c.handles = .empty;
+ c.lines = .empty;
+ c.follower = null;
+ c.marker = null;
+ const la = c.life.allocator();
+ for (fixtures) |fx| try putFile(la, c.root, fx[0], fx[1]);
+ removeFile(la, c.root, "fresh.txt");
+ c.sess = try Session.initFile(c.gpa, fixtures[0][1]);
+ const s_ = c.s();
+ const first = try serialsOf(la, (try readAll(c, la, "/index")) orelse "");
+ for (fixtures[0..3]) |fx| try s_.write("/look", print(la, "{s}/{s}\n", .{ c.root, fx[0] }));
+ for (first) |serial| if (try s_.walk(print(la, "/pane/{d}", .{serial}))) |w| {
+ var walked = w;
+ _ = s_.ask(&walked, .remove, .{});
+ };
+ c.serials = .fromOwnedSlice(try serialsOf(la, (try readAll(c, la, "/index")) orelse ""));
+ if (std.hash.Wyhash.hash(c.seed, std.mem.asBytes(&at_step)) & 1 == 1) {
+ var w = (try s_.walk("/log")) orelse return error.NoLog;
+ if (s_.ask(&w, .open, .{ .omode = ORDWR }).errno != 0) return error.NoLog;
+ if (s_.ask(&w, .write, .{ .data = "follow new\n" }).errno != 0) return error.NoFollow;
+ c.follower = w;
+ }
+ }
+};
+
+fn putFile(a: Allocator, dir: []const u8, name: []const u8, data: []const u8) !void {
+ const path = try std.fmt.allocPrintSentinel(a, "{s}/{s}", .{ dir, name }, 0);
+ const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644));
+ if (fd < 0) return error.CannotWrite;
+ defer _ = libc.close(fd);
+ if (data.len > 0 and libc.write(fd, data.ptr, data.len) != @as(isize, @intCast(data.len))) return error.CannotWrite;
+}
+
+fn removeFile(a: Allocator, dir: []const u8, name: []const u8) void {
+ const path = std.fmt.allocPrintSentinel(a, "{s}/{s}", .{ dir, name }, 0) catch oom();
+ _ = libc.unlink(path);
+}
+
+fn serialsOf(a: Allocator, index: []const u8) ![]u32 {
+ var out: std.ArrayList(u32) = .empty;
+ if (index.len == 0) return out.items;
+ // A blank line is unparsable too, as Python's splitlines saw it.
+ var lines = std.mem.splitScalar(u8, if (index[index.len - 1] == '\n') index[0 .. index.len - 1] else index, '\n');
+ while (lines.next()) |line| {
+ var it = std.mem.tokenizeAny(u8, line, " \t");
+ try out.append(a, std.fmt.parseInt(u32, it.next() orelse return error.Unparsable, 10) catch return error.Unparsable);
+ }
+ return out.items;
+}
+
+fn hostPath(path: []const u8) bool {
+ var it = std.mem.tokenizeScalar(u8, path, '/');
+ const first = it.next() orelse return false;
+ return std.mem.eql(u8, first, "os") or std.mem.eql(u8, first, "src");
+}
+
+fn isHeld(path: []const u8) bool {
+ return std.mem.eql(u8, std.mem.trimEnd(u8, path, "/"), "/log") or std.mem.endsWith(u8, path, "/event") or
+ std.mem.endsWith(u8, path, "/pty/data") or std.mem.endsWith(u8, path, "/pty/run");
+}
+
+/// An open the rule names: pane/new's (it makes a pane) and a truncation
+/// ('every write or truncation the tree refused'); others are `open`.
+fn openKind(path: []const u8, mode: u8) Kind {
+ if (std.mem.eql(u8, std.mem.trimEnd(u8, path, "/"), "/pane/new") and mode & 3 == 0) return .open_new;
+ if (mode & OTRUNC != 0 and mode & 3 != 0) return .trunc;
+ return .open;
+}
+
+/// `path` walked, recorded when the walk is refused.
+fn walk(c: *Ctx, a: Allocator, res: *Res, path: []const u8) !?Fid {
+ const w = (try c.s().walk(path)) orelse {
+ res.add(a, .{ .kind = .walk, .path = path, .ok = false, .why = c.s().said });
+ return null;
+ };
+ return .{ .w = w };
+}
+
+/// The open the engine would let through, refused as it refuses: a mode
+/// bit the file lacks, a directory opened to write; OTRUNC truncates first.
+fn open(c: *Ctx, a: Allocator, res: *Res, f: *Fid, path: []const u8, mode: u8) !bool {
+ const kind = openKind(path, mode);
+ const rw = mode & 3;
+ var need: u16 = 0;
+ if (rw == OREAD or rw == ORDWR) need |= 0o400;
+ if (rw == OWRITE or rw == ORDWR or mode & OTRUNC != 0) need |= 0o200;
+ if (rw == 3 or (f.w.dir and (rw != OREAD or mode & OTRUNC != 0)) or f.w.mode & need != need) {
+ res.add(a, .{ .kind = kind, .path = path, .ok = false, .why = "permission denied" });
+ return false;
+ }
+ if (mode & OTRUNC != 0) {
+ const t = c.s().ask(&f.w, .truncate, .{});
+ if (t.held) return c.hang(a, "truncate", path);
+ if (t.errno != 0) {
+ res.add(a, .{ .kind = kind, .path = path, .ok = false, .why = c.s().said });
+ return false;
+ }
+ }
+ const o = c.s().ask(&f.w, .open, .{ .omode = mode });
+ if (o.held) return c.hang(a, "open", path);
+ res.add(a, .{ .kind = kind, .path = path, .ok = o.errno == 0, .why = if (o.errno != 0) c.s().said else "" });
+ f.mode = mode;
+ return o.errno == 0;
+}
+
+/// One read at `off`: its bytes, or null when refused or held. A directory
+/// reads on from where it got to, or from 0, as the engine's diroff does.
+fn readOnce(c: *Ctx, a: Allocator, res: *Res, f: *Fid, path: []const u8, off: u64, count: u32) !?[]const u8 {
+ if (!f.w.opened or f.mode & 3 == OWRITE) {
+ res.add(a, .{ .kind = .read, .path = path, .ok = false, .why = "bad use of fid" });
+ return null;
+ }
+ if (f.w.dir and off != f.pos) {
+ if (off != 0) {
+ res.add(a, .{ .kind = .read, .path = path, .ok = false, .why = "bad offset in directory read" });
+ return null;
+ }
+ f.pos = 0;
+ f.index = 0;
+ }
+ const ans = c.s().ask(&f.w, if (f.w.dir) .readdir else .read, .{ .off = if (f.w.dir) f.index else off, .size = @min(count, read_max) });
+ if (ans.held) {
+ if (!isHeld(path)) return c.hang(a, "read", path);
+ res.add(a, .{ .kind = .read, .path = path, .ok = null });
+ return null;
+ }
+ res.add(a, .{ .kind = .read, .path = path, .ok = ans.errno == 0, .why = if (ans.errno != 0) c.s().said else "" });
+ if (ans.errno != 0) return null;
+ if (f.w.dir) {
+ f.index += std.mem.count(u8, ans.bytes, "\n");
+ f.pos += ans.bytes.len;
+ }
+ return try a.dupe(u8, ans.bytes);
+}
+
+fn writeOnce(c: *Ctx, a: Allocator, res: *Res, f: *Fid, path: []const u8, off: u64, data: []const u8) !bool {
+ // A write on a fid opened for reading is 9P misuse, refused by the
+ // engine: not a write the tree refused, and the rule does not name it.
+ if (f.mode & 3 == OREAD) {
+ res.add(a, .{ .kind = .write_badfid, .path = path, .ok = false, .why = "bad use of fid" });
+ return false;
+ }
+ const ans = c.s().ask(&f.w, .write, .{ .off = off, .data = data });
+ if (ans.held) return c.hang(a, "write", path);
+ res.add(a, .{ .kind = .write, .path = path, .ok = ans.errno == 0, .why = if (ans.errno != 0) c.s().said else "" });
+ return ans.errno == 0;
+}
+
+fn clunk(c: *Ctx, a: Allocator, res: *Res, f: *Fid, path: []const u8) !bool {
+ const kind: Kind = if (f.mode & 3 != 0) .clunk_write else .clunk;
+ if (!f.w.opened) {
+ res.add(a, .{ .kind = kind, .path = path, .ok = true });
+ return true;
+ }
+ const ans = c.s().ask(&f.w, .clunk, .{});
+ if (ans.held) return c.hang(a, "clunk", path);
+ res.add(a, .{ .kind = kind, .path = path, .ok = ans.errno == 0, .why = if (ans.errno != 0) c.s().said else "" });
+ return ans.errno == 0;
+}
+
+/// The lines a lines file runs for `data` written in pieces of `sizes`
+/// (docs/typ/reference.typ, tree.zig write): each ended line, and the
+/// unended tail of a piece shorter than a Twrite holds, or of one not a
+/// multiple of 4096 bytes, as acme takes each write. An upper bound: the
+/// tree now waits for the newline whatever a piece's size.
+fn servedLines(a: Allocator, data: []const u8, sizes: []const usize, room_: usize) []const []const u8 {
+ var lines: std.ArrayList([]const u8) = .empty;
+ var pending: []const u8 = "";
+ var at: usize = 0;
+ for (sizes) |size| {
+ const piece = data[at..][0..size];
+ at += size;
+ pending = cat(a, &.{ pending, piece });
+ while (std.mem.indexOfScalar(u8, pending, '\n')) |nl| {
+ lines.append(a, pending[0..nl]) catch oom();
+ pending = pending[nl + 1 ..];
+ }
+ if (pending.len > 0 and piece.len < room_ and piece.len % 4096 != 0) {
+ lines.append(a, pending) catch oom();
+ pending = "";
+ }
+ }
+ if (pending.len > 0) lines.append(a, pending) catch oom();
+ return lines.items;
+}
+
+fn selfCheck(a: Allocator) void {
+ const cases = .{
+ .{ "a\nb\n", &[_]usize{4}, &[_][]const u8{ "a", "b" } },
+ .{ "ab\n", &[_]usize{ 1, 1, 1 }, &[_][]const u8{ "a", "b", "" } },
+ .{ "ab\ncd", &[_]usize{ 3, 2 }, &[_][]const u8{ "ab", "cd" } },
+ };
+ // A piece that fills its Twrite goes on into the next.
+ const full = servedLines(a, "abcd", &.{ 2, 2 }, 2);
+ if (full.len != 1 or !std.mem.eql(u8, full[0], "abcd")) @panic("servedLines");
+ inline for (cases) |case| {
+ const got = servedLines(a, case[0], case[1], room);
+ if (got.len != case[2].len) @panic("servedLines");
+ for (got, case[2]) |g, w| if (!std.mem.eql(u8, g, w)) @panic("servedLines");
+ }
+}
+
+/// Errs a successful write may log (docs/typ/reference.typ): a look that
+/// finds nothing logs one, per line, whether written to look, written back
+/// as an event record (acme's xfid.c:842) or run as the Look builtin; an
+/// Edit block whose text never ended runs, and fails, at the close.
+fn allowances(a: Allocator, res: *Res, path: []const u8, data: []const u8, sizes: []const usize) void {
+ const b = base(path);
+ const served = servedLines(a, data, sizes, room);
+ var n: usize = 0;
+ var lines: usize = 0;
+ for (served) |line| {
+ if (std.mem.trim(u8, line, " \t\n\r\x0b\x0c").len > 0) lines += 1;
+ }
+ if (std.mem.eql(u8, b, "look")) {
+ // A look's line is its text, blanks and all: one of blanks alone is
+ // a look too; only an empty one (or a lone \r) is none.
+ for (served) |line| if (std.mem.trimEnd(u8, line, "\r").len > 0) {
+ n += 1;
+ };
+ } else if (std.mem.eql(u8, b, "event")) {
+ n = lines;
+ } else if (std.mem.eql(u8, b, "exec") or std.mem.eql(u8, b, "ctl") or std.mem.eql(u8, b, "tagexec")) {
+ for (served) |line| {
+ var it = std.mem.tokenizeAny(u8, line, " \t\n\r\x0b\x0c");
+ if (it.next()) |w| if (std.mem.eql(u8, w, "Look")) {
+ n += 1;
+ };
+ }
+ if (std.mem.indexOf(u8, data, "Edit") != null and lines > 1) n += 1;
+ } else return;
+ if (n > 0) res.add(a, .{ .kind = .allow, .path = path, .ok = true, .n = n });
+}
+
+fn chunking(a: Allocator, spec: Chunks, n: usize, cap: usize) []const usize {
+ if (n == 0) return &.{0};
+ var out: std.ArrayList(usize) = .empty;
+ var total: usize = 0;
+ switch (spec) {
+ .bytes => for (0..n) |_| out.append(a, 1) catch oom(),
+ .sizes => |sizes| for (sizes) |want| {
+ if (total >= n) break;
+ const size = @max(1, @min(want, cap, n - total));
+ out.append(a, size) catch oom();
+ total += size;
+ },
+ .whole => {},
+ }
+ if (spec != .bytes) while (total < n) {
+ const size = @min(cap, n - total);
+ out.append(a, size) catch oom();
+ total += size;
+ };
+ return out.items;
+}
+
+fn exec(c: *Ctx, a: Allocator, res: *Res, op: Op) anyerror!void {
+ switch (op) {
+ .write => |o| {
+ if (hostPath(o.path)) {
+ res.note = "refused by guard: host tree";
+ return;
+ }
+ var f = (try walk(c, a, res, o.path)) orelse return;
+ const opened = try open(c, a, res, &f, o.path, o.mode);
+ var sent: []const u8 = "";
+ if (opened) {
+ var offset: usize = 0;
+ var took: std.ArrayList(usize) = .empty;
+ for (chunking(a, o.chunks, o.data.len, write_max)) |size| {
+ const piece = o.data[offset..][0..size];
+ const ok = try writeOnce(c, a, res, &f, o.path, o.offset orelse offset, piece);
+ offset += size;
+ sent = o.data[0..offset];
+ if (ok) try took.append(a, size);
+ if (!ok) break;
+ }
+ // Misses are allowed the Twrites that succeeded: a line
+ // written a byte a Twrite is a look a byte, each its own write.
+ var sum: usize = 0;
+ for (took.items) |t| sum += t;
+ allowances(a, res, o.path, o.data[0..sum], took.items);
+ }
+ const closed = try clunk(c, a, res, &f, o.path);
+ // What the open holds when it closes: the last line written, if unended.
+ if (opened and o.mode & 3 != 0 and sent.len > 0 and !std.mem.endsWith(u8, sent, "\n"))
+ res.add(a, .{ .kind = .close_runs, .path = o.path, .ok = closed });
+ },
+ .read => |o| {
+ var f = (try walk(c, a, res, o.path)) orelse return;
+ if (try open(c, a, res, &f, o.path, o.mode)) {
+ var off = o.offset;
+ for (0..o.reads) |_| {
+ const got = (try readOnce(c, a, res, &f, o.path, off, o.count)) orelse break;
+ if (got.len == 0) break;
+ off += got.len;
+ }
+ }
+ _ = try clunk(c, a, res, &f, o.path);
+ },
+ .stat => |path| {
+ var f = (try walk(c, a, res, path)) orelse return;
+ res.add(a, .{ .kind = .walk, .path = path, .ok = true });
+ const ans = c.s().ask(&f.w, .stat, .{});
+ res.add(a, .{ .kind = .stat, .path = path, .ok = ans.errno == 0 });
+ },
+ .remove => |path| {
+ if (hostPath(path)) {
+ res.note = "refused by guard: host tree";
+ return;
+ }
+ var f = (try walk(c, a, res, path)) orelse return;
+ res.add(a, .{ .kind = .walk, .path = path, .ok = true });
+ const ans = c.s().ask(&f.w, .remove, .{});
+ if (ans.held) return c.hang(a, "remove", path);
+ res.add(a, .{ .kind = .remove, .path = path, .ok = ans.errno == 0, .why = if (ans.errno != 0) c.s().said else "" });
+ },
+ .create => |o| {
+ if (hostPath(o.path)) {
+ res.note = "refused by guard: host tree";
+ return;
+ }
+ var f = (try walk(c, a, res, o.path)) orelse return;
+ res.add(a, .{ .kind = .walk, .path = o.path, .ok = true });
+ const ans = c.s().ask(&f.w, .create, .{ .data = o.name, .perm = o.perm, .omode = o.mode });
+ res.add(a, .{ .kind = .create, .path = cat(a, &.{ o.path, "/", o.name }), .ok = ans.errno == 0 });
+ if (f.w.opened) _ = c.s().ask(&f.w, .clunk, .{});
+ },
+ .hold => |o| {
+ if (hostPath(o.path) and o.mode & 3 != 0) {
+ res.note = "refused by guard: host tree";
+ return;
+ }
+ var f = (try walk(c, a, res, o.path)) orelse return;
+ if (try open(c, a, res, &f, o.path, o.mode))
+ try c.handles.append(c.life.allocator(), .{ .fid = f, .path = try c.life.allocator().dupe(u8, o.path) });
+ },
+ .hwrite => |o| {
+ if (c.handles.items.len == 0) {
+ res.note = "no such handle";
+ return;
+ }
+ const h = &c.handles.items[o.h % c.handles.items.len];
+ var prng = Random.DefaultPrng.init(o.seed);
+ const want = resolveData(c, a, h.path, dataFor(prng.random(), a, h.path));
+ const data = want[0..@min(want.len, write_max)]; // one Twrite
+ h.last = try c.life.allocator().dupe(u8, data);
+ if (try writeOnce(c, a, res, &h.fid, h.path, h.off, data)) {
+ allowances(a, res, h.path, data, &.{data.len});
+ h.off += data.len;
+ }
+ },
+ .hread => |o| {
+ if (c.handles.items.len == 0) {
+ res.note = "no such handle";
+ return;
+ }
+ const h = &c.handles.items[o.h % c.handles.items.len];
+ const offset = o.offset orelse h.off;
+ if (try readOnce(c, a, res, &h.fid, h.path, offset, o.count)) |got| h.off = offset + got.len;
+ },
+ .clunk => |i| {
+ if (c.handles.items.len == 0) {
+ res.note = "no such handle";
+ return;
+ }
+ var h = c.handles.orderedRemove(i % c.handles.items.len);
+ const ok = try clunk(c, a, res, &h.fid, h.path);
+ if (h.fid.mode & 3 != 0 and h.last.len > 0 and !std.mem.endsWith(u8, h.last, "\n"))
+ res.add(a, .{ .kind = .close_runs, .path = h.path, .ok = ok });
+ },
+ .restore => |data| {
+ var f = (try walk(c, a, res, "/ctl")) orelse return;
+ if (!try open(c, a, res, &f, "/ctl", OWRITE)) return;
+ const ok = try writeOnce(c, a, res, &f, "/ctl", 0, data);
+ _ = try clunk(c, a, res, &f, "/ctl");
+ if (!ok) return;
+ // Taken: a real server hangs every connection up and serves the
+ // dump's panes again. What the connections held is let go.
+ res.hung_up = true;
+ for (c.handles.items) |*h| _ = c.s().ask(&h.fid.w, .clunk, .{});
+ c.handles.clearRetainingCapacity();
+ c.marker = null;
+ },
+ .host => |o| if (o.data) |data| try putFile(a, c.root, o.name, data) else removeFile(a, c.root, o.name),
+ .seq => |ops| for (ops) |sub| try exec(c, a, res, sub),
+ .clear => unreachable, // resolved into a seq
+ }
+}
+
+/// Tokens replaced with what is live when the op runs: `{pN}` the Nth
+/// serial of /index, mod its length (0 when there is none), `{cN}` a
+/// column's, `{gN}` a stale serial's (999 when there is none).
+fn resolveStr(c: *Ctx, a: Allocator, text: []const u8) []const u8 {
+ if (std.mem.indexOfScalar(u8, text, '{') == null) return text;
+ var out: std.ArrayList(u8) = .empty;
+ var i: usize = 0;
+ while (i < text.len) {
+ if (text[i] == '{' and i + 2 < text.len and std.mem.indexOfScalar(u8, "pcg", text[i + 1]) != null) {
+ var j = i + 2;
+ while (j < text.len and std.ascii.isDigit(text[j])) j += 1;
+ if (j > i + 2 and j < text.len and text[j] == '}') {
+ const n = std.fmt.parseInt(usize, text[i + 2 .. j], 10) catch 0;
+ const live = switch (text[i + 1]) {
+ 'p' => c.serials.items,
+ 'c' => c.cols.items,
+ else => c.gone.items,
+ };
+ const none: u32 = if (text[i + 1] == 'g') 999 else 0;
+ out.print(a, "{d}", .{if (live.len > 0) live[n % live.len] else none}) catch oom();
+ i = j + 1;
+ continue;
+ }
+ }
+ out.append(a, text[i]) catch oom();
+ i += 1;
+ }
+ return out.items;
+}
+
+fn resolveData(c: *Ctx, a: Allocator, path: []const u8, data: []const u8) []const u8 {
+ return if (std.mem.indexOf(u8, path, "/focus") != null) resolveStr(c, a, data) else data;
+}
+
+fn resolve(c: *Ctx, a: Allocator, op: Op) Op {
+ return switch (op) {
+ .write => |o| blk: {
+ var w = o;
+ w.path = resolveStr(c, a, o.path);
+ w.data = resolveData(c, a, w.path, o.data);
+ break :blk .{ .write = w };
+ },
+ .read => |o| blk: {
+ var r = o;
+ r.path = resolveStr(c, a, o.path);
+ break :blk .{ .read = r };
+ },
+ .stat => |p| .{ .stat = resolveStr(c, a, p) },
+ .remove => |p| .{ .remove = resolveStr(c, a, p) },
+ .create => |o| blk: {
+ var r = o;
+ r.path = resolveStr(c, a, o.path);
+ break :blk .{ .create = r };
+ },
+ .hold => |o| .{ .hold = .{ .path = resolveStr(c, a, o.path), .mode = o.mode } },
+ .seq => |ops| blk: {
+ const out = a.alloc(Op, ops.len) catch oom();
+ for (ops, out) |o, *r| r.* = resolve(c, a, o);
+ break :blk .{ .seq = out };
+ },
+ .clear => |o| blk: {
+ var ops: std.ArrayList(Op) = .empty;
+ const keep: ?u32 = if (o.keep) |k| (if (c.serials.items.len > 0) c.serials.items[k % c.serials.items.len] else null) else null;
+ for (c.serials.items) |serial| if (serial != keep) ops.append(a, .{ .remove = print(a, "/pane/{d}", .{serial}) }) catch oom();
+ if (o.cols) for (c.cols.items) |col| ops.append(a, .{ .remove = print(a, "/col/{d}", .{col}) }) catch oom();
+ if (o.rebuild) ops.append(a, .{ .read = .{ .path = "/pane/new", .count = 64, .reads = 1 } }) catch oom();
+ break :blk .{ .seq = ops.items };
+ },
+ else => op,
+ };
+}
+
+fn opData(op: Op) []const u8 {
+ const data = switch (op) {
+ .write => |o| o.data,
+ .restore => |d| d,
+ .host => |o| o.data orelse "",
+ else => "",
+ };
+ return data[0..@min(data.len, 200)];
+}
+
+fn opPath(op: Op) []const u8 {
+ return switch (op) {
+ .write => |o| o.path,
+ .read => |o| o.path,
+ .stat, .remove, .restore => |p| p,
+ .create => |o| o.path,
+ .hold => |o| o.path,
+ .host => |o| o.name,
+ else => "",
+ };
+}
+
+// ---------------------------------------------------------------------------
+// Reading the session back.
+
+fn readAll(c: *Ctx, a: Allocator, path: []const u8) !?[]const u8 {
+ const s = c.s();
+ var w = (try s.walk(path)) orelse return null;
+ if (s.ask(&w, .open, .{}).errno != 0) return null;
+ defer _ = s.ask(&w, .clunk, .{});
+ return try readFid(c, a, &w, read_max);
+}
+
+/// A whole file on an open, read from 0 in `count`-byte reads; a
+/// directory's names, a line each (one entry a read when `count` is not a
+/// whole read's).
+fn readFid(c: *Ctx, a: Allocator, w: *Session.Walked, count: u32) ![]const u8 {
+ var out: std.ArrayList(u8) = .empty;
+ if (w.dir) {
+ var index: u64 = 0;
+ while (true) {
+ const ans = c.s().ask(w, .readdir, .{ .off = index, .size = count });
+ if (ans.errno != 0) return error.ReadRefused;
+ if (ans.bytes.len == 0) break;
+ if (count == read_max) {
+ try out.appendSlice(a, ans.bytes);
+ break;
+ }
+ try out.appendSlice(a, ans.bytes[0 .. std.mem.indexOfScalar(u8, ans.bytes, '\n').? + 1]);
+ index += 1;
+ }
+ return out.items;
+ }
+ while (out.items.len < 1 << 22) {
+ const ans = c.s().ask(w, .read, .{ .off = out.items.len, .size = count });
+ if (ans.held) break;
+ if (ans.errno != 0) return error.ReadRefused;
+ if (ans.bytes.len == 0) break;
+ try out.appendSlice(a, ans.bytes);
+ }
+ return out.items;
+}
+
+fn names(c: *Ctx, a: Allocator, path: []const u8) !?[]const []const u8 {
+ var w = (try c.s().walk(path)) orelse return null;
+ if (c.s().ask(&w, .open, .{}).errno != 0) return null;
+ defer _ = c.s().ask(&w, .clunk, .{});
+ return try splitLines(a, try readFid(c, a, &w, read_max));
+}
+
+fn splitLines(a: Allocator, text: []const u8) ![]const []const u8 {
+ var out: std.ArrayList([]const u8) = .empty;
+ var it = std.mem.splitScalar(u8, text, '\n');
+ while (it.next()) |line| try out.append(a, line);
+ if (out.items.len > 0 and out.items[out.items.len - 1].len == 0) _ = out.pop();
+ return out.items;
+}
+
+fn isDigits(text: []const u8) bool {
+ if (text.len == 0) return false;
+ for (text) |b| if (!std.ascii.isDigit(b)) return false;
+ return true;
+}
+
+// ---------------------------------------------------------------------------
+// The log window: the records one step caused, between two unique markers.
+
+/// An unknown ctl word logs `err - ctl: unknown control message "<word>"`.
+fn marker(c: *Ctx, a: Allocator) !?[]const []const u8 {
+ const s = c.s();
+ const word = print(a, "m9p-mark-{d}", .{c.step});
+ const quoted = print(a, "\"{s}\"", .{word});
+ if (try s.refused("/ctl", print(a, "{s}\n", .{word})) == 0) return c.fail(a, "log", "marker write was taken", .{});
+ const prev = c.marker;
+ c.marker = c.step;
+ const prev_quoted: ?[]const u8 = if (prev) |p| print(a, "\"m9p-mark-{d}\"", .{p}) else null;
+ if (c.follower) |*w| {
+ const la = c.life.allocator();
+ while (true) {
+ const ans = s.ask(w, .read, .{ .size = 8192 });
+ if (ans.held or ans.errno != 0 or ans.bytes.len == 0) break;
+ for (try splitLines(a, ans.bytes)) |line| try c.lines.append(la, try la.dupe(u8, line));
+ }
+ const lines = c.lines.items;
+ const end = lastWith(lines, quoted) orelse return c.fail(a, "log", "marker record never reached the follower", .{});
+ var start: usize = 0;
+ if (prev_quoted) |pq| start = (lastWith(lines[0..end], pq) orelse return null) + 1;
+ const window = try a.dupe([]const u8, lines[start..end]);
+ c.lines.replaceRangeAssumeCapacity(0, end, &.{});
+ return window;
+ }
+ const lines = try splitLines(a, (try readAll(c, a, "/log")) orelse "");
+ const end = lastWith(lines, quoted) orelse return c.fail(a, "log", "marker record missing from /log", .{});
+ const pq = prev_quoted orelse return null;
+ const start = lastWith(lines[0..end], pq) orelse return null; // the ring moved past it
+ return lines[start + 1 .. end];
+}
+
+fn lastWith(lines: []const []const u8, needle: []const u8) ?usize {
+ var i = lines.len;
+ while (i > 0) {
+ i -= 1;
+ if (std.mem.indexOf(u8, lines[i], needle) != null) return i;
+ }
+ return null;
+}
+
+/// Records of `kind` (err|msg), counting `(xN)` repeats (docs/typ/
+/// reference.typ: a record said again is counted; a follower sees each
+/// count as a line of its own).
+fn occurrences(a: Allocator, lines: []const []const u8, kind: []const u8) []const []const u8 {
+ var out: std.ArrayList([]const u8) = .empty;
+ var last_text: ?[]const u8 = null;
+ var last_n: usize = 0;
+ for (lines) |line| {
+ if (!(std.mem.startsWith(u8, line, kind) and line.len > kind.len and line[kind.len] == ' ')) {
+ last_text = null;
+ continue;
+ }
+ var text = line;
+ var n: usize = 1;
+ if (std.mem.endsWith(u8, line, ")")) if (std.mem.lastIndexOf(u8, line, " (x")) |i| {
+ if (isDigits(line[i + 3 .. line.len - 1])) {
+ text = line[0..i];
+ n = std.fmt.parseInt(usize, line[i + 3 .. line.len - 1], 10) catch 1;
+ }
+ };
+ const new = if (last_text != null and std.mem.eql(u8, text, last_text.?)) n -| last_n else n;
+ for (0..new) |_| out.append(a, text) catch oom();
+ last_text = text;
+ last_n = n;
+ }
+ return out.items;
+}
+
+// ---------------------------------------------------------------------------
+// The rules, each with its citation. Each answers why it failed, or null.
+
+/// docs/typ/reference.typ 'One rule for what fails': a write fails whenever
+/// what it asked for fails ... and logs its reason exactly once, as `err
+/// <serial|-> <file>: <why>`, with no `msg` for it. What is not a failure:
+/// a look that finds nothing answers nothing and logs one `err`, the write
+/// succeeding.
+fn oneFailureRule(c: *Ctx, a: Allocator, res: *Res) ?[]const u8 {
+ _ = c;
+ const window = res.window orelse return null;
+ if (res.hung_up) return null;
+ // The rule is a write's: a refused open (pane/new's and an OTRUNC one
+ // too), create or remove logs no err, nor does a write to pane/new
+ // (`permission denied`: it is only read).
+ var failed: std.ArrayList(Rec) = .empty;
+ var succeeded = false;
+ var allowed: usize = 0;
+ var closes: usize = 0;
+ for (res.recs.items) |r| {
+ if (r.kind == .allow) allowed += r.n;
+ if (r.kind == .close_runs) closes += 1;
+ if (r.kind == .write and r.ok == true) succeeded = true;
+ if (Res.isWrite(r) and r.ok == false and (r.kind == .write or r.kind == .clunk_write) and
+ !std.mem.eql(u8, std.mem.trimEnd(u8, r.path, "/"), "/pane/new")) failed.append(a, r) catch oom();
+ }
+ const errs = occurrences(a, window, "err");
+ // docs/typ/reference.typ 'A write of command lines ... what is left when
+ // it closes runs at the close ... and its failure is in the log alone'.
+ const lo = failed.items.len;
+ const hi = lo + allowed + closes;
+ if (errs.len < lo or errs.len > hi) {
+ var why: []const u8 = "";
+ for (failed.items) |f| why = print(a, "{s} ({t} {s}: {s})", .{ why, f.kind, f.path, f.why });
+ return print(a, "{d} failed write(s) logged {d} err record(s) (allowed {d}..{d}); failed={s} window={s}", .{ lo, errs.len, lo, hi, why, joined(a, window) });
+ }
+ // A msg is a failed write's only when no Twrite of the op succeeded: a
+ // line cut across Twrites runs a piece a Twrite, and a piece that ran
+ // (a Save) says so while a later one is refused. A file watch's notice
+ // is the host's news, said whenever it arrives: no write's.
+ var msgs: std.ArrayList([]const u8) = .empty;
+ for (occurrences(a, window, "msg")) |m| {
+ if (std.mem.indexOf(u8, m, " deleted on disk (") != null or std.mem.indexOf(u8, m, " changed on disk (") != null or
+ std.mem.indexOf(u8, m, " reloaded ") != null) continue;
+ msgs.append(a, m) catch oom();
+ }
+ if (failed.items.len > 0 and msgs.items.len > 0 and !succeeded)
+ return print(a, "a failed write also logged msg: {s}; failed {s}", .{ joined(a, msgs.items), failed.items[0].path });
+ return null;
+}
+
+const line_files = [_][]const u8{ "look", "exec", "tagexec", "ctl" };
+
+/// docs/typ/reference.typ: 'A write of command lines -- to look, exec,
+/// tagexec, a ctl of the root, a pane or a column, or a column's exec --
+/// runs each line once it is whole ... what is left when it closes runs at
+/// the close'. Another file that fails only at its close breaks 'a write
+/// fails whenever what it asked for fails' (or the list is short).
+fn closeRunsOnlyLineFiles(c: *Ctx, a: Allocator, res: *Res) ?[]const u8 {
+ _ = c;
+ const window = res.window orelse return null;
+ for (occurrences(a, window, "err")) |line| {
+ // `err <serial|-> <file>: <why>`
+ const rest = line["err ".len..];
+ const sp = std.mem.indexOfScalar(u8, rest, ' ') orelse continue;
+ if (sp == 0) continue;
+ const colon = std.mem.indexOfScalar(u8, rest[sp + 1 ..], ':') orelse continue;
+ if (colon == 0) continue;
+ const name = base(rest[sp + 1 ..][0..colon]);
+ var skip = false;
+ for (line_files) |l| skip = skip or std.mem.eql(u8, name, l);
+ for (res.recs.items) |r| skip = skip or (Res.isWrite(r) and r.ok == false and std.mem.eql(u8, base(r.path), name));
+ if (skip) continue;
+ for (res.recs.items) |r| if (r.kind == .close_runs and std.mem.eql(u8, base(r.path), name))
+ return print(a, "a write to {s} with no newline was answered, then failed at the close: {s}", .{ name, line });
+ }
+ return null;
+}
+
+/// docs/typ/reference.typ /index: one line per pane; /pane/<n>/ each
+/// pane's directory.
+fn indexMatchesPaneDirs(c: *Ctx, a: Allocator, res: *Res) !?[]const u8 {
+ _ = res;
+ const first = (try readAll(c, a, "/index")) orelse return "/index unreadable";
+ const listed = (try names(c, a, "/pane")) orelse return "walking /pane failed";
+ const second = (try readAll(c, a, "/index")) orelse return "/index unreadable";
+ const serials = serialsOf(a, second) catch return print(a, "/index unparsable: {s}", .{second});
+ const la = c.life.allocator();
+ for (c.serials.items) |old| {
+ if (std.mem.indexOfScalar(u32, serials, old) == null and std.mem.indexOfScalar(u32, c.gone.items, old) == null) try c.gone.append(la, old);
+ }
+ std.mem.sort(u32, c.gone.items, {}, std.sort.asc(u32));
+ if (c.gone.items.len > 20) c.gone.replaceRangeAssumeCapacity(0, c.gone.items.len - 20, &.{});
+ c.serials = .fromOwnedSlice(try la.dupe(u32, serials));
+ if (!std.mem.eql(u8, first, second)) return null; // it changed between the reads; no claim
+ var dirs: std.ArrayList(u32) = .empty;
+ for (listed) |n| {
+ if (isDigits(n)) {
+ try dirs.append(a, std.fmt.parseInt(u32, n, 10) catch return print(a, "/pane lists {s}", .{n}));
+ } else if (!std.mem.eql(u8, n, "new")) return print(a, "/pane lists a non-serial entry {s}", .{n});
+ }
+ const sorted = try a.dupe(u32, serials);
+ std.mem.sort(u32, sorted, {}, std.sort.asc(u32));
+ std.mem.sort(u32, dirs.items, {}, std.sort.asc(u32));
+ if (!std.mem.eql(u32, sorted, dirs.items)) return print(a, "/index serials {any} != /pane dirs {any}", .{ sorted, dirs.items });
+ return null;
+}
+
+const listings = [_][]const u8{ "/index", "/layout", "/", "/pane", "/col", "/pane/{s}", "/status", "/commands", "/pane/{s}/tag", "/recent", "/README" };
+
+/// 9P read(5): reads at successive offsets see the one file; a listing
+/// read in chunks equals one read whole within the same open.
+fn chunkedEqualsWhole(c: *Ctx, a: Allocator, res: *Res) !?[]const u8 {
+ _ = res;
+ var path: []const u8 = listings[c.step % listings.len];
+ if (std.mem.indexOf(u8, path, "{s}")) |i| {
+ if (c.serials.items.len == 0) return null;
+ path = print(a, "{s}{d}{s}", .{ path[0..i], c.serials.items[c.step % c.serials.items.len], path[i + 3 ..] });
+ }
+ var w = (try c.s().walk(path)) orelse return null; // gone since
+ if (c.s().ask(&w, .open, .{}).errno != 0) return null;
+ defer _ = c.s().ask(&w, .clunk, .{});
+ const whole = try readFid(c, a, &w, read_max);
+ const size: u32 = @intCast(if (w.dir) 128 + (c.step * 37) % 900 else 1 + (c.step * 7) % 50);
+ const chunked = try readFid(c, a, &w, size);
+ if (w.dir) {
+ if (!std.mem.eql(u8, whole, chunked)) return print(a, "{s}: whole {s} != chunked({d}) {s}", .{ path, whole, size, chunked });
+ } else if (!std.mem.eql(u8, whole, chunked) and !std.mem.eql(u8, try readFid(c, a, &w, read_max), chunked)) {
+ // A live file (a tag says `running`, then `exit 0`) may change
+ // between the two reads: only chunks no whole read matches, read
+ // again, are a chunking fault.
+ return print(a, "{s}: whole {s} != chunked({d}) {s}", .{ path, whole[0..@min(200, whole.len)], size, chunked[0..@min(200, chunked.len)] });
+ }
+ return null;
+}
+
+fn joined(a: Allocator, lines: []const []const u8) []const u8 {
+ var out: []const u8 = "[";
+ for (lines, 0..) |l, i| out = cat(a, &.{ out, if (i > 0) " | " else "", l });
+ return cat(a, &.{ out, "]" });
+}
+
+/// One op and every rule after it.
+fn runStep(c: *Ctx, a: Allocator, raw: Op) Fail!void {
+ const op = resolve(c, a, raw);
+ const what = print(a, "{t} {s}", .{ op, opPath(op) });
+ var res: Res = .{};
+ if (c.verbose) std.debug.print(" op {d}: {s} {f}\n", .{ c.step, what, std.zig.fmtString(opData(op)) });
+ exec(c, a, &res, op) catch |err| switch (err) {
+ error.Fail => return error.Fail,
+ else => return c.fail(a, "harness", "{s}: {t}", .{ what, err }),
+ };
+ // Closing the session's last pane quits it (docs/typ/reference.typ).
+ if (c.s().ended()) return error.Ended;
+ checks(c, a, &res) catch |err| switch (err) {
+ error.Fail => return error.Fail,
+ else => return c.fail(a, "checker", "after {s}: {t}", .{ what, err }),
+ };
+}
+
+fn checks(c: *Ctx, a: Allocator, res: *Res) !void {
+ const window = try marker(c, a);
+ res.window = if (res.hung_up) null else window;
+ if (c.verbose) {
+ if (res.note.len > 0) std.debug.print(" -- {s}\n", .{res.note});
+ for (res.recs.items) |r| std.debug.print(" {t} {f} -> {s} {s}\n", .{ r.kind, std.zig.fmtString(r.path), if (r.ok) |ok| (if (ok) "ok" else "Rerror") else "held", r.why });
+ for (window orelse &.{}) |line| std.debug.print(" log: {s}\n", .{line});
+ }
+ const rules = .{ .{ "one_failure_rule", oneFailureRule }, .{ "close_runs_only_line_files", closeRunsOnlyLineFiles }, .{ "index_matches_pane_dirs", indexMatchesPaneDirs }, .{ "chunked_equals_whole", chunkedEqualsWhole } };
+ inline for (rules) |rule| {
+ const got = rule[1](c, a, res);
+ const problem = if (@typeInfo(@TypeOf(got)) == .error_union) try got else got;
+ if (problem) |why| return c.fail(a, rule[0], "{s}", .{why});
+ }
+ // The column list, for the next ops.
+ if (try names(c, a, "/col")) |cols| {
+ c.cols = .empty;
+ for (cols) |n| if (isDigits(n)) try c.cols.append(c.life.allocator(), std.fmt.parseInt(u32, n, 10) catch continue);
+ }
+}
+
+/// What makes two failures the same bug: the kind, and the words with
+/// numbers, quoted words and paths blurred.
+fn signature(a: Allocator, kind: []const u8, why: []const u8) []const u8 {
+ var out: std.ArrayList(u8) = .empty;
+ out.print(a, "{s}:", .{kind}) catch oom();
+ const singles = std.mem.indexOf(u8, why, "window=") == null;
+ var i: usize = 0;
+ while (i < why.len and out.items.len < kind.len + 1 + 300) {
+ const ch = why[i];
+ if (ch == '"' or (ch == '\'' and singles)) {
+ if (std.mem.indexOfScalarPos(u8, why, i + 1, ch)) |close| {
+ out.appendSlice(a, &.{ ch, ch }) catch oom();
+ i = close + 1;
+ continue;
+ }
+ }
+ if (std.mem.startsWith(u8, why[i..], "/home/") or std.mem.startsWith(u8, why[i..], "/tmp/pardes-mky-")) {
+ out.appendSlice(a, "PATH") catch oom();
+ while (i < why.len and !std.ascii.isWhitespace(why[i])) i += 1;
+ continue;
+ }
+ if (std.ascii.isDigit(ch)) {
+ out.append(a, 'N') catch oom();
+ while (i < why.len and std.ascii.isDigit(why[i])) i += 1;
+ continue;
+ }
+ out.append(a, ch) catch oom();
+ i += 1;
+ }
+ return out.items;
+}
+
+/// The child's run of one seed: its failures as `mky fail <step> <sig>`
+/// lines, each step announced as `mky step <n>` first, so the parent knows
+/// where a panic or a hang was.
+fn fuzz(gpa: Allocator, seed: u64, steps: usize, only: ?[]const usize, verbose: bool, root: []const u8) !u8 {
+ var c: Ctx = .{ .gpa = gpa, .seed = seed, .root = root, .verbose = verbose, .life = .init(gpa) };
+ var prng = Random.DefaultPrng.init(seed);
+ const r = prng.random();
+ var step_arena: std.heap.ArenaAllocator = .init(gpa);
+ var found: std.StringArrayHashMapUnmanaged(struct { n: usize, first: usize }) = .empty;
+ try c.fresh(0);
+ const t0 = nowMs();
+ var ended: usize = 0;
+ var ran: usize = 0;
+ for (0..steps) |step| {
+ _ = step_arena.reset(.retain_capacity);
+ const a = step_arena.allocator();
+ c.step = step;
+ const op = gen(r, a);
+ if (only) |list| if (std.mem.indexOfScalar(usize, list, step) == null) continue;
+ std.debug.print("mky step {d}\n", .{step});
+ ran += 1;
+ runStep(&c, a, op) catch |err| switch (err) {
+ error.Ended => {
+ ended += 1;
+ try c.fresh(step + 1);
+ continue;
+ },
+ error.Fail => {
+ const sig = try gpa.dupe(u8, signature(a, c.fail_kind, c.fail_why));
+ std.debug.print("[seed {d} step {d}] FAIL {s}: {s}\n", .{ seed, step, c.fail_kind, c.fail_why[0..@min(c.fail_why.len, 400)] });
+ std.debug.print("mky fail {d} {s}\n", .{ step, sig });
+ const got = try found.getOrPut(gpa, sig);
+ if (got.found_existing) got.value_ptr.n += 1 else got.value_ptr.* = .{ .n = 1, .first = step };
+ if (only != null) break;
+ try c.fresh(step + 1);
+ },
+ };
+ if (step > 0 and step % 500 == 0)
+ std.debug.print("[seed {d}] step {d}/{d}, {d} distinct failure(s)\n", .{ seed, step, steps, found.count() });
+ }
+ const dt = @max(1, nowMs() - t0);
+ std.debug.print("[seed {d}] {d} steps in {d} ms ({d}/s), {d} clean quit(s)\n", .{ seed, ran, dt, @divTrunc(@as(i64, @intCast(ran)) * 1000, dt), ended });
+ for (found.keys(), found.values()) |sig, v| std.debug.print(" {d}x {s} (first at step {d})\n", .{ v.n, sig[0..@min(sig.len, 200)], v.first });
+ if (c.sess) |*s| s.deinit();
+ return if (found.count() > 0) 1 else 0;
+}
+
+fn nowMs() i64 {
+ var ts: libc.timespec = undefined;
+ _ = libc.clock_gettime(.MONOTONIC, &ts);
+ return @as(i64, ts.sec) * 1000 + @divTrunc(@as(i64, ts.nsec), std.time.ns_per_ms);
+}
+
+// ---------------------------------------------------------------------------
+// The parent: a child a run, in its own root.
+
+const Found = struct { step: usize, sig: []const u8 };
+
+/// One child run of `seed`; its failures, a panic or a hang among them.
+/// Its output is passed on unless `quiet` (a shrink's trials).
+fn spawn(gpa: Allocator, seed: u64, steps: usize, only: ?[]const usize, verbose: bool, quiet: bool) ![]Found {
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var template = "/tmp/pardes-mky-XXXXXX".*;
+ const root = std.mem.span(mkdtemp(&template) orelse return error.NoTempDir);
+ defer {
+ const rm = std.fmt.allocPrintSentinel(a, "rm -rf -- '{s}'", .{root}, 0) catch oom();
+ _ = system(rm);
+ }
+ var argv: std.ArrayList(?[*:0]const u8) = .empty;
+ for ([_][]const u8{ "pardes-monkey-session", "--seed", print(a, "{d}", .{seed}), "--steps", print(a, "{d}", .{steps}) }) |arg| try argv.append(a, try a.dupeSentinel(u8, arg, 0));
+ if (verbose) try argv.append(a, "-v");
+ if (only) |list| {
+ var text: std.ArrayList(u8) = .empty;
+ for (list, 0..) |n, i| try text.print(a, "{s}{d}", .{ if (i > 0) "," else "", n });
+ try argv.append(a, "--only");
+ try argv.append(a, try a.dupeSentinel(u8, text.items, 0));
+ }
+ try argv.append(a, null);
+ // The session's HOME, XDG directories and dump are the root's; what a
+ // session of the caller's left in the environment is not passed on.
+ var envp: std.ArrayList(?[*:0]const u8) = .empty;
+ for ([_][2][]const u8{
+ .{ "MKY_ROOT", root },
+ .{ "HOME", root },
+ .{ "XDG_RUNTIME_DIR", root },
+ .{ "XDG_CONFIG_HOME", print(a, "{s}/config", .{root}) },
+ .{ "XDG_STATE_HOME", print(a, "{s}/state", .{root}) },
+ .{ "XDG_DATA_HOME", print(a, "{s}/data", .{root}) },
+ .{ "XDG_CACHE_HOME", print(a, "{s}/cache", .{root}) },
+ .{ "PARDES_DUMP", print(a, "{s}/m9p.dump.zon", .{root}) },
+ .{ "SHELL", "/bin/sh" },
+ .{ "PARDES_NOTIME", "1" },
+ }) |kv| try envp.append(a, try std.fmt.allocPrintSentinel(a, "{s}={s}", .{ kv[0], kv[1] }, 0));
+ var e: usize = 0;
+ while (environ[e]) |entry| : (e += 1) {
+ const kv = std.mem.span(entry);
+ if (std.mem.startsWith(u8, kv, "PARDES_") or std.mem.startsWith(u8, kv, "NINE_MOUNT=") or std.mem.startsWith(u8, kv, "NAMESPACE=")) continue;
+ try envp.append(a, entry);
+ }
+ try envp.append(a, null);
+ var fds: [2]c_int = undefined;
+ if (libc.pipe(&fds) != 0) return error.NoPipe;
+ const root_z = try a.dupeSentinel(u8, root, 0);
+ const pid = libc.fork();
+ if (pid < 0) return error.ForkFailed;
+ if (pid == 0) {
+ _ = libc.dup2(fds[1], 1);
+ _ = libc.dup2(fds[1], 2);
+ _ = libc.close(fds[0]);
+ if (libc.chdir(root_z) != 0) _exit(126);
+ _ = execve("/proc/self/exe", @ptrCast(argv.items.ptr), @ptrCast(envp.items.ptr));
+ _exit(127);
+ }
+ _ = libc.close(fds[1]);
+ defer _ = libc.close(fds[0]);
+ var found: std.ArrayList(Found) = .empty;
+ var pending: std.ArrayList(u8) = .empty;
+ var last_step: usize = 0;
+ var panic_line: ?[]const u8 = null;
+ // The core's own output during the current step, shown if it crashes.
+ var said: std.ArrayList(u8) = .empty;
+ var hung = false;
+ var buf: [64 * 1024]u8 = undefined;
+ while (true) {
+ var pfd = [_]libc.pollfd{.{ .fd = fds[0], .events = libc.POLL.IN, .revents = 0 }};
+ if (libc.poll(&pfd, 1, hang_ms) == 0) {
+ hung = true;
+ _ = libc.kill(pid, libc.SIG.KILL);
+ continue;
+ }
+ const n = libc.read(fds[0], &buf, buf.len);
+ if (n <= 0) break;
+ try pending.appendSlice(a, buf[0..@intCast(n)]);
+ while (std.mem.indexOfScalar(u8, pending.items, '\n')) |nl| {
+ const line = try a.dupe(u8, pending.items[0..nl]);
+ pending.replaceRangeAssumeCapacity(0, nl + 1, &.{});
+ if (std.mem.startsWith(u8, line, "mky step ")) {
+ last_step = std.fmt.parseInt(usize, line["mky step ".len..], 10) catch last_step;
+ said.clearRetainingCapacity();
+ } else if (std.mem.startsWith(u8, line, "mky fail ")) {
+ var it = std.mem.splitScalar(u8, line["mky fail ".len..], ' ');
+ const at = std.fmt.parseInt(usize, it.next().?, 10) catch 0;
+ try found.append(gpa, .{ .step = at, .sig = try gpa.dupe(u8, it.rest()) });
+ } else {
+ if (panic_line == null and std.mem.indexOf(u8, line, "panic: ") != null) panic_line = line;
+ if (!quiet and (std.mem.startsWith(u8, line, "[seed ") or std.mem.startsWith(u8, line, " "))) {
+ std.debug.print("{s}\n", .{line});
+ } else try said.print(a, "{s}\n", .{line});
+ }
+ }
+ }
+ var status: c_int = 0;
+ _ = libc.waitpid(pid, &status, 0);
+ const sig = status & 0x7f;
+ const code = (status >> 8) & 0xff;
+ const lost: ?[]const u8 = if (hung)
+ print(a, "hang:no step finished within {d} ms", .{hang_ms})
+ else if (sig != 0)
+ signature(a, "crash", print(a, "signal {d} {s}", .{ sig, panic_line orelse "" }))
+ else if (code > 1)
+ print(a, "harness:exit {d}", .{code})
+ else
+ null;
+ if (lost) |why| {
+ if (!quiet) std.debug.print("{s}[seed {d} step {d}] FAIL {s}\n", .{ if (sig != 0 or code > 1) said.items else "", seed, last_step, why });
+ try found.append(gpa, .{ .step = last_step, .sig = try gpa.dupe(u8, why) });
+ }
+ return found.toOwnedSlice(gpa);
+}
+
+/// Zeller's delta debugging: a 1-minimal failing subsequence.
+fn ddmin(a: Allocator, start: []const usize, ctx: anytype, fails: fn (@TypeOf(ctx), []const usize) bool) ![]const usize {
+ var items = start;
+ var n: usize = 2;
+ while (items.len >= 2) {
+ const size = @max(1, items.len / n);
+ var reduced = false;
+ var lo: usize = 0;
+ while (lo < items.len) : (lo += size) {
+ const hi = @min(items.len, lo + size);
+ const complement = try std.mem.concat(a, usize, &.{ items[0..lo], items[hi..] });
+ if (complement.len > 0 and fails(ctx, complement)) {
+ items = complement;
+ n = @max(n - 1, 2);
+ reduced = true;
+ break;
+ }
+ }
+ if (!reduced) {
+ if (n >= items.len) break;
+ n = @min(items.len, n * 2);
+ }
+ }
+ return items;
+}
+
+const Trial = struct {
+ gpa: Allocator,
+ seed: u64,
+ steps: usize,
+ sig: []const u8,
+ budget: *usize,
+
+ fn fails(t: *const Trial, only: []const usize) bool {
+ if (t.budget.* == 0) return false;
+ t.budget.* -= 1;
+ const found = spawn(t.gpa, t.seed, t.steps, only, false, true) catch return false;
+ return found.len > 0 and std.mem.eql(u8, found[0].sig, t.sig);
+ }
+};
+
+fn shrink(gpa: Allocator, seed: u64, f: Found, budget: usize) !void {
+ var left = budget;
+ const trial: Trial = .{ .gpa = gpa, .seed = seed, .steps = f.step + 1, .sig = f.sig, .budget = &left };
+ const body = try gpa.alloc(usize, f.step + 1);
+ for (body, 0..) |*b, i| b.* = i;
+ if (!trial.fails(body)) {
+ std.debug.print(" not reproduced on replay: --seed {d} --steps {d}\n", .{ seed, f.step + 1 });
+ return;
+ }
+ const small = try ddmin(gpa, body, &trial, Trial.fails);
+ var text: std.ArrayList(u8) = .empty;
+ for (small, 0..) |n, i| try text.print(gpa, "{s}{d}", .{ if (i > 0) "," else "", n });
+ std.debug.print(" shrunk {d} -> {d} ops: zig build monkey-session -Dplatform=tty -- --seed {d} --steps {d} --only {s} -v\n", .{ body.len, small.len, seed, f.step + 1, text.items });
+}
+
+fn usage() noreturn {
+ std.debug.print("usage: pardes-monkey-session [--seed N]... [--steps M] [--only i,j,k] [--no-shrink] [--shrink-budget N] [--smoke] [-v]\n", .{});
+ std.process.exit(2);
+}
+
+pub fn main(init: std.process.Init) !void {
+ const gpa = std.heap.page_allocator;
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
+ var seeds: std.ArrayList(u64) = .empty;
+ var steps: ?usize = null;
+ var only: ?[]const usize = null;
+ var no_shrink = false;
+ var budget: usize = 300;
+ var smoke = false;
+ var verbose = false;
+ var i: usize = 1;
+ while (i < args.len) : (i += 1) {
+ const arg = args[i];
+ const value = if (i + 1 < args.len) args[i + 1] else "";
+ if (std.mem.eql(u8, arg, "--seed")) {
+ try seeds.append(gpa, std.fmt.parseInt(u64, value, 10) catch usage());
+ i += 1;
+ } else if (std.mem.eql(u8, arg, "--steps")) {
+ steps = std.fmt.parseInt(usize, value, 10) catch usage();
+ i += 1;
+ } else if (std.mem.eql(u8, arg, "--only")) {
+ var list: std.ArrayList(usize) = .empty;
+ var it = std.mem.tokenizeScalar(u8, value, ',');
+ while (it.next()) |n| try list.append(gpa, std.fmt.parseInt(usize, n, 10) catch usage());
+ only = list.items;
+ i += 1;
+ } else if (std.mem.eql(u8, arg, "--shrink-budget")) {
+ budget = std.fmt.parseInt(usize, value, 10) catch usage();
+ i += 1;
+ } else if (std.mem.eql(u8, arg, "--no-shrink")) {
+ no_shrink = true;
+ } else if (std.mem.eql(u8, arg, "--smoke")) {
+ smoke = true;
+ } else if (std.mem.eql(u8, arg, "-v") or std.mem.eql(u8, arg, "--verbose")) {
+ verbose = true;
+ } else usage();
+ }
+ if (libc.getenv("MKY_ROOT")) |root| {
+ var check: std.heap.ArenaAllocator = .init(gpa);
+ selfCheck(check.allocator());
+ check.deinit();
+ // An error out of the run (a fresh session's /index unparsable, its
+ // log refusing `follow new`) is a harness failure, not exit 1 with no
+ // failure line, which the parent would take for a clean run.
+ std.process.exit(fuzz(gpa, if (seeds.items.len > 0) seeds.items[0] else 1, steps orelse 1000, only, verbose, std.mem.span(root)) catch |err| {
+ std.debug.print("mky error {t}\n", .{err});
+ std.process.exit(3);
+ });
+ }
+ if (smoke) {
+ // A short fixed-seed run that fails on any failure.
+ seeds.clearRetainingCapacity();
+ try seeds.append(gpa, 20260929);
+ steps = steps orelse 800;
+ no_shrink = true;
+ }
+ if (seeds.items.len == 0) try seeds.append(gpa, 1);
+ var total: usize = 0;
+ for (seeds.items) |seed| {
+ const found = try spawn(gpa, seed, steps orelse 1000, only, verbose, false);
+ total += found.len;
+ if (no_shrink or only != null) continue;
+ var seen: std.StringHashMapUnmanaged(void) = .empty;
+ for (found) |f| if (!(try seen.getOrPut(gpa, f.sig)).found_existing) try shrink(gpa, seed, f, budget);
+ }
+ if (total > 0) std.process.exit(1);
+}
diff --git a/test/process.zig b/test/process.zig
index 3d74c72b..fd423985 100644
--- a/test/process.zig
+++ b/test/process.zig
@@ -183,6 +183,33 @@ pub const Conn = struct {
return names.toOwnedSlice(gpa);
}
+ // fsa:
+
+ /// `data` written to `path` opened OTRUNC (a shell's `>`), the open
+ /// clunked after; a refusal by a write or the clunk is `error.Refused`.
+ pub fn replace(c: *Conn, path: []const u8, data: []const u8) !void {
+ try c.writeCut(path, 1 | 16, data, msize - 24);
+ }
+
+ /// `data` written to `path` (opened `mode`) in writes of at most `cut`
+ /// bytes, as a client with a smaller msize cuts them.
+ pub fn writeCut(c: *Conn, path: []const u8, mode: u8, data: []const u8, cut: usize) !void {
+ const fid = try c.hold(path, mode);
+ var at: usize = 0;
+ while (at < data.len) {
+ const n = c.writeFid(fid, at, data[at..@min(data.len, at + cut)]) catch |err| {
+ c.clunk(fid);
+ return err;
+ };
+ if (n == 0) {
+ c.clunk(fid);
+ return error.ShortWrite;
+ }
+ at += n;
+ }
+ _ = try c.call(.{ .clunk = .{ .fid = fid } });
+ }
+
// fsb:
/// `dial`, versioned at `size` (a client of small frames, at least
@@ -226,26 +253,77 @@ pub const Conn = struct {
return c.cl.msize;
}
- /// `data` written to `path` truncated first (`>`), in as many writes
- /// as it takes.
- pub fn replace(c: *Conn, path: []const u8, data: []const u8) !void {
- const fid = try c.hold(path, 1 | 16);
- var off: usize = 0;
- while (off < data.len) {
- const n = c.writeFid(fid, off, data[off..@min(data.len, off + c.cl.maxWrite())]) catch |err| {
- c.clunk(fid);
- return err;
- };
- off += n;
+
+ /// A file or directory (`perm` with c9.dmdir) made in `dir`.
+ pub fn create(c: *Conn, dir: []const u8, name: []const u8, perm: u32) !void {
+ const fid = try c.walk(dir);
+ defer c.clunk(fid);
+ _ = try c.call(.{ .create = .{ .fid = fid, .name = name, .perm = perm, .mode = 0 } });
+ }
+
+ /// A new pane, made by an open of /pane/new; its serial.
+ pub fn newPane(c: *Conn) !u32 {
+ const fid = try c.hold("/pane/new", 0);
+ defer c.clunk(fid);
+ return std.fmt.parseInt(u32, std.mem.trim(u8, try c.readFid(fid, 0, 64), "\n"), 10);
+ }
+
+ /// The newest pane's serial: /index is ordered by serial.
+ pub fn newest(c: *Conn, gpa: std.mem.Allocator) !u32 {
+ const index = try c.read(gpa, "/index");
+ defer gpa.free(index);
+ var rows = std.mem.splitBackwardsScalar(u8, std.mem.trimEnd(u8, index, "\n"), '\n');
+ const last = rows.first();
+ return std.fmt.parseInt(u32, last[0 .. std.mem.indexOfScalar(u8, last, ' ') orelse last.len], 10);
+ }
+
+
+ /// The words a server refuses a new connection's Tversion with (its
+ /// slots all taken), copied into `buf`; error.NotRefused when it took it.
+ pub fn dialRefusal(gpa: std.mem.Allocator, path: []const u8, buf: []u8) ![]const u8 {
+ const c = try gpa.create(Conn);
+ defer gpa.destroy(c);
+ c.* = .{ .fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0), .cl = undefined };
+ c.cl = c9.Client.init(.{ .in = &c.in, .out = &c.out });
+ if (c.fd < 0) return error.NoSocket;
+ defer _ = libc.close(c.fd);
+ var addr: libc.sockaddr.un = .{ .path = @splat(0) };
+ if (path.len >= addr.path.len) return error.NameTooLong;
+ @memcpy(addr.path[0..path.len], path);
+ if (libc.connect(c.fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) != 0) return error.ConnectFailed;
+ _ = c.call(.{ .version = .{ .msize = msize } }) catch |err| {
+ if (err != error.Refused) return err;
+ const n = @min(buf.len, c.said.len);
+ @memcpy(buf[0..n], c.said[0..n]);
+ return buf[0..n];
+ };
+ return error.NotRefused;
+ }
+
+ /// A request sent without waiting for its answer (several in flight on
+ /// one connection); its tag.
+ pub fn send(c: *Conn, req: c9.Client.Request) !u16 {
+ const tag = c.cl.submit(req) catch return error.Protocol;
+ while (c.cl.output().len > 0) {
+ const out = c.cl.output();
+ const n = libc.write(c.fd, out.ptr, out.len);
+ if (n <= 0) return error.Hangup;
+ c.cl.wrote(@intCast(n));
}
- _ = try c.call(.{ .clunk = .{ .fid = fid } });
+ return tag;
}
- /// A new pane, made by opening /pane/new; its serial.
- pub fn newPane(c: *Conn, gpa: std.mem.Allocator) !u32 {
- const got = try c.read(gpa, "/pane/new");
- defer gpa.free(got);
- return std.fmt.parseInt(u32, std.mem.trim(u8, got, "\n"), 10);
+ /// The next answer to come, to whichever request `send` sent; a read's
+ /// bytes valid until the next call.
+ pub fn answer(c: *Conn) !c9.Client.Done {
+ while (true) {
+ if (c.cl.take()) |done| return done;
+ var buf: [msize]u8 = undefined;
+ const n = libc.read(c.fd, &buf, buf.len);
+ if (n <= 0) return error.Hangup;
+ var at: usize = 0;
+ while (at < n) at += c.cl.push(buf[at..@intCast(n)]);
+ }
}
/// The serials /index lists, in its order.
@@ -261,15 +339,15 @@ pub const Conn = struct {
/// A Look at `path` from pane `source`, or from a scratch pane removed
/// after; the one pane it answers (test/fs.py's look).
pub fn look(c: *Conn, gpa: std.mem.Allocator, path: []const u8, source: ?u32) !u32 {
- const from = source orelse try c.newPane(gpa);
+ const from = source orelse try c.newPane();
var buf: [64]u8 = undefined;
const line = try std.fmt.allocPrint(gpa, "{s}\n", .{path});
defer gpa.free(line);
try c.write(try std.fmt.bufPrint(&buf, "/pane/{d}/look", .{from}), line);
const got = try c.read(gpa, "/look");
defer gpa.free(got);
- const answer = std.mem.trim(u8, got, "\n");
- const opened = std.fmt.parseInt(u32, answer, 10) catch {
+ const said_back = std.mem.trim(u8, got, "\n");
+ const opened = std.fmt.parseInt(u32, said_back, 10) catch {
std.debug.print("look {s}: answered {s}\n", .{ path, got });
return error.NotOneLook;
};
@@ -649,6 +727,14 @@ fn sleepMs(ms: u32) void {
_ = libc.nanosleep(&ts, null);
}
+// fsa:
+
+/// A wait of `ms` milliseconds, for what a test gives a session time to do.
+pub fn sleep(ms: u32) void {
+ var ts: libc.timespec = .{ .sec = @intCast(ms / 1000), .nsec = @as(isize, ms % 1000) * std.time.ns_per_ms };
+ _ = libc.nanosleep(&ts, null);
+}
+
// fsb:
extern "c" fn readdir(dir: *libc.DIR) ?*libc.dirent;
diff --git a/test/process/builtins.zig b/test/process/builtins.zig
index 39a043c0..410a3f54 100644
--- a/test/process/builtins.zig
+++ b/test/process/builtins.zig
@@ -24,7 +24,7 @@ fn lspinfo(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
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(a);
+ const control = try c.newPane();
try c.exec(control, "Lspinfo\n");
const deadline = process.nowMs() + 3_000;
const lsp = while (true) {
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
index a6797901..fffc2867 100644
--- a/test/process/flags.zig
+++ b/test/process/flags.zig
@@ -201,7 +201,7 @@ fn forwarding(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
c.clunk(limit);
_ = try c.read(a, "/pane/1/ctl");
- const control = try c.newPane(a);
+ const control = try c.newPane();
try c.exec(control, "Dump\n");
const deadline = process.nowMs() + 3_000;
while (!d.exists("forwarding.dump.zon")) {
diff --git a/test/process/mounts.zig b/test/process/mounts.zig
index 7d44f4fb..10dc7b5a 100644
--- a/test/process/mounts.zig
+++ b/test/process/mounts.zig
@@ -19,7 +19,7 @@ fn peer(gpa: std.mem.Allocator, exe: [:0]const u8, a: std.mem.Allocator) !struct
var d = try Detached.start(gpa, exe, "first", .{});
errdefer d.deinit();
const c = try Conn.dial(gpa, d.socket());
- const serial = try c.newPane(a);
+ const serial = try c.newPane();
try c.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{serial}), "replaced\nevent\n");
return .{ d, c, serial };
}
@@ -80,7 +80,7 @@ fn runtime(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
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(a);
+ 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"));
@@ -164,7 +164,7 @@ fn quic(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
defer client.deinit();
const remote = try Conn.dial(gpa, client.socket());
defer remote.deinit(gpa);
- const control = try remote.newPane(a);
+ 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});
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/root_ctl.zig b/test/process/root_ctl.zig
index 87c9e777..008cc882 100644
--- a/test/process/root_ctl.zig
+++ b/test/process/root_ctl.zig
@@ -1,15 +1,111 @@
-//! docs/typ/reference.typ, The root ctl <root-ctl>: Dump and Restore in a
-//! real session, detached and in a pty.
+//! 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;
@@ -34,7 +130,7 @@ fn restore(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
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(a);
+ 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}));
diff --git a/test/process/terminals.zig b/test/process/terminals.zig
new file mode 100644
index 00000000..e810ceb1
--- /dev/null
+++ b/test/process/terminals.zig
@@ -0,0 +1,352 @@
+//! 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 {
+ try drive(gpa, exe, "printf \"AGENT_READY> \"; read answer", null, 0);
+}
+
+fn history(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ try drive(gpa, exe, "printf \"AGENT_READY> \"; sleep .3; seq 1 3000; printf \"AGENT_READY> \"; read answer", null, 1000);
+}
+
+fn rawKey(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
+ try drive(gpa, exe, "stty raw -echo; printf \"AGENT_READY> \"; dd bs=1 count=1 of=/dev/null 2>/dev/null; stty sane; seq 1 3000; printf \"AGENT_READY> \"; read answer", 0x0f, 1000);
+}
+
+/// The script run in a new terminal's shell, as `/bin/sh command.sh`: its
+/// prompt waited for, `key` sent and the prompt waited for again, the body
+/// holding at least `rows` lines (and, with none asked, fewer than a
+/// thousand: nothing printed is nothing kept), then a probe typed and
+/// taken back with ^U.
+fn drive(gpa: std.mem.Allocator, exe: [:0]const u8, script: []const u8, key: ?u8, rows: usize) !void {
+ var d = try process.Detached.start(gpa, exe, "agent", .{});
+ defer d.deinit();
+ const f = &d.files;
+ try d.put("command.sh", f.at("exec /bin/sh -c '{s}'\n", .{script}));
+ try f.write("/ctl", "Shell /bin/sh\n");
+ try f.write("/exec", "Tty\n");
+ const term = try f.focused();
+ const body = f.at("/pane/{d}/body", .{term});
+ const data = f.at("/pane/{d}/pty/data", .{term});
+ try until(f, body, struct {
+ fn ok(text: []const u8) bool {
+ return std.mem.trim(u8, text, " \n").len > 0;
+ }
+ }.ok);
+ try f.write(data, f.at("/bin/sh {s}/command.sh\r", .{d.root()}));
+ _ = try settled(f);
+ if (key) |k| {
+ try f.write(data, &.{k});
+ _ = try settled(f);
+ }
+ const deadline = eh.nowMs() + 10_000;
+ while (lineCount(try f.read(body)) < rows) {
+ if (eh.nowMs() > deadline) return error.ShortHistory;
+ sleepMs(50);
+ }
+ 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);
+ try f.write(data, probe);
+ const shown_by = eh.nowMs() + 10_000;
+ while (std.mem.indexOf(u8, try unwrapped(f), probe) == null) {
+ if (eh.nowMs() > shown_by) return error.ProbeNotShown;
+ sleepMs(10);
+ }
+ try f.write(data, "\x15");
+ const back_by = eh.nowMs() + 10_000;
+ while (!std.mem.eql(u8, try f.screen(), before)) {
+ if (eh.nowMs() > back_by) return error.ScreenNotRestored;
+ sleepMs(10);
+ }
+}
+
+/// The screen once it shows the prompt and holds still between two reads.
+fn settled(f: *process.Files) ![]const u8 {
+ const deadline = eh.nowMs() + 10_000;
+ var previous: []const u8 = "";
+ while (true) {
+ const text = try f.screen();
+ if (std.mem.indexOf(u8, text, ready) != null and std.mem.eql(u8, text, previous)) return text;
+ if (eh.nowMs() > deadline) {
+ std.debug.print("no settled prompt on the screen:\n{s}\n", .{text});
+ return error.NotReady;
+ }
+ previous = text;
+ sleepMs(50);
+ }
+}
+
+fn until(f: *process.Files, path: []const u8, ok: fn ([]const u8) bool) !void {
+ const deadline = eh.nowMs() + 10_000;
+ while (!ok(try f.read(path))) {
+ if (eh.nowMs() > deadline) return error.Timeout;
+ sleepMs(10);
+ }
+}
+
+/// The screen's text with its rows run together, as a long line wraps.
+fn unwrapped(f: *process.Files) ![]const u8 {
+ const text = try f.screen();
+ const out = try f.arena.allocator().alloc(u8, text.len);
+ var n: usize = 0;
+ for (text) |c| if (c != '\n') {
+ out[n] = c;
+ n += 1;
+ };
+ return out[0..n];
+}
+
+fn lineCount(text: []const u8) usize {
+ return std.mem.count(u8, text, "\n") + @intFromBool(text.len > 0 and text[text.len - 1] != '\n');
+}
+
+fn sleepMs(ms: u32) void {
+ var ts: std.c.timespec = .{ .sec = 0, .nsec = @as(isize, ms) * std.time.ns_per_ms };
+ _ = std.c.nanosleep(&ts, null);
+}
diff --git a/test/process/tree.zig b/test/process/tree.zig
index d0a8173d..358e7040 100644
--- a/test/process/tree.zig
+++ b/test/process/tree.zig
@@ -1,12 +1,19 @@
-//! docs/typ/reference.typ, The tree <tree>, through a real session's
-//! socket: what a client on the wire sees, and /os on the disk.
+//! 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 },
@@ -14,6 +21,473 @@ pub const cases = .{
.{ "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();
diff --git a/test/process_test.zig b/test/process_test.zig
index 828edff9..2f1b5791 100644
--- a/test/process_test.zig
+++ b/test/process_test.zig
@@ -7,12 +7,14 @@ 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"),
};
@@ -31,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/session.zig b/test/session.zig
index f572a4b1..41f2c376 100644
--- a/test/session.zig
+++ b/test/session.zig
@@ -343,6 +343,77 @@ pub const Session = struct {
const style = json.object.get("styles").?.array.items[@intCast(cell.array.items[1].integer)];
return std.json.Stringify.valueAlloc(a, style.object.get("bg").?, .{});
}
+
+ // mky: the 9P monkey's raw requests (test/monkey9p.zig). Quiet, and
+ // answering what came back rather than failing; the server engine's
+ // own checks (a write on a read-only open, OTRUNC's truncation first)
+ // are the monkey's to make.
+
+ /// A walked fid: its node, its open (`opened`, `fid.handle`), and what
+ /// the walk's last step said of it.
+ pub const Walked = struct { fid: Fid, dir: bool, mode: u16, opened: bool = false };
+ /// `held`: parked (`.again`), as a read with nothing yet is. `bytes`, a
+ /// read's (a readdir's names, a line each), last until the next request.
+ pub const Answer = struct { errno: u16 = 0, held: bool = false, bytes: []const u8 = "" };
+ pub const Ask = enum { open, create, read, readdir, write, clunk, remove, stat, truncate };
+
+ /// `path` walked from the root, not opened; null, its words in `said`,
+ /// when a step is refused.
+ pub fn walk(s: *Session, path: []const u8) !?Walked {
+ var r = s.serve(.{ .tag = 0, .op = .getattr, .node = pardes.ctlfs.root });
+ var node: u64 = pardes.ctlfs.root;
+ var parts = std.mem.tokenizeScalar(u8, path, '/');
+ while (parts.next()) |name| {
+ r = s.serve(.{ .tag = 0, .op = .lookup, .node = node, .data = name });
+ if (r.status != .ok) {
+ s.said = try s.arena.allocator().dupe(u8, r.ename);
+ return null;
+ }
+ node = r.attr.node;
+ }
+ return .{ .fid = .{ .node = node, .handle = 0 }, .dir = r.attr.dir, .mode = r.attr.mode };
+ }
+
+ /// One request on `w` as the engine passes it on (a clunk of an open,
+ /// a remove as a release that removes); `off` is a readdir's entry index.
+ pub fn ask(s: *Session, w: *Walked, what: Ask, a: struct { off: u64 = 0, size: u32 = 0, data: []const u8 = "", omode: u8 = 0, perm: u32 = 0 }) Answer {
+ const f = w.fid;
+ const r = s.serve(switch (what) {
+ .open => .{ .tag = 0, .op = .open, .node = f.node, .omode = a.omode },
+ .create => .{ .tag = 0, .op = .open, .node = f.node, .create = true, .data = a.data, .perm = a.perm, .omode = a.omode },
+ .read => .{ .tag = 0, .op = .read, .node = f.node, .handle = f.handle, .off = a.off, .size = a.size },
+ .readdir => .{ .tag = 0, .op = .readdir, .node = f.node, .handle = f.handle, .off = a.off, .size = a.size },
+ .write => .{ .tag = 0, .op = .write, .node = f.node, .handle = f.handle, .off = a.off, .data = a.data },
+ .clunk, .remove => .{ .tag = 0, .op = .release, .node = f.node, .handle = f.handle, .opened = w.opened, .remove = what == .remove },
+ .stat => .{ .tag = 0, .op = .getattr, .node = f.node, .handle = f.handle },
+ .truncate => .{ .tag = 0, .op = .setattr, .node = f.node, .truncate = true },
+ });
+ if (r.status == .again) return .{ .held = true };
+ if (r.status != .ok) {
+ s.said = s.arena.allocator().dupe(u8, r.ename) catch "";
+ return .{ .errno = if (r.errno == 0) 5 else r.errno };
+ }
+ if (what == .open or what == .create) {
+ w.fid.handle = r.handle;
+ w.opened = true;
+ }
+ if (what == .clunk or what == .remove) w.opened = false;
+ if (what != .readdir) return .{ .bytes = pardesOf(s).fsPayload(r) };
+ // A dirent: node (8), dir (1), name length (1), name.
+ const raw = pardesOf(s).fsPayload(r);
+ var names: std.ArrayList(u8) = .empty;
+ var i: usize = 0;
+ while (i + 10 <= raw.len) : (i += 10 + raw[i + 9]) {
+ names.appendSlice(s.arena.allocator(), raw[i + 10 ..][0..raw[i + 9]]) catch break;
+ names.append(s.arena.allocator(), '\n') catch break;
+ }
+ return .{ .bytes = names.items };
+ }
+
+ /// Whether the session has quit, as closing its last pane quits it.
+ pub fn ended(s: *Session) bool {
+ return pardesOf(s).quit;
+ }
};
fn refusal(path: []const u8, r: Reply) error{Refused} {
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())