Add an "unsafe" symlink to the symlink test case so we can see what happens.
[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 runtest() {
28     echo $ECHO_N "Test $1: $ECHO_C"
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
37 }
38
39 printmsg() {
40     echo "$1"
41 }
42
43
44 rsync_ls_lR() {
45     find "$@" -print | sort | xargs $TLS
46 }
47
48
49 ####################
50 # Build test directories TO and FROM, with FROM full of files.
51
52 hands_setup() {
53     # Clean before creation
54     rm -rf $FROM
55     rm -rf $TO
56
57     [ -d $TMP ] || mkdir $TMP
58     [ -d $FROM ] || mkdir $FROM
59     [ -d $TO ] || mkdir $TO
60
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
71     # set up test data
72     touch ${FROM}/empty
73     mkdir ${FROM}/emptydir
74
75     # a hundred lines of text or so
76     rsync_ls_lR "${srcdir}" > ${FROM}/filelist
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
80     umask 0
81     ln -s nolf ${FROM}/nolf-symlink
82     umask 022
83
84     cat $srcdir/*.c > ${FROM}/text
85     mkdir ${FROM}/dir
86     cp ${FROM}/text ${FROM}/dir
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
92
93 #      echo testing head:
94 #      ls -lR ${srcdir} | head -10 || echo failed
95 }
96
97
98 hands_cleanup() {
99     rm -r "$TMP"
100 }
101
102
103
104
105 ####################
106 # Many machines do not have "mkdir -p", so we have to build up long paths.
107 # How boring.  
108 makepath () {
109     echo "        makepath $1"
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 
121             if [ -d "$c" ] || mkdir "$c" 
122             then
123                 cd "$c" || return $?
124             else
125                 echo "failed to create $c" >&2; return $?
126             fi
127         done
128     )
129 }
130
131
132
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
137 checkit() {
138     log=${LOG}
139     failed=
140     # the log accumulates all output; we only display it if there 
141     # is a problem.
142
143     echo "Running: \"$1\""  >${log}
144     echo "">>${log}
145     eval "$1"  >>${log} 2>&1
146     status=$?
147     if [ $status != 0 ]; then
148         failed="YES";
149     fi
150
151     echo "-------------">>${log}
152     echo "check how the files compare with diff:">>${log}
153     echo "">>${log}
154     diff -cr $2 $3 >>${log} 2>&1 || failed=YES
155     echo "-------------">>${log}
156     echo "check how the directory listings compare with diff:">>${log}
157     echo "">>${log}
158     ( cd "$2" && rsync_ls_lR . ) > ${TMP}/ls-from 2>>${log}
159     ( cd "$3" && rsync_ls_lR . ) > ${TMP}/ls-to  2>>${log}
160     diff -c ${TMP}/ls-from ${TMP}/ls-to >>${log} 2>&1 || failed=YES
161     if [ -z "${failed}" ] ; then
162         rm $log
163         return 0
164     else
165         cat ${log}
166         rm ${log}
167         return 1
168     fi
169 }
170
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
175 checkforlogs() {
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
186
187 build_rsyncd_conf() {
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
197 # rsyncd configuration file autogenerated by $0
198
199 pid file = $pidfile
200 use chroot = no
201 hosts allow = localhost, 127.0.0.1
202 log file = $logfile
203
204 [test-from] = $scratchdir/daemon-from/
205         read only = yes
206
207 [test-to] = $scratchdir/daemon-to/
208         read only = no
209 EOF
210 }
211
212
213 build_symlinks() {
214     fromdir="$scratchdir/from"
215     todir="$scratchdir/to"
216     mkdir "$fromdir"
217     date >"$fromdir/referent"
218     ln -s referent "$fromdir/relative"
219     ln -s "$fromdir/referent" "$fromdir/absolute"
220     ln -s nonexistent "$fromdir/dangling"
221     ln -s "$srcdir/rsync.c" "$fromdir/unsafe"
222 }
223
224 test_fail() {
225     echo "$@" >&2
226     exit 1
227 }
228
229 # be reproducible
230 umask 077