From 54e52b9a396ccff30758a06c9b5f30241876d48b Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Thu, 24 Sep 2020 11:48:24 +0200 Subject: tests: simplify program detection Signed-off-by: Daiki Ueno --- tests/cert-reencoding.sh | 4 +- tests/cert-tests/certtool | 4 +- tests/ocsp-tests/ocsp-must-staple-connection | 8 ++-- tests/ocsp-tests/ocsp-tls-connection | 4 +- tests/scripts/common.sh | 68 ++++++++++++++-------------- tests/slow/test-ciphers-common.sh | 4 +- tests/slow/test-hash-large.sh | 18 +++----- 7 files changed, 54 insertions(+), 56 deletions(-) diff --git a/tests/cert-reencoding.sh b/tests/cert-reencoding.sh index 22396558a0..547a37ce10 100755 --- a/tests/cert-reencoding.sh +++ b/tests/cert-reencoding.sh @@ -68,8 +68,8 @@ TLS_SERVER_PORT=$PORT eval "${GETPORT}" # Check for OpenSSL -OPENSSL=`which openssl` -if ! test -x "${OPENSSL}"; then +: ${OPENSSL=openssl} +if ! ("$OPENSSL" version) > /dev/null 2>&1; then echo "You need openssl to run this test." exit 77 fi diff --git a/tests/cert-tests/certtool b/tests/cert-tests/certtool index 4b194ac9ca..11b8b8f636 100755 --- a/tests/cert-tests/certtool +++ b/tests/cert-tests/certtool @@ -39,8 +39,8 @@ if ! test -z "${VALGRIND}"; then VALGRIND="${LIBTOOL:-libtool} --mode=execute ${VALGRIND}" fi -SETSID=$(which setsid) -if test -n "${SETSID}";then +: ${SETSID=setsid} +if ("$SETSID" --version) >/dev/null 2>&1; then ${VALGRIND} "${CERTTOOL}" --generate-privkey --rsa --outfile ${TMPFILE1} --pkcs8 --password ${PASS} if test $? != 0;then echo "private key generation failed" diff --git a/tests/ocsp-tests/ocsp-must-staple-connection b/tests/ocsp-tests/ocsp-must-staple-connection index 284864ff39..049491add6 100755 --- a/tests/ocsp-tests/ocsp-must-staple-connection +++ b/tests/ocsp-tests/ocsp-must-staple-connection @@ -69,8 +69,8 @@ OCSP_PORT=$PORT SERVER_START_TIMEOUT=10 # Check for OpenSSL -OPENSSL=`which openssl` -if ! test -x "${OPENSSL}"; then +: ${OPENSSL=openssl} +if ! ("$OPENSSL" version) > /dev/null 2>&1; then echo "You need openssl to run this test." exit 77 fi @@ -316,8 +316,8 @@ eval "${GETPORT}" TLS_SERVER_PORT=$PORT PORT=${TLS_SERVER_PORT} -TIMEOUT=$(which timeout) -if test -n "$TIMEOUT";then +: ${TIMEOUT=timeout} +if ("$TIMEOUT" --version) >/dev/null 2>&1; then ${TIMEOUT} 30 "${SERV}" --echo --disable-client-cert \ --x509keyfile="${srcdir}/ocsp-tests/certs/server_good.key" \ --x509certfile="${SERVER_CERT_FILE}" \ diff --git a/tests/ocsp-tests/ocsp-tls-connection b/tests/ocsp-tests/ocsp-tls-connection index 5431a3e670..84eda22b27 100755 --- a/tests/ocsp-tests/ocsp-tls-connection +++ b/tests/ocsp-tests/ocsp-tls-connection @@ -69,8 +69,8 @@ OCSP_PORT=$PORT SERVER_START_TIMEOUT=10 # Check for OpenSSL -OPENSSL=`which openssl` -if ! test -x "${OPENSSL}"; then +: ${OPENSSL=openssl} +if ! ("$OPENSSL" version) > /dev/null 2>&1; then echo "You need openssl to run this test." exit 77 fi diff --git a/tests/scripts/common.sh b/tests/scripts/common.sh index 275988ebd9..b9ed7eff99 100644 --- a/tests/scripts/common.sh +++ b/tests/scripts/common.sh @@ -26,35 +26,43 @@ export TZ="UTC" # command in the caller's PFCMD, or exit, indicating an unsupported # test. Prefer ss from iproute2 over the older netstat. have_port_finder() { - for file in $(which ss 2> /dev/null) /*bin/ss /usr/*bin/ss /usr/local/*bin/ss;do - if test -x "$file";then - PFCMD="$file";return 0 - fi - done + # Prefer PFCMD if set + if test "${PFCMD+set}" = set; then + return + fi - if test -z "$PFCMD";then - for file in $(which netstat 2> /dev/null) /bin/netstat /usr/bin/netstat /usr/local/bin/netstat;do - if test -x "$file";then - PFCMD="$file";return 0 + if (ss --version) > /dev/null 2>&1; then + PFCMD=ss + return + fi + + # 'ss' might be installed in /sbin + for dir in /sbin /usr/sbin /usr/local/sbin; do + if ($dir/ss --version) > /dev/null 2>&1; then + PFCMD=$dir/ss + return fi done - fi - if test -z "$PFCMD";then - echo "neither ss nor netstat found" - exit 1 + # We can't assume netstat --version for portability reasons + if (type netstat) > /dev/null 2>&1; then + PFCMD=netstat + return fi + + echo "neither ss nor netstat found" 1>&2 + exit 77 } check_if_port_in_use() { - local PORT="$1" - local PFCMD; have_port_finder + local PORT=$1 + have_port_finder $PFCMD -an|grep "[\:\.]$PORT" >/dev/null 2>&1 } check_if_port_listening() { - local PORT="$1" - local PFCMD; have_port_finder + local PORT=$1 + have_port_finder $PFCMD -anl|grep "[\:\.]$PORT"|grep LISTEN >/dev/null 2>&1 } @@ -101,26 +109,20 @@ fail() { exit_if_non_x86() { -which lscpu >/dev/null 2>&1 -if test $? = 0;then - $(which lscpu)|grep Architecture|grep x86 - if test $? != 0;then - echo "non-x86 CPU detected" - exit 0 - fi -fi + if (lscpu --version) >/dev/null 2>&1 && \ + ! lscpu 2>/dev/null | grep 'Architecture:[ ]*x86' >/dev/null; then + echo "non-x86 CPU detected" + exit + fi } exit_if_non_padlock() { -which lscpu >/dev/null 2>&1 -if test $? = 0;then - $(which lscpu)|grep Flags|grep phe - if test $? != 0;then - echo "non-Via padlock CPU detected" - exit 0 - fi -fi + if (lscpu --version) >/dev/null 2>&1 && \ + ! lscpu 2>/dev/null | grep 'Flags:[ ]*phe' >/dev/null; then + echo "non-Via padlock CPU detected" + exit + fi } wait_for_port() diff --git a/tests/slow/test-ciphers-common.sh b/tests/slow/test-ciphers-common.sh index b1a238671a..e8b38c84ed 100644 --- a/tests/slow/test-ciphers-common.sh +++ b/tests/slow/test-ciphers-common.sh @@ -74,8 +74,8 @@ if test $ret != 0; then fi #SHANI -$(which lscpu)|grep Flags|grep sha_ni >/dev/null -if test $? = 0;then +if (lscpu --version) >/dev/null 2>&1 && \ + lscpu 2>/dev/null | grep 'Flags:[ ]*sha_ni' >/dev/null; then GNUTLS_CPUID_OVERRIDE=0x20 ${PROG} ret=$? if test $ret != 0; then diff --git a/tests/slow/test-hash-large.sh b/tests/slow/test-hash-large.sh index 359a15ff72..7a052bddcd 100755 --- a/tests/slow/test-hash-large.sh +++ b/tests/slow/test-hash-large.sh @@ -44,23 +44,19 @@ run_test() { #0x1: no optimizations #"": default optimizations -SSSE3FLAG="" -SHANIFLAG="" -which lscpu >/dev/null 2>&1 -if test $? = 0;then - $(which lscpu)|grep Architecture|grep x86 >/dev/null - if test $? = 0;then - SSSE3FLAG="0x4" +FLAGS="" +if (lscpu --version) >/dev/null 2>&1; then + if lscpu 2>/dev/null | grep 'Flags:[ ]*ssse3' >/dev/null; then + FLAGS="$FLAGS 0x4" fi - $(which lscpu)|grep Flags|grep sha_ni >/dev/null - if test $? = 0;then - SHANIFLAG="0x20" + if lscpu 2>/dev/null | grep 'Flags:[ ]*sha_ni' >/dev/null; then + FLAGS="$FLAGS 0x20" fi fi WAITPID="" -for flags in "" "0x1" ${SSSE3FLAG} ${SHANIFLAG};do +for flags in "" "0x1" ${FLAGS};do run_test ${flags} & WAITPID="${WAITPID} $!" done -- cgit v1.2.1