mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-13 21:36:00 +03:00
Adds support to resources on the unit tests
This commit is contained in:
parent
b021c15e4b
commit
158ec7b2e1
@ -82,6 +82,7 @@ using modsecurity::operators::Operator;
|
||||
using namespace modsecurity::actions::transformations;
|
||||
|
||||
std::string default_test_path = "test-cases/secrules-language-tests/operators";
|
||||
std::list<std::string> resources;
|
||||
|
||||
void print_help() {
|
||||
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,
|
||||
ModSecurityTestResults<UnitTest>* res) {
|
||||
std::string error;
|
||||
bool found = true;
|
||||
|
||||
if (test->m_automake_output) {
|
||||
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") {
|
||||
Operator *op = Operator::instantiate(t->name, t->param);
|
||||
op->init(t->filename, &error);
|
||||
@ -189,6 +204,13 @@ int main(int argc, char **argv) {
|
||||
ModSecurityTest<UnitTest> test;
|
||||
ModSecurityTestResults<UnitTest> results;
|
||||
|
||||
#ifdef WITH_GEOIP
|
||||
resources.push_back("geoip");
|
||||
#endif
|
||||
#ifdef WITH_CURL
|
||||
resources.push_back("curl");
|
||||
#endif
|
||||
|
||||
test.cmd_options(argc, argv);
|
||||
if (!test.m_automake_output) {
|
||||
std::cout << test.header();
|
||||
@ -212,12 +234,23 @@ int main(int argc, char **argv) {
|
||||
perform_unit_test(&test, t, &r);
|
||||
|
||||
if (!test.m_automake_output) {
|
||||
int skp = 0;
|
||||
if (r.size() == 0) {
|
||||
std::cout << KGRN << r.size() << " tests failed.";
|
||||
std::cout << KGRN << "0 tests failed.";
|
||||
} else {
|
||||
std::cout << KRED << r.size() << " tests failed.";
|
||||
for (auto &i : r) {
|
||||
if (i->skipped == true) {
|
||||
skp++;
|
||||
}
|
||||
std::cout << RESET << std::endl;
|
||||
}
|
||||
std::cout << KRED << r.size()-skp << " tests failed.";
|
||||
}
|
||||
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());
|
||||
@ -239,8 +272,18 @@ int main(int argc, char **argv) {
|
||||
if (results.size() == 0) {
|
||||
std::cout << KGRN << "All tests passed" << RESET << std::endl;
|
||||
} 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;
|
||||
if (skp > 0) {
|
||||
std::cout << " " << std::to_string(skp) << " ";
|
||||
std::cout << "skipped.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -129,12 +129,14 @@ UnitTest *UnitTest::from_yajl_node(yajl_val &node) {
|
||||
const char *key = node->u.object.keys[ i ];
|
||||
yajl_val val = node->u.object.values[ i ];
|
||||
|
||||
|
||||
u->skipped = false;
|
||||
if (strcmp(key, "param") == 0) {
|
||||
u->param = YAJL_GET_STRING(val);
|
||||
} else if (strcmp(key, "input") == 0) {
|
||||
u->input = YAJL_GET_STRING(val);
|
||||
json2bin(&u->input);
|
||||
} else if (strcmp(key, "resource") == 0) {
|
||||
u->resource = YAJL_GET_STRING(val);
|
||||
} else if (strcmp(key, "name") == 0) {
|
||||
u->name = YAJL_GET_STRING(val);
|
||||
} else if (strcmp(key, "type") == 0) {
|
||||
|
@ -33,12 +33,14 @@ class UnitTest {
|
||||
|
||||
std::string param;
|
||||
std::string input;
|
||||
std::string resource;
|
||||
std::string name;
|
||||
std::string type;
|
||||
std::string filename;
|
||||
std::string output;
|
||||
int ret;
|
||||
int obtained;
|
||||
int skipped;
|
||||
std::string obtainedOutput;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user