Debian Codegraph @

testsuite/ki72-safe-links-backup_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # KI-72: --safe-links must be honored in the backup symlink hard-link path.
3 # Verifies: SW-REQ-073
4 #
5 # In make_backup_inner(), when CAN_HARDLINK_SYMLINK is defined (Linux, macOS)
6 # the hard-link fast path link_or_rename() succeeded for a symlink and then
7 # "goto success" skipped the safe_symlinks check that lives in the copy-fallback
8 # path. The net effect was that an escaping symlink already present in the
9 # destination (e.g. "escape -> ../../sensitive") was silently preserved in the
10 # backup area (as "<name>.bak") even when --safe-links was given.
11 #
12 # This forces exactly that backup (a destination symlink is replaced by a
13 # regular file from the source) and asserts:
14 # * WITH --safe-links : the escaping symlink is NOT backed up (no .bak).
15 # * WITHOUT --safe-links : the escaping symlink IS backed up (.bak is a
16 # symlink with the same escaping target).
17 #
18 # Pure local client behaviour: no daemon/root/tcp. RED on the unfixed tree on
19 # any platform whose link_or_rename() can hard-link a symlink (CAN_HARDLINK_SYMLINK);
20 # GREEN everywhere once the check runs before the fast path.
21
22 import os
23 import subprocess
24
25 from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail
26
27 base = SCRATCHDIR / 'ki72-safe-links-backup'
28
29 # The escaping target: at the transfer top level the symlink path is depth 0,
30 # so any leading "../" makes unsafe_symlink() reject it. Use two levels to be
31 # unambiguously out-of-tree regardless of how the receiver names the leaf.
32 ESCAPE_TARGET = '../../sensitive'
33
34
35 def build():
36 """Fresh src/ and dest/ where the next sync must back up dest's escaping
37 symlink (src carries a regular file of the same name that replaces it)."""
38 rmtree(base)
39 src = base / 'src'
40 dest = base / 'dest'
41 src.mkdir(parents=True)
42 dest.mkdir(parents=True)
43 (src / 'keep.txt').write_text('source regular file\n')
44 # A regular file at src that differs from dest's symlink -> forces an update
45 # and therefore a backup of the old dest entry.
46 (src / 'escape').write_text('replacement regular file\n')
47 # The pre-existing escaping symlink in the destination that gets backed up.
48 os.symlink(ESCAPE_TARGET, dest / 'escape')
49 return src, dest
50
51
52 def run(src, dest, *extra):
53 proc = subprocess.run(
54 rsync_argv('-a', '--backup', '--suffix=.bak', *extra,
55 f'{src}/', f'{dest}/'),
56 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
57 return proc
58
59
60 # ---------------------------------------------------------------------------
61 # 1. WITH --safe-links: the unsafe symlink must NOT be backed up.
62 # ---------------------------------------------------------------------------
63 src, dest = build()
64 proc = run(src, dest, '--safe-links')
65 if proc.returncode != 0:
66 test_fail("KI-72: rsync exited %d with --safe-links\n%s%s"
67 % (proc.returncode, proc.stdout, proc.stderr))
68
69 bak = dest / 'escape.bak'
70 if os.path.lexists(bak):
71 test_fail(
72 "KI-72: --safe-links was bypassed on the backup hard-link path: the "
73 "escaping symlink was backed up to %s (target %r) instead of being "
74 "dropped." % (bak, os.readlink(bak) if os.path.islink(bak) else '?'))
75
76 # The replacement regular file must still have been transferred.
77 if not (dest / 'escape').is_file():
78 test_fail("KI-72: the replacement regular file 'escape' was not transferred")
79
80
81 # ---------------------------------------------------------------------------
82 # 2. WITHOUT --safe-links: the escaping symlink IS backed up (positive
83 # control, so the assertion above isn't vacuous -- backups really happen).
84 # ---------------------------------------------------------------------------
85 src, dest = build()
86 proc = run(src, dest)
87 if proc.returncode != 0:
88 test_fail("KI-72: rsync exited %d without --safe-links\n%s%s"
89 % (proc.returncode, proc.stdout, proc.stderr))
90
91 bak = dest / 'escape.bak'
92 if not os.path.islink(bak):
93 test_fail("KI-72: without --safe-links the escaping symlink should have "
94 "been backed up, but %s is missing or not a symlink." % bak)
95 if os.readlink(bak) != ESCAPE_TARGET:
96 test_fail("KI-72: backup symlink points to %r, expected %r"
97 % (os.readlink(bak), ESCAPE_TARGET))
98
99 # ---------------------------------------------------------------------------
100 # 3. WITH --safe-links, a SAFE (in-tree) symlink must STILL be backed up.
101 # The fix skips the backup for an unsafe OR unreadable link; this guards
102 # against it over-blocking and dropping legitimate safe symlinks too.
103 # ---------------------------------------------------------------------------
104 rmtree(base)
105 src = base / 'src'
106 dest = base / 'dest'
107 src.mkdir(parents=True)
108 dest.mkdir(parents=True)
109 (src / 'keep.txt').write_text('source regular file\n')
110 (src / 'link').write_text('replacement regular file\n')
111 # An in-tree (safe) symlink in dest that gets replaced -> must be backed up.
112 os.symlink('keep.txt', dest / 'link')
113
114 proc = run(src, dest, '--safe-links')
115 if proc.returncode != 0:
116 test_fail("KI-72: rsync exited %d backing up a safe symlink\n%s%s"
117 % (proc.returncode, proc.stdout, proc.stderr))
118
119 bak = dest / 'link.bak'
120 if not os.path.islink(bak):
121 test_fail("KI-72: a SAFE symlink was wrongly dropped from the backup area "
122 "under --safe-links (%s missing or not a symlink) -- the fix is "
123 "over-blocking." % bak)
124 if os.readlink(bak) != 'keep.txt':
125 test_fail("KI-72: safe backup symlink points to %r, expected 'keep.txt'"
126 % os.readlink(bak))
127
128 print("ki72-safe-links-backup: --safe-links drops an escaping symlink from the "
129 "backup area, preserves a safe one, while a plain --backup preserves both")