Skip to content

Commit 55c20d4

Browse files
WGH-zimmerle
authored andcommitted
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 26f08cb commit 55c20d4

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

test/unit/unit.cc

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
using modsecurity_test::UnitTest;
4242
using modsecurity_test::ModSecurityTest;
4343
using modsecurity_test::ModSecurityTestResults;
44+
using modsecurity::ModSecurity;
45+
using modsecurity::Rule;
46+
using modsecurity::Rules;
47+
using modsecurity::Transaction;
4448
using modsecurity::actions::transformations::Transformation;
4549
using modsecurity::operators::Operator;
4650

@@ -53,8 +57,20 @@ void print_help() {
5357
std::cout << std::endl;
5458
}
5559

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+
}
5671

57-
void perform_unit_test(ModSecurityTest<UnitTest> *test, UnitTest *t,
72+
static void perform_unit_test(ModSecurity *modsec,
73+
ModSecurityTest<UnitTest> *test, UnitTest *t,
5874
ModSecurityTestResults<UnitTest>* res) {
5975
std::string error;
6076
bool found = true;
@@ -77,11 +93,27 @@ void perform_unit_test(ModSecurityTest<UnitTest> *test, UnitTest *t,
7793
}
7894

7995
if (t->type == "op") {
96+
Rules rules{};
97+
Transaction transaction{modsec, &rules, NULL};
8098
Operator *op = Operator::instantiate(t->name, t->param);
99+
Rule rule{NULL, NULL, NULL, "", 1};
100+
101+
// Rx operator won't capture groups otherwise
102+
rule.m_containsCaptureAction = true;
103+
81104
op->init(t->filename, &error);
82-
int ret = op->evaluate(NULL, NULL, t->input, NULL);
105+
int ret = op->evaluate(&transaction, &rule, t->input, NULL);
83106
t->obtained = ret;
84-
if (ret != t->ret) {
107+
108+
bool pass = (ret == t->ret);
109+
if (t->re_groups.size() > 0) {
110+
t->obtained_re_groups = get_capturing_groups(transaction);
111+
if (t->re_groups != t->obtained_re_groups) {
112+
pass = false;
113+
}
114+
}
115+
116+
if (!pass) {
85117
res->push_back(t);
86118
if (test->m_automake_output) {
87119
std::cout << "FAIL ";
@@ -152,6 +184,8 @@ int main(int argc, char **argv) {
152184
test.load_tests("test-cases/secrules-language-tests/transformations");
153185
}
154186

187+
ModSecurity modsec{};
188+
155189
for (std::pair<std::string, std::vector<UnitTest *> *> a : test) {
156190
std::vector<UnitTest *> *tests = a.second;
157191

@@ -162,7 +196,7 @@ int main(int argc, char **argv) {
162196
if (!test.m_automake_output) {
163197
std::cout << " " << a.first << "...\t";
164198
}
165-
perform_unit_test(&test, t, &r);
199+
perform_unit_test(&modsec, &test, t, &r);
166200

167201
if (!test.m_automake_output) {
168202
int skp = 0;

test/unit/unit_test.cc

Lines changed: 37 additions & 0 deletions
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(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

Lines changed: 4 additions & 0 deletions
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)