File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / rsync / testsuite / rsync.fns
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Fri Feb 17 15:09:30 2012 UTC (12 years, 4 months ago) by misho
Branches: rsync, MAIN
CVS tags: rsync3_0_9p0, RSYNC3_0_9, HEAD
rsync

    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: tmpdir="$scratchdir"
   22: fromdir="$tmpdir/from"
   23: todir="$tmpdir/to"
   24: chkdir="$tmpdir/chk"
   25: 
   26: # For itemized output:
   27: all_plus='+++++++++'
   28: allspace='         '
   29: dots='.....' # trailing dots after changes
   30: tab_ch='	' # a single tab character
   31: 
   32: # Berkley's nice.
   33: PATH="$PATH:/usr/ucb"
   34: 
   35: if diff -u "$srcdir/testsuite/rsync.fns" "$srcdir/testsuite/rsync.fns" >/dev/null 2>&1; then
   36:     diffopt="-u"
   37: else
   38:     diffopt="-c"
   39: fi
   40: 
   41: HOME="$scratchdir"
   42: export HOME
   43: 
   44: runtest() {
   45:     echo $ECHO_N "Test $1: $ECHO_C"
   46:     if eval "$2"
   47:     then
   48: 	echo "$ECHO_T	done."
   49: 	return 0
   50:     else
   51: 	echo "$ECHO_T failed!"
   52: 	return 1
   53:     fi
   54: }
   55: 
   56: set_cp_destdir() {
   57:     while test $# -gt 1; do
   58: 	shift
   59:     done
   60:     destdir="$1"
   61: }
   62: 
   63: # Perform a "cp -p", making sure that timestamps are really the same,
   64: # even if the copy rounded microsecond times on the destination file.
   65: cp_touch() {
   66:     cp -p "${@}" || test_fail "cp -p failed"
   67:     if test $# -gt 2 -o -d "$2"; then
   68: 	set_cp_destdir "${@}" # sets destdir var
   69: 	while test $# -gt 1; do
   70: 	    destname="$destdir/`basename $1`"
   71: 	    touch -r "$destname" "$1" "$destname"
   72: 	    shift
   73: 	done
   74:     else
   75: 	touch -r "$2" "$1" "$2"
   76:     fi
   77: }
   78: 
   79: # Call this if you want to filter out verbose messages (-v or -vv) from
   80: # the output of an rsync run (whittling the output down to just the file
   81: # messages).  This isn't needed if you use -i without -v.
   82: filter_outfile() {
   83:     sed -e '/^building file list /d' \
   84: 	-e '/^sending incremental file list/d' \
   85: 	-e '/^created directory /d' \
   86: 	-e '/^done$/d' \
   87: 	-e '/ --whole-file$/d' \
   88: 	-e '/^total: /d' \
   89: 	-e '/^client charset: /d' \
   90: 	-e '/^server charset: /d' \
   91: 	-e '/^$/,$d' \
   92: 	<"$outfile" >"$outfile.new"
   93:     mv "$outfile.new" "$outfile"
   94: }
   95: 
   96: printmsg() {
   97:     echo "$1"
   98: }
   99: 
  100: rsync_ls_lR() {
  101:     find "$@" -print | sort | sed 's/ /\\ /g' | xargs "$TOOLDIR/tls" $TLS_ARGS
  102: }
  103: 
  104: get_testuid() {
  105:     id 2>/dev/null | sed 's/^[^0-9]*\([0-9][0-9]*\).*/\1/'
  106: }
  107: 
  108: check_perms() {
  109:     perms=`"$TOOLDIR/tls" "$1" | sed 's/^[-d]\(.........\).*/\1/'`
  110:     if test $perms = $2; then
  111: 	return 0
  112:     fi
  113:     echo "permissions: $perms on $1"
  114:     echo "should be:   $2"
  115:     test_fail "failed test $3"
  116: }
  117: 
  118: rsync_getgroups() { 
  119:     "$TOOLDIR/getgroups"
  120: }
  121: 
  122: 
  123: ####################
  124: # Build test directories $todir and $fromdir, with $fromdir full of files.
  125: 
  126: hands_setup() {
  127:     # Clean before creation
  128:     rm -rf "$fromdir"
  129:     rm -rf "$todir"
  130: 
  131:     [ -d "$tmpdir" ] || mkdir "$tmpdir"
  132:     [ -d "$fromdir" ] || mkdir "$fromdir"
  133:     [ -d "$todir" ] || mkdir "$todir"
  134: 
  135:     # On some BSD systems, the umask affects the mode of created
  136:     # symlinks, even though the mode apparently has no effect on how
  137:     # the links behave in the future, and it cannot be changed using
  138:     # chmod!  rsync always sets its umask to 000 so that it can
  139:     # accurately recreate permissions, but this script is probably run
  140:     # with a different umask. 
  141: 
  142:     # This causes a little problem that "ls -l" of the two will not be
  143:     # the same.  So, we need to set our umask before doing any creations.
  144: 
  145:     # set up test data
  146:     touch "$fromdir/empty"
  147:     mkdir "$fromdir/emptydir"
  148: 
  149:     # a hundred lines of text or so
  150:     rsync_ls_lR "$srcdir" > "$fromdir/filelist"
  151: 
  152:     echo $ECHO_N "This file has no trailing lf$ECHO_C" > "$fromdir/nolf"
  153:     umask 0
  154:     ln -s nolf "$fromdir/nolf-symlink"
  155:     umask 022
  156: 
  157:     cat "$srcdir"/*.c > "$fromdir/text"
  158:     mkdir "$fromdir/dir"
  159:     cp "$fromdir/text" "$fromdir/dir"
  160:     mkdir "$fromdir/dir/subdir"
  161:     echo some data > "$fromdir/dir/subdir/foobar.baz"
  162:     mkdir "$fromdir/dir/subdir/subsubdir"
  163:     if [ -r /etc ]; then
  164: 	ls -ltr /etc > "$fromdir/dir/subdir/subsubdir/etc-ltr-list"
  165:     else
  166: 	ls -ltr / > "$fromdir/dir/subdir/subsubdir/etc-ltr-list"
  167:     fi
  168:     mkdir "$fromdir/dir/subdir/subsubdir2"
  169:     if [ -r /bin ]; then
  170: 	ls -lt /bin > "$fromdir/dir/subdir/subsubdir2/bin-lt-list"
  171:     else
  172: 	ls -lt / > "$fromdir/dir/subdir/subsubdir2/bin-lt-list"
  173:     fi
  174: 
  175: #      echo testing head:
  176: #      ls -lR "$srcdir" | head -10 || echo failed
  177: }
  178: 
  179: 
  180: ####################
  181: # Many machines do not have "mkdir -p", so we have to build up long paths.
  182: # How boring.  
  183: makepath() {
  184:     for p in "${@}"; do
  185: 	(echo "        makepath $p"
  186: 
  187: 	# Absolut Unix.
  188: 	if echo $p | grep '^/' >/dev/null
  189: 	then
  190: 	    cd /
  191: 	fi
  192:     
  193: 	# This will break if $p contains a space.
  194: 	for c in `echo $p | tr '/' ' '`
  195: 	do 
  196: 	    if [ -d "$c" ] || mkdir "$c" 
  197: 	    then
  198: 		cd "$c" || return $?
  199: 	    else
  200: 		echo "failed to create $c" >&2; return $?
  201: 	    fi
  202: 	done)
  203:     done
  204: }
  205: 
  206: 
  207: 
  208: ###########################
  209: # Run a test (in '$1') then compare directories $2 and $3 to see if
  210: # there are any difference.  If there are, explain them.
  211: 
  212: # So normally basically $1 should be an rsync command, and $2 and $3
  213: # the source and destination directories.  This is only good when you
  214: # expect to transfer the whole directory exactly as is.  If some files
  215: # should be excluded, you might need to use something else.
  216: 
  217: checkit() {
  218:     failed=
  219: 
  220:     # We can just write everything to stdout/stderr, because the
  221:     # wrapper hides it unless there is a problem.
  222: 
  223:     echo "Running: \"$1\""  
  224:     eval "$1" 
  225:     status=$?
  226:     if [ $status != 0 ]; then
  227: 	failed="$failed status=$status"
  228:     fi
  229: 
  230:     echo "-------------"
  231:     echo "check how the directory listings compare with diff:"
  232:     echo ""
  233:     ( cd "$2" && rsync_ls_lR . ) > "$tmpdir/ls-from"
  234:     ( cd "$3" && rsync_ls_lR . ) > "$tmpdir/ls-to"
  235:     diff $diffopt "$tmpdir/ls-from" "$tmpdir/ls-to" || failed="$failed dir-diff"
  236: 
  237:     echo "-------------"
  238:     echo "check how the files compare with diff:"
  239:     echo ""
  240:     if [ "x$4" != x ]; then
  241: 	echo "  === Skipping (as directed) ==="
  242:     else
  243: 	diff -r $diffopt "$2" "$3" || failed="$failed file-diff"
  244:     fi
  245: 
  246:     echo "-------------"
  247:     if [ -z "$failed" ] ; then
  248: 	return 0
  249:     fi
  250: 
  251:     echo "Failed: $failed"
  252:     return 1
  253: }
  254: 
  255: 
  256: build_rsyncd_conf() {
  257:     # Build an appropriate configuration file
  258:     conf="$scratchdir/test-rsyncd.conf"
  259:     echo "building configuration $conf"
  260: 
  261:     port=2612
  262:     pidfile="$scratchdir/rsyncd.pid"
  263:     logfile="$scratchdir/rsyncd.log"
  264:     hostname=`uname -n`
  265: 
  266:     cat >"$conf" <<EOF
  267: # rsyncd configuration file autogenerated by $0
  268: 
  269: pid file = $pidfile
  270: use chroot = no
  271: munge symlinks = no
  272: hosts allow = localhost 127.0.0.0/24 192.168.0.0/16 10.0.0.0/8 $hostname
  273: log file = $logfile
  274: log format = %i %h [%a] %m (%u) %l %f%L
  275: transfer logging = yes
  276: exclude = ? foobar.baz
  277: max verbosity = 9
  278: uid = 0
  279: gid = 0
  280: 
  281: [test-from]
  282: 	path = $fromdir
  283: 	read only = yes
  284: 	comment = r/o
  285: 
  286: [test-to]
  287: 	path = $todir
  288: 	read only = no
  289: 	comment = r/w
  290: 
  291: [test-scratch]
  292: 	path = $scratchdir
  293: 	read only = no
  294: 
  295: [test-hidden]
  296: 	path = $fromdir
  297: 	list = no
  298: EOF
  299: 
  300:     # Build a helper script to ignore exit code 23
  301:     ignore23="$scratchdir/ignore23"
  302:     echo "building help script $ignore23"
  303: 
  304:     cat >"$ignore23" <<'EOT'
  305: if "${@}"; then
  306:     exit
  307: fi
  308: 
  309: ret=$?
  310: 
  311: if test $ret = 23; then
  312:     exit
  313: fi
  314: 
  315: exit $ret
  316: EOT
  317: chmod +x "$ignore23"
  318: }
  319: 
  320: 
  321: build_symlinks() {
  322:     mkdir "$fromdir"
  323:     date >"$fromdir/referent"
  324:     ln -s referent "$fromdir/relative"
  325:     ln -s "$fromdir/referent" "$fromdir/absolute"
  326:     ln -s nonexistent "$fromdir/dangling"
  327:     ln -s "$srcdir/rsync.c" "$fromdir/unsafe"
  328: }
  329: 
  330: test_fail() {
  331:     echo "$@" >&2
  332:     exit 1
  333: }
  334: 
  335: test_skipped() {
  336:     echo "$@" >&2
  337:     echo "$@" > "$tmpdir/whyskipped"
  338:     exit 77
  339: }
  340: 
  341: # It failed, but we expected that.  don't dump out error logs, 
  342: # because most users won't want to see them.  But do leave
  343: # the working directory around.
  344: test_xfail() {
  345:     echo "$@" >&2
  346:     exit 78
  347: }
  348: 
  349: # Determine what shell command will appropriately test for links.
  350: ln -s foo "$scratchdir/testlink"
  351: for cmd in test /bin/test /usr/bin/test /usr/ucb/bin/test /usr/ucb/test
  352: do
  353:     for switch in -h -L
  354:     do
  355:         if $cmd $switch "$scratchdir/testlink" 2>/dev/null
  356: 	then
  357: 	    # how nice
  358: 	    TEST_SYMLINK_CMD="$cmd $switch"
  359: 	    # i wonder if break 2 is portable?
  360: 	    break 2
  361: 	fi
  362:    done
  363: done
  364: # ok, now get rid of it
  365: rm "$scratchdir/testlink"
  366: 
  367: 
  368: if [ "x$TEST_SYMLINK_CMD" = 'x' ]
  369: then
  370:     test_fail "Couldn't determine how to test for symlinks"
  371: else
  372:     echo "Testing for symlinks using '$TEST_SYMLINK_CMD'"
  373: fi
  374: 	
  375: 
  376: # Test whether something is a link, allowing for shell peculiarities
  377: is_a_link() {
  378:     # note the variable contains the first option and therefore is not quoted
  379:     $TEST_SYMLINK_CMD "$1"
  380: }
  381: 
  382: 
  383: # We need to set the umask to be reproducible.  Note also that when we
  384: # do some daemon tests as root, we will setuid() and therefore the
  385: # directory has to be writable by the nobody user in some cases.  The
  386: # best thing is probably to explicitly chmod those directories after
  387: # creation.
  388:  
  389: umask 022

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>