Skip to content

Commit 500966b

Browse files
committed
tests/setup_swtpm.sh: Add script to setup temporary TPM
Add the tests/setup_swtpm.sh script which setup a Software TPM in a temporary directory, starts the swtpm socket, and sets the environment TCTI accordingly. This allows the tests to be executed locally, even with the "testing" feature. Unfortunately, it is not possible to cleanup some of the transient objects created during tests, being necessary to cleanup manually between runs by running: $ tpm2_flushcontext -t -l -s Another caveat is that the tests need to run on a single thread to avoid test cases that create objects to run in parallel, which can fill up the TPM memory with transient object contexts. For this, please run the tests on a single thread: $ cargo test --features=testing -- --test-threads=1 The swtpm socket process is stopped when exiting from the started shell. Fixes: #259 Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent 49db173 commit 500966b

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

tests/setup_swtpm.sh

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright 2021 Keylime Authors
4+
5+
# Store the old TCTI setting
6+
OLD_TCTI=$TCTI
7+
OLD_TPM2TOOLS_TCTI=$TPM2TOOLS_TCTI
8+
9+
set -euf -o pipefail
10+
11+
if [[ $# -eq 0 ]] || [[ -z "$1" ]]; then
12+
TEMPDIR=$(mktemp -d)
13+
TPMDIR="${TEMPDIR}/tpmdir"
14+
mkdir -p ${TPMDIR}
15+
else
16+
echo "Using TPM state from $1"
17+
TPMDIR=$1
18+
fi
19+
20+
# Manufacture a new Software TPM
21+
swtpm_setup --tpm2 \
22+
--tpmstate ${TPMDIR} \
23+
--createek --decryption --create-ek-cert \
24+
--create-platform-cert \
25+
--lock-nvram \
26+
--not-overwrite \
27+
--pcr-banks sha256 \
28+
--display
29+
30+
function start_swtpm {
31+
# Initialize the swtpm socket
32+
swtpm socket --tpm2 \
33+
--tpmstate dir=${TPMDIR} \
34+
--flags startup-clear \
35+
--ctrl type=tcp,port=2322 \
36+
--server type=tcp,port=2321 \
37+
--log level=1 &
38+
SWTPM_PID=$!
39+
}
40+
41+
function stop_swtpm {
42+
# Stop swtpm if running
43+
if [[ -n "$SWTPM_PID" ]]; then
44+
echo "Stopping swtpm"
45+
kill $SWTPM_PID
46+
fi
47+
}
48+
49+
# Set cleanup function to run at exit
50+
function cleanup {
51+
echo "-------- Restore TCTI settings"
52+
TCTI=$OLD_TCTI
53+
TPM2TOOLS_TCTI=$OLD_TPM2TOOLS_TCTI
54+
55+
echo "-------- Cleanup processes"
56+
stop_swtpm
57+
}
58+
trap cleanup EXIT
59+
60+
# Set the TCTI to use the swtpm socket
61+
export TCTI=swtpm
62+
export TPM2TOOLS_TCTI=swtpm
63+
64+
start_swtpm
65+
bash

0 commit comments

Comments
 (0)