5 export BUP_DIR="$TOP/buptest.tmp"
14 "$TOP/t/hardlink-sets" "$@"
17 # Very simple metadata tests -- create a test tree then check that bup
18 # meta can reproduce the metadata correctly (according to bup xstat)
19 # via create, extract, start-extract, and finish-extract. The current
20 # tests are crude, and this does not fully test devices, varying
21 # users/groups, acls, attrs, etc.
26 export PATH="$TOP:$PATH" # pick up bup
27 # Skip atime (test elsewhere) to avoid the observer effect.
28 find . | sort | xargs bup xstat --exclude-fields ctime,atime,size
34 test "$(whoami)" == root -a -z "$FAKEROOTKEY"
39 if ! actually-root; then
42 # Go to greater lengths to deal with any test detritus.
44 test -e "$@" || continue
45 chattr -fR = "$@" || true
52 test-src-create-extract()
54 # Test bup meta create/extract for ./src -> ./src-restore.
55 # Also writes to ./src-stat and ./src-restore-stat.
57 (cd src && WVPASS genstat) > src-stat
58 WVPASS bup meta --create --recurse --file src.meta src
60 force-delete src-restore
63 WVPASS bup meta --extract --file ../src.meta
65 (cd src && genstat >../../src-restore-stat) || WVFAIL
66 WVPASS diff -U5 ../src-stat ../src-restore-stat
67 # Test start/finish extract.
69 WVPASS bup meta --start-extract --file ../src.meta
71 WVPASS bup meta --finish-extract --file ../src.meta
72 (cd src && genstat >../../src-restore-stat) || WVFAIL
73 WVPASS diff -U5 ../src-stat ../src-restore-stat
77 test-src-save-restore()
79 # Test bup save/restore metadata for ./src -> ./src-restore. Also
80 # writes to ./src.bup. Note that for now this just tests the
81 # restore below src/, in order to avoid having to worry about
82 # operations that require root (like chown /home).
87 export BUP_DIR=$(pwd)/src.bup
90 WVPASS bup save -t -n src src
92 force-delete src-restore
94 WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
95 WVPASS test -d src-restore/src
96 WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
104 if ! actually-root; then return 0; fi
106 umount "$TOP/bupmeta.tmp/testfs" || true
107 umount "$TOP/bupmeta.tmp/testfs-limited" || true
111 trap universal-cleanup EXIT
116 force-delete "$BUP_DIR"
117 force-delete "$TOP/bupmeta.tmp"
118 mkdir -p "$TOP/bupmeta.tmp/src"
119 cp -pPR Documentation cmd lib t "$TOP/bupmeta.tmp"/src
121 # Add some hard links for the general tests.
123 cd "$TOP/bupmeta.tmp"/src
124 touch hardlink-target
125 ln hardlink-target hardlink-1
126 ln hardlink-target hardlink-2
127 ln hardlink-target hardlink-3
130 # Add some trivial files for the index, modify, save tests.
132 cd "$TOP/bupmeta.tmp"/src
134 touch volatile/{1,2,3}
137 # Regression test for metadata sort order. Previously, these two
138 # entries would sort in the wrong order because the metadata
139 # entries were being sorted by mangled name, but the index isn't.
140 dd if=/dev/zero of="$TOP/bupmeta.tmp"/src/foo bs=1k count=33
141 touch -d 2011-11-11 "$TOP/bupmeta.tmp"/src/foo
142 touch -d 2011-12-12 "$TOP/bupmeta.tmp"/src/foo-bar
144 t/mksock "$TOP/bupmeta.tmp/src/test-socket" || true
147 # Use the test tree to check bup meta.
148 WVSTART 'meta --create/--extract'
151 cd "$TOP/bupmeta.tmp"
152 test-src-create-extract
154 # Test a top-level file (not dir).
156 WVPASS bup meta -cf src-file.meta src-file
159 WVPASS bup meta -xf ../src-file.meta
162 # Use the test tree to check bup save/restore metadata.
163 WVSTART 'metadata save/restore (general)'
166 cd "$TOP/bupmeta.tmp"
167 test-src-save-restore
170 # Test that we pull the index (not filesystem) metadata for any
171 # unchanged files whenever we're saving other files in a given
173 WVSTART 'metadata save/restore (using index metadata)'
176 cd "$TOP/bupmeta.tmp"
178 # ...for now -- might be a problem with hardlink restores that was
179 # causing noise wrt this test.
182 # Pause here to keep the filesystem changes far enough away from
183 # the first index run that bup won't cap their index timestamps
184 # (see "bup help index" for more information). Without this
185 # sleep, the compare-trees test below "Bup should *not* pick up
186 # these metadata..." may fail.
192 export BUP_DIR=$(pwd)/src.bup
195 WVPASS bup save -t -n src src
197 force-delete src-restore-1
199 WVPASS bup restore -C src-restore-1 "/src/latest$(pwd)/"
200 WVPASS test -d src-restore-1/src
201 WVPASS "$TOP/t/compare-trees" -c src/ src-restore-1/src/
203 echo "blarg" > src/volatile/1
204 cp -a src/volatile/1 src-restore-1/src/volatile/
207 # Bup should *not* pick up these metadata changes.
210 WVPASS bup save -t -n src src
212 force-delete src-restore-2
214 WVPASS bup restore -C src-restore-2 "/src/latest$(pwd)/"
215 WVPASS test -d src-restore-2/src
216 WVPASS "$TOP/t/compare-trees" -c src-restore-1/src/ src-restore-2/src/
222 setup-hardlink-test()
225 cd "$TOP/bupmeta.tmp"
232 hardlink-test-run-restore()
234 force-delete src-restore
236 WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
237 WVPASS test -d src-restore/src
240 # Test hardlinks more carefully.
241 WVSTART 'metadata save/restore (hardlinks)'
245 export BUP_DIR="$TOP/bupmeta.tmp/src.bup"
246 force-delete "$TOP/bupmeta.tmp"
247 mkdir -p "$TOP/bupmeta.tmp"
249 cd "$TOP/bupmeta.tmp"
251 # Test trivial case - single hardlink.
254 cd "$TOP/bupmeta.tmp"/src
255 touch hardlink-target
256 ln hardlink-target hardlink-1
259 WVPASS bup save -t -n src src
260 hardlink-test-run-restore
261 WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
263 # Test the case where the hardlink hasn't changed, but the tree
264 # needs to be saved again. i.e. the save-cmd.py "if hashvalid:"
267 cd "$TOP/bupmeta.tmp"/src
268 echo whatever > something-new
271 WVPASS bup save -t -n src src
272 hardlink-test-run-restore
273 WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
275 # Test hardlink changes between index runs.
278 cd "$TOP/bupmeta.tmp"/src
279 touch hardlink-target-a
280 touch hardlink-target-b
281 ln hardlink-target-a hardlink-b-1
282 ln hardlink-target-a hardlink-a-1
284 WVPASS bup index -vv src
286 ln src/hardlink-target-b src/hardlink-b-1
287 WVPASS bup index -vv src
288 WVPASS bup save -t -n src src
289 hardlink-test-run-restore
290 echo ./src/hardlink-a-1 > hardlink-sets.expected
291 echo ./src/hardlink-target-a >> hardlink-sets.expected
292 echo >> hardlink-sets.expected
293 echo ./src/hardlink-b-1 >> hardlink-sets.expected
294 echo ./src/hardlink-target-b >> hardlink-sets.expected
295 (cd src-restore && hardlink-sets .) > hardlink-sets.restored
296 WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
298 # Test hardlink changes between index and save -- hardlink set [a
299 # b c d] changes to [a b] [c d]. At least right now bup should
300 # notice and recreate the latter.
302 cd "$TOP/bupmeta.tmp"/src
308 WVPASS bup index -vv src
312 WVPASS bup save -t -n src src
313 hardlink-test-run-restore
314 echo ./src/a > hardlink-sets.expected
315 echo ./src/b >> hardlink-sets.expected
316 echo >> hardlink-sets.expected
317 echo ./src/c >> hardlink-sets.expected
318 echo ./src/d >> hardlink-sets.expected
319 (cd src-restore && hardlink-sets .) > hardlink-sets.restored
320 WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
322 # Test that we don't link outside restore tree.
324 cd "$TOP/bupmeta.tmp"
328 WVPASS bup index -vv src
329 WVPASS bup save -t -n src src
330 force-delete src-restore
332 WVPASS bup restore -C src-restore "/src/latest$(pwd)/src/a/"
333 WVPASS test -e src-restore/1
334 echo -n > hardlink-sets.expected
335 (cd src-restore && hardlink-sets .) > hardlink-sets.restored
336 WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
338 # Test that we do link within separate sub-trees.
340 cd "$TOP/bupmeta.tmp"
344 WVPASS bup index -vv src/a src/b
345 WVPASS bup save -t -n src src/a src/b
346 hardlink-test-run-restore
347 echo ./src/a/1 > hardlink-sets.expected
348 echo ./src/b/1 >> hardlink-sets.expected
349 (cd src-restore && hardlink-sets .) > hardlink-sets.restored
350 WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
353 WVSTART 'meta --edit'
355 force-delete "$TOP/bupmeta.tmp"
356 mkdir "$TOP/bupmeta.tmp"
357 cd "$TOP/bupmeta.tmp"
359 WVPASS bup meta -cf src.meta src
361 WVPASS bup meta --edit --set-uid 0 src.meta | WVPASS bup meta -tvvf - \
362 | WVPASS grep -qE '^uid: 0'
363 WVPASS bup meta --edit --set-uid 1000 src.meta | WVPASS bup meta -tvvf - \
364 | WVPASS grep -qE '^uid: 1000'
366 WVPASS bup meta --edit --set-gid 0 src.meta | WVPASS bup meta -tvvf - \
367 | WVPASS grep -qE '^gid: 0'
368 WVPASS bup meta --edit --set-gid 1000 src.meta | WVPASS bup meta -tvvf - \
369 | WVPASS grep -qE '^gid: 1000'
371 WVPASS bup meta --edit --set-user foo src.meta | WVPASS bup meta -tvvf - \
372 | WVPASS grep -qE '^user: foo'
373 WVPASS bup meta --edit --set-user bar src.meta | WVPASS bup meta -tvvf - \
374 | WVPASS grep -qE '^user: bar'
375 WVPASS bup meta --edit --unset-user src.meta | WVPASS bup meta -tvvf - \
376 | WVPASS grep -qE '^user:'
377 WVPASS bup meta --edit --set-user bar --unset-user src.meta \
378 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user:'
379 WVPASS bup meta --edit --unset-user --set-user bar src.meta \
380 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user: bar'
382 WVPASS bup meta --edit --set-group foo src.meta | WVPASS bup meta -tvvf - \
383 | WVPASS grep -qE '^group: foo'
384 WVPASS bup meta --edit --set-group bar src.meta | WVPASS bup meta -tvvf - \
385 | WVPASS grep -qE '^group: bar'
386 WVPASS bup meta --edit --unset-group src.meta | WVPASS bup meta -tvvf - \
387 | WVPASS grep -qE '^group:'
388 WVPASS bup meta --edit --set-group bar --unset-group src.meta \
389 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^group:'
390 WVPASS bup meta --edit --unset-group --set-group bar src.meta \
391 | WVPASS bup meta -tvvf - | grep -qE '^group: bar'
394 # Test ownership restoration (when not root or fakeroot).
396 if test "$(whoami)" == root; then
400 WVSTART 'metadata (restoration of ownership)'
401 force-delete "$TOP/bupmeta.tmp"
402 mkdir "$TOP/bupmeta.tmp"
403 cd "$TOP/bupmeta.tmp"
405 WVPASS bup meta -cf src.meta src
409 # Make sure we don't change (or try to change) the user when not root.
410 WVPASS bup meta --edit --set-user root ../src.meta | WVPASS bup meta -x
411 WVPASS bup xstat src | WVPASS grep -qvE '^user: root'
413 WVPASS bup meta --edit --unset-user --set-uid 0 ../src.meta \
415 WVPASS bup xstat src | grep -qvE '^user: root'
417 # Make sure we can restore one of the user's groups.
418 last_group="$(python -c 'import os,grp; \
419 print grp.getgrgid(os.getgroups()[0])[0]')"
421 WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
423 WVPASS bup xstat src | WVPASS grep -qE "^group: $last_group"
425 # Make sure we can restore one of the user's gids.
427 last_gid="$(echo ${user_gids/* /})"
429 WVPASS bup meta --edit --unset-group --set-gid "$last_gid" ../src.meta \
431 WVPASS bup xstat src | WVPASS grep -qE "^gid: $last_gid"
433 # Test --numeric-ids (gid).
435 current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:')
436 WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
437 | WVPASS bup meta -x --numeric-ids
438 new_gidx=$(bup xstat src | grep -e '^gid:')
439 WVPASSEQ "$current_gidx" "$new_gidx"
441 # Test that restoring an unknown user works.
442 unknown_user=$("$TOP"/t/unknown-owner --user)
444 current_uidx=$(bup meta -tvvf ../src.meta | grep -e '^uid:')
445 WVPASS bup meta --edit --set-user "$unknown_user" ../src.meta \
447 new_uidx=$(bup xstat src | grep -e '^uid:')
448 WVPASSEQ "$current_uidx" "$new_uidx"
450 # Test that restoring an unknown group works.
451 unknown_group=$("$TOP"/t/unknown-owner --group)
453 current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:')
454 WVPASS bup meta --edit --set-group "$unknown_group" ../src.meta \
456 new_gidx=$(bup xstat src | grep -e '^gid:')
457 WVPASSEQ "$current_gidx" "$new_gidx"
460 # Test ownership restoration (when root or fakeroot).
462 if test "$(whoami)" != root; then
466 WVSTART 'metadata (restoration of ownership as root)'
467 force-delete "$TOP/bupmeta.tmp"
468 mkdir "$TOP/bupmeta.tmp"
469 cd "$TOP/bupmeta.tmp"
471 chown 0:0 src # In case the parent dir is sgid, etc.
472 WVPASS bup meta -cf src.meta src
475 chmod 700 dest # so we can't accidentally do something insecure
478 # Make sure we can restore a uid.
479 WVPASS bup meta --edit --unset-user --set-uid 42 ../src.meta \
481 WVPASS bup xstat src | WVPASS grep -qE '^uid: 42'
483 # Make sure we can restore a gid.
484 WVPASS bup meta --edit --unset-group --set-gid 42 ../src.meta \
486 WVPASS bup xstat src | WVPASS grep -qE '^gid: 42'
488 some_user=$("$TOP"/t/some-owner --user)
489 some_group=$("$TOP"/t/some-owner --group)
491 # Try to restore a user (and see that user trumps uid when uid is not 0).
492 WVPASS bup meta --edit --set-uid 42 --set-user "$some_user" ../src.meta \
494 WVPASS bup xstat src | WVPASS grep -qE "^user: $some_user"
496 # Try to restore a group (and see that group trumps gid when gid is not 0).
497 WVPASS bup meta --edit --set-gid 42 --set-group "$some_group" ../src.meta \
499 WVPASS bup xstat src | WVPASS grep -qE "^group: $some_user"
501 # Make sure a uid of 0 trumps a non-root user.
502 WVPASS bup meta --edit --set-user "$some_user" ../src.meta \
504 WVPASS bup xstat src | WVPASS grep -qvE "^user: $some_user"
505 WVPASS bup xstat src | WVPASS grep -qE "^uid: 0"
507 # Make sure a gid of 0 trumps a non-root group.
508 WVPASS bup meta --edit --set-group "$some_user" ../src.meta \
510 WVPASS bup xstat src | WVPASS grep -qvE "^group: $some_group"
511 WVPASS bup xstat src | WVPASS grep -qE "^gid: 0"
513 # Test --numeric-ids (gid). Note the name 'root' is not handled
514 # specially, so we use that here as the test group name. We
515 # assume that the root group's gid is never 42.
517 WVPASS bup meta --edit --set-group root --set-gid 42 ../src.meta \
518 | WVPASS bup meta -x --numeric-ids
519 new_gidx=$(bup xstat src | grep -e '^gid:')
520 WVPASSEQ "$new_gidx" 'gid: 42'
522 # Test --numeric-ids (uid). Note the name 'root' is not handled
523 # specially, so we use that here as the test user name. We assume
524 # that the root user's uid is never 42.
526 WVPASS bup meta --edit --set-user root --set-uid 42 ../src.meta \
527 | WVPASS bup meta -x --numeric-ids
528 new_uidx=$(bup xstat src | grep -e '^uid:')
529 WVPASSEQ "$new_uidx" 'uid: 42'
531 # Test that restoring an unknown user works.
532 unknown_user=$("$TOP"/t/unknown-owners --user)
534 WVPASS bup meta --edit --set-uid 42 --set-user "$unknown_user" ../src.meta \
536 new_uidx=$(bup xstat src | grep -e '^uid:')
537 WVPASSEQ "$new_uidx" 'uid: 42'
539 # Test that restoring an unknown group works.
540 unknown_group=$("$TOP"/t/unknown-owners --group)
542 WVPASS bup meta --edit \
543 --set-gid 42 --set-group "$unknown_group" ../src.meta \
545 new_gidx=$(bup xstat src | grep -e '^gid:')
546 WVPASSEQ "$new_gidx" 'gid: 42'
549 # Root-only tests that require an FS with all the trimmings: ACLs,
550 # Linux attr, Linux xattr, etc.
551 if actually-root; then
554 # Some cleanup handled in universal-cleanup() above.
555 # These tests are only likely to work under Linux for now
557 [[ $(uname) =~ Linux ]] || exit 0
559 WVSTART 'meta - general (as root)'
561 cd "$TOP/bupmeta.tmp"
563 umount testfs || true
564 dd if=/dev/zero of=testfs.img bs=1M count=32
565 mke2fs -F -j -m 0 testfs.img
567 mount -o loop,acl,user_xattr testfs.img testfs
568 # Hide, so that tests can't create risks.
569 chown root:root testfs
572 umount testfs-limited || true
573 dd if=/dev/zero of=testfs-limited.img bs=1M count=32
574 mkfs -t vfat testfs-limited.img
576 mount -o loop,uid=root,gid=root,umask=0077 \
577 testfs-limited.img testfs-limited
579 #cp -a src testfs/src
580 cp -pPR src testfs/src
581 (cd testfs && test-src-create-extract)
583 WVSTART 'meta - atime (as root)'
584 force-delete testfs/src
589 PYTHONPATH="$TOP/lib" \
590 python -c "from bup import xstat; \
591 x = xstat.timespec_to_nsecs((42, 0));\
592 xstat.utime('testfs/src/foo', (x, x));\
593 xstat.utime('testfs/src/bar', (x, x));"
595 WVPASS bup meta -v --create --recurse --file src.meta src
596 bup meta -tvf src.meta
598 force-delete src-restore
601 WVPASS bup meta --extract --file ../src.meta
602 WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
603 WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
604 # Test start/finish extract.
606 WVPASS bup meta --start-extract --file ../src.meta
608 WVPASS bup meta --finish-extract --file ../src.meta
609 WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
610 WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
613 WVSTART 'meta - Linux attr (as root)'
614 force-delete testfs/src
619 chattr +acdeijstuADST testfs/src/foo
620 chattr +acdeijstuADST testfs/src/bar
621 (cd testfs && test-src-create-extract)
622 # Test restoration to a limited filesystem (vfat).
624 WVPASS bup meta --create --recurse --file testfs/src.meta \
626 force-delete testfs-limited/src-restore
627 mkdir testfs-limited/src-restore
628 cd testfs-limited/src-restore
629 WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
630 | WVPASS grep -e '^Linux chattr:' \
632 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
636 WVSTART 'meta - Linux xattr (as root)'
637 force-delete testfs/src
642 attr -s foo -V bar testfs/src/foo
643 attr -s foo -V bar testfs/src/bar
644 (cd testfs && test-src-create-extract)
646 # Test restoration to a limited filesystem (vfat).
648 WVPASS bup meta --create --recurse --file testfs/src.meta \
650 force-delete testfs-limited/src-restore
651 mkdir testfs-limited/src-restore
652 cd testfs-limited/src-restore
653 WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
654 | WVPASS grep -e '^xattr\.set:' \
656 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
660 WVSTART 'meta - POSIX.1e ACLs (as root)'
661 force-delete testfs/src
666 setfacl -m u:root:r testfs/src/foo
667 setfacl -m u:root:r testfs/src/bar
668 (cd testfs && test-src-create-extract)
670 # Test restoration to a limited filesystem (vfat).
672 WVPASS bup meta --create --recurse --file testfs/src.meta \
674 force-delete testfs-limited/src-restore
675 mkdir testfs-limited/src-restore
676 cd testfs-limited/src-restore
677 WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
678 | WVPASS grep -e '^POSIX1e ACL applyto:' \
680 'import sys; exit(not len(sys.stdin.readlines()) == 2)'