Debian Codegraph @

testsuite/daemon-refuse-delete-alias_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """A refuse rule naming one spelling of an option must cover the others.
3
4 --del and --delete-during are the same capability written two ways, but the
5 popt table spells them differently: --del is POPT_ARG_NONE with a destination
6 (popt stores 1), --delete-during is POPT_ARG_VAL storing 1. A refuse rule
7 matched on the raw table fields therefore disabled only the spelling the
8 administrator happened to write, and the other one still worked.
9
10 Both directions are checked, because the mismatch is symmetric: a rule naming
11 the canonical option was evaded by the short alias, and a rule naming the
12 alias was evaded by the canonical option.
13
14 No raw protocol here, and no custom client: --remote-option (-M) puts the
15 option in the daemon's argv verbatim, which is exactly the reach an ordinary
16 user has.
17 """
18
19 import subprocess
20
21 from rsyncfns import (
22 SCRATCHDIR, makepath, rmtree, rsync_argv, start_test_daemon, test_fail,
23 write_daemon_conf,
24 )
25
26 PORT = 12951
27
28 base = SCRATCHDIR / 'daemon-refuse-delete-alias'
29 rmtree(base)
30 src = base / 'src'
31 makepath(src)
32 (src / 'keep').write_text('KEEP\n')
33
34 # Two modules, each refusing one spelling of the same capability.
35 mods = {}
36 for name, rule in (('refuses_canonical', 'delete-during'), ('refuses_alias', 'del')):
37 d = base / name
38 makepath(d)
39 mods[name] = d
40
41 conf = write_daemon_conf([
42 ('refuses_canonical', {'path': str(mods['refuses_canonical']), 'read only': 'no',
43 'use chroot': 'no', 'refuse options': 'delete-during'}),
44 ('refuses_alias', {'path': str(mods['refuses_alias']), 'read only': 'no',
45 'use chroot': 'no', 'refuse options': 'del'}),
46 # No rule at all: proves the pushes themselves work, so a refusal below
47 # is the rule firing rather than the transfer being broken.
48 ('open', {'path': str(base / 'open'), 'read only': 'no', 'use chroot': 'no'}),
49 ], name='refuse-delete-alias.conf')
50 makepath(base / 'open')
51 url = start_test_daemon(conf, PORT)
52
53
54 def push(module, *opts):
55 return subprocess.run(
56 rsync_argv('-r', *opts, f'{src}/', f'{url}{module}/'),
57 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=120)
58
59
60 def refused(proc, spelling):
61 """Did the daemon refuse this exact spelling?
62
63 Requiring the option name, not just the word "refuse", keeps an unrelated
64 refusal from standing in for the one under test -- and it doubles as proof
65 that -M actually delivered the option to the daemon, which the control
66 below cannot show on its own: a silently discarded -M would let the
67 transfer succeed just the same."""
68 return (proc.returncode != 0
69 and f'configured to refuse --{spelling}' in proc.stdout)
70
71
72 # Control: both spellings transfer where nothing refuses them, so a refusal
73 # below is the rule firing rather than the push being broken. That -M reached
74 # the daemon at all is established by the spelling-specific refusals, not here:
75 # a silently dropped -M would pass this control too.
76 for opt in ('-M--del', '-M--delete-during'):
77 proc = push('open', '--delete', opt)
78 if proc.returncode != 0 or not (base / 'open' / 'keep').is_file():
79 test_fail(f'control failed: {opt} could not push to a module with no '
80 f'refuse rule (rc={proc.returncode}, '
81 f'output={proc.stdout.strip()[:300]!r})')
82
83 # The reported direction: the rule names the canonical option, the client
84 # sends the alias.
85 proc = push('refuses_canonical', '--delete', '-M--del')
86 if not refused(proc, 'del'):
87 test_fail('"refuse options = delete-during" did not refuse --del, which '
88 'sets the very same delete_during: the rule names a capability, '
89 f'not one spelling of it (rc={proc.returncode}, '
90 f'output={proc.stdout.strip()[:300]!r})')
91
92 # ...and the mirror, since the table shapes differ in both directions.
93 proc = push('refuses_alias', '--delete', '-M--delete-during')
94 if not refused(proc, 'delete-during'):
95 test_fail('"refuse options = del" did not refuse --delete-during '
96 f'(rc={proc.returncode}, output={proc.stdout.strip()[:300]!r})')
97
98 # And each rule still refuses the spelling it actually names.
99 proc = push('refuses_canonical', '--delete', '-M--delete-during')
100 if not refused(proc, 'delete-during'):
101 test_fail('"refuse options = delete-during" did not even refuse '
102 f'--delete-during (rc={proc.returncode}, '
103 f'output={proc.stdout.strip()[:300]!r})')
104 proc = push('refuses_alias', '--delete', '-M--del')
105 if not refused(proc, 'del'):
106 test_fail(f'"refuse options = del" did not even refuse --del '
107 f'(rc={proc.returncode}, output={proc.stdout.strip()[:300]!r})')
108
109 print('a refuse rule for one spelling of --delete-during covers --del too')