Skip to content

Commit cb7010b

Browse files
committed
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.
1 parent 0f01bad commit cb7010b

File tree

14 files changed

+398
-116
lines changed

14 files changed

+398
-116
lines changed

.github/workflows/main.yml

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,70 @@ env:
1717

1818
jobs:
1919
build-linux-autotools:
20+
name: Build for Linux using Autotools
2021
runs-on: ubuntu-latest
2122
strategy:
2223
matrix:
2324
CC: ["gcc", "clang" ]
25+
2426
steps:
25-
- uses: actions/checkout@v3
26-
- name: setup
27-
run: |
28-
sudo apt-get update && sudo apt-get install -y libcunit1-dev libtool libtool-bin exuberant-ctags valgrind
29-
./autogen.sh
30-
- name: configure
31-
run: |
32-
# mkdir build-${{matrix.CC}}
33-
# cd build-${{matrix.CC}}
34-
$GITHUB_WORKSPACE/configure --enable-tests
35-
- name: compile
36-
run: |
37-
# cd build-${{matrix.CC}}
38-
make EXTRA_CFLAGS=-Werror
39-
- name: tests
40-
run: |
41-
# cd build-${{matrix.CC}}
42-
libtool --mode=execute valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet tests/unit-tests/testdriver
43-
libtool --mode=execute valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet tests/ccm-test
27+
- name: checkout
28+
uses: actions/checkout@v3
29+
- name: setup
30+
uses: awalsh128/cache-apt-pkgs-action@latest
31+
with:
32+
packages: libcunit1-dev libtool libtool-bin exuberant-ctags valgrind
33+
version: 1.0
34+
- name: configure
35+
run: |
36+
# mkdir build-${{matrix.CC}}
37+
# cd build-${{matrix.CC}}
38+
$GITHUB_WORKSPACE/autogen.sh
39+
$GITHUB_WORKSPACE/configure --enable-tests
40+
- name: compile
41+
run: |
42+
# cd build-${{matrix.CC}}
43+
make EXTRA_CFLAGS=-Werror
44+
- name: tests
45+
run: |
46+
# cd build-${{matrix.CC}}
47+
libtool --mode=execute valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet tests/unit-tests/testdriver
48+
libtool --mode=execute valgrind --track-origins=yes --leak-check=yes --show-reachable=yes --error-exitcode=123 --quiet tests/ccm-test
4449
45-
build-linux-cmake:
46-
name: Build for Linux using CMake
47-
runs-on: ubuntu-latest
50+
build-cmake:
51+
name: Build using CMake
52+
runs-on: ${{ matrix.config.os }}
53+
strategy:
54+
matrix:
55+
config:
56+
- os: ubuntu-latest
57+
vcpkg_triplet: x64-linux-release
58+
- os: macos-latest
59+
vcpkg_triplet: arm64-osx-release
60+
- os: windows-latest
61+
vcpkg_triplet: x64-windows-release
4862

4963
steps:
50-
- uses: actions/checkout@v3
51-
- name: Build tinydtls
64+
- name: checkout
65+
uses: actions/checkout@v3
66+
- name: setup
67+
uses: johnwason/vcpkg-action@v7
68+
id: vcpkg
69+
with:
70+
pkgs: cunit
71+
triplet: ${{ matrix.config.vcpkg_triplet }}
72+
cache-key: ${{ matrix.config.os }}
73+
token: ${{ github.token }}
74+
- name: configure
5275
run: |
5376
cmake -E make_directory build_test
5477
cd build_test
55-
cmake -DWARNING_TO_ERROR=ON -Dmake_tests=ON ..
56-
cmake --build .
57-
build-macos-cmake:
58-
name: Build for macOS using CMake
59-
runs-on: macos-latest
60-
61-
steps:
62-
- uses: actions/checkout@v3
63-
- name: Build tinydtls
78+
cmake ${{ steps.vcpkg.outputs.vcpkg-cmake-config }} -G "Unix Makefiles" -DWARNING_TO_ERROR=ON -Dmake_tests=ON ..
79+
- name: compile
6480
run: |
65-
cmake -E make_directory build_test
6681
cd build_test
67-
cmake -DWARNING_TO_ERROR=ON -Dmake_tests=ON ..
6882
cmake --build .
69-
70-
build-windows:
71-
name: Build for Windows using CMake
72-
runs-on: windows-latest
73-
74-
steps:
75-
- uses: actions/checkout@v3
76-
- name: Build tinydtls
83+
- name: test
7784
run: |
78-
cmake -E make_directory build_test
7985
cd build_test
80-
cmake -G "Unix Makefiles" -DWARNING_TO_ERROR=ON ..
81-
cmake --build .
86+
ctest --verbose

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ CMakeFiles
9191
tinydtls.dir
9292
Debug
9393
Release
94+
*.json
95+
out

