test: Fix optimization test

This commit is contained in:
Felipe Zimmerle 2021-05-03 19:37:10 -03:00
parent 1376882f7d
commit 1e2ccc1578

View File

@ -27,6 +27,9 @@
#include "src/parser/driver.h" #include "src/parser/driver.h"
#include "src/utils/https_client.h" #include "src/utils/https_client.h"
#include "modsecurity/transaction.h" #include "modsecurity/transaction.h"
#include "modsecurity/rule_unconditional.h"
#include "modsecurity/rule_with_operator.h"
void print_help() { void print_help() {
std::cout << "Use ./optimization /path/to/files.something" << std::endl; std::cout << "Use ./optimization /path/to/files.something" << std::endl;
@ -77,18 +80,16 @@ int main(int argc, char **argv) {
std::cout << " rules)" << std::endl; std::cout << " rules)" << std::endl;
std::unordered_map<std::string, int> operators; std::unordered_map<std::string, int> operators;
std::unordered_map<std::string, int> variables;
std::unordered_map<std::string, int> op2var;
for (int i = 0; i < rules->size(); i++) { for (int i = 0; i < rules->size(); i++) {
auto z = rules->at(i); auto z = rules->at(i);
//std::string key; std::string key;
if (z == NULL) { if (z == NULL) {
continue; continue;
} }
#if 0
if (z->isUnconditional() == false) { if (dynamic_cast<modsecurity::RuleUnconditional *>(z.get()) != nullptr) {
std::string op = z->getOperatorName(); std::string op = "Unconditional";
if (operators.count(op) > 0) { if (operators.count(op) > 0) {
operators[op] = 1 + operators[op]; operators[op] = 1 + operators[op];
} else { } else {
@ -96,34 +97,25 @@ int main(int argc, char **argv) {
} }
key = op; key = op;
} }
#endif
#if 0 if (dynamic_cast<modsecurity::RuleWithOperator *>(z.get()) != nullptr) {
FIXME: This test may not be useful anymore. Disabling it for now. auto *rwo = dynamic_cast<modsecurity::RuleWithOperator *>(z.get());
if (z->m_variables != NULL) { std::string op = rwo->getOperatorName();
std::string var = std::string("") + z->m_variables; if (operators.count(op) > 0) {
if (variables.count(var) > 0) { operators[op] = 1 + operators[op];
variables[var] = 1 + variables[var];
} else { } else {
variables[var] = 1; operators[op] = 1;
} }
key = key + var; key = op;
} }
if (z->m_variables != NULL && z->m_op != NULL) {
if (op2var.count(key) > 0) {
op2var[key] = 1 + op2var[key];
} else {
op2var[key] = 1;
}
}
#endif
} }
if (operators.empty()) {
if (operators.empty() && variables.empty() && op2var.empty()) {
std::cout << " ~ no SecRule found ~ " << std::endl; std::cout << " ~ no SecRule found ~ " << std::endl;
continue; continue;
} }
std::cout << " Operators" << std::endl; std::cout << " Operators" << std::endl;
for (auto &z : operators) { for (auto &z : operators) {
auto &s = z.second; auto &s = z.second;
@ -132,21 +124,6 @@ int main(int argc, char **argv) {
std::cout << std::endl; std::cout << std::endl;
} }
std::cout << " Variables" << std::endl;
for (auto &z : variables) {
auto &s = z.second;
std::cout << " " << std::left << std::setw(20) << z.first;
std::cout << std::right << std::setw(4) << s;
std::cout << std::endl;
}
std::cout << " Operators applied to variables" << std::endl;
for (auto &z : op2var) {
auto &s = z.second;
std::cout << " " << std::left << std::setw(40) << z.first;
std::cout << std::right << std::setw(4) << s;
std::cout << std::endl;
}
total += rules->size(); total += rules->size();
} }
std::cout << std::endl; std::cout << std::endl;