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