AutoConf.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ check_function_exists (strdup HAVE_STRDUP)
6868
check_function_exists (strerror HAVE_STRERROR)
6969
check_function_exists (strnlen HAVE_STRNLEN)
7070
check_function_exists (fls HAVE_FLS)
71+
check_function_exists (snprintf HAVE_SNPRINTF)
7172
check_function_exists (vprintf HAVE_VPRINTF)
7273
check_function_exists (inet_ntop HAVE_INET_NTOP)
73-
check_function_exists (random HAVE_RANDOM)
74+
check_function_exists (random HAVE_RANDOM)
7475

7576
if(HAVE_SYS_RANDOM_H)
7677
# zephyr/ncs 1.9.1 seems to link getrandom but doesn't offer a header

CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
#
2020
###############################################################################
2121

22-
cmake_minimum_required(VERSION 3.5)
22+
cmake_minimum_required(VERSION 3.10)
2323

2424
project(tinydtls)
2525

2626
include (AutoConf.cmake)
27+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
2728

2829
if(NOT ZEPHYR_BASE)
2930
option(BUILD_SHARED_LIBS "Link using shared libs" OFF)
@@ -52,6 +53,10 @@ endif()
5253

5354
option(WARNING_TO_ERROR "force all compiler warnings to be errors" OFF)
5455

56+
if(MSVC)
57+
option(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "Export all symbols when compiling to a .dll" ON)
58+
endif()
59+
5560
configure_file(dtls_config.h.cmake.in dtls_config.h )
5661

5762
add_library(tinydtls)
@@ -73,14 +78,13 @@ target_sources(tinydtls PRIVATE
7378
ecc/ecc.c)
7479

