]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Support "negative" squid-conf-tests (#1214)
authorAlex Rousskov <rousskov@measurement-factory.com>
Tue, 20 Dec 2022 15:41:40 +0000 (15:41 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Wed, 21 Dec 2022 22:35:16 +0000 (22:35 +0000)
The test suite can now check that Squid rejects a given malformed
configuration file and that the rejection reason matches the expected
one. The latter reduces the probability that a successful "negative"
test outcome would hide a bug (because Squid has rejected a malformed
file but for a reason unrelated to what the test was trying to verify).

For now, enable just one acl regex test case, addressing an old TODO.

To improve "negative" test coverage, we would need to generate test
configurations using a configuration template and a list of problematic
expressions (with corresponding failure messages), but this hard-coded
approach is already better than nothing.

test-suite/Makefile.am
test-suite/squidconf/bad-regex.conf [new file with mode: 0644]
test-suite/squidconf/bad-regex.conf.instructions [new file with mode: 0644]
test-suite/squidconf/regex.conf
test-suite/test-squid-conf.sh

index 5d78c6b5ab64a4bfd1bb2fd3c584538f952d0fa6..8e806d433edc890d4c608e44e6aaa7734146fe14 100644 (file)
@@ -154,4 +154,4 @@ squid-conf-tests: $(srcdir)/test-squid-conf.sh $(top_builddir)/src/squid.conf.de
        done; \
        if test "$$failed" -eq 0; then cp $(TRUE) $@ ; fi
 
-CLEANFILES += squid-conf-tests
+CLEANFILES += squid-conf-tests squid-stderr.log
diff --git a/test-suite/squidconf/bad-regex.conf b/test-suite/squidconf/bad-regex.conf
new file mode 100644 (file)
index 0000000..6f23152
--- /dev/null
@@ -0,0 +1,8 @@
+## Copyright (C) 1996-2022 The Squid Software Foundation and contributors
+##
+## Squid software is distributed under GPLv2+ license and includes
+## contributions from numerous individuals and organizations.
+## Please see the COPYING and CONTRIBUTORS files for details.
+##
+
+acl foo browser *
diff --git a/test-suite/squidconf/bad-regex.conf.instructions b/test-suite/squidconf/bad-regex.conf.instructions
new file mode 100644 (file)
index 0000000..de8f529
--- /dev/null
@@ -0,0 +1,2 @@
+expect-failure ERROR:.configuration.failure:.POSIX.regcomp.*failure
+
index b1f3b8b620251d714d541456bb2edf277a728765..236dc166a989db02d5196df9c9d643a4ddace0c4 100644 (file)
@@ -18,6 +18,3 @@ acl G dstdom_regex \.g...le\.com$
 
 acl B browser ^Mozilla
 acl B browser ^Java/[0-9]+(\.[0-9]+)?
-
-# TODO: Support testing invalid configurations, like this one:
-# acl foo browser *
index 35ef761a9fc775b8204f99d340301c3653cbb357..d1425d8b9a13757fce92fa50aaadf1e199766727 100755 (executable)
@@ -15,6 +15,9 @@ top_builddir=$1
 sbindir=$2
 configFile=$3
 
+# If set, expect non-zero Squid exit code, with a matching stderr message
+failureRegex=""
+
 instructionsFile="$configFile.instructions"
 if test -e $instructionsFile
 then
@@ -34,6 +37,25 @@ then
             continue; # skip comment lines
         fi
 
+        if test "$instructionName" = "expect-failure"
+        then
+            if test -n "$failureRegex"
+            then
+                echo "$here: ERROR: Repeated $instructionName instruction";
+                exit 1;
+            fi
+
+            failureRegex="$p1"
+
+            if test -n "$p2"
+            then
+                echo "$here: ERROR: Bad $instructionName instruction: Unexpected second parameter: $p2";
+                exit 1;
+            fi
+
+            continue;
+        fi
+
         if test "$instructionName" = "skip-unless-autoconf-defines"
         then
             # Skip test unless the given macro is #defined in autoconf.h
@@ -68,8 +90,34 @@ then
             exit 1;
         fi
     done < $instructionsFile
+fi
+
+errorLog="squid-stderr.log"
 
-    # TODO: Add support for the "require-failure" instruction.
+$sbindir/squid -k parse -f $configFile 2> $errorLog
+result=$?
+
+if test -z "$failureRegex"
+then
+    # a positive test does not require special $result interpretation
+    exit $?
 fi
 
-exec $sbindir/squid -k parse -f $configFile
+if test "$result" -eq 0
+then
+    echo "ERROR: Squid successfully parsed malformed $configFile instead of rejecting it"
+    exit 1;
+fi
+
+if ! grep -q -E "$failureRegex" $errorLog
+then
+    echo "ERROR: Squid rejected malformed $configFile but did not emit an expected message to stderr"
+    echo "    expected error message regex: $failureRegex"
+    echo "Squid stderr output:"
+    cat $errorLog
+    exit 1;
+fi
+
+echo "Squid rejected malformed $configFile as expected; Squid exit code: $result"
+exit 0
+