Linux: Handle protected_regular in inplace writes (#241)
[rsync.git] / testsuite / rsync.fns
1 #!/bin/sh
2
3 # Copyright (C) 2001 by Martin Pool <mbp@samba.org>
4
5 # General-purpose test functions for rsync.
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version
9 # 2 as published by the Free Software Foundation.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 tmpdir="$scratchdir"
21 fromdir="$tmpdir/from"
22 todir="$tmpdir/to"
23 chkdir="$tmpdir/chk"
24
25 chkfile="$scratchdir/rsync.chk"
26 outfile="$scratchdir/rsync.out"
27
28 # For itemized output:
29 all_plus='+++++++++'
30 allspace='         '
31 dots='.....' # trailing dots after changes
32 tab_ch='        ' # a single tab character
33
34 # Berkley's nice.
35 PATH="$PATH:/usr/ucb"
36
37 if diff -u "$suitedir/rsync.fns" "$suitedir/rsync.fns" >/dev/null 2>&1; then
38     diffopt="-u"
39 else
40     diffopt="-c"
41 fi
42
43 HOME="$scratchdir"
44 export HOME
45
46 runtest() {
47     echo $ECHO_N "Test $1: $ECHO_C"
48     if eval "$2"; then
49         echo "$ECHO_T   done."
50         return 0
51     else
52         echo "$ECHO_T failed!"
53         return 1
54     fi
55 }
56
57 set_cp_destdir() {
58     while test $# -gt 1; do
59         shift
60     done
61     destdir="$1"
62 }
63
64 # Perform a "cp -p", making sure that timestamps are really the same,
65 # even if the copy rounded microsecond times on the destination file.
66 cp_touch() {
67     cp_p "${@}"
68     if test $# -gt 2 -o -d "$2"; then
69         set_cp_destdir "${@}" # sets destdir var
70         while test $# -gt 1; do
71             destname="$destdir/`basename $1`"
72             touch -r "$destname" "$1" "$destname"
73             shift
74         done
75     else
76         touch -r "$2" "$1" "$2"
77     fi
78 }
79
80 # Call this if you want to filter (stdin -> stdout) verbose messages (-v or
81 # -vv) from an rsync run (whittling the output down to just the file messages).
82 # This isn't needed if you use -i without -v.
83 v_filt() {
84     sed -e '/^building file list /d' \
85         -e '/^sending incremental file list/d' \
86         -e '/^created directory /d' \
87         -e '/^done$/d' \
88         -e '/ --whole-file$/d' \
89         -e '/^total: /d' \
90         -e '/^client charset: /d' \
91         -e '/^server charset: /d' \
92         -e '/^$/,$d'
93 }
94
95 printmsg() {
96     echo "$1"
97 }
98
99 rsync_ls_lR() {
100     find "$@" -name .git -prune -o -name auto-build-save -prune -o -print | \
101         sort | sed 's/ /\\ /g' | xargs "$TOOLDIR/tls" $TLS_ARGS
102 }
103
104 get_testuid() {
105     uid=`id -u 2>/dev/null || true`
106     case "$uid" in
107         [0-9]*) echo "$uid" ;;
108         *) id 2>/dev/null | sed 's/^[^0-9]*\([0-9][0-9]*\).*/\1/' ;;
109     esac
110 }
111
112 get_rootuid() {
113     uid=`id -u root 2>/dev/null || true`
114     case "$uid" in
115         [0-9]*) echo "$uid" ;;
116         *) echo 0 ;;
117     esac
118 }
119
120 get_rootgid() {
121     gid=`id -g root 2>/dev/null || true`
122     case "$gid" in
123         [0-9]*) echo "$gid" ;;
124         *) echo 0 ;;
125     esac
126 }
127
128 # When copying via "cp -p", we want to ensure that a non-root user does not
129 # preserve ownership (we want our files to be created as the testing user).
130 # For instance, a Cygwin CI run might have git files owned by a different
131 # user than the (admin) user running the tests.
132 cp_cmd="cp -p"
133 if test x`get_testuid` != x0; then
134     case `cp --help 2>/dev/null` in
135         *--no-preserve=*) cp_cmd="cp -p --no-preserve=ownership" ;;
136     esac
137 fi
138 cp_p() {
139     $cp_cmd "${@}" || test_fail "$cp_cmd failed"
140 }
141
142 check_perms() {
143     perms=`"$TOOLDIR/tls" "$1" | sed 's/^[-d]\(.........\).*/\1/'`
144     if test $perms = $2; then
145         return 0
146     fi
147     echo "permissions: $perms on $1"
148     echo "should be:   $2"
149     test_fail "failed test $3"
150 }
151
152 rsync_getgroups() {
153     "$TOOLDIR/getgroups"
154 }
155
156
157 ####################
158 # Build test directories $todir and $fromdir, with $fromdir full of files.
159
160 hands_setup() {
161     # Clean before creation
162     rm -rf "$fromdir"
163     rm -rf "$todir"
164
165     [ -d "$tmpdir" ] || mkdir "$tmpdir"
166     [ -d "$fromdir" ] || mkdir "$fromdir"
167     [ -d "$todir" ] || mkdir "$todir"
168
169     # On some BSD systems, the umask affects the mode of created
170     # symlinks, even though the mode apparently has no effect on how
171     # the links behave in the future, and it cannot be changed using
172     # chmod!  rsync always sets its umask to 000 so that it can
173     # accurately recreate permissions, but this script is probably run
174     # with a different umask.
175
176     # This causes a little problem that "ls -l" of the two will not be
177     # the same.  So, we need to set our umask before doing any creations.
178
179     # set up test data
180     touch "$fromdir/empty"
181     mkdir "$fromdir/emptydir"
182
183     # a hundred lines of text or so
184     rsync_ls_lR "$srcdir" > "$fromdir/filelist"
185
186     echo $ECHO_N "This file has no trailing lf$ECHO_C" > "$fromdir/nolf"
187     umask 0
188     ln -s nolf "$fromdir/nolf-symlink"
189     umask 022
190
191     cat "$srcdir"/*.c > "$fromdir/text"
192     mkdir "$fromdir/dir"
193     cp "$fromdir/text" "$fromdir/dir"
194     mkdir "$fromdir/dir/subdir"
195     echo some data > "$fromdir/dir/subdir/foobar.baz"
196     mkdir "$fromdir/dir/subdir/subsubdir"
197     if [ -r /etc ]; then
198         ls -ltr /etc > "$fromdir/dir/subdir/subsubdir/etc-ltr-list"
199     else
200         ls -ltr / > "$fromdir/dir/subdir/subsubdir/etc-ltr-list"
201     fi
202     mkdir "$fromdir/dir/subdir/subsubdir2"
203     if [ -r /bin ]; then
204         ls -lt /bin > "$fromdir/dir/subdir/subsubdir2/bin-lt-list"
205     else
206         ls -lt / > "$fromdir/dir/subdir/subsubdir2/bin-lt-list"
207     fi
208
209 #      echo testing head:
210 #      ls -lR "$srcdir" | head -10 || echo failed
211 }
212
213
214 ####################
215 # Many machines do not have "mkdir -p", so we have to build up long paths.
216 # How boring.
217 makepath() {
218     for p in "${@}"; do
219         (echo "        makepath $p"
220
221         # Absolute Unix path.
222         if echo $p | grep '^/' >/dev/null; then
223             cd /
224         fi
225
226         # This will break if $p contains a space.
227         for c in `echo $p | tr '/' ' '`; do
228             if [ -d "$c" ] || mkdir "$c"; then
229                 cd "$c" || return $?
230             else
231                 echo "failed to create $c" >&2; return $?
232             fi
233         done)
234     done
235 }
236
237
238 ###########################
239 # Run a test (in '$1') then compare directories $2 and $3 to see if
240 # there are any difference.  If there are, explain them.
241
242 # So normally basically $1 should be an rsync command, and $2 and $3
243 # the source and destination directories.  This is only good when you
244 # expect to transfer the whole directory exactly as is.  If some files
245 # should be excluded, you might need to use something else.
246
247 checkit() {
248     failed=
249
250     # We can just write everything to stdout/stderr, because the
251     # wrapper hides it unless there is a problem.
252
253     case "x$TLS_ARGS" in
254     *--atimes*)
255         ( cd "$2" && rsync_ls_lR . ) > "$tmpdir/ls-from"
256         ;;
257     *)
258         ;;
259     esac
260
261     echo "Running: \"$1\""
262     eval "$1"
263     status=$?
264     if [ $status != 0 ]; then
265         failed="$failed status=$status"
266     fi
267
268     case "x$TLS_ARGS" in
269     *--atimes*)
270         ;;
271     *)
272         ( cd "$2" && rsync_ls_lR . ) > "$tmpdir/ls-from"
273         ;;
274     esac
275
276     echo "-------------"
277     echo "check how the directory listings compare with diff:"
278     echo ""
279     ( cd "$3" && rsync_ls_lR . ) > "$tmpdir/ls-to"
280     diff $diffopt "$tmpdir/ls-from" "$tmpdir/ls-to" || failed="$failed dir-diff"
281
282     echo "-------------"
283     echo "check how the files compare with diff:"
284     echo ""
285     if [ "x$4" != x ]; then
286         echo "  === Skipping (as directed) ==="
287     else
288         diff -r $diffopt "$2" "$3" || failed="$failed file-diff"
289     fi
290
291     echo "-------------"
292     if [ -z "$failed" ]; then
293         return 0
294     fi
295
296     echo "Failed: $failed"
297     return 1
298 }
299
300
301 # Run a test in $1 and make sure it has a zero exit status.  Capture the
302 # output into $outfile and echo it to stdout.
303 checktee() {
304     echo "Running: \"$1\""
305     eval "$1" >"$outfile"
306     status=$?
307     cat "$outfile"
308     if [ $status != 0 ]; then
309         echo "Failed: status=$status"
310         return 1
311     fi
312     return 0
313 }
314
315
316 # Slurp stdin into $chkfile and then call checkdiff2().
317 checkdiff() {
318     cat >"$chkfile" # Save off stdin
319     checkdiff2 "${@}"
320 }
321
322
323 # Run a test in $1 and make sure it has a zero exit status.  Capture the output
324 # into $outfile.  If $2 is set, use it to filter the outfile.  If resulting
325 # outfile differs from the chkfile data, fail with an error.
326 checkdiff2() {
327     failed=
328
329     echo "Running: \"$1\""
330     eval "$1" >"$outfile"
331     status=$?
332     cat "$outfile"
333     if [ $status != 0 ]; then
334         failed="$failed status=$status"
335     fi
336
337     if [ -n "$2" ]; then
338         eval "cat '$outfile' | $2 >'$outfile.new'"
339         mv "$outfile.new" "$outfile"
340     fi
341
342     diff $diffopt "$chkfile" "$outfile" || failed="$failed output differs"
343
344     if [ -n "$failed" ]; then
345         echo "Failed:$failed"
346         return 1
347     fi
348     return 0
349 }
350
351
352 build_rsyncd_conf() {
353     # Build an appropriate configuration file
354     conf="$scratchdir/test-rsyncd.conf"
355     echo "building configuration $conf"
356
357     port=2612
358     pidfile="$scratchdir/rsyncd.pid"
359     logfile="$scratchdir/rsyncd.log"
360     hostname=`uname -n`
361
362     my_uid=`get_testuid`
363     root_uid=`get_rootuid`
364     root_gid=`get_rootgid`
365
366     uid_setting="uid = $root_uid"
367     gid_setting="gid = $root_gid"
368
369     if test x"$my_uid" != x"$root_uid"; then
370         # Non-root cannot specify uid & gid settings
371         uid_setting="#$uid_setting"
372         gid_setting="#$gid_setting"
373     fi
374
375     cat >"$conf" <<EOF
376 # rsyncd configuration file autogenerated by $0
377
378 pid file = $pidfile
379 use chroot = no
380 munge symlinks = no
381 hosts allow = localhost 127.0.0.0/24 192.168.0.0/16 10.0.0.0/8 $hostname
382 log file = $logfile
383 transfer logging = yes
384 # We don't define log format here so that the test-hidden module will default
385 # to the internal static string (since we had a crash trying to tweak it).
386 exclude = ? foobar.baz
387 max verbosity = 4
388 $uid_setting
389 $gid_setting
390
391 [test-from]
392         path = $fromdir
393         log format = %i %h [%a] %m (%u) %l %f%L
394         read only = yes
395         comment = r/o
396
397 [test-to]
398         path = $todir
399         log format = %i %h [%a] %m (%u) %l %f%L
400         read only = no
401         comment = r/w
402
403 [test-scratch]
404         path = $scratchdir
405         log format = %i %h [%a] %m (%u) %l %f%L
406         read only = no
407
408 [test-hidden]
409         path = $fromdir
410         list = no
411 EOF
412
413     # Build a helper script to ignore exit code 23
414     ignore23="$scratchdir/ignore23"
415     echo "building help script $ignore23"
416
417     cat >"$ignore23" <<'EOT'
418 if "${@}"; then
419     exit
420 fi
421
422 ret=$?
423
424 if test $ret = 23; then
425     exit
426 fi
427
428 exit $ret
429 EOT
430 chmod +x "$ignore23"
431 }
432
433
434 build_symlinks() {
435     mkdir "$fromdir"
436     date >"$fromdir/referent"
437     ln -s referent "$fromdir/relative"
438     ln -s "$fromdir/referent" "$fromdir/absolute"
439     ln -s nonexistent "$fromdir/dangling"
440     ln -s "$srcdir/rsync.c" "$fromdir/unsafe"
441 }
442
443 test_fail() {
444     echo "$@" >&2
445     exit 1
446 }
447
448 test_skipped() {
449     echo "$@" >&2
450     echo "$@" > "$tmpdir/whyskipped"
451     exit 77
452 }
453
454 # It failed, but we expected that.  Don't dump out error logs,
455 # because most users won't want to see them.  But do leave
456 # the working directory around.
457 test_xfail() {
458     echo "$@" >&2
459     exit 78
460 }
461
462 # Determine what shell command will appropriately test for links.
463 ln -s foo "$scratchdir/testlink"
464 for cmd in test /bin/test /usr/bin/test /usr/ucb/bin/test /usr/ucb/test; do
465     for switch in -h -L; do
466         if $cmd $switch "$scratchdir/testlink" 2>/dev/null; then
467             # how nice
468             TEST_SYMLINK_CMD="$cmd $switch"
469             # i wonder if break 2 is portable?
470             break 2
471         fi
472     done
473 done
474 # ok, now get rid of it
475 rm "$scratchdir/testlink"
476
477
478 if [ "x$TEST_SYMLINK_CMD" = 'x' ]; then
479     test_fail "Couldn't determine how to test for symlinks"
480 else
481     echo "Testing for symlinks using '$TEST_SYMLINK_CMD'"
482 fi
483
484
485 # Test whether something is a link, allowing for shell peculiarities
486 is_a_link() {
487     # note the variable contains the first option and therefore is not quoted
488     $TEST_SYMLINK_CMD "$1"
489 }
490
491
492 # We need to set the umask to be reproducible.  Note also that when we
493 # do some daemon tests as root, we will setuid() and therefore the
494 # directory has to be writable by the nobody user in some cases.  The
495 # best thing is probably to explicitly chmod those directories after
496 # creation.
497
498 umask 022