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