Skip to content

Commit

Permalink
Add support to regressions tests for matching multiple values in erro…
Browse files Browse the repository at this point in the history
…r_log.

- Update `Variable offset - FILES_NAMES` regression test to specify
  expected error log output with two strings, because the order in which
  they're processed can vary in different systems.
  - The test currently expects a specific order, which is not guaranteed
    as the underlying structure in `AnchoredSetVariable::resolve` is a
    `std::unordered_multimap`.
  - The test is now enabled on Windows builds too.
  • Loading branch information
eduar-hte committed Oct 24, 2024
1 parent b30a12e commit 7a66954
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ jobs:
jq "map(if .title == \"Test match variable (1/n)\" then .enabled = 0 else . end)" issue-2423-msg-in-chain.json > tmp.json && move /Y tmp.json issue-2423-msg-in-chain.json
jq "map(if .title == \"Test match variable (2/n)\" then .enabled = 0 else . end)" issue-2423-msg-in-chain.json > tmp.json && move /Y tmp.json issue-2423-msg-in-chain.json
jq "map(if .title == \"Test match variable (3/n)\" then .enabled = 0 else . end)" issue-2423-msg-in-chain.json > tmp.json && move /Y tmp.json issue-2423-msg-in-chain.json
jq "map(if .title == \"Variable offset - FILES_NAMES\" then .enabled = 0 else . end)" offset-variable.json > tmp.json && move /Y tmp.json offset-variable.json
- name: Run tests
working-directory: build\win32\build
run: |
Expand Down
6 changes: 4 additions & 2 deletions test/regression/regression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ void perform_unit_test(const ModSecurityTest<RegressionTest> &test,
std::to_string(t->http_code) +
" got: " + std::to_string(r.status) + "\n";
testRes->passed = false;
} else if (!contains(context.m_server_log.str(), t->error_log)) {
} else if (auto errit = std::find_if(t->error_log.begin(), t->error_log.end(),
[&context](const auto &x) { return !contains(context.m_server_log.str(), x); });
errit != t->error_log.end()) {
if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << std::endl;
Expand All @@ -351,7 +353,7 @@ void perform_unit_test(const ModSecurityTest<RegressionTest> &test,
testRes->reason << "Error log was not matching the " \
<< "expected results." << std::endl;
testRes->reason << KWHT << "Expecting: " << RESET \
<< t->error_log + "";
<< *errit + "";
testRes->passed = false;
} else if (!t->audit_log.empty() && !contains(getAuditLogContent(modsec_transaction.m_rules->m_auditLog->m_path1), t->audit_log)) {
if (test.m_automake_output) {
Expand Down
9 changes: 8 additions & 1 deletion test/regression/regression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,14 @@ RegressionTest *RegressionTest::from_yajl_node(const yajl_val &node) {
u->debug_log = YAJL_GET_STRING(val2);
}
if (strcmp(key2, "error_log") == 0) {
u->error_log = YAJL_GET_STRING(val2);
if (val2->u.array.len == 0)
u->error_log.insert(YAJL_GET_STRING(val2));
else {
for (int k = 0; k < val2->u.array.len; k++) {
yajl_val vale = val2->u.array.values[k];
u->error_log.insert(YAJL_GET_STRING(vale));
}
}
}
if (strcmp(key2, "http_code") == 0) {
u->http_code = YAJL_GET_INTEGER(val2);
Expand Down
3 changes: 2 additions & 1 deletion test/regression/regression_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <unordered_map>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <utility>

Expand Down Expand Up @@ -54,7 +55,7 @@ class RegressionTest {

std::string audit_log;
std::string debug_log;
std::string error_log;
std::set<std::string> error_log;
std::string parser_error;

std::string clientIp;
Expand Down
5 changes: 4 additions & 1 deletion test/test-cases/regression/offset-variable.json
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,10 @@
]
},
"expected":{
"error_log":"o0,8o0,8v491,8t:trimo0,16o0,16v709,16t:trim"
"error_log":[
"o0,8o0,8v491,8t:trim",
"o0,16o0,16v709,16t:trim"
]
},
"rules":[
"SecRequestBodyAccess On",
Expand Down

0 comments on commit 7a66954

Please sign in to comment.