Debian Codegraph @

testsuite/unsafe-byname_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/unsafe-byname.test.
3 #
4 # Call directly into the t_unsafe helper (which wraps unsafe_symlink()) and
5 # verify its judgement on a battery of crafted target/curdir pairs.
6
7 import os
8 import subprocess
9
10 from rsyncfns import TOOLDIR, test_fail
11
12
13 t_unsafe = str(TOOLDIR / 't_unsafe')
14
15 # `pwd` in the shell version is the cwd of the test process — the runner
16 # starts each test in TOOLDIR. os.getcwd() reproduces that exactly.
17 pwd = os.getcwd()
18
19 # (link_target, curdir, expected) — expected is "safe" or "unsafe".
20 CASES = [
21 ('file', 'from', 'safe'),
22 ('dir/file', 'from', 'safe'),
23 ('dir/./file', 'from', 'safe'),
24 ('dir/.', 'from', 'safe'),
25 ('dir/', 'from', 'safe'),
26
27 ('/etc/passwd', 'from', 'unsafe'),
28 ('//../etc/passwd', 'from', 'unsafe'),
29 ('//./etc/passwd', 'from', 'unsafe'),
30
31 ('./foo', 'from', 'safe'),
32 ('../foo', 'from', 'unsafe'),
33 ('./../foo', 'from', 'unsafe'),
34 ('.//../foo', 'from', 'unsafe'),
35 ('./../foo', 'from/..', 'unsafe'),
36 ('../dest', 'from/dir', 'safe'),
37 ('../../dest', 'from//dir', 'unsafe'),
38 ('..//../dest', 'from/dir', 'unsafe'),
39
40 ('..', 'from/file', 'safe'),
41 ('../..', 'from/file', 'unsafe'),
42 ('..//..', 'from//file', 'unsafe'),
43 ('dir/..', 'from', 'unsafe'),
44 ('dir/../..', 'from', 'unsafe'),
45 ('dir/..//..', 'from', 'unsafe'),
46
47 ('', 'from', 'unsafe'),
48
49 # Based on tests from unsafe-links by Vladimir Michl.
50 ('../../unsafe/unsafefile', 'from/safe', 'unsafe'),
51 ('..//../unsafe/unsafefile', 'from/safe', 'unsafe'),
52 ('../files/file1', 'from/safe', 'safe'),
53
54 ('../../unsafe/unsafefile', 'safe', 'unsafe'),
55 ('../files/file1', 'safe', 'unsafe'),
56
57 ('../../unsafe/unsafefile', f'{pwd}/from/safe', 'safe'),
58 ('../files/file1', f'{pwd}/from/safe', 'safe'),
59 ]
60
61
62 failures = []
63 for target, curdir, expected in CASES:
64 proc = subprocess.run(
65 [t_unsafe, target, curdir],
66 capture_output=True, text=True,
67 )
68 if proc.returncode != 0:
69 test_fail(f"Failed to check {target!r} {curdir!r}: exit {proc.returncode}")
70 got = proc.stdout.strip()
71 if got != expected:
72 failures.append(
73 f"t_unsafe {target!r} {curdir!r} returned {got!r}, expected {expected!r}"
74 )
75
76 if failures:
77 test_fail("\n".join(failures))