tests: Fix issues related to SC2164

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 773ba1e..0cec99d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -244,9 +244,8 @@
 # SC2126: Consider using grep -c instead of grep | wc
 # SC2143: Use grep -q instead of comparing output with [ -n .. ].
 # SC2148: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.
-# SC2164: Use cd ... || exit in case cd fails.
 # SC2181: Check exit code directly with e.g. if mycmd;, not indirectly with $?.
-	shellcheck -e SC2001,SC2009,SC2010,SC2115,SC2126,SC2143,SC2148,SC2164,SC2181 \
+	shellcheck -e SC2001,SC2009,SC2010,SC2115,SC2126,SC2143,SC2148,SC2181 \
 		$(TESTS) $(TEST_UTILS) $(filter _test_%,$(EXTRA_DIST))
 
 check: check-am check-display
diff --git a/tests/_test_migration_key_2 b/tests/_test_migration_key_2
index eb1848a..71f61f4 100755
--- a/tests/_test_migration_key_2
+++ b/tests/_test_migration_key_2
@@ -3,7 +3,7 @@
 # Run the test_save_load_encrypted_state with swtpm_ioctl using the
 # read/write interface rather than ioctl
 export VTPM_NAME="vtpm-test2-migration-key"
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 bash test_migration_key
diff --git a/tests/test_encrypted_state b/tests/test_encrypted_state
index 656c5ae..6fbb867 100755
--- a/tests/test_encrypted_state
+++ b/tests/test_encrypted_state
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_getcap b/tests/test_getcap
index 0b4546a..ed42f6c 100755
--- a/tests/test_getcap
+++ b/tests/test_getcap
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_hashing b/tests/test_hashing
index 286c4e1..764a618 100755
--- a/tests/test_hashing
+++ b/tests/test_hashing
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_hashing2 b/tests/test_hashing2
index 3a538bb..b9e43e1 100755
--- a/tests/test_hashing2
+++ b/tests/test_hashing2
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_init b/tests/test_init
index 95c8d7c..fdb7d9c 100755
--- a/tests/test_init
+++ b/tests/test_init
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_locality b/tests/test_locality
index 4c88991..f6a7636 100755
--- a/tests/test_locality
+++ b/tests/test_locality
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_migration_key b/tests/test_migration_key
index 2e7384f..9523201 100755
--- a/tests/test_migration_key
+++ b/tests/test_migration_key
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_print_capabilities b/tests/test_print_capabilities
index b46aeec..0085afd 100755
--- a/tests/test_print_capabilities
+++ b/tests/test_print_capabilities
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IFACE=cuse
 bash _test_print_capabilities
diff --git a/tests/test_print_states b/tests/test_print_states
index 6dc025a..d8813fb 100755
--- a/tests/test_print_states
+++ b/tests/test_print_states
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IFACE=cuse
 bash _test_print_states
diff --git a/tests/test_resume_volatile b/tests/test_resume_volatile
index a17d2f8..5f93465 100755
--- a/tests/test_resume_volatile
+++ b/tests/test_resume_volatile
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_save_load_encrypted_state b/tests/test_save_load_encrypted_state
index d7f3168..337dbc5 100755
--- a/tests/test_save_load_encrypted_state
+++ b/tests/test_save_load_encrypted_state
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_save_load_state b/tests/test_save_load_state
index 60ac13d..5935410 100755
--- a/tests/test_save_load_state
+++ b/tests/test_save_load_state
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_setbuffersize b/tests/test_setbuffersize
index aa7131c..e105329 100755
--- a/tests/test_setbuffersize
+++ b/tests/test_setbuffersize
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_swtpm_bios b/tests/test_swtpm_bios
index ee631d6..1004484 100755
--- a/tests/test_swtpm_bios
+++ b/tests/test_swtpm_bios
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm12 b/tests/test_tpm12
index dabfd80..f298bd6 100755
--- a/tests/test_tpm12
+++ b/tests/test_tpm12
@@ -58,7 +58,7 @@
 	--tpmstate "dir=${WORKDIR}/tpm12.2" \
 	--flags not-need-init
 
-pushd "${WORKDIR}" &>/dev/null
+pushd "${WORKDIR}" &>/dev/null || exit 1
 
 for ((i = 0; i < 3; i++)); do
 	curl -sJOL https://sourceforge.net/projects/ibmswtpm/files/tpm4769tar.gz/download
@@ -76,7 +76,7 @@
 fi
 tar -xzf tpm4769tar.gz
 
-pushd libtpm &>/dev/null
+pushd libtpm &>/dev/null || exit 1
 
 patch -p0 < "${TESTDIR}/patches/libtpm.patch"
 if [ $? -ne 0 ]; then
