Debian Codegraph @

testsuite/acls-default_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/acls-default.test.
3 #
4 # Test that rsync obeys POSIX default ACLs on the destination's parent
5 # directory when creating the transfer's container directory, even
6 # though that parent is outside the transfer itself.
7
8 import os
9 import re
10 import shlex
11 import subprocess
12
13 from rsyncfns import (
14 SCRATCHDIR,
15 check_perms, run_rsync, test_fail, test_skipped,
16 )
17
18
19 vv = run_rsync('-VV', check=True, capture_output=True)
20 if '"ACLs": true' not in vv.stdout:
21 test_skipped("Rsync is configured without ACL support")
22
23 setfacl_nodef = os.environ.get('setfacl_nodef', 'true')
24 if setfacl_nodef == 'true':
25 test_skipped("I don't know how to use your setfacl command")
26
27 if '-k' in setfacl_nodef.split():
28 seed_opts = ['-dm', 'u::7,g::5,o:5']
29 else:
30 seed_opts = ['-m', 'd:u::7,d:g::5,d:o:5']
31
32 # Seed the scratch dir with a default ACL so the upcoming testit() runs
33 # inherit a known-base; if setfacl rejects this the FS doesn't have ACLs.
34 proc = subprocess.run(['setfacl', *seed_opts, str(SCRATCHDIR)])
35 if proc.returncode != 0:
36 test_skipped("Your filesystem has ACLs disabled")
37
38
39 def testit(dirname, default_acl, file_expected, prog_expected):
40 """Set a default ACL on a destination parent dir, then verify that
41 a transfer into a fresh subdir picks up the inherited perms."""
42 todir = SCRATCHDIR / dirname
43 todir.mkdir()
44 # Clear any inherited default ACL first -- and confirm it succeeded, so the
45 # no-default-ACL cases can't silently inherit the scratch dir's seeded
46 # default ACL and test the wrong base state.
47 if subprocess.run(shlex.split(setfacl_nodef) + [str(todir)]).returncode != 0:
48 test_fail(f"{dirname}: clearing the inherited default ACL failed")
49 if default_acl:
50 if '-k' in setfacl_nodef.split():
51 opts = ['-dm', default_acl]
52 else:
53 # Each "u:/g:/o:/m:" prefix becomes "d:u:/d:g:/d:o:/d:m:".
54 translated = re.sub(r'([ugom]:)', r'd:\1', default_acl)
55 opts = ['-m', translated]
56 subprocess.run(['setfacl', *opts, str(todir)], check=True)
57
58 run_rsync('-rvv',
59 str(SCRATCHDIR / 'dir'),
60 str(SCRATCHDIR / 'file'),
61 str(SCRATCHDIR / 'program'),
62 f'{todir}/to/')
63
64 check_perms(todir / 'to', prog_expected)
65 check_perms(todir / 'to' / 'dir', prog_expected)
66 check_perms(todir / 'to' / 'file', file_expected)
67 check_perms(todir / 'to' / 'program', prog_expected)
68
69 # get_local_name shouldn't mess up a single-file transfer.
70 run_rsync('-rvv',
71 str(SCRATCHDIR / 'file'),
72 f'{todir}/to/anotherfile')
73 check_perms(todir / 'to' / 'anotherfile', file_expected)
74
75 # And the no-regular-file case (sole-dir transfer).
76 run_rsync('-rvv',
77 f'{SCRATCHDIR / "dir"}/',
78 f'{todir}/to/anotherdir/')
79 check_perms(todir / 'to' / 'anotherdir', prog_expected)
80
81
82 (SCRATCHDIR / 'dir').mkdir()
83 (SCRATCHDIR / 'file').write_text("File!\n")
84 (SCRATCHDIR / 'program').write_text("#!/bin/sh\n")
85 os.chmod(SCRATCHDIR / 'dir', 0o777)
86 os.chmod(SCRATCHDIR / 'file', 0o666)
87 os.chmod(SCRATCHDIR / 'program', 0o777)
88
89 os.umask(0o077)
90 testit('da777', 'u::7,g::7,o:7', 'rw-rw-rw-', 'rwxrwxrwx')
91 testit('da775', 'u::7,g::7,o:5', 'rw-rw-r--', 'rwxrwxr-x')
92 testit('da750', 'u::7,g::5,o:0', 'rw-r-----', 'rwxr-x---')
93 testit('da750mask', 'u::7,u:0:7,g::7,m:5,o:0', 'rw-r-----', 'rwxr-x---')
94 testit('noda1', '', 'rw-------', 'rwx------')
95 os.umask(0o000)
96 testit('noda2', '', 'rw-rw-rw-', 'rwxrwxrwx')
97 os.umask(0o022)
98 testit('noda3', '', 'rw-r--r--', 'rwxr-xr-x')