#!/bin/bash
#
# Samuel Jero
# Ohio University
# March 28, 2012
#
# This test pertains to issue #344 "Create Bpsource commandline option for ttl".
# This test confirms that bpsource can now send bundles with varying ttl values.
# The test uses netcat to receive BP bundles.


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

echo "bpsource ttl option test complete."
exit $RETVAL	
}

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

echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: Testing the functionality of issue 344.
        Confirms that bpsource can now send bundles with varying ttl values.
        This test uses netcat to receive BP bundles."
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

# Check that prerequisite software for the test is installed
PROGRAMTEST="netcat"
OS=`uname`
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..."
	exit 2
fi

sleep 1
RETVAL=1


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


#Test 1---default TTL (300sec)
echo "Test 1---Check bpsource default TTL (300 sec)"
echo "Starting netcat server for test..."
cd netcat_files/
if [ "$OS" = "SunOS" ]; then
	netcat -u -l -p 4556 -s localhost > response.bpseg &
	NETCAT_SVR_PID=$!
else
	netcat -u -l -p 4556 > response.bpseg &
	NETCAT_SVR_PID=$!
fi
cd ../

echo "Using bpsource to send bundles with default ttl..."
cd 1.ipn.bp.udp
bpsource ipn:1.1 'test'
cd ../

echo "Waiting 2 seconds for netcat server to receive bundle..."
sleep 2

echo "Stopping netcat server..."
kill -2 $NETCAT_SVR_PID >/dev/null 2>&1
sleep 1 
kill -9 $NETCAT_SVR_PID >/dev/null 2>&1

# assemble the packet to be readable by wireshark
# 1.) Copy the lower layers of a precorded bundle
# 2.) Append the BP layer from netcat
cd netcat_files/
cp lowerlayers response.cap
cat response.bpseg >> response.cap

# Now we analyze the TTL value from the BP header of the bundle. We measure
# from the end of the bundle to avoid the SDNV timestamp field which will have
# a constantly changing length.
RESULTS=`cat response.cap | tail  -c14 | xxd -p -c 256 | cut -c 1-4 | tr -d '\n'`
echo "Expected value: 822c"
echo "  Actual value: $RESULTS"
if [ "$RESULTS" = "822c" ]; then
	echo "The Bundle set by ION had the correct default TTL."
	echo ""
	RETVAL=0
else
	# We have a problem.  The BP response doesn't have the ttl we expect it to have.  Test failed.
	echo "The Bundle sent by ION did not have the correct default TTL.  FAILURE!"
	echo ""
	RETVAL=1
	cd ..
	end
fi
cd ..


#Test 2---Larger TTL (3000sec)
echo "Test 2---Try Increasing bpsource TTL (3000sec)"
echo "Starting netcat server for test..."
cd netcat_files/
if [ "$OS" = "SunOS" ]; then
	netcat -u -l -p 4556 -s localhost > response.bpseg &
	NETCAT_SVR_PID=$!
else
	netcat -u -l -p 4556 > response.bpseg &
	NETCAT_SVR_PID=$!
fi
cd ../

echo "Using bpsource to send bundles with ttl of 3000 sec..."
cd 1.ipn.bp.udp
bpsource ipn:1.1 'test' -t3000
cd ../

echo "Waiting 2 seconds for netcat server to receive bundle..."
sleep 2

echo "Stopping netcat server..."
kill -2 $NETCAT_SVR_PID >/dev/null 2>&1
sleep 1 
kill -9 $NETCAT_SVR_PID >/dev/null 2>&1

# assemble the packet to be readable by wireshark
# 1.) Copy the lower layers of a precorded bundle
# 2.) Append the BP layer from netcat
cd netcat_files/
cp lowerlayers response.cap
cat response.bpseg >> response.cap

# Now we analyze the TTL value from the BP header of the bundle. We measure
# from the end of the bundle to avoid the SDNV timestamp field which will have
# a constantly changing length.
RESULTS=`cat response.cap | tail  -c14 | xxd -p -c 256 | cut -c 1-4 | tr -d '\n'`
echo "Expected value: 9738"
echo "  Actual value: $RESULTS"
if [ "$RESULTS" = "9738" ]; then
	echo "The Bundle set by ION had the correct TTL."
	echo ""
	RETVAL=0
else
	# We have a problem.  The BP response doesn't have the ttl we expect it to have.  Test failed.
	echo "The Bundle sent by ION did not have the correct TTL.  FAILURE!"
	echo ""
	RETVAL=1
	cd ..
	end
fi
cd ..


#Test 3---Smaller TTL (30sec)
echo "Test 3--Try Decreasing bpsource TTL (30sec)"
echo "Starting netcat server for test..."
cd netcat_files/
if [ "$OS" = "SunOS" ]; then
	netcat -u -l -p 4556 -s localhost > response.bpseg &
	NETCAT_SVR_PID=$!
else
	netcat -u -l -p 4556 > response.bpseg &
	NETCAT_SVR_PID=$!
fi
cd ../

echo "Using bpsource to send bundles with ttl of 30 sec..."
cd 1.ipn.bp.udp
bpsource ipn:1.1 'test' -t30
cd ../

echo "Waiting 2 seconds for netcat server to receive bundle..."
sleep 2

echo "Stopping netcat server..."
kill -2 $NETCAT_SVR_PID >/dev/null 2>&1
sleep 1 
kill -9 $NETCAT_SVR_PID >/dev/null 2>&1

# assemble the packet to be readable by wireshark
# 1.) Copy the lower layers of a precorded bundle
# 2.) Append the BP layer from netcat
cd netcat_files/
cp lowerlayers response.cap
cat response.bpseg >> response.cap

# Now we analyze the TTL value from the BP header of the bundle. We measure
# from the end of the bundle to avoid the SDNV timestamp field which will have
# a constantly changing length.
RESULTS=`cat response.cap | tail  -c13 | xxd -p -c 256 | cut -c 1-2 | tr -d '\n'`
echo "Expected value: 1e"
echo "  Actual value: $RESULTS"
if [ "$RESULTS" = "1e" ]; then
	echo "The Bundle set by ION had the correct TTL. SUCCESS!"
	echo ""
	RETVAL=0
else
	# We have a problem.  The BP response doesn't have the ttl we expect it to have.  Test failed.
	echo "The Bundle sent by ION did not have the correct TTL.  FAILURE!"
	echo ""
	RETVAL=1
fi
cd ..

end

