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