BSD machines don't seem to have head(1).
[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 F1=text1
25 LOG=${TMP}/log
26 RSYNC="$rsync_bin"
27
28 runtest() {
29     echo $ECHO_N "Test $1: $ECHO_C"
30     if eval "$2"
31     then
32         echo "${ECHO_T} done."
33         return 0
34     else
35         echo "${ECHO_T} failed!"
36         return 1
37     fi
38 }
39
40 printmsg() {
41     echo "$1"
42 }
43
44
45 ####################
46 # Build test directories TO and FROM, with FROM full of files.
47
48 hands_setup() {
49     # Clean before creation
50     rm -rf $FROM
51     rm -rf $TO
52     
53     [ -d $FROM ] || mkdir $FROM
54     [ -d $TO ] || mkdir $TO
55
56     # On some BSD systems, the umask affects the mode of created
57     # symlinks, even though the mode apparently has no effect on how
58     # the links behave in the future, and it cannot be changed using
59     # chmod!  rsync always sets its umask to 000 so that it can
60     # accurately recreate permissions, but this script is probably run
61     # with a different umask. 
62
63     # This causes a little problem that "ls -l" of the two will not be
64     # the same.  So, we need to set our umask before doing any creations.
65
66     # set up test data
67     touch ${FROM}/empty
68     mkdir ${FROM}/emptydir
69
70     # a hundred lines of text or so
71     ls -lR $(srcdir) >${FROM}/filelist
72
73     # This might fail on systems that don't have -n
74     echo $ECHO_N "This file has no trailing lf$ECHO_C" > ${FROM}/nolf
75     umask 0
76     ln -s nolf ${FROM}/nolf-symlink
77     umask 077
78
79     cat $srcdir/*.c > ${FROM}/${F1}
80     mkdir ${FROM}/dir
81     cp ${FROM}/${F1} ${FROM}/dir
82     mkdir ${FROM}/dir/subdir
83     mkdir ${FROM}/dir/subdir/subsubdir
84     ls -ltr /etc > ${FROM}/dir/subdir/subsubdir/etc-ltr-list
85     mkdir ${FROM}/dir/subdir/subsubdir2
86     ls -lt /bin > ${FROM}/dir/subdir/subsubdir2/bin-lt-list
87 }
88
89
90
91
92 ####################
93 # Many machines do not have "mkdir -p", so we have to build up long paths.
94 # How boring.  
95 makepath () {
96     p="$1"
97     (
98         # Absolut Unix.
99         if echo $p | grep '^/' >/dev/null
100         then
101             cd /
102         fi
103     
104         # This will break if $1 contains a space.
105         for c in `echo $p | tr '/' ' '`
106         do 
107             [ -d "$c" ] || mkdir "$c" || return $? 
108             cd "$c" || return $?
109         done
110     )
111 }
112
113
114
115 ###########################
116 # Run a test (in '$1') then compare directories $2 and $3 to see if
117 # there are any difference.  If there are, explain them.
118
119 checkit() {
120     log=${LOG}
121     failed=
122     # the log accumulates all output; we only display it if there 
123     # is a problem.
124
125     echo "Running: \"$1\""  >${log}
126     echo "">>${log}
127     eval "$1"  >>${log} 2>&1
128     status=$?
129     if [ $status != 0 ]; then
130         failed="YES";
131     fi
132
133     echo "-------------">>${log}
134     echo "check how the files compare with diff:">>${log}
135     echo "">>${log}
136     diff -cr $2 $3 >>${log} 2>&1 || failed=YES
137     echo "-------------">>${log}
138     echo "check how the directory listings compare with diff:">>${log}
139     echo "">>${log}
140     ( cd $2 ; ls -laR ) > ${TMP}/ls-from 2>>${log}
141     ( cd $3 ; ls -laR ) > ${TMP}/ls-to  2>>${log}
142     diff -c ${TMP}/ls-from ${TMP}/ls-to >>${log} 2>&1 || failed=YES
143     if [ -z "${failed}" ] ; then
144         rm $log
145         return 0
146     else
147         cat ${log}
148         rm ${log}
149         return 1
150     fi
151 }
152
153
154 # In fact, we need a more general feature of capturing all stderr/log files,
155 # and dumping them if something goes wrong.
156
157 checkforlogs() {
158   # skip it if we're under debian-test
159   if test -n "${Debian}" ; then return 0 ; fi
160
161   if [ -f $1 -a -s $1 ] ; then
162         echo "Failures have occurred.  $1 follows:" >&2
163         cat $1 >&2
164         exit 1
165   fi
166 }
167
168
169 build_rsyncd_conf() {
170     # Build an appropriate configuration file
171     conf="$scratchdir/test-rsyncd.conf"
172     echo "building configuration $conf"
173
174     port=2612
175     pidfile="$scratchdir/rsyncd.pid"
176     logfile="$scratchdir/rsyncd.log"
177
178     cat >$conf <<EOF
179 # rsyncd configuration file autogenerated by $0
180
181 pid file = $pidfile
182 use chroot = no
183 hosts allow = localhost, 127.0.0.1
184 log file = $logfile
185
186 [test-from] = $scratchdir/daemon-from/
187         read only = yes
188
189 [test-to] = $scratchdir/daemon-to/
190         read only = no
191 EOF
192 }
193
194