#!/bin/sh
#
# upstream-test-suite - Run the upstream test suite on the installed binary
# Copyright (C) 2019 Mike Miller
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# This is the installed path to OpenConnect to be tested
export OPENCONNECT=/usr/sbin/openconnect

# Make a copy of the tests directory from the source package
topdir=$AUTOPKGTEST_TMP
testdir=$topdir/tests
mkdir -p $testdir
cp -a tests/* $testdir
cd $testdir

# Save test script output in the autopkgtest artifact directory
logdir=$AUTOPKGTEST_ARTIFACTS/log
mkdir -p $logdir

# Emulate running configure substitutions on .in files needed by tests
for fn in configs/test-user-cert.config configs/test-user-pass.config softhsm2.conf; do
  sed -e 's|@abs_top_srcdir@|'$topdir'|g' \
      -e 's|@top_srcdir@|'$topdir'|g' \
      -e 's|@OCSERV_USER@|'$(id -un)'|g' \
      -e 's|@OCSERV_GROUP@|'$(id -gn)'|g' \
      $fn.in > $fn
done

ec_keys=$(ls certs/ec-key-*)
user_keys=$(ls certs/user-key-* | grep -Ev 'pbes2-sha256|md5-des|nonascii')

# Replicate the environment expected by the test scripts in 'make check'
export key_list="$user_keys $ec_keys"
export pkcs11_keys="object=RSA id=%01 object=EC id=%03"
export pkcs11_tokens="openconnect-test openconnect-test1 openconnect-test2"

# Run the test scripts in sequence, exiting at the end if any fail
status=0
for s in auth-certificate auth-nonascii auth-pkcs11 auth-username-pass id-test; do
  sh $s > $logdir/$s.log 2>&1
  if [ $? -eq 0 ]; then
    echo "PASS: $s"
  else
    echo "FAIL: $s"
    status=1
  fi
done

exit $status
