#!/bin/bash
#
# Copyright (C) 2020 William Dauchy
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# we start server in ns1, but listen in ns2, and test client from ns3

SERV="${SERV:-../src/ocserv}"
PIDFILE=ocserv-pid.$$.tmp
CLIPID=oc-pid.$$.tmp
srcdir=${srcdir:-.}
OUTFILE=ss.$$.tmp
SS=$(which ss)

ADDRESS=10.213.2.1
ADDRESS2=10.213.2.2
CLI_ADDRESS=10.213.1.1
CLI_ADDRESS2=10.213.1.2
VPNNET=172.17.215.0/24
VPNADDR=172.17.215.1

function finish {
  set +e
  echo " * Cleaning up..."
  test -f "${CLIPID}" && kill $(cat ${CLIPID}) >/dev/null 2>&1
  test -n "${CLIPID}" && rm -f ${CLIPID} >/dev/null 2>&1
  test -n "${PID}" && kill ${PID} >/dev/null 2>&1
  test -n "${PIDFILE}" && rm -f ${PIDFILE} >/dev/null 2>&1
  test -n "${CONFIG}" && rm -f ${CONFIG} >/dev/null 2>&1
  test -n "${OUTFILE}" && rm -f ${OUTFILE} >/dev/null 2>&1
}
trap finish EXIT

. `dirname $0`/common.sh
. `dirname $0`/ns.sh
LISTEN_NS=$NSNAME2

eval "${GETPORT}"

update_config test-namespace-listen.config
if test "$VERBOSE" = 1;then
DEBUG="-d 3"
fi

${CMDNS1} ${SERV} -p ${PIDFILE} -f -c ${CONFIG} ${DEBUG} & PID=$!

sleep 5

echo "server: check listen tcp"
${SS} -N ${LISTEN_NS} -nt -o state listening sport = :${PORT} src ${ADDRESS} > ${OUTFILE}
grep -E "0[ ]+[0-9]+[ ]+${ADDRESS}:${PORT}[ ]+.*?:\*" ${OUTFILE}
if test $? != 0; then
	echo "server: check listen tcp failed"
	cat ${OUTFILE}
	exit 1
fi

echo "server: check listen udp"
${SS} -N ${LISTEN_NS} -nu -o state unconnected sport = :${PORT} src ${ADDRESS} > ${OUTFILE}
grep -E "0[ ]+0[ ]+${ADDRESS}:${PORT}[ ]+.*?:\*" ${OUTFILE}
if test $? != 0; then
	echo "server: check listen udp failed"
	cat ${OUTFILE}
	exit 1
fi

echo " connecting to server"
(echo "test" | ${CMDNS3} $OPENCONNECT $ADDRESS:$PORT -u "test" --servercert=d66b507ae074d03b02eafca40d35f87dd81049d3 --pid-file=${CLIPID} -b) ||
	fail $PID "could not connect to server"
sleep 5

echo "server: check estab tcp"
${SS} -N ${LISTEN_NS} -nt -o state established sport = :${PORT} src ${ADDRESS} dst ${CLI_ADDRESS2} > ${OUTFILE}
grep -E "0[ ]+0[ ]+${ADDRESS}:${PORT}[ ]+${CLI_ADDRESS2}:[0-9]+" ${OUTFILE}
if test $? != 0; then
	echo "server: check estab tcp failed"
	cat ${OUTFILE}
	exit 1
fi

echo "server: check estab udp"
${SS} -N ${LISTEN_NS} -nu -o state established sport = :${PORT} src ${ADDRESS} dst ${CLI_ADDRESS2} > ${OUTFILE}
grep -E "0[ ]+0[ ]+${ADDRESS}:${PORT}[ ]+${CLI_ADDRESS2}:[0-9]+" ${OUTFILE}
if test $? != 0; then
	echo "server: check estab udp failed"
	cat ${OUTFILE}
	exit 1
fi

echo "client: check estab tcp"
${SS} -N ${NSNAME3} -nt -o state established dport = :${PORT} src ${CLI_ADDRESS2} dst ${ADDRESS} > ${OUTFILE}
grep -E "0[ ]+0[ ]+${CLI_ADDRESS2}:[0-9]+[ ]+${ADDRESS}:${PORT}" ${OUTFILE}
if test $? != 0; then
	echo "client: check estab tcp failed"
	cat ${OUTFILE}
	exit 1
fi

echo "client: check estab udp"
${SS} -N ${NSNAME3} -nu -o state established dport = :${PORT} src ${CLI_ADDRESS2} dst ${ADDRESS} > ${OUTFILE}
grep -E "0[ ]+0[ ]+${CLI_ADDRESS2}:[0-9]+[ ]+${ADDRESS}:${PORT}" ${OUTFILE}
if test $? != 0; then
	echo "client: check estab udp failed"
	cat ${OUTFILE}
	exit 1
fi

kill $(cat ${CLIPID})
kill ${PID}
wait

exit 0
