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 # Regression test for metadata sort order. Previously, these two
131 # entries would sort in the wrong order because the metadata
132 # entries were being sorted by mangled name, but the index isn't.
133 dd if=/dev/zero of="$TOP/bupmeta.tmp"/src/foo bs=1k count=33
134 touch -d 2011-11-11 "$TOP/bupmeta.tmp"/src/foo
135 touch -d 2011-12-12 "$TOP/bupmeta.tmp"/src/foo-bar
137 t/mksock "$TOP/bupmeta.tmp/src/test-socket" || true
140 # Use the test tree to check bup meta.
141 WVSTART 'meta --create/--extract'
144 cd "$TOP/bupmeta.tmp"
145 test-src-create-extract
147 # Test a top-level file (not dir).
149 WVPASS bup meta -cf src-file.meta src-file
152 WVPASS bup meta -xf ../src-file.meta
155 # Use the test tree to check bup save/restore metadata.
156 WVSTART 'metadata save/restore (general)'
159 cd "$TOP/bupmeta.tmp"
160 test-src-save-restore
163 setup-hardlink-test()
166 cd "$TOP/bupmeta.tmp"
173 hardlink-test-run-restore()
175 force-delete src-restore
177 WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
178 WVPASS test -d src-restore/src
181 # Test hardlinks more carefully.
182 WVSTART 'metadata save/restore (hardlinks)'
186 export BUP_DIR="$TOP/bupmeta.tmp/src.bup"
187 force-delete "$TOP/bupmeta.tmp"
188 mkdir -p "$TOP/bupmeta.tmp"
190 cd "$TOP/bupmeta.tmp"
192 # Test trivial case - single hardlink.
195 cd "$TOP/bupmeta.tmp"/src
196 touch hardlink-target
197 ln hardlink-target hardlink-1
200 WVPASS bup save -t -n src src
201 hardlink-test-run-restore
202 WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
204 # Test hardlink changes between index runs.
207 cd "$TOP/bupmeta.tmp"/src
208 touch hardlink-target-a
209 touch hardlink-target-b
210 ln hardlink-target-a hardlink-b-1
211 ln hardlink-target-a hardlink-a-1
213 WVPASS bup index -vv src
215 ln src/hardlink-target-b src/hardlink-b-1
216 WVPASS bup index -vv src
217 WVPASS bup save -t -n src src
218 hardlink-test-run-restore
219 echo ./src/hardlink-a-1 > hardlink-sets.expected
220 echo ./src/hardlink-target-a >> hardlink-sets.expected
221 echo >> hardlink-sets.expected
222 echo ./src/hardlink-b-1 >> hardlink-sets.expected
223 echo ./src/hardlink-target-b >> hardlink-sets.expected
224 (cd src-restore && hardlink-sets .) > hardlink-sets.restored
225 WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
227 # Test hardlink changes between index and save -- hardlink set [a
228 # b c d] changes to [a b] [c d]. At least right now bup should
229 # notice and recreate the latter.
231 cd "$TOP/bupmeta.tmp"/src
237 WVPASS bup index -vv src
241 WVPASS bup save -t -n src src
242 hardlink-test-run-restore
243 echo ./src/a > hardlink-sets.expected
244 echo ./src/b >> hardlink-sets.expected
245 echo >> hardlink-sets.expected
246 echo ./src/c >> hardlink-sets.expected
247 echo ./src/d >> hardlink-sets.expected
248 (cd src-restore && hardlink-sets .) > hardlink-sets.restored
249 WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
251 # Test that we don't link outside restore tree.
253 cd "$TOP/bupmeta.tmp"
257 WVPASS bup index -vv src
258 WVPASS bup save -t -n src src
259 force-delete src-restore
261 WVPASS bup restore -C src-restore "/src/latest$(pwd)/src/a/"
262 WVPASS test -e src-restore/1
263 echo -n > 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 do link within separate sub-trees.
269 cd "$TOP/bupmeta.tmp"
273 WVPASS bup index -vv src/a src/b
274 WVPASS bup save -t -n src src/a src/b
275 hardlink-test-run-restore
276 echo ./src/a/1 > hardlink-sets.expected
277 echo ./src/b/1 >> hardlink-sets.expected
278 (cd src-restore && hardlink-sets .) > hardlink-sets.restored
279 WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
282 WVSTART 'meta --edit'
284 force-delete "$TOP/bupmeta.tmp"
285 mkdir "$TOP/bupmeta.tmp"
286 cd "$TOP/bupmeta.tmp"
288 WVPASS bup meta -cf src.meta src
290 WVPASS bup meta --edit --set-uid 0 src.meta | WVPASS bup meta -tvvf - \
291 | WVPASS grep -qE '^uid: 0'
292 WVPASS bup meta --edit --set-uid 1000 src.meta | WVPASS bup meta -tvvf - \
293 | WVPASS grep -qE '^uid: 1000'
295 WVPASS bup meta --edit --set-gid 0 src.meta | WVPASS bup meta -tvvf - \
296 | WVPASS grep -qE '^gid: 0'
297 WVPASS bup meta --edit --set-gid 1000 src.meta | WVPASS bup meta -tvvf - \
298 | WVPASS grep -qE '^gid: 1000'
300 WVPASS bup meta --edit --set-user foo src.meta | WVPASS bup meta -tvvf - \
301 | WVPASS grep -qE '^user: foo'
302 WVPASS bup meta --edit --set-user bar src.meta | WVPASS bup meta -tvvf - \
303 | WVPASS grep -qE '^user: bar'
304 WVPASS bup meta --edit --unset-user src.meta | WVPASS bup meta -tvvf - \
305 | WVPASS grep -qE '^user:'
306 WVPASS bup meta --edit --set-user bar --unset-user src.meta \
307 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user:'
308 WVPASS bup meta --edit --unset-user --set-user bar src.meta \
309 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user: bar'
311 WVPASS bup meta --edit --set-group foo src.meta | WVPASS bup meta -tvvf - \
312 | WVPASS grep -qE '^group: foo'
313 WVPASS bup meta --edit --set-group bar src.meta | WVPASS bup meta -tvvf - \
314 | WVPASS grep -qE '^group: bar'
315 WVPASS bup meta --edit --unset-group src.meta | WVPASS bup meta -tvvf - \
316 | WVPASS grep -qE '^group:'
317 WVPASS bup meta --edit --set-group bar --unset-group src.meta \
318 | WVPASS bup meta -tvvf - | WVPASS grep -qE '^group:'
319 WVPASS bup meta --edit --unset-group --set-group bar src.meta \
320 | WVPASS bup meta -tvvf - | grep -qE '^group: bar'
323 # Test ownership restoration (when not root or fakeroot).
325 if test "$(whoami)" == root; then
329 WVSTART 'metadata (restoration of ownership)'
330 force-delete "$TOP/bupmeta.tmp"
331 mkdir "$TOP/bupmeta.tmp"
332 cd "$TOP/bupmeta.tmp"
334 WVPASS bup meta -cf src.meta src
338 # Make sure we don't change (or try to change) the user when not root.
339 WVPASS bup meta --edit --set-user root ../src.meta | WVPASS bup meta -x
340 WVPASS bup xstat src | WVPASS grep -qvE '^user: root'
342 WVPASS bup meta --edit --unset-user --set-uid 0 ../src.meta \
344 WVPASS bup xstat src | grep -qvE '^user: root'
346 # Make sure we can restore one of the user's groups.
347 last_group="$(python -c 'import os,grp; \
348 print grp.getgrgid(os.getgroups()[0])[0]')"
350 WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
352 WVPASS bup xstat src | WVPASS grep -qE "^group: $last_group"
354 # Make sure we can restore one of the user's gids.
356 last_gid="$(echo ${user_gids/* /})"
358 WVPASS bup meta --edit --unset-group --set-gid "$last_gid" ../src.meta \
360 WVPASS bup xstat src | WVPASS grep -qE "^gid: $last_gid"
362 # Test --numeric-ids (gid).
364 current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:')
365 WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
366 | WVPASS bup meta -x --numeric-ids
367 new_gidx=$(bup xstat src | grep -e '^gid:')
368 WVPASSEQ "$current_gidx" "$new_gidx"
370 # Test that restoring an unknown user works.
371 unknown_user=$("$TOP"/t/unknown-owner --user)
373 current_uidx=$(bup meta -tvvf ../src.meta | grep -e '^uid:')
374 WVPASS bup meta --edit --set-user "$unknown_user" ../src.meta \
376 new_uidx=$(bup xstat src | grep -e '^uid:')
377 WVPASSEQ "$current_uidx" "$new_uidx"
379 # Test that restoring an unknown group works.
380 unknown_group=$("$TOP"/t/unknown-owner --group)
382 current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:')
383 WVPASS bup meta --edit --set-group "$unknown_group" ../src.meta \
385 new_gidx=$(bup xstat src | grep -e '^gid:')
386 WVPASSEQ "$current_gidx" "$new_gidx"
389 # Test ownership restoration (when root or fakeroot).
391 if test "$(whoami)" != root; then
395 WVSTART 'metadata (restoration of ownership as root)'
396 force-delete "$TOP/bupmeta.tmp"
397 mkdir "$TOP/bupmeta.tmp"
398 cd "$TOP/bupmeta.tmp"
400 chown 0:0 src # In case the parent dir is sgid, etc.
401 WVPASS bup meta -cf src.meta src
404 chmod 700 dest # so we can't accidentally do something insecure
407 # Make sure we can restore a uid.
408 WVPASS bup meta --edit --unset-user --set-uid 42 ../src.meta \
410 WVPASS bup xstat src | WVPASS grep -qE '^uid: 42'
412 # Make sure we can restore a gid.
413 WVPASS bup meta --edit --unset-group --set-gid 42 ../src.meta \
415 WVPASS bup xstat src | WVPASS grep -qE '^gid: 42'
417 some_user=$("$TOP"/t/some-owner --user)
418 some_group=$("$TOP"/t/some-owner --group)
420 # Try to restore a user (and see that user trumps uid when uid is not 0).
421 WVPASS bup meta --edit --set-uid 42 --set-user "$some_user" ../src.meta \
423 WVPASS bup xstat src | WVPASS grep -qE "^user: $some_user"
425 # Try to restore a group (and see that group trumps gid when gid is not 0).
426 WVPASS bup meta --edit --set-gid 42 --set-group "$some_group" ../src.meta \
428 WVPASS bup xstat src | WVPASS grep -qE "^group: $some_user"
430 # Make sure a uid of 0 trumps a non-root user.
431 WVPASS bup meta --edit --set-user "$some_user" ../src.meta \
433 WVPASS bup xstat src | WVPASS grep -qvE "^user: $some_user"
434 WVPASS bup xstat src | WVPASS grep -qE "^uid: 0"
436 # Make sure a gid of 0 trumps a non-root group.
437 WVPASS bup meta --edit --set-group "$some_user" ../src.meta \
439 WVPASS bup xstat src | WVPASS grep -qvE "^group: $some_group"
440 WVPASS bup xstat src | WVPASS grep -qE "^gid: 0"
442 # Test --numeric-ids (gid). Note the name 'root' is not handled
443 # specially, so we use that here as the test group name. We
444 # assume that the root group's gid is never 42.
446 WVPASS bup meta --edit --set-group root --set-gid 42 ../src.meta \
447 | WVPASS bup meta -x --numeric-ids
448 new_gidx=$(bup xstat src | grep -e '^gid:')
449 WVPASSEQ "$new_gidx" 'gid: 42'
451 # Test --numeric-ids (uid). Note the name 'root' is not handled
452 # specially, so we use that here as the test user name. We assume
453 # that the root user's uid is never 42.
455 WVPASS bup meta --edit --set-user root --set-uid 42 ../src.meta \
456 | WVPASS bup meta -x --numeric-ids
457 new_uidx=$(bup xstat src | grep -e '^uid:')
458 WVPASSEQ "$new_uidx" 'uid: 42'
460 # Test that restoring an unknown user works.
461 unknown_user=$("$TOP"/t/unknown-owners --user)
463 WVPASS bup meta --edit --set-uid 42 --set-user "$unknown_user" ../src.meta \
465 new_uidx=$(bup xstat src | grep -e '^uid:')
466 WVPASSEQ "$new_uidx" 'uid: 42'
468 # Test that restoring an unknown group works.
469 unknown_group=$("$TOP"/t/unknown-owners --group)
471 WVPASS bup meta --edit \
472 --set-gid 42 --set-group "$unknown_group" ../src.meta \
474 new_gidx=$(bup xstat src | grep -e '^gid:')
475 WVPASSEQ "$new_gidx" 'gid: 42'
478 # Root-only tests that require an FS with all the trimmings: ACLs,
479 # Linux attr, Linux xattr, etc.
480 if actually-root; then
483 # Some cleanup handled in universal-cleanup() above.
484 # These tests are only likely to work under Linux for now
486 [[ $(uname) =~ Linux ]] || exit 0
488 WVSTART 'meta - general (as root)'
490 cd "$TOP/bupmeta.tmp"
492 umount testfs || true
493 dd if=/dev/zero of=testfs.img bs=1M count=32
494 mke2fs -F -j -m 0 testfs.img
496 mount -o loop,acl,user_xattr testfs.img testfs
497 # Hide, so that tests can't create risks.
498 chown root:root testfs
501 umount testfs-limited || true
502 dd if=/dev/zero of=testfs-limited.img bs=1M count=32
503 mkfs -t vfat testfs-limited.img
505 mount -o loop,uid=root,gid=root,umask=0077 \
506 testfs-limited.img testfs-limited
508 #cp -a src testfs/src
509 cp -pPR src testfs/src
510 (cd testfs && test-src-create-extract)
512 WVSTART 'meta - atime (as root)'
513 force-delete testfs/src
518 PYTHONPATH="$TOP/lib" \
519 python -c "from bup import xstat; \
520 x = xstat.timespec_to_nsecs((42, 0));\
521 xstat.utime('testfs/src/foo', (x, x));\
522 xstat.utime('testfs/src/bar', (x, x));"
524 WVPASS bup meta -v --create --recurse --file src.meta src
525 bup meta -tvf src.meta
527 force-delete src-restore
530 WVPASS bup meta --extract --file ../src.meta
531 WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
532 WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
533 # Test start/finish extract.
535 WVPASS bup meta --start-extract --file ../src.meta
537 WVPASS bup meta --finish-extract --file ../src.meta
538 WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
539 WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
542 WVSTART 'meta - Linux attr (as root)'
543 force-delete testfs/src
548 chattr +acdeijstuADST testfs/src/foo
549 chattr +acdeijstuADST testfs/src/bar
550 (cd testfs && test-src-create-extract)
551 # Test restoration to a limited filesystem (vfat).
553 WVPASS bup meta --create --recurse --file testfs/src.meta \
555 force-delete testfs-limited/src-restore
556 mkdir testfs-limited/src-restore
557 cd testfs-limited/src-restore
558 WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
559 | WVPASS grep -e '^Linux chattr:' \
561 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
565 WVSTART 'meta - Linux xattr (as root)'
566 force-delete testfs/src
571 attr -s foo -V bar testfs/src/foo
572 attr -s foo -V bar testfs/src/bar
573 (cd testfs && test-src-create-extract)
575 # Test restoration to a limited filesystem (vfat).
577 WVPASS bup meta --create --recurse --file testfs/src.meta \
579 force-delete testfs-limited/src-restore
580 mkdir testfs-limited/src-restore
581 cd testfs-limited/src-restore
582 WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
583 | WVPASS grep -e '^xattr\.set:' \
585 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
589 WVSTART 'meta - POSIX.1e ACLs (as root)'
590 force-delete testfs/src
595 setfacl -m u:root:r testfs/src/foo
596 setfacl -m u:root:r testfs/src/bar
597 (cd testfs && test-src-create-extract)
599 # Test restoration to a limited filesystem (vfat).
601 WVPASS bup meta --create --recurse --file testfs/src.meta \
603 force-delete testfs-limited/src-restore
604 mkdir testfs-limited/src-restore
605 cd testfs-limited/src-restore
606 WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
607 | WVPASS grep -e '^POSIX1e ACL applyto:' \
609 'import sys; exit(not len(sys.stdin.readlines()) == 2)'