More notes
[rsync/rsync.git] / testsuite / rsync.fns
CommitLineData
d820215b
MP
1#! /bin/sh
2
3# Copyright (C) 2001 by Martin Pool <mbp@samba.org>
4
5# General-purpose test functions for rsync.
3a4c683f 6
0154b302
MP
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
3a4c683f
MP
21TMP="$scratchdir"
22FROM=${TMP}/from
23TO=${TMP}/to
3a4c683f 24LOG=${TMP}/log
3fedd74b 25RSYNC="$rsync_bin"
3a4c683f
MP
26
27runtest() {
28 echo $ECHO_N "Test $1: $ECHO_C"
501972bf
MP
29 if eval "$2"
30 then
31 echo "${ECHO_T} done."
32 return 0
33 else
34 echo "${ECHO_T} failed!"
35 return 1
36 fi
3a4c683f
MP
37}
38
39printmsg() {
40 echo "$1"
41}
42
e052b21f 43
6773a779 44rsync_ls_lR() {
57835c00
MP
45 find "$@" -print | sort | xargs $TLS
46}
47
48
e052b21f
MP
49####################
50# Build test directories TO and FROM, with FROM full of files.
51
3a4c683f 52hands_setup() {
501972bf
MP
53 # Clean before creation
54 rm -rf $FROM
55 rm -rf $TO
3d807132
MP
56
57 [ -d $TMP ] || mkdir $TMP
3a4c683f
MP
58 [ -d $FROM ] || mkdir $FROM
59 [ -d $TO ] || mkdir $TO
60
e052b21f
MP
61 # On some BSD systems, the umask affects the mode of created
62 # symlinks, even though the mode apparently has no effect on how
63 # the links behave in the future, and it cannot be changed using
64 # chmod! rsync always sets its umask to 000 so that it can
65 # accurately recreate permissions, but this script is probably run
66 # with a different umask.
67
68 # This causes a little problem that "ls -l" of the two will not be
69 # the same. So, we need to set our umask before doing any creations.
70
3a4c683f
MP
71 # set up test data
72 touch ${FROM}/empty
73 mkdir ${FROM}/emptydir
e8ca5901 74
571a4b26 75 # a hundred lines of text or so
3d807132 76 rsync_ls_lR "${srcdir}" > ${FROM}/filelist
3a4c683f
MP
77
78 # This might fail on systems that don't have -n
79 echo $ECHO_N "This file has no trailing lf$ECHO_C" > ${FROM}/nolf
0154b302 80 umask 0
3a4c683f 81 ln -s nolf ${FROM}/nolf-symlink
4c80c473 82 umask 022
0154b302 83
4c80c473 84 cat $srcdir/*.c > ${FROM}/text
3a4c683f 85 mkdir ${FROM}/dir
4c80c473 86 cp ${FROM}/text ${FROM}/dir
3a4c683f
MP
87 mkdir ${FROM}/dir/subdir
88 mkdir ${FROM}/dir/subdir/subsubdir
89 ls -ltr /etc > ${FROM}/dir/subdir/subsubdir/etc-ltr-list
90 mkdir ${FROM}/dir/subdir/subsubdir2
91 ls -lt /bin > ${FROM}/dir/subdir/subsubdir2/bin-lt-list
7d691654 92
8f98c608
MP
93# echo testing head:
94# ls -lR ${srcdir} | head -10 || echo failed
3a4c683f 95}
3fedd74b
MP
96
97
3d807132
MP
98hands_cleanup() {
99 rm -r "$TMP"
100}
101
102
e052b21f 103
99cdaff7
MP
104
105####################
106# Many machines do not have "mkdir -p", so we have to build up long paths.
107# How boring.
108makepath () {
7c1b7890 109 echo " makepath $1"
99cdaff7
MP
110 p="$1"
111 (
112 # Absolut Unix.
113 if echo $p | grep '^/' >/dev/null
114 then
115 cd /
116 fi
117
118 # This will break if $1 contains a space.
119 for c in `echo $p | tr '/' ' '`
120 do
112e7311
MP
121 if [ -d "$c" ] || mkdir "$c"
122 then
123 cd "$c" || return $?
124 else
125 echo "failed to create $c" >&2; return $?
126 fi
99cdaff7
MP
127 done
128 )
129}
130
131
132
3fedd74b
MP
133###########################
134# Run a test (in '$1') then compare directories $2 and $3 to see if
135# there are any difference. If there are, explain them.
136
137checkit() {
3fedd74b 138 failed=
3fedd74b 139
cf72f204
MP
140 # We can just write everything to stdout/stderr, because the
141 # wrapper hides it unless there is a problem.
142
143 echo "Running: \"$1\""
144 eval "$1"
3fedd74b
MP
145 status=$?
146 if [ $status != 0 ]; then
147 failed="YES";
148 fi
149
cf72f204
MP
150 echo "-------------"
151 echo "check how the files compare with diff:"
152 echo ""
d58e4c27
MP
153 for f in `cd "$2"; find . -type f -print `
154 do
371d1c36 155 diff -c "$2"/"$f" "$3"/"$f" || failed=YES
d58e4c27
MP
156 done
157
cf72f204
MP
158 echo "-------------"
159 echo "check how the directory listings compare with diff:"
160 echo ""
161 ( cd "$2" && rsync_ls_lR . ) > ${TMP}/ls-from
162 ( cd "$3" && rsync_ls_lR . ) > ${TMP}/ls-to
163 diff -c ${TMP}/ls-from ${TMP}/ls-to || failed=YES
3fedd74b 164 if [ -z "${failed}" ] ; then
3fedd74b
MP
165 return 0
166 else
3fedd74b
MP
167 return 1
168 fi
169}
170
863dff51
MP
171
172# In fact, we need a more general feature of capturing all stderr/log files,
173# and dumping them if something goes wrong.
174
175checkforlogs() {
176 # skip it if we're under debian-test
177 if test -n "${Debian}" ; then return 0 ; fi
178
179 if [ -f $1 -a -s $1 ] ; then
180 echo "Failures have occurred. $1 follows:" >&2
181 cat $1 >&2
182 exit 1
183 fi
184}
185
d2094cc3 186
4a7cb3e8 187build_rsyncd_conf() {
d2094cc3
MP
188 # Build an appropriate configuration file
189 conf="$scratchdir/test-rsyncd.conf"
190 echo "building configuration $conf"
191
192 port=2612
193 pidfile="$scratchdir/rsyncd.pid"
194 logfile="$scratchdir/rsyncd.log"
195
196 cat >$conf <<EOF
3aae15ec 197# rsyncd configuration file autogenerated by $0
d2094cc3 198
3aae15ec
MP
199pid file = $pidfile
200use chroot = no
201hosts allow = localhost, 127.0.0.1
202log file = $logfile
d2094cc3 203
2e6c7f45
MP
204[test-from]
205 path = $FROM
3aae15ec 206 read only = yes
d2094cc3 207
2e6c7f45
MP
208[test-to]
209 path = $TO
3aae15ec 210 read only = no
d2094cc3
MP
211EOF
212}
213
214
3cd2af41
MP
215build_symlinks() {
216 fromdir="$scratchdir/from"
217 todir="$scratchdir/to"
218 mkdir "$fromdir"
219 date >"$fromdir/referent"
220 ln -s referent "$fromdir/relative"
221 ln -s "$fromdir/referent" "$fromdir/absolute"
222 ln -s nonexistent "$fromdir/dangling"
1db8b61d 223 ln -s "$srcdir/rsync.c" "$fromdir/unsafe"
3cd2af41
MP
224}
225
226test_fail() {
227 echo "$@" >&2
228 exit 1
229}
230
a217ad30
MP
231test_skipped() {
232 echo "$@" >&2
233 exit 77
234}
235
be2f866b
MP
236# It failed, but we expected that. don't dump out error logs,
237# because most users won't want to see them. But do leave
238# the working directory around.
239test_xfail() {
240 echo "$@" >&2
241 exit 78
242}
243
3d807132
MP
244# be reproducible
245umask 077