@@ -58,6 +58,30 @@ bool contains(const std::string &s, const std::string &pattern) {
5858 return ret;
5959}
6060
61+ void clearAuditLog (const std::string &filename) {
62+ if (!filename.empty ()) {
63+ std::ifstream file;
64+ file.open (filename.c_str (), std::ifstream::out | std::ifstream::trunc);
65+ if (!file.is_open () || file.fail ()) {
66+ std::cout << std::endl << " Failed to clear previous contents of audit log: " \
67+ << filename << std::endl;
68+ }
69+ file.close ();
70+ }
71+ }
72+ std::string getAuditLogContent (const std::string &filename) {
73+ std::stringstream buffer;
74+ if (!filename.empty ()) {
75+ try {
76+ std::ifstream t (filename);
77+ buffer << t.rdbuf ();
78+ } catch (...) {
79+ std::cout << " Failed to read file:" << filename << std::endl;
80+ }
81+ }
82+ return buffer.str ();
83+ }
84+
6185
6286void actions (ModSecurityTestResults<RegressionTest> *r,
6387 modsecurity::Transaction *a, std::stringstream *serverLog) {
@@ -278,6 +302,8 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
278302 modsec_transaction = new modsecurity::Transaction (modsec, modsec_rules,
279303 &serverLog);
280304
305+ clearAuditLog (modsec_transaction->m_rules ->m_auditLog ->m_path1 );
306+
281307 modsec_transaction->processConnection (t->clientIp .c_str (),
282308 t->clientPort , t->serverIp .c_str (), t->serverPort );
283309
@@ -393,6 +419,19 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
393419 testRes->reason << KWHT << " Expecting: " << RESET \
394420 << t->error_log + " " ;
395421 testRes->passed = false ;
422+ } else if (!t->audit_log .empty ()
423+ && !contains (getAuditLogContent (modsec_transaction->m_rules ->m_auditLog ->m_path1 ), t->audit_log )) {
424+ if (test->m_automake_output ) {
425+ std::cout << " :test-result: FAIL " << filename \
426+ << " :" << t->name << std::endl;
427+ } else {
428+ std::cout << KRED << " failed!" << RESET << std::endl;
429+ }
430+ testRes->reason << " Audit log was not matching the " \
431+ << " expected results." << std::endl;
432+ testRes->reason << KWHT << " Expecting: " << RESET \
433+ << t->audit_log + " " ;
434+ testRes->passed = false ;
396435 } else {
397436 if (test->m_automake_output ) {
398437 std::cout << " :test-result: PASS " << filename \
@@ -410,6 +449,8 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
410449 testRes->reason << d->log_messages () << std::endl;
411450 testRes->reason << KWHT << " Error log:" << RESET << std::endl;
412451 testRes->reason << serverLog.str () << std::endl;
452+ testRes->reason << KWHT << " Audit log:" << RESET << std::endl;
453+ testRes->reason << getAuditLogContent (modsec_transaction->m_rules ->m_auditLog ->m_path1 ) << std::endl;
413454 }
414455 }
415456
0 commit comments