#!/usr/bin/env bash
trap 'killm f' EXIT
#
# 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
cd 1.ipn.bp.ltp.udp
./ionstart > node.stdout

../../../system_up -i "p 30" -l "p 30" -b "p 30"

if [ "$?" -eq 3 ]
then
    echo ""
else
    echo "Node not started: Aborting Test"
    exit 1
fi


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=$(command -v "${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 ] || [ -z "$RESULTS" ] || [ "$WHICHFAILMESSAGE" -eq 0 ]; then
    echo "${PROGRAMTEST} is not present in this system; skipping Test 2"
    end  # Ensure 'end' is defined or replace it with 'exit' or another command
fi

sleep 1

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

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
