Skip to content

Commit

Permalink
tests: Make test scripts do not rely on current working directory
Browse files Browse the repository at this point in the history
For better support for out-of-tree builds, this removes assumption in
which directory the test scripts are running.

Signed-off-by: Daiki Ueno <[email protected]>
  • Loading branch information
ueno committed Nov 6, 2023
1 parent 7d89b44 commit f706b05
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 47 deletions.
2 changes: 2 additions & 0 deletions tests/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Copyright (C) 2022 Simo Sorce <[email protected]>
# SPDX-License-Identifier: Apache-2.0

: ${TESTBLDDIR=.}

title()
{
case "$1" in
Expand Down
2 changes: 1 addition & 1 deletion tests/openssl.cnf.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ activate = 1
[pkcs11_sect]
module = @libtoollibs@/pkcs11@SHARED_EXT@
pkcs11-module-init-args = configDir=@testsblddir@/tmp.softokn/tokens
pkcs11-module-token-pin = file:@testsblddir@/pinfile.txt
pkcs11-module-token-pin = file:@PINFILE@
#pkcs11-module-allow-export
#pkcs11-module-load-behavior
##QUIRKS
Expand Down
26 changes: 13 additions & 13 deletions tests/setup-softhsm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ find_softhsm \

title SECTION "Set up testing system"

TMPPDIR="tmp.softhsm"
TMPPDIR="${TESTBLDDIR}/tmp.softhsm"

if [ -d ${TMPPDIR} ]; then
rm -fr ${TMPPDIR}
fi
mkdir ${TMPPDIR}

PINVALUE="12345678"
PINFILE="${PWD}/pinfile.txt"
PINFILE="${TMPPDIR}/pinfile.txt"
echo ${PINVALUE} > "${PINFILE}"

#RANDOM data
Expand All @@ -105,7 +105,7 @@ mkdir ${TOKDIR}

# Create SoftHSM configuration file
cat >"$TMPPDIR/softhsm.conf" <<EOF
directories.tokendir = $PWD/$TOKDIR
directories.tokendir = $TOKDIR
objectstore.backend = file
log.level = DEBUG
EOF
Expand Down Expand Up @@ -336,14 +336,14 @@ pkcs11-tool -O --login --pin=$PINVALUE --module="$P11LIB"
echo " ----------------------------------------------------------------------------------------------------"

title PARA "Output configurations"
BASEDIR=$(pwd)
OPENSSL_CONF=${BASEDIR}/${TMPPDIR}/openssl.cnf
OPENSSL_CONF=${TMPPDIR}/openssl.cnf

title LINE "Generate openssl config file"
sed -e "s|@libtoollibs[@]|${LIBSPATH}|g" \
sed -e "s|@libtoollibs@|${LIBSPATH}|g" \
-e "s|@testsblddir@|${TESTBLDDIR}|g" \
-e "s|@testsdir[@]|${BASEDIR}/${TMPPDIR}|g" \
-e "s|@testsdir@|${TMPPDIR}|g" \
-e "s|@SHARED_EXT@|${SHARED_EXT}|g" \
-e "s|@PINFILE@|${PINFILE}|g" \
-e "s|##QUIRKS|pkcs11-module-quirks = no-deinit|g" \
-e "/pkcs11-module-init-args/d" \
"${TESTSSRCDIR}/openssl.cnf.in" > "${OPENSSL_CONF}"
Expand All @@ -353,17 +353,17 @@ cat >> ${TMPPDIR}/testvars <<DBGSCRIPT
export P11LIB=${P11LIB}
export P11KITCLIENTPATH=${P11KITCLIENTPATH}
export PKCS11_PROVIDER_MODULE=${P11LIB}
export PKCS11_PROVIDER_DEBUG="file:${BASEDIR}/${TMPPDIR}/p11prov-debug.log"
export PKCS11_PROVIDER_DEBUG="file:${TMPPDIR}/p11prov-debug.log"
export OPENSSL_CONF="${OPENSSL_CONF}"
export SOFTHSM2_CONF=${BASEDIR}/${TMPPDIR}/softhsm.conf
export SOFTHSM2_CONF=${TMPPDIR}/softhsm.conf
export TESTSSRCDIR="${TESTSSRCDIR}"
export TESTBLDDIR="${TESTBLDDIR}"
export TOKDIR="${BASEDIR}/${TOKDIR}"
export TMPPDIR="${BASEDIR}/${TMPPDIR}"
export TOKDIR="${TOKDIR}"
export TMPPDIR="${TMPPDIR}"
export PINVALUE="${PINVALUE}"
export SEEDFILE="${BASEDIR}/${TMPPDIR}/noisefile.bin"
export RAND64FILE="${BASEDIR}/${TMPPDIR}/64krandom.bin"
export SEEDFILE="${TMPPDIR}/noisefile.bin"
export RAND64FILE="${TMPPDIR}/64krandom.bin"
export BASEURIWITHPIN="${BASEURIWITHPIN}"
export BASEURI="${BASEURI}"
Expand Down
22 changes: 11 additions & 11 deletions tests/setup-softokn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ fi

title SECTION "Set up testing system"

TMPPDIR="tmp.softokn"
TMPPDIR="${TESTBLDDIR}/tmp.softokn"
if [ -d ${TMPPDIR} ]; then
rm -fr ${TMPPDIR}
fi
mkdir ${TMPPDIR}

PINVALUE="12345678"
PINFILE="${PWD}/pinfile.txt"
PINFILE="${TMPPDIR}/pinfile.txt"
echo ${PINVALUE} > "${PINFILE}"

#RANDOM data
Expand Down Expand Up @@ -152,30 +152,30 @@ certutil -K -d "${TOKDIR}" -f "${PINFILE}"
echo " ----------------------------------------------------------------------------------------------------"

title PARA "Output configurations"
BASEDIR=$(pwd)
OPENSSL_CONF=${BASEDIR}/${TMPPDIR}/openssl.cnf
OPENSSL_CONF=${TMPPDIR}/openssl.cnf

title LINE "Generate openssl config file"
sed -e "s|@libtoollibs[@]|${LIBSPATH}|g" \
sed -e "s|@libtoollibs@|${LIBSPATH}|g" \
-e "s|@testsblddir@|${TESTBLDDIR}|g" \
-e "s|@testsdir[@]|${BASEDIR}/${TMPPDIR}|g" \
-e "s|@testsdir@|${TMPPDIR}|g" \
-e "s|@SHARED_EXT@|${SHARED_EXT}|g" \
-e "s|@PINFILE@|${PINFILE}|g" \
"${TESTSSRCDIR}/openssl.cnf.in" > "${OPENSSL_CONF}"

title LINE "Export tests variables to ${TMPPDIR}/testvars"
cat > "${TMPPDIR}/testvars" <<DBGSCRIPT
export PKCS11_PROVIDER_DEBUG="file:${BASEDIR}/${TMPPDIR}/p11prov-debug.log"
export PKCS11_PROVIDER_DEBUG="file:${TMPPDIR}/p11prov-debug.log"
export PKCS11_PROVIDER_MODULE="${SOFTOKNPATH%%/}/libsoftokn3${SHARED_EXT}"
export OPENSSL_CONF="${OPENSSL_CONF}"
export TESTSSRCDIR="${TESTSSRCDIR}"
export TESTBLDDIR="${TESTBLDDIR}"
export PINFILE="${PINFILE}"
export TOKDIR="${BASEDIR}/${TOKDIR}"
export TMPPDIR="${BASEDIR}/${TMPPDIR}"
export TOKDIR="${TOKDIR}"
export TMPPDIR="${TMPPDIR}"
export PINVALUE="${PINVALUE}"
export SEEDFILE="${BASEDIR}/${TMPPDIR}/noisefile.bin"
export RAND64FILE="${BASEDIR}/${TMPPDIR}/64krandom.bin"
export SEEDFILE="${TMPPDIR}/noisefile.bin"
export RAND64FILE="${TMPPDIR}/64krandom.bin"
export BASEURIWITHPIN="${BASEURIWITHPIN}"
export BASEURI="${BASEURI}"
Expand Down
30 changes: 15 additions & 15 deletions tests/tbasic
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ fi
OPENSSL_CONF=${ORIG_OPENSSL_CONF}

title PARA "Test EVP_PKEY_eq on public RSA key both on token"
$CHECKER ./tcmpkeys "$PUBURI" "$PUBURI"
$CHECKER ${TESTBLDDIR}/tcmpkeys "$PUBURI" "$PUBURI"
title PARA "Test EVP_PKEY_eq on public EC key both on token"
# shellcheck disable=SC2153 # ECURIs and ECXURIs are not spelling errors
$CHECKER ./tcmpkeys "$ECPUBURI" "$ECPUBURI"
$CHECKER ${TESTBLDDIR}/tcmpkeys "$ECPUBURI" "$ECPUBURI"
if [[ -n $ECXPUBURI ]]; then
title PARA "Test EVP_PKEY_eq on public explicit EC key both on token"
$CHECKER ./tcmpkeys "$ECXPUBURI" "$ECXPUBURI"
$CHECKER ${TESTBLDDIR}/tcmpkeys "$ECXPUBURI" "$ECXPUBURI"
fi

# It's important to test the commutative property since in the
Expand All @@ -168,40 +168,40 @@ fi
# in the second case it's the other way around.

title PARA "Test EVP_PKEY_eq on public RSA key via import"
$CHECKER ./tcmpkeys "$PUBURI" "${TMPPDIR}"/rsa.pub.uripin.pem
$CHECKER ${TESTBLDDIR}/tcmpkeys "$PUBURI" "${TMPPDIR}"/rsa.pub.uripin.pem
title PARA "Match private RSA key against public key"
$CHECKER ./tcmpkeys "$PRIURI" "${TMPPDIR}"/rsa.pub.uripin.pem
$CHECKER ${TESTBLDDIR}/tcmpkeys "$PRIURI" "${TMPPDIR}"/rsa.pub.uripin.pem
title PARA "Match private RSA key against public key (commutativity)"
$CHECKER ./tcmpkeys "${TMPPDIR}"/rsa.pub.uripin.pem "$PRIURI"
$CHECKER ${TESTBLDDIR}/tcmpkeys "${TMPPDIR}"/rsa.pub.uripin.pem "$PRIURI"

title PARA "Test EVP_PKEY_eq on public EC key via import"
$CHECKER ./tcmpkeys "$ECPUBURI" "${TMPPDIR}"/ec.pub.uripin.pem
$CHECKER ${TESTBLDDIR}/tcmpkeys "$ECPUBURI" "${TMPPDIR}"/ec.pub.uripin.pem
title PARA "Match private EC key against public key"
$CHECKER ./tcmpkeys "$ECPRIURI" "${TMPPDIR}"/ec.pub.uripin.pem
$CHECKER ${TESTBLDDIR}/tcmpkeys "$ECPRIURI" "${TMPPDIR}"/ec.pub.uripin.pem
title PARA "Match private EC key against public key (commutativity)"
$CHECKER ./tcmpkeys "${TMPPDIR}"/ec.pub.uripin.pem "$ECPRIURI"
$CHECKER ${TESTBLDDIR}/tcmpkeys "${TMPPDIR}"/ec.pub.uripin.pem "$ECPRIURI"

if [[ -n $ECXPUBURI ]]; then
echo "ECXPUBURI is $ECXPUBURI"
title PARA "Test EVP_PKEY_eq on public explicit EC key via import"
$CHECKER ./tcmpkeys "$ECXPUBURI" "${TMPPDIR}"/ecx.pub.uripin.pem
$CHECKER ${TESTBLDDIR}/tcmpkeys "$ECXPUBURI" "${TMPPDIR}"/ecx.pub.uripin.pem
title PARA "Match private explicit EC key against public key"
# shellcheck disable=SC2153 # ECURIs and ECXURIs are not spelling errors
$CHECKER ./tcmpkeys "$ECXPRIURI" "${TMPPDIR}"/ecx.pub.uripin.pem
$CHECKER ${TESTBLDDIR}/tcmpkeys "$ECXPRIURI" "${TMPPDIR}"/ecx.pub.uripin.pem
title PARA "Match private explicit EC key against public key (commutativity)"
$CHECKER ./tcmpkeys "${TMPPDIR}"/ecx.pub.uripin.pem "$ECXPRIURI"
$CHECKER ${TESTBLDDIR}/tcmpkeys "${TMPPDIR}"/ecx.pub.uripin.pem "$ECXPRIURI"
fi

title PARA "Test EVP_PKEY_eq with key exporting disabled"
ORIG_OPENSSL_CONF=${OPENSSL_CONF}
OPENSSL_CONF=${OPENSSL_CONF}.noexport
title PARA "Test RSA key"
$CHECKER ./tcmpkeys "$PUBURI" "$PUBURI"
$CHECKER ${TESTBLDDIR}/tcmpkeys "$PUBURI" "$PUBURI"
title PARA "Test EC key"
$CHECKER ./tcmpkeys "$ECPUBURI" "$ECPUBURI"
$CHECKER ${TESTBLDDIR}/tcmpkeys "$ECPUBURI" "$ECPUBURI"
if [[ -n $ECXPUBURI ]]; then
title PARA "Test explicit EC key"
$CHECKER ./tcmpkeys "$ECXPUBURI" "$ECXPUBURI"
$CHECKER ${TESTBLDDIR}/tcmpkeys "$ECXPUBURI" "$ECXPUBURI"
fi
OPENSSL_CONF=${ORIG_OPENSSL_CONF}

Expand Down
2 changes: 1 addition & 1 deletion tests/tdigest
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ sed "s/#pkcs11-module-load-behavior/pkcs11-module-load-behavior = early/" \
OPENSSL_CONF=${OPENSSL_CONF}.early_load

title PARA "Test Digests support"
$CHECKER ./tdigests
$CHECKER ${TESTBLDDIR}/tdigests

exit 0
12 changes: 7 additions & 5 deletions tests/test-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@
# Copyright (C) 2022 Simo sorce <[email protected]>
# SPDX-License-Identifier: Apache-2.0

TEST_PATH=$(dirname "${1}")
: ${TEST_PATH=$(dirname "${1}")}
BNAME=$(basename "${1}")

: ${TESTBLDDIR=.}

# the test name is {TEST_NAME}-{TOKEN_DRIVER}.t
# split extension
NAME=${BNAME%.*}
TEST_NAME=${NAME%-*}
TOKEN_DRIVER=${NAME#*-}

if [ -f "./tmp.${TOKEN_DRIVER}/testvars" ]; then
if [ -f "${TESTBLDDIR}/tmp.${TOKEN_DRIVER}/testvars" ]; then
# shellcheck source=/dev/null # we do not care about linting this source
source "./tmp.${TOKEN_DRIVER}/testvars"
source "${TESTBLDDIR}/tmp.${TOKEN_DRIVER}/testvars"
else
exit 77 # token not configured, skip
fi

# some tests are compiled, others are just distributed scripts
# so we need to check both the current tests build dir and the
# source tests dir in the out-of-source buils case (used by
# source tests dir in the out-of-source build case (used by
# make distcheck for example)
if [ -f "${TEST_PATH}/t${TEST_NAME}" ]; then
COMMAND="${TEST_PATH}/t${TEST_NAME}"
else
COMMAND="./t${TEST_NAME}"
COMMAND="${TESTBLDDIR}/t${TEST_NAME}"
fi

# Run the tests under valgrind with appropriate flags
Expand Down
2 changes: 1 addition & 1 deletion tests/ttls
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if [ -n "$CHECKER" ]; then
fi

title PARA "Test SSL_CTX creation"
$CHECKER ./tlsctx
$CHECKER ${TESTBLDDIR}/tlsctx

title PARA "Test an actual TLS connection"
rm -f "${TMPPDIR}/s_server_input"
Expand Down

0 comments on commit f706b05

Please sign in to comment.