From: Alex Rousskov Date: Tue, 20 Dec 2022 15:41:40 +0000 (+0000) Subject: Support "negative" squid-conf-tests (#1214) X-Git-Tag: SQUID_6_0_1~52 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=12cac459b014c63fde919cccd3d35b88d1cf010d;p=thirdparty%2Fsquid.git Support "negative" squid-conf-tests (#1214) 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. --- diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index 5d78c6b5ab..8e806d433e 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -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 index 0000000000..6f23152f21 --- /dev/null +++ b/test-suite/squidconf/bad-regex.conf @@ -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 index 0000000000..de8f529e2e --- /dev/null +++ b/test-suite/squidconf/bad-regex.conf.instructions @@ -0,0 +1,2 @@ +expect-failure ERROR:.configuration.failure:.POSIX.regcomp.*failure + diff --git a/test-suite/squidconf/regex.conf b/test-suite/squidconf/regex.conf index b1f3b8b620..236dc166a9 100644 --- a/test-suite/squidconf/regex.conf +++ b/test-suite/squidconf/regex.conf @@ -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 * diff --git a/test-suite/test-squid-conf.sh b/test-suite/test-squid-conf.sh index 35ef761a9f..d1425d8b9a 100755 --- a/test-suite/test-squid-conf.sh +++ b/test-suite/test-squid-conf.sh @@ -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 +