Debian Codegraph @

testsuite/chmod-option_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/chmod-option.test.
3 #
4 # Test --chmod and the daemon-side "incoming chmod = ..." setting.
5 # Covers a 2.6.8 bug where pushing a new directory with --no-perms to a
6 # daemon with an incoming chmod made the daemon mis-classify the dir as
7 # a file for the purposes of applying the incoming chmod.
8
9 import os
10 import shutil
11
12 from rsyncfns import (
13 FROMDIR, SCRATCHDIR, TODIR,
14 build_rsyncd_conf, check_perms, checkit, makepath, rmtree,
15 run_rsync, start_test_daemon, test_fail,
16 )
17
18
19 DAEMON_PORT = 12875
20
21
22 checkdir = SCRATCHDIR / 'check'
23
24 FROMDIR.mkdir(parents=True, exist_ok=True)
25 (FROMDIR / 'name1').write_text("This is the file\n")
26 (FROMDIR / 'name2').write_text("This is the other file\n")
27 (FROMDIR / 'dir1').mkdir()
28 (FROMDIR / 'dir2').mkdir()
29
30 os.chmod(FROMDIR / 'name1', 0o4700)
31 os.chmod(FROMDIR / 'dir1', 0o700)
32 os.chmod(FROMDIR / 'dir2', 0o770)
33
34 # Baseline copy of source.
35 checkit(['-avv', f'{FROMDIR}/', f'{checkdir}/'], FROMDIR, checkdir)
36
37 # Pin umask to 002 for the rest of the test and DO NOT restore it: rsync's
38 # --chmod `D+w` honours the process umask, so the expected tree (built just
39 # below) and the rsync run that follows must use the same umask -- exactly
40 # as the shell test did (it set `umask 002` and left it in effect). Without
41 # this the test fails under a different ambient umask (e.g. 077).
42 os.umask(0o002)
43
44 # Manually apply the mode transform that --chmod ug-s,a+rX,D+w should
45 # produce on the destination, then verify rsync's transform matches.
46 for entry in checkdir.iterdir():
47 # ug-s,a+rX: clear setuid/setgid; add r everywhere; add x where
48 # any existing x or the entry is a dir.
49 st = entry.stat()
50 mode = st.st_mode & ~0o6000
51 mode |= 0o444 # a+r
52 if entry.is_dir() or (st.st_mode & 0o111):
53 mode |= 0o111 # a+X
54 os.chmod(entry, mode)
55 # `chmod +w` with no explicit who: adds w for every category not masked by
56 # the current umask. Under umask 002 that's u+w AND g+w.
57 plus_w = 0o222 & ~0o002
58 for d in (checkdir, checkdir / 'dir1', checkdir / 'dir2'):
59 st = d.stat()
60 os.chmod(d, st.st_mode | plus_w)
61
62 checkit(['-avv', '--chmod', 'ug-s,a+rX,D+w', f'{FROMDIR}/', f'{TODIR}/'],
63 checkdir, TODIR)
64
65 def check_permcopy(chmod_arg, start_mode, expected, is_dir=False):
66 rmtree(FROMDIR)
67 rmtree(TODIR)
68 makepath(FROMDIR)
69 permcopy = FROMDIR / 'permcopy'
70 if is_dir:
71 permcopy.mkdir()
72 else:
73 permcopy.write_text('permcopy\n')
74 os.chmod(permcopy, start_mode)
75 run_rsync('-avv', f'--chmod={chmod_arg}', f'{FROMDIR}/', f'{TODIR}/')
76 check_perms(TODIR / 'permcopy', expected)
77
78
79 # Exercise chmod(1)-style permission copies.
80 check_permcopy('g=o,o=', 0o647, 'rw-rwx---')
81 check_permcopy('g=u', 0o741, 'rwxrwx--x')
82 check_permcopy('g-o', 0o775, 'rwx-w-r-x')
83 check_permcopy('u=g', 0o4755, 'r-xr-xr-x')
84 check_permcopy('g=u', 0o2755, 'rwxrwxr-x')
85 check_permcopy('o=u', 0o1750, 'rwxr-xrwx', is_dir=True)
86
87 rmtree(FROMDIR)
88 rmtree(TODIR)
89 makepath(FROMDIR)
90 (FROMDIR / 'permcopy').write_text('permcopy\n')
91 proc = run_rsync('-avv', '--chmod=g=ur', f'{FROMDIR}/', f'{TODIR}/',
92 check=False, capture_output=True)
93 if proc.returncode == 0:
94 test_fail('--chmod=g=ur was not rejected')
95
96 # Now exercise the F-only chmod path.
97 rmtree(FROMDIR)
98 rmtree(checkdir)
99 rmtree(TODIR)
100 makepath(TODIR, FROMDIR / 'foo')
101 (FROMDIR / 'bar').touch()
102
103 checkit(['-avv', f'{FROMDIR}/', f'{checkdir}/'], FROMDIR, checkdir)
104 os.chmod(FROMDIR / 'bar', (FROMDIR / 'bar').stat().st_mode | 0o001) # o+x
105
106 checkit(['-avv', '--chmod=Fo-x', f'{FROMDIR}/', f'{TODIR}/'], checkdir, TODIR)
107
108 # 2.6.8 regression: pushing a new directory via --no-perms to a daemon
109 # with an "incoming chmod" once mis-classified the directory as a file.
110 conf = build_rsyncd_conf()
111 with open(conf, 'a') as f:
112 f.write(f"""
113 [test-incoming-chmod]
114 \tpath = {TODIR}
115 \tread only = no
116 \tincoming chmod = Fo-x
117 """)
118
119 url = start_test_daemon(conf, DAEMON_PORT)
120
121 rmtree(TODIR)
122 makepath(TODIR)
123
124 checkit(['-avv', '--no-perms', f'{FROMDIR}/',
125 f'{url}test-incoming-chmod/'],
126 checkdir, TODIR, allowed_codes=(0, 23))