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