#!/bin/ksh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2007 Jan Pechanec.  All rights reserved.
# Use is subject to license terms.
#
# Script for testing pscp. Version: 2007-12-30
#

t1=tmp1
t2=tmp2
rr="random.data"
rand="$t1/$rr"
rand2="$t2/$rr"
host=`hostname`
conns=20

[ $# -ne 2 ] && echo "usage: pscp-test <full-path-to-tmp-dir> <full-path-to-pscp>" && exit 1

expr $1 : "^/" >/dev/null && expr $1 : "^/" >/dev/null || \
    { echo "you must use full paths" >&2; exit 1; }

cd $1 || { echo "can't cd to $1" >&2; exit 1; }

[ -x "$2" -a -r "$2" ] || { echo "bad pscp path" >&2; exit 1; }
grep "^pscp - parallel data transfer based on SSH$" "$2" >/dev/null
[ $? -ne 0 ] && echo "$2 doesn't look like pscp script" >&2 && exit 1

# prepare the environment
free=`df -k . | tail -1 | awk '{ print $4 }'`
if [ "$free" -lt 600000 ]; then
	echo "need at least 0.6GB of free space for testing" >&2
	exit 1
fi

echo "creating temporary directories in $1"
mkdir $t1 || { echo "can't create $t1 dir" >&2; exit 1; }
mkdir $t2 || { echo "can't create $t2 dir" >&2; exit 1; }

echo "creating 250MB tmp file in $1/$t1"
dd if=/dev/urandom of=$rand bs=1024 count=25000
[ $? -ne 0 ] && echo "couldn't create $rand" >&2 && exit 1
echo "tmp file created"

cd $t2
tt=d1
printf "\n== #$tt downloading to file ==\n"
$2 -s $host:$1/$rand ./$rr
[ $? -ne 0 ] && echo "test #$tt failed" >&2 && exit 1
rm ./$rr

tt=d2
printf "\n== #$tt downloading to current directory ==\n"
$2 -s $host:$1/$rand .
[ $? -ne 0 ] && echo "test #$tt failed" >&2 && exit 1
rm ./$rr

tt=d3
printf "\n== #$tt downloading non-regular file (SHOULD FAIL) ==\n"
$2 -s $host:$1/ .
[ $? -ne 1 ] && echo "test #$tt failed" >&2 && exit 1

tt=d4
printf "\n== #$tt downloading non-existent file (SHOULD FAIL) ==\n"
$2 -s $host:$1/$rand-$$ .
[ $? -ne 1 ] && echo "test #$tt failed" >&2 && exit 1

tt=d5
printf "\n== #$tt downloading to non-existent directory (SHOULD FAIL) ==\n"
$2 -s $host:$1/$rand xx-$$/xx-$$
[ $? -ne 1 ] && echo "test #$tt failed" >&2 && exit 1

tt=d6
printf "\n== #$tt downloading to current directory with $conns connections ==\n"
$2 -c $conns -s $host:$1/$rand .
[ $? -ne 0 ] && echo "test #$tt failed" >&2 && exit 1
rm ./$rr

cd ../$t1

tt=u1
printf "\n== #$tt uploading to file ==\n"
$2 -s $rr $host:$1/$rand2
[ $? -ne 0 ] && echo "test #$tt failed" >&2 && exit 1
rm ../$t2/$rr

tt=u2
printf "\n== #$tt uploading to remote directory ==\n"
$2 -s $rr $host:$1/$rand2
[ $? -ne 0 ] && echo "test #$tt failed" >&2 && exit 1
rm ../$t2/$rr

tt=u3
printf "\n== #$tt uploading to non-existent directory (SHOULD FAIL) ==\n"
$2 -s $rr $host:$1/$t1/$t1/$t2
[ $? -ne 1 ] && echo "test #$tt failed" >&2 && exit 1

tt=u4
printf "\n== #$tt uploading non-existent file (SHOULD FAIL) ==\n"
$2 -s $rr-$$ $host:$1/$t1
[ $? -ne 1 ] && echo "test #$tt failed" >&2 && exit 1

tt=u5
printf "\n== #$tt uploading to remote directory with $conns connections ==\n"
$2 -s -c $conns $rr $host:$1/$rand2
[ $? -ne 0 ] && echo "test #$tt failed" >&2 && exit 1
rm ../$t2/$rr

tt=f1
printf "\n== #$tt testing fake transfer test ==\n"
$2 -t $rr $host:$1/$rand2
[ $? -ne 0 ] && echo "test #$tt failed" >&2 && exit 1

tt=l1
printf "\n== #$tt local to local (SHOULD FAIL) ==\n"
$2 -s $rr $rr-$$
[ $? -ne 1 ] && echo "test #$tt failed" >&2 && exit 1

tt=r1
printf "\n== #$tt remote to remote (SHOULD FAIL) ==\n"
$2 -s $host:$1/$t1/$rr $host:$1/$t2/$rr
[ $? -ne 1 ] && echo "test #$tt failed" >&2 && exit 1

# clean up
rm $1/$rand
cd ..
rmdir $1/$t1
rmdir $1/$t2

printf "\nALL TESTS PASSED\n"
