tests: add unit test boilerplate for (D)TLS

A simple structure for (D)TLS-specific unit tests has been added.
Currently, only general support and the version number as reported
by coap_get_tls_library_version() are checked.
This commit is contained in:
Olaf Bergmann committed 2018-02-14 17:28:37 +01:00
1 parent b1943714ad
commit c74e1da5a9
4 files changed
+113 -1

No files matched your search

+2 -1
View File
@@ -21,7 +21,8 @@ testdriver_SOURCES = \
test_pdu.c \
test_sendqueue.c \
test_uri.c \
test_wellknown.c
test_wellknown.c \
test_tls.c
testdriver_LDADD = $(CUNIT_LIBS) $(DTLS_LIBS) $(top_builddir)/.libs/libcoap-$(LIBCOAP_API_VERSION).la
+98
View File
@@ -0,0 +1,98 @@
/* libcoap unit tests
*
* Copyright (C) 2018 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap_config.h"
#include "test_tls.h"
#include <coap.h>
#undef HAVE_DTLS
#ifdef HAVE_LIBTINYDTLS
#define HAVE_DTLS 1
#include <tinydtls.h>
#include <dtls.h>
#endif /* HAVE_LIBTINYDTLS */
#ifdef HAVE_OPENSSL
#define HAVE_DTLS 1
#include <openssl/ssl.h>
#endif /* HAVE_OPENSSL */
#ifdef HAVE_GNUTLS
#define HAVE_DTLS 1
#include <gnutls/gnutls.h>
#endif /* HAVE_GNUTLS */
static void
t_tls1(void) {
int need_dtls = 0;
#ifdef HAVE_DTLS
need_dtls = 1;
#endif /* HAVE_DTLS */
CU_ASSERT(coap_dtls_is_supported() == need_dtls);
}
static void
t_tls2(void) {
coap_tls_version_t *v = coap_get_tls_library_version();
coap_tls_version_t version;
memset(&version, 0, sizeof(coap_tls_version_t));
#if defined(HAVE_OPENSSL)
version.version = SSLeay();
version.type = COAP_TLS_LIBRARY_OPENSSL;
#elif defined(HAVE_LIBTINYDTLS)
version.version = DTLS_VERSION;
version.type = COAP_TLS_LIBRARY_TINYDTLS;
#elif defined(HAVE_GNUTLS)
version.version = GNUTLS_VERSION_NUMBER;
version.type = COAP_TLS_LIBRARY_GNUTLS;
#else /* no DTLS */
version.version = 0;
version.type = COAP_TLS_LIBRARY_NOTLS;
#endif /* HAVE_OPENSSL || HAVE_LIBTINYDTLS */
CU_ASSERT_PTR_NOT_NULL_FATAL(v);
CU_ASSERT(version.version == v->version);
CU_ASSERT(version.type == v->type);
}
static int
t_tls_tests_create(void) {
coap_dtls_startup();
return 0;
}
CU_pSuite
t_init_tls_tests(void) {
CU_pSuite suite;
suite = CU_add_suite("TLS", t_tls_tests_create, NULL);
if (!suite) { /* signal error */
fprintf(stderr, "W: cannot add TLS test suite (%s)\n",
CU_get_error_msg());
return NULL;
}
#define TLS_TEST(s,t) \
if (!CU_ADD_TEST(s,t)) { \
fprintf(stderr, "W: cannot add TLS test (%s)\n", \
CU_get_error_msg()); \
}
TLS_TEST(suite, t_tls1);
TLS_TEST(suite, t_tls2);
return suite;
}
+11
View File
@@ -0,0 +1,11 @@
/* libcoap unit tests
*
* Copyright (C) 2018 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include <CUnit/CUnit.h>
CU_pSuite t_init_tls_tests(void);
+2
View File
@@ -11,6 +11,7 @@
#include "test_error_response.h"
#include "test_sendqueue.h"
#include "test_wellknown.h"
#include "test_tls.h"
#include "libcoap.h"
#ifdef __GNUC__
@@ -36,6 +37,7 @@ main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) {
t_init_error_response_tests();
t_init_sendqueue_tests();
t_init_wellknown_tests();
t_init_tls_tests();
CU_basic_set_mode(run_mode);
result = CU_basic_run_tests();