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