Added const reported by cppcheck 2.14

This commit is contained in:
Eduardo Arias
2024-04-28 21:23:43 -03:00
committed by Eduardo Arias
parent d053ec6de6
commit bbef22b3b5
46 changed files with 110 additions and 112 deletions

View File

@@ -44,7 +44,7 @@ char rules_file[] = "basic_rules.conf";
const char* const help_message = "Usage: benchmark [num_iterations|-h|-?|--help]";
int main(int argc, char *argv[]) {
int main(int argc, const char *argv[]) {
unsigned long long NUM_REQUESTS(1000000);

View File

@@ -89,7 +89,7 @@ template <class T>
void
ModSecurityTest<T>::load_tests(const std::string &path) {
DIR *dir;
struct dirent *ent;
const struct dirent *ent;
struct stat buffer;
if ((dir = opendir(path.c_str())) == nullptr) {

View File

@@ -54,7 +54,7 @@ int main(int argc, char **argv) {
}
for (auto &x : files) {
for (const auto &x : files) {
std::cout << "Loading file: " << x << std::endl;
if (modsecRules->loadFromUri(x.c_str()) < 0) {
std::cout << "Not able to load the rules" << std::endl;
@@ -96,8 +96,8 @@ int main(int argc, char **argv) {
}
}
if (auto rwo = dynamic_cast<modsecurity::RuleWithOperator *>(z.get())) {
std::string op = rwo->getOperatorName();
if (const auto *rwo = dynamic_cast<modsecurity::RuleWithOperator *>(z.get())) {
const auto op = rwo->getOperatorName();
if (operators.count(op) > 0) {
operators[op] = 1 + operators[op];
} else {

View File

@@ -26,6 +26,7 @@
#include <string>
#include <list>
#include <algorithm>
#include <cassert>
#include "modsecurity/rules_set.h"
#include "modsecurity/modsecurity.h"
@@ -110,8 +111,8 @@ void actions(ModSecurityTestResults<RegressionTest> *r,
}
}
void perform_unit_test(ModSecurityTest<RegressionTest> *test,
std::vector<std::unique_ptr<RegressionTest>> &tests,
void perform_unit_test(const ModSecurityTest<RegressionTest> &test,
const std::vector<std::unique_ptr<RegressionTest>> &tests,
ModSecurityTestResults<RegressionTestResult> *res, int *count)
{
for (auto &t : tests) {
@@ -131,7 +132,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
filename = t->filename;
}
if (!test->m_automake_output) {
if (!test.m_automake_output) {
std::cout << std::setw(3) << std::right <<
std::to_string(*count) << " ";
std::cout << std::setw(50) << std::left << filename;
@@ -139,7 +140,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
}
if (t->enabled == 0) {
if (test->m_automake_output) {
if (test.m_automake_output) {
std::cout << ":test-result: SKIP" << filename \
<< ":" << t->name << std::endl;
} else {
@@ -173,7 +174,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
testRes->reason << KCYN << "compiled with support " << std::endl;
testRes->reason << KCYN << "to: " << t->resource << std::endl;
testRes->reason << RESET << std::endl;
if (test->m_automake_output) {
if (test.m_automake_output) {
std::cout << ":test-result: SKIP " << filename \
<< ":" << t->name << std::endl;
} else {
@@ -192,7 +193,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
* Not expecting any error, thus return the error to
* the user.
*/
if (test->m_automake_output) {
if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl;
} else {
@@ -213,7 +214,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
const auto s = context.m_modsec_rules.getParserError();
if (regex_search(s, &match, re)) {
if (test->m_automake_output) {
if (test.m_automake_output) {
std::cout << ":test-result: PASS " << filename \
<< ":" << t->name << std::endl;
} else {
@@ -227,7 +228,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
continue;
} else {
/* Parser error was expected, but with a different content */
if (test->m_automake_output) {
if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl;
} else {
@@ -249,7 +250,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
} else {
/* Parser error was expected but never happened */
if (t->parser_error.empty() == false) {
if (test->m_automake_output) {
if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl;
} else {
@@ -318,7 +319,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
const auto *d = static_cast<CustomDebugLog *>(context.m_modsec_rules.m_debugLog);
if (!d->contains(t->debug_log)) {
if (test->m_automake_output) {
if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl;
} else {
@@ -330,7 +331,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
<< t->debug_log + "";
testRes->passed = false;
} else if (r.status != t->http_code) {
if (test->m_automake_output) {
if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl;
} else {
@@ -341,7 +342,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
" got: " + std::to_string(r.status) + "\n";
testRes->passed = false;
} else if (!contains(context.m_server_log.str(), t->error_log)) {
if (test->m_automake_output) {
if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << std::endl;
} else {
@@ -353,7 +354,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
<< t->error_log + "";
testRes->passed = false;
} else if (!t->audit_log.empty() && !contains(getAuditLogContent(modsec_transaction.m_rules->m_auditLog->m_path1), t->audit_log)) {
if (test->m_automake_output) {
if (test.m_automake_output) {
std::cout << ":test-result: FAIL " << filename \
<< ":" << t->name << ":" << *count << std::endl;
} else {
@@ -365,7 +366,7 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
<< t->audit_log + "";
testRes->passed = false;
} else {
if (test->m_automake_output) {
if (test.m_automake_output) {
std::cout << ":test-result: PASS " << filename \
<< ":" << t->name << std::endl;
} else {
@@ -471,8 +472,8 @@ int main(int argc, char **argv)
test_number++;
if ((test.m_test_number == 0)
|| (test_number == test.m_test_number)) {
auto &tests = test[a];
perform_unit_test(&test, tests, &res, &counter);
const auto &tests = test[a];
perform_unit_test(test, tests, &res, &counter);
}
}

View File

@@ -92,7 +92,7 @@ struct TransformationTest {
return tfn;
}
static UnitTestResult eval(const ItemType &tfn, const UnitTest &t, modsecurity::Transaction &transaction) {
static UnitTestResult eval(const ItemType &tfn, const UnitTest &t, const modsecurity::Transaction &transaction) {
auto ret = t.input;
tfn.transform(ret, &transaction);
return {1, ret};

View File

@@ -75,7 +75,7 @@ void json2bin(std::string *str) {
}
std::string UnitTest::print() {
std::string UnitTest::print() const {
std::stringstream i;
i << KRED << "Test failed." << RESET;

View File

@@ -35,7 +35,7 @@ class UnitTest {
public:
static UnitTest *from_yajl_node(const yajl_val &);
std::string print();
std::string print() const;
std::string param;
std::string input;