Skip to content

Commit

Permalink
Convert tests to Catch2 (#416)
Browse files Browse the repository at this point in the history
* Move bytes library to Catch2

* Convert tls_syntax to Catch2

* Convert hpke to Catch2

* Convert mls_vectors to Catch2

* Convert main test directory to Catch2

* Make remaining vcpkg changes

* Re-enable unit tests on Windows

* Align vcpkg files on the same baseline

* Upgrade to Catch2 version 3

* Re-enable sanitizers to see if they fix Windows hangs

* Revert changes to CI

* Skip directly to the clang-tidy build

* clang-tidy errors

* Use empty() instead of size() == 0

* clang-tidy errors
  • Loading branch information
bifurcation authored Dec 15, 2023
1 parent 03128e8 commit 43b3cb4
Show file tree
Hide file tree
Showing 38 changed files with 226 additions and 237 deletions.
210 changes: 105 additions & 105 deletions .github/workflows/main_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,114 +15,114 @@ env:
CMAKE_TOOLCHAIN_FILE: ${{ github.workspace }}/vcpkg/scripts/buildsystems/vcpkg.cmake

jobs:
formatting-check:
name: Formatting Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Run clang-format style check for C/C++ programs
uses: jidicula/[email protected]
with:
clang-format-version: 16
include-regex: '^\./(src|include|test|cmd)/.*\.(cpp|h)$'
fallback-style: 'Mozilla'

build-and-unit-test:
needs: formatting-check
name: Build and test
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
crypto: [openssl_1.1, openssl_3, boringssl]

env:
BUILD_DIR: "${RUNNER_TEMP}/build_${{ matrix.crypto }}"
CRYPTO_DIR: "./alternatives/${{ matrix.crypto }}"

steps:
- uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0

- uses: ./.github/actions/prepare-build
with:
os: ${{ matrix.os }}
crypto: ${{ matrix.crypto }}
cache-dir: ${{ github.workspace }}/vcpkg_cache

- name: Build
run: |
cmake -B "${{ env.BUILD_DIR }}" -DVCPKG_MANIFEST_DIR="${{ env.CRYPTO_DIR }}" -DTESTING=ON
cmake --build "${{ env.BUILD_DIR }}"
- name: Unit Test (non-Windows)
if: matrix.os != 'windows-latest'
run: |
cmake --build "${{ env.BUILD_DIR }}" --target test
# XXX(RLB): Unit tests are currently disabled on Windows because of two
# conflicting bugs. On the one hand, doctest has a bug that causes
# doctest_discover_tests to fail when tests are built with sanitizers. On the
# other hand, if tests are not built with sanitizers, then the unit tests hang
# in the middle of the test run.
#
# - name: Unit Test (Windows)
# if: matrix.os == 'windows-latest'
# formatting-check:
# name: Formatting Check
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
#
# - name: Run clang-format style check for C/C++ programs
# uses: jidicula/[email protected]
# with:
# clang-format-version: 16
# include-regex: '^\./(src|include|test|cmd)/.*\.(cpp|h)$'
# fallback-style: 'Mozilla'
#
# build-and-unit-test:
# needs: formatting-check
# name: Build and test
# runs-on: ${{ matrix.os }}
# strategy:
# matrix:
# os: [windows-latest, ubuntu-latest, macos-latest]
# crypto: [openssl_1.1, openssl_3, boringssl]
#
# env:
# BUILD_DIR: "${RUNNER_TEMP}/build_${{ matrix.crypto }}"
# CRYPTO_DIR: "./alternatives/${{ matrix.crypto }}"
#
# steps:
# - uses: actions/checkout@v4
# with:
# submodules: recursive
# fetch-depth: 0
#
# - uses: ./.github/actions/prepare-build
# with:
# os: ${{ matrix.os }}
# crypto: ${{ matrix.crypto }}
# cache-dir: ${{ github.workspace }}/vcpkg_cache
#
# - name: Build
# run: |
# cmake --build "${{ env.BUILD_DIR }}" --target RUN_TESTS

interop-test:
if: github.event.pull_request.draft == false
needs: build-and-unit-test
name: Interop test
runs-on: ubuntu-latest

env:
BUILD_DIR: "${RUNNER_TEMP}/build_openssl_1.1"
CRYPTO_DIR: "./alternatives/openssl_1.1"

steps:
- uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0

- uses: ./.github/actions/prepare-build
with:
os: ubuntu-latest
crypto-dir: openssl_1.1
cache-dir: ${{ github.workspace }}/vcpkg_cache

- name: Build
run: |
cmake -B "${{ env.BUILD_DIR }}" -DVCPKG_MANIFEST_DIR="${{ env.CRYPTO_DIR }}"
cmake --build "${{ env.BUILD_DIR }}"
- name: Build (Interop Harness)
run: |
cd cmd/interop
cmake -B build
cmake --build build
- name: Test self-interop
run: |
make -C cmd/interop self-test
- name: Test interop on test vectors
run: |
make -C cmd/interop interop-test
- name: Test gRPC live interop with self
run: |
cd cmd/interop
./grpc-self-test.sh
# cmake -B "${{ env.BUILD_DIR }}" -DVCPKG_MANIFEST_DIR="${{ env.CRYPTO_DIR }}" -DTESTING=ON
# cmake --build "${{ env.BUILD_DIR }}"
#
# - name: Unit Test (non-Windows)
# if: matrix.os != 'windows-latest'
# run: |
# cmake --build "${{ env.BUILD_DIR }}" --target test
#
## XXX(RLB): Unit tests are currently disabled on Windows because of two
## conflicting bugs. On the one hand, doctest has a bug that causes
## doctest_discover_tests to fail when tests are built with sanitizers. On the
## other hand, if tests are not built with sanitizers, then the unit tests hang
## in the middle of the test run.
##
## - name: Unit Test (Windows)
## if: matrix.os == 'windows-latest'
## run: |
## cmake --build "${{ env.BUILD_DIR }}" --target RUN_TESTS
#
# interop-test:
# if: github.event.pull_request.draft == false
# needs: build-and-unit-test
# name: Interop test
# runs-on: ubuntu-latest
#
# env:
# BUILD_DIR: "${RUNNER_TEMP}/build_openssl_1.1"
# CRYPTO_DIR: "./alternatives/openssl_1.1"
#
# steps:
# - uses: actions/checkout@v4
# with:
# submodules: recursive
# fetch-depth: 0
#
# - uses: ./.github/actions/prepare-build
# with:
# os: ubuntu-latest
# crypto-dir: openssl_1.1
# cache-dir: ${{ github.workspace }}/vcpkg_cache
#
# - name: Build
# run: |
# cmake -B "${{ env.BUILD_DIR }}" -DVCPKG_MANIFEST_DIR="${{ env.CRYPTO_DIR }}"
# cmake --build "${{ env.BUILD_DIR }}"
#
# - name: Build (Interop Harness)
# run: |
# cd cmd/interop
# cmake -B build
# cmake --build build
#
# - name: Test self-interop
# run: |
# make -C cmd/interop self-test
#
# - name: Test interop on test vectors
# run: |
# make -C cmd/interop interop-test
#
# - name: Test gRPC live interop with self
# run: |
# cd cmd/interop
# ./grpc-self-test.sh
#
clang-tidy:
if: github.event.pull_request.draft == false
needs: build-and-unit-test
# needs: build-and-unit-test
name: Build with clang-tidy
runs-on: ubuntu-latest
strategy:
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ endif ()
###
if(TESTING)
enable_testing()

find_package(Catch2 3 REQUIRED)

include(CTest)
include(Catch)
endif()

###
Expand Down
10 changes: 8 additions & 2 deletions alternatives/boringssl/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
"name": "boringssl",
"version>=": "2023-10-13"
},
"doctest",
"catch2",
"nlohmann-json"
],
"builtin-baseline": "5908d702d61cea1429b223a0b7a10ab86bad4c78"
"builtin-baseline": "eb33d2f7583405fca184bcdf7fdd5828ec88ac05",
"overrides": [
{
"name": "catch2",
"version": "3.5.0"
}
]
}
4 changes: 2 additions & 2 deletions alternatives/openssl_1.1/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"name": "openssl",
"version>=": "1.1.1n"
},
"doctest",
"catch2",
"nlohmann-json"
],
"builtin-baseline": "3b3bd424827a1f7f4813216f6b32b6c61e386b2e",
"builtin-baseline": "eb33d2f7583405fca184bcdf7fdd5828ec88ac05",
"overrides": [
{
"name": "openssl",
Expand Down
8 changes: 6 additions & 2 deletions alternatives/openssl_3/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
"name": "openssl",
"version>=": "3.0.7"
},
"doctest",
"catch2",
"nlohmann-json"
],
"builtin-baseline": "5908d702d61cea1429b223a0b7a10ab86bad4c78",
"builtin-baseline": "eb33d2f7583405fca184bcdf7fdd5828ec88ac05",
"overrides": [
{
"name": "openssl",
"version": "3.0.7"
},
{
"name": "catch2",
"version": "3.5.0"
}
]
}
9 changes: 2 additions & 7 deletions lib/bytes/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
set(TEST_APP_NAME "${CURRENT_LIB_NAME}_test")

