Adds support for jsDecode transformation

This commit is contained in:
Felipe Zimmerle
2015-08-05 14:41:43 -03:00
parent 4f47651a6f
commit 391002c665
10 changed files with 198 additions and 30 deletions

View File

@@ -22,6 +22,7 @@
#include "modsecurity/modsecurity.h"
#include "modsecurity/rules.h"
#include "operators/operator.h"
#include "actions/transformations/transformation.h"
#include "common/modsecurity_test.h"
#include "common/modsecurity_test_results.h"
@@ -32,6 +33,8 @@
using modsecurity_test::UnitTest;
using modsecurity_test::ModSecurityTest;
using modsecurity_test::ModSecurityTestResults;
using ModSecurity::actions::transformations::Transformation;
using ModSecurity::operators::Operator;
std::string default_test_path = "test-cases/secrules-language-tests/operators";
@@ -55,17 +58,35 @@ void print_help() {
void perform_unit_test(UnitTest *t, ModSecurityTestResults<UnitTest>* res) {
const char *error = NULL;
ModSecurity::operators::Operator *op =
ModSecurity::operators::Operator::instantiate("\"@" + t->name + \
" " + t->param + "\"");
op->init(&error);
int ret = 0;
int ret = op->evaluate(NULL, t->input);
if (ret != t->ret) {
t->obtained = ret;
res->push_back(t);
if (t->type == "op") {
Operator *op = Operator::instantiate("\"@" + t->name + \
" " + t->param + "\"");
op->init(&error);
ret = op->evaluate(NULL, t->input);
if (ret != t->ret) {
t->obtained = ret;
res->push_back(t);
}
delete op;
} else if (t->type == "tfn") {
Transformation *tfn = Transformation::instantiate("t:" + t->name);
std::string ret = tfn->evaluate(t->input, NULL);
t->obtained = 1;
if (ret != t->output) {
std::cout << "ret: !" << ret << "!" << std::endl;
std::cout << "obt: !" << t->output << "!" << std::endl;
t->obtainedOutput = ret;
res->push_back(t);
}
delete tfn;
} else {
std::cerr << "Failed. Test type is unknown: << " << t->type;
std::cerr << std::endl;
}
delete op;
}

View File

@@ -35,9 +35,16 @@ std::string UnitTest::print() {
i << " \"name\": \"" << this->name << "\"" << std::endl;
i << " \"input\": \"" << this->input << "\"" << std::endl;
i << " \"param\": \"" << this->param << "\"" << std::endl;
i << " \"output\": \"" << this->output << "\"" << std::endl;
i << "}" << std::endl;
i << "Expecting: " << this->ret << " - operator returned: ";
i << this->obtained << std::endl;
if (this->ret != this->obtained) {
i << "Expecting: " << this->ret << " - operator returned: ";
i << this->obtained << std::endl;
}
if (this->output != this->obtainedOutput) {
i << "Expecting: " << this->output << " - operator returned: ";
i << this->obtainedOutput << std::endl;
}
return i.str();
}
@@ -62,6 +69,8 @@ UnitTest *UnitTest::from_yajl_node(yajl_val &node) {
u->type = YAJL_GET_STRING(val);
} else if (strcmp(key, "ret") == 0) {
u->ret = YAJL_GET_INTEGER(val);
} else if (strcmp(key, "output") == 0) {
u->output = YAJL_GET_STRING(val);
}
}

View File

@@ -36,8 +36,10 @@ class UnitTest {
std::string name;
std::string type;
std::string filename;
std::string output;
int ret;
int obtained;
std::string obtainedOutput;
};
} // namespace modsecurity_test