]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - test-suite/test-squid-conf.sh
Fix dupe handling in Splay ACLs: src, dst, http_status, etc. (#1632)
[thirdparty/squid.git] / test-suite / test-squid-conf.sh
index 05fcaf34b5d76088fbd8db78b26b071796463aff..5dd9d139fa066a2fb35d88b750f22600db85a76c 100755 (executable)
@@ -22,6 +22,10 @@ expectFailure=no
 # If set, expect a matching stderr message
 messageRegex=""
 
+# If set, expect stderr messages matching regexes in the named file.
+# See expectMessages() and matchEachRegex().
+messageRegexFilename=""
+
 expectMessage()
 {
     local p1="$1"
@@ -43,6 +47,121 @@ expectMessage()
     fi
 }
 
+# Starts expecting Squid stderr lines that match configured regular
+# expressions. Expressions are read from standard input, one extended regex
+# per line, until a line matching here-document terminator in $p1 is read.
+# Empty lines are ignored.
+expectMessages()
+{
+    local p1="$1"
+    local p2="$2"
+    local where="$3"
+
+    if test -n "$messageRegexFilename"
+    then
+        echo "$where: ERROR: Repeated message-setting instruction"
+        exit 1
+    fi
+
+    if test -z "$p1"
+    then
+        echo "$where: ERROR: Missing here-doc terminator"
+        exit 1
+    fi
+    local heredocTerminator="$p1"
+
+    if test -n "$p2"
+    then
+        echo "$where: ERROR: Bad here-doc: Unexpected input after '$terminator' terminator: $p2"
+        exit 1
+    fi
+
+    messageRegexFilename="squid-expected-messages"
+    if ! :> $messageRegexFilename
+    then
+        echo "$where: ERROR: Cannot create a temporary file named $messageRegexFilename"
+        exit 1
+    fi
+
+    local foundTerminator=0;
+    while read hereDocLine
+    do
+        lineNo=$(($lineNo+1))
+        where="$instructionsFile:$lineNo";
+
+        if test "<<$hereDocLine" = "$heredocTerminator"
+        then
+            foundTerminator=1
+            break;
+        fi
+
+        # skip empty lines; they cannot be used as regexes and they improve
+        # here-document formatting
+        if test -z "$hereDocLine"
+        then
+            continue;
+        fi
+
+        if ! printf '%s\n' "$hereDocLine" >> $messageRegexFilename
+        then
+            echo "$where: ERROR: Cannot write to a temporary file named $messageRegexFilename"
+            exit 1
+        fi
+    done
+
+    if test $foundTerminator != 1
+    then
+        echo "$where: ERROR: Input ended before here-doc terminator ($heredocTerminator)"
+        exit 1
+    fi
+}
+
+# Checks that each of the extended regexes (in the given file) matches line(s)
+# in the given Squid log file. A log line can match at most once.
+matchEachRegex()
+{
+    local regexFilename="$1"
+    local errLog="$2"
+
+    local errorLogRemaining="$errorLog.unmatched";
+    local errorLogNext="$errorLog.next";
+
+    if ! cp $errorLog $errorLogRemaining
+    then
+        echo "ERROR: Cannot create a temporary file named $errorLogRemaining"
+        exit 1
+    fi
+
+    local result=0
+    while read regex
+    do
+        if grep -q -E "$regex" $errorLogRemaining
+        then
+            # No good way to distinguish a possible lack of "grep -v" matches
+            # from grep errors because both result in non-zero grep exit code.
+            # For now, assume that error log always has some "extra" lines,
+            # guaranteeing at least one "grep -v non-empty-regex" match.
+            if ! grep -v -E "$regex" $errorLogRemaining > $errorLogNext || ! mv $errorLogNext $errorLogRemaining
+            then
+                echo "ERROR: Temporary file manipulation failure"
+                exit 1
+            fi
+        else
+            echo "ERROR: Squid did not emit an expected message to stderr"
+            echo "    expected message regex: $regex"
+            result=1
+        fi
+    done < $regexFilename
+
+    if test $result != 0
+    then
+        echo "Unmatched Squid stderr lines (see $errorLogRemaining):"
+        cat $errorLogRemaining
+    fi
+
+    return $result
+}
+
 instructionsFile="$configFile.instructions"
 if test -e $instructionsFile
 then
@@ -75,6 +194,12 @@ then
             continue;
         fi
 
+        if test "$instructionName" = "expect-messages"
+        then
+            expectMessages "$p1" "$p2" "$here"
+            continue;
+        fi
+
         if test "$instructionName" = "skip-unless-autoconf-defines"
         then
             # Skip test unless the given macro is #defined in autoconf.h
@@ -136,6 +261,12 @@ then
     updateOutcome 1
 fi
 
+if test -n "$messageRegexFilename" && ! matchEachRegex $messageRegexFilename $errorLog
+then
+    # matchEachRegex reports errors
+    updateOutcome 1
+fi
+
 if test $expectFailure = no
 then
     if test "$result" -ne 0