From 85fe8977e98cc0e84609e9250ba34b0d455b96df Mon Sep 17 00:00:00 2001 From: Cristian Pop Date: Tue, 8 Dec 2020 15:03:30 -0800 Subject: [PATCH] [LTS_07_2020] Fixing OpenSSL and WolfSSL hostname validation. Removing ESP adapters. (#490) * Fixing OpenSSL and WolfSSL hostname validation. Removing ESP adapters. * Fixing OpenSSL version detection. Removing gcc44, gcc46 support. Fixing wolf/bear build scripts. --- adapters/agenttime_esp8266.c | 27 - adapters/esp8266_mock.h | 306 ---- adapters/platform_esp8266.c | 45 - adapters/threadapi_esp8266.c | 31 - adapters/tickcounter_esp8266.c | 56 - adapters/tlsio_openssl.c | 44 +- adapters/tlsio_ssl_esp8266.c | 880 ---------- adapters/tlsio_wolfssl.c | 44 +- build/.vsts-ci.yml | 44 - jenkins/linux_bearssl.sh | 2 +- jenkins/linux_wolfssl.sh | 2 +- tests/CMakeLists.txt | 1 - tests/tlsio_esp8266_ut/CMakeLists.txt | 24 - tests/tlsio_esp8266_ut/main.c | 12 - tests/tlsio_esp8266_ut/tlsio_esp8266_ut.c | 1875 --------------------- tests/tlsio_wolfssl_ut/tlsio_wolfssl_ut.c | 38 +- 16 files changed, 122 insertions(+), 3309 deletions(-) delete mode 100644 adapters/agenttime_esp8266.c delete mode 100644 adapters/esp8266_mock.h delete mode 100644 adapters/platform_esp8266.c delete mode 100644 adapters/threadapi_esp8266.c delete mode 100644 adapters/tickcounter_esp8266.c delete mode 100644 adapters/tlsio_ssl_esp8266.c delete mode 100644 tests/tlsio_esp8266_ut/CMakeLists.txt delete mode 100644 tests/tlsio_esp8266_ut/main.c delete mode 100644 tests/tlsio_esp8266_ut/tlsio_esp8266_ut.c diff --git a/adapters/agenttime_esp8266.c b/adapters/agenttime_esp8266.c deleted file mode 100644 index 2e501f2e1..000000000 --- a/adapters/agenttime_esp8266.c +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#include "azure_c_shared_utility/agenttime.h" -#include "azure_c_shared_utility/xlogging.h" -#include "lwip/apps/sntp.h" -#include "lwip/apps/sntp_time.h" - -time_t get_time(time_t* currentTime) -{ - return sntp_get_current_timestamp(); -} - -double get_difftime(time_t stopTime, time_t startTime) -{ - return (double)stopTime - (double)startTime; -} - -struct tm* get_gmtime(time_t* currentTime) -{ - return NULL; -} - -char* get_ctime(time_t* timeToGet) -{ - return NULL; -} diff --git a/adapters/esp8266_mock.h b/adapters/esp8266_mock.h deleted file mode 100644 index 3531026f9..000000000 --- a/adapters/esp8266_mock.h +++ /dev/null @@ -1,306 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#ifndef _ESP8266_MOCK_H_ -#define _ESP8266_MOCK_H_ - -#include -#include "umock_c/umock_c_prod.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -typedef void SSL; -typedef void SSL_CTX; -typedef void SSL_METHOD; - -/* - * SSL_CTX_new - create a SSL context - * - * @param method - the SSL context configuration file - * - * @return the context point, if create failed return NULL - */ -//SSL_CTX* SSL_CTX_new(SSL_METHOD *method); -MOCKABLE_FUNCTION(, SSL_CTX*, SSL_CTX_new, SSL_METHOD*, method); - - -/* - * SSL_CTX_free - free a SSL context - * - * @param method - the SSL context point - * - * @return none - */ -//void SSL_CTX_free(SSL_CTX *ctx); -MOCKABLE_FUNCTION(, void, SSL_CTX_free, SSL_CTX*, ctx); - -/* - * SSL_new - create a SSL - * - * @param ssl_ctx - the SSL context which includes the SSL parameter - * - * @return the result - * result = 0 : successfully - * result < 0 : error, you may see the mbedtls error code - */ -//SSL* SSL_new(SSL_CTX *ssl_ctx); -MOCKABLE_FUNCTION(, SSL*, SSL_new, SSL_CTX*, ssl_ctx); - -/* - * SSL_free - free the SSL - * - * @param ssl - the SSL point which has been "SSL_new" - * - * @return none - */ -//void SSL_free(SSL *ssl); -MOCKABLE_FUNCTION(, void, SSL_free, SSL*, ssl); - -/* - * SSL_connect - connect to the remote SSL server - * - * @param ssl - the SSL point which has been "SSL_new" - * - * @return the result - * result = 0 : successfully - * result < 0 : error, you can see the mbedtls error code - */ -//int SSL_connect(SSL *ssl); -MOCKABLE_FUNCTION(, int, SSL_connect, SSL*, ssl); - -/* - * SSL_read - read data from remote - * - * @param ssl - the SSL point which has been connected - * @param buffer - the received data point - * @param len - the received data length - * - * @return the result - * result > 0 : the length of the received data - * result = 0 : the connect is closed - * result < 0 : error, you can see the mbedtls error code - */ -// int SSL_read(SSL *ssl, void *buffer, int len); -MOCKABLE_FUNCTION(, int, SSL_read, SSL*, ssl, void*, buffer, int, len); - -/* - * SSL_write - send the data to remote - * - * @param ssl - the SSL point which has been connected - * @param buffer - the send data point - * @param len - the send data length - * - * @return the result of verifying - * result > 0 : the length of the written data - * result = 0 : the connect is closed - * result < 0 : error, you can see the mbedtls error code - */ -//int SSL_write(SSL *ssl, const void *buffer, int len); -MOCKABLE_FUNCTION(, int, SSL_write, SSL*, ssl, const void*, buffer, int, len); - -/* - * SSL_shutdown - shutdown the connection to the remote - * - * @param ssl - the SSL point which has been connected or accepted - * - * @return the result - * result = 0 : successfully - * result < 0 : error, you may see the mbedtls error code - */ -//int SSL_shutdown(SSL *ssl); -MOCKABLE_FUNCTION(, int, SSL_shutdown, SSL*, ssl); - -/* - * SSL_set_fd - set the socket file description to the SSL - * - * @param ssl - the SSL point which has been "SSL_new" - * @param fd - socket file description - * - * @return the result - * result = 1 : successfully - * result <= 0 : error, SSL is NULL or socket file description is NULL - */ -//int SSL_set_fd(SSL *ssl, int fd); -MOCKABLE_FUNCTION(, int, SSL_set_fd, SSL*, ssl, int, fd); - -/* - * TLSv1_client_method - create the target SSL context client method - * - * @return the TLSV1.0 version SSL context client method - */ -//SSL_METHOD* TLSv1_client_method(void); -MOCKABLE_FUNCTION(, SSL_METHOD*, TLSv1_client_method); - -/** - * @brief set the SSL context read buffer length - * - * @param ctx - SSL context point - * @param len - read buffer length - * - * @return none - */ -//void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len); -MOCKABLE_FUNCTION(, void, SSL_CTX_set_default_read_buffer_len, SSL_CTX*, ctx, size_t, len); - -/** - * @brief get SSL error code - * - * @param ssl - SSL point - * @param ret_code - SSL return code - * - * @return SSL error number - */ -//int SSL_get_error(const SSL *ssl, int ret_code); -MOCKABLE_FUNCTION(, int, SSL_get_error, const SSL*, ssl, int, ret_code); - -#define MEMP_NUM_NETCONN 10 - - -typedef uint32_t u32_t; -#define socklen_t unsigned int -typedef uint8_t u8_t; -typedef uint8_t uint8; -typedef uint16_t u16_t; -typedef int32_t err_t; - -struct ip_addr { - u32_t addr; -}; -typedef struct ip_addr ip_addr_t; - -#define SOL_SOCKET 0xfff /* options for socket level */ -#define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */ -#define SO_RCVBUF 0x1002 /* receive buffer size */ -#define SO_SNDLOWAT 0x1003 /* Unimplemented: send low-water mark */ -#define SO_RCVLOWAT 0x1004 /* Unimplemented: receive low-water mark */ -#define SO_SNDTIMEO 0x1005 /* Unimplemented: send timeout */ -#define SO_RCVTIMEO 0x1006 /* receive timeout */ -#define SO_ERROR 0x1007 /* get error status and clear */ -#define SO_TYPE 0x1008 /* get socket type */ -#define SO_CONTIMEO 0x1009 /* Unimplemented: connect timeout */ -#define SO_NO_CHECK 0x100a /* don't create UDP checksum */ -#define SO_REUSEADDR 1 /* Enable address reuse */ - -#define SO_KEEPALIVE 0x0008 /* keep connections alive */ -#define IPPROTO_TCP 6 -#define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */ -#define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */ -#define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */ -#define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */ -#define SSL_ERROR_WANT_READ 2 -#define SSL_ERROR_WANT_WRITE 3 - -#define AF_INET 2 -/* Socket protocol types (TCP/UDP/RAW) */ -#define SOCK_STREAM 1 -#define SOCK_DGRAM 2 -#define SOCK_RAW 3 - -int ioctl(int s, long cmd, void *argp); -#define F_GETFL 3 -#define F_SETFL 4 -#define O_NONBLOCK 1 /* nonblocking I/O */ -#define O_NDELAY 1 /* same as O_NONBLOCK, for compatibility */ -#define LOCAL - - - -typedef u8_t sa_family_t; -typedef u16_t in_port_t; - -typedef u32_t in_addr_t; - -struct in_addr { - in_addr_t s_addr; -}; - - -struct sockaddr_in { - u8_t sin_len; - sa_family_t sin_family; - in_port_t sin_port; - struct in_addr sin_addr; -#define SIN_ZERO_LEN 8 - char sin_zero[SIN_ZERO_LEN]; -}; - - -struct sockaddr { - u8_t sa_len; - sa_family_t sa_family; -#if LWIP_IPV6 - char sa_data[22]; -#else /* LWIP_IPV6 */ - char sa_data[14]; -#endif /* LWIP_IPV6 */ -}; - -/* FD_SET used for lwip_select */ -//int my_FD_ISSET(int n, void* p); -#ifndef FD_SET - #undef FD_SETSIZE - /* Make FD_SETSIZE match NUM_SOCKETS in socket.c */ - #define FD_SETSIZE MEMP_NUM_NETCONN - #define FD_SET(n, p) ((p)->fd_bits[(n)/8] |= (1 << ((n) & 7))) - #define FD_CLR(n, p) ((p)->fd_bits[(n)/8] &= ~(1 << ((n) & 7))) - //#define FD_ISSET(n,p) my_FD_ISSET(n, p) - //((p)->fd_bits[(n)/8] & (1 << ((n) & 7))) - #define FD_ZERO(p) memset((void*)(p),0,sizeof(*(p))) - - typedef struct fd_set { - unsigned char fd_bits [(FD_SETSIZE+7)/8]; - } fd_set; - -#endif /* FD_SET */ - -struct timeval { - long tv_sec; /* seconds */ - long tv_usec; /* and microseconds */ -}; - -//err_t netconn_gethostbyname(const char *name, ip_addr_t *addr); -MOCKABLE_FUNCTION(, err_t, netconn_gethostbyname, const char*, name, ip_addr_t*, addr); - -//int socket(int domain, int type, int protocol); -MOCKABLE_FUNCTION(, int, socket, int, domain, int, type, int, protocol); - -//int bind(int s, const struct sockaddr* name, socklen_t namelen); -MOCKABLE_FUNCTION(, int, bind, int, s, const struct sockaddr*, name, socklen_t, namelen); - -//int connect(int s, const struct sockaddr *name, socklen_t namelen); -MOCKABLE_FUNCTION(, int, connect, int, s, const struct sockaddr*, name, socklen_t, namelen); - -//int lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) -MOCKABLE_FUNCTION(, int, getsockopt, int, s, int, level, int, optname, void*, optval, socklen_t*, optlen); - -//int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, -// struct timeval *timeout); -MOCKABLE_FUNCTION(, int, lwip_select, int, maxfdp1, fd_set*, readset, fd_set*, writeset, fd_set*, exceptset, struct timeval*, timeout); - -//os_delay_us(int us); -MOCKABLE_FUNCTION(, void, os_delay_us, int, us); - -//int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen); -MOCKABLE_FUNCTION(, int, setsockopt, int, s, int, level, int, optname, const void*, optval, socklen_t, optlen); - -//int close(int s) -MOCKABLE_FUNCTION(, int, close, int, s); - -MOCKABLE_FUNCTION(, int, FD_ISSET, int, n, void*, p); - -//int fcntl(int s, int cmd, int val); -MOCKABLE_FUNCTION(, int, fcntl, int, s, int, cmd, int, val); - - -#define htons(x) (x) -#define ntohs(x) (x) -#define htonl(x) (x) -#define ntohl(x) (x) - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif diff --git a/adapters/platform_esp8266.c b/adapters/platform_esp8266.c deleted file mode 100644 index e1792a86d..000000000 --- a/adapters/platform_esp8266.c +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#include -#ifdef _CRTDBG_MAP_ALLOC -#include -#endif -#include "azure_c_shared_utility/platform.h" -#include "azure_c_shared_utility/xio.h" -#include "azure_c_shared_utility/tlsio_openssl.h" -#include "azure_c_shared_utility/xlogging.h" -#include "lwip/apps/sntp.h" -#include "lwip/apps/sntp_time.h" - -int platform_init(void) -{ - sntp_init(); - u32_t ts = 0; - while(ts == 0){ - vTaskDelay(5000 / portTICK_RATE_MS); - ts = sntp_get_current_timestamp(); - LogInfo("%s", sntp_get_real_time(ts)); - } - return 0; -} - -const IO_INTERFACE_DESCRIPTION* platform_get_default_tlsio(void) -{ - return tlsio_openssl_get_interface_description(); -} - -STRING_HANDLE platform_get_platform_info(PLATFORM_INFO_OPTION options) -{ - // No applicable options, so ignoring parameter - (void)options; - - // Expected format: "(; ; )" - - return STRING_construct("(native; esp8266; undefined)"); -} - -void platform_deinit(void) -{ - sntp_stop(); -} diff --git a/adapters/threadapi_esp8266.c b/adapters/threadapi_esp8266.c deleted file mode 100644 index ad173deab..000000000 --- a/adapters/threadapi_esp8266.c +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#include -#include -#include "azure_c_shared_utility/threadapi.h" -#include "azure_c_shared_utility/xlogging.h" - -MU_DEFINE_ENUM_STRINGS(THREADAPI_RESULT, THREADAPI_RESULT_VALUES); - -THREADAPI_RESULT ThreadAPI_Create(THREAD_HANDLE* threadHandle, THREAD_START_FUNC func, void* arg) -{ - LogError("ESP8266 RTOS does not support multi-thread function."); - return THREADAPI_ERROR; -} - -THREADAPI_RESULT ThreadAPI_Join(THREAD_HANDLE threadHandle, int* res) -{ - LogError("ESP8266 RTOS does not support multi-thread function."); - return THREADAPI_ERROR; -} - -void ThreadAPI_Exit(int res) -{ - vTaskDelete(NULL); -} - -void ThreadAPI_Sleep(unsigned int milliseconds) -{ - vTaskDelay(milliseconds); -} diff --git a/adapters/tickcounter_esp8266.c b/adapters/tickcounter_esp8266.c deleted file mode 100644 index 357b4ec35..000000000 --- a/adapters/tickcounter_esp8266.c +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (C) Firmwave Ltd., All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#include -#ifdef _CRTDBG_MAP_ALLOC -#include -#endif - -#include "azure_c_shared_utility/gballoc.h" - -#include -#include -#include "azure_c_shared_utility/tickcounter.h" -#include "azure_c_shared_utility/xlogging.h" - -typedef struct TICK_COUNTER_INSTANCE_TAG -{ - unsigned char dummy; -} TICK_COUNTER_INSTANCE; - -TICK_COUNTER_HANDLE tickcounter_create(void) -{ - TICK_COUNTER_INSTANCE* result = (TICK_COUNTER_INSTANCE*)malloc(sizeof(TICK_COUNTER_INSTANCE)); - if (result == NULL) - { - LogError("Failed creating tick counter"); - } - return result; -} - -void tickcounter_destroy(TICK_COUNTER_HANDLE tick_counter) -{ - if (tick_counter != NULL) - { - free(tick_counter); - } -} - -int tickcounter_get_current_ms(TICK_COUNTER_HANDLE tick_counter, tickcounter_ms_t * current_ms) -{ - int result; - - if (tick_counter == NULL || current_ms == NULL) - { - LogError("tickcounter failed: Invalid Arguments.\r\n"); - result = __LINE__; - } - else - { - //Currently configTICK_RATE_HZ is set to 100 (100Hz); a tick is 10ms - *current_ms = (tickcounter_ms_t)xTaskGetTickCount()*10; - result = 0; - } - - return result; -} diff --git a/adapters/tlsio_openssl.c b/adapters/tlsio_openssl.c index e793d4627..a73c129c0 100644 --- a/adapters/tlsio_openssl.c +++ b/adapters/tlsio_openssl.c @@ -75,6 +75,7 @@ typedef struct TLS_IO_INSTANCE_TAG TLS_CERTIFICATE_VALIDATION_CALLBACK tls_validation_callback; void* tls_validation_callback_data; char* hostname; + bool ignore_host_name_check; } TLS_IO_INSTANCE; struct CRYPTO_dynlock_value @@ -932,6 +933,31 @@ static int add_certificate_to_store(TLS_IO_INSTANCE* tls_io_instance, const char return result; } +static int enable_domain_check(TLS_IO_INSTANCE* tlsInstance) +{ + int result = 0; + + if (!tlsInstance->ignore_host_name_check) + { +#if (OPENSSL_VERSION_NUMBER < 0x10002000L) +#error "OpenSSL v1.0.2 or above required. See https://wiki.openssl.org/index.php/Hostname_validation for details." +#endif + X509_VERIFY_PARAM *param = SSL_get0_param(tlsInstance->ssl); + + X509_VERIFY_PARAM_set_hostflags(param, 0); + if (!X509_VERIFY_PARAM_set1_host(param, tlsInstance->hostname, strlen(tlsInstance->hostname))) + { + result = MU_FAILURE; + } + else + { + SSL_set_verify(tlsInstance->ssl, SSL_VERIFY_PEER, NULL); + } + } + + return result; +} + static int create_openssl_instance(TLS_IO_INSTANCE* tlsInstance) { int result; @@ -1055,7 +1081,18 @@ static int create_openssl_instance(TLS_IO_INSTANCE* tlsInstance) (void)BIO_free(tlsInstance->out_bio); SSL_CTX_free(tlsInstance->ssl_context); tlsInstance->ssl_context = NULL; - log_ERR_get_error("Failed setting SSL hostname."); + log_ERR_get_error("Failed setting SNI hostname hint."); + result = MU_FAILURE; + } + else if (enable_domain_check(tlsInstance)) + { + SSL_free(tlsInstance->ssl); + tlsInstance->ssl = NULL; + (void)BIO_free(tlsInstance->in_bio); + (void)BIO_free(tlsInstance->out_bio); + SSL_CTX_free(tlsInstance->ssl_context); + tlsInstance->ssl_context = NULL; + log_ERR_get_error("Failed to configure domain name verification."); result = MU_FAILURE; } else @@ -1183,6 +1220,7 @@ CONCRETE_IO_HANDLE tlsio_openssl_create(void* io_create_parameters) result->tls_validation_callback_data = NULL; result->x509_certificate = NULL; result->x509_private_key = NULL; + result->ignore_host_name_check = false; result->tls_version = VERSION_1_2; @@ -1638,8 +1676,10 @@ int tlsio_openssl_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, c // No need to do anything for Openssl result = 0; } - else if (strcmp("ignore_server_name_check", optionName) == 0) + else if (strcmp("ignore_host_name_check", optionName) == 0) { + bool* server_name_check = (bool*)value; + tls_io_instance->ignore_host_name_check = *server_name_check; result = 0; } else diff --git a/adapters/tlsio_ssl_esp8266.c b/adapters/tlsio_ssl_esp8266.c deleted file mode 100644 index 9f096373b..000000000 --- a/adapters/tlsio_ssl_esp8266.c +++ /dev/null @@ -1,880 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#include -#ifdef _CRTDBG_MAP_ALLOC -#include -#endif - -#ifdef FREERTOS_ARCH_ESP8266 -#include "openssl/ssl.h" -#include "lwip/opt.h" -#include "lwip/sockets.h" -#include "lwip/sys.h" - -#else -//mock header -#include "esp8266_mock.h" -#include "azure_c_shared_utility/gballoc.h" -#endif - -#include -#include -#include "azure_c_shared_utility/lock.h" -/* Codes_SRS_TLSIO_SSL_ESP8266_99_001: [ The tlsio_ssl_esp8266 shall implement and export all the Concrete functions in the VTable IO_INTERFACE_DESCRIPTION defined in the xio.h. ]*/ -/* Codes_SRS_TLSIO_SSL_ESP8266_99_002: [ The tlsio_ssl_esp8266 shall report the open operation status using the IO_OPEN_RESULT enumerator defined in the xio.h.]*/ -/* Codes_SRS_TLSIO_SSL_ESP8266_99_003: [ The tlsio_ssl_esp8266 shall report the send operation status using the IO_SEND_RESULT enumerator defined in the xio.h. ]*/ -/* Codes_SRS_TLSIO_SSL_ESP8266_99_004: [ The tlsio_ssl_esp8266 shall call the callbacks functions defined in the xio.h. ]*/ -#include "azure_c_shared_utility/tlsio_openssl.h" -/* Codes_SRS_TLSIO_SSL_ESP8266_99_005: [ The tlsio_ssl_esp8266 shall received the connection information using the TLSIO_CONFIG structure defined in tlsio.h. ]*/ -#include "azure_c_shared_utility/tlsio.h" -#include "azure_c_shared_utility/socketio.h" -#include "azure_c_shared_utility/xlogging.h" -#include "azure_c_shared_utility/crt_abstractions.h" - -#define OPENSSL_FRAGMENT_SIZE 5120 -#define OPENSSL_LOCAL_TCP_PORT 1000 -/* Codes_SSRS_TLSIO_SSL_ESP8266_99_027: [ The tlsio_openssl_open shall set the tlsio to try to open the connection for MAX_RETRY times before assuming that connection failed. ]*/ -#define MAX_RETRY 20 -#define RETRY_DELAY 1000 * 1000 * 10 // 10s -#define RECEIVE_BUFFER_SIZE 1024 -#define OPENSSL_SELECT_TIMEOUT 20 - -struct timeval timeout = { OPENSSL_SELECT_TIMEOUT, 0 }; - -typedef enum TLSIO_STATE_TAG -{ - TLSIO_STATE_NOT_OPEN, - TLSIO_STATE_OPENING, - TLSIO_STATE_OPEN, - TLSIO_STATE_CLOSING, - TLSIO_STATE_ERROR -} TLSIO_STATE; - -typedef struct TLS_IO_INSTANCE_TAG -{ - ON_BYTES_RECEIVED on_bytes_received; - ON_IO_OPEN_COMPLETE on_io_open_complete; - ON_IO_CLOSE_COMPLETE on_io_close_complete; - ON_IO_ERROR on_io_error; - void* on_bytes_received_context; - void* on_io_open_complete_context; - void* on_io_close_complete_context; - void* on_io_error_context; - SSL* ssl; - SSL_CTX* ssl_context; - TLSIO_STATE tlsio_state; - char* hostname; - int port; - char* certificate; - const char* x509certificate; - const char* x509privatekey; - int sock; - ip_addr_t target_ip; -} TLS_IO_INSTANCE; - -/*this function destroys an option previously created*/ -static void tlsio_openssl_DestroyOption(const char* name, const void* value) -{ - /*since all options for this layer are actually string copies, disposing of one is just calling free*/ - if ( - (name == NULL) || (value == NULL) - ) - { - LogError("invalid parameter detected: const char* name=%p, const void* value=%p", name, value); - } - else - { - if ( - (strcmp(name, "TrustedCerts") == 0) || - (strcmp(name, "x509certificate") == 0) || - (strcmp(name, "x509privatekey") == 0) - ) - { - free((void*)value); - } - else - { - LogError("not handled option : %s", name); - } - } -} - - -/* Codes_SRS_TLSIO_SSL_ESP8266_99_078: [ The tlsio_openssl_retrieveoptions shall not do anything, and return NULL. ]*/ -static OPTIONHANDLER_HANDLE tlsio_openssl_retrieveoptions(CONCRETE_IO_HANDLE tlsio_handle) -{ - (void)(tlsio_handle); - - (void)tlsio_openssl_DestroyOption; - /* Not implementing any options */ - return NULL; -} - -static const IO_INTERFACE_DESCRIPTION tlsio_openssl_interface_description = -{ - tlsio_openssl_retrieveoptions, - tlsio_openssl_create, - tlsio_openssl_destroy, - tlsio_openssl_open, - tlsio_openssl_close, - tlsio_openssl_send, - tlsio_openssl_dowork, - tlsio_openssl_setoption -}; - -static void indicate_open_complete(TLS_IO_INSTANCE* tls_io_instance, IO_OPEN_RESULT open_result) -{ - if (tls_io_instance->on_io_open_complete == NULL) - { - LogError("NULL on_io_open_complete."); - } - else - { - /* Codes_TLSIO_SSL_ESP8266_99_002: [ The tlsio_ssl_esp8266 shall report the open operation status using the IO_OPEN_RESULT enumerator defined in the xio.h.]*/ - tls_io_instance->on_io_open_complete(tls_io_instance->on_io_open_complete_context, open_result); - } -} - - -static int lwip_net_errno(int fd) -{ - int sock_errno = 0; - u32_t optlen = sizeof(sock_errno); - getsockopt(fd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); - return sock_errno; -} - -static void lwip_set_non_block(int fd) -{ - int flags = -1; - int error = 0; - - while((void)1,1){ - flags = fcntl(fd, F_GETFL, 0); - if (flags == -1){ - error = lwip_net_errno(fd); - if (error != EINTR){ - break; - } - } else{ - break; - } - } - - fcntl(fd, F_SETFL, O_NONBLOCK); - -} - -LOCAL int openssl_thread_LWIP_CONNECTION(TLS_IO_INSTANCE* p) -{ - //LogInfo("openssl_thread_LWIP_CONNECTION begin: %d", system_get_free_heap_size()); - //system_show_malloc(); - - int result = 0; - int ret = 0; - int sock; - - struct sockaddr_in sock_addr; - fd_set readset; - fd_set writeset; - fd_set errset; - SSL_CTX *ctx; - SSL *ssl; - - TLS_IO_INSTANCE* tls_io_instance = p; - - LogInfo("OpenSSL thread start..."); - - { - LogInfo("create SSL context"); - /* Codes_SRS_TLSIO_SSL_ESP8266_99_085: [ If SSL_CTX_new failed, the tlsio_openssl_open shall return __LINE__. ] */ - ctx = SSL_CTX_new(TLSv1_client_method()); - if (ctx == NULL) { - result = __LINE__; - LogError("create new SSL CTX failed"); - } - else - { - tls_io_instance->ssl_context = ctx; - LogInfo("set SSL context read buffer size"); - SSL_CTX_set_default_read_buffer_len(ctx, OPENSSL_FRAGMENT_SIZE); - // LogInfo("create socket ......"); - // LogInfo("size before creating socket: %d", system_get_free_heap_size()); - /* Codes_SRS_TLSIO_SSL_ESP8266_99_080: [ If socket failed, the tlsio_openssl_open shall return __LINE__. ] */ - sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0) { - result = __LINE__; - LogError("create socket failed"); - } - else - { - int keepAlive = 1; //enable keepalive - int keepIdle = 20; //20s - int keepInterval = 2; //2s - int keepCount = 3; //retry # of times - - tls_io_instance->sock = sock; - LogInfo("sock: %d", sock); - LogInfo("create socket OK"); - - LogInfo("set socket keep-alive "); - - ret = 0; - ret = ret || setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive, sizeof(keepAlive)); - ret = ret || setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, (void *)&keepIdle, sizeof(keepIdle)); - ret = ret || setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, (void *)&keepInterval, sizeof(keepInterval)); - ret = ret || setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, (void *)&keepCount, sizeof(keepCount)); - - if (ret != 0){ - result = __LINE__; - LogError("set socket keep-alive failed, ret = %d ", ret); - } - else - { - LogInfo("set socket keep-alive OK"); - - lwip_set_non_block(sock); - - LogInfo("bind socket ......"); - memset(&sock_addr, 0, sizeof(sock_addr)); - sock_addr.sin_family = AF_INET; - sock_addr.sin_addr.s_addr = 0; - sock_addr.sin_port = 0; // random local port - /* Codes_SRS_TLSIO_SSL_ESP8266_99_082: [ If bind failed, the tlsio_openssl_open shall return __LINE__. ] */ - ret = bind(sock, (struct sockaddr*)&sock_addr, sizeof(sock_addr)); - - // LogInfo("bind return: %d", ret); - if (ret != 0) { - result = __LINE__; - LogError("bind socket failed"); - } - else - { - LogInfo("bind socket OK"); - - memset(&sock_addr, 0, sizeof(sock_addr)); - sock_addr.sin_family = AF_INET; - sock_addr.sin_addr.s_addr = tls_io_instance->target_ip.addr; - sock_addr.sin_port = (in_port_t)htons(tls_io_instance->port); - - ret = connect(sock, (struct sockaddr*)&sock_addr, sizeof(sock_addr)); - //LogInfo("connect return: %d %s", ret, ip_ntoa(&tls_io_instance->target_ip)); - //LogInfo("EINPROGRESS: %d", EINPROGRESS); - if (ret == -1) { - ret = lwip_net_errno(sock); - LogInfo("lwip_net_errno ret: %d", ret); - /* Codes_SRS_TLSIO_SSL_ESP8266_99_083: [ If connect and getsockopt failed, the tlsio_openssl_open shall return __LINE__. ] */ - if (ret != 115) { // EINPROGRESS - result = __LINE__; - ret = -1; - LogError("socket connect failed, not EINPROGRESS %s", tls_io_instance->hostname); - } - } - - if(ret != -1) - { - FD_ZERO(&readset); - FD_ZERO(&writeset); - FD_ZERO(&errset); - - FD_SET(sock, &readset); - FD_SET(sock, &writeset); - FD_SET(sock, &errset); - - ret = lwip_select(sock + 1, NULL, &writeset, &errset, NULL); - if (ret <= 0) { - result = __LINE__; - LogError("select failed: %d", lwip_net_errno(sock)); - } else - { - if (!FD_ISSET(sock, &writeset) || FD_ISSET(sock, &errset)) { - result = __LINE__; - LogError("socket Error: %d", lwip_net_errno(sock)); - }else - { - { - LogInfo("Socket Connect OK"); - /* Codes_SRS_TLSIO_SSL_ESP8266_99_087: [ If SSL_new failed, the tlsio_openssl_open shall return __LINE__. ] */ - ssl = SSL_new(ctx); - //LogInfo("after ssl new"); - if (ssl == NULL) { - result = __LINE__; - LogError("create ssl failed"); - } - else - { - - tls_io_instance->ssl = ssl; - // LogInfo("SSL set fd"); - /* Codes_SRS_TLSIO_SSL_ESP8266_99_088: [ If SSL_set_fd failed, the tlsio_openssl_open shall return __LINE__. ] */ - ret = SSL_set_fd(ssl, sock); - LogInfo("SSL_set_fd ret:%d", ret); - if (ret != 1){ - result = __LINE__; - LogError("SSL_set_fd failed"); - } - else{ - int retry_connect = 0; - int connect_succeeded = false; - - LogInfo("SSL connect... "); - - FD_ZERO(&readset); - FD_SET(sock, &readset); - FD_ZERO(&writeset); - FD_SET(sock, &writeset); - FD_ZERO(&errset); - FD_SET(sock, &errset); - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_027: [ The tlsio_openssl_open shall set the tlsio to try to open the connection for MAX_RETRY times before assuming that connection failed. ]*/ - while (retry_connect < MAX_RETRY) - { - int ssl_state; - - ret = lwip_select(sock + 1, &readset, &writeset, &errset, &timeout); - - if (ret == 0) { - result = __LINE__; - LogInfo("SSL connect timeout"); - break; - } - if (FD_ISSET(sock, &errset)) { - unsigned int len; - result = __LINE__; - LogInfo("error return : %d", lwip_net_errno(sock)); - len = (unsigned int) sizeof( int ); - if (0 != getsockopt (sock, SOL_SOCKET, SO_ERROR, &ret, &len)) - LogInfo("SSL error ret : %d", ret); // socket is in error state - break; - } - - ret = SSL_connect(ssl); - if (ret == 1) { // ssl connect success - connect_succeeded = true; - break; - } - - FD_ZERO(&readset); - FD_ZERO(&writeset); - FD_ZERO(&errset); - FD_SET(sock, &errset); - - ssl_state = SSL_get_error(ssl, ret); - if (ssl_state == SSL_ERROR_WANT_READ) { - FD_SET(sock, &readset); - } else if(ssl_state == SSL_ERROR_WANT_WRITE) { - FD_SET(sock, &writeset); - } else { - LogInfo("SSL state:%d", ssl_state); - result = __LINE__; - break; - } - - retry_connect = retry_connect + 1; - //LogInfo("SSL connect retry: %d", retry_connect); - os_delay_us(RETRY_DELAY); - } - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_089: [ If SSL_connect failed, the tlsio_openssl_open shall return __LINE__. ] */ - if (connect_succeeded == false) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_042: [ If the tlsio_openssl_open retry SSL_connect to open more than MAX_RETRY times without success, it shall return __LINE__. ]*/ - result = __LINE__; - LogError("SSL_connect failed"); - }else{ - LogInfo("SSL connect ok"); - result = 0; - } - } - } - } - - } - } - } - } - - } - } - - } - } - //LogInfo("openssl_thread_LWIP_CONNECTION end: %d", system_get_free_heap_size()); - - if(result!=0){ - tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; - } - return result; -} - -static int decode_ssl_received_bytes(TLS_IO_INSTANCE* tls_io_instance) -{ - int result; - /* Codes_SRS_TLSIO_SSL_ESP8266_99_075: [ The tlsio_openssl_dowork shall create a buffer to store the data received from the ssl client. ]*/ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_076: [ The tlsio_openssl_dowork shall delete the buffer to store the data received from the ssl client. ]*/ - unsigned char buffer[RECEIVE_BUFFER_SIZE]; - int rcv_bytes; - - rcv_bytes = SSL_read(tls_io_instance->ssl, buffer, sizeof(buffer)); - // LogInfo("ssl_read: rcv_bytes %d", rcv_bytes); - /* Codes_SRS_TLSIO_SSL_ESP8266_99_071: [ If there are no received data in the ssl client, the tlsio_openssl_dowork shall do nothing. ]*/ - if (rcv_bytes >= 0) - { - result = 0; - if (tls_io_instance->on_bytes_received == NULL) - { - LogError("NULL on_bytes_received."); - } - else - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_070: [ If there are received data in the ssl client, the tlsio_openssl_dowork shall read this data and call the on_bytes_received with the pointer to the buffer with the data. ]*/ - tls_io_instance->on_bytes_received(tls_io_instance->on_bytes_received_context, buffer, rcv_bytes); - } - } - else{ - result = __LINE__; - } - - return result; -} - -static int destroy_openssl_instance(TLS_IO_INSTANCE* tls_io_instance) -{ - int result = 0; - //LogInfo("destroy openssl begin: %d", system_get_free_heap_size()); - if (tls_io_instance != NULL) - { - if (tls_io_instance->ssl != NULL) - { - SSL_free(tls_io_instance->ssl); - tls_io_instance->ssl = NULL; - LogInfo("SSL_free"); - } - if (tls_io_instance->sock >= 0) - { - int close_ret = close(tls_io_instance->sock); - if (close_ret != 0){ - result = __LINE__;; - LogError("close socket failed"); - } - LogInfo("close socket"); - } - if (tls_io_instance->ssl_context != NULL) - { - SSL_CTX_free(tls_io_instance->ssl_context); - tls_io_instance->ssl_context = NULL; - LogInfo("SSL_ctx_free"); - } - - //LogInfo("destroy end: %d", system_get_free_heap_size()); - } - - return result; -} - -/* Codes_SRS_TLSIO_SSL_ESP8266_99_005: [ The tlsio_ssl_esp8266 shall received the connection information using the TLSIO_CONFIG structure defined in tlsio.h. ]*/ -/* Codes_SRS_TLSIO_SSL_ESP8266_99_009: [ The tlsio_openssl_create shall create a new instance of the tlsio for esp8266. ]*/ -/* Codes_SRS_TLSIO_SSL_ESP8266_99_017: [ The tlsio_openssl_create shall receive the connection configuration (TLSIO_CONFIG). ]*/ -CONCRETE_IO_HANDLE tlsio_openssl_create(void* io_create_parameters) -{ - //LogInfo("tlsio_openssl_create begin: %d", system_get_free_heap_size()); - /* Codes_SRS_TLSIO_SSL_ESP8266_99_005: [ The tlsio_ssl_esp8266 shall received the connection information using the TLSIO_CONFIG structure defined in tlsio.h. ]*/ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_017: [ The tlsio_openssl_create shall receive the connection configuration (TLSIO_CONFIG). ]*/ - TLSIO_CONFIG* tls_io_config = (TLSIO_CONFIG*)io_create_parameters; - TLS_IO_INSTANCE* result; - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_013: [ The tlsio_openssl_create shall return NULL when io_create_parameters is NULL. ]*/ - if (tls_io_config == NULL) - { - result = NULL; - LogError("NULL tls_io_config."); - } - else - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_011: [ The tlsio_openssl_create shall allocate memory to control the tlsio instance. ]*/ - result = (TLS_IO_INSTANCE*) malloc(sizeof(TLS_IO_INSTANCE)); - - if (result == NULL) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_012: [ If there is not enough memory to create the tlsio, the tlsio_openssl_create shall return NULL as the handle. ]*/ - LogError("Failed allocating TLSIO instance."); - } - else - { - memset(result, 0, sizeof(TLS_IO_INSTANCE)); - mallocAndStrcpy_s(&result->hostname, tls_io_config->hostname); - result->port = (int)tls_io_config->port; - result->ssl_context = NULL; - result->ssl = NULL; - result->certificate = NULL; - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_016: [ The tlsio_openssl_create shall initialize all callback pointers as NULL. ]*/ - result->on_bytes_received = NULL; - result->on_bytes_received_context = NULL; - - result->on_io_open_complete = NULL; - result->on_io_open_complete_context = NULL; - - result->on_io_close_complete = NULL; - result->on_io_close_complete_context = NULL; - - result->on_io_error = NULL; - result->on_io_error_context = NULL; - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_020: [ If tlsio_openssl_create get success to create the tlsio instance, it shall set the tlsio state as TLSIO_STATE_NOT_OPEN. ]*/ - result->tlsio_state = TLSIO_STATE_NOT_OPEN; - - result->x509certificate = NULL; - result->x509privatekey = NULL; - } - } - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_010: [ The tlsio_openssl_create shall return a non-NULL handle on success. ]*/ - return (CONCRETE_IO_HANDLE)result; -} - -/* Codes_SRS_TLSIO_SSL_ESP8266_99_021: [ The tlsio_openssl_destroy shall destroy a created instance of the tlsio for esp8266 identified by the CONCRETE_IO_HANDLE. ]*/ -void tlsio_openssl_destroy(CONCRETE_IO_HANDLE tls_io) -{ - TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_024: [ If the tlsio_handle is NULL, the tlsio_openssl_destroy shall not do anything. ]*/ - if (tls_io == NULL) - { - LogError("NULL tls_io."); - } - else - { - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_025: [ If the tlsio state is TLSIO_STATE_OPENING, TLSIO_STATE_OPEN, or TLSIO_STATE_CLOSING, the tlsio_openssl_destroy shall destroy the tlsio, but log an error. ]*/ - if ((tls_io_instance->tlsio_state == TLSIO_STATE_OPENING) || - (tls_io_instance->tlsio_state == TLSIO_STATE_OPEN) || - (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING)) - { - LogError("TLS destroyed with a SSL connection still active."); - } - /* Codes_SRS_TLSIO_SSL_ESP8266_99_022: [ The tlsio_openssl_destroy shall free all memory allocated for tlsio_instance. ]*/ - if (tls_io_instance->certificate != NULL) - { - free(tls_io_instance->certificate); - } - if (tls_io_instance->hostname != NULL) - { - free(tls_io_instance->hostname); - } - if (tls_io_instance->x509certificate != NULL) - { - free((void*)tls_io_instance->x509certificate); - } - if (tls_io_instance->x509privatekey != NULL) - { - free((void*)tls_io_instance->x509privatekey); - } - /* Codes_SRS_TLSIO_SSL_ESP8266_99_021: [ The tlsio_openssl_destroy shall destroy a created instance of the tlsio for esp8266 identified by the CONCRETE_IO_HANDLE. ]*/ - free(tls_io_instance); - } - //LogInfo("tlsio_openssl_destroy end: %d", system_get_free_heap_size()); -} - -/* Codes_SRS_TLSIO_SSL_ESP8266_99_026: [ The tlsio_openssl_open shall start the process to open the ssl connection with the host provided in the tlsio_openssl_create. ]*/ -int tlsio_openssl_open(CONCRETE_IO_HANDLE tls_io, ON_IO_OPEN_COMPLETE on_io_open_complete, void* on_io_open_complete_context, ON_BYTES_RECEIVED on_bytes_received, void* on_bytes_received_context, ON_IO_ERROR on_io_error, void* on_io_error_context) -{ - //LogInfo("tlsio_openssl_open begin: %d", system_get_free_heap_size()); - int result; - - if (tls_io == NULL) - { - /* Codes_TLSIO_SSL_ESP8266_99_036: [ If the tls_io handle is NULL, the tlsio_openssl_open shall not do anything, and return _LINE_. ]*/ - result = __LINE__; - LogError("NULL tls_io."); - } - else - { - - TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_035: [ If the tlsio state is not TLSIO_STATE_NOT_OPEN and not TLSIO_STATE_ERROR, then tlsio_openssl_open shall set the tlsio state as TLSIO_STATE_ERROR, and return _LINE_. ]*/ - if (tls_io_instance->tlsio_state != TLSIO_STATE_NOT_OPEN && tls_io_instance->tlsio_state != TLSIO_STATE_ERROR) - { - tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; - tls_io_instance->on_io_error = on_io_error; - tls_io_instance->on_io_error_context = on_io_error_context; - - result = __LINE__; - LogError("Invalid tlsio_state for open. Expected state is TLSIO_STATE_NOT_OPEN or TLSIO_STATE_ERROR."); - if (tls_io_instance->on_io_error != NULL) - { - tls_io_instance->on_io_error(tls_io_instance->on_io_error_context); - } - } - else - { - int netconn_retry = 0; - int ret; - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_004: [ The tlsio_ssl_esp8266 shall call the callbacks functions defined in the xio.h. ]*/ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_006: [ The tlsio_ssl_esp8266 shall return the status of all async operations using the callbacks. ]*/ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_007: [ If the callback function is set as NULL. The tlsio_ssl_esp8266 shall not call anything. ]*/ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ]*/ - tls_io_instance->on_io_open_complete = on_io_open_complete; - /* Codes_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ]*/ - tls_io_instance->on_io_open_complete_context = on_io_open_complete_context; - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_030: [ The tlsio_openssl_open shall store the provided on_bytes_received callback function address. ]*/ - tls_io_instance->on_bytes_received = on_bytes_received; - /* Codes_SRS_TLSIO_SSL_ESP8266_99_031: [ The tlsio_openssl_open shall store the provided on_bytes_received_context handle. ]*/ - tls_io_instance->on_bytes_received_context = on_bytes_received_context; - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ]*/ - tls_io_instance->on_io_error = on_io_error; - /* Codes_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ]*/ - tls_io_instance->on_io_error_context = on_io_error_context; - - tls_io_instance->tlsio_state = TLSIO_STATE_OPENING; - - do { - //LogInfo("size before netconn_gethostbyname: %d", system_get_free_heap_size()); - /* Codes_SRS_TLSIO_SSL_ESP8266_99_018: [ The tlsio_openssl_open shall convert the provide hostName to an IP address. ]*/ - ret = netconn_gethostbyname(tls_io_instance->hostname, &tls_io_instance->target_ip); - /* Codes_SRS_TLSIO_SSL_ESP8266_99_027: [ The tlsio_openssl_open shall set the tlsio to try to open the connection for MAX_RETRY times before assuming that connection failed. ]*/ - } while((ret != 0) && netconn_retry++ < MAX_RETRY); - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_019: [ If the WiFi cannot find the IP for the hostName, the tlsio_openssl_open shall return __LINE__. ]*/ - if (ret != 0 || openssl_thread_LWIP_CONNECTION(tls_io_instance) != 0){ - tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; - /* Codes_SRS_TLSIO_SSL_ESP8266_99_039: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_ERROR. ]*/ - indicate_open_complete(tls_io_instance, IO_OPEN_ERROR); - /* Codes_SRS_TLSIO_SSL_ESP8266_99_037: [ If the ssl client is not connected, the tlsio_openssl_open shall change the state to TLSIO_STATE_ERROR, log the error, and return _LINE_. ]*/ - result = __LINE__; - LogError("openssl_thread_LWIP_CONNECTION failed."); - if (tls_io_instance->on_io_error != NULL) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_040: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_error callback was provided, it shall call the on_io_error. ]*/ - tls_io_instance->on_io_error(tls_io_instance->on_io_error_context); - } - }else{ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_034: [ If tlsio_openssl_open get success to open the ssl connection, it shall set the tlsio state as TLSIO_STATE_OPEN, and return 0. ]*/ - tls_io_instance->tlsio_state = TLSIO_STATE_OPEN; - /* Codes_SRS_TLSIO_SSL_ESP8266_99_041: [ If the tlsio_openssl_open get success to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_OK. ]*/ - indicate_open_complete(tls_io_instance, IO_OPEN_OK); - result = 0; - os_delay_us(5000000); //delay added to give reconnect time to send last message - //LogInfo("tlsio_openssl_open end: %d", system_get_free_heap_size()); - } - } - } - return result; -} - -/* Codes_SRS_TLSIO_SSL_ESP8266_99_043: [ The tlsio_openssl_close shall start the process to close the ssl connection. ]*/ -int tlsio_openssl_close(CONCRETE_IO_HANDLE tls_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context) -{ - int result; - - //LogInfo("tlsio_openssl_close begin: %d", system_get_free_heap_size()); - LogInfo("tlsio_openssl_close"); - - if (tls_io == NULL) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_049: [ If the tlsio_handle is NULL, the tlsio_openssl_close shall not do anything, and return _LINE_. ]*/ - result = __LINE__; - LogError("NULL tls_io."); - } - else - { - TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; - /* Codes_SRS_TLSIO_SSL_ESP8266_99_048: [ If the tlsio state is TLSIO_STATE_NOT_OPEN, TLSIO_STATE_OPENING, or TLSIO_STATE_CLOSING, the tlsio_openssl_close shall set the tlsio state as TLSIO_STATE_ERROR, and return _LINE_. ]*/ - if ((tls_io_instance->tlsio_state == TLSIO_STATE_NOT_OPEN) || - (tls_io_instance->tlsio_state == TLSIO_STATE_CLOSING) || - (tls_io_instance->tlsio_state == TLSIO_STATE_OPENING)) - { - result = __LINE__; - tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; - LogError("Invalid tlsio_state for close. Expected state is TLSIO_STATE_OPEN or TLSIO_STATE_ERROR."); - } - else - { - int ret; - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_045: [ The tlsio_openssl_close shall store the provided on_io_close_complete callback function address. ]*/ - tls_io_instance->on_io_close_complete = on_io_close_complete; - /* Codes_SRS_TLSIO_SSL_ESP8266_99_046: [ The tlsio_openssl_close shall store the provided on_io_close_complete_context handle. ]*/ - tls_io_instance->on_io_close_complete_context = callback_context; - /* Codes_SRS_TLSIO_SSL_ESP8266_99_047: [ If tlsio_openssl_close get success to start the process to close the ssl connection, it shall set the tlsio state as TLSIO_STATE_CLOSING, and return 0. ]*/ - tls_io_instance->tlsio_state = TLSIO_STATE_CLOSING; - - ret = destroy_openssl_instance(tls_io_instance); - if (ret != 0) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_052: [ If tlsio_openssl_close fails to shutdown the ssl connection, it shall set the tlsio state as TLSIO_STATE_ERROR, and return _LINE_, and call on_io_error. ]*/ - result = __LINE__; - tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; - if (tls_io_instance->on_io_error != NULL) - { - tls_io_instance->on_io_error(tls_io_instance->on_io_error_context); - } - } - else - { - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_050: [ If tlsio_openssl_close successfully destroys the ssl connection, it shall set the tlsio state as TLSIO_STATE_NOT_OPEN, and return 0. ]*/ - tls_io_instance->tlsio_state = TLSIO_STATE_NOT_OPEN; - result = 0; - if (tls_io_instance->on_io_close_complete != NULL) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_051: [ If tlsio_openssl_close successfully destroys the ssl connection, it shall call on_io_close_complete. ]*/ - tls_io_instance->on_io_close_complete(tls_io_instance->on_io_close_complete_context); - } - } - } - } - //LogInfo("tlsio_openssl_close end: %d", system_get_free_heap_size()); - return result; -} -/* Codes_SRS_TLSIO_SSL_ESP8266_99_053: [ The tlsio_openssl_send shall send all bytes in a buffer to the ssl connection. ]*/ -int tlsio_openssl_send(CONCRETE_IO_HANDLE tls_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) -{ - int result; - - if ((tls_io == NULL) || (buffer == NULL) || (size == 0)) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_060: [ If the tls_io handle is NULL, the tlsio_openssl_send shall not do anything, and return _LINE_. ]*/ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_061: [ If the buffer is NULL, the tlsio_openssl_send shall not do anything, and return _LINE_. ]*/ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_062: [ If the size is 0, the tlsio_openssl_send shall not do anything, and return _LINE_. ]*/ - result = __LINE__; - LogError("Invalid parameter."); - } - else - { - TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; - - if (tls_io_instance->tlsio_state != TLSIO_STATE_OPEN) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_059: [ If the tlsio state is not TLSIO_STATE_OPEN, the tlsio_openssl_send shall return _LINE_. ]*/ - result = __LINE__; - LogError("TLS is not ready to send data. Expected state is TLSIO_STATE_OPEN."); - //LogInfo("TLS is not ready to send data. Expected state is TLSIO_STATE_OPEN."); - } - else - { - int total_write = 0; - int ret = 0; - int need_sent_bytes = (int)size; - - fd_set writeset; - fd_set errset; - /* Codes_SRS_TLSIO_SSL_ESP8266_99_056: [ The ssl will continue to send all data in the buffer until all bytes have been sent. ]*/ - while(need_sent_bytes > 0) - { - FD_ZERO(&writeset); - FD_SET(tls_io_instance->sock, &writeset); - FD_ZERO(&errset); - FD_SET(tls_io_instance->sock, &errset); - - ret = lwip_select(tls_io_instance->sock + 1, NULL, &writeset, &errset, &timeout); - if (ret == 0) { - result = __LINE__; - LogError("select timeout and no data to be write"); - break; - } else if (ret < 0 || FD_ISSET(tls_io_instance->sock, &errset)) { - result = __LINE__; - LogError("get error %d", lwip_net_errno(tls_io_instance->sock)); - break; - } - ret = SSL_write(tls_io_instance->ssl, ((uint8*)buffer)+total_write, (int)size); - // LogInfo("SSL_write ret: %d", ret); - //LogInfo("SSL_write res: %d, size: %d, retry: %d", res, size, retry); - if(ret > 0){ - total_write += ret; - need_sent_bytes = need_sent_bytes - ret; - } - else - { - result = __LINE__; - LogError("SSL_write failed."); - break; - } - } - - if (need_sent_bytes != 0) - { - LogInfo("ssl write failed, return [-0x%x]", -ret); - - ret = SSL_shutdown(tls_io_instance->ssl); - LogInfo("SSL_shutdown ret: %d", ret); - - /* Codes_SRS_TLSIO_SSL_ESP8266_99_054: [ The tlsio_openssl_send shall use the provided on_io_send_complete callback function address. ]*/ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_055: [ The tlsio_openssl_send shall use the provided on_io_send_complete_context handle. ]*/ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_057: [ If the ssl was not able to send all the bytes in the buffer, the tlsio_openssl_send shall call the on_send_complete with IO_SEND_ERROR, and return _LINE_. ]*/ - result = __LINE__; - if (on_send_complete != NULL) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_003: [ The tlsio_ssl_esp8266 shall report the send operation status using the IO_SEND_RESULT enumerator defined in the xio.h. ]*/ - on_send_complete(callback_context, IO_SEND_ERROR); - } - } - else - { - result = 0; - // LogInfo("SSL Write OK"); - /* Codes_SRS_TLSIO_SSL_ESP8266_99_058: [ If the ssl finish to send all bytes in the buffer, then tlsio_openssl_send shall call the on_send_complete with IO_SEND_OK, and return 0 ]*/ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_054: [ The tlsio_openssl_send shall use the provided on_io_send_complete callback function address. ]*/ - /* Codes_SRS_TLSIO_SSL_ESP8266_99_055: [ The tlsio_openssl_send shall use the provided on_io_send_complete_context handle. ]*/ - if (on_send_complete != NULL) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_003: [ The tlsio_ssl_esp8266 shall report the send operation status using the IO_SEND_RESULT enumerator defined in the xio.h. ]*/ - on_send_complete(callback_context, IO_SEND_OK); - } - } - - // LogInfo("total write: %d", total_write); - } - } - return result; -} - -/* Codes_SRS_TLSIO_SSL_ESP8266_99_063: [ The tlsio_openssl_dowork shall execute the async jobs for the tlsio. ]*/ -void tlsio_openssl_dowork(CONCRETE_IO_HANDLE tls_io) -{ - if (tls_io == NULL) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_074: [ If the tlsio handle is NULL, the tlsio_openssl_dowork shall not do anything. ]*/ - LogError("NULL tls_io."); - } - else - { - TLS_IO_INSTANCE* tls_io_instance = (TLS_IO_INSTANCE*)tls_io; - - if (tls_io_instance->tlsio_state == TLSIO_STATE_OPEN) - { - /* Codes_SRS_TLSIO_SSL_ESP8266_99_069: [ If the tlsio state is TLSIO_STATE_OPEN, the tlsio_openssl_dowork shall read data from the ssl client. ]*/ - int ret = decode_ssl_received_bytes(tls_io_instance); - if (ret != 0) - { - ret = SSL_shutdown(tls_io_instance->ssl); - //LogInfo("SSL_shutdown ret: %d", ret); - } - } - else - { - //LogError("Invalid tlsio_state for dowork. Expected state is TLSIO_STATE_OPEN."); - } - } - -} - -/* Codes_SRS_TLSIO_SSL_ESP8266_99_077: [ The tlsio_openssl_setoption shall not do anything, and return 0. ]*/ -int tlsio_openssl_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, const void* value) -{ - (void)tls_io, (void)optionName, (void)value; - - /* Not implementing any options */ - return 0; -} - -/* Codes_SRS_TLSIO_SSL_ESP8266_99_008: [ The tlsio_openssl_get_interface_description shall return the VTable IO_INTERFACE_DESCRIPTION. ]*/ -const IO_INTERFACE_DESCRIPTION* tlsio_openssl_get_interface_description(void) -{ - return &tlsio_openssl_interface_description; -} diff --git a/adapters/tlsio_wolfssl.c b/adapters/tlsio_wolfssl.c index c197d4db3..384f7fafe 100644 --- a/adapters/tlsio_wolfssl.c +++ b/adapters/tlsio_wolfssl.c @@ -51,6 +51,8 @@ typedef struct TLS_IO_INSTANCE_TAG char* x509certificate; char* x509privatekey; int wolfssl_device_id; + char* hostname; + bool ignore_host_name_check; } TLS_IO_INSTANCE; STATIC_VAR_UNUSED const char* const OPTION_WOLFSSL_SET_DEVICE_ID = "SetDeviceId"; @@ -288,7 +290,8 @@ static void on_underlying_io_open_complete(void* context, IO_OPEN_RESULT open_re res = wolfSSL_connect(tls_io_instance->ssl); if (res != SSL_SUCCESS) { - LogError("WolfSSL connect failed"); + // Error codes explained in https://www.wolfssl.com/docs/wolfssl-manual/appendix-c/ + LogError("WolfSSL connect failed (%d)", wolfSSL_get_error(tls_io_instance->ssl, res)); indicate_open_complete(tls_io_instance, IO_OPEN_ERROR); tls_io_instance->tlsio_state = TLSIO_STATE_ERROR; } @@ -565,10 +568,31 @@ static int create_wolfssl_instance(TLS_IO_INSTANCE* tls_io_instance) return result; } +static int enable_domain_check(TLS_IO_INSTANCE* tls_io_instance) +{ + int result = 0; + + if (!tls_io_instance->ignore_host_name_check) + { + if (wolfSSL_check_domain_name(tls_io_instance->ssl, tls_io_instance->hostname) != WOLFSSL_SUCCESS) + { + result = MU_FAILURE; + } + } + + return result; +} + static int prepare_wolfssl_open(TLS_IO_INSTANCE* tls_io_instance) { int result; - if (add_certificate_to_store(tls_io_instance) != 0) + + if (enable_domain_check(tls_io_instance)) + { + LogError("Failed to configure domain name verification"); + result = MU_FAILURE; + } + else if (add_certificate_to_store(tls_io_instance) != 0) { LogError("Failed to add certificates to store"); result = MU_FAILURE; @@ -638,6 +662,12 @@ CONCRETE_IO_HANDLE tlsio_wolfssl_create(void* io_create_parameters) free(result); result = NULL; } + else if (mallocAndStrcpy_s(&result->hostname, tls_io_config->hostname) != 0) + { + LogError("Failed copying the target hostname."); + free(result); + result = NULL; + } else { // Set the recv and send function on the wolfssl context object @@ -667,6 +697,7 @@ CONCRETE_IO_HANDLE tlsio_wolfssl_create(void* io_create_parameters) { LogError("Failed getting socket IO interface description."); wolfSSL_CTX_free(result->ssl_context); + free(result->hostname); free(result); result = NULL; } @@ -677,6 +708,7 @@ CONCRETE_IO_HANDLE tlsio_wolfssl_create(void* io_create_parameters) { LogError("Failure connecting to underlying socket_io"); wolfSSL_CTX_free(result->ssl_context); + free(result->hostname); free(result); result = NULL; } @@ -684,6 +716,7 @@ CONCRETE_IO_HANDLE tlsio_wolfssl_create(void* io_create_parameters) { LogError("Failure connecting to underlying socket_io"); wolfSSL_CTX_free(result->ssl_context); + free(result->hostname); free(result); result = NULL; } @@ -726,6 +759,7 @@ void tlsio_wolfssl_destroy(CONCRETE_IO_HANDLE tls_io) tls_io_instance->ssl_context = NULL; xio_destroy(tls_io_instance->socket_io); + free(tls_io_instance->hostname); free(tls_io); } } @@ -965,6 +999,12 @@ int tlsio_wolfssl_setoption(CONCRETE_IO_HANDLE tls_io, const char* optionName, c } } #endif + else if (strcmp("ignore_host_name_check", optionName) == 0) + { + bool* server_name_check = (bool*)value; + tls_io_instance->ignore_host_name_check = *server_name_check; + result = 0; + } else { if (tls_io_instance->socket_io == NULL) diff --git a/build/.vsts-ci.yml b/build/.vsts-ci.yml index 0f085ae85..170cc3ee5 100644 --- a/build/.vsts-ci.yml +++ b/build/.vsts-ci.yml @@ -60,50 +60,6 @@ jobs: displayName: 'build' - script: sudo rm -rf $(Agent.BuildDirectory)/* displayName: 'cleanup' -- job: gcc44 - variables: - _PREVIEW_VSTS_DOCKER_IMAGE: "aziotbld/linux-c-ubuntu-gcc4.4" - pool: aziotbld-lin01 - displayName: 'gcc4.4' - steps: - - script: | - if [ -f "jenkins/linux_c_gcc44.sh" ] - then - sudo ./jenkins/linux_c_gcc44.sh - fi - env: - IOTHUB_CONNECTION_STRING: $(IOTHUB-CONNECTION-STRING) - IOTHUB_EVENTHUB_CONNECTION_STRING: $(IOTHUB-EVENTHUB-CONNECTION-STRING) - IOTHUB_E2E_X509_CERT_BASE64: $(IOTHUB-E2E-X509-CERT-BASE64) - IOTHUB_E2E_X509_PRIVATE_KEY_BASE64: $(IOTHUB-E2E-X509-PRIVATE-KEY-BASE64) - IOTHUB_POLICY_KEY: $(IOTHUB-POLICY-KEY) - displayName: 'build' - - script: sudo rm -rf $(Agent.BuildDirectory)/* - displayName: 'cleanup' - condition: always() -- job: gcc46 - variables: - _PREVIEW_VSTS_DOCKER_IMAGE: "aziotbld/gcc-4.6:latest" - pool: aziotbld-lin01 - displayName: 'gcc-4.6' - steps: - - script: | - if [ -f "jenkins/linux_c_gcc44.sh" ] - then - sudo ./jenkins/linux_c_gcc44.sh - fi - env: - IOTHUB_CONNECTION_STRING: $(IOTHUB-CONNECTION-STRING) - IOTHUB_EVENTHUB_CONNECTION_STRING: $(IOTHUB-EVENTHUB-CONNECTION-STRING) - IOTHUB_E2E_X509_CERT_BASE64: $(IOTHUB-E2E-X509-CERT-BASE64) - IOTHUB_E2E_X509_PRIVATE_KEY_BASE64: $(IOTHUB-E2E-X509-PRIVATE-KEY-BASE64) - IOTHUB_POLICY_KEY: $(IOTHUB-POLICY-KEY) - IOT_DPS_CONNECTION_STRING: $(IOT-DPS-CONNECTION-STRING) - IOT_DPS_ID_SCOPE: $(IOT-DPS-ID-SCOPE) - displayName: 'build' - - script: sudo rm -rf $(Agent.BuildDirectory)/* - displayName: 'cleanup' - condition: always() - job: linuxoptions variables: _PREVIEW_VSTS_DOCKER_IMAGE: "aziotbld/vsts-linux-c-ubuntu" diff --git a/jenkins/linux_bearssl.sh b/jenkins/linux_bearssl.sh index 56a98b030..b060950ba 100644 --- a/jenkins/linux_bearssl.sh +++ b/jenkins/linux_bearssl.sh @@ -11,7 +11,7 @@ curl --version # Set the default cores CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu) -cmake . -Bcmake -Duse_bearssl:BOOL=ON -Drun_unittests:BOOL=ON +cmake . -Bcmake -Duse_bearssl:BOOL=ON -Drun_unittests:BOOL=ON -Duse_openssl:BOOL=OFF cd cmake make --jobs=$CORES diff --git a/jenkins/linux_wolfssl.sh b/jenkins/linux_wolfssl.sh index b0fc5a861..8512b6f8b 100755 --- a/jenkins/linux_wolfssl.sh +++ b/jenkins/linux_wolfssl.sh @@ -15,7 +15,7 @@ CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu) rm -r -f $build_folder mkdir -p $build_folder pushd $build_folder -cmake $build_root -Drun_unittests:BOOL=ON -Duse_wolfssl:BOOL=ON +cmake $build_root -Drun_unittests:BOOL=ON -Duse_wolfssl:BOOL=ON -Duse_openssl:BOOL=OFF make --jobs=$CORES #use doctored openssl diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3528ca7ec..ae2859c68 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -93,7 +93,6 @@ if(${run_unittests}) add_subdirectory(utf8_checker_ut) add_subdirectory(http_proxy_io_ut) if(NOT DEFINED MACOSX) - add_subdirectory(tlsio_esp8266_ut) #add_subdirectory(socket_async_ut) #add_subdirectory(dns_resolver_ut) endif() diff --git a/tests/tlsio_esp8266_ut/CMakeLists.txt b/tests/tlsio_esp8266_ut/CMakeLists.txt deleted file mode 100644 index eda6c5c65..000000000 --- a/tests/tlsio_esp8266_ut/CMakeLists.txt +++ /dev/null @@ -1,24 +0,0 @@ -#Copyright (c) Microsoft. All rights reserved. -#Licensed under the MIT license. See LICENSE file in the project root for full license information. - -cmake_minimum_required(VERSION 2.8.11) - -set(theseTestsName tlsio_esp8266_ut) - -include_directories(${SHARED_UTIL_REAL_TEST_FOLDER}) - -set(${theseTestsName}_test_files -${theseTestsName}.c -) - -set(${theseTestsName}_c_files -../../adapters/tlsio_ssl_esp8266.c -../real_test_files/real_crt_abstractions.c -) - -set(${theseTestsName}_h_files -) - -build_c_test_artifacts(${theseTestsName} ON "tests/azure_c_shared_utility_tests") - -compile_c_test_artifacts_as(${theseTestsName} C99) diff --git a/tests/tlsio_esp8266_ut/main.c b/tests/tlsio_esp8266_ut/main.c deleted file mode 100644 index bd4b99325..000000000 --- a/tests/tlsio_esp8266_ut/main.c +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#include -#include "testrunnerswitcher.h" - -int main(void) -{ - size_t failedTestCount = 0; - RUN_TEST_SUITE(tlsio_esp8266_ut, failedTestCount); - return failedTestCount; -} diff --git a/tests/tlsio_esp8266_ut/tlsio_esp8266_ut.c b/tests/tlsio_esp8266_ut/tlsio_esp8266_ut.c deleted file mode 100644 index 1afbccf49..000000000 --- a/tests/tlsio_esp8266_ut/tlsio_esp8266_ut.c +++ /dev/null @@ -1,1875 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#ifdef WIN32 -#define WIN32_LEAN_AND_MEAN -#endif - -#include -#include -#ifdef _CRTDBG_MAP_ALLOC -#include -#endif - -/** - * Include the C standards here. - */ -#include -#include - -/** - * Include the test tools. - */ -#include "azure_macro_utils/macro_utils.h" -#include "testrunnerswitcher.h" -#include "umock_c/umock_c.h" -#include "umock_c/umocktypes_charptr.h" -#include "umock_c/umock_c_negative_tests.h" - -/** - * Include the mockable headers here. - */ -#define ENABLE_MOCKS -#include "../../adapters/esp8266_mock.h" -#undef ENABLE_MOCKS - -/** - * Include the target header after the ENABLE_MOCKS session. - */ -#include "azure_c_shared_utility/xio.h" -#include "azure_c_shared_utility/tlsio.h" - -#define TEST_CREATE_CONNECTION_HOST_NAME (const char*)"https://test.azure-devices.net" -#define TEST_CREATE_CONNECTION_PORT (int)443 - -static const TLSIO_CONFIG tlsio_config = { TEST_CREATE_CONNECTION_HOST_NAME, TEST_CREATE_CONNECTION_PORT, NULL, NULL }; - -static int g_ssl_write_success = 1; -static int g_ssl_read_returns_data = 1; -static size_t g_on_bytes_received_buffer_size = 0; - -static int g_gethostbyname_success = 1; -static int g_socket_success = 1; -static int g_setsockopt_success = 1; -static int g_bind_success = 1; -static int g_getsockopt_success = 1; -static int g_connect_success = 1; -static int g_ssl_lwip_select_success = 1; -static int g_ssl_ctx_new_success = 1; -static int g_ssl_new_success = 1; -static int g_ssl_set_fd_success = 1; -static int g_ssl_connect_success = 1; -static int g_ssl_shutdown_success = 1; -static int g_ssl_TLSv1clientmethod_success = 1; -static int g_ssl_fd_isset = 0; -static int g_ssl_get_error_success = 1; -static int g_socket_close_success = 1; -static SSL_CTX* g_ctx = NULL; -static SSL* g_ssl = NULL; -static SSL_METHOD* g_sslmethod = NULL; -static void* g_mallocptr = NULL; -static char* g_destination = NULL; - -#define MAX_RETRY 20 -#define RECEIVE_BUFFER_SIZE 1024 - -void* my_gballoc_malloc(size_t size) -{ - g_mallocptr = malloc(size); - return g_mallocptr; -} - -void* my_gballoc_realloc(void* ptr, size_t size) -{ - return realloc(ptr, size); -} - -void my_gballoc_free(void* ptr) -{ - free(ptr); -} - -typedef enum TLSIO_STATE_TAG -{ - TLSIO_STATE_NOT_OPEN, - TLSIO_STATE_OPENING, - TLSIO_STATE_OPEN, - TLSIO_STATE_CLOSING, - TLSIO_STATE_ERROR -} TLSIO_STATE; - -typedef struct TLS_IO_INSTANCE_TAG -{ - ON_BYTES_RECEIVED on_bytes_received; - ON_IO_OPEN_COMPLETE on_io_open_complete; - ON_IO_CLOSE_COMPLETE on_io_close_complete; - ON_IO_ERROR on_io_error; - void* on_bytes_received_context; - void* on_io_open_complete_context; - void* on_io_close_complete_context; - void* on_io_error_context; - SSL* ssl; - SSL_CTX* ssl_context; - TLSIO_STATE tlsio_state; - char* hostname; - int port; - char* certificate; - const char* x509certificate; - const char* x509privatekey; - int sock; - ip_addr_t target_ip; -} TLS_IO_INSTANCE; - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - const IO_INTERFACE_DESCRIPTION* tlsio_openssl_get_interface_description(void); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -int my_mallocAndStrcpy_s(char** destination, const char* source){ - (void)(source); - *destination = (char*)malloc(1); - g_destination = *destination; - return 0; -} -int my_SSL_get_error(const SSL *ssl, int ret_code){ - (void)(ret_code), (void)(ssl); - if (g_ssl_get_error_success == 1){ - return SSL_ERROR_WANT_READ; - }else{ - return 0; - } - -} -int my_FD_ISSET(int n, void* p){ - (void)(n),(void)(p); - if(g_ssl_fd_isset == 0){ - g_ssl_fd_isset++; - return 1; - }else{ - return 0; - } -} - -int my_lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, - struct timeval *timeout){ - (void)(maxfdp1),(void)(readset),(void)(writeset),(void)(exceptset),(void)(timeout); - - if(g_ssl_lwip_select_success == 1) - { - return 1; - }else{ - return 0; - } -} - -int my_SSL_write(SSL *ssl, const void *buffer, int len){ - (void)(ssl),(void)(buffer); - - if (g_ssl_write_success){ - return len; - }else{ - return 0; - } -} - -void my_SSL_CTX_free(SSL_CTX *ctx){ - (void)(ctx); -} - -void my_SSL_free(SSL *ssl){ - (void)(ssl); -} - -int my_SSL_read(SSL *ssl, void *buffer, int len){ - (void)(ssl),(void)(buffer); - if (g_ssl_read_returns_data){ - return len; - }else{ - return 0; - } -} - -int my_SSL_connect(SSL *ssl){ - (void)(ssl); - if (g_ssl_connect_success == 1){ - return 1; - }else{ - return -1; - } -} - -int my_SSL_shutdown(SSL *ssl){ - (void)(ssl); - if (g_ssl_shutdown_success == 1){ - return 0; - }else{ - return -1; - } -} - -int my_socket(int domain, int type, int protocol){ - (void)(domain),(void)(type), (void)(protocol); - if (g_socket_success == 1){ - return 0; - }else{ - return -1; - } -} - -int my_SSL_set_fd(SSL *ssl, int fd){ - (void)(ssl),(void)(fd); - if (g_ssl_set_fd_success == 1){ - return 1; - }else{ - return 0; - } -} -//NOTE: malloc(1) is used here since SSL is defined as void in esp8266_mock.h -SSL* my_SSL_new(SSL_CTX *ssl_ctx){ - (void)(ssl_ctx); - if (g_ssl_new_success == 1){ - g_ssl = (SSL*)malloc(1); - return g_ssl; - }else{ - return NULL; - } -} - -//NOTE: malloc(1) is used here since SSL_CTX is defined as void in esp8266_mock.h -SSL_CTX* my_SSL_CTX_new(SSL_METHOD *method){ - (void)(method); - if(g_ssl_ctx_new_success == 1) - { - if(method != NULL){ - g_ctx = (SSL_CTX*)malloc(1); - return g_ctx; - } - else - { - return NULL; - } - }else{ - return NULL; - } -} - -void my_SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len) -{ - (void)(ctx),(void)(len); - -} - -int my_fcntl(int s, int cmd, int val) -{ - (void)(s); - (void)(cmd); - (void)(val); - return 0; -} -err_t my_netconn_gethostbyname(const char *name, ip_addr_t *target_ip){ - (void)(name),(void)(target_ip); - if (g_gethostbyname_success == 1){ - return 0; - }else{ - return -1; - } -} - -//NOTE: malloc(1) is used here since SSL_METHOD is defined as void in esp8266_mock.h -SSL_METHOD* my_TLSv1_client_method(void){ - if (g_ssl_TLSv1clientmethod_success == 1){ - g_sslmethod = (SSL_METHOD*)malloc(1); - return g_sslmethod; - }else{ - return NULL; - } -} - -int my_bind(int s, const struct sockaddr* name, socklen_t namelen){ - (void)(s),(void)(name),(void)(namelen); - if (g_bind_success == 1){ - return 0; - }else{ - return -1; - } -} - -int my_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen){ - (void)(s),(void)(level),(void)(optname),(void)(optval),(void)(optlen); - if (g_setsockopt_success == 1){ - return 0; - }else{ - return -1; - } -} - -int my_close(int s){ - (void)(s); - if (g_socket_close_success == 1){ - return 0; - } else{ - return 1; - } -} - -int my_connect(int s, const struct sockaddr *name, socklen_t namelen){ - (void)(s),(void)(name), (void)(namelen); - if (g_connect_success == 1){ - return 0; - }else{ - return -1; - } -} -#undef EINPROGRESS -#define EINPROGRESS 115 /* Operation now in progress */ -int my_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen){ - (void)(s), (void)(level), (void)(optname), (void)(optval), (void)(optlen); - if (g_getsockopt_success == 1){ - return EINPROGRESS; - }else{ - return -1; - } -} - -void my_os_delay_us(int us){ - (void)(us); -} - -static void on_bytes_received(void* context, const unsigned char* buffer, size_t size) -{ - (void)(context), (void)(buffer), (void)(size); - g_on_bytes_received_buffer_size = size; -} - -#define ENABLE_MOCKS -#include "azure_c_shared_utility/gballoc.h" -#include "azure_c_shared_utility/crt_abstractions.h" -#include "azure_c_shared_utility/optionhandler.h" - -IMPLEMENT_UMOCK_C_ENUM_TYPE(IO_OPEN_RESULT, IO_OPEN_RESULT_VALUES); -IMPLEMENT_UMOCK_C_ENUM_TYPE(IO_SEND_RESULT, IO_SEND_RESULT_VALUES); - - -MOCK_FUNCTION_WITH_CODE(, void, test_on_io_error, void*, context) -MOCK_FUNCTION_END(); -MOCK_FUNCTION_WITH_CODE(, void, test_on_bytes_received, void*, context, const unsigned char*, buffer, size_t, size) -MOCK_FUNCTION_END(); -MOCK_FUNCTION_WITH_CODE(, void, test_on_io_open_complete, void*, context, IO_OPEN_RESULT, open_result) -MOCK_FUNCTION_END(); -MOCK_FUNCTION_WITH_CODE(, void, test_on_io_close_complete, void*, context) -MOCK_FUNCTION_END(); -MOCK_FUNCTION_WITH_CODE(, void, test_on_send_complete, void*, context, IO_SEND_RESULT, send_result) -MOCK_FUNCTION_END(); -#undef ENABLE_MOCKS - - /** - * You can create some global variables that your test will need in some way. - */ -static void* g_GenericPointer; - -/** - * Umock error will helps you to identify errors in the test suite or in the way that you are - * using it, just keep it as is. - */ -MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES) - -static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code) -{ - ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code)); -} - -/** - * This is necessary for the test suite, just keep as is. - */ -static TEST_MUTEX_HANDLE g_testByTest; - -/** - * Tests begin here. - * - * RUN_TEST_SUITE(tlsio_esp8266_ut, failedTestCount); - * - */ -BEGIN_TEST_SUITE(tlsio_esp8266_ut) - - /** - * This is the place where we initialize the test system. Replace the test name to associate the test - * suite with your test cases. - * It is called once, before start the tests. - */ - TEST_SUITE_INITIALIZE(a) - { - int result; - g_testByTest = TEST_MUTEX_CREATE(); - ASSERT_IS_NOT_NULL(g_testByTest); - - (void)umock_c_init(on_umock_c_error); - - result = umocktypes_charptr_register_types(); - ASSERT_ARE_EQUAL(int, 0, result); - - /** - * It is necessary to identify the types defined on your target. With it, the test system will - * know how to use it. - * - * On the target.h example, there is the type TARGET_HANDLE that is a void* - */ - //REGISTER_UMOCK_ALIAS_TYPE(CALLEE_HANDLE, void*); - - - /** - * Or you can combine, for example, in the success case malloc will call my_gballoc_malloc, and for - * the failed cases, it will return NULL. - */ - REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc); - REGISTER_GLOBAL_MOCK_FAIL_RETURN(gballoc_malloc, NULL); - REGISTER_GLOBAL_MOCK_HOOK(gballoc_realloc, my_gballoc_realloc); - REGISTER_GLOBAL_MOCK_FAIL_RETURN(gballoc_realloc, NULL); - REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free); - - - //REGISTER_GLOBAL_MOCK_HOOK(OptionHandler_Create, my_OptionHandler_Create); - REGISTER_GLOBAL_MOCK_HOOK(mallocAndStrcpy_s, my_mallocAndStrcpy_s); - REGISTER_GLOBAL_MOCK_HOOK(lwip_select, my_lwip_select); - REGISTER_GLOBAL_MOCK_HOOK(SSL_write, my_SSL_write); - REGISTER_GLOBAL_MOCK_HOOK(SSL_CTX_free, my_SSL_CTX_free); - REGISTER_GLOBAL_MOCK_HOOK(SSL_free, my_SSL_free); - REGISTER_GLOBAL_MOCK_HOOK(SSL_read, my_SSL_read); - REGISTER_GLOBAL_MOCK_HOOK(SSL_connect, my_SSL_connect); - REGISTER_GLOBAL_MOCK_HOOK(SSL_shutdown, my_SSL_shutdown); - REGISTER_GLOBAL_MOCK_HOOK(SSL_set_fd, my_SSL_set_fd); - REGISTER_GLOBAL_MOCK_HOOK(SSL_new, my_SSL_new); - REGISTER_GLOBAL_MOCK_HOOK(SSL_CTX_new, my_SSL_CTX_new); - REGISTER_GLOBAL_MOCK_HOOK(netconn_gethostbyname, my_netconn_gethostbyname); - REGISTER_GLOBAL_MOCK_HOOK(TLSv1_client_method, my_TLSv1_client_method); - REGISTER_GLOBAL_MOCK_HOOK(bind, my_bind); - REGISTER_GLOBAL_MOCK_HOOK(getsockopt, my_getsockopt); - REGISTER_GLOBAL_MOCK_HOOK(socket, my_socket); - REGISTER_GLOBAL_MOCK_HOOK(SSL_CTX_set_default_read_buffer_len, my_SSL_CTX_set_default_read_buffer_len); - REGISTER_GLOBAL_MOCK_HOOK(setsockopt, my_setsockopt); - REGISTER_GLOBAL_MOCK_HOOK(close, my_close); - REGISTER_GLOBAL_MOCK_HOOK(connect, my_connect); - REGISTER_GLOBAL_MOCK_HOOK(FD_ISSET, my_FD_ISSET); - REGISTER_GLOBAL_MOCK_HOOK(SSL_get_error, my_SSL_get_error); - REGISTER_GLOBAL_MOCK_HOOK(fcntl, my_fcntl); - REGISTER_TYPE(IO_SEND_RESULT, IO_SEND_RESULT); - REGISTER_TYPE(IO_OPEN_RESULT, IO_OPEN_RESULT); - - /** - * You can initialize other global variables here, for instance image that you have a standard void* that will be converted - * any pointer that your test needs. - */ - g_GenericPointer = malloc(1); - ASSERT_IS_NOT_NULL(g_GenericPointer); - } - - /** - * The test suite will call this function to cleanup your machine. - * It is called only once, after all tests is done. - */ - TEST_SUITE_CLEANUP(TestClassCleanup) - { - free(g_GenericPointer); - - umock_c_deinit(); - - TEST_MUTEX_DESTROY(g_testByTest); - } - - /** - * The test suite will call this function to prepare the machine for the new test. - * It is called before execute each test. - */ - TEST_FUNCTION_INITIALIZE(initialize) - { - if (TEST_MUTEX_ACQUIRE(g_testByTest)) - { - ASSERT_FAIL("Could not acquire test serialization mutex."); - } - - g_ssl_fd_isset = 1; - - umock_c_reset_all_calls(); - } - - /** - * The test suite will call this function to cleanup your machine for the next test. - * It is called after execute each test. - */ - TEST_FUNCTION_CLEANUP(cleans) - { - TEST_MUTEX_RELEASE(g_testByTest); - } - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_063: [ The tlsio_openssl_dowork shall execute the async jobs for the tlsio. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_069: [ If the tlsio state is TLSIO_STATE_OPEN, the tlsio_openssl_dowork shall read data from the ssl client. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_070: [ If there are received data in the ssl client, the tlsio_openssl_dowork shall read this data and call the on_bytes_received with the pointer to the buffer with the data. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_075: [ The tlsio_openssl_dowork shall create a buffer to store the data received from the ssl client. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_076: [ The tlsio_openssl_dowork shall delete the buffer to store the data received from the ssl client. ] */ - TEST_FUNCTION(tlsio_openssl_dowork_withdata__succeed) - { - ///arrange - TLS_IO_INSTANCE instance; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - g_ssl_read_returns_data = 1; - memset(&instance, 0, sizeof(TLS_IO_INSTANCE)); - instance.on_bytes_received = on_bytes_received; - instance.tlsio_state = TLSIO_STATE_OPEN; - - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - umock_c_reset_all_calls(); - EXPECTED_CALL(SSL_read(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG)); - - ///act - tlsioInterfaces->concrete_io_dowork(&instance); - - ///assert - ASSERT_ARE_EQUAL(size_t, g_on_bytes_received_buffer_size, RECEIVE_BUFFER_SIZE); - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ///cleanup - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_071: [** If there are no received data in the ssl client, the tlsio_openssl_dowork shall do nothing. ] */ - TEST_FUNCTION(tlsio_openssl_dowork_withoutdata__succeed) - { - ///arrange - TLS_IO_INSTANCE instance; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - g_ssl_read_returns_data = 0; - memset(&instance, 0, sizeof(TLS_IO_INSTANCE)); - instance.on_bytes_received = on_bytes_received; - instance.tlsio_state = TLSIO_STATE_OPEN; - - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - umock_c_reset_all_calls(); - EXPECTED_CALL(SSL_read(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG)); - - ///act - tlsioInterfaces->concrete_io_dowork(&instance); - - ///assert - ASSERT_ARE_EQUAL(size_t, g_on_bytes_received_buffer_size, 0); - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ///cleanup - } - - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_074: [ If the tlsio handle is NULL, the tlsio_openssl_dowork shall not do anything. ] */ - TEST_FUNCTION(tlsio_openssl_dowork__failed) - { - ///arrange - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - ///act - tlsioInterfaces->concrete_io_dowork(NULL); - - ///assert - ///cleanup - } - - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_053: [ The tlsio_openssl_send shall send all bytes in a buffer to the ssl connection. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_054: [ The tlsio_openssl_send shall use the provided on_io_send_complete callback function address. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_055: [ The tlsio_openssl_send shall use the provided on_io_send_complete_context handle. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_056: [ The ssl will continue to send all data in the buffer until all bytes have been sent. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_058: [ If the ssl finish to send all bytes in the buffer, then tlsio_openssl_send shall call the on_send_complete with IO_SEND_OK, and return 0 ] */ - TEST_FUNCTION(tlsio_openssl_send__SSL_write__succeed) - { - ///arrange - int result = 0; - TLS_IO_INSTANCE instance; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - unsigned char test_buffer[] = { 0x42, 0x43 }; - memset(&instance, 0, sizeof(TLS_IO_INSTANCE)); - instance.tlsio_state = TLSIO_STATE_OPEN; - g_ssl_write_success = 1; - - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - umock_c_reset_all_calls(); - STRICT_EXPECTED_CALL(lwip_select(IGNORED_NUM_ARG, IGNORED_PTR_ARG, - IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - EXPECTED_CALL(SSL_write(IGNORED_PTR_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)); - STRICT_EXPECTED_CALL(test_on_send_complete((void*)0x4242, IO_SEND_OK)); - - ///act - result = tlsioInterfaces->concrete_io_send(&instance, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_EQUAL(int, result, 0); - ///cleanup - } - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_060: [ If the tls_io handle is NULL, the tlsio_openssl_send shall not do anything, and return _LINE_. ] */ - TEST_FUNCTION(tlsio_openssl_send_null_handle__failed) - { - ///arrange - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - ///act - result = tlsioInterfaces->concrete_io_send(NULL, NULL, 0, NULL, NULL); - - ///assert - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - } - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_061: [ If the buffer is NULL, the tlsio_openssl_send shall not do anything, and return _LINE_. ] */ - TEST_FUNCTION(tlsio_openssl_send__SSL_write_null_buffer__failed) - { - ///arrange - int result = 0; - TLS_IO_INSTANCE instance; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - memset(&instance, 0, sizeof(TLS_IO_INSTANCE)); - instance.tlsio_state = TLSIO_STATE_OPEN; - g_ssl_write_success = 1; - - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - ///act - result = tlsioInterfaces->concrete_io_send(&instance, NULL, 0, test_on_send_complete, (void*)0x4242); - - ///assert - ASSERT_ARE_NOT_EQUAL(int, result, 0); - ///cleanup - } - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_062: [ If the size is 0, the tlsio_openssl_send shall not do anything, and return _LINE_. ] */ - TEST_FUNCTION(tlsio_openssl_send__SSL_write_size_zero__failed) - { - ///arrange - int result = 0; - TLS_IO_INSTANCE instance; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - unsigned char test_buffer[] = {0x42}; - memset(&instance, 0, sizeof(TLS_IO_INSTANCE)); - instance.tlsio_state = TLSIO_STATE_OPEN; - g_ssl_write_success = 1; - - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - ///act - result = tlsioInterfaces->concrete_io_send(&instance, test_buffer, 0, test_on_send_complete, (void*)0x4242); - - ///assert - ASSERT_ARE_NOT_EQUAL(int, result, 0); - ///cleanup - } - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_059: [ If the tlsio state is not TLSIO_STATE_OPEN, the tlsio_openssl_send shall return _LINE_. ] */ - TEST_FUNCTION(tlsio_openssl_send_wrong_state__failed) - { - ///arrange - int result; - TLS_IO_INSTANCE instance; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - memset(&instance, 0, sizeof(TLS_IO_INSTANCE)); - instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - ///act - result = tlsioInterfaces->concrete_io_send(&instance, NULL, 0, NULL, NULL); - - ///assert - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - } - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_057: [ If the ssl was not able to send all the bytes in the buffer, the tlsio_openssl_send shall call the on_send_complete with IO_SEND_ERROR, and return _LINE_. ] */ - TEST_FUNCTION(tlsio_openssl_send__SSL_write__failed) - { - ///arrange - int result = 0; - TLS_IO_INSTANCE instance; - unsigned char test_buffer[] = { 0x42, 0x43 }; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - memset(&instance, 0, sizeof(TLS_IO_INSTANCE)); - instance.tlsio_state = TLSIO_STATE_OPEN; - g_ssl_write_success = 0; - - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - umock_c_reset_all_calls(); - STRICT_EXPECTED_CALL(lwip_select(IGNORED_NUM_ARG, IGNORED_PTR_ARG, - IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - EXPECTED_CALL(SSL_write(IGNORED_PTR_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)); - EXPECTED_CALL(SSL_shutdown(IGNORED_PTR_ARG)); - - STRICT_EXPECTED_CALL(test_on_send_complete((void*)0x4242, IO_SEND_ERROR)); - - ///act - result = tlsioInterfaces->concrete_io_send(&instance, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, result, 0); - ///cleanup - } - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_043: [ The tlsio_openssl_close shall start the process to close the ssl connection. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_045: [ The tlsio_openssl_close shall store the provided on_io_close_complete callback function address. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_046: [ The tlsio_openssl_close shall store the provided on_io_close_complete_context handle. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_047: [ If tlsio_openssl_close get success to start the process to close the ssl connection, it shall set the tlsio state as TLSIO_STATE_CLOSING, and return 0. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_050: [ If tlsio_openssl_close successfully destroys the ssl connection, it shall set the tlsio state as TLSIO_STATE_NOT_OPEN, and return 0. ] */ - /* Tests_SRS_TLSIO_SSL_ESP8266_99_051: [ If tlsio_openssl_close successfully destroys the ssl connection, it shall call on_io_close_complete. ] */ - TEST_FUNCTION(tlsio_openssl_close__succeed) - { - ///arrange - int result; - TLS_IO_INSTANCE instance; - SSL_CTX *ctx; - SSL *ssl; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - memset(&instance, 0, sizeof(TLS_IO_INSTANCE)); - g_ssl_TLSv1clientmethod_success = 0; - ctx = SSL_CTX_new(TLSv1_client_method()); - ASSERT_IS_NULL(ctx); - g_ssl_TLSv1clientmethod_success = 1; - ctx = SSL_CTX_new(TLSv1_client_method()); - ASSERT_IS_NOT_NULL(ctx); - ssl = SSL_new(ctx); - g_ssl_shutdown_success = 1; - g_socket_close_success = 1; - - instance.tlsio_state = TLSIO_STATE_OPEN; - instance.ssl = ssl; - instance.ssl_context = ctx; - - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - umock_c_reset_all_calls(); - STRICT_EXPECTED_CALL(SSL_free(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(close(IGNORED_NUM_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_CTX_free(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(test_on_io_close_complete((void*)0x4242)); - ///act - result = tlsioInterfaces->concrete_io_close(&instance, test_on_io_close_complete, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_EQUAL(int, 0, result); - ASSERT_ARE_EQUAL(int, (int)TLSIO_STATE_NOT_OPEN, instance.tlsio_state); - ///cleanup - if(g_ctx != NULL){ - free(g_ctx); - } - if(g_ssl != NULL){ - free(g_ssl); - } - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - } - - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_048: [ If the tlsio state is TLSIO_STATE_NOT_OPEN, TLSIO_STATE_OPENING, or TLSIO_STATE_CLOSING, the tlsio_openssl_close shall set the tlsio state as TLSIO_STATE_ERROR, and return _LINE_. ] */ - TEST_FUNCTION(tlsio_openssl_close_wrong_state__failed) - { - ///arrange - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - TLS_IO_INSTANCE instance; - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - memset(&instance, 0, sizeof(TLS_IO_INSTANCE)); - instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - ///act - result = tlsioInterfaces->concrete_io_close(&instance, test_on_io_close_complete, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - instance.tlsio_state = TLSIO_STATE_CLOSING; - ///act - result = tlsioInterfaces->concrete_io_close(&instance, NULL, NULL); - - ///assert - ASSERT_ARE_NOT_EQUAL(int, 0, result); - ASSERT_ARE_EQUAL(int, (int)TLSIO_STATE_ERROR, instance.tlsio_state); - ///cleanup - } - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_049: [ If the tlsio_handle is NULL, the tlsio_openssl_close shall not do anything, and return _LINE_. ] */ - TEST_FUNCTION(tlsio_openssl_close_null_handle__failed) - { - ///arrange - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - ///act - result = tlsioInterfaces->concrete_io_close(NULL, test_on_io_close_complete, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - ///cleanup - } - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_052: [ If tlsio_openssl_close fails to shutdown the ssl connection, it shall set the tlsio state as TLSIO_STATE_ERROR, and return _LINE_, and call on_io_error. ] */ - TEST_FUNCTION(tlsio_openssl_close_shutdown__failed) - { - ///arrange - int result; - TLS_IO_INSTANCE instance; - SSL_CTX *ctx; - SSL *ssl; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - memset(&instance, 0, sizeof(TLS_IO_INSTANCE)); - g_ssl_TLSv1clientmethod_success = 0; - ctx = SSL_CTX_new(TLSv1_client_method()); - ASSERT_IS_NULL(ctx); - g_ssl_TLSv1clientmethod_success = 1; - ctx = SSL_CTX_new(TLSv1_client_method()); - ASSERT_IS_NOT_NULL(ctx); - ssl = SSL_new(ctx); - g_socket_close_success = 0; - - instance.tlsio_state = TLSIO_STATE_OPEN; - instance.ssl = ssl; - instance.ssl_context = ctx; - instance.on_io_error = test_on_io_error; - - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - umock_c_reset_all_calls(); - STRICT_EXPECTED_CALL(SSL_free(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(close(IGNORED_NUM_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_CTX_free(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(test_on_io_error(NULL)); - ///act - result = tlsioInterfaces->concrete_io_close(&instance, test_on_io_close_complete, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - ASSERT_ARE_EQUAL(int, (int)TLSIO_STATE_ERROR, instance.tlsio_state); - ///cleanup - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - if(g_ctx != NULL){ - free(g_ctx); - } - if(g_ssl != NULL){ - free(g_ssl); - } - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_021: [ The tlsio_openssl_destroy shall destroy a created instance of the tlsio for ESP8266 identified by the CONCRETE_IO_HANDLE. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_022: [ The tlsio_openssl_destroy shall free all memory allocated for tlsio_instance. ] */ - TEST_FUNCTION(tlsio_openssl_destroy__succeed) - { - ///arrange - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - TLS_IO_INSTANCE* instance = (TLS_IO_INSTANCE*)malloc(sizeof(TLS_IO_INSTANCE)); - memset(instance, 0, sizeof(TLS_IO_INSTANCE)); - instance->tlsio_state = TLSIO_STATE_NOT_OPEN; - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - umock_c_reset_all_calls(); - STRICT_EXPECTED_CALL(free(instance)); - - ///act - tlsioInterfaces->concrete_io_destroy(instance); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ///cleanup - } - - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_024: [ If the tlsio_handle is NULL, the tlsio_openssl_destroy shall not do anything. ] */ - TEST_FUNCTION(tlsio_openssl_destroy_NULL_handle__failed) - { - ///arrange - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - ///act - tlsioInterfaces->concrete_io_destroy(NULL); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ///cleanup - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_025: [ If the tlsio state is TLSIO_STATE_OPENING, TLSIO_STATE_OPEN, or TLSIO_STATE_CLOSING, the tlsio_openssl_destroy shall destroy the tlsio, but log an error. ] */ - TEST_FUNCTION(tlsio_openssl_destroy_wrong_state__failed) - { - ///arrange - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - TLS_IO_INSTANCE* instance = (TLS_IO_INSTANCE*)malloc(sizeof(TLS_IO_INSTANCE)); - memset(instance, 0, sizeof(TLS_IO_INSTANCE)); - instance->tlsio_state = TLSIO_STATE_OPENING; - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - umock_c_reset_all_calls(); - STRICT_EXPECTED_CALL(free(instance)); - - ///act - tlsioInterfaces->concrete_io_destroy(instance); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ///cleanup - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_018: [ The tlsio_openssl_open shall convert the provide hostName to an IP address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_026: [ The tlsio_openssl_open shall start the process to open the ssl connection with the host provided in the tlsio_openssl_create. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_030: [ The tlsio_openssl_open shall store the provided on_bytes_received callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_031: [ The tlsio_openssl_open shall store the provided on_bytes_received_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_034: [ If tlsio_openssl_open get success to open the ssl connection, it shall set the tlsio state as TLSIO_STATE_OPEN, and return 0. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_041: [ If the tlsio_openssl_open get success to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_OK. ] */ - TEST_FUNCTION(tlsio_openssl_open__succeed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - - tls_io_instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - - g_gethostbyname_success = 1; - g_socket_success = 1; - g_setsockopt_success = 1; - g_bind_success = 1; - g_connect_success = 1; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 1; - - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - g_ssl_fd_isset = 0; - g_ssl_get_error_success = 1; - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(netconn_gethostbyname(IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(TLSv1_client_method()); - STRICT_EXPECTED_CALL(SSL_CTX_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_CTX_set_default_read_buffer_len(IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(socket(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(bind(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(connect(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(lwip_select(IGNORED_NUM_ARG, IGNORED_PTR_ARG, - IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(SSL_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_set_fd(IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - - STRICT_EXPECTED_CALL(lwip_select(IGNORED_NUM_ARG, IGNORED_PTR_ARG, - IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(SSL_connect(IGNORED_PTR_ARG)).IgnoreArgument(1); - - STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_OK)); - STRICT_EXPECTED_CALL(os_delay_us(IGNORED_NUM_ARG)).IgnoreArgument(1); - - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(int, 0, result); - /** - * The follow assert will compare the expected calls with the actual calls. If it is different, - * it will show the serialized strings with the differences in the log. - */ - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_EQUAL(int, (int)TLSIO_STATE_OPEN, tls_io_instance.tlsio_state); - - ///cleanup - if(g_ctx != NULL){ - free(g_ctx); - } - if(g_ssl != NULL){ - free(g_ssl); - } - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - - } - - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_035: [ If the tlsio state is not TLSIO_STATE_NOT_OPEN and not TLSIO_STATE_ERROR, then tlsio_openssl_open shall set the tlsio state as TLSIO_STATE_ERROR, and return _LINE_. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ] */ - TEST_FUNCTION(tlsio_openssl_open_invalid_state__failed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - g_gethostbyname_success = 1; - g_socket_success = 1; - g_setsockopt_success = 1; - g_bind_success = 1; - g_connect_success = 1; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 1; - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - tls_io_instance.tlsio_state = TLSIO_STATE_OPENING; - - STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4242)); - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - ASSERT_ARE_EQUAL(int, (int)TLSIO_STATE_ERROR, tls_io_instance.tlsio_state); - ///cleanup - } - - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_036: [ If the tls_io handle is NULL, the tlsio_openssl_open shall not do anything, and return _LINE_. ] */ - TEST_FUNCTION(tlsio_openssl_open_NULL_handle__failed) - { - ///arrange - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - g_gethostbyname_success = 1; - g_socket_success = 1; - g_setsockopt_success = 1; - g_bind_success = 1; - g_connect_success = 1; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 1; - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - ///act - result = tlsioInterfaces->concrete_io_open(NULL, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_019: [ If the WiFi cannot find the IP for the hostName, the tlsio_openssl_open shall return __LINE__. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_027: [ The tlsio_openssl_open shall set the tlsio to try to open the connection for MAX_RETRY times before assuming that connection failed. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_037: [ If the ssl client is not connected, the tlsio_openssl_open shall change the state to TLSIO_STATE_ERROR, log the error, and return _LINE_. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_039: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_ERROR. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_040: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_error callback was provided, it shall call the on_io_error. ] */ - TEST_FUNCTION(tlsio_openssl_open_gethostbyname__failed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - int retry = 0; - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - tls_io_instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - g_gethostbyname_success = 0; - g_socket_success = 1; - g_setsockopt_success = 1; - g_bind_success = 1; - g_connect_success = 1; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 1; - - umock_c_reset_all_calls(); - - do{ - STRICT_EXPECTED_CALL(netconn_gethostbyname(IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - }while(retry++ < MAX_RETRY); - - STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR)); - STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4242)); - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_080: [ If socket failed, the tlsio_openssl_open shall return __LINE__. ]*/ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_037: [ If the ssl client is not connected, the tlsio_openssl_open shall change the state to TLSIO_STATE_ERROR, log the error, and return _LINE_. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_039: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_ERROR. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_040: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_error callback was provided, it shall call the on_io_error. ] */ - TEST_FUNCTION(tlsio_openssl_open_socket__failed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - tls_io_instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - ASSERT_IS_NOT_NULL(tlsioInterfaces); - g_gethostbyname_success = 1; - g_socket_success = 0; - g_setsockopt_success = 1; - g_bind_success = 1; - g_connect_success = 1; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 1; - g_ssl_fd_isset = 0; - g_ssl_get_error_success = 1; - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(netconn_gethostbyname(IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(TLSv1_client_method()); - STRICT_EXPECTED_CALL(SSL_CTX_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_CTX_set_default_read_buffer_len(IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(socket(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR)); - STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4242)); - - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - if(g_ctx != NULL){ - free(g_ctx); - } - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_081: [ If setsockopt failed, the tlsio_openssl_open shall return __LINE__. ]*/ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_037: [ If the ssl client is not connected, the tlsio_openssl_open shall change the state to TLSIO_STATE_ERROR, log the error, and return _LINE_. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_039: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_ERROR. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_040: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_error callback was provided, it shall call the on_io_error. ] */ - TEST_FUNCTION(tlsio_openssl_open_setsockopt__failed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - tls_io_instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - ASSERT_IS_NOT_NULL(tlsioInterfaces); - g_gethostbyname_success = 1; - g_socket_success = 1; - g_setsockopt_success = 0; - g_bind_success = 1; - g_connect_success = 1; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 1; - g_ssl_fd_isset = 0; - g_ssl_get_error_success = 1; - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(netconn_gethostbyname(IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(TLSv1_client_method()); - STRICT_EXPECTED_CALL(SSL_CTX_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_CTX_set_default_read_buffer_len(IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(socket(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR)); - STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4242)); - - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - if(g_ctx != NULL){ - free(g_ctx); - } - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_082: [ If bind failed, the tlsio_openssl_open shall return __LINE__. ]*/ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_037: [ If the ssl client is not connected, the tlsio_openssl_open shall change the state to TLSIO_STATE_ERROR, log the error, and return _LINE_. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_039: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_ERROR. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_040: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_error callback was provided, it shall call the on_io_error. ] */ - TEST_FUNCTION(tlsio_openssl_open_bind__failed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - tls_io_instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - ASSERT_IS_NOT_NULL(tlsioInterfaces); - g_gethostbyname_success = 1; - g_socket_success = 1; - g_setsockopt_success = 1; - g_bind_success = 0; - g_connect_success = 1; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 1; - g_ssl_fd_isset = 0; - g_ssl_get_error_success = 1; - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(netconn_gethostbyname(IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(TLSv1_client_method()); - STRICT_EXPECTED_CALL(SSL_CTX_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_CTX_set_default_read_buffer_len(IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(socket(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(bind(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR)); - STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4242)); - - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - if(g_ctx != NULL){ - free(g_ctx); - } - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_083: [ If connect and getsockopt failed, the tlsio_openssl_open shall return __LINE__. ]*/ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_037: [ If the ssl client is not connected, the tlsio_openssl_open shall change the state to TLSIO_STATE_ERROR, log the error, and return _LINE_. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_039: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_ERROR. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_040: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_error callback was provided, it shall call the on_io_error. ] */ - TEST_FUNCTION(tlsio_openssl_open_connect_getsockopt__failed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - tls_io_instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - ASSERT_IS_NOT_NULL(tlsioInterfaces); - g_gethostbyname_success = 1; - g_socket_success = 1; - g_setsockopt_success = 1; - g_bind_success = 1; - g_connect_success = 0; - g_getsockopt_success = 0; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 1; - g_ssl_fd_isset = 0; - g_ssl_get_error_success = 1; - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(netconn_gethostbyname(IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(TLSv1_client_method()); - STRICT_EXPECTED_CALL(SSL_CTX_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_CTX_set_default_read_buffer_len(IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(socket(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(bind(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(connect(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(getsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR)); - STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4242)); - - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - if(g_ctx != NULL){ - free(g_ctx); - } - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_084: [ If lwip_select failed, the tlsio_openssl_open shall return __LINE__. ]*/ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_027: [ The tlsio_openssl_open shall set the tlsio to try to open the connection for MAX_RETRY times before assuming that connection failed. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_037: [ If the ssl client is not connected, the tlsio_openssl_open shall change the state to TLSIO_STATE_ERROR, log the error, and return _LINE_. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_039: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_ERROR. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_040: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_error callback was provided, it shall call the on_io_error. ] */ - TEST_FUNCTION(tlsio_openssl_open_lwip_select__failed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - tls_io_instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - ASSERT_IS_NOT_NULL(tlsioInterfaces); - g_gethostbyname_success = 1; - g_socket_success = 1; - g_setsockopt_success = 1; - g_bind_success = 1; - g_connect_success = 1; - g_ssl_lwip_select_success = 0; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 1; - g_ssl_fd_isset = 0; - g_ssl_get_error_success = 1; - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(netconn_gethostbyname(IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(TLSv1_client_method()); - STRICT_EXPECTED_CALL(SSL_CTX_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_CTX_set_default_read_buffer_len(IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(socket(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(bind(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(connect(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(lwip_select(IGNORED_NUM_ARG, IGNORED_PTR_ARG, - IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(getsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR)); - STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4242)); - - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - if(g_ctx != NULL){ - free(g_ctx); - } - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_085: [ If SSL_CTX_new failed, the tlsio_openssl_open shall return __LINE__. ]*/ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_037: [ If the ssl client is not connected, the tlsio_openssl_open shall change the state to TLSIO_STATE_ERROR, log the error, and return _LINE_. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_039: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_ERROR. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_040: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_error callback was provided, it shall call the on_io_error. ] */ - TEST_FUNCTION(tlsio_openssl_open_sslctxnew__failed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - tls_io_instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - ASSERT_IS_NOT_NULL(tlsioInterfaces); - g_gethostbyname_success = 1; - g_socket_success = 1; - g_setsockopt_success = 1; - g_bind_success = 1; - g_connect_success = 1; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 0; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 1; - g_ssl_fd_isset = 0; - g_ssl_get_error_success = 1; - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(netconn_gethostbyname(IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(TLSv1_client_method()); - STRICT_EXPECTED_CALL(SSL_CTX_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR)); - STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4242)); - - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_087: [ If SSL_new failed, the tlsio_openssl_open shall return __LINE__. ]*/ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_037: [ If the ssl client is not connected, the tlsio_openssl_open shall change the state to TLSIO_STATE_ERROR, log the error, and return _LINE_. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_039: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_ERROR. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_040: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_error callback was provided, it shall call the on_io_error. ] */ - TEST_FUNCTION(tlsio_openssl_open_sslnew__failed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - tls_io_instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - ASSERT_IS_NOT_NULL(tlsioInterfaces); - g_gethostbyname_success = 1; - g_socket_success = 1; - g_setsockopt_success = 1; - g_bind_success = 1; - g_connect_success = 1; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 0; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 1; - g_ssl_fd_isset = 0; - g_ssl_get_error_success = 1; - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(netconn_gethostbyname(IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(TLSv1_client_method()); - STRICT_EXPECTED_CALL(SSL_CTX_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_CTX_set_default_read_buffer_len(IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(socket(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(bind(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(connect(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(lwip_select(IGNORED_NUM_ARG, IGNORED_PTR_ARG, - IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(SSL_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR)); - STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4242)); - - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - if(g_ctx != NULL){ - free(g_ctx); - } - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_088: [ If SSL_set_fd failed, the tlsio_openssl_open shall return __LINE__. ]*/ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_037: [ If the ssl client is not connected, the tlsio_openssl_open shall change the state to TLSIO_STATE_ERROR, log the error, and return _LINE_. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_039: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_ERROR. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_040: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_error callback was provided, it shall call the on_io_error. ] */ - TEST_FUNCTION(tlsio_openssl_open_sslsetfd__failed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - tls_io_instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - ASSERT_IS_NOT_NULL(tlsioInterfaces); - g_gethostbyname_success = 1; - g_socket_success = 1; - g_setsockopt_success = 1; - g_bind_success = 1; - g_connect_success = 1; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 0; - g_ssl_connect_success = 1; - g_ssl_fd_isset = 0; - g_ssl_get_error_success = 1; - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(netconn_gethostbyname(IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(TLSv1_client_method()); - STRICT_EXPECTED_CALL(SSL_CTX_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_CTX_set_default_read_buffer_len(IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(socket(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(bind(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(connect(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(lwip_select(IGNORED_NUM_ARG, IGNORED_PTR_ARG, - IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(SSL_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_set_fd(IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR)); - STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4242)); - - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - if(g_ctx != NULL){ - free(g_ctx); - } - if (g_ssl != NULL){ - free(g_ssl); - } - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_089: [ If SSL_connect failed, the tlsio_openssl_open shall return __LINE__. ]*/ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_042: [ If the tlsio_openssl_open retry SSL_connect to open more than MAX_RETRY times without success, it shall return __LINE__. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_032: [ The tlsio_openssl_open shall store the provided on_io_error callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_033: [ The tlsio_openssl_open shall store the provided on_io_error_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_028: [ The tlsio_openssl_open shall store the provided on_io_open_complete callback function address. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_029: [ The tlsio_openssl_open shall store the provided on_io_open_complete_context handle. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_037: [ If the ssl client is not connected, the tlsio_openssl_open shall change the state to TLSIO_STATE_ERROR, log the error, and return _LINE_. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_039: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_open_complete callback was provided, it shall call the on_io_open_complete with IO_OPEN_ERROR. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_040: [ If the tlsio_openssl_open failed to open the tls connection, and the on_io_error callback was provided, it shall call the on_io_error. ] */ - TEST_FUNCTION(tlsio_openssl_open_sslconnect__failed) - { - ///arrange - TLS_IO_INSTANCE tls_io_instance; - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - int retry = 0; - memset(&tls_io_instance, 0, sizeof(TLS_IO_INSTANCE)); - tls_io_instance.tlsio_state = TLSIO_STATE_NOT_OPEN; - ASSERT_IS_NOT_NULL(tlsioInterfaces); - g_gethostbyname_success = 1; - g_socket_success = 1; - g_setsockopt_success = 1; - g_bind_success = 1; - g_connect_success = 1; - g_ssl_lwip_select_success = 1; - g_ssl_ctx_new_success = 1; - g_ssl_new_success = 1; - g_ssl_set_fd_success = 1; - g_ssl_connect_success = 0; - g_ssl_fd_isset = 0; - g_ssl_get_error_success = 1; - - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(netconn_gethostbyname(IGNORED_PTR_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(TLSv1_client_method()); - STRICT_EXPECTED_CALL(SSL_CTX_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_CTX_set_default_read_buffer_len(IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(socket(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(setsockopt(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(fcntl(IGNORED_NUM_ARG,IGNORED_NUM_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(bind(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(connect(IGNORED_NUM_ARG,IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3); - STRICT_EXPECTED_CALL(lwip_select(IGNORED_NUM_ARG, IGNORED_PTR_ARG, - IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(SSL_new(IGNORED_PTR_ARG)).IgnoreArgument(1); - - STRICT_EXPECTED_CALL(SSL_set_fd(IGNORED_PTR_ARG, IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - while(retry < MAX_RETRY){ - STRICT_EXPECTED_CALL(lwip_select(IGNORED_NUM_ARG, IGNORED_PTR_ARG, - IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2).IgnoreArgument(3).IgnoreArgument(4).IgnoreArgument(5); - STRICT_EXPECTED_CALL(FD_ISSET(IGNORED_NUM_ARG,IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(SSL_connect(IGNORED_PTR_ARG)).IgnoreArgument(1); - STRICT_EXPECTED_CALL(SSL_get_error(IGNORED_PTR_ARG,IGNORED_NUM_ARG)).IgnoreArgument(1).IgnoreArgument(2); - STRICT_EXPECTED_CALL(os_delay_us(IGNORED_NUM_ARG)).IgnoreArgument(1); - retry++; - } - STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR)); - STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4242)); - - ///act - result = tlsioInterfaces->concrete_io_open(&tls_io_instance, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4242, test_on_io_error, (void*)0x4242); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_NOT_EQUAL(int, 0, result); - - ///cleanup - if(g_ctx != NULL){ - free(g_ctx); - } - if (g_ssl != NULL){ - free(g_ssl); - } - if (g_sslmethod != NULL){ - free(g_sslmethod); - } - } - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_012: [ If there is not enough memory to create the tlsio, the tlsio_openssl_create shall return NULL as the handle. ] */ - TEST_FUNCTION(tlsio_openssl_create_malloc__failed) - { - ///arrange - OPTIONHANDLER_HANDLE result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - int negativeTestsInitResult = umock_c_negative_tests_init(); - size_t i; - - ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult); - - tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1); - - umock_c_negative_tests_snapshot(); - - for (i = 0; i < umock_c_negative_tests_call_count(); i++) - { - umock_c_negative_tests_reset(); - umock_c_negative_tests_fail_call(i); - ///act - result = (OPTIONHANDLER_HANDLE)tlsioInterfaces->concrete_io_create((void*)&tlsio_config); - - ///assert - ASSERT_IS_NULL(result); - } - - ///cleanup - umock_c_negative_tests_deinit(); - } - - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_009: [ The tlsio_openssl_create shall create a new instance of the tlsio for esp8266. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_010: [ The tlsio_openssl_create shall return a non-NULL handle on success. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_011: [ The tlsio_openssl_create shall allocate memory to control the tlsio instance. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_016: [ The tlsio_openssl_create shall initialize all callback pointers as NULL. ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_017: [ The tlsio_openssl_create shall receive the connection configuration (TLSIO_CONFIG). ] */ - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_020: [ If tlsio_openssl_create get success to create the tlsio instance, it shall set the tlsio state as TLSIO_STATE_NOT_OPEN. ] */ - TEST_FUNCTION(tlsio_openssl_create__succeed) - { - TLS_IO_INSTANCE* result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - umock_c_reset_all_calls(); - - STRICT_EXPECTED_CALL(gballoc_malloc(sizeof(TLS_IO_INSTANCE))); - STRICT_EXPECTED_CALL(mallocAndStrcpy_s(&g_destination, IGNORED_PTR_ARG)).IgnoreArgument(1).IgnoreArgument(2); - - ///act - result = (TLS_IO_INSTANCE*)tlsioInterfaces->concrete_io_create((void*)&tlsio_config); - - ///assert - ASSERT_IS_NOT_NULL(result); - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_EQUAL(int, (int)TLSIO_STATE_NOT_OPEN, result->tlsio_state); - ///cleanup - if (g_mallocptr != NULL){ - free(g_mallocptr); - } - if (g_destination != NULL){ - free(g_destination); - } - } - - - /* TESTS_SRS_TLSIO_SSL_ESP8266_99_013: [ The tlsio_openssl_create shall return NULL when io_create_parameters is NULL. ] */ - TEST_FUNCTION(tlsio_openssl_create_NULL_parameters__failed) - { - ///arrange - OPTIONHANDLER_HANDLE result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - ///act - result = (OPTIONHANDLER_HANDLE)tlsioInterfaces->concrete_io_create(NULL); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_IS_NULL(result); - - ///cleanup - } - - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_078: [ The tlsio_openssl_retrieveoptions shall not do anything, and return NULL. ]*/ - TEST_FUNCTION(tlsio_openssl_retrieveoptions__succeed) - { - ///arrange - OPTIONHANDLER_HANDLE result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - umock_c_reset_all_calls(); - - ///act - result = tlsioInterfaces->concrete_io_retrieveoptions(NULL); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_IS_NULL(result); - - ///cleanup - } - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_077: [ he tlsio_openssl_setoption shall not do anything, and return 0. ]*/ - TEST_FUNCTION(tlsio_openssl_setoption__succeed) - { - ///arrange - int result; - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces = tlsio_openssl_get_interface_description(); - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - umock_c_reset_all_calls(); - - ///act - result = tlsioInterfaces->concrete_io_setoption(NULL, NULL, NULL); - - ///assert - ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); - ASSERT_ARE_EQUAL(int, 0, result); - - ///cleanup - } - - /* Tests_SRS_TLSIO_SSL_ESP8266_99_008: [ The tlsio_openssl_get_interface_description shall return the VTable IO_INTERFACE_DESCRIPTION. ]*/ - TEST_FUNCTION(tlsio_openssl_get_interface_description__succeed) - { - ///arrange - const IO_INTERFACE_DESCRIPTION* tlsioInterfaces; - - ///act - tlsioInterfaces = tlsio_openssl_get_interface_description(); - - ///assert - ASSERT_IS_NOT_NULL(tlsioInterfaces); - - ///cleanup - } - - -END_TEST_SUITE(tlsio_esp8266_ut) \ No newline at end of file diff --git a/tests/tlsio_wolfssl_ut/tlsio_wolfssl_ut.c b/tests/tlsio_wolfssl_ut/tlsio_wolfssl_ut.c index bd950238d..555e501ab 100644 --- a/tests/tlsio_wolfssl_ut/tlsio_wolfssl_ut.c +++ b/tests/tlsio_wolfssl_ut/tlsio_wolfssl_ut.c @@ -69,6 +69,7 @@ static XIO_HANDLE TEST_IO_HANDLE = (XIO_HANDLE)0x0015; static const unsigned char TEST_BUFFER[] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA }; static const size_t TEST_BUFFER_LEN = BUFFER_LEN; static const char* TEST_TRUSTED_CERT = "test_trusted_cert"; +static const char* TEST_HOSTNAME = "hostname.test"; static HandShakeDoneCb g_handshake_done_cb = NULL; static void* g_handshake_done_ctx = NULL; @@ -130,7 +131,10 @@ MOCK_FUNCTION_END(SSL_SUCCESS) MOCK_FUNCTION_WITH_CODE(WOLFSSL_API, int, wolfSSL_SetDevId, WOLFSSL*, ssl, int, devId) MOCK_FUNCTION_END(WOLFSSL_SUCCESS) #endif - +MOCK_FUNCTION_WITH_CODE(WOLFSSL_API, int, wolfSSL_get_error, WOLFSSL*, ssl, int, ret) +MOCK_FUNCTION_END(SSL_SUCCESS) +MOCK_FUNCTION_WITH_CODE(WOLFSSL_API, int, wolfSSL_check_domain_name, WOLFSSL*, ssl, const char*, dn) +MOCK_FUNCTION_END(SSL_SUCCESS) MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES) @@ -249,6 +253,7 @@ TEST_FUNCTION(tlsio_wolfssl_create_succeeds) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; /*STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)); STRICT_EXPECTED_CALL(wolfTLSv1_2_client_method()); @@ -285,13 +290,15 @@ TEST_FUNCTION(tlsio_wolfssl_destroy_succeeds) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); STRICT_EXPECTED_CALL(wolfSSL_free(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(wolfSSL_CTX_free(IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(xio_destroy(IGNORED_PTR_ARG)); - STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); + STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); // hostname + STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); // tlsio //act tlsio_wolfssl_destroy(io_handle); @@ -334,6 +341,7 @@ TEST_FUNCTION(tlsio_wolfssl_open_succeeds) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); @@ -353,6 +361,7 @@ TEST_FUNCTION(tlsio_wolfssl_open_with_cert_succeeds) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_setoption(io_handle, SU_OPTION_X509_CERT, TEST_TRUSTED_CERT); (void)tlsio_wolfssl_setoption(io_handle, SU_OPTION_X509_PRIVATE_KEY, TEST_TRUSTED_CERT); @@ -374,10 +383,12 @@ TEST_FUNCTION(tlsio_wolfssl_open_set_dev_id_succeeds) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); STRICT_EXPECTED_CALL(wolfSSL_SetDevId(TEST_WOLFSSL, 11)); + STRICT_EXPECTED_CALL(wolfSSL_check_domain_name(TEST_WOLFSSL, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(xio_open(TEST_IO_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(wolfSSL_connect(TEST_WOLFSSL)); @@ -402,9 +413,11 @@ TEST_FUNCTION(tlsio_wolfssl_open_set_dev_id_2_succeeds) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); + STRICT_EXPECTED_CALL(wolfSSL_check_domain_name(TEST_WOLFSSL, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(xio_open(TEST_IO_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG)); STRICT_EXPECTED_CALL(wolfSSL_connect(TEST_WOLFSSL)); STRICT_EXPECTED_CALL(wolfSSL_SetDevId(TEST_WOLFSSL, 11)); @@ -430,6 +443,7 @@ TEST_FUNCTION(tlsio_wolfssl_on_handshake_done_succeed) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -463,6 +477,7 @@ TEST_FUNCTION(tlsio_wolfssl_close_succeeds) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -482,6 +497,7 @@ TEST_FUNCTION(tlsio_wolfssl_close_not_open_succeeds) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); @@ -514,6 +530,7 @@ TEST_FUNCTION(tlsio_wolfssl_send_buffer_0_fail) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -534,6 +551,7 @@ TEST_FUNCTION(tlsio_wolfssl_send_not_open_fail) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); @@ -552,6 +570,7 @@ TEST_FUNCTION(tlsio_wolfssl_send_succeeds) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -572,6 +591,7 @@ TEST_FUNCTION(tlsio_wolfssl_send_write_returns_zero_fail) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -607,6 +627,7 @@ TEST_FUNCTION(tlsio_wolfssl_dowork_NOT_OPEN_succeeds) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); @@ -625,6 +646,7 @@ TEST_FUNCTION(tlsio_wolfssl_dowork_succeeds) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -685,6 +707,7 @@ TEST_FUNCTION(tlsio_wolfssl_setoption_option_name_NULL_Fail) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); @@ -703,6 +726,7 @@ TEST_FUNCTION(tlsio_wolfssl_setoption_trusted_cert_succeed) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); @@ -721,6 +745,7 @@ TEST_FUNCTION(tlsio_wolfssl_setoption_trusted_cert_twice_succeed) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); @@ -743,6 +768,7 @@ TEST_FUNCTION(tlsio_wolfssl_setoption_device_id_succeed) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); @@ -762,6 +788,7 @@ TEST_FUNCTION(tlsio_wolfssl_setoption_device_id_fail) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); umock_c_reset_all_calls(); @@ -784,6 +811,7 @@ TEST_FUNCTION(tlsio_wolfssl_on_underlying_io_bytes_received_ctx_NULL_succeess) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -803,6 +831,7 @@ TEST_FUNCTION(tlsio_wolfssl_on_underlying_io_bytes_received_realloc_NULL_success //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -824,6 +853,7 @@ TEST_FUNCTION(tlsio_wolfssl_on_underlying_io_bytes_received_success) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -845,6 +875,7 @@ TEST_FUNCTION(tlsio_wolfssl_on_underlying_io_error_success) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -864,6 +895,7 @@ TEST_FUNCTION(tlsio_wolfssl_on_underlying_io_error_ctx_NULL_success) //arrange TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -884,6 +916,7 @@ TEST_FUNCTION(tlsio_wolfssl_on_io_recv_on_open_success) char recv_buff[BUFFER_LEN]; TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); (void)tlsio_wolfssl_open(io_handle, on_io_open_complete, NULL, on_bytes_recv, NULL, on_error, NULL); umock_c_reset_all_calls(); @@ -905,6 +938,7 @@ TEST_FUNCTION(tlsio_wolfssl_on_io_recv_timeout_success) char recv_buff[BUFFER_LEN]; TLSIO_CONFIG tls_io_config; memset(&tls_io_config, 0, sizeof(tls_io_config)); + tls_io_config.hostname = TEST_HOSTNAME; CONCRETE_IO_HANDLE io_handle = tlsio_wolfssl_create(&tls_io_config); // ensure we stay in the handshake mode g_handshake_done_cb = NULL;