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