Files
jwt-cpp/tests/CMakeLists.txt
Chris Mc a6927cb814 Add support for CMake Presets (#348)
* dev container + vscode extensions

* exploring cmake presets for CI

* ignore user presets

* add configure to build preset

windows ran in debug, which means the multi config needs more help

* coverage preset

* creating a fuzzing preset

* fuzzing fix cwd + more configs to preset

* move cmake set vars to right section

https://learn.microsoft.com/en-us/cpp/build/cmake-presets-vs?view=msvc-170

"You can set C and C++ compilers by using cacheVariables.CMAKE_C_COMPILER and cacheVariables.CMAKE_CXX_COMPILER in a Configure Preset. It's equivalent to passing -D CMAKE_C_COMPILER=<value> and -D CMAKE_CXX_COMPILER=<value> to CMake from the command line."

* fix workdir for new fuzz run targets

* split up presets into more files

* fix include order (liking this less)

* add asan to presets

* fix copy paste

* add asan build preset

* move ubsan to presets

* replace mustache with a cmake script

* touch ups

* restore file to void merge conflicts

* fix order for args

* remove action wrapper

more quotes for inputs

* revert bad render of boost_json

* fix casing

* format changes

* test to see if output is working

* last try

* git revert moving mustache to cmake

* cmake not enough for presets

* drop ci prefix

* new test for `library_ROOT` search path

* install with presets

* cmake --install does not take a preset

* install with preset is not yet supported

https://gitlab.kitware.com/cmake/cmake/-/issues/23208

* add missing build step

* more global fuzzing flags to toolchain file

* fixup toolchain in presets

* fix pathing and limit configs to tested platforms

* clean up + use presets for clang-tidy

* dry up sanitizer flags

* try install with a preset

* remove duplicated test

* notes about hunter test

* skip checking the compiler since this causes errors with the same CMake uses in combinations with the fuzz sanitizer flag

https://github.com/prince-chrismc/jwt-cpp/actions/runs/7481024827/job/20361722539?pr=33

```
    /usr/bin/ld: /usr/bin/ld: DWARF error: invalid or unhandled FORM value: 0x22
    CMakeFiles/cmTC_ff7c6.dir/testCCompiler.c.o: in function `main':
    testCCompiler.c:(.text.main[main]+0x0): multiple definition of `main'; /usr/lib/llvm-14/lib/clang/14.0.0/lib/linux/libclang_rt.fuzzer-x86_64.a(FuzzerMain.cpp.o):(.text.main+0x0): first defined here
    /usr/bin/ld: /usr/lib/llvm-14/lib/clang/14.0.0/lib/linux/libclang_rt.fuzzer-x86_64.a(FuzzerMain.cpp.o): in function `main':
    (.text.main+0x12): undefined reference to `LLVMFuzzerTestOneInput'
```

* remove system name to disable cross compiling

* tests do not pass clang-tidy

* drop toolchain, use target specific flags

* leave flags in cmake

* missing deps + clean apt files

* remove some code duplication

* fix typo

* devcontainers fix, test and add wolfssl

refactored installer to have a script that could be reused

* add missing build context

* pass version to script

* dont use bash syntax

* fix typo

* upload tar.gz for new releases

* revert name to manually trigger it

* Update and rename nuget.yml to release.yml

* disable nuget for testing

* add a zip as well

* back to basics

* remove remove testing code

* update workflow name

* enable testing needs to be in the root

* add a test preset and use it in some workflows

* fix linting tests

* bump github actions

* bump python setup

* drop dev containers

* dont install openssl on windows

* revert merge conflicts

* fix whitespace

* fix private claims example to have a valid token

#351

* put back versioned format

I dont remember why, it looks like it was debugging
2024-06-15 07:52:23 -07:00

86 lines
3.5 KiB
CMake

if(JWT_DISABLE_BASE64)
message(FATAL_ERROR "Tests requires the base64 support to be enabled!")
endif()
if(JWT_DISABLE_PICOJSON)
message(FATAL_ERROR "Tests requires the picojson support to be enabled!")
endif()
include(GoogleTest)
if(HUNTER_ENABLED)
hunter_add_package(GTest)
endif()
find_package(GTest)
if(NOT TARGET GTest::gtest_main)
message(STATUS "jwt-cpp: using FetchContent for GTest")
include(FetchContent)
fetchcontent_declare(googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip)
# https://google.github.io/googletest/quickstart-cmake.html
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
fetchcontent_makeavailable(googletest)
endif()
set(TEST_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/BaseTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ClaimTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Keys.cpp ${CMAKE_CURRENT_SOURCE_DIR}/HelperTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TestMain.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TokenFormatTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TokenTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/JwksTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/OpenSSLErrorTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/traits/NlohmannTest.cpp)
find_package(jsoncons CONFIG)
if(TARGET jsoncons)
list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/traits/JsonconsTest.cpp)
endif()
include("private-find-boost-json")
if(TARGET boost_json)
list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/traits/BoostJsonTest.cpp)
endif()
find_package(jsoncpp CONFIG)
if(TARGET jsoncpp_static)
list(APPEND TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/traits/OspJsoncppTest.cpp)
endif()
add_executable(jwt-cpp-test ${TEST_SOURCES})
# NOTE: Don't use space inside a generator expression here, because the function prematurely breaks the expression into
# multiple lines. https://cmake.org/pipermail/cmake/2018-April/067422.html
set(JWT_TESTER_GCC_FLAGS -Wall -Wextra -Wpedantic -ggdb)
set(JWT_TESTER_CLANG_FLAGS -Weverything -Wno-c++98-compat -Wno-global-constructors -Wno-weak-vtables)
target_compile_options(
jwt-cpp-test PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/W4> $<$<CXX_COMPILER_ID:GNU>:${JWT_TESTER_GCC_FLAGS}>
$<$<CXX_COMPILER_ID:Clang>:${JWT_TESTER_CLANG_FLAGS}>)
if(HUNTER_ENABLED)
target_link_libraries(jwt-cpp-test PRIVATE GTest::gtest GTest::gtest_main)
# Define a compile define to bypass openssl error tests
target_compile_definitions(jwt-cpp-test PRIVATE HUNTER_ENABLED=1)
else()
# https://github.com/google/googletest/blob/eb80f759d595874a5e905a3342bd8e2af4c0a12d/googletest/README.md?plain=1#L62-L64
target_link_libraries(jwt-cpp-test PRIVATE GTest::gtest_main)
if(TARGET jsoncons)
target_link_libraries(jwt-cpp-test PRIVATE jsoncons)
endif()
if(TARGET boost_json)
target_link_libraries(jwt-cpp-test PRIVATE boost_json)
endif()
if(TARGET jsoncpp_static)
target_link_libraries(jwt-cpp-test PRIVATE jsoncpp_static)
endif()
endif()
target_link_libraries(jwt-cpp-test PRIVATE jwt-cpp nlohmann_json::nlohmann_json
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:${CMAKE_DL_LIBS}>)
gtest_discover_tests(jwt-cpp-test)
add_custom_target(jwt-cpp-test-run COMMAND jwt-cpp-test)
if(JWT_ENABLE_COVERAGE)
include("code-coverage")
setup_coverage(jwt-cpp-test)
set(COVERAGE_EXCLUDES "/usr/**" "/home/*/.conan/**" "*test*" "*build*" "**/nlohmann/json.hpp"
"**/picojson/picojson.h" "*boost*" "*jsoncons*")
setup_target_for_coverage_lcov(NAME coverage EXECUTABLE ${CMAKE_CURRENT_BINARY_DIR}/jwt-cpp-test)
endif()