Skip to content

Commit e6e2989

Browse files
committed
Configure test fixture using CTest for Windows build
- Added new test/test_suite.in with list of regression and unit tests previously in Makefile.am, to be shared between Unix and Windows builds. - Updated regression.cc & unit.cc to return the number of failed tests to indicate to CTest that the test failed. Similarly, a crash or unhandled exception terminates the process with a non-zero exit code. - This change doesn't affect running the tests with autotest in Unix builds because this processes test output from custom-test-driver & test-suite.sh, and ignores the exit code of the test runner. - Removed comment in test/test-cases/regression-offset-variable.json as this is not supported by JSON and prevents strict parsers to read and process the file. - Minor change in regression.cc's clearAuditLog to replace std::ifstream with std::ofstream as the mode to open the flag applies to an output stream. - Minor change in unit.cc to simplify code that deletes tests. - Minor changes to test/custom-test-driver to correct usage information.
1 parent a8e132f commit e6e2989

File tree

9 files changed

+338
-282
lines changed

9 files changed

+338
-282
lines changed

Makefile.am

Lines changed: 1 addition & 256 deletions
Large diffs are not rendered by default.

build/win32/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,39 @@ add_regression_test_capability(WITH_CURL HAVE_CURL)
151151
add_regression_test_capability(WITH_LMDB HAVE_LMDB)
152152
add_regression_test_capability(WITH_MAXMIND HAVE_MAXMIND)
153153

154+
enable_testing()
155+
156+
file(READ ${BASE_DIR}/test/test-suite.in TEST_FILES_RAW)
157+
string(REPLACE "\n" ";" TEST_FILES ${TEST_FILES_RAW})
158+
159+
foreach(TEST_FILE ${TEST_FILES})
160+
# ignore comment lines
161+
string(FIND ${TEST_FILE} "#" is_comment)
162+
if(NOT is_comment EQUAL 0)
163+
string(FIND ${TEST_FILE} "TESTS+=" is_valid_prefix)
164+
if(NOT is_valid_prefix EQUAL 0)
165+
message(FATAL_ERROR "Invalid prefix in line: ${TEST_FILE}")
166+
endif()
167+
168+
# remove 'TESTS+=' prefix and 'test/' too because tests are launched
169+
# from that directory
170+
string(SUBSTRING ${TEST_FILE} 12 -1 TEST_FILE)
171+
172+
# test name
173+
get_filename_component(TEST_NAME ${TEST_FILE} NAME_WE)
174+
175+
# determine test runner based on test path prefix
176+
string(FIND ${TEST_FILE} "test-cases/regression/" is_regression_test)
177+
if(is_regression_test EQUAL 0)
178+
set(TEST_RUNNER "regression_tests")
179+
else()
180+
set(TEST_RUNNER "unit_tests")
181+
endif()
182+
183+
add_test(NAME ${TEST_NAME} COMMAND ${TEST_RUNNER} ${TEST_FILE} WORKING_DIRECTORY ${BASE_DIR}/test)
184+
endif()
185+
endforeach()
186+
154187
# benchmark
155188
add_executable(benchmark ${BASE_DIR}/test/benchmark/benchmark.cc)
156189
setTestTargetProperties(benchmark)

build/win32/docker/Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,35 @@ ARG USE_ASAN=
7777

7878
RUN C:\BuildTools\VC\Auxiliary\Build\vcvars64.bat && vcbuild.bat %BUILD_TYPE% %ARCH% %USE_ASAN%
7979

