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.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
test_coap_connection_handler.c
00001 /* 00002 * Copyright (c) 2015-2017, Arm Limited and affiliates. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #include "test_coap_connection_handler.h" 00018 #include <string.h> 00019 #include <inttypes.h> 00020 #include "nsdynmemLIB_stub.h" 00021 #include "mbedtls/platform.h" 00022 #include "mbedtls/ssl.h" 00023 #include "coap_connection_handler.h" 00024 #include "coap_security_handler_stub.h" 00025 #include "ns_timer_stub.h" 00026 #include "socket_api.h" 00027 #include "socket_api_stub.h" 00028 #include "net_interface.h" 00029 00030 int send_to_sock_cb(int8_t socket_id, uint8_t address, uint16_t port, const unsigned char *a, int b) 00031 { 00032 return 1; 00033 } 00034 00035 int receive_from_sock_cb(int8_t socket_id, uint8_t address, uint16_t port, unsigned char *a, int b) 00036 { 00037 return 1; 00038 } 00039 00040 int get_passwd_cb(int8_t socket_id, uint8_t address, uint16_t port, uint8_t *pw_ptr, uint8_t *pw_len) 00041 { 00042 return 0; 00043 } 00044 00045 void sec_done_cb_test(int8_t socket_id, uint8_t address[static 16], uint16_t port, uint8_t keyblock[static 40]) 00046 { 00047 return 1; 00048 } 00049 00050 bool test_connection_handler_create() 00051 { 00052 coap_security_handler_stub.counter = -1; 00053 if (NULL != connection_handler_create(NULL, NULL, NULL, NULL)) { 00054 return false; 00055 } 00056 00057 if (NULL != connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL)) { 00058 return false; 00059 } 00060 00061 nsdynmemlib_stub.returnCounter = 1; 00062 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL); 00063 if (NULL == handler) { 00064 return false; 00065 } 00066 ns_dyn_mem_free(handler); 00067 return true; 00068 } 00069 00070 bool test_connection_handler_destroy() 00071 { 00072 coap_security_handler_stub.counter = -1; 00073 nsdynmemlib_stub.returnCounter = 1; 00074 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL); 00075 00076 connection_handler_destroy(handler, false); 00077 return true; 00078 } 00079 00080 bool test_coap_connection_handler_open_connection() 00081 { 00082 coap_security_handler_stub.counter = -1; 00083 nsdynmemlib_stub.returnCounter = 1; 00084 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL); 00085 00086 if (-1 != coap_connection_handler_open_connection(NULL, 0, false, false, false, false)) { 00087 return false; 00088 } 00089 00090 if (-1 != coap_connection_handler_open_connection(handler, 0, false, false, false, false)) { 00091 return false; 00092 } 00093 00094 ns_dyn_mem_free(handler); 00095 nsdynmemlib_stub.returnCounter = 1; 00096 handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00097 if (-1 != coap_connection_handler_open_connection(handler, 0, true, true, true, false)) { 00098 return false; 00099 } 00100 00101 nsdynmemlib_stub.returnCounter = 2; 00102 if (0 != coap_connection_handler_open_connection(handler, 0, true, true, true, false)) { 00103 return false; 00104 } 00105 00106 nsdynmemlib_stub.returnCounter = 2; 00107 if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, true)) { 00108 return false; 00109 } 00110 00111 //open second one 00112 nsdynmemlib_stub.returnCounter = 1; 00113 coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00114 nsdynmemlib_stub.returnCounter = 2; 00115 if (0 != coap_connection_handler_open_connection(handler2, 22, false, true, true, true)) { 00116 return false; 00117 } 00118 00119 if (0 != coap_connection_handler_open_connection(handler, 23, false, false, false, false)) { 00120 return false; 00121 } 00122 00123 connection_handler_destroy(handler2, false); 00124 connection_handler_destroy(handler, false); 00125 return true; 00126 } 00127 00128 bool test_coap_connection_handler_send_data() 00129 { 00130 coap_security_handler_stub.counter = -1; 00131 if (-1 != coap_connection_handler_send_data(NULL, NULL, ns_in6addr_any, NULL, 0, false)) { 00132 return false; 00133 } 00134 00135 ns_address_t addr; 00136 memset(addr.address, 1, 16); 00137 addr.identifier = 22; 00138 00139 nsdynmemlib_stub.returnCounter = 1; 00140 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00141 nsdynmemlib_stub.returnCounter = 2; 00142 if (0 != coap_connection_handler_open_connection(handler, 22, false, true, false, false)) { 00143 return false; 00144 } 00145 00146 if (-1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) { 00147 return false; 00148 } 00149 00150 connection_handler_destroy(handler, false); 00151 00152 coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc(); 00153 00154 nsdynmemlib_stub.returnCounter = 1; 00155 handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00156 nsdynmemlib_stub.returnCounter = 4; 00157 if (0 != coap_connection_handler_open_connection(handler, 22, false, true, false, false)) { 00158 return false; 00159 } 00160 00161 if (-1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) { 00162 return false; 00163 } 00164 00165 if (-1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) { 00166 return false; 00167 } 00168 00169 connection_handler_destroy(handler, false); 00170 00171 free(coap_security_handler_stub.sec_obj); 00172 coap_security_handler_stub.sec_obj = NULL; 00173 00174 //NON SECURE HERE --> 00175 nsdynmemlib_stub.returnCounter = 1; 00176 handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00177 nsdynmemlib_stub.returnCounter = 2; 00178 if (0 != coap_connection_handler_open_connection(handler, 22, false, false, false, false)) { 00179 return false; 00180 } 00181 00182 00183 if (1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) { 00184 return false; 00185 } 00186 connection_handler_destroy(handler, false); 00187 00188 nsdynmemlib_stub.returnCounter = 1; 00189 handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00190 nsdynmemlib_stub.returnCounter = 2; 00191 if (0 != coap_connection_handler_open_connection(handler, 22, false, false, true, false)) { 00192 return false; 00193 } 00194 00195 socket_api_stub.int8_value = 7; 00196 if (1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true)) { 00197 return false; 00198 } 00199 connection_handler_destroy(handler, false); 00200 00201 //<-- NON SECURE HERE 00202 00203 return true; 00204 } 00205 00206 bool test_coap_connection_handler_virtual_recv() 00207 { 00208 coap_security_handler_stub.counter = -1; 00209 uint8_t buf[16]; 00210 memset(&buf, 1, 16); 00211 if (-1 != coap_connection_handler_virtual_recv(NULL, buf, 12, NULL, 0)) { 00212 return false; 00213 } 00214 00215 nsdynmemlib_stub.returnCounter = 1; 00216 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00217 nsdynmemlib_stub.returnCounter = 2; 00218 if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) { 00219 return false; 00220 } 00221 00222 if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, NULL, 0)) { 00223 return false; 00224 } 00225 00226 nsdynmemlib_stub.returnCounter = 1; 00227 if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, NULL, 0)) { 00228 return false; 00229 } 00230 00231 ns_timer_stub.int8_value = 0; 00232 nsdynmemlib_stub.returnCounter = 3; 00233 if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, &buf, 1)) { 00234 return false; 00235 } 00236 00237 //handler->socket->data still in memory 00238 coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc(); 00239 00240 ns_timer_stub.int8_value = -1; 00241 nsdynmemlib_stub.returnCounter = 3; 00242 if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, &buf, 1)) { 00243 return false; 00244 } 00245 00246 ns_timer_stub.int8_value = 0; 00247 nsdynmemlib_stub.returnCounter = 3; 00248 if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, &buf, 1)) { 00249 return false; 00250 } 00251 00252 connection_handler_destroy(handler, false); 00253 00254 nsdynmemlib_stub.returnCounter = 1; 00255 coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb_test); 00256 nsdynmemlib_stub.returnCounter = 2; 00257 if (0 != coap_connection_handler_open_connection(handler2, 24, false, true, true, false)) { 00258 return false; 00259 } 00260 00261 nsdynmemlib_stub.returnCounter = 3; 00262 if (0 != coap_connection_handler_virtual_recv(handler2, buf, 12, &buf, 1)) { 00263 return false; 00264 } 00265 00266 nsdynmemlib_stub.returnCounter = 1; 00267 coap_security_handler_stub.int_value = 0; 00268 if (0 != coap_connection_handler_virtual_recv(handler2, buf, 12, &buf, 1)) { 00269 return false; 00270 } 00271 00272 nsdynmemlib_stub.returnCounter = 1; 00273 if (0 != coap_connection_handler_virtual_recv(handler2, buf, 12, &buf, 1)) { 00274 return false; 00275 } 00276 00277 nsdynmemlib_stub.returnCounter = 1; 00278 coap_security_handler_stub.int_value = MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; 00279 if (0 != coap_connection_handler_virtual_recv(handler2, buf, 12, &buf, 1)) { 00280 return false; 00281 } 00282 00283 connection_handler_destroy(handler2, false); 00284 00285 free(coap_security_handler_stub.sec_obj); 00286 coap_security_handler_stub.sec_obj = NULL; 00287 00288 nsdynmemlib_stub.returnCounter = 1; 00289 coap_conn_handler_t *handler3 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb_test); 00290 nsdynmemlib_stub.returnCounter = 2; 00291 if (0 != coap_connection_handler_open_connection(handler3, 26, false, false, true, false)) { 00292 return false; 00293 } 00294 00295 nsdynmemlib_stub.returnCounter = 3; 00296 if (0 != coap_connection_handler_virtual_recv(handler3, buf, 12, &buf, 1)) { 00297 return false; 00298 } 00299 00300 connection_handler_destroy(handler3, false); 00301 00302 return true; 00303 } 00304 00305 bool test_coap_connection_handler_socket_belongs_to() 00306 { 00307 coap_security_handler_stub.counter = -1; 00308 if (false != coap_connection_handler_socket_belongs_to(NULL, 2)) { 00309 return false; 00310 } 00311 00312 socket_api_stub.int8_value = 0; 00313 nsdynmemlib_stub.returnCounter = 1; 00314 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00315 nsdynmemlib_stub.returnCounter = 2; 00316 if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) { 00317 return false; 00318 } 00319 00320 if (true != coap_connection_handler_socket_belongs_to(handler, 0)) { 00321 return false; 00322 } 00323 00324 if (false != coap_connection_handler_socket_belongs_to(handler, 3)) { 00325 return false; 00326 } 00327 connection_handler_destroy(handler, false); 00328 00329 nsdynmemlib_stub.returnCounter = 0; 00330 return true; 00331 } 00332 00333 bool test_timer_callbacks() 00334 { 00335 coap_security_handler_stub.counter = -1; 00336 uint8_t buf[16]; 00337 memset(&buf, 1, 16); 00338 00339 nsdynmemlib_stub.returnCounter = 1; 00340 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00341 nsdynmemlib_stub.returnCounter = 2; 00342 if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) { 00343 return false; 00344 } 00345 00346 //handler->socket->data still in memory 00347 coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc(); 00348 00349 ns_timer_stub.int8_value = 0; 00350 nsdynmemlib_stub.returnCounter = 3; 00351 ns_timer_stub.int8_value = 5; 00352 if (-1 != coap_connection_handler_virtual_recv(handler, buf, 12, &buf, 1)) { 00353 return false; 00354 } 00355 00356 //Note next tests will affect ns_timer test (cycle & cycle_count 00357 if (coap_security_handler_stub.start_timer_cb) { 00358 coap_security_handler_stub.start_timer_cb(1, 0, 0); 00359 00360 coap_security_handler_stub.start_timer_cb(1, 1, 2); 00361 } 00362 00363 if (coap_security_handler_stub.timer_status_cb) { 00364 if (-1 != coap_security_handler_stub.timer_status_cb(4)) { 00365 return false; 00366 } 00367 00368 if (0 != coap_security_handler_stub.timer_status_cb(1)) { 00369 return false; 00370 } 00371 } 00372 00373 if (ns_timer_stub.cb) { 00374 ns_timer_stub.cb(4, 0); 00375 00376 ns_timer_stub.cb(5, 0); 00377 00378 coap_security_handler_stub.int_value = MBEDTLS_ERR_SSL_TIMEOUT; 00379 00380 ns_timer_stub.cb(5, 0); 00381 coap_security_handler_stub.int_value = 0; 00382 } 00383 00384 connection_handler_destroy(handler, false); 00385 free(coap_security_handler_stub.sec_obj); 00386 coap_security_handler_stub.sec_obj = NULL; 00387 return true; 00388 } 00389 00390 bool test_socket_api_callbacks() 00391 { 00392 coap_security_handler_stub.counter = -1; 00393 uint8_t buf[16]; 00394 memset(&buf, 1, 16); 00395 00396 socket_callback_t *sckt_data = (socket_callback_t *)malloc(sizeof(socket_callback_t)); 00397 memset(sckt_data, 0, sizeof(socket_callback_t)); 00398 00399 coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc(); 00400 00401 socket_api_stub.int8_value = 0; 00402 nsdynmemlib_stub.returnCounter = 1; 00403 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00404 nsdynmemlib_stub.returnCounter = 2; 00405 if (0 != coap_connection_handler_open_connection(handler, 22, false, false, true, false)) { 00406 free(sckt_data); 00407 return false; 00408 } 00409 00410 if (socket_api_stub.recv_cb) { 00411 sckt_data->event_type = SOCKET_DATA; 00412 sckt_data->d_len = 1; 00413 socket_api_stub.int8_value = -1; 00414 socket_api_stub.recv_cb(sckt_data); 00415 00416 nsdynmemlib_stub.returnCounter = 1; 00417 socket_api_stub.recv_cb(sckt_data); 00418 00419 nsdynmemlib_stub.returnCounter = 1; 00420 socket_api_stub.int8_value = 1; 00421 socket_api_stub.recv_cb(sckt_data); 00422 } 00423 00424 connection_handler_destroy(handler, false); 00425 00426 nsdynmemlib_stub.returnCounter = 1; 00427 coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb_test); 00428 nsdynmemlib_stub.returnCounter = 2; 00429 if (0 != coap_connection_handler_open_connection(handler2, 22, false, true, true, false)) { 00430 return false; 00431 } 00432 00433 if (socket_api_stub.recv_cb) { 00434 nsdynmemlib_stub.returnCounter = 1; 00435 socket_api_stub.int8_value = 1; 00436 sckt_data->socket_id = 1; 00437 socket_api_stub.recv_cb(sckt_data); 00438 00439 nsdynmemlib_stub.returnCounter = 2; 00440 socket_api_stub.int8_value = 1; 00441 sckt_data->socket_id = 1; 00442 socket_api_stub.recv_cb(sckt_data); 00443 00444 nsdynmemlib_stub.returnCounter = 1; 00445 socket_api_stub.int8_value = 1; 00446 sckt_data->socket_id = 1; 00447 socket_api_stub.recv_cb(sckt_data); 00448 00449 coap_security_handler_stub.int_value = 4; 00450 nsdynmemlib_stub.returnCounter = 1; 00451 socket_api_stub.int8_value = 1; 00452 sckt_data->socket_id = 1; 00453 socket_api_stub.recv_cb(sckt_data); 00454 00455 coap_security_handler_stub.int_value = MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; 00456 nsdynmemlib_stub.returnCounter = 1; 00457 socket_api_stub.int8_value = 1; 00458 sckt_data->socket_id = 1; 00459 socket_api_stub.recv_cb(sckt_data); 00460 } 00461 00462 connection_handler_destroy(handler2, false); 00463 00464 free(coap_security_handler_stub.sec_obj); 00465 coap_security_handler_stub.sec_obj = NULL; 00466 00467 free(sckt_data); 00468 sckt_data = NULL; 00469 return true; 00470 } 00471 00472 bool test_security_callbacks() 00473 { 00474 coap_security_handler_stub.counter = -1; 00475 uint8_t buf[16]; 00476 memset(&buf, 1, 16); 00477 00478 socket_callback_t *sckt_data = (socket_callback_t *)malloc(sizeof(socket_callback_t)); 00479 memset(sckt_data, 0, sizeof(socket_callback_t)); 00480 00481 coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc(); 00482 00483 nsdynmemlib_stub.returnCounter = 1; 00484 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00485 nsdynmemlib_stub.returnCounter = 2; 00486 if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) { 00487 free(sckt_data); 00488 return false; 00489 } 00490 00491 if (socket_api_stub.recv_cb) { 00492 sckt_data->event_type = SOCKET_DATA; 00493 sckt_data->d_len = 1; 00494 nsdynmemlib_stub.returnCounter = 2; 00495 socket_api_stub.int8_value = 1; 00496 sckt_data->socket_id = 0; 00497 socket_api_stub.recv_cb(sckt_data); 00498 } 00499 00500 if (coap_security_handler_stub.send_cb) { 00501 coap_security_handler_stub.send_cb(0, buf, 22, &buf, 16); 00502 } 00503 if (coap_security_handler_stub.receive_cb) { 00504 coap_security_handler_stub.receive_cb(0, &buf, 16); 00505 } 00506 00507 connection_handler_destroy(handler, false); 00508 00509 free(coap_security_handler_stub.sec_obj); 00510 coap_security_handler_stub.sec_obj = NULL; 00511 00512 free(sckt_data); 00513 sckt_data = NULL; 00514 return true; 00515 } 00516 00517 bool test_coap_connection_handler_msg_prevalidate_cb_read_and_set() 00518 { 00519 coap_security_handler_stub.counter = -1; 00520 00521 coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc(); 00522 00523 nsdynmemlib_stub.returnCounter = 1; 00524 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00525 nsdynmemlib_stub.returnCounter = 2; 00526 if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) { 00527 return false; 00528 } 00529 00530 if (-1 != coap_connection_handler_msg_prevalidate_callback_set(NULL, 1000)) { 00531 return false; 00532 } 00533 00534 if (0 != coap_connection_handler_msg_prevalidate_callback_set(handler, 1000)) { 00535 return false; 00536 } 00537 00538 uint16_t listen_socket_port; 00539 if (NULL != coap_connection_handler_msg_prevalidate_callback_get(NULL, &listen_socket_port)) { 00540 return false; 00541 } 00542 00543 if (1000 != coap_connection_handler_msg_prevalidate_callback_get(handler, &listen_socket_port)) { 00544 return false; 00545 } 00546 00547 connection_handler_destroy(handler, false); 00548 00549 free(coap_security_handler_stub.sec_obj); 00550 coap_security_handler_stub.sec_obj = NULL; 00551 00552 return true; 00553 } 00554 00555 bool test_coap_connection_handler_find_by_socket_port() 00556 { 00557 coap_conn_handler_t *handler_ref; 00558 coap_security_handler_stub.counter = -1; 00559 00560 coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc(); 00561 00562 nsdynmemlib_stub.returnCounter = 1; 00563 coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL); 00564 nsdynmemlib_stub.returnCounter = 2; 00565 if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) { 00566 return false; 00567 } 00568 00569 handler_ref = coap_connection_handler_find_by_socket_port(1000); 00570 if (NULL != handler_ref) { 00571 return false; 00572 } 00573 00574 handler_ref = coap_connection_handler_find_by_socket_port(22); 00575 if (handler_ref->_recv_cb != receive_from_sock_cb) { 00576 return false; 00577 } 00578 00579 connection_handler_destroy(handler, false); 00580 00581 free(coap_security_handler_stub.sec_obj); 00582 coap_security_handler_stub.sec_obj = NULL; 00583 00584 return true; 00585 } 00586 00587
Generated on Tue Jul 12 2022 13:54:55 by
