ocserv/tests/test-namespace-listen
Nikos Mavrogiannopoulos bc4b1c5f12 tests: auto-generate random addresses instead of hard-coding them
The main benefit is that there is less manual work to add a
test (discovery of unique random addresses is not necessary),
but it also ensures that the tests can run on environments where the
previously hard-coded addresses were present.

Signed-off-by: Nikos Mavrogiannopoulos <n.mavrogiannopoulos@gmail.com>
2023-12-29 21:18:28 +01:00

120 lines
3.6 KiB
Bash
Executable File

#!/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)
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`/random-net.sh
. `dirname $0`/random-net2.sh
. `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=pin-sha256:xp3scfzy3rOQsv9NcOve/8YVVv+pHr4qNCXEXrNl5s8= --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