Add SPDX license identifiers to files under tests/ossfuzz
[xz/debian.git] / tests / tests.cmake
blob62c546ced3e65c7ca62465ad9278a29833845b68
1 # SPDX-License-Identifier: 0BSD
3 #############################################################################
5 # Optional file to be included by the top-level CMakeLists.txt to run tests
7 # The CMake rules for the tests are in this separate optional file so
8 # that it's trivial to just delete the whole "tests" directory and still
9 # get an otherwise normal CMake-based build. This way it's easy to ensure
10 # that nothing in the "tests" directory can affect the build process.
12 # Author: Lasse Collin
14 #############################################################################
16 include(CTest)
18 if(BUILD_TESTING)
19     #################
20     # liblzma tests #
21     #################
23     set(LIBLZMA_TESTS
24         test_bcj_exact_size
25         test_block_header
26         test_check
27         test_filter_flags
28         test_filter_str
29         test_hardware
30         test_index
31         test_index_hash
32         test_lzip_decoder
33         test_memlimit
34         test_stream_flags
35         test_vli
36     )
38     # MicroLZMA encoder is needed for both encoder and decoder tests.
39     # If MicroLZMA decoder is not configured but LZMA1 decoder is, then
40     # test_microlzma will fail to compile because this configuration is
41     # not possible in the Autotools build, so the test was not made to
42     # support it since it would have required additional changes.
43     if (MICROLZMA_ENCODER AND (MICROLZMA_DECODER
44             OR NOT "lzma1" IN_LIST DECODERS))
45         list(APPEND LIBLZMA_TESTS test_microlzma)
46     endif()
48     foreach(TEST IN LISTS LIBLZMA_TESTS)
49         add_executable("${TEST}" "tests/${TEST}.c")
51         target_include_directories("${TEST}" PRIVATE
52             src/common
53             src/liblzma/api
54             src/liblzma
55         )
57         target_link_libraries("${TEST}" PRIVATE liblzma)
59         # Put the test programs into their own subdirectory so they don't
60         # pollute the top-level dir which might contain xz and xzdec.
61         set_target_properties("${TEST}" PROPERTIES
62             RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/tests_bin"
63         )
65         add_test(NAME "${TEST}"
66                  COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tests_bin/${TEST}"
67         )
69         # Set srcdir environment variable so that the tests find their
70         # input files from the source tree.
71         #
72         # Set the return code for skipped tests to match Automake convention.
73         set_tests_properties("${TEST}" PROPERTIES
74             ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
75             SKIP_RETURN_CODE 77
76         )
77     endforeach()
80     ###########################
81     # Command line tool tests #
82     ###########################
84     # Since the CMake-based build doesn't use config.h, the test scripts
85     # cannot grep the contents of config.h to know which features have
86     # been disabled. When config.h is missing, they assume that all
87     # features are enabled. Thus, check if certain groups of features have
88     # been disabled and then possibly skip some of the tests entirely instead
89     # of letting them fail.
90     set(SUPPORTED_FILTERS_SORTED "${SUPPORTED_FILTERS}")
91     list(SORT SUPPORTED_FILTERS_SORTED)
93     set(ENCODERS_SORTED "${ENCODERS}")
94     list(SORT ENCODERS_SORTED)
96     if("${ENCODERS_SORTED}" STREQUAL "${SUPPORTED_FILTERS_SORTED}")
97         set(HAVE_ALL_ENCODERS ON)
98     else()
99         set(HAVE_ALL_ENCODERS OFF)
100     endif()
102     set(DECODERS_SORTED "${DECODERS}")
103     list(SORT DECODERS_SORTED)
105     if("${DECODERS_SORTED}" STREQUAL "${SUPPORTED_FILTERS_SORTED}")
106         set(HAVE_ALL_DECODERS ON)
107     else()
108         set(HAVE_ALL_DECODERS OFF)
109     endif()
111     set(ADDITIONAL_SUPPORTED_CHECKS_SORTED "${ADDITIONAL_SUPPORTED_CHECKS}")
112     list(SORT ADDITIONAL_SUPPORTED_CHECKS_SORTED)
114     set(ADDITIONAL_CHECK_TYPES_SORTED "${ADDITIONAL_CHECK_TYPES}")
115     list(SORT ADDITIONAL_CHECK_TYPES_SORTED)
117     if("${ADDITIONAL_SUPPORTED_CHECKS_SORTED}" STREQUAL
118         "${ADDITIONAL_CHECK_TYPES_SORTED}")
119         set(HAVE_ALL_CHECK_TYPES ON)
120     else()
121         set(HAVE_ALL_CHECK_TYPES OFF)
122     endif()
124     # test_scripts.sh only needs LZMA2 decoder and CRC32.
125     if(UNIX AND HAVE_DECODERS)
126         file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_scripts")
128         add_test(NAME test_scripts.sh
129             COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_scripts.sh" ".."
130             WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_scripts"
131         )
133         set_tests_properties(test_scripts.sh PROPERTIES
134             ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
135             SKIP_RETURN_CODE 77
136         )
137     endif()
139     # test_suffix.sh only needs LZMA2 encoder and decoder.
140     if(UNIX AND HAVE_ENCODERS AND HAVE_DECODERS)
141         file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_suffix")
143         add_test(NAME test_suffix.sh
144             COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_suffix.sh" ".."
145             WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_suffix"
146         )
148         set_tests_properties(test_suffix.sh PROPERTIES
149             SKIP_RETURN_CODE 77
150         )
151     endif()
153     # The test_compress.sh based tests compress and decompress using different
154     # filters so run it only if all encoders and decoders have been enabled.
155     if(UNIX AND HAVE_ALL_ENCODERS AND HAVE_ALL_DECODERS)
156         file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_compress")
158         add_executable(create_compress_files tests/create_compress_files.c)
159         target_include_directories(create_compress_files PRIVATE src/common)
160         set_target_properties(create_compress_files PROPERTIES
161                               RUNTIME_OUTPUT_DIRECTORY test_compress)
163         foreach(T compress_generated_abc
164                   compress_generated_text
165                   compress_generated_random)
166             add_test(NAME "test_${T}"
167                 COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_compress.sh"
168                         "${T}" ".."
169                 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_compress"
170             )
172             set_tests_properties("test_${T}" PROPERTIES
173                 ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
174                 SKIP_RETURN_CODE 77
175             )
176         endforeach()
177     endif()
179     # test_files.sh decompresses files that use different filters and
180     # check types so run it only if support for all of them has been enabled.
181     if(UNIX AND HAVE_ALL_DECODERS AND HAVE_ALL_CHECK_TYPES AND LZIP_DECODER)
182         # test_files.sh doesn't make any temporary files but it
183         # must not be run at the top-level build directory because
184         # it checks if ../config.h exists. We don't want to read
185         # files outside the build directory!
186         file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_files")
188         add_test(NAME test_files.sh
189             COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_files.sh" ".."
190             WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_files"
191         )
193         set_tests_properties(test_files.sh PROPERTIES
194             ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}/tests"
195             SKIP_RETURN_CODE 77
196         )
197     endif()
198 endif()