Skip to contentSkip to conversation

denoise tests/t_lpback.sh

PATCH Superseded Gert Doering 2 participants 5 messages Oct 4, 2022 – Oct 5, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 denoise tests/t_lpback.sh

Introduce V=<nn> levels to t_lpback.sh self test

V=0 - do not print any output at all
V=1 - print intro line, summary at end, and "FAIL"+Log for failing tests V=99 - print everything + summary

code-wise, introduce test_start() / test_end() functions which do the $? check as well, so the actual testing code is streamlined.

---
 tests/t_lpback.sh | 88 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 57 insertions(+), 31 deletions(-)
tests/t_lpback.sh +57 -31
index 67952994..4cf29d26 100755
@@ -24,6 +24,52 @@ top_builddir="${top_builddir:-..}"
 trap "rm -f key.$$ tc-server-key.$$ tc-client-key.$$ log.$$ ; trap 0 ; exit 77" 1 2 15
 trap "rm -f key.$$ tc-server-key.$$ tc-client-key.$$ log.$$ ; exit 1" 0 3
 
+# verbosity, defaults to "1"
+V="${V:-1}"
+tests_passed=0
+tests_failed=0
+
+# ----------------------------------------------------------
+# helper functions
+# ----------------------------------------------------------
+
+# output progress information
+#  depending on verbosity level, collect & print only on failure
+test_start()
+{
+    case $V in
+	0) outbuf="" ;;			# no per-test output at all
+	1) outbuf="$@" ;;		# compact, details only on failure
+	*) printf "$@" ;;		# print all
+    esac
+}
+test_end()
+{
+    RC=$1 ; LOG=$2
+    if [ $RC != 0 ]
+    then
+	case $V in
+	    0) ;;				# no per-test output
+	    1) echo "$outbuf" "FAIL (RC=$RC)"; cat $LOG ;;
+	    *) echo "FAIL (RC=$RC)"; cat $LOG ;;
+	esac
+        e=1
+	tests_failed=$(( $tests_failed + 1 ))
+    else
+	case $V in
+	    0|1) ;;				# no per-test output for 'OK'
+	    *) echo "OK" ;;			# print all
+	esac
+	tests_passed=$(( $tests_passed + 1 ))
+    fi
+}
+
+# if running with V=1, give an indication what test runs now
+if [ "$V" == 1  ] ; then
+    echo "$0: running with V=$V, only printing test fails"
+fi
+
+
 # Get list of supported ciphers from openvpn --show-ciphers output
 CIPHERS=$(${top_builddir}/src/openvpn/openvpn --show-ciphers | \
             sed -e '/The following/,/^$/d' -e s'/ .*//' -e '/^[[:space:]]*$/d')
@@ -49,38 +95,20 @@ set +e
 
 for cipher in ${CIPHERS}
 do
-    printf "Testing cipher ${cipher}... "
+    test_start "Testing cipher ${cipher}... "
     ( "${top_builddir}/src/openvpn/openvpn" --test-crypto --secret key.$$ --cipher ${cipher} ) >log.$$ 2>&1
-    if [ $? != 0 ] ; then
-        echo "FAILED"
-        cat log.$$
-        e=1
-    else
-        echo "OK"
-    fi
+    test_end $? log.$$
 done
 
-printf "Testing tls-crypt-v2 server key generation... "
+test_start "Testing tls-crypt-v2 server key generation... "
 "${top_builddir}/src/openvpn/openvpn" \
     --genkey tls-crypt-v2-server tc-server-key.$$ >log.$$ 2>&1
-if [ $? != 0 ] ; then
-    echo "FAILED"
-    cat log.$$
-    e=1
-else
-    echo "OK"
-fi
+test_end $? log.$$
 
-printf "Testing tls-crypt-v2 key generation (no metadata)... "
+test_start "Testing tls-crypt-v2 key generation (no metadata)... "
 "${top_builddir}/src/openvpn/openvpn" --tls-crypt-v2 tc-server-key.$$ \
     --genkey tls-crypt-v2-client tc-client-key.$$ >log.$$ 2>&1
-if [ $? != 0 ] ; then
-    echo "FAILED"
-    cat log.$$
-    e=1
-else
-    echo "OK"
-fi
+test_end $? log.$$
 
 # Generate max-length base64 metadata ('A' is 0b000000 in base64)
 METADATA=""
@@ -89,16 +117,14 @@ while [ $i -lt 732 ]; do
     METADATA="${METADATA}A"
     i=$(expr $i + 1)
 done
-printf "Testing tls-crypt-v2 key generation (max length metadata)... "
+test_start "Testing tls-crypt-v2 key generation (max length metadata)... "
 "${top_builddir}/src/openvpn/openvpn" --tls-crypt-v2 tc-server-key.$$ \
     --genkey tls-crypt-v2-client tc-client-key.$$ "${METADATA}" \
     >log.$$ 2>&1
