Doc permissions while testing.
[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 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 # So normally basically $1 should be an rsync command, and $2 and $3
141 # the source and destination directories.  This is only good when you
142 # expect to transfer the whole directory exactly as is.  If some files
143 # should be excluded, you might need to use something else.
144
145 checkit() {
146     failed=
147
148     # We can just write everything to stdout/stderr, because the
149     # wrapper hides it unless there is a problem.
150
151     echo "Running: \"$1\""  
152     eval "$1" 
153     status=$?
154     if [ $status != 0 ]; then
155         failed="YES";
156     fi
157
158     echo "-------------"
159     echo "check how the files compare with diff:"
160     echo ""
161     for f in `cd "$2"; find . -type f -print `
162     do 
163         diff -c "$2"/"$f" "$3"/"$f" || failed=YES
164     done
165
166     echo "-------------"
167     echo "check how the directory listings compare with diff:"
168     echo ""
169     ( cd "$2" && rsync_ls_lR . ) > ${TMP}/ls-from 
170     ( cd "$3" && rsync_ls_lR . ) > ${TMP}/ls-to 
171     diff -c ${TMP}/ls-from ${TMP}/ls-to || failed=YES
172     if [ -z "${failed}" ] ; then
173         return 0
174     else
175         return 1
176     fi
177 }
178
179
180 # In fact, we need a more general feature of capturing all stderr/log files,
181 # and dumping them if something goes wrong.
182
183 checkforlogs() {
184   # skip it if we're under debian-test
185   if test -n "${Debian}" ; then return 0 ; fi
186
187   if [ -f $1 -a -s $1 ] ; then
188         echo "Failures have occurred.  $1 follows:" >&2
189         cat $1 >&2
190         exit 1
191   fi
192 }
193
194
195 build_rsyncd_conf() {
196     # Build an appropriate configuration file
197     conf="$scratchdir/test-rsyncd.conf"
198     echo "building configuration $conf"
199
200     port=2612
201     pidfile="$scratchdir/rsyncd.pid"
202     logfile="$scratchdir/rsyncd.log"
203
204     cat >$conf <<EOF
205 # rsyncd configuration file autogenerated by $0
206
207 pid file = $pidfile
208 use chroot = no
209 hosts allow = localhost, 127.0.0.1
210 log file = $logfile
211
212 # We don't know if this machine has "nobody" or "nogroup", so use the quasi-canonical
213 # values of (uint16_t) -2. 
214
215 uid = 65534
216 gid = 65534
217
218 [test-from]
219         path = $FROM
220         read only = yes
221
222 [test-to]
223         path = $TO
224         read only = no
225 EOF
226 }
227
228
229 build_symlinks() {
230     fromdir="$scratchdir/from"
231     todir="$scratchdir/to"
232     mkdir "$fromdir"
233     date >"$fromdir/referent"
234     ln -s referent "$fromdir/relative"
235     ln -s "$fromdir/referent" "$fromdir/absolute"
236     ln -s nonexistent "$fromdir/dangling"
237     ln -s "$srcdir/rsync.c" "$fromdir/unsafe"
238 }
239
240 test_fail() {
241     echo "$@" >&2
242     exit 1
243 }
244
245 test_skipped() {
246     echo "$@" >&2
247     echo "$@" > "$TMP/whyskipped"
248     exit 77
249 }
250
251 # It failed, but we expected that.  don't dump out error logs, 
252 # because most users won't want to see them.  But do leave
253 # the working directory around.
254 test_xfail() {
255     echo "$@" >&2
256     exit 78
257 }
258
259 # Test whether something is a link, allowing for shell peculiarities
260 is_a_link() {
261     test -L "$1" || test -h "$1" || /usr/bin/test -L "$1" || /usr/bin/test -h "$1"
262 }
263
264
265 # We need to set the umask to be reproducible.  Note also that when we
266 # do some daemon tests as root, we will setuid() and therefore the
267 # directory has to be writable by the nobody user in some cases.  The
268 # best thing is probably to explicitly chmod those directories after
269 # creation.
270  
271 umask 022