diff --git a/build/win32/CMakeLists.txt b/build/win32/CMakeLists.txt index cebf4c9be2..fbf39f08d9 100644 --- a/build/win32/CMakeLists.txt +++ b/build/win32/CMakeLists.txt @@ -171,13 +171,13 @@ endfunction() # unit tests file(GLOB unitTestSources ${BASE_DIR}/test/unit/*.cc) -add_executable(unit_tests ${unitTestSources}) +add_executable(unit_tests ${unitTestSources} ${BASE_DIR}/test/common/custom_debug_log.cc) setTestTargetProperties(unit_tests) target_compile_options(unit_tests PRIVATE /wd4805) # regression tests file(GLOB regressionTestsSources ${BASE_DIR}/test/regression/*.cc) -add_executable(regression_tests ${regressionTestsSources}) +add_executable(regression_tests ${regressionTestsSources} ${BASE_DIR}/test/common/custom_debug_log.cc) setTestTargetProperties(regression_tests) macro(add_regression_test_capability compile_definition flag) diff --git a/test/Makefile.am b/test/Makefile.am index 4d4396ae82..2c1ba02a49 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -31,7 +31,8 @@ EXTRA_DIST = \ noinst_PROGRAMS += unit_tests unit_tests_SOURCES = \ unit/unit.cc \ - unit/unit_test.cc + unit/unit_test.cc \ + common/custom_debug_log.cc noinst_HEADERS = \ @@ -94,7 +95,7 @@ noinst_PROGRAMS += regression_tests regression_tests_SOURCES = \ regression/regression.cc \ regression/regression_test.cc \ - regression/custom_debug_log.cc + common/custom_debug_log.cc regression_tests_LDADD = \ $(CURL_LDADD) \ diff --git a/test/common/custom_debug_log.cc b/test/common/custom_debug_log.cc new file mode 100644 index 0000000000..1091ac0b75 --- /dev/null +++ b/test/common/custom_debug_log.cc @@ -0,0 +1,59 @@ +/* + * ModSecurity, http://www.modsecurity.org/ + * Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/) + * + * You may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * If any of the files related to licensing are missing or if you have any + * other questions related to licensing please contact Trustwave Holdings, Inc. + * directly using the email address security@modsecurity.org. + * + */ + +#include "custom_debug_log.h" + +#include +#include + +#include "modsecurity/debug_log.h" +#include "src/utils/regex.h" + +namespace modsecurity_test +{ + + CustomDebugLog::~CustomDebugLog() {} + + void CustomDebugLog::write(int level, const std::string &message) + { + m_log << "[" << level << "] " << message << std::endl; + } + + void CustomDebugLog::write(int level, const std::string &id, + const std::string &uri, const std::string &msg) + { + std::string msgf = "[" + std::to_string(level) + "] " + msg; + msgf = "[" + id + "] [" + uri + "] " + msgf; + m_log << msgf << std::endl; + } + + bool const CustomDebugLog::contains(const std::string &pattern) const + { + modsecurity::Utils::Regex re(pattern); + std::string s = m_log.str(); + return modsecurity::Utils::regex_search(s, re); + } + + std::string const CustomDebugLog::log_messages() const + { + return m_log.str(); + } + + int CustomDebugLog::getDebugLogLevel() + { + return 9; + } + +} // namespace modsecurity_test diff --git a/test/regression/custom_debug_log.h b/test/common/custom_debug_log.h similarity index 100% rename from test/regression/custom_debug_log.h rename to test/common/custom_debug_log.h diff --git a/test/common/modsecurity_test_context.h b/test/common/modsecurity_test_context.h new file mode 100644 index 0000000000..369717ec19 --- /dev/null +++ b/test/common/modsecurity_test_context.h @@ -0,0 +1,47 @@ +#ifndef TEST_COMMON_MODSECURITY_TEST_CONTEXT_H_ +#define TEST_COMMON_MODSECURITY_TEST_CONTEXT_H_ + +#include "modsecurity/modsecurity.h" +#include "modsecurity/rules_set.h" +#include "modsecurity/transaction.h" +#include "custom_debug_log.h" + +#include + +namespace modsecurity_test +{ + + class ModSecurityTestContext + { + public: + explicit ModSecurityTestContext(const std::string &connector) + : m_modsec_rules(new CustomDebugLog) + { + m_modsec.setConnectorInformation(connector); + m_modsec.setServerLogCb(logCb); + } + ~ModSecurityTestContext() = default; + + modsecurity::Transaction create_transaction() + { + return modsecurity::Transaction(&m_modsec, + &m_modsec_rules, + &m_server_log); + } + + modsecurity::ModSecurity m_modsec; + modsecurity::RulesSet m_modsec_rules; + std::stringstream m_server_log; + + private: + static void logCb(void *data, const void *msgv) + { + const char *msg = reinterpret_cast(msgv); + std::stringstream *ss = (std::stringstream *)data; + *ss << msg << std::endl; + } + }; + +} // namespace modsecurity_test + +#endif // TEST_COMMON_MODSECURITY_TEST_H_ diff --git a/test/regression/custom_debug_log.cc b/test/regression/custom_debug_log.cc deleted file mode 100644 index 1e7de0e02a..0000000000 --- a/test/regression/custom_debug_log.cc +++ /dev/null @@ -1,55 +0,0 @@ -/* - * ModSecurity, http://www.modsecurity.org/ - * Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/) - * - * You may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * If any of the files related to licensing are missing or if you have any - * other questions related to licensing please contact Trustwave Holdings, Inc. - * directly using the email address security@modsecurity.org. - * - */ - -#include "test/regression/custom_debug_log.h" - -#include -#include - -#include "modsecurity/debug_log.h" -#include "src/utils/regex.h" - -namespace modsecurity_test { - -CustomDebugLog::~CustomDebugLog() { } - -void CustomDebugLog::write(int level, const std::string& message) { - m_log << "[" << level << "] " << message << std::endl; -} - -void CustomDebugLog::write(int level, const std::string &id, - const std::string &uri, const std::string &msg) { - std::string msgf = "[" + std::to_string(level) + "] " + msg; - msgf = "[" + id + "] [" + uri + "] " + msgf; - m_log << msgf << std::endl; -} - -bool const CustomDebugLog::contains(const std::string& pattern) const { - modsecurity::Utils::Regex re(pattern); - std::string s = m_log.str(); - return modsecurity::Utils::regex_search(s, re); -} - -std::string const CustomDebugLog::log_messages() const { - return m_log.str(); -} - - -int CustomDebugLog::getDebugLogLevel() { - return 9; -} - - -} // namespace modsecurity_test diff --git a/test/regression/regression.cc b/test/regression/regression.cc index a6ede61c72..68dd6a0182 100644 --- a/test/regression/regression.cc +++ b/test/regression/regression.cc @@ -33,7 +33,7 @@ #include "test/common/colors.h" #include "test/regression/regression_test.h" #include "test/common/modsecurity_test_results.h" -#include "test/regression/custom_debug_log.h" +#include "test/common/modsecurity_test_context.h" #include "src/utils/regex.h" using modsecurity_test::CustomDebugLog; @@ -42,20 +42,22 @@ using modsecurity_test::ModSecurityTestResults; using modsecurity_test::RegressionTest; using modsecurity_test::RegressionTestResult; +using modsecurity::Utils::Regex; using modsecurity::Utils::regex_search; using modsecurity::Utils::SMatch; -using modsecurity::Utils::Regex; std::string default_test_path = "test-cases/regression"; std::list resources; -void print_help() { +void print_help() +{ std::cout << "Use ./regression-tests /path/to/file" << std::endl; std::cout << std::endl; std::cout << std::endl; } -bool contains(const std::string &s, const std::string &pattern) { +bool contains(const std::string &s, const std::string &pattern) +{ bool ret; modsecurity::Utils::Regex re(pattern); ret = modsecurity::Utils::regex_search(s, re); @@ -71,38 +73,48 @@ void clearAuditLog(const std::string &filename) { } } } -std::string getAuditLogContent(const std::string &filename) { +std::string getAuditLogContent(const std::string &filename) +{ std::stringstream buffer; - if (!filename.empty()) { - try { - std::ifstream t(filename); - buffer << t.rdbuf(); - } catch (...) { + if (!filename.empty()) + { + try + { + std::ifstream t(filename); + buffer << t.rdbuf(); + } + catch (...) + { std::cout << "Failed to read file:" << filename << std::endl; } } return buffer.str(); } - void actions(ModSecurityTestResults *r, - modsecurity::Transaction *a, std::stringstream *serverLog) { + modsecurity::Transaction *a, std::stringstream *serverLog) +{ modsecurity::ModSecurityIntervention it; memset(&it, '\0', sizeof(modsecurity::ModSecurityIntervention)); it.status = 200; - if (a->intervention(&it) == true) { - if (it.pause != 0) { + if (a->intervention(&it) == true) + { + if (it.pause != 0) + { // FIXME: } - if (it.status != 0) { + if (it.status != 0) + { r->status = it.status; } - if (it.url != NULL) { + if (it.url != NULL) + { r->location.append(it.url); - free(it.url); - it.url = NULL; + free(it.url); + it.url = NULL; } - if (it.log != NULL) { + if (it.log != NULL) + { *serverLog << it.log; free(it.log); it.log = NULL; @@ -110,24 +122,14 @@ void actions(ModSecurityTestResults *r, } } -void logCb(void *data, const void *msgv) { - const char *msg = reinterpret_cast(msgv); - std::stringstream *ss = (std::stringstream *) data; - *ss << msg << std::endl; -} - - void perform_unit_test(ModSecurityTest *test, - std::vector *tests, - ModSecurityTestResults *res, int *count) { - - for (RegressionTest *t : *tests) { - CustomDebugLog *debug_log = new CustomDebugLog(); - modsecurity::ModSecurity *modsec = NULL; - modsecurity::RulesSet *modsec_rules = NULL; - modsecurity::Transaction *modsec_transaction = NULL; + std::vector *tests, + ModSecurityTestResults *res, int *count) +{ + + for (RegressionTest *t : *tests) + { ModSecurityTestResults r; - std::stringstream serverLog; RegressionTestResult *testRes = new RegressionTestResult(); testRes->test = t; @@ -136,25 +138,32 @@ void perform_unit_test(ModSecurityTest *test, size_t offset = t->filename.find_last_of("/\\"); std::string filename(""); - if (offset != std::string::npos) { + if (offset != std::string::npos) + { filename = std::string(t->filename, offset + 1, - t->filename.length() - offset - 1); - } else { + t->filename.length() - offset - 1); + } + else + { filename = t->filename; } - if (!test->m_automake_output) { - std::cout << std::setw(3) << std::right << - std::to_string(*count) << " "; + if (!test->m_automake_output) + { + std::cout << std::setw(3) << std::right << std::to_string(*count) << " "; std::cout << std::setw(50) << std::left << filename; std::cout << std::setw(70) << std::left << t->name; } - if (t->enabled == 0) { - if (test->m_automake_output) { - std::cout << ":test-result: SKIP" << filename \ - << ":" << t->name << std::endl; - } else { + if (t->enabled == 0) + { + if (test->m_automake_output) + { + std::cout << ":test-result: SKIP" << filename + << ":" << t->name << std::endl; + } + else + { std::cout << KCYN << "disabled" << RESET << std::endl; } res->push_back(testRes); @@ -169,77 +178,79 @@ void perform_unit_test(ModSecurityTest *test, unlink("./modsec-shared-collections-lock"); #endif - modsec = new modsecurity::ModSecurity(); - modsec->setConnectorInformation("ModSecurity-regression v0.0.1-alpha" \ - " (ModSecurity regression test utility)"); - modsec->setServerLogCb(logCb); - modsec_rules = new modsecurity::RulesSet(debug_log); + modsecurity_test::ModSecurityTestContext context("ModSecurity-regression v0.0.1-alpha" + " (ModSecurity regression test utility)"); bool found = true; - if (t->resource.empty() == false) { - found = (std::find(resources.begin(), resources.end(), t->resource) - != resources.end()); + if (t->resource.empty() == false) + { + found = (std::find(resources.begin(), resources.end(), t->resource) != resources.end()); } - if (!found) { + if (!found) + { testRes->passed = false; testRes->skipped = true; testRes->reason << KCYN << "ModSecurity was not " << std::endl; testRes->reason << KCYN << "compiled with support " << std::endl; testRes->reason << KCYN << "to: " << t->resource << std::endl; testRes->reason << RESET << std::endl; - if (test->m_automake_output) { - std::cout << ":test-result: SKIP " << filename \ - << ":" << t->name << std::endl; - } else { + if (test->m_automake_output) + { + std::cout << ":test-result: SKIP " << filename + << ":" << t->name << std::endl; + } + else + { std::cout << KCYN << "skipped!" << RESET << std::endl; } res->push_back(testRes); - delete modsec_transaction; - delete modsec_rules; - delete modsec; - continue; } - modsec_rules->load("SecDebugLogLevel 9"); - if (modsec_rules->load(t->rules.c_str(), filename) < 0) { + context.m_modsec_rules.load("SecDebugLogLevel 9"); + if (context.m_modsec_rules.load(t->rules.c_str(), filename) < 0) + { /* Parser error */ - if (t->parser_error.empty() == true) { + if (t->parser_error.empty() == true) + { /* * Not expecting any error, thus return the error to * the user. */ - if (test->m_automake_output) { - std::cout << ":test-result: FAIL " << filename \ - << ":" << t->name << ":" << *count << std::endl; - } else { + if (test->m_automake_output) + { + std::cout << ":test-result: FAIL " << filename + << ":" << t->name << ":" << *count << std::endl; + } + else + { std::cout << KRED << "failed!" << RESET << std::endl; } - testRes->reason << KRED << "parse failed." << RESET \ - << std::endl; - testRes->reason << modsec_rules->getParserError() \ - << std::endl; + testRes->reason << KRED << "parse failed." << RESET + << std::endl; + testRes->reason << context.m_modsec_rules.getParserError() + << std::endl; testRes->passed = false; res->push_back(testRes); - delete modsec_transaction; - delete modsec_rules; - delete modsec; - continue; } Regex re(t->parser_error); SMatch match; - std::string s = modsec_rules->getParserError(); - - if (regex_search(s, &match, re)) { - if (test->m_automake_output) { - std::cout << ":test-result: PASS " << filename \ - << ":" << t->name << std::endl; - } else { + const auto s = context.m_modsec_rules.getParserError(); + + if (regex_search(s, &match, re)) + { + if (test->m_automake_output) + { + std::cout << ":test-result: PASS " << filename + << ":" << t->name << std::endl; + } + else + { std::cout << KGRN << "passed!" << RESET << std::endl; } /* Parser error was expected, thus, the test passed. */ @@ -247,236 +258,212 @@ void perform_unit_test(ModSecurityTest *test, testRes->passed = true; res->push_back(testRes); - delete modsec_transaction; - delete modsec_rules; - delete modsec; - continue; - } else { + } + else + { /* Parser error was expected, but with a different content */ - if (test->m_automake_output) { - std::cout << ":test-result: FAIL " << filename \ - << ":" << t->name << ":" << *count << std::endl; - } else { + if (test->m_automake_output) + { + std::cout << ":test-result: FAIL " << filename + << ":" << t->name << ":" << *count << std::endl; + } + else + { std::cout << KRED << "failed!" << RESET << std::endl; } testRes->reason << KRED << "failed!" << RESET << std::endl; - testRes->reason << KWHT << "Expected a parser error." \ - << RESET << std::endl; - testRes->reason << KWHT << "Expected: " << RESET \ - << t->parser_error << std::endl; - testRes->reason << KWHT << "Produced: " << RESET \ - << s << std::endl; + testRes->reason << KWHT << "Expected a parser error." + << RESET << std::endl; + testRes->reason << KWHT << "Expected: " << RESET + << t->parser_error << std::endl; + testRes->reason << KWHT << "Produced: " << RESET + << s << std::endl; testRes->passed = false; res->push_back(testRes); - delete modsec_transaction; - delete modsec_rules; - delete modsec; - continue; } - } else { + } + else + { /* Parser error was expected but never happened */ - if (t->parser_error.empty() == false) { - if (test->m_automake_output) { - std::cout << ":test-result: FAIL " << filename \ - << ":" << t->name << ":" << *count << std::endl; - } else { + if (t->parser_error.empty() == false) + { + if (test->m_automake_output) + { + std::cout << ":test-result: FAIL " << filename + << ":" << t->name << ":" << *count << std::endl; + } + else + { std::cout << KRED << "failed!" << RESET << std::endl; - std::cout << KWHT << "Expected a parser error." \ - << RESET << std::endl; - std::cout << KWHT << "Expected: " << RESET \ - << t->parser_error << std::endl; + std::cout << KWHT << "Expected a parser error." + << RESET << std::endl; + std::cout << KWHT << "Expected: " << RESET + << t->parser_error << std::endl; } testRes->passed = false; res->push_back(testRes); - delete modsec_transaction; - delete modsec_rules; - delete modsec; - continue; } } - modsec_transaction = new modsecurity::Transaction(modsec, modsec_rules, - &serverLog); + auto modsec_transaction = context.create_transaction(); - clearAuditLog(modsec_transaction->m_rules->m_auditLog->m_path1); + clearAuditLog(modsec_transaction.m_rules->m_auditLog->m_path1); - modsec_transaction->processConnection(t->clientIp.c_str(), - t->clientPort, t->serverIp.c_str(), t->serverPort); + modsec_transaction.processConnection(t->clientIp.c_str(), + t->clientPort, t->serverIp.c_str(), t->serverPort); if (t->hostname != "") { - modsec_transaction->setRequestHostName(t->hostname); + modsec_transaction.setRequestHostName(t->hostname); } - actions(&r, modsec_transaction, &serverLog); -#if 0 - if (r.status != 200) { - goto end; - } -#endif + actions(&r, &modsec_transaction, &context.m_server_log); - modsec_transaction->processURI(t->uri.c_str(), t->method.c_str(), - t->httpVersion.c_str()); + modsec_transaction.processURI(t->uri.c_str(), t->method.c_str(), + t->httpVersion.c_str()); - actions(&r, modsec_transaction, &serverLog); -#if 0 - if (r.status != 200) { - goto end; - } -#endif + actions(&r, &modsec_transaction, &context.m_server_log); - for (std::pair headers : - t->request_headers) { - modsec_transaction->addRequestHeader(headers.first.c_str(), - headers.second.c_str()); + for (const auto &headers : t->request_headers) + { + modsec_transaction.addRequestHeader(headers.first.c_str(), + headers.second.c_str()); } - modsec_transaction->processRequestHeaders(); - actions(&r, modsec_transaction, &serverLog); -#if 0 - if (r.status != 200) { - goto end; - } -#endif + modsec_transaction.processRequestHeaders(); + actions(&r, &modsec_transaction, &context.m_server_log); - modsec_transaction->appendRequestBody( + modsec_transaction.appendRequestBody( (unsigned char *)t->request_body.c_str(), t->request_body.size()); - modsec_transaction->processRequestBody(); - actions(&r, modsec_transaction, &serverLog); -#if 0 - if (r.status != 200) { - goto end; - } -#endif + modsec_transaction.processRequestBody(); + actions(&r, &modsec_transaction, &context.m_server_log); - for (std::pair headers : - t->response_headers) { - modsec_transaction->addResponseHeader(headers.first.c_str(), - headers.second.c_str()); + for (const auto &headers : t->response_headers) + { + modsec_transaction.addResponseHeader(headers.first.c_str(), + headers.second.c_str()); } - modsec_transaction->processResponseHeaders(r.status, - t->response_protocol); - actions(&r, modsec_transaction, &serverLog); -#if 0 - if (r.status != 200) { - goto end; - } -#endif + modsec_transaction.processResponseHeaders(r.status, + t->response_protocol); + actions(&r, &modsec_transaction, &context.m_server_log); - modsec_transaction->appendResponseBody( + modsec_transaction.appendResponseBody( (unsigned char *)t->response_body.c_str(), t->response_body.size()); - modsec_transaction->processResponseBody(); - actions(&r, modsec_transaction, &serverLog); -#if 0 - if (r.status != 200) { - goto end; - } -#endif + modsec_transaction.processResponseBody(); + actions(&r, &modsec_transaction, &context.m_server_log); -#if 0 -end: -#endif - modsec_transaction->processLogging(); + modsec_transaction.processLogging(); - CustomDebugLog *d = reinterpret_cast - (modsec_rules->m_debugLog); + const auto *d = reinterpret_cast(context.m_modsec_rules.m_debugLog); - if (d != NULL) { - if (!d->contains(t->debug_log)) { - if (test->m_automake_output) { - std::cout << ":test-result: FAIL " << filename \ - << ":" << t->name << ":" << *count << std::endl; - } else { - std::cout << KRED << "failed!" << RESET << std::endl; - } - testRes->reason << "Debug log was not matching the " \ - << "expected results." << std::endl; - testRes->reason << KWHT << "Expecting: " << RESET \ - << t->debug_log + ""; - testRes->passed = false; - } else if (r.status != t->http_code) { - if (test->m_automake_output) { - std::cout << ":test-result: FAIL " << filename \ - << ":" << t->name << ":" << *count << std::endl; - } else { - std::cout << KRED << "failed!" << RESET << std::endl; - } - testRes->reason << "HTTP code mismatch. expecting: " + \ - std::to_string(t->http_code) + \ - " got: " + std::to_string(r.status) + "\n"; - testRes->passed = false; - } else if (!contains(serverLog.str(), t->error_log)) { - if (test->m_automake_output) { - std::cout << ":test-result: FAIL " << filename \ - << ":" << t->name << std::endl; - } else { - std::cout << KRED << "failed!" << RESET << std::endl; - } - testRes->reason << "Error log was not matching the " \ - << "expected results." << std::endl; - testRes->reason << KWHT << "Expecting: " << RESET \ - << t->error_log + ""; - 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) { - std::cout << ":test-result: FAIL " << filename \ - << ":" << t->name << ":" << *count << std::endl; - } else { - std::cout << KRED << "failed!" << RESET << std::endl; - } - testRes->reason << "Audit log was not matching the " \ - << "expected results." << std::endl; - testRes->reason << KWHT << "Expecting: " << RESET \ - << t->audit_log + ""; - testRes->passed = false; - } else { - if (test->m_automake_output) { - std::cout << ":test-result: PASS " << filename \ - << ":" << t->name << std::endl; - } else { - std::cout << KGRN << "passed!" << RESET << std::endl; - } - testRes->passed = true; - goto after_debug_log; + if (!d->contains(t->debug_log)) + { + if (test->m_automake_output) + { + std::cout << ":test-result: FAIL " << filename + << ":" << t->name << ":" << *count << std::endl; } - - if (testRes->passed == false) { - testRes->reason << std::endl; - testRes->reason << KWHT << "Debug log:" << RESET << std::endl; - testRes->reason << d->log_messages() << std::endl; - testRes->reason << KWHT << "Error log:" << RESET << std::endl; - testRes->reason << serverLog.str() << std::endl; - testRes->reason << KWHT << "Audit log:" << RESET << std::endl; - testRes->reason << getAuditLogContent(modsec_transaction->m_rules->m_auditLog->m_path1) << std::endl; + else + { + std::cout << KRED << "failed!" << RESET << std::endl; + } + testRes->reason << "Debug log was not matching the " + << "expected results." << std::endl; + testRes->reason << KWHT << "Expecting: " << RESET + << t->debug_log + ""; + testRes->passed = false; + } + else if (r.status != t->http_code) + { + if (test->m_automake_output) + { + std::cout << ":test-result: FAIL " << filename + << ":" << t->name << ":" << *count << std::endl; + } + else + { + std::cout << KRED << "failed!" << RESET << std::endl; } + testRes->reason << "HTTP code mismatch. expecting: " + + 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)) + { + if (test->m_automake_output) + { + std::cout << ":test-result: FAIL " << filename + << ":" << t->name << std::endl; + } + else + { + std::cout << KRED << "failed!" << RESET << std::endl; + } + testRes->reason << "Error log was not matching the " + << "expected results." << std::endl; + testRes->reason << KWHT << "Expecting: " << RESET + << t->error_log + ""; + 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) + { + std::cout << ":test-result: FAIL " << filename + << ":" << t->name << ":" << *count << std::endl; + } + else + { + std::cout << KRED << "failed!" << RESET << std::endl; + } + testRes->reason << "Audit log was not matching the " + << "expected results." << std::endl; + testRes->reason << KWHT << "Expecting: " << RESET + << t->audit_log + ""; + testRes->passed = false; + } + else + { + if (test->m_automake_output) + { + std::cout << ":test-result: PASS " << filename + << ":" << t->name << std::endl; + } + else + { + std::cout << KGRN << "passed!" << RESET << std::endl; + } + testRes->passed = true; } - -after_debug_log: - if (d != NULL) { - r.log_raw_debug_log = d->log_messages(); + if (testRes->passed == false) + { + testRes->reason << std::endl; + testRes->reason << KWHT << "Debug log:" << RESET << std::endl; + testRes->reason << d->log_messages() << std::endl; + testRes->reason << KWHT << "Error log:" << RESET << std::endl; + testRes->reason << context.m_server_log.str() << std::endl; + testRes->reason << KWHT << "Audit log:" << RESET << std::endl; + testRes->reason << getAuditLogContent(modsec_transaction.m_rules->m_auditLog->m_path1) << std::endl; } - delete modsec_transaction; - delete modsec_rules; - delete modsec; - /* delete debug_log; */ + r.log_raw_debug_log = d->log_messages(); res->push_back(testRes); } } - -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ ModSecurityTest test; std::string ver(MODSECURITY_VERSION); @@ -520,13 +507,15 @@ int main(int argc, char **argv) { return 0; #else test.cmd_options(argc, argv); - if (!test.m_automake_output && !test.m_count_all) { + if (!test.m_automake_output && !test.m_count_all) + { std::cout << test.header(); } test.load_tests(); - if (!test.m_automake_output && !test.m_count_all) { + if (!test.m_automake_output && !test.m_count_all) + { std::cout << std::setw(4) << std::right << "# "; std::cout << std::setw(50) << std::left << "File Name"; std::cout << std::setw(70) << std::left << "Test Name"; @@ -541,21 +530,24 @@ int main(int argc, char **argv) { int counter = 0; std::list keyList; - for (std::pair *> a : test) { + for (const auto &a : test) + { keyList.push_back(a.first); } keyList.sort(); - if (test.m_count_all) { + if (test.m_count_all) + { std::cout << std::to_string(keyList.size()) << std::endl; exit(0); } ModSecurityTestResults res; - for (const std::string &a : keyList) { + for (const std::string &a : keyList) + { test_number++; - if ((test.m_test_number == 0) - || (test_number == test.m_test_number)) { + if ((test.m_test_number == 0) || (test_number == test.m_test_number)) + { std::vector *tests = test[a]; perform_unit_test(&test, tests, &res, &counter); } @@ -568,25 +560,31 @@ int main(int argc, char **argv) { int disabled = 0; int skipped = 0; - for (RegressionTestResult *r : res) { - if (r->skipped == true) { + for (RegressionTestResult *r : res) + { + if (r->skipped == true) + { skipped++; } - if (r->disabled == true) { + if (r->disabled == true) + { disabled++; } - if (r->passed == true) { + if (r->passed == true) + { passed++; } - if (!r->passed && !r->skipped && !r->disabled) { - if (!test.m_automake_output) { - std::cout << KRED << "Test failed." << RESET << KWHT \ - << " From: " \ - << RESET << r->test->filename << "." << std::endl; - std::cout << KWHT << "Test name: " << RESET \ - << r->test->name \ - << "." << std::endl; + if (!r->passed && !r->skipped && !r->disabled) + { + if (!test.m_automake_output) + { + std::cout << KRED << "Test failed." << RESET << KWHT + << " From: " + << RESET << r->test->filename << "." << std::endl; + std::cout << KWHT << "Test name: " << RESET + << r->test->name + << "." << std::endl; std::cout << KWHT << "Reason: " << RESET << std::endl; std::cout << r->reason.str() << std::endl; } @@ -595,23 +593,29 @@ int main(int argc, char **argv) { delete r; } - if (!test.m_automake_output) { - std::cout << "Ran a total of: " << std::to_string(failed + passed) \ - << " regression tests - "; - if (failed == 0) { + if (!test.m_automake_output) + { + std::cout << "Ran a total of: " << std::to_string(failed + passed) + << " regression tests - "; + if (failed == 0) + { std::cout << KGRN << "All tests passed. " << RESET; - } else { + } + else + { std::cout << KRED << failed << " failed. " << RESET; } std::cout << KCYN << std::to_string(skipped) << " "; - std::cout << "skipped test(s). " << std::to_string(disabled) << " "; + std::cout << "skipped test(s). " << std::to_string(disabled) << " "; std::cout << "disabled test(s)." << RESET << std::endl; } - for (std::pair *> a : test) { + for (auto a : test) + { std::vector *vec = a.second; - for (int i = 0; i < vec->size(); i++) { + for (int i = 0; i < vec->size(); i++) + { delete vec->at(i); } delete vec;