From 94b0918891e890ffac786e449dee4a4117cc8a51 Mon Sep 17 00:00:00 2001 From: raspopov Date: Tue, 7 Oct 2025 18:44:00 +0300 Subject: [PATCH] Fix CMake test building A CMake unit test build was added. The original tests (examples) were fixed so they can be compiled on Windows using MSVC2022 and MinGW. The `ccm-test` has been updated so that it returns the status of test completion. The file `getopt.c` was borrowed from the libcoap project. The `CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS` variable must be set before a `add_libray()` call. Unimportant compiler warnings were disabled, and other warnings were fixed. GitHub Actions are unified and optimized. Added "Visual Studio 17 2022" job. --- .github/workflows/main.yml | 108 +++++++++++++++-------------- .gitignore | 2 + AutoConf.cmake | 3 +- CMakeLists.txt | 117 +++++++++++++++++++++++++++----- cmake/FindCUnit.cmake | 101 +++++++++++++++++++++++++++ dtls.c | 10 +-- dtls_debug.c | 10 +-- dtls_debug.h | 12 ++-- tests/CMakeLists.txt | 29 +++++--- tests/ccm-test.c | 20 ++++-- tests/dtls-client.c | 90 +++++++++++++++++++----- tests/dtls-server.c | 55 ++++++++++----- tests/getopt.c | 65 ++++++++++++++++++ tests/unit-tests/CMakeLists.txt | 26 +++++++ tests/unit-tests/test_ccm.c | 2 - tests/unit-tests/test_ccm.h | 2 +- tests/unit-tests/test_ecc.c | 20 +++--- tests/unit-tests/test_ecc.h | 2 +- tests/unit-tests/test_prf.c | 2 - tests/unit-tests/test_prf.h | 2 +- tests/unit-tests/testdriver.c | 17 +++-- tests/unit-tests/testdriver.h | 26 +++++++ 22 files changed, 564 insertions(+), 157 deletions(-) create mode 100644 cmake/FindCUnit.cmake create mode 100644 tests/getopt.c create mode 100644 tests/unit-tests/CMakeLists.txt create mode 100644 tests/unit-tests/testdriver.h diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b1c61c49..63d6f149 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,65 +17,71 @@ env: jobs: build-linux-autotools: + name: Build for Linux using Autotools runs-on: ubuntu-latest strategy: matrix: CC: ["gcc", "clang" ] - steps: - - uses: actions/checkout@v3 - - name: setup - run: | - sudo apt-get update && sudo apt-get install -y libcunit1-dev libtool libtool-bin exuberant-ctags valgrind - ./autogen.sh - - name: configure - run: | - # mkdir build-${{matrix.CC}} - # cd build-${{matrix.CC}} - $GITHUB_WORKSPACE/configure --enable-tests - - name: compile - run: | - # cd build-${{matrix.CC}} - make EXTRA_CFLAGS=-Werror - - name: tests - run: | - # cd build-${{matrix.CC}} - libtool --mode=execute valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet tests/unit-tests/testdriver - libtool --mode=execute valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet tests/ccm-test - - build-linux-cmake: - name: Build for Linux using CMake - runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Build tinydtls + - name: checkout + uses: actions/checkout@v3 + - name: setup + uses: awalsh128/cache-apt-pkgs-action@latest + with: + packages: libcunit1-dev libtool libtool-bin exuberant-ctags valgrind + version: 1.0 + - name: configure run: | - cmake -E make_directory build_test - cd build_test - cmake -DWARNING_TO_ERROR=ON -Dmake_tests=ON .. - cmake --build . - build-macos-cmake: - name: Build for macOS using CMake - runs-on: macos-latest - - steps: - - uses: actions/checkout@v3 - - name: Build tinydtls + # mkdir build-${{matrix.CC}} + # cd build-${{matrix.CC}} + $GITHUB_WORKSPACE/autogen.sh + $GITHUB_WORKSPACE/configure --enable-tests + - name: compile run: | - cmake -E make_directory build_test - cd build_test - cmake -DWARNING_TO_ERROR=ON -Dmake_tests=ON .. - cmake --build . + # cd build-${{matrix.CC}} + make EXTRA_CFLAGS=-Werror + - name: tests + run: | + # cd build-${{matrix.CC}} + libtool --mode=execute valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet tests/unit-tests/testdriver + libtool --mode=execute valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet tests/ccm-test - build-windows: - name: Build for Windows using CMake - runs-on: windows-latest + build-cmake: + name: Build using CMake + runs-on: ${{ matrix.config.os }} + strategy: + fail-fast: false + matrix: + config: + - os: ubuntu-latest + vcpkg_triplet: x64-linux-release + compiler: "Unix Makefiles" + - os: macos-latest + vcpkg_triplet: arm64-osx-release + compiler: "Unix Makefiles" + - os: windows-latest + vcpkg_triplet: x64-windows-release + compiler: "Unix Makefiles" + - os: windows-latest + vcpkg_triplet: x64-windows-release + compiler: "Visual Studio 17 2022" steps: - - uses: actions/checkout@v3 - - name: Build tinydtls - run: | - cmake -E make_directory build_test - cd build_test - cmake -G "Unix Makefiles" -DWARNING_TO_ERROR=ON .. - cmake --build . + - name: checkout + uses: actions/checkout@v3 + - name: vcpkg-setup + uses: johnwason/vcpkg-action@v7 + id: vcpkg + with: + pkgs: cunit + triplet: ${{ matrix.config.vcpkg_triplet }} + cache-key: ${{ matrix.config.os }} + token: ${{ github.token }} + - name: configure + run: cmake ${{ steps.vcpkg.outputs.vcpkg-cmake-config }} -G "${{ matrix.config.compiler }}" -DWARNING_TO_ERROR=ON -Dmake_tests=ON -DENABLE_DOCS=OFF -B build_test + - name: compile + run: cmake --build build_test + - name: test + working-directory: build_test + run: ctest -C Debug --verbose diff --git a/.gitignore b/.gitignore index 3a2c53e5..785b682e 100644 --- a/.gitignore +++ b/.gitignore @@ -91,3 +91,5 @@ CMakeFiles tinydtls.dir Debug Release +*.json +out diff --git a/AutoConf.cmake b/AutoConf.cmake index a86f5a18..a03accd1 100644 --- a/AutoConf.cmake +++ b/AutoConf.cmake @@ -68,9 +68,10 @@ check_function_exists (strdup HAVE_STRDUP) check_function_exists (strerror HAVE_STRERROR) check_function_exists (strnlen HAVE_STRNLEN) check_function_exists (fls HAVE_FLS) +check_function_exists (snprintf HAVE_SNPRINTF) check_function_exists (vprintf HAVE_VPRINTF) check_function_exists (inet_ntop HAVE_INET_NTOP) -check_function_exists (random HAVE_RANDOM) +check_function_exists (random HAVE_RANDOM) if(HAVE_SYS_RANDOM_H) # zephyr/ncs 1.9.1 seems to link getrandom but doesn't offer a header diff --git a/CMakeLists.txt b/CMakeLists.txt index cbfe4041..fbe279fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,11 +19,18 @@ # ############################################################################### -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.10) -project(tinydtls) +project(tinydtls + VERSION "0.8.6" + DESCRIPTION "A C library for Datagram Transport Layer Security (DTLS) supporting both client and server functionality." + HOMEPAGE_URL "https://projects.eclipse.org/projects/iot.tinydtls" + LANGUAGES C) + +set(CMAKE_C_STANDARD 99) include (AutoConf.cmake) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) if(NOT ZEPHYR_BASE) option(BUILD_SHARED_LIBS "Link using shared libs" OFF) @@ -39,9 +46,10 @@ if(NOT PLATFORM) set_property(CACHE PLATFORM PROPERTY STRINGS "contiki" "espidf" "posix" "riot" "zephyr" "windows") endif() -set(PACKAGE_NAME "tinydtls") -set(PACKAGE_VERSION "0.8.6" ) -set(SOVERSION "0" ) +set(PACKAGE_NAME "${PROJECT_NAME}") +set(PACKAGE_VERSION "${PROJECT_VERSION}" ) +set(PACKAGE_URL "${PROJECT_HOMEPAGE_URL}") +set(SOVERSION "0" ) if(NOT ZEPHYR_BASE) option(DTLS_ECC "disable/enable support for TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8" ON ) @@ -52,7 +60,11 @@ endif() option(WARNING_TO_ERROR "force all compiler warnings to be errors" OFF) -configure_file(dtls_config.h.cmake.in dtls_config.h ) +if(MSVC) + option(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "Export all symbols when compiling to a .dll" ON) +endif() + +configure_file(dtls_config.h.cmake.in dtls_config.h) add_library(tinydtls) @@ -73,14 +85,13 @@ target_sources(tinydtls PRIVATE ecc/ecc.c) target_include_directories(tinydtls PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) -target_compile_definitions(tinydtls PUBLIC DTLSv12 WITH_SHA256 SHA2_USE_INTTYPES_H DTLS_CHECK_CONTENTTYPE) +target_compile_definitions(tinydtls PUBLIC DTLSv12 WITH_SHA256 SHA2_USE_INTTYPES_H DTLS_CHECK_CONTENTTYPE $<$:TEST_INCLUDE>) if(MSVC) - option(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "Export all symbols when compiling to a .dll" ON) - target_compile_options(tinydtls PRIVATE -Wall) + target_compile_options(tinydtls PRIVATE -Wall -wd4127 -wd4200 -wd4242 -wd4244 -wd4267 -wd4702 -wd4710 -wd4711 -wd4668 -wd4820 -wd5045 -D_CRT_SECURE_NO_WARNINGS) if(${WARNING_TO_ERROR}) target_compile_options(tinydtls PRIVATE -WX) - endif() + endif() elseif(NOT ZEPHYR_BASE) target_compile_options(tinydtls PRIVATE -fPIC -pedantic -std=c99 -Wall -Wextra -Wformat-security -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wshadow -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wunused) if(${WARNING_TO_ERROR}) @@ -92,12 +103,10 @@ set_target_properties(tinydtls PROPERTIES VERSION ${PACKAGE_VERSION} SOVERSION $ if( ${make_tests} ) add_subdirectory(tests) -endif() - -if(BUILD_SHARED_LIBS) - install(TARGETS tinydtls LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ) -else() - install(TARGETS tinydtls DESTINATION ${CMAKE_INSTALL_LIBDIR} ) + add_subdirectory(tests/unit-tests) + include(CTest) + add_test(NAME test COMMAND testdriver) + add_test(NAME ccm-test COMMAND ccm-test) endif() if(NOT (${CMAKE_VERSION} VERSION_LESS "3.18.0")) @@ -105,3 +114,79 @@ if(NOT (${CMAKE_VERSION} VERSION_LESS "3.18.0")) NEWLINE_STYLE UNIX CONTENT "*") endif() + +include(GNUInstallDirs) +install(TARGETS tinydtls + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}") +install(FILES + alert.h + ccm.h + crypto.h + dtls.h + "${CMAKE_CURRENT_BINARY_DIR}/dtls_config.h" + dtls_debug.h + dtls_mutex.h + dtls_prng.h + dtls_time.h + global.h + hmac.h + netq.h + numeric.h + peer.h + session.h + state.h + tinydtls.h + uthash.h + utlist.h + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/tinydtls") +install(FILES + aes/rijndael.h + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/tinydtls/aes") +install(FILES + sha2/sha2.h + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/tinydtls/sha2") +install(FILES + ecc/ecc.h + ecc/test_helper.h + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/tinydtls/ecc") + +# pkg-config +set(prefix "${CMAKE_INSTALL_PREFIX}") +set(exec_prefix "\${prefix}") +set(includedir "\${prefix}/include") +set(libdir "\${exec_prefix}/lib") +configure_file(tinydtls.pc.in tinydtls.pc @ONLY) +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/tinydtls.pc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") + +# Doxygen +option(ENABLE_DOCS "build Doxygen documentation" ON) +if(ENABLE_DOCS) + find_package(Doxygen REQUIRED) + set(DOXYGEN_QUIET YES) + set(DOXYGEN_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") + set(DOXYGEN_PROJECT_NAME "${PACKAGE_NAME}") + set(DOXYGEN_PROJECT_NUMBER "${PACKAGE_VERSION}") + set(DOXYGEN_RECURSIVE NO) + set(DOXYGEN_EXTRACT_ALL YES) + set(DOXYGEN_EXTRACT_STATIC YES) + set(DOXYGEN_OPTIMIZE_OUTPUT_FOR_C YES) + set(DOXYGEN_DOT_IMAGE_FORMAT svg) + set(DOXYGEN_INTERACTIVE_SVG YES) + doxygen_add_docs(docs + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/aes + ${CMAKE_CURRENT_SOURCE_DIR}/ecc + ${CMAKE_CURRENT_SOURCE_DIR}/sha2 + COMMENT "Building Doxygen documentation") + add_custom_command(TARGET tinydtls POST_BUILD + COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target docs) + install(DIRECTORY + "${CMAKE_CURRENT_BINARY_DIR}/html/" + TYPE DOC + PATTERN "*.md5" EXCLUDE + PATTERN "*.map" EXCLUDE) +endif() diff --git a/cmake/FindCUnit.cmake b/cmake/FindCUnit.cmake new file mode 100644 index 00000000..c2ef6cf2 --- /dev/null +++ b/cmake/FindCUnit.cmake @@ -0,0 +1,101 @@ +# FindCUnit +# --------- +# +# Find the CUnit library. +# +# Imported Targets +# ^^^^^^^^^^^^^^^^ +# +# This module defines the following :prop_tgt:`IMPORTED` targets: +# +# ``CUnit::cunit`` +# The CUnit library, if found. +# +# Result Variables +# ^^^^^^^^^^^^^^^^ +# +# This module will set the following variables in your project: +# +# ``CUnit_FOUND`` +# System has the CUnit library. +# ``CUnit_VERSION`` +# The version of CUnit found. +# ``CUnit_INCLUDE_DIR`` +# The CUnit include directory. +# ``CUnit_LIBRARIES`` +# All CUnit libraries. +# +# Hints +# ^^^^^ +# +# Set ``CUnit_ROOT_DIR`` to the root directory of a CUnit installation. + +if(CUnit_INCLUDE_DIR AND CUnit_LIBRARIES) + # in cache already + set(CUnit_FIND_QUIETLY TRUE) +endif() + +if(CUnit_ROOT_DIR) + set(_CUnit_EXTRA_FIND_ARGS "NO_CMAKE_FIND_ROOT_PATH") +endif() + +find_package(PkgConfig QUIET) +if(PKG_CONFIG_FOUND) + pkg_check_modules(PC_CUNIT QUIET cunit) +endif() + +find_path( + CUnit_INCLUDE_DIR + NAMES CUnit/CUnit.h + PATH_SUFFIXES include + HINTS ${PROJECT_SOURCE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ${CUnit_ROOT_DIR} + ${PC_CUNIT_INCLUDE_DIRS} + ${_CUnit_EXTRA_FIND_ARGS}) + +find_library( + CUnit_LIBRARIES + NAMES cunit + PATH_SUFFIXES lib + HINTS ${PROJECT_SOURCE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ${CUnit_ROOT_DIR} + ${PC_CUNIT_LIBRARY_DIRS} + ${_CUnit_EXTRA_FIND_ARGS}) + +if(CUnit_INCLUDE_DIR AND EXISTS "${CUnit_INCLUDE_DIR}/CUnit/CUnit.h") + file(STRINGS "${CUnit_INCLUDE_DIR}/CUnit/CUnit.h" CUnit_VERSION_STR + REGEX "#[\t ]*define[\t ]+CU_VERSION[\t ]+\"[^\"]+\"") + string(REGEX REPLACE "^.*CU_VERSION[\t ]+\"([^\"]+)\"" + "\\1" CUnit_VERSION_STR "${CUnit_VERSION_STR}") + set(CUnit_VERSION "${CUnit_VERSION_STR}") + unset(CUnit_VERSION_STR) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args( + CUnit + FAIL_MESSAGE "Could NOT find CUnit, try to set the path to CUnit root folder in the system variable CUnit_ROOT_DIR" + REQUIRED_VARS CUnit_INCLUDE_DIR + CUnit_LIBRARIES + VERSION_VAR CUnit_VERSION) + +if(NOT + TARGET + CUnit::cunit) + add_library( + CUnit::cunit + UNKNOWN + IMPORTED) + set_target_properties( + CUnit::cunit + PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CUnit_INCLUDE_DIR}" + VERSION "${CUnit_VERSION}" + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${CUnit_LIBRARIES}") +endif() + +mark_as_advanced( + CUnit_INCLUDE_DIR + CUnit_LIBRARIES) diff --git a/dtls.c b/dtls.c index dbc0ba44..ac22d0a9 100644 --- a/dtls.c +++ b/dtls.c @@ -1678,7 +1678,7 @@ dtls_prepare_record(dtls_peer_t *peer, dtls_security_parameters_t *security, unsigned int i; if (*rlen < DTLS_RH_LENGTH) { - dtls_alert("The sendbuf (%zu bytes) is too small\n", *rlen); + dtls_alert("The sendbuf (%u bytes) is too small\n", (unsigned)*rlen); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR); } @@ -2862,7 +2862,7 @@ dtls_send_server_hello_msgs(dtls_context_t *ctx, dtls_peer_t *peer) #ifdef DTLS_ECC if (DTLS_KEY_EXCHANGE_ECDHE_ECDSA == key_exchange_algorithm) { - const dtls_ecdsa_key_t *ecdsa_key; + const dtls_ecdsa_key_t *ecdsa_key = NULL; res = CALL(ctx, get_ecdsa_key, &peer->session, &ecdsa_key); if (res < 0) { @@ -3437,8 +3437,8 @@ check_server_certificate(dtls_context_t *ctx, data += DTLS_HS_LENGTH; if (dtls_uint24_to_int(data) != DTLS_EC_SUBJECTPUBLICKEY_SIZE) { - dtls_alert("expect length of %zu bytes for certificate\n", - DTLS_EC_SUBJECTPUBLICKEY_SIZE); + dtls_alert("expect length of %u bytes for certificate\n", + (unsigned)DTLS_EC_SUBJECTPUBLICKEY_SIZE); return dtls_alert_fatal_create(DTLS_ALERT_DECODE_ERROR); } data += sizeof(uint24); @@ -3697,7 +3697,7 @@ check_server_hellodone(dtls_context_t *ctx, { int res; #ifdef DTLS_ECC - const dtls_ecdsa_key_t *ecdsa_key; + const dtls_ecdsa_key_t *ecdsa_key = NULL; #endif /* DTLS_ECC */ dtls_handshake_parameters_t *handshake = peer->handshake_params; diff --git a/dtls_debug.c b/dtls_debug.c index 236fe705..54bb6089 100644 --- a/dtls_debug.c +++ b/dtls_debug.c @@ -247,7 +247,7 @@ dsrv_print_addr(const session_t *addr, char *buf, size_t len) { #endif /* ! HAVE_INET_NTOP */ } -#if !defined(WITH_CONTIKI) && !defined(_MSC_VER) +#if !defined(WITH_CONTIKI) static void dtls_logging_handler(log_t level, const char *message) { @@ -410,7 +410,7 @@ dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, s dtls_mutex_lock(&static_mutex); #endif /* DTLS_CONSTRAINED_STACK */ if (extend) { - snprintf(message, sizeof(message), "%s: (%zu bytes):\n", name, length); + snprintf(message, sizeof(message), "%s: (%u bytes):\n", name, (unsigned)length); log_handler(level, message); len = 0; while (length-- && len + 1 < sizeof(message)) { @@ -439,7 +439,7 @@ dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, s } } } else { - len = snprintf(message, sizeof(message), "%s: (%zu bytes): ", name, length); + len = snprintf(message, sizeof(message), "%s: (%u bytes): ", name, (unsigned)length); while (length-- && len + 1 < sizeof(message)) { len += snprintf(&message[len], sizeof(message)-len, "%02X", *buf++); } @@ -474,7 +474,7 @@ dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, s PRINTF("%s ", loglevels[level]); if (extend) { - PRINTF("%s: (%zu bytes):\n", name, length); + PRINTF("%s: (%u bytes):\n", name, (unsigned)length); while (length--) { if (n % 16 == 0) @@ -491,7 +491,7 @@ dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, s } } } else { - PRINTF("%s: (%zu bytes): ", name, length); + PRINTF("%s: (%u bytes): ", name, (unsigned)length); while (length--) PRINTF("%02X", *buf++); } diff --git a/dtls_debug.h b/dtls_debug.h index a02cab4e..d7c61a54 100644 --- a/dtls_debug.h +++ b/dtls_debug.h @@ -100,13 +100,13 @@ void dtls_set_log_handler(dtls_log_handler_t app_handler); * Writes the given text to \c stdout. The text is output only when \p * level is below or equal to the log level that set by * set_log_level(). */ -#ifdef HAVE_VPRINTF +#if !defined(WITH_CONTIKI) #if (defined(__GNUC__) && !defined(__MINGW32__)) void dsrv_log(log_t level, const char *format, ...) __attribute__ ((format(printf, 2, 3))); #else /* !__GNUC__ && !__MINGW32__ */ void dsrv_log(log_t level, const char *format, ...); #endif /* !__GNUC__ && !__MINGW32__ */ -#else +#elif defined(HAVE_VPRINTF) #define dsrv_log(level, format, ...) PRINTF(format, ##__VA_ARGS__) #endif @@ -129,8 +129,8 @@ void dtls_dsrv_log_addr(log_t level, const char *name, const session_t *addr); #define dtls_notice(...) LOG_INF(__VA_ARGS__) #define dtls_info(...) LOG_INF(__VA_ARGS__) #define dtls_debug(...) LOG_DBG(__VA_ARGS__) -#define dtls_debug_hexdump(name, buf, length) { LOG_DBG("%s (%zu bytes):", name, (size_t)(length)); LOG_HEXDUMP_DBG(buf, length, name); } -#define dtls_debug_dump(name, buf, length) { LOG_DBG("%s (%zu bytes):", name, (size_t)(length)); LOG_HEXDUMP_DBG(buf, length, name); } +#define dtls_debug_hexdump(name, buf, length) { LOG_DBG("%s (%u bytes):", name, (unsigned)(length)); LOG_HEXDUMP_DBG(buf, length, name); } +#define dtls_debug_dump(name, buf, length) { LOG_DBG("%s (%u bytes):", name, (unsigned)(length)); LOG_HEXDUMP_DBG(buf, length, name); } #elif defined(RIOT_VERSION) #define dtls_emerg(...) LOG_ERROR(__VA_ARGS__) #define dtls_alert(...) LOG_ERROR(__VA_ARGS__) @@ -139,8 +139,8 @@ void dtls_dsrv_log_addr(log_t level, const char *name, const session_t *addr); #define dtls_notice(...) LOG_INFO(__VA_ARGS__) #define dtls_info(...) LOG_INFO(__VA_ARGS__) #define dtls_debug(...) LOG_DEBUG(__VA_ARGS__) -#define dtls_debug_hexdump(name, buf, length) { if (LOG_DEBUG <= LOG_LEVEL) { LOG_DEBUG("-= %s (%zu bytes) =-\n", name, (size_t)(length)); od_hex_dump(buf, length, 0); }} -#define dtls_debug_dump(name, buf, length) { if (LOG_DEBUG <= LOG_LEVEL) { LOG_DEBUG("%s (%zu bytes):", name, (size_t)(length)); od_hex_dump(buf, length, 0); }} +#define dtls_debug_hexdump(name, buf, length) { if (LOG_DEBUG <= LOG_LEVEL) { LOG_DEBUG("-= %s (%u bytes) =-\n", name, (unsigned)(length)); od_hex_dump(buf, length, 0); }} +#define dtls_debug_dump(name, buf, length) { if (LOG_DEBUG <= LOG_LEVEL) { LOG_DEBUG("%s (%u bytes):", name, (unsigned)(length)); od_hex_dump(buf, length, 0); }} #else /* neither RIOT nor Zephyr */ #define dtls_emerg(...) dsrv_log(DTLS_LOG_EMERG, __VA_ARGS__) #define dtls_alert(...) dsrv_log(DTLS_LOG_ALERT, __VA_ARGS__) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5bf6f54a..5f1d9681 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,28 +19,37 @@ # ############################################################################### -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.10) project(tinydtls-tests) +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_STANDARD_REQUIRED ON) + add_executable(dtls-server dtls-server.c dtls_ciphers_util.c) -target_link_libraries(dtls-server LINK_PUBLIC tinydtls) -target_compile_options(dtls-server PUBLIC -Wall -DTEST_INCLUDE -DDTLSv12 -DWITH_SHA256) +target_link_libraries(dtls-server PRIVATE tinydtls $<$:Ws2_32>) +target_compile_options(dtls-server PRIVATE -Wall -DTEST_INCLUDE -DDTLSv12 -DWITH_SHA256 + $<$:-wd4200 -wd4242 -wd4244 -wd4267 -wd4388 -wd4710 -wd4711 -wd4668 -wd4820 -wd4996 -wd5045 -D_CRT_SECURE_NO_WARNINGS>) if(${WARNING_TO_ERROR}) - target_compile_options(dtls-server PUBLIC -Werror) + target_compile_options(dtls-server PRIVATE $,-WX,-Werror>) endif() add_executable(ccm-test ccm-test.c) -target_link_libraries(ccm-test LINK_PUBLIC tinydtls) -target_compile_options(ccm-test PUBLIC -Wall -DTEST_INCLUDE -DDTLSv12 -DWITH_SHA256) +target_link_libraries(ccm-test PRIVATE tinydtls) +target_compile_options(ccm-test PRIVATE -Wall -DTEST_INCLUDE -DDTLSv12 -DWITH_SHA256 + $<$:-wd4267 -wd4710 -wd4711 -wd5045>) if(${WARNING_TO_ERROR}) - target_compile_options(ccm-test PUBLIC -Werror) + target_compile_options(ccm-test PRIVATE $,-WX,-Werror>) endif() add_executable(dtls-client dtls-client.c dtls_ciphers_util.c) -target_link_libraries(dtls-client LINK_PUBLIC tinydtls) -target_compile_options(dtls-client PUBLIC -Wall -DTEST_INCLUDE -DDTLSv12 -DWITH_SHA256) +target_link_libraries(dtls-client PRIVATE tinydtls $<$:Ws2_32>) +target_compile_options(dtls-client PRIVATE -Wall -DTEST_INCLUDE -DDTLSv12 -DWITH_SHA256 + $<$:-wd4200 -wd4242 -wd4244 -wd4267 -wd4388 -wd4710 -wd4711 -wd4668 -wd4820 -wd4996 -wd5045 -D_CRT_SECURE_NO_WARNINGS>) if(${WARNING_TO_ERROR}) - target_compile_options(dtls-client PUBLIC -Werror) + target_compile_options(dtls-client PRIVATE $,-WX,-Werror>) endif() +include(GNUInstallDirs) +install(TARGETS dtls-client dtls-server ccm-test + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") diff --git a/tests/ccm-test.c b/tests/ccm-test.c index 7e2e7f87..0ef32ae4 100644 --- a/tests/ccm-test.c +++ b/tests/ccm-test.c @@ -1,3 +1,5 @@ +#include "tinydtls.h" + #include #include #include @@ -12,7 +14,6 @@ #endif /* WITH_CONTIKI */ //#include "dtls_debug.h" -#include "tinydtls.h" #include "numeric.h" #include "ccm.h" @@ -43,6 +44,7 @@ int main(int argc, char **argv) { #endif /* WITH_CONTIKI */ long int len; size_t n; + int result = 0; rijndael_ctx ctx; @@ -63,9 +65,11 @@ int main(int argc, char **argv) { data[n].msg, data[n].la); len += + data[n].la; - printf("Packet Vector #%lu ", n+1); - if ((size_t)len != data[n].r_lm || memcmp(data[n].msg, data[n].result, len)) + printf("Packet Vector #%u ", (unsigned)(n+1)); + if ((size_t)len != data[n].r_lm || memcmp(data[n].msg, data[n].result, len)) { + result = 1; printf("FAILED, "); + } else printf("OK, "); @@ -76,15 +80,17 @@ int main(int argc, char **argv) { data[n].msg + data[n].la, len - data[n].la, data[n].msg, data[n].la); - if (len < 0) - printf("Packet Vector #%lu: cannot decrypt message\n", n+1); + if (len < 0) { + result = 1; + printf("Packet Vector #%u: cannot decrypt message\n", (unsigned)(n+1)); + } else - printf("\t*** MAC verified (total length = %lu) ***\n", len + data[n].la); + printf("\t*** MAC verified (total length = %u) ***\n", (unsigned)(len + data[n].la)); } #ifdef WITH_CONTIKI PROCESS_END(); #else /* WITH_CONTIKI */ - return 0; + return result; #endif /* WITH_CONTIKI */ } diff --git a/tests/dtls-client.c b/tests/dtls-client.c index da8f7d73..7188a4fb 100644 --- a/tests/dtls-client.c +++ b/tests/dtls-client.c @@ -20,19 +20,43 @@ #include #include #include +#ifdef HAVE_UNISTD_H #include +#else /* ! HAVE_UNISTD_H */ +#include "getopt.c" +#endif /* ! HAVE_UNISTD_H */ #include +#ifdef HAVE_NETINET_IN_H #include +#endif /* HAVE_NETINET_IN_H */ #include +#ifdef HAVE_SYS_SOCKET_H #include +#endif /* HAVE_SYS_SOCKET_H */ #ifdef HAVE_SYS_TIME_H #include #endif /* HAVE_SYS_TIME_H */ - +#ifdef HAVE_ARPA_INET_H #include +#endif /* HAVE_ARPA_INET_H */ +#ifdef HAVE_NETDB_H #include +#endif /* HAVE_NETDB_H */ #include +#ifdef IS_WINDOWS +#include +#include +#include +#define MSG_DONTWAIT 0 +#undef MSG_TRUNC +#define MSG_TRUNC 0 +#ifdef _MSC_VER +#define STDOUT_FILENO 1 +typedef int ssize_t; +#endif /* _MSC_VER */ +#endif /* IS_WINDOWS */ + #include "global.h" #include "dtls_debug.h" #include "dtls_ciphers_util.h" @@ -137,6 +161,8 @@ get_psk_info(struct dtls_context_t *ctx UNUSED_PARAM, dtls_credentials_type_t type, const unsigned char *id, size_t id_len, unsigned char *result, size_t result_length) { + (void)ctx; + (void)session; switch (type) { case DTLS_PSK_IDENTITY: @@ -214,11 +240,13 @@ try_send(struct dtls_context_t *ctx, session_t *dst, size_t len, char *buf) { } } +#ifndef IS_WINDOWS static void handle_stdin(size_t *len, char *buf, size_t buf_len) { if (fgets(buf + *len, buf_len - *len, stdin)) *len += strlen(buf + *len); } +#endif /* IS_WINDOWS */ static int read_from_peer(struct dtls_context_t *ctx, @@ -237,7 +265,7 @@ send_to_peer(struct dtls_context_t *ctx, session_t *session, uint8 *data, size_t len) { int fd = *(int *)dtls_get_app_data(ctx); - return sendto(fd, data, len, MSG_DONTWAIT, + return sendto(fd, (const char *)data, len, MSG_DONTWAIT, &session->addr.sa, session->size); } @@ -278,11 +306,15 @@ dtls_handle_read(struct dtls_context_t *ctx) { memset(&session, 0, sizeof(session_t)); session.size = sizeof(session.addr); - len = recvfrom(fd, buf, MAX_READ_BUF, MSG_TRUNC, + len = recvfrom(fd, (char *)buf, MAX_READ_BUF, MSG_TRUNC, &session.addr.sa, &session.size); if (len < 0) { +#ifdef IS_WINDOWS + fprintf(stderr, "recvfrom: %d\n", WSAGetLastError()); +#else /* ! IS_WINDOWS */ perror("recvfrom"); +#endif /* ! IS_WINDOWS */ return -1; } else { dtls_dsrv_log_addr(DTLS_LOG_DEBUG, "peer", &session); @@ -379,9 +411,9 @@ usage( const char *program, const char *version) { "\t-p port\t\tlisten on specified port\n" "\t \t\t(default is an ephemeral free port).\n" "\t-r\t\tforce renegotiation info (RFC5746)\n" - "\t-v num\t\tverbosity level (default: 3)\n" + "\t-v num\t\tverbosity level (default: %d)\n" "\tDefault destination port: %d\n", - DEFAULT_PORT); + dtls_get_log_level(), DEFAULT_PORT); } static dtls_handler_t cb = { @@ -415,7 +447,7 @@ main(int argc, char **argv) { struct timeval timeout; unsigned short dst_port = 0; unsigned short local_port = 0; - log_t log_level = DTLS_LOG_WARN; + log_t log_level = dtls_get_log_level(); int fd; ssize_t result; int on = 1; @@ -429,6 +461,11 @@ main(int argc, char **argv) { memset(&dst, 0, sizeof(session_t)); memset(&listen, 0, sizeof(session_t)); +#ifdef IS_WINDOWS + WSADATA wsaData; + WSAStartup(MAKEWORD(2, 2), &wsaData); +#endif /* IS_WINDOWS */ + dtls_init(); #ifdef DTLS_PSK @@ -530,14 +567,14 @@ main(int argc, char **argv) { dtls_set_log_level(log_level); /* init socket and set it to non-blocking */ - fd = socket(dst.addr.sa.sa_family, SOCK_DGRAM, 0); + fd = socket(dst.addr.sa.sa_family, SOCK_DGRAM, IPPROTO_UDP); if (fd < 0) { dtls_alert("socket: %s\n", strerror(errno)); return 0; } - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ) < 0) { + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on) ) < 0) { dtls_alert("setsockopt SO_REUSEADDR: %s\n", strerror(errno)); } #if 0 @@ -550,15 +587,15 @@ main(int argc, char **argv) { on = 1; if (dst.addr.sa.sa_family == AF_INET6) { #ifdef IPV6_RECVPKTINFO - if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on) ) < 0) { + if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, (const char *)&on, sizeof(on) ) < 0) { #else /* IPV6_RECVPKTINFO */ - if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof(on) ) < 0) { + if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, (const char *)&on, sizeof(on) ) < 0) { #endif /* IPV6_RECVPKTINFO */ dtls_alert("setsockopt IPV6_PKTINFO: %s\n", strerror(errno)); } } else { - if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on) ) < 0) { + if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, (const char *)&on, sizeof(on) ) < 0) { dtls_alert("setsockopt IP_PKTINFO: %s\n", strerror(errno)); } } @@ -581,10 +618,11 @@ main(int argc, char **argv) { } } - if (signal(SIGINT, dtls_handle_signal) == SIG_ERR) { - dtls_alert("An error occurred while setting a signal handler.\n"); - return EXIT_FAILURE; - } + signal(SIGINT, dtls_handle_signal); + signal(SIGTERM, dtls_handle_signal); +#ifdef IS_WINDOWS + signal(SIGBREAK, dtls_handle_signal); +#endif /* IS_WINDOWS */ dtls_context = dtls_new_context(&fd); if (!dtls_context) { @@ -600,33 +638,51 @@ main(int argc, char **argv) { FD_ZERO(&rfds); FD_ZERO(&wfds); +#ifndef IS_WINDOWS FD_SET(fileno(stdin), &rfds); +#endif /* IS_WINDOWS */ FD_SET(fd, &rfds); /* FD_SET(fd, &wfds); */ - timeout.tv_sec = 5; - timeout.tv_usec = 0; + timeout.tv_sec = 0; + timeout.tv_usec = 100000; result = select(fd+1, &rfds, &wfds, 0, &timeout); if (result < 0) { /* error */ +#ifdef IS_WINDOWS + fprintf(stderr, "select: %d\n", WSAGetLastError()); +#else /* ! IS_WINDOWS */ if (errno != EINTR) perror("select"); +#endif /* ! IS_WINDOWS */ } else if (result == 0) { /* timeout */ +#ifdef IS_WINDOWS + if (kbhit()) { + buf[ len++ ] = (char)getch(); + putch(buf[len - 1]); + if (len && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) { + buf[len] = 0; + buf_ready = 1; + } + } +#endif /* ! IS_WINDOWS */ } else { /* ok */ if (FD_ISSET(fd, &wfds)) /* FIXME */; else if (FD_ISSET(fd, &rfds)) dtls_handle_read(dtls_context); +#ifndef IS_WINDOWS else if (FD_ISSET(fileno(stdin), &rfds)) { handle_stdin(&len, buf, sizeof(buf)); if (len && buf[len - 1] == '\n') { buf_ready = 1; } } +#endif /* ! IS_WINDOWS */ } if (buf_ready) { diff --git a/tests/dtls-server.c b/tests/dtls-server.c index 37eeef24..ffc5214a 100644 --- a/tests/dtls-server.c +++ b/tests/dtls-server.c @@ -12,6 +12,8 @@ * *******************************************************************************/ +#include "tinydtls.h" + /* This is needed for apple */ #define __APPLE_USE_RFC_3542 @@ -19,23 +21,31 @@ #include #include #include +#ifdef HAVE_UNISTD_H #include +#else /* ! HAVE_UNISTD_H */ +#include "getopt.c" +#endif /* ! HAVE_UNISTD_H */ #include #ifdef HAVE_SYS_TIME_H #include #endif /* HAVE_SYS_TIME_H */ #include -#include "tinydtls.h" #include "dtls_debug.h" #include "dtls_ciphers_util.h" #include "dtls.h" #ifdef IS_WINDOWS +#include #include -#pragma comment(lib, "Ws2_32.lib") #define MSG_DONTWAIT 0 +#undef MSG_TRUNC #define MSG_TRUNC 0 +#ifdef _MSC_VER +#define STDOUT_FILENO 1 +typedef int ssize_t; +#endif /* _MSC_VER */ #else /* ! IS_WINDOWS */ #include #include @@ -192,7 +202,7 @@ send_to_peer(struct dtls_context_t *ctx, session_t *session, uint8 *data, size_t len) { int fd = *(int *)dtls_get_app_data(ctx); - return sendto(fd, data, len, MSG_DONTWAIT, + return sendto(fd, (const char*)data, len, MSG_DONTWAIT, &session->addr.sa, session->size); } @@ -231,11 +241,15 @@ dtls_handle_read(struct dtls_context_t *ctx) { memset(&session, 0, sizeof(session_t)); session.size = sizeof(session.addr); - len = recvfrom(*fd, buf, sizeof(buf), MSG_TRUNC, + len = recvfrom(*fd, (char*)buf, sizeof(buf), MSG_TRUNC, &session.addr.sa, &session.size); if (len < 0) { +#ifdef IS_WINDOWS + fprintf(stderr, "recvfrom: %d\n", WSAGetLastError()); +#else /* ! IS_WINDOWS */ perror("recvfrom"); +#endif /* ! IS_WINDOWS */ return -1; } else { dtls_debug("got %d bytes from port %d\n", len, @@ -317,8 +331,8 @@ usage(const char *program, const char *version) { fprintf(stderr, "\t-e\t\tforce extended master secret (RFC7627)\n" "\t-p port\t\tlisten on specified port (default is %d)\n" "\t-r\t\tforce renegotiation info (RFC5746)\n" - "\t-v num\t\tverbosity level (default: 3)\n", - DEFAULT_PORT); + "\t-v num\t\tverbosity level (default: %d)\n", + DEFAULT_PORT, dtls_get_log_level()); } static dtls_handler_t cb = { @@ -337,7 +351,7 @@ static dtls_handler_t cb = { int main(int argc, char **argv) { - log_t log_level = DTLS_LOG_WARN; + log_t log_level = dtls_get_log_level(); fd_set rfds, wfds; struct timeval timeout; int fd, opt, result; @@ -396,15 +410,22 @@ main(int argc, char **argv) { dtls_set_log_level(log_level); +#ifdef IS_WINDOWS + WSADATA wsaData; + WSAStartup(MAKEWORD(2, 2), &wsaData); +#endif /* IS_WINDOWS */ + + dtls_init(); + /* init socket and set it to non-blocking */ - fd = socket(listen_addr.sin6_family, SOCK_DGRAM, 0); + fd = socket(listen_addr.sin6_family, SOCK_DGRAM, IPPROTO_UDP); if (fd < 0) { dtls_alert("socket: %s\n", strerror(errno)); return 0; } - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ) < 0) { + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on) ) < 0) { dtls_alert("setsockopt SO_REUSEADDR: %s\n", strerror(errno)); } #if 0 @@ -416,18 +437,18 @@ main(int argc, char **argv) { #endif on = 1; if (listen_addr.sin6_family == AF_INET6) { - if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off)) < 0) { + if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&off, sizeof(off)) < 0) { dtls_alert("setsockopt IPV6_V6ONLY: %s\n", strerror(errno)); } #ifdef IPV6_RECVPKTINFO - if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on) ) < 0) { + if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, (const char*)&on, sizeof(on) ) < 0) { #else /* IPV6_RECVPKTINFO */ - if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof(on) ) < 0) { + if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, (const char*)&on, sizeof(on) ) < 0) { #endif /* IPV6_RECVPKTINFO */ dtls_alert("setsockopt IPV6_PKTINFO: %s\n", strerror(errno)); } } - if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on)) < 0) { + if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, (const char*)&on, sizeof(on)) < 0) { dtls_alert("setsockopt IP_PKTINFO: %s\n", strerror(errno)); } @@ -438,9 +459,11 @@ main(int argc, char **argv) { goto error; } - dtls_init(); - -#ifndef IS_WINDOWS +#ifdef IS_WINDOWS + signal(SIGINT, dtls_handle_signal); + signal(SIGTERM, dtls_handle_signal); + signal(SIGBREAK, dtls_handle_signal); +#else /* ! IS_WINDOWS */ memset (&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = dtls_handle_signal; diff --git a/tests/getopt.c b/tests/getopt.c new file mode 100644 index 00000000..59719e12 --- /dev/null +++ b/tests/getopt.c @@ -0,0 +1,65 @@ +/* + * This file was copied from the following newsgroup posting: + * + * Newsgroups: mod.std.unix + * Subject: public domain AT&T getopt source + * Date: 3 Nov 85 19:34:15 GMT + * + * Here's something you've all been waiting for: the AT&T public domain + * source for getopt(3). It is the code which was given out at the 1985 + * UNIFORUM conference in Dallas. I obtained it by electronic mail + * directly from AT&T. The people there assure me that it is indeed + * in the public domain. + */ + +#include +#include + +static int optind = 1; +static int optopt; +static char *optarg; + +static int +getopt(int argc, char *argv[], const char *opts) { + static int sp = 1; + int c; + char *cp; + + if (sp == 1) { + if (optind >= argc || + argv[optind][0] != '-' || argv[optind][1] == '\0') + return EOF; + else if (!strcmp(argv[optind], "--")) { + optind++; + return EOF; + } + } + optopt = c = argv[optind][sp]; + if (c == ':' || !(cp = strchr(opts, c))) { + fprintf(stderr, ": illegal option -- %c\n", c); + if (argv[optind][++sp] == '\0') { + optind++; + sp = 1; + } + return '?'; + } + if (*++cp == ':') { + if (argv[optind][sp+1] != '\0') + optarg = &argv[optind++][sp+1]; + else if (++optind >= argc) { + fprintf(stderr, ": option requires an argument -- %c\n", c); + sp = 1; + return '?'; + } else + optarg = argv[optind++]; + sp = 1; + } else { + if (argv[optind][++sp] == '\0') { + sp = 1; + optind++; + } + optarg = NULL; + } + + return c; +} diff --git a/tests/unit-tests/CMakeLists.txt b/tests/unit-tests/CMakeLists.txt new file mode 100644 index 00000000..7a32cf39 --- /dev/null +++ b/tests/unit-tests/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.10) + +find_package(CUnit REQUIRED) + +project(testdriver) + +add_executable(testdriver + testdriver.c + test_ccm.c + test_ecc.c + test_prf.c) + +target_link_libraries(testdriver PRIVATE tinydtls CUnit::cunit) +target_compile_options(testdriver PRIVATE -Wall + $<$:-wd4267 -wd4464 -wd4710 -wd4820 -wd5045>) +if(${WARNING_TO_ERROR}) + target_compile_options(dtls-server PRIVATE $,-WX,-Werror>) +endif() + +if(EXISTS "${CUnit_INCLUDE_DIR}/../bin/cunit.dll") + add_custom_command(TARGET testdriver POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CUnit_INCLUDE_DIR}/../bin/cunit.dll" + "$" + COMMENT "Copying cunit.dll to test binary directory") +endif() diff --git a/tests/unit-tests/test_ccm.c b/tests/unit-tests/test_ccm.c index f54df8f9..42d2a561 100644 --- a/tests/unit-tests/test_ccm.c +++ b/tests/unit-tests/test_ccm.c @@ -20,8 +20,6 @@ #include "crypto.h" #include "../ccm-testdata.c" -#include - static uint8_t buf[1024]; static void diff --git a/tests/unit-tests/test_ccm.h b/tests/unit-tests/test_ccm.h index 5a8d933d..d659f6eb 100644 --- a/tests/unit-tests/test_ccm.h +++ b/tests/unit-tests/test_ccm.h @@ -10,6 +10,6 @@ * http://www.eclipse.org/org/documents/edl-v10.php. */ -#include +#include "testdriver.h" CU_pSuite t_init_ccm_tests(void); diff --git a/tests/unit-tests/test_ecc.c b/tests/unit-tests/test_ecc.c index a2dc99fe..b610d3c5 100644 --- a/tests/unit-tests/test_ecc.c +++ b/tests/unit-tests/test_ecc.c @@ -10,8 +10,6 @@ * http://www.eclipse.org/org/documents/edl-v10.php. */ -#include - #include "dtls_config.h" #include "dtls_prng.h" #include "test_ecc.h" @@ -20,8 +18,6 @@ #include "tinydtls.h" #include "crypto.h" -#include - //These are testvalues taken from the NIST P-256 definition //6b17d1f2 e12c4247 f8bce6e5 63a440f2 77037d81 2deb33a0 f4a13945 d898c296 uint32_t BasePointx[8] = { 0xd898c296, 0xf4a13945, 0x2deb33a0, 0x77037d81, @@ -157,20 +153,20 @@ t_test_ecc_ecdsa(void) { ecc_ec_mult(BasePointx, BasePointy, ecdsaTestSecret, pub_x, pub_y); ret = ecc_ecdsa_sign(ecdsaTestSecret, ecdsaTestMessage, ecdsaTestRand1, tempx, tempy); - assert(ecc_isSame(tempx, ecdsaTestresultR1, arrayLength)); - assert(ecc_isSame(tempy, ecdsaTestresultS1, arrayLength)); - assert(ret == 0); + CU_ASSERT(ecc_isSame(tempx, ecdsaTestresultR1, arrayLength)); + CU_ASSERT(ecc_isSame(tempy, ecdsaTestresultS1, arrayLength)); + CU_ASSERT(ret == 0); ret = ecc_ecdsa_validate(pub_x, pub_y, ecdsaTestMessage, tempx, tempy); - assert(ret == 0); + CU_ASSERT(ret == 0); ret = ecc_ecdsa_sign(ecdsaTestSecret, ecdsaTestMessage, ecdsaTestRand2, tempx, tempy); - assert(ecc_isSame(tempx, ecdsaTestresultR2, arrayLength)); - assert(ecc_isSame(tempy, ecdsaTestresultS2, arrayLength)); - assert(ret == 0); + CU_ASSERT(ecc_isSame(tempx, ecdsaTestresultR2, arrayLength)); + CU_ASSERT(ecc_isSame(tempy, ecdsaTestresultS2, arrayLength)); + CU_ASSERT(ret == 0); ret = ecc_ecdsa_validate(pub_x, pub_y, ecdsaTestMessage, tempx, tempy); - assert(ret == 0); + CU_ASSERT(ret == 0); } static void diff --git a/tests/unit-tests/test_ecc.h b/tests/unit-tests/test_ecc.h index ef6ab523..fd42d954 100644 --- a/tests/unit-tests/test_ecc.h +++ b/tests/unit-tests/test_ecc.h @@ -10,6 +10,6 @@ * http://www.eclipse.org/org/documents/edl-v10.php. */ -#include +#include "testdriver.h" CU_pSuite t_init_ecc_tests(void); diff --git a/tests/unit-tests/test_prf.c b/tests/unit-tests/test_prf.c index 1fb53f12..b09a2f37 100644 --- a/tests/unit-tests/test_prf.c +++ b/tests/unit-tests/test_prf.c @@ -19,8 +19,6 @@ #include "tinydtls.h" #include "crypto.h" -#include - /* Check against SHA256 test vector from * https://mailarchive.ietf.org/arch/msg/tls/fzVCzk-z3FShgGJ6DOXqM1ydxms/ */ diff --git a/tests/unit-tests/test_prf.h b/tests/unit-tests/test_prf.h index bc726aba..f57d7603 100644 --- a/tests/unit-tests/test_prf.h +++ b/tests/unit-tests/test_prf.h @@ -10,6 +10,6 @@ * http://www.eclipse.org/org/documents/edl-v10.php. */ -#include +#include "testdriver.h" CU_pSuite t_init_prf_tests(void); diff --git a/tests/unit-tests/testdriver.c b/tests/unit-tests/testdriver.c index 17be7533..936df2b4 100644 --- a/tests/unit-tests/testdriver.c +++ b/tests/unit-tests/testdriver.c @@ -1,7 +1,16 @@ -#include - -#include -#include +/******************************************************************************* + * + * Copyright (c) 2020 Olaf Bergmann (TZI) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v. 1.0 which accompanies this distribution. + * + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + */ + +#include "testdriver.h" #include "test_ccm.h" #include "test_ecc.h" diff --git a/tests/unit-tests/testdriver.h b/tests/unit-tests/testdriver.h new file mode 100644 index 00000000..3f910675 --- /dev/null +++ b/tests/unit-tests/testdriver.h @@ -0,0 +1,26 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Olaf Bergmann (TZI) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v. 1.0 which accompanies this distribution. + * + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + */ + +#ifndef _TESTDRIVER_H_ +#define _TESTDRIVER_H_ + +#include + +// A bug fix for an outdated CUnit 2.1-3 that was compiled using a new MSVC +#if _MSC_VER >= 1900 +#undef snprintf +#endif + +#include +#include + +#endif /* _TESTDRIVER_H_ */