diff --git a/coap-service/coap_service_api.h b/coap-service/coap_service_api.h index 4834acfb971..015a30c3186 100644 --- a/coap-service/coap_service_api.h +++ b/coap-service/coap_service_api.h @@ -40,9 +40,9 @@ extern "C" { #define COAP_SERVICE_ACCESS_DELETE_ALLOWED 0x08 // Bits for service options -#define COAP_SERVICE_OPTIONS_NONE 0x00 +#define COAP_SERVICE_OPTIONS_NONE 0x00 #define COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET 0x01 -#define COAP_SERVICE_OPTIONS_SECURE 0x02 +#define COAP_SERVICE_OPTIONS_SECURE 0x02 #define COAP_SERVICE_OPTIONS_EPHEMERAL_PORT 0x04 /** Coap interface selected as socket interface */ #define COAP_SERVICE_OPTIONS_SELECT_SOCKET_IF 0x08 @@ -52,7 +52,7 @@ extern "C" { #define COAP_SERVICE_OPTIONS_SECURE_BYPASS 0x80 // Bits for request options -#define COAP_REQUEST_OPTIONS_NONE 0x00 +#define COAP_REQUEST_OPTIONS_NONE 0x00 #define COAP_REQUEST_OPTIONS_ADDRESS_DEFAULT 0x00//!< default is not setting either short or long. #define COAP_REQUEST_OPTIONS_ADDRESS_LONG 0x01 #define COAP_REQUEST_OPTIONS_ADDRESS_SHORT 0x02 @@ -105,7 +105,7 @@ typedef int coap_service_request_recv_cb(int8_t service_id, uint8_t source_addre * * \return 0 for success / -1 for failure */ -typedef int coap_service_security_start_cb(int8_t service_id, uint8_t address[static 16], uint16_t port, uint8_t* pw, uint8_t *pw_len); +typedef int coap_service_security_start_cb(int8_t service_id, uint8_t address[static 16], uint16_t port, uint8_t *pw, uint8_t *pw_len); /** * \brief CoAP service security done callback @@ -142,7 +142,7 @@ extern int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, * * \param service_id Id number of the current service. */ -extern void coap_service_delete( int8_t service_id ); +extern void coap_service_delete(int8_t service_id); /** * \brief Close secure connection @@ -243,7 +243,7 @@ extern int8_t coap_service_unregister_uri(int8_t service_id, const char *uri); * \return msg_id Id number of the current message. */ extern uint16_t coap_service_request_send(int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16], uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, - sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb); + sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb); /** * \brief Sends CoAP service response @@ -261,7 +261,7 @@ extern uint16_t coap_service_request_send(int8_t service_id, uint8_t options, co * \return -1 For failure *- 0 For success */ -extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len); +extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len); /** * \brief Sends CoAP service response @@ -280,7 +280,7 @@ extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_ * \return -1 For failure *- 0 For success */ -extern int8_t coap_service_response_send_by_msg_id(int8_t service_id, uint8_t options, uint16_t msg_id, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len); +extern int8_t coap_service_response_send_by_msg_id(int8_t service_id, uint8_t options, uint16_t msg_id, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len); /** * \brief Delete CoAP request transaction diff --git a/source/coap_connection_handler.c b/source/coap_connection_handler.c index 22348883b5a..4103fd6f496 100644 --- a/source/coap_connection_handler.c +++ b/source/coap_connection_handler.c @@ -32,7 +32,7 @@ typedef enum session_state_e { SECURE_SESSION_HANDSHAKE_ONGOING = 0, SECURE_SESSION_OK, SECURE_SESSION_CLOSED -}session_state_t; +} session_state_t; typedef struct internal_socket_s { coap_conn_handler_t *parent; @@ -64,7 +64,7 @@ static NS_LIST_DEFINE(socket_list, internal_socket_t, link); static uint8_t max_handshakes = MAX_ONGOING_HANDSHAKES; static uint8_t max_sessions = MAX_SECURE_SESSION_COUNT; -static void timer_cb(void* param); +static void timer_cb(void *param); static void recv_sckt_msg(void *cb_res); #ifdef COAP_SECURITY_AVAILABLE @@ -132,11 +132,11 @@ static void secure_session_delete(secure_session_t *this) if (this) { ns_list_remove(&secure_session_list, this); transactions_delete_all(this->remote_host.address, this->remote_host.identifier); - if( this->sec_handler ){ + if (this->sec_handler) { coap_security_destroy(this->sec_handler); this->sec_handler = NULL; } - if(this->timer.timer){ + if (this->timer.timer) { eventOS_timeout_cancel(this->timer.timer); } ns_dyn_mem_free(this); @@ -160,21 +160,21 @@ static int8_t virtual_socket_id_allocate() static secure_session_t *secure_session_create(internal_socket_t *parent, const uint8_t *address_ptr, uint16_t port, SecureConnectionMode secure_mode) { uint8_t handshakes = 0; - if(!address_ptr){ + if (!address_ptr) { return NULL; } - if(max_sessions <= ns_list_count(&secure_session_list)){ + if (max_sessions <= ns_list_count(&secure_session_list)) { // Seek & destroy oldest session where close notify have been sent secure_session_t *to_be_removed = NULL; ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) { - if(cur_ptr->session_state == SECURE_SESSION_CLOSED){ - if(!to_be_removed || cur_ptr->last_contact_time < to_be_removed->last_contact_time){ + if (cur_ptr->session_state == SECURE_SESSION_CLOSED) { + if (!to_be_removed || cur_ptr->last_contact_time < to_be_removed->last_contact_time) { to_be_removed = cur_ptr; } } } - if(!to_be_removed){ + if (!to_be_removed) { tr_err("max session count exceeded"); return NULL; } @@ -184,11 +184,11 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const // Count for ongoing handshakes ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) { - if(cur_ptr->session_state == SECURE_SESSION_HANDSHAKE_ONGOING){ + if (cur_ptr->session_state == SECURE_SESSION_HANDSHAKE_ONGOING) { handshakes++; } } - if(handshakes >= max_handshakes) { + if (handshakes >= max_handshakes) { tr_err("ongoing handshakes exceeded"); return NULL; } @@ -201,8 +201,8 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const uint8_t timer_id = 1; - while(secure_session_find_by_timer_id(timer_id)){ - if(timer_id == 0xff){ + while (secure_session_find_by_timer_id(timer_id)) { + if (timer_id == 0xff) { ns_dyn_mem_free(this); return NULL; } @@ -215,8 +215,8 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const this->remote_host.identifier = port; this->sec_handler = coap_security_create(parent->socket, this->timer.id, this, secure_mode, - &secure_session_sendto, &secure_session_recvfrom, &start_timer, &timer_status); - if( !this->sec_handler ){ + &secure_session_sendto, &secure_session_recvfrom, &start_timer, &timer_status); + if (!this->sec_handler) { tr_err("security create failed"); ns_dyn_mem_free(this); return NULL; @@ -230,11 +230,12 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const } -static void clear_secure_sessions(internal_socket_t *this){ - if( this ){ +static void clear_secure_sessions(internal_socket_t *this) +{ + if (this) { ns_list_foreach_safe(secure_session_t, cur_ptr, &secure_session_list) { - if( cur_ptr->parent == this ){ - coap_security_send_close_alert( cur_ptr->sec_handler ); + if (cur_ptr->parent == this) { + coap_security_send_close_alert(cur_ptr->sec_handler); secure_session_delete(cur_ptr); } } @@ -245,9 +246,9 @@ static secure_session_t *secure_session_find(internal_socket_t *parent, const ui { secure_session_t *this = NULL; ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) { - if( cur_ptr->sec_handler ){ + if (cur_ptr->sec_handler) { if (cur_ptr->parent == parent && cur_ptr->remote_host.identifier == port && - memcmp(cur_ptr->remote_host.address, address_ptr, 16) == 0) { + memcmp(cur_ptr->remote_host.address, address_ptr, 16) == 0) { this = cur_ptr; break; } @@ -297,13 +298,13 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem this->real_socket = real_socket; this->bypass_link_sec = bypassSec; this->socket = -1; - if( real_socket ){ - if( use_ephemeral_port ){ //socket_api creates ephemeral port if the one provided is 0 + if (real_socket) { + if (use_ephemeral_port) { //socket_api creates ephemeral port if the one provided is 0 listen_port = 0; } - if( !is_secure ){ + if (!is_secure) { this->socket = socket_open(SOCKET_UDP, listen_port, recv_sckt_msg); - }else{ + } else { #ifdef COAP_SECURITY_AVAILABLE this->socket = socket_open(SOCKET_UDP, listen_port, secure_recv_sckt_msg); #else @@ -311,18 +312,24 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem #endif } // Socket create failed - if(this->socket < 0){ + if (this->socket < 0) { ns_dyn_mem_free(this); return NULL; } - socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_LINK_LAYER_SECURITY, &(const int8_t) {bypassSec ? 0 : 1}, sizeof(int8_t)); + socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_LINK_LAYER_SECURITY, &(const int8_t) { + bypassSec ? 0 : 1 + }, sizeof(int8_t)); // XXX API for this? May want to get clever to do recommended first query = 1 hop, retries = whole PAN - socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_MULTICAST_HOPS, &(const int16_t) {16}, sizeof(int16_t)); + socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_MULTICAST_HOPS, &(const int16_t) { + 16 + }, sizeof(int16_t)); // Set socket option to receive packet info - socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_RECVPKTINFO, &(const bool) {1}, sizeof(bool)); + socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_IPV6_RECVPKTINFO, &(const bool) { + 1 + }, sizeof(bool)); if (socket_interface_selection > 0) { // Interface selection requested as socket_interface_selection set socket_setsockopt(this->socket, SOCKET_IPPROTO_IPV6, SOCKET_INTERFACE_SELECT, &socket_interface_selection, sizeof(socket_interface_selection)); @@ -343,15 +350,15 @@ static void int_socket_delete(internal_socket_t *this) { if (this) { this->usage_counter--; - if(this->usage_counter == 0){ + if (this->usage_counter == 0) { clear_secure_sessions(this); socket_close(this->socket); ns_list_remove(&socket_list, this); - if( this->data ){ + if (this->data) { ns_dyn_mem_free(this->data); this->data = NULL; } - if(this->parent){ + if (this->parent) { ns_dyn_mem_free(this->parent); } ns_dyn_mem_free(this); @@ -363,7 +370,7 @@ static internal_socket_t *int_socket_find_by_socket_id(int8_t id) { internal_socket_t *this = NULL; ns_list_foreach(internal_socket_t, cur_ptr, &socket_list) { - if( cur_ptr->socket == id ) { + if (cur_ptr->socket == id) { this = cur_ptr; break; } @@ -377,8 +384,8 @@ static internal_socket_t *int_socket_find(uint16_t port, bool is_secure, bool is internal_socket_t *this = NULL; ns_list_foreach(internal_socket_t, cur_ptr, &socket_list) { - if( cur_ptr->listen_port == port && cur_ptr->real_socket == is_real_socket && - is_secure == cur_ptr->is_secure /*&& bypass_link_sec == bypassSec*/) { + if (cur_ptr->listen_port == port && cur_ptr->real_socket == is_real_socket && + is_secure == cur_ptr->is_secure /*&& bypass_link_sec == bypassSec*/) { this = cur_ptr; break; } @@ -412,7 +419,7 @@ static int send_to_real_socket(int8_t socket_id, const ns_address_t *address, co cmsg->cmsg_level = SOCKET_IPPROTO_IPV6; cmsg->cmsg_len = NS_CMSG_LEN(sizeof(ns_in6_pktinfo_t)); - pktinfo = (ns_in6_pktinfo_t*)NS_CMSG_DATA(cmsg); + pktinfo = (ns_in6_pktinfo_t *)NS_CMSG_DATA(cmsg); pktinfo->ipi6_ifindex = 0; memcpy(pktinfo->ipi6_addr, source_address, 16); } @@ -424,14 +431,15 @@ static int secure_session_sendto(int8_t socket_id, void *handle, const void *buf { secure_session_t *session = handle; internal_socket_t *sock = int_socket_find_by_socket_id(socket_id); - if(!sock){ + if (!sock) { return -1; } - if(!sock->real_socket){ + if (!sock->real_socket) { // Send to virtual socket cb int ret = sock->parent->_send_cb(sock->socket, session->remote_host.address, session->remote_host.identifier, buf, len); - if( ret < 0 ) + if (ret < 0) { return ret; + } return len; } @@ -457,7 +465,7 @@ static int secure_session_recvfrom(int8_t socket_id, unsigned char *buf, size_t (void)len; internal_socket_t *sock = int_socket_find_by_socket_id(socket_id); if (sock->data && sock->data_len > 0) { - memcpy( buf, sock->data, sock->data_len ); + memcpy(buf, sock->data, sock->data_len); int l = sock->data_len; ns_dyn_mem_free(sock->data); sock->data = NULL; @@ -477,21 +485,19 @@ static void timer_cb(void *param) { secure_session_t *sec = param; - if( sec && is_secure_session_valid(sec)){ - if(sec->timer.fin_ms > sec->timer.int_ms){ + if (sec && is_secure_session_valid(sec)) { + if (sec->timer.fin_ms > sec->timer.int_ms) { /* Intermediate expiry */ sec->timer.fin_ms -= sec->timer.int_ms; sec->timer.state = TIMER_STATE_INT_EXPIRY; int error = coap_security_handler_continue_connecting(sec->sec_handler); - if(MBEDTLS_ERR_SSL_TIMEOUT == error) { + if (MBEDTLS_ERR_SSL_TIMEOUT == error) { //TODO: How do we handle timeouts? secure_session_delete(sec); + } else { + sec->timer.timer = eventOS_timeout_ms(timer_cb, sec->timer.int_ms, (void *)sec); } - else{ - sec->timer.timer = eventOS_timeout_ms(timer_cb, sec->timer.int_ms, (void*)sec); - } - } - else{ + } else { /* We have counted the number of cycles - finish */ eventOS_timeout_cancel(sec->timer.timer); sec->timer.fin_ms = 0; @@ -499,7 +505,7 @@ static void timer_cb(void *param) sec->timer.timer = NULL; sec->timer.state = TIMER_STATE_FIN_EXPIRY; int error = coap_security_handler_continue_connecting(sec->sec_handler); - if(MBEDTLS_ERR_SSL_TIMEOUT == error) { + if (MBEDTLS_ERR_SSL_TIMEOUT == error) { //TODO: How do we handle timeouts? secure_session_delete(sec); } @@ -510,12 +516,12 @@ static void timer_cb(void *param) static void start_timer(int8_t timer_id, uint32_t int_ms, uint32_t fin_ms) { secure_session_t *sec = secure_session_find_by_timer_id(timer_id); - if( sec ){ + if (sec) { if ((int_ms > 0) && (fin_ms > 0)) { sec->timer.int_ms = int_ms; sec->timer.fin_ms = fin_ms; sec->timer.state = TIMER_STATE_NO_EXPIRY; - if(sec->timer.timer){ + if (sec->timer.timer) { eventOS_timeout_cancel(sec->timer.timer); } sec->timer.timer = eventOS_timeout_ms(timer_cb, int_ms, sec); @@ -533,7 +539,7 @@ static void start_timer(int8_t timer_id, uint32_t int_ms, uint32_t fin_ms) static int timer_status(int8_t timer_id) { secure_session_t *sec = secure_session_find_by_timer_id(timer_id); - if( sec ){ + if (sec) { return (int)sec->timer.state; } return TIMER_STATE_CANCELLED; @@ -577,7 +583,7 @@ static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_a while (cmsg) { switch (cmsg->cmsg_type) { case SOCKET_IPV6_PKTINFO: - pkt = (ns_in6_pktinfo_t*)NS_CMSG_DATA(cmsg); + pkt = (ns_in6_pktinfo_t *)NS_CMSG_DATA(cmsg); break; default: break; @@ -649,7 +655,7 @@ static void secure_recv_sckt_msg(void *cb_res) session->last_contact_time = coap_service_get_internal_timer_ticks(); // Start handshake if (!coap_security_handler_is_started(session->sec_handler)) { - if(-1 == coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, sock->timeout_min, sock->timeout_max)) { + if (-1 == coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, sock->timeout_min, sock->timeout_max)) { tr_err("Connection start failed"); secure_session_delete(session); } @@ -664,22 +670,22 @@ static void secure_recv_sckt_msg(void *cb_res) eventOS_timeout_cancel(session->timer.timer); session->timer.timer = NULL; session->session_state = SECURE_SESSION_OK; - if( sock->parent->_security_done_cb ){ + if (sock->parent->_security_done_cb) { sock->parent->_security_done_cb(sock->socket, src_address.address, - src_address.identifier, - (void *)coap_security_handler_keyblock(session->sec_handler)); + src_address.identifier, + (void *)coap_security_handler_keyblock(session->sec_handler)); } - } else if (ret < 0){ + } else if (ret < 0) { // error handling // TODO: here we also should clear CoAP retransmission buffer and inform that CoAP request sending is failed. secure_session_delete(session); } - //Session valid + //Session valid } else { unsigned char *data = ns_dyn_mem_temporary_alloc(sock->data_len); int len = 0; len = coap_security_handler_read(session->sec_handler, data, sock->data_len); - if( len < 0 ){ + if (len < 0) { if (len != MBEDTLS_ERR_SSL_WANT_READ && len != MBEDTLS_ERR_SSL_WANT_WRITE && len != MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE) { secure_session_delete(session); @@ -715,7 +721,7 @@ static void recv_sckt_msg(void *cb_res) int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t address[static 16], uint16_t port, uint8_t *data_ptr, uint16_t data_len) { - if(!handler || !handler->socket) { + if (!handler || !handler->socket) { return -1; } internal_socket_t *sock = handler->socket; @@ -768,16 +774,15 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a if (coap_security_handler_is_started(session->sec_handler)) { if (session->session_state == SECURE_SESSION_HANDSHAKE_ONGOING) { int ret = coap_security_handler_continue_connecting(session->sec_handler); - if(ret == 0){ + if (ret == 0) { session->session_state = SECURE_SESSION_OK; - if( handler->_security_done_cb ){ + if (handler->_security_done_cb) { handler->_security_done_cb(sock->socket, - address, port, - (void *)coap_security_handler_keyblock(session->sec_handler)); + address, port, + (void *)coap_security_handler_keyblock(session->sec_handler)); } return 0; - } - else if (ret < 0) { + } else if (ret < 0) { // error handling // TODO: here we also should clear CoAP retransmission buffer and inform that CoAP request sending is failed. secure_session_delete(session); @@ -819,16 +824,16 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a } coap_conn_handler_t *connection_handler_create(receive_from_socket_cb *recv_from_cb, - send_to_socket_cb *send_to_cb, - get_pw_cb *pw_cb, - security_done_cb *done_cb ) + send_to_socket_cb *send_to_cb, + get_pw_cb *pw_cb, + security_done_cb *done_cb) { - if(recv_from_cb == NULL) { + if (recv_from_cb == NULL) { return NULL; } coap_conn_handler_t *handler = ns_dyn_mem_alloc(sizeof(coap_conn_handler_t)); - if(!handler){ + if (!handler) { return NULL; } memset(handler, 0, sizeof(coap_conn_handler_t)); @@ -844,25 +849,25 @@ coap_conn_handler_t *connection_handler_create(receive_from_socket_cb *recv_from void connection_handler_destroy(coap_conn_handler_t *handler, bool multicast_group_leave) { - if(handler){ + if (handler) { if (multicast_group_leave) { coap_multicast_group_join_or_leave(handler->socket->socket, SOCKET_IPV6_LEAVE_GROUP, handler->socket_interface_selection); } if (handler->security_keys) { ns_dyn_mem_free(handler->security_keys); } - int_socket_delete(handler->socket); - ns_dyn_mem_free(handler); + int_socket_delete(handler->socket); + ns_dyn_mem_free(handler); } } -void connection_handler_close_secure_connection( coap_conn_handler_t *handler, uint8_t destination_addr_ptr[static 16], uint16_t port ) +void connection_handler_close_secure_connection(coap_conn_handler_t *handler, uint8_t destination_addr_ptr[static 16], uint16_t port) { if (handler) { if (handler->socket && handler->socket->is_secure) { - secure_session_t *session = secure_session_find( handler->socket, destination_addr_ptr, port ); + secure_session_t *session = secure_session_find(handler->socket, destination_addr_ptr, port); if (session) { - coap_security_send_close_alert( session->sec_handler ); + coap_security_send_close_alert(session->sec_handler); session->session_state = SECURE_SESSION_CLOSED; session->last_contact_time = coap_service_get_internal_timer_ticks(); } @@ -883,7 +888,7 @@ int coap_connection_handler_open_connection(coap_conn_handler_t *handler, uint16 int_socket_delete(handler->socket); } - internal_socket_t *current = !use_ephemeral_port?int_socket_find(listen_port, is_secure, is_real_socket, bypassSec):NULL; + internal_socket_t *current = !use_ephemeral_port ? int_socket_find(listen_port, is_secure, is_real_socket, bypassSec) : NULL; if (!current) { handler->socket = int_socket_create(listen_port, use_ephemeral_port, is_secure, is_real_socket, bypassSec, handler->socket_interface_selection, handler->registered_to_multicast); if (!handler->socket) { @@ -918,7 +923,7 @@ int coap_connection_handler_send_data(coap_conn_handler_t *handler, const ns_add memset(&security_material, 0, sizeof(coap_security_keys_t)); - if (!handler->_get_password_cb || 0 != handler->_get_password_cb(handler->socket->socket, (uint8_t*)dest_addr->address, dest_addr->identifier, &security_material)) { + if (!handler->_get_password_cb || 0 != handler->_get_password_cb(handler->socket->socket, (uint8_t *)dest_addr->address, dest_addr->identifier, &security_material)) { return -1; } @@ -932,11 +937,11 @@ int coap_connection_handler_send_data(coap_conn_handler_t *handler, const ns_add } else if (session->session_state == SECURE_SESSION_OK) { session->last_contact_time = coap_service_get_internal_timer_ticks(); - if (0 > coap_security_handler_send_message(session->sec_handler, data_ptr, data_len )) { + if (0 > coap_security_handler_send_message(session->sec_handler, data_ptr, data_len)) { return -1; } } - /* Unsecure */ + /* Unsecure */ } else { /* Virtual socket */ if (!handler->socket->real_socket && handler->_send_cb) { @@ -964,11 +969,11 @@ int coap_connection_handler_send_data(coap_conn_handler_t *handler, const ns_add bool coap_connection_handler_socket_belongs_to(coap_conn_handler_t *handler, int8_t socket_id) { - if( !handler || !handler->socket){ + if (!handler || !handler->socket) { return false; } - if( handler->socket->socket == socket_id){ + if (handler->socket->socket == socket_id) { return true; } return false; @@ -976,7 +981,7 @@ bool coap_connection_handler_socket_belongs_to(coap_conn_handler_t *handler, int int8_t coap_connection_handler_set_timeout(coap_conn_handler_t *handler, uint32_t min, uint32_t max) { - if(!handler || !handler->socket){ + if (!handler || !handler->socket) { return -1; } handler->socket->timeout_max = max; @@ -999,19 +1004,19 @@ int8_t coap_connection_handler_handshake_limits_set(uint8_t handshakes_limit, ui /* No need to call every second - call rather like every minute (SECURE_SESSION_CLEAN_INTERVAL sets this) */ void coap_connection_handler_exec(uint32_t time) { - if(ns_list_count(&secure_session_list)){ + if (ns_list_count(&secure_session_list)) { // Seek & destroy old sessions where close notify have been sent ns_list_foreach_safe(secure_session_t, cur_ptr, &secure_session_list) { - if(cur_ptr->session_state == SECURE_SESSION_CLOSED) { - if((cur_ptr->last_contact_time + CLOSED_SECURE_SESSION_TIMEOUT) <= time){ + if (cur_ptr->session_state == SECURE_SESSION_CLOSED) { + if ((cur_ptr->last_contact_time + CLOSED_SECURE_SESSION_TIMEOUT) <= time) { secure_session_delete(cur_ptr); } - } else if(cur_ptr->session_state == SECURE_SESSION_OK){ - if((cur_ptr->last_contact_time + OPEN_SECURE_SESSION_TIMEOUT) <= time){ + } else if (cur_ptr->session_state == SECURE_SESSION_OK) { + if ((cur_ptr->last_contact_time + OPEN_SECURE_SESSION_TIMEOUT) <= time) { secure_session_delete(cur_ptr); } - } else if(cur_ptr->session_state == SECURE_SESSION_HANDSHAKE_ONGOING){ - if((cur_ptr->last_contact_time + ONGOING_HANDSHAKE_TIMEOUT) <= time){ + } else if (cur_ptr->session_state == SECURE_SESSION_HANDSHAKE_ONGOING) { + if ((cur_ptr->last_contact_time + ONGOING_HANDSHAKE_TIMEOUT) <= time) { secure_session_delete(cur_ptr); } } diff --git a/source/coap_message_handler.c b/source/coap_message_handler.c index be7055b822e..802b75a3c6f 100644 --- a/source/coap_message_handler.c +++ b/source/coap_message_handler.c @@ -55,8 +55,8 @@ static coap_transaction_t *transaction_find_client_by_token(uint8_t *token, uint ns_list_foreach(coap_transaction_t, cur_ptr, &request_list) { if ((cur_ptr->token_len == token_len) && (memcmp(cur_ptr->token, token, token_len) == 0) && cur_ptr->client_request) { - this = cur_ptr; - break; + this = cur_ptr; + break; } } return this; @@ -200,7 +200,7 @@ static int8_t coap_rx_function(sn_coap_hdr_s *resp_ptr, sn_nsdl_addr_s *address_ if (!resp_ptr || !address_ptr) { return -1; } - if(resp_ptr->token_ptr){ + if (resp_ptr->token_ptr) { this = transaction_find_client_by_token(resp_ptr->token_ptr, resp_ptr->token_len, address_ptr->addr_ptr, address_ptr->port); } if (!this) { @@ -216,7 +216,8 @@ static int8_t coap_rx_function(sn_coap_hdr_s *resp_ptr, sn_nsdl_addr_s *address_ } coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void *), - uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *)){ + uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *)) +{ if ((used_malloc_func_ptr == NULL) || (used_free_func_ptr == NULL) || (used_tx_callback_ptr == NULL)) { return NULL; @@ -236,7 +237,7 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint handle->sn_coap_service_malloc = used_malloc_func_ptr; handle->coap = sn_coap_protocol_init(used_malloc_func_ptr, used_free_func_ptr, used_tx_callback_ptr, &coap_rx_function); - if( !handle->coap ){ + if (!handle->coap) { ns_dyn_mem_free(handle); return NULL; } @@ -253,12 +254,13 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint return handle; } -int8_t coap_message_handler_destroy(coap_msg_handler_t *handle){ - if( !handle ){ +int8_t coap_message_handler_destroy(coap_msg_handler_t *handle) +{ + if (!handle) { return -1; } - if( handle->coap ){ + if (handle->coap) { sn_coap_protocol_destroy(handle->coap); } @@ -285,13 +287,14 @@ coap_transaction_t *coap_message_handler_transaction_valid(coap_transaction_t *t coap_transaction_t *coap_message_handler_find_transaction(uint8_t *address_ptr, uint16_t port) { - if( !address_ptr ) + if (!address_ptr) { return NULL; - return transaction_find_by_address( address_ptr, port ); + } + return transaction_find_by_address(address_ptr, port); } int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16], - uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *)) + uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *)) { sn_nsdl_addr_s src_addr; sn_coap_hdr_s *coap_message; @@ -344,10 +347,10 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t } if (cb(socket_id, coap_message, transaction_ptr) < 0) { // negative return value = message ignored -> delete transaction - transaction_delete(transaction_ptr); + transaction_delete(transaction_ptr); } goto exit; - /* Response received */ + /* Response received */ } else { transaction_delete(transaction_ptr); // transaction_ptr not needed in response if (coap_message->token_ptr) { @@ -377,8 +380,8 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t } uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16], - uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, - sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb) + uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, + sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb) { coap_transaction_t *transaction_ptr; sn_coap_hdr_s request; @@ -413,10 +416,10 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se request.uri_path_len = strlen(uri); request.content_format = cont_type; - do{ - randLIB_get_n_bytes_random(token,4); - }while(transaction_find_client_by_token(token, 4, destination_addr, destination_port)); - memcpy(transaction_ptr->token,token,4); + do { + randLIB_get_n_bytes_random(token, 4); + } while (transaction_find_client_by_token(token, 4, destination_addr, destination_port)); + memcpy(transaction_ptr->token, token, 4); transaction_ptr->token_len = 4; request.token_ptr = transaction_ptr->token; request.token_len = 4; @@ -428,7 +431,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se data_len = sn_coap_builder_calc_needed_packet_data_size_2(&request, sn_coap_protocol_get_configured_blockwise_size(handle->coap)); data_ptr = own_alloc(data_len); - if(data_len > 0 && !data_ptr){ + if (data_len > 0 && !data_ptr) { transaction_delete(transaction_ptr); return 0; } @@ -451,11 +454,11 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se // Free allocated data own_free(data_ptr); - if(request.options_list_ptr) { + if (request.options_list_ptr) { own_free(request.options_list_ptr); } - if(request_response_cb == NULL){ + if (request_response_cb == NULL) { //No response expected return 0; } @@ -512,7 +515,7 @@ int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t ser } response = sn_coap_build_response(handle->coap, request_ptr, message_code); - if( !response ){ + if (!response) { return -1; } response->payload_len = payload_len; @@ -522,14 +525,14 @@ int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t ser ret_val = coap_message_handler_resp_build_and_send(handle, response, transaction_ptr); sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, response); - if(ret_val == 0) { + if (ret_val == 0) { transaction_delete(transaction_ptr); } return ret_val; } -int8_t coap_message_handler_response_send_by_msg_id(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, uint16_t msg_id, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len) +int8_t coap_message_handler_response_send_by_msg_id(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, uint16_t msg_id, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len) { sn_coap_hdr_s response; coap_transaction_t *transaction_ptr; @@ -561,7 +564,7 @@ int8_t coap_message_handler_response_send_by_msg_id(coap_msg_handler_t *handle, } ret_val = coap_message_handler_resp_build_and_send(handle, &response, transaction_ptr); - if(ret_val == 0) { + if (ret_val == 0) { transaction_delete(transaction_ptr); } @@ -609,9 +612,10 @@ int8_t coap_message_handler_request_delete_by_service_id(coap_msg_handler_t *han return 0; } -int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time){ +int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time) +{ - if( !handle ){ + if (!handle) { return -1; } diff --git a/source/coap_security_handler.c b/source/coap_security_handler.c index 4271906ba4d..0cd28220bf5 100644 --- a/source/coap_security_handler.c +++ b/source/coap_security_handler.c @@ -84,32 +84,33 @@ static const int PSK_SUITES[] = { #define TRACE_GROUP "CsSh" -static void set_timer( void *sec_obj, uint32_t int_ms, uint32_t fin_ms ); -static int get_timer( void *sec_obj ); +static void set_timer(void *sec_obj, uint32_t int_ms, uint32_t fin_ms); +static int get_timer(void *sec_obj); -int entropy_poll( void *data, unsigned char *output, size_t len, size_t *olen ); +int entropy_poll(void *data, unsigned char *output, size_t len, size_t *olen); //Point these back to M2MConnectionHandler!!! -int f_send( void *ctx, const unsigned char *buf, size_t len ); +int f_send(void *ctx, const unsigned char *buf, size_t len); int f_recv(void *ctx, unsigned char *buf, size_t len); -static int coap_security_handler_init(coap_security_t *sec){ +static int coap_security_handler_init(coap_security_t *sec) +{ const char *pers = "dtls_client"; #ifdef COAP_SERVICE_PROVIDE_STRONG_ENTROPY_SOURCE const int entropy_source_type = MBEDTLS_ENTROPY_SOURCE_STRONG; #else const int entropy_source_type = MBEDTLS_ENTROPY_SOURCE_WEAK; #endif - - mbedtls_ssl_init( &sec->_ssl ); - mbedtls_ssl_config_init( &sec->_conf ); - mbedtls_ctr_drbg_init( &sec->_ctr_drbg ); - mbedtls_entropy_init( &sec->_entropy ); + + mbedtls_ssl_init(&sec->_ssl); + mbedtls_ssl_config_init(&sec->_conf); + mbedtls_ctr_drbg_init(&sec->_ctr_drbg); + mbedtls_entropy_init(&sec->_entropy); #if defined(MBEDTLS_X509_CRT_PARSE_C) - mbedtls_x509_crt_init( &sec->_cacert ); - mbedtls_x509_crt_init( &sec->_owncert ); - mbedtls_pk_init( &sec->_pkey ); + mbedtls_x509_crt_init(&sec->_cacert); + mbedtls_x509_crt_init(&sec->_owncert); + mbedtls_pk_init(&sec->_pkey); #endif memset(&sec->_cookie, 0, sizeof(simple_cookie_t)); @@ -117,15 +118,14 @@ static int coap_security_handler_init(coap_security_t *sec){ sec->_is_started = false; - if( mbedtls_entropy_add_source( &sec->_entropy, entropy_poll, NULL, - 128, entropy_source_type ) < 0 ){ + if (mbedtls_entropy_add_source(&sec->_entropy, entropy_poll, NULL, + 128, entropy_source_type) < 0) { return -1; } - if( ( mbedtls_ctr_drbg_seed( &sec->_ctr_drbg, mbedtls_entropy_func, &sec->_entropy, + if ((mbedtls_ctr_drbg_seed(&sec->_ctr_drbg, mbedtls_entropy_func, &sec->_entropy, (const unsigned char *) pers, - strlen( pers ) ) ) != 0 ) - { + strlen(pers))) != 0) { return -1; } return 0; @@ -141,31 +141,32 @@ const void *coap_security_handler_keyblock(const coap_security_t *sec) return sec->_keyblk.value; } -static void coap_security_handler_reset(coap_security_t *sec){ +static void coap_security_handler_reset(coap_security_t *sec) +{ #if defined(MBEDTLS_X509_CRT_PARSE_C) mbedtls_x509_crt_free(&sec->_cacert); mbedtls_x509_crt_free(&sec->_owncert); mbedtls_pk_free(&sec->_pkey); #endif - mbedtls_entropy_free( &sec->_entropy ); - mbedtls_ctr_drbg_free( &sec->_ctr_drbg ); + mbedtls_entropy_free(&sec->_entropy); + mbedtls_ctr_drbg_free(&sec->_ctr_drbg); mbedtls_ssl_config_free(&sec->_conf); mbedtls_ssl_free(&sec->_ssl); } coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle, SecureConnectionMode mode, - send_cb *socket_cb, - receive_cb *receive_data_cb, - start_timer_cb *timer_start_cb, - timer_status_cb *timer_stat_cb) + send_cb *socket_cb, + receive_cb *receive_data_cb, + start_timer_cb *timer_start_cb, + timer_status_cb *timer_stat_cb) { if (socket_cb == NULL || receive_data_cb == NULL || timer_start_cb == NULL || timer_stat_cb == NULL) { return NULL; } coap_security_t *this = ns_dyn_mem_alloc(sizeof(coap_security_t)); - if( !this ){ + if (!this) { return NULL; } memset(this, 0, sizeof(coap_security_t)); @@ -187,8 +188,9 @@ coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *h return this; } -void coap_security_destroy(coap_security_t *sec){ - if( sec ){ +void coap_security_destroy(coap_security_t *sec) +{ + if (sec) { coap_security_handler_reset(sec); ns_dyn_mem_free(sec); sec = NULL; @@ -259,8 +261,8 @@ static int simple_cookie_check(void *ctx, (void)ilen; if ((p_cookie->len == 0) || - (clen != p_cookie->len) || - (memcmp(cookie, p_cookie->value, p_cookie->len) != 0)) { + (clen != p_cookie->len) || + (memcmp(cookie, p_cookie->value, p_cookie->len) != 0)) { return -1; /* This is what it is in mbedtls... */ } return 0; @@ -290,45 +292,45 @@ static int export_key_block(void *ctx, } #endif -static int coap_security_handler_configure_keys (coap_security_t *sec, coap_security_keys_t keys, bool is_server) +static int coap_security_handler_configure_keys(coap_security_t *sec, coap_security_keys_t keys, bool is_server) { (void) is_server; int ret = -1; - switch( sec->_conn_mode ){ - case CERTIFICATE:{ + switch (sec->_conn_mode) { + case CERTIFICATE: { #if defined(MBEDTLS_X509_CRT_PARSE_C) - if( keys._cert && mbedtls_x509_crt_parse( &sec->_owncert, keys._cert, keys._cert_len ) < 0 ){ - break; - } + if (keys._cert && mbedtls_x509_crt_parse(&sec->_owncert, keys._cert, keys._cert_len) < 0) { + break; + } - if( mbedtls_pk_parse_key(&sec->_pkey, keys._priv_key, keys._priv_key_len, NULL, 0) < 0){ - break; - } + if (mbedtls_pk_parse_key(&sec->_pkey, keys._priv_key, keys._priv_key_len, NULL, 0) < 0) { + break; + } - if (0 != mbedtls_ssl_conf_own_cert(&sec->_conf, &sec->_owncert, &sec->_pkey)) { - break; - } + if (0 != mbedtls_ssl_conf_own_cert(&sec->_conf, &sec->_owncert, &sec->_pkey)) { + break; + } - mbedtls_ssl_conf_authmode( &sec->_conf, MBEDTLS_SSL_VERIFY_NONE ); - mbedtls_ssl_conf_ca_chain( &sec->_conf, &sec->_owncert, NULL ); - ret = 0; + mbedtls_ssl_conf_authmode(&sec->_conf, MBEDTLS_SSL_VERIFY_NONE); + mbedtls_ssl_conf_ca_chain(&sec->_conf, &sec->_owncert, NULL); + ret = 0; #endif - break; + break; } case PSK: { #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) - if( 0 != mbedtls_ssl_conf_psk(&sec->_conf, keys._priv_key, keys._priv_key_len, keys._cert, keys._cert_len) ){ - break; - } - mbedtls_ssl_conf_ciphersuites(&sec->_conf, PSK_SUITES); - ret = 0; + if (0 != mbedtls_ssl_conf_psk(&sec->_conf, keys._priv_key, keys._priv_key_len, keys._cert, keys._cert_len)) { + break; + } + mbedtls_ssl_conf_ciphersuites(&sec->_conf, PSK_SUITES); + ret = 0; #endif - break; + break; } case ECJPAKE: { #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) - if( mbedtls_ssl_set_hs_ecjpake_password(&sec->_ssl, keys._key, keys._key_len) != 0 ){ + if (mbedtls_ssl_set_hs_ecjpake_password(&sec->_ssl, keys._key, keys._key_len) != 0) { return -1; } mbedtls_ssl_conf_ciphersuites(&sec->_conf, ECJPAKE_SUITES); @@ -337,65 +339,62 @@ static int coap_security_handler_configure_keys (coap_security_t *sec, coap_secu mbedtls_ssl_conf_export_keys_cb(&sec->_conf, export_key_block, &sec->_keyblk); - ret = 0; + ret = 0; #endif - break; + break; } default: - break; + break; } return ret; } int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys, uint32_t timeout_min, uint32_t timeout_max) { - if( !sec ){ + if (!sec) { return -1; } sec->_is_blocking = false; int endpoint = MBEDTLS_SSL_IS_CLIENT; - if( is_server ){ + if (is_server) { endpoint = MBEDTLS_SSL_IS_SERVER; } int mode = MBEDTLS_SSL_TRANSPORT_DATAGRAM; - if( sock_mode == TLS ){ + if (sock_mode == TLS) { mode = MBEDTLS_SSL_TRANSPORT_STREAM; } - if( ( mbedtls_ssl_config_defaults( &sec->_conf, - endpoint, - mode, 0 ) ) != 0 ) - { + if ((mbedtls_ssl_config_defaults(&sec->_conf, + endpoint, + mode, 0)) != 0) { return -1; } - if(!timeout_max && !timeout_min){ - mbedtls_ssl_conf_handshake_timeout( &sec->_conf, DTLS_HANDSHAKE_TIMEOUT_MIN, DTLS_HANDSHAKE_TIMEOUT_MAX ); - } - else{ - mbedtls_ssl_conf_handshake_timeout( &sec->_conf, timeout_min, timeout_max ); + if (!timeout_max && !timeout_min) { + mbedtls_ssl_conf_handshake_timeout(&sec->_conf, DTLS_HANDSHAKE_TIMEOUT_MIN, DTLS_HANDSHAKE_TIMEOUT_MAX); + } else { + mbedtls_ssl_conf_handshake_timeout(&sec->_conf, timeout_min, timeout_max); } - mbedtls_ssl_conf_rng( &sec->_conf, mbedtls_ctr_drbg_random, &sec->_ctr_drbg ); + mbedtls_ssl_conf_rng(&sec->_conf, mbedtls_ctr_drbg_random, &sec->_ctr_drbg); - if( ( mbedtls_ssl_setup( &sec->_ssl, &sec->_conf ) ) != 0 ) - { - return -1; + if ((mbedtls_ssl_setup(&sec->_ssl, &sec->_conf)) != 0) { + return -1; } - mbedtls_ssl_set_bio( &sec->_ssl, sec, - f_send, f_recv, NULL ); + mbedtls_ssl_set_bio(&sec->_ssl, sec, + f_send, f_recv, NULL); - mbedtls_ssl_set_timer_cb( &sec->_ssl, sec, set_timer, - get_timer ); + mbedtls_ssl_set_timer_cb(&sec->_ssl, sec, set_timer, + get_timer); #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) //TODO: Figure out better way!!! //Password should never be stored in multiple places!!! - if ((sec->_conn_mode == ECJPAKE) && is_server && keys._key_len > 0){ + if ((sec->_conn_mode == ECJPAKE) && is_server && keys._key_len > 0) { memcpy(sec->_pw, keys._key, keys._key_len); sec->_pw_len = keys._key_len; } @@ -414,49 +413,49 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser mbedtls_ssl_conf_min_version(&sec->_conf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MAJOR_VERSION_3); mbedtls_ssl_conf_max_version(&sec->_conf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MAJOR_VERSION_3); - + sec->_is_started = true; - int ret = mbedtls_ssl_handshake_step( &sec->_ssl ); - if( ret == 0 ){ - ret = mbedtls_ssl_handshake_step( &sec->_ssl ); - if( is_server && 0 == ret){ - ret = coap_security_handler_continue_connecting( sec ); + int ret = mbedtls_ssl_handshake_step(&sec->_ssl); + if (ret == 0) { + ret = mbedtls_ssl_handshake_step(&sec->_ssl); + if (is_server && 0 == ret) { + ret = coap_security_handler_continue_connecting(sec); } } - if( ret >= 0){ + if (ret >= 0) { ret = 1; - }else{ + } else { ret = -1; } return ret; } -int coap_security_handler_continue_connecting(coap_security_t *sec){ +int coap_security_handler_continue_connecting(coap_security_t *sec) +{ int ret = -1; - while( ret != MBEDTLS_ERR_SSL_WANT_READ ){ - ret = mbedtls_ssl_handshake_step( &sec->_ssl ); - if( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED == ret){ + while (ret != MBEDTLS_ERR_SSL_WANT_READ) { + ret = mbedtls_ssl_handshake_step(&sec->_ssl); + if (MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED == ret) { mbedtls_ssl_session_reset(&sec->_ssl); #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) - if( mbedtls_ssl_set_hs_ecjpake_password(&sec->_ssl, sec->_pw, sec->_pw_len) != 0 ){ + if (mbedtls_ssl_set_hs_ecjpake_password(&sec->_ssl, sec->_pw, sec->_pw_len) != 0) { return -1; } #endif return 1; - } - else if(ret && (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)){ + } else if (ret && (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)) { return ret; } - if( sec->_ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER ){ + if (sec->_ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER) { return 0; } } - if(ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE){ + if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) { return 1; } @@ -464,13 +463,15 @@ int coap_security_handler_continue_connecting(coap_security_t *sec){ } -int coap_security_handler_send_message(coap_security_t *sec, unsigned char *message, size_t len){ - int ret=-1; +int coap_security_handler_send_message(coap_security_t *sec, unsigned char *message, size_t len) +{ + int ret = -1; - if( sec ){ - do ret = mbedtls_ssl_write( &sec->_ssl, (unsigned char *) message, len ); - while( ret == MBEDTLS_ERR_SSL_WANT_READ || - ret == MBEDTLS_ERR_SSL_WANT_WRITE ); + if (sec) { + do { + ret = mbedtls_ssl_write(&sec->_ssl, (unsigned char *) message, len); + } while (ret == MBEDTLS_ERR_SSL_WANT_READ || + ret == MBEDTLS_ERR_SSL_WANT_WRITE); } return ret; //bytes written @@ -478,27 +479,28 @@ int coap_security_handler_send_message(coap_security_t *sec, unsigned char *mess int coap_security_send_close_alert(coap_security_t *sec) { - if( !sec ){ + if (!sec) { return -1; } - if(!mbedtls_ssl_close_notify(&sec->_ssl)){ + if (!mbedtls_ssl_close_notify(&sec->_ssl)) { return 0; } return -1; } -int coap_security_handler_read(coap_security_t *sec, unsigned char* buffer, size_t len){ - int ret=-1; +int coap_security_handler_read(coap_security_t *sec, unsigned char *buffer, size_t len) +{ + int ret = -1; int max_loops = 100; - if( sec && buffer ){ - memset( buffer, 0, len ); + if (sec && buffer) { + memset(buffer, 0, len); do { - ret = mbedtls_ssl_read( &sec->_ssl, buffer, len ); - } while( (ret == MBEDTLS_ERR_SSL_WANT_READ || + ret = mbedtls_ssl_read(&sec->_ssl, buffer, len); + } while ((ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) - && --max_loops); + && --max_loops); } return ret; //bytes read } @@ -516,8 +518,8 @@ int coap_security_handler_read(coap_security_t *sec, unsigned char* buffer, size static void set_timer(void *sec_obj, uint32_t int_ms, uint32_t fin_ms) { coap_security_t *sec = (coap_security_t *)sec_obj; - if( sec->_start_timer_cb ){ - sec->_start_timer_cb( sec->_timer_id, int_ms, fin_ms); + if (sec->_start_timer_cb) { + sec->_start_timer_cb(sec->_timer_id, int_ms, fin_ms); } } @@ -531,41 +533,43 @@ static void set_timer(void *sec_obj, uint32_t int_ms, uint32_t fin_ms) static int get_timer(void *sec_obj) { coap_security_t *sec = (coap_security_t *)sec_obj; - if( sec->_timer_status_cb ){ + if (sec->_timer_status_cb) { return sec->_timer_status_cb(sec->_timer_id); } return -1; } -int f_send( void *ctx, const unsigned char *buf, size_t len){ +int f_send(void *ctx, const unsigned char *buf, size_t len) +{ coap_security_t *sec = (coap_security_t *)ctx; return sec->_send_cb(sec->_socket_id, sec->_handle, buf, len); } -int f_recv(void *ctx, unsigned char *buf, size_t len){ +int f_recv(void *ctx, unsigned char *buf, size_t len) +{ coap_security_t *sec = (coap_security_t *)ctx; return sec->_receive_cb(sec->_socket_id, buf, len); } -int entropy_poll( void *ctx, unsigned char *output, size_t len, - size_t *olen ) +int entropy_poll(void *ctx, unsigned char *output, size_t len, + size_t *olen) { (void)ctx; //TODO: change to more secure random randLIB_seed_random(); - char *c = (char*)ns_dyn_mem_temporary_alloc(len); - if( !c ){ + char *c = (char *)ns_dyn_mem_temporary_alloc(len); + if (!c) { return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; } memset(c, 0, len); - for(uint16_t i=0; i < len; i++){ + for (uint16_t i = 0; i < len; i++) { *(c + i) = (char)randLIB_get_8bit(); } memmove(output, c, len); *olen = len; ns_dyn_mem_free(c); - return( 0 ); + return (0); } #endif // COAP_SECURITY_AVAILABLE diff --git a/source/coap_service_api.c b/source/coap_service_api.c index f5c4cc3913f..d39ec701c0d 100644 --- a/source/coap_service_api.c +++ b/source/coap_service_api.c @@ -95,7 +95,7 @@ static coap_service_t *service_find_by_socket(int8_t socket_id) { coap_service_t *this = NULL; ns_list_foreach(coap_service_t, cur_ptr, &instance_list) { - if( coap_connection_handler_socket_belongs_to(cur_ptr->conn_handler, socket_id) ){ + if (coap_connection_handler_socket_belongs_to(cur_ptr->conn_handler, socket_id)) { this = cur_ptr; break; } @@ -177,7 +177,7 @@ static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_ad dest_addr.type = ADDRESS_IPV6; ret_val = coap_connection_handler_send_data(this->conn_handler, &dest_addr, transaction_ptr->local_address, - data_ptr, data_len, (this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS); + data_ptr, data_len, (this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS); if (ret_val == 0) { if (!transaction_ptr->data_ptr) { transaction_ptr->data_ptr = ns_dyn_mem_alloc(data_len); @@ -204,7 +204,7 @@ static void service_event_handler(arm_event_s *event) if (event->event_type == ARM_LIB_SYSTEM_TIMER_EVENT && event->event_id == COAP_TICK_TIMER) { coap_message_handler_exec(coap_service_handle, coap_ticks++); - if(coap_ticks && !(coap_ticks % SECURE_SESSION_CLEAN_INTERVAL)){ + if (coap_ticks && !(coap_ticks % SECURE_SESSION_CLEAN_INTERVAL)) { coap_connection_handler_exec(coap_ticks); } } @@ -224,7 +224,7 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m tr_debug("not registered uri %.*s", coap_message->uri_path_len, coap_message->uri_path_ptr); if (coap_message->msg_type == COAP_MSG_TYPE_CONFIRMABLE) { coap_message_handler_response_send(coap_service_handle, transaction_ptr->service_id, COAP_SERVICE_OPTIONS_NONE, coap_message, - COAP_MSG_CODE_RESPONSE_NOT_FOUND, COAP_CT_NONE, NULL, 0); + COAP_MSG_CODE_RESPONSE_NOT_FOUND, COAP_CT_NONE, NULL, 0); return 0; } return -1; @@ -234,7 +234,7 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m if (uri_reg_ptr && uri_reg_ptr->request_recv_cb) { tr_debug("Service %d, call request recv cb uri %.*s", this->service_id, coap_message->uri_path_len, coap_message->uri_path_ptr); - if ((this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS ) {//TODO Add secure bypass option + if ((this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS) { //TODO Add secure bypass option // Service has secure bypass active TODO this is not defined in interface // this check can be removed I think transaction_ptr->options = COAP_REQUEST_OPTIONS_SECURE_BYPASS; @@ -274,7 +274,7 @@ static int virtual_send_cb(int8_t socket_id, const uint8_t address[static 16], u coap_service_t *this = service_find_by_socket(socket_id); if (this && this->virtual_socket_send_cb) { tr_debug("send to virtual socket, service: %d", this->service_id); - return this->virtual_socket_send_cb(this->service_id, (uint8_t*)address, port, data_ptr, data_len); + return this->virtual_socket_send_cb(this->service_id, (uint8_t *)address, port, data_ptr, data_len); } return -1; } @@ -297,7 +297,7 @@ static void sec_done_cb(int8_t socket_id, uint8_t address[static 16], uint16_t p dest_addr.type = ADDRESS_IPV6; coap_connection_handler_send_data(this->conn_handler, &dest_addr, transaction_ptr->local_address, - transaction_ptr->data_ptr, transaction_ptr->data_len, (this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS); + transaction_ptr->data_ptr, transaction_ptr->data_len, (this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS); ns_dyn_mem_free(transaction_ptr->data_ptr); transaction_ptr->data_ptr = NULL; transaction_ptr->data_len = 0; @@ -339,7 +339,7 @@ static int get_passwd_cb(int8_t socket_id, uint8_t address[static 16], uint16_t } int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options, - coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb) + coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb) { coap_service_t *this = ns_dyn_mem_alloc(sizeof(coap_service_t)); @@ -366,7 +366,7 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_ } this->conn_handler = connection_handler_create(recv_cb, virtual_send_cb, get_passwd_cb, sec_done_cb); - if(!this->conn_handler){ + if (!this->conn_handler) { ns_dyn_mem_free(this); return -1; } @@ -379,10 +379,10 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_ this->conn_handler->registered_to_multicast = this->service_options & COAP_SERVICE_OPTIONS_MULTICAST_JOIN; if (0 > coap_connection_handler_open_connection(this->conn_handler, listen_port, - (this->service_options & COAP_SERVICE_OPTIONS_EPHEMERAL_PORT), - (this->service_options & COAP_SERVICE_OPTIONS_SECURE), - !(this->service_options & COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET), - (this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS))) { + (this->service_options & COAP_SERVICE_OPTIONS_EPHEMERAL_PORT), + (this->service_options & COAP_SERVICE_OPTIONS_SECURE), + !(this->service_options & COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET), + (this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS))) { ns_dyn_mem_free(this->conn_handler); ns_dyn_mem_free(this); return -1; @@ -408,7 +408,7 @@ void coap_service_delete(int8_t service_id) return; } - if (this->conn_handler){ + if (this->conn_handler) { bool leave_multicast_group = false; if (coap_service_can_leave_multicast_group(this->conn_handler)) { // This is the last handler joined to multicast group @@ -435,7 +435,7 @@ extern void coap_service_close_secure_connection(int8_t service_id, uint8_t dest if (!this || !destination_addr_ptr) { return; } - if (this->conn_handler){ + if (this->conn_handler) { connection_handler_close_secure_connection(this->conn_handler, destination_addr_ptr, port); } } @@ -476,7 +476,7 @@ int8_t coap_service_register_uri(int8_t service_id, const char *uri, uint8_t all uri_reg_ptr = uri_registration_find(this, uri, uri_len); if (!uri_reg_ptr) { uri_reg_ptr = ns_dyn_mem_alloc(sizeof(uri_registration_t)); - if( !uri_reg_ptr ){ + if (!uri_reg_ptr) { tr_error("Uri registration failed, OOM"); return -2; } @@ -523,17 +523,20 @@ int8_t coap_service_unregister_uri(int8_t service_id, const char *uri) } uint16_t coap_service_request_send(int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16], uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, - sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb){ + sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb) +{ //TODO: coap_service_response_recv is an ugly cast, this should be refactored away + sn_coap_hdr_s MUST NOT be exposed to users of coap-service! //Callback would be still needed, but where to store callback? return coap_message_handler_request_send(coap_service_handle, service_id, options, destination_addr, destination_port, msg_type, msg_code, uri, cont_type, payload_ptr, payload_len, request_response_cb); } -int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len){ +int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len) +{ return coap_message_handler_response_send(coap_service_handle, service_id, options, request_ptr, message_code, content_type, payload_ptr, payload_len); } -int8_t coap_service_response_send_by_msg_id(int8_t service_id, uint8_t options, uint16_t msg_id, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len) { +int8_t coap_service_response_send_by_msg_id(int8_t service_id, uint8_t options, uint16_t msg_id, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len) +{ return coap_message_handler_response_send_by_msg_id(coap_service_handle, service_id, options, msg_id, message_code, content_type, payload_ptr, payload_len); } @@ -550,7 +553,7 @@ void coap_service_request_delete_by_service_id(int8_t service_id) int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max) { coap_service_t *this = service_find(service_id); - if(!this){ + if (!this) { return -1; } @@ -582,7 +585,7 @@ uint16_t coap_service_id_find_by_socket(int8_t socket_id) { coap_service_t *this = service_find_by_socket(socket_id); - return this ? this->service_id:0; + return this ? this->service_id : 0; } int8_t coap_service_certificate_set(int8_t service_id, const unsigned char *cert, uint16_t cert_len, const unsigned char *priv_key, uint8_t priv_key_len) diff --git a/source/include/coap_connection_handler.h b/source/include/coap_connection_handler.h index 23339527940..bee6f1722c1 100644 --- a/source/include/coap_connection_handler.h +++ b/source/include/coap_connection_handler.h @@ -39,7 +39,7 @@ typedef int receive_from_socket_cb(int8_t socket_id, uint8_t src_address[static typedef int get_pw_cb(int8_t socket_id, uint8_t address[static 16], uint16_t port, coap_security_keys_t *security_ptr); typedef void security_done_cb(int8_t socket_id, uint8_t address[static 16], uint16_t port, uint8_t keyblock[static 40]); -typedef struct coap_conn_handler_s{ +typedef struct coap_conn_handler_s { struct internal_socket_s *socket; coap_security_keys_t *security_keys; @@ -53,13 +53,13 @@ typedef struct coap_conn_handler_s{ } coap_conn_handler_t; coap_conn_handler_t *connection_handler_create(receive_from_socket_cb *recv_from_cb, - send_to_socket_cb *send_to_cb, - get_pw_cb *pw_cb, - security_done_cb *done_cb); + send_to_socket_cb *send_to_cb, + get_pw_cb *pw_cb, + security_done_cb *done_cb); -void connection_handler_destroy( coap_conn_handler_t *handler, bool multicast_group_leave); +void connection_handler_destroy(coap_conn_handler_t *handler, bool multicast_group_leave); -void connection_handler_close_secure_connection( coap_conn_handler_t *handler, uint8_t destination_addr_ptr[static 16], uint16_t port ); +void connection_handler_close_secure_connection(coap_conn_handler_t *handler, uint8_t destination_addr_ptr[static 16], uint16_t port); int coap_connection_handler_open_connection(coap_conn_handler_t *handler, uint16_t listen_port, bool use_ephemeral_port, bool is_secure, bool real_socket, bool bypassSec); diff --git a/source/include/coap_message_handler.h b/source/include/coap_message_handler.h index 377ca400c20..76a13fec9e1 100644 --- a/source/include/coap_message_handler.h +++ b/source/include/coap_message_handler.h @@ -76,7 +76,7 @@ typedef struct coap_transaction { extern coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void *), - uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *)); + uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *)); extern int8_t coap_message_handler_destroy(coap_msg_handler_t *handle); @@ -85,14 +85,14 @@ extern coap_transaction_t *coap_message_handler_transaction_valid(coap_transacti extern coap_transaction_t *coap_message_handler_find_transaction(uint8_t *address_ptr, uint16_t port); extern int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16], - uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *)); + uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *)); extern uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16], - uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, sn_coap_content_format_e cont_type, - const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb); + uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, sn_coap_content_format_e cont_type, + const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb); extern int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, - sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len); + sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len); extern int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t service_id, uint16_t msg_id); @@ -105,6 +105,6 @@ extern void transaction_delete(coap_transaction_t *this); extern void transactions_delete_all(uint8_t *address_ptr, uint16_t port); extern int8_t coap_message_handler_response_send_by_msg_id(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, uint16_t msg_id, sn_coap_msg_code_e message_code, - sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len); + sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len); #endif diff --git a/source/include/coap_security_handler.h b/source/include/coap_security_handler.h index 0a4d948a789..db0a1276d76 100644 --- a/source/include/coap_security_handler.h +++ b/source/include/coap_security_handler.h @@ -55,13 +55,13 @@ typedef int timer_status_cb(int8_t timer_id); typedef enum { DTLS = 0, TLS = 1 -}SecureSocketMode; +} SecureSocketMode; typedef enum { CERTIFICATE, PSK, ECJPAKE -}SecureConnectionMode; +} SecureConnectionMode; typedef struct { SecureConnectionMode mode; @@ -80,11 +80,11 @@ typedef struct coap_security_s coap_security_t; #ifdef COAP_SECURITY_AVAILABLE coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle, - SecureConnectionMode mode, - send_cb *send_cb, - receive_cb *receive_cb, - start_timer_cb *start_timer_cb, - timer_status_cb *timer_status_cb); + SecureConnectionMode mode, + send_cb *send_cb, + receive_cb *receive_cb, + start_timer_cb *start_timer_cb, + timer_status_cb *timer_status_cb); void coap_security_destroy(coap_security_t *sec); @@ -96,7 +96,7 @@ int coap_security_handler_send_message(coap_security_t *sec, unsigned char *mess int coap_security_send_close_alert(coap_security_t *sec); -int coap_security_handler_read(coap_security_t *sec, unsigned char* buffer, size_t len); +int coap_security_handler_read(coap_security_t *sec, unsigned char *buffer, size_t len); bool coap_security_handler_is_started(const coap_security_t *sec); diff --git a/test/coap-service/unittest/coap_connection_handler/test_coap_connection_handler.c b/test/coap-service/unittest/coap_connection_handler/test_coap_connection_handler.c index a7f1c437bca..9a4fc4c6e5d 100644 --- a/test/coap-service/unittest/coap_connection_handler/test_coap_connection_handler.c +++ b/test/coap-service/unittest/coap_connection_handler/test_coap_connection_handler.c @@ -50,16 +50,19 @@ void sec_done_cb_test(int8_t socket_id, uint8_t address[static 16], uint16_t por bool test_connection_handler_create() { coap_security_handler_stub.counter = -1; - if( NULL != connection_handler_create(NULL, NULL, NULL, NULL) ) + if (NULL != connection_handler_create(NULL, NULL, NULL, NULL)) { return false; + } - if( NULL != connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL) ) + if (NULL != connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL)) { return false; + } nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL); - if( NULL == handler ) + if (NULL == handler) { return false; + } ns_dyn_mem_free(handler); return true; } @@ -80,35 +83,42 @@ bool test_coap_connection_handler_open_connection() nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL); - if( -1 != coap_connection_handler_open_connection(NULL, 0,false,false,false,false) ) + if (-1 != coap_connection_handler_open_connection(NULL, 0, false, false, false, false)) { return false; + } - if( -1 != coap_connection_handler_open_connection(handler, 0,false,false,false,false) ) + if (-1 != coap_connection_handler_open_connection(handler, 0, false, false, false, false)) { return false; + } ns_dyn_mem_free(handler); nsdynmemlib_stub.returnCounter = 1; handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); - if( -1 != coap_connection_handler_open_connection(handler, 0,true,true,true,false) ) + if (-1 != coap_connection_handler_open_connection(handler, 0, true, true, true, false)) { return false; + } nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler, 0,true,true,true,false) ) + if (0 != coap_connection_handler_open_connection(handler, 0, true, true, true, false)) { return false; + } nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,true) ) + if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, true)) { return false; + } //open second one nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler2, 22,false,true,true,true) ) + if (0 != coap_connection_handler_open_connection(handler2, 22, false, true, true, true)) { return false; + } - if( 0 != coap_connection_handler_open_connection(handler, 23,false,false,false,false) ) + if (0 != coap_connection_handler_open_connection(handler, 23, false, false, false, false)) { return false; + } connection_handler_destroy(handler2, false); connection_handler_destroy(handler, false); @@ -118,8 +128,9 @@ bool test_coap_connection_handler_open_connection() bool test_coap_connection_handler_send_data() { coap_security_handler_stub.counter = -1; - if( -1 != coap_connection_handler_send_data(NULL, NULL, ns_in6addr_any, NULL, 0, false)) + if (-1 != coap_connection_handler_send_data(NULL, NULL, ns_in6addr_any, NULL, 0, false)) { return false; + } ns_address_t addr; memset(addr.address, 1, 16); @@ -128,11 +139,13 @@ bool test_coap_connection_handler_send_data() nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false) ) + if (0 != coap_connection_handler_open_connection(handler, 22, false, true, false, false)) { return false; + } - if( -1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) + if (-1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) { return false; + } connection_handler_destroy(handler, false); @@ -141,14 +154,17 @@ bool test_coap_connection_handler_send_data() nsdynmemlib_stub.returnCounter = 1; handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); nsdynmemlib_stub.returnCounter = 4; - if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false) ) + if (0 != coap_connection_handler_open_connection(handler, 22, false, true, false, false)) { return false; + } - if( -1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) + if (-1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) { return false; + } - if( -1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) + if (-1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) { return false; + } connection_handler_destroy(handler, false); @@ -159,23 +175,27 @@ bool test_coap_connection_handler_send_data() nsdynmemlib_stub.returnCounter = 1; handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,false,false) ) + if (0 != coap_connection_handler_open_connection(handler, 22, false, false, false, false)) { return false; + } - if( 1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) + if (1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) { return false; - connection_handler_destroy(handler ,false); + } + connection_handler_destroy(handler, false); nsdynmemlib_stub.returnCounter = 1; handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,true,false) ) + if (0 != coap_connection_handler_open_connection(handler, 22, false, false, true, false)) { return false; + } socket_api_stub.int8_value = 7; - if( 1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) + if (1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) { return false; + } connection_handler_destroy(handler, false); //<-- NON SECURE HERE @@ -188,65 +208,77 @@ bool test_coap_connection_handler_virtual_recv() coap_security_handler_stub.counter = -1; uint8_t buf[16]; memset(&buf, 1, 16); - if( -1 != coap_connection_handler_virtual_recv(NULL,buf, 12, NULL, 0) ) + if (-1 != coap_connection_handler_virtual_recv(NULL, buf, 12, NULL, 0)) { return false; + } nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) ) + if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) { return false; + } - if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, NULL, 0) ) + if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, NULL, 0)) { return false; + } nsdynmemlib_stub.returnCounter = 1; - if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, NULL, 0) ) + if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, NULL, 0)) { return false; + } ns_timer_stub.int8_value = 0; nsdynmemlib_stub.returnCounter = 3; - if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) ) + if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, &buf, 1)) { return false; + } //handler->socket->data still in memory coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc(); ns_timer_stub.int8_value = -1; nsdynmemlib_stub.returnCounter = 3; - if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) ) + if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, &buf, 1)) { return false; + } ns_timer_stub.int8_value = 0; nsdynmemlib_stub.returnCounter = 3; - if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) ) + if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, &buf, 1)) { return false; + } connection_handler_destroy(handler, false); nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb_test); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler2, 24,false,true,true,false) ) + if (0 != coap_connection_handler_open_connection(handler2, 24, false, true, true, false)) { return false; + } nsdynmemlib_stub.returnCounter = 3; - if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) ) + if (0 != coap_connection_handler_virtual_recv(handler2, buf, 12, &buf, 1)) { return false; + } nsdynmemlib_stub.returnCounter = 1; coap_security_handler_stub.int_value = 0; - if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) ) + if (0 != coap_connection_handler_virtual_recv(handler2, buf, 12, &buf, 1)) { return false; + } nsdynmemlib_stub.returnCounter = 1; - if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) ) + if (0 != coap_connection_handler_virtual_recv(handler2, buf, 12, &buf, 1)) { return false; + } nsdynmemlib_stub.returnCounter = 1; coap_security_handler_stub.int_value = MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; - if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) ) + if (0 != coap_connection_handler_virtual_recv(handler2, buf, 12, &buf, 1)) { return false; + } connection_handler_destroy(handler2, false); @@ -256,12 +288,14 @@ bool test_coap_connection_handler_virtual_recv() nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler3 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb_test); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler3, 26,false,false,true,false) ) + if (0 != coap_connection_handler_open_connection(handler3, 26, false, false, true, false)) { return false; + } nsdynmemlib_stub.returnCounter = 3; - if( 0 != coap_connection_handler_virtual_recv(handler3,buf, 12, &buf, 1) ) + if (0 != coap_connection_handler_virtual_recv(handler3, buf, 12, &buf, 1)) { return false; + } connection_handler_destroy(handler3, false); @@ -271,21 +305,25 @@ bool test_coap_connection_handler_virtual_recv() bool test_coap_connection_handler_socket_belongs_to() { coap_security_handler_stub.counter = -1; - if( false != coap_connection_handler_socket_belongs_to(NULL, 2) ) + if (false != coap_connection_handler_socket_belongs_to(NULL, 2)) { return false; + } socket_api_stub.int8_value = 0; nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) ) + if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) { return false; + } - if( true != coap_connection_handler_socket_belongs_to(handler, 0) ) + if (true != coap_connection_handler_socket_belongs_to(handler, 0)) { return false; + } - if( false != coap_connection_handler_socket_belongs_to(handler, 3) ) + if (false != coap_connection_handler_socket_belongs_to(handler, 3)) { return false; + } connection_handler_destroy(handler, false); nsdynmemlib_stub.returnCounter = 0; @@ -301,8 +339,9 @@ bool test_timer_callbacks() nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) ) + if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) { return false; + } //handler->socket->data still in memory coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc(); @@ -310,25 +349,28 @@ bool test_timer_callbacks() ns_timer_stub.int8_value = 0; nsdynmemlib_stub.returnCounter = 3; ns_timer_stub.int8_value = 5; - if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) ) + if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, &buf, 1)) { return false; + } //Note next tests will affect ns_timer test (cycle & cycle_count - if( coap_security_handler_stub.start_timer_cb ){ + if (coap_security_handler_stub.start_timer_cb) { coap_security_handler_stub.start_timer_cb(1, 0, 0); coap_security_handler_stub.start_timer_cb(1, 1, 2); } - if( coap_security_handler_stub.timer_status_cb ){ - if( -1 != coap_security_handler_stub.timer_status_cb(4) ) + if (coap_security_handler_stub.timer_status_cb) { + if (-1 != coap_security_handler_stub.timer_status_cb(4)) { return false; + } - if( 0 != coap_security_handler_stub.timer_status_cb(1) ) + if (0 != coap_security_handler_stub.timer_status_cb(1)) { return false; + } } - if( ns_timer_stub.cb ){ + if (ns_timer_stub.cb) { ns_timer_stub.cb(4, 0); ns_timer_stub.cb(5, 0); @@ -360,10 +402,11 @@ bool test_socket_api_callbacks() nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,true,false) ) + if (0 != coap_connection_handler_open_connection(handler, 22, false, false, true, false)) { return false; + } - if( socket_api_stub.recv_cb ){ + if (socket_api_stub.recv_cb) { sckt_data->event_type = SOCKET_DATA; sckt_data->d_len = 1; socket_api_stub.int8_value = -1; @@ -382,10 +425,11 @@ bool test_socket_api_callbacks() nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb_test); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler2, 22,false,true,true,false) ) + if (0 != coap_connection_handler_open_connection(handler2, 22, false, true, true, false)) { return false; + } - if( socket_api_stub.recv_cb ){ + if (socket_api_stub.recv_cb) { nsdynmemlib_stub.returnCounter = 1; socket_api_stub.int8_value = 1; sckt_data->socket_id = 1; @@ -438,10 +482,11 @@ bool test_security_callbacks() nsdynmemlib_stub.returnCounter = 1; coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) ) + if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) { return false; + } - if( socket_api_stub.recv_cb ){ + if (socket_api_stub.recv_cb) { sckt_data->event_type = SOCKET_DATA; sckt_data->d_len = 1; nsdynmemlib_stub.returnCounter = 2; @@ -450,10 +495,10 @@ bool test_security_callbacks() socket_api_stub.recv_cb(sckt_data); } - if( coap_security_handler_stub.send_cb ){ + if (coap_security_handler_stub.send_cb) { coap_security_handler_stub.send_cb(0, buf, 22, &buf, 16); } - if( coap_security_handler_stub.receive_cb ){ + if (coap_security_handler_stub.receive_cb) { coap_security_handler_stub.receive_cb(0, &buf, 16); } diff --git a/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c b/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c index c4953c7e7a4..46dfab8f702 100644 --- a/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c +++ b/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c @@ -30,7 +30,7 @@ int transaction_cb = 0; static void *test_own_alloc(uint16_t size) { - if( retCounter > 0 ){ + if (retCounter > 0) { retCounter--; return malloc(size); } @@ -49,7 +49,8 @@ static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_ad return 0; } -int resp_recv(int8_t service_id, uint16_t msg_id, sn_coap_hdr_s *response_ptr){ +int resp_recv(int8_t service_id, uint16_t msg_id, sn_coap_hdr_s *response_ptr) +{ return retValue; } @@ -66,25 +67,31 @@ static int transaction_recv_cb(int8_t service_id, uint8_t source_address[static bool test_coap_message_handler_init() { - if( NULL != coap_message_handler_init(NULL, NULL, NULL) ) + if (NULL != coap_message_handler_init(NULL, NULL, NULL)) { return false; - if( NULL != coap_message_handler_init(&test_own_alloc, NULL, NULL) ) + } + if (NULL != coap_message_handler_init(&test_own_alloc, NULL, NULL)) { return false; - if( NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, NULL) ) + } + if (NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, NULL)) { return false; - if( NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function) ) + } + if (NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function)) { return false; + } retCounter = 1; sn_coap_protocol_stub.expectedCoap = NULL; - if( NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function) ) + if (NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function)) { return false; + } retCounter = 1; - sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); + sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); nsdynmemlib_stub.returnCounter = 1; coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); - if( NULL == handle ) + if (NULL == handle) { return false; + } free(sn_coap_protocol_stub.expectedCoap); sn_coap_protocol_stub.expectedCoap = NULL; free(handle); @@ -93,16 +100,18 @@ bool test_coap_message_handler_init() bool test_coap_message_handler_destroy() { - if( -1 != coap_message_handler_destroy(NULL) ) + if (-1 != coap_message_handler_destroy(NULL)) { return false; + } retCounter = 1; - sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); + sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); nsdynmemlib_stub.returnCounter = 1; coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); - if( 0 != coap_message_handler_destroy(handle) ) + if (0 != coap_message_handler_destroy(handle)) { return false; + } free(sn_coap_protocol_stub.expectedCoap); return true; @@ -110,10 +119,11 @@ bool test_coap_message_handler_destroy() bool test_coap_message_handler_find_transaction() { - if( NULL != coap_message_handler_find_transaction(NULL, 0)) + if (NULL != coap_message_handler_find_transaction(NULL, 0)) { return false; + } retCounter = 1; - sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); + sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); nsdynmemlib_stub.returnCounter = 1; coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); @@ -127,11 +137,13 @@ bool test_coap_message_handler_find_transaction() sn_coap_builder_stub.expectedUint16 = 1; nsdynmemlib_stub.returnCounter = 3; - if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) + if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) { return false; + } - if( NULL == coap_message_handler_find_transaction(&buf, 24)) + if (NULL == coap_message_handler_find_transaction(&buf, 24)) { return false; + } free(sn_coap_protocol_stub.expectedCoap); sn_coap_protocol_stub.expectedCoap = NULL; @@ -145,27 +157,30 @@ bool test_coap_message_handler_coap_msg_process() memset(&buf, 1, 16); bool ret_val = false; /*Handler is null*/ - if( -1 != coap_message_handler_coap_msg_process(NULL, 0, buf, 22, ns_in6addr_any, NULL, 0, NULL)) + if (-1 != coap_message_handler_coap_msg_process(NULL, 0, buf, 22, ns_in6addr_any, NULL, 0, NULL)) { goto exit; + } retCounter = 1; - sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); + sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); nsdynmemlib_stub.returnCounter = 1; coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); sn_coap_protocol_stub.expectedHeader = NULL; /* Coap parse returns null */ - if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if (-1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) { goto exit; + } sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); sn_coap_protocol_stub.expectedHeader->coap_status = 66; nsdynmemlib_stub.returnCounter = 1; /* Coap library responds */ - if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if (-1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) { goto exit; + } sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); @@ -174,8 +189,9 @@ bool test_coap_message_handler_coap_msg_process() retValue = 0; /* request received */ nsdynmemlib_stub.returnCounter = 1; - if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if (0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) { goto exit; + } sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); @@ -183,8 +199,9 @@ bool test_coap_message_handler_coap_msg_process() sn_coap_protocol_stub.expectedHeader->msg_code = 1; nsdynmemlib_stub.returnCounter = 1; retValue = -1; - if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if (0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) { goto exit; + } sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); @@ -192,8 +209,9 @@ bool test_coap_message_handler_coap_msg_process() sn_coap_protocol_stub.expectedHeader->msg_code = 333; nsdynmemlib_stub.returnCounter = 1; - if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if (-1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) { goto exit; + } sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); @@ -207,13 +225,15 @@ bool test_coap_message_handler_coap_msg_process() sn_coap_builder_stub.expectedUint16 = 1; nsdynmemlib_stub.returnCounter = 3; - if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) + if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) { goto exit; + } sn_coap_protocol_stub.expectedHeader->msg_id = 2; - if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if (-1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) { goto exit; + } ret_val = true; exit: @@ -226,7 +246,7 @@ bool test_coap_message_handler_coap_msg_process() bool test_coap_message_handler_request_send() { retCounter = 1; - sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); + sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); nsdynmemlib_stub.returnCounter = 1; coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); @@ -235,57 +255,68 @@ bool test_coap_message_handler_request_send() memset(&buf, 1, 16); char uri[3] = "rs"; - if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) + if (0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) { return false; + } sn_coap_builder_stub.expectedUint16 = 1; nsdynmemlib_stub.returnCounter = 1; - if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) + if (0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) { return false; + } sn_coap_builder_stub.expectedUint16 = 1; nsdynmemlib_stub.returnCounter = 3; - if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) + if (0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) { return false; + } sn_coap_builder_stub.expectedUint16 = 1; nsdynmemlib_stub.returnCounter = 3; - if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) + if (0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) { return false; + } sn_coap_builder_stub.expectedUint16 = 1; nsdynmemlib_stub.returnCounter = 3; - if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) + if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) { return false; + } /* Clear all transactions */ - if( 0 != coap_message_handler_exec(handle, 0xffffffff)) + if (0 != coap_message_handler_exec(handle, 0xffffffff)) { return false; + } sn_coap_protocol_stub.expectedInt16 = -4; nsdynmemlib_stub.returnCounter = 3; - if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb)) + if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb)) { return false; + } transaction_cb = 0; sn_coap_protocol_stub.expectedInt8 = 0; - if( 0 != coap_message_handler_exec(handle, 12)) + if (0 != coap_message_handler_exec(handle, 12)) { return false; + } - if (transaction_cb != 1) + if (transaction_cb != 1) { return false; + } sn_coap_protocol_stub.expectedInt16 = -2; nsdynmemlib_stub.returnCounter = 3; - if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb)) + if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb)) { return false; + } transaction_cb = 0; - if( 0 != coap_message_handler_exec(handle, 2)) { + if (0 != coap_message_handler_exec(handle, 2)) { return false; } - if (transaction_cb != 1) + if (transaction_cb != 1) { return false; + } free(sn_coap_protocol_stub.expectedCoap); @@ -297,7 +328,7 @@ bool test_coap_message_handler_request_send() bool test_coap_message_handler_request_delete() { retCounter = 1; - sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); + sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); nsdynmemlib_stub.returnCounter = 1; coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); @@ -308,19 +339,23 @@ bool test_coap_message_handler_request_delete() uri[0] = "r"; uri[1] = "s"; uri[2] = "\0"; - if( 0 == coap_message_handler_request_delete(NULL, 1, 1)) + if (0 == coap_message_handler_request_delete(NULL, 1, 1)) { return false; + } - if( 0 == coap_message_handler_request_delete(handle, 1, 1)) + if (0 == coap_message_handler_request_delete(handle, 1, 1)) { return false; + } sn_coap_builder_stub.expectedUint16 = 1; nsdynmemlib_stub.returnCounter = 3; - if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) + if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) { return false; + } - if( 0 != coap_message_handler_request_delete(handle, 1, 2)) + if (0 != coap_message_handler_request_delete(handle, 1, 2)) { return false; + } free(sn_coap_protocol_stub.expectedCoap); sn_coap_protocol_stub.expectedCoap = NULL; @@ -331,7 +366,7 @@ bool test_coap_message_handler_request_delete() bool test_coap_message_handler_request_delete_by_service_id() { retCounter = 1; - sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); + sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); nsdynmemlib_stub.returnCounter = 1; coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); @@ -341,19 +376,23 @@ bool test_coap_message_handler_request_delete_by_service_id() memset(&buf, 1, 16); char uri[3] = "rs"; - if( 0 == coap_message_handler_request_delete_by_service_id(NULL, 1)) + if (0 == coap_message_handler_request_delete_by_service_id(NULL, 1)) { return false; + } - if( 0 != coap_message_handler_request_delete_by_service_id(handle, 1)) + if (0 != coap_message_handler_request_delete_by_service_id(handle, 1)) { return false; + } sn_coap_builder_stub.expectedUint16 = 1; nsdynmemlib_stub.returnCounter = 3; - if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) + if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) { return false; + } - if( 0 != coap_message_handler_request_delete_by_service_id(handle, 3)) + if (0 != coap_message_handler_request_delete_by_service_id(handle, 3)) { return false; + } free(sn_coap_protocol_stub.expectedCoap); sn_coap_protocol_stub.expectedCoap = NULL; @@ -364,19 +403,21 @@ bool test_coap_message_handler_request_delete_by_service_id() bool test_coap_message_handler_response_send() { - if( -1 != coap_message_handler_response_send(NULL, 2, 0, NULL, 1,3,NULL, 0)) + if (-1 != coap_message_handler_response_send(NULL, 2, 0, NULL, 1, 3, NULL, 0)) { return false; + } retCounter = 1; - sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); + sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); nsdynmemlib_stub.returnCounter = 1; coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); sn_coap_hdr_s *header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); memset(header, 0, sizeof(sn_coap_hdr_s)); - if( -2 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) + if (-2 != coap_message_handler_response_send(handle, 2, 0, header, 1, 3, NULL, 0)) { return false; + } uint8_t buf[16]; memset(&buf, 1, 16); @@ -386,30 +427,34 @@ bool test_coap_message_handler_response_send() uri[2] = "\0"; sn_coap_builder_stub.expectedUint16 = 1; nsdynmemlib_stub.returnCounter = 3; - if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) + if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) { return false; + } header->msg_id = 2; sn_coap_builder_stub.expectedUint16 = 2; - coap_transaction_t * tx = coap_message_handler_find_transaction(&buf, 24); - if( tx ){ + coap_transaction_t *tx = coap_message_handler_find_transaction(&buf, 24); + if (tx) { tx->client_request = false; } sn_coap_builder_stub.expectedHeader = NULL; - if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) + if (-1 != coap_message_handler_response_send(handle, 2, 0, header, 1, 3, NULL, 0)) { return false; + } sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); nsdynmemlib_stub.returnCounter = 0; - if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) + if (-1 != coap_message_handler_response_send(handle, 2, 0, header, 1, 3, NULL, 0)) { return false; + } sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); nsdynmemlib_stub.returnCounter = 1; - if( 0 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) + if (0 != coap_message_handler_response_send(handle, 2, 0, header, 1, 3, NULL, 0)) { return false; + } free(header); free(sn_coap_protocol_stub.expectedCoap); @@ -421,52 +466,62 @@ bool test_coap_message_handler_response_send() bool test_coap_message_handler_exec() { /* Null as a parameter */ - if( -1 != coap_message_handler_exec(NULL, 0)) + if (-1 != coap_message_handler_exec(NULL, 0)) { return false; + } retCounter = 1; - sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); + sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); nsdynmemlib_stub.returnCounter = 1; coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); - if( 0 != coap_message_handler_exec(handle, 0)) + if (0 != coap_message_handler_exec(handle, 0)) { return false; + } nsdynmemlib_stub.returnCounter = 1; coap_transaction_t *transact_ptr = transaction_create(); /* Transaction not timed out*/ - if( 0 != coap_message_handler_exec(handle, 0)) + if (0 != coap_message_handler_exec(handle, 0)) { return false; + } - if (transaction_cb != 0) + if (transaction_cb != 0) { return false; + } /* Timed out, no CB */ - if( 0 != coap_message_handler_exec(handle, 300)) + if (0 != coap_message_handler_exec(handle, 300)) { return false; + } - if (transaction_cb != 0) + if (transaction_cb != 0) { return false; + } nsdynmemlib_stub.returnCounter = 1; transact_ptr = transaction_create(); transact_ptr->resp_cb = transaction_recv_cb; /* Transaction not timed out */ - if( 0 != coap_message_handler_exec(handle, 0)) + if (0 != coap_message_handler_exec(handle, 0)) { return false; + } - if (transaction_cb != 0) + if (transaction_cb != 0) { return false; + } /* Transaction timed out */ - if( 0 != coap_message_handler_exec(handle, 300)) + if (0 != coap_message_handler_exec(handle, 300)) { return false; + } - if (transaction_cb == 0) + if (transaction_cb == 0) { return false; + } /* Teardown */ free(sn_coap_protocol_stub.expectedCoap); diff --git a/test/coap-service/unittest/coap_security_handler/test_coap_security_handler.c b/test/coap-service/unittest/coap_security_handler/test_coap_security_handler.c index 2fcca03a744..396d689f8e3 100644 --- a/test/coap-service/unittest/coap_security_handler/test_coap_security_handler.c +++ b/test/coap-service/unittest/coap_security_handler/test_coap_security_handler.c @@ -43,28 +43,33 @@ static int timer_status_callback(int8_t timer_id) bool test_thread_security_create() { - if( NULL != coap_security_create(1,2,NULL,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, NULL) ) + if (NULL != coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, NULL)) { return false; + } - if( NULL != coap_security_create(1,2,NULL,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback) ) + if (NULL != coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback)) { return false; + } nsdynmemlib_stub.returnCounter = 1; mbedtls_stub.expected_int = -1; - if( NULL != coap_security_create(1,2,NULL,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback) ) + if (NULL != coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback)) { return false; + } mbedtls_stub.expected_int = 0; nsdynmemlib_stub.returnCounter = 2; mbedtls_stub.crt_expected_int = -1; - if( NULL != coap_security_create(1,2,NULL,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback) ) + if (NULL != coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback)) { return false; + } nsdynmemlib_stub.returnCounter = 2; mbedtls_stub.crt_expected_int = 0; - coap_security_t *handle = coap_security_create(1,2,NULL,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); - if( NULL == handle ) + coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); + if (NULL == handle) { return false; + } coap_security_destroy(handle); @@ -75,9 +80,10 @@ bool test_thread_security_destroy() { nsdynmemlib_stub.returnCounter = 2; mbedtls_stub.crt_expected_int = 0; - coap_security_t *handle = coap_security_create(1,2,NULL,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); - if( NULL == handle ) + coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); + if (NULL == handle) { return false; + } coap_security_destroy(handle); return true; @@ -87,16 +93,18 @@ bool test_coap_security_handler_connect() { nsdynmemlib_stub.returnCounter = 2; mbedtls_stub.crt_expected_int = 0; - coap_security_t *handle = coap_security_create(1,2,NULL,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); - if( NULL == handle ) + coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); + if (NULL == handle) { return false; + } unsigned char pw = "pwd"; coap_security_keys_t keys; keys._key = &pw; keys._key_len = 3; - if( -1 != coap_security_handler_connect_non_blocking(NULL, true, DTLS, keys, 0, 1) ) + if (-1 != coap_security_handler_connect_non_blocking(NULL, true, DTLS, keys, 0, 1)) { return false; + } mbedtls_stub.useCounter = true; mbedtls_stub.counter = 0; mbedtls_stub.retArray[0] = -1; @@ -108,19 +116,22 @@ bool test_coap_security_handler_connect() mbedtls_stub.retArray[6] = -1; mbedtls_stub.retArray[7] = -1; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) + if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) { return false; + } mbedtls_stub.counter = 0; mbedtls_stub.retArray[0] = 0; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) + if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) { return false; + } mbedtls_stub.counter = 0; // mbedtls_stub.retArray[0] = 0; mbedtls_stub.retArray[1] = 0; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) + if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) { return false; + } simple_cookie_t c; memset(&c, 0, sizeof(simple_cookie_t)); @@ -131,8 +142,9 @@ bool test_coap_security_handler_connect() // mbedtls_stub.retArray[0] = 0; // mbedtls_stub.retArray[1] = 0; mbedtls_stub.retArray[2] = 0; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) + if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) { return false; + } c.len = 8; memset(&c.value, 1, 8); @@ -145,8 +157,9 @@ bool test_coap_security_handler_connect() // mbedtls_stub.retArray[1] = 0; // mbedtls_stub.retArray[2] = 0; mbedtls_stub.retArray[3] = 0; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) + if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) { return false; + } mbedtls_stub.counter = 0; // mbedtls_stub.retArray[0] = 0; @@ -154,8 +167,9 @@ bool test_coap_security_handler_connect() // mbedtls_stub.retArray[2] = 0; // mbedtls_stub.retArray[3] = 0; mbedtls_stub.retArray[4] = 0; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) + if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) { return false; + } mbedtls_stub.counter = 0; // mbedtls_stub.retArray[0] = 0; @@ -165,20 +179,23 @@ bool test_coap_security_handler_connect() // mbedtls_stub.retArray[4] = 0; mbedtls_stub.retArray[6] = 0; mbedtls_stub.retArray[7] = 0; - if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) + if (1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) { return false; + } mbedtls_stub.counter = 0; mbedtls_stub.retArray[5] = MBEDTLS_ERR_SSL_BAD_HS_FINISHED; - if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) + if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) { return false; + } mbedtls_stub.counter = 0; mbedtls_stub.retArray[5] = HANDSHAKE_FINISHED_VALUE; - if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) ) + if (1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) { return false; + } coap_security_destroy(handle); return true; @@ -188,9 +205,10 @@ bool test_coap_security_handler_continue_connecting() { nsdynmemlib_stub.returnCounter = 2; mbedtls_stub.crt_expected_int = 0; - coap_security_t *handle = coap_security_create(1,2,NULL,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); - if( NULL == handle ) + coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); + if (NULL == handle) { return false; + } mbedtls_stub.useCounter = true; mbedtls_stub.counter = 0; @@ -198,34 +216,39 @@ bool test_coap_security_handler_continue_connecting() mbedtls_stub.retArray[1] = -1; mbedtls_stub.retArray[2] = -1; - if( -1 != coap_security_handler_continue_connecting(handle) ) + if (-1 != coap_security_handler_continue_connecting(handle)) { return false; + } mbedtls_stub.counter = 0; mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED; mbedtls_stub.retArray[1] = 0; mbedtls_stub.retArray[2] = 0; - if( 1 != coap_security_handler_continue_connecting(handle) ) + if (1 != coap_security_handler_continue_connecting(handle)) { return false; + } mbedtls_stub.counter = 0; mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_BAD_HS_FINISHED; - if( MBEDTLS_ERR_SSL_BAD_HS_FINISHED != coap_security_handler_continue_connecting(handle) ) + if (MBEDTLS_ERR_SSL_BAD_HS_FINISHED != coap_security_handler_continue_connecting(handle)) { return false; + } mbedtls_stub.counter = 0; mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_WANT_READ; - if( 1 != coap_security_handler_continue_connecting(handle) ) + if (1 != coap_security_handler_continue_connecting(handle)) { return false; + } mbedtls_stub.counter = 0; mbedtls_stub.retArray[0] = HANDSHAKE_FINISHED_VALUE_RETURN_ZERO; - if( 0 != coap_security_handler_continue_connecting(handle) ) + if (0 != coap_security_handler_continue_connecting(handle)) { return false; + } coap_security_destroy(handle); return true; @@ -235,17 +258,20 @@ bool test_coap_security_handler_send_message() { nsdynmemlib_stub.returnCounter = 2; mbedtls_stub.crt_expected_int = 0; - coap_security_t *handle = coap_security_create(1,2,NULL,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); - if( NULL == handle ) + coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); + if (NULL == handle) { return false; + } - if( -1 != coap_security_handler_send_message(NULL, NULL, 0)) + if (-1 != coap_security_handler_send_message(NULL, NULL, 0)) { return false; + } mbedtls_stub.expected_int = 6; unsigned char cbuf[6]; - if( 6 != coap_security_handler_send_message(handle, &cbuf, 6)) + if (6 != coap_security_handler_send_message(handle, &cbuf, 6)) { return false; + } coap_security_destroy(handle); return true; @@ -255,16 +281,19 @@ bool test_thread_security_send_close_alert() { nsdynmemlib_stub.returnCounter = 2; mbedtls_stub.crt_expected_int = 0; - coap_security_t *handle = coap_security_create(1,2,NULL,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); - if( NULL == handle ) + coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); + if (NULL == handle) { return false; + } - if( -1 != coap_security_send_close_alert(NULL)) + if (-1 != coap_security_send_close_alert(NULL)) { return false; + } mbedtls_stub.expected_int = 0; - if( 0 != coap_security_send_close_alert(handle)) + if (0 != coap_security_send_close_alert(handle)) { return false; + } coap_security_destroy(handle); return true; @@ -274,17 +303,20 @@ bool test_coap_security_handler_read() { nsdynmemlib_stub.returnCounter = 2; mbedtls_stub.crt_expected_int = 0; - coap_security_t *handle = coap_security_create(1,2,NULL,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); - if( NULL == handle ) + coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback); + if (NULL == handle) { return false; + } - if( -1 != coap_security_handler_read(NULL, NULL, 0)) + if (-1 != coap_security_handler_read(NULL, NULL, 0)) { return false; + } mbedtls_stub.expected_int = 6; unsigned char cbuf[6]; - if( 6 != coap_security_handler_read(handle, &cbuf, 6)) + if (6 != coap_security_handler_read(handle, &cbuf, 6)) { return false; + } coap_security_destroy(handle); return true; diff --git a/test/coap-service/unittest/coap_service_api/test_coap_service_api.c b/test/coap-service/unittest/coap_service_api/test_coap_service_api.c index 9d558ace7be..54ef40b5a79 100644 --- a/test/coap-service/unittest/coap_service_api/test_coap_service_api.c +++ b/test/coap-service/unittest/coap_service_api/test_coap_service_api.c @@ -25,11 +25,12 @@ #include "net_interface.h" #include "coap_service_api.c" -int sec_done_cb_test(int8_t service_id, uint8_t address[static 16], uint8_t keyblock[static 40]){ +int sec_done_cb_test(int8_t service_id, uint8_t address[static 16], uint8_t keyblock[static 40]) +{ return 2; } -int sec_start_cb(int8_t service_id, uint8_t address[static 16], uint16_t port, uint8_t* pw, uint8_t *pw_len) +int sec_start_cb(int8_t service_id, uint8_t address[static 16], uint16_t port, uint8_t *pw, uint8_t *pw_len) { return 0; } @@ -46,40 +47,45 @@ int virtual_sock_send_cb(int8_t service_id, uint8_t destination_addr_ptr[static bool test_coap_service_initialize() { - if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (-1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } nsdynmemlib_stub.returnCounter = 1; thread_conn_handler_stub.handler_obj = NULL; - if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (-1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); coap_message_handler_stub.coap_ptr = NULL; nsdynmemlib_stub.returnCounter = 1; thread_conn_handler_stub.int_value = -1; - if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL )) - return false; + if (-1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { + return false; + } - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; thread_conn_handler_stub.int_value = 0; - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } nsdynmemlib_stub.returnCounter = 1; - if( 2 != coap_service_initialize(3, 4, 0, NULL, NULL )) + if (2 != coap_service_initialize(3, 4, 0, NULL, NULL)) { return false; + } coap_service_delete(2); coap_service_delete(1); - free( thread_conn_handler_stub.handler_obj ); + free(thread_conn_handler_stub.handler_obj); thread_conn_handler_stub.handler_obj = NULL; return true; } @@ -88,17 +94,18 @@ bool test_coap_service_delete() { coap_service_delete(1); - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; coap_message_handler_stub.coap_ptr = NULL; - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } coap_service_delete(1); - free( thread_conn_handler_stub.handler_obj ); + free(thread_conn_handler_stub.handler_obj); thread_conn_handler_stub.handler_obj = NULL; return true; @@ -107,24 +114,27 @@ bool test_coap_service_delete() bool test_coap_service_virtual_socket_recv() { uint8_t buf[16]; - if( -1 != coap_service_virtual_socket_recv(1, &buf, 10, NULL, 0) ) + if (-1 != coap_service_virtual_socket_recv(1, &buf, 10, NULL, 0)) { return false; + } - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; coap_message_handler_stub.coap_ptr = NULL; - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } thread_conn_handler_stub.int_value = 5; - if( 5 != coap_service_virtual_socket_recv(1, &buf, 10, NULL, 0) ) + if (5 != coap_service_virtual_socket_recv(1, &buf, 10, NULL, 0)) { return false; + } coap_service_delete(1); - free( thread_conn_handler_stub.handler_obj ); + free(thread_conn_handler_stub.handler_obj); thread_conn_handler_stub.handler_obj = NULL; thread_conn_handler_stub.int_value = 0; @@ -134,23 +144,26 @@ bool test_coap_service_virtual_socket_recv() bool test_coap_service_virtual_socket_set_cb() { - if( -1 != coap_service_virtual_socket_set_cb(1, NULL) ) + if (-1 != coap_service_virtual_socket_set_cb(1, NULL)) { return false; + } - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; coap_message_handler_stub.coap_ptr = NULL; - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } - if( 0 != coap_service_virtual_socket_set_cb(1, NULL) ) + if (0 != coap_service_virtual_socket_set_cb(1, NULL)) { return false; + } coap_service_delete(1); - free( thread_conn_handler_stub.handler_obj ); + free(thread_conn_handler_stub.handler_obj); thread_conn_handler_stub.handler_obj = NULL; return true; @@ -158,31 +171,36 @@ bool test_coap_service_virtual_socket_set_cb() bool test_coap_service_register_uri() { - if( -1 != coap_service_register_uri(1, "as", 1, &request_recv_cb)) + if (-1 != coap_service_register_uri(1, "as", 1, &request_recv_cb)) { return false; + } - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; coap_message_handler_stub.coap_ptr = NULL; - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } - if( -2 != coap_service_register_uri(1, "as", 1, &request_recv_cb) ) + if (-2 != coap_service_register_uri(1, "as", 1, &request_recv_cb)) { return false; + } nsdynmemlib_stub.returnCounter = 1; - if( -2 != coap_service_register_uri(1, "as", 1, &request_recv_cb) ) + if (-2 != coap_service_register_uri(1, "as", 1, &request_recv_cb)) { return false; + } nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_service_register_uri(1, "as", 1, &request_recv_cb) ) + if (0 != coap_service_register_uri(1, "as", 1, &request_recv_cb)) { return false; + } coap_service_delete(1); - free( thread_conn_handler_stub.handler_obj ); + free(thread_conn_handler_stub.handler_obj); thread_conn_handler_stub.handler_obj = NULL; return true; @@ -190,31 +208,36 @@ bool test_coap_service_register_uri() bool test_coap_service_unregister_uri() { - if( -1 != coap_service_unregister_uri(1, "as")) + if (-1 != coap_service_unregister_uri(1, "as")) { return false; + } - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; coap_message_handler_stub.coap_ptr = NULL; thread_conn_handler_stub.int_value = 0; - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_service_register_uri(1, "as", 1, &request_recv_cb) ) + if (0 != coap_service_register_uri(1, "as", 1, &request_recv_cb)) { return false; + } - if( -2 != coap_service_unregister_uri(1, "ts") ) + if (-2 != coap_service_unregister_uri(1, "ts")) { return false; + } - if( 0 != coap_service_unregister_uri(1, "as") ) + if (0 != coap_service_unregister_uri(1, "as")) { return false; + } coap_service_delete(1); - free( thread_conn_handler_stub.handler_obj ); + free(thread_conn_handler_stub.handler_obj); thread_conn_handler_stub.handler_obj = NULL; return true; @@ -224,15 +247,17 @@ bool test_coap_service_request_send() { uint8_t buf[16]; coap_message_handler_stub.uint16_value = 6; - if( 6 != coap_service_request_send(0,0,&buf,0,0,0,NULL, 0,NULL,0,NULL)) + if (6 != coap_service_request_send(0, 0, &buf, 0, 0, 0, NULL, 0, NULL, 0, NULL)) { return false; + } return true; } bool test_coap_service_request_delete() { - if( 0 != coap_service_request_delete(NULL,0)) + if (0 != coap_service_request_delete(NULL, 0)) { return false; + } return true; } @@ -246,29 +271,33 @@ bool test_coap_service_response_send() { uint8_t buf[16]; coap_message_handler_stub.int8_value = 6; - if( 6 != coap_service_response_send(0,0,NULL, 65, 0,NULL, 0)) + if (6 != coap_service_response_send(0, 0, NULL, 65, 0, NULL, 0)) { return false; + } return true; } bool test_coap_callbacks() { - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; coap_message_handler_stub.coap_ptr = (coap_msg_handler_t *)malloc(sizeof(coap_msg_handler_t)); memset(coap_message_handler_stub.coap_ptr, 0, sizeof(coap_msg_handler_t)); - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } - if( 0 != coap_message_handler_stub.coap_ptr->sn_coap_service_malloc(0)) + if (0 != coap_message_handler_stub.coap_ptr->sn_coap_service_malloc(0)) { return false; + } nsdynmemlib_stub.returnCounter = 1; void *handle = coap_message_handler_stub.coap_ptr->sn_coap_service_malloc(5); - if( 0 == handle ) + if (0 == handle) { return false; + } coap_message_handler_stub.coap_ptr->sn_coap_service_free(handle); @@ -279,34 +308,38 @@ bool test_coap_callbacks() addr.addr_len = 2; addr.port = 4; addr.addr_ptr = &data; - if( 0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(NULL, 0, &addr, NULL)) + if (0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(NULL, 0, &addr, NULL)) { return false; + } coap_transaction_t *tr = (coap_transaction_t *)malloc(sizeof(coap_transaction_t)); memset(tr, 0, sizeof(coap_transaction_t)); - if( 0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 0, &addr, tr)) + if (0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 0, &addr, tr)) { return false; + } tr->service_id = 1; thread_conn_handler_stub.int_value = -2; - if( 0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 0, &addr, tr)) + if (0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 0, &addr, tr)) { return false; + } nsdynmemlib_stub.returnCounter = 1; - if( 0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 2, &addr, tr)) + if (0 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 2, &addr, tr)) { return false; + } free(tr->data_ptr); free(tr); coap_service_delete(1); - free( coap_message_handler_stub.coap_ptr ); + free(coap_message_handler_stub.coap_ptr); coap_message_handler_stub.coap_ptr = NULL; coap_service_handle = NULL; - free( thread_conn_handler_stub.handler_obj ); + free(thread_conn_handler_stub.handler_obj); thread_conn_handler_stub.handler_obj = NULL; return true; @@ -315,13 +348,14 @@ bool test_coap_callbacks() #define COAP_TICK_TIMER 0xf1 //MUST BE SAME AS IN coap_service_api.c bool test_eventOS_callbacks() { - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } - if( eventOs_event_stub.event_ptr ){ + if (eventOs_event_stub.event_ptr) { arm_event_s event; event.event_type = ARM_LIB_TASKLET_INIT_EVENT; eventOs_event_stub.event_ptr(&event); @@ -332,7 +366,7 @@ bool test_eventOS_callbacks() } coap_service_delete(1); - free( thread_conn_handler_stub.handler_obj ); + free(thread_conn_handler_stub.handler_obj); thread_conn_handler_stub.handler_obj = NULL; return true; } @@ -340,65 +374,75 @@ bool test_eventOS_callbacks() bool test_conn_handler_callbacks() { uint8_t buf[16]; - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; - if( 1 != coap_service_initialize(1, 2, COAP_SERVICE_OPTIONS_SECURE_BYPASS, &sec_start_cb, &sec_done_cb_test )) + if (1 != coap_service_initialize(1, 2, COAP_SERVICE_OPTIONS_SECURE_BYPASS, &sec_start_cb, &sec_done_cb_test)) { return false; + } - if( thread_conn_handler_stub.send_to_sock_cb ){ + if (thread_conn_handler_stub.send_to_sock_cb) { thread_conn_handler_stub.bool_value = true; coap_service_virtual_socket_set_cb(1, &virtual_sock_send_cb); - if( 2 != thread_conn_handler_stub.send_to_sock_cb(1, buf, 12, NULL, 0)) + if (2 != thread_conn_handler_stub.send_to_sock_cb(1, buf, 12, NULL, 0)) { return false; + } thread_conn_handler_stub.bool_value = false; - if( -1 != thread_conn_handler_stub.send_to_sock_cb(1, buf, 12, NULL, 0)) + if (-1 != thread_conn_handler_stub.send_to_sock_cb(1, buf, 12, NULL, 0)) { return false; + } } - if( thread_conn_handler_stub.receive_from_sock_cb ){ + if (thread_conn_handler_stub.receive_from_sock_cb) { coap_message_handler_stub.int16_value = 2; - if( -1 != thread_conn_handler_stub.receive_from_sock_cb(1, buf, 12, NULL, NULL, 0)) + if (-1 != thread_conn_handler_stub.receive_from_sock_cb(1, buf, 12, NULL, NULL, 0)) { return false; + } nsdynmemlib_stub.returnCounter = 1; - uint8_t * ptr = ns_dyn_mem_alloc(5); + uint8_t *ptr = ns_dyn_mem_alloc(5); memset(ptr, 3, 5); nsdynmemlib_stub.returnCounter = 1; - if( 2 != thread_conn_handler_stub.receive_from_sock_cb(1, buf, 12, NULL, ptr, 5)) + if (2 != thread_conn_handler_stub.receive_from_sock_cb(1, buf, 12, NULL, ptr, 5)) { return false; + } ns_dyn_mem_free(ptr); coap_message_handler_stub.int16_value = 0; //This could be moved to own test function, //but thread_conn_handler_stub.receive_from_sock_cb must be called successfully - if( coap_message_handler_stub.cb ){ - if( -1 != coap_message_handler_stub.cb(1, NULL, NULL) ) + if (coap_message_handler_stub.cb) { + if (-1 != coap_message_handler_stub.cb(1, NULL, NULL)) { return false; + } - sn_coap_hdr_s * coap = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); + sn_coap_hdr_s *coap = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); memset(coap, 0, sizeof(sn_coap_hdr_s)); uint8_t uri[2] = "as"; coap->uri_path_ptr = &uri; - coap->uri_path_len=2; + coap->uri_path_len = 2; - if( -1 != coap_message_handler_stub.cb(1, coap, NULL) ) + if (-1 != coap_message_handler_stub.cb(1, coap, NULL)) { return false; + } thread_conn_handler_stub.bool_value = true; nsdynmemlib_stub.returnCounter = 2; - if( 0 != coap_service_register_uri(1, "as", 1, &request_recv_cb) ) + if (0 != coap_service_register_uri(1, "as", 1, &request_recv_cb)) { return false; + } - if( -1 != coap_message_handler_stub.cb(1, coap, NULL) ) + if (-1 != coap_message_handler_stub.cb(1, coap, NULL)) { return false; + } coap_transaction_t *tr = (coap_transaction_t *)malloc(sizeof(coap_transaction_t)); memset(tr, 0, sizeof(coap_transaction_t)); - if( 2 != coap_message_handler_stub.cb(1, coap, tr) ) + if (2 != coap_message_handler_stub.cb(1, coap, tr)) { return false; + } free(tr); tr = NULL; @@ -409,20 +453,22 @@ bool test_conn_handler_callbacks() } } - if(thread_conn_handler_stub.get_passwd_cb){ + if (thread_conn_handler_stub.get_passwd_cb) { coap_security_keys_t security_ptr; memset(&security_ptr, 0, sizeof(coap_security_keys_t)); nsdynmemlib_stub.returnCounter = 1; thread_conn_handler_stub.bool_value = true; - if( 0 != thread_conn_handler_stub.get_passwd_cb(1, buf, 12, &security_ptr)) + if (0 != thread_conn_handler_stub.get_passwd_cb(1, buf, 12, &security_ptr)) { return false; + } free(security_ptr._key); thread_conn_handler_stub.bool_value = false; - if( -1 != thread_conn_handler_stub.get_passwd_cb(1, buf, 12, NULL)) + if (-1 != thread_conn_handler_stub.get_passwd_cb(1, buf, 12, NULL)) { return false; + } } - if(thread_conn_handler_stub.sec_done_cb){ + if (thread_conn_handler_stub.sec_done_cb) { uint8_t block[40]; thread_conn_handler_stub.bool_value = true; @@ -443,7 +489,7 @@ bool test_conn_handler_callbacks() } coap_service_delete(1); - free( thread_conn_handler_stub.handler_obj ); + free(thread_conn_handler_stub.handler_obj); thread_conn_handler_stub.handler_obj = NULL; return true; @@ -457,12 +503,13 @@ bool test_certificate_set() } /* Init service */ - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } /* Allocation fails */ if (-1 != coap_service_certificate_set(1, NULL, 0, NULL, 0)) { @@ -491,12 +538,13 @@ bool test_handshake_timeout_set() } /* Init service */ - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } /* All OK */ nsdynmemlib_stub.returnCounter = 1; @@ -521,17 +569,18 @@ bool test_coap_duplcate_msg_buffer_set() } /* Init service */ - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); coap_message_handler_stub.coap_ptr = (coap_msg_handler_t *)malloc(sizeof(coap_msg_handler_t)); memset(coap_message_handler_stub.coap_ptr, 0, sizeof(coap_msg_handler_t)); nsdynmemlib_stub.returnCounter = 1; - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { ret = false; + } /* All OK */ - if(0 != coap_service_set_duplicate_message_buffer(0, 0)) { + if (0 != coap_service_set_duplicate_message_buffer(0, 0)) { ret = false; } @@ -560,22 +609,23 @@ bool test_coap_service_if_find_by_socket() } /* Init service */ - thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t *)malloc(sizeof(coap_conn_handler_t)); memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); nsdynmemlib_stub.returnCounter = 1; thread_conn_handler_stub.bool_value = 0; - if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL )) + if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) { return false; + } /* No matching service ID - return false */ - if(0 != coap_service_id_find_by_socket(1)) { + if (0 != coap_service_id_find_by_socket(1)) { return false; } thread_conn_handler_stub.bool_value = 1; /* All OK */ - if(1 != coap_service_id_find_by_socket(1)) { + if (1 != coap_service_id_find_by_socket(1)) { return false; } diff --git a/test/coap-service/unittest/stub/coap_connection_handler_stub.c b/test/coap-service/unittest/stub/coap_connection_handler_stub.c index 56da167069e..0ddbee1a2c4 100644 --- a/test/coap-service/unittest/stub/coap_connection_handler_stub.c +++ b/test/coap-service/unittest/stub/coap_connection_handler_stub.c @@ -34,9 +34,9 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a } coap_conn_handler_t *connection_handler_create(int (*recv_cb)(int8_t socket_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *, int), - int (*send_cb)(int8_t socket_id, uint8_t const address[static 16], uint16_t port, const void *, int), - int (*pw_cb)(int8_t socket_id, uint8_t address[static 16], uint16_t port, coap_security_keys_t *security_ptr), - void(*done_cb)(int8_t socket_id, uint8_t address[static 16], uint16_t port, uint8_t keyblock[static KEY_BLOCK_LEN]) ) + int (*send_cb)(int8_t socket_id, uint8_t const address[static 16], uint16_t port, const void *, int), + int (*pw_cb)(int8_t socket_id, uint8_t address[static 16], uint16_t port, coap_security_keys_t *security_ptr), + void(*done_cb)(int8_t socket_id, uint8_t address[static 16], uint16_t port, uint8_t keyblock[static KEY_BLOCK_LEN])) { thread_conn_handler_stub.send_to_sock_cb = send_cb; thread_conn_handler_stub.receive_from_sock_cb = recv_cb; @@ -45,11 +45,11 @@ coap_conn_handler_t *connection_handler_create(int (*recv_cb)(int8_t socket_id, return thread_conn_handler_stub.handler_obj; } -void connection_handler_destroy( coap_conn_handler_t *handler, bool multicast_group_leave) +void connection_handler_destroy(coap_conn_handler_t *handler, bool multicast_group_leave) { } -void connection_handler_close_secure_connection( coap_conn_handler_t *handler, uint8_t destination_addr_ptr[static 16], uint16_t port ) +void connection_handler_close_secure_connection(coap_conn_handler_t *handler, uint8_t destination_addr_ptr[static 16], uint16_t port) { } diff --git a/test/coap-service/unittest/stub/coap_message_handler_stub.c b/test/coap-service/unittest/stub/coap_message_handler_stub.c index 33b4ba22b3f..3c289ee1332 100644 --- a/test/coap-service/unittest/stub/coap_message_handler_stub.c +++ b/test/coap-service/unittest/stub/coap_message_handler_stub.c @@ -20,9 +20,9 @@ coap_message_handler_stub_def coap_message_handler_stub; coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void *), - uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *)) + uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *)) { - if(coap_message_handler_stub.coap_ptr){ + if (coap_message_handler_stub.coap_ptr) { coap_message_handler_stub.coap_ptr->sn_coap_service_malloc = used_malloc_func_ptr; coap_message_handler_stub.coap_ptr->sn_coap_service_free = used_free_func_ptr; coap_message_handler_stub.coap_ptr->sn_coap_tx_callback = used_tx_callback_ptr; @@ -56,20 +56,20 @@ coap_transaction_t *coap_message_handler_find_transaction(uint8_t *address_ptr, } int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16], - uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *)) + uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *)) { coap_message_handler_stub.cb = cb; return coap_message_handler_stub.int16_value; } -uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, - const uint8_t destination_addr[static 16], uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, - const char *uri, sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb) +uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, + const uint8_t destination_addr[static 16], uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, + const char *uri, sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb) { return coap_message_handler_stub.uint16_value; } -int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code,sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len) +int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len) { return coap_message_handler_stub.int8_value; } @@ -90,7 +90,7 @@ int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_ti } int8_t coap_message_handler_response_send_by_msg_id(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, uint16_t msg_id, sn_coap_msg_code_e message_code, - sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len) + sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len) { return coap_message_handler_stub.int8_value; } diff --git a/test/coap-service/unittest/stub/coap_security_handler_stub.c b/test/coap-service/unittest/stub/coap_security_handler_stub.c index c82c32e9ae2..d62e162eed6 100644 --- a/test/coap-service/unittest/stub/coap_security_handler_stub.c +++ b/test/coap-service/unittest/stub/coap_security_handler_stub.c @@ -30,10 +30,10 @@ struct coap_security_s { }; coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle, SecureConnectionMode mode, - int (*send_cb)(int8_t socket_id, void *handle, const void *, size_t), - int (*receive_cb)(int8_t socket_id, unsigned char *, size_t), - void (*start_timer_cb)(int8_t timer_id, uint32_t min, uint32_t fin), - int (*timer_status_cb)(int8_t timer_id)) + int (*send_cb)(int8_t socket_id, void *handle, const void *, size_t), + int (*receive_cb)(int8_t socket_id, unsigned char *, size_t), + void (*start_timer_cb)(int8_t timer_id, uint32_t min, uint32_t fin), + int (*timer_status_cb)(int8_t timer_id)) { coap_security_handler_stub.send_cb = send_cb; coap_security_handler_stub.receive_cb = receive_cb; @@ -56,7 +56,7 @@ void coap_security_destroy(coap_security_t *sec) int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys, uint32_t timeout_min, uint32_t timeout_max) { sec->_is_started = true; - if( coap_security_handler_stub.counter >= 0){ + if (coap_security_handler_stub.counter >= 0) { return coap_security_handler_stub.values[coap_security_handler_stub.counter--]; } return coap_security_handler_stub.int_value; @@ -64,7 +64,7 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser int coap_security_handler_continue_connecting(coap_security_t *sec) { - if( coap_security_handler_stub.counter >= 0){ + if (coap_security_handler_stub.counter >= 0) { return coap_security_handler_stub.values[coap_security_handler_stub.counter--]; } @@ -74,7 +74,7 @@ int coap_security_handler_continue_connecting(coap_security_t *sec) int coap_security_handler_send_message(coap_security_t *sec, unsigned char *message, size_t len) { - if( coap_security_handler_stub.counter >= 0){ + if (coap_security_handler_stub.counter >= 0) { return coap_security_handler_stub.values[coap_security_handler_stub.counter--]; } return coap_security_handler_stub.int_value; @@ -82,15 +82,15 @@ int coap_security_handler_send_message(coap_security_t *sec, unsigned char *mess int coap_security_send_close_alert(coap_security_t *sec) { - if( coap_security_handler_stub.counter >= 0){ + if (coap_security_handler_stub.counter >= 0) { return coap_security_handler_stub.values[coap_security_handler_stub.counter--]; } return coap_security_handler_stub.int_value; } -int coap_security_handler_read(coap_security_t *sec, unsigned char* buffer, size_t len) +int coap_security_handler_read(coap_security_t *sec, unsigned char *buffer, size_t len) { - if( coap_security_handler_stub.counter >= 0){ + if (coap_security_handler_stub.counter >= 0) { return coap_security_handler_stub.values[coap_security_handler_stub.counter--]; } return coap_security_handler_stub.int_value; diff --git a/test/coap-service/unittest/stub/coap_security_handler_stub.h b/test/coap-service/unittest/stub/coap_security_handler_stub.h index edf1ce5aa51..c445c5d5986 100644 --- a/test/coap-service/unittest/stub/coap_security_handler_stub.h +++ b/test/coap-service/unittest/stub/coap_security_handler_stub.h @@ -20,7 +20,7 @@ #include #include "coap_security_handler.h" -typedef struct tsh{ +typedef struct tsh { coap_security_t *sec_obj; int int_value; int counter; diff --git a/test/coap-service/unittest/stub/coap_service_api_stub.c b/test/coap-service/unittest/stub/coap_service_api_stub.c index 81d6b751b25..e2ab1f40747 100644 --- a/test/coap-service/unittest/stub/coap_service_api_stub.c +++ b/test/coap-service/unittest/stub/coap_service_api_stub.c @@ -33,7 +33,7 @@ coap_msg_handler_t *coap_service_handle = NULL; int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options, - coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb) + coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb) { return 0; } @@ -67,7 +67,7 @@ uint16_t coap_service_request_send(int8_t service_id, uint8_t options, const uin return 0; } -int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len) +int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len) { return 0; } diff --git a/test/coap-service/unittest/stub/mbedtls_stub.c b/test/coap-service/unittest/stub/mbedtls_stub.c index 8ee286f8408..f02fba1f9c2 100644 --- a/test/coap-service/unittest/stub/mbedtls_stub.c +++ b/test/coap-service/unittest/stub/mbedtls_stub.c @@ -20,157 +20,179 @@ mbedtls_stub_def mbedtls_stub; //From ssl.h -int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) +int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl) { - if( mbedtls_stub.useCounter ){ + if (mbedtls_stub.useCounter) { - if( mbedtls_stub.retArray[mbedtls_stub.counter] == HANDSHAKE_FINISHED_VALUE || - mbedtls_stub.retArray[mbedtls_stub.counter] == HANDSHAKE_FINISHED_VALUE_RETURN_ZERO){ + if (mbedtls_stub.retArray[mbedtls_stub.counter] == HANDSHAKE_FINISHED_VALUE || + mbedtls_stub.retArray[mbedtls_stub.counter] == HANDSHAKE_FINISHED_VALUE_RETURN_ZERO) { ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; - if(mbedtls_stub.retArray[mbedtls_stub.counter] == HANDSHAKE_FINISHED_VALUE_RETURN_ZERO) + if (mbedtls_stub.retArray[mbedtls_stub.counter] == HANDSHAKE_FINISHED_VALUE_RETURN_ZERO) { return 0; + } } return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } -int mbedtls_ssl_close_notify( mbedtls_ssl_context *a ) +int mbedtls_ssl_close_notify(mbedtls_ssl_context *a) { - if( mbedtls_stub.useCounter ){ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } -void mbedtls_ssl_init( mbedtls_ssl_context *a ){ +void mbedtls_ssl_init(mbedtls_ssl_context *a) +{ } -void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor ){ +void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor) +{ } -void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor ){ - +void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor) +{ + } -void mbedtls_ssl_config_init( mbedtls_ssl_config *a ){ +void mbedtls_ssl_config_init(mbedtls_ssl_config *a) +{ } -void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *a, uint32_t b, uint32_t c) +void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *a, uint32_t b, uint32_t c) { } -void mbedtls_ssl_free( mbedtls_ssl_context *a ){ +void mbedtls_ssl_free(mbedtls_ssl_context *a) +{ } -int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *a, +int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *a, mbedtls_x509_crt *b, - mbedtls_pk_context *c ){ - if( mbedtls_stub.useCounter ){ + mbedtls_pk_context *c) +{ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } -void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *a, int c ){ +void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *a, int c) +{ } -void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *a, +void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *a, mbedtls_x509_crt *b, - mbedtls_x509_crl *c ){ + mbedtls_x509_crl *c) +{ } -int mbedtls_ssl_conf_psk( mbedtls_ssl_config *a, - const unsigned char *b, size_t c, - const unsigned char *d, size_t e ){ - if( mbedtls_stub.useCounter ){ +int mbedtls_ssl_conf_psk(mbedtls_ssl_config *a, + const unsigned char *b, size_t c, + const unsigned char *d, size_t e) +{ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } -int mbedtls_ssl_config_defaults( mbedtls_ssl_config *a, - int b, int c, int d){ - if( mbedtls_stub.useCounter ){ +int mbedtls_ssl_config_defaults(mbedtls_ssl_config *a, + int b, int c, int d) +{ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } -void mbedtls_ssl_conf_rng( mbedtls_ssl_config *a, - int (*f_rng)(void *, unsigned char *, size_t), - void *b ){ +void mbedtls_ssl_conf_rng(mbedtls_ssl_config *a, + int (*f_rng)(void *, unsigned char *, size_t), + void *b) +{ } -void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *a, +void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *a, const int *b) { } -int mbedtls_ssl_setup( mbedtls_ssl_context *a, - const mbedtls_ssl_config *b ){ - if( mbedtls_stub.useCounter ){ +int mbedtls_ssl_setup(mbedtls_ssl_context *a, + const mbedtls_ssl_config *b) +{ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } -void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, - void *p_bio, - int (*f_send)(void *, const unsigned char *, size_t), - int (*f_recv)(void *, unsigned char *, size_t), - int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t) ){ - if( p_bio != NULL ){ - if( f_send ) - f_send( p_bio, NULL, 0 ); - if( f_recv ) - f_recv( p_bio, NULL, 0 ); - if( f_recv_timeout ) - f_recv_timeout( p_bio, NULL, 0, 0 ); +void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl, + void *p_bio, + int (*f_send)(void *, const unsigned char *, size_t), + int (*f_recv)(void *, unsigned char *, size_t), + int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t)) +{ + if (p_bio != NULL) { + if (f_send) { + f_send(p_bio, NULL, 0); + } + if (f_recv) { + f_recv(p_bio, NULL, 0); + } + if (f_recv_timeout) { + f_recv_timeout(p_bio, NULL, 0, 0); + } } } -void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *a, - void *ctx, - void (*f_set_timer)(void *, uint32_t int_ms, uint32_t fin_ms), - int (*f_get_timer)(void *) ){ +void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *a, + void *ctx, + void (*f_set_timer)(void *, uint32_t int_ms, uint32_t fin_ms), + int (*f_get_timer)(void *)) +{ f_set_timer(ctx, 1, 2); f_get_timer(ctx); - if(mbedtls_stub.invalidate_timer){ + if (mbedtls_stub.invalidate_timer) { f_set_timer(ctx, 0, 0); } f_get_timer(ctx); } -int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ){ - if( mbedtls_stub.useCounter ){ +int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl) +{ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } -uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *a ){ +uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *a) +{ return mbedtls_stub.uint32_value; } -int mbedtls_ssl_read( mbedtls_ssl_context *a, unsigned char *b, size_t c){ - if( mbedtls_stub.useCounter ){ +int mbedtls_ssl_read(mbedtls_ssl_context *a, unsigned char *b, size_t c) +{ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } -int mbedtls_ssl_write( mbedtls_ssl_context *a, const unsigned char *b, size_t c ){ - if( mbedtls_stub.useCounter ){ +int mbedtls_ssl_write(mbedtls_ssl_context *a, const unsigned char *b, size_t c) +{ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; @@ -179,144 +201,157 @@ int mbedtls_ssl_write( mbedtls_ssl_context *a, const unsigned char *b, size_t c //From crt_drbg.h -int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *a, - int (*f_entropy)(void *, unsigned char *, size_t), - void *b, - const unsigned char *c, - size_t d ){ +int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context *a, + int (*f_entropy)(void *, unsigned char *, size_t), + void *b, + const unsigned char *c, + size_t d) +{ return mbedtls_stub.crt_expected_int; } -void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *a ){ +void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *a) +{ } -void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *a ){ +void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *a) +{ } -int mbedtls_ctr_drbg_random_with_add( void *a, - unsigned char *b, size_t c, - const unsigned char *d, size_t e ){ +int mbedtls_ctr_drbg_random_with_add(void *a, + unsigned char *b, size_t c, + const unsigned char *d, size_t e) +{ return mbedtls_stub.crt_expected_int; } -int mbedtls_ctr_drbg_random( void *p_rng, - unsigned char *output, size_t output_len ){ +int mbedtls_ctr_drbg_random(void *p_rng, + unsigned char *output, size_t output_len) +{ return mbedtls_stub.crt_expected_int; } //From x509_crt.h -void mbedtls_x509_crt_init( mbedtls_x509_crt *a ){ +void mbedtls_x509_crt_init(mbedtls_x509_crt *a) +{ } -void mbedtls_x509_crt_free( mbedtls_x509_crt *a ){ +void mbedtls_x509_crt_free(mbedtls_x509_crt *a) +{ } -int mbedtls_x509_crt_parse( mbedtls_x509_crt *a, const unsigned char *b, size_t c ){ - if( mbedtls_stub.useCounter ){ +int mbedtls_x509_crt_parse(mbedtls_x509_crt *a, const unsigned char *b, size_t c) +{ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } //From entropy.h -void mbedtls_entropy_init( mbedtls_entropy_context *a ){ +void mbedtls_entropy_init(mbedtls_entropy_context *a) +{ } -void mbedtls_entropy_free( mbedtls_entropy_context *ctx ){ +void mbedtls_entropy_free(mbedtls_entropy_context *ctx) +{ } -int mbedtls_entropy_func( void *a, unsigned char *b, size_t c ){ - if( mbedtls_stub.useCounter ){ +int mbedtls_entropy_func(void *a, unsigned char *b, size_t c) +{ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } -int mbedtls_entropy_add_source( mbedtls_entropy_context *a, - mbedtls_entropy_f_source_ptr f_source, void *b, - size_t c, int d ){ +int mbedtls_entropy_add_source(mbedtls_entropy_context *a, + mbedtls_entropy_f_source_ptr f_source, void *b, + size_t c, int d) +{ unsigned char buf[2]; size_t len; f_source(NULL, buf, 1, &len); - if( mbedtls_stub.useCounter ){ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } //From pk.h -int mbedtls_pk_parse_key( mbedtls_pk_context *a, - const unsigned char *b, size_t c, - const unsigned char *d, size_t e ){ - if( mbedtls_stub.useCounter ){ +int mbedtls_pk_parse_key(mbedtls_pk_context *a, + const unsigned char *b, size_t c, + const unsigned char *d, size_t e) +{ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } -void mbedtls_pk_init( mbedtls_pk_context *ctx ) +void mbedtls_pk_init(mbedtls_pk_context *ctx) { } -void mbedtls_pk_free( mbedtls_pk_context *ctx ) +void mbedtls_pk_free(mbedtls_pk_context *ctx) { } -void mbedtls_ssl_config_free( mbedtls_ssl_config *a) +void mbedtls_ssl_config_free(mbedtls_ssl_config *a) { } -int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, - const unsigned char *pw, - size_t pw_len ) +int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, + const unsigned char *pw, + size_t pw_len) { - if( mbedtls_stub.useCounter ){ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; } -void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf, - mbedtls_ssl_cookie_write_t *f_cookie_write, - mbedtls_ssl_cookie_check_t *f_cookie_check, - void *p_cookie ) +void mbedtls_ssl_conf_dtls_cookies(mbedtls_ssl_config *conf, + mbedtls_ssl_cookie_write_t *f_cookie_write, + mbedtls_ssl_cookie_check_t *f_cookie_check, + void *p_cookie) { - if( mbedtls_stub.cookie_obj && f_cookie_check && mbedtls_stub.cookie_len > 0 ){ + if (mbedtls_stub.cookie_obj && f_cookie_check && mbedtls_stub.cookie_len > 0) { f_cookie_check(mbedtls_stub.cookie_obj, &mbedtls_stub.cookie_value, mbedtls_stub.cookie_len, NULL, 0); } - if( mbedtls_stub.cookie_obj && f_cookie_write && mbedtls_stub.cookie_len > 0 ){ + if (mbedtls_stub.cookie_obj && f_cookie_write && mbedtls_stub.cookie_len > 0) { unsigned char out[16]; unsigned char *ptr = &out; - f_cookie_write(mbedtls_stub.cookie_obj, &ptr, ptr+mbedtls_stub.cookie_len, NULL, 0); + f_cookie_write(mbedtls_stub.cookie_obj, &ptr, ptr + mbedtls_stub.cookie_len, NULL, 0); } } -void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, - mbedtls_ssl_export_keys_t *f_export_keys, - void *p_export_keys ) +void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf, + mbedtls_ssl_export_keys_t *f_export_keys, + void *p_export_keys) { - if( f_export_keys && p_export_keys){ + if (f_export_keys && p_export_keys) { unsigned char value[40]; memset(&value, 1, 40); - f_export_keys(p_export_keys, &value, "", 0, 0,0); //failure case + f_export_keys(p_export_keys, &value, "", 0, 0, 0); //failure case - f_export_keys(p_export_keys, &value, "", 0, 20,0); //success case + f_export_keys(p_export_keys, &value, "", 0, 20, 0); //success case } } -int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) +int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl) { - if( mbedtls_stub.useCounter ){ + if (mbedtls_stub.useCounter) { return mbedtls_stub.retArray[mbedtls_stub.counter++]; } return mbedtls_stub.expected_int; diff --git a/test/coap-service/unittest/stub/nsdynmemLIB_stub.c b/test/coap-service/unittest/stub/nsdynmemLIB_stub.c index c3883d61cce..4228148e490 100644 --- a/test/coap-service/unittest/stub/nsdynmemLIB_stub.c +++ b/test/coap-service/unittest/stub/nsdynmemLIB_stub.c @@ -37,31 +37,25 @@ void ns_dyn_mem_init(void *heap, ns_mem_heap_size_t h_size, void (*passed_fptr)( void *ns_dyn_mem_alloc(ns_mem_block_size_t alloc_size) { - if (nsdynmemlib_stub.returnCounter > 0) - { + if (nsdynmemlib_stub.returnCounter > 0) { nsdynmemlib_stub.returnCounter--; return malloc(alloc_size); - } - else - { - return(nsdynmemlib_stub.expectedPointer); + } else { + return (nsdynmemlib_stub.expectedPointer); } } void *ns_dyn_mem_temporary_alloc(ns_mem_block_size_t alloc_size) { - if (nsdynmemlib_stub.returnCounter > 0) - { + if (nsdynmemlib_stub.returnCounter > 0) { nsdynmemlib_stub.returnCounter--; return malloc(alloc_size); - } - else - { - return(nsdynmemlib_stub.expectedPointer); + } else { + return (nsdynmemlib_stub.expectedPointer); } } void ns_dyn_mem_free(void *block) { - free(block); + free(block); } diff --git a/test/coap-service/unittest/stub/nsdynmemLIB_stub.h b/test/coap-service/unittest/stub/nsdynmemLIB_stub.h index e15fdbff058..e5dfa751865 100644 --- a/test/coap-service/unittest/stub/nsdynmemLIB_stub.h +++ b/test/coap-service/unittest/stub/nsdynmemLIB_stub.h @@ -25,7 +25,7 @@ extern "C" { typedef struct { uint8_t returnCounter; - void* expectedPointer; + void *expectedPointer; } nsdynmemlib_stub_data_t; extern nsdynmemlib_stub_data_t nsdynmemlib_stub; diff --git a/test/coap-service/unittest/stub/randLIB_stub.c b/test/coap-service/unittest/stub/randLIB_stub.c index c52ea94bb46..97154cf2eeb 100644 --- a/test/coap-service/unittest/stub/randLIB_stub.c +++ b/test/coap-service/unittest/stub/randLIB_stub.c @@ -48,8 +48,8 @@ uint32_t randLIB_get_32bit(void) void *randLIB_get_n_bytes_random(void *data_ptr, uint8_t count) { - if(data_ptr && count > 0){ - *((int*)data_ptr) = counter++%255; + if (data_ptr && count > 0) { + *((int *)data_ptr) = counter++ % 255; } return data_ptr; } diff --git a/test/coap-service/unittest/stub/sn_coap_protocol_stub.c b/test/coap-service/unittest/stub/sn_coap_protocol_stub.c index fcab0aa2d85..85ca20e9bb3 100644 --- a/test/coap-service/unittest/stub/sn_coap_protocol_stub.c +++ b/test/coap-service/unittest/stub/sn_coap_protocol_stub.c @@ -49,7 +49,7 @@ struct coap_s *sn_coap_protocol_init(void *(*used_malloc_func_ptr)(uint16_t), vo uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *), int8_t (*used_rx_callback_ptr)(sn_coap_hdr_s *, sn_nsdl_addr_s *, void *param)) { - if( sn_coap_protocol_stub.expectedCoap ){ + if (sn_coap_protocol_stub.expectedCoap) { sn_coap_protocol_stub.expectedCoap->sn_coap_protocol_free = used_free_func_ptr; sn_coap_protocol_stub.expectedCoap->sn_coap_protocol_malloc = used_malloc_func_ptr; sn_coap_protocol_stub.expectedCoap->sn_coap_rx_callback = used_rx_callback_ptr; diff --git a/test/coap-service/unittest/stub/socket_api_stub.c b/test/coap-service/unittest/stub/socket_api_stub.c index d9cb2ebecd1..541a6e11ecb 100644 --- a/test/coap-service/unittest/stub/socket_api_stub.c +++ b/test/coap-service/unittest/stub/socket_api_stub.c @@ -22,7 +22,7 @@ const uint8_t ns_in6addr_any[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, int8_t socket_raw_open(void (*passed_fptr)(void *)) { - if( socket_api_stub.counter >= 0){ + if (socket_api_stub.counter >= 0) { return socket_api_stub.values[socket_api_stub.counter--]; } @@ -31,7 +31,7 @@ int8_t socket_raw_open(void (*passed_fptr)(void *)) int8_t socket_open(uint8_t protocol, uint16_t identifier, void (*passed_fptr)(void *)) { socket_api_stub.recv_cb = passed_fptr; - if( socket_api_stub.counter >= 0){ + if (socket_api_stub.counter >= 0) { return socket_api_stub.values[socket_api_stub.counter--]; } @@ -40,7 +40,7 @@ int8_t socket_open(uint8_t protocol, uint16_t identifier, void (*passed_fptr)(vo int8_t socket_close(int8_t socket) { - if( socket_api_stub.counter >= 0){ + if (socket_api_stub.counter >= 0) { return socket_api_stub.values[socket_api_stub.counter--]; } @@ -48,7 +48,7 @@ int8_t socket_close(int8_t socket) } int8_t socket_listen(int8_t socket, uint8_t backlog) { - if( socket_api_stub.counter >= 0){ + if (socket_api_stub.counter >= 0) { return socket_api_stub.values[socket_api_stub.counter--]; } @@ -56,7 +56,7 @@ int8_t socket_listen(int8_t socket, uint8_t backlog) } int8_t socket_connect(int8_t socket, ns_address_t *address, uint8_t randomly_take_src_number) { - if( socket_api_stub.counter >= 0){ + if (socket_api_stub.counter >= 0) { return socket_api_stub.values[socket_api_stub.counter--]; } @@ -64,7 +64,7 @@ int8_t socket_connect(int8_t socket, ns_address_t *address, uint8_t randomly_tak } int8_t socket_bind(int8_t socket, const ns_address_t *address) { - if( socket_api_stub.counter >= 0){ + if (socket_api_stub.counter >= 0) { return socket_api_stub.values[socket_api_stub.counter--]; } @@ -73,11 +73,11 @@ int8_t socket_bind(int8_t socket, const ns_address_t *address) int16_t socket_read(int8_t socket, ns_address_t *address, uint8_t *buffer, uint16_t length) { - if( address ){ + if (address) { memset(&address->address, 0, 16); address->identifier = 0; } - if( socket_api_stub.counter >= 0){ + if (socket_api_stub.counter >= 0) { return socket_api_stub.values[socket_api_stub.counter--]; } @@ -85,7 +85,7 @@ int16_t socket_read(int8_t socket, ns_address_t *address, uint8_t *buffer, uint1 } int8_t socket_getpeername(int8_t socket, ns_address_t *address) { - if( socket_api_stub.counter >= 0){ + if (socket_api_stub.counter >= 0) { return socket_api_stub.values[socket_api_stub.counter--]; } @@ -93,7 +93,7 @@ int8_t socket_getpeername(int8_t socket, ns_address_t *address) } int8_t socket_setsockopt(int8_t socket, uint8_t level, uint8_t opt_name, const void *opt_value, uint16_t opt_len) { - if( socket_api_stub.counter >= 0){ + if (socket_api_stub.counter >= 0) { return socket_api_stub.values[socket_api_stub.counter--]; } @@ -101,7 +101,7 @@ int8_t socket_setsockopt(int8_t socket, uint8_t level, uint8_t opt_name, const v } int8_t socket_getsockopt(int8_t socket, uint8_t level, uint8_t opt_name, void *opt_value, uint16_t *opt_len) { - if( socket_api_stub.counter >= 0){ + if (socket_api_stub.counter >= 0) { return socket_api_stub.values[socket_api_stub.counter--]; } @@ -109,7 +109,7 @@ int8_t socket_getsockopt(int8_t socket, uint8_t level, uint8_t opt_name, void *o } int16_t socket_sendmsg(int8_t socket, const ns_msghdr_t *msg, int flags) { - if( socket_api_stub.counter >= 0){ + if (socket_api_stub.counter >= 0) { return socket_api_stub.values[socket_api_stub.counter--]; } diff --git a/test/coap-service/unittest/stub/timeout_stub.c b/test/coap-service/unittest/stub/timeout_stub.c index 939b6e6aa6b..fcd63a86fc1 100644 --- a/test/coap-service/unittest/stub/timeout_stub.c +++ b/test/coap-service/unittest/stub/timeout_stub.c @@ -15,7 +15,7 @@ * limitations under the License. */ - #include "eventOS_event_timer.h" +#include "eventOS_event_timer.h" // Timeout structure, already typedefed to timeout_t struct timeout_entry_t {