From 20cba2ecd9009b0c1c1a32f9c47845a7d90d3075 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sat, 21 Jan 2023 17:43:22 +0900 Subject: [PATCH] Support CTest (#1468) * Add test/CMakeLists.txt to enable testing with CTest Add HTTPLIB_TEST option Downlaod GoogleTest source using FetchContent module Generate cert files to test httplib with OpenSSL * Generate cert2.pem with a new certificate request * Use the latest GoogleTest library --- CMakeLists.txt | 8 +++- test/CMakeLists.txt | 93 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 test/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 709390a..e5e2ae3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ * HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on) * HTTPLIB_REQUIRE_BROTLI (default off) * HTTPLIB_COMPILE (default off) + * HTTPLIB_TEST (default off) * BROTLI_USE_STATIC_LIBS - tells Cmake to use the static Brotli libs (only works if you have them installed). * OPENSSL_USE_STATIC_LIBS - tells Cmake to use the static OpenSSL libs (only works if you have them installed). @@ -88,7 +89,7 @@ option(HTTPLIB_COMPILE "If ON, uses a Python script to split the header into a c if(HTTPLIB_COMPILE) set(HTTPLIB_IS_COMPILED TRUE) endif() - +option(HTTPLIB_TEST "Enables testing and builds tests" OFF) option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF) option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON) # Defaults to static library @@ -278,3 +279,8 @@ install(EXPORT httplibTargets NAMESPACE ${PROJECT_NAME}:: DESTINATION ${_TARGET_INSTALL_CMAKEDIR} ) + +if(HTTPLIB_TEST) + include(CTest) + add_subdirectory(test) +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..eb97912 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,93 @@ +cmake_policy(SET CMP0135 NEW) + +include(FetchContent) +include(GoogleTest) + +set(BUILD_GMOCK OFF) +set(INSTALL_GTEST OFF) +set(gtest_force_shared_crt ON) + +FetchContent_Declare( + gtest + URL https://github.com/google/googletest/archive/main.tar.gz +) +FetchContent_MakeAvailable(gtest) + +add_executable(httplib-test test.cc) +target_compile_options(httplib-test PRIVATE "$<$:/utf-8>") +target_link_libraries(httplib-test PRIVATE httplib GTest::gtest_main) +gtest_discover_tests(httplib-test) + +execute_process( + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/www www + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/www2 www2 + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/www3 www3 + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_LIST_DIR}/ca-bundle.crt ca-bundle.crt + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_LIST_DIR}/image.jpg image.jpg + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY +) + +if(HTTPLIB_IS_USING_OPENSSL) + find_program(OPENSSL_COMMAND + NAMES openssl + PATHS ${OPENSSL_INCLUDE_DIR}/../bin + REQUIRED + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} genrsa 2048 + OUTPUT_FILE key.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} req -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem + COMMAND ${OPENSSL_COMMAND} x509 -days 3650 -req -signkey key.pem + OUTPUT_FILE cert.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} req -x509 -new -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} genrsa 2048 + OUTPUT_FILE rootCA.key.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} req -x509 -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.rootCA.conf -key rootCA.key.pem -days 1024 + OUTPUT_FILE rootCA.cert.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} genrsa 2048 + OUTPUT_FILE client.key.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} req -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key client.key.pem + COMMAND ${OPENSSL_COMMAND} x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial + OUTPUT_FILE client.cert.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} genrsa -passout pass:test123! 2048 + OUTPUT_FILE key_encrypted.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} req -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key_encrypted.pem + COMMAND ${OPENSSL_COMMAND} x509 -days 3650 -req -signkey key_encrypted.pem + OUTPUT_FILE cert_encrypted.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) +endif()