Debian Codegraph @

testsuite/keep-dirlinks-rule_test.py

raw symbols SCIP
1 """Pin the concrete rule behind `--keep-dirlinks` (-K).
2
3 The rsync(1) `--keep-dirlinks` entry leads with a clear rule -- "treat a symlink
4 to a directory as though it were a real directory, but only if it matches a real
5 directory from the sender" -- but then a vague security caution ("you must trust
6 all the symlinks ... the user could then (on a subsequent copy) replace the
7 symlink ..."). This test pins the deterministic part of the behaviour so the
8 caution can be tied to a concrete mechanism:
9
10 - sender has a real dir of that name -> dest symlink-to-dir is KEPT and the
11 contents land THROUGH it (this is the followed-into-target behaviour the
12 caution warns about);
13 - sender has a non-dir (file) of that name -> -K does not apply, the dest
14 symlink is replaced by the file;
15 - default (no -K) -> the dest symlink is deleted and recreated as a real dir.
16
17 Pure local client behaviour: no daemon/root/tcp. Cross-version: expected
18 identical against --rsync-bin=old_versions/rsync_3.2.7.
19 """
20
21 import os
22 import subprocess
23
24 from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail
25
26 base = SCRATCHDIR / 'keep-dirlinks'
27 rmtree(base)
28 base.mkdir(parents=True)
29
30
31 def dest_with_symlink(name):
32 """Fresh dest where `name` is a symlink to a real 'target' dir."""
33 d = base / name
34 rmtree(d)
35 (d / 'target').mkdir(parents=True)
36 os.symlink('target', d / 'd')
37 return d
38
39
40 def run(src, dst, *opts):
41 subprocess.run(rsync_argv('-a', *opts, f'{src}/', f'{dst}/'),
42 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
43
44
45 # sender with a real directory 'd' containing a file.
46 sdir = base / 'src_dir'
47 (sdir / 'd').mkdir(parents=True)
48 (sdir / 'd' / 'f').write_text('X\n')
49
50 # sender with 'd' as a plain FILE.
51 sfile = base / 'src_file'
52 sfile.mkdir()
53 (sfile / 'd').write_text('FILE\n')
54
55 # 1) -K + sender dir match: symlink kept, contents land through it.
56 d = dest_with_symlink('case_match')
57 run(sdir, d, '--keep-dirlinks')
58 if not (d / 'd').is_symlink():
59 test_fail("--keep-dirlinks: dest symlink-to-dir was replaced despite a sender dir match")
60 through = d / 'target' / 'f'
61 if not through.is_file() or through.read_text() != 'X\n':
62 test_fail("--keep-dirlinks: contents did not land THROUGH the kept symlink")
63
64 # 2) -K + sender is a file: -K doesn't apply, symlink replaced by the file.
65 d = dest_with_symlink('case_file')
66 run(sfile, d, '--keep-dirlinks')
67 if (d / 'd').is_symlink():
68 test_fail("--keep-dirlinks: dest symlink kept even though the sender 'd' is a file")
69 if not (d / 'd').is_file() or (d / 'd').read_text() != 'FILE\n':
70 test_fail("--keep-dirlinks: sender file did not replace the dest symlink")
71
72 # 3) default (no -K): symlink deleted and recreated as a real directory.
73 d = dest_with_symlink('case_default')
74 run(sdir, d)
75 if (d / 'd').is_symlink():
76 test_fail("default: dest symlink-to-dir should have been replaced by a real dir")
77 if not (d / 'd').is_dir() or not (d / 'd' / 'f').is_file():
78 test_fail("default: 'd' was not recreated as a real directory with the file")
79
80 print("keep-dirlinks-rule: -K keeps+follows a dest symlink-to-dir only on a "
81 "sender DIR match (contents land through it); a sender file replaces it; "
82 "the default replaces the symlink with a real directory")