diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 9687703..12e8a88 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -22,10 +22,8 @@ jobs: - name: setup run: | - mkdir build - cd build - cmake .. -DJWT_BUILD_EXAMPLES=OFF - sudo make install + cmake --preset release + sudo cmake --build --preset release --target install - name: test working-directory: tests/cmake @@ -95,6 +93,29 @@ jobs: cmake . -DTEST:STRING="defaults-enabled" cmake --build . + preset-support-but-not-enough: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - uses: ./.github/actions/install/cmake + with: + version: "3.24.4" + url: "https://cmake.org/files/v3.24/cmake-3.24.4.tar.gz" + - uses: ./.github/actions/install/gtest + + - name: setup + run: | + mkdir build + cd build + cmake .. -DJWT_BUILD_EXAMPLES=ON -DJWT_BUILD_TESTS=ON + sudo make install + + - name: test + run: | + cd tests/cmake + cmake . -DTEST:STRING="defaults-enabled" + cmake --build . + custom-install-linux: runs-on: ubuntu-latest steps: @@ -117,7 +138,7 @@ jobs: root-hint-install-linux: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: lukka/get-cmake@latest - name: setup diff --git a/.github/workflows/cross-platform.yml b/.github/workflows/cross-platform.yml index 7d0a7a0..a4f23a3 100644 --- a/.github/workflows/cross-platform.yml +++ b/.github/workflows/cross-platform.yml @@ -14,22 +14,17 @@ jobs: steps: - uses: actions/checkout@v4 - - run: cmake -E make_directory ${{ github.workspace }}/build - name: configure - shell: bash # access regardless of the host operating system - working-directory: ${{ github.workspace }}/build - run: cmake $GITHUB_WORKSPACE -DJWT_BUILD_EXAMPLES=ON + run: cmake --preset examples - name: build - working-directory: ${{ github.workspace }}/build - shell: bash - run: cmake --build . + run: cmake --build --preset examples - name: test run: | - cmake --build build/ --target rsa-create-run - cmake --build build/ --target rsa-verify-run + cmake --build --preset examples --target rsa-create-run + cmake --build --preset examples --target rsa-verify-run - if: github.event_name == 'push' && always() uses: ./.github/actions/badge diff --git a/.github/workflows/jwt.yml b/.github/workflows/jwt.yml index 2291cbb..46eed97 100644 --- a/.github/workflows/jwt.yml +++ b/.github/workflows/jwt.yml @@ -18,14 +18,9 @@ jobs: - uses: ./.github/actions/install/open-source-parsers-jsoncpp - name: configure - run: | - mkdir build - cd build - cmake .. -DJWT_BUILD_EXAMPLES=OFF -DJWT_BUILD_TESTS=ON -DJWT_ENABLE_COVERAGE=ON -DCMAKE_BUILD_TYPE=Debug - + run: cmake --preset coverage - name: run - working-directory: build - run: make jwt-cpp-test coverage + run: cmake --build --preset coverage - uses: coverallsapp/github-action@v2 with: @@ -38,21 +33,19 @@ jobs: steps: - uses: actions/checkout@v4 - uses: lukka/get-cmake@latest - - uses: ./.github/actions/install/gtest - name: configure - run: | - mkdir build - cd build - cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DJWT_ENABLE_FUZZING=ON + run: cmake --preset ci-fuzzing + - name: build + run: cmake --build --preset ci-fuzzing - name: run run: | - cmake --build build/ --target jwt-cpp-fuzz-BaseEncodeFuzz-run - cmake --build build/ --target jwt-cpp-fuzz-BaseDecodeFuzz-run - cmake --build build/ --target jwt-cpp-fuzz-TokenDecodeFuzz-run + cmake --build --preset ci-fuzzing --target jwt-cpp-fuzz-BaseEncodeFuzz-run + cmake --build --preset ci-fuzzing --target jwt-cpp-fuzz-BaseDecodeFuzz-run + cmake --build --preset ci-fuzzing --target jwt-cpp-fuzz-TokenDecodeFuzz-run - asan: # Based on https://gist.github.com/jlblancoc/44be9d4d466f0a973b1f3808a8e56782 + asan: runs-on: ubuntu-latest strategy: fail-fast: false @@ -69,46 +62,34 @@ jobs: version: ${{ matrix.openssl.tag }} - name: configure - run: | - mkdir build - cd build - cmake .. -DJWT_BUILD_TESTS=ON -DCMAKE_CXX_FLAGS="-fsanitize=address -fsanitize=leak -g" \ - -DCMAKE_C_FLAGS="-fsanitize=address -fsanitize=leak -g" \ - -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address -fsanitize=leak" \ - -DCMAKE_MODULE_LINKER_FLAGS="-fsanitize=address -fsanitize=leak" + run: cmake --preset ci-asan + - name: build + run: cmake --build --preset ci-asan - name: run - working-directory: build run: | - make - export ASAN_OPTIONS=check_initialization_order=true:detect_stack_use_after_return=true:fast_unwind_on_malloc=0 - ./example/rsa-create - ./example/rsa-verify - ./example/jwks-verify - ./tests/jwt-cpp-test + cmake --build --preset ci-asan --target private-claims-run + cmake --build --preset ci-asan --target rsa-create-run + cmake --build --preset ci-asan --target rsa-verify-run + cmake --build --preset ci-asan --target jwks-verify-run + cmake --build --preset ci-asan --target jwt-cpp-test-run ubsan: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: lukka/get-cmake@latest - uses: ./.github/actions/install/gtest - name: configure - run: | - mkdir build - cd build - cmake .. -DJWT_BUILD_TESTS=ON -DCMAKE_CXX_STANDARD=20 \ - -DCMAKE_CXX_FLAGS="-fsanitize=undefined -fsanitize=return -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize-recover=all -g" \ - -DCMAKE_C_FLAGS="-fsanitize=undefined -fsanitize=return -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize-recover=all -g" \ - -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=undefined -fsanitize=return -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize-recover=all" \ - -DCMAKE_MODULE_LINKER_FLAGS="-fsanitize=undefined -fsanitize=return -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize-recover=all" + run: cmake --preset ci-ubsan + - name: build + run: cmake --build --preset ci-ubsan -DCMAKE_CXX_STANDARD=20 - name: run - working-directory: build run: | - make - export UBSAN_OPTIONS=print_stacktrace=1 - ./example/rsa-create - ./example/rsa-verify - ./tests/jwt-cpp-test + cmake --build --preset ci-ubsan --target private-claims-run + cmake --build --preset ci-ubsan --target rsa-create-run + cmake --build --preset ci-ubsan --target rsa-verify-run + cmake --build --preset ci-ubsan --target jwks-verify-run + cmake --build --preset ci-ubsan --target jwt-cpp-test-run diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index ced7ca4..6d8410b 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -36,7 +36,7 @@ jobs: matrix: files: ["**/CMakeLists.txt", "cmake/code-coverage.cmake"] steps: - - uses: actions/setup-python@v2 + - uses: actions/setup-python@v5 with: python-version: "3.x" - run: pip install cmakelang @@ -54,13 +54,9 @@ jobs: - uses: lukka/get-cmake@latest - uses: actions/checkout@v4 - name: configure - run: | - mkdir build - cd build - cmake .. -DCMAKE_CXX_CLANG_TIDY="clang-tidy;-fix" + run: cmake --preset examples -DCMAKE_CXX_CLANG_TIDY="clang-tidy;-fix" - name: run - working-directory: build - run: make + run: cmake --build --preset examples - uses: ./.github/actions/process-linting-results with: linter_name: clang-tidy diff --git a/.github/workflows/ssl.yml b/.github/workflows/ssl.yml index a60c6f7..d96e3dd 100644 --- a/.github/workflows/ssl.yml +++ b/.github/workflows/ssl.yml @@ -27,10 +27,10 @@ jobs: version: ${{ matrix.openssl.tag }} - name: configure - run: cmake . -DJWT_BUILD_TESTS=ON -DOPENSSL_ROOT_DIR=/tmp - - run: make + run: cmake --preset unit-tests -DOPENSSL_ROOT_DIR=/tmp + - run: cmake --build --preset unit-tests - name: test - run: ./tests/jwt-cpp-test + run: ctest --preset unit-tests - if: github.event_name == 'push' && always() uses: ./.github/actions/badge @@ -50,8 +50,10 @@ jobs: version: "openssl-3.0.5" - name: configure - run: cmake . -DJWT_BUILD_TESTS=ON -DOPENSSL_ROOT_DIR=/tmp -DCMAKE_CXX_FLAGS="-DOPENSSL_NO_DEPRECATED=1" -DCMAKE_C_FLAGS="-DOPENSSL_NO_DEPRECATED=1" - - run: make + run: cmake --preset unit-tests -DOPENSSL_ROOT_DIR=/tmp -DCMAKE_CXX_FLAGS="-DOPENSSL_NO_DEPRECATED=1" -DCMAKE_C_FLAGS="-DOPENSSL_NO_DEPRECATED=1" + - run: cmake --build --preset unit-tests + - name: test + run: ctest --preset unit-tests libressl: runs-on: ubuntu-latest @@ -69,10 +71,10 @@ jobs: version: ${{ matrix.libressl }} - name: configure - run: cmake . -DJWT_BUILD_TESTS=ON -DJWT_SSL_LIBRARY:STRING=LibreSSL - - run: make + run: cmake --preset unit-tests -DJWT_SSL_LIBRARY:STRING=LibreSSL + - run: cmake --build --preset unit-tests - name: test - run: ./tests/jwt-cpp-test + run: ctest --preset unit-tests - if: github.event_name == 'push' && always() uses: ./.github/actions/badge @@ -98,10 +100,10 @@ jobs: version: ${{ matrix.wolfssl.ref }} - name: configure - run: cmake . -DJWT_BUILD_TESTS=ON -DJWT_SSL_LIBRARY:STRING=wolfSSL - - run: make + run: cmake --preset unit-tests -DJWT_SSL_LIBRARY:STRING=wolfSSL + - run: cmake --build --preset unit-tests - name: test - run: ./tests/jwt-cpp-test + run: ctest --preset unit-tests - if: github.event_name == 'push' && always() uses: ./.github/actions/badge diff --git a/.gitignore b/.gitignore index dc994bd..a594c29 100644 --- a/.gitignore +++ b/.gitignore @@ -306,9 +306,15 @@ $RECYCLE.BIN/ test *.o *.o.d -.vscode + +# IDE-specific files +.vscode/ +.vscode/!extensions.json # Allow to provide recommended extensions + # ClangD cache files .cache build/* package-lock.json + +CMakeUserPresets.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..59e6f7d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + "recommendations": [ + "ms-vscode.cpptools", + "ms-vscode.cpptools-extension-pack", + "ms-vscode.cmake-tools", + "twxs.cmake", + "matepek.vscode-catch2-test-adapter", + "GitHub.vscode-github-actions" + ] +} diff --git a/CMakeLists.txt b/CMakeLists.txt index ad9632a..474605c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,6 +158,7 @@ if(JWT_BUILD_EXAMPLES) endif() if(JWT_BUILD_TESTS) + enable_testing() add_subdirectory(tests) endif() diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..03dc922 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,33 @@ +{ + "version": 6, + "cmakeMinimumRequired": { + "major": 3, + "minor": 25, + "patch": 0 + }, + "include": [ + "example/CMakePresets.json", + "tests/CMakePresets.json" + ], + "configurePresets": [ + { + "name": "dev", + "displayName": "Development", + "inherits": "debug", + "environment": { + "CMAKE_EXPORT_COMPILE_COMMANDS": "ON" + }, + "cacheVariables": { + "JWT_BUILD_EXAMPLES": "ON", + "JWT_BUILD_TESTS": "ON" + } + } + ], + "buildPresets": [ + { + "name": "dev", + "configurePreset": "dev", + "configuration": "Debug" + } + ] +} \ No newline at end of file diff --git a/cmake/CMakePresets.json b/cmake/CMakePresets.json new file mode 100644 index 0000000..8c4fb85 --- /dev/null +++ b/cmake/CMakePresets.json @@ -0,0 +1,48 @@ +{ + "version": 6, + "cmakeMinimumRequired": { + "major": 3, + "minor": 25, + "patch": 0 + }, + "configurePresets": [ + { + "name": "default", + "displayName": "Default Config", + "hidden": true, + "binaryDir": "${sourceDir}/build", + "cacheVariables": { + "JWT_BUILD_EXAMPLES": "OFF", + "JWT_BUILD_TESTS": "OFF" + } + }, + { + "name": "debug", + "displayName": "Debug", + "inherits": "default", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + }, + { + "name": "release", + "displayName": "Release", + "inherits": "default", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + } + ], + "buildPresets": [ + { + "name": "debug", + "configurePreset": "debug", + "configuration": "Debug" + }, + { + "name": "release", + "configurePreset": "release", + "configuration": "Release" + } + ] +} \ No newline at end of file diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 94b749e..3f87f42 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -17,6 +17,7 @@ add_custom_target(print-claims-run COMMAND print-claims) add_executable(private-claims private-claims.cpp) target_link_libraries(private-claims jwt-cpp::jwt-cpp) +add_custom_target(private-claims-run COMMAND private-claims) add_executable(rsa-create rsa-create.cpp) target_link_libraries(rsa-create jwt-cpp::jwt-cpp) diff --git a/example/CMakePresets.json b/example/CMakePresets.json new file mode 100644 index 0000000..b483883 --- /dev/null +++ b/example/CMakePresets.json @@ -0,0 +1,32 @@ +{ + "version": 6, + "cmakeMinimumRequired": { + "major": 3, + "minor": 25, + "patch": 0 + }, + "include": [ + "../cmake/CMakePresets.json" + ], + "configurePresets": [ + { + "name": "examples", + "displayName": "Build Examples", + "inherits": "release", + "cacheVariables": { + "JWT_BUILD_EXAMPLES": "ON" + } + } + ], + "buildPresets": [ + { + "name": "examples", + "configurePreset": "examples", + "configuration": "Release", + "targets": [ + "rsa-create", + "rsa-verify" + ] + } + ] +} \ No newline at end of file diff --git a/example/private-claims.cpp b/example/private-claims.cpp index 730cf40..cd4375c 100644 --- a/example/private-claims.cpp +++ b/example/private-claims.cpp @@ -21,7 +21,7 @@ int main() { .set_issuer("auth.mydomain.io") .set_audience("mydomain.io") .set_issued_at(time) - .set_not_before(time + sec{15}) + .set_not_before(time - sec{15}) .set_expires_at(time + sec{15} + min{2}) .set_payload_claim("boolean", picojson::value(true)) .set_payload_claim("integer", picojson::value(int64_t{12345})) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 356aa3b..6c7b755 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,7 +6,6 @@ if(JWT_DISABLE_PICOJSON) message(FATAL_ERROR "Tests requires the picojson support to be enabled!") endif() -enable_testing() include(GoogleTest) if(HUNTER_ENABLED) hunter_add_package(GTest) diff --git a/tests/CMakePresets.json b/tests/CMakePresets.json new file mode 100644 index 0000000..ee1af5b --- /dev/null +++ b/tests/CMakePresets.json @@ -0,0 +1,146 @@ +{ + "version": 6, + "cmakeMinimumRequired": { + "major": 3, + "minor": 25, + "patch": 0 + }, + "include": [ + "../cmake/CMakePresets.json" + ], + "configurePresets": [ + { + "name": "linux", + "hidden": true, + "description": "This is only available on Linux", + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Linux" + } + }, + { + "name": "coverage", + "inherits": ["debug", "linux"], + "displayName": "Coverage", + "description": "This is only available on Linux", + "cacheVariables": { + "JWT_BUILD_TESTS": "ON", + "JWT_ENABLE_COVERAGE": "ON" + } + }, + { + "name": "unit-tests", + "inherits": ["debug"], + "displayName": "Unit Tests", + "cacheVariables": { + "JWT_BUILD_TESTS": "ON" + } + }, + { + "name": "ci-fuzzing", + "inherits": ["debug", "linux"], + "displayName": "Fuzz Testing", + "description": "This is only available on Linux with Clang", + "cacheVariables": { + "JWT_ENABLE_FUZZING": "ON", + "CMAKE_C_COMPILER": "clang", + "CMAKE_CXX_COMPILER": "clang++" + } + }, + { + "name": "ci-asan", + "inherits": ["debug", "linux"], + "displayName": "Address/Leak Sanitizer", + "description": "This is only available on Linux", + "environment": { + "ASAN_FLAGS": "-fsanitize=address -fsanitize=leak", + "ASAN_OPTIONS": "check_initialization_order=true:detect_stack_use_after_return=true:fast_unwind_on_malloc=0" + }, + "cacheVariables": { + "JWT_BUILD_EXAMPLES": "ON", + "JWT_BUILD_TESTS": "ON", + "CMAKE_C_FLAGS": "$env{ASAN_FLAGS}", + "CMAKE_CXX_FLAGS": "$env{ASAN_FLAGS}", + "CMAKE_EXE_LINKER_FLAGS": "$env{ASAN_FLAGS}", + "CMAKE_MODULE_LINKER_FLAGS": "$env{ASAN_FLAGS}" + } + }, + { + "name": "ci-ubsan", + "inherits": ["debug", "linux"], + "displayName": "Undefined Behavior Sanitizer", + "description": "This is only available on Linux", + "environment": { + "UBSAN_FLAGS": "-fsanitize=undefined -fsanitize=return -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize-recover=all", + "UBSAN_OPTIONS": "print_stacktrace=1" + }, + "cacheVariables": { + "JWT_BUILD_EXAMPLES": "ON", + "JWT_BUILD_TESTS": "ON", + "CMAKE_C_FLAGS": "$env{UBSAN_FLAGS}", + "CMAKE_CXX_FLAGS": "$env{UBSAN_FLAGS}", + "CMAKE_EXE_LINKER_FLAGS": "$env{UBSAN_FLAGS}", + "CMAKE_MODULE_LINKER_FLAGS": "$env{UBSAN_FLAGS}" + } + } + ], + "buildPresets": [ + { + "name": "coverage", + "configurePreset": "coverage", + "configuration": "Debug", + "targets": [ + "jwt-cpp-test", + "coverage" + ] + }, + { + "name": "unit-tests", + "configurePreset": "unit-tests", + "configuration": "Debug", + "targets": [ + "jwt-cpp-test" + ] + }, + { + "name": "ci-fuzzing", + "configurePreset": "ci-fuzzing", + "targets": [ + "jwt-cpp-fuzz-BaseEncodeFuzz", + "jwt-cpp-fuzz-BaseDecodeFuzz", + "jwt-cpp-fuzz-TokenDecodeFuzz" + ] + }, + { + "name": "ci-asan", + "configurePreset": "ci-asan", + "targets": [ + "rsa-create", + "rsa-verify", + "jwks-verify", + "jwt-cpp-test" + ] + }, + { + "name": "ci-ubsan", + "configurePreset": "ci-ubsan", + "targets": [ + "rsa-create", + "rsa-verify", + "jwks-verify", + "jwt-cpp-test" + ] + } + ], + "testPresets": [ + { + "name": "unit-tests", + "description": "Run the unit tests", + "configurePreset": "unit-tests", + "execution": { + "noTestsAction": "error" + } + } + ] +} \ No newline at end of file