From 65b2e473e1922d0faabb4c58830153573907cdcd Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Thu, 14 Sep 2023 14:08:14 +0900 Subject: [PATCH] tests: Port shell-script tests to using test-init.sh Signed-off-by: Daiki Ueno --- p11-kit/test-messages.sh | 43 +++++++----- p11-kit/test-objects.sh | 146 ++++++++++++++++++--------------------- p11-kit/test-profiles.sh | 40 ++++++----- p11-kit/test-server.sh | 122 ++++++++++++++++---------------- 4 files changed, 176 insertions(+), 175 deletions(-) diff --git a/p11-kit/test-messages.sh b/p11-kit/test-messages.sh index 7283e7763..e232d72fc 100755 --- a/p11-kit/test-messages.sh +++ b/p11-kit/test-messages.sh @@ -1,18 +1,24 @@ #!/bin/sh -set -e +test "${abs_top_builddir+set}" = set || { + echo "set abs_top_builddir" 1>&2 + exit 1 +} -testdir=$PWD/test-messages-$$ -test -d "$testdir" || mkdir "$testdir" +. "$abs_top_builddir/common/test-init.sh" -cleanup () { - rm -rf "$testdir" +setup() { + testdir=$PWD/test-messages-$$ + test -d "$testdir" || mkdir "$testdir" + cd "$testdir" } -trap cleanup 0 -cd "$testdir" +teardown() { + rm -rf "$testdir" +} -cat > messages.exp < messages.exp < messages.out + : ${WINE=} + if ! ${WINE} "$abs_top_builddir"/p11-kit/print-messages | tr -d '\r' > messages.out; then + assert_fail "unable to run print-messages" + fi -echo 1..1 + : ${DIFF=diff} + if ! ${DIFF} messages.exp messages.out > messages.diff; then + sed 's/^/# /' messages.diff + assert_fail "output contains incorrect messages" + fi +} -: ${DIFF=diff} -if ${DIFF} messages.exp messages.out > messages.diff; then - echo "ok 1 /messages/return-code" -else - echo "not ok 1 /messages/return-code" - sed 's/^/# /' messages.diff - exit 1 -fi +run test_messages diff --git a/p11-kit/test-objects.sh b/p11-kit/test-objects.sh index ec4d7cc2f..f7b802f92 100755 --- a/p11-kit/test-objects.sh +++ b/p11-kit/test-objects.sh @@ -1,18 +1,24 @@ #!/bin/sh -set -e +test "${abs_top_builddir+set}" = set || { + echo "set abs_top_builddir" 1>&2 + exit 1 +} -testdir=$PWD/test-objects-$$ -test -d "$testdir" || mkdir "$testdir" +. "$abs_top_builddir/common/test-init.sh" -cleanup () { - rm -rf "$testdir" +setup() { + testdir=$PWD/test-objects-$$ + test -d "$testdir" || mkdir "$testdir" + cd "$testdir" } -trap cleanup 0 -cd "$testdir" +teardown() { + rm -rf "$testdir" +} -cat > list.exp < list.exp < list.out - -echo 1..6 + "$abs_top_builddir"/p11-kit/p11-kit-testable list-objects -q "pkcs11:" > list.out -: ${DIFF=diff} -if ${DIFF} list.exp list.out > list.diff; then - echo "ok 1 /objects/list-objects-all" -else - echo "not ok 1 /objects/list-objects-all" - sed 's/^/# /' list.diff - exit 1 -fi + : ${DIFF=diff} + if ! ${DIFF} list.exp list.out > list.diff; then + sed 's/^/# /' list.diff + assert_fail "output contains incorrect result" + fi +} -cat > list.exp < list.exp < list.out - -echo 2..6 + "$abs_top_builddir"/p11-kit/p11-kit-testable list-objects -q "pkcs11:type=data" > list.out -: ${DIFF=diff} -if ${DIFF} list.exp list.out > list.diff; then - echo "ok 2 /objects/list-objects-data" -else - echo "not ok 2 /objects/list-objects-data" - sed 's/^/# /' list.diff - exit 1 -fi + : ${DIFF=diff} + if ! ${DIFF} list.exp list.out > list.diff; then + sed 's/^/# /' list.diff + assert_fail "output contains wrong result" + fi +} -cat > list.exp < list.exp < list.out + "$abs_top_builddir"/p11-kit/p11-kit-testable list-objects -q "pkcs11:model=TEST%20MODEL;manufacturer=TEST%20MANUFACTURER;serial=TEST%20SERIAL;token=TEST%20LABEL;object=TEST%20CERTIFICATE;type=cert" > list.out -echo 3..6 - -: ${DIFF=diff} -if ${DIFF} list.exp list.out > list.diff; then - echo "ok 3 /objects/list-objects-specific" -else - echo "not ok 3 /objects/list-objects-specific" - sed 's/^/# /' list.diff - exit 1 -fi + : ${DIFF=diff} + if ! ${DIFF} list.exp list.out > list.diff; then + sed 's/^/# /' list.diff + assert_fail "output contains wrong result" + fi +} -cat > list.exp < list.exp < list.out + "$abs_top_builddir"/p11-kit/p11-kit-testable list-objects -q "pkcs11:model=NONEXISTENT" > list.out -echo 4..6 - -: ${DIFF=diff} -if ${DIFF} list.exp list.out > list.diff; then - echo "ok 4 /objects/list-objects-nonexistent" -else - echo "not ok 4 /objects/list-objects-nonexistent" - sed 's/^/# /' list.diff - exit 1 -fi + : ${DIFF=diff} + if ! ${DIFF} list.exp list.out > list.diff; then + sed 's/^/# /' list.diff + assert_fail "output contains wrong result" + fi +} -cat > list.exp < list.exp < list.out + "$abs_top_builddir"/p11-kit/p11-kit-testable export-object -q "pkcs11:model=TEST%20MODEL;manufacturer=TEST%20MANUFACTURER;serial=TEST%20SERIAL;token=TEST%20LABEL;object=TEST%20CERTIFICATE;type=cert" > list.out -echo 5..6 - -: ${DIFF=diff} -if ${DIFF} list.exp list.out > list.diff; then - echo "ok 5 /objects/export-object-cert" -else - echo "not ok 5 /objects/export-object-cert" - sed 's/^/# /' list.diff - exit 1 -fi + : ${DIFF=diff} + if ! ${DIFF} list.exp list.out > list.diff; then + sed 's/^/# /' list.diff + assert_fail "output contains wrong result" + fi +} -cat > list.exp < list.exp < list.out + "$abs_top_builddir"/p11-kit/p11-kit-testable export-object -q "pkcs11:model=TEST%20MODEL;manufacturer=TEST%20MANUFACTURER;serial=TEST%20SERIAL;token=TEST%20LABEL;object=TEST%20PUBLIC%20KEY;type=public" > list.out -echo 6..6 + : ${DIFF=diff} + if ! ${DIFF} list.exp list.out > list.diff; then + sed 's/^/# /' list.diff + assert_fail "output contains wrong result" + fi +} -: ${DIFF=diff} -if ${DIFF} list.exp list.out > list.diff; then - echo "ok 6 /objects/export-object-pubkey" -else - echo "not ok 6 /objects/export-object-pubkey" - sed 's/^/# /' list.diff - exit 1 -fi +run test_list_all test_list_with_type test_list_exact test_list_nonexistent \ + test_export_cert test_export_pubkey diff --git a/p11-kit/test-profiles.sh b/p11-kit/test-profiles.sh index 0742c6414..2ba081fec 100755 --- a/p11-kit/test-profiles.sh +++ b/p11-kit/test-profiles.sh @@ -1,30 +1,34 @@ #!/bin/sh -set -e +test "${abs_top_builddir+set}" = set || { + echo "set abs_top_builddir" 1>&2 + exit 1 +} -testdir=$PWD/test-profiles-$$ -test -d "$testdir" || mkdir "$testdir" +. "$abs_top_builddir/common/test-init.sh" -cleanup () { - rm -rf "$testdir" +setup() { + testdir=$PWD/test-profiles-$$ + test -d "$testdir" || mkdir "$testdir" + cd "$testdir" } -trap cleanup 0 -cd "$testdir" +teardown() { + rm -rf "$testdir" +} -cat > list.exp < list.exp < list.out + "$abs_top_builddir"/p11-kit/p11-kit-testable list-profiles -q pkcs11: > list.out -echo 1..1 + : ${DIFF=diff} + if ! ${DIFF} list.exp list.out > list.diff; then + sed 's/^/# /' list.diff + assert_fail "output contains wrong results" + fi +} -: ${DIFF=diff} -if ${DIFF} list.exp list.out > list.diff; then - echo "ok 1 /profiles/list" -else - echo "not ok 1 /profiles/list" - sed 's/^/# /' list.diff - exit 1 -fi +run test_list_profiles diff --git a/p11-kit/test-server.sh b/p11-kit/test-server.sh index f776d560e..b8992350f 100755 --- a/p11-kit/test-server.sh +++ b/p11-kit/test-server.sh @@ -1,80 +1,80 @@ #!/bin/sh -set -e +test "${abs_top_builddir+set}" = set || { + echo "set abs_top_builddir" 1>&2 + exit 1 +} + +. "$abs_top_builddir/common/test-init.sh" : ${P11_MODULE_PATH="$abs_top_builddir"/.libs} -testdir=`mktemp -d -t test-server.XXXXXX` -if test $? -ne 0; then - echo "cannot create temporary directory" >&2 - exit 77 -fi +setup() { + testdir=`mktemp -d -t test-server.XXXXXX` + if test $? -ne 0; then + echo "cannot create temporary directory" >&2 + exit 77 + fi + cd "$testdir" + + unset P11_KIT_SERVER_ADDRESS + unset P11_KIT_SERVER_PID -cleanup () { + export XDG_RUNTIME_DIR="$testdir" + export P11_KIT_PRIVATEDIR="${abs_top_builddir}/p11-kit" + export ASAN_OPTIONS="verify_asan_link_order=0" +} + +teardown() { rm -rf "$testdir" + if test "${P11_KIT_SERVER_PID+set}" = "set"; then + kill "$P11_KIT_SERVER_PID" + fi } -trap cleanup 0 -cd "$testdir" +test_server_access() { + "$abs_top_builddir"/p11-kit/p11-kit-server-testable -s --provider "$P11_MODULE_PATH"/mock-one.so pkcs11: > start.env 2> start.err + if test $? -ne 0; then + sed 's/^/# /' start.err + assert_fail "unable to start server" + fi -unset P11_KIT_SERVER_ADDRESS -unset P11_KIT_SERVER_PID + . ./start.env -export XDG_RUNTIME_DIR="$testdir" -export P11_KIT_PRIVATEDIR="${abs_top_builddir}/p11-kit" -export ASAN_OPTIONS="verify_asan_link_order=0" + if test "${P11_KIT_SERVER_ADDRESS-unset}" = "unset"; then + assert_fail "P11_KIT_SERVER_ADDRESS is not set" + fi -echo 1..5 + if test "${P11_KIT_SERVER_PID-unset}" = "unset"; then + assert_fail "P11_KIT_SERVER_PID is not set" + fi -"$abs_top_builddir"/p11-kit/p11-kit-server-testable -s --provider "$P11_MODULE_PATH"/mock-one.so pkcs11: > start.env 2> start.err -if test $? -eq 0; then - echo "ok 1 /server/start" -else - echo "not ok 1 /server/start" - sed 's/^/# /' start.err - exit 1 -fi + : ${P11TOOL=p11tool} + if "$P11TOOL" --version > /dev/null; then + "$P11TOOL" --provider "$P11_MODULE_PATH"/p11-kit-client.so --list-tokens > /dev/null 2> p11tool.err + if test $? -ne 0; then + sed 's/^/# /' p11tool.err + assert_fail "unable to access server" + fi + else + skip "p11tool not found" + fi -. ./start.env + "$abs_top_builddir"/p11-kit/p11-kit-server-testable -s -k > stop.env 2> stop.err + if test $? -ne 0; then + sed 's/^/# /' stop.err + assert_fail "unable to stop server" + fi -if test "${P11_KIT_SERVER_ADDRESS+set}" = "set" && test "${P11_KIT_SERVER_PID+set}" = "set"; then - echo "ok 2 /server/start-env" -else - pkill -f p11-kit-server - echo "not ok 2 /server/start-env" - exit 1 -fi + . ./stop.env -if p11tool --version > /dev/null; then - p11tool --provider "$P11_MODULE_PATH"/p11-kit-client.so --list-tokens > /dev/null 2> p11tool.err - if test $? -eq 0; then - echo "ok 3 /server/client-access" - else - pkill -f p11-kit-server - echo "not ok 3 /server/client-access" - sed 's/^/# /' p11tool.err - exit 1 + if test "${P11_KIT_SERVER_ADDRESS+set}" = "set"; then + assert_fail "P11_KIT_SERVER_ADDRESS is still set" fi -else - echo "ok 3 /server/client-access" - echo "cannot find p11tool" >&2 -fi - -"$abs_top_builddir"/p11-kit/p11-kit-server-testable -s -k > stop.env 2> stop.err -if test $? -eq 0; then - echo "ok 4 /server/stop" -else - pkill -f p11-kit-server - echo "not ok 4 /server/stop" - sed 's/^/# /' stop.err - exit 1 -fi -. ./stop.env + if test "${P11_KIT_SERVER_PID+set}" = "set"; then + assert_fail "P11_KIT_SERVER_PID is still set" + fi +} -if test "${P11_KIT_SERVER_ADDRESS-unset}" = "unset" && test "${P11_KIT_SERVER_PID-unset}" = "unset"; then - echo "ok 5 /server/stop-env" -else - echo "not ok 5 /server/stop-env" - exit 1 -fi +run test_server_access