Skip to content

Commit d742dd2

Browse files
committed
Add support for capturing group test cases
This enables unit tests to compare the matching groups as well, not just binary match-no match.
1 parent 61427f9 commit d742dd2

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

test/unit/unit.cc

+48-4
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
#include <iostream>
2020
#include <ctime>
2121
#include <string>
22+
#include <cassert>
2223

2324
#include "modsecurity/rules_set.h"
2425
#include "modsecurity/modsecurity.h"
2526
#include "src/operators/operator.h"
2627
#include "src/actions/transformations/transformation.h"
28+
#include "src/actions/capture.h"
2729
#include "modsecurity/transaction.h"
2830
#include "modsecurity/actions/action.h"
2931

@@ -40,6 +42,9 @@
4042
using modsecurity_test::UnitTest;
4143
using modsecurity_test::ModSecurityTest;
4244
using modsecurity_test::ModSecurityTestResults;
45+
using modsecurity::ModSecurity;
46+
using modsecurity::RuleWithActions;
47+
using modsecurity::Transaction;
4348
using modsecurity::actions::transformations::Transformation;
4449
using modsecurity::operators::Operator;
4550

@@ -52,8 +57,30 @@ void print_help() {
5257
std::cout << std::endl;
5358
}
5459

60+
static std::vector<std::string> get_capturing_groups(Transaction &transaction) {
61+
// capturing groups are stored in the TX collection as "0", "1", and so on
62+
std::vector<std::string> res;
63+
for (int i = 0;; i++) {
64+
const std::string key = std::to_string(i);
65+
auto s = transaction.m_collections.m_tx_collection->resolveFirst(key);
66+
if (s == NULL) break;
67+
res.push_back(*s);
68+
}
69+
return res;
70+
}
71+
72+
static std::unique_ptr<RuleWithActions> create_fake_capture_rule() {
73+
auto actions = new modsecurity::Actions;
74+
actions->push_back(new modsecurity::actions::Capture(""));
75+
auto rule = std::unique_ptr<RuleWithActions>(new RuleWithActions{actions, nullptr, std::unique_ptr<std::string>(new std::string("")), 1});
76+
77+
assert(rule->hasCaptureAction());
5578

56-
void perform_unit_test(ModSecurityTest<UnitTest> *test, UnitTest *t,
79+
return rule;
80+
}
81+
82+
static void perform_unit_test(ModSecurity *modsec,
83+
ModSecurityTest<UnitTest> *test, UnitTest *t,
5784
ModSecurityTestResults<UnitTest>* res) {
5885
std::string error;
5986
bool found = true;
@@ -76,11 +103,26 @@ void perform_unit_test(ModSecurityTest<UnitTest> *test, UnitTest *t,
76103
}
77104

78105
if (t->type == "op") {
106+
modsecurity::RulesSet rules{};
107+
Transaction transaction{modsec, &rules, NULL};
79108
Operator *op = Operator::instantiate(t->name, t->param);
109+
110+
// Rx operator won't capture groups otherwise
111+
auto rule = create_fake_capture_rule();
112+
80113
op->init(t->filename, &error);
81-
int ret = op->evaluate(NULL, NULL, t->input, NULL);
114+
int ret = op->evaluate(&transaction, rule.get(), t->input, NULL);
82115
t->obtained = ret;
83-
if (ret != t->ret) {
116+
117+
bool pass = (ret == t->ret);
118+
if (t->re_groups.size() > 0) {
119+
t->obtained_re_groups = get_capturing_groups(transaction);
120+
if (t->re_groups != t->obtained_re_groups) {
121+
pass = false;
122+
}
123+
}
124+
125+
if (!pass) {
84126
res->push_back(t);
85127
if (test->m_automake_output) {
86128
std::cout << "FAIL ";
@@ -151,6 +193,8 @@ int main(int argc, char **argv) {
151193
test.load_tests("test-cases/secrules-language-tests/transformations");
152194
}
153195

196+
ModSecurity modsec{};
197+
154198
for (std::pair<std::string, std::vector<UnitTest *> *> a : test) {
155199
std::vector<UnitTest *> *tests = a.second;
156200

@@ -161,7 +205,7 @@ int main(int argc, char **argv) {
161205
if (!test.m_automake_output) {
162206
std::cout << " " << a.first << "...\t";
163207
}
164-
perform_unit_test(&test, t, &r);
208+
perform_unit_test(&modsec, &test, t, &r);
165209

166210
if (!test.m_automake_output) {
167211
int skp = 0;

test/unit/unit_test.cc

+37
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ void json2bin(std::string *str) {
8888
// replaceAll(str, "\\f", '\f');
8989
}
9090

91+
static void print_array(std::stringstream &i,
92+
const std::vector<std::string> array) {
93+
i << "[";
94+
bool first = true;
95+
for (const auto &s : array) {
96+
if (first) {
97+
first = false;
98+
} else {
99+
i << ", ";
100+
}
101+
i << "\"" << modsecurity::utils::string::toHexIfNeeded(s) << "\"";
102+
}
103+
i << "]";
104+
}
91105

92106
std::string UnitTest::print() {
93107
std::stringstream i;
@@ -101,6 +115,12 @@ std::string UnitTest::print() {
101115
i << " \"input\": \"" << this->input << "\"" << std::endl;
102116
i << " \"param\": \"" << this->param << "\"" << std::endl;
103117
i << " \"output\": \"" << this->output << "\"" << std::endl;
118+
if (this->re_groups.size() != 0) {
119+
i << " \"re_groups\": ";
120+
print_array(i, this->re_groups);
121+
i << std::endl;
122+
123+
}
104124
i << "}" << std::endl;
105125
if (this->ret != this->obtained) {
106126
i << "Expecting: \"" << this->ret << "\" - returned: \"";
@@ -114,6 +134,13 @@ std::string UnitTest::print() {
114134
i << "\"";
115135
i << std::endl;
116136
}
137+
if (this->re_groups.size() && this->re_groups != this->obtained_re_groups) {
138+
i << "Expecting:\n ";
139+
print_array(i, this->re_groups);
140+
i << "\nObtained:\n ";
141+
print_array(i, this->obtained_re_groups);
142+
i << std::endl;
143+
}
117144

118145
return i.str();
119146
}
@@ -149,6 +176,16 @@ UnitTest *UnitTest::from_yajl_node(const yajl_val &node) {
149176
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53690
150177
*
151178
*/
179+
} else if (strcmp(key, "re_groups") == 0) {
180+
auto arr = YAJL_GET_ARRAY(val);
181+
if (arr == NULL) {
182+
continue;
183+
}
184+
for (int i = 0; i < arr->len; i++) {
185+
const char *s = YAJL_GET_STRING(arr->values[i]);
186+
if (s == NULL) continue;
187+
u->re_groups.push_back(s);
188+
}
152189
}
153190
}
154191

test/unit/unit_test.h

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class UnitTest {
4242
int obtained;
4343
int skipped;
4444
std::string obtainedOutput;
45+
46+
// for regular expression operator tests
47+
std::vector<std::string> re_groups;
48+
std::vector<std::string> obtained_re_groups;
4549
};
4650

4751
} // namespace modsecurity_test

0 commit comments

Comments
 (0)