#!/bin/bash
#
# Copyright (C) 2021 Nikos Mavrogiannopoulos
#
# This file is part of ocserv.
#
# ocserv 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.
#
# ocserv 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 GnuTLS; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

# Test peer-to-peer networks:
# - IPv6: use a /127 network and make sure the server is assigned the
#         network address.
# - IPv4: use a /30 network for now, until we support /31 networks, and
#         make sure the server is assigned the network address + 1.

SERV="${SERV:-../src/ocserv}"
srcdir=${srcdir:-.}
PIDFILE=ocserv-pid.$$.tmp
CLIPID=oc-pid.$$.tmp
IP=$(command -v ip)

. `dirname $0`/common.sh

eval "${GETPORT}"

if test -z "${IP}";then
	echo "no IP tool is present"
	exit 77
fi

if test "$(id -u)" != "0";then
	echo "This test must be run as root"
	exit 77
fi


echo "Testing ocserv with point-to-point networks (IPv4 /30, IPv6 /127)... "

function finish {
  echo " * Cleaning up..."
  cleanup_client_server
}
trap finish EXIT

OCCTL_SOCKET=./p2p-iface-$$.socket
USERNAME=test

IPCALC=$(command -v ipcalc-ng)
if test -z "${IPCALC}"; then
	IPCALC=$(command -v ipcalc)
fi

if test -z "${IPCALC}"; then
	echo "ipcalc was not found"
	exit 1
fi

# Generate random IPv4 P2P network (/30 - RFC 3021)
# /30 provides 4 addresses: network, 2 usable hosts, broadcast
ret=0
while [ $ret = 0 ]
do
	eval $(${IPCALC} -r 30 -np --minaddr --maxaddr)
	VPNNET="${NETWORK}/${PREFIX}"
	VPNADDR=${MINADDR}           # Server address
	VPNADDR_CLI=${MAXADDR}       # Client address
	ping -W 1 -c 2 ${VPNADDR} >/dev/null 2>&1
	ret=$?
done

# Generate random IPv6 P2P network (/127 - RFC 6164)
# /127 provides exactly 2 addresses for point-to-point links
ret=0
while [ $ret = 0 ]
do
	eval $(${IPCALC} -r 127 -np --minaddr --maxaddr)
	VPNNET6="${NETWORK}/${PREFIX}"
	VPNADDR6=${MINADDR}          # Server address
	VPNADDR6_CLI=${MAXADDR}      # Client address
	ping -W 1 -c 2 ${VPNADDR6} >/dev/null 2>&1
	ret=$?
done

echo "**************************"
echo "VPN IPv4 network: $VPNNET"
echo "VPN IPv4 server address: $VPNADDR"
echo "VPN IPv6 network: $VPNNET6"
echo "VPN IPv6 server address: $VPNADDR6"
echo "Client IPv4 address: $VPNADDR_CLI"
echo "Client IPv6 address: $VPNADDR6_CLI"
echo "**************************"

#####################. `dirname $0`/ns.sh

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

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

echo -n "Connecting to P2P interface... "
echo "test" | ${CMDNS1} $OPENCONNECT -q $ADDRESS:$PORT -u ${USERNAME} --servercert=pin-sha256:xp3scfzy3rOQsv9NcOve/8YVVv+pHr4qNCXEXrNl5s8= -s ${srcdir}/scripts/vpnc-script --pid-file=${CLIPID} --passwd-on-stdin -b
if test $? != 0;then
	echo "Could not connect to server"
	exit 1
fi

echo ok

set -e
echo -n "Verifying assigned IPv4 address... "
ASSIGNED_IPV4=$(${CMDNS2} ${OCCTL} -s ${OCCTL_SOCKET} show user ${USERNAME} | grep "IPv4:" | awk '{print $2}')
if [ "${ASSIGNED_IPV4}" != "${VPNADDR_CLI}" ];then
	echo "FAILED"
	echo "Expected IPv4: ${VPNADDR_CLI}, got: ${ASSIGNED_IPV4}"
	exit 1
fi
echo "ok (${ASSIGNED_IPV4})"

echo -n "Verifying assigned IPv6 address... "
ASSIGNED_IPV6=$(${CMDNS2} ${OCCTL} -s ${OCCTL_SOCKET} show user ${USERNAME} | grep "IPv6:" | awk '{print $2}')
if [ "${ASSIGNED_IPV6}" != "${VPNADDR6_CLI}" ];then
	echo "FAILED"
	echo "Expected IPv6: ${VPNADDR6_CLI}, got: ${ASSIGNED_IPV6}"
	exit 1
fi
echo "ok (${ASSIGNED_IPV6})"

echo ok

kill $PID
wait

exit 0
