Debian Codegraph @

testsuite/ownership-depth_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Coverage of --groupmap / --chown / --usermap / -o at depth.
3
4 Remapping is applied to a file AND a directory at every level. As root we can
5 remap to an arbitrary uid/gid (root may always chown/chgrp), so the uid side is
6 covered too. As a normal user we can still remap the group to a secondary group
7 we belong to; the uid side then needs root and is skipped.
8 """
9
10 # Rerun under the fleet harness's non-root pass (testsuite/fleettest.py): the uid
11 # remap only runs as root, so a non-root run exercises the group-only path too.
12 fleet_nonroot = True
13
14 import grp
15 import os
16
17 from rsyncfns import (
18 FROMDIR, TODIR,
19 get_rootuid, get_testuid, make_tree, rmtree, rsync_getgroups, run_rsync,
20 test_fail, test_skipped, walk_dirs, walk_files,
21 )
22
23 src = FROMDIR
24 is_root = get_testuid() == get_rootuid()
25 prim = os.getgid()
26
27
28 def seed():
29 rmtree(src)
30 rmtree(TODIR)
31 make_tree(src, depth=3)
32 entries = [p.relative_to(src) for p in (walk_dirs(src) + walk_files(src))]
33 # Normalise the source group so a prim->target remap is observable.
34 for rel in entries:
35 if os.stat(src / rel).st_gid != prim:
36 os.chown(src / rel, -1, prim)
37 return entries
38
39
40 def assert_all(entries, *, gid=None, uid=None, label=''):
41 for rel in entries:
42 st = os.stat(TODIR / rel)
43 if gid is not None and st.st_gid != gid:
44 test_fail(f"{label}: group of {rel} is {st.st_gid}, expected {gid}")
45 if uid is not None and st.st_uid != uid:
46 test_fail(f"{label}: owner of {rel} is {st.st_uid}, expected {uid}")
47
48
49 try:
50 grp.getgrgid(prim)
51 prim_has_name = True
52 except KeyError:
53 prim_has_name = False
54
55
56 if is_root:
57 # Root may assign any numeric id (it need not exist); pick targets that
58 # differ from the source's ids so the remap is observable.
59 target_gid = 1 if prim == 0 else 0
60 target_uid = 1 if get_testuid() == 0 else 0
61
62 entries = seed()
63 run_rsync('-a', f'--groupmap={prim}:{target_gid}', f'{src}/', f'{TODIR}/')
64 assert_all(entries, gid=target_gid, label='--groupmap (root)')
65
66 entries = seed()
67 run_rsync('-a', f'--groupmap=*:{target_gid}', f'{src}/', f'{TODIR}/')
68 assert_all(entries, gid=target_gid, label='--groupmap wildcard (root)')
69
70 if prim_has_name:
71 entries = seed()
72 run_rsync('-a', f'--groupmap=:{target_gid}', f'{src}/', f'{TODIR}/')
73 assert_all(entries, gid=prim, label='--groupmap empty named group (root)')
74
75 entries = seed()
76 run_rsync('-a', '--numeric-ids', f'--groupmap=:{target_gid}',
77 f'{src}/', f'{TODIR}/')
78 assert_all(entries, gid=target_gid, label='--groupmap empty nameless group (root)')
79
80 entries = seed()
81 run_rsync('-a', f'--chown=:{target_gid}', f'{src}/', f'{TODIR}/')
82 assert_all(entries, gid=target_gid, label='--chown group (root)')
83
84 entries = seed()
85 run_rsync('-a', f'--usermap=*:{target_uid}', f'{src}/', f'{TODIR}/')
86 assert_all(entries, uid=target_uid, label='--usermap (root)')
87
88 entries = seed()
89 run_rsync('-a', f'--chown={target_uid}:{target_gid}', f'{src}/', f'{TODIR}/')
90 assert_all(entries, uid=target_uid, gid=target_gid,
91 label='--chown user:group (root)')
92 print("ownership-depth: --groupmap/--chown/--usermap verified at depth (root)")
93 else:
94 groups = [int(g) for g in rsync_getgroups()]
95 secs = [g for g in groups if g != prim]
96 if not secs:
97 test_skipped("non-root with no secondary group to remap to")
98 sec = secs[0]
99
100 entries = seed()
101 run_rsync('-a', f'--groupmap={prim}:{sec}', f'{src}/', f'{TODIR}/')
102 assert_all(entries, gid=sec, label='--groupmap')
103
104 entries = seed()
105 run_rsync('-a', f'--groupmap=*:{sec}', f'{src}/', f'{TODIR}/')
106 assert_all(entries, gid=sec, label='--groupmap wildcard')
107
108 if prim_has_name:
109 entries = seed()
110 run_rsync('-a', f'--groupmap=:{sec}', f'{src}/', f'{TODIR}/')
111 assert_all(entries, gid=prim, label='--groupmap empty named group')
112
113 entries = seed()
114 run_rsync('-a', '--numeric-ids', f'--groupmap=:{sec}', f'{src}/', f'{TODIR}/')
115 assert_all(entries, gid=sec, label='--groupmap empty nameless group')
116
117 entries = seed()
118 run_rsync('-a', f'--chown=:{sec}', f'{src}/', f'{TODIR}/')
119 assert_all(entries, gid=sec, label='--chown group')
120 print("ownership-depth: --groupmap/--chown group remap verified at depth "
121 "(-o/--usermap user remap needs root -- skipped)")