Very first commit: libmodsecurity

Check the README.md file for further information about the libmodsecurity.
This commit is contained in:
Felipe Zimmerle
2015-06-26 14:35:15 -03:00
parent 33cbe0452a
commit 95cb4c56ab
153 changed files with 12862 additions and 0 deletions

116
test/unit/unit.cc Normal file
View File

@@ -0,0 +1,116 @@
/**
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address security@modsecurity.org.
*
*/
#include <string.h>
#include <ctime>
#include <iostream>
#include <string>
#include "modsecurity/modsecurity.h"
#include "modsecurity/rules.h"
#include "operators/operator.h"
#include "common/modsecurity_test.h"
#include "common/modsecurity_test_results.h"
#include "common/colors.h"
#include "unit/unit_test.h"
using modsecurity_test::UnitTest;
using modsecurity_test::ModSecurityTest;
using modsecurity_test::ModSecurityTestResults;
std::string default_test_path = "test-cases/secrules-language-tests/operators";
void print_help() {
#ifdef HAS_GETOPT
std::cout << "Use ./unit [--no-color] -t /path/to/test/files ";
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << " -h\t\tThis help message" << std::endl;
std::cout << " -v\t\tVerbose" << std::endl;
std::cout << " -c\t\tNo color" << std::endl;
std::cout << " -t\t\tPath to test cases" << std::endl;
#else
std::cout << "Use ./unit /path/to/file" << std::endl;
#endif
std::cout << std::endl;
std::cout << std::endl;
}
void perform_unit_test(UnitTest *t, ModSecurityTestResults<UnitTest>* res) {
ModSecurity::operators::Operator *op =
ModSecurity::operators::Operator::instantiate("\"@" + t->name + \
" " + t->param + "\"");
int ret = op->evaluate(NULL, t->input);
if (ret != t->ret) {
t->obtained = ret;
res->push_back(t);
}
}
int main(int argc, char **argv) {
int total = 0;
ModSecurityTest<UnitTest> test;
ModSecurityTestResults<UnitTest> results;
test.cmd_options(argc, argv);
std::cout << test.header();
test.load_tests();
for (std::pair<std::string, std::vector<UnitTest *> *> a : test) {
std::vector<UnitTest *> *tests = a.second;
total += tests->size();
for (UnitTest *t : *tests) {
ModSecurityTestResults<UnitTest> r;
std::cout << " " << a.first << "...\t";
perform_unit_test(t, &r);
if (r.size() == 0) {
std::cout << KGRN << r.size() << " tests failed.";
} else {
std::cout << KRED << r.size() << " tests failed.";
}
std::cout << RESET << std::endl;
results.insert(results.end(), r.begin(), r.end());
}
}
std::cout << "Total >> " << total << std::endl;
for (UnitTest *t : results) {
std::cout << t->print() << std::endl;
}
std::cout << std::endl;
std::cout << "Ran a total of: " << total << " unit tests - ";
if (results.size() == 0) {
std::cout << KGRN << "All tests passed" << RESET << std::endl;
} else {
std::cout << KRED << results.size() << " failed." << RESET << std::endl;
}
}

71
test/unit/unit_test.cc Normal file
View File

@@ -0,0 +1,71 @@
/**
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address security@modsecurity.org.
*
*/
#include "unit/unit_test.h"
#include <string.h>
#include <sstream>
#include <string>
#include "common/colors.h"
namespace modsecurity_test {
std::string UnitTest::print() {
std::stringstream i;
i << KRED << "Test failed." << RESET;
i << " From: " << this->filename << std::endl;
i << "{" << std::endl;
i << " \"ret\": \"" << this->ret << "\"" << std::endl;
i << " \"type\": \"" << this->type << "\"" << std::endl;
i << " \"name\": \"" << this->name << "\"" << std::endl;
i << " \"input\": \"" << this->input << "\"" << std::endl;
i << " \"param\": \"" << this->param << "\"" << std::endl;
i << "}" << std::endl;
i << "Expecting: " << this->ret << " - operator returned: ";
i << this->obtained << std::endl;
return i.str();
}
UnitTest *UnitTest::from_yajl_node(yajl_val &node) {
size_t num_tests = node->u.object.len;
UnitTest *u = new UnitTest();
for (int i = 0; i < num_tests; i++) {
const char *key = node->u.object.keys[ i ];
yajl_val val = node->u.object.values[ i ];
if (strcmp(key, "param") == 0) {
u->param = YAJL_GET_STRING(val);
} else if (strcmp(key, "input") == 0) {
u->input = YAJL_GET_STRING(val);
} else if (strcmp(key, "name") == 0) {
u->name = YAJL_GET_STRING(val);
} else if (strcmp(key, "type") == 0) {
u->type = YAJL_GET_STRING(val);
} else if (strcmp(key, "ret") == 0) {
u->ret = YAJL_GET_INTEGER(val);
}
}
return u;
}
} // namespace modsecurity_test

45
test/unit/unit_test.h Normal file
View File

@@ -0,0 +1,45 @@
/**
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address security@modsecurity.org.
*
*/
#include <yajl/yajl_tree.h>
#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
#ifndef TEST_UNIT_UNIT_TEST_H_
#define TEST_UNIT_UNIT_TEST_H_
namespace modsecurity_test {
class UnitTest {
public:
static UnitTest *from_yajl_node(yajl_val &);
std::string print();
std::string param;
std::string input;
std::string name;
std::string type;
std::string filename;
int ret;
int obtained;
};
} // namespace modsecurity_test
#endif // TEST_UNIT_UNIT_TEST_H_