Skip to content

Commit 5576ba1

Browse files
committed
Merge branch 'v3/master' into process_partial_json_xml_req_body.master_merged
2 parents d15344f + 5ed6c9e commit 5576ba1

File tree

203 files changed

+27401
-22958
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

203 files changed

+27401
-22958
lines changed

test/common/modsecurity_test.cc

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,22 @@ bool ModSecurityTest<T>::load_test_json(const std::string &file) {
6868
return false;
6969
}
7070

71-
size_t num_tests = node->u.array.len;
72-
for ( int i = 0; i < num_tests; i++ ) {
73-
yajl_val obj = node->u.array.values[i];
74-
75-
auto u = std::unique_ptr<T>(T::from_yajl_node(obj));
71+
if (m_format) {
72+
auto u = T::from_yajl_node(node);
7673
u->filename = file;
7774

78-
const auto key = u->filename + ":" + u->name;
79-
(*this)[key].push_back(std::move(u));
75+
(*this)[file].push_back(std::move(u));
76+
} else {
77+
size_t num_tests = node->u.array.len;
78+
for ( int i = 0; i < num_tests; i++ ) {
79+
yajl_val obj = node->u.array.values[i];
80+
81+
auto u = T::from_yajl_node(obj);
82+
u->filename = file;
83+
84+
const auto key = u->filename + ":" + u->name;
85+
(*this)[key].push_back(std::move(u));
86+
}
8087
}
8188

8289
yajl_tree_free(node);
@@ -140,6 +147,13 @@ void ModSecurityTest<T>::cmd_options(int argc, char **argv) {
140147
i++;
141148
m_test_multithreaded = true;
142149
}
150+
if (argc > i && strcmp(argv[i], "format") == 0) {
151+
i++;
152+
m_format = true;
153+
}
154+
if (std::getenv("UPDATE_CONTENT_LENGTH")) {
155+
m_update_content_length = true;
156+
}
143157
if (std::getenv("AUTOMAKE_TESTS")) {
144158
m_automake_output = true;
145159
}

test/common/modsecurity_test.h

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <utility>
1818
#include <string>
1919
#include <vector>
20-
#include <unordered_map>
20+
#include <map>
2121

2222
#ifndef TEST_COMMON_MODSECURITY_TEST_H_
2323
#define TEST_COMMON_MODSECURITY_TEST_H_
@@ -29,15 +29,9 @@ extern std::string default_test_path;
2929
namespace modsecurity_test {
3030

3131
template <class T> class ModSecurityTest :
32-
public std::unordered_map<std::string, std::vector<std::unique_ptr<T>>> {
32+
public std::map<std::string, std::vector<std::unique_ptr<T>>> {
3333
public:
34-
ModSecurityTest()
35-
: m_test_number(0),
36-
m_automake_output(false),
37-
m_count_all(false),
38-
m_test_multithreaded(false),
39-
m_always_show_log(false),
40-
m_show_request_body_size(false) { }
34+
ModSecurityTest() = default;
4135

4236
std::string header();
4337
void cmd_options(int, char **);
@@ -46,14 +40,16 @@ template <class T> class ModSecurityTest :
4640
bool load_test_json(const std::string &file);
4741

4842
std::string target;
49-
bool verbose = false;
50-
bool color = false;
51-
int m_test_number;
52-
bool m_automake_output;
53-
bool m_count_all;
54-
bool m_test_multithreaded;
55-
bool m_always_show_log;
56-
bool m_show_request_body_size;
43+
bool verbose{false};
44+
bool color{false};
45+
int m_test_number{0};
46+
bool m_automake_output{false};
47+
bool m_count_all{false};
48+
bool m_test_multithreaded{false};
49+
bool m_format{false};
50+
bool m_update_content_length{false};
51+
bool m_always_show_log{false};
52+
bool m_show_request_body_size{false};
5753
};
5854

5955
} // namespace modsecurity_test

test/common/modsecurity_test_results.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515

1616
#include <iostream>
17-
#include <unordered_map>
1817
#include <vector>
1918
#include <string>
2019

test/regression/regression.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ using modsecurity_test::CustomDebugLog;
4141
using modsecurity_test::ModSecurityTest;
4242
using modsecurity_test::ModSecurityTestResults;
4343
using modsecurity_test::RegressionTest;
44+
using modsecurity_test::RegressionTests;
4445
using modsecurity_test::RegressionTestResult;
4546

4647
using modsecurity::Utils::regex_search;
@@ -441,6 +442,35 @@ int main(int argc, char **argv)
441442
return 0;
442443
#else
443444
test.cmd_options(argc, argv);
445+
446+
if (test.m_format) {
447+
#ifdef WITH_YAJL
448+
std::cout << "start formatting test case JSON files" << std::endl;
449+
ModSecurityTest<RegressionTests> test2;
450+
test2.cmd_options(argc, argv);
451+
test2.load_tests();
452+
for (const auto &[name, tests] : test2) {
453+
std::ofstream ofs{name};
454+
if (!ofs.is_open()) {
455+
std::cerr << "cannot open " << name << " for writing." << std::endl;
456+
return 1;
457+
}
458+
if (test2.m_update_content_length) {
459+
tests[0]->update_content_lengths();
460+
}
461+
ofs << tests[0]->toJSON();
462+
ofs.close();
463+
std::cout << "written formatted JSON to " << name << std::endl;
464+
}
465+
std::cout << "finished formatting files." << std::endl;
466+
return 0;
467+
#else
468+
std::cout << "Test utility cannot format test case JSON files without being built with YAJL." \
469+
<< std::endl;
470+
return 1;
471+
#endif
472+
}
473+
444474
if (!test.m_automake_output && !test.m_count_all) {
445475
std::cout << test.header();
446476
}

0 commit comments

Comments
 (0)