Debian Codegraph @

testsuite/unsafe-links_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/unsafe-links.test.
3 #
4 # Verifies the three relevant policies for "unsafe" (escape-the-tree) symlinks:
5 # * default -a copies them as symlinks (no special handling),
6 # * --copy-links materialises ALL symlinks as files,
7 # * --copy-unsafe-links materialises only the unsafe ones, leaving safe
8 # in-tree symlinks as symlinks.
9
10 import os
11
12 from rsyncfns import TMPDIR, is_a_link, rmtree, run_rsync, test_fail
13
14
15 def assert_symlink(path, target):
16 if not is_a_link(path):
17 test_fail(f"File {path} is not a symlink")
18 actual = os.readlink(path)
19 if actual != target:
20 test_fail(f"symlink {path} target is {actual!r}, expected {target!r}")
21
22
23 def assert_regular(path):
24 if not os.path.isfile(path):
25 test_fail(f"File {path} is not regular file or not exists")
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
44
45 print("rsync with relative path and just -a")
46 run_rsync('-avv', 'from/safe/', 'to')
47 assert_symlink("to/links/file1", "../files/file1")
48 assert_symlink("to/links/file2", "../files/file2")
49 assert_symlink("to/links/unsafefile", "../../unsafe/unsafefile")
50
51 print("rsync with relative path and -a --copy-links")
52 run_rsync('-avv', '--copy-links', 'from/safe/', 'to')
53 assert_regular("to/links/file1")
54 assert_regular("to/links/file2")
55 assert_regular("to/links/unsafefile")
56
57 print("rsync with relative path and --copy-unsafe-links")
58 run_rsync('-avv', '--copy-unsafe-links', 'from/safe/', 'to')
59 assert_symlink("to/links/file1", "../files/file1")
60 assert_symlink("to/links/file2", "../files/file2")
61 assert_regular("to/links/unsafefile")
62
63 rmtree("to")
64 print("rsync with relative2 path")
65 # Mirror the shell `(cd from; rsync ... safe/ ../to)` subshell — chdir, rsync,
66 # then chdir back so the final block uses an absolute source again.
67 saved = os.getcwd()
68 os.chdir("from")
69 try:
70 run_rsync('-avv', '--copy-unsafe-links', 'safe/', '../to')
71 finally:
72 os.chdir(saved)
73 assert_symlink("to/links/file1", "../files/file1")
74 assert_symlink("to/links/file2", "../files/file2")
75 assert_regular("to/links/unsafefile")
76
77 rmtree("to")
78 print("rsync with absolute path")
79 run_rsync('-avv', '--copy-unsafe-links', f'{os.getcwd()}/from/safe/', 'to')
80 assert_symlink("to/links/file1", "../files/file1")
81 assert_symlink("to/links/file2", "../files/file2")
82 assert_regular("to/links/unsafefile")