7580
target_include_directories(tinydtls PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
76-
target_compile_definitions(tinydtls PUBLIC DTLSv12 WITH_SHA256 SHA2_USE_INTTYPES_H DTLS_CHECK_CONTENTTYPE)
81+
target_compile_definitions(tinydtls PUBLIC DTLSv12 WITH_SHA256 SHA2_USE_INTTYPES_H DTLS_CHECK_CONTENTTYPE $<$<BOOL:${make_tests}>:TEST_INCLUDE>)
7782

7883
if(MSVC)
79-
option(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "Export all symbols when compiling to a .dll" ON)
80-
target_compile_options(tinydtls PRIVATE -Wall)
84+
target_compile_options(tinydtls PRIVATE -Wall -wd4200 -wd4710 -wd4711 -wd4668 -wd4820 -wd5045 -D_CRT_SECURE_NO_WARNINGS)
8185
if(${WARNING_TO_ERROR})
8286
target_compile_options(tinydtls PRIVATE -WX)
83-
endif()
87+
endif()
8488
elseif(NOT ZEPHYR_BASE)
8589
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)
8690
if(${WARNING_TO_ERROR})
@@ -92,6 +96,10 @@ set_target_properties(tinydtls PROPERTIES VERSION ${PACKAGE_VERSION} SOVERSION $
9296

9397
if( ${make_tests} )
9498
add_subdirectory(tests)
99+
add_subdirectory(tests/unit-tests)
100+
include(CTest)
101+
add_test(NAME test COMMAND testdriver)
102+
add_test(NAME ccm-test COMMAND ccm-test)
95103
endif()
96104

97105
if(BUILD_SHARED_LIBS)

cmake/FindCUnit.cmake

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# FindCUnit
2+
# ---------
3+
#
4+
# Find the CUnit library.
5+
#
6+
# Imported Targets
7+
# ^^^^^^^^^^^^^^^^
8+
#
9+
# This module defines the following :prop_tgt:`IMPORTED` targets:
10+
#
11+
# ``CUnit::cunit``
12+
# The CUnit library, if found.
13+
#
14+
# Result Variables
15+
# ^^^^^^^^^^^^^^^^
16+
#
17+
# This module will set the following variables in your project:
18+
#
19+
# ``CUnit_FOUND``
20+
# System has the CUnit library.
21+
# ``CUnit_VERSION``
22+
# The version of CUnit found.
23+
# ``CUnit_INCLUDE_DIR``
24+
# The CUnit include directory.
25+
# ``CUnit_LIBRARIES``
26+
# All CUnit libraries.
27+
#
28+
# Hints
29+
# ^^^^^
30+
#
31+
# Set ``CUnit_ROOT_DIR`` to the root directory of a CUnit installation.
32+
33+
if(CUnit_INCLUDE_DIR AND CUnit_LIBRARIES)
34+
# in cache already
35+
set(CUnit_FIND_QUIETLY TRUE)
36+
endif()
37+
38+
if(CUnit_ROOT_DIR)
39+
set(_CUnit_EXTRA_FIND_ARGS "NO_CMAKE_FIND_ROOT_PATH")
40+
endif()
41+
42+
find_path(
43+
CUnit_INCLUDE_DIR
44+
NAMES CUnit/CUnit.h
45+
PATH_SUFFIXES include
46+
HINTS ${PROJECT_SOURCE_DIR}
47+
${CMAKE_CURRENT_BINARY_DIR}
48+
${CUnit_ROOT_DIR}
49+
${_CUnit_EXTRA_FIND_ARGS})
50+
51+
find_library(
52+
CUnit_LIBRARIES
53+
NAMES cunit
54+
PATH_SUFFIXES lib
55+
HINTS ${PROJECT_SOURCE_DIR}
56+
${CMAKE_CURRENT_BINARY_DIR}
57+
${CUnit_ROOT_DIR}
58+
${_CUnit_EXTRA_FIND_ARGS})
59+
60+
if(CUnit_INCLUDE_DIR AND EXISTS "${CUnit_INCLUDE_DIR}/CUnit/CUnit.h")
61+
file(STRINGS "${CUnit_INCLUDE_DIR}/CUnit/CUnit.h" CUnit_VERSION_STR
62+
REGEX "#[\t ]*define[\t ]+CU_VERSION[\t ]+\"[^\"]+\"")
63+
string(REGEX REPLACE "^.*CU_VERSION[\t ]+\"([^\"]+)\""
64+
"\\1" CUnit_VERSION_STR "${CUnit_VERSION_STR}")
65+
set(CUnit_VERSION "${CUnit_VERSION_STR}")
66+
unset(CUnit_VERSION_STR)
67+
endif()
68+
69+
include(FindPackageHandleStandardArgs)
70+
find_package_handle_standard_args(
71+
CUnit
72+
FAIL_MESSAGE "Could NOT find CUnit, try to set the path to CUnit root folder in the system variable CUnit_ROOT_DIR"
73+
REQUIRED_VARS CUnit_INCLUDE_DIR
74+
CUnit_LIBRARIES
75+
VERSION_VAR CUnit_VERSION)
76+
77+
if(NOT
78+
TARGET
79+
CUnit::cunit)
80+
add_library(
81+
CUnit::cunit
82+
UNKNOWN
83+
IMPORTED)
84+
set_target_properties(
85+
CUnit::cunit
86+
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CUnit_INCLUDE_DIR}"
87+
VERSION "${CUnit_VERSION}"
88+
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
89+
IMPORTED_LOCATION "${CUnit_LIBRARIES}")
90+
endif()
91+
92+
mark_as_advanced(
93+
CUnit_INCLUDE_DIR
94+
CUnit_LIBRARIES)