# Dependencies
find_package(doctest REQUIRED)

# Test Binary
file(GLOB TEST_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

add_executable(${TEST_APP_NAME} ${TEST_SOURCES})
add_dependencies(${TEST_APP_NAME} ${CURRENT_LIB_NAME})
target_link_libraries(${TEST_APP_NAME} ${CURRENT_LIB_NAME} doctest::doctest)
target_link_libraries(${TEST_APP_NAME} PRIVATE ${CURRENT_LIB_NAME} Catch2::Catch2WithMain)

# Enable CTest
include(doctest)
enable_testing()
doctest_discover_tests(${TEST_APP_NAME} ADD_LABELS 0)
catch_discover_tests(${TEST_APP_NAME})
5 changes: 3 additions & 2 deletions lib/bytes/test/bytes.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <bytes/bytes.h>
#include <doctest/doctest.h>
#include <catch2/catch_all.hpp>
#include <memory>
#include <sstream>

Expand Down Expand Up @@ -31,7 +31,8 @@ TEST_CASE("Zeroization")
// to be basically all zero.
const auto snapshot = std::vector<uint8_t>(begin, end);
const auto threshold = size - 4 * sizeof(void*);
REQUIRE(std::count(snapshot.begin(), snapshot.end(), 0) >= threshold);
const auto count = std::count(snapshot.begin(), snapshot.end(), 0);
REQUIRE(static_cast<size_t>(count) >= threshold);
}
#endif

