5 export BUP_DIR="$TOP/buptest.tmp"
12 # Very simple metadata tests -- create a test tree then check that bup
13 # meta can reproduce the metadata correctly (according to bup xstat)
14 # via create, extract, start-extract, and finish-extract. The current
15 # tests are crude, and this does not fully test devices, varying
16 # users/groups, acls, attrs, etc.
21 export PATH="$TOP:$PATH" # pick up bup
22 # Skip atime (test elsewhere) to avoid the observer effect.
23 find . | sort | xargs bup xstat --exclude-fields ctime,atime,size
29 test "$(whoami)" == root -a -z "$FAKEROOTKEY"
34 if ! actually-root; then
37 # Go to greater lengths to deal with any test detritus.
39 test -e "$@" || continue
40 chattr -fR = "$@" || true
53 trap "rm -rf ${tmpfile}" EXIT
54 rsync -ni -aHAX "$1" "$2" > "${tmpfile}"
55 if test $(wc -l < "${tmpfile}") != 0; then
56 echo "ERROR: detected differences between $1 and $2"
63 test-src-create-extract()
65 # Test bup meta create/extract for ./src -> ./src-restore.
66 # Also writes to ./src-stat and ./src-restore-stat.
68 (cd src && WVPASS genstat) > src-stat
69 WVPASS bup meta --create --recurse --file src.meta src
71 force-delete src-restore
74 WVPASS bup meta --extract --file ../src.meta
76 (cd src && genstat >../../src-restore-stat) || WVFAIL
77 WVPASS diff -U5 ../src-stat ../src-restore-stat
78 # Test start/finish extract.
80 WVPASS bup meta --start-extract --file ../src.meta
82 WVPASS bup meta --finish-extract --file ../src.meta
83 (cd src && genstat >../../src-restore-stat) || WVFAIL
84 WVPASS diff -U5 ../src-stat ../src-restore-stat
88 test-src-save-restore()
90 # Test bup save/restore metadata for ./src -> ./src-restore. Also
91 # writes to ./src.bup. Note that for now this just tests the
92 # restore below src/, in order to avoid having to worry about
93 # operations that require root (like chown /home).
98 export BUP_DIR=$(pwd)/src.bup
101 WVPASS bup save -t -n src src
103 force-delete src-restore
105 WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
106 WVPASS test -d src-restore/src
107 WVPASS compare-trees src/ src-restore/src/
113 if actually-root; then
114 umount "$TOP/bupmeta.tmp/testfs" || true
120 force-delete "$BUP_DIR"
121 force-delete "$TOP/bupmeta.tmp"
122 mkdir -p "$TOP/bupmeta.tmp/src"
123 cp -pPR Documentation cmd lib t "$TOP/bupmeta.tmp"/src
125 # Regression test for metadata sort order. Previously, these two
126 # entries would sort in the wrong order because the metadata
127 # entries were being sorted by mangled name, but the index isn't.
128 dd if=/dev/zero of="$TOP/bupmeta.tmp"/src/foo bs=1k count=33
129 touch -d 2011-11-11 "$TOP/bupmeta.tmp"/src/foo
130 touch -d 2011-12-12 "$TOP/bupmeta.tmp"/src/foo-bar
132 t/mksock "$TOP/bupmeta.tmp/src/test-socket" || true
135 # Use the test tree to check bup meta.
136 WVSTART 'meta --create/--extract'
139 cd "$TOP/bupmeta.tmp"
140 test-src-create-extract
142 # Test a top-level file (not dir).
144 WVPASS bup meta -cf src-file.meta src-file
147 WVPASS bup meta -xf ../src-file.meta
150 # Use the test tree to check bup save/restore metadata.
151 WVSTART 'metadata save/restore (general)'
154 cd "$TOP/bupmeta.tmp"
155 test-src-save-restore
158 WVSTART 'meta --edit'
160 force-delete "$TOP/bupmeta.tmp"
161 mkdir "$TOP/bupmeta.tmp"
162 cd "$TOP/bupmeta.tmp"
164 WVPASS bup meta -cf src.meta src
166 WVPASS bup meta --edit --set-uid 0 src.meta | WVPASS bup meta -tvvf - \
167 | WVPASS grep -qE '^uid: 0'
168 WVPASS bup meta --edit --set-uid 1000 src.meta | WVPASS bup meta -tvvf - \
169 | WVPASS grep -qE '^uid: 1000'
171 WVPASS bup meta --edit --set-gid 0 src.meta | WVPASS bup meta -tvvf - \
172 | WVPASS grep -qE '^gid: 0'
173 WVPASS bup meta --edit --set-gid 1000 src.meta | WVPASS bup meta -tvvf - \
174 | WVPASS grep -qE '^gid: 1000'
176 WVPASS bup meta --edit --set-user foo src.meta | WVPASS bup meta -tvvf - \
177 | WVPASS grep -qE '^user: foo'
178 WVPASS bup meta --edit --set-user bar src.meta | WVPASS bup meta -tvvf - \
179 | WVPASS grep -qE '^user: bar'
180 WVPASS bup meta --edit --unset-user src.meta | WVPASS bup meta -tvvf - \
181 | WVPASS grep -qE '^user:'
182 WVPASS bup meta --edit --set-user bar --unset-user src.meta \
183 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user:'
184 WVPASS bup meta --edit --unset-user --set-user bar src.meta \
185 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user: bar'
187 WVPASS bup meta --edit --set-group foo src.meta | WVPASS bup meta -tvvf - \
188 | WVPASS grep -qE '^group: foo'
189 WVPASS bup meta --edit --set-group bar src.meta | WVPASS bup meta -tvvf - \
190 | WVPASS grep -qE '^group: bar'
191 WVPASS bup meta --edit --unset-group src.meta | WVPASS bup meta -tvvf - \
192 | WVPASS grep -qE '^group:'
193 WVPASS bup meta --edit --set-group bar --unset-group src.meta \
194 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^group:'
195 WVPASS bup meta --edit --unset-group --set-group bar src.meta \
196 | WVPASS bup meta -tvvf - | grep -qE '^group: bar'
199 # Test ownership restoration (when not root or fakeroot).
201 if test "$(whoami)" == root; then
205 WVSTART 'metadata (restoration of ownership)'
206 force-delete "$TOP/bupmeta.tmp"
207 mkdir "$TOP/bupmeta.tmp"
208 cd "$TOP/bupmeta.tmp"
210 WVPASS bup meta -cf src.meta src
214 # Make sure we don't change (or try to change) the user when not root.
215 WVPASS bup meta --edit --set-user root ../src.meta | WVPASS bup meta -x
216 WVPASS bup xstat src | WVPASS grep -qvE '^user: root'
218 WVPASS bup meta --edit --unset-user --set-uid 0 ../src.meta \
220 WVPASS bup xstat src | grep -qvE '^user: root'
222 # Make sure we can restore one of the user's groups.
223 user_groups="$(groups)"
224 last_group="$(echo ${user_groups/* /})"
226 WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
228 WVPASS bup xstat src | WVPASS grep -qE "^group: $last_group"
230 # Make sure we can restore one of the user's gids.
232 last_gid="$(echo ${user_gids/* /})"
234 WVPASS bup meta --edit --unset-group --set-gid "$last_gid" ../src.meta \
236 WVPASS bup xstat src | WVPASS grep -qE "^gid: $last_gid"
238 # Test --numeric-ids (gid).
240 current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:')
241 WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
242 | WVPASS bup meta -x --numeric-ids
243 new_gidx=$(bup xstat src | grep -e '^gid:')
244 WVPASSEQ "$current_gidx" "$new_gidx"
246 # Test that restoring an unknown user works.
247 unknown_user=$("$TOP"/t/unknown-owner --user)
249 current_uidx=$(bup meta -tvvf ../src.meta | grep -e '^uid:')
250 WVPASS bup meta --edit --set-user "$unknown_user" ../src.meta \
252 new_uidx=$(bup xstat src | grep -e '^uid:')
253 WVPASSEQ "$current_uidx" "$new_uidx"
255 # Test that restoring an unknown group works.
256 unknown_group=$("$TOP"/t/unknown-owner --group)
258 current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:')
259 WVPASS bup meta --edit --set-group "$unknown_group" ../src.meta \
261 new_gidx=$(bup xstat src | grep -e '^gid:')
262 WVPASSEQ "$current_gidx" "$new_gidx"
265 # Test ownership restoration (when root or fakeroot).
267 if test "$(whoami)" != root; then
271 WVSTART 'metadata (restoration of ownership as root)'
272 force-delete "$TOP/bupmeta.tmp"
273 mkdir "$TOP/bupmeta.tmp"
274 cd "$TOP/bupmeta.tmp"
276 WVPASS bup meta -cf src.meta src
279 chmod 700 dest # so we can't accidentally do something insecure
282 # Make sure we can restore a uid.
283 WVPASS bup meta --edit --unset-user --set-uid 42 ../src.meta \
285 WVPASS bup xstat src | WVPASS grep -qE '^uid: 42'
287 # Make sure we can restore a gid.
288 WVPASS bup meta --edit --unset-group --set-gid 42 ../src.meta \
290 WVPASS bup xstat src | WVPASS grep -qE '^gid: 42'
292 some_user=$("$TOP"/t/some-owner --user)
293 some_group=$("$TOP"/t/some-owner --group)
295 # Try to restore a user (and see that user trumps uid when uid is not 0).
296 WVPASS bup meta --edit --set-uid 42 --set-user "$some_user" ../src.meta \
298 WVPASS bup xstat src | WVPASS grep -qE "^user: $some_user"
300 # Try to restore a group (and see that group trumps gid when gid is not 0).
301 WVPASS bup meta --edit --set-gid 42 --set-group "$some_group" ../src.meta \
303 WVPASS bup xstat src | WVPASS grep -qE "^group: $some_user"
305 # Make sure a uid of 0 trumps a non-root user.
306 WVPASS bup meta --edit --set-user "$some_user" ../src.meta \
308 WVPASS bup xstat src | WVPASS grep -qvE "^user: $some_user"
309 WVPASS bup xstat src | WVPASS grep -qE "^uid: 0"
311 # Make sure a gid of 0 trumps a non-root group.
312 WVPASS bup meta --edit --set-group "$some_user" ../src.meta \
314 WVPASS bup xstat src | WVPASS grep -qvE "^group: $some_group"
315 WVPASS bup xstat src | WVPASS grep -qE "^gid: 0"
317 # Test --numeric-ids (gid). Note the name 'root' is not handled
318 # specially, so we use that here as the test group name. We
319 # assume that the root group's gid is never 42.
321 WVPASS bup meta --edit --set-group root --set-gid 42 ../src.meta \
322 | WVPASS bup meta -x --numeric-ids
323 new_gidx=$(bup xstat src | grep -e '^gid:')
324 WVPASSEQ "$new_gidx" 'gid: 42'
326 # Test --numeric-ids (uid). Note the name 'root' is not handled
327 # specially, so we use that here as the test user name. We assume
328 # that the root user's uid is never 42.
330 WVPASS bup meta --edit --set-user root --set-uid 42 ../src.meta \
331 | WVPASS bup meta -x --numeric-ids
332 new_uidx=$(bup xstat src | grep -e '^uid:')
333 WVPASSEQ "$new_uidx" 'uid: 42'
335 # Test that restoring an unknown user works.
336 unknown_user=$("$TOP"/t/unknown-owners --user)
338 WVPASS bup meta --edit --set-uid 42 --set-user "$unknown_user" ../src.meta \
340 new_uidx=$(bup xstat src | grep -e '^uid:')
341 WVPASSEQ "$new_uidx" 'uid: 42'
343 # Test that restoring an unknown group works.
344 unknown_group=$("$TOP"/t/unknown-owners --group)
346 WVPASS bup meta --edit \
347 --set-gid 42 --set-group "$unknown_group" ../src.meta \
349 new_gidx=$(bup xstat src | grep -e '^gid:')
350 WVPASSEQ "$new_gidx" 'gid: 42'
353 # Root-only tests that require an FS with all the trimmings: ACLs,
354 # Linux attr, Linux xattr, etc.
355 if actually-root; then
357 # These tests are only likely to work under Linux for now
359 [[ $(uname) =~ Linux ]] || exit 0
364 umount "$TOP/bupmeta.tmp/testfs" || true
367 trap cleanup_at_exit EXIT
369 WVSTART 'meta - general (as root)'
371 cd "$TOP/bupmeta.tmp"
373 umount testfs || true
374 dd if=/dev/zero of=testfs.img bs=1M count=32
375 mke2fs -F -j -m 0 testfs.img
377 mount -o loop,acl,user_xattr testfs.img testfs
378 # Hide, so that tests can't create risks.
379 chown root:root testfs
382 #cp -a src testfs/src
383 cp -pPR src testfs/src
384 (cd testfs && test-src-create-extract)
386 WVSTART 'meta - atime (as root)'
387 force-delete testfs/src
392 PYTHONPATH="$TOP/lib" \
393 python -c "from bup import xstat; \
394 x = xstat.timespec_to_nsecs((42, 0));\
395 xstat.utime('testfs/src/foo', (x, x));\
396 xstat.utime('testfs/src/bar', (x, x));"
398 WVPASS bup meta -v --create --recurse --file src.meta src
399 bup meta -tvf src.meta
401 force-delete src-restore
404 WVPASS bup meta --extract --file ../src.meta
405 WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
406 WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
407 # Test start/finish extract.
409 WVPASS bup meta --start-extract --file ../src.meta
411 WVPASS bup meta --finish-extract --file ../src.meta
412 WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
413 WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
416 WVSTART 'meta - Linux attr (as root)'
417 force-delete testfs/src
422 chattr +acdeijstuADST testfs/src/foo
423 chattr +acdeijstuADST testfs/src/bar
424 (cd testfs && test-src-create-extract)
427 WVSTART 'meta - Linux xattr (as root)'
428 force-delete testfs/src
433 attr -s foo -V bar testfs/src/foo
434 attr -s foo -V bar testfs/src/bar
435 (cd testfs && test-src-create-extract)
438 WVSTART 'meta - POSIX.1e ACLs (as root)'
439 force-delete testfs/src
444 setfacl -m u:root:r testfs/src/foo
445 setfacl -m u:root:r testfs/src/bar
446 (cd testfs && test-src-create-extract)