dtls.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ dtls_prepare_record(dtls_peer_t *peer, dtls_security_parameters_t *security,
16781678
unsigned int i;
16791679

16801680
if (*rlen < DTLS_RH_LENGTH) {
1681-
dtls_alert("The sendbuf (%zu bytes) is too small\n", *rlen);
1681+
dtls_alert("The sendbuf (%u bytes) is too small\n", (unsigned)*rlen);
16821682
return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
16831683
}
16841684

@@ -3437,8 +3437,8 @@ check_server_certificate(dtls_context_t *ctx,
34373437
data += DTLS_HS_LENGTH;
34383438

34393439
if (dtls_uint24_to_int(data) != DTLS_EC_SUBJECTPUBLICKEY_SIZE) {
3440-
dtls_alert("expect length of %zu bytes for certificate\n",
3441-
DTLS_EC_SUBJECTPUBLICKEY_SIZE);
3440+
dtls_alert("expect length of %u bytes for certificate\n",
3441+
(unsigned)DTLS_EC_SUBJECTPUBLICKEY_SIZE);
34423442
return dtls_alert_fatal_create(DTLS_ALERT_DECODE_ERROR);
34433443
}
34443444
data += sizeof(uint24);

dtls_debug.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ dsrv_print_addr(const session_t *addr, char *buf, size_t len) {
247247
#endif /* ! HAVE_INET_NTOP */
248248
}
249249

250-
#if !defined(WITH_CONTIKI) && !defined(_MSC_VER)
250+
#if !defined(WITH_CONTIKI)
251251

252252
static void
253253
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
410410
dtls_mutex_lock(&static_mutex);
411411
#endif /* DTLS_CONSTRAINED_STACK */
412412
if (extend) {
413-
snprintf(message, sizeof(message), "%s: (%zu bytes):\n", name, length);
413+
snprintf(message, sizeof(message), "%s: (%u bytes):\n", name, (unsigned)length);
414414
log_handler(level, message);
415415
len = 0;
416416
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
439439
}
440440
}
441441
} else {
442-
len = snprintf(message, sizeof(message), "%s: (%zu bytes): ", name, length);
442+
len = snprintf(message, sizeof(message), "%s: (%u bytes): ", name, (unsigned)length);
443443
while (length-- && len + 1 < sizeof(message)) {
444444
len += snprintf(&message[len], sizeof(message)-len, "%02X", *buf++);
445445
}
@@ -474,7 +474,7 @@ dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, s
474474
PRINTF("%s ", loglevels[level]);
475475

476476
if (extend) {
477-
PRINTF("%s: (%zu bytes):\n", name, length);
477+
PRINTF("%s: (%u bytes):\n", name, (unsigned)length);
478478

479479
while (length--) {
480480
if (n % 16 == 0)
@@ -491,7 +491,7 @@ dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, s
491491
}
492492
}
493493
} else {
494-
PRINTF("%s: (%zu bytes): ", name, length);
494+
PRINTF("%s: (%u bytes): ", name, (unsigned)length);
495495
while (length--)
496496
PRINTF("%02X", *buf++);
497497
}

dtls_debug.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ void dtls_set_log_handler(dtls_log_handler_t app_handler);
100100
* Writes the given text to \c stdout. The text is output only when \p
101101
* level is below or equal to the log level that set by
102102
* set_log_level(). */
103-
#ifdef HAVE_VPRINTF
103+
#if !defined(WITH_CONTIKI)
104104
#if (defined(__GNUC__) && !defined(__MINGW32__))
105105
void dsrv_log(log_t level, const char *format, ...) __attribute__ ((format(printf, 2, 3)));
106106
#else /* !__GNUC__ && !__MINGW32__ */
107107
void dsrv_log(log_t level, const char *format, ...);
108108
#endif /* !__GNUC__ && !__MINGW32__ */
109-
#else
109+
#elif defined(HAVE_VPRINTF)
110110
#define dsrv_log(level, format, ...) PRINTF(format, ##__VA_ARGS__)
111111
#endif
112112

@@ -129,8 +129,8 @@ void dtls_dsrv_log_addr(log_t level, const char *name, const session_t *addr);
129129
#define dtls_notice(...) LOG_INF(__VA_ARGS__)
130130
#define dtls_info(...) LOG_INF(__VA_ARGS__)
131131
#define dtls_debug(...) LOG_DBG(__VA_ARGS__)
132-
#define dtls_debug_hexdump(name, buf, length) { LOG_DBG("%s (%zu bytes):", name, (size_t)(length)); LOG_HEXDUMP_DBG(buf, length, name); }
133-
#define dtls_debug_dump(name, buf, length) { LOG_DBG("%s (%zu bytes):", name, (size_t)(length)); LOG_HEXDUMP_DBG(buf, length, name); }
132+
#define dtls_debug_hexdump(name, buf, length) { LOG_DBG("%s (%u bytes):", name, (unsigned)(length)); LOG_HEXDUMP_DBG(buf, length, name); }
133+
#define dtls_debug_dump(name, buf, length) { LOG_DBG("%s (%u bytes):", name, (unsigned)(length)); LOG_HEXDUMP_DBG(buf, length, name); }
134134
#elif defined(RIOT_VERSION)
135135
#define dtls_emerg(...) LOG_ERROR(__VA_ARGS__)
136136
#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);
139139
#define dtls_notice(...) LOG_INFO(__VA_ARGS__)
140140
#define dtls_info(...) LOG_INFO(__VA_ARGS__)
141141
#define dtls_debug(...) LOG_DEBUG(__VA_ARGS__)
142-
#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); }}
143-
#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); }}
142+
#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); }}
143+
#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); }}
144144
#else /* neither RIOT nor Zephyr */
145145
#define dtls_emerg(...) dsrv_log(DTLS_LOG_EMERG, __VA_ARGS__)
146146
#define dtls_alert(...) dsrv_log(DTLS_LOG_ALERT, __VA_ARGS__)

0 commit comments

Comments
 (0)