Merge commit 'e6a851f0a7310462f5f65e9f7955f9fdc71b84f0' into mbedos511

* commit 'e6a851f0a7310462f5f65e9f7955f9fdc71b84f0':
  Squashed 'features/nanostack/coap-service/' changes from bc331ca..c45afcd
pull/8647/head
Arto Kinnunen 2018-11-09 16:40:43 +02:00
commit e1ef0e4347
32 changed files with 959 additions and 740 deletions

View File

@ -230,7 +230,8 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
}
static void clear_secure_sessions(internal_socket_t *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) {
@ -316,13 +317,19 @@ static internal_socket_t *int_socket_create(uint16_t listen_port, bool use_ephem
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));
@ -430,8 +437,9 @@ static int secure_session_sendto(int8_t socket_id, void *handle, const void *buf
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;
}
@ -486,12 +494,10 @@ static void timer_cb(void *param)
if (MBEDTLS_ERR_SSL_TIMEOUT == error) {
//TODO: How do we handle timeouts?
secure_session_delete(sec);
}
else{
} 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;
@ -776,8 +782,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
(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);

View File

@ -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;
@ -253,7 +254,8 @@ 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){
int8_t coap_message_handler_destroy(coap_msg_handler_t *handle)
{
if (!handle) {
return -1;
}
@ -285,8 +287,9 @@ 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);
}
@ -609,7 +612,8 @@ 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) {
return -1;

View File

@ -93,7 +93,8 @@ int entropy_poll( void *data, unsigned char *output, size_t len, size_t *olen );
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;
@ -124,8 +125,7 @@ static int coap_security_handler_init(coap_security_t *sec){
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,7 +141,8 @@ 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);
@ -187,7 +188,8 @@ 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){
void coap_security_destroy(coap_security_t *sec)
{
if (sec) {
coap_security_handler_reset(sec);
ns_dyn_mem_free(sec);
@ -367,22 +369,19 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser
if ((mbedtls_ssl_config_defaults(&sec->_conf,
endpoint,
mode, 0 ) ) != 0 )
{
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{
} 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);
if( ( mbedtls_ssl_setup( &sec->_ssl, &sec->_conf ) ) != 0 )
{
if ((mbedtls_ssl_setup(&sec->_ssl, &sec->_conf)) != 0) {
return -1;
}
@ -433,7 +432,8 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser
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) {
@ -446,8 +446,7 @@ int coap_security_handler_continue_connecting(coap_security_t *sec){
}
#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;
}
@ -464,12 +463,14 @@ 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 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 ||
do {
ret = mbedtls_ssl_write(&sec->_ssl, (unsigned char *) message, len);
} while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
ret == MBEDTLS_ERR_SSL_WANT_WRITE);
}
@ -488,7 +489,8 @@ int coap_security_send_close_alert(coap_security_t *sec)
return -1;
}
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)
{
int ret = -1;
int max_loops = 100;
@ -537,12 +539,14 @@ static int get_timer(void *sec_obj)
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);
}

View File

@ -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);
}

View File

@ -19,12 +19,10 @@
TEST_GROUP(coap_connection_handler)
{
void setup()
{
void setup() {
}
void teardown()
{
void teardown() {
}
};

View File

@ -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);
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,8 +349,9 @@ 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) {
@ -321,12 +361,14 @@ bool test_timer_callbacks()
}
if (coap_security_handler_stub.timer_status_cb) {
if( -1 != coap_security_handler_stub.timer_status_cb(4) )
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) {
ns_timer_stub.cb(4, 0);
@ -360,8 +402,9 @@ 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) {
sckt_data->event_type = SOCKET_DATA;
@ -382,8 +425,9 @@ 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) {
nsdynmemlib_stub.returnCounter = 1;
@ -438,8 +482,9 @@ 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) {
sckt_data->event_type = SOCKET_DATA;

View File

@ -19,12 +19,10 @@
TEST_GROUP(coap_message_handler)
{
void setup()
{
void setup() {
}
void teardown()
{
void teardown() {
}
};

View File

@ -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));
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));
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,8 +119,9 @@ 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));
memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
@ -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,8 +157,9 @@ 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));
@ -156,16 +169,18 @@ bool test_coap_message_handler_coap_msg_process()
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:
@ -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)) {
return false;
}
if (transaction_cb != 1)
if (transaction_cb != 1) {
return false;
}
free(sn_coap_protocol_stub.expectedCoap);
@ -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;
@ -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,8 +403,9 @@ 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));
@ -375,8 +415,9 @@ bool test_coap_message_handler_response_send()
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,8 +427,9 @@ 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;
@ -396,20 +438,23 @@ bool test_coap_message_handler_response_send()
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,8 +466,9 @@ 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));
@ -430,43 +476,52 @@ bool test_coap_message_handler_exec()
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);

View File

@ -21,14 +21,12 @@
TEST_GROUP(coap_security_handler)
{
void setup()
{
void setup() {
nsdynmemlib_stub.returnCounter = 0;
mbedtls_stub.useCounter = false;
}
void teardown()
{
void teardown() {
}
};

View File

@ -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 )
if (NULL == handle) {
return false;
}
coap_security_destroy(handle);
@ -76,8 +81,9 @@ 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 )
if (NULL == handle) {
return false;
}
coap_security_destroy(handle);
return true;
@ -88,15 +94,17 @@ 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 )
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;
@ -189,8 +206,9 @@ 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 )
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;
@ -236,16 +259,19 @@ 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 )
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;
@ -256,15 +282,18 @@ 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 )
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;
@ -275,16 +304,19 @@ 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 )
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;