80+
# test suite
81+
#
82+
83+
# setup test environment
84+
RUN cmd.exe /C md \tmp
85+
RUN cmd.exe /C md \bin
86+
RUN cmd.exe /C copy "C:\Program Files\GIT\usr\bin" \bin > NUL
87+
RUN cmd.exe /C copy "C:\Program Files\GIT\usr\bin\echo.exe" \bin\echo > NUL
88+
89+
# disable tests that don't work on windows
90+
ARG JQ_VERSION=1.7.1
91+
ARG JQ_BINARY=jq-windows-amd64.exe
92+
ARG JQ_URL=https://github.com/jqlang/jq/releases/download/jq-${JQ_VERSION}/${JQ_BINARY}
93+
94+
ARG JQ_BIN=C:\TEMP\jq.exe
95+
ADD ${JQ_URL} ${JQ_BIN}
96+
97+
WORKDIR ${MOD_SECURITY_DIR}\test\test-cases\regression
98+
99+
RUN %JQ_BIN% "map(if .title == \"Test match variable (1/n)\" then .enabled = 0 else . end)" issue-2423-msg-in-chain.json > tmp.json && move /Y tmp.json issue-2423-msg-in-chain.json
100+
RUN %JQ_BIN% "map(if .title == \"Test match variable (2/n)\" then .enabled = 0 else . end)" issue-2423-msg-in-chain.json > tmp.json && move /Y tmp.json issue-2423-msg-in-chain.json
101+
RUN %JQ_BIN% "map(if .title == \"Test match variable (3/n)\" then .enabled = 0 else . end)" issue-2423-msg-in-chain.json > tmp.json && move /Y tmp.json issue-2423-msg-in-chain.json
102+
RUN %JQ_BIN% "map(if .title == \"Variable offset - FILES_NAMES\" then .enabled = 0 else . end)" offset-variable.json > tmp.json && move /Y tmp.json offset-variable.json
103+
104+
# run tests
105+
WORKDIR ${MOD_SECURITY_DIR}\build\win32\build
106+
107+
RUN C:\BuildTools\VC\Auxiliary\Build\vcvars64.bat && ctest -C %BUILD_TYPE% --output-on-failure
108+
80109
# setup container's entrypoint
81110
#
82111

test/custom-test-driver

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ print_usage ()
4242
{
4343
cat <<END
4444
Usage:
45-
test-driver --test-name=NAME --log-file=PATH --trs-file=PATH
46-
[--expect-failure={yes|no}] [--color-tests={yes|no}]
47-
[--enable-hard-errors={yes|no}] [--]
45+
test-driver --test-name NAME --log-file PATH --trs-file PATH
46+
[--expect-failure {yes|no}] [--color-tests {yes|no}]
47+
[--enable-hard-errors {yes|no}] [--]
4848
TEST-SCRIPT [TEST-SCRIPT-ARGUMENTS]
4949
The '--test-name', '--log-file' and '--trs-file' options are mandatory.
5050
END

test/regression/regression.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,11 @@ bool contains(const std::string &s, const std::string &pattern) {
6464

6565
void clearAuditLog(const std::string &filename) {
6666
if (!filename.empty()) {
67-
std::ifstream file;
68-
file.open(filename.c_str(), std::ifstream::out | std::ifstream::trunc);
67+
std::ofstream file{filename.c_str(), std::ofstream::out | std::ofstream::trunc};
6968
if (!file.is_open() || file.fail()) {
7069
std::cout << std::endl << "Failed to clear previous contents of audit log: " \
7170
<< filename << std::endl;
7271
}
73-
file.close();
7472
}
7573
}
7674
std::string getAuditLogContent(const std::string &filename) {
@@ -510,6 +508,7 @@ int main(int argc, char **argv) {
510508
#ifdef NO_LOGS
511509
std::cout << "Test utility cannot work without logging support." \
512510
<< std::endl;
511+
return 0;
513512
#else
514513
test.cmd_options(argc, argv);
515514
if (!test.m_automake_output && !test.m_count_all) {
@@ -609,6 +608,6 @@ int main(int argc, char **argv) {
609608
delete vec;
610609
}
611610

611+
return failed;
612612
#endif
613-
return 0;
614613
}

test/test-cases/regression/offset-variable.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@
215215
]
216216
},
217217
"expected":{
218-
// should not match
219218
},
220219
"rules":[
221220
"SecRequestBodyAccess On",
@@ -248,7 +247,6 @@
248247
]
249248
},
250249
"expected":{
251-
// should not match
252250
},
253251
"rules":[
254252
"SecRequestBodyAccess On",

0 commit comments

Comments
 (0)