-if [ $? != 0 ] ; then
-    echo "FAILED"
-    cat log.$$
-    e=1
-else
-    echo "OK"
+test_end $? log.$$
+
+if [ "$V" -ge 1  ] ; then
+    echo "$0: tests passed: $tests_passed  failed: $tests_failed"
 fi
 
 rm key.$$ tc-server-key.$$ tc-client-key.$$ log.$$
Signature · 1 line
2.37.3
#1 lore raw
Quoted 8 lines · Gert Doering

Introduce V=<nn> levels to t_lpback.sh self test

V=0 - do not print any output at all
V=1 - print intro line, summary at end, and "FAIL"+Log for failing tests V=99 - print everything + summary

code-wise, introduce test_start() / test_end() functions which do the $? check as well, so the actual testing code is streamlined.

Small issue on dash, see below. Otherwise looks good to me.

Quoted 28 lines
tests/t_lpback.sh +20 -0
index 67952994..4cf29d26 100755
@@ -24,6 +24,52 @@ top_builddir="${top_builddir:-..}"
 trap "rm -f key.$$ tc-server-key.$$ tc-client-key.$$ log.$$ ; trap 0 ; exit 77" 1 2 15
 trap "rm -f key.$$ tc-server-key.$$ tc-client-key.$$ log.$$ ; exit 1" 0 3
 
+# verbosity, defaults to "1"
+V="${V:-1}"
+tests_passed=0
+tests_failed=0
+
+# ----------------------------------------------------------
+# helper functions
+# ----------------------------------------------------------
+
+# output progress information
+#  depending on verbosity level, collect & print only on failure
+test_start()
+{
+    case $V in
+	0) outbuf="" ;;			# no per-test output at all
+	1) outbuf="$@" ;;		# compact, details only on failure
+	*) printf "$@" ;;		# print all
+    esac
+}
+test_end()

tests/t_lpback.sh top_builddir="${top_builddir:-..}" · line 56

Quoted 10 lines
tests/t_lpback.sh +10 -0
+{
+    RC=$1 ; LOG=$2
+    if [ $RC != 0 ]
+    then
+	case $V in
+	    0) ;;				# no per-test output
+	    1) echo "$outbuf" "FAIL (RC=$RC)"; cat $LOG ;;
+	    *) echo "FAIL (RC=$RC)"; cat $LOG ;;
+	esac
+        e=1

Wrong indent.

tests/t_lpback.sh top_builddir="${top_builddir:-..}" · line 68

Quoted 12 lines
tests/t_lpback.sh +12 -0
+	tests_failed=$(( $tests_failed + 1 ))
+    else
+	case $V in
+	    0|1) ;;				# no per-test output for 'OK'
+	    *) echo "OK" ;;			# print all
+	esac
+	tests_passed=$(( $tests_passed + 1 ))
+    fi
+}
+
+# if running with V=1, give an indication what test runs now
+if [ "$V" == 1  ] ; then

With dash this fails with
./t_lpback.sh: 68: [: 1: unexpected operator

Could use -eq (or "="), but "==" seems a bashism.

tests/t_lpback.sh top_builddir="${top_builddir:-..}" · line 72

Quoted 4 lines
tests/t_lpback.sh +4 -0
+    echo "$0: running with V=$V, only printing test fails"
+fi
+
+

[...]

Regards,

Signature · 1 line
  Frank Lichtenheld
#2 lore raw

PATCH v2 1/1 denoise tests/t_lpback.sh

In reply to Gert Doering

Introduce V=<nn> levels to t_lpback.sh self test

V=0 - do not print any output at all
V=1 - print intro line, summary at end, and "FAIL"+Log for failing tests V=99 - print everything + summary

code-wise, introduce test_start() / test_end() functions which do the $? check as well, so the actual testing code is streamlined.

v2:
replace indent tabs with spaces
change [ $V == 1 ] expression to [ $V = 1 ] (POSIXly correct)

---
 tests/t_lpback.sh | 88 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 57 insertions(+), 31 deletions(-)
tests/t_lpback.sh +57 -31
index 67952994..5aabbd5e 100755
@@ -24,6 +24,52 @@ top_builddir="${top_builddir:-..}"
 trap "rm -f key.$$ tc-server-key.$$ tc-client-key.$$ log.$$ ; trap 0 ; exit 77" 1 2 15
 trap "rm -f key.$$ tc-server-key.$$ tc-client-key.$$ log.$$ ; exit 1" 0 3
 
+# verbosity, defaults to "1"
+V="${V:-1}"
+tests_passed=0
+tests_failed=0
+
+# ----------------------------------------------------------
+# helper functions
+# ----------------------------------------------------------
+
+# output progress information
+#  depending on verbosity level, collect & print only on failure
+test_start()
+{
+    case $V in
+        0) outbuf="" ;;                  # no per-test output at all
+        1) outbuf="$@" ;;                # compact, details only on failure
+        *) printf "$@" ;;                # print all
+    esac
+}
+test_end()
+{
+    RC=$1 ; LOG=$2
+    if [ $RC != 0 ]
+    then
+        case $V in
+            0) ;;                                # no per-test output
+            1) echo "$outbuf" "FAIL (RC=$RC)"; cat $LOG ;;
+            *) echo "FAIL (RC=$RC)"; cat $LOG ;;
+        esac
+        e=1
+        tests_failed=$(( $tests_failed + 1 ))
+    else
+        case $V in
+            0|1) ;;                              # no per-test output for 'OK'
+            *) echo "OK"                         # print all
+        esac
+        tests_passed=$(( $tests_passed + 1 ))
+    fi
+}
+
+# if running with V=1, give an indication what test runs now
+if [ "$V" = 1  ] ; then
+    echo "$0: running with V=$V, only printing test fails"
+fi
+
+
 # Get list of supported ciphers from openvpn --show-ciphers output
 CIPHERS=$(${top_builddir}/src/openvpn/openvpn --show-ciphers | \
             sed -e '/The following/,/^$/d' -e s'/ .*//' -e '/^[[:space:]]*$/d')
