Returns elegant errors if rules load operation failed

This commit is contained in:
Felipe Zimmerle
2015-07-25 03:04:57 -03:00
parent b8f7fb441d
commit 7ba5c76c78
6 changed files with 117 additions and 76 deletions

View File

@@ -19,6 +19,7 @@
#include <iostream>
#include <string>
#include <list>
#include <regex>
#include "modsecurity/modsecurity.h"
#include "modsecurity/rules.h"
@@ -89,14 +90,34 @@ void perform_unit_test(std::vector<RegressionTest *> *tests,
" (ModSecurity regression test utility)");
modsec_rules = new ModSecurity::Rules(debug_log);
if (modsec_rules->load(t->rules.c_str()) == false) {
std::cerr << "parse failed." << std::endl;
std::cout << std::endl;
std::cout << "-------------" << std::endl;
std::cout << modsec_rules->getParserError() << std::endl;
std::cout << "-------------" << std::endl;
return;
if (modsec_rules->load(t->rules.c_str(), filename) == false) {
if (t->parser_error.empty() == true) {
std::cerr << "parse failed." << std::endl;
std::cout << modsec_rules->getParserError() << std::endl;
return;
}
std::regex re(t->parser_error);
std::smatch match;
std::string s = modsec_rules->getParserError();
if (std::regex_search(s, match, re) && match.size() >= 1) {
std::cout << "passed!" << std::endl;
return;
} else {
std::cout << "Expected a parser error." << std::endl;
std::cout << "Expected: " << t->parser_error << std::endl;
std::cout << "Produced: " << s << std::endl;
return;
}
} else {
if (t->parser_error.empty() == false) {
std::cout << "Expected a parser error." << std::endl;
std::cout << "Expected: " << t->parser_error << std::endl;
}
}
modsec_assay = new ModSecurity::Assay(modsec, modsec_rules);
modsec_assay->processConnection(t->clientIp.c_str(),
@@ -219,6 +240,7 @@ int main(int argc, char **argv) {
keyList.push_back(a.first);
}
keyList.sort();
for (std::string a : keyList) {
std::vector<RegressionTest *> *tests = test[a];
ModSecurityTestResults<RegressionTest> res;
@@ -227,7 +249,6 @@ int main(int argc, char **argv) {
test_log << res.log_raw_debug_log;
}
test_log.close();
return 0;

View File

@@ -188,6 +188,9 @@ RegressionTest *RegressionTest::from_yajl_node(const yajl_val &node) {
if (strcmp(key2, "redirect_url") == 0) {
u->redirect_url = YAJL_GET_STRING(val2);
}
if (strcmp(key2, "parser_error") == 0) {
u->parser_error = YAJL_GET_STRING(val2);
}
}
}
if (strcmp(key, "rules") == 0) {

View File

@@ -50,6 +50,7 @@ class RegressionTest {
std::string audit_log;
std::string debug_log;
std::string error_log;
std::string parser_error;
std::string clientIp;
std::string serverIp;