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