From 18c862a84a18c0870b232a98b6a566069cd6795e Mon Sep 17 00:00:00 2001 From: Felipe Zimmerle Date: Fri, 20 Nov 2015 09:54:52 -0300 Subject: [PATCH] Adds the concept of `resources' to the regression test utility If a given resource is not available the test is skipped. Useful to test operators that depends on 3rd party libraries that may not be available, for instance: GeoIP. --- test/regression/regression.cc | 43 ++++++++++++++++++-- test/regression/regression_test.cc | 3 ++ test/regression/regression_test.h | 7 ++++ test/test-cases/regression/variable-GEO.json | 11 +++++ 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/test/regression/regression.cc b/test/regression/regression.cc index a7187615..5ef9450b 100644 --- a/test/regression/regression.cc +++ b/test/regression/regression.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include "modsecurity/modsecurity.h" #include "modsecurity/rules.h" @@ -41,6 +42,7 @@ using ModSecurity::Utils::SMatch; using ModSecurity::Utils::Regex; std::string default_test_path = "test-cases/regression"; +std::list resources; void print_help() { std::cout << "Use ./regression-tests /path/to/file" << std::endl; @@ -108,6 +110,24 @@ void perform_unit_test(std::vector *tests, modsec->setServerLogCb(logCb); modsec_rules = new ModSecurity::Rules(debug_log); + bool found = true; + if (t->resource.empty() == false) { + found = (std::find(resources.begin(), resources.end(), t->resource) + != resources.end()); + } + + 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; + std::cout << KCYN << "skipped!" << RESET << std::endl; + res->push_back(testRes); + continue; + } + if (modsec_rules->load(t->rules.c_str(), filename) < 0) { if (t->parser_error.empty() == true) { testRes->reason << KRED << "parse failed." << RESET \ @@ -263,6 +283,10 @@ int main(int argc, char **argv) { ModSecurityTest test; ModSecurityTestResults results; +#ifdef WITH_GEOIP + resources.push_back("geoip"); +#endif + #ifdef NO_LOGS std::cout << "Test utility cannot work without logging support." \ << std::endl; @@ -300,10 +324,14 @@ int main(int argc, char **argv) { int passed = 0; int failed = 0; + int skipped = 0; for (RegressionTestResult *r : res) { - if (r->passed) { + if (r->skipped == true) { + skipped++; + } + if (r->passed == true && r->skipped == false) { passed++; - } else { + } else if (r->skipped == false) { std::cout << KRED << "Test failed." << RESET << KWHT << " From: " \ << RESET << r->test->filename << "." << std::endl; std::cout << KWHT << "Test name: " << RESET << r->test->name \ @@ -317,9 +345,16 @@ int main(int argc, char **argv) { std::cout << "Ran a total of: " << std::to_string(failed + passed) \ << " regression tests - "; if (failed == 0) { - std::cout << KGRN << "All tests passed" << RESET << std::endl; + std::cout << KGRN << "All tests passed." << RESET; } else { - std::cout << KRED << failed << " failed." << RESET << std::endl; + std::cout << KRED << failed << " failed." << RESET; + } + + if (skipped > 0) { + std::cout << KCYN << " " << std::to_string(skipped) << " "; + std::cout << "skipped tests." << RESET << std::endl; + } else { + std::cout << std::endl; } for (std::pair *> a : test) { diff --git a/test/regression/regression_test.cc b/test/regression/regression_test.cc index 43eac55a..4e175599 100644 --- a/test/regression/regression_test.cc +++ b/test/regression/regression_test.cc @@ -104,6 +104,9 @@ RegressionTest *RegressionTest::from_yajl_node(const yajl_val &node) { if (strcmp(key, "url") == 0) { u->url = YAJL_GET_STRING(val); } + if (strcmp(key, "resource") == 0) { + u->resource = YAJL_GET_STRING(val); + } if (strcmp(key, "github_issue") == 0) { u->github_issue = YAJL_GET_INTEGER(val); } diff --git a/test/regression/regression_test.h b/test/regression/regression_test.h index 27204a45..75b9a00a 100644 --- a/test/regression/regression_test.h +++ b/test/regression/regression_test.h @@ -62,6 +62,7 @@ class RegressionTest { std::string method; std::string httpVersion; std::string uri; + std::string resource; static inline std::string yajl_array_to_str(const yajl_val &node); static inline std::vector yajl_array_to_vec_str( @@ -76,7 +77,13 @@ class RegressionTest { class RegressionTestResult { public: + RegressionTestResult() : + passed(false), + skipped(false), + test(NULL) { } + bool passed; + bool skipped; RegressionTest *test; std::stringstream reason; }; diff --git a/test/test-cases/regression/variable-GEO.json b/test/test-cases/regression/variable-GEO.json index 88684b68..2516bbb9 100644 --- a/test/test-cases/regression/variable-GEO.json +++ b/test/test-cases/regression/variable-GEO.json @@ -2,6 +2,7 @@ { "enabled":1, "version_min":300000, + "resource":"geoip", "title":"Testing Variables :: GEO:LONGITUDE", "client":{ "ip":"64.17.254.216", @@ -45,6 +46,7 @@ { "enabled":1, "version_min":300000, + "resource":"geoip", "title":"Testing Variables :: GEO:COUNTRY_NAME", "client":{ "ip":"64.17.254.216", @@ -88,6 +90,7 @@ { "enabled":1, "version_min":300000, + "resource":"geoip", "title":"Testing Variables :: GEO:LATITUDE", "client":{ "ip":"64.17.254.216", @@ -131,6 +134,7 @@ { "enabled":1, "version_min":300000, + "resource":"geoip", "title":"Testing Variables :: GEO:COUNTRY_CODE3", "client":{ "ip":"64.17.254.216", @@ -174,6 +178,7 @@ { "enabled":1, "version_min":300000, + "resource":"geoip", "title":"Testing Variables :: GEO:COUNTRY_CODE", "client":{ "ip":"64.17.254.216", @@ -217,6 +222,7 @@ { "enabled":1, "version_min":300000, + "resource":"geoip", "title":"Testing Variables :: GEO:COUNTRY_CONTINENT", "client":{ "ip":"64.17.254.216", @@ -260,6 +266,7 @@ { "enabled":1, "version_min":300000, + "resource":"geoip", "title":"Testing Variables :: GEO:AREA_CODE", "client":{ "ip":"64.17.254.216", @@ -303,6 +310,7 @@ { "enabled":1, "version_min":300000, + "resource":"geoip", "title":"Testing Variables :: GEO:DMA_CODE", "client":{ "ip":"64.17.254.216", @@ -346,6 +354,7 @@ { "enabled":1, "version_min":300000, + "resource":"geoip", "title":"Testing Variables :: GEO:POSTAL_CODE", "client":{ "ip":"64.17.254.216", @@ -389,6 +398,7 @@ { "enabled":1, "version_min":300000, + "resource":"geoip", "title":"Testing Variables :: GEO:REGION", "client":{ "ip":"64.17.254.216", @@ -432,6 +442,7 @@ { "enabled":1, "version_min":300000, + "resource":"geoip", "title":"Testing Variables :: GEO:CITY", "client":{ "ip":"64.17.254.216",