]> git.proxmox.com Git - swtpm.git/commitdiff
tests: Move local functions to common file and handle errors better
authorStefan Berger <stefanb@linux.ibm.com>
Sun, 12 Jan 2020 22:41:56 +0000 (17:41 -0500)
committerStefan Berger <stefanb@us.ibm.com>
Mon, 13 Jan 2020 20:03:01 +0000 (15:03 -0500)
Move wait_port_open and wait_port_closed to common file and handle
the timeout errors in test_commandline.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
tests/common
tests/test_commandline

index aa5db42d0d90aefff16cfd3e74e7561d2f57d881..000f66aa7fd244ddeca27547a063bb64a1249ce6 100644 (file)
@@ -190,6 +190,54 @@ function wait_serversocket_gone()
   return 0
 }
 
+# Wait for a TCP port to open for listening
+# @1: port
+# @2: id of process to open port
+# @3: timeout in seconds
+function wait_port_open()
+{
+       local port=$1
+       local pid=$2
+       local timeout=$3
+
+       local loops=$((timeout * 10)) loop
+
+       for ((loop = 0; loop < loops; loop++)); do
+               if [ -n "$(netstat -naptl 2>/dev/null |
+                          grep "LISTEN" |
+                          grep " $pid/" |
+                          grep ":$port ")" ]; then
+                       return 1
+               fi
+               sleep 0.1
+       done
+       return 0
+}
+
+# Wait for a TCP listening port to close
+# @1: port
+# @2: id of process to close port
+# @3: timeout in seconds
+function wait_port_closed()
+{
+       local port=$1
+       local pid=$2
+       local timeout=$3
+
+       local loops=$((timeout * 10)) loop
+
+       for ((loop = 0; loop < loops; loop++)); do
+               if [ -z "$(netstat -naptl 2>/dev/null |
+                          grep "LISTEN" |
+                          grep " $pid/" |
+                          grep ":$port ")" ]; then
+                       return 1
+               fi
+               sleep 0.1
+       done
+       return 0
+}
+
 # Run the swtpm_ioctl command
 #
 # @param1: type of interface
index 23132c1595cbfa92d20a2b9c7e1e911db4a2004b..6024c7f9e80f6e8681d295787279f363b3e72102 100755 (executable)
@@ -30,41 +30,6 @@ export TCSD_TCP_DEVICE_HOSTNAME=localhost
 export TCSD_TCP_DEVICE_PORT=$PORT
 export TCSD_USE_TCP_DEVICE=1
 
-function wait_port_open()
-{
-       local port=$1
-       local pid=$2
-
-       sleep 0.2
-       for ((i = 0; i < 20; i++)); do
-               if [ -n "$(netstat -naptl 2>/dev/null |
-                          grep "LISTEN" |
-                          grep " $pid/" |
-                          grep ":$port ")" ]; then
-                       return 0
-               fi
-               sleep 0.2
-       done
-       return 1
-}
-
-function wait_port_closed()
-{
-       local port=$1
-       local pid=$2
-
-       for ((i = 0; i < 20; i++)); do
-               if [ -z "$(netstat -naptl 2>/dev/null |
-                          grep "LISTEN" |
-                          grep " $pid/" |
-                          grep ":$port ")" ]; then
-                       return 0
-               fi
-               sleep 0.2
-       done
-       return 1
-}
-
 # Test 1: test port and directory command line parameters; use log level 20
 FILEMODE=641
 exec 100<>$LOG_FILE
@@ -78,7 +43,10 @@ $SWTPM_EXE socket \
 PID=$!
 exec 100>&-
 
-wait_port_open $PORT $PID
+if wait_port_open $PORT $PID 4; then
+       echo "Test 1 failed: TPM did not open port $PORT"
+       exit 1
+fi
 
 kill_quiet -0 $PID
 if [ $? -ne 0 ]; then
@@ -134,7 +102,10 @@ TPMDIR=`mktemp -d`
 $SWTPM_EXE socket --flags not-need-init -p $PORT --tpmstate dir=$TPMDIR -t &>/dev/null &
 PID=$!
 
-wait_port_open $PORT $PID
+if wait_port_open $PORT $PID 4; then
+       echo "Test 1 failed: TPM did not open port $PORT"
+       exit
+fi
 
 exec 20<&1-; exec 21<&2-
 kill_quiet -0 $PID
@@ -154,9 +125,15 @@ fi
 
 exec 100>&-
 
-wait_port_closed $PORT $PID
-# Give it time to fully shut down
-wait_process_gone $PID 6
+if wait_port_closed $PORT $PID 4; then
+       echo "Test 2 failed: TPM did not close port"
+       exit 1
+fi
+
+if wait_process_gone $PID 4; then
+       echo "Test 2 failed: TPM process did not shut down"
+       exit 1
+fi
 
 exec 20<&1-; exec 21<&2-
 kill_quiet -0 $PID