diff --git a/tests/base64decode.sh b/tests/base64decode.sh index eef96fe..a0c656c 100755 --- a/tests/base64decode.sh +++ b/tests/base64decode.sh @@ -1,23 +1,26 @@ -#!/usr/bin/env bash +#!/bin/sh input=$(mktemp) binary=$(mktemp) -trap "rm -f $input $binary" EXIT +trap 'rm -f "$input" "$binary"' 0 -function sseq() +sseq() { - for ((i = $1; i < $2; i=i<<1)); do - echo $i + i=$1 + limit=$2 + while [ "$i" -lt "$limit" ]; do + echo "$i" + i=$((i << 1)) done } -function do_base64() +do_base64() { - if [ -n "$(type -p base64)" ]; then - base64 $1 - elif [ -n "$(type -p uuencode)" ]; then - uuencode -m $1 data | sed 1d | sed '$d' + if command -v base64 >/dev/null 2>&1; then + base64 "$1" + elif command -v uuencode >/dev/null 2>&1; then + uuencode -m "$1" data | sed 1d | sed '$d' else echo "No tool found for base64 encoding." >&2 exit 1 @@ -26,13 +29,10 @@ function do_base64() for i in $(sseq 1 1024) 2048 10240; do - echo $i - dd if=/dev/urandom of=$binary bs=1 count=$i &>/dev/null - echo "-----BEGIN INITSTATE-----" > $input - do_base64 $binary >> $input - echo "-----END INITSTATE-----" >> $input - ./base64decode $input $binary - if [ $? -ne 0 ]; then - exit 1 - fi + echo "$i" + dd if=/dev/urandom of="$binary" bs=1 count="$i" >/dev/null 2>&1 + echo "-----BEGIN INITSTATE-----" > "$input" + do_base64 "$binary" >> "$input" + echo "-----END INITSTATE-----" >> "$input" + ./base64decode "$input" "$binary" || exit 1 done diff --git a/tests/common b/tests/common index 11e2548..c03f822 100644 --- a/tests/common +++ b/tests/common @@ -3,12 +3,15 @@ # Get the size of a file in bytes # # @1: filename -function get_filesize() +get_filesize() { - if [[ "$(uname -s)" =~ (Linux|CYGWIN_NT-|GNU) ]]; then - stat -c%s "$1" - else - # OpenBSD - stat -f%z "$1" - fi + case "$(uname -s)" in + Linux|CYGWIN_NT-*|GNU) + stat -c%s "$1" + ;; + *) + # OpenBSD + stat -f%z "$1" + ;; + esac } diff --git a/tests/fuzz.sh b/tests/fuzz.sh index 5e21c37..ebe1b80 100755 --- a/tests/fuzz.sh +++ b/tests/fuzz.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh TESTDIR=${abs_top_testdir:-$(dirname "$0")} DIR=${PWD} @@ -12,8 +12,8 @@ while :; do echo "Passing test cases $l to $((l + MAXLINES))" tmp=$(echo "${corpus}" | sed -n "${l},$((l + MAXLINES))p") [ -z "${tmp}" ] && exit 0 - ${DIR}/fuzz ${tmp} + "${DIR}/fuzz" ${tmp} rc=$? - [ $rc -ne 0 ] && exit $rc + [ "$rc" -ne 0 ] && exit "$rc" l=$((l + MAXLINES)) done diff --git a/tests/oss-fuzz.sh b/tests/oss-fuzz.sh index ba65c8c..b00eed0 100755 --- a/tests/oss-fuzz.sh +++ b/tests/oss-fuzz.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh set -ex @@ -8,20 +8,21 @@ export WORK=${WORK:-$(pwd)} export OUT=${OUT:-$(pwd)/out} CFLAGS="${CFLAGS} -fno-sanitize=bounds" # due to casts to Crypt_Int* -mkdir -p $OUT +mkdir -p "$OUT" build=$WORK/build -rm -rf $build -mkdir -p $build +rm -rf "$build" +mkdir -p "$build" export LIBTPMS=$(pwd) autoreconf -vfi -cd $build -$LIBTPMS/configure --disable-shared --enable-static --with-openssl --with-tpm2 -make -j$(nproc) && make -C tests fuzz +cd "$build" +"$LIBTPMS"/configure --disable-shared --enable-static --with-openssl --with-tpm2 +jobs=$(getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1) +make -j"$jobs" && make -C tests fuzz zip -jqr $OUT/fuzz_seed_corpus.zip "$LIBTPMS/tests/corpus-execute-command" -find $build -type f -executable -name "fuzz*" -exec mv {} $OUT \; -find $build -type f -name "*.options" -exec mv {} $OUT \; +find "$build" -type f -executable -name "fuzz*" -exec mv {} "$OUT" \; +find "$build" -type f -name "*.options" -exec mv {} "$OUT" \; diff --git a/tests/run-fuzzer.sh b/tests/run-fuzzer.sh index 0a86c63..2d47f46 100755 --- a/tests/run-fuzzer.sh +++ b/tests/run-fuzzer.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # For the license, see the LICENSE file in the root directory. @@ -6,18 +6,19 @@ DIR=${PWD}/$(dirname "$0") ROOT=${DIR}/.. WORKDIR=$(mktemp -d) -function cleanup() +cleanup() { - rm -rf ${WORKDIR} + rm -rf "${WORKDIR}" } -trap "cleanup" QUIT EXIT +trap 'cleanup' 0 3 -pushd $WORKDIR +oldpwd=$(pwd) +cd "$WORKDIR" || exit 1 -${DIR}/fuzz $@ ${DIR}/corpus-execute-command +"${DIR}/fuzz" "$@" "${DIR}/corpus-execute-command" rc=$? -popd +cd "$oldpwd" || exit 1 exit $rc diff --git a/tests/tpm2_createprimary.sh b/tests/tpm2_createprimary.sh index 9b24848..7796d59 100755 --- a/tests/tpm2_createprimary.sh +++ b/tests/tpm2_createprimary.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # For the license, see the LICENSE file in the root directory. diff --git a/tests/tpm2_cve-2023-1017.sh b/tests/tpm2_cve-2023-1017.sh index 662b090..86dd001 100755 --- a/tests/tpm2_cve-2023-1017.sh +++ b/tests/tpm2_cve-2023-1017.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # For the license, see the LICENSE file in the root directory. diff --git a/tests/tpm2_cve-2023-1018.sh b/tests/tpm2_cve-2023-1018.sh index debe2e7..69abcfb 100755 --- a/tests/tpm2_cve-2023-1018.sh +++ b/tests/tpm2_cve-2023-1018.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # For the license, see the LICENSE file in the root directory. diff --git a/tests/tpm2_pcr_read.sh b/tests/tpm2_pcr_read.sh index fcd8858..24c9281 100755 --- a/tests/tpm2_pcr_read.sh +++ b/tests/tpm2_pcr_read.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # For the license, see the LICENSE file in the root directory. diff --git a/tests/tpm2_run_test.sh b/tests/tpm2_run_test.sh index 2f5437a..f0b3732 100755 --- a/tests/tpm2_run_test.sh +++ b/tests/tpm2_run_test.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # For the license, see the LICENSE file in the root directory. @@ -10,20 +10,22 @@ WORKDIR=$(mktemp -d) . "${TESTDIR}/common" -function cleanup() +cleanup() { rm -rf "${WORKDIR}" } -trap "cleanup" QUIT EXIT +trap 'cleanup' 0 3 -pushd "$WORKDIR" &>/dev/null || exit 1 +oldpwd=$(pwd) +cd "$WORKDIR" || exit 1 "${DIR}/${1}" rc=$? if ! fs=$(get_filesize NVChip); then - exit 1 + cd "$oldpwd" || exit 1 + exit 1 fi exp=176832 if [ "$fs" -ne "$exp" ]; then @@ -33,6 +35,6 @@ if [ "$fs" -ne "$exp" ]; then rc=1 fi -popd &>/dev/null || exit 1 +cd "$oldpwd" || exit 1 exit $rc diff --git a/tests/tpm2_selftest.sh b/tests/tpm2_selftest.sh index d06e2ee..22504e5 100755 --- a/tests/tpm2_selftest.sh +++ b/tests/tpm2_selftest.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # For the license, see the LICENSE file in the root directory. diff --git a/tests/tpm2_setprofile.sh b/tests/tpm2_setprofile.sh index 88c6f52..8e77618 100755 --- a/tests/tpm2_setprofile.sh +++ b/tests/tpm2_setprofile.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # For the license, see the LICENSE file in the root directory. @@ -10,20 +10,22 @@ WORKDIR=$(mktemp -d) . "${TESTDIR}/common" -function cleanup() +cleanup() { rm -rf "${WORKDIR}" } -trap "cleanup" QUIT EXIT +trap 'cleanup' 0 3 -pushd "$WORKDIR" &>/dev/null || exit 1 +oldpwd=$(pwd) +cd "$WORKDIR" || exit 1 "${DIR}/tpm2_setprofile" rc=$? if ! fs=$(get_filesize NVChip); then - exit 1 + cd "$oldpwd" || exit 1 + exit 1 fi exp=176832 if [ "$fs" -ne "$exp" ]; then @@ -33,6 +35,6 @@ if [ "$fs" -ne "$exp" ]; then rc=1 fi -popd &>/dev/null || exit 1 +cd "$oldpwd" || exit 1 exit "$rc"