Debian Codegraph @

testsuite/rrsync-no-overwrite-delay-updates_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """rrsync -no-overwrite must protect implicit delay-update partial files."""
3
4 import os
5 import shlex
6 import signal
7 import subprocess
8 import sys
9
10 signal.signal(signal.SIGUSR1, signal.SIG_IGN)
11 signal.signal(signal.SIGUSR2, signal.SIG_IGN)
12 if '--shell' in sys.argv:
13 i = sys.argv.index('--shell') + 2
14 env = {**os.environ, 'SSH_ORIGINAL_COMMAND': ' '.join(sys.argv[i:])}
15 signal.signal(signal.SIGUSR1, signal.SIG_DFL)
16 signal.signal(signal.SIGUSR2, signal.SIG_DFL)
17 wrapper, root = env['RRSYNC_WRAPPER'], env['RRSYNC_ROOT']
18 flags = env.get('RRSYNC_FLAGS', '-wo -no-overwrite -no-lock').split()
19 os.execve(wrapper, [wrapper, *flags, root], env)
20
21 from rsyncfns import RSYNC, SCRATCHDIR, makepath, patched_rrsync, rmtree, rsync_argv, rsync_path_arg
22
23 base = SCRATCHDIR / 'rrsync-no-overwrite-delay-updates'
24 rmtree(base)
25 src = base / 'src'
26 root = base / 'root'
27 makepath(src, root / '.~tmp~')
28 (src / 'victim').write_bytes(b'NEW')
29 protected = root / '.~tmp~' / 'victim'
30 protected.write_bytes(b'POLICY')
31
32 # RSYNC may be a multi-word command (the runner's --protocol=N, or valgrind)
33 # while rrsync hands its RSYNC to execlp() as a single executable name, so it
34 # has to be wrapped before patched_rrsync() sees it.
35 shim = base / 'rsync-shim'
36 shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n')
37 shim.chmod(0o755)
38 wrapper = patched_rrsync(base, rsync_path=str(shim))
39 rsh = f'{shlex.quote(sys.executable)} {shlex.quote(os.path.abspath(__file__))} --shell'
40 env = {**os.environ, 'RRSYNC_WRAPPER': str(wrapper), 'RRSYNC_ROOT': str(root)}
41 got = subprocess.run(
42 rsync_argv('-rI', '--delay-updates', '-e', rsh,
43 f'{src}/victim', 'ignored:'),
44 env=env, capture_output=True, text=True, timeout=20)
45
46 state = protected.read_bytes() if protected.exists() else None
47 assert got.returncode != 0 and 'option --delay-updates has been disabled' in got.stderr, (
48 f'rrsync -no-overwrite accepted --delay-updates: rc={got.returncode}, '
49 f'stdout={got.stdout!r}, stderr={got.stderr!r}')
50 assert state == b'POLICY', f'delay-updates consumed an existing file: state={state!r}'
51
52 # The refusal must be CONDITIONAL on -no-overwrite. Run the identical option
53 # through a wrapper without it: if that is refused too, the option is simply
54 # disabled for every rrsync deployment and the assertion above proves nothing
55 # about the policy. A fresh root keeps this from disturbing the checks above.
56 plain_root = base / 'root-plain'
57 rmtree(plain_root)
58 makepath(plain_root / '.~tmp~')
59 (plain_root / '.~tmp~' / 'victim').write_bytes(b'POLICY')
60 plain_env = {**env, 'RRSYNC_ROOT': str(plain_root), 'RRSYNC_FLAGS': '-wo -no-lock'}
61 plain = subprocess.run(
62 rsync_argv('-rI', '--delay-updates', '-e', rsh, f'{src}/victim', 'ignored:'),
63 env=plain_env, capture_output=True, text=True, timeout=20)
64 assert plain.returncode == 0, (
65 'without -no-overwrite the same option must be accepted, or the refusal '
66 'above is unconditional and says nothing about the policy: '
67 f'rc={plain.returncode}, stderr={plain.stderr!r}')