• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libcoap unit tests
2  *
3  * Copyright (C) 2018 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 HAVE_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 /* HAVE_LIBTINYDTLS */
31 
32 #ifdef HAVE_OPENSSL
33 #define HAVE_DTLS 1
34 #include <openssl/ssl.h>
35 #endif /* HAVE_OPENSSL */
36 
37 #ifdef HAVE_LIBGNUTLS
38 #define HAVE_DTLS 1
39 #include <gnutls/gnutls.h>
40 #endif /* HAVE_LIBGNUTLS */
41 
42 #ifdef HAVE_MBEDTLS
43 #define HAVE_DTLS 1
44 #include <mbedtls/version.h>
45 #endif /* HAVE_MBEDTLS */
46 
47 static void
t_tls1(void)48 t_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 
57 static void
t_tls2(void)58 t_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(HAVE_OPENSSL)
65   version.version = SSLeay();
66   version.type = COAP_TLS_LIBRARY_OPENSSL;
67 #elif defined(HAVE_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(HAVE_LIBGNUTLS)
85   version.version = GNUTLS_VERSION_NUMBER;
86   version.type = COAP_TLS_LIBRARY_GNUTLS;
87 #elif defined(HAVE_MBEDTLS)
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 /* HAVE_OPENSSL || HAVE_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 
100 static int
t_tls_tests_create(void)101 t_tls_tests_create(void) {
102   coap_startup();
103   return 0;
104 }
105 
106 CU_pSuite
t_init_tls_tests(void)107 t_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 
130