Debian Codegraph @

tests/_test_tpm2_setbuffersize

raw symbols SCIP
1 #!/bin/bash
2
3 # For the license, see the LICENSE file in the root directory.
4 #set -x
5
6 ROOT=${abs_top_builddir:-$(pwd)/..}
7 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
8
9 VTPM_NAME="vtpm-test-tpm2-setbuffersize"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 TPM_PATH="$(mktemp -d)" || exit 1
12 STATE_FILE=$TPM_PATH/tpm2-00.permall
13 OUTFILE=$TPM_PATH/output
14 VOLATILE_STATE_FILE=$TPM_PATH/tpm2-00.volatilestate
15 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
16 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
17 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
18
19 function cleanup()
20 {
21 pid=${SWTPM_PID}
22 if [ -n "$pid" ]; then
23 kill_quiet -9 "$pid"
24 fi
25 rm -rf "$TPM_PATH"
26 }
27
28 trap "cleanup" EXIT
29
30 source "${TESTDIR}/common"
31 [ "${SWTPM_INTERFACE}" == cuse ] && source "${TESTDIR}/test_cuse"
32
33 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
34
35 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" --tpm2
36
37 if ! kill_quiet -0 "${SWTPM_PID}"; then
38 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
39 exit 1
40 fi
41
42 # Check the buffer size
43 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -b 0 > "${OUTFILE}"; then
44 echo "Error: Could not get the buffersize of the ${SWTPM_INTERFACE} TPM."
45 exit 1
46 fi
47 cat "${OUTFILE}"
48
49 if ! grep "TPM buffersize" "${OUTFILE}" | grep -q 4096; then
50 echo "Error: The TPM buffersize of the ${SWTPM_INTERFACE} TPM is not 4096."
51 exit 1
52 fi
53
54 # set the buffer size -- it's not going to change but command must not fail
55 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -b 4000 > "${OUTFILE}"; then
56 echo "Error: Could not set the buffersize of the ${SWTPM_INTERFACE} TPM."
57 exit 1
58 fi
59 cat "${OUTFILE}"
60
61 if ! grep "TPM buffersize" "${OUTFILE}" | grep -q 4000; then
62 echo "Error: Could not set the buffersize of the ${SWTPM_INTERFACE} TPM to 4000."
63 exit 1
64 fi
65
66 # Init the TPM
67 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
68 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
69 exit 1
70 fi
71
72 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
73 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
74 exit 1
75 fi
76
77 # Set the buffer size -- should fail
78 if ERR="$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -b 4096 2>&1)"; then
79 echo "Error: Could set the buffersize while the ${SWTPM_INTERFACE} TPM is running."
80 exit 1
81 fi
82 exp="TPM result from PTM_SET_BUFFERSIZE: 0xa"
83 if [ "$ERR" != "$exp" ]; then
84 echo "Error: Unexpected error message"
85 echo "Received: $ERR"
86 echo "Expected: $exp"
87 exit 1
88 fi
89
90 # Startup the TPM2
91 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00')
92 exp=' 80 01 00 00 00 0a 00 00 00 00'
93 if [ "$RES" != "$exp" ]; then
94 echo "Error: Did not get expected result from TPM2_Startup(SU_Clear)"
95 echo "expected: $exp"
96 echo "received: $RES"
97 exit 1
98 fi
99
100 # Read the Buffer sizes; we want to see '4000' (0xfa0) in the buffer sizes now
101 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x16\x00\x00\x01\x7a\x00\x00\x00\x06\x00\x00\x01\x1e\x00\x00\x00\x02')
102 exp=' 80 01 00 00 00 23 00 00 00 00 01 00 00 00 06 00 00 00 02 00 00 01 1e 00 00 0f a0 00 00 01 1f 00 00 0f a0'
103 if [ "$RES" != "$exp" ]; then
104 echo "Error: Did not get expected result from TPM2_GetCapability()"
105 echo "expected: $exp"
106 echo "received: $RES"
107 exit 1
108 fi
109
110 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
111 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
112 exit 1
113 fi
114
115 if wait_process_gone "${SWTPM_PID}" 4; then
116 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
117 exit 1
118 fi
119
120 if [ ! -e "$STATE_FILE" ]; then
121 echo "Error: TPM state file $STATE_FILE does not exist."
122 exit 1
123 fi
124
125 echo "OK"
126
127 exit 0