Adds support to resources on the unit tests

This commit is contained in:
Felipe Zimmerle 2017-02-03 21:47:03 -03:00 committed by Felipe Zimmerle
parent b021c15e4b
commit 158ec7b2e1
No known key found for this signature in database
GPG Key ID: E6DFB08CE8B11277
3 changed files with 52 additions and 5 deletions

View File

@ -82,6 +82,7 @@ using modsecurity::operators::Operator;
using namespace modsecurity::actions::transformations; using namespace modsecurity::actions::transformations;
std::string default_test_path = "test-cases/secrules-language-tests/operators"; std::string default_test_path = "test-cases/secrules-language-tests/operators";
std::list<std::string> resources;
void print_help() { void print_help() {
std::cout << "Use ./unit /path/to/file" << std::endl; std::cout << "Use ./unit /path/to/file" << std::endl;
@ -138,11 +139,25 @@ Transformation* t_instantiate(std::string a) {
void perform_unit_test(ModSecurityTest<UnitTest> *test, UnitTest *t, void perform_unit_test(ModSecurityTest<UnitTest> *test, UnitTest *t,
ModSecurityTestResults<UnitTest>* res) { ModSecurityTestResults<UnitTest>* res) {
std::string error; std::string error;
bool found = true;
if (test->m_automake_output) { if (test->m_automake_output) {
std::cout << ":test-result: "; std::cout << ":test-result: ";
} }
if (t->resource.empty() == false) {
found = (std::find(resources.begin(), resources.end(), t->resource)
!= resources.end());
}
if (!found) {
t->skipped = true;
res->push_back(t);
if (test->m_automake_output) {
std::cout << "SKIP ";
}
}
if (t->type == "op") { if (t->type == "op") {
Operator *op = Operator::instantiate(t->name, t->param); Operator *op = Operator::instantiate(t->name, t->param);
op->init(t->filename, &error); op->init(t->filename, &error);
@ -189,6 +204,13 @@ int main(int argc, char **argv) {
ModSecurityTest<UnitTest> test; ModSecurityTest<UnitTest> test;
ModSecurityTestResults<UnitTest> results; ModSecurityTestResults<UnitTest> results;
#ifdef WITH_GEOIP
resources.push_back("geoip");
#endif
#ifdef WITH_CURL
resources.push_back("curl");
#endif
test.cmd_options(argc, argv); test.cmd_options(argc, argv);
if (!test.m_automake_output) { if (!test.m_automake_output) {
std::cout << test.header(); std::cout << test.header();
@ -212,12 +234,23 @@ int main(int argc, char **argv) {
perform_unit_test(&test, t, &r); perform_unit_test(&test, t, &r);
if (!test.m_automake_output) { if (!test.m_automake_output) {
int skp = 0;
if (r.size() == 0) { if (r.size() == 0) {
std::cout << KGRN << r.size() << " tests failed."; std::cout << KGRN << "0 tests failed.";
} else { } else {
std::cout << KRED << r.size() << " tests failed."; for (auto &i : r) {
if (i->skipped == true) {
skp++;
}
}
std::cout << KRED << r.size()-skp << " tests failed.";
} }
std::cout << RESET << std::endl; std::cout << RESET;
if (skp > 0) {
std::cout << " " << std::to_string(skp) << " ";
std::cout << "skipped.";
}
std::cout << std::endl;
} }
results.insert(results.end(), r.begin(), r.end()); results.insert(results.end(), r.begin(), r.end());
@ -239,8 +272,18 @@ int main(int argc, char **argv) {
if (results.size() == 0) { if (results.size() == 0) {
std::cout << KGRN << "All tests passed" << RESET << std::endl; std::cout << KGRN << "All tests passed" << RESET << std::endl;
} else { } else {
std::cout << KRED << results.size() << " failed."; int skp = 0;
for (auto &i : results) {
if (i->skipped == true) {
skp++;
}
}
std::cout << KRED << results.size()-skp << " failed.";
std::cout << RESET << std::endl; std::cout << RESET << std::endl;
if (skp > 0) {
std::cout << " " << std::to_string(skp) << " ";
std::cout << "skipped.";
}
} }
} }

View File

@ -129,12 +129,14 @@ UnitTest *UnitTest::from_yajl_node(yajl_val &node) {
const char *key = node->u.object.keys[ i ]; const char *key = node->u.object.keys[ i ];
yajl_val val = node->u.object.values[ i ]; yajl_val val = node->u.object.values[ i ];
u->skipped = false;
if (strcmp(key, "param") == 0) { if (strcmp(key, "param") == 0) {
u->param = YAJL_GET_STRING(val); u->param = YAJL_GET_STRING(val);
} else if (strcmp(key, "input") == 0) { } else if (strcmp(key, "input") == 0) {
u->input = YAJL_GET_STRING(val); u->input = YAJL_GET_STRING(val);
json2bin(&u->input); json2bin(&u->input);
} else if (strcmp(key, "resource") == 0) {
u->resource = YAJL_GET_STRING(val);
} else if (strcmp(key, "name") == 0) { } else if (strcmp(key, "name") == 0) {
u->name = YAJL_GET_STRING(val); u->name = YAJL_GET_STRING(val);
} else if (strcmp(key, "type") == 0) { } else if (strcmp(key, "type") == 0) {

View File

@ -33,12 +33,14 @@ class UnitTest {
std::string param; std::string param;
std::string input; std::string input;
std::string resource;
std::string name; std::string name;
std::string type; std::string type;
std::string filename; std::string filename;
std::string output; std::string output;
int ret; int ret;
int obtained; int obtained;
int skipped;
std::string obtainedOutput; std::string obtainedOutput;
}; };