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