diff options
Diffstat (limited to 'test/fs.py')
| -rw-r--r-- | test/fs.py | 139 |
1 files changed, 89 insertions, 50 deletions
@@ -22,8 +22,10 @@ from ninep import Client @contextlib.contextmanager def session(binary, root, name, *options, socket_name=None, file=None, tty=False, network=None, launch=None, inherited=None, terminal=None): + # No pager of the caller's (a session's own PAGER, SYSTEMD_PAGER...): + # its shells get the session's defaults, the ones under test. env = {key: value for key, value in os.environ.items() - if not key.startswith('PARDES_')} + if not key.startswith('PARDES_') and 'PAGER' not in key} env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), PARDES_NOTIME='1', PARDES_DUMP=str(root / (name + '.dump.zon'))) @@ -47,6 +49,7 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False preexec = lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0) if launch is not None: args = [binary, *launch] + existing = set(root.glob('pardes-9p-*.sock')) child = subprocess.Popen(args, cwd=root, env=env, stdin=slave, stdout=slave if tty else subprocess.DEVNULL, stderr=subprocess.PIPE, start_new_session=True, @@ -55,7 +58,9 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False # Borrowed handles for input-driving probes, valid inside this context. terminal.update(pid=child.pid, input_fd=master) if launch is not None and socket_name is None: - address = root / f'pardes-9p-{child.pid}.sock' + # Named by the session's pid: a terminal's session is forked + # off the process launched, which stays as its stub. + address = None if slave is not None: os.close(slave) slave = None @@ -68,7 +73,11 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False reader = threading.Thread(target=drain, daemon=True) reader.start() deadline = time.monotonic() + 10 - while not address.exists(): + while address is None or not address.exists(): + if address is None: + address = next(iter(set(root.glob('pardes-9p-*.sock')) - existing), None) + if address is not None: + continue if child.poll() is not None: raise AssertionError((args, child.returncode, child.stderr.read().decode())) if time.monotonic() > deadline: @@ -512,8 +521,10 @@ def run_file(binary): assert b'\x1b' not in client.read(f'/pane/{pager[0]}/body') client.remove(f'/pane/{pager[0]}') # systemd's tools page through their own variable: set too, its - # secure mode off, the path unquoted where it needs none. - systemd_pager = run(client, term, b'printenv SYSTEMD_PAGER SYSTEMD_PAGERSECURE\n') + # secure mode off, the path unquoted where it needs none. Read + # by the shell: coreutils 9.12's printenv quotes a value with a + # blank on a terminal. + systemd_pager = run(client, term, b'printf "%s\\n" "$SYSTEMD_PAGER" "$SYSTEMD_PAGERSECURE"\n') assert systemd_pager.startswith(b'exit 0\n') and systemd_pager.rstrip().endswith(b' -\n0'), systemd_pager if shutil.which('journalctl'): assert run(client, term, b'journalctl -n 3 --no-hostname\n').startswith(b'exit ') @@ -544,8 +555,9 @@ def run_file(binary): time.sleep(.05) locked.chmod(0o600) try: - client.write(f'/pane/{term}/pty/ctl', b'exec\n') - raise AssertionError('pty/ctl exec of a shell that cannot start was taken') + if os.geteuid() != 0: # root enters any directory + client.write(f'/pane/{term}/pty/ctl', b'exec\n') + raise AssertionError('pty/ctl exec of a shell that cannot start was taken') except OSError as refused: assert 'shell' in str(refused), refused finally: @@ -984,19 +996,24 @@ def python_repl_runs_one_line(binary): def signal_cleans_up(binary): - """A detached session told to stop by a signal unlinks its sockets.""" + """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): - detached = root / 'pardes-detached-sig.sock' - assert address.exists() and detached.exists() + 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() or detached.exists()) and time.monotonic() < deadline: + while address.exists() and time.monotonic() < deadline: time.sleep(.01) - assert not address.exists() and not detached.exists(), (sig, list(root.iterdir())) + assert not address.exists(), (sig, list(root.iterdir())) def odd_paths(binary): @@ -1422,10 +1439,9 @@ def test(binary, quic=False): # The new log names its panes, then where they came from. log = restored.read('/log') assert b'\nrestore ' + str(saved).encode() + b'\n' in log, log - # then which serial each pane had: restored <old> <new> - assert (b'\n'.join(b'restored %d %d' % (old, new) for old, new in zip(sorted(old_ids), sorted(int(r.split()[0]) for r in restored.read('/index').splitlines()))) + b'\nrestoredcol ') in log, log + # Its panes are the dump's, their serials too. restored_ids = [int(row.split()[0]) for row in restored.read('/index').splitlines()] - assert len(restored_ids) == 2 and old_ids.isdisjoint(restored_ids), restored_ids + assert len(restored_ids) == 2 and set(restored_ids) == old_ids, restored_ids file_id, control_id = restored_ids contents = restored.read(f'/pane/{file_id}/body') assert contents == b'dumped state\n', (contents, saved.read_text(), restored.read('/index')) @@ -1651,15 +1667,17 @@ def test(binary, quic=False): 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 / '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 + # (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. @@ -1747,34 +1765,43 @@ def test(binary, quic=False): 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 - # and `rgb` in the colour given, the rest in the pane's ink. + # 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 - assert client.read(f'/pane/{made_now}/body') == b'red rgb plain\n' - screen = client.screen() - for row in range(screen['rows']): - cells = screen['cells'][row * screen['cols']:(row + 1) * screen['cols']] - text = ''.join(cell[0] for cell in cells) - at = text.find('red rgb plain') - if at >= 0: - break - else: + 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 = [screen['styles'][cells[at + i][1]]['fg'] for i in (0, 4, 8)] + 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 - # The directory's one +Pager: the next paged text refills it. + # 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 - assert serials() - before == {made_now}, (serials(), before) - assert client.read(f'/pane/{made_now}/body') == b'again\nprogress 100%\n' - client.remove(f'/pane/{made_now}') + 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 the one +Pager (each write once replaced the last). + # 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, @@ -1789,7 +1816,8 @@ def test(binary, quic=False): stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=600) huge.wait() assert fed.returncode == 0, fed.stderr - assert serials() - before == {big_pane} + 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:]) @@ -1812,16 +1840,17 @@ def test(binary, quic=False): 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. + # not a pane that only fails at Save. (Root writes anywhere.) shut = root / 'shut-dir' shut.mkdir() shut.chmod(0o500) try: - 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) + 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'), @@ -1885,6 +1914,7 @@ def test(binary, quic=False): 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, @@ -1897,12 +1927,21 @@ def test(binary, quic=False): if select.select([master], [], [], .1)[0]: seen += os.read(master, 65536) time.sleep(.3) - own_socket = root / f'pardes-9p-{killed.pid}.sock' - assert own_socket.exists() + # 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: |
