We now figure out if diff accepts -u and we'll use it instead of -c,
[rsync/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
21 TMP="$scratchdir"
22 FROM=${TMP}/from
23 TO=${TMP}/to
24 LOG=${TMP}/log
25 RSYNC="$rsync_bin"
26
27 # Berkley's nice.
28 PATH="$PATH:/usr/ucb"
29
30 if diff -u $srcdir/testsuite/rsync.fns $srcdir/testsuite/rsync.fns >/dev/null 2>&1; then
31     diffopt="-u"
32 else
33     diffopt="-c"
34 fi
35
36 runtest() {
37     echo $ECHO_N "Test $1: $ECHO_C"
38     if eval "$2"
39     then
40         echo "${ECHO_T} done."
41         return 0
42     else
43         echo "${ECHO_T} failed!"
44         return 1
45     fi
46 }
47
48 printmsg() {
49     echo "$1"
50 }
51
52
53 rsync_ls_lR() {
54     find "$@" -print | sort | xargs "$TOOLDIR/tls"
55 }
56
57 rsync_getgroups() { 
58     "$TOOLDIR/getgroups"
59 }
60
61
62 ####################
63 # Build test directories TO and FROM, with FROM full of files.
64
65 hands_setup() {
66     # Clean before creation
67     rm -rf $FROM
68     rm -rf $TO
69
70     [ -d $TMP ] || mkdir $TMP
71     [ -d $FROM ] || mkdir $FROM
72     [ -d $TO ] || mkdir $TO
73
74     # On some BSD systems, the umask affects the mode of created
75     # symlinks, even though the mode apparently has no effect on how
76     # the links behave in the future, and it cannot be changed using
77     # chmod!  rsync always sets its umask to 000 so that it can
78     # accurately recreate permissions, but this script is probably run
79     # with a different umask. 
80
81     # This causes a little problem that "ls -l" of the two will not be
82     # the same.  So, we need to set our umask before doing any creations.
83
84     # set up test data
85     touch ${FROM}/empty
86     mkdir ${FROM}/emptydir
87
88     # a hundred lines of text or so
89     rsync_ls_lR "${srcdir}" > ${FROM}/filelist
90
91     # This might fail on systems that don't have -n
92     echo $ECHO_N "This file has no trailing lf$ECHO_C" > ${FROM}/nolf
93     umask 0
94     ln -s nolf ${FROM}/nolf-symlink
95     umask 022
96
97     cat $srcdir/*.c > ${FROM}/text
98     mkdir ${FROM}/dir
99     cp ${FROM}/text ${FROM}/dir
100     mkdir ${FROM}/dir/subdir
101     mkdir ${FROM}/dir/subdir/subsubdir
102     ls -ltr /etc > ${FROM}/dir/subdir/subsubdir/etc-ltr-list
103     mkdir ${FROM}/dir/subdir/subsubdir2
104     ls -lt /bin > ${FROM}/dir/subdir/subsubdir2/bin-lt-list
105
106 #      echo testing head:
107 #      ls -lR ${srcdir} | head -10 || echo failed
108 }
109
110
111 ####################
112 # Many machines do not have "mkdir -p", so we have to build up long paths.
113 # How boring.  
114 makepath () {
115     echo "        makepath $1"
116     p="$1"
117     (
118         # Absolut Unix.
119         if echo $p | grep '^/' >/dev/null
120         then
121             cd /
122         fi
123     
124         # This will break if $1 contains a space.
125         for c in `echo $p | tr '/' ' '`
126         do 
127             if [ -d "$c" ] || mkdir "$c" 
128             then
129                 cd "$c" || return $?
130             else
131                 echo "failed to create $c" >&2; return $?
132             fi
133         done
134     )
135 }
136
137
138
139 ###########################
140 # Run a test (in '$1') then compare directories $2 and $3 to see if
141 # there are any difference.  If there are, explain them.
142
143 # So normally basically $1 should be an rsync command, and $2 and $3
144 # the source and destination directories.  This is only good when you
145 # expect to transfer the whole directory exactly as is.  If some files
146 # should be excluded, you might need to use something else.
147
148 checkit() {
149     failed=
150
151     # We can just write everything to stdout/stderr, because the
152     # wrapper hides it unless there is a problem.
153
154     echo "Running: \"$1\""  
155     eval "$1" 
156     status=$?
157     if [ $status != 0 ]; then
158         failed="YES";
159     fi
160
161     echo "-------------"
162     echo "check how the files compare with diff:"
163     echo ""
164     for f in `cd "$2"; find . -type f -print `
165     do 
166         diff $diffopt "$2"/"$f" "$3"/"$f" || failed=YES
167     done
168
169     echo "-------------"
170     echo "check how the directory listings compare with diff:"
171     echo ""
172     ( cd "$2" && rsync_ls_lR . ) > ${TMP}/ls-from 
173     ( cd "$3" && rsync_ls_lR . ) > ${TMP}/ls-to 
174     diff $diffopt ${TMP}/ls-from ${TMP}/ls-to || failed=YES
175     if [ -z "${failed}" ] ; then
176         return 0
177     else
178         return 1
179     fi
180 }
181
182
183 build_rsyncd_conf() {
184     # Build an appropriate configuration file
185     conf="$scratchdir/test-rsyncd.conf"
186     echo "building configuration $conf"
187
188     port=2612
189     pidfile="$scratchdir/rsyncd.pid"
190     logfile="$scratchdir/rsyncd.log"
191
192     cat >$conf <<EOF
193 # rsyncd configuration file autogenerated by $0
194
195 pid file = $pidfile
196 use chroot = no
197 hosts allow = localhost, 127.0.0.1
198 log file = $logfile
199
200 uid = 0
201 gid = 0
202
203 [test-from]
204         path = $FROM
205         read only = yes
206
207 [test-to]
208         path = $TO
209         read only = no
210 EOF
211 }
212
213
214 build_symlinks() {
215     fromdir="$scratchdir/from"
216     todir="$scratchdir/to"
217     mkdir "$fromdir"
218     date >"$fromdir/referent"
219     ln -s referent "$fromdir/relative"
220     ln -s "$fromdir/referent" "$fromdir/absolute"
221     ln -s nonexistent "$fromdir/dangling"
222     ln -s "$srcdir/rsync.c" "$fromdir/unsafe"
223 }
224
225 test_fail() {
226     echo "$@" >&2
227     exit 1
228 }
229
230 test_skipped() {
231     echo "$@" >&2
232     echo "$@" > "$TMP/whyskipped"
233     exit 77
234 }
235
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.
239 test_xfail() {
240     echo "$@" >&2
241     exit 78
242 }
243
244 # Determine what shell command will appropriately test for links.
245 ln -s foo "$scratchdir/testlink"
246 for cmd in test /bin/test /usr/bin/test /usr/ucb/bin/test /usr/ucb/test
247 do
248     for switch in -h -L
249     do
250         if $cmd $switch "$scratchdir/testlink" 2>/dev/null
251         then
252             # how nice
253             TEST_SYMLINK_CMD="$cmd $switch"
254             # i wonder if break 2 is portable?
255             break 2
256         fi
257    done
258 done
259 # ok, now get rid of it
260 rm "$scratchdir/testlink"
261
262
263 if [ "x$TEST_SYMLINK_CMD" = 'x' ]
264 then
265     test_fail "Couldn't determine how to test for symlinks"
266 else
267     echo "Testing for symlinks using '$TEST_SYMLINK_CMD'"
268 fi
269         
270
271 # Test whether something is a link, allowing for shell peculiarities
272 is_a_link() {
273     # note the variable contains the first option and therefore is not quoted
274     $TEST_SYMLINK_CMD "$1"
275 }
276
277
278 # We need to set the umask to be reproducible.  Note also that when we
279 # do some daemon tests as root, we will setuid() and therefore the
280 # directory has to be writable by the nobody user in some cases.  The
281 # best thing is probably to explicitly chmod those directories after
282 # creation.
283  
284 umask 022