Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 53 additions & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,66 @@ jobs:

steps:
- uses: actions/checkout@v6
- name: setup
run: |
sudo apt-get update && sudo apt-get install -y libcunit1-dev
- name: Build tinydtls
run: |
cmake -E make_directory build_test
cd build_test
cmake -DWARNING_TO_ERROR=ON -Dmake_tests=ON ..
cmake --build .
- name: Install tinydtls
run: |
cd build_test
mkdir -p test-install
cp cmake_install.cmake test-install
cmake --install ./test-install --prefix ./test-install

build-macos-cmake:
name: Build for macOS using CMake
runs-on: macos-latest

steps:
- uses: actions/checkout@v6
- name: setup

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a MacOS-specific change in a PR that explicitly addresses MSVC is a bit weird.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair comment - I will remove the Mac-OS CI run from building the tests and create a separate PR for it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, because building unit-tests is introduced in this PR, cunit needs to be installed, or MAC (APPLE) needs to be explicitly removed from building testdriver in tests/CMakeLists.txt and then a new PR created specifically for MacOS.

run: |
brew install cunit
brew --prefix cunit
- name: Build tinydtls
run: |
cmake -E make_directory build_test
cd build_test
cmake -DWARNING_TO_ERROR=ON -Dmake_tests=ON ..
cmake --build .
- name: Install tinydtls
run: |
cd build_test
mkdir -p test-install
cp cmake_install.cmake test-install
cmake --install ./test-install --prefix ./test-install

build-windows-unix:
name: Build for Windows using CMake + Unix Makefiles
runs-on: windows-latest

steps:
- uses: actions/checkout@v6
- name: Build tinydtls
run: |
cmake -E make_directory build_test
cd build_test
cmake -G "Unix Makefiles" -DWARNING_TO_ERROR=ON -Dmake_tests=ON ..
cmake --build .
- name: Install tinydtls
run: |
cd build_test
mkdir -p test-install
cp cmake_install.cmake test-install
cmake --install ./test-install --prefix ./test-install

build-windows:
name: Build for Windows using CMake
build-windows-vs:
name: Build for Windows using CMake + Visual Studio
runs-on: windows-latest

steps:
Expand All @@ -77,7 +116,7 @@ jobs:
run: |
cmake -E make_directory build_test
cd build_test
cmake -G "Unix Makefiles" -DWARNING_TO_ERROR=ON ..
cmake -Dmake_tests=ON ..
cmake --build .

mingw-cmake:
Expand Down Expand Up @@ -106,13 +145,18 @@ jobs:
with:
msystem: ${{matrix.OS}}
update: true
install: git mingw-w64-${{env.MINGW_VARIANT}}-gcc mingw-w64-${{env.MINGW_VARIANT}}-cmake mingw-w64-${{env.MINGW_VARIANT}}-openssl
install: git mingw-w64-${{env.MINGW_VARIANT}}-gcc mingw-w64-${{env.MINGW_VARIANT}}-cmake mingw-w64-${{env.MINGW_VARIANT}}-openssl mingw-w64-${{env.MINGW_VARIANT}}-cunit
- name: MinGW ${{matrix.OS}} build
run: |
echo 'Running in MSYS2 ${{matrix.OS}}!'
cmake -E remove_directory build
cmake -E make_directory build
cd build
cmake .. -DWARNING_TO_ERROR=ON
cmake -E remove_directory build_test
cmake -E make_directory build_test
cd build_test
cmake -DWARNING_TO_ERROR=ON -Dmake_tests=ON ..
cmake --build .
cmake --build . -- install
- name: MinGW ${{matrix.OS}} install
run: |
cd build_test
mkdir -p test-install
cp cmake_install.cmake test-install
cmake --install ./test-install --prefix ./test-install
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ endif()

option(make_tests "Make test programs and examples" OFF)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()

if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()

