Debian Codegraph @

testsuite/no-implied-dirs-symlink_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Pin the receiver-side symlink behaviour of -R `--no-implied-dirs`.
3
4 The rsync(1) `--no-implied-dirs` entry describes the effect euphemistically:
5 "the receiving rsync updates "path/foo/file" using the existing path elements,
6 which means that the file ends up being created in "path/bar"." It does not
7 spell out the security-relevant consequence: an EXISTING destination symlink
8 used as a path element is FOLLOWED, so the write is silently redirected through
9 it. The default (implied dirs) instead deletes that symlink and recreates it as
10 a real directory.
11
12 This test makes both behaviours explicit and measured. Pure local client
13 behaviour: no daemon/root/tcp. Cross-version: expected identical against
14 --rsync-bin=old_versions/rsync_3.2.7.
15 """
16
17 import os
18 import subprocess
19
20 from rsyncfns import SCRATCHDIR, forced_protocol, rmtree, rsync_argv, test_fail
21
22 # The -R "/./" implied-dir marker is a protocol-30+ feature; at protocol 29 the
23 # generator rejects the multi-component "path/file" as an invalid path and
24 # aborts (verified identical on the 3.2.7 oracle), so the behaviour this test
25 # pins isn't reachable there. Pass-through rather than skip, matching the
26 # sibling relative-implied test's handling of the same proto-29 limitation.
27 proto = forced_protocol()
28 if proto is not None and proto < 30:
29 print(f"no-implied-dirs-symlink: protocol {proto} -- the -R /./ implied-dir "
30 "path element is rejected by the proto-29 generator; nothing to test")
31 raise SystemExit(0)
32
33 base = SCRATCHDIR / 'no-implied-dirs'
34 rmtree(base)
35 base.mkdir(parents=True)
36
37 src = base / 'src'
38 (src / 'path').mkdir(parents=True)
39 (src / 'path' / 'file').write_text('NEW\n')
40
41
42 def run(dst, *opts):
43 """Fresh dst with 'path' pre-existing as a symlink -> 'real'; transfer
44 src/./path/file with -R and the given opts."""
45 rmtree(dst)
46 (dst / 'real').mkdir(parents=True)
47 os.symlink('real', dst / 'path')
48 subprocess.run(rsync_argv('-a', '-R', *opts, f'{src}/./path/file', f'{dst}/'),
49 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
50 return dst
51
52
53 # --no-implied-dirs: the dest symlink is kept and followed -> file lands in real/.
54 d = run(base / 'noimplied', '--no-implied-dirs')
55 if not (d / 'path').is_symlink():
56 test_fail("--no-implied-dirs: dest 'path' symlink was replaced (expected kept)")
57 through = d / 'real' / 'file'
58 if not through.is_file() or through.read_text() != 'NEW\n':
59 test_fail("--no-implied-dirs: write was not redirected through the symlink "
60 "(expected real/file to receive the content)")
61
62 # default (implied dirs): the dest symlink is deleted and recreated as a real dir.
63 d = run(base / 'implied')
64 if (d / 'path').is_symlink():
65 test_fail("default: dest 'path' symlink should have been replaced by a real dir")
66 direct = d / 'path' / 'file'
67 if not direct.is_file() or direct.read_text() != 'NEW\n':
68 test_fail("default: file did not land in the recreated real directory")
69
70 print("no-implied-dirs-symlink: -R --no-implied-dirs FOLLOWS an existing dest "
71 "symlink path element (write redirected through it); the default replaces "
72 "the symlink with a real directory")