Getting rid of recent "chown" since we decided to forego trying to run
[rsync/rsync.git] / testsuite / rsync.fns
CommitLineData
d820215b
MP
1#! /bin/sh
2
3# Copyright (C) 2001 by Martin Pool <mbp@samba.org>
4
5# General-purpose test functions for rsync.
3a4c683f 6
0154b302
MP
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
3a4c683f
MP
21TMP="$scratchdir"
22FROM=${TMP}/from
23TO=${TMP}/to
3a4c683f 24LOG=${TMP}/log
3fedd74b 25RSYNC="$rsync_bin"
3a4c683f 26
34db05b4
WD
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.
31RSYNCD_UID=65534
32RSYNCD_GID=65534
33
2094283b 34# Berkley's nice.
96553aa7 35PATH="$PATH:/usr/ucb"
2094283b 36
3a4c683f
MP
37runtest() {
38 echo $ECHO_N "Test $1: $ECHO_C"
501972bf
MP
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
3a4c683f
MP
47}
48
49printmsg() {
50 echo "$1"
51}
52
e052b21f 53
6773a779 54rsync_ls_lR() {
dfef3f10
MP
55 find "$@" -print | sort | xargs "$TOOLDIR/tls"
56}
57
58rsync_getgroups() {
59 "$TOOLDIR/getgroups"
57835c00
MP
60}
61
62
e052b21f
MP
63####################
64# Build test directories TO and FROM, with FROM full of files.
65
3a4c683f 66hands_setup() {
501972bf
MP
67 # Clean before creation
68 rm -rf $FROM
69 rm -rf $TO
3d807132
MP
70
71 [ -d $TMP ] || mkdir $TMP
3a4c683f
MP
72 [ -d $FROM ] || mkdir $FROM
73 [ -d $TO ] || mkdir $TO
74
e052b21f
MP
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
3a4c683f
MP
85 # set up test data
86 touch ${FROM}/empty
87 mkdir ${FROM}/emptydir
e8ca5901 88
571a4b26 89 # a hundred lines of text or so
3d807132 90 rsync_ls_lR "${srcdir}" > ${FROM}/filelist
3a4c683f
MP
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
0154b302 94 umask 0
3a4c683f 95 ln -s nolf ${FROM}/nolf-symlink
4c80c473 96 umask 022
0154b302 97
4c80c473 98 cat $srcdir/*.c > ${FROM}/text
3a4c683f 99 mkdir ${FROM}/dir
4c80c473 100 cp ${FROM}/text ${FROM}/dir
3a4c683f
MP
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
7d691654 106
8f98c608
MP
107# echo testing head:
108# ls -lR ${srcdir} | head -10 || echo failed
3a4c683f 109}
3fedd74b
MP
110
111
99cdaff7
MP
112####################
113# Many machines do not have "mkdir -p", so we have to build up long paths.
114# How boring.
115makepath () {
7c1b7890 116 echo " makepath $1"
99cdaff7
MP
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
112e7311
MP
128 if [ -d "$c" ] || mkdir "$c"
129 then
130 cd "$c" || return $?
131 else
132 echo "failed to create $c" >&2; return $?
133 fi
99cdaff7
MP
134 done
135 )
136}
137
138
139
3fedd74b
MP
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
f494f286
MP
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
3fedd74b 149checkit() {
3fedd74b 150 failed=
3fedd74b 151
cf72f204
MP
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"
3fedd74b
MP
157 status=$?
158 if [ $status != 0 ]; then
159 failed="YES";
160 fi
161
cf72f204
MP
162 echo "-------------"
163 echo "check how the files compare with diff:"
164 echo ""
d58e4c27
MP
165 for f in `cd "$2"; find . -type f -print `
166 do
371d1c36 167 diff -c "$2"/"$f" "$3"/"$f" || failed=YES
d58e4c27
MP
168 done
169
cf72f204
MP
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
3fedd74b 176 if [ -z "${failed}" ] ; then
3fedd74b
MP
177 return 0
178 else
3fedd74b
MP
179 return 1
180 fi
181}
182
863dff51 183
4a7cb3e8 184build_rsyncd_conf() {
d2094cc3
MP
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
3aae15ec 194# rsyncd configuration file autogenerated by $0
d2094cc3 195
3aae15ec
MP
196pid file = $pidfile
197use chroot = no
198hosts allow = localhost, 127.0.0.1
199log file = $logfile
d2094cc3 200
34db05b4
WD
201uid = $RSYNCD_UID
202gid = $RSYNCD_GID
98c1b325 203
2e6c7f45
MP
204[test-from]
205 path = $FROM
3aae15ec 206 read only = yes
d2094cc3 207
2e6c7f45
MP
208[test-to]
209 path = $TO
3aae15ec 210 read only = no
d2094cc3
MP
211EOF
212}
213
214
3cd2af41
MP
215build_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"
1db8b61d 223 ln -s "$srcdir/rsync.c" "$fromdir/unsafe"
3cd2af41
MP
224}
225
226test_fail() {
227 echo "$@" >&2
228 exit 1
229}
230
a217ad30
MP
231test_skipped() {
232 echo "$@" >&2
7d821932 233 echo "$@" > "$TMP/whyskipped"
a217ad30
MP
234 exit 77
235}
236
be2f866b
MP
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.
240test_xfail() {
241 echo "$@" >&2
242 exit 78
243}
244
d1239eae
MP
245# Determine what shell command will appropriately test for links.
246ln -s foo "$scratchdir/testlink"
247for cmd in test /bin/test /usr/bin/test /usr/ucb/bin/test /usr/ucb/test
248do
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
259done
cca4e067
MP
260# ok, now get rid of it
261rm "$scratchdir/testlink"
d1239eae
MP
262
263
264if [ "x$TEST_SYMLINK_CMD" = 'x' ]
265then
266 test_fail "Couldn't determine how to test for symlinks"
267else
268 echo "Testing for symlinks using '$TEST_SYMLINK_CMD'"
269fi
270
271
d96d3893
MP
272# Test whether something is a link, allowing for shell peculiarities
273is_a_link() {
d1239eae
MP
274 # note the variable contains the first option and therefore is not quoted
275 $TEST_SYMLINK_CMD "$1"
d96d3893
MP
276}
277
77867907
MP
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
ad301e48 285umask 022