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