]> arthur.barton.de Git - bup.git/blob - t/test-meta.sh
573ad8c4083ea1a1fcee4668e2803ead9346868d
[bup.git] / t / test-meta.sh
1 #!/usr/bin/env bash
2 . wvtest-bup.sh || exit $?
3 . t/lib.sh || exit $?
4
5 set -o pipefail
6
7 root_status="$(t/root-status)" || exit $?
8
9 TOP="$(WVPASS pwd)" || exit $?
10 tmpdir="$(WVPASS wvmktempdir)" || exit $?
11 export BUP_DIR="$tmpdir/bup"
12
13 # Assume that mvmktempdir will always use the same dir.
14 timestamp_resolutions="$(t/ns-timestamp-resolutions "$tmpdir/canary")" \
15     || exit $?
16 atime_resolution="$(echo $timestamp_resolutions | WVPASS cut -d' ' -f 1)" \
17     || exit $?
18 mtime_resolution="$(echo $timestamp_resolutions | WVPASS cut -d' ' -f 2)" \
19     || exit $?
20 WVPASS rm "$tmpdir/canary"
21
22 bup()
23 {
24     "$TOP/bup" "$@"
25 }
26
27 hardlink-sets()
28 {
29     "$TOP/t/hardlink-sets" "$@"
30 }
31
32 id-other-than()
33 {
34     "$TOP/t/id-other-than" "$@"
35 }
36
37 # Very simple metadata tests -- create a test tree then check that bup
38 # meta can reproduce the metadata correctly (according to bup xstat)
39 # via create, extract, start-extract, and finish-extract.  The current
40 # tests are crude, and this does not fully test devices, varying
41 # users/groups, acls, attrs, etc.
42
43 genstat()
44 {
45     (
46         export PATH="$TOP:$PATH" # pick up bup
47         # Skip atime (test elsewhere) to avoid the observer effect.
48         WVPASS find . | WVPASS sort \
49             | WVPASS xargs bup xstat \
50             --mtime-resolution "$mtime_resolution"ns \
51             --exclude-fields ctime,atime,size
52     )
53 }
54
55 test-src-create-extract()
56 {
57     # Test bup meta create/extract for ./src -> ./src-restore.
58     # Also writes to ./src-stat and ./src-restore-stat.
59     (
60         (WVPASS cd src; WVPASS genstat) > src-stat || exit $?
61         WVPASS bup meta --create --recurse --file src.meta src
62         # Test extract.
63         WVPASS force-delete src-restore
64         WVPASS mkdir src-restore
65         WVPASS cd src-restore
66         WVPASS bup meta --extract --file ../src.meta
67         WVPASS test -d src
68         (WVPASS cd src; WVPASS genstat >../../src-restore-stat) || exit $?
69         WVPASS diff -U5 ../src-stat ../src-restore-stat
70         # Test start/finish extract.
71         WVPASS force-delete src
72         WVPASS bup meta --start-extract --file ../src.meta
73         WVPASS test -d src
74         WVPASS bup meta --finish-extract --file ../src.meta
75         (WVPASS cd src; WVPASS genstat >../../src-restore-stat) || exit $?
76         WVPASS diff -U5 ../src-stat ../src-restore-stat
77     )
78 }
79
80 test-src-save-restore()
81 {
82     # Test bup save/restore metadata for ./src -> ./src-restore.  Also
83     # writes to BUP_DIR.  Note that for now this just tests the
84     # restore below src/, in order to avoid having to worry about
85     # operations that require root (like chown /home).
86     (
87         WVPASS rm -rf "$BUP_DIR"
88         WVPASS bup init
89         WVPASS bup index src
90         WVPASS bup save -t -n src src
91         # Test extract.
92         WVPASS force-delete src-restore
93         WVPASS mkdir 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/
97         WVPASS rm -rf src.bup
98     )
99 }
100
101 setup-test-tree()
102 {
103     WVPASS make PREFIX="$tmpdir"/src install
104
105     # Add some hard links for the general tests.
106     (
107         WVPASS cd "$tmpdir"/src
108         WVPASS touch hardlink-target
109         WVPASS ln hardlink-target hardlink-1
110         WVPASS ln hardlink-target hardlink-2
111         WVPASS ln hardlink-target hardlink-3
112     ) || exit $?
113
114     # Add some trivial files for the index, modify, save tests.
115     (
116         WVPASS cd "$tmpdir"/src
117         WVPASS mkdir volatile
118         WVPASS touch volatile/{1,2,3}
119     ) || exit $?
120
121     # Regression test for metadata sort order.  Previously, these two
122     # entries would sort in the wrong order because the metadata
123     # entries were being sorted by mangled name, but the index isn't.
124     WVPASS dd if=/dev/zero of="$tmpdir"/src/foo bs=1k count=33
125     WVPASS touch -t 201111111111 "$tmpdir"/src/foo
126     WVPASS touch -t 201112121111 "$tmpdir"/src/foo-bar
127
128     t/mksock "$tmpdir"/src/test-socket || true
129 }
130
131 # Use the test tree to check bup meta.
132 WVSTART 'meta --create/--extract'
133 (
134     tmpdir="$(WVPASS wvmktempdir)" || exit $?
135     export BUP_DIR="$tmpdir/bup"
136     WVPASS setup-test-tree
137     WVPASS cd "$tmpdir"
138     WVPASS test-src-create-extract
139
140     # Test a top-level file (not dir).
141     WVPASS touch src-file
142     WVPASS bup meta -cf src-file.meta src-file
143     WVPASS mkdir dest
144     WVPASS cd dest
145     WVPASS bup meta -xf ../src-file.meta
146     WVPASS rm -r "$tmpdir"
147 ) || exit $?
148
149 # Use the test tree to check bup save/restore metadata.
150 WVSTART 'metadata save/restore (general)'
151 (
152     tmpdir="$(WVPASS wvmktempdir)" || exit $?
153     export BUP_DIR="$tmpdir/bup"
154     WVPASS setup-test-tree
155     WVPASS cd "$tmpdir"
156     WVPASS test-src-save-restore
157
158     # Test a deeper subdir/ to make sure top-level non-dir metadata is
159     # restored correctly.  We need at least one dir and one non-dir at
160     # the "top-level".
161     WVPASS test -d src/bin
162     WVPASS test -f src/bin/bup
163     WVPASS rm -rf "$BUP_DIR"
164     WVPASS bup init
165     WVPASS touch -t 201111111111 src-restore # Make sure the top won't match.
166     WVPASS bup index src
167     WVPASS bup save -t -n src src
168     WVPASS force-delete src-restore
169     WVPASS bup restore -C src-restore "/src/latest$(pwd)/src/lib/"
170     WVPASS touch -t 201211111111 src-restore # Make sure the top won't match.
171     # Check that the only difference is the top dir.
172     WVFAIL $TOP/t/compare-trees -c src/lib/ src-restore/ > tmp-compare-trees
173     WVPASSEQ $(cat tmp-compare-trees | wc -l) 1
174     # Note: OS X rsync itemize output is currently only 9 chars, not 11.
175     expected_diff_rx='^\.d\.\.t.\.\.\.\.?\.? \./$'
176     if ! grep -qE "$expected_diff_rx" tmp-compare-trees; then
177         echo -n 'tmp-compare-trees: ' 1>&2
178         cat tmp-compare-trees 1>&2
179     fi
180     WVPASS grep -qE "$expected_diff_rx" tmp-compare-trees
181     WVPASS rm -r "$tmpdir"
182 ) || exit $?
183
184 # Test that we pull the index (not filesystem) metadata for any
185 # unchanged files whenever we're saving other files in a given
186 # directory.
187 WVSTART 'metadata save/restore (using index metadata)'
188 (
189     tmpdir="$(WVPASS wvmktempdir)" || exit $?
190     export BUP_DIR="$tmpdir/bup"
191     WVPASS setup-test-tree
192     WVPASS cd "$tmpdir"
193
194     # ...for now -- might be a problem with hardlink restores that was
195     # causing noise wrt this test.
196     WVPASS rm -rf src/hardlink*
197
198     # Pause here to keep the filesystem changes far enough away from
199     # the first index run that bup won't cap their index timestamps
200     # (see "bup help index" for more information).  Without this
201     # sleep, the compare-trees test below "Bup should *not* pick up
202     # these metadata..." may fail.
203     WVPASS sleep 1
204
205     WVPASS rm -rf "$BUP_DIR"
206     WVPASS bup init
207     WVPASS bup index src
208     WVPASS bup save -t -n src src
209
210     WVPASS force-delete src-restore-1
211     WVPASS mkdir src-restore-1
212     WVPASS bup restore -C src-restore-1 "/src/latest$(pwd)/"
213     WVPASS test -d src-restore-1/src
214     WVPASS "$TOP/t/compare-trees" -c src/ src-restore-1/src/
215
216     WVPASS echo "blarg" > src/volatile/1
217     WVPASS cp -a src/volatile/1 src-restore-1/src/volatile/
218     WVPASS bup index src
219
220     # Bup should *not* pick up these metadata changes.
221     WVPASS touch src/volatile/2
222
223     WVPASS bup save -t -n src src
224
225     WVPASS force-delete src-restore-2
226     WVPASS mkdir src-restore-2
227     WVPASS bup restore -C src-restore-2 "/src/latest$(pwd)/"
228     WVPASS test -d src-restore-2/src
229     WVPASS "$TOP/t/compare-trees" -c src-restore-1/src/ src-restore-2/src/
230
231     WVPASS rm -r "$tmpdir"
232
233 ) || exit $?
234
235
236 setup-hardlink-test()
237 {
238     WVPASS rm -rf "$tmpdir/src" "$BUP_DIR"
239     WVPASS bup init
240     WVPASS mkdir "$tmpdir/src"
241 }
242
243 hardlink-test-run-restore()
244 {
245     WVPASS force-delete src-restore
246     WVPASS mkdir src-restore
247     WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
248     WVPASS test -d src-restore/src
249 }
250
251 # Test hardlinks more carefully.
252 WVSTART 'metadata save/restore (hardlinks)'
253 (
254     tmpdir="$(WVPASS wvmktempdir)" || exit $?    
255     export BUP_DIR="$tmpdir/bup"
256
257     WVPASS setup-hardlink-test
258     WVPASS cd "$tmpdir"
259     
260     # Test trivial case - single hardlink.
261     (
262         WVPASS cd src
263         WVPASS touch hardlink-target
264         WVPASS ln hardlink-target hardlink-1
265     ) || exit $?
266     WVPASS bup index src
267     WVPASS bup save -t -n src src
268     WVPASS hardlink-test-run-restore
269     WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
270
271     # Test the case where the hardlink hasn't changed, but the tree
272     # needs to be saved again. i.e. the save-cmd.py "if hashvalid:"
273     # case.
274     (
275         WVPASS cd src
276         WVPASS echo whatever > something-new
277     ) || exit $?
278     WVPASS bup index src
279     WVPASS bup save -t -n src src
280     WVPASS hardlink-test-run-restore
281     WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
282
283     # Test hardlink changes between index runs.
284     #
285     WVPASS setup-hardlink-test
286     WVPASS cd src
287     WVPASS touch hardlink-target-a
288     WVPASS touch hardlink-target-b
289     WVPASS ln hardlink-target-a hardlink-b-1
290     WVPASS ln hardlink-target-a hardlink-a-1
291     WVPASS cd ..
292     WVPASS bup index -vv src
293     WVPASS rm src/hardlink-b-1
294     WVPASS ln src/hardlink-target-b src/hardlink-b-1
295     WVPASS bup index -vv src
296     WVPASS bup save -t -n src src
297     WVPASS hardlink-test-run-restore
298     WVPASS echo ./src/hardlink-a-1 > hardlink-sets.expected
299     WVPASS echo ./src/hardlink-target-a >> hardlink-sets.expected
300     WVPASS echo >> hardlink-sets.expected
301     WVPASS echo ./src/hardlink-b-1 >> hardlink-sets.expected
302     WVPASS echo ./src/hardlink-target-b >> hardlink-sets.expected
303     (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
304         || exit $?
305     WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
306
307     # Test hardlink changes between index and save -- hardlink set [a
308     # b c d] changes to [a b] [c d].  At least right now bup should
309     # notice and recreate the latter.
310     WVPASS setup-hardlink-test
311     WVPASS cd "$tmpdir"/src
312     WVPASS touch a
313     WVPASS ln a b
314     WVPASS ln a c
315     WVPASS ln a d
316     WVPASS cd ..
317     WVPASS bup index -vv src
318     WVPASS rm src/c src/d
319     WVPASS touch src/c
320     WVPASS ln src/c src/d
321     WVPASS bup save -t -n src src
322     WVPASS hardlink-test-run-restore
323     WVPASS echo ./src/a > hardlink-sets.expected
324     WVPASS echo ./src/b >> hardlink-sets.expected
325     WVPASS echo >> hardlink-sets.expected
326     WVPASS echo ./src/c >> hardlink-sets.expected
327     WVPASS echo ./src/d >> hardlink-sets.expected
328     (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
329         || exit $?
330     WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
331
332     # Test that we don't link outside restore tree.
333     WVPASS setup-hardlink-test
334     WVPASS cd "$tmpdir"
335     WVPASS mkdir src/a src/b
336     WVPASS touch src/a/1
337     WVPASS ln src/a/1 src/b/1
338     WVPASS bup index -vv src
339     WVPASS bup save -t -n src src
340     WVPASS force-delete src-restore
341     WVPASS mkdir src-restore
342     WVPASS bup restore -C src-restore "/src/latest$(pwd)/src/a/"
343     WVPASS test -e src-restore/1
344     WVPASS echo -n > hardlink-sets.expected
345     (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
346         || exit $?
347     WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
348
349     # Test that we do link within separate sub-trees.
350     WVPASS setup-hardlink-test
351     WVPASS cd "$tmpdir"
352     WVPASS mkdir src/a src/b
353     WVPASS touch src/a/1
354     WVPASS ln src/a/1 src/b/1
355     WVPASS bup index -vv src/a src/b
356     WVPASS bup save -t -n src src/a src/b
357     WVPASS hardlink-test-run-restore
358     WVPASS echo ./src/a/1 > hardlink-sets.expected
359     WVPASS echo ./src/b/1 >> hardlink-sets.expected
360     (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
361         || exit $?
362     WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
363
364     WVPASS rm -r "$tmpdir"
365
366 ) || exit $?
367
368 WVSTART 'meta --edit'
369 (
370     tmpdir="$(WVPASS wvmktempdir)" || exit $?    
371     WVPASS cd "$tmpdir"
372     WVPASS mkdir src
373
374     WVPASS bup meta -cf src.meta src
375
376     WVPASS bup meta --edit --set-uid 0 src.meta | WVPASS bup meta -tvvf - \
377         | WVPASS grep -qE '^uid: 0'
378     WVPASS bup meta --edit --set-uid 1000 src.meta | WVPASS bup meta -tvvf - \
379         | WVPASS grep -qE '^uid: 1000'
380
381     WVPASS bup meta --edit --set-gid 0 src.meta | WVPASS bup meta -tvvf - \
382         | WVPASS grep -qE '^gid: 0'
383     WVPASS bup meta --edit --set-gid 1000 src.meta | WVPASS bup meta -tvvf - \
384         | WVPASS grep -qE '^gid: 1000'
385
386     WVPASS bup meta --edit --set-user foo src.meta | WVPASS bup meta -tvvf - \
387         | WVPASS grep -qE '^user: foo'
388     WVPASS bup meta --edit --set-user bar src.meta | WVPASS bup meta -tvvf - \
389         | WVPASS grep -qE '^user: bar'
390     WVPASS bup meta --edit --unset-user src.meta | WVPASS bup meta -tvvf - \
391         | WVPASS grep -qE '^user:'
392     WVPASS bup meta --edit --set-user bar --unset-user src.meta \
393         | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user:'
394     WVPASS bup meta --edit --unset-user --set-user bar src.meta \
395         | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user: bar'
396
397     WVPASS bup meta --edit --set-group foo src.meta | WVPASS bup meta -tvvf - \
398         | WVPASS grep -qE '^group: foo'
399     WVPASS bup meta --edit --set-group bar src.meta | WVPASS bup meta -tvvf - \
400         | WVPASS grep -qE '^group: bar'
401     WVPASS bup meta --edit --unset-group src.meta | WVPASS bup meta -tvvf - \
402         | WVPASS grep -qE '^group:'
403     WVPASS bup meta --edit --set-group bar --unset-group src.meta \
404         | WVPASS bup meta -tvvf - | WVPASS grep -qE '^group:'
405     WVPASS bup meta --edit --unset-group --set-group bar src.meta \
406         | WVPASS bup meta -tvvf - | grep -qE '^group: bar'
407
408     WVPASS rm -r "$tmpdir"
409
410 ) || exit $?
411
412 WVSTART 'meta --no-recurse'
413 (
414     tmpdir="$(WVPASS wvmktempdir)" || exit $?    
415     WVPASS cd "$tmpdir"
416     WVPASS mkdir src
417     WVPASS mkdir src/foo
418     WVPASS touch src/foo/{1,2,3}
419     WVPASS bup meta -cf src.meta src
420     WVPASSEQ "$(LC_ALL=C; bup meta -tf src.meta | sort)" "src/
421 src/foo/
422 src/foo/1
423 src/foo/2
424 src/foo/3"
425     WVPASS bup meta --no-recurse -cf src.meta src
426     WVPASSEQ "$(LC_ALL=C; bup meta -tf src.meta | sort)" "src/"
427     WVPASS rm -r "$tmpdir"
428 ) || exit $?
429
430 # Test ownership restoration (when not root or fakeroot).
431 (
432     if [ "$root_status" != none ]; then
433         exit 0
434     fi
435
436     tmpdir="$(WVPASS wvmktempdir)" || exit $?    
437
438     first_group="$(WVPASS python -c 'import os,grp; \
439       print grp.getgrgid(os.getgroups()[0])[0]')" || exit $?
440     last_group="$(python -c 'import os,grp; \
441       print grp.getgrgid(os.getgroups()[-1])[0]')" || exit $?
442     last_group_erx="$(escape-erx "$last_group")"
443
444     WVSTART 'metadata (restoration of ownership)'
445     WVPASS cd "$tmpdir"
446     WVPASS touch src
447     # Some systems always assign the parent dir group to new paths
448     # (sgid).  Make sure the group is one we're in.
449     WVPASS chgrp -R "$first_group" src
450
451     WVPASS bup meta -cf src.meta src
452
453     WVPASS mkdir dest
454     WVPASS cd dest
455     # Make sure we don't change (or try to change) the user when not root.
456     WVPASS bup meta --edit --set-user root ../src.meta | WVPASS bup meta -x
457     WVPASS bup xstat src | WVPASS grep -qvE '^user: root'
458     WVPASS rm -rf src
459     WVPASS bup meta --edit --unset-user --set-uid 0 ../src.meta \
460         | WVPASS bup meta -x
461     WVPASS bup xstat src | WVPASS grep -qvE '^user: root'
462
463     # Make sure we can restore one of the user's groups.
464     WVPASS rm -rf src
465     WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
466         | WVPASS bup meta -x
467     WVPASS bup xstat src | WVPASS grep -qE "^group: $last_group_erx"
468
469     # Make sure we can restore one of the user's gids.
470     user_gids="$(id -G)" || exit $?
471     last_gid="$(echo ${user_gids/* /})" || exit $?
472     WVPASS rm -rf src
473     WVPASS bup meta --edit --unset-group --set-gid "$last_gid" ../src.meta \
474         | WVPASS bup meta -x
475     WVPASS bup xstat src | WVPASS grep -qE "^gid: $last_gid"
476
477     # Test --numeric-ids (gid).
478     WVPASS rm -rf src
479     current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:') || exit $?
480     WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
481         | WVPASS bup meta -x --numeric-ids
482     new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
483     WVPASSEQ "$current_gidx" "$new_gidx"
484
485     # Test that restoring an unknown user works.
486     unknown_user=$("$TOP"/t/unknown-owner --user) || exit $?
487     WVPASS rm -rf src
488     current_uidx=$(bup meta -tvvf ../src.meta | grep -e '^uid:') || exit $?
489     WVPASS bup meta --edit --set-user "$unknown_user" ../src.meta \
490         | WVPASS bup meta -x
491     new_uidx=$(bup xstat src | grep -e '^uid:') || exit $?
492     WVPASSEQ "$current_uidx" "$new_uidx"
493
494     # Test that restoring an unknown group works.
495     unknown_group=$("$TOP"/t/unknown-owner --group) || exit $?
496     WVPASS rm -rf src
497     current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:') || exit $?
498     WVPASS bup meta --edit --set-group "$unknown_group" ../src.meta \
499         | WVPASS bup meta -x
500     new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
501     WVPASSEQ "$current_gidx" "$new_gidx"
502
503     WVPASS rm -r "$tmpdir"
504
505 ) || exit $?
506
507 # Test ownership restoration (when root or fakeroot).
508 (
509     if [ "$root_status" = none ]; then
510         exit 0
511     fi
512
513     tmpdir="$(WVPASS wvmktempdir)" || exit $?    
514
515     uid=$(WVPASS id -un) || exit $?
516     gid=$(WVPASS id -gn) || exit $?
517
518     WVSTART 'metadata (restoration of ownership as root)'
519     WVPASS cd "$tmpdir"
520     WVPASS touch src
521     WVPASS chown "$uid:$gid" src # In case the parent dir is sgid, etc.
522     WVPASS bup meta -cf src.meta src
523
524     WVPASS mkdir dest
525     WVPASS chmod 700 dest # so we can't accidentally do something insecure
526     WVPASS cd dest
527
528     other_uinfo="$(id-other-than --user "$uid")" || exit $?
529     other_user="${other_uinfo%%:*}"
530     other_uid="${other_uinfo##*:}"
531
532     other_ginfo="$(id-other-than --group "$gid")" || exit $?
533     other_group="${other_ginfo%%:*}"
534     other_gid="${other_ginfo##*:}"
535
536     # Make sure we can restore a uid (must be in /etc/passwd b/c cygwin).
537     WVPASS bup meta --edit --unset-user --set-uid "$other_uid" ../src.meta \
538         | WVPASS bup meta -x
539     WVPASS bup xstat src | WVPASS grep -qE "^uid: $other_uid"
540
541     # Make sure we can restore a gid (must be in /etc/group b/c cygwin).
542     WVPASS bup meta --edit --unset-group --set-gid "$other_gid" ../src.meta \
543         | WVPASS bup meta -x
544     WVPASS bup xstat src | WVPASS grep -qE "^gid: $other_gid"
545
546     other_uinfo2="$(id-other-than --user "$(id -un)" "$other_user")" || exit $?
547     other_user2="${other_uinfo2%%:*}"
548     other_user2_erx="$(escape-erx "$other_user2")" || exit $?
549     other_uid2="${other_uinfo2##*:}"
550
551     other_ginfo2="$(id-other-than --group "$(id -gn)" "$other_group")" || exit $?
552     other_group2="${other_ginfo2%%:*}"
553     other_group2_erx="$(escape-erx "$other_group2")" || exit $?
554     other_gid2="${other_ginfo2##*:}"
555
556     # Try to restore a user (and see that user trumps uid when uid is not 0).
557     WVPASS bup meta --edit \
558         --set-uid "$other_uid" --set-user "$other_user2" ../src.meta \
559         | WVPASS bup meta -x
560     WVPASS bup xstat src | WVPASS grep -qE "^user: $other_user2_erx"
561
562     # Try to restore a group (and see that group trumps gid when gid is not 0).
563     WVPASS bup meta --edit \
564         --set-gid "$other_gid" --set-group "$other_group2" ../src.meta \
565         | WVPASS bup meta -x
566     WVPASS bup xstat src | WVPASS grep -qE "^group: $other_group2_erx"
567
568     # Test --numeric-ids (uid).  Note the name 'root' is not handled
569     # specially, so we use that here as the test user name.  We assume
570     # that the root user's uid is never 42.
571     WVPASS rm -rf src
572     WVPASS bup meta --edit --set-user root --set-uid "$other_uid" ../src.meta \
573         | WVPASS bup meta -x --numeric-ids
574     new_uidx=$(bup xstat src | grep -e '^uid:') || exit $?
575     WVPASSEQ "$new_uidx" "uid: $other_uid"
576
577     # Test --numeric-ids (gid).  Note the name 'root' is not handled
578     # specially, so we use that here as the test group name.  We
579     # assume that the root group's gid is never 42.
580     WVPASS rm -rf src
581     WVPASS bup meta --edit --set-group root --set-gid "$other_gid" ../src.meta \
582         | WVPASS bup meta -x --numeric-ids
583     new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
584     WVPASSEQ "$new_gidx" "gid: $other_gid"
585
586     # Test that restoring an unknown user works.
587     unknown_user=$("$TOP"/t/unknown-owner --user) || exit $?
588     WVPASS rm -rf src
589     WVPASS bup meta --edit \
590         --set-uid "$other_uid" --set-user "$unknown_user" ../src.meta \
591         | WVPASS bup meta -x
592     new_uidx=$(bup xstat src | grep -e '^uid:') || exit $?
593     WVPASSEQ "$new_uidx" "uid: $other_uid"
594
595     # Test that restoring an unknown group works.
596     unknown_group=$("$TOP"/t/unknown-owner --group) || exit $?
597     WVPASS rm -rf src
598     WVPASS bup meta --edit \
599         --set-gid "$other_gid" --set-group "$unknown_group" ../src.meta \
600         | WVPASS bup meta -x
601     new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
602     WVPASSEQ "$new_gidx" "gid: $other_gid"
603
604     if ! [[ $(uname) =~ CYGWIN ]]; then
605         # For now, skip these on Cygwin because it doesn't allow
606         # restoring an unknown uid/gid.
607
608         # Make sure a uid of 0 trumps a non-root user.
609         WVPASS bup meta --edit --set-user "$other_user2" ../src.meta \
610             | WVPASS bup meta -x
611         WVPASS bup xstat src | WVPASS grep -qvE "^user: $other_user2_erx"
612         WVPASS bup xstat src | WVPASS grep -qE "^uid: 0"
613
614         # Make sure a gid of 0 trumps a non-root group.
615         WVPASS bup meta --edit --set-group "$other_group2" ../src.meta \
616             | WVPASS bup meta -x
617         WVPASS bup xstat src | WVPASS grep -qvE "^group: $other_group2_erx"
618         WVPASS bup xstat src | WVPASS grep -qE "^gid: 0"
619     fi
620
621     WVPASS rm -r "$tmpdir"
622
623 ) || exit $?
624
625
626 # Root-only tests that require an FS with all the trimmings: ACLs,
627 # Linux attr, Linux xattr, etc.
628 if [ "$root_status" = root ]; then
629     (
630         # Some cleanup handled in universal-cleanup() above.
631         # These tests are only likely to work under Linux for now
632         # (patches welcome).
633         [[ $(uname) =~ Linux ]] || exit 0
634
635         if ! modprobe loop; then
636             echo 'Unable to load loopback module; skipping dependent tests.' 1>&2
637             exit 0
638         fi
639
640         testfs="$(WVPASS wvmkmountpt)" || exit $?
641         testfs_limited="$(WVPASS wvmkmountpt)" || exit $?
642         tmpdir="$(WVPASS wvmktempdir)" || exit $?
643         export BUP_DIR="$tmpdir/bup"
644
645         WVSTART 'meta - general (as root)'
646         WVPASS setup-test-tree
647         WVPASS cd "$tmpdir"
648
649         umount "$testfs"
650         WVPASS dd if=/dev/zero of=testfs.img bs=1M count=32
651         # Make sure we have all the options the chattr test needs
652         # (i.e. create a "normal" ext4 filesystem).
653         WVPASS mke2fs -F -m 0 \
654             -I 256 \
655             -O has_journal,extent,huge_file,flex_bg,uninit_bg,dir_nlink,extra_isize \
656             testfs.img
657         WVPASS mount -o loop,acl,user_xattr testfs.img "$testfs"
658         # Hide, so that tests can't create risks.
659         WVPASS chown root:root "$testfs"
660         WVPASS chmod 0700 "$testfs"
661
662         umount "$testfs_limited"
663         WVPASS dd if=/dev/zero of=testfs-limited.img bs=1M count=32
664         WVPASS mkfs -t vfat testfs-limited.img
665         WVPASS mount -o loop,uid=root,gid=root,umask=0077 \
666             testfs-limited.img "$testfs_limited"
667
668         WVPASS cp -pPR src "$testfs"/src
669         (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
670
671         WVSTART 'meta - atime (as root)'
672         WVPASS force-delete "$testfs"/src
673         WVPASS mkdir "$testfs"/src
674         (
675             WVPASS mkdir "$testfs"/src/foo
676             WVPASS touch "$testfs"/src/bar
677             PYTHONPATH="$TOP/lib" \
678                 WVPASS python -c "from bup import xstat; \
679                 x = xstat.timespec_to_nsecs((42, 0));\
680                    xstat.utime('$testfs/src/foo', (x, x));\
681                    xstat.utime('$testfs/src/bar', (x, x));"
682             WVPASS cd "$testfs"
683             WVPASS bup meta -v --create --recurse --file src.meta src
684             WVPASS bup meta -tvf src.meta
685             # Test extract.
686             WVPASS force-delete src-restore
687             WVPASS mkdir src-restore
688             WVPASS cd src-restore
689             WVPASS bup meta --extract --file ../src.meta
690             WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
691             WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
692             # Test start/finish extract.
693             WVPASS force-delete src
694             WVPASS bup meta --start-extract --file ../src.meta
695             WVPASS test -d src
696             WVPASS bup meta --finish-extract --file ../src.meta
697             WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
698             WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
699         ) || exit $?
700
701         WVSTART 'meta - Linux attr (as root)'
702         WVPASS force-delete "$testfs"/src
703         WVPASS mkdir "$testfs"/src
704         (
705             WVPASS touch "$testfs"/src/foo
706             WVPASS mkdir "$testfs"/src/bar
707             WVPASS chattr +acdeijstuADST "$testfs"/src/foo
708             WVPASS chattr +acdeijstuADST "$testfs"/src/bar
709             (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
710             # Test restoration to a limited filesystem (vfat).
711             (
712                 WVPASS bup meta --create --recurse --file "$testfs"/src.meta \
713                     "$testfs"/src
714                 WVPASS force-delete "$testfs_limited"/src-restore
715                 WVPASS mkdir "$testfs_limited"/src-restore
716                 WVPASS cd "$testfs_limited"/src-restore
717                 WVFAIL bup meta --extract --file "$testfs"/src.meta 2>&1 \
718                     | WVPASS grep -e '^Linux chattr:' \
719                     | WVPASS python -c \
720                     'import sys; exit(not len(sys.stdin.readlines()) == 3)'
721             ) || exit $?
722         ) || exit $?
723
724         WVSTART 'meta - Linux xattr (as root)'
725         WVPASS force-delete "$testfs"/src
726         WVPASS mkdir "$testfs"/src
727         WVPASS touch "$testfs"/src/foo
728         WVPASS mkdir "$testfs"/src/bar
729         WVPASS attr -s foo -V bar "$testfs"/src/foo
730         WVPASS attr -s foo -V bar "$testfs"/src/bar
731         (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
732
733         # Test restoration to a limited filesystem (vfat).
734         (
735             WVPASS bup meta --create --recurse --file "$testfs"/src.meta \
736                 "$testfs"/src
737             WVPASS force-delete "$testfs_limited"/src-restore
738             WVPASS mkdir "$testfs_limited"/src-restore
739             WVPASS cd "$testfs_limited"/src-restore
740             WVFAIL bup meta --extract --file "$testfs"/src.meta
741             WVFAIL bup meta --extract --file "$testfs"/src.meta 2>&1 \
742                 | WVPASS grep -e "^xattr\.set '" \
743                 | WVPASS python -c \
744                 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
745         ) || exit $?
746
747         WVSTART 'meta - POSIX.1e ACLs (as root)'
748         WVPASS force-delete "$testfs"/src
749         WVPASS mkdir "$testfs"/src
750         WVPASS touch "$testfs"/src/foo
751         WVPASS mkdir "$testfs"/src/bar
752         WVPASS setfacl -m u:root:r "$testfs"/src/foo
753         WVPASS setfacl -m u:root:r "$testfs"/src/bar
754         (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
755
756         # Test restoration to a limited filesystem (vfat).
757         (
758             WVPASS bup meta --create --recurse --file "$testfs"/src.meta \
759                 "$testfs"/src
760             WVPASS force-delete "$testfs_limited"/src-restore
761             WVPASS mkdir "$testfs_limited"/src-restore
762             WVPASS cd "$testfs_limited"/src-restore
763             WVFAIL bup meta --extract --file "$testfs"/src.meta 2>&1 \
764                 | WVPASS grep -e '^POSIX1e ACL applyto:' \
765                 | WVPASS python -c \
766                 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
767         ) || exit $?
768
769         WVPASS umount "$testfs"
770         WVPASS umount "$testfs_limited"
771         WVPASS rm -r "$testfs" "$testfs_limited"
772
773         WVPASS rm -r "$tmpdir"
774
775     ) || exit $?
776 fi
777
778 WVPASS rm -r "$tmpdir"