@@ -88,7 +88,7 @@
 LIBS="" CFLAGS="-g -O2 -DOPENSSL_SUPPRESS_DEPRECATED=1" ./configure
 make -j "$(nproc)"
 
-pushd utils &>/dev/null
+pushd utils &>/dev/null || exit 1
 
 # variables used by TPM 1.2 tools and test suite
 export TPM_SERVER_PORT=${TPM_SERVER_PORT} \
@@ -143,8 +143,8 @@
 $SWTPM_IOCTL --tcp :65443 -s
 wait_process_gone "${SWTPM_PID}" 4
 
-popd &>/dev/null
-popd &>/dev/null
+popd &>/dev/null || exit 1
+popd &>/dev/null || exit 1
 
 echo "OK"
 
diff --git a/tests/test_tpm2_avoid_da_lockout b/tests/test_tpm2_avoid_da_lockout
index 643f586..5d21447 100755
--- a/tests/test_tpm2_avoid_da_lockout
+++ b/tests/test_tpm2_avoid_da_lockout
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_derived_keys b/tests/test_tpm2_derived_keys
index c99ae5a..095aad2 100755
--- a/tests/test_tpm2_derived_keys
+++ b/tests/test_tpm2_derived_keys
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_encrypted_state b/tests/test_tpm2_encrypted_state
index 91f07b0..57dbf13 100755
--- a/tests/test_tpm2_encrypted_state
+++ b/tests/test_tpm2_encrypted_state
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_file_permissions b/tests/test_tpm2_file_permissions
index 0a72602..710c92c 100755
--- a/tests/test_tpm2_file_permissions
+++ b/tests/test_tpm2_file_permissions
@@ -24,7 +24,7 @@
 ROOT=${abs_top_builddir:-$(dirname "$0")/..}
 source "${TESTDIR}/common"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_INTERFACE=cuse
 bash _test_tpm2_file_permissions
diff --git a/tests/test_tpm2_getcap b/tests/test_tpm2_getcap
index 7251dbf..2def986 100755
--- a/tests/test_tpm2_getcap
+++ b/tests/test_tpm2_getcap
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_hashing b/tests/test_tpm2_hashing
index e35c8b2..28563bf 100755
--- a/tests/test_tpm2_hashing
+++ b/tests/test_tpm2_hashing
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_hashing2 b/tests/test_tpm2_hashing2
index b6cdd04..8e0a738 100755
--- a/tests/test_tpm2_hashing2
+++ b/tests/test_tpm2_hashing2
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_hashing3 b/tests/test_tpm2_hashing3
index e9c31ec..4800d63 100755
--- a/tests/test_tpm2_hashing3
+++ b/tests/test_tpm2_hashing3
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_ibmtss2 b/tests/test_tpm2_ibmtss2
index 335d2bc..1dd4f52 100755
--- a/tests/test_tpm2_ibmtss2
+++ b/tests/test_tpm2_ibmtss2
@@ -11,7 +11,7 @@
 
 ROOT=${abs_top_builddir:-$(pwd)/..}
 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
-ABSTESTDIR=$(cd "${TESTDIR}" &>/dev/null;echo "${PWD}")
+ABSTESTDIR=$(cd "${TESTDIR}" &>/dev/null || exit 1;echo "${PWD}")
 
 PATCHESDIR=${ABSTESTDIR}/patches
 
@@ -44,11 +44,11 @@
 	--tpmstate "dir=${WORKDIR}" \
 	--flags not-need-init
 
-pushd "${WORKDIR}" &>/dev/null
+pushd "${WORKDIR}" &>/dev/null || exit 1
 
 git clone https://git.code.sf.net/p/ibmtpm20tss/tss ibmtpm20tss-tss
 
-pushd ibmtpm20tss-tss &>/dev/null
+pushd ibmtpm20tss-tss &>/dev/null || exit 1
 
 git checkout tags/v1.6.0
 if [ $? -ne 0 ]; then
@@ -108,7 +108,7 @@
 ./configure --disable-tpm-1.2
 make -j4
 
-pushd utils
+pushd utils || exit 1
 
 export TPM_SERVER_NAME=127.0.0.1
 export TPM_INTERFACE_TYPE=socsim
@@ -145,9 +145,9 @@
 	ret=1
 fi
 
-popd &>/dev/null
-popd &>/dev/null
-popd &>/dev/null
+popd &>/dev/null || exit 1
+popd &>/dev/null || exit 1
+popd &>/dev/null || exit 1
 
 [ $ret -eq 0 ] && echo "OK"
 
