#!/bin/bash
#
# Samuel Jero
# Ohio University
# October 10, 2011
#
# This test pertains to issue #313 "Overlapping memcpy in BP bundle acquisition system".
# This test sends multiple several bundles inside an LTP segment to ensure that
# they are correctly separated out without corruption. The first part checks
# for error messages in the log file indicitive of corruption. The second part uses
# cfdptest to send a file as a sequence of many bundles and verifies that
# the file was received correctly.


function end(){
# Shut down ION processes.
echo "Stopping ION..."
./ionstop &
sleep 5
killm

echo "Overlapping memcpy() test complete"
cd ..
exit $RETVAL
}


CONFIGFILES=" \
./1.ipn.bp.ltp.udp/config.ionrc \
./1.ipn.bp.ltp.udp/config.ltprc \
./1.ipn.bp.ltp.udp/config.bprc \
./1.ipn.bp.ltp.udp/config.ipnrc \
./1.ipn.bp.ltp.udp/config.cfdprc"


echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: Testing the functionality of issue 313.
        Confirms that multiple bundles sent to BP at the same time
        are correctly processed without corruption"
echo
echo "CONFIG: 1 node custom:"
echo
for N in $CONFIGFILES
do
	echo "$N:"
	cat $N
	echo "# EOF"
	echo
done
echo "OUTPUT: Terminal messages will relay results."
echo
echo "########################################"

./cleanup

RETVAL=1


#Start ION
echo "Starting ion node..."
export ION_NODE_LIST_DIR=$PWD
rm -f ./ion_nodes
cd 1.ipn.bp.ltp.udp
./ionstart > node.stdout

sleep 10

echo ""
echo "Test 1---Use bpdriver and bpcounter to send a bunch of bundles and check log for error messages"
echo "Starting bpcounter..."
bpcounter ipn:1.1 &	
BPR_PID=$!

echo "Sending bundles..."
bpdriver 100 ipn:1.2 ipn:1.1 -100


echo "Waiting 5 seconds for bpcounter to receive bundles..."
sleep 5

echo "Stopping bprecvfile..."
kill -2 $BPR_PID >/dev/null 2>&1
sleep 1 
kill -9 $BPR_PID >/dev/null 2>&1

#Compare files
GREP=`grep "Wrong bundle version (should be 6)" ion.log`
if [ -n "$GREP" ]; then
	#We have a problem. The Bundles were corrupted in transit.
	echo "The Bundles were corrupted in transit.	FAILURE!"
	echo ""
	RETVAL=1
	end
else
	# Success.
	echo "ION Successfully transmitted the bundles	 SUCCESS!"
	echo ""
	RETVAL=0
fi
	

echo "Test 2---if cfdptest is installed, send and verify a file"
# Check if CFDP is available on this system
PROGRAMTEST="cfdptest"
RESULTS=`which ${PROGRAMTEST}`
WHICHRETURN=$?
echo "${RESULTS}" | grep "^no ${PROGRAMTEST} in"
WHICHFAILMESSAGE=$?
# which could return the proper fail condition, or the results could be
# empty, or you're on solaris and your results will be "no netcat in $PATH".
if [ $WHICHRETURN -ne 0 -o -z "${RESULTS}" -o $WHICHFAILMESSAGE -eq 0 ] ; then
	echo "${PROGRAMTEST} is not present in this system; skipping Test 2"
	end
fi
sleep 1

echo "Starting CFDP..."
cfdpadmin config.cfdprc

echo "Sending file..."
CMD_FILE="cfdpcmds"
echo "d 1" > $CMD_FILE
echo "f ../dotest" >> $CMD_FILE
echo "t test" >> $CMD_FILE
echo "&" >> $CMD_FILE
cfdptest < $CMD_FILE

echo "Waiting 5 seconds for file to be received..."
sleep 5

DIFF=`diff ../dotest test`
if [ -n "$DIFF" ] && [ -e "test" ]; then
	#we have a problem
	echo "The file was corrupted in transit. FAILURE!"
	echo ""
	RETVAL=1
else
	#Success
	echo "The file was transfered successfully. SUCCESS!"
	echo ""
	RETVAL=0
fi

#Stopping CFDP
cfdpadmin .
sleep 1

end
