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
58 trap "rm -rf '${tmpfile}'" EXIT
59 rsync -ni -aHAX "$1" "$2" > "${tmpfile}"
60 if test $(wc -l < "${tmpfile}") != 0; then
61 echo "ERROR: detected differences between $1 and $2"
68 test-src-create-extract()
70 # Test bup meta create/extract for ./src -> ./src-restore.
71 # Also writes to ./src-stat and ./src-restore-stat.
73 (cd src && WVPASS genstat) > src-stat
74 WVPASS bup meta --create --recurse --file src.meta src
76 force-delete src-restore
79 WVPASS bup meta --extract --file ../src.meta
81 (cd src && genstat >../../src-restore-stat) || WVFAIL
82 WVPASS diff -U5 ../src-stat ../src-restore-stat
83 # Test start/finish extract.
85 WVPASS bup meta --start-extract --file ../src.meta
87 WVPASS bup meta --finish-extract --file ../src.meta
88 (cd src && genstat >../../src-restore-stat) || WVFAIL
89 WVPASS diff -U5 ../src-stat ../src-restore-stat
93 test-src-save-restore()
95 # Test bup save/restore metadata for ./src -> ./src-restore. Also
96 # writes to ./src.bup. Note that for now this just tests the
97 # restore below src/, in order to avoid having to worry about
98 # operations that require root (like chown /home).
103 export BUP_DIR=$(pwd)/src.bup
106 WVPASS bup save -t -n src src
108 force-delete src-restore
110 WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
111 WVPASS test -d src-restore/src
112 WVPASS compare-trees src/ src-restore/src/
120 if ! actually-root; then return 0; fi
122 umount "$TOP/bupmeta.tmp/testfs" || true
123 umount "$TOP/bupmeta.tmp/testfs-limited" || true
127 trap universal-cleanup EXIT
132 force-delete "$BUP_DIR"
133 force-delete "$TOP/bupmeta.tmp"
134 mkdir -p "$TOP/bupmeta.tmp/src"
135 cp -pPR Documentation cmd lib t "$TOP/bupmeta.tmp"/src
137 # Add some hard links for the general tests.
139 cd "$TOP/bupmeta.tmp"/src
140 touch hardlink-target
141 ln hardlink-target hardlink-1
142 ln hardlink-target hardlink-2
143 ln hardlink-target hardlink-3
146 # Regression test for metadata sort order. Previously, these two
147 # entries would sort in the wrong order because the metadata
148 # entries were being sorted by mangled name, but the index isn't.
149 dd if=/dev/zero of="$TOP/bupmeta.tmp"/src/foo bs=1k count=33
150 touch -d 2011-11-11 "$TOP/bupmeta.tmp"/src/foo
151 touch -d 2011-12-12 "$TOP/bupmeta.tmp"/src/foo-bar
153 t/mksock "$TOP/bupmeta.tmp/src/test-socket" || true
156 # Use the test tree to check bup meta.
157 WVSTART 'meta --create/--extract'
160 cd "$TOP/bupmeta.tmp"
161 test-src-create-extract
163 # Test a top-level file (not dir).
165 WVPASS bup meta -cf src-file.meta src-file
168 WVPASS bup meta -xf ../src-file.meta
171 # Use the test tree to check bup save/restore metadata.
172 WVSTART 'metadata save/restore (general)'
175 cd "$TOP/bupmeta.tmp"
176 test-src-save-restore
179 setup-hardlink-test()
182 cd "$TOP/bupmeta.tmp"
189 hardlink-test-run-restore()
191 force-delete src-restore
193 WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
194 WVPASS test -d src-restore/src
197 # Test hardlinks more carefully.
198 WVSTART 'metadata save/restore (hardlinks)'
202 export BUP_DIR="$TOP/bupmeta.tmp/src.bup"
203 force-delete "$TOP/bupmeta.tmp"
204 mkdir -p "$TOP/bupmeta.tmp"
206 cd "$TOP/bupmeta.tmp"
208 # Test trivial case - single hardlink.
211 cd "$TOP/bupmeta.tmp"/src
212 touch hardlink-target
213 ln hardlink-target hardlink-1
216 WVPASS bup save -t -n src src
217 hardlink-test-run-restore
218 WVPASS compare-trees src/ src-restore/src/
220 # Test hardlink changes between index runs.
223 cd "$TOP/bupmeta.tmp"/src
224 touch hardlink-target-a
225 touch hardlink-target-b
226 ln hardlink-target-a hardlink-b-1
227 ln hardlink-target-a hardlink-a-1
229 WVPASS bup index -vv src
231 ln src/hardlink-target-b src/hardlink-b-1
232 WVPASS bup index -vv src
233 WVPASS bup save -t -n src src
234 hardlink-test-run-restore
235 echo ./src/hardlink-a-1 > hardlink-sets.expected
236 echo ./src/hardlink-target-a >> hardlink-sets.expected
237 echo >> hardlink-sets.expected
238 echo ./src/hardlink-b-1 >> hardlink-sets.expected
239 echo ./src/hardlink-target-b >> hardlink-sets.expected
240 (cd src-restore && hardlink-sets .) > hardlink-sets.restored
241 WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
243 # Test hardlink changes between index and save -- hardlink set [a
244 # b c d] changes to [a b] [c d]. At least right now bup should
245 # notice and recreate the latter.
247 cd "$TOP/bupmeta.tmp"/src
253 WVPASS bup index -vv src
257 WVPASS bup save -t -n src src
258 hardlink-test-run-restore
259 echo ./src/a > hardlink-sets.expected
260 echo ./src/b >> hardlink-sets.expected
261 echo >> hardlink-sets.expected
262 echo ./src/c >> hardlink-sets.expected
263 echo ./src/d >> hardlink-sets.expected
264 (cd src-restore && hardlink-sets .) > hardlink-sets.restored
265 WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
267 # Test that we don't link outside restore tree.
269 cd "$TOP/bupmeta.tmp"
273 WVPASS bup index -vv src
274 WVPASS bup save -t -n src src
275 force-delete src-restore
277 WVPASS bup restore -C src-restore "/src/latest$(pwd)/src/a/"
278 WVPASS test -e src-restore/1
279 echo -n > hardlink-sets.expected
280 (cd src-restore && hardlink-sets .) > hardlink-sets.restored
281 WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
283 # Test that we do link within separate sub-trees.
285 cd "$TOP/bupmeta.tmp"
289 WVPASS bup index -vv src/a src/b
290 WVPASS bup save -t -n src src/a src/b
291 hardlink-test-run-restore
292 echo ./src/a/1 > hardlink-sets.expected
293 echo ./src/b/1 >> hardlink-sets.expected
294 (cd src-restore && hardlink-sets .) > hardlink-sets.restored
295 WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
298 WVSTART 'meta --edit'
300 force-delete "$TOP/bupmeta.tmp"
301 mkdir "$TOP/bupmeta.tmp"
302 cd "$TOP/bupmeta.tmp"
304 WVPASS bup meta -cf src.meta src
306 WVPASS bup meta --edit --set-uid 0 src.meta | WVPASS bup meta -tvvf - \
307 | WVPASS grep -qE '^uid: 0'
308 WVPASS bup meta --edit --set-uid 1000 src.meta | WVPASS bup meta -tvvf - \
309 | WVPASS grep -qE '^uid: 1000'
311 WVPASS bup meta --edit --set-gid 0 src.meta | WVPASS bup meta -tvvf - \
312 | WVPASS grep -qE '^gid: 0'
313 WVPASS bup meta --edit --set-gid 1000 src.meta | WVPASS bup meta -tvvf - \
314 | WVPASS grep -qE '^gid: 1000'
316 WVPASS bup meta --edit --set-user foo src.meta | WVPASS bup meta -tvvf - \
317 | WVPASS grep -qE '^user: foo'
318 WVPASS bup meta --edit --set-user bar src.meta | WVPASS bup meta -tvvf - \
319 | WVPASS grep -qE '^user: bar'
320 WVPASS bup meta --edit --unset-user src.meta | WVPASS bup meta -tvvf - \
321 | WVPASS grep -qE '^user:'
322 WVPASS bup meta --edit --set-user bar --unset-user src.meta \
323 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user:'
324 WVPASS bup meta --edit --unset-user --set-user bar src.meta \
325 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user: bar'
327 WVPASS bup meta --edit --set-group foo src.meta | WVPASS bup meta -tvvf - \
328 | WVPASS grep -qE '^group: foo'
329 WVPASS bup meta --edit --set-group bar src.meta | WVPASS bup meta -tvvf - \
330 | WVPASS grep -qE '^group: bar'
331 WVPASS bup meta --edit --unset-group src.meta | WVPASS bup meta -tvvf - \
332 | WVPASS grep -qE '^group:'
333 WVPASS bup meta --edit --set-group bar --unset-group src.meta \
334 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^group:'
335 WVPASS bup meta --edit --unset-group --set-group bar src.meta \
336 | WVPASS bup meta -tvvf - | grep -qE '^group: bar'
339 # Test ownership restoration (when not root or fakeroot).
341 if test "$(whoami)" == root; then
345 WVSTART 'metadata (restoration of ownership)'
346 force-delete "$TOP/bupmeta.tmp"
347 mkdir "$TOP/bupmeta.tmp"
348 cd "$TOP/bupmeta.tmp"
350 WVPASS bup meta -cf src.meta src
354 # Make sure we don't change (or try to change) the user when not root.
355 WVPASS bup meta --edit --set-user root ../src.meta | WVPASS bup meta -x
356 WVPASS bup xstat src | WVPASS grep -qvE '^user: root'
358 WVPASS bup meta --edit --unset-user --set-uid 0 ../src.meta \
360 WVPASS bup xstat src | grep -qvE '^user: root'
362 # Make sure we can restore one of the user's groups.
363 last_group="$(python -c 'import os,grp; \
364 print grp.getgrgid(os.getgroups()[0])[0]')"
366 WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
368 WVPASS bup xstat src | WVPASS grep -qE "^group: $last_group"
370 # Make sure we can restore one of the user's gids.
372 last_gid="$(echo ${user_gids/* /})"
374 WVPASS bup meta --edit --unset-group --set-gid "$last_gid" ../src.meta \
376 WVPASS bup xstat src | WVPASS grep -qE "^gid: $last_gid"
378 # Test --numeric-ids (gid).
380 current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:')
381 WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
382 | WVPASS bup meta -x --numeric-ids
383 new_gidx=$(bup xstat src | grep -e '^gid:')
384 WVPASSEQ "$current_gidx" "$new_gidx"
386 # Test that restoring an unknown user works.
387 unknown_user=$("$TOP"/t/unknown-owner --user)
389 current_uidx=$(bup meta -tvvf ../src.meta | grep -e '^uid:')
390 WVPASS bup meta --edit --set-user "$unknown_user" ../src.meta \
392 new_uidx=$(bup xstat src | grep -e '^uid:')
393 WVPASSEQ "$current_uidx" "$new_uidx"
395 # Test that restoring an unknown group works.
396 unknown_group=$("$TOP"/t/unknown-owner --group)
398 current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:')
399 WVPASS bup meta --edit --set-group "$unknown_group" ../src.meta \
401 new_gidx=$(bup xstat src | grep -e '^gid:')
402 WVPASSEQ "$current_gidx" "$new_gidx"
405 # Test ownership restoration (when root or fakeroot).
407 if test "$(whoami)" != root; then
411 WVSTART 'metadata (restoration of ownership as root)'
412 force-delete "$TOP/bupmeta.tmp"
413 mkdir "$TOP/bupmeta.tmp"
414 cd "$TOP/bupmeta.tmp"
416 chown 0:0 src # In case the parent dir is sgid, etc.
417 WVPASS bup meta -cf src.meta src
420 chmod 700 dest # so we can't accidentally do something insecure
423 # Make sure we can restore a uid.
424 WVPASS bup meta --edit --unset-user --set-uid 42 ../src.meta \
426 WVPASS bup xstat src | WVPASS grep -qE '^uid: 42'
428 # Make sure we can restore a gid.
429 WVPASS bup meta --edit --unset-group --set-gid 42 ../src.meta \
431 WVPASS bup xstat src | WVPASS grep -qE '^gid: 42'
433 some_user=$("$TOP"/t/some-owner --user)
434 some_group=$("$TOP"/t/some-owner --group)
436 # Try to restore a user (and see that user trumps uid when uid is not 0).
437 WVPASS bup meta --edit --set-uid 42 --set-user "$some_user" ../src.meta \
439 WVPASS bup xstat src | WVPASS grep -qE "^user: $some_user"
441 # Try to restore a group (and see that group trumps gid when gid is not 0).
442 WVPASS bup meta --edit --set-gid 42 --set-group "$some_group" ../src.meta \
444 WVPASS bup xstat src | WVPASS grep -qE "^group: $some_user"
446 # Make sure a uid of 0 trumps a non-root user.
447 WVPASS bup meta --edit --set-user "$some_user" ../src.meta \
449 WVPASS bup xstat src | WVPASS grep -qvE "^user: $some_user"
450 WVPASS bup xstat src | WVPASS grep -qE "^uid: 0"
452 # Make sure a gid of 0 trumps a non-root group.
453 WVPASS bup meta --edit --set-group "$some_user" ../src.meta \
455 WVPASS bup xstat src | WVPASS grep -qvE "^group: $some_group"
456 WVPASS bup xstat src | WVPASS grep -qE "^gid: 0"
458 # Test --numeric-ids (gid). Note the name 'root' is not handled
459 # specially, so we use that here as the test group name. We
460 # assume that the root group's gid is never 42.
462 WVPASS bup meta --edit --set-group root --set-gid 42 ../src.meta \
463 | WVPASS bup meta -x --numeric-ids
464 new_gidx=$(bup xstat src | grep -e '^gid:')
465 WVPASSEQ "$new_gidx" 'gid: 42'
467 # Test --numeric-ids (uid). Note the name 'root' is not handled
468 # specially, so we use that here as the test user name. We assume
469 # that the root user's uid is never 42.
471 WVPASS bup meta --edit --set-user root --set-uid 42 ../src.meta \
472 | WVPASS bup meta -x --numeric-ids
473 new_uidx=$(bup xstat src | grep -e '^uid:')
474 WVPASSEQ "$new_uidx" 'uid: 42'
476 # Test that restoring an unknown user works.
477 unknown_user=$("$TOP"/t/unknown-owners --user)
479 WVPASS bup meta --edit --set-uid 42 --set-user "$unknown_user" ../src.meta \
481 new_uidx=$(bup xstat src | grep -e '^uid:')
482 WVPASSEQ "$new_uidx" 'uid: 42'
484 # Test that restoring an unknown group works.
485 unknown_group=$("$TOP"/t/unknown-owners --group)
487 WVPASS bup meta --edit \
488 --set-gid 42 --set-group "$unknown_group" ../src.meta \
490 new_gidx=$(bup xstat src | grep -e '^gid:')
491 WVPASSEQ "$new_gidx" 'gid: 42'
494 # Root-only tests that require an FS with all the trimmings: ACLs,
495 # Linux attr, Linux xattr, etc.
496 if actually-root; then
499 # Some cleanup handled in universal-cleanup() above.
500 # These tests are only likely to work under Linux for now
502 [[ $(uname) =~ Linux ]] || exit 0
504 WVSTART 'meta - general (as root)'
506 cd "$TOP/bupmeta.tmp"
508 umount testfs || true
509 dd if=/dev/zero of=testfs.img bs=1M count=32
510 mke2fs -F -j -m 0 testfs.img
512 mount -o loop,acl,user_xattr testfs.img testfs
513 # Hide, so that tests can't create risks.
514 chown root:root testfs
517 umount testfs-limited || true
518 dd if=/dev/zero of=testfs-limited.img bs=1M count=32
519 mkfs -t vfat testfs-limited.img
521 mount -o loop,uid=root,gid=root,umask=0077 \
522 testfs-limited.img testfs-limited
524 #cp -a src testfs/src
525 cp -pPR src testfs/src
526 (cd testfs && test-src-create-extract)
528 WVSTART 'meta - atime (as root)'
529 force-delete testfs/src
534 PYTHONPATH="$TOP/lib" \
535 python -c "from bup import xstat; \
536 x = xstat.timespec_to_nsecs((42, 0));\
537 xstat.utime('testfs/src/foo', (x, x));\
538 xstat.utime('testfs/src/bar', (x, x));"
540 WVPASS bup meta -v --create --recurse --file src.meta src
541 bup meta -tvf src.meta
543 force-delete src-restore
546 WVPASS bup meta --extract --file ../src.meta
547 WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
548 WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
549 # Test start/finish extract.
551 WVPASS bup meta --start-extract --file ../src.meta
553 WVPASS bup meta --finish-extract --file ../src.meta
554 WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
555 WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
558 WVSTART 'meta - Linux attr (as root)'
559 force-delete testfs/src
564 chattr +acdeijstuADST testfs/src/foo
565 chattr +acdeijstuADST testfs/src/bar
566 (cd testfs && test-src-create-extract)
567 # Test restoration to a limited filesystem (vfat).
569 WVPASS bup meta --create --recurse --file testfs/src.meta \
571 force-delete testfs-limited/src-restore
572 mkdir testfs-limited/src-restore
573 cd testfs-limited/src-restore
574 WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
575 | WVPASS grep -e '^Linux chattr:' \
577 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
581 WVSTART 'meta - Linux xattr (as root)'
582 force-delete testfs/src
587 attr -s foo -V bar testfs/src/foo
588 attr -s foo -V bar testfs/src/bar
589 (cd testfs && test-src-create-extract)
591 # Test restoration to a limited filesystem (vfat).
593 WVPASS bup meta --create --recurse --file testfs/src.meta \
595 force-delete testfs-limited/src-restore
596 mkdir testfs-limited/src-restore
597 cd testfs-limited/src-restore
598 WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
599 | WVPASS grep -e '^xattr\.set:' \
601 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
605 WVSTART 'meta - POSIX.1e ACLs (as root)'
606 force-delete testfs/src
611 setfacl -m u:root:r testfs/src/foo
612 setfacl -m u:root:r testfs/src/bar
613 (cd testfs && test-src-create-extract)
615 # Test restoration to a limited filesystem (vfat).
617 WVPASS bup meta --create --recurse --file testfs/src.meta \
619 force-delete testfs-limited/src-restore
620 mkdir testfs-limited/src-restore
621 cd testfs-limited/src-restore
622 WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
623 | WVPASS grep -e '^POSIX1e ACL applyto:' \
625 'import sys; exit(not len(sys.stdin.readlines()) == 2)'