diff --git a/tests/test_tpm2_init b/tests/test_tpm2_init
index 7cb9a1e..4c6b094 100755
--- a/tests/test_tpm2_init
+++ b/tests/test_tpm2_init
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_locality b/tests/test_tpm2_locality
index 048af8a..cf00f36 100755
--- a/tests/test_tpm2_locality
+++ b/tests/test_tpm2_locality
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_migration_key b/tests/test_tpm2_migration_key
index 8bf95a8..399706e 100755
--- a/tests/test_tpm2_migration_key
+++ b/tests/test_tpm2_migration_key
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_partial_reads b/tests/test_tpm2_partial_reads
index 51a27c6..2fb2bfe 100755
--- a/tests/test_tpm2_partial_reads
+++ b/tests/test_tpm2_partial_reads
@@ -3,7 +3,7 @@
 # For the license, see the LICENSE file in the root directory.
 # set -x
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 ROOT=${abs_top_builddir:-$(pwd)/..}
 
diff --git a/tests/test_tpm2_print_capabilities b/tests/test_tpm2_print_capabilities
index fa739ae..bfe6279 100755
--- a/tests/test_tpm2_print_capabilities
+++ b/tests/test_tpm2_print_capabilities
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IFACE=cuse
 bash _test_tpm2_print_capabilities
diff --git a/tests/test_tpm2_print_states b/tests/test_tpm2_print_states
index 2d73923..5c63b77 100755
--- a/tests/test_tpm2_print_states
+++ b/tests/test_tpm2_print_states
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IFACE=cuse
 bash _test_tpm2_print_states
diff --git a/tests/test_tpm2_probe b/tests/test_tpm2_probe
index 9c9ae6f..2399a74 100755
--- a/tests/test_tpm2_probe
+++ b/tests/test_tpm2_probe
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_resume_volatile b/tests/test_tpm2_resume_volatile
index 8cf23a0..025defe 100755
--- a/tests/test_tpm2_resume_volatile
+++ b/tests/test_tpm2_resume_volatile
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_save_load_encrypted_state b/tests/test_tpm2_save_load_encrypted_state
index d7b3d9e..3b2fb2f 100755
--- a/tests/test_tpm2_save_load_encrypted_state
+++ b/tests/test_tpm2_save_load_encrypted_state
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_save_load_state b/tests/test_tpm2_save_load_state
index 9b361d9..14d1f5f 100755
--- a/tests/test_tpm2_save_load_state
+++ b/tests/test_tpm2_save_load_state
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_save_load_state_da_timeout b/tests/test_tpm2_save_load_state_da_timeout
index dd63a07..bc50855 100755
--- a/tests/test_tpm2_save_load_state_da_timeout
+++ b/tests/test_tpm2_save_load_state_da_timeout
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_save_load_state_locking b/tests/test_tpm2_save_load_state_locking
index 65752f1..86704d4 100755
--- a/tests/test_tpm2_save_load_state_locking
+++ b/tests/test_tpm2_save_load_state_locking
@@ -10,7 +10,7 @@
 	exit 77
 fi
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_savestate b/tests/test_tpm2_savestate
index b4ef125..c65dd25 100755
--- a/tests/test_tpm2_savestate
+++ b/tests/test_tpm2_savestate
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_setbuffersize b/tests/test_tpm2_setbuffersize
index 921886d..e56bb56 100755
--- a/tests/test_tpm2_setbuffersize
+++ b/tests/test_tpm2_setbuffersize
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_swtpm_bios b/tests/test_tpm2_swtpm_bios
index 4206cd7..49ce8d9 100755
--- a/tests/test_tpm2_swtpm_bios
+++ b/tests/test_tpm2_swtpm_bios
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_volatilestate b/tests/test_tpm2_volatilestate
index dd134ef..fde3221 100755
--- a/tests/test_tpm2_volatilestate
+++ b/tests/test_tpm2_volatilestate
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm2_wrongorder b/tests/test_tpm2_wrongorder
index a3dad69..ca50681 100755
--- a/tests/test_tpm2_wrongorder
+++ b/tests/test_tpm2_wrongorder
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm20 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_tpm_probe b/tests/test_tpm_probe
index 0528ba8..0f25fea 100755
--- a/tests/test_tpm_probe
+++ b/tests/test_tpm_probe
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_volatilestate b/tests/test_volatilestate
index 29fe26d..1f89a16 100755
--- a/tests/test_volatilestate
+++ b/tests/test_volatilestate
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse
diff --git a/tests/test_wrongorder b/tests/test_wrongorder
index f53998f..b4b8725 100755
--- a/tests/test_wrongorder
+++ b/tests/test_wrongorder
@@ -5,7 +5,7 @@
 source "${TESTDIR}/common"
 skip_test_no_tpm12 "${SWTPM_EXE}"
 
-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || exit 1
 
 export SWTPM_IOCTL_BUFFERSIZE=100
 export SWTPM_INTERFACE=cuse