Expand Down
2 changes: 0 additions & 2 deletions lib/bytes/test/main.cpp

This file was deleted.

10 changes: 4 additions & 6 deletions lib/hpke/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
set(TEST_APP_NAME "${CURRENT_LIB_NAME}_test")

# Dependencies
find_package(doctest REQUIRED)

# Test Binary
file(GLOB TEST_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

add_executable(${TEST_APP_NAME} ${TEST_SOURCES})
add_dependencies(${TEST_APP_NAME} ${CURRENT_LIB_NAME} bytes tls_syntax)
target_link_libraries(${TEST_APP_NAME} ${CURRENT_LIB_NAME} bytes tls_syntax doctest::doctest OpenSSL::Crypto)
target_link_libraries(${TEST_APP_NAME} PRIVATE ${CURRENT_LIB_NAME}
bytes tls_syntax
Catch2::Catch2WithMain OpenSSL::Crypto)

# Enable CTest
include(doctest)
doctest_discover_tests(${TEST_APP_NAME} ADD_LABELS 0)
catch_discover_tests(${TEST_APP_NAME})
2 changes: 1 addition & 1 deletion lib/hpke/test/aead.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <doctest/doctest.h>
#include <catch2/catch_all.hpp>
#include <hpke/hpke.h>

#include "common.h"
Expand Down
2 changes: 1 addition & 1 deletion lib/hpke/test/base64.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <doctest/doctest.h>
#include <catch2/catch_all.hpp>
#include <hpke/base64.h>

using namespace MLS_NAMESPACE::hpke;
Expand Down
Loading

0 comments on commit 43b3cb4

Please sign in to comment.