@@ -49,38 +95,20 @@ set +e
 
 for cipher in ${CIPHERS}
 do
-    printf "Testing cipher ${cipher}... "
+    test_start "Testing cipher ${cipher}... "
     ( "${top_builddir}/src/openvpn/openvpn" --test-crypto --secret key.$$ --cipher ${cipher} ) >log.$$ 2>&1
-    if [ $? != 0 ] ; then
-        echo "FAILED"
-        cat log.$$
-        e=1
-    else
-        echo "OK"
-    fi
+    test_end $? log.$$
 done
 
-printf "Testing tls-crypt-v2 server key generation... "
+test_start "Testing tls-crypt-v2 server key generation... "
 "${top_builddir}/src/openvpn/openvpn" \
     --genkey tls-crypt-v2-server tc-server-key.$$ >log.$$ 2>&1
-if [ $? != 0 ] ; then
-    echo "FAILED"
-    cat log.$$
-    e=1
-else
-    echo "OK"
-fi
+test_end $? log.$$
 
-printf "Testing tls-crypt-v2 key generation (no metadata)... "
+test_start "Testing tls-crypt-v2 key generation (no metadata)... "
 "${top_builddir}/src/openvpn/openvpn" --tls-crypt-v2 tc-server-key.$$ \
     --genkey tls-crypt-v2-client tc-client-key.$$ >log.$$ 2>&1
-if [ $? != 0 ] ; then
-    echo "FAILED"
-    cat log.$$
-    e=1
-else
-    echo "OK"
-fi
+test_end $? log.$$
 
 # Generate max-length base64 metadata ('A' is 0b000000 in base64)
 METADATA=""
@@ -89,16 +117,14 @@ while [ $i -lt 732 ]; do
     METADATA="${METADATA}A"
     i=$(expr $i + 1)
 done
-printf "Testing tls-crypt-v2 key generation (max length metadata)... "
+test_start "Testing tls-crypt-v2 key generation (max length metadata)... "
 "${top_builddir}/src/openvpn/openvpn" --tls-crypt-v2 tc-server-key.$$ \
     --genkey tls-crypt-v2-client tc-client-key.$$ "${METADATA}" \
     >log.$$ 2>&1
-if [ $? != 0 ] ; then
-    echo "FAILED"
-    cat log.$$
-    e=1
-else
-    echo "OK"
+test_end $? log.$$
+
+if [ "$V" -ge 1  ] ; then
+    echo "$0: tests passed: $tests_passed  failed: $tests_failed"
 fi
 
 rm key.$$ tc-server-key.$$ tc-client-key.$$ log.$$
Signature · 1 line
2.37.3
Acked-by on v2 1/1
#3 lore raw
Quoted 6 lines · Gert Doering

Introduce V=<nn> levels to t_lpback.sh self test

V=0 - do not print any output at all
V=1 - print intro line, summary at end, and "FAIL"+Log for failing tests V=99 - print everything + summary

Quoted 7 lines

code-wise, introduce test_start() / test_end() functions which do the $? check as well, so the actual testing code is streamlined.

v2:
replace indent tabs with spaces
change [ $V == 1 ] expression to [ $V = 1 ] (POSIXly correct)

Signature · 1 line
  Frank Lichtenheld
#4 lore raw
In reply to Gert Doering

Thanks for review & finding the "some shells do, some don't" wart in v1.

Patch has been applied to the master branch.

commit 977e0650c11500d3696419afbedf4a8f42ab6760

Author: Gert Doering
Date:   Tue Oct 4 15:14:03 2022 +0200
     denoise tests/t_lpback.sh

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Frank Lichtenheld <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25332.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering