Debian Codegraph @

testsuite/symlink-dirlink-basis_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/symlink-dirlink-basis.test.
3 #
4 # Regression test for https://github.com/RsyncProject/rsync/issues/715:
5 # updating a file through a directory symlink with -K (--copy-dirlinks)
6 # regressed after the CVE-2026-29518 fix introduced
7 # secure_relative_open() with O_NOFOLLOW on every path component. The
8 # fix split the path so basedir (dirname) follows symlinks while only
9 # the final component is O_NOFOLLOW'd.
10 #
11 # We exercise: basic dir-symlink update, compressed update, nested
12 # dir-symlinks, --backup, --inplace, top-level file (no split needed),
13 # --partial-dir with protocol < 29, and a basic protocol < 29 transfer.
14
15 import filecmp
16 import os
17 import subprocess
18 import time
19
20 from rsyncfns import (
21 RSYNC, SCRATCHDIR, RSYNC_PEER, SRCDIR, TMPDIR,
22 make_data_file, rsync_argv, test_fail, rsync_path_arg, rsh_cmd,
23 )
24
25
26 # The secure resolver follows an in-tree directory symlink (the basis dir) on
27 # every platform: the per-component walk readlinks the symlink and walks its
28 # target off held dirfds (issue #715), so this transfer through a dir-symlinked
29 # basis succeeds uniformly.
30
31 os.environ['RSYNC_RSH'] = rsh_cmd()
32 # HOME -> SCRATCHDIR is set up by rsyncfns import.
33
34 srcbase = TMPDIR / 'src_files' # avoid clash with the runner's $scratchdir/src symlink
35 srcbase.mkdir(parents=True, exist_ok=True)
36 home = SCRATCHDIR
37
38
39 def make_testfile(path) -> None:
40 """~32 KiB of non-trivial content -- large enough to trigger rsync's
41 block-matching delta path."""
42 make_data_file(path, 32768)
43
44
45 def push(*args, label: str) -> None:
46 # --rsync-path goes BEFORE the positional source/dest args, matching
47 # the shell test's order. With it at the end rsync mis-classifies
48 # the destination and reports "Unexpected remote arg: localhost:".
49 saved = os.getcwd()
50 os.chdir(srcbase)
51 try:
52 proc = subprocess.run(
53 rsync_argv(f'--rsync-path={rsync_path_arg()}', *args),
54 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
55 )
56 print(proc.stdout, end='')
57 if proc.returncode != 0:
58 test_fail(f"{label}: rsync exited {proc.returncode}")
59 finally:
60 os.chdir(saved)
61
62
63 def assert_same(label: str, a, b) -> None:
64 if not filecmp.cmp(a, b, shallow=False):
65 test_fail(f"{label}: content mismatch between {a} and {b}")
66
67
68 # Test 1: basic directory-symlink update.
69 (home / 'real-dir').mkdir()
70 os.symlink('real-dir', home / 'dir')
71 (srcbase / 'dir').mkdir()
72 make_testfile(srcbase / 'dir' / 'file')
73
74 push('-KRlptv', 'dir/file', 'localhost:', label="test 1 initial")
75 if not (home / 'real-dir' / 'file').is_file():
76 test_fail("test 1: initial transfer did not create file through symlink")
77 assert_same("test 1 initial", srcbase / 'dir' / 'file', home / 'real-dir' / 'file')
78
79 # Trigger delta transfer.
80 with open(srcbase / 'dir' / 'file', 'ab') as f:
81 f.write(b"appended update\n")
82 time.sleep(1)
83 (srcbase / 'dir' / 'file').touch()
84
85 push('-KRlptv', 'dir/file', 'localhost:', label="test 1 update")
86 assert_same("test 1 update", srcbase / 'dir' / 'file', home / 'real-dir' / 'file')
87
88
89 # Test 2: compression.
90 with open(srcbase / 'dir' / 'file', 'ab') as f:
91 f.write(b"another line\n")
92 time.sleep(1)
93 (srcbase / 'dir' / 'file').touch()
94
95 push('-KRlptzv', 'dir/file', 'localhost:', label="test 2")
96 assert_same("test 2", srcbase / 'dir' / 'file', home / 'real-dir' / 'file')
97
98
99 # Test 3: nested directory symlinks.
100 (home / 'nested_real' / 'sub').mkdir(parents=True)
101 os.symlink('nested_real', home / 'nested')
102
103 (srcbase / 'nested' / 'sub').mkdir(parents=True)
104 make_testfile(srcbase / 'nested' / 'sub' / 'data.txt')
105
106 push('-KRlptv', 'nested/sub/data.txt', 'localhost:', label="test 3 initial")
107
108 with open(srcbase / 'nested' / 'sub' / 'data.txt', 'ab') as f:
109 f.write(b"appended nested\n")
110 time.sleep(1)
111 (srcbase / 'nested' / 'sub' / 'data.txt').touch()
112
113 push('-KRlptv', 'nested/sub/data.txt', 'localhost:', label="test 3 update")
114 assert_same("test 3 update",
115 srcbase / 'nested' / 'sub' / 'data.txt',
116 home / 'nested_real' / 'sub' / 'data.txt')
117
118
119 # Test 4: --backup with directory symlinks.
120 (home / 'real-dir' / 'file').unlink()
121 (home / 'real-dir' / 'file~').unlink(missing_ok=True)
122 make_testfile(srcbase / 'dir' / 'file')
123
124 push('-KRlptv', 'dir/file', 'localhost:', label="test 4 initial")
125
126 old_content = (srcbase / 'dir' / 'file').read_bytes() # the backup should hold this
127 with open(srcbase / 'dir' / 'file', 'ab') as f:
128 f.write(b"backup update\n")
129 time.sleep(1)
130 (srcbase / 'dir' / 'file').touch()
131
132 push('-KRlptv', '--backup', 'dir/file', 'localhost:', label="test 4 update")
133 assert_same("test 4 update", srcbase / 'dir' / 'file', home / 'real-dir' / 'file')
134 if not (home / 'real-dir' / 'file~').is_file():
135 test_fail("test 4: backup file was not created")
136 # The backup must hold the OLD content, not just exist.
137 if (home / 'real-dir' / 'file~').read_bytes() != old_content:
138 test_fail("test 4: backup file~ does not hold the pre-update content")
139
140
141 # Test 5: --inplace.
142 (home / 'real-dir' / 'file').unlink()
143 (home / 'real-dir' / 'file~').unlink(missing_ok=True)
144 make_testfile(srcbase / 'dir' / 'file')
145
146 push('-KRlptv', '--inplace', 'dir/file', 'localhost:', label="test 5 initial")
147
148 with open(srcbase / 'dir' / 'file', 'ab') as f:
149 f.write(b"inplace update\n")
150 time.sleep(1)
151 (srcbase / 'dir' / 'file').touch()
152
153 push('-KRlptv', '--inplace', 'dir/file', 'localhost:', label="test 5 update")
154 assert_same("test 5 update", srcbase / 'dir' / 'file', home / 'real-dir' / 'file')
155
156
157 # Test 6: top-level file (no dirname split needed).
158 make_testfile(srcbase / 'topfile')
159 home.mkdir(parents=True, exist_ok=True)
160
161 push('-Rlptv', 'topfile', 'localhost:', label="test 6 initial")
162
163 with open(srcbase / 'topfile', 'ab') as f:
164 f.write(b"toplevel update\n")
165 time.sleep(1)
166 (srcbase / 'topfile').touch()
167
168 push('-Rlptv', 'topfile', 'localhost:', label="test 6 update")
169 assert_same("test 6 update", srcbase / 'topfile', home / 'topfile')
170
171
172 # Test 7: --partial-dir with protocol < 29.
173 (home / 'real-dir' / 'file').unlink(missing_ok=True)
174 make_testfile(srcbase / 'dir' / 'file')
175
176 push('-KRlptv', '--protocol=28', '--partial-dir=.rsync-partial',
177 'dir/file', 'localhost:', label="test 7 initial")
178
179 with open(srcbase / 'dir' / 'file', 'ab') as f:
180 f.write(b"partial-dir update\n")
181 time.sleep(1)
182 (srcbase / 'dir' / 'file').touch()
183
184 push('-KRlptv', '--protocol=28', '--partial-dir=.rsync-partial',
185 'dir/file', 'localhost:', label="test 7 update")
186 assert_same("test 7 update", srcbase / 'dir' / 'file', home / 'real-dir' / 'file')
187
188
189 # Test 8: protocol < 29 basic update.
190 (home / 'real-dir' / 'file').unlink(missing_ok=True)
191 make_testfile(srcbase / 'dir' / 'file')
192
193 push('-KRlptv', '--protocol=28', 'dir/file', 'localhost:', label="test 8 initial")
194
195 with open(srcbase / 'dir' / 'file', 'ab') as f:
196 f.write(b"proto28 update\n")
197 time.sleep(1)
198 (srcbase / 'dir' / 'file').touch()
199
200 push('-KRlptv', '--protocol=28', 'dir/file', 'localhost:', label="test 8 update")
201 assert_same("test 8 update", srcbase / 'dir' / 'file', home / 'real-dir' / 'file')