mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-14 05:45:59 +03:00
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:
parent
09a958544d
commit
18c862a84a
@ -19,6 +19,7 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
|
||||
#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<std::string> resources;
|
||||
|
||||
void print_help() {
|
||||
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_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<RegressionTest> test;
|
||||
ModSecurityTestResults<RegressionTest> 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<std::string, std::vector<RegressionTest *> *> a : test) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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<std::string> 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;
|
||||
};
|
||||
|
@ -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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user