Skip to content

Commit b58c462

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 846386a commit b58c462

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
@@ -86,6 +86,20 @@ void json2bin(std::string *str) {
8686
// replaceAll(str, "\\f", '\f');
8787
}
8888

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

90104
std::string UnitTest::print() {
91105
std::stringstream i;
@@ -99,6 +113,12 @@ std::string UnitTest::print() {
99113
i << " \"input\": \"" << this->input << "\"" << std::endl;
100114
i << " \"param\": \"" << this->param << "\"" << std::endl;
101115
i << " \"output\": \"" << this->output << "\"" << std::endl;
116+
if (this->re_groups.size() != 0) {
117+
i << " \"re_groups\": ";
118+
print_array(i, this->re_groups);
119+
i << std::endl;
120+
121+
}
102122
i << "}" << std::endl;
103123
if (this->ret != this->obtained) {
104124
i << "Expecting: \"" << this->ret << "\" - returned: \"";
@@ -112,6 +132,13 @@ std::string UnitTest::print() {
112132
i << "\"";
113133
i << std::endl;
114134
}
135+
if (this->re_groups.size() && this->re_groups != this->obtained_re_groups) {
136+
i << "Expecting:\n ";
137+
print_array(i, this->re_groups);
138+
i << "\nObtained:\n ";
139+
print_array(i, this->obtained_re_groups);
140+
i << std::endl;
141+
}
115142

116143
return i.str();
117144
}
@@ -147,6 +174,16 @@ UnitTest *UnitTest::from_yajl_node(yajl_val &node) {
147174
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53690
148175
*
149176
*/
177+
} else if (strcmp(key, "re_groups") == 0) {
178+
auto arr = YAJL_GET_ARRAY(val);
179+
if (arr == NULL) {
180+
continue;
181+
}
182+
for (int i = 0; i < arr->len; i++) {
183+
const char *s = YAJL_GET_STRING(arr->values[i]);
184+
if (s == NULL) continue;
185+
u->re_groups.push_back(s);
186+
}
150187
}
151188
}
152189

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)