if(NOT PLATFORM)
# PLATFORM seems to be not used
set(PLATFORM "posix" CACHE STRING "Choose platform." FORCE)
Expand Down Expand Up @@ -77,7 +85,7 @@ target_compile_definitions(tinydtls PUBLIC DTLSv12 WITH_SHA256 SHA2_USE_INTTYPES

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 /wd4820 /wd4668)
if(${WARNING_TO_ERROR})
target_compile_options(tinydtls PRIVATE -WX)
endif()
Expand All @@ -92,6 +100,7 @@ set_target_properties(tinydtls PROPERTIES VERSION ${PACKAGE_VERSION} SOVERSION $

if( ${make_tests} )
add_subdirectory(tests)
target_compile_options(tinydtls PRIVATE -DTEST_INCLUDE)
endif()

if(BUILD_SHARED_LIBS)
Expand Down
6 changes: 3 additions & 3 deletions dtls_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,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) {
Expand Down Expand Up @@ -322,7 +322,7 @@ dsrv_log(log_t level, const char *format, ...) {

#elif defined (HAVE_VPRINTF) /* WITH_CONTIKI */
void
dsrv_log(log_t level, char *format, ...) {
dsrv_log(log_t level, const char *format, ...) {
static char timebuf[32];
va_list ap;

Expand Down Expand Up @@ -370,7 +370,7 @@ void dump(unsigned char *buf, size_t len) {
void dtls_dsrv_log_addr(log_t level, const char *name, const session_t *addr)
{
char addrbuf[73];
int len;
size_t len;

len = dsrv_print_addr(addr, addrbuf, sizeof(addrbuf));
if (!len)
Expand Down
2 changes: 1 addition & 1 deletion dtls_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ 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(HAVE_VPRINTF) || defined (_MSC_VER)
#if (defined(__GNUC__) && !defined(__MINGW32__))
void dsrv_log(log_t level, const char *format, ...) __attribute__ ((format(printf, 2, 3)));
#else /* !__GNUC__ && !__MINGW32__ */
Expand Down
48 changes: 39 additions & 9 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,54 @@ cmake_minimum_required(VERSION 3.10)

project(tinydtls-tests LANGUAGES C )

if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
# set(COMPILE_OPTIONS -DTEST_INCLUDE -DDTLSv12 -DWITH_SHA256 /Wall /wd4820 /wd4668)
set(COMPILE_OPTIONS -DTEST_INCLUDE -DDTLSv12 -DWITH_SHA256)
if(${WARNING_TO_ERROR})
set(COMPILE_OPTIONS -WX ${COMPILE_OPTIONS})
endif()
else()
set(COMPILE_OPTIONS -Wall -DTEST_INCLUDE -DDTLSv12 -DWITH_SHA256)
if(${WARNING_TO_ERROR})
set(COMPILE_OPTIONS -Werror ${COMPILE_OPTIONS})
endif()
if(APPLE)
set(COMPILE_OPTIONS -I/opt/homebrew/opt/cunit/include ${COMPILE_OPTIONS})
endif()
endif()

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()

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)
if(${WARNING_TO_ERROR})
target_compile_options(dtls-server PUBLIC -Werror)
if(MINGW OR WIN32)
target_link_libraries(dtls-server LINK_PUBLIC Ws2_32)
endif()
target_compile_options(dtls-server PUBLIC ${COMPILE_OPTIONS})

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)
if(${WARNING_TO_ERROR})
target_compile_options(ccm-test PUBLIC -Werror)
if(MINGW OR WIN32)
target_link_libraries(ccm-test LINK_PUBLIC Ws2_32)
endif()
target_compile_options(ccm-test PUBLIC ${COMPILE_OPTIONS})

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)
if(${WARNING_TO_ERROR})
target_compile_options(dtls-client PUBLIC -Werror)
if(MINGW OR WIN32)
target_link_libraries(dtls-client LINK_PUBLIC Ws2_32)
endif()
target_compile_options(dtls-client PUBLIC ${COMPILE_OPTIONS})

if(NOT MSVC AND NOT MINGW)
add_executable(testdriver unit-tests/testdriver.c unit-tests/test_ccm.c unit-tests/test_ecc.c unit-tests/test_prf.c)
target_link_libraries(testdriver LINK_PUBLIC tinydtls)
if(APPLE)
target_link_options(testdriver PUBLIC -L/opt/homebrew/opt/cunit/lib)
endif()
target_link_libraries(testdriver LINK_PUBLIC cunit)
target_compile_options(testdriver PUBLIC ${COMPILE_OPTIONS})
endif()
6 changes: 3 additions & 3 deletions tests/ccm-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int main(int argc, char **argv) {
data[n].msg, data[n].la);

len += + data[n].la;
printf("Packet Vector #%lu ", n+1);
printf("Packet Vector #%zu ", n+1);
if ((size_t)len != data[n].r_lm || memcmp(data[n].msg, data[n].result, len))
printf("FAILED, ");
else
Expand All @@ -77,9 +77,9 @@ int main(int argc, char **argv) {
data[n].msg, data[n].la);

if (len < 0)
printf("Packet Vector #%lu: cannot decrypt message\n", n+1);
printf("Packet Vector #%zu: cannot decrypt message\n", n+1);
else
printf("\t*** MAC verified (total length = %lu) ***\n", len + data[n].la);
printf("\t*** MAC verified (total length = %zu) ***\n", len + data[n].la);
}

#ifdef WITH_CONTIKI
Expand Down
49 changes: 41 additions & 8 deletions tests/dtls-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,55 @@
/* This is needed for apple */
#define __APPLE_USE_RFC_3542

#include "tinydtls.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <ctype.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif /* HAVE_NETINET_IN_H */
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif /* HAVE_SYS_SOCKET_H */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */

#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif /* HAVE_ARPA_INET_H */
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif /* HAVE_NETDB_H */
#include <signal.h>

#ifdef IS_WINDOWS
#ifdef _MSC_VER
#include "getopt.c"
#define fileno _fileno
#endif /* _MSC_VER */
#define OPTVAL_T(t) (const char*)(t)
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1 /* Standard output. */
#endif /* STDOUT_FILENO */
#else /* ! IS_WINDOWS */
#define OPTVAL_T(t) (t)
#endif /* ! IS_WINDOWS */

#include "global.h"
#include "dtls_debug.h"
#include "dtls_ciphers_util.h"
#include "dtls.h"

#ifndef MSG_TRUNC
#define MSG_TRUNC 0
#endif /* MSG_TRUNC */

#define DEFAULT_PORT 20220

#define PSK_DEFAULT_IDENTITY "Client_identity"
Expand Down Expand Up @@ -94,10 +122,10 @@ static const unsigned char ecdsa_pub_key_y[] = {
#endif /* DTLS_ECC */

#ifdef DTLS_PSK
static ssize_t
static int
read_from_file(char *arg, unsigned char *buf, size_t max_buf_len) {
FILE *f;
ssize_t result = 0;
int result = 0;

f = fopen(arg, "r");
if (f == NULL)
Expand Down Expand Up @@ -237,8 +265,13 @@ send_to_peer(struct dtls_context_t *ctx,
session_t *session, uint8 *data, size_t len) {

int fd = *(int *)dtls_get_app_data(ctx);
#ifdef IS_WINDOWS
return sendto(fd, (char *)data, len, 0,
&session->addr.sa, session->size);
#else /* ! IS_WINDOWS */
return sendto(fd, data, len, MSG_DONTWAIT,
&session->addr.sa, session->size);
#endif /* ! IS_WINDOWS */
}

static void
Expand Down Expand Up @@ -278,7 +311,7 @@ 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) {
Expand Down Expand Up @@ -420,7 +453,7 @@ main(int argc, char **argv) {
unsigned short local_port = 0;
log_t log_level = DTLS_LOG_WARN;
int fd;
ssize_t result;
int result;
int on = 1;
int opt = 0;
int res;
Expand Down Expand Up @@ -540,7 +573,7 @@ main(int argc, char **argv) {
return 0;
}

if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ) < 0) {
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on), sizeof(on) ) < 0) {
dtls_alert("setsockopt SO_REUSEADDR: %s\n", strerror(errno));
}
#if 0
Expand All @@ -553,15 +586,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, OPTVAL_T(&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, OPTVAL_T(&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, OPTVAL_T(&on), sizeof(on) ) < 0) {
dtls_alert("setsockopt IP_PKTINFO: %s\n", strerror(errno));
}
}
Expand Down
Loading
Loading