Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os by
test_coap_connection_handler.c
00001 /* 00002 * Copyright (c) 2015-2016 ARM Limited. All Rights Reserved. 00003 */ 00004 #include "test_coap_connection_handler.h" 00005 #include <string.h> 00006 #include <inttypes.h> 00007 #include "nsdynmemLIB_stub.h" 00008 #include "mbedtls/platform.h" 00009 #include "mbedtls/ssl.h" 00010 #include "coap_connection_handler.h" 00011 #include "coap_security_handler_stub.h" 00012 #include "ns_timer_stub.h" 00013 #include "socket_api.h" 00014 #include "socket_api_stub.h" 00015 #include "net_interface.h" 00016 00017 int send_to_sock_cb(int8_t socket_id, uint8_t address, uint16_t port, const unsigned char *a, int b) 00018 { 00019 return 1; 00020 } 00021 00022 int receive_from_sock_cb(int8_t socket_id, uint8_t address, uint16_t port, unsigned char *a, int b) 00023 { 00024 return 1; 00025 } 00026 00027 int get_passwd_cb(int8_t socket_id, uint8_t address, uint16_t port, uint8_t *pw_ptr, uint8_t *pw_len) 00028 { 00029 return 0; 00030 } 00031 00032 void sec_done_cb(int8_t socket_id, uint8_t address[static 16], uint16_t port, uint8_t keyblock[static 40]) 00033 { 00034 return 1; 00035 } 00036 00037 bool test_connection_handler_create() 00038 { 00039 coap_security_handler_stub.counter = -1; 00040 if( NULL != connection_handler_create(NULL, NULL, NULL, NULL) ) 00041 return false; 00042 00043 if( NULL != connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL) ) 00044 return false; 00045 00046 nsdynmemlib_stub.returnCounter = 1; 00047 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL); 00048 if( NULL == handler ) 00049 return false; 00050 ns_dyn_mem_free(handler); 00051 return true; 00052 } 00053 00054 bool test_connection_handler_destroy() 00055 { 00056 coap_security_handler_stub.counter = -1; 00057 nsdynmemlib_stub.returnCounter = 1; 00058 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL); 00059 00060 connection_handler_destroy(handler); 00061 return true; 00062 } 00063 00064 bool test_coap_connection_handler_open_connection() 00065 { 00066 coap_security_handler_stub.counter = -1; 00067 nsdynmemlib_stub.returnCounter = 1; 00068 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL); 00069 00070 if( -1 != coap_connection_handler_open_connection(NULL, 0,false,false,false,false) ) 00071 return false; 00072 00073 if( -1 != coap_connection_handler_open_connection(handler, 0,false,false,false,false) ) 00074 return false; 00075 00076 ns_dyn_mem_free(handler); 00077 nsdynmemlib_stub.returnCounter = 1; 00078 handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00079 if( -1 != coap_connection_handler_open_connection(handler, 0,true,true,true,false) ) 00080 return false; 00081 00082 nsdynmemlib_stub.returnCounter = 2; 00083 if( 0 != coap_connection_handler_open_connection(handler, 0,true,true,true,false) ) 00084 return false; 00085 00086 nsdynmemlib_stub.returnCounter = 2; 00087 if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,true) ) 00088 return false; 00089 00090 //open second one 00091 nsdynmemlib_stub.returnCounter = 1; 00092 coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00093 nsdynmemlib_stub.returnCounter = 2; 00094 if( 0 != coap_connection_handler_open_connection(handler2, 22,false,true,true,true) ) 00095 return false; 00096 00097 if( 0 != coap_connection_handler_open_connection(handler, 23,false,false,false,false) ) 00098 return false; 00099 00100 connection_handler_destroy(handler2); 00101 connection_handler_destroy(handler); 00102 return true; 00103 } 00104 00105 bool test_coap_connection_handler_send_data() 00106 { 00107 coap_security_handler_stub.counter = -1; 00108 if( -1 != coap_connection_handler_send_data(NULL, NULL, NULL, 0, false)) 00109 return false; 00110 00111 ns_address_t addr; 00112 memset(addr.address, 1, 16); 00113 addr.identifier = 22; 00114 00115 nsdynmemlib_stub.returnCounter = 1; 00116 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00117 nsdynmemlib_stub.returnCounter = 2; 00118 if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false) ) 00119 return false; 00120 00121 if( -1 != coap_connection_handler_send_data(handler, &addr, NULL, 0, true)) 00122 return false; 00123 00124 connection_handler_destroy(handler); 00125 00126 coap_security_handler_stub.sec_obj = (coap_security_t *)malloc(sizeof(coap_security_t)); 00127 memset(coap_security_handler_stub.sec_obj, 0, sizeof(coap_security_t)); 00128 coap_security_handler_stub.sec_obj->_remote_port = 22; 00129 memset(coap_security_handler_stub.sec_obj->_remote_address, 1, 16 ); 00130 00131 nsdynmemlib_stub.returnCounter = 1; 00132 handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00133 nsdynmemlib_stub.returnCounter = 4; 00134 if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false) ) 00135 return false; 00136 00137 if( -1 != coap_connection_handler_send_data(handler, &addr, NULL, 0, true)) 00138 return false; 00139 00140 if( -1 != coap_connection_handler_send_data(handler, &addr, NULL, 0, true)) 00141 return false; 00142 00143 connection_handler_destroy(handler); 00144 00145 free(coap_security_handler_stub.sec_obj); 00146 coap_security_handler_stub.sec_obj = NULL; 00147 00148 //NON SECURE HERE --> 00149 nsdynmemlib_stub.returnCounter = 1; 00150 handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00151 nsdynmemlib_stub.returnCounter = 2; 00152 if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,false,false) ) 00153 return false; 00154 00155 00156 if( 1 != coap_connection_handler_send_data(handler, &addr, NULL, 0, true)) 00157 return false; 00158 connection_handler_destroy(handler); 00159 00160 nsdynmemlib_stub.returnCounter = 1; 00161 handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00162 nsdynmemlib_stub.returnCounter = 2; 00163 if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,true,false) ) 00164 return false; 00165 00166 socket_api_stub.int8_value = 7; 00167 if( 7 != coap_connection_handler_send_data(handler, &addr, NULL, 0, true)) 00168 return false; 00169 connection_handler_destroy(handler); 00170 00171 //<-- NON SECURE HERE 00172 00173 return true; 00174 } 00175 00176 bool test_coap_connection_handler_virtual_recv() 00177 { 00178 coap_security_handler_stub.counter = -1; 00179 uint8_t buf[16]; 00180 memset(&buf, 1, 16); 00181 if( -1 != coap_connection_handler_virtual_recv(NULL,buf, 12, NULL, 0) ) 00182 return false; 00183 00184 nsdynmemlib_stub.returnCounter = 1; 00185 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00186 nsdynmemlib_stub.returnCounter = 2; 00187 if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) ) 00188 return false; 00189 00190 if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, NULL, 0) ) 00191 return false; 00192 00193 nsdynmemlib_stub.returnCounter = 1; 00194 if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, NULL, 0) ) 00195 return false; 00196 00197 ns_timer_stub.int8_value = 0; 00198 nsdynmemlib_stub.returnCounter = 3; 00199 if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) ) 00200 return false; 00201 00202 //handler->socket->data still in memory 00203 coap_security_handler_stub.sec_obj = (coap_security_t *)malloc(sizeof(coap_security_t)); 00204 memset(coap_security_handler_stub.sec_obj, 0, sizeof(coap_security_t)); 00205 coap_security_handler_stub.sec_obj->_remote_port = 55; 00206 memset(coap_security_handler_stub.sec_obj->_remote_address, 4, 16 ); 00207 00208 ns_timer_stub.int8_value = -1; 00209 nsdynmemlib_stub.returnCounter = 3; 00210 if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) ) 00211 return false; 00212 00213 ns_timer_stub.int8_value = 0; 00214 nsdynmemlib_stub.returnCounter = 3; 00215 if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) ) 00216 return false; 00217 00218 connection_handler_destroy(handler); 00219 00220 nsdynmemlib_stub.returnCounter = 1; 00221 coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb); 00222 nsdynmemlib_stub.returnCounter = 2; 00223 if( 0 != coap_connection_handler_open_connection(handler2, 24,false,true,true,false) ) 00224 return false; 00225 00226 nsdynmemlib_stub.returnCounter = 3; 00227 if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) ) 00228 return false; 00229 00230 nsdynmemlib_stub.returnCounter = 1; 00231 coap_security_handler_stub.int_value = 0; 00232 coap_security_handler_stub.sec_obj->_remote_port = 12; 00233 memset(coap_security_handler_stub.sec_obj->_remote_address, 1, 16 ); 00234 if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) ) 00235 return false; 00236 00237 nsdynmemlib_stub.returnCounter = 1; 00238 if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) ) 00239 return false; 00240 00241 nsdynmemlib_stub.returnCounter = 1; 00242 coap_security_handler_stub.int_value = MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; 00243 if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) ) 00244 return false; 00245 00246 connection_handler_destroy(handler2); 00247 00248 free(coap_security_handler_stub.sec_obj); 00249 coap_security_handler_stub.sec_obj = NULL; 00250 00251 nsdynmemlib_stub.returnCounter = 1; 00252 coap_conn_handler_t *handler3 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb); 00253 nsdynmemlib_stub.returnCounter = 2; 00254 if( 0 != coap_connection_handler_open_connection(handler3, 26,false,false,true,false) ) 00255 return false; 00256 00257 nsdynmemlib_stub.returnCounter = 3; 00258 if( 0 != coap_connection_handler_virtual_recv(handler3,buf, 12, &buf, 1) ) 00259 return false; 00260 00261 connection_handler_destroy(handler3); 00262 00263 return true; 00264 } 00265 00266 bool test_coap_connection_handler_socket_belongs_to() 00267 { 00268 coap_security_handler_stub.counter = -1; 00269 if( false != coap_connection_handler_socket_belongs_to(NULL, 2) ) 00270 return false; 00271 00272 socket_api_stub.int8_value = 0; 00273 nsdynmemlib_stub.returnCounter = 1; 00274 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00275 nsdynmemlib_stub.returnCounter = 2; 00276 if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) ) 00277 return false; 00278 00279 if( true != coap_connection_handler_socket_belongs_to(handler, 0) ) 00280 return false; 00281 00282 if( false != coap_connection_handler_socket_belongs_to(handler, 3) ) 00283 return false; 00284 connection_handler_destroy(handler); 00285 00286 nsdynmemlib_stub.returnCounter = 0; 00287 return true; 00288 } 00289 00290 bool test_timer_callbacks() 00291 { 00292 coap_security_handler_stub.counter = -1; 00293 uint8_t buf[16]; 00294 memset(&buf, 1, 16); 00295 00296 nsdynmemlib_stub.returnCounter = 1; 00297 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00298 nsdynmemlib_stub.returnCounter = 2; 00299 if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) ) 00300 return false; 00301 00302 //handler->socket->data still in memory 00303 coap_security_handler_stub.sec_obj = (coap_security_t *)malloc(sizeof(coap_security_t)); 00304 memset(coap_security_handler_stub.sec_obj, 0, sizeof(coap_security_t)); 00305 coap_security_handler_stub.sec_obj->_remote_port = 55; 00306 memset(coap_security_handler_stub.sec_obj->_remote_address, 4, 16 ); 00307 coap_security_handler_stub.sec_obj->_timer_id = 5; 00308 00309 ns_timer_stub.int8_value = 0; 00310 nsdynmemlib_stub.returnCounter = 3; 00311 ns_timer_stub.int8_value = 5; 00312 if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) ) 00313 return false; 00314 00315 //Note next tests will affect ns_timer test (cycle & cycle_count 00316 if( coap_security_handler_stub.start_timer_cb ){ 00317 coap_security_handler_stub.start_timer_cb(1, 0, 0); 00318 00319 coap_security_handler_stub.start_timer_cb(1, 1, 2); 00320 } 00321 00322 if( coap_security_handler_stub.timer_status_cb ){ 00323 if( -1 != coap_security_handler_stub.timer_status_cb(4) ) 00324 return false; 00325 00326 if( 0 != coap_security_handler_stub.timer_status_cb(1) ) 00327 return false; 00328 } 00329 00330 if( ns_timer_stub.cb ){ 00331 ns_timer_stub.cb(4, 0); 00332 00333 ns_timer_stub.cb(5, 0); 00334 00335 coap_security_handler_stub.int_value = MBEDTLS_ERR_SSL_TIMEOUT; 00336 00337 ns_timer_stub.cb(5, 0); 00338 coap_security_handler_stub.int_value = 0; 00339 } 00340 00341 connection_handler_destroy(handler); 00342 free(coap_security_handler_stub.sec_obj); 00343 coap_security_handler_stub.sec_obj = NULL; 00344 return true; 00345 } 00346 00347 bool test_socket_api_callbacks() 00348 { 00349 coap_security_handler_stub.counter = -1; 00350 uint8_t buf[16]; 00351 memset(&buf, 1, 16); 00352 00353 socket_callback_t *sckt_data = (socket_callback_t *)malloc(sizeof(socket_callback_t)); 00354 memset(sckt_data, 0, sizeof(socket_callback_t)); 00355 00356 coap_security_handler_stub.sec_obj = (coap_security_t *)malloc(sizeof(coap_security_t)); 00357 memset(coap_security_handler_stub.sec_obj, 0, sizeof(coap_security_t)); 00358 00359 socket_api_stub.int8_value = 0; 00360 nsdynmemlib_stub.returnCounter = 1; 00361 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00362 nsdynmemlib_stub.returnCounter = 2; 00363 if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,true,false) ) 00364 return false; 00365 00366 if( socket_api_stub.recv_cb ){ 00367 sckt_data->event_type = SOCKET_DATA; 00368 sckt_data->d_len = 1; 00369 socket_api_stub.int8_value = -1; 00370 socket_api_stub.recv_cb(sckt_data); 00371 00372 nsdynmemlib_stub.returnCounter = 1; 00373 socket_api_stub.recv_cb(sckt_data); 00374 00375 nsdynmemlib_stub.returnCounter = 1; 00376 socket_api_stub.int8_value = 1; 00377 socket_api_stub.recv_cb(sckt_data); 00378 } 00379 00380 connection_handler_destroy(handler); 00381 00382 nsdynmemlib_stub.returnCounter = 1; 00383 coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb); 00384 nsdynmemlib_stub.returnCounter = 2; 00385 if( 0 != coap_connection_handler_open_connection(handler2, 22,false,true,true,false) ) 00386 return false; 00387 00388 if( socket_api_stub.recv_cb ){ 00389 nsdynmemlib_stub.returnCounter = 1; 00390 socket_api_stub.int8_value = 1; 00391 sckt_data->socket_id = 1; 00392 socket_api_stub.recv_cb(sckt_data); 00393 00394 nsdynmemlib_stub.returnCounter = 2; 00395 socket_api_stub.int8_value = 1; 00396 sckt_data->socket_id = 1; 00397 socket_api_stub.recv_cb(sckt_data); 00398 00399 nsdynmemlib_stub.returnCounter = 1; 00400 socket_api_stub.int8_value = 1; 00401 sckt_data->socket_id = 1; 00402 socket_api_stub.recv_cb(sckt_data); 00403 00404 coap_security_handler_stub.int_value = 4; 00405 nsdynmemlib_stub.returnCounter = 1; 00406 socket_api_stub.int8_value = 1; 00407 sckt_data->socket_id = 1; 00408 socket_api_stub.recv_cb(sckt_data); 00409 00410 coap_security_handler_stub.int_value = MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; 00411 nsdynmemlib_stub.returnCounter = 1; 00412 socket_api_stub.int8_value = 1; 00413 sckt_data->socket_id = 1; 00414 socket_api_stub.recv_cb(sckt_data); 00415 } 00416 00417 connection_handler_destroy(handler2); 00418 00419 free(coap_security_handler_stub.sec_obj); 00420 coap_security_handler_stub.sec_obj = NULL; 00421 00422 free(sckt_data); 00423 sckt_data = NULL; 00424 return true; 00425 } 00426 00427 bool test_security_callbacks() 00428 { 00429 coap_security_handler_stub.counter = -1; 00430 uint8_t buf[16]; 00431 memset(&buf, 1, 16); 00432 00433 socket_callback_t *sckt_data = (socket_callback_t *)malloc(sizeof(socket_callback_t)); 00434 memset(sckt_data, 0, sizeof(socket_callback_t)); 00435 00436 coap_security_handler_stub.sec_obj = (coap_security_t *)malloc(sizeof(coap_security_t)); 00437 memset(coap_security_handler_stub.sec_obj, 0, sizeof(coap_security_t)); 00438 00439 nsdynmemlib_stub.returnCounter = 1; 00440 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00441 nsdynmemlib_stub.returnCounter = 2; 00442 if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) ) 00443 return false; 00444 00445 if( socket_api_stub.recv_cb ){ 00446 sckt_data->event_type = SOCKET_DATA; 00447 sckt_data->d_len = 1; 00448 nsdynmemlib_stub.returnCounter = 2; 00449 socket_api_stub.int8_value = 1; 00450 sckt_data->socket_id = 0; 00451 socket_api_stub.recv_cb(sckt_data); 00452 } 00453 00454 if( coap_security_handler_stub.send_cb ){ 00455 coap_security_handler_stub.send_cb(0, buf, 22, &buf, 16); 00456 } 00457 if( coap_security_handler_stub.receive_cb ){ 00458 coap_security_handler_stub.receive_cb(0, &buf, 16); 00459 } 00460 00461 connection_handler_destroy(handler); 00462 00463 free(coap_security_handler_stub.sec_obj); 00464 coap_security_handler_stub.sec_obj = NULL; 00465 00466 free(sckt_data); 00467 sckt_data = NULL; 00468 return true; 00469 }
Generated on Tue Jul 12 2022 13:16:13 by
