]> arthur.barton.de Git - bup.git/blob - t/test-restore-map-owner.sh
Avoid uid/gid 0 metadata tests when ids don't exist
[bup.git] / t / test-restore-map-owner.sh
1 #!/usr/bin/env bash
2 . ./wvtest-bup.sh
3
4 if [ $(t/root-status) != root ]; then
5     echo 'Not root: skipping restore --map-* tests.'
6     exit 0 # FIXME: add WVSKIP.
7 fi
8
9 top="$(WVPASS pwd)" || exit $?
10 tmpdir="$(WVPASS wvmktempdir)" || exit $?
11 export BUP_DIR="$tmpdir/bup"
12
13 bup() { "$top/bup" "$@"; }
14
15 uid=$(WVPASS id -u) || exit $?
16 user=$(WVPASS id -un) || exit $?
17 gid=$(WVPASS id -g) || exit $?
18 group=$(WVPASS id -gn) || exit $?
19
20 other_uinfo=$(WVPASS t/id-other-than --user "$user") || exit $?
21 other_user="${other_uinfo%%:*}"
22 other_uid="${other_uinfo##*:}"
23
24 other_ginfo=$(WVPASS t/id-other-than --group "$group" 0) || exit $?
25 other_group="${other_ginfo%%:*}"
26 other_gid="${other_ginfo##*:}"
27
28 WVPASS bup init
29 WVPASS cd "$tmpdir"
30
31 WVSTART "restore --map-user/group/uid/gid (control)"
32 WVPASS mkdir src
33 WVPASS touch src/foo
34 # Some systems assign the parent dir group to new paths.
35 WVPASS chgrp -R "$group" src
36 WVPASS bup index src
37 WVPASS bup save -n src src
38 WVPASS bup restore -C dest "src/latest/$(pwd)/src/"
39 WVPASS bup xstat dest/foo > foo-xstat
40 WVPASS grep -qE "^user: $user\$" foo-xstat
41 WVPASS grep -qE "^uid: $uid\$" foo-xstat
42 WVPASS grep -qE "^group: $group\$" foo-xstat
43 WVPASS grep -qE "^gid: $gid\$" foo-xstat
44
45 WVSTART "restore --map-user/group/uid/gid (user/group)"
46 WVPASS rm -rf dest
47 # Have to remap uid/gid too because we're root and 0 would win).
48 WVPASS bup restore -C dest \
49     --map-uid "$uid=$other_uid" --map-gid "$gid=$other_gid" \
50     --map-user "$user=$other_user" --map-group "$group=$other_group" \
51     "src/latest/$(pwd)/src/"
52 WVPASS bup xstat dest/foo > foo-xstat
53 WVPASS grep -qE "^user: $other_user\$" foo-xstat
54 WVPASS grep -qE "^uid: $other_uid\$" foo-xstat
55 WVPASS grep -qE "^group: $other_group\$" foo-xstat
56 WVPASS grep -qE "^gid: $other_gid\$" foo-xstat
57
58 WVSTART "restore --map-user/group/uid/gid (user/group trumps uid/gid)"
59 WVPASS rm -rf dest
60 WVPASS bup restore -C dest \
61     --map-uid "$uid=$other_uid" --map-gid "$gid=$other_gid" \
62     "src/latest/$(pwd)/src/"
63 # Should be no changes.
64 WVPASS bup xstat dest/foo > foo-xstat
65 WVPASS grep -qE "^user: $user\$" foo-xstat
66 WVPASS grep -qE "^uid: $uid\$" foo-xstat
67 WVPASS grep -qE "^group: $group\$" foo-xstat
68 WVPASS grep -qE "^gid: $gid\$" foo-xstat
69
70 WVSTART "restore --map-user/group/uid/gid (uid/gid)"
71 WVPASS rm -rf dest
72 WVPASS bup restore -C dest \
73     --map-user "$user=" --map-group "$group=" \
74     --map-uid "$uid=$other_uid" --map-gid "$gid=$other_gid" \
75     "src/latest/$(pwd)/src/"
76 WVPASS bup xstat dest/foo > foo-xstat
77 WVPASS grep -qE "^user: $other_user\$" foo-xstat
78 WVPASS grep -qE "^uid: $other_uid\$" foo-xstat
79 WVPASS grep -qE "^group: $other_group\$" foo-xstat
80 WVPASS grep -qE "^gid: $other_gid\$" foo-xstat
81
82 has_uid_gid_0=$(WVPASS python -c "
83 import grp, pwd
84 try:
85   pwd.getpwuid(0)
86   grp.getgrgid(0)
87   print 'yes'
88 except KeyError, ex:
89   pass
90 " 2>/dev/null) || exit $?
91 if [ "$has_uid_gid_0" == yes ]
92 then
93     WVSTART "restore --map-user/group/uid/gid (zero uid/gid trumps all)"
94     WVPASS rm -rf dest
95     WVPASS bup restore -C dest \
96         --map-user "$user=$other_user" --map-group "$group=$other_group" \
97         --map-uid "$uid=0" --map-gid "$gid=0" \
98         "src/latest/$(pwd)/src/"
99     WVPASS bup xstat dest/foo > foo-xstat
100     WVPASS grep -qE "^uid: 0\$" foo-xstat
101     WVPASS grep -qE "^gid: 0\$" foo-xstat
102
103     WVPASS rm -rf "$tmpdir"
104 fi