| 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 | failed= |
| 139 | |
| 140 | # We can just write everything to stdout/stderr, because the |
| 141 | # wrapper hides it unless there is a problem. |
| 142 | |
| 143 | echo "Running: \"$1\"" |
| 144 | eval "$1" |
| 145 | status=$? |
| 146 | if [ $status != 0 ]; then |
| 147 | failed="YES"; |
| 148 | fi |
| 149 | |
| 150 | echo "-------------" |
| 151 | echo "check how the files compare with diff:" |
| 152 | echo "" |
| 153 | diff -cr $2 $3 || failed=YES |
| 154 | echo "-------------" |
| 155 | echo "check how the directory listings compare with diff:" |
| 156 | echo "" |
| 157 | ( cd "$2" && rsync_ls_lR . ) > ${TMP}/ls-from |
| 158 | ( cd "$3" && rsync_ls_lR . ) > ${TMP}/ls-to |
| 159 | diff -c ${TMP}/ls-from ${TMP}/ls-to || failed=YES |
| 160 | if [ -z "${failed}" ] ; then |
| 161 | return 0 |
| 162 | else |
| 163 | return 1 |
| 164 | fi |
| 165 | } |
| 166 | |
| 167 | |
| 168 | # In fact, we need a more general feature of capturing all stderr/log files, |
| 169 | # and dumping them if something goes wrong. |
| 170 | |
| 171 | checkforlogs() { |
| 172 | # skip it if we're under debian-test |
| 173 | if test -n "${Debian}" ; then return 0 ; fi |
| 174 | |
| 175 | if [ -f $1 -a -s $1 ] ; then |
| 176 | echo "Failures have occurred. $1 follows:" >&2 |
| 177 | cat $1 >&2 |
| 178 | exit 1 |
| 179 | fi |
| 180 | } |
| 181 | |
| 182 | |
| 183 | build_rsyncd_conf() { |
| 184 | # Build an appropriate configuration file |
| 185 | conf="$scratchdir/test-rsyncd.conf" |
| 186 | echo "building configuration $conf" |
| 187 | |
| 188 | port=2612 |
| 189 | pidfile="$scratchdir/rsyncd.pid" |
| 190 | logfile="$scratchdir/rsyncd.log" |
| 191 | |
| 192 | cat >$conf <<EOF |
| 193 | # rsyncd configuration file autogenerated by $0 |
| 194 | |
| 195 | pid file = $pidfile |
| 196 | use chroot = no |
| 197 | hosts allow = localhost, 127.0.0.1 |
| 198 | log file = $logfile |
| 199 | |
| 200 | [test-from] = $scratchdir/daemon-from/ |
| 201 | read only = yes |
| 202 | |
| 203 | [test-to] = $scratchdir/daemon-to/ |
| 204 | read only = no |
| 205 | EOF |
| 206 | } |
| 207 | |
| 208 | |
| 209 | build_symlinks() { |
| 210 | fromdir="$scratchdir/from" |
| 211 | todir="$scratchdir/to" |
| 212 | mkdir "$fromdir" |
| 213 | date >"$fromdir/referent" |
| 214 | ln -s referent "$fromdir/relative" |
| 215 | ln -s "$fromdir/referent" "$fromdir/absolute" |
| 216 | ln -s nonexistent "$fromdir/dangling" |
| 217 | ln -s "$srcdir/rsync.c" "$fromdir/unsafe" |
| 218 | } |
| 219 | |
| 220 | test_fail() { |
| 221 | echo "$@" >&2 |
| 222 | exit 1 |
| 223 | } |
| 224 | |
| 225 | # be reproducible |
| 226 | umask 077 |