From ef0d017705146a74bf7cc2706b9796f227ea6977 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 16:33:08 -0300 Subject: test: test/fs.py's discovery, run_file, exit_answers, unattended_messages_expire, directory_panes, putall_answers_after_its_saves, new_terminals_named_once, recent_survives_restart, store_shared_between_windows, odd_paths, delcol_last_keeps_window, cut_writes and edit_commands in Zig, the Python deleted (and its now unused newest, walk_tree, del_twice and the --discovery switch): at T2 in new test/process/{tree,terminals,root_ctl,edit}.zig and other_files.zig (the tree's listing, stats, log follow, ctl lock, focus, commands, sel, Save making directories, the 16 slots; a write cut at an 8 KiB msize; pty/run's answers, busy and shell gone; terminals named once; Exit; MessageLinger unattended; Putall; odd paths and ThemeFile; Edit's < | > off the loop, a held write answered after a later read and flushed, X and w; /recent across a restart and a store two sessions share), at T1 a directory pane in test/behaviour/look_and_exec.zig and 9P's new after Delcol of the last column in columns_and_tags.zig; discovery brought up to the tree as documented (sessions, kbdin, mousein at the root, /index stats 0, a sel write stays selected, the unknown-message words, a look's line in dot, a Save under / refused as permission denied), as the Python had gone stale; process.Conn gains replace, writeCut, create, newPane, newest, look, dialRefusal, send and answer, and process.sleep; a failing process test prints its error return trace; each checked to fail against a break (a detached session 161 wide, 17 connection slots, a pty taking one row, Edit's failure said as exit code, ThemeFile's .zon words changed, Exit not quitting, unattended messages never expiring, a directory pane's sub/ without its slash, Putall's modified-on-disk words changed, /recent saying shut for closed, a missing interpreter said as missing) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- test/process_test.zig | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'test/process_test.zig') diff --git a/test/process_test.zig b/test/process_test.zig index 9924b9dd..9ae6bb58 100644 --- a/test/process_test.zig +++ b/test/process_test.zig @@ -7,6 +7,10 @@ const std = @import("std"); const files = .{ @import("process/other_files.zig"), + @import("process/tree.zig"), + @import("process/terminals.zig"), + @import("process/root_ctl.zig"), + @import("process/edit.zig"), }; pub fn main(init: std.process.Init) !void { @@ -24,6 +28,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 }); } } -- cgit v1.3 From 6bcb18e021a09d51539e628106a594748637ead2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 16:46:18 -0300 Subject: test: fs.py's inline checks, quic_test, workflow_canary, python_repl_runs_one_line and signal_cleans_up moved to Zig, deleted from fs.py (its named cases stay): 21 process cases in test/process/ (flags: bad options refused in words, --9p naming the socket, forwarding a launch inside a session and the launches that start their own (mount, name, tcp, load, shells, toggle, quic), --wait and its refusals, pardes - and its +Pager (escapes, inks and Filter, progress, 11 MiB, the 256 MiB cut), $winid, the stdin boot, no terminal to draw on, a killed tty editor and its swept sockets, a signal unlinking the socket; mounts: --mount at startup, Mount and Unmount at run time, QUIC and TCP v4 and v6 detached and in a pty (skipped without -Dquic=true, passed with it); tree: the root and /os, 256-byte frames, a held ctl line, a frozen /screen, Look's precedence, 255-byte names; root-ctl: Restore detached and in a pty; builtins: Lspinfo; look-and-exec: the canary; repls: Python's pyrepl) and 3 behaviour tests (a 60 KB body write, truncated, a missing pane refused; a body write's EI record; keyword colour, not bold); process.zig gains, under fsb:, Conn.dialSized, dialTcp, replace, newPane, serials, look, pane, exec, execRefused, removePane, Files.fg, Detached.exists, abs, startTty, and Child (a program in a root's environment, its stdin fed, on a pty if asked, its stderr kept), run, code, entries, canonical, quicIncluded, nowMs, nap, has, check; each checked to fail against a break (no such option reworded, SyntaxBold on by default, a body insert's record not EI, Grep's miss reworded, already inside pardes reworded, a session already running reworded, Lspinfo's servers line reworded, no terminal to draw on reworded, unknown control message reworded); behaviour, process, fs-unit and core tests and the rest of fs.py pass Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- test/behaviour/event.zig | 11 + test/behaviour/settings.zig | 34 ++ test/behaviour/tree.zig | 15 + test/fs.py | 1128 ---------------------------------------- test/process.zig | 444 ++++++++++++++++ test/process/builtins.zig | 52 ++ test/process/flags.zig | 778 +++++++++++++++++++++++++++ test/process/look_and_exec.zig | 239 +++++++++ test/process/mounts.zig | 212 ++++++++ test/process/repls.zig | 79 +++ test/process/root_ctl.zig | 87 ++++ test/process/tree.zig | 178 +++++++ test/process_test.zig | 7 + 13 files changed, 2136 insertions(+), 1128 deletions(-) create mode 100644 test/process/builtins.zig create mode 100644 test/process/flags.zig create mode 100644 test/process/look_and_exec.zig create mode 100644 test/process/mounts.zig create mode 100644 test/process/repls.zig create mode 100644 test/process/root_ctl.zig create mode 100644 test/process/tree.zig (limited to 'test/process_test.zig') diff --git a/test/behaviour/event.zig b/test/behaviour/event.zig index 77a80e30..65222175 100644 --- a/test/behaviour/event.zig +++ b/test/behaviour/event.zig @@ -69,3 +69,14 @@ test "event: a bad event record says what is wrong with it" { try std.testing.expect(has(s.said, c[1])); } } + +test "event: a write to the body is an EI record to the reader holding event" { + var s = try Session.initFile(std.testing.allocator, "replaced\n"); + defer s.deinit(); + const n = try s.focused(); + const event = try s.hold(s.at("/pane/{d}/event", .{n}), 0); + defer s.clunk(event); + try s.write(s.at("/pane/{d}/body", .{n}), "event\n"); + const record = try s.readFid(event); + try std.testing.expect(std.mem.startsWith(u8, record, "EI") and has(record, "event\n")); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig index a24a3010..9a14ff50 100644 --- a/test/behaviour/settings.zig +++ b/test/behaviour/settings.zig @@ -269,3 +269,37 @@ test "settings: every on/off setting the root ctl reads flips when its word is w try std.testing.expectEqualStrings("Theme acme", try setting(&s, "Theme")); try std.testing.expectEqualStrings("Shell /bin/sh", try setting(&s, "Shell")); } + +/// A cell's style (one of `Session.styles`) as its bold flag and ink. +fn look(s: *Session, style: []const u8) !struct { bool, []const u8 } { + const a = s.arena.allocator(); + const json = try std.json.parseFromSliceLeaky(std.json.Value, a, style, .{}); + return .{ json.object.get("bold").?.bool, try std.json.Stringify.valueAlloc(a, json.object.get("fg").?, .{}) }; +} + +test "settings: a keyword is coloured apart from the text beside it, and not bold (SyntaxBold is off by default), at a long file's end" { + var s = try Session.initFile(std.testing.allocator, "initial\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), "keywords.zig\n"); + var source: std.ArrayList(u8) = .empty; + for (0..9999) |_| try source.appendSlice(s.arena.allocator(), "// padding\n"); + try source.appendSlice(s.arena.allocator(), "fn check() void {\n if (true) return;\n}\n"); + try s.replace(s.at("/pane/{d}/body", .{n}), source.items); + const screen = try s.screen(); + const styles = try s.styles(); + const cols = std.mem.indexOfScalar(u8, screen, '\n').?; + try std.testing.expectEqual(styles.len, cols * (std.mem.count(u8, screen, "\n") + 1)); + for ([_][]const u8{ "fn", "if" }) |word| { + const col, const row = try s.where(s.at("{s} ", .{word})); + const at = row * cols + col; + _, const ink = try look(&s, styles[at]); + for (styles[at..][0..word.len]) |cell| { + const bold, const each = try look(&s, cell); + try std.testing.expect(!bold); + try std.testing.expectEqualStrings(ink, each); + } + _, const plain = try look(&s, styles[at + word.len]); + try std.testing.expect(!std.mem.eql(u8, ink, plain)); + } +} diff --git a/test/behaviour/tree.zig b/test/behaviour/tree.zig index 7f9b37a4..56806589 100644 --- a/test/behaviour/tree.zig +++ b/test/behaviour/tree.zig @@ -42,3 +42,18 @@ test "tree: a terminal is listed as term with its directory as name, which a wri try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/name", .{n}), "/elsewhere\n")); try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid rename: a terminal is named")); } + +test "tree: a body takes a 60 KB write whole, a truncating one replaces it, and a pane not there is refused" { + var s = try Session.initFile(std.testing.allocator, "initial\n"); + defer s.deinit(); + var payload: std.ArrayList(u8) = .empty; + for (0..3000) |_| try payload.appendSlice(s.arena.allocator(), "const value = \"λ\";\n"); + const n = try s.newPane(); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(body, payload.items); + try s.expect(body, payload.items); + try s.replace(body, "replaced\n"); + try s.expect(body, "replaced\n"); + try std.testing.expectError(error.Refused, s.read("/pane/4294967295/body")); + try s.expect(body, "replaced\n"); +} diff --git a/test/fs.py b/test/fs.py index c5dd6234..fa3388fe 100644 --- a/test/fs.py +++ b/test/fs.py @@ -4,9 +4,6 @@ import fcntl import json import os import shutil -import select -import signal -import socket from pathlib import Path import subprocess import struct @@ -777,245 +774,6 @@ def store_shared_between_windows(binary): assert (root / '.local' / 'state' / 'pardes' / 'store').exists() -def workflow_canary(binary): - """One session driven as the dogfood agents drive it, asserting the log - and the read-backs at every step: the canary for the main paths (the - round-18 Grep that never found anything went through every narrower - test). Look files, Grep and Find hits and misses, a plain word looked - at, Edit in two files, Save, get, pty/run failing then passing, a - command pane and Kill, a REPL send, the layout and a column's files, - ask/answer, and Dump/Restore last.""" - started = time.monotonic() - with tempfile.TemporaryDirectory(prefix='pardes-flow-') as directory: - root = Path(directory) - (root / 'a.txt').write_bytes(b'alpha one\nshared word\n') - (root / 'b.txt').write_bytes(b'beta two\nshared word\n') - (root / 's.sh').write_bytes(b'echo from-file\n') - - def wait(ready, what, seconds=10): - deadline = time.monotonic() + seconds - while not ready(): - assert time.monotonic() < deadline, what - time.sleep(.05) - - with session(binary, root, 'flow') as (client, address): - def log(): - return client.read('/log').decode('utf-8', 'replace') - - def made_by(serial, line): - """The pane an exec at `serial` made, as exec reads it back.""" - client.write(f'/pane/{serial}/exec', line) - return int(client.read(f'/pane/{serial}/exec').split()[-1]) - - # Look opens files, and the log says new. - a = look(client, str(root / 'a.txt')) - b = look(client, str(root / 'b.txt')) - assert client.read(f'/pane/{a}/body') == b'alpha one\nshared word\n' - assert f'new {a} {root}/a.txt\n' in log() and f'new {b} {root}/b.txt\n' in log() - - # Grep and Find: a hit fills its query pane, line 1 the query and - # the lines under each file's name; a miss fails ENOENT. - grep = made_by(a, b'Grep shared\n') - rows = client.read(f'/pane/{grep}/body').decode() - assert rows == 'Grep shared\na.txt\na.txt:2:1 shared word\nb.txt\nb.txt:2:1 shared word\n', rows - client.remove(f'/pane/{grep}') - execute_refused(client, a, 'Grep zzqqnomatch', 'Grep: text not found') - found = made_by(a, b'Find b.txt\n') - assert 'b.txt' in client.read(f'/pane/{found}/body').decode() - client.remove(f'/pane/{found}') - execute_refused(client, a, 'Find zzqqnomatch', 'Find: name not found') - - # A plain word looked at is found in the pane, after its dot. - client.write(f'/pane/{a}/dot', b' 0 0 ') - client.write(f'/pane/{a}/look', b'shared\n') - assert client.read(f'/pane/{a}/dot').split() == [b'10', b'16'], client.read(f'/pane/{a}/dot') - - # Edit in two files, then Save each: on disk, and logged. - for pane in (a, b): - client.write(f'/pane/{pane}/ctl', b'Edit ,s/shared/SHARED/g\n') - assert client.read(f'/pane/{b}/body') == b'beta two\nSHARED word\n' - for pane in (a, b): - client.write(f'/pane/{pane}/exec', b'Save\n') - assert (root / 'a.txt').read_bytes() == b'alpha one\nSHARED word\n' - assert f'save {a} {root}/a.txt\n' in log() and f'save {b} {root}/b.txt\n' in log() - - # get reloads what changed on disk. - (root / 'b.txt').write_bytes(b'from disk\n') - client.write(f'/pane/{b}/ctl', b'get\n') - assert client.read(f'/pane/{b}/body') == b'from disk\n' - - # A terminal: pty/run failing then passing. - term = made_by(1, b'Tty\n') - - def run(line): - fid = client.open(f'/pane/{term}/pty/run', 2) - try: - client.rpc(118, struct.pack('= 2, 'the REPL did not get it') - client.write(f'/pane/{term}/ctl', b'Repl -\n') - client.write(f'/pane/{term}/pty/data', b'\x04') - wait(lambda: run(b'true\n') == b'exit 0\n', 'the terminal did not come back to its shell') - - # The layout and a column's files: Newcol, its tag, rmdir. - assert len(client.read('/layout').splitlines()) >= 2 # a column, then `active` - client.write('/ctl', b'Newcol\n') - columns = [row for row in client.read('/layout').splitlines() if not row.startswith(b'active')] - assert len(columns) == 2, columns - empty = columns[-1].split()[0].decode() - assert b'New' in client.read(f'/col/{empty}/tag') - client.remove(f'/col/{empty}') - assert len([row for row in client.read('/layout').splitlines() if not row.startswith(b'active')]) == 1 - - # ask/answer: Save on a terminal asks for a path over 9P. - client.write(f'/pane/{term}/exec', b'Save\n') - wait(lambda: f'ask {term} save path\n' in log(), 'no ask logged') - client.write(f'/pane/{term}/ctl', f'answer {root}/scroll.txt\n'.encode()) - wait(lambda: (root / 'scroll.txt').exists(), 'the answered Save wrote nothing') - assert f'answer {term} {root}/scroll.txt\n' in log() - - # Dump, then Restore it: the new session names its panes. - client.write('/ctl', b'Dump\n') - saved = root / 'flow.dump.zon' - wait(saved.exists, 'Dump wrote nothing') - assert f'dump {saved}\n' in log() - try: - client.write('/ctl', f'Restore {saved}\n'.encode()) - except (EOFError, OSError): - pass - time.sleep(.5) - with Client(address) as restored: - wait(lambda: f'restore {saved}' in restored.read('/log').decode(), 'Restore not logged') - names = restored.read('/index').decode() - assert f'{root}/a.txt' in names and f'{root}/b.txt' in names, names - # Recent goes on across the Restore, and is kept after it. - assert f'{root}/a.txt\n' in restored.read('/recent').decode(), restored.read('/recent') - (root / 'after.txt').write_bytes(b'after\n') - look(restored, str(root / 'after.txt')) - assert f'open {root}/after.txt\n' in restored.read('/recent').decode() - state = root / '.local' / 'state' / 'pardes' / 'store' - wait(lambda: f'{root}/after.txt'.encode() in state.read_bytes(), 'Recent not kept after Restore') - assert time.monotonic() - started < 30, 'the canary took too long' - - -def python_repl_runs_one_line(binary): - """A one-line exec sent to Python's own REPL (3.13's pyrepl, which asks - for bracketed paste) runs at once, with no second Enter: the Enter - reaches it after the paste, not inside it. A real pty, a real python3.""" - python = shutil.which('python3') - if python is None: - return - version = subprocess.run([python, '-c', 'import sys; print(sys.version_info >= (3, 13))'], - capture_output=True, text=True).stdout.strip() - if version != 'True': - return - with tempfile.TemporaryDirectory(prefix='pardes-repl-') as directory: - with session(binary, Path(directory), 'repl') as (client, _): - def panes(client): - return {int(row.split()[0]) for row in client.read('/index').splitlines()} - before = panes(client) - client.write('/pane/1/ctl', b'Tty\n') - deadline = time.monotonic() + 10 - while time.monotonic() < deadline and panes(client) == before: - time.sleep(.05) - tty = max(panes(client) - before) - def body(): - return client.read(f'/pane/{tty}/body').decode('utf-8', 'replace') - def wait_for(text, seconds=15): - deadline = time.monotonic() + seconds - while time.monotonic() < deadline: - if text in body(): - return True - time.sleep(.1) - return False - assert wait_for('$') or wait_for('>'), body() - client.write(f'/pane/{tty}/pty/data', python.encode() + b' -q\r') - assert wait_for('>>>'), body() - client.write(f'/pane/{tty}/ctl', b'Repl python\n') - # An exec on the REPL's own body goes to the REPL, as its event - # record written back does: an empty range acts on its text. - fid = client.open(f'/pane/{tty}/event', 2) - try: - record = b'MX0 0 0 10 print(6*7)\n' - client.rpc(118, struct.pack('>>'), (code, body()[-200:]) - finally: - client.close(fid) - - -def signal_cleans_up(binary): - """A detached session told to stop by a signal unlinks its sockets. A - hangup is a terminal's end, not a session's: the daemon keeps it.""" - for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP]: - with tempfile.TemporaryDirectory(prefix='pardes-sig-') as directory: - root = Path(directory) - seen = {} - with session(binary, root, 'sig', terminal=seen) as (client, address): - assert address.exists() - if sig == signal.SIGHUP: - os.kill(seen['pid'], sig) - time.sleep(.3) - assert address.exists() and client.read('/index'), 'a hangup ended a kept session' - sig = signal.SIGTERM - os.kill(seen['pid'], sig) - deadline = time.monotonic() + 5 - while address.exists() and time.monotonic() < deadline: - time.sleep(.01) - assert not address.exists(), (sig, list(root.iterdir())) - - def odd_paths(binary): """Long and odd paths written where a path goes -- name, look, DumpDir, Dump, Save -- are refused or said, never a crash: a part over 255 bytes @@ -1217,897 +975,11 @@ def test(binary, quic=False): new_terminals_named_once(binary) directory_panes(binary) putall_answers_after_its_saves(binary) - workflow_canary(binary) recent_survives_restart(binary) store_shared_between_windows(binary) - python_repl_runs_one_line(binary) - signal_cleans_up(binary) - for options, message in [ - (['--fs'], b'no such option'), - (['--fs9'], b'no such option'), - (['--', 'a', 'b'], b'is the second'), - (['--9p'], b'--9p needs a socket name'), - (['--9p='], b'invalid 9P socket name'), - (['--9p=a/b'], b'invalid 9P socket name'), - (['--attach=work', '--9p=work'], b'--attach has none'), - (['--9p-tcp'], b'--9p-tcp needs'), - (['--9p-tcp='], b'--9p-tcp needs'), - (['--9p-tcp=tcp!localhost!5640'], b'--9p-tcp needs'), - (['--9p-tcp=quic!127.0.0.1!5640'], b'--9p-tcp needs'), - (['--9p-tcp=tcp!127.0.0.1!5640', '--9p-tcp=tcp!127.0.0.1!5641'], b'specified twice'), - (['--attach=work', '--9p-tcp=tcp!127.0.0.1!5640'], b'--attach has none'), - (['--9p-quic'], b'--9p-quic needs'), - (['--9p-quic='], b'--9p-quic needs'), - (['--9p-quic=tcp!127.0.0.1!5640'], b'--9p-quic needs'), - (['--mount=os=work'], b'invalid mount name'), - (['--mount=peer='], b'invalid mount name or dial'), - (['--mount=peer=one', '--mount=peer=two'], b'duplicate mount'), - (['--mount=peer=tcp!127.0.0.1!0'], b'invalid mount dial'), - (['--attach=work', '--mount=peer=one'], b'--attach has none'), - ]: - result = subprocess.run([binary, *options], capture_output=True, timeout=5) - assert result.returncode == 1 and message in result.stderr, (options, result) - with tempfile.TemporaryDirectory(prefix='pardes-fs-') as directory: - root = Path(directory) - with session(binary, root, 'renamed', '--9p=explicit', socket_name='explicit') as (named, named_address): - assert named_address.name == 'pardes-9p-explicit.sock' - assert not (root / 'pardes-9p-renamed.sock').exists() - assert named.read('/pane/1/body') == b'initial\n' - with session(binary, root, 'first') as (client, address): - assert {'pane', 'index', 'status', 'look', 'exec', 'log', 'os'} <= set(client.list('/')) - assert 'self' not in client.list('/') - assert client.read('/pane/1/body') == b'initial\n' - assert client.read('/os' + str(root / 'first.txt')) == b'initial\n' - assert 'first.txt' in client.list('/os' + str(root)) - assert client.read('/pane/1/../1/body') == b'initial\n' - assert client.read('/os' + str(root) + '/../' + root.name + '/first.txt') == b'initial\n' - - listing = root / 'listing' - listing.mkdir() - (listing / 'broken').symlink_to(listing / 'missing') - for number in range(24): - (listing / f'entry-{number:02}').touch() - with Client(address, msize=256) as small: - assert set(small.list('/os' + str(listing))) == { - f'entry-{number:02}' for number in range(24)} - # 64 KiB frames: a ctl line with no newline is held whatever the - # size of its write -- a mount cuts lines anywhere -- and runs at - # the close, its failure then in the log. - with Client(address, msize=65536) as big: - assert big.msize == 65536 - ctl = big.open('/pane/1/ctl', 1) - line = b'bogus' + b'x' * 20000 - big.rpc(118, struct.pack('= 0 and f'\nexit {serial} ' in log[ran:]: - body = client.read(f'/pane/{serial}/body').decode().split('% echo winid')[-1] - client.remove(f'/pane/{serial}') - return body - assert time.monotonic() < deadline, log[-400:] - time.sleep(.05) - first_pane = min(int(row.split()[0]) for row in client.read('/index').splitlines()) - assert f'winid={first_pane}\n' in command_body(f'/pane/{first_pane}/tagexec', b'echo winid=$winid\n') - assert 'winid=\n' in command_body('/tagexec', b'echo winid=$winid\n') - # A second --detach of a name a live session holds: said, exit 1, - # no Zig error trace. - second_env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} - second_env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root)) - second = subprocess.run([binary, '--detach=wait'], cwd=root, env=second_env, - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, timeout=20) - assert second.returncode == 1, second - assert second.stderr == b'pardes: a session named wait is already running\n', second.stderr - status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines()) - env = {k: v for k, v in os.environ.items() if k != 'PARDES_FORWARD_LOOK'} - env.update(PARDES_9P=str(address), PARDES_PANE='1', PARDES_PID=status['pid']) - edited = root / 'wait edit.txt' - edited.write_bytes(b'edit me\n') - - def serials(): - return {int(row.split()[0]) for row in client.read('/index').splitlines()} - - def launch(flag): - before = serials() - child = subprocess.Popen([binary, flag, str(edited)], cwd=root, env=env, - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) - deadline = time.monotonic() + 3 - while child.poll() is None and not serials() - before and time.monotonic() < deadline: - time.sleep(.01) - return child - - first = launch('--wait') - opened, = serials() - {1} - second = launch('-w') - time.sleep(.5) - assert first.poll() is None and second.poll() is None, (first.stderr.read(), second.stderr.read()) - assert serials() == {1, opened} - client.write(f'/pane/{opened}/body', b'edited\n', truncate=True) - execute(client, opened, 'Save') - client.remove(f'/pane/{opened}') - deleted = time.monotonic() - assert first.wait(timeout=5) == 0 - # Woken by the `del` record, not a poll. - assert time.monotonic() - deleted < .05, time.monotonic() - deleted - assert second.wait(timeout=5) == 0 - assert edited.read_bytes() == b'edited\n' - # --wait from outside the session's panes, $PARDES_9P alone (an - # agent's EDITOR): asked of the session, not a screen of its own. - outside_env = {k: v for k, v in env.items() if k not in ('PARDES_PANE', 'PARDES_PID')} - far = root / 'wait far.txt' - far.write_bytes(b'far\n') - before = serials() - waiting = subprocess.Popen([binary, '--wait', str(far)], cwd=root, env=outside_env, - stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) - deadline = time.monotonic() + 5 - while not serials() - before and time.monotonic() < deadline: - time.sleep(.01) - far_pane, = serials() - before - time.sleep(.3) - assert waiting.poll() is None, waiting.stderr.read() - client.remove(f'/pane/{far_pane}') - assert waiting.wait(timeout=5) == 0 - # --wait with a $PARDES_9P that answers no more: said, exit 1, - # never a screen of its own drawn in the asker's terminal. - dead_env = dict(outside_env, PARDES_9P=str(root / 'pardes-9p-gone.sock')) - gone = subprocess.run([binary, '--wait', str(far)], cwd=root, env=dead_env, capture_output=True, timeout=10) - assert gone.returncode == 1 and b'does not answer' in gone.stderr, gone - # --wait with no FILE says it needs one, and waits on nothing. - bare = subprocess.run([binary, '--wait'], cwd=root, env=env, capture_output=True, timeout=10) - assert bare.returncode == 1 and b'--wait needs a FILE' in bare.stderr, bare - # `--` ends the options: a name starting with a dash is a name. - dash = root / '-dash.txt' - dash.write_bytes(b'dash\n') - before = serials() - assert subprocess.run([binary, '--', dash.name], cwd=root, env=env, capture_output=True, timeout=10).returncode == 0 - dashed, = serials() - before - assert client.read(f'/pane/{dashed}/name').rstrip(b'\n') == str(dash).encode() - client.remove(f'/pane/{dashed}') - # A name under a directory that may not be searched is refused, - # permission denied, exit 1, no pane: its Save could never be. - # (Root searches any directory.) - if os.geteuid() != 0: - (root / 'noperm').mkdir() - (root / 'noperm').chmod(0) - before = serials() - try: - denied = subprocess.run([binary, 'noperm/a/b/c'], cwd=root, env=env, capture_output=True, timeout=10) - finally: - (root / 'noperm').chmod(0o755) - assert denied.returncode == 1 and b'permission denied' in denied.stderr, denied - assert serials() == before - # --wait waits on the pane its look answered, on the look's own - # open: a name /index shows escaped (a backslash is `\\` there) - # is still the pane waited on, not one already gone. - odd = root / 'wait\\odd.txt' - odd.write_bytes(b'odd\n') - before = serials() - waiting = subprocess.Popen([binary, '--wait', str(odd)], cwd=root, env=env, - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) - deadline = time.monotonic() + 3 - while not serials() - before and time.monotonic() < deadline: - time.sleep(.01) - odd_pane, = serials() - before - time.sleep(.5) - assert waiting.poll() is None, waiting.stderr.read() - client.remove(f'/pane/{odd_pane}') - assert waiting.wait(timeout=5) == 0 - # A name not there yet opens a pane named it, as acme's B does, - # and Save creates it: git's and fish's temporary files are. - fresh = root / 'wait new.txt' - before = serials() - made = subprocess.Popen([binary, '--wait', fresh.name], cwd=root, env=env, - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) - # The launch makes the pane (pane/new), then names it: wait for - # the name, not the pane. - deadline = time.monotonic() + 3 - while str(fresh).encode() not in client.read('/index'): - assert made.poll() is None, made.stderr.read() - assert time.monotonic() < deadline, 'a new name opened no pane named it' - time.sleep(.01) - named, = serials() - before - client.write(f'/pane/{named}/body', b'new\n') - execute(client, named, 'Save') - client.remove(f'/pane/{named}') - code = made.wait(timeout=5) - assert code == 0, (code, made.stderr.read()) - assert fresh.read_bytes() == b'new\n' - # ...in a directory not there either: forwarded all the same, and - # Save makes the directories. - deep = root / 'wait-missing' / 'sub' / 'new.txt' - before = serials() - made = subprocess.Popen([binary, 'wait-missing/sub/new.txt'], cwd=root, env=env, - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) - assert made.wait(timeout=5) == 0, made.stderr.read() - deadline = time.monotonic() + 3 - while str(deep).encode() not in client.read('/index'): - assert time.monotonic() < deadline, 'a name in a missing directory opened no pane named it' - time.sleep(.01) - named, = serials() - before - client.write(f'/pane/{named}/body', b'deep\n') - execute(client, named, 'Save') - client.remove(f'/pane/{named}') - assert deep.read_bytes() == b'deep\n' - # `pardes '~/x'`, quoted so the shell left the `~`: home all the - # same: the launching shell's HOME. - before = serials() - quoted = subprocess.run([binary, '~/fwd-tilde.txt'], cwd=root, env=dict(env, HOME=str(root)), - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, timeout=10) - assert quoted.returncode == 0, quoted.stderr - assert f'{root}/fwd-tilde.txt'.encode() in client.read('/index'), client.read('/index') - for serial in serials() - before: - client.remove(f'/pane/{serial}') - # A name the session refuses: its reason and exit 1, as B's, - # never a nested editor, and no pane left for it. - before = serials() - refused = subprocess.run([binary, 'x' * 300 + '.txt'], cwd=root, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert refused.returncode == 1, refused - assert b'a component over 255 bytes' in refused.stderr, refused.stderr - assert serials() == before, (serials(), before) - # A pane address the session has not is refused in words, exit - # 1, no pane made for a file of that name; one it has is looked - # at. A name with a newline is said in words, not an error name. - # `pardes -` (a terminal's PAGER): stdin, its escapes stripped, in a - # +Pager pane of the session, clean, and back at once. - fed = subprocess.run([binary, '-'], cwd=root, env=env, input=b'\x1b[1mbold\x1b[0m text\n', - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert fed.returncode == 0, fed.stderr - made_now, = serials() - before - assert client.read(f'/pane/{made_now}/name') == f'{root}/+Pager\n'.encode() - assert client.read(f'/pane/{made_now}/body') == b'bold text\n' - row = [r for r in client.read('/index').splitlines() if r.split()[0] == str(made_now).encode()][0] - assert row.split()[2] == b'0', row # clean: nothing to ask at Exit - # Its `new` record names it as it is: no +New and a rename. - log_now = client.read('/log').decode() - assert f'\nnew {made_now} {root}/+Pager\n' in '\n' + log_now, log_now[-400:] - assert f'\nrename {made_now} ' not in '\n' + log_now - # Its colours are the pane's, drawn and never read: the body is - # the plain text, and the screen shows `red` in the theme's red, - # the rest in the pane's ink. Mapped through the theme, as a - # terminal's are; Filter shows them as sent, `rgb` in the colour - # given. - # Each run is its own +Pager; one there already keeps its page. - fed = subprocess.run([binary, '-'], cwd=root, env=env, - input=b'\x1b]0;title\x07\x1b[31mred\x1b[m \x1b[38;2;1;2;3mrgb\x1b[m plain\n', - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert fed.returncode == 0, fed.stderr - coloured, = serials() - before - {made_now} - assert client.read(f'/pane/{coloured}/name') == f'{root}/+Pager\n'.encode() - assert client.read(f'/pane/{coloured}/body') == b'red rgb plain\n' - assert client.read(f'/pane/{made_now}/body') == b'bold text\n' - def inks(): - screen = client.screen() - for row in range(screen['rows']): - cells = screen['cells'][row * screen['cols']:(row + 1) * screen['cols']] - at = ''.join(cell[0] for cell in cells).find('red rgb plain') - if at >= 0: - return [screen['styles'][cells[at + i][1]]['fg'] for i in (0, 4, 8)] - raise AssertionError(('the paged text is not on the screen', screen)) - ink = inks() - assert ink[0] != ink[2] and ink[1] != {'rgb': [1, 2, 3]}, ink - client.write(f'/pane/{coloured}/exec', b'Filter\n') - ink = inks() - assert ink[1] == {'rgb': [1, 2, 3]}, ink - assert ink[0] != ink[2] and ink[0] != ink[1], ink - # A progress line keeps its last state. - fed = subprocess.run([binary, '-'], cwd=root, env=env, input=b'again\r\nprogress 10%\rprogress 100%\n', - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert fed.returncode == 0, fed.stderr - again, = serials() - before - {made_now, coloured} - assert client.read(f'/pane/{again}/body') == b'again\nprogress 100%\n' - for serial in (made_now, coloured, again): - client.remove(f'/pane/{serial}') - # More than one 8 MiB write's worth arrives whole, byte for byte, - # in its +Pager (each write once replaced the last). - big = b''.join(b'line %08d of the paged text\n' % i for i in range(400000)) # ~11 MiB - assert len(big) > 8 * 1024 * 1024 - fed = subprocess.run([binary, '-'], cwd=root, env=env, input=big, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=120) - assert fed.returncode == 0, fed.stderr - big_pane, = serials() - before - assert client.read(f'/pane/{big_pane}/body') == big, 'the paged body is not the text' - # Past the 256 MiB cap: the first 256 MiB, then the cut note. - cap = 256 * 1024 * 1024 - huge = subprocess.Popen(['sh', '-c', f'yes 0123456789abcdef | head -c {cap + 4096}'], stdout=subprocess.PIPE) - fed = subprocess.run([binary, '-'], cwd=root, env=env, stdin=huge.stdout, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=600) - huge.wait() - assert fed.returncode == 0, fed.stderr - client.remove(f'/pane/{big_pane}') - big_pane, = serials() - before - got = client.read(f'/pane/{big_pane}/body') - note = b'\n[pardes: the rest of stdin is cut: a pane holds at most a file\'s limit]\n' - assert got.endswith(note) and len(got) == cap + len(note), (len(got), got[-120:]) - assert got[:cap] == (b'0123456789abcdef\n' * (cap // 17 + 1))[:cap] - del got, big - client.remove(f'/pane/{big_pane}') - # From a directory whose name could not be a ctl line: the - # session's directory's +Pager, the text whole, no stray +New. - odd = root / 'two\nlines' - odd.mkdir() - fed = subprocess.run([binary, '-'], cwd=odd, env=env, input=b'odd dir text\n', - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert fed.returncode == 0, fed.stderr - odd_pane, = serials() - before - assert client.read(f'/pane/{odd_pane}/name') == f'{root}/+Pager\n'.encode() - assert client.read(f'/pane/{odd_pane}/body') == b'odd dir text\n' - client.remove(f'/pane/{odd_pane}') - # Nothing on stdin: no pane, and done. - fed = subprocess.run([binary, '-'], cwd=root, env=env, input=b'', - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert fed.returncode == 0 and serials() == before, (fed, serials()) - # A new name in a directory there but not writable: refused now, - # not a pane that only fails at Save. (Root writes anywhere.) - shut = root / 'shut-dir' - shut.mkdir() - shut.chmod(0o500) - try: - if os.geteuid() != 0: - refused = subprocess.run([binary, 'shut-dir/new.txt'], cwd=root, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert refused.returncode == 1, refused - assert b'may not be read or written' in refused.stderr, refused.stderr - assert serials() == before, (serials(), before) - finally: - shut.chmod(0o755) - for word, said in (('@p999:1', b'pardes: @p999:1: this session has no such pane'), - ('two\nlines.txt', b'pardes: two\\nlines.txt: a file name is one line')): - refused = subprocess.run([binary, word], cwd=root, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert refused.returncode == 1, refused - assert said in refused.stderr, refused.stderr - assert b'NotOneLine' not in refused.stderr and b'NoSuchPane' not in refused.stderr - assert serials() == before, (serials(), before) - looked = subprocess.run([binary, '@p1:1'], cwd=root, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert looked.returncode == 0, looked.stderr - assert serials() == before, (serials(), before) - orphan = launch('--wait') - assert orphan.poll() is None - assert orphan.wait(timeout=5) == 1 - - # `pardes -` outside a session: an editor whose first pane is a - # +Pager of stdin, as `vim -`; here a detached one, to read it. - outside_env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} - outside_env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), PARDES_NOTIME='1') - boot = subprocess.Popen([binary, '--detach=stdinboot', '-'], cwd=root, env=outside_env, - stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, - start_new_session=True) - try: - boot.stdin.write(b'\x1b[32mfrom stdin\x1b[m\nsecond\n') - boot.stdin.close() - sock = root / 'pardes-9p-stdinboot.sock' - deadline = time.monotonic() + 10 - while not sock.exists(): - assert boot.poll() is None, boot.stderr.read() - assert time.monotonic() < deadline, 'the stdin boot never listened' - time.sleep(.05) - with Client(str(sock)) as booted: - first = booted.read('/index').split(b'\n')[0].split() - assert first[3] == f'{root}/+Pager'.encode(), first - assert booted.read(f'/pane/{first[0].decode()}/body') == b'from stdin\nsecond\n' - finally: - boot.terminate() - boot.wait(timeout=10) - - # Killed with SIGTERM or SIGHUP, a tty editor puts its terminal - # back (main screen, cooked mode) before it dies: a nested one - # killed in a pane leaves that pane usable. - # ...and its 9P socket goes with it. One a dead editor left behind - # (no process has its pid) is swept at the next start; a live - # pid's, here this test's own, never is. - def unix_socket_file(path): - made = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - made.bind(str(path)) - made.close() - for sig in (signal.SIGTERM, signal.SIGHUP): - dead_socket = root / 'pardes-9p-2147483647.sock' - live_socket = root / f'pardes-9p-{os.getpid()}.sock' - for path in (dead_socket, live_socket): - if not path.exists(): - unix_socket_file(path) - master, slave = os.openpty() - fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack('HHHH', 24, 80, 0, 0)) - cooked = termios.tcgetattr(slave) - env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} - env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), TERM='xterm-256color', PARDES_NOTIME='1') - existing = set(root.glob('pardes-9p-*.sock')) - killed = subprocess.Popen([binary, '--tty', 'x.txt'], cwd=root, env=env, - stdin=slave, stdout=slave, stderr=subprocess.DEVNULL, - start_new_session=True, - preexec_fn=lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0)) - seen = b'' - try: - deadline = time.monotonic() + 10 - while b'\x1b[?1049h' not in seen: - assert time.monotonic() < deadline, seen[-200:] - if select.select([master], [], [], .1)[0]: - seen += os.read(master, 65536) - time.sleep(.3) - # The session's own, named by its pid: forked off the - # launched process, which stays as its stub. - own_socket, = set(root.glob('pardes-9p-*.sock')) - existing - assert not dead_socket.exists() - assert live_socket.exists() - killed.send_signal(sig) - assert killed.wait(timeout=5) == -sig - if sig == signal.SIGHUP: - # A hangup is the terminal's end: the session carries on - # detached (the daemon keeps it) until told to end. - assert own_socket.exists(), own_socket - os.kill(int(own_socket.name[len('pardes-9p-'):-len('.sock')]), signal.SIGTERM) - deadline = time.monotonic() + 5 - while own_socket.exists() and time.monotonic() < deadline: - time.sleep(.01) - assert not own_socket.exists(), own_socket - while select.select([master], [], [], .2)[0]: - try: - chunk = os.read(master, 65536) - except OSError: - break - if not chunk: - break - seen += chunk - tail = seen[seen.rindex(b'\x1b[?1049h'):] - assert b'\x1b[?1049l' in tail, tail[-200:] - assert termios.tcgetattr(slave)[3] & termios.ICANON == cooked[3] & termios.ICANON - finally: - if killed.poll() is None: - killed.kill() - killed.wait() - os.close(master) - os.close(slave) - - # A launch with no terminal to draw on (a pty that is no one's - # controlling terminal) says so and exits 1, no error trace. - master, slave = os.openpty() - try: - env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} - env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), TERM='xterm-256color') - result = subprocess.run([binary, '--tty', 'x.txt'], cwd=root, env=env, stdin=slave, stdout=slave, - stderr=subprocess.PIPE, start_new_session=True, timeout=10) - finally: - os.close(master) - os.close(slave) - assert result.returncode == 1, result - assert b'pardes: no terminal to draw on' in result.stderr and b'.zig:' not in result.stderr, result.stderr - - for tty in [False, True]: - name = 'lsp-tty' if tty else 'lsp-detached' - with session(binary, root, name, tty=tty) as (client, _): - control = new_pane(client, b'') - execute(client, control, 'Lspinfo') - deadline = time.monotonic() + 3 - while True: - matches = [int(row.split()[0]) for row in client.read('/index').splitlines() - if b'+Lsp' in row] - if matches: - break - assert time.monotonic() < deadline, (name, 'Lspinfo did not complete') - time.sleep(.005) - assert len(matches) == 1, matches - result = client.read(f'/pane/{matches[0]}/body') - assert result.startswith(b'backend: lsp-client\n'), result - assert b'protocol servers (lsp-client):' in result, result - assert client.read('/pane/1/body') == b'initial\n' - assert '+Lsp' in ''.join(cell[0] for cell in client.screen()['cells']) - - with session(binary, root, 'highlight') as (client, _): - client.write('/pane/1/name', b'keywords.zig\n') - source = b'// padding\n' * 9999 + b'fn check() void {\n if (true) return;\n}\n' - client.write('/pane/1/body', source, truncate=True) - screen = client.screen() - assert len(screen['cells']) == screen['cols'] * screen['rows'] - found = set() - for row in range(screen['rows']): - cells = screen['cells'][row * screen['cols']:(row + 1) * screen['cols']] - text = ''.join(cell[0] for cell in cells) - for word in ['fn', 'if']: - start = text.find(word + ' ') - if start < 0: - continue - styles = [screen['styles'][cell[1]] for cell in cells[start:start + len(word)]] - # Weight is opt-in (SyntaxBold, off by default); colour is - # not, so the keyword must differ from the text beside it. - plain = screen['styles'][cells[start + len(word)][1]] - assert all(not style['bold'] and style['fg'] == styles[0]['fg'] for style in styles), (word, styles) - assert styles[0]['fg'] != plain['fg'], (word, styles[0], plain) - found.add(word) - assert found == {'fn', 'if'}, screen - - with session(binary, root, 'long-names') as (client, _): - directory = root / ('directory-' + 'd' * 54) - directory.mkdir() - for length in [29, 63, 127, 255]: - name = 'f' * (length - 4) + '.txt' - path = directory / name - path.write_bytes(b'long filename\n') - wire_path = '/os' + str(path) - assert name in client.list('/os' + str(directory)) - assert client.read(wire_path) == b'long filename\n' - client.write(wire_path, b'written through long filename\n', truncate=True) - assert path.read_bytes() == b'written through long filename\n' - opened = look(client, '/n/os' + str(path)) - assert client.read(f'/pane/{opened}/body') == path.read_bytes() - client.remove(f'/pane/{opened}') # a long name's tag takes rows - opened = look(client, '/n/os' + str(directory)) - listing = client.read(f'/pane/{opened}/body') - for path in directory.iterdir(): - assert ('/n/os' + str(path)).encode() in listing - if quic: - quic_test(binary) print(f'9P integration: passed ({time.monotonic() - started:.3f}s)') -def quic_test(binary): - for tty in [False, True]: - for host in ['127.0.0.1', '::1']: - with tempfile.TemporaryDirectory(prefix='pardes-quic-fs-') as directory: - root = Path(directory) - ports = {} - with session(binary, root, 'server', '--9p-tcp=tcp!' + host + '!0', - '--9p-quic=quic!' + host + '!0', tty=tty, network=ports) as (unix, address): - assert set(unix.read('/listeners').decode().splitlines()) == { - 'unix!' + str(address), 'tcp!' + host + '!' + str(ports['TCP']), - 'quic!' + host + '!' + str(ports['QUIC'])} - with Client((host, ports['TCP'])) as tcp: - unix_body = unix.open('/pane/1/body') - tcp_body = tcp.open('/pane/1/body') - with session(binary, root, 'client') as (remote, _): - control = new_pane(remote, b'') - dial = 'quic!' + host + '!' + str(ports['QUIC']) - execute(remote, control, 'Mount peer ' + dial) - opened = look(remote, '/n/peer/pane/1/body', source=control) - body = f'/pane/{opened}/body' - assert remote.read(body) == b'initial\n' - payload = 'saved café λ\n'.encode() + b'x' * (3 * 8192 + 27) + b'\n' - remote.write(body, payload, truncate=True) - remote.write(f'/pane/{opened}/exec', b'Save\n') - assert unix.read_fid(unix_body) == payload[:unix.msize - 11] - assert tcp.read('/pane/1/body') == payload - tcp.write('/pane/1/body', b'TCP to QUIC Get\n', truncate=True) - remote.write(f'/pane/{opened}/ctl', b'get\n') - assert remote.read(body) == b'TCP to QUIC Get\n' - assert unix.read_fid(unix_body) == b'TCP to QUIC Get\n' - assert tcp.read_fid(tcp_body) == b'TCP to QUIC Get\n' - screen_pane = look(remote, '/n/peer/screen', source=control) - screen = json.loads(remote.read(f'/pane/{screen_pane}/body')) - assert len(screen['cells']) == screen['cols'] * screen['rows'] - # The peer's own screen, whole, over QUIC. Its pane's - # tag, not its text: where the view sits after the - # body shrank from a long wrapped line depends on - # whether a frame was drawn meanwhile (sometimes - # on the empty last line, ` 2`, text off-screen). - assert 'server.txt Save' in ''.join(cell[0] for cell in screen['cells']) - large = ('normal λ line\n' * 20000).encode() - fixture = root / 'large.txt' - fixture.write_bytes(large) - large_pane = look(remote, '/n/peer/os' + str(fixture), source=control) - assert remote.read(f'/pane/{large_pane}/body') == large - edited = b'QUIC file saved\n' - remote.write(f'/pane/{large_pane}/body', edited, truncate=True) - remote.write(f'/pane/{large_pane}/exec', b'Save\n') - assert fixture.read_bytes() == edited - for pane in [opened, screen_pane, large_pane]: - remote.write(f'/pane/{pane}/exec', b'Del\n') - execute(remote, control, 'Unmount peer') - roots = look(remote, '/n', source=control) - assert remote.read(f'/pane/{roots}/body') == b'/n/os/\n/n/self/\n' - assert unix.read_fid(unix_body) == b'TCP to QUIC Get\n' - assert tcp.read_fid(tcp_body) == b'TCP to QUIC Get\n' - # Not checked: a session Looking at its OWN tree through - # a QUIC mount ('Mount own quic!...'). QUIC is still - # answered on the editor's thread (cloud9's QUIC adapter - # is not std.Io based), so that Look waits on the loop - # it is running in and opens nothing; features.txt lists - # it under "Left as they are". Unix and TCP self-mounts - # are covered by test/selfmount.py. - unix.close(unix_body) - tcp.close(tcp_body) - if __name__ == '__main__': if len(sys.argv) in [3, 4] and sys.argv[2] == '--discovery': diff --git a/test/process.zig b/test/process.zig index f797c063..3d74c72b 100644 --- a/test/process.zig +++ b/test/process.zig @@ -182,6 +182,128 @@ pub const Conn = struct { } return names.toOwnedSlice(gpa); } + + // fsb: + + /// `dial`, versioned at `size` (a client of small frames, at least + /// 217, cloud9's least). + pub fn dialSized(gpa: std.mem.Allocator, path: []const u8, size: u32) !*Conn { + var addr: libc.sockaddr.un = .{ .path = undefined }; + if (path.len >= addr.path.len) return error.NameTooLong; + @memset(&addr.path, 0); + @memcpy(addr.path[0..path.len], path); + return connectTo(gpa, libc.AF.UNIX, @ptrCast(&addr), @sizeOf(libc.sockaddr.un), size); + } + + /// A connection over TCP to `host` (numeric, v4 or v6) at `port`. + pub fn dialTcp(gpa: std.mem.Allocator, host: [:0]const u8, port: u16) !*Conn { + if (std.mem.indexOfScalar(u8, host, ':') != null) { + var addr: libc.sockaddr.in6 = .{ .port = std.mem.nativeToBig(u16, port), .flowinfo = 0, .addr = undefined, .scope_id = 0 }; + if (inet_pton(libc.AF.INET6, host, &addr.addr) != 1) return error.BadHost; + return connectTo(gpa, libc.AF.INET6, @ptrCast(&addr), @sizeOf(libc.sockaddr.in6), msize); + } + var addr: libc.sockaddr.in = .{ .port = std.mem.nativeToBig(u16, port), .addr = undefined }; + if (inet_pton(libc.AF.INET, host, &addr.addr) != 1) return error.BadHost; + return connectTo(gpa, libc.AF.INET, @ptrCast(&addr), @sizeOf(libc.sockaddr.in), msize); + } + + fn connectTo(gpa: std.mem.Allocator, family: c_uint, addr: *const libc.sockaddr, len: libc.socklen_t, size: u32) !*Conn { + const c = try gpa.create(Conn); + errdefer gpa.destroy(c); + c.* = .{ .fd = -1, .cl = undefined }; + c.cl = c9.Client.init(.{ .in = &c.in, .out = &c.out }); + c.fd = libc.socket(family, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0); + if (c.fd < 0) return error.NoSocket; + errdefer _ = libc.close(c.fd); + if (libc.connect(c.fd, addr, len) != 0) return error.ConnectFailed; + _ = try c.call(.{ .version = .{ .msize = size } }); + _ = try c.call(.{ .attach = .{ .fid = 0, .uname = "test" } }); + return c; + } + + /// The frame size the server agreed to. + pub fn msizeAgreed(c: *const Conn) u32 { + return c.cl.msize; + } + + /// `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; + } + _ = try c.call(.{ .clunk = .{ .fid = fid } }); + } + + /// 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 serials /index lists, in its order. + pub fn serials(c: *Conn, gpa: std.mem.Allocator) ![]u32 { + const index = try c.read(gpa, "/index"); + defer gpa.free(index); + var out: std.ArrayList(u32) = .empty; + var rows = std.mem.tokenizeScalar(u8, index, '\n'); + while (rows.next()) |row| try out.append(gpa, try std.fmt.parseInt(u32, row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse row.len], 10)); + return out.toOwnedSlice(gpa); + } + + /// A Look at `path` from pane `source`, or from a scratch pane removed + /// after; the one pane it answers (test/fs.py's look). + pub fn look(c: *Conn, gpa: std.mem.Allocator, path: []const u8, source: ?u32) !u32 { + const from = source orelse try c.newPane(gpa); + 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 { + std.debug.print("look {s}: answered {s}\n", .{ path, got }); + return error.NotOneLook; + }; + if (opened == from) return error.NotOneLook; + // A column holds only so many panes, each its tag and two rows. + if (source == null) try c.remove(try std.fmt.bufPrint(&buf, "/pane/{d}", .{from})); + return opened; + } + + /// Pane `n`'s `file` read whole. + pub fn pane(c: *Conn, gpa: std.mem.Allocator, n: u32, file: []const u8) ![]u8 { + var buf: [64]u8 = undefined; + return c.read(gpa, try std.fmt.bufPrint(&buf, "/pane/{d}/{s}", .{ n, file })); + } + + /// A line written to pane `n`'s exec, a middle click on it. + pub fn exec(c: *Conn, n: u32, line: []const u8) !void { + var buf: [64]u8 = undefined; + try c.write(try std.fmt.bufPrint(&buf, "/pane/{d}/exec", .{n}), line); + } + + /// An exec that fails: refused, its words holding `words`. + pub fn execRefused(c: *Conn, n: u32, line: []const u8, words: []const u8) !void { + if (c.exec(n, line)) |_| { + std.debug.print("{s}: taken\n", .{line}); + return error.Taken; + } else |err| if (err != error.Refused) return err; + try check(has(c.said, words), "{s}: refused {s}", .{ line, c.said }); + } + + pub fn removePane(c: *Conn, n: u32) !void { + var buf: [64]u8 = undefined; + try c.remove(try std.fmt.bufPrint(&buf, "/pane/{d}", .{n})); + } }; /// A running pardes's file tree, dialled by its pid or socket path. @@ -276,6 +398,18 @@ pub const Files = struct { pub fn at(f: *Files, comptime fmt: []const u8, args: anytype) []const u8 { return std.fmt.allocPrint(f.arena.allocator(), fmt, args) catch @panic("out of memory"); } + + // fsb: + + /// The ink /screen draws at a cell, as its JSON (`{"rgb":[1,2,3]}`). + pub fn fg(f: *Files, col: usize, row: usize) ![]const u8 { + const a = f.arena.allocator(); + const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try f.read("/screen"), .{}); + const cols: usize = @intCast(json.object.get("cols").?.integer); + const cell = json.object.get("cells").?.array.items[row * cols + col]; + const style = json.object.get("styles").?.array.items[@intCast(cell.array.items[1].integer)]; + return std.json.Stringify.valueAlloc(a, style.object.get("fg").?, .{}); + } }; /// The files of the app a pty harness started. @@ -476,9 +610,319 @@ pub const Detached = struct { } d.pid = 0; } + + // fsb: + + /// Whether `rel` under the root is there (a dangling link is not). + pub fn exists(d: *const Detached, rel: []const u8) bool { + var buf: [4096]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&buf, "{s}/{s}", .{ d.root(), rel }, 0) catch return false; + return libc.access(path, 0) == 0; + } + + /// `rel` under the root, absolute, kept until deinit. + pub fn abs(d: *Detached, rel: []const u8) [:0]const u8 { + return std.fmt.allocPrintSentinel(d.files.arena.allocator(), "{s}/{s}", .{ d.root(), rel }, 0) catch @panic("out of memory"); + } + + /// Another session in this root, in a pty, as fs.py's tty sessions: + /// `pardes --tty --9p= /.txt` (holding + /// "initial\n"), back once its socket (`abs("pardes-9p-.sock")`) + /// is there. + pub fn startTty(d: *Detached, exe: [:0]const u8, name: []const u8, args: []const [:0]const u8) !Child { + const a = d.files.arena.allocator(); + try d.put(try std.fmt.allocPrint(a, "{s}.txt", .{name}), "initial\n"); + var argv: std.ArrayList([:0]const u8) = .empty; + try argv.appendSlice(a, &.{ exe, "--tty", try std.fmt.allocPrintSentinel(a, "--9p={s}", .{name}, 0) }); + try argv.appendSlice(a, args); + try argv.append(a, d.abs(try std.fmt.allocPrint(a, "{s}.txt", .{name}))); + const env = try a.dupe([2][:0]const u8, &.{.{ "PARDES_DUMP", d.abs(try std.fmt.allocPrint(a, "{s}.dump.zon", .{name})) }}); + var c = try Child.spawn(argv.items, .{ .root = d.root(), .env = env, .pty = .{ .log = d.abs(try std.fmt.allocPrint(a, "{s}.pty", .{name})) } }); + errdefer c.deinit(); + try c.waitPath(d.abs(try std.fmt.allocPrint(a, "pardes-9p-{s}.sock", .{name})), 10_000); + return c; + } }; fn sleepMs(ms: u32) void { var ts: libc.timespec = .{ .sec = 0, .nsec = @as(isize, ms) * std.time.ns_per_ms }; _ = libc.nanosleep(&ts, null); } + +// fsb: + +extern "c" fn readdir(dir: *libc.DIR) ?*libc.dirent; +extern "c" fn ioctl(fd: c_int, request: c_ulong, ...) c_int; +extern "c" fn openpty(master: *c_int, slave: *c_int, name: ?[*]u8, termp: ?*const anyopaque, winp: ?*const std.posix.winsize) c_int; +extern "c" fn signal(sig: c_int, handler: usize) usize; +extern "c" var environ: [*:null]?[*:0]u8; +extern "c" fn inet_pton(family: c_uint, src: [*:0]const u8, dst: *anyopaque) c_int; +extern "c" fn pipe2(fds: *[2]c_int, flags: c_int) c_int; +// shortcut: Linux's O_CLOEXEC, TIOCSCTTY and TCGETS, the only host these +// run on; take them from std for another. +const o_cloexec: c_int = 0o2000000; + +/// A program run as a shell in `root` would run it: the caller's +/// environment without a session's (PARDES_*, a PAGER), HOME and the +/// runtime and config directories the root, no daemon (PARDES_DAEMON=off: +/// the process is the session) and no language servers, unless `env` says +/// otherwise. Its stderr is kept (`said`). +pub const Child = struct { + pid: libc.pid_t, + err_fd: c_int, + status: ?c_int = null, + /// A pty's ends, when on one, and the process copying its output to + /// `Pty.log`. + master: c_int = -1, + slave: c_int = -1, + drain: libc.pid_t = 0, + + pub const Pty = struct { + /// Where everything the program draws goes. + log: [:0]const u8, + /// Made its controlling terminal; else a pty that is no one's. + ctty: bool = true, + }; + + pub const Options = struct { + /// HOME and the runtime and config directories; null: the caller's. + root: ?[]const u8 = null, + /// Where it runs; null: `root`, or here. + cwd: ?[]const u8 = null, + /// Set after the root's. + env: []const [2][:0]const u8 = &.{}, + /// Unset after that. + unset: []const [:0]const u8 = &.{}, + /// Its stdin, written whole and closed; null: /dev/null. + stdin: ?[]const u8 = null, + /// Started in a 24x80 pty, its stdin and stdout. + pty: ?Pty = null, + }; + + pub fn spawn(argv: []const [:0]const u8, opts: Options) !Child { + // A child gone before its stdin is written is EPIPE, not our end. + _ = signal(@intCast(@intFromEnum(libc.SIG.PIPE)), 1); + var args: [32:null]?[*:0]const u8 = @splat(null); + if (argv.len > args.len) return error.TooManyArgs; + for (argv, 0..) |a, i| args[i] = a; + var bufs: [4][4096]u8 = undefined; + // The build hands the binary over by a relative path, and the child + // runs elsewhere. + if (std.mem.indexOfScalar(u8, argv[0], '/') != null) args[0] = realpath(argv[0], &bufs[3]) orelse return error.NoBinary; + const root_z: ?[:0]const u8 = if (opts.root) |r| try std.fmt.bufPrintSentinel(&bufs[0], "{s}", .{r}, 0) else null; + const config = if (opts.root) |r| try std.fmt.bufPrintSentinel(&bufs[1], "{s}/config", .{r}, 0) else ""; + const cwd: ?[:0]const u8 = if (opts.cwd) |c| try std.fmt.bufPrintSentinel(&bufs[2], "{s}", .{c}, 0) else root_z; + // Close-on-exec, so a later child holds none of these. + var err_pipe: [2]c_int = undefined; + if (pipe2(&err_pipe, o_cloexec) != 0) return error.NoPipe; + var in_pipe: [2]c_int = .{ -1, -1 }; + if (opts.stdin != null and pipe2(&in_pipe, o_cloexec) != 0) return error.NoPipe; + var c: Child = .{ .pid = 0, .err_fd = err_pipe[0] }; + if (opts.pty != null) { + var ws: std.posix.winsize = .{ .row = 24, .col = 80, .xpixel = 0, .ypixel = 0 }; + if (openpty(&c.master, &c.slave, null, null, &ws) != 0) return error.NoPty; + for ([_]c_int{ c.master, c.slave }) |fd| _ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1)); // FD_CLOEXEC + } + const pid = libc.fork(); + if (pid < 0) return error.ForkFailed; + if (pid == 0) { + var i: usize = 0; + while (environ[i]) |kv| { + const entry = std.mem.span(kv); + const eq = std.mem.indexOfScalar(u8, entry, '=') orelse entry.len; + if (std.mem.startsWith(u8, entry, "PARDES_") or std.mem.indexOf(u8, entry[0..eq], "PAGER") != null) { + var name: [256]u8 = undefined; + const n = std.fmt.bufPrintSentinel(&name, "{s}", .{entry[0..@min(eq, 255)]}, 0) catch _exit(126); + _ = unsetenv(n); + i = 0; + } else i += 1; + } + if (root_z) |r| for ([_][2][*:0]const u8{ + .{ "HOME", r }, .{ "XDG_RUNTIME_DIR", r }, .{ "XDG_CONFIG_HOME", config }, .{ "PARDES_NOTIME", "1" }, + }) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126); + for ([_][2][*:0]const u8{ + .{ "PARDES_DAEMON", "off" }, .{ "PARDES_LSP_RS", "" }, .{ "PARDES_LSP_C", "" }, .{ "PARDES_LSP_GO", "" }, + .{ "PARDES_LSP_TS", "" }, .{ "PARDES_LSP_PY", "" }, + }) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126); + if (opts.pty != null and setenv("TERM", "xterm-256color", 1) != 0) _exit(126); + for (opts.env) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126); + for (opts.unset) |name| _ = unsetenv(name); + if (cwd) |d| if (libc.chdir(d) != 0) _exit(126); + _ = libc.setsid(); + if (opts.pty) |p| { + if (p.ctty) _ = ioctl(c.slave, 0x540E, @as(c_int, 0)); + _ = libc.dup2(c.slave, 0); + _ = libc.dup2(c.slave, 1); + } else { + const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR }, @as(libc.mode_t, 0)); + _ = libc.dup2(if (opts.stdin != null) in_pipe[0] else null_fd, 0); + _ = libc.dup2(null_fd, 1); + } + _ = libc.dup2(err_pipe[1], 2); + _ = execvp(args[0].?, &args); + _exit(127); + } + c.pid = pid; + _ = libc.close(err_pipe[1]); + if (opts.stdin) |bytes| { + _ = libc.close(in_pipe[0]); + var off: usize = 0; + while (off < bytes.len) { + const n = libc.write(in_pipe[1], bytes[off..].ptr, @min(bytes.len - off, 1 << 20)); + if (n <= 0) break; + off += @intCast(n); + } + _ = libc.close(in_pipe[1]); + } + if (opts.pty) |p| { + const drain = libc.fork(); + if (drain == 0) { + const fd = libc.open(p.log, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644)); + var buf: [16 * 1024]u8 = undefined; + while (true) { + const n = libc.read(c.master, &buf, buf.len); + if (n <= 0) _exit(0); + _ = libc.write(fd, &buf, @intCast(n)); + } + } + c.drain = drain; + } + return c; + } + + /// Its wait(2) word once it has exited, waiting up to `ms`; null while it runs. + pub fn wait(c: *Child, ms: i64) ?c_int { + if (c.status) |s| return s; + const deadline = eh.nowMs() + ms; + while (true) { + var status: c_int = 0; + if (libc.waitpid(c.pid, &status, std.c.W.NOHANG) == c.pid) { + c.status = status; + return status; + } + if (eh.nowMs() >= deadline) return null; + sleepMs(1); + } + } + + /// Everything it wrote to stderr, read to its end (its exit, or every + /// process holding it gone). + pub fn said(c: *Child, gpa: std.mem.Allocator) ![]u8 { + var out: std.ArrayList(u8) = .empty; + var buf: [4096]u8 = undefined; + while (true) { + const n = libc.read(c.err_fd, &buf, buf.len); + if (n <= 0) break; + try out.appendSlice(gpa, buf[0..@intCast(n)]); + } + return out.toOwnedSlice(gpa); + } + + /// Waits up to `ms` for `path` (its socket) to be there; its stderr + /// said if it exits first. + pub fn waitPath(c: *Child, path: []const u8, ms: i64) !void { + var buf: [4096]u8 = undefined; + const z = try std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0); + const deadline = eh.nowMs() + ms; + while (libc.access(z, 0) != 0) { + if (c.wait(0)) |status| { + const words = c.said(std.heap.page_allocator) catch ""; + std.debug.print("exited, status {d}, before {s}: {s}\n", .{ status, path, words }); + return error.Exited; + } + if (eh.nowMs() > deadline) return error.Timeout; + sleepMs(5); + } + } + + pub fn kill(c: *const Child, sig: libc.SIG) void { + if (c.status == null) _ = libc.kill(c.pid, sig); + } + + /// Ended (SIGTERM, SIGKILL after 5 s) and its fds closed. + pub fn deinit(c: *Child) void { + if (c.wait(0) == null) { + _ = libc.kill(c.pid, libc.SIG.TERM); + if (c.wait(5_000) == null) { + _ = libc.kill(c.pid, libc.SIG.KILL); + _ = c.wait(5_000); + } + } + if (c.drain > 0) { + _ = libc.kill(c.drain, libc.SIG.KILL); + var st: c_int = 0; + _ = libc.waitpid(c.drain, &st, 0); + } + for ([_]c_int{ c.err_fd, c.master, c.slave }) |fd| if (fd >= 0) { + _ = libc.close(fd); + }; + } +}; + +/// What a run ended with: its exit code (a signal's is minus it, as +/// Python's returncode) and its stderr. +pub const Ran = struct { code: c_int, said: []u8 }; + +/// `argv` run to its end. +pub fn run(gpa: std.mem.Allocator, argv: []const [:0]const u8, opts: Child.Options) !Ran { + var c = try Child.spawn(argv, opts); + defer c.deinit(); + const words = try c.said(gpa); + var status: c_int = 0; + _ = libc.waitpid(c.pid, &status, 0); + c.status = status; + return .{ .code = code(status), .said = words }; +} + +/// A wait(2) word as an exit code, a signal's as minus it. +pub fn code(status: c_int) c_int { + const sig = status & 0x7f; + return if (sig != 0) -sig else (status >> 8) & 0xff; +} + +/// The names in directory `dir`, kept in `gpa`. +pub fn entries(gpa: std.mem.Allocator, dir: []const u8) ![]const []const u8 { + var buf: [4096]u8 = undefined; + const d = libc.opendir(try std.fmt.bufPrintSentinel(&buf, "{s}", .{dir}, 0)) orelse return error.NoDir; + defer _ = libc.closedir(d); + var names: std.ArrayList([]const u8) = .empty; + while (readdir(d)) |e| { + const name = std.mem.sliceTo(&e.name, 0); + if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) continue; + try names.append(gpa, try gpa.dupe(u8, name)); + } + return names.toOwnedSlice(gpa); +} + +/// Whether the line discipline of the terminal at `fd` is canonical. +pub fn canonical(fd: c_int) !bool { + var t: std.os.linux.termios = undefined; + if (ioctl(fd, 0x5401, &t) != 0) return error.NoTermios; // TCGETS + return t.lflag.ICANON; +} + +/// Monotonic milliseconds. +pub const nowMs = eh.nowMs; + +pub fn nap(ms: u32) void { + sleepMs(ms); +} + +pub fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +/// `ok`, or what is wrong said and the test failed. +pub fn check(ok: bool, comptime what: []const u8, args: anytype) !void { + if (ok) return; + std.debug.print(what ++ "\n", args); + return error.TestUnexpectedResult; +} + +/// Whether `exe` was built with QUIC (-Dquic=true): refused for that, or +/// for --attach's own reason, which comes after. +pub fn quicIncluded(gpa: std.mem.Allocator, exe: [:0]const u8) !bool { + const r = try run(gpa, &.{ exe, "--attach=work", "--9p-quic=quic!127.0.0.1!0" }, .{}); + defer gpa.free(r.said); + return !has(r.said, "QUIC is not included"); +} diff --git a/test/process/builtins.zig b/test/process/builtins.zig new file mode 100644 index 00000000..39a043c0 --- /dev/null +++ b/test/process/builtins.zig @@ -0,0 +1,52 @@ +//! docs/typ/reference.typ, Builtins a real session runs, detached +//! and in a pty. +const std = @import("std"); +const process = @import("../process.zig"); +const Conn = process.Conn; +const has = process.has; +const check = process.check; + +pub const cases = .{ + .{ "builtins: Lspinfo opens one +Lsp pane saying the backend and its servers, detached and in a pty", lspinfo }, +}; + +fn lspinfo(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + for ([_]bool{ false, true }) |tty| { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const name = if (tty) "lsp-tty" else "lsp-detached"; + var d = try process.Detached.start(gpa, exe, if (tty) "lsp-root" else name, .{}); + defer d.deinit(); + var term: ?process.Child = null; + defer if (term) |*t| t.deinit(); + if (tty) term = try d.startTty(exe, name, &.{}); + const address = d.abs(try std.fmt.allocPrint(a, "pardes-9p-{s}.sock", .{name})); + const c = try Conn.dial(gpa, address); + defer c.deinit(gpa); + const control = try c.newPane(a); + try c.exec(control, "Lspinfo\n"); + const deadline = process.nowMs() + 3_000; + const lsp = while (true) { + var found: ?u32 = null; + var count: usize = 0; + var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n'); + while (rows.next()) |row| if (has(row, "+Lsp")) { + count += 1; + found = try std.fmt.parseInt(u32, row[0..std.mem.indexOfScalar(u8, row, ' ').?], 10); + }; + if (found) |n| { + try check(count == 1, "{s}: {d} +Lsp panes", .{ name, count }); + break n; + } + if (process.nowMs() > deadline) return error.LspinfoNeverDone; + process.nap(5); + }; + const body = try c.pane(a, lsp, "body"); + try check(std.mem.startsWith(u8, body, "backend: lsp-client\n") and has(body, "protocol servers (lsp-client):"), "{s}", .{body}); + try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/body")); + var f: process.Files = .init(gpa, address); + defer f.deinit(); + try check(has(try f.screen(), "+Lsp"), "+Lsp not drawn", .{}); + } +} diff --git a/test/process/flags.zig b/test/process/flags.zig new file mode 100644 index 00000000..2ae8d0c7 --- /dev/null +++ b/test/process/flags.zig @@ -0,0 +1,778 @@ +//! docs/typ/reference.typ, Flags : a real pardes launched with +//! them, inside a session and out (test/fs.py's inline checks). +const std = @import("std"); +const libc = std.c; +const process = @import("../process.zig"); +const Conn = process.Conn; +const Child = process.Child; + +pub const cases = .{ + .{ "flags: a bad option, socket name, dial or mount is refused in words, exit 1", refusals }, + .{ "flags: --9p= names the socket, not --detach's name", explicitName }, + .{ "flags: a launch inside a session forwards its file there, refuses what it cannot, and one with flags of a session's own starts its own", forwarding }, + .{ "flags: --wait lasts until its pane goes; a forwarded name is opened, made or refused in words; pardes - pages stdin into a +Pager", waitAndPager }, + .{ "flags: a command from a pane's tag gets $winid, one from the workspace's tag none", winid }, + .{ "flags: pardes - outside a session boots on stdin's +Pager; a launch with no terminal to draw on says so", outside }, + .{ "flags: a tty editor killed by SIGTERM or SIGHUP puts its terminal back and its socket goes; a dead editor's socket is swept", killedTty }, + .{ "flags: a detached session told to stop by a signal unlinks its socket; a hangup does not stop it", signalCleansUp }, +}; + +extern "c" fn realpath(path: [*:0]const u8, resolved: [*]u8) ?[*:0]const u8; + +const long_name: [:0]const u8 = blk: { + var name: [304:0]u8 = @splat('x'); + @memcpy(name[300..], ".txt"); + const done = name; + break :blk &done; +}; + +const has = process.has; +const check = process.check; + +fn z(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) [:0]const u8 { + return std.fmt.allocPrintSentinel(a, fmt, args, 0) catch @panic("out of memory"); +} + +fn contains(set: []const u32, n: u32) bool { + return std.mem.indexOfScalar(u32, set, n) != null; +} + +/// The serials in `after` not in `before`. +fn added(a: std.mem.Allocator, before: []const u32, after: []const u32) []const u32 { + var out: std.ArrayList(u32) = .empty; + for (after) |n| if (!contains(before, n)) out.append(a, n) catch @panic("out of memory"); + return out.items; +} + +/// The one pane made since `before`. +fn madeOne(a: std.mem.Allocator, c: *Conn, before: []const u32) !u32 { + const new = added(a, before, try c.serials(a)); + try check(new.len == 1, "made {any}, not one pane", .{new}); + return new[0]; +} + +fn same(a: []const u32, b: []const u32) bool { + return std.mem.eql(u32, a, b); +} + +/// The session's pid, from /status. +fn statusPid(a: std.mem.Allocator, c: *Conn) ![:0]const u8 { + var lines = std.mem.tokenizeScalar(u8, try c.read(a, "/status"), '\n'); + while (lines.next()) |line| if (std.mem.startsWith(u8, line, "pid ")) return z(a, "{s}", .{line[4..]}); + return error.NoPid; +} + +/// Waits up to `ms` for the session's log to hold `text`. +fn waitLog(a: std.mem.Allocator, c: *Conn, text: []const u8, ms: i64) !void { + const deadline = process.nowMs() + ms; + while (!has(try c.read(a, "/log"), text)) { + if (process.nowMs() > deadline) { + std.debug.print("not in the log: {s}\n", .{text}); + return error.Timeout; + } + process.nap(20); + } +} + +fn isSocketName(name: []const u8) bool { + return std.mem.startsWith(u8, name, "pardes-9p-") and std.mem.endsWith(u8, name, ".sock"); +} + +/// The socket a launched session made in `root`, none of `existing`: one +/// named by its pid, as fs.py's glob found it. +fn newSocket(a: std.mem.Allocator, root: []const u8, existing: []const []const u8, child: *Child) ![]const u8 { + const deadline = process.nowMs() + 10_000; + while (true) { + for (try process.entries(a, root)) |name| { + if (!isSocketName(name)) continue; + for (existing) |old| { + if (std.mem.eql(u8, old, name)) break; + } else return std.fmt.allocPrint(a, "{s}/{s}", .{ root, name }); + } + if (child.wait(0)) |status| { + std.debug.print("exited {d} before its socket: {s}\n", .{ status, child.said(a) catch "" }); + return error.Exited; + } + if (process.nowMs() > deadline) return error.NoSocket; + process.nap(5); + } +} + +fn refusals(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const Row = struct { []const [:0]const u8, []const u8 }; + for ([_]Row{ + .{ &.{"--fs"}, "no such option" }, + .{ &.{"--fs9"}, "no such option" }, + .{ &.{ "--", "a", "b" }, "is the second" }, + .{ &.{"--9p"}, "--9p needs a socket name" }, + .{ &.{"--9p="}, "invalid 9P socket name" }, + .{ &.{"--9p=a/b"}, "invalid 9P socket name" }, + .{ &.{ "--attach=work", "--9p=work" }, "--attach has none" }, + .{ &.{"--9p-tcp"}, "--9p-tcp needs" }, + .{ &.{"--9p-tcp="}, "--9p-tcp needs" }, + .{ &.{"--9p-tcp=tcp!localhost!5640"}, "--9p-tcp needs" }, + .{ &.{"--9p-tcp=quic!127.0.0.1!5640"}, "--9p-tcp needs" }, + .{ &.{ "--9p-tcp=tcp!127.0.0.1!5640", "--9p-tcp=tcp!127.0.0.1!5641" }, "specified twice" }, + .{ &.{ "--attach=work", "--9p-tcp=tcp!127.0.0.1!5640" }, "--attach has none" }, + .{ &.{"--9p-quic"}, "--9p-quic needs" }, + .{ &.{"--9p-quic="}, "--9p-quic needs" }, + .{ &.{"--9p-quic=tcp!127.0.0.1!5640"}, "--9p-quic needs" }, + .{ &.{"--mount=os=work"}, "invalid mount name" }, + .{ &.{"--mount=peer="}, "invalid mount name or dial" }, + .{ &.{ "--mount=peer=one", "--mount=peer=two" }, "duplicate mount" }, + .{ &.{"--mount=peer=tcp!127.0.0.1!0"}, "invalid mount dial" }, + .{ &.{ "--attach=work", "--mount=peer=one" }, "--attach has none" }, + }) |row| { + var argv: [4][:0]const u8 = undefined; + argv[0] = exe; + for (row[0], 1..) |arg, i| argv[i] = arg; + const r = try process.run(a, argv[0 .. 1 + row[0].len], .{}); + try check(r.code == 1 and has(r.said, row[1]), "{any}: exit {d}: {s}", .{ row[0], r.code, r.said }); + } +} + +fn explicitName(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var d = try process.Detached.start(gpa, exe, "renamed", .{ .args = &.{"--9p=explicit"}, .socket = "explicit" }); + defer d.deinit(); + try std.testing.expect(d.exists("pardes-9p-explicit.sock")); + try std.testing.expect(!d.exists("pardes-9p-renamed.sock")); + try d.files.expect("/pane/1/body", "initial\n"); +} + +fn forwarding(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "forwarding", .{}); + defer d.deinit(); + const c = try Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const address = d.abs("pardes-9p-forwarding.sock"); + _ = libc.mkdir(d.abs("child-working-directory"), 0o755); + const child_dir = d.abs("child-working-directory"); + const spaced_text = "first line\nsecond line\n"; + try d.put("child-working-directory/space name.txt", spaced_text); + const spaced = d.abs("child-working-directory/space name.txt"); + // PARDES_PID says "you are inside a pardes"; the socket and the pane + // only say how to reach it. + const pid = try statusPid(a, c); + const inherited = [_][2][:0]const u8{ .{ "PARDES_9P", address }, .{ "PARDES_PANE", "1" }, .{ "PARDES_PID", pid } }; + const opts: Child.Options = .{ .root = d.root(), .cwd = child_dir, .env = &inherited }; + var opened: u32 = 0; + const Word = struct { [:0]const u8, []const u8, ?[2]u32, bool }; + for ([_]Word{ + .{ "space name.txt:2:4", spaced_text, .{ 14, 14 }, false }, + .{ "/n/self/pane/1/body", "initial\n", null, false }, + .{ "/virtual/pane/1/body:1:2-4", "initial\n", .{ 1, 4 }, true }, + }) |row| { + const before = try c.serials(a); + const r = try process.run(a, &.{ exe, "--tty", row[0] }, opts); + try check(r.code == 0, "{s}: exit {d}: {s}", .{ row[0], r.code, r.said }); + if (row[3]) { + try check(same(before, try c.serials(a)), "{s} made a pane", .{row[0]}); + } else opened = try madeOne(a, c, before); + try std.testing.expectEqualStrings(row[1], try c.pane(a, opened, "body")); + try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/body")); + // The selection is dot; reading it takes no command at all. + if (row[2]) |sel| { + var it = std.mem.tokenizeScalar(u8, try c.pane(a, opened, "dot"), ' '); + const got = [2]u32{ try std.fmt.parseInt(u32, it.next().?, 10), try std.fmt.parseInt(u32, std.mem.trim(u8, it.next().?, " \n"), 10) }; + try check(std.mem.eql(u32, &got, &sel), "{s}: dot {any}, not {any}", .{ row[0], got, sel }); + } + } + // A launch with no file inside a session is refused, and never clears + // the address limit its parent's pane holds. + const addr = try c.hold("/pane/1/addr", 2); + const limit = try c.hold("/pane/1/limit", 2); + _ = try c.writeFid(addr, 0, "#0,#3"); + _ = try c.writeFid(limit, 0, "#0,#3"); + _ = try c.writeFid(addr, 0, "#0"); + for (0..8) |_| { + const r = try process.run(a, &.{exe}, opts); + try check(r.code == 1 and has(r.said, "already inside pardes"), "no-argument child: exit {d}: {s}", .{ r.code, r.said }); + if (c.writeFid(addr, 0, "/tial/")) |_| { + return error.LimitCleared; + } else |_| {} + } + c.clunk(addr); + c.clunk(limit); + _ = try c.read(a, "/pane/1/ctl"); + + const control = try c.newPane(a); + try c.exec(control, "Dump\n"); + const deadline = process.nowMs() + 3_000; + while (!d.exists("forwarding.dump.zon")) { + if (process.nowMs() > deadline) return error.NoDump; + process.nap(5); + } + const saved = d.abs("forwarding.dump.zon"); + const before = try c.read(a, "/index"); + // Flags of a session's own start one, never forwarded. + const Launch = struct { []const u8, []const [:0]const u8, ?[]const u8 }; + for ([_]Launch{ + .{ "mount", &.{ "--tty", z(a, "--mount=peer={s}", .{address}), "/n/peer/pane/1/body" }, null }, + .{ "name", &.{ "--tty", "--9p=forwarded-name" }, "forwarded-name" }, + .{ "tcp", &.{ "--tty", "--9p-tcp=tcp!127.0.0.1!0" }, null }, + .{ "load", &.{ "--tty", "-l", saved }, null }, + .{ "shells", &.{ "-n", "3" }, null }, + .{ "toggle", &.{ "--tty", "--tty-toggle=q" }, null }, + .{ "toggle-arg", &.{ "--tty", "--tty-toggle", "q" }, null }, + .{ "quic", &.{ "--tty", "--9p-quic=quic!127.0.0.1!0" }, null }, + }) |row| { + if (std.mem.eql(u8, row[0], "quic") and !try process.quicIncluded(a, exe)) continue; + var argv: [8][:0]const u8 = undefined; + argv[0] = exe; + for (row[1], 1..) |arg, i| argv[i] = arg; + const existing = try process.entries(a, d.root()); + var child = try Child.spawn(argv[0 .. 1 + row[1].len], .{ .root = d.root(), .env = &inherited, .pty = .{ .log = d.abs(try std.fmt.allocPrint(a, "forward-{s}.pty", .{row[0]})) } }); + defer child.deinit(); + const sock = if (row[2]) |name| blk: { + const p = try std.fmt.allocPrint(a, "{s}/pardes-9p-{s}.sock", .{ d.root(), name }); + try child.waitPath(p, 10_000); + break :blk p; + } else try newSocket(a, d.root(), existing, &child); + try check(!std.mem.eql(u8, sock, address), "{s} forwarded", .{row[0]}); + var local: process.Files = .init(gpa, sock); + defer local.deinit(); + const index = try local.read("/index"); + const rows = std.mem.count(u8, index, "\n"); + if (std.mem.eql(u8, row[0], "mount")) { + try check(rows == 1 and has(index, "/n/peer/"), "mount: {s}", .{index}); + try local.expect("/pane/1/body", "initial\n"); + } else if (std.mem.eql(u8, row[0], "load")) { + try std.testing.expectEqualStrings(before, index); + try local.expect("/pane/1/body", "initial\n"); + } else { + // A bare tty launch is a shell plus the empty text pane the + // boot puts under it; --shells=3 is three shells, no scratch. + try check(rows == @as(usize, if (std.mem.eql(u8, row[0], "shells")) 3 else 2), "{s}: {s}", .{ row[0], index }); + } + if (std.mem.eql(u8, row[0], "tcp") or std.mem.eql(u8, row[0], "quic")) + try check(has(try local.read("/listeners"), try std.fmt.allocPrint(a, "{s}!127.0.0.1!", .{row[0]})), "{s}: no listener", .{row[0]}); + try std.testing.expectEqualStrings(before, try c.read(a, "/index")); + try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/body")); + } + + // A session that answers takes the file or says why, as acme's B does: + // a pane it has not is its refusal and exit 1, never a nested editor. + const gone = try process.run(a, &.{ exe, "--tty", spaced }, .{ .root = d.root(), .env = &.{ inherited[0], .{ "PARDES_PANE", "4294967295" }, inherited[2] } }); + try check(gone.code == 1 and std.mem.startsWith(u8, gone.said, "pardes: "), "gone pane: exit {d}: {s}", .{ gone.code, gone.said }); + try std.testing.expectEqualStrings(before, try c.read(a, "/index")); + const absent = d.abs("absent.sock"); + const stale = [_][2][:0]const u8{ .{ "PARDES_9P", absent }, inherited[1], inherited[2] }; + const Stale = struct { []const u8, []const [2][:0]const u8, ?[:0]const u8 }; + for ([_]Stale{ + .{ "stale-file", &stale, spaced }, + .{ "stale-missing", &stale, "missing-child-file.txt" }, + .{ "stale-noarg", &stale, null }, + // No pid is "not inside a pardes at all", as a plain shell has. + .{ "not-nested", inherited[0..2], spaced }, + }) |row| { + const existing = try process.entries(a, d.root()); + var argv: [3][:0]const u8 = .{ exe, "--tty", undefined }; + if (row[2]) |w| argv[2] = w; + var child = try Child.spawn(argv[0..if (row[2] != null) 3 else 2], .{ .root = d.root(), .env = row[1], .pty = .{ .log = d.abs(try std.fmt.allocPrint(a, "{s}.pty", .{row[0]})) } }); + defer child.deinit(); + const sock = try newSocket(a, d.root(), existing, &child); + try check(!std.mem.eql(u8, sock, address), "{s} forwarded", .{row[0]}); + var local: process.Files = .init(gpa, sock); + defer local.deinit(); + if (row[2]) |w| { + if (std.mem.eql(u8, w, spaced)) { + try local.expect("/pane/1/body", spaced_text); + } else try check(has(try local.read("/index"), w) or has(try local.screen(), w), "{s}: {s} not shown", .{ row[0], w }); + } + try std.testing.expectEqualStrings(before, try c.read(a, "/index")); + } +} + +/// `pardes ` started, back once it has made a pane or exited. +fn launch(a: std.mem.Allocator, c: *Conn, argv: []const [:0]const u8, opts: Child.Options) !Child { + const before = try c.serials(a); + var child = try Child.spawn(argv, opts); + const deadline = process.nowMs() + 3_000; + while (child.wait(0) == null and same(before, try c.serials(a)) and process.nowMs() < deadline) process.nap(10); + return child; +} + +fn running(a: std.mem.Allocator, child: *Child) !void { + if (child.wait(0)) |status| { + std.debug.print("exited {d}: {s}\n", .{ status, child.said(a) catch "" }); + return error.Exited; + } +} + +fn exitsWith(child: *Child, want: c_int, ms: i64) !void { + const status = child.wait(ms) orelse return error.StillRunning; + try std.testing.expectEqual(want, process.code(status)); +} + +fn waitAndPager(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "wait", .{}); + var ended = false; + defer if (!ended) d.deinit(); + const c = try Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const root = d.root(); + // A second --detach of a name a live session holds: said, exit 1, no + // Zig error trace. + const second_detach = try process.run(a, &.{ exe, "--detach=wait" }, .{ .root = root }); + try std.testing.expectEqual(@as(c_int, 1), second_detach.code); + try std.testing.expectEqualStrings("pardes: a session named wait is already running\n", second_detach.said); + const address = d.abs("pardes-9p-wait.sock"); + const env = [_][2][:0]const u8{ .{ "PARDES_9P", address }, .{ "PARDES_PANE", "1" }, .{ "PARDES_PID", try statusPid(a, c) } }; + const opts: Child.Options = .{ .root = root, .env = &env }; + try d.put("wait edit.txt", "edit me\n"); + const edited = d.abs("wait edit.txt"); + + // --wait (acme's E): a launch inside a pane lasts until the pane it + // opened is deleted; one on a file already open waits on that pane. + var first = try launch(a, c, &.{ exe, "--wait", edited }, opts); + defer first.deinit(); + const opened = blk: { + const now = try c.serials(a); + try check(now.len == 2 and now[0] == 1, "--wait: {any}", .{now}); + break :blk now[1]; + }; + var second = try launch(a, c, &.{ exe, "-w", edited }, opts); + defer second.deinit(); + process.nap(500); + try running(a, &first); + try running(a, &second); + try check(same(try c.serials(a), &.{ 1, opened }), "-w made a pane", .{}); + try c.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), "edited\n"); + try c.exec(opened, "Save\n"); + try c.removePane(opened); + const deleted = process.nowMs(); + try exitsWith(&first, 0, 5_000); + // Woken by the `del` record, not a poll. + try check(process.nowMs() - deleted < 50, "--wait woke {d} ms after the del", .{process.nowMs() - deleted}); + try exitsWith(&second, 0, 5_000); + try std.testing.expectEqualStrings("edited\n", try d.get(a, "wait edit.txt")); + + // --wait from outside the session's panes, $PARDES_9P alone (an + // agent's EDITOR): asked of the session, not a screen of its own. + const outside_env = env[0..1]; + try d.put("wait far.txt", "far\n"); + const far = d.abs("wait far.txt"); + { + const before = try c.serials(a); + var waiting = try Child.spawn(&.{ exe, "--wait", far }, .{ .root = root, .env = outside_env }); + defer waiting.deinit(); + const deadline = process.nowMs() + 5_000; + while (same(before, try c.serials(a)) and process.nowMs() < deadline) process.nap(10); + const far_pane = try madeOne(a, c, before); + process.nap(300); + try running(a, &waiting); + try c.removePane(far_pane); + try exitsWith(&waiting, 0, 5_000); + } + // --wait with a $PARDES_9P that answers no more: said, exit 1. + const dead = try process.run(a, &.{ exe, "--wait", far }, .{ .root = root, .env = &.{.{ "PARDES_9P", d.abs("pardes-9p-gone.sock") }} }); + try check(dead.code == 1 and has(dead.said, "does not answer"), "dead: exit {d}: {s}", .{ dead.code, dead.said }); + // --wait with no FILE says it needs one, and waits on nothing. + const bare = try process.run(a, &.{ exe, "--wait" }, opts); + try check(bare.code == 1 and has(bare.said, "--wait needs a FILE"), "bare: exit {d}: {s}", .{ bare.code, bare.said }); + // `--` ends the options: a name starting with a dash is a name. + try d.put("-dash.txt", "dash\n"); + { + const before = try c.serials(a); + const r = try process.run(a, &.{ exe, "--", "-dash.txt" }, opts); + try check(r.code == 0, "--: exit {d}: {s}", .{ r.code, r.said }); + const dashed = try madeOne(a, c, before); + try std.testing.expectEqualStrings(d.abs("-dash.txt"), std.mem.trimEnd(u8, try c.pane(a, dashed, "name"), "\n")); + try c.removePane(dashed); + } + // A name under a directory that may not be searched is refused, + // permission denied, exit 1, no pane. (Root searches any directory.) + if (libc.geteuid() != 0) { + _ = libc.mkdir(d.abs("noperm"), 0); + const before = try c.serials(a); + const denied = process.run(a, &.{ exe, "noperm/a/b/c" }, opts); + _ = libc.chmod(d.abs("noperm"), 0o755); + const r = try denied; + try check(r.code == 1 and has(r.said, "permission denied"), "noperm: exit {d}: {s}", .{ r.code, r.said }); + try check(same(before, try c.serials(a)), "noperm made a pane", .{}); + } + // --wait waits on the pane its look answered: a name /index shows + // escaped (a backslash is `\\` there) is still the pane waited on. + try d.put("wait\\odd.txt", "odd\n"); + { + const before = try c.serials(a); + var waiting = try launch(a, c, &.{ exe, "--wait", d.abs("wait\\odd.txt") }, opts); + defer waiting.deinit(); + const odd_pane = try madeOne(a, c, before); + process.nap(500); + try running(a, &waiting); + try c.removePane(odd_pane); + try exitsWith(&waiting, 0, 5_000); + } + // A name not there yet opens a pane named it, as acme's B does, and + // Save creates it: git's and fish's temporary files are. + { + const before = try c.serials(a); + var made = try Child.spawn(&.{ exe, "--wait", "wait new.txt" }, opts); + defer made.deinit(); + // The launch makes the pane, then names it: wait for the name. + const deadline = process.nowMs() + 3_000; + while (!has(try c.read(a, "/index"), d.abs("wait new.txt"))) { + try running(a, &made); + if (process.nowMs() > deadline) return error.NoPaneNamed; + process.nap(10); + } + const named = try madeOne(a, c, before); + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{named}), "new\n"); + try c.exec(named, "Save\n"); + try c.removePane(named); + try exitsWith(&made, 0, 5_000); + try std.testing.expectEqualStrings("new\n", try d.get(a, "wait new.txt")); + } + // ...in a directory not there either: forwarded all the same, and Save + // makes the directories. + { + const before = try c.serials(a); + const r = try process.run(a, &.{ exe, "wait-missing/sub/new.txt" }, opts); + try check(r.code == 0, "deep: exit {d}: {s}", .{ r.code, r.said }); + const deadline = process.nowMs() + 3_000; + while (!has(try c.read(a, "/index"), d.abs("wait-missing/sub/new.txt"))) { + if (process.nowMs() > deadline) return error.NoPaneNamed; + process.nap(10); + } + const named = try madeOne(a, c, before); + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{named}), "deep\n"); + try c.exec(named, "Save\n"); + try c.removePane(named); + try std.testing.expectEqualStrings("deep\n", try d.get(a, "wait-missing/sub/new.txt")); + } + // `pardes '~/x'`, quoted so the shell left the `~`: the launching + // shell's HOME all the same. + { + const before = try c.serials(a); + const r = try process.run(a, &.{ exe, "~/fwd-tilde.txt" }, opts); + try check(r.code == 0, "tilde: exit {d}: {s}", .{ r.code, r.said }); + try check(has(try c.read(a, "/index"), d.abs("fwd-tilde.txt")), "no ~ pane", .{}); + for (added(a, before, try c.serials(a))) |n| try c.removePane(n); + } + // A name the session refuses: its reason and exit 1, no pane left. + const before = try c.serials(a); + { + const r = try process.run(a, &.{ exe, long_name }, opts); + try check(r.code == 1 and has(r.said, "a component over 255 bytes"), "long: exit {d}: {s}", .{ r.code, r.said }); + try check(same(before, try c.serials(a)), "a long name made a pane", .{}); + } + // `pardes -` (a terminal's PAGER): stdin, its escapes stripped, in a + // +Pager pane of the session, clean, and back at once. + const pager_name = try std.fmt.allocPrint(a, "{s}/+Pager\n", .{root}); + const made_now = blk: { + const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "\x1b[1mbold\x1b[0m text\n" }); + try check(r.code == 0, "pager: exit {d}: {s}", .{ r.code, r.said }); + break :blk try madeOne(a, c, before); + }; + try std.testing.expectEqualStrings(pager_name, try c.pane(a, made_now, "name")); + try std.testing.expectEqualStrings("bold text\n", try c.pane(a, made_now, "body")); + { + var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n'); + const row = while (rows.next()) |r| { + if (std.mem.startsWith(u8, r, try std.fmt.allocPrint(a, "{d} ", .{made_now}))) break r; + } else return error.NotListed; + var words = std.mem.tokenizeScalar(u8, row, ' '); + _ = words.next(); + _ = words.next(); + try check(std.mem.eql(u8, words.next().?, "0"), "a +Pager is dirty: {s}", .{row}); // clean: nothing to ask at Exit + // Its `new` record names it as it is: no +New and a rename. + const log = try std.fmt.allocPrint(a, "\n{s}", .{try c.read(a, "/log")}); + try check(has(log, try std.fmt.allocPrint(a, "\nnew {d} {s}/+Pager\n", .{ made_now, root })), "no new record: {s}", .{log}); + try check(!has(log, try std.fmt.allocPrint(a, "\nrename {d} ", .{made_now})), "a +Pager renamed", .{}); + } + // Its colours are the pane's, drawn and never read: mapped through the + // theme, as a terminal's are; Filter shows them as sent. Each run is its + // own +Pager; one there already keeps its page. + const coloured = blk: { + const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "\x1b]0;title\x07\x1b[31mred\x1b[m \x1b[38;2;1;2;3mrgb\x1b[m plain\n" }); + try check(r.code == 0, "colours: exit {d}: {s}", .{ r.code, r.said }); + const new = added(a, before, try c.serials(a)); + try check(new.len == 2 and new[0] == made_now, "colours: {any}", .{new}); + break :blk new[1]; + }; + try std.testing.expectEqualStrings(pager_name, try c.pane(a, coloured, "name")); + try std.testing.expectEqualStrings("red rgb plain\n", try c.pane(a, coloured, "body")); + try std.testing.expectEqualStrings("bold text\n", try c.pane(a, made_now, "body")); + const rgb = "{\"rgb\":[1,2,3]}"; + { + const ink = try inks(&d.files); + try check(!std.mem.eql(u8, ink[0], ink[2]) and !std.mem.eql(u8, ink[1], rgb), "drawn inks {s} {s} {s}", .{ ink[0], ink[1], ink[2] }); + } + try c.exec(coloured, "Filter\n"); + { + const ink = try inks(&d.files); + try std.testing.expectEqualStrings(rgb, ink[1]); + try check(!std.mem.eql(u8, ink[0], ink[2]) and !std.mem.eql(u8, ink[0], ink[1]), "filtered inks {s} {s} {s}", .{ ink[0], ink[1], ink[2] }); + } + // A progress line keeps its last state. + const again = blk: { + const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "again\r\nprogress 10%\rprogress 100%\n" }); + try check(r.code == 0, "progress: exit {d}: {s}", .{ r.code, r.said }); + const new = added(a, before, try c.serials(a)); + try check(new.len == 3, "progress: {any}", .{new}); + break :blk new[2]; + }; + try std.testing.expectEqualStrings("again\nprogress 100%\n", try c.pane(a, again, "body")); + for ([_]u32{ made_now, coloured, again }) |n| try c.removePane(n); + // More than one 8 MiB write's worth arrives whole, byte for byte. + { + var big: std.ArrayList(u8) = .empty; + defer big.deinit(gpa); + for (0..400_000) |i| try big.print(gpa, "line {d:0>8} of the paged text\n", .{i}); // ~11 MiB + try std.testing.expect(big.items.len > 8 * 1024 * 1024); + const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = big.items }); + try check(r.code == 0, "big: exit {d}: {s}", .{ r.code, r.said }); + const big_pane = try madeOne(a, c, before); + const got = try c.read(gpa, try std.fmt.allocPrint(a, "/pane/{d}/body", .{big_pane})); + defer gpa.free(got); + try check(std.mem.eql(u8, got, big.items), "the paged body is not the text", .{}); + // Past the 256 MiB cap: the first 256 MiB, then the cut note. + const cap = 256 * 1024 * 1024; + var exe_buf: [4096]u8 = undefined; + const huge = try process.run(a, &.{ "sh", "-c", z(a, "yes 0123456789abcdef | head -c {d} | \"$0\" -", .{cap + 4096}), std.mem.span(realpath(exe, &exe_buf) orelse return error.NoBinary) }, .{ .root = root, .env = &env }); + try check(huge.code == 0, "huge: exit {d}: {s}", .{ huge.code, huge.said }); + try c.removePane(big_pane); + } + { + const cap = 256 * 1024 * 1024; + const big_pane = try madeOne(a, c, before); + const got = try c.read(gpa, try std.fmt.allocPrint(a, "/pane/{d}/body", .{big_pane})); + defer gpa.free(got); + const note = "\n[pardes: the rest of stdin is cut: a pane holds at most a file's limit]\n"; + try check(std.mem.endsWith(u8, got, note) and got.len == cap + note.len, "huge: {d} bytes", .{got.len}); + const line = "0123456789abcdef\n"; + var i: usize = 0; + while (i < cap) : (i += line.len) { + const n = @min(line.len, cap - i); + if (!std.mem.eql(u8, got[i..][0..n], line[0..n])) return error.NotTheText; + } + try c.removePane(big_pane); + } + // From a directory whose name could not be a ctl line: the session's + // directory's +Pager, the text whole, no stray +New. + _ = libc.mkdir(d.abs("two\nlines"), 0o755); + { + const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .cwd = d.abs("two\nlines"), .env = &env, .stdin = "odd dir text\n" }); + try check(r.code == 0, "odd dir: exit {d}: {s}", .{ r.code, r.said }); + const odd_pane = try madeOne(a, c, before); + try std.testing.expectEqualStrings(pager_name, try c.pane(a, odd_pane, "name")); + try std.testing.expectEqualStrings("odd dir text\n", try c.pane(a, odd_pane, "body")); + try c.removePane(odd_pane); + } + // Nothing on stdin: no pane, and done. + { + const r = try process.run(a, &.{ exe, "-" }, .{ .root = root, .env = &env, .stdin = "" }); + try check(r.code == 0 and same(before, try c.serials(a)), "empty stdin: exit {d}: {s}", .{ r.code, r.said }); + } + // A new name in a directory there but not writable: refused now, not a + // pane that only fails at Save. (Root writes anywhere.) + _ = libc.mkdir(d.abs("shut-dir"), 0o500); + if (libc.geteuid() != 0) { + const r = try process.run(a, &.{ exe, "shut-dir/new.txt" }, opts); + try check(r.code == 1 and has(r.said, "may not be read or written"), "shut-dir: exit {d}: {s}", .{ r.code, r.said }); + try check(same(before, try c.serials(a)), "shut-dir made a pane", .{}); + } + _ = libc.chmod(d.abs("shut-dir"), 0o755); + // A pane address the session has not is refused in words, exit 1, no + // pane made for a file of that name; one it has is looked at. A name + // with a newline is said in words, not an error name. + for ([_][2][:0]const u8{ + .{ "@p999:1", "pardes: @p999:1: this session has no such pane" }, + .{ "two\nlines.txt", "pardes: two\\nlines.txt: a file name is one line" }, + }) |row| { + const r = try process.run(a, &.{ exe, row[0] }, opts); + try check(r.code == 1 and has(r.said, row[1]), "{s}: exit {d}: {s}", .{ row[0], r.code, r.said }); + try check(!has(r.said, "NotOneLine") and !has(r.said, "NoSuchPane"), "an error name: {s}", .{r.said}); + try check(same(before, try c.serials(a)), "{s} made a pane", .{row[0]}); + } + const looked = try process.run(a, &.{ exe, "@p1:1" }, opts); + try check(looked.code == 0, "@p1:1: exit {d}: {s}", .{ looked.code, looked.said }); + try check(same(before, try c.serials(a)), "@p1:1 made a pane", .{}); + // A session that goes away ends a --wait on it with 1. + var orphan = try launch(a, c, &.{ exe, "--wait", edited }, opts); + defer orphan.deinit(); + try running(a, &orphan); + d.deinit(); + ended = true; + try exitsWith(&orphan, 1, 5_000); +} + +/// The inks of `red rgb plain`'s three words, as /screen draws them. +fn inks(f: *process.Files) ![3][]const u8 { + const col, const row = try f.where("red rgb plain"); + return .{ try f.fg(col, row), try f.fg(col + 4, row), try f.fg(col + 8, row) }; +} + +fn winid(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "winid", .{}); + defer d.deinit(); + const c = try Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const first = std.mem.min(u32, try c.serials(a)); + try check(has(try commandBody(a, c, try std.fmt.allocPrint(a, "/pane/{d}/tagexec", .{first})), try std.fmt.allocPrint(a, "winid={d}\n", .{first})), "a pane's tag: no $winid", .{}); + try check(has(try commandBody(a, c, "/tagexec"), "winid=\n"), "the workspace's tag: a $winid", .{}); +} + +/// What `echo winid=$winid` written to `node` printed, its pane removed. +fn commandBody(a: std.mem.Allocator, c: *Conn, node: []const u8) ![]const u8 { + try c.write(node, "echo winid=$winid\n"); + var words = std.mem.tokenizeAny(u8, try c.read(a, node), " \n"); + var last: []const u8 = ""; + while (words.next()) |w| last = w; + const serial = try std.fmt.parseInt(u32, last, 10); + const deadline = process.nowMs() + 10_000; + while (true) { + const log = try c.read(a, "/log"); + if (std.mem.lastIndexOf(u8, log, try std.fmt.allocPrint(a, "\nrun {d} ", .{serial}))) |ran| { + if (has(log[ran..], try std.fmt.allocPrint(a, "\nexit {d} ", .{serial}))) { + const body = try c.pane(a, serial, "body"); + var parts = std.mem.splitSequence(u8, body, "% echo winid"); + var tail: []const u8 = ""; + while (parts.next()) |p| tail = p; + try c.removePane(serial); + return tail; + } + } + if (process.nowMs() > deadline) return error.NeverExited; + process.nap(50); + } +} + +fn outside(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + // A session for its root alone. + var d = try process.Detached.start(gpa, exe, "outside", .{}); + defer d.deinit(); + // `pardes -` outside a session: an editor whose first pane is a +Pager + // of stdin, as `vim -`; here a detached one, to read it. + { + var boot = try Child.spawn(&.{ exe, "--detach=stdinboot", "-" }, .{ .root = d.root(), .stdin = "\x1b[32mfrom stdin\x1b[m\nsecond\n" }); + defer boot.deinit(); + const sock = d.abs("pardes-9p-stdinboot.sock"); + try boot.waitPath(sock, 10_000); + var booted: process.Files = .init(gpa, sock); + defer booted.deinit(); + var first = std.mem.tokenizeScalar(u8, std.mem.sliceTo(try booted.read("/index"), '\n'), ' '); + const serial = first.next().?; + _ = first.next(); + _ = first.next(); + try std.testing.expectEqualStrings(d.abs("+Pager"), first.next().?); + try booted.expect(booted.at("/pane/{s}/body", .{serial}), "from stdin\nsecond\n"); + } + // A launch with no terminal to draw on (a pty that is no one's + // controlling terminal) says so and exits 1, no error trace. + var tty = try Child.spawn(&.{ exe, "--tty", "x.txt" }, .{ .root = d.root(), .pty = .{ .log = d.abs("noctty.pty"), .ctty = false } }); + defer tty.deinit(); + const words = try tty.said(a); + try exitsWith(&tty, 1, 10_000); + try check(has(words, "pardes: no terminal to draw on") and !has(words, ".zig:"), "no terminal: {s}", .{words}); +} + +/// A unix socket's file at `path`, bound and closed, as a dead editor +/// leaves one. +fn socketFile(path: []const u8) !void { + const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM, 0); + if (fd < 0) return error.NoSocket; + defer _ = libc.close(fd); + var addr: libc.sockaddr.un = .{ .path = undefined }; + @memset(&addr.path, 0); + @memcpy(addr.path[0..path.len], path); + if (libc.bind(fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) != 0) return error.NoBind; +} + +fn killedTty(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "killed", .{}); + defer d.deinit(); + // Killed with SIGTERM or SIGHUP, a tty editor puts its terminal back + // (main screen, cooked mode) before it dies, and its 9P socket goes + // with it. One a dead editor left (no process has its pid) is swept at + // the next start; a live pid's, here this test's own, never is. + for ([_]libc.SIG{ .TERM, .HUP }) |sig| { + const dead = "pardes-9p-2147483647.sock"; + const live = try std.fmt.allocPrint(a, "pardes-9p-{d}.sock", .{libc.getpid()}); + for ([_][]const u8{ dead, live }) |name| if (!d.exists(name)) try socketFile(d.abs(name)); + const existing = try process.entries(a, d.root()); + const log = d.abs(try std.fmt.allocPrint(a, "killed-{t}.pty", .{sig})); + // The daemon's, as a user's editor has: a hangup leaves the session. + var killed = try Child.spawn(&.{ exe, "--tty", "x.txt" }, .{ .root = d.root(), .env = &.{.{ "PARDES_DAEMON", "on" }}, .pty = .{ .log = log } }); + defer killed.deinit(); + const deadline = process.nowMs() + 10_000; + while (!has(d.get(a, std.fs.path.basename(log)) catch "", "\x1b[?1049h")) { + if (process.nowMs() > deadline) return error.NeverDrew; + process.nap(20); + } + process.nap(300); + // The session's own, named by its pid: forked off the launched + // process, which stays as its stub. + const own = try newSocket(a, d.root(), existing, &killed); + try check(!d.exists(dead), "a dead editor's socket kept", .{}); + try check(d.exists(live), "a live pid's socket swept", .{}); + killed.kill(sig); + try exitsWith(&killed, -@as(c_int, @intCast(@intFromEnum(sig))), 5_000); + const own_z = z(a, "{s}", .{own}); + if (sig == .HUP) { + // A hangup is the terminal's end: the session carries on + // detached (the daemon keeps it) until told to end. + try check(libc.access(own_z, 0) == 0, "a hangup ended the session", .{}); + const name = std.fs.path.basename(own); + _ = libc.kill(try std.fmt.parseInt(libc.pid_t, name["pardes-9p-".len .. name.len - ".sock".len], 10), .TERM); + } + const gone_by = process.nowMs() + 5_000; + while (libc.access(own_z, 0) == 0 and process.nowMs() < gone_by) process.nap(10); + try check(libc.access(own_z, 0) != 0, "{s} left behind", .{own}); + process.nap(200); + const seen = try d.get(a, std.fs.path.basename(log)); + const tail = seen[std.mem.lastIndexOf(u8, seen, "\x1b[?1049h").?..]; + try check(has(tail, "\x1b[?1049l"), "the main screen not put back", .{}); + try check(try process.canonical(killed.slave), "the terminal left raw", .{}); + daemonGone(&d); + } +} + +/// The daemon a session had (PARDES_DAEMON=on) outlives it a little and, +/// going, makes its runtime directory (the root) again: waited for. +fn daemonGone(d: *const process.Detached) void { + const deadline = process.nowMs() + 5_000; + while (d.exists("pardes-daemon.sock") and process.nowMs() < deadline) process.nap(20); + process.nap(100); +} + +fn signalCleansUp(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + // A detached session told to stop by a signal unlinks its sockets. A + // hangup is a terminal's end, not a session's: the daemon keeps it. + for ([_]libc.SIG{ .TERM, .INT, .HUP }) |first| { + var d = try process.Detached.start(gpa, exe, "sig", .{ .env = &.{.{ "PARDES_DAEMON", "on" }} }); + defer d.deinit(); + try std.testing.expect(d.exists("pardes-9p-sig.sock")); + var sig = first; + if (sig == .HUP) { + _ = libc.kill(d.pid, sig); + process.nap(300); + try check(d.exists("pardes-9p-sig.sock") and (try d.files.read("/index")).len > 0, "a hangup ended a kept session", .{}); + sig = .TERM; + } + _ = libc.kill(d.pid, sig); + const deadline = process.nowMs() + 5_000; + while (d.exists("pardes-9p-sig.sock") and process.nowMs() < deadline) process.nap(10); + try check(!d.exists("pardes-9p-sig.sock"), "{t}: the socket left", .{first}); + daemonGone(&d); + } +} diff --git a/test/process/look_and_exec.zig b/test/process/look_and_exec.zig new file mode 100644 index 00000000..2a0266f8 --- /dev/null +++ b/test/process/look_and_exec.zig @@ -0,0 +1,239 @@ +//! docs/typ/reference.typ, look and exec : one session +//! driven as the dogfood agents drive it (test/fs.py's workflow_canary). +const std = @import("std"); +const process = @import("../process.zig"); +const Conn = process.Conn; +const has = process.has; +const check = process.check; + +pub const cases = .{ + .{ "look-and-exec: the canary: Look, Grep and Find hit and miss, a word looked at, Edit and Save, get, pty/run, Kill, a REPL send, columns, ask and answer, Dump and Restore, in under 30 s", canary }, +}; + +const Flow = struct { + a: std.mem.Allocator, + c: *Conn, + + fn log(f: Flow) ![]const u8 { + return f.c.read(f.a, "/log"); + } + + /// Waits up to 10 s for the log to hold `text`. + fn waitLog(f: Flow, text: []const u8) !void { + const deadline = process.nowMs() + 10_000; + while (!has(try f.log(), text)) { + if (process.nowMs() > deadline) { + std.debug.print("not in the log: {s}\n", .{text}); + return error.Timeout; + } + process.nap(50); + } + } + + /// The pane an exec at `serial` made, as exec reads it back. + fn madeBy(f: Flow, serial: u32, line: []const u8) !u32 { + try f.c.exec(serial, line); + var words = std.mem.tokenizeAny(u8, try f.c.pane(f.a, serial, "exec"), " \n"); + var last: []const u8 = ""; + while (words.next()) |w| last = w; + return std.fmt.parseInt(u32, last, 10); + } + + /// A line run in terminal `term` through pty/run: its answer. + fn run(f: Flow, term: u32, line: []const u8) ![]const u8 { + const fid = try f.c.hold(try std.fmt.allocPrint(f.a, "/pane/{d}/pty/run", .{term}), 2); + defer f.c.clunk(fid); + _ = try f.c.writeFid(fid, 0, line); + return f.a.dupe(u8, try f.c.readFid(fid, 0, 65536)); + } + + /// An event record written back on pane `n`'s event, as a reader acts. + fn event(f: Flow, n: u32, record: []const u8) !void { + const fid = try f.c.hold(try std.fmt.allocPrint(f.a, "/pane/{d}/event", .{n}), 2); + defer f.c.clunk(fid); + _ = try f.c.writeFid(fid, 0, record); + } + + fn columns(f: Flow) !usize { + var n: usize = 0; + var rows = std.mem.tokenizeScalar(u8, try f.c.read(f.a, "/layout"), '\n'); + while (rows.next()) |row| n += @intFromBool(!std.mem.startsWith(u8, row, "active")); + return n; + } +}; + +fn canary(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + const started = process.nowMs(); + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "flow", .{}); + defer d.deinit(); + try d.put("a.txt", "alpha one\nshared word\n"); + try d.put("b.txt", "beta two\nshared word\n"); + try d.put("s.sh", "echo from-file\n"); + const root = d.root(); + const c = try Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const f: Flow = .{ .a = a, .c = c }; + + // Look opens files, and the log says new. + const pa = try c.look(a, d.abs("a.txt"), null); + const pb = try c.look(a, d.abs("b.txt"), null); + try std.testing.expectEqualStrings("alpha one\nshared word\n", try c.pane(a, pa, "body")); + try check(has(try f.log(), try std.fmt.allocPrint(a, "new {d} {s}/a.txt\n", .{ pa, root })) and + has(try f.log(), try std.fmt.allocPrint(a, "new {d} {s}/b.txt\n", .{ pb, root })), "no new records", .{}); + + // Grep and Find: a hit fills its query pane, line 1 the query and the + // lines under each file's name; a miss fails ENOENT. + const grep = try f.madeBy(pa, "Grep shared\n"); + try std.testing.expectEqualStrings("Grep shared\na.txt\na.txt:2:1 shared word\nb.txt\nb.txt:2:1 shared word\n", try c.pane(a, grep, "body")); + try c.removePane(grep); + try c.execRefused(pa, "Grep zzqqnomatch\n", "Grep: text not found"); + const found = try f.madeBy(pa, "Find b.txt\n"); + try check(has(try c.pane(a, found, "body"), "b.txt"), "Find found nothing", .{}); + try c.removePane(found); + try c.execRefused(pa, "Find zzqqnomatch\n", "Find: name not found"); + + // A plain word looked at is found in the pane, after its dot. + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/dot", .{pa}), " 0 0 "); + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/look", .{pa}), "shared\n"); + { + var it = std.mem.tokenizeAny(u8, try c.pane(a, pa, "dot"), " \n"); + try check(std.mem.eql(u8, it.next().?, "10") and std.mem.eql(u8, it.next().?, "16"), "the word not found after dot", .{}); + } + + // Edit in two files, then Save each: on disk, and logged. + for ([_]u32{ pa, pb }) |n| try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{n}), "Edit ,s/shared/SHARED/g\n"); + try std.testing.expectEqualStrings("beta two\nSHARED word\n", try c.pane(a, pb, "body")); + for ([_]u32{ pa, pb }) |n| try c.exec(n, "Save\n"); + try std.testing.expectEqualStrings("alpha one\nSHARED word\n", try d.get(a, "a.txt")); + try check(has(try f.log(), try std.fmt.allocPrint(a, "save {d} {s}/a.txt\n", .{ pa, root })) and + has(try f.log(), try std.fmt.allocPrint(a, "save {d} {s}/b.txt\n", .{ pb, root })), "no save records", .{}); + + // get reloads what changed on disk. + try d.put("b.txt", "from disk\n"); + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{pb}), "get\n"); + try std.testing.expectEqualStrings("from disk\n", try c.pane(a, pb, "body")); + + // A terminal: pty/run failing then passing. + const term = try f.madeBy(1, "Tty\n"); + { + const deadline = process.nowMs() + 10_000; + while (true) { + const answer = try f.run(term, "true\n"); + if (std.mem.eql(u8, answer, "exit 0\n")) break; + if (process.nowMs() > deadline) { + std.debug.print("pty/run answered {s}\n", .{answer}); + return error.Timeout; + } + process.nap(50); + } + } + try std.testing.expectEqualStrings("exit 1\n", try f.run(term, "false\n")); + try std.testing.expectEqualStrings("exit 0\npassed\n", try f.run(term, "echo passed\n")); + // (A run's answer is its record: the log has no err for these.) + try check(!has(try f.log(), try std.fmt.allocPrint(a, "err {d} pty/run", .{term})), "pty/run logged an err", .{}); + + // A command pane, and Kill: its exit is logged. + const sleeper = try f.madeBy(pa, "sleep 30\n"); + try check(has(try f.log(), try std.fmt.allocPrint(a, "run {d} sleep 30\n", .{sleeper})), "no run record", .{}); + try c.write("/ctl", "Kill sleep\n"); + try f.waitLog(try std.fmt.allocPrint(a, "\nexit {d} ", .{sleeper})); + + // A REPL send: a program in the terminal (cat, which says back what it + // is sent) bound as bash's REPL; an exec on a .sh file's body (its event + // written back) goes there. + const s = try c.look(a, d.abs("s.sh"), null); + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/pty/data", .{term}), "cat\r"); + { + const deadline = process.nowMs() + 10_000; + while (true) { + const status = std.mem.trimEnd(u8, try c.pane(a, term, "pty/status"), " \n"); + if (std.mem.endsWith(u8, status, " 1") or std.mem.eql(u8, status, "1")) break; + if (process.nowMs() > deadline) return error.ReplNeverStarted; + process.nap(50); + } + } + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{term}), "Repl bash\n"); + try f.event(s, "MX0 0 0 14 echo from-file\n"); + try f.waitLog(try std.fmt.allocPrint(a, "\nsend {d} {d} ", .{ s, term })); + { + const deadline = process.nowMs() + 10_000; + while (std.mem.count(u8, try c.pane(a, term, "body"), "echo from-file") < 2) { + if (process.nowMs() > deadline) return error.ReplNeverGotIt; + process.nap(50); + } + } + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{term}), "Repl -\n"); + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/pty/data", .{term}), "\x04"); + { + const deadline = process.nowMs() + 10_000; + while (!std.mem.eql(u8, try f.run(term, "true\n"), "exit 0\n")) { + if (process.nowMs() > deadline) return error.ShellNeverBack; + process.nap(50); + } + } + + // The layout and a column's files: Newcol, its tag, rmdir. + try check(std.mem.count(u8, try c.read(a, "/layout"), "\n") >= 2, "no column in /layout", .{}); // a column, then `active` + try c.write("/ctl", "Newcol\n"); + try std.testing.expectEqual(@as(usize, 2), try f.columns()); + const empty = blk: { + var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/layout"), '\n'); + var last: []const u8 = ""; + while (rows.next()) |row| if (!std.mem.startsWith(u8, row, "active")) { + last = row; + }; + break :blk std.mem.sliceTo(last, ' '); + }; + try check(has(try c.read(a, try std.fmt.allocPrint(a, "/col/{s}/tag", .{empty})), "New"), "a column's tag has no New", .{}); + try c.remove(try std.fmt.allocPrint(a, "/col/{s}", .{empty})); + try std.testing.expectEqual(@as(usize, 1), try f.columns()); + + // ask/answer: Save on a terminal asks for a path over 9P. + try c.exec(term, "Save\n"); + try f.waitLog(try std.fmt.allocPrint(a, "ask {d} save path\n", .{term})); + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{term}), try std.fmt.allocPrint(a, "answer {s}/scroll.txt\n", .{root})); + { + const deadline = process.nowMs() + 10_000; + while (!d.exists("scroll.txt")) { + if (process.nowMs() > deadline) return error.AnsweredSaveWroteNothing; + process.nap(50); + } + } + try check(has(try f.log(), try std.fmt.allocPrint(a, "answer {d} {s}/scroll.txt\n", .{ term, root })), "no answer record", .{}); + + // Dump, then Restore it: the new session names its panes. + try c.write("/ctl", "Dump\n"); + const saved = d.abs("flow.dump.zon"); + { + const deadline = process.nowMs() + 10_000; + while (!d.exists("flow.dump.zon")) { + if (process.nowMs() > deadline) return error.DumpWroteNothing; + process.nap(50); + } + } + try check(has(try f.log(), try std.fmt.allocPrint(a, "dump {s}\n", .{saved})), "no dump record", .{}); + c.write("/ctl", try std.fmt.allocPrint(a, "Restore {s}\n", .{saved})) catch {}; + process.nap(500); + const restored = try Conn.dial(gpa, d.socket()); + defer restored.deinit(gpa); + const r: Flow = .{ .a = a, .c = restored }; + try r.waitLog(try std.fmt.allocPrint(a, "restore {s}", .{saved})); + const names = try restored.read(a, "/index"); + try check(has(names, d.abs("a.txt")) and has(names, d.abs("b.txt")), "restored {s}", .{names}); + // Recent goes on across the Restore, and is kept after it. + try check(has(try restored.read(a, "/recent"), try std.fmt.allocPrint(a, "{s}/a.txt\n", .{root})), "Recent lost", .{}); + try d.put("after.txt", "after\n"); + _ = try restored.look(a, d.abs("after.txt"), null); + try check(has(try restored.read(a, "/recent"), try std.fmt.allocPrint(a, "open {s}/after.txt\n", .{root})), "no open in Recent", .{}); + { + const deadline = process.nowMs() + 10_000; + while (!has(d.get(a, ".local/state/pardes/store") catch "", d.abs("after.txt"))) { + if (process.nowMs() > deadline) return error.RecentNotKept; + process.nap(50); + } + } + try check(process.nowMs() - started < 30_000, "the canary took {d} ms", .{process.nowMs() - started}); +} diff --git a/test/process/mounts.zig b/test/process/mounts.zig new file mode 100644 index 00000000..7d44f4fb --- /dev/null +++ b/test/process/mounts.zig @@ -0,0 +1,212 @@ +//! docs/typ/reference.typ, Mounts : sessions mounting each other's +//! trees over their sockets, at startup and at run time. +const std = @import("std"); +const process = @import("../process.zig"); +const Conn = process.Conn; +const Detached = process.Detached; +const has = process.has; +const check = process.check; + +pub const cases = .{ + .{ "mounts: --mount at startup reads a peer's panes and its disk, a Save through it lands there, and a path not there is a miss in the log", startup }, + .{ "mounts: a session listening on TCP and QUIC too (-Dquic=true) is mounted over QUIC, saved and got through, its screen read, and unmounted, v4 and v6, detached and in a pty", quic }, + .{ "mounts: Mount and Unmount at run time, refused in words when mounted already or in use; a session's /n lists its mounts; a file through a mount opens at startup", runtime }, +}; + +/// A session whose pane `serial` holds "replaced\nevent\n", as fs.py's +/// `first` had one when its peers mounted it. +fn peer(gpa: std.mem.Allocator, exe: [:0]const u8, a: std.mem.Allocator) !struct { Detached, *Conn, u32 } { + var d = try Detached.start(gpa, exe, "first", .{}); + errdefer d.deinit(); + const c = try Conn.dial(gpa, d.socket()); + const serial = try c.newPane(a); + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{serial}), "replaced\nevent\n"); + return .{ d, c, serial }; +} + +fn startup(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var first, const client, const serial = try peer(gpa, exe, a); + defer first.deinit(); + defer client.deinit(gpa); + const address = first.abs("pardes-9p-first.sock"); + const own = first.abs("pardes-9p-second.sock"); + var second = try Detached.start(gpa, exe, "second", .{ + .root = first.root(), + .args = &.{ try std.fmt.allocPrintSentinel(a, "--mount=peer={s}", .{address}, 0), try std.fmt.allocPrintSentinel(a, "--mount=own={s}", .{own}, 0) }, + }); + defer second.deinit(); + const remote = try Conn.dial(gpa, second.socket()); + defer remote.deinit(gpa); + var opened = try remote.look(a, "/n/own/pane/1/body", null); + try std.testing.expectEqualStrings("initial\n", try remote.pane(a, opened, "body")); + opened = try remote.look(a, try std.fmt.allocPrint(a, "/n/peer/pane/{d}/body", .{serial}), null); + try std.testing.expectEqualStrings("replaced\nevent\n", try remote.pane(a, opened, "body")); + try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), "saved to peer\n"); + try remote.exec(opened, "Save\n"); + try std.testing.expectEqualStrings("saved to peer\n", try client.pane(a, serial, "body")); + + var large: std.ArrayList(u8) = .empty; + for (0..60000) |_| try large.appendSlice(a, "mounted file contents\n"); + try first.put("large.txt", large.items); + opened = try remote.look(a, try std.fmt.allocPrint(a, "/n/peer/os{s}", .{first.abs("large.txt")}), null); + try check(std.mem.eql(u8, try remote.pane(a, opened, "body"), large.items), "the mounted file is not whole", .{}); + const edited = try std.fmt.allocPrint(a, "{s}edited\n", .{large.items[0 .. large.items.len - 3]}); + try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), edited); + try remote.exec(opened, "Save\n"); + try check(std.mem.eql(u8, try first.get(a, "large.txt"), edited), "the Save through the mount is not on disk", .{}); + try check(std.mem.eql(u8, try client.read(a, try std.fmt.allocPrint(a, "/os{s}", .{first.abs("large.txt")})), edited), "the peer's /os disagrees", .{}); + // A path not there under a peer's mount is a miss, as any path to no + // file is: the write answered, the err logged. + try remote.write("/look", try std.fmt.allocPrint(a, "/n/peer/os{s}\n", .{first.abs("no-such-file.txt")})); + try check(has(try remote.read(a, "/log"), "no such file"), "no miss logged", .{}); + try client.exec(serial, "Del\n"); + for (try client.list(a, "/pane")) |n| try check(!std.mem.eql(u8, n, try std.fmt.allocPrint(a, "{d}", .{serial})), "Del left the pane", .{}); +} + +fn runtime(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var first, const client, _ = try peer(gpa, exe, a); + defer first.deinit(); + defer client.deinit(gpa); + const address = first.abs("pardes-9p-first.sock"); + { + var d = try Detached.start(gpa, exe, "runtime", .{ .root = first.root() }); + defer d.deinit(); + const remote = try Conn.dial(gpa, d.socket()); + defer remote.deinit(gpa); + const own_address = first.abs("pardes-9p-runtime.sock"); + const control = try remote.newPane(a); + try remote.exec(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{address})); + var opened = try remote.look(a, "/n/peer/pane/1/body", control); + try std.testing.expectEqualStrings(try client.read(a, "/pane/1/body"), try remote.pane(a, opened, "body")); + // A builtin that fails through exec fails the write (one rule). + try remote.execRefused(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{own_address}), "already mounted"); + try remote.execRefused(control, "Unmount peer\n", "mount in use"); // a pane is open on it + try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), "runtime mounted Save\n"); + try remote.exec(opened, "Save\n"); + try std.testing.expectEqualStrings("runtime mounted Save\n", try client.read(a, "/pane/1/body")); + try std.testing.expectEqualStrings("initial\n", try remote.read(a, "/pane/1/body")); + const roots = try remote.look(a, "/n", control); + try check(has(try remote.pane(a, roots, "body"), "/n/peer/\n"), "/n lists no peer", .{}); + try remote.exec(opened, "Del\n"); + try remote.exec(control, "Unmount peer\n"); + try remote.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{roots}), "get\n"); + try std.testing.expectEqualStrings("/n/os/\n/n/self/\n", try remote.pane(a, roots, "body")); + try remote.exec(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{own_address})); + opened = try remote.look(a, "/n/peer/pane/1/body", control); + try std.testing.expectEqualStrings("initial\n", try remote.pane(a, opened, "body")); + } + var d = try Detached.start(gpa, exe, "mounted-startup", .{ + .root = first.root(), + .args = &.{try std.fmt.allocPrintSentinel(a, "--mount=peer={s}", .{address}, 0)}, + .file = "/n/peer/pane/1/body:1", + }); + defer d.deinit(); + try d.files.expect("/pane/1/body", "runtime mounted Save\n"); +} + +/// The port of /listeners' `!!` line. +fn port(listeners: []const u8, net: []const u8) !u16 { + var rows = std.mem.tokenizeScalar(u8, listeners, '\n'); + while (rows.next()) |row| { + var parts = std.mem.splitScalar(u8, row, '!'); + if (!std.mem.eql(u8, parts.first(), net)) continue; + _ = parts.next() orelse return error.BadListener; + const p = try std.fmt.parseInt(u16, parts.next() orelse return error.BadListener, 10); + try check(parts.next() == null and p > 0, "listener {s}", .{row}); + return p; + } + return error.NoListener; +} + +fn quic(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + if (!try process.quicIncluded(gpa, exe)) { + std.debug.print("SKIP a build without QUIC (-Dquic=true)\n", .{}); + return; + } + for ([_]bool{ false, true }) |tty| for ([_][:0]const u8{ "127.0.0.1", "::1" }) |host| { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const args = [_][:0]const u8{ + try std.fmt.allocPrintSentinel(a, "--9p-tcp=tcp!{s}!0", .{host}, 0), + try std.fmt.allocPrintSentinel(a, "--9p-quic=quic!{s}!0", .{host}, 0), + }; + var d = try Detached.start(gpa, exe, if (tty) "quic-root" else "server", .{ .args = if (tty) &.{} else &args }); + defer d.deinit(); + var term: ?process.Child = null; + defer if (term) |*t| t.deinit(); + if (tty) term = try d.startTty(exe, "server", &args); + const address = d.abs("pardes-9p-server.sock"); + const unix = try Conn.dial(gpa, address); + defer unix.deinit(gpa); + const listeners = try unix.read(a, "/listeners"); + const tcp_port = try port(listeners, "tcp"); + const quic_port = try port(listeners, "quic"); + try std.testing.expectEqual(@as(usize, 3), std.mem.count(u8, listeners, "\n")); + for ([_][]const u8{ + try std.fmt.allocPrint(a, "unix!{s}\n", .{address}), + try std.fmt.allocPrint(a, "tcp!{s}!{d}\n", .{ host, tcp_port }), + try std.fmt.allocPrint(a, "quic!{s}!{d}\n", .{ host, quic_port }), + }) |row| try check(has(listeners, row), "no {s} in {s}", .{ row, listeners }); + const tcp = try Conn.dialTcp(gpa, host, tcp_port); + defer tcp.deinit(gpa); + const unix_body = try unix.hold("/pane/1/body", 0); + defer unix.clunk(unix_body); + const tcp_body = try tcp.hold("/pane/1/body", 0); + defer tcp.clunk(tcp_body); + var client = try Detached.start(gpa, exe, "client", .{ .root = d.root() }); + defer client.deinit(); + const remote = try Conn.dial(gpa, client.socket()); + defer remote.deinit(gpa); + const control = try remote.newPane(a); + try remote.exec(control, try std.fmt.allocPrint(a, "Mount peer quic!{s}!{d}\n", .{ host, quic_port })); + const opened = try remote.look(a, "/n/peer/pane/1/body", control); + const body = try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}); + try std.testing.expectEqualStrings("initial\n", try remote.read(a, body)); + var payload: std.ArrayList(u8) = .empty; + try payload.appendSlice(a, "saved café λ\n"); + try payload.appendNTimes(a, 'x', 3 * 8192 + 27); + try payload.append(a, '\n'); + try remote.replace(body, payload.items); + try remote.exec(opened, "Save\n"); + try std.testing.expectEqualStrings(payload.items[0..@min(payload.items.len, unix.cl.maxRead())], try unix.readFid(unix_body, 0, 65536)); + try check(std.mem.eql(u8, try tcp.read(a, "/pane/1/body"), payload.items), "TCP reads another body", .{}); + try tcp.replace("/pane/1/body", "TCP to QUIC Get\n"); + try remote.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{opened}), "get\n"); + try std.testing.expectEqualStrings("TCP to QUIC Get\n", try remote.read(a, body)); + try std.testing.expectEqualStrings("TCP to QUIC Get\n", try unix.readFid(unix_body, 0, 65536)); + try std.testing.expectEqualStrings("TCP to QUIC Get\n", try tcp.readFid(tcp_body, 0, 65536)); + // The peer's own screen, whole, over QUIC: its pane's tag, not its + // text, whose place after the body shrank depends on a frame drawn. + const screen_pane = try remote.look(a, "/n/peer/screen", control); + const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try remote.pane(a, screen_pane, "body"), .{}); + const cells = json.object.get("cells").?.array.items; + try std.testing.expectEqual(@as(usize, @intCast(json.object.get("cols").?.integer * json.object.get("rows").?.integer)), cells.len); + var text: std.ArrayList(u8) = .empty; + for (cells) |cell| try text.appendSlice(a, cell.array.items[0].string); + try check(has(text.items, "server.txt Save"), "the peer's screen has no tag", .{}); + var large: std.ArrayList(u8) = .empty; + for (0..20000) |_| try large.appendSlice(a, "normal λ line\n"); + try d.put("large.txt", large.items); + const large_pane = try remote.look(a, try std.fmt.allocPrint(a, "/n/peer/os{s}", .{d.abs("large.txt")}), control); + try check(std.mem.eql(u8, try remote.pane(a, large_pane, "body"), large.items), "the large file is not whole", .{}); + try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{large_pane}), "QUIC file saved\n"); + try remote.exec(large_pane, "Save\n"); + try std.testing.expectEqualStrings("QUIC file saved\n", try d.get(a, "large.txt")); + for ([_]u32{ opened, screen_pane, large_pane }) |n| try remote.exec(n, "Del\n"); + try remote.exec(control, "Unmount peer\n"); + const roots = try remote.look(a, "/n", control); + try std.testing.expectEqualStrings("/n/os/\n/n/self/\n", try remote.pane(a, roots, "body")); + try std.testing.expectEqualStrings("TCP to QUIC Get\n", try unix.readFid(unix_body, 0, 65536)); + try std.testing.expectEqualStrings("TCP to QUIC Get\n", try tcp.readFid(tcp_body, 0, 65536)); + // Not checked: a session Looking at its OWN tree through a QUIC + // mount: QUIC is answered on the editor's thread, so that Look waits + // on the loop it runs in (features.txt, "Left as they are"). + }; +} diff --git a/test/process/repls.zig b/test/process/repls.zig new file mode 100644 index 00000000..09e25e96 --- /dev/null +++ b/test/process/repls.zig @@ -0,0 +1,79 @@ +//! docs/typ/reference.typ, REPLs : an exec sent to a real REPL in +//! a real pty. +const std = @import("std"); +const process = @import("../process.zig"); +const Conn = process.Conn; +const has = process.has; +const check = process.check; + +pub const cases = .{ + .{ "repls: a one-line exec sent to Python's own REPL (pyrepl, bracketed paste) runs at once, after idling too, and a block runs whole", pythonRepl }, +}; + +/// Waits up to `ms` for the terminal's body to hold `text`. +fn waitFor(a: std.mem.Allocator, c: *Conn, tty: u32, text: []const u8, ms: i64) !bool { + const deadline = process.nowMs() + ms; + while (process.nowMs() < deadline) { + if (has(try c.pane(a, tty, "body"), text)) return true; + process.nap(100); + } + return false; +} + +fn pythonRepl(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + // 3.13's pyrepl asks for bracketed paste; an older one or none is no test. + const version = try process.run(a, &.{ "python3", "-c", "import sys; sys.exit(sys.version_info < (3, 13))" }, .{}); + if (version.code != 0) { + std.debug.print("SKIP no python3 3.13 or later\n", .{}); + return; + } + var d = try process.Detached.start(gpa, exe, "repl", .{}); + defer d.deinit(); + const c = try Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const before = try c.serials(a); + try c.write("/pane/1/ctl", "Tty\n"); + const deadline = process.nowMs() + 10_000; + var tty: u32 = 0; + while (tty == 0 and process.nowMs() < deadline) : (process.nap(50)) { + for (try c.serials(a)) |n| if (std.mem.indexOfScalar(u32, before, n) == null) { + tty = @max(tty, n); + }; + } + try check(tty != 0, "Tty made no pane", .{}); + try check(try waitFor(a, c, tty, "$", 15_000) or try waitFor(a, c, tty, ">", 15_000), "no prompt: {s}", .{try c.pane(a, tty, "body")}); + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/pty/data", .{tty}), "python3 -q\r"); + try check(try waitFor(a, c, tty, ">>>", 15_000), "no >>>: {s}", .{try c.pane(a, tty, "body")}); + try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{tty}), "Repl python\n"); + // An exec on the REPL's own body goes to the REPL, as its event record + // written back does: an empty range acts on its text. + const fid = try c.hold(try std.fmt.allocPrint(a, "/pane/{d}/event", .{tty}), 2); + defer c.clunk(fid); + _ = try c.writeFid(fid, 0, "MX0 0 0 10 print(6*7)\n"); + try check(try waitFor(a, c, tty, "\n42", 15_000), "no 42: {s}", .{try c.pane(a, tty, "body")}); + // After the session has sat idle, the Enter still waits for the paste: + // core time is as old as the last frame then. + for (0..5) |n| { + process.nap(2_000); + const line = try std.fmt.allocPrint(a, "print({d})", .{1000 + n}); + _ = try c.writeFid(fid, 0, try std.fmt.allocPrint(a, "MX0 0 0 {d} {s}\n", .{ line.len, line })); + try check(try waitFor(a, c, tty, try std.fmt.allocPrint(a, "\n{d}", .{1000 + n}), 15_000), "{d}: {s}", .{ n, try c.pane(a, tty, "body") }); + } + // More than one line, with and without a newline at the end, and a block + // with a statement after it: each runs whole, and the REPL is back at + // its prompt, not at `...`. + for ([_]struct { []const u8, []const []const u8 }{ + .{ "print(11)\nprint(22)", &.{ "11", "22" } }, + .{ "print(33)\nprint(44)\n", &.{ "33", "44" } }, + .{ "for i in range(2):\n print(55 + i)\nprint(77)\n", &.{ "55", "56", "77" } }, + }) |row| { + _ = try c.writeFid(fid, 0, try std.fmt.allocPrint(a, "MX0 0 0 {d} {s}\n", .{ row[0].len, row[0] })); + for (row[1]) |out| try check(try waitFor(a, c, tty, try std.fmt.allocPrint(a, "\n{s}\n", .{out}), 15_000), "{s}: no {s}: {s}", .{ row[0], out, try c.pane(a, tty, "body") }); + process.nap(500); + const body = try c.pane(a, tty, "body"); + try check(std.mem.endsWith(u8, std.mem.trimEnd(u8, body, " \t\r\n"), ">>>"), "{s}: not at its prompt: {s}", .{ row[0], body[body.len -| 200..] }); + } +} diff --git a/test/process/root_ctl.zig b/test/process/root_ctl.zig new file mode 100644 index 00000000..87c9e777 --- /dev/null +++ b/test/process/root_ctl.zig @@ -0,0 +1,87 @@ +//! docs/typ/reference.typ, The root ctl : Dump and Restore in a +//! real session, detached and in a pty. +const std = @import("std"); +const process = @import("../process.zig"); +const Conn = process.Conn; +const has = process.has; +const check = process.check; + +pub const cases = .{ + .{ "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 }, +}; + +/// A connection to `path`, waiting up to 5 s for it to answer. +fn redial(gpa: std.mem.Allocator, path: []const u8) !*Conn { + const deadline = process.nowMs() + 5_000; + while (true) { + if (Conn.dial(gpa, path)) |c| return c else |err| if (process.nowMs() > deadline) return err; + process.nap(20); + } +} + +fn restore(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + for ([_]bool{ false, true }) |tty| { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const name = if (tty) "restore tty space" else "restore detached space"; + var d = try process.Detached.start(gpa, exe, if (tty) "restore-root" else name, .{}); + defer d.deinit(); + var term: ?process.Child = null; + defer if (term) |*t| t.deinit(); + if (tty) term = try d.startTty(exe, name, &.{}); + const address = d.abs(try std.fmt.allocPrint(a, "pardes-9p-{s}.sock", .{name})); + const old = try Conn.dial(gpa, address); + defer old.deinit(gpa); + try old.replace("/pane/1/body", "dumped state\n"); + const control = try old.newPane(a); + try old.execRefused(control, try std.fmt.allocPrint(a, "Restore {s}\n", .{d.abs("missing dump.zon")}), "no such file"); + try std.testing.expectEqualStrings("dumped state\n", try old.read(a, "/pane/1/body")); + try old.exec(control, try std.fmt.allocPrint(a, "Mount own {s}\n", .{address})); + try old.exec(control, "Dump\n"); + const saved_rel = try std.fmt.allocPrint(a, "{s}.dump.zon", .{name}); + const saved = d.abs(saved_rel); + const deadline = process.nowMs() + 5_000; + while (!d.exists(saved_rel)) { + if (process.nowMs() > deadline) return error.NoDump; + process.nap(5); + } + const held_body = try old.hold("/pane/1/body", 0); + const held_screen = try old.hold("/screen", 0); + const old_ids = try old.serials(a); + try std.testing.expectEqualStrings("dumped state\n", try old.readFid(held_body, 0, 65536)); + try std.testing.expectEqualStrings("{", try old.readFid(held_screen, 0, 1)); + try old.replace("/pane/1/body", "changed after dump\n"); + // Unsaved text is asked about once, as Exit asks; the second Restore + // goes ahead, and is answered before the hang-up. + const line = try std.fmt.allocPrint(a, "Restore {s}\n", .{saved}); + if (old.exec(control, line)) |_| {} else |err| { + if (err != error.Refused) return err; + try check(has(old.said, "Modified"), "Restore refused: {s}", .{old.said}); + } + try std.testing.expectEqualStrings("changed after dump\n", try old.read(a, "/pane/1/body")); + // Answered before the hang-up: the write's reply comes; the clunk + // after it may meet the connection already cut. + const restore_fid = try old.hold(try std.fmt.allocPrint(a, "/pane/{d}/exec", .{control}), 1); + _ = try old.writeFid(restore_fid, 0, line); + old.clunk(restore_fid); + // Refused while the connections are cut, or cut already. + if (old.readFid(held_body, 0, 65536)) |_| return error.OldFidUsable else |_| {} + const restored = try redial(gpa, address); + defer restored.deinit(gpa); + // The new log names its panes, then where they came from. + const log = try restored.read(a, "/log"); + try check(has(log, try std.fmt.allocPrint(a, "\nrestore {s}\n", .{saved})), "no restore logged: {s}", .{log}); + // Its panes are the dump's, their serials too. + const ids = try restored.serials(a); + try check(ids.len == 2 and std.mem.eql(u32, ids, old_ids), "restored {any}, dumped {any}", .{ ids, old_ids }); + try std.testing.expectEqualStrings("dumped state\n", try restored.pane(a, ids[0], "body")); + try check((try restored.pane(a, ids[1], "body")).len == 0, "commands ran through exec, not the body", .{}); + var screen: process.Files = .init(gpa, address); + defer screen.deinit(); + const width = try std.unicode.utf8CountCodepoints(std.mem.sliceTo(try screen.screen(), '\n')); + try std.testing.expectEqual(@as(usize, if (tty) 80 else 160), width); // a detached one is 160x50 + const opened = try restored.look(a, try std.fmt.allocPrint(a, "/n/own/os{s}", .{d.abs(try std.fmt.allocPrint(a, "{s}.txt", .{name}))}), ids[1]); + try std.testing.expectEqualStrings("initial\n", try restored.pane(a, opened, "body")); + } +} diff --git a/test/process/tree.zig b/test/process/tree.zig new file mode 100644 index 00000000..c7b9e657 --- /dev/null +++ b/test/process/tree.zig @@ -0,0 +1,178 @@ +//! docs/typ/reference.typ, The tree , through a real session's +//! socket: what a client on the wire sees, and /os on the disk. +const std = @import("std"); +const process = @import("../process.zig"); +const Conn = process.Conn; +const has = process.has; +const check = process.check; + +pub const cases = .{ + .{ "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: a ctl line with no newline is held whatever its write's size, and runs at the close, its failure in the log", heldLine }, + .{ "tree: /screen freezes at its open; a dozen connections in turn each read a body", frozenScreen }, + .{ "tree: a Look at a relative name finds the disk's file first; /n/self is the session's tree, /n/os the disk", looks }, + .{ "tree: names up to 255 bytes list, read, write and open through /os and /n/os, and a directory lists them", longNames }, +}; + +fn rootAndOs(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "first", .{}); + defer d.deinit(); + const c = try Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const root = d.root(); + const names = try c.list(a, "/"); + for ([_][]const u8{ "pane", "index", "status", "look", "exec", "log", "os" }) |want| { + for (names) |n| { + if (std.mem.eql(u8, n, want)) break; + } else return error.NotListed; + } + for (names) |n| try check(!std.mem.eql(u8, n, "self"), "self listed", .{}); + try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/body")); + const os_path = try std.fmt.allocPrint(a, "/os{s}/first.txt", .{root}); + try std.testing.expectEqualStrings("initial\n", try c.read(a, os_path)); + const listed = try c.list(a, try std.fmt.allocPrint(a, "/os{s}", .{root})); + for (listed) |n| { + if (std.mem.eql(u8, n, "first.txt")) break; + } else return error.NotListed; + try std.testing.expectEqualStrings("initial\n", try c.read(a, "/pane/1/../1/body")); + try std.testing.expectEqualStrings("initial\n", try c.read(a, try std.fmt.allocPrint(a, "/os{s}/../{s}/first.txt", .{ root, std.fs.path.basename(root) }))); + try c.replace(os_path, "written through 9P\n"); + try std.testing.expectEqualStrings("written through 9P\n", try d.get(a, "first.txt")); + try std.testing.expectEqualStrings("written through 9P\n", try c.read(a, os_path)); +} + +fn frames(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "frames", .{}); + defer d.deinit(); + _ = std.c.mkdir(d.abs("listing"), 0o755); + _ = symlink(d.abs("listing/missing"), d.abs("listing/broken")); + for (0..24) |i| try d.put(try std.fmt.allocPrint(a, "listing/entry-{d:0>2}", .{i}), ""); + const small = try Conn.dialSized(gpa, d.socket(), 256); + defer small.deinit(gpa); + const names = try small.list(a, try std.fmt.allocPrint(a, "/os{s}/listing", .{d.root()})); + try check(names.len == 24, "listed {d}: {any}", .{ names.len, names }); + for (names) |n| try check(std.mem.startsWith(u8, n, "entry-"), "listed {s}", .{n}); + const big = try Conn.dial(gpa, d.socket()); + defer big.deinit(gpa); + try std.testing.expectEqual(@as(u32, 65536), big.msizeAgreed()); +} + +extern "c" fn symlink(target: [*:0]const u8, path: [*:0]const u8) c_int; + +fn heldLine(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "held", .{}); + defer d.deinit(); + // 64 KiB frames: a line with no newline is held whatever the size of its + // write (a mount cuts lines anywhere), and runs at the close. + const c = try Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const ctl = try c.hold("/pane/1/ctl", 1); + const line = try a.alloc(u8, 5 + 20000); + @memcpy(line[0..5], "bogus"); + @memset(line[5..], 'x'); + _ = try c.writeFid(ctl, 0, line); + try check(!has(try c.read(a, "/log"), "ctl: unknown control message"), "ran before the close", .{}); + c.clunk(ctl); + try check(has(try c.read(a, "/log"), "ctl: unknown control message"), "never ran", .{}); +} + +fn frozenScreen(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "frozen", .{}); + defer d.deinit(); + const c = try Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const frozen = try c.hold("/screen", 0); + var before: std.ArrayList(u8) = .empty; + try before.appendSlice(a, try c.readFid(frozen, 0, 31)); + try c.write("/pane/1/body", "screen changed\n"); + while (true) { + const chunk = try c.readFid(frozen, before.items.len, 65536); + if (chunk.len == 0) break; + try before.appendSlice(a, chunk); + } + c.clunk(frozen); + const json = try std.json.parseFromSliceLeaky(std.json.Value, a, before.items, .{}); + var old: std.ArrayList(u8) = .empty; + for (json.object.get("cells").?.array.items) |cell| try old.appendSlice(a, cell.array.items[0].string); + try check(has(old.items, "initial") and !has(old.items, "screen changed"), "the open's screen moved", .{}); + try check(has(try d.files.screen(), "screen changed"), "a new open shows the old screen", .{}); + const body = "initial\nscreen changed\n"; + for (0..12) |i| { + const other = try Conn.dial(gpa, d.socket()); + defer other.deinit(gpa); + try check(std.mem.eql(u8, try other.read(a, "/pane/1/body"), body), "connection {d}", .{i}); + } +} + +fn looks(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "first", .{}); + defer d.deinit(); + const c = try Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + // A build with its sources (-Dembed-sources) serves them under /src. + for (try c.list(a, "/")) |n| if (std.mem.eql(u8, n, "src")) { + const source = try c.look(a, "/virtual/src/pardes.zig", null); + try check(has(try c.pane(a, source, "body"), "pub const Pardes"), "/virtual/src", .{}); + }; + try d.put("index", "OS takes precedence\n"); + var opened = try c.look(a, "index", null); + try std.testing.expectEqualStrings("OS takes precedence\n", try c.pane(a, opened, "body")); + opened = try c.look(a, "/n/self/index", null); + const index = try c.pane(a, opened, "body"); + try check(std.mem.startsWith(u8, index, "1 ") and has(index, "first.txt"), "/n/self/index: {s}", .{index}); + try d.put("explicit.txt", "explicit OS mount\n"); + opened = try c.look(a, try std.fmt.allocPrint(a, "/n/os{s}", .{d.abs("explicit.txt")}), null); + try std.testing.expectEqualStrings("explicit OS mount\n", try c.pane(a, opened, "body")); +} + +fn longNames(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "long-names", .{}); + defer d.deinit(); + const c = try Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const dir_rel = try a.alloc(u8, 64); + @memcpy(dir_rel[0..10], "directory-"); + @memset(dir_rel[10..], 'd'); + _ = std.c.mkdir(d.abs(dir_rel), 0o755); + const dir = d.abs(dir_rel); + for ([_]usize{ 29, 63, 127, 255 }) |length| { + const name = try a.alloc(u8, length); + @memset(name[0 .. length - 4], 'f'); + @memcpy(name[length - 4 ..], ".txt"); + const rel = try std.fmt.allocPrint(a, "{s}/{s}", .{ dir_rel, name }); + try d.put(rel, "long filename\n"); + const wire = try std.fmt.allocPrint(a, "/os{s}", .{d.abs(rel)}); + const listed = try c.list(a, try std.fmt.allocPrint(a, "/os{s}", .{dir})); + for (listed) |n| { + if (std.mem.eql(u8, n, name)) break; + } else return error.NotListed; + try std.testing.expectEqualStrings("long filename\n", try c.read(a, wire)); + try c.replace(wire, "written through long filename\n"); + try std.testing.expectEqualStrings("written through long filename\n", try d.get(a, rel)); + const opened = try c.look(a, try std.fmt.allocPrint(a, "/n/os{s}", .{d.abs(rel)}), null); + try std.testing.expectEqualStrings("written through long filename\n", try c.pane(a, opened, "body")); + try c.removePane(opened); // a long name's tag takes rows + } + const opened = try c.look(a, try std.fmt.allocPrint(a, "/n/os{s}", .{dir}), null); + const listing = try c.pane(a, opened, "body"); + for (try process.entries(a, dir)) |n| try check(has(listing, try std.fmt.allocPrint(a, "/n/os{s}/{s}", .{ dir, n })), "{s} not listed", .{n}); +} diff --git a/test/process_test.zig b/test/process_test.zig index 9924b9dd..828edff9 100644 --- a/test/process_test.zig +++ b/test/process_test.zig @@ -6,7 +6,14 @@ const std = @import("std"); const files = .{ + @import("process/builtins.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/tree.zig"), }; pub fn main(init: std.process.Init) !void { -- cgit v1.3 From 2693c24cf44b8788ec26f98eeedc4d74b5ac98de Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 16:50:10 -0300 Subject: test: the 9P monkey at T1 and the agent driver's interaction checks at T2, for the Python ports: test/monkey9p.zig (was test/monkey9p.py) drives an in-process Session with random 9P requests, every op drawn from the seed alone (panes, columns, stale serials and held files as tokens resolved when the op runs), so --seed/--steps replay a run, --only replays any subset of its steps and a failure shrinks by delta debugging over them; each run is a child of the program in a throwaway root (its HOME, XDG directories and dump), so a panic or a hang is a failure reported and shrunk like the rules' (one failure rule, close runs only line files, /index matching /pane, chunked reads equal whole, the log marker); the 9P engine's own refusals and OTRUNC's truncation made by the monkey, Session doing no effects; steps monkey-session and monkey-session-smoke, the smoke part of behaviour-test; Session's mky helpers walk, ask (a raw request answering errno, held or bytes) and ended; test/process/terminals.zig, three cases of test/agent_session_test.py (a prompt that settles, input shown and ^U taking the screen back; history past a screenful in the body; a raw-mode control byte through pty/data), the driver's own argument, output, timeout and cleanup checks left in Python; monkey9p.py deleted; each checked to fail against a break: serveFs not noting a pane file's err (one failure rule), noting it twice (one failure rule's upper bound), a msg beside a refused pane write (its msg clause), /pane leaving out serial 3 (index matching pane dirs), stagedReply skipping a byte past offset 0 on small reads (chunked equals whole), a panic in pty/run's write (a crash, shrunk 377 -> 3 ops), the root ctl taking an unknown word (the log marker), pty/data dropping a lone control byte (raw key), a 4 KiB terminal scrollback (history, raw key), pty/data dropping a write's last byte (all three terminal cases) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- build.zig | 23 + docs/typ/building.typ | 4 +- test/agent_session_test.py | 13 +- test/monkey9p.py | 2010 -------------------------------------------- test/monkey9p.zig | 1669 ++++++++++++++++++++++++++++++++++++ test/process/terminals.zig | 121 +++ test/process_test.zig | 1 + test/session.zig | 71 ++ 8 files changed, 1894 insertions(+), 2018 deletions(-) delete mode 100644 test/monkey9p.py create mode 100644 test/monkey9p.zig create mode 100644 test/process/terminals.zig (limited to 'test/process_test.zig') diff --git a/build.zig b/build.zig index c90485b6..78c01c61 100644 --- a/build.zig +++ b/build.zig @@ -1893,6 +1893,29 @@ pub fn build(b: *std.Build) void { const behaviour_test_step = b.step("behaviour-test", "run the behavioural tests, through the 9P tree alone (test/behaviour/)"); behaviour_test_step.dependOn(&run_behaviour_tests.step); if (testMatch(b, unit_profile, test_filters, behaviour_test_step)) |check| check.addArtifactArg(behaviour_tests); + // The 9P monkey at T1 (test/monkey9p.zig): random requests against + // an in-process session, the documented rules checked after each. + // Its fixed-seed smoke run is part of behaviour-test. + const monkey_session = b.addExecutable(.{ + .name = "pardes-monkey-session", + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = b.path("test/monkey9p.zig"), + .link_libc = true, + }), + }); + monkey_session.root_module.addImport("session", session_module); + const run_monkey_session = b.addRunArtifact(monkey_session); + run_monkey_session.addPassthruArgs(); + run_monkey_session.has_side_effects = true; + b.step("monkey-session", "random 9P requests against an in-process session, checking the documented rules (-- [--seed N]... [--steps M] [--only i,j,k] [--no-shrink] [-v])").dependOn(&run_monkey_session.step); + const run_monkey_session_smoke = b.addRunArtifact(monkey_session); + run_monkey_session_smoke.addArg("--smoke"); + run_monkey_session_smoke.has_side_effects = true; + const monkey_session_smoke_step = b.step("monkey-session-smoke", "a short fixed-seed run of monkey-session (part of behaviour-test)"); + monkey_session_smoke_step.dependOn(&run_monkey_session_smoke.step); + if (test_filters.len == 0) behaviour_test_step.dependOn(&run_monkey_session_smoke.step); // Kitty graphics conformance at the core: what a terminal pane's // program sends, what it reads back, what ghostty stores and what a diff --git a/docs/typ/building.typ b/docs/typ/building.typ index d8c089f6..551face1 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -79,8 +79,8 @@ zig build kitty-harness kitty graphics through a pane end to end (a fake kitt kitten icat and chafa too when installed) zig build syntax-test tree-sitter tests without building the editor zig build syntax deterministic per-byte highlighting snapshots -zig build fs-test real sessions and mounts over 9P (with a short 9P monkey) -zig build monkey-9p random 9P operations checking the documented rules +zig build fs-test real sessions and mounts over 9P +zig build monkey-session random in-process 9P requests checking the documented rules zig build agent-session-test interactive session driver checks over 9P zig build 9p-test freestanding protocol tests zig build 9p-io-test native 9P transports and client 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/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 -- [--seed N] [--steps M] - zig build monkey-9p -Dplatform=tty --prefix -- --replay FILE - zig build monkey-9p -Dplatform=tty --prefix -- --shrink FILE - python3 test/monkey9p.py [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 /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 -/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(' 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(' 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(' 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('.\\*?![]{}\'"') - - -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 = 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 : - `, 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 (` deleted on disk ...`, `changed on - # disk`, a stamped `reloaded ` 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// 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(' {"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(' 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..03c42f2a --- /dev/null +++ b/test/monkey9p.zig @@ -0,0 +1,1669 @@ +//! 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; + var lines = std.mem.tokenizeScalar(u8, 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) []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" } }, + }; + inline for (cases) |case| { + const got = servedLines(a, case[0], case[1]); + 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); + 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 ""`. +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 +/// : `, 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 : ` + 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// 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 ` +/// lines, each step announced as `mky step ` 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) 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(); + std.process.exit(try fuzz(gpa, if (seeds.items.len > 0) seeds.items[0] else 1, steps orelse 1000, only, verbose, std.mem.span(root))); + } + 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/terminals.zig b/test/process/terminals.zig new file mode 100644 index 00000000..ba73a64e --- /dev/null +++ b/test/process/terminals.zig @@ -0,0 +1,121 @@ +//! docs/typ/guide.typ, Terminals : an interactive program in a +//! real session's shell, driven through its pty files as an agent drives +//! one (test/agent_session_test.py's interaction checks, which went through +//! test/agent_session.py). +const std = @import("std"); +const process = @import("../process.zig"); +const eh = @import("../e2e_harness.zig"); + +pub const cases = .{ + .{ "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 }, +}; + +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 (lines(try f.read(body)) < rows) { + if (eh.nowMs() > deadline) return error.ShortHistory; + sleepMs(50); + } + if (rows == 0) try std.testing.expect(lines(try f.read(body)) < 1000); + 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 lines(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_test.zig b/test/process_test.zig index 9924b9dd..5f2fcb83 100644 --- a/test/process_test.zig +++ b/test/process_test.zig @@ -7,6 +7,7 @@ const std = @import("std"); const files = .{ @import("process/other_files.zig"), + @import("process/terminals.zig"), }; pub fn main(init: std.process.Init) !void { 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} { -- cgit v1.3