Debian Codegraph @

testsuite/safe-links_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/safe-links.test.
3 #
4 # --safe-links must drop symlinks whose target escapes the transfer root.
5 # In-tree symlinks survive; escape-attempt symlinks (../../..., a/a/../../...)
6 # must NOT appear at the destination at all.
7
8 import os
9
10 from rsyncfns import TMPDIR, is_a_link, run_rsync, test_fail
11
12
13 def assert_symlink(path, target):
14 if not is_a_link(path):
15 test_fail(f"File {path} is not a symlink")
16 actual = os.readlink(path)
17 if actual != target:
18 test_fail(f"symlink {path} target is {actual!r}, expected {target!r}")
19
20
21 def assert_notexist(path):
22 if os.path.exists(path):
23 test_fail(f"File {path} exists")
24 if os.path.islink(path):
25 test_fail(f"File {path} exists as a symlink")
26
27
28 os.chdir(TMPDIR)
29
30 os.mkdir("from")
31 os.mkdir("from/safe")
32 os.mkdir("from/unsafe")
33 os.mkdir("from/safe/files")
34 os.mkdir("from/safe/links")
35
36 open("from/safe/files/file1", "w").close()
37 open("from/safe/files/file2", "w").close()
38 open("from/unsafe/unsafefile", "w").close()
39
40 os.symlink("../files/file1", "from/safe/links/file1")
41 os.symlink("../files/file2", "from/safe/links/file2")
42 os.symlink("../../unsafe/unsafefile", "from/safe/links/unsafefile")
43 os.symlink("a/a/a/../../../unsafe2", "from/safe/links/unsafe2")
44
45 print("rsync with relative path and --safe-links")
46 run_rsync('-avv', '--safe-links', 'from/safe/', 'to')
47
48 assert_symlink("to/links/file1", "../files/file1")
49 assert_symlink("to/links/file2", "../files/file2")
50 assert_notexist("to/links/unsafefile")
51 assert_notexist("to/links/unsafe2")