]> arthur.barton.de Git - bup.git/blob - t/test-meta.sh
test-restore-map-owner: accommodate python 3 and test there
[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 "$TOP/t/sync-tree" "$TOP/t/sampledata/" "$tmpdir/src/"
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/var/cmd
162     WVPASS test -f src/var/cmd/save-cmd.py
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/var/."
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/var/ src-restore/ > tmp-compare-trees
173     WVPASSEQ $(cat tmp-compare-trees | wc -l) 1
174     # The number of rsync status characters varies, so accept any
175     # number of trailing dots.  For example OS X native rsync produces
176     # 9, but Homebrew's produces 12, while on other platforms, 11 is
177     # common.
178     expected_diff_rx='^\.d\.\.t\.\.\.(\.)+ \./$'
179     if ! grep -qE "$expected_diff_rx" tmp-compare-trees; then
180         echo -n 'tmp-compare-trees: ' 1>&2
181         cat tmp-compare-trees 1>&2
182     fi
183     WVPASS grep -qE "$expected_diff_rx" tmp-compare-trees
184     WVPASS rm -r "$tmpdir"
185 ) || exit $?
186
187 # Test that we pull the index (not filesystem) metadata for any
188 # unchanged files whenever we're saving other files in a given
189 # directory.
190 WVSTART 'metadata save/restore (using index metadata)'
191 (
192     tmpdir="$(WVPASS wvmktempdir)" || exit $?
193     export BUP_DIR="$tmpdir/bup"
194     WVPASS setup-test-tree
195     WVPASS cd "$tmpdir"
196
197     # ...for now -- might be a problem with hardlink restores that was
198     # causing noise wrt this test.
199     WVPASS rm -rf src/hardlink*
200
201     # Pause here to keep the filesystem changes far enough away from
202     # the first index run that bup won't cap their index timestamps
203     # (see "bup help index" for more information).  Without this
204     # sleep, the compare-trees test below "Bup should *not* pick up
205     # these metadata..." may fail.
206     WVPASS sleep 1
207
208     WVPASS rm -rf "$BUP_DIR"
209     WVPASS bup init
210     WVPASS bup index src
211     WVPASS bup save -t -n src src
212
213     WVPASS force-delete src-restore-1
214     WVPASS mkdir src-restore-1
215     WVPASS bup restore -C src-restore-1 "/src/latest$(pwd)/"
216     WVPASS test -d src-restore-1/src
217     WVPASS "$TOP/t/compare-trees" -c src/ src-restore-1/src/
218
219     WVPASS echo "blarg" > src/volatile/1
220     WVPASS cp -pP src/volatile/1 src-restore-1/src/volatile/
221     WVPASS bup index src
222
223     # Bup should *not* pick up these metadata changes.
224     WVPASS touch src/volatile/2
225
226     WVPASS bup save -t -n src src
227
228     WVPASS force-delete src-restore-2
229     WVPASS mkdir src-restore-2
230     WVPASS bup restore -C src-restore-2 "/src/latest$(pwd)/"
231     WVPASS test -d src-restore-2/src
232     WVPASS "$TOP/t/compare-trees" -c src-restore-1/src/ src-restore-2/src/
233
234     WVPASS rm -r "$tmpdir"
235
236 ) || exit $?
237
238
239 setup-hardlink-test()
240 {
241     WVPASS rm -rf "$tmpdir/src" "$BUP_DIR"
242     WVPASS bup init
243     WVPASS mkdir "$tmpdir/src"
244 }
245
246 hardlink-test-run-restore()
247 {
248     WVPASS force-delete src-restore
249     WVPASS mkdir src-restore
250     WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
251     WVPASS test -d src-restore/src
252 }
253
254 # Test hardlinks more carefully.
255 WVSTART 'metadata save/restore (hardlinks)'
256 (
257     tmpdir="$(WVPASS wvmktempdir)" || exit $?    
258     export BUP_DIR="$tmpdir/bup"
259
260     WVPASS setup-hardlink-test
261     WVPASS cd "$tmpdir"
262     
263     # Test trivial case - single hardlink.
264     (
265         WVPASS cd src
266         WVPASS touch hardlink-target
267         WVPASS ln hardlink-target hardlink-1
268     ) || exit $?
269     WVPASS bup index src
270     WVPASS bup save -t -n src src
271     WVPASS hardlink-test-run-restore
272     WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
273
274     # Test the case where the hardlink hasn't changed, but the tree
275     # needs to be saved again. i.e. the save-cmd.py "if hashvalid:"
276     # case.
277     (
278         WVPASS cd src
279         WVPASS echo whatever > something-new
280     ) || exit $?
281     WVPASS bup index src
282     WVPASS bup save -t -n src src
283     WVPASS hardlink-test-run-restore
284     WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
285
286     # Test hardlink changes between index runs.
287     #
288     WVPASS setup-hardlink-test
289     WVPASS cd src
290     WVPASS touch hardlink-target-a
291     WVPASS touch hardlink-target-b
292     WVPASS ln hardlink-target-a hardlink-b-1
293     WVPASS ln hardlink-target-a hardlink-a-1
294     WVPASS cd ..
295     WVPASS bup index -vv src
296     WVPASS rm src/hardlink-b-1
297     WVPASS ln src/hardlink-target-b src/hardlink-b-1
298     WVPASS bup index -vv src
299     WVPASS bup save -t -n src src
300     WVPASS hardlink-test-run-restore
301     WVPASS echo ./src/hardlink-a-1 > hardlink-sets.expected
302     WVPASS echo ./src/hardlink-target-a >> hardlink-sets.expected
303     WVPASS echo >> hardlink-sets.expected
304     WVPASS echo ./src/hardlink-b-1 >> hardlink-sets.expected
305     WVPASS echo ./src/hardlink-target-b >> hardlink-sets.expected
306     (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
307         || exit $?
308     WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
309
310     # Test hardlink changes between index and save -- hardlink set [a
311     # b c d] changes to [a b] [c d].  At least right now bup should
312     # notice and recreate the latter.
313     WVPASS setup-hardlink-test
314     WVPASS cd "$tmpdir"/src
315     WVPASS touch a
316     WVPASS ln a b
317     WVPASS ln a c
318     WVPASS ln a d
319     WVPASS cd ..
320     WVPASS bup index -vv src
321     WVPASS rm src/c src/d
322     WVPASS touch src/c
323     WVPASS ln src/c src/d
324     WVPASS bup save -t -n src src
325     WVPASS hardlink-test-run-restore
326     WVPASS echo ./src/a > hardlink-sets.expected
327     WVPASS echo ./src/b >> hardlink-sets.expected
328     WVPASS echo >> hardlink-sets.expected
329     WVPASS echo ./src/c >> hardlink-sets.expected
330     WVPASS echo ./src/d >> hardlink-sets.expected
331     (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
332         || exit $?
333     WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
334
335     # Test that we don't link outside restore tree.
336     WVPASS setup-hardlink-test
337     WVPASS cd "$tmpdir"
338     WVPASS mkdir src/a src/b
339     WVPASS touch src/a/1
340     WVPASS ln src/a/1 src/b/1
341     WVPASS bup index -vv src
342     WVPASS bup save -t -n src src
343     WVPASS force-delete src-restore
344     WVPASS mkdir src-restore
345     WVPASS bup restore -C src-restore "/src/latest$(pwd)/src/a/"
346     WVPASS test -e src-restore/1
347     WVPASS echo -n > hardlink-sets.expected
348     (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
349         || exit $?
350     WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
351
352     # Test that we do link within separate sub-trees.
353     WVPASS setup-hardlink-test
354     WVPASS cd "$tmpdir"
355     WVPASS mkdir src/a src/b
356     WVPASS touch src/a/1
357     WVPASS ln src/a/1 src/b/1
358     WVPASS bup index -vv src/a src/b
359     WVPASS bup save -t -n src src/a src/b
360     WVPASS hardlink-test-run-restore
361     WVPASS echo ./src/a/1 > hardlink-sets.expected
362     WVPASS echo ./src/b/1 >> hardlink-sets.expected
363     (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
364         || exit $?
365     WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
366
367     WVPASS rm -r "$tmpdir"
368
369 ) || exit $?
370
371 WVSTART 'meta --edit'
372 (
373     tmpdir="$(WVPASS wvmktempdir)" || exit $?    
374     WVPASS cd "$tmpdir"
375     WVPASS mkdir src
376
377     WVPASS bup meta -cf src.meta src
378
379     WVPASS bup meta --edit --set-uid 0 src.meta | WVPASS bup meta -tvvf - \
380         | WVPASS grep -qE '^uid: 0'
381     WVPASS bup meta --edit --set-uid 1000 src.meta | WVPASS bup meta -tvvf - \
382         | WVPASS grep -qE '^uid: 1000'
383
384     WVPASS bup meta --edit --set-gid 0 src.meta | WVPASS bup meta -tvvf - \
385         | WVPASS grep -qE '^gid: 0'
386     WVPASS bup meta --edit --set-gid 1000 src.meta | WVPASS bup meta -tvvf - \
387         | WVPASS grep -qE '^gid: 1000'
388
389     WVPASS bup meta --edit --set-user foo src.meta | WVPASS bup meta -tvvf - \
390         | WVPASS grep -qE '^user: foo'
391     WVPASS bup meta --edit --set-user bar src.meta | WVPASS bup meta -tvvf - \
392         | WVPASS grep -qE '^user: bar'
393     WVPASS bup meta --edit --unset-user src.meta | WVPASS bup meta -tvvf - \
394         | WVPASS grep -qE '^user:'
395     WVPASS bup meta --edit --set-user bar --unset-user src.meta \
396         | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user:'
397     WVPASS bup meta --edit --unset-user --set-user bar src.meta \
398         | WVPASS bup meta -tvvf - | WVPASS grep -qE '^user: bar'
399
400     WVPASS bup meta --edit --set-group foo src.meta | WVPASS bup meta -tvvf - \
401         | WVPASS grep -qE '^group: foo'
402     WVPASS bup meta --edit --set-group bar src.meta | WVPASS bup meta -tvvf - \
403         | WVPASS grep -qE '^group: bar'
404     WVPASS bup meta --edit --unset-group src.meta | WVPASS bup meta -tvvf - \
405         | WVPASS grep -qE '^group:'
406     WVPASS bup meta --edit --set-group bar --unset-group src.meta \
407         | WVPASS bup meta -tvvf - | WVPASS grep -qE '^group:'
408     WVPASS bup meta --edit --unset-group --set-group bar src.meta \
409         | WVPASS bup meta -tvvf - | grep -qE '^group: bar'
410
411     WVPASS rm -r "$tmpdir"
412
413 ) || exit $?
414
415 WVSTART 'meta --no-recurse'
416 (
417     tmpdir="$(WVPASS wvmktempdir)" || exit $?    
418     WVPASS cd "$tmpdir"
419     WVPASS mkdir src
420     WVPASS mkdir src/foo
421     WVPASS touch src/foo/{1,2,3}
422     WVPASS bup meta -cf src.meta src
423     WVPASSEQ "$(bup meta -tf src.meta | LC_ALL=C sort)" "src/
424 src/foo/
425 src/foo/1
426 src/foo/2
427 src/foo/3"
428     WVPASS bup meta --no-recurse -cf src.meta src
429     WVPASSEQ "$(bup meta -tf src.meta | LC_ALL=C sort)" "src/"
430     WVPASS rm -r "$tmpdir"
431 ) || exit $?
432
433 # Test ownership restoration (when not root or fakeroot).
434 (
435     if [ "$root_status" != none ]; then
436         exit 0
437     fi
438
439     tmpdir="$(WVPASS wvmktempdir)" || exit $?    
440
441     # FIXME: binary groups
442     first_group="$(WVPASS bup-python -c 'import os,grp; \
443       print(grp.getgrgid(os.getgroups()[0])[0])')" || exit $?
444     last_group="$(bup-python -c 'import os,grp; \
445       print(grp.getgrgid(os.getgroups()[-1])[0])')" || exit $?
446     last_group_erx="$(escape-erx "$last_group")"
447
448     WVSTART 'metadata (restoration of ownership)'
449     WVPASS cd "$tmpdir"
450     WVPASS touch src
451     # Some systems always assign the parent dir group to new paths
452     # (sgid).  Make sure the group is one we're in.
453     WVPASS chgrp -R "$first_group" src
454
455     WVPASS bup meta -cf src.meta src
456
457     WVPASS mkdir dest
458     WVPASS cd dest
459     # Make sure we don't change (or try to change) the user when not root.
460     WVPASS bup meta --edit --set-user root ../src.meta | WVPASS bup meta -x
461     WVPASS bup xstat src | WVPASS grep -qvE '^user: root'
462     WVPASS rm -rf src
463     WVPASS bup meta --edit --unset-user --set-uid 0 ../src.meta \
464         | WVPASS bup meta -x
465     WVPASS bup xstat src | WVPASS grep -qvE '^user: root'
466
467     # Make sure we can restore one of the user's groups.
468     WVPASS rm -rf src
469     WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
470         | WVPASS bup meta -x
471     WVPASS bup xstat src | WVPASS grep -qE "^group: $last_group_erx"
472
473     # Make sure we can restore one of the user's gids.
474     user_gids="$(id -G)" || exit $?
475     last_gid="$(echo ${user_gids/* /})" || exit $?
476     WVPASS rm -rf src
477     WVPASS bup meta --edit --unset-group --set-gid "$last_gid" ../src.meta \
478         | WVPASS bup meta -x
479     WVPASS bup xstat src | WVPASS grep -qE "^gid: $last_gid"
480
481     # Test --numeric-ids (gid).
482     WVPASS rm -rf src
483     current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:') || exit $?
484     WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
485         | WVPASS bup meta -x --numeric-ids
486     new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
487     WVPASSEQ "$current_gidx" "$new_gidx"
488
489     # Test that restoring an unknown user works.
490     unknown_user=$("$TOP"/t/unknown-owner --user) || exit $?
491     WVPASS rm -rf src
492     current_uidx=$(bup meta -tvvf ../src.meta | grep -e '^uid:') || exit $?
493     WVPASS bup meta --edit --set-user "$unknown_user" ../src.meta \
494         | WVPASS bup meta -x
495     new_uidx=$(bup xstat src | grep -e '^uid:') || exit $?
496     WVPASSEQ "$current_uidx" "$new_uidx"
497
498     # Test that restoring an unknown group works.
499     unknown_group=$("$TOP"/t/unknown-owner --group) || exit $?
500     WVPASS rm -rf src
501     current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:') || exit $?
502     WVPASS bup meta --edit --set-group "$unknown_group" ../src.meta \
503         | WVPASS bup meta -x
504     new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
505     WVPASSEQ "$current_gidx" "$new_gidx"
506
507     WVPASS rm -r "$tmpdir"
508
509 ) || exit $?
510
511 # Test ownership restoration (when root or fakeroot).
512 (
513     if [ "$root_status" = none ]; then
514         exit 0
515     fi
516
517     tmpdir="$(WVPASS wvmktempdir)" || exit $?    
518
519     uid=$(WVPASS id -un) || exit $?
520     gid=$(WVPASS id -gn) || exit $?
521
522     WVSTART 'metadata (restoration of ownership as root)'
523     WVPASS cd "$tmpdir"
524     WVPASS touch src
525     WVPASS chown "$uid:$gid" src # In case the parent dir is sgid, etc.
526     WVPASS bup meta -cf src.meta src
527
528     WVPASS mkdir dest
529     WVPASS chmod 700 dest # so we can't accidentally do something insecure
530     WVPASS cd dest
531
532     other_uinfo="$(id-other-than --user "$uid")" || exit $?
533     other_user="${other_uinfo%%:*}"
534     other_uid="${other_uinfo##*:}"
535
536     other_ginfo="$(id-other-than --group "$gid")" || exit $?
537     other_group="${other_ginfo%%:*}"
538     other_gid="${other_ginfo##*:}"
539
540     # Make sure we can restore a uid (must be in /etc/passwd b/c cygwin).
541     WVPASS bup meta --edit --unset-user --set-uid "$other_uid" ../src.meta \
542         | WVPASS bup meta -x
543     WVPASS bup xstat src | WVPASS grep -qE "^uid: $other_uid"
544
545     # Make sure we can restore a gid (must be in /etc/group b/c cygwin).
546     WVPASS bup meta --edit --unset-group --set-gid "$other_gid" ../src.meta \
547         | WVPASS bup meta -x
548     WVPASS bup xstat src | WVPASS grep -qE "^gid: $other_gid"
549
550     other_uinfo2="$(id-other-than --user "$(id -un)" "$other_user")" || exit $?
551     other_user2="${other_uinfo2%%:*}"
552     other_user2_erx="$(escape-erx "$other_user2")" || exit $?
553     other_uid2="${other_uinfo2##*:}"
554
555     other_ginfo2="$(id-other-than --group "$(id -gn)" "$other_group")" || exit $?
556     other_group2="${other_ginfo2%%:*}"
557     other_group2_erx="$(escape-erx "$other_group2")" || exit $?
558     other_gid2="${other_ginfo2##*:}"
559
560     # Try to restore a user (and see that user trumps uid when uid is not 0).
561     WVPASS bup meta --edit \
562         --set-uid "$other_uid" --set-user "$other_user2" ../src.meta \
563         | WVPASS bup meta -x
564     WVPASS bup xstat src | WVPASS grep -qE "^user: $other_user2_erx"
565
566     # Try to restore a group (and see that group trumps gid when gid is not 0).
567     WVPASS bup meta --edit \
568         --set-gid "$other_gid" --set-group "$other_group2" ../src.meta \
569         | WVPASS bup meta -x
570     WVPASS bup xstat src | WVPASS grep -qE "^group: $other_group2_erx"
571
572     # Test --numeric-ids (uid).  Note the name 'root' is not handled
573     # specially, so we use that here as the test user name.  We assume
574     # that the root user's uid is never 42.
575     WVPASS rm -rf src
576     WVPASS bup meta --edit --set-user root --set-uid "$other_uid" ../src.meta \
577         | WVPASS bup meta -x --numeric-ids
578     new_uidx=$(bup xstat src | grep -e '^uid:') || exit $?
579     WVPASSEQ "$new_uidx" "uid: $other_uid"
580
581     # Test --numeric-ids (gid).  Note the name 'root' is not handled
582     # specially, so we use that here as the test group name.  We
583     # assume that the root group's gid is never 42.
584     WVPASS rm -rf src
585     WVPASS bup meta --edit --set-group root --set-gid "$other_gid" ../src.meta \
586         | WVPASS bup meta -x --numeric-ids
587     new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
588     WVPASSEQ "$new_gidx" "gid: $other_gid"
589
590     # Test that restoring an unknown user works.
591     unknown_user=$("$TOP"/t/unknown-owner --user) || exit $?
592     WVPASS rm -rf src
593     WVPASS bup meta --edit \
594         --set-uid "$other_uid" --set-user "$unknown_user" ../src.meta \
595         | WVPASS bup meta -x
596     new_uidx=$(bup xstat src | grep -e '^uid:') || exit $?
597     WVPASSEQ "$new_uidx" "uid: $other_uid"
598
599     # Test that restoring an unknown group works.
600     unknown_group=$("$TOP"/t/unknown-owner --group) || exit $?
601     WVPASS rm -rf src
602     WVPASS bup meta --edit \
603         --set-gid "$other_gid" --set-group "$unknown_group" ../src.meta \
604         | WVPASS bup meta -x
605     new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
606     WVPASSEQ "$new_gidx" "gid: $other_gid"
607
608     if ! [[ $(uname) =~ CYGWIN ]]; then
609         # For now, skip these on Cygwin because it doesn't allow
610         # restoring an unknown uid/gid.
611
612         # Make sure a uid of 0 trumps a non-root user.
613         WVPASS bup meta --edit --set-user "$other_user2" ../src.meta \
614             | WVPASS bup meta -x
615         WVPASS bup xstat src | WVPASS grep -qvE "^user: $other_user2_erx"
616         WVPASS bup xstat src | WVPASS grep -qE "^uid: 0"
617
618         # Make sure a gid of 0 trumps a non-root group.
619         WVPASS bup meta --edit --set-group "$other_group2" ../src.meta \
620             | WVPASS bup meta -x
621         WVPASS bup xstat src | WVPASS grep -qvE "^group: $other_group2_erx"
622         WVPASS bup xstat src | WVPASS grep -qE "^gid: 0"
623     fi
624
625     WVPASS rm -r "$tmpdir"
626
627 ) || exit $?
628
629
630 # Root-only tests that require an FS with all the trimmings: ACLs,
631 # Linux attr, Linux xattr, etc.
632 if [ "$root_status" = root ]; then
633     (
634         # Some cleanup handled in universal-cleanup() above.
635         # These tests are only likely to work under Linux for now
636         # (patches welcome).
637         [[ $(uname) =~ Linux ]] || exit 0
638
639         if ! modprobe loop; then
640             echo 'Unable to load loopback module; skipping dependent tests.' 1>&2
641             exit 0
642         fi
643
644         testfs="$(WVPASS wvmkmountpt)" || exit $?
645         testfs_limited="$(WVPASS wvmkmountpt)" || exit $?
646         tmpdir="$(WVPASS wvmktempdir)" || exit $?
647         export BUP_DIR="$tmpdir/bup"
648
649         WVSTART 'meta - general (as root)'
650         WVPASS setup-test-tree
651         WVPASS cd "$tmpdir"
652
653         umount "$testfs"
654         WVPASS dd if=/dev/zero of=testfs.img bs=1M count=32
655         # Make sure we have all the options the chattr test needs
656         # (i.e. create a "normal" ext4 filesystem).
657         WVPASS mke2fs -F -m 0 \
658             -I 256 \
659             -O has_journal,extent,huge_file,flex_bg,uninit_bg,dir_nlink,extra_isize \
660             testfs.img
661         WVPASS mount -o loop,acl,user_xattr testfs.img "$testfs"
662         # Hide, so that tests can't create risks.
663         WVPASS chown root:root "$testfs"
664         WVPASS chmod 0700 "$testfs"
665
666         umount "$testfs_limited"
667         WVPASS dd if=/dev/zero of=testfs-limited.img bs=1M count=32
668         WVPASS mkfs -t vfat testfs-limited.img
669         WVPASS mount -o loop,uid=root,gid=root,umask=0077 \
670             testfs-limited.img "$testfs_limited"
671
672         WVPASS cp -pPR src "$testfs"/src
673         (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
674
675         WVSTART 'meta - atime (as root)'
676         WVPASS force-delete "$testfs"/src
677         WVPASS mkdir "$testfs"/src
678         (
679             WVPASS mkdir "$testfs"/src/foo
680             WVPASS touch "$testfs"/src/bar
681             WVPASS bup-python -c "from bup import xstat; \
682                 x = xstat.timespec_to_nsecs((42, 0));\
683                 xstat.utime(b'$testfs/src/foo', (x, x));\
684                 xstat.utime(b'$testfs/src/bar', (x, x));"
685             WVPASS cd "$testfs"
686             WVPASS bup meta -v --create --recurse --file src.meta src
687             WVPASS bup meta -tvf src.meta
688             # Test extract.
689             WVPASS force-delete src-restore
690             WVPASS mkdir src-restore
691             WVPASS cd src-restore
692             WVPASS bup meta --extract --file ../src.meta
693             WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
694             WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
695             # Test start/finish extract.
696             WVPASS force-delete src
697             WVPASS bup meta --start-extract --file ../src.meta
698             WVPASS test -d src
699             WVPASS bup meta --finish-extract --file ../src.meta
700             WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
701             WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
702         ) || exit $?
703
704         WVSTART 'meta - Linux attr (as root)'
705         WVPASS force-delete "$testfs"/src
706         WVPASS mkdir "$testfs"/src
707         (
708             WVPASS touch "$testfs"/src/foo
709             WVPASS mkdir "$testfs"/src/bar
710             WVPASS chattr +acdeijstuADS "$testfs"/src/foo
711             WVPASS chattr +acdeijstuADST "$testfs"/src/bar
712             (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
713             # Test restoration to a limited filesystem (vfat).
714             (
715                 WVPASS bup meta --create --recurse --file "$testfs"/src.meta \
716                     "$testfs"/src
717                 WVPASS force-delete "$testfs_limited"/src-restore
718                 WVPASS mkdir "$testfs_limited"/src-restore
719                 WVPASS cd "$testfs_limited"/src-restore
720                 WVFAIL bup meta --extract --file "$testfs"/src.meta 2>&1 \
721                     | WVPASS grep -e '^Linux chattr:' \
722                     | WVPASS bup-python -c \
723                     'import sys; exit(not len(sys.stdin.readlines()) == 3)'
724             ) || exit $?
725         ) || exit $?
726
727         WVSTART 'meta - Linux xattr (as root)'
728         WVPASS force-delete "$testfs"/src
729         WVPASS mkdir "$testfs"/src
730         WVPASS touch "$testfs"/src/foo
731         WVPASS mkdir "$testfs"/src/bar
732         WVPASS attr -s foo -V bar "$testfs"/src/foo
733         WVPASS attr -s foo -V bar "$testfs"/src/bar
734         (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
735
736         # Test restoration to a limited filesystem (vfat).
737         (
738             WVPASS bup meta --create --recurse --file "$testfs"/src.meta \
739                 "$testfs"/src
740             WVPASS force-delete "$testfs_limited"/src-restore
741             WVPASS mkdir "$testfs_limited"/src-restore
742             WVPASS cd "$testfs_limited"/src-restore
743             WVFAIL bup meta --extract --file "$testfs"/src.meta
744             WVFAIL bup meta --extract --file "$testfs"/src.meta 2>&1 \
745                 | WVPASS grep -e "^xattr\.set u\?'" \
746                 | WVPASS bup-python -c \
747                 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
748         ) || exit $?
749
750         WVSTART 'meta - POSIX.1e ACLs (as root)'
751         WVPASS force-delete "$testfs"/src
752         WVPASS mkdir "$testfs"/src
753         WVPASS touch "$testfs"/src/foo
754         WVPASS mkdir "$testfs"/src/bar
755         WVPASS setfacl -m u:root:r "$testfs"/src/foo
756         WVPASS setfacl -m u:root:r "$testfs"/src/bar
757         (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
758
759         # Test restoration to a limited filesystem (vfat).
760         (
761             WVPASS bup meta --create --recurse --file "$testfs"/src.meta \
762                 "$testfs"/src
763             WVPASS force-delete "$testfs_limited"/src-restore
764             WVPASS mkdir "$testfs_limited"/src-restore
765             WVPASS cd "$testfs_limited"/src-restore
766             WVFAIL bup meta --extract --file "$testfs"/src.meta 2>&1 \
767                 | WVPASS grep -e '^POSIX1e ACL applyto:' \
768                 | WVPASS bup-python -c \
769                 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
770         ) || exit $?
771
772         WVPASS umount "$testfs"
773         WVPASS umount "$testfs_limited"
774         WVPASS rm -r "$testfs" "$testfs_limited"
775
776         WVPASS rm -r "$tmpdir"
777
778     ) || exit $?
779 fi
780
781 WVPASS rm -r "$tmpdir"