mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-09-29 19:24:29 +03:00
Added support to run unit tests in a multithreaded context
- This is controlled by specifying the 'mtstress' argument when running `unit_test`. - The goal is to detect if the operator/transformation fails in this context. - In this mode, the test will be executed 5'000 times in 50 threads concurrently. - Allocation & initialization of the operator/transformation is performed once in the main thread, while the evaluation is executed in the threads. - This is consistent with the library's support for multithreading, where initialization and loading of rules is expected to run once. See issue #3215.
This commit is contained in:
@@ -93,13 +93,13 @@ bool ModSecurityTest<T>::load_test_json(const std::string &file) {
|
||||
|
||||
|
||||
template <class T>
|
||||
std::pair<std::string, std::vector<T *>>*
|
||||
void
|
||||
ModSecurityTest<T>::load_tests(const std::string &path) {
|
||||
DIR *dir;
|
||||
struct dirent *ent;
|
||||
struct stat buffer;
|
||||
|
||||
if ((dir = opendir(path.c_str())) == NULL) {
|
||||
if ((dir = opendir(path.c_str())) == nullptr) {
|
||||
/* if target is a file, use it as a single test. */
|
||||
if (stat(path.c_str(), &buffer) == 0) {
|
||||
if (load_test_json(path) == false) {
|
||||
@@ -107,10 +107,10 @@ ModSecurityTest<T>::load_tests(const std::string &path) {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
while ((ent = readdir(dir)) != NULL) {
|
||||
while ((ent = readdir(dir)) != nullptr) {
|
||||
std::string filename = ent->d_name;
|
||||
std::string json = ".json";
|
||||
if (filename.size() < json.size()
|
||||
@@ -123,16 +123,15 @@ ModSecurityTest<T>::load_tests(const std::string &path) {
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
std::pair<std::string, std::vector<T *>>* ModSecurityTest<T>::load_tests() {
|
||||
return load_tests(this->target);
|
||||
void ModSecurityTest<T>::load_tests() {
|
||||
load_tests(this->target);
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
void ModSecurityTest<T>::cmd_options(int argc, char **argv) {
|
||||
int i = 1;
|
||||
@@ -144,6 +143,10 @@ void ModSecurityTest<T>::cmd_options(int argc, char **argv) {
|
||||
i++;
|
||||
m_count_all = true;
|
||||
}
|
||||
if (argc > i && strcmp(argv[i], "mtstress") == 0) {
|
||||
i++;
|
||||
m_test_multithreaded = true;
|
||||
}
|
||||
if (std::getenv("AUTOMAKE_TESTS")) {
|
||||
m_automake_output = true;
|
||||
}
|
||||
|
@@ -34,12 +34,13 @@ template <class T> class ModSecurityTest :
|
||||
ModSecurityTest()
|
||||
: m_test_number(0),
|
||||
m_automake_output(false),
|
||||
m_count_all(false) { }
|
||||
m_count_all(false),
|
||||
m_test_multithreaded(false) { }
|
||||
|
||||
std::string header();
|
||||
void cmd_options(int, char **);
|
||||
std::pair<std::string, std::vector<T *>>* load_tests();
|
||||
std::pair<std::string, std::vector<T *>>* load_tests(const std::string &path);
|
||||
void load_tests();
|
||||
void load_tests(const std::string &path);
|
||||
bool load_test_json(const std::string &file);
|
||||
|
||||
std::string target;
|
||||
@@ -48,6 +49,7 @@ template <class T> class ModSecurityTest :
|
||||
int m_test_number;
|
||||
bool m_automake_output;
|
||||
bool m_count_all;
|
||||
bool m_test_multithreaded;
|
||||
};
|
||||
|
||||
} // namespace modsecurity_test
|
||||
|
Reference in New Issue
Block a user