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