1/* libcoap unit tests 2 * 3 * Copyright (C) 2018,2022-2023 Olaf Bergmann <bergmann@tzi.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 * 7 * This file is part of the CoAP library libcoap. Please see 8 * README for terms of use. 9 */ 10 11#include "test_common.h" 12#include "test_tls.h" 13 14#undef HAVE_DTLS 15 16#ifdef COAP_WITH_LIBTINYDTLS 17#define HAVE_DTLS 1 18 19/* Need to #undef these to stop compiler warnings when tinydtls.h is included */ 20#undef PACKAGE_BUGREPORT 21#undef PACKAGE_NAME 22#undef PACKAGE_STRING 23#undef PACKAGE_TARNAME 24#undef PACKAGE_URL 25#undef PACKAGE_VERSION 26 27#include <tinydtls.h> 28#include <dtls.h> 29#include <dtls_debug.h> 30#endif /* COAP_WITH_LIBTINYDTLS */ 31 32#ifdef COAP_WITH_LIBOPENSSL 33#define HAVE_DTLS 1 34#include <openssl/ssl.h> 35#endif /* COAP_WITH_LIBOPENSSL */ 36 37#ifdef COAP_WITH_LIBGNUTLS 38#define HAVE_DTLS 1 39#include <gnutls/gnutls.h> 40#endif /* COAP_WITH_LIBGNUTLS */ 41 42#ifdef COAP_WITH_LIBMBEDTLS 43#define HAVE_DTLS 1 44#include <mbedtls/version.h> 45#endif /* COAP_WITH_LIBMBEDTLS */ 46 47static void 48t_tls1(void) { 49 int need_dtls = 0; 50#ifdef HAVE_DTLS 51 need_dtls = 1; 52#endif /* HAVE_DTLS */ 53 54 CU_ASSERT(coap_dtls_is_supported() == need_dtls); 55} 56 57static void 58t_tls2(void) { 59 coap_tls_version_t *v = coap_get_tls_library_version(); 60 coap_tls_version_t version; 61 62 memset(&version, 0, sizeof(coap_tls_version_t)); 63 64#if defined(COAP_WITH_LIBOPENSSL) 65 version.version = SSLeay(); 66 version.type = COAP_TLS_LIBRARY_OPENSSL; 67#elif defined(COAP_WITH_LIBTINYDTLS) 68 const char *vers = dtls_package_version(); 69 version.version = 0; 70 if (vers) { 71 long int p1, p2 = 0, p3 = 0; 72 char *endptr; 73 74 p1 = strtol(vers, &endptr, 10); 75 if (*endptr == '.') { 76 p2 = strtol(endptr+1, &endptr, 10); 77 if (*endptr == '.') { 78 p3 = strtol(endptr+1, &endptr, 10); 79 } 80 } 81 version.version = (p1 << 16) | (p2 << 8) | p3; 82 } 83 version.type = COAP_TLS_LIBRARY_TINYDTLS; 84#elif defined(COAP_WITH_LIBGNUTLS) 85 version.version = GNUTLS_VERSION_NUMBER; 86 version.type = COAP_TLS_LIBRARY_GNUTLS; 87#elif defined(COAP_WITH_LIBMBEDTLS) 88 version.version = MBEDTLS_VERSION_NUMBER; 89 version.type = COAP_TLS_LIBRARY_MBEDTLS; 90#else /* no DTLS */ 91 version.version = 0; 92 version.type = COAP_TLS_LIBRARY_NOTLS; 93#endif /* COAP_WITH_LIBOPENSSL || COAP_WITH_LIBTINYDTLS */ 94 95 CU_ASSERT_PTR_NOT_NULL_FATAL(v); 96 CU_ASSERT(version.version == v->version); 97 CU_ASSERT(version.type == v->type); 98} 99 100static int 101t_tls_tests_create(void) { 102 coap_startup(); 103 return 0; 104} 105 106CU_pSuite 107t_init_tls_tests(void) { 108 CU_pSuite suite; 109 110 suite = CU_add_suite("TLS", t_tls_tests_create, NULL); 111 if (!suite) { /* signal error */ 112 fprintf(stderr, "W: cannot add TLS test suite (%s)\n", 113 CU_get_error_msg()); 114 115 return NULL; 116 } 117 118#define TLS_TEST(s,t) \ 119 if (!CU_ADD_TEST(s,t)) { \ 120 fprintf(stderr, "W: cannot add TLS test (%s)\n", \ 121 CU_get_error_msg()); \ 122 } 123 124 TLS_TEST(suite, t_tls1); 125 TLS_TEST(suite, t_tls2); 126 127 return suite; 128} 129