]>
Commit | Line | Data |
---|---|---|
8f0f381f | 1 | #!/usr/bin/env bash |
e46a2b66 SB |
2 | |
3 | # For the license, see the LICENSE file in the root directory. | |
4 | ||
cc410ca9 SB |
5 | if [ "$(uname -s)" != "Linux" ]; then |
6 | # Due to netstat | |
7 | echo "This test only runs only Linux." | |
8 | exit 77 | |
9 | fi | |
10 | ||
313cf75c SB |
11 | ROOT=${abs_top_builddir:-$(dirname "$0")/..} |
12 | TESTDIR=${abs_top_testdir:=$(dirname "$0")} | |
13 | ||
14 | # need SWTPM to be set | |
15 | source ${TESTDIR}/common | |
f1adde9f | 16 | skip_test_no_tpm12 "${SWTPM_EXE}" |
313cf75c | 17 | |
cce7503c | 18 | TPMDIR="$(mktemp -d)" || exit 1 |
b2151737 | 19 | PID_FILE=$TPMDIR/${SWTPM}.pid |
89d85f9a | 20 | LOG_FILE=$TPMDIR/${SWTPM}.log |
e46a2b66 | 21 | |
313cf75c | 22 | source ${TESTDIR}/test_common |
89d85f9a | 23 | |
e46a2b66 SB |
24 | trap "cleanup" SIGTERM EXIT |
25 | ||
26 | function cleanup() | |
27 | { | |
28 | rm -rf $TPMDIR | |
9fb43c7a | 29 | if [ -n "$PID" ]; then |
47c7ea77 | 30 | kill_quiet -SIGTERM $PID 2>/dev/null |
9fb43c7a | 31 | fi |
e46a2b66 SB |
32 | } |
33 | ||
34 | PORT=11234 | |
35 | ||
36 | export TCSD_TCP_DEVICE_HOSTNAME=localhost | |
37 | export TCSD_TCP_DEVICE_PORT=$PORT | |
38 | export TCSD_USE_TCP_DEVICE=1 | |
39 | ||
89d85f9a | 40 | # Test 1: test port and directory command line parameters; use log level 20 |
f487473c | 41 | FILEMODE=641 |
e533180b | 42 | exec 100<>$LOG_FILE |
e46a2b66 | 43 | |
89d85f9a SB |
44 | $SWTPM_EXE socket \ |
45 | -p $PORT \ | |
f487473c | 46 | --tpmstate dir=$TPMDIR,mode=$FILEMODE \ |
89d85f9a | 47 | --pid file=$PID_FILE \ |
e533180b | 48 | --log fd=100,level=20 \ |
930c7ba1 SB |
49 | --flags not-need-init \ |
50 | ${SWTPM_TEST_SECCOMP_OPT} & | |
e46a2b66 | 51 | PID=$! |
e533180b | 52 | exec 100>&- |
e46a2b66 | 53 | |
c5748a53 SB |
54 | if wait_port_open $PORT $PID 4; then |
55 | echo "Test 1 failed: TPM did not open port $PORT" | |
56 | exit 1 | |
57 | fi | |
e46a2b66 | 58 | |
47c7ea77 | 59 | kill_quiet -0 $PID |
e46a2b66 SB |
60 | if [ $? -ne 0 ]; then |
61 | echo "Test 1 failed: TPM process not running" | |
62 | exit 1 | |
63 | fi | |
64 | ||
f59c3300 SB |
65 | if wait_for_file $PID_FILE 3; then |
66 | echo "Error: ${SWTPM_INTERFACE} TPM did not write pidfile." | |
b2151737 SB |
67 | exit 1 |
68 | fi | |
69 | ||
f59c3300 | 70 | validate_pidfile $PID $PID_FILE |
b2151737 | 71 | |
313cf75c | 72 | ${SWTPM_BIOS} &>/dev/null |
e46a2b66 | 73 | if [ $? -ne 0 ]; then |
b82eb7e7 | 74 | echo "Test 1 failed: ${SWTPM_BIOS} did not work" |
e46a2b66 SB |
75 | exit 1 |
76 | fi | |
77 | ||
f487473c SB |
78 | filemode=$(get_filemode ${TPMDIR}/tpm-00.permall) |
79 | if [ "$filemode" != "$FILEMODE" ]; then | |
80 | echo "Filemode bits are wrong" | |
81 | echo "Expected: $FILEMODE" | |
82 | echo "Actual : $filemode" | |
83 | exit 1 | |
84 | fi | |
85 | ||
89d85f9a SB |
86 | check_logfile_patterns_level_20 $LOG_FILE |
87 | rm -f $LOG_FILE | |
88 | ||
47c7ea77 | 89 | kill_quiet -SIGTERM $PID &>/dev/null |
bfdad297 | 90 | wait_process_gone $PID 2 |
e46a2b66 SB |
91 | |
92 | exec 20<&1-; exec 21<&2- | |
47c7ea77 | 93 | kill_quiet -0 $PID &>/dev/null |
e46a2b66 SB |
94 | RES=$? |
95 | exec 1<&20-; exec 2<&21- | |
96 | ||
97 | if [ $RES -eq 0 ]; then | |
47c7ea77 | 98 | kill_quiet -SIGKILL $PID |
e46a2b66 SB |
99 | echo "Test 1 failed: TPM process did not terminate on SIGTERM" |
100 | exit 1 | |
101 | fi | |
102 | ||
103 | echo "Test 1 passed" | |
104 | cleanup | |
105 | ||
53b55e71 SB |
106 | # Test 2: test port, directory and terminate command line parameters (-t) |
107 | # that causes the swtpm process to exit upon connection close | |
cce7503c | 108 | TPMDIR="$(mktemp -d)" || exit 1 |
e46a2b66 | 109 | |
930c7ba1 SB |
110 | $SWTPM_EXE socket \ |
111 | --flags not-need-init \ | |
112 | -p $PORT \ | |
113 | --tpmstate dir=$TPMDIR \ | |
114 | -t \ | |
115 | ${SWTPM_TEST_SECCOMP_OPT} &>/dev/null & | |
e46a2b66 SB |
116 | PID=$! |
117 | ||
c5748a53 SB |
118 | if wait_port_open $PORT $PID 4; then |
119 | echo "Test 1 failed: TPM did not open port $PORT" | |
120 | exit | |
121 | fi | |
e46a2b66 SB |
122 | |
123 | exec 20<&1-; exec 21<&2- | |
47c7ea77 | 124 | kill_quiet -0 $PID |
53b55e71 | 125 | RES=$? |
e46a2b66 SB |
126 | exec 1<&20-; exec 2<&21- |
127 | ||
53b55e71 | 128 | if [ $RES -ne 0 ]; then |
e46a2b66 SB |
129 | echo "Test 2 failed: TPM process not running" |
130 | exit 1 | |
131 | fi | |
132 | ||
420a18f1 | 133 | exec 100<>/dev/tcp/localhost/$PORT |
e46a2b66 SB |
134 | if [ $? -ne 0 ]; then |
135 | echo "Test 2 failed: Could not connect to TPM" | |
136 | exit 1 | |
137 | fi | |
138 | ||
420a18f1 | 139 | exec 100>&- |
e46a2b66 | 140 | |
99f442f3 | 141 | if wait_port_closed $PORT $PID 8; then |
c5748a53 SB |
142 | echo "Test 2 failed: TPM did not close port" |
143 | exit 1 | |
144 | fi | |
145 | ||
146 | if wait_process_gone $PID 4; then | |
147 | echo "Test 2 failed: TPM process did not shut down" | |
148 | exit 1 | |
149 | fi | |
e46a2b66 SB |
150 | |
151 | exec 20<&1-; exec 21<&2- | |
47c7ea77 | 152 | kill_quiet -0 $PID |
e46a2b66 SB |
153 | RES=$? |
154 | exec 1<&20-; exec 2<&21- | |
155 | ||
156 | if [ $RES -eq 0 ]; then | |
47c7ea77 | 157 | kill_quiet -SIGKILL $PID |
e46a2b66 SB |
158 | echo "Test 2 failed: TPM process did not terminate on connection loss" |
159 | exit 1 | |
160 | fi | |
161 | ||
162 | echo "Test 2 passed" | |
53b55e71 | 163 | |
93edca48 AV |
164 | # Test 3: test --fd= and --ctrl type=unxio,clientfd= |
165 | # The python script execs swtpm with client sockets | |
166 | exec 20<&1-; exec 21<&2- | |
313cf75c | 167 | LOG=$(PID_FILE=$TPMDIR/swtpm.pid SWTPM_EXE=$SWTPM_EXE TPMDIR=$TPMDIR exec $TESTDIR/test_clientfds.py) |
93edca48 AV |
168 | RES=$? |
169 | exec 1<&20-; exec 2<&21- | |
170 | ||
171 | if [ $RES -ne 0 ]; then | |
172 | echo "Test 3 failed: $LOG" | |
173 | exit 1 | |
174 | fi | |
175 | ||
176 | echo "Test 3 passed" | |
b82eb7e7 ET |
177 | cleanup |
178 | ||
179 | # Test 4: --tpmstate backend-uri=dir:// parameter test | |
cce7503c | 180 | TPMDIR="$(mktemp -d)" || exit 1 |
b82eb7e7 ET |
181 | PID_FILE=$TPMDIR/${SWTPM}.pid |
182 | FILEMODE=641 | |
183 | ||
184 | $SWTPM_EXE socket \ | |
185 | -p $PORT \ | |
186 | --tpmstate backend-uri=dir://$TPMDIR,mode=$FILEMODE \ | |
187 | --pid file=$PID_FILE \ | |
188 | --flags not-need-init \ | |
189 | ${SWTPM_TEST_SECCOMP_OPT} & | |
190 | PID=$! | |
191 | ||
192 | if wait_port_open $PORT $PID 4; then | |
193 | echo "Test 4 failed: TPM did not open port $PORT" | |
194 | exit 1 | |
195 | fi | |
196 | ||
197 | kill_quiet -0 $PID | |
198 | if [ $? -ne 0 ]; then | |
199 | echo "Test 4 failed: TPM process not running" | |
200 | exit 1 | |
201 | fi | |
202 | ||
203 | if wait_for_file $PID_FILE 3; then | |
204 | echo "Error: ${SWTPM_INTERFACE} TPM did not write pidfile." | |
205 | exit 1 | |
206 | fi | |
207 | ||
208 | validate_pidfile $PID $PID_FILE | |
209 | ||
210 | ${SWTPM_BIOS} &>/dev/null | |
211 | if [ $? -ne 0 ]; then | |
212 | echo "Test 4 failed: ${SWTPM_BIOS} did not work" | |
213 | exit 1 | |
214 | fi | |
215 | ||
216 | filemode=$(get_filemode ${TPMDIR}/tpm-00.permall) | |
217 | if [ "$filemode" != "$FILEMODE" ]; then | |
218 | echo "Filemode bits are wrong" | |
219 | echo "Expected: $FILEMODE" | |
220 | echo "Actual : $filemode" | |
221 | exit 1 | |
222 | fi | |
223 | ||
224 | kill_quiet -SIGTERM $PID &>/dev/null | |
225 | wait_process_gone $PID 2 | |
226 | ||
227 | exec 20<&1-; exec 21<&2- | |
228 | kill_quiet -0 $PID &>/dev/null | |
229 | RES=$? | |
230 | exec 1<&20-; exec 2<&21- | |
231 | ||
232 | if [ $RES -eq 0 ]; then | |
233 | kill_quiet -SIGKILL $PID | |
234 | echo "Test 4 failed: TPM process did not terminate on SIGTERM" | |
235 | exit 1 | |
236 | fi | |
237 | ||
238 | echo "Test 4 passed" | |
239 | cleanup | |
93edca48 | 240 | |
53b55e71 | 241 | exit 0 |