Debian Codegraph @

testsuite/daemon-munge_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Daemon coverage: munge symlinks.
3
4 A module with "munge symlinks = yes" stores incoming symlinks with a
5 /rsyncd-munged/ prefix (so they can't be used to escape the module) and strips
6 that prefix from outgoing symlinks. Verify both directions on a symlink several
7 levels deep.
8 """
9
10 import os
11 import subprocess
12
13 from rsyncfns import (
14 FROMDIR, SCRATCHDIR,
15 assert_is_symlink, make_tree, makepath, rmtree, rsync_argv,
16 start_test_daemon, test_fail, write_daemon_conf,
17 )
18
19 DAEMON_PORT = 12890
20
21 src = FROMDIR
22 deep = os.path.join('d1', 'd2')
23 rmtree(src)
24 make_tree(src, depth=3)
25 os.symlink('f3', src / deep / 'sl') # deep symlink -> f3
26
27 mungedest = SCRATCHDIR / 'mungedest'
28 pulled = SCRATCHDIR / 'mungepull'
29 for d in (mungedest, pulled):
30 rmtree(d)
31 makepath(mungedest, pulled)
32
33 conf = write_daemon_conf([
34 ('munge', {'path': mungedest, 'read only': 'no', 'munge symlinks': 'yes'}),
35 ])
36 url = start_test_daemon(conf, DAEMON_PORT)
37
38 # --- push: the stored symlink is munged with the /rsyncd-munged/ prefix ------
39 subprocess.run(rsync_argv('-al', f'{src}/', f'{url}munge/'),
40 stdout=subprocess.DEVNULL)
41 stored = mungedest / deep / 'sl'
42 assert_is_symlink(stored, label='munge stored symlink')
43 target = os.readlink(stored)
44 if target != '/rsyncd-munged/f3':
45 test_fail(f"munge symlinks stored {target!r}, expected '/rsyncd-munged/f3'")
46
47 # --- pull: the prefix is stripped back off on the way out -------------------
48 subprocess.run(rsync_argv('-al', f'{url}munge/', f'{pulled}/'),
49 stdout=subprocess.DEVNULL)
50 out = pulled / deep / 'sl'
51 assert_is_symlink(out, target='f3', label='munge stripped on pull')
52
53 print("daemon-munge: munge symlinks adds/strips /rsyncd-munged/ at depth")