Debian Codegraph @

tests/common

raw
1
2 # shellcheck shell=bash
3
4 SWTPM_TEST_UNINSTALLED=1
5
6 if [ -n "${SWTPM_TEST_UNINSTALLED}" ]; then
7 SWTPM_EXE=${SWTPM_EXE:-${ROOT}/src/swtpm/swtpm}
8 SWTPM_IOCTL=${SWTPM_IOCTL:-${ROOT}/src/swtpm_ioctl/swtpm_ioctl}
9 SWTPM_BIOS=${SWTPM_BIOS:-${ROOT}/src/swtpm_bios/swtpm_bios}
10 SWTPM_SETUP=${SWTPM_SETUP:-${ROOT}/src/swtpm_setup/swtpm_setup}
11 SWTPM_CERT=${SWTPM_CERT:-${ROOT}/src/swtpm_cert/swtpm_cert}
12 SWTPM_LOCALCA=${SWTPM_LOCALCA:-${ROOT}/src/swtpm_localca/swtpm_localca}
13 SWTPM_SETUP_CONF="${ROOT}/samples/swtpm_setup.conf"
14 else
15 SWTPM_EXE=${SWTPM_EXE:-$(type -P swtpm)}
16 SWTPM_IOCTL=${SWTPM_IOCTL:-$(type -P swtpm_ioctl)}
17 SWTPM_BIOS=${SWTPM_BIOS:-$(type -P swtpm_bios)}
18 SWTPM_SETUP=${SWTPM_SETUP:-$(type -P swtpm_setup)}
19 SWTPM_CERT=${SWTPM_CERT:-$(type -P swtpm_cert)}
20 SWTPM_LOCALCA=${SWTPM_LOCALCA:-$(type -P swtpm_localca)}
21 SWTPM_SETUP_CONF="$(dirname "$0")/swtpm_setup.conf"
22 fi
23
24 # SC2034 unused
25 export SWTPM_SETUP_CONF
26
27 ECHO=$(type -P echo)
28
29 case "$(uname -s)" in
30 Darwin)
31 CERTTOOL=gnutls-certtool;;
32 *)
33 CERTTOOL=certtool;;
34 esac
35 export CERTTOOL
36
37 # Note: Do not use file descriptors above 127 due to OpenBSD.
38
39 # Kill a process quietly
40 # @1: signal, e.g. -9
41 # @2: pid
42 function kill_quiet()
43 {
44 local sig="$1"
45 local pid="$2"
46
47 bash -c "kill $sig $pid &>/dev/null"
48 return $?
49 }
50
51 # Wait for a regular file to appear and for it to have > 0 bytes
52 #
53 # @1: filename
54 # @2: timeout in seconds
55 function wait_for_file()
56 {
57 local filename="$1"
58 local timeout="$2"
59
60 local loops=$((timeout * 10)) loop
61
62 for ((loop=0; loop<loops; loop++)); do
63 [ -f "${filename}" ] && [ "$(get_filesize "${filename}")" != 0 ] && {
64 return 1
65 }
66 sleep 0.1
67 done
68 return 0
69 }
70
71 # Wait for a regular file to disappear
72 #
73 # @1: filename
74 # @2: timeout in seconds
75 function wait_file_gone()
76 {
77 local filename="$1"
78 local timeout="$2"
79
80 local loops=$((timeout * 10)) loop
81
82 for ((loop=0; loop<loops; loop++)); do
83 [ -f "${filename}" ] || return 1
84 sleep 0.1
85 done
86 return 0
87 }
88
89 # Wait for a process with given PID to be gone
90 #
91 # @1: pid
92 # @2: timeout in seconds
93 function wait_process_gone()
94 {
95 local pid="$1"
96 local timeout="$2"
97
98 local loops=$((timeout * 10)) loop
99
100 for ((loop=0; loop<loops; loop++)); do
101 kill_quiet -0 "${pid}" || return 1
102 sleep 0.1
103 done
104 return 0
105 }
106
107 # Wait for a chardev to appear
108 #
109 # @1: filename
110 # @2: timeout in seconds
111 function wait_for_chardev()
112 {
113 local filename="$1"
114 local timeout="$2"
115
116 local loops=$((timeout * 10)) loop
117
118 for ((loop=0; loop<loops; loop++)); do
119 [ -c "${filename}" ] && return 1
120 sleep 0.1
121 done
122 return 0
123 }
124
125 # Wait for a chardev to disappear
126 #
127 # @1: filename
128 # @2: timeout in seconds
129 function wait_chardev_gone()
130 {
131 local filename="$1"
132 local timeout="$2"
133
134 local loops=$((timeout * 10)) loop
135
136 for ((loop=0; loop<loops; loop++)); do
137 [ -c "${filename}" ] || return 1
138 sleep 0.1
139 done
140 return 0
141 }
142
143 # Wait for a socket file to appear
144 #
145 # @1: filename
146 # @2: timeout in seconds
147 function wait_for_socketfile()
148 {
149 local filename="$1"
150 local timeout="$2"
151
152 local loops=$((timeout * 10)) loop
153
154 for ((loop=0; loop<loops; loop++)); do
155 [ -S "${filename}" ] && return 1
156 sleep 0.1
157 done
158 return 0
159 }
160
161 # Wait for a socket file to disappear
162 #
163 # @1: filename
164 # @2: timeout in seconds
165 function wait_socketfile_gone()
166 {
167 local filename="$1"
168 local timeout="$2"
169
170 local loops=$((timeout * 10)) loop
171
172 for ((loop=0; loop<loops; loop++)); do
173 [ -S "${filename}" ] || return 1
174 sleep 0.1
175 done
176 return 0
177 }
178
179 # Wait for a server socket to appear
180 #
181 # @1: port
182 # @2: host
183 # @3: timeout in seconds
184 function wait_for_serversocket()
185 {
186 local port="$1"
187 local host="$2"
188 local timeout="$3"
189
190 local loops=$((timeout * 10)) loop
191
192 for ((loop=0; loop<loops; loop++)); do
193 if (exec 127<>"/dev/tcp/${host}/${port}") &>/dev/null; then
194 return 1
195 fi
196 sleep 0.1
197 done
198 return 0
199 }
200
201 # Wait for a server socket to disappear
202 #
203 # @1: port
204 # @2: host
205 # @3: timeout in seconds
206 function wait_serversocket_gone()
207 {
208 local port="$1"
209 local host="$2"
210 local timeout="$3"
211
212 local loops=$((timeout * 10)) loop
213
214 for ((loop=0; loop<loops; loop++)); do
215 if ! (exec 127<>"/dev/tcp/${host}/${port}") &>/dev/null; then
216 return 1
217 fi
218 sleep 0.1
219 done
220 return 0
221 }
222
223 # Wait for a TCP port to open for listening
224 # @1: port
225 # @2: id of process to open port
226 # @3: timeout in seconds
227 function wait_port_open()
228 {
229 local port=$1
230 local pid=$2
231 local timeout=$3
232
233 local loops=$((timeout * 10)) loop
234 local NETSTAT
235
236 NETSTAT=$(type -P netstat)
237
238 for ((loop = 0; loop < loops; loop++)); do
239 if [ -n "$NETSTAT" ]; then
240 if netstat -naptl 2>/dev/null |
241 grep "LISTEN" |
242 grep " $pid/" |
243 grep -q ":$port "; then
244 return 1
245 fi
246 else
247 if ss -nptl | \
248 grep ",pid=${pid}," | \
249 grep -q ":$port "; then
250 return 1
251 fi
252 fi
253 sleep 0.1
254 done
255 return 0
256 }
257
258 # Wait for a TCP listening port to close
259 # @1: port
260 # @2: id of process to close port
261 # @3: timeout in seconds
262 function wait_port_closed()
263 {
264 local port=$1
265 local pid=$2
266 local timeout=$3
267
268 local loops=$((timeout * 10)) loop NETSTAT
269
270 NETSTAT=$(type -P netstat)
271
272 for ((loop = 0; loop < loops; loop++)); do
273 if [ -n "$NETSTAT" ]; then
274 if ! netstat -naptl 2>/dev/null | \
275 grep "LISTEN" | \
276 grep " $pid/" | \
277 grep -q ":$port "; then
278 return 1
279 fi
280 else
281 if ! ss -nptl | \
282 grep ",pid=${pid}," | \
283 grep -q ":$port "; then
284 return 1
285 fi
286 fi
287 sleep 0.1
288 done
289 return 0
290 }
291
292 # Run the swtpm_ioctl command
293 #
294 # @param1: type of interface
295 function run_swtpm_ioctl()
296 {
297 local iface=$1; shift
298
299 case "${iface}" in
300 cuse)
301 [ -z "${SWTPM_DEV_NAME}" ] && {
302 echo "SWTPM_DEV_NAME not defined"
303 exit 1
304 }
305 ${SWTPM_IOCTL} "$@" "${SWTPM_DEV_NAME}"
306 return $?
307 ;;
308 socket+socket|unix+socket)
309 [ -z "${SWTPM_SERVER_NAME}" ] && {
310 echo "SWTPM_SERVER_NAME not defined"
311 exit 1
312 }
313 [ -z "${SWTPM_SERVER_PORT}" ] && {
314 echo "SWTPM_SERVER_PORT not defined"
315 exit 1
316 }
317 ${SWTPM_IOCTL} \
318 --tcp "${SWTPM_SERVER_NAME}:${SWTPM_CTRL_PORT}" \
319 "$@"
320 return $?
321 ;;
322 socket+unix|unix+unix)
323 [ -z "${SWTPM_CTRL_UNIX_PATH}" ] && {
324 echo "SWTPM_CTRL_UNIX_PATH not defined"
325 exit 1
326 }
327 ${SWTPM_IOCTL} \
328 --unix "${SWTPM_CTRL_UNIX_PATH}" \
329 "$@"
330 return $?
331 ;;
332 esac
333 }
334
335 # Start the swtpm in the background
336 #
337 # @param1: type of interface
338 # @param2.. : parameters to pass to 'swtpm'
339 function run_swtpm()
340 {
341 local iface=$1; shift
342 local swtpm_server_disconnect=""
343
344 echo "==== Starting swtpm with interfaces ${iface} ===="
345 if [ -z "${SWTPM_SERVER_NO_DISCONNECT}" ]; then
346 swtpm_server_disconnect=",disconnect"
347 fi
348
349 case "${iface}" in
350 cuse)
351 [ -z "${SWTPM_DEV_NAME}" ] && {
352 echo "SWTPM_DEV_NAME not defined"
353 exit 1
354 }
355
356 if wait_chardev_gone "${SWTPM_DEV_NAME}" 2; then
357 echo "${SWTPM_DEV_NAME} is still there and may be used."
358 exit 1
359 fi
360
361 ${SWTPM_EXE} cuse "$@" \
362 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} \
363 -n "${SWTPM_DEV_NAME##*/}"
364 rc=$?
365 if [ $rc -ne 0 ]; then
366 echo "Could not run ${SWTPM_EXE} using ${iface}"
367 exit 1
368 fi
369 if wait_for_chardev "${SWTPM_DEV_NAME}" 2; then
370 echo "$SWTPM_DEV_NAME did not appear"
371 exit 1
372 fi
373
374 SWTPM_PID=$(ps aux |
375 grep "cuse" |
376 grep -E " ${SWTPM_DEV_NAME##*/}\$" |
377 grep -v grep |
378 gawk '{print $2}')
379 return $?
380 ;;
381 socket+socket)
382 [ -z "${SWTPM_SERVER_PORT}" ] && {
383 echo "SWTPM_SERVER_PORT not defined"
384 exit 1
385 }
386 [ -z "${SWTPM_CTRL_PORT}" ] && {
387 echo "SWTPM_CTRL_PORT not defined"
388 exit 1
389 }
390
391 if wait_serversocket_gone "${SWTPM_SERVER_PORT}" 127.0.0.1 2; then
392 echo "Port ${SWTPM_SERVER_PORT} is still used"
393 exit 1
394 fi
395 if wait_serversocket_gone "${SWTPM_CTRL_PORT}" 127.0.0.1 1; then
396 echo "Port ${SWTPM_CTRL_PORT} is still used"
397 exit 1
398 fi
399
400 ${SWTPM_EXE} socket "$@" \
401 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} \
402 --server "type=tcp,port=${SWTPM_SERVER_PORT}${swtpm_server_disconnect}" \
403 --ctrl "type=tcp,port=${SWTPM_CTRL_PORT}" &
404 rc=$?
405 if [ $rc -ne 0 ]; then
406 echo "Could not run ${SWTPM_EXE} using ${iface}"
407 exit 1
408 fi
409 SWTPM_PID=$!
410 if wait_for_serversocket "${SWTPM_SERVER_PORT}" 127.0.0.1 2; then
411 echo "Server did not open port ${SWTPM_SERVER_PORT}"
412 kill -9 "${SWTPM_PID}"
413 exit 1
414 fi
415 if wait_for_serversocket "${SWTPM_CTRL_PORT}" 127.0.0.1 1; then
416 echo "Server did not open port ${SWTPM_CTRL_PORT}"
417 kill -9 "${SWTPM_PID}"
418 exit 1
419 fi
420 return 0
421 ;;
422 socket+unix)
423 [ -z "${SWTPM_SERVER_PORT}" ] && {
424 echo "SWTPM_SERVER_PORT not defined"
425 exit 1
426 }
427 [ -z "${SWTPM_CTRL_UNIX_PATH}" ] && {
428 echo "SWTPM_CTRL_UNIX_PATH not defined"
429 exit 1
430 }
431
432 if wait_serversocket_gone "${SWTPM_SERVER_PORT}" 127.0.0.1 2; then
433 echo "Port ${SWTPM_SERVER_PORT} is still used"
434 exit 1
435 fi
436 if wait_socketfile_gone "${SWTPM_CTRL_UNIX_PATH}" 2; then
437 echo "Unix socket ${SWTPM_CTRL_UNIX_PATH} is still there"
438 exit 1
439 fi
440
441 ${SWTPM_EXE} socket "$@" \
442 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} \
443 --server "type=tcp,port=${SWTPM_SERVER_PORT}${swtpm_server_disconnect}" \
444 --ctrl "type=unixio,path=${SWTPM_CTRL_UNIX_PATH}" &
445 rc=$?
446 if [ $rc -ne 0 ]; then
447 echo "Could not run ${SWTPM_EXE} using ${iface}"
448 exit 1
449 fi
450 [ $rc -ne 0 ] && return $rc
451 SWTPM_PID=$!
452 if wait_for_serversocket "${SWTPM_SERVER_PORT}" 127.0.0.1 2; then
453 echo "Server did not open port ${SWTPM_SERVER_PORT}"
454 kill -9 "${SWTPM_PID}"
455 exit 1
456 fi
457 if wait_for_socketfile "${SWTPM_CTRL_UNIX_PATH}" 1; then
458 echo "Server did not create UnixIO socket ${SWTPM_CTRL_UNIX_PATH}"
459 kill -9 "${SWTPM_PID}"
460 exit 1
461 fi
462 return 0
463 ;;
464 unix+socket)
465 [ -z "${SWTPM_CMD_UNIX_PATH}" ] && {
466 echo "SWTPM_CMD_UNIX_PATH not defined"
467 exit 1
468 }
469 [ -z "${SWTPM_CTRL_PORT}" ] && {
470 echo "SWTPM_CTRL_PORT not defined"
471 exit 1
472 }
473
474 if wait_socketfile_gone "${SWTPM_CMD_UNIX_PATH}" 2; then
475 echo "Unix socket ${SWTPM_CMD_UNIX_PATH} is still there"
476 exit 1
477 fi
478 if wait_serversocket_gone "${SWTPM_CTRL_PORT}" 127.0.0.1 1; then
479 echo "Port ${SWTPM_CTRL_PORT} is still used"
480 exit 1
481 fi
482
483 ${SWTPM_EXE} socket "$@" \
484 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} \
485 --server "type=unixio,path=${SWTPM_CMD_UNIX_PATH}" \
486 --ctrl "type=tcp,port=${SWTPM_CTRL_PORT}" &
487 rc=$?
488 if [ $rc -ne 0 ]; then
489 echo "Could not run ${SWTPM_EXE} using ${iface}"
490 exit 1
491 fi
492 SWTPM_PID=$!
493 if wait_for_socketfile "${SWTPM_CMD_UNIX_PATH}" 2; then
494 echo "Server did not create UnixIO socket ${SWTPM_CMD_UNIX_PATH}"
495 kill -9 "${SWTPM_PID}"
496 exit 1
497 fi
498 if wait_for_serversocket "${SWTPM_CTRL_PORT}" 127.0.0.1 1; then
499 echo "Server did not open port ${SWTPM_CTRL_PORT}"
500 kill -9 "${SWTPM_PID}"
501 exit 1
502 fi
503 return 0
504 ;;
505 unix+unix)
506 [ -z "${SWTPM_CMD_UNIX_PATH}" ] && {
507 echo "SWTPM_CMD_UNIX_PATH not defined"
508 exit 1
509 }
510 [ -z "${SWTPM_CTRL_UNIX_PATH}" ] && {
511 echo "SWTPM_CTRL_UNIX_PATH not defined"
512 exit 1
513 }
514
515 if wait_socketfile_gone "${SWTPM_CMD_UNIX_PATH}" 2; then
516 echo "Unix socket ${SWTPM_CMD_UNIX_PATH} is still there"
517 exit 1
518 fi
519 if wait_socketfile_gone "${SWTPM_CTRL_UNIX_PATH}" 2; then
520 echo "Unix socket ${SWTPM_CTRL_UNIX_PATH} is still there"
521 exit 1
522 fi
523
524 ${SWTPM_EXE} socket "$@" \
525 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} \
526 --server "type=unixio,path=${SWTPM_CMD_UNIX_PATH}" \
527 --ctrl "type=unixio,path=${SWTPM_CTRL_UNIX_PATH}" &
528 rc=$?
529 if [ $rc -ne 0 ]; then
530 echo "Could not run ${SWTPM_EXE} using ${iface}"
531 exit 1
532 fi
533 SWTPM_PID=$!
534 if wait_for_socketfile "${SWTPM_CMD_UNIX_PATH}" 2; then
535 echo "Server did not create UnixIO socket ${SWTPM_CMD_UNIX_PATH}"
536 kill -9 "${SWTPM_PID}"
537 exit 1
538 fi
539 if wait_for_socketfile "${SWTPM_CTRL_UNIX_PATH}" 1; then
540 echo "Server did not create UnixIO socket ${SWTPM_CTRL_UNIX_PATH}"
541 kill -9 "${SWTPM_PID}"
542 exit 1
543 fi
544 return 0
545 ;;
546 esac
547 }
548
549 # Open the command channel/device on fd 100
550 #
551 # @param1: type of interface
552 # @param2: must be '100'
553 function swtpm_open_cmddev()
554 {
555 local iface=$1; shift
556
557 [ "$1" != "100" ] && {
558 echo "swtpm_opendev: Filedescriptor must be 100"
559 exit 1
560 }
561
562 case "${iface}" in
563 cuse)
564 [ -z "${SWTPM_DEV_NAME}" ] && {
565 echo "SWTPM_DEV_NAME not defined"
566 exit 1
567 }
568 exec 100>&-
569 exec 100<>"${SWTPM_DEV_NAME}"
570 return $?
571 ;;
572 socket+socket|socket+unix)
573 [ -z "${SWTPM_SERVER_NAME}" ] && {
574 echo "SWTPM_SERVER_NAME not defined"
575 exit 1
576 }
577 [ -z "${SWTPM_SERVER_PORT}" ] && {
578 echo "SWTPM_SERVER_PORT not defined"
579 exit 1
580 }
581 # Must first close on OS/X
582 exec 100>&-
583 exec 100<>"/dev/tcp/${SWTPM_SERVER_NAME}/${SWTPM_SERVER_PORT}"
584 return $?
585 ;;
586 unix+socket|unix+unix)
587 ;;
588 *)
589 echo "swtpm_opendev: unsupported interface $iface"
590 exit 1
591 esac
592 }
593
594 # Transmit a command on fd 100
595 #
596 # @param1: type of interface
597 function swtpm_cmd_tx()
598 {
599 local iface=$1
600 local cmd_path
601
602 cmd_path=$(mktemp)
603
604 swtpm_open_cmddev "${iface}" 100
605
606 case "${iface}" in
607 cuse)
608 echo -en "$2" > "${cmd_path}"
609 cat "${cmd_path}" >&100
610 cat <&100 | \
611 od -t x1 -A n | \
612 tr -s ' ' | \
613 tr -d '\n' | \
614 sed 's/ $//g'
615 ;;
616 socket+socket|socket+unix)
617 echo -en "$2" > "${cmd_path}"
618 cat "${cmd_path}" >&100
619 cat <&100 | od -t x1 -A n | \
620 tr -s ' ' | \
621 tr -d '\n' | \
622 sed 's/ $//g'
623 ;;
624 unix+socket|unix+unix)
625 echo -en "$2" > "${cmd_path}"
626 socat -x -t50 \
627 "FILE:${cmd_path},rdonly" \
628 "UNIX-CLIENT:${SWTPM_CMD_UNIX_PATH}" 2>&1 | \
629 sed -n '/^ /p' | \
630 tail -n1
631 ;;
632 *)
633 echo "swtpm_opendev: unsupported interface $iface"
634 rm -f "${cmd_path}"
635 exit 1
636 esac
637
638 rm -f "${cmd_path}"
639 }
640
641 # Transmit a control command on fd 101
642 #
643 # @param1: type of interface
644 function swtpm_ctrl_tx()
645 {
646 local iface=$1
647
648 local ctrl_path
649
650 case "${iface}" in
651 socket+socket|unix+socket)
652 $ECHO -en "$2" >&101
653 cat <&101 | od -t x1 -A n -w128
654 ;;
655 socket+unix|unix+unix)
656 ctrl_path=$(mktemp)
657 echo -en "$2" > "${ctrl_path}"
658 socat -x -t50 \
659 "FILE:${ctrl_path},rdonly" \
660 "UNIX-CLIENT:${SWTPM_CTRL_UNIX_PATH}" 2>&1 | \
661 sed -n '/^ /p' | \
662 tail -n1
663 rm -f "${ctrl_path}"
664 ;;
665 *)
666 echo "swtpm_opendev: unsupported interface $iface"
667 exit 1
668 esac
669 }
670
671
672 # Run swtpm_bios
673 #
674 # @param1: type of interface
675 # @param2 ...: parameters to pass to swtpm_bios
676 function run_swtpm_bios()
677 {
678 local iface=$1
679
680 shift
681
682 case "${iface}" in
683 cuse)
684 [ -z "${SWTPM_DEV_NAME}" ] && {
685 echo "SWTPM_DEV_NAME not defined"
686 exit 1
687 }
688 ${SWTPM_BIOS} --tpm-device "${SWTPM_DEV_NAME}" "$@"
689 return $?
690 ;;
691 unix+unix|unix+socket)
692 [ -z "${SWTPM_CMD_UNIX_PATH}" ] && {
693 echo "SWTPM_CMD_UNIX_PATH not defined"
694 exit 1
695 }
696 ${SWTPM_BIOS} --unix "${SWTPM_CMD_UNIX_PATH}" "$@"
697 return $?
698 ;;
699 socket+unix|socket+socket)
700 [ -z "${SWTPM_SERVER_PORT}" ] && {
701 echo "SWTPM_SERVER_PORT not defined"
702 exit 1
703 }
704 ${SWTPM_BIOS} --tcp "${SWTPM_SERVER_NAME}:${SWTPM_SERVER_PORT}" "$@"
705 return $?
706 ;;
707 *)
708 echo "run_swtpm_bios: unsupported interface $iface"
709 exit 1
710 esac
711 }
712
713 # Get the size of a file in bytes
714 #
715 # @1: filename
716 function get_filesize()
717 {
718 if [[ "$(uname -s)" =~ (Linux|CYGWIN_NT-) ]]; then
719 stat -c%s "$1"
720 else
721 # OpenBSD
722 stat -f%z "$1"
723 fi
724 }
725
726 # Get the file mode bits in octal format
727 #
728 # @1: filename
729 function get_filemode()
730 {
731 if [[ "$(uname -s)" =~ (Linux|CYGWIN_NT-) ]]; then
732 stat -c%a "$1"
733 else
734 # BSDs
735 stat -f%Lp "$1"
736 fi
737 }
738
739 # Get the file owner uid and gid
740 #
741 # @1: filename
742 function get_fileowner()
743 {
744 if [[ "$(uname -s)" =~ (Linux|CYGWIN_NT-) ]]; then
745 stat -c"%u %g" "$1"
746 else
747 # BSDs
748 stat -f"%u %g" "$1"
749 fi
750 }
751
752 # Get the file owner user name and group name
753 #
754 # @1: filename
755 function get_fileowner_names()
756 {
757 if [[ "$(uname -s)" =~ (Linux|CYGWIN_NT-) ]]; then
758 stat -c"%U %G" "$1"
759 else
760 # BSDs
761 stat -f"%Su %Sg" "$1"
762 fi
763 }
764
765 # Get the SHA1 of a file
766 #
767 # @1: filename
768 function get_sha1_file()
769 {
770 if ! [ -r "$1" ]; then
771 echo "[file $1 does not exist]"
772 return
773 fi
774 case "$(uname -s)" in
775 Linux|CYGWIN*)
776 sha1sum "$1" | cut -f1 -d" "
777 ;;
778 Darwin)
779 shasum "$1" | cut -f1 -d" "
780 ;;
781 *)
782 # OpenBSD
783 sha1 "$1" | cut -d "=" -f2 | tr -d " "
784 esac
785 }
786
787 # Display process that have the same name
788 #
789 # @1: process name to match
790 function display_processes_by_name()
791 {
792 local name="$1"
793
794 if false; then
795 ps aux | grep "${name}" | grep -v grep
796 fi
797 }
798
799 # Check whether seccomp support is compiled in
800 #
801 # @1: path to swtpm
802 #
803 # Returns 0 if seccomp is supported, 1 otherwise
804 function has_seccomp_support()
805 {
806 local swtpm_exe="$1"
807
808 local tmp
809
810 tmp=$(${swtpm_exe} socket --help | grep -E "\--seccomp")
811 [ -n "${tmp}" ] && return 0
812 return 1
813 }
814
815 # Check whether the given process runs with the given seccomp
816 # profile type IF the given swtpm executable has seccomp support
817 #
818 # @1: Path to swtpm executable from which process was started
819 # @2: The process ID
820 # @3: The expected seccomp profile type
821 function check_seccomp_profile()
822 {
823 local swtpm_exe="$1"
824 local swtpm_pid="$2"
825 local profile="$3"
826
827 local tmp
828
829 if ! has_seccomp_support "${swtpm_exe}"; then
830 return 0
831 fi
832 if [ -n "${SWTPM_TEST_SECCOMP_OPT}" ]; then
833 return 0
834 fi
835
836 tmp=$(grep -E "^Seccomp" /proc/self/status |
837 cut -d":" -f2 |
838 tr -d '\t')
839 if [ "${tmp}" != "0" ]; then
840 echo "check_seccomp_profile: skipping check since test env." \
841 "runs with in a seccomp profile overriding --seccomp"
842 return 0
843 fi
844
845 tmp=$(grep -E "^Seccomp" "/proc/${swtpm_pid}/status" |
846 cut -d":" -f2 |
847 tr -d '\t')
848 if [ "${tmp}" != "${profile}" ]; then
849 echo "Process ${swtpm_pid} has wrong seccomp profile type"
850 echo "Expected: ${profile}"
851 echo "Actual : ${tmp}"
852 return 1
853 fi
854 return 0
855 }
856
857 # Validate the content of the pid file
858 # @1: Expected PID
859 # @2: pid file filename
860 function validate_pidfile()
861 {
862 local pid="$1"
863 local pidfile="$2"
864
865 local rpid
866
867 rpid="$(cat "$pidfile")"
868
869 if [ -z "$rpid" ]; then
870 sleep 0.1
871 rpid="$(cat "$pidfile")"
872 fi
873
874 if [ "$pid" != "$rpid" ]; then
875 echo "Error: pid file contains unexpected PID value."
876 echo "expected: $pid"
877 echo "actual : $(cat "${pidfile}")"
878 exit 1
879 fi
880 }
881
882 # Check whether swtpm can use a TPM 1.2
883 function skip_test_no_tpm12()
884 {
885 local swtpm_exe="$1"
886
887 local res
888
889 res=$(${swtpm_exe} socket --print-capabilities | grep '"tpm-1.2"')
890 if [ -z "${res}" ]; then
891 echo "${swtpm_exe} does not provide a TPM 1.2"
892 exit 77
893 fi
894 }
895
896 # Check whether swtpm can use a TPM 2.0
897 function skip_test_no_tpm20()
898 {
899 local swtpm_exe="$1"
900
901 local res
902
903 res=$(${swtpm_exe} socket --print-capabilities | grep '"tpm-2.0"')
904 if [ -z "${res}" ]; then
905 echo "${swtpm_exe} does not provide a TPM 2.0"
906 exit 77
907 fi
908 }
909
910 # Test whether swtpm has a chardev interface; Returns 0 if true, 1 otherwise
911 function test_swtpm_has_chardev()
912 {
913 local swtpm_exe="$1"
914
915 local res
916
917 res=$(${swtpm_exe} chardev --help 2>&1 |
918 grep "Unsupported TPM interface")
919 if [ -z "${res}" ]; then
920 return 0
921 fi
922 return 1
923 }
924
925 # Skip test if swtpm does not support chardev interface
926 function skip_test_no_chardev()
927 {
928 local swtpm_exe="$1"
929
930 if ! test_swtpm_has_chardev "${swtpm_exe}"; then
931 echo "${swtpm_exe} does not support chardev interface"
932 exit 77
933 fi
934 }
935
936 # Check whether swtpm links with ASAN
937 function skip_test_linked_with_asan()
938 {
939 local swtpm_exe="$1"
940
941 local act_exe
942
943 if [[ "$(uname -s)" =~ Linux ]]; then
944 if ! file "${swtpm_exe}" | grep -q ELF; then
945 act_exe="$(dirname "${swtpm_exe}")"/.libs/"$(basename "${swtpm_exe}")"
946 else
947 act_exe="${swtpm_exe}"
948 fi
949 if nm "${act_exe}" | grep -q "__asan_"; then
950 echo "${act_exe} is built with ASAN"
951 exit 77
952 fi
953 fi
954 }
955
956 # Have swtpm lock or re-lock the storage. Neither locking nor re-locking
957 # may produce an error
958 #
959 # @param1: reason like 'lock' or 're-lock'
960 function swtpm_lock_storage()
961 {
962 local reason="$1"
963
964 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --lock-storage 0; then
965 echo "Error: 'swtpm_ioctl --lock-storage' to ${reason} storage failed"
966 exit 1
967 fi
968 }
969
970 # Check that swtpm has no lock on the directory backend storage
971 function check_swtpm_no_storage_lock()
972 {
973 local pid="$1"
974
975 if [ -d "/proc/${pid}/fd" ]; then
976 # shellcheck disable=SC2010
977 if ls -l "/proc/${pid}/fd" | grep -q -E "\.lock\$"; then
978 echo "Error: swtpm must not have storage locked"
979 ls -l "/proc/${pid}/fd"
980 exit 1
981 fi
982 elif [ -n "$(type -P lsof)" ]; then
983 if lsof -p "${pid}" | grep -q -e "\.lock\$"; then
984 echo "Error: swtpm must not have storage locked"
985 lsof -p "${pid}"
986 exit 1
987 fi
988 else
989 echo "Missing procfs directory and lsof tool to determine open files."
990 exit 1
991 fi
992 }
993
994 # Check that swtpm has a lock on the directory backend storage
995 function check_swtpm_storage_locked()
996 {
997 local pid="$1"
998
999 if [ -d "/proc/${pid}/fd" ]; then
1000 # shellcheck disable=SC2010
1001 if ! ls -l "/proc/${pid}/fd" | grep -q -E "\.lock\$"; then
1002 echo "Error: swtpm must have storage locked"
1003 ls -l "/proc/${pid}/fd"
1004 exit 1
1005 fi
1006 elif [ -n "$(type -P lsof)" ]; then
1007 if ! lsof -p "${pid}" | grep -q -e "\.lock\$"; then
1008 echo "Error: swtpm must have storage locked"
1009 lsof -p "${pid}"
1010 exit 1
1011 fi
1012 else
1013 echo "Missing procfs directory and lsof tool to determine open files."
1014 exit 1
1015 fi
1016 }
1017
1018 # Get the name of the distro; only a few distros are supported for specific tests
1019 function get_distro_name()
1020 {
1021 if [ -r /etc/redhat-release ]; then
1022 if grep -qE "^Red Hat Enterprise" /etc/redhat-release; then
1023 echo "RHEL"
1024 elif grep -qE "^CentOS Stream release" /etc/redhat-release; then
1025 echo "CentOS"
1026 fi
1027 fi
1028 }
1029
1030 function get_rhel_version()
1031 {
1032 sed -n 's/Red Hat Enterprise Linux release \([^ ]*\) .*/\1/p' /etc/redhat-release |
1033 gawk '{split($0,a,"."); print a[1]*100 + a[2]}'
1034 }
1035
1036 function get_centos_version()
1037 {
1038 sed -n 's/CentOS Stream release \([0-9]*\).*/\1/p' /etc/redhat-release |
1039 gawk '{print $0*100}'
1040 }