View File

@ -19,12 +19,10 @@
TEST_GROUP(coap_service_api)
{
void setup()
{
void setup() {
}
void teardown()
{
void teardown() {
}
};

View File

@ -25,7 +25,8 @@
#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;
}
@ -46,14 +47,16 @@ 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));
memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
@ -61,20 +64,23 @@ bool test_coap_service_initialize()
nsdynmemlib_stub.returnCounter = 1;
thread_conn_handler_stub.int_value = -1;
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));
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);
@ -93,8 +99,9 @@ bool test_coap_service_delete()
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);
@ -107,20 +114,23 @@ 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));
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);
@ -134,19 +144,22 @@ 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));
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);
@ -158,27 +171,32 @@ 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));
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);
@ -190,8 +208,9 @@ 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));
memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
@ -199,18 +218,22 @@ bool test_coap_service_unregister_uri()
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);
@ -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,8 +271,9 @@ 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;
}
@ -259,16 +285,19 @@ bool test_coap_callbacks()
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,23 +308,27 @@ 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);
@ -318,8 +351,9 @@ bool test_eventOS_callbacks()
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) {
arm_event_s event;
@ -343,38 +377,44 @@ bool test_conn_handler_callbacks()
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) {
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) {
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);
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 (-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));
memset(coap, 0, sizeof(sn_coap_hdr_s));
@ -383,22 +423,26 @@ bool test_conn_handler_callbacks()
coap->uri_path_ptr = &uri;
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;
@ -414,13 +458,15 @@ bool test_conn_handler_callbacks()
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) {
uint8_t block[40];
@ -461,8 +507,9 @@ bool test_certificate_set()
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)) {
@ -495,8 +542,9 @@ bool test_handshake_timeout_set()
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;
@ -527,8 +575,9 @@ bool test_coap_duplcate_msg_buffer_set()
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)) {
@ -565,8 +614,9 @@ bool test_coap_service_if_find_by_socket()
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)) {

View File

@ -28,9 +28,10 @@ int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
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;
@ -44,18 +45,22 @@ int mbedtls_ssl_close_notify( mbedtls_ssl_context *a )
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)
{
}
@ -64,32 +69,37 @@ void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *a, uint32_t b, uint
}
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,
mbedtls_x509_crt *b,
mbedtls_pk_context *c ){
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,
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 ){
const unsigned char *d, size_t e)
{
if (mbedtls_stub.useCounter) {
return mbedtls_stub.retArray[mbedtls_stub.counter++];
}
@ -97,7 +107,8 @@ int mbedtls_ssl_conf_psk( mbedtls_ssl_config *a,
}
int mbedtls_ssl_config_defaults(mbedtls_ssl_config *a,
int b, int c, int d){
int b, int c, int d)
{
if (mbedtls_stub.useCounter) {
return mbedtls_stub.retArray[mbedtls_stub.counter++];
}
@ -106,7 +117,8 @@ int mbedtls_ssl_config_defaults( mbedtls_ssl_config *a,
void mbedtls_ssl_conf_rng(mbedtls_ssl_config *a,
int (*f_rng)(void *, unsigned char *, size_t),
void *b ){
void *b)
{
}
@ -117,7 +129,8 @@ void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *a,
}
int mbedtls_ssl_setup(mbedtls_ssl_context *a,
const mbedtls_ssl_config *b ){
const mbedtls_ssl_config *b)
{
if (mbedtls_stub.useCounter) {
return mbedtls_stub.retArray[mbedtls_stub.counter++];
}
@ -128,21 +141,26 @@ 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) ){
int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t))
{
if (p_bio != NULL) {
if( f_send )
if (f_send) {
f_send(p_bio, NULL, 0);
if( f_recv )
}
if (f_recv) {
f_recv(p_bio, NULL, 0);
if( f_recv_timeout )
}
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 *) ){
int (*f_get_timer)(void *))
{
f_set_timer(ctx, 1, 2);
f_get_timer(ctx);
if (mbedtls_stub.invalidate_timer) {
@ -151,25 +169,29 @@ void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *a,
f_get_timer(ctx);
}
int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ){
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){
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 ){
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++];
}
@ -183,39 +205,47 @@ 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 ){
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 ){
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 ){
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 ){
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++];
}
@ -223,15 +253,18 @@ int mbedtls_x509_crt_parse( mbedtls_x509_crt *a, const unsigned char *b, size_t
}
//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 ){
int mbedtls_entropy_func(void *a, unsigned char *b, size_t c)
{
if (mbedtls_stub.useCounter) {
return mbedtls_stub.retArray[mbedtls_stub.counter++];
}
@ -240,7 +273,8 @@ int mbedtls_entropy_func( void *a, unsigned char *b, size_t c ){
int mbedtls_entropy_add_source(mbedtls_entropy_context *a,
mbedtls_entropy_f_source_ptr f_source, void *b,
size_t c, int d ){
size_t c, int d)
{
unsigned char buf[2];
size_t len;
f_source(NULL, buf, 1, &len);
@ -254,7 +288,8 @@ int mbedtls_entropy_add_source( mbedtls_entropy_context *a,
//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 ){
const unsigned char *d, size_t e)
{
if (mbedtls_stub.useCounter) {
return mbedtls_stub.retArray[mbedtls_stub.counter++];
}

View File

@ -37,26 +37,20 @@ 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
{
} 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
{
} else {
return (nsdynmemlib_stub.expectedPointer);
}
}