diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c8ee7944..f41830ab4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,11 +99,19 @@ add_custom_target( DEPENDS generate_java generate_jar ) +if(WITH_TESTS) + add_dependencies(java generate_tests) +endif(WITH_TESTS) + add_custom_target( native ALL DEPENDS generate_c generate_so symkey_library ) +if(WITH_TESTS) + add_dependencies(native generate_test_so) +endif(WITH_TESTS) + if(WITH_JAVA) install( FILES diff --git a/cmake/JSSCommon.cmake b/cmake/JSSCommon.cmake index ee8868694..99d2e9aac 100644 --- a/cmake/JSSCommon.cmake +++ b/cmake/JSSCommon.cmake @@ -24,6 +24,15 @@ macro(jss_build) jss_build_includes() jss_build_c() jss_build_jars() + + if(WITH_TESTS) + jss_build_test_globs() + jss_build_tests() + jss_build_test_includes() + jss_build_test_c() + jss_build_test_jars() + endif(WITH_TESTS) + jss_build_javadocs() endmacro() @@ -33,40 +42,40 @@ macro(jss_build_globs) list(APPEND JAVA_SOURCES "${_JAVA_SOURCE}") endforeach() - file(GLOB_RECURSE _JAVA_SOURCES base/src/test/java/*.java) - foreach(_JAVA_SOURCE ${_JAVA_SOURCES}) - list(APPEND JAVA_TEST_SOURCES "${_JAVA_SOURCE}") - endforeach() - # Write the Java sources to a file to reduce the size of the javac # command line. list(SORT JAVA_SOURCES) - list(SORT JAVA_TEST_SOURCES) jss_list_join(JAVA_SOURCES "\n" JAVA_SOURCES_CONTENTS) - jss_list_join(JAVA_TEST_SOURCES "\n" JAVA_TEST_SOURCES_CONTENTS) file(WRITE "${JAVA_SOURCES_FILE}" "${JAVA_SOURCES_CONTENTS}") - file(WRITE "${JAVA_TEST_SOURCES_FILE}" "${JAVA_TEST_SOURCES_CONTENTS}") - file(GLOB_RECURSE _C_HEADERS native/src/main/native/*.h) foreach(_C_HEADER ${_C_HEADERS}) list(APPEND C_HEADERS "${_C_HEADER}") endforeach() - file(GLOB_RECURSE _C_HEADERS native/src/test/native/*.h) - foreach(_C_HEADER ${_C_HEADERS}) - list(APPEND C_TEST_HEADERS "${_C_HEADER}") - endforeach() - - # We exclude any C files in the tests directory because they shouldn't - # contribute to our library. They should instead be built as part of the - # test suite and probably be built as stand alone binaries which link - # against libjss.so (at most). file(GLOB_RECURSE _C_SOURCES native/src/main/native/*.c) foreach(_C_SOURCE ${_C_SOURCES}) list(APPEND C_SOURCES "${_C_SOURCE}") endforeach() +endmacro() + +macro(jss_build_test_globs) + + file(GLOB_RECURSE _JAVA_SOURCES base/src/test/java/*.java) + foreach(_JAVA_SOURCE ${_JAVA_SOURCES}) + list(APPEND JAVA_TEST_SOURCES "${_JAVA_SOURCE}") + endforeach() + + list(SORT JAVA_TEST_SOURCES) + jss_list_join(JAVA_TEST_SOURCES "\n" JAVA_TEST_SOURCES_CONTENTS) + + file(WRITE "${JAVA_TEST_SOURCES_FILE}" "${JAVA_TEST_SOURCES_CONTENTS}") + + file(GLOB_RECURSE _C_HEADERS native/src/test/native/*.h) + foreach(_C_HEADER ${_C_HEADERS}) + list(APPEND C_TEST_HEADERS "${_C_HEADER}") + endforeach() file(GLOB_RECURSE _C_SOURCES native/src/test/native/*.c) foreach(_C_SOURCE ${_C_SOURCES}) @@ -81,7 +90,6 @@ macro(jss_build_java) # on, but it also must be the last-thing created; thus, we touch # ${JNI_OUTPUTS} after the javac command finishes. set(JNI_OUTPUTS "${TARGETS_OUTPUT_DIR}/finished_generate_java") - set(TESTS_JNI_OUTPUTS "${TARGETS_OUTPUT_DIR}/finished_tests_generate_java") # We frequently use the add_custom_command + add_custom_target wrapper due # to a quirk of CMake. This is documented more extensively in the @@ -95,6 +103,16 @@ macro(jss_build_java) DEPENDS ${JAVA_SOURCES} ) + add_custom_target( + generate_java + DEPENDS ${JNI_OUTPUTS} + ) +endmacro() + +macro(jss_build_tests) + + set(TESTS_JNI_OUTPUTS "${TARGETS_OUTPUT_DIR}/finished_tests_generate_java") + add_custom_command( OUTPUT "${TESTS_JNI_OUTPUTS}" COMMAND ${Java_JAVAC_EXECUTABLE} ${JSS_TEST_JAVAC_FLAGS} -d ${TESTS_CLASSES_OUTPUT_DIR} -h ${TESTS_JNI_OUTPUT_DIR} @${JAVA_TEST_SOURCES_FILE} @@ -103,8 +121,8 @@ macro(jss_build_java) ) add_custom_target( - generate_java - DEPENDS ${JNI_OUTPUTS} ${TESTS_JNI_OUTPUTS} + generate_tests + DEPENDS ${TESTS_JNI_OUTPUTS} ) endmacro() @@ -119,12 +137,18 @@ macro(jss_build_includes) file(COPY "${C_HEADER}" DESTINATION ${INCLUDE_OUTPUT_DIR}) endforeach() + add_custom_target( + generate_includes + ) +endmacro() + +macro(jss_build_test_includes) foreach(C_TEST_HEADER ${C_TEST_HEADERS}) file(COPY "${C_TEST_HEADER}" DESTINATION ${TESTS_INCLUDE_OUTPUT_DIR}) endforeach() add_custom_target( - generate_includes + generate_test_includes ) endmacro() @@ -167,15 +191,11 @@ macro(jss_build_c) DEPENDS ${C_OUTPUTS} ) - # We generate two libraries: build/lib/libjss.so and build/libjss.so: - # the former is for testing and is unversioned, so all symbols are public - # and can thus be tested; the latter is for releases and is versioned, - # limiting which symbols are made public. We only need to make the JNI - # symbols public as libjss.so should only be used from Java in conjunction - # with jss.jar. + # The build/libjss.so is for releases and is versioned, limiting which + # symbols are made public. We only need to make the JNI symbols public + # as libjss.so should only be used from Java in conjunction with jss.jar. add_custom_command( - OUTPUT "${JSS_SO_PATH}" "${JSS_TESTS_SO_PATH}" - COMMAND ${CMAKE_C_COMPILER} -o ${JSS_TESTS_SO_PATH} ${LIB_OUTPUT_DIR}/*.o ${JSS_LD_FLAGS} ${JSS_LIBRARY_FLAGS} + OUTPUT "${JSS_SO_PATH}" COMMAND ${CMAKE_C_COMPILER} -o ${JSS_SO_PATH} ${LIB_OUTPUT_DIR}/*.o ${JSS_LD_FLAGS} ${JSS_VERSION_SCRIPT} ${JSS_LIBRARY_FLAGS} DEPENDS generate_c ) @@ -183,7 +203,22 @@ macro(jss_build_c) # Add a target for anything depending on the library existing. add_custom_target( generate_so - DEPENDS ${JSS_SO_PATH} ${JSS_TESTS_SO_PATH} + DEPENDS ${JSS_SO_PATH} + ) +endmacro() + +macro(jss_build_test_c) + # The build/lib/libjss.so is for testing and is unversioned, + # so all symbols are public and can thus be tested. + add_custom_command( + OUTPUT "${JSS_TESTS_SO_PATH}" + COMMAND ${CMAKE_C_COMPILER} -o ${JSS_TESTS_SO_PATH} ${LIB_OUTPUT_DIR}/*.o ${JSS_LD_FLAGS} ${JSS_LIBRARY_FLAGS} + DEPENDS generate_c + ) + + add_custom_target( + generate_test_so + DEPENDS ${JSS_TESTS_SO_PATH} ) endmacro() @@ -213,19 +248,19 @@ macro(jss_build_jars) generate_jar DEPENDS "${JSS_JAR_PATH}" ) +endmacro() - if(WITH_TESTS) - add_custom_target( - generate_tests_jar - DEPENDS generate_java) +macro(jss_build_test_jars) + add_custom_target( + generate_tests_jar + DEPENDS generate_java) - add_custom_command( - TARGET generate_tests_jar - COMMAND "${Java_JAR_EXECUTABLE}" cmf "${CMAKE_BINARY_DIR}/MANIFEST.MF" ${JSS_TESTS_JAR_PATH} -C "${TESTS_CLASSES_OUTPUT_DIR}" org - ) + add_custom_command( + TARGET generate_tests_jar + COMMAND "${Java_JAR_EXECUTABLE}" cmf "${CMAKE_BINARY_DIR}/MANIFEST.MF" ${JSS_TESTS_JAR_PATH} -C "${TESTS_CLASSES_OUTPUT_DIR}" org + ) - add_dependencies(generate_jar generate_tests_jar) - endif(WITH_TESTS) + add_dependencies(generate_jar generate_tests_jar) endmacro() # Build javadocs from the source files diff --git a/cmake/JSSConfig.cmake b/cmake/JSSConfig.cmake index d6abbe015..8dd126c24 100644 --- a/cmake/JSSConfig.cmake +++ b/cmake/JSSConfig.cmake @@ -15,8 +15,10 @@ macro(jss_config) # Configure java-related flags jss_config_java() - # Configure test variables - jss_config_tests() + if(WITH_TESTS) + # Configure test variables + jss_config_tests() + endif(WITH_TESTS) # Check symbols to see what tests we run jss_config_symbols() @@ -227,34 +229,6 @@ macro(jss_config_java) SLF4J_JDK14_JAR NAMES jdk14 slf4j/jdk14 slf4j-jdk14 ) - find_jar( - JUNIT5_API_JAR - NAMES junit-jupiter-api junit5/junit-jupiter-api - ) - find_jar( - JUNIT5_ENGINE_JAR - NAMES junit-jupiter-engine junit5/junit-jupiter-engine - ) - find_jar( - JUNIT5_PLATFORM_COMMONS_JAR - NAMES junit-platform-commons junit5/junit-platform-commons - ) - find_jar( - JUNIT5_PLATFORM_ENGINE_JAR - NAMES junit-platform-engine junit5/junit-platform-engine - ) - find_jar( - JUNIT5_PLATFORM_LAUNCHER_JAR - NAMES junit-platform-launcher junit5/junit-platform-launcher - ) - find_jar( - OPENTEST4J_JAR - NAMES opentest4j opentest4j/opentest4j - ) - find_jar( - HAMCREST_JAR - NAMES hamcrest/core hamcrest-core hamcrest/hamcrest - ) # Validate that we've found the required JARs if(SLF4J_API_JAR STREQUAL "SLF4J_API_JAR-NOTFOUND") @@ -269,40 +243,9 @@ macro(jss_config_java) message(WARNING "Test dependency sfl4j-jdk14.jar not found by find_jar! Tests might not run properly.") endif() - if(JUNIT5_API_JAR STREQUAL "JUNIT5_API_JAR-NOTFOUND") - message(FATAL_ERROR "Test dependency JUnit 5 API not found by find_jar! Tests will not compile.") - endif() - - if(JUNIT5_ENGINE_JAR STREQUAL "JUNIT5_ENGINE_JAR-NOTFOUND") - message(WARNING "Test dependency JUnit 5 engine not found by find_jar! Tests could not execute.") - endif() - - if(JUNIT5_PLATFORM_COMMONS_JAR STREQUAL "JUNIT5_PLATFORM_COMMONS_JAR-NOTFOUND") - message(WARNING "Test dependency JUnit 5 platform commons not found by find_jar! Tests could not execute.") - endif() - - if(JUNIT5_PLATFORM_ENGINE_JAR STREQUAL "JUNIT5_PLATFORM_ENGINE_JAR-NOTFOUND") - message(WARNING "Test dependency JUnit 5 platform engine not found by find_jar! Tests could not execute.") - endif() - - if(JUNIT5_PLATFORM_LAUNCHER_JAR STREQUAL "JUNIT5_PLATFORM_LAUNCHER_JAR-NOTFOUND") - message(WARNING "Test dependency JUnit 5 platform launcher not found by find_jar! Tests could not execute.") - endif() - - if(OPENTEST4J_JAR STREQUAL "OPENTEST4J_JAR-NOTFOUND") - message(WARNING "Test dependency opentest4j not found by find_jar! Tests could not execute.") - endif() - - if(HAMCREST_JAR STREQUAL "HAMCREST_JAR-NOTFOUND") - message(WARNING "Test dependency hamcrest/core.jar not found by find_jar! Tests might not run properly.") - endif() - - # Set class paths + # Set class path set(JAVAC_CLASSPATH "${SLF4J_API_JAR}:${LANG_JAR}") - set(TEST_CLASSPATH "${JSS_JAR_PATH}:${JSS_TESTS_JAR_PATH}:${JAVAC_CLASSPATH}:${SLF4J_JDK14_JAR}:${JUNIT5_API_JAR}:${JUNIT5_ENGINE_JAR}:${JUNIT5_PLATFORM_COMMONS_JAR}:${JUNIT5_PLATFORM_ENGINE_JAR}:${JUNIT5_PLATFORM_LAUNCHER_JAR}:${OPENTEST4J_JAR}:${HAMCREST_JAR}") - message(STATUS "javac classpath: ${JAVAC_CLASSPATH}") - message(STATUS "tests classpath: ${TEST_CLASSPATH}") # Set compile flags for JSS list(APPEND JSS_JAVAC_FLAGS "-classpath") @@ -329,40 +272,13 @@ macro(jss_config_java) list(APPEND JSS_JAVAC_FLAGS "-O") endif() - # Set compile flags for JSS test suite - list(APPEND JSS_TEST_JAVAC_FLAGS "-classpath") - list(APPEND JSS_TEST_JAVAC_FLAGS "${JAVAC_CLASSPATH}:${JUNIT5_API_JAR}:${JUNIT5_PLATFORM_COMMONS_JAR}:${JUNIT5_PLATFORM_ENGINE_JAR}:${JUNIT5_PLATFORM_LAUNCHER_JAR}:${CLASSES_OUTPUT_DIR}") - list(APPEND JSS_TEST_JAVAC_FLAGS "-sourcepath") - list(APPEND JSS_TEST_JAVAC_FLAGS "${PROJECT_SOURCE_DIR}/base/src/main/java:${PROJECT_SOURCE_DIR}/base/src/test/java") - - # Ensure we're compatible with JDK 17 - list(APPEND JSS_TEST_JAVAC_FLAGS "-target") - list(APPEND JSS_TEST_JAVAC_FLAGS "17") - list(APPEND JSS_TEST_JAVAC_FLAGS "-source") - list(APPEND JSS_TEST_JAVAC_FLAGS "17") - # Handle passed-in javac flags as well; assume they are valid. separate_arguments(PASSED_JAVAC_FLAGS UNIX_COMMAND "$ENV{JAVACFLAGS}") - foreach(PASSED_JAVAC_FLAG ${PASSED_JAVAC_FLAGS}) - list(APPEND JSS_TEST_JAVAC_FLAGS "${PASSED_JAVAC_FLAG}") - endforeach() - - if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") - list(APPEND JSS_TEST_JAVAC_FLAGS "-g") - else() - list(APPEND JSS_TEST_JAVAC_FLAGS "-O") - endif() - message(STATUS "JSS JAVAC FLAGS: ${JSS_JAVAC_FLAGS}") - message(STATUS "JSS TEST JAVAC FLAGS: ${JSS_TEST_JAVAC_FLAGS}") # Variables for javadoc building. set(JSS_WINDOW_TITLE "JSS: Java Security Services") - set(JSS_BASE_PORT 2876) - math(EXPR JSS_TEST_PORT_CLIENTAUTH ${JSS_BASE_PORT}+0) - math(EXPR JSS_TEST_PORT_CLIENTAUTH_FIPS ${JSS_BASE_PORT}+1) - # Create META-INF directory for provider file(MAKE_DIRECTORY "${CLASSES_OUTPUT_DIR}/META-INF/services") endmacro() @@ -507,6 +423,96 @@ macro(jss_config_symbols) endmacro() macro(jss_config_tests) + + find_jar( + JUNIT5_API_JAR + NAMES junit-jupiter-api junit5/junit-jupiter-api + ) + find_jar( + JUNIT5_ENGINE_JAR + NAMES junit-jupiter-engine junit5/junit-jupiter-engine + ) + find_jar( + JUNIT5_PLATFORM_COMMONS_JAR + NAMES junit-platform-commons junit5/junit-platform-commons + ) + find_jar( + JUNIT5_PLATFORM_ENGINE_JAR + NAMES junit-platform-engine junit5/junit-platform-engine + ) + find_jar( + JUNIT5_PLATFORM_LAUNCHER_JAR + NAMES junit-platform-launcher junit5/junit-platform-launcher + ) + find_jar( + OPENTEST4J_JAR + NAMES opentest4j opentest4j/opentest4j + ) + find_jar( + HAMCREST_JAR + NAMES hamcrest/core hamcrest-core hamcrest/hamcrest + ) + + if(JUNIT5_API_JAR STREQUAL "JUNIT5_API_JAR-NOTFOUND") + message(FATAL_ERROR "Test dependency JUnit 5 API not found by find_jar! Tests will not compile.") + endif() + + if(JUNIT5_ENGINE_JAR STREQUAL "JUNIT5_ENGINE_JAR-NOTFOUND") + message(WARNING "Test dependency JUnit 5 engine not found by find_jar! Tests could not execute.") + endif() + + if(JUNIT5_PLATFORM_COMMONS_JAR STREQUAL "JUNIT5_PLATFORM_COMMONS_JAR-NOTFOUND") + message(WARNING "Test dependency JUnit 5 platform commons not found by find_jar! Tests could not execute.") + endif() + + if(JUNIT5_PLATFORM_ENGINE_JAR STREQUAL "JUNIT5_PLATFORM_ENGINE_JAR-NOTFOUND") + message(WARNING "Test dependency JUnit 5 platform engine not found by find_jar! Tests could not execute.") + endif() + + if(JUNIT5_PLATFORM_LAUNCHER_JAR STREQUAL "JUNIT5_PLATFORM_LAUNCHER_JAR-NOTFOUND") + message(WARNING "Test dependency JUnit 5 platform launcher not found by find_jar! Tests could not execute.") + endif() + + if(OPENTEST4J_JAR STREQUAL "OPENTEST4J_JAR-NOTFOUND") + message(WARNING "Test dependency opentest4j not found by find_jar! Tests could not execute.") + endif() + + if(HAMCREST_JAR STREQUAL "HAMCREST_JAR-NOTFOUND") + message(WARNING "Test dependency hamcrest/core.jar not found by find_jar! Tests might not run properly.") + endif() + + # Set test class path + set(TEST_CLASSPATH "${JSS_JAR_PATH}:${JSS_TESTS_JAR_PATH}:${JAVAC_CLASSPATH}:${SLF4J_JDK14_JAR}:${JUNIT5_API_JAR}:${JUNIT5_ENGINE_JAR}:${JUNIT5_PLATFORM_COMMONS_JAR}:${JUNIT5_PLATFORM_ENGINE_JAR}:${JUNIT5_PLATFORM_LAUNCHER_JAR}:${OPENTEST4J_JAR}:${HAMCREST_JAR}") + message(STATUS "tests classpath: ${TEST_CLASSPATH}") + + # Set compile flags for JSS test suite + list(APPEND JSS_TEST_JAVAC_FLAGS "-classpath") + list(APPEND JSS_TEST_JAVAC_FLAGS "${JAVAC_CLASSPATH}:${JUNIT5_API_JAR}:${JUNIT5_PLATFORM_COMMONS_JAR}:${JUNIT5_PLATFORM_ENGINE_JAR}:${JUNIT5_PLATFORM_LAUNCHER_JAR}:${CLASSES_OUTPUT_DIR}") + list(APPEND JSS_TEST_JAVAC_FLAGS "-sourcepath") + list(APPEND JSS_TEST_JAVAC_FLAGS "${PROJECT_SOURCE_DIR}/base/src/main/java:${PROJECT_SOURCE_DIR}/base/src/test/java") + + # Ensure we're compatible with JDK 17 + list(APPEND JSS_TEST_JAVAC_FLAGS "-target") + list(APPEND JSS_TEST_JAVAC_FLAGS "17") + list(APPEND JSS_TEST_JAVAC_FLAGS "-source") + list(APPEND JSS_TEST_JAVAC_FLAGS "17") + + foreach(PASSED_JAVAC_FLAG ${PASSED_JAVAC_FLAGS}) + list(APPEND JSS_TEST_JAVAC_FLAGS "${PASSED_JAVAC_FLAG}") + endforeach() + + if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") + list(APPEND JSS_TEST_JAVAC_FLAGS "-g") + else() + list(APPEND JSS_TEST_JAVAC_FLAGS "-O") + endif() + + message(STATUS "JSS TEST JAVAC FLAGS: ${JSS_TEST_JAVAC_FLAGS}") + + set(JSS_BASE_PORT 2876) + math(EXPR JSS_TEST_PORT_CLIENTAUTH ${JSS_BASE_PORT}+0) + math(EXPR JSS_TEST_PORT_CLIENTAUTH_FIPS ${JSS_BASE_PORT}+1) + # Common variables used as arguments to several tests set(JSS_TEST_DIR "${PROJECT_SOURCE_DIR}/base/src/test/java/org/mozilla/jss/tests") set(PASSWORD_FILE "${JSS_TEST_DIR}/passwords") diff --git a/cmake/JSSTests.cmake b/cmake/JSSTests.cmake index 5cdd484a9..a4c2355f4 100644 --- a/cmake/JSSTests.cmake +++ b/cmake/JSSTests.cmake @@ -481,7 +481,7 @@ macro(jss_tests_compile_c C_FILE C_OUTPUT C_TARGET) DEPENDS "${C_FILE}" DEPENDS "${JSS_TESTS_SO_PATH}" DEPENDS generate_java - DEPENDS generate_includes + DEPENDS generate_test_includes ) add_custom_target( @@ -489,7 +489,7 @@ macro(jss_tests_compile_c C_FILE C_OUTPUT C_TARGET) DEPENDS "${C_OUTPUT}" ) - add_dependencies("generate_so" "generate_c_${C_TARGET}") + add_dependencies("generate_test_so" "generate_c_${C_TARGET}") endmacro() function(jss_test_java) diff --git a/symkey/CMakeLists.txt b/symkey/CMakeLists.txt index e52429834..6dc4d534a 100644 --- a/symkey/CMakeLists.txt +++ b/symkey/CMakeLists.txt @@ -30,6 +30,10 @@ add_library(${SYMKEY_SHARED_LIBRARY} SHARED add_dependencies(${SYMKEY_SHARED_LIBRARY} generate_so) +if(WITH_TESTS) + add_dependencies(${SYMKEY_SHARED_LIBRARY} generate_test_so) +endif(WITH_TESTS) + set_target_properties(${SYMKEY_SHARED_LIBRARY} PROPERTIES OUTPUT_NAME diff --git a/tools/Dockerfiles/fedora_rawhide b/tools/Dockerfiles/fedora_rawhide index 9dae7d035..cedd2996e 100644 --- a/tools/Dockerfiles/fedora_rawhide +++ b/tools/Dockerfiles/fedora_rawhide @@ -7,7 +7,7 @@ RUN true \ java-17-openjdk nss-tools \ apache-commons-lang3 gcc-c++ java-17-openjdk-devel \ jpackage-utils slf4j nss zlib-devel nss-devel \ - nspr-devel slf4j-jdk14 junit \ + nspr-devel slf4j-jdk14 junit5 \ && mkdir -p /home/sandbox \ && dnf clean -y all \ && rm -rf /usr/share/doc /usr/share/doc-base \