Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_coap_connection_handler.c Source File

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     if( NULL != connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL) )
00057         return false;
00058 
00059     nsdynmemlib_stub.returnCounter = 1;
00060     coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL);
00061     if( NULL == handler )
00062         return false;
00063     ns_dyn_mem_free(handler);
00064     return true;
00065 }
00066 
00067 bool test_connection_handler_destroy()
00068 {
00069     coap_security_handler_stub.counter = -1;
00070     nsdynmemlib_stub.returnCounter = 1;
00071     coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL);
00072 
00073     connection_handler_destroy(handler, false);
00074     return true;
00075 }
00076 
00077 bool test_coap_connection_handler_open_connection()
00078 {
00079     coap_security_handler_stub.counter = -1;
00080     nsdynmemlib_stub.returnCounter = 1;
00081     coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, NULL, NULL, NULL);
00082 
00083     if( -1 != coap_connection_handler_open_connection(NULL, 0,false,false,false,false) )
00084         return false;
00085 
00086     if( -1 != coap_connection_handler_open_connection(handler, 0,false,false,false,false) )
00087         return false;
00088 
00089     ns_dyn_mem_free(handler);
00090     nsdynmemlib_stub.returnCounter = 1;
00091     handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
00092     if( -1 != coap_connection_handler_open_connection(handler, 0,true,true,true,false) )
00093         return false;
00094 
00095     nsdynmemlib_stub.returnCounter = 2;
00096     if( 0 != coap_connection_handler_open_connection(handler, 0,true,true,true,false) )
00097         return false;
00098 
00099     nsdynmemlib_stub.returnCounter = 2;
00100     if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,true) )
00101         return false;
00102 
00103     //open second one
00104     nsdynmemlib_stub.returnCounter = 1;
00105     coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
00106     nsdynmemlib_stub.returnCounter = 2;
00107     if( 0 != coap_connection_handler_open_connection(handler2, 22,false,true,true,true) )
00108         return false;
00109 
00110     if( 0 != coap_connection_handler_open_connection(handler, 23,false,false,false,false) )
00111         return false;
00112 
00113     connection_handler_destroy(handler2, false);
00114     connection_handler_destroy(handler, false);
00115     return true;
00116 }
00117 
00118 bool test_coap_connection_handler_send_data()
00119 {
00120     coap_security_handler_stub.counter = -1;
00121     if( -1 != coap_connection_handler_send_data(NULL, NULL, ns_in6addr_any, NULL, 0, false))
00122         return false;
00123 
00124     ns_address_t addr;
00125     memset(addr.address, 1, 16);
00126     addr.identifier = 22;
00127 
00128     nsdynmemlib_stub.returnCounter = 1;
00129     coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
00130     nsdynmemlib_stub.returnCounter = 2;
00131     if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false) )
00132         return false;
00133 
00134     if( -1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true))
00135         return false;
00136 
00137     connection_handler_destroy(handler, false);
00138 
00139     coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc();
00140 
00141     nsdynmemlib_stub.returnCounter = 1;
00142     handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
00143     nsdynmemlib_stub.returnCounter = 4;
00144     if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,false,false) )
00145         return false;
00146 
00147     if( -1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true))
00148         return false;
00149 
00150     if( -1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true))
00151         return false;
00152 
00153     connection_handler_destroy(handler, false);
00154 
00155     free(coap_security_handler_stub.sec_obj);
00156     coap_security_handler_stub.sec_obj = NULL;
00157 
00158     //NON SECURE HERE -->
00159     nsdynmemlib_stub.returnCounter = 1;
00160     handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
00161     nsdynmemlib_stub.returnCounter = 2;
00162     if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,false,false) )
00163         return false;
00164 
00165 
00166     if( 1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true))
00167         return false;
00168     connection_handler_destroy(handler ,false);
00169 
00170     nsdynmemlib_stub.returnCounter = 1;
00171     handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
00172     nsdynmemlib_stub.returnCounter = 2;
00173     if( 0 != coap_connection_handler_open_connection(handler, 22,false,false,true,false) )
00174         return false;
00175 
00176     socket_api_stub.int8_value = 7;
00177     if( 1 != coap_connection_handler_send_data(handler, &addr, ns_in6addr_any, NULL, 0, true))
00178         return false;
00179     connection_handler_destroy(handler, false);
00180 
00181     //<-- NON SECURE HERE
00182 
00183     return true;
00184 }
00185 
00186 bool test_coap_connection_handler_virtual_recv()
00187 {
00188     coap_security_handler_stub.counter = -1;
00189     uint8_t buf[16];
00190     memset(&buf, 1, 16);
00191     if( -1 != coap_connection_handler_virtual_recv(NULL,buf, 12, NULL, 0) )
00192         return false;
00193 
00194     nsdynmemlib_stub.returnCounter = 1;
00195     coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
00196     nsdynmemlib_stub.returnCounter = 2;
00197     if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
00198         return false;
00199 
00200     if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, NULL, 0) )
00201         return false;
00202 
00203     nsdynmemlib_stub.returnCounter = 1;
00204     if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, NULL, 0) )
00205         return false;
00206 
00207     ns_timer_stub.int8_value = 0;
00208     nsdynmemlib_stub.returnCounter = 3;
00209     if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) )
00210         return false;
00211 
00212     //handler->socket->data still in memory
00213     coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc();
00214 
00215     ns_timer_stub.int8_value = -1;
00216     nsdynmemlib_stub.returnCounter = 3;
00217     if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) )
00218         return false;
00219 
00220     ns_timer_stub.int8_value = 0;
00221     nsdynmemlib_stub.returnCounter = 3;
00222     if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) )
00223         return false;
00224 
00225     connection_handler_destroy(handler, false);
00226 
00227     nsdynmemlib_stub.returnCounter = 1;
00228     coap_conn_handler_t *handler2 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb_test);
00229     nsdynmemlib_stub.returnCounter = 2;
00230     if( 0 != coap_connection_handler_open_connection(handler2, 24,false,true,true,false) )
00231         return false;
00232 
00233     nsdynmemlib_stub.returnCounter = 3;
00234     if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) )
00235         return false;
00236 
00237     nsdynmemlib_stub.returnCounter = 1;
00238     coap_security_handler_stub.int_value = 0;
00239     if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) )
00240         return false;
00241 
00242     nsdynmemlib_stub.returnCounter = 1;
00243     if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) )
00244         return false;
00245 
00246     nsdynmemlib_stub.returnCounter = 1;
00247     coap_security_handler_stub.int_value = MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY;
00248     if( 0 != coap_connection_handler_virtual_recv(handler2,buf, 12, &buf, 1) )
00249         return false;
00250 
00251     connection_handler_destroy(handler2, false);
00252 
00253     free(coap_security_handler_stub.sec_obj);
00254     coap_security_handler_stub.sec_obj = NULL;
00255 
00256     nsdynmemlib_stub.returnCounter = 1;
00257     coap_conn_handler_t *handler3 = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, &get_passwd_cb, &sec_done_cb_test);
00258     nsdynmemlib_stub.returnCounter = 2;
00259     if( 0 != coap_connection_handler_open_connection(handler3, 26,false,false,true,false) )
00260         return false;
00261 
00262     nsdynmemlib_stub.returnCounter = 3;
00263     if( 0 != coap_connection_handler_virtual_recv(handler3,buf, 12, &buf, 1) )
00264         return false;
00265 
00266     connection_handler_destroy(handler3, false);
00267 
00268     return true;
00269 }
00270 
00271 bool test_coap_connection_handler_socket_belongs_to()
00272 {
00273     coap_security_handler_stub.counter = -1;
00274     if( false != coap_connection_handler_socket_belongs_to(NULL, 2) )
00275         return false;
00276 
00277     socket_api_stub.int8_value = 0;
00278     nsdynmemlib_stub.returnCounter = 1;
00279     coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
00280     nsdynmemlib_stub.returnCounter = 2;
00281     if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
00282         return false;
00283 
00284     if( true != coap_connection_handler_socket_belongs_to(handler, 0) )
00285         return false;
00286 
00287     if( false != coap_connection_handler_socket_belongs_to(handler, 3) )
00288         return false;
00289     connection_handler_destroy(handler, false);
00290 
00291     nsdynmemlib_stub.returnCounter = 0;
00292     return true;
00293 }
00294 
00295 bool test_timer_callbacks()
00296 {
00297     coap_security_handler_stub.counter = -1;
00298     uint8_t buf[16];
00299     memset(&buf, 1, 16);
00300 
00301     nsdynmemlib_stub.returnCounter = 1;
00302     coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
00303     nsdynmemlib_stub.returnCounter = 2;
00304     if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
00305         return false;
00306 
00307     //handler->socket->data still in memory
00308     coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc();
00309 
00310     ns_timer_stub.int8_value = 0;
00311     nsdynmemlib_stub.returnCounter = 3;
00312     ns_timer_stub.int8_value = 5;
00313     if( -1 != coap_connection_handler_virtual_recv(handler,buf, 12, &buf, 1) )
00314         return false;
00315 
00316     //Note next tests will affect ns_timer test (cycle & cycle_count
00317     if( coap_security_handler_stub.start_timer_cb ){
00318         coap_security_handler_stub.start_timer_cb(1, 0, 0);
00319 
00320         coap_security_handler_stub.start_timer_cb(1, 1, 2);
00321     }
00322 
00323     if( coap_security_handler_stub.timer_status_cb ){
00324         if( -1 != coap_security_handler_stub.timer_status_cb(4) )
00325             return false;
00326 
00327         if( 0 != coap_security_handler_stub.timer_status_cb(1) )
00328             return false;
00329     }
00330 
00331     if( ns_timer_stub.cb ){
00332         ns_timer_stub.cb(4, 0);
00333 
00334         ns_timer_stub.cb(5, 0);
00335 
00336         coap_security_handler_stub.int_value = MBEDTLS_ERR_SSL_TIMEOUT;
00337 
00338         ns_timer_stub.cb(5, 0);
00339         coap_security_handler_stub.int_value = 0;
00340     }
00341 
00342     connection_handler_destroy(handler, false);
00343     free(coap_security_handler_stub.sec_obj);
00344     coap_security_handler_stub.sec_obj = NULL;
00345     return true;
00346 }
00347 
00348 bool test_socket_api_callbacks()
00349 {
00350     coap_security_handler_stub.counter = -1;
00351     uint8_t buf[16];
00352     memset(&buf, 1, 16);
00353 
00354     socket_callback_t *sckt_data = (socket_callback_t *)malloc(sizeof(socket_callback_t));
00355     memset(sckt_data, 0, sizeof(socket_callback_t));
00356 
00357     coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc();
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, false);
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_test);
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, false);
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_handler_stub_alloc();
00437 
00438     nsdynmemlib_stub.returnCounter = 1;
00439     coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
00440     nsdynmemlib_stub.returnCounter = 2;
00441     if( 0 != coap_connection_handler_open_connection(handler, 22,false,true,true,false) )
00442         return false;
00443 
00444     if( socket_api_stub.recv_cb ){
00445         sckt_data->event_type = SOCKET_DATA;
00446         sckt_data->d_len = 1;
00447         nsdynmemlib_stub.returnCounter = 2;
00448         socket_api_stub.int8_value = 1;
00449         sckt_data->socket_id = 0;
00450         socket_api_stub.recv_cb(sckt_data);
00451     }
00452 
00453     if( coap_security_handler_stub.send_cb ){
00454         coap_security_handler_stub.send_cb(0, buf, 22, &buf, 16);
00455     }
00456     if( coap_security_handler_stub.receive_cb ){
00457         coap_security_handler_stub.receive_cb(0, &buf, 16);
00458     }
00459 
00460     connection_handler_destroy(handler, false);
00461 
00462     free(coap_security_handler_stub.sec_obj);
00463     coap_security_handler_stub.sec_obj = NULL;
00464 
00465     free(sckt_data);
00466     sckt_data = NULL;
00467     return true;
00468 }