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.
This commit is contained in:
Felipe Zimmerle 2015-11-20 09:54:52 -03:00
parent 09a958544d
commit 18c862a84a
4 changed files with 60 additions and 4 deletions

View File

@ -19,6 +19,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <list> #include <list>
#include <algorithm>
#include "modsecurity/modsecurity.h" #include "modsecurity/modsecurity.h"
#include "modsecurity/rules.h" #include "modsecurity/rules.h"
@ -41,6 +42,7 @@ using ModSecurity::Utils::SMatch;
using ModSecurity::Utils::Regex; using ModSecurity::Utils::Regex;
std::string default_test_path = "test-cases/regression"; std::string default_test_path = "test-cases/regression";
std::list<std::string> resources;
void print_help() { void print_help() {
std::cout << "Use ./regression-tests /path/to/file" << std::endl; std::cout << "Use ./regression-tests /path/to/file" << std::endl;
@ -108,6 +110,24 @@ void perform_unit_test(std::vector<RegressionTest *> *tests,
modsec->setServerLogCb(logCb); modsec->setServerLogCb(logCb);
modsec_rules = new ModSecurity::Rules(debug_log); 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 (modsec_rules->load(t->rules.c_str(), filename) < 0) {
if (t->parser_error.empty() == true) { if (t->parser_error.empty() == true) {
testRes->reason << KRED << "parse failed." << RESET \ testRes->reason << KRED << "parse failed." << RESET \
@ -263,6 +283,10 @@ int main(int argc, char **argv) {
ModSecurityTest<RegressionTest> test; ModSecurityTest<RegressionTest> test;
ModSecurityTestResults<RegressionTest> results; ModSecurityTestResults<RegressionTest> results;
#ifdef WITH_GEOIP
resources.push_back("geoip");
#endif
#ifdef NO_LOGS #ifdef NO_LOGS
std::cout << "Test utility cannot work without logging support." \ std::cout << "Test utility cannot work without logging support." \
<< std::endl; << std::endl;
@ -300,10 +324,14 @@ int main(int argc, char **argv) {
int passed = 0; int passed = 0;
int failed = 0; int failed = 0;
int skipped = 0;
for (RegressionTestResult *r : res) { for (RegressionTestResult *r : res) {
if (r->passed) { if (r->skipped == true) {
skipped++;
}
if (r->passed == true && r->skipped == false) {
passed++; passed++;
} else { } else if (r->skipped == false) {
std::cout << KRED << "Test failed." << RESET << KWHT << " From: " \ std::cout << KRED << "Test failed." << RESET << KWHT << " From: " \
<< RESET << r->test->filename << "." << std::endl; << RESET << r->test->filename << "." << std::endl;
std::cout << KWHT << "Test name: " << RESET << r->test->name \ 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) \ std::cout << "Ran a total of: " << std::to_string(failed + passed) \
<< " regression tests - "; << " regression tests - ";
if (failed == 0) { if (failed == 0) {
std::cout << KGRN << "All tests passed" << RESET << std::endl; std::cout << KGRN << "All tests passed." << RESET;
} else { } 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<std::string, std::vector<RegressionTest *> *> a : test) { for (std::pair<std::string, std::vector<RegressionTest *> *> a : test) {

View File

@ -104,6 +104,9 @@ RegressionTest *RegressionTest::from_yajl_node(const yajl_val &node) {
if (strcmp(key, "url") == 0) { if (strcmp(key, "url") == 0) {
u->url = YAJL_GET_STRING(val); u->url = YAJL_GET_STRING(val);
} }
if (strcmp(key, "resource") == 0) {
u->resource = YAJL_GET_STRING(val);
}
if (strcmp(key, "github_issue") == 0) { if (strcmp(key, "github_issue") == 0) {
u->github_issue = YAJL_GET_INTEGER(val); u->github_issue = YAJL_GET_INTEGER(val);
} }

View File

@ -62,6 +62,7 @@ class RegressionTest {
std::string method; std::string method;
std::string httpVersion; std::string httpVersion;
std::string uri; std::string uri;
std::string resource;
static inline std::string yajl_array_to_str(const yajl_val &node); static inline std::string yajl_array_to_str(const yajl_val &node);
static inline std::vector<std::string> yajl_array_to_vec_str( static inline std::vector<std::string> yajl_array_to_vec_str(
@ -76,7 +77,13 @@ class RegressionTest {
class RegressionTestResult { class RegressionTestResult {
public: public:
RegressionTestResult() :
passed(false),
skipped(false),
test(NULL) { }
bool passed; bool passed;
bool skipped;
RegressionTest *test; RegressionTest *test;
std::stringstream reason; std::stringstream reason;
}; };

View File

@ -2,6 +2,7 @@
{ {
"enabled":1, "enabled":1,
"version_min":300000, "version_min":300000,
"resource":"geoip",
"title":"Testing Variables :: GEO:LONGITUDE", "title":"Testing Variables :: GEO:LONGITUDE",
"client":{ "client":{
"ip":"64.17.254.216", "ip":"64.17.254.216",
@ -45,6 +46,7 @@
{ {
"enabled":1, "enabled":1,
"version_min":300000, "version_min":300000,
"resource":"geoip",
"title":"Testing Variables :: GEO:COUNTRY_NAME", "title":"Testing Variables :: GEO:COUNTRY_NAME",
"client":{ "client":{
"ip":"64.17.254.216", "ip":"64.17.254.216",
@ -88,6 +90,7 @@
{ {
"enabled":1, "enabled":1,
"version_min":300000, "version_min":300000,
"resource":"geoip",
"title":"Testing Variables :: GEO:LATITUDE", "title":"Testing Variables :: GEO:LATITUDE",
"client":{ "client":{
"ip":"64.17.254.216", "ip":"64.17.254.216",
@ -131,6 +134,7 @@
{ {
"enabled":1, "enabled":1,
"version_min":300000, "version_min":300000,
"resource":"geoip",
"title":"Testing Variables :: GEO:COUNTRY_CODE3", "title":"Testing Variables :: GEO:COUNTRY_CODE3",
"client":{ "client":{
"ip":"64.17.254.216", "ip":"64.17.254.216",
@ -174,6 +178,7 @@
{ {
"enabled":1, "enabled":1,
"version_min":300000, "version_min":300000,
"resource":"geoip",
"title":"Testing Variables :: GEO:COUNTRY_CODE", "title":"Testing Variables :: GEO:COUNTRY_CODE",
"client":{ "client":{
"ip":"64.17.254.216", "ip":"64.17.254.216",
@ -217,6 +222,7 @@
{ {
"enabled":1, "enabled":1,
"version_min":300000, "version_min":300000,
"resource":"geoip",
"title":"Testing Variables :: GEO:COUNTRY_CONTINENT", "title":"Testing Variables :: GEO:COUNTRY_CONTINENT",
"client":{ "client":{
"ip":"64.17.254.216", "ip":"64.17.254.216",
@ -260,6 +266,7 @@
{ {
"enabled":1, "enabled":1,
"version_min":300000, "version_min":300000,
"resource":"geoip",
"title":"Testing Variables :: GEO:AREA_CODE", "title":"Testing Variables :: GEO:AREA_CODE",
"client":{ "client":{
"ip":"64.17.254.216", "ip":"64.17.254.216",
@ -303,6 +310,7 @@
{ {
"enabled":1, "enabled":1,
"version_min":300000, "version_min":300000,
"resource":"geoip",
"title":"Testing Variables :: GEO:DMA_CODE", "title":"Testing Variables :: GEO:DMA_CODE",
"client":{ "client":{
"ip":"64.17.254.216", "ip":"64.17.254.216",
@ -346,6 +354,7 @@
{ {
"enabled":1, "enabled":1,
"version_min":300000, "version_min":300000,
"resource":"geoip",
"title":"Testing Variables :: GEO:POSTAL_CODE", "title":"Testing Variables :: GEO:POSTAL_CODE",
"client":{ "client":{
"ip":"64.17.254.216", "ip":"64.17.254.216",
@ -389,6 +398,7 @@
{ {
"enabled":1, "enabled":1,
"version_min":300000, "version_min":300000,
"resource":"geoip",
"title":"Testing Variables :: GEO:REGION", "title":"Testing Variables :: GEO:REGION",
"client":{ "client":{
"ip":"64.17.254.216", "ip":"64.17.254.216",
@ -432,6 +442,7 @@
{ {
"enabled":1, "enabled":1,
"version_min":300000, "version_min":300000,
"resource":"geoip",
"title":"Testing Variables :: GEO:CITY", "title":"Testing Variables :: GEO:CITY",
"client":{ "client":{
"ip":"64.17.254.216", "ip":"64.17.254.216",