diff --git a/build/win32/CMakeLists.txt b/build/win32/CMakeLists.txt index cebf4c9b..fbf39f08 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 4d4396ae..2c1ba02a 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 00000000..7f69b58c --- /dev/null +++ b/test/common/custom_debug_log.cc @@ -0,0 +1,53 @@ +/* + * 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 00000000..46461cb2 --- /dev/null +++ b/test/common/modsecurity_test_context.h @@ -0,0 +1,42 @@ +#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 1e7de0e0..00000000 --- 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 a6ede61c..5992d2cf 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; @@ -110,24 +110,11 @@ 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; ModSecurityTestResults r; - std::stringstream serverLog; RegressionTestResult *testRes = new RegressionTestResult(); testRes->test = t; @@ -169,11 +156,8 @@ 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_test::ModSecurityTestContext context("ModSecurity-regression v0.0.1-alpha" \ " (ModSecurity regression test utility)"); - modsec->setServerLogCb(logCb); - modsec_rules = new modsecurity::RulesSet(debug_log); bool found = true; if (t->resource.empty() == false) { @@ -196,15 +180,11 @@ void perform_unit_test(ModSecurityTest *test, } 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) { /* @@ -219,21 +199,17 @@ void perform_unit_test(ModSecurityTest *test, } testRes->reason << KRED << "parse failed." << RESET \ << std::endl; - testRes->reason << modsec_rules->getParserError() \ + 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(); + const auto s = context.m_modsec_rules.getParserError(); if (regex_search(s, &match, re)) { if (test->m_automake_output) { @@ -247,10 +223,6 @@ void perform_unit_test(ModSecurityTest *test, testRes->passed = true; res->push_back(testRes); - delete modsec_transaction; - delete modsec_rules; - delete modsec; - continue; } else { /* Parser error was expected, but with a different content */ @@ -271,10 +243,6 @@ void perform_unit_test(ModSecurityTest *test, testRes->passed = false; res->push_back(testRes); - delete modsec_transaction; - delete modsec_rules; - delete modsec; - continue; } } else { @@ -293,190 +261,136 @@ void perform_unit_test(ModSecurityTest *test, 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(), + 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(), + 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(), + 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(), + for (const auto &headers : t->response_headers) { + modsec_transaction.addResponseHeader(headers.first.c_str(), headers.second.c_str()); } - modsec_transaction->processResponseHeaders(r.status, + modsec_transaction.processResponseHeaders(r.status, t->response_protocol); - actions(&r, modsec_transaction, &serverLog); -#if 0 - if (r.status != 200) { - goto end; - } -#endif + 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; + if (!d->contains(t->debug_log)) { + 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: PASS " << filename \ - << ":" << t->name << std::endl; - } else { - std::cout << KGRN << "passed!" << RESET << std::endl; - } - testRes->passed = true; - goto after_debug_log; + std::cout << KRED << "failed!" << RESET << 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; + 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); @@ -541,7 +455,7 @@ 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(); @@ -554,7 +468,7 @@ int main(int argc, char **argv) { ModSecurityTestResults res; for (const std::string &a : keyList) { test_number++; - if ((test.m_test_number == 0) + if ((test.m_test_number == 0) || (test_number == test.m_test_number)) { std::vector *tests = test[a]; perform_unit_test(&test, tests, &res, &counter); @@ -605,11 +519,11 @@ int main(int argc, char **argv) { } 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++) { delete vec->at(i);