ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_coap_service_api.c Source File

test_coap_service_api.c

00001 /*
00002  * Copyright (c) 2015 ARM Limited. All Rights Reserved.
00003  */
00004 #include "test_coap_service_api.h"
00005 #include <string.h>
00006 #include "coap_service_api.h"
00007 #include "nsdynmemLIB_stub.h"
00008 #include "coap_connection_handler_stub.h"
00009 #include "coap_message_handler_stub.h"
00010 #include "eventOS_event_stub.h"
00011 #include "eventOS_event.h"
00012 #include "net_interface.h"
00013 
00014 int sec_done_cb(int8_t service_id, uint8_t address[static 16], uint8_t keyblock[static 40]){
00015     return 2;
00016 }
00017 
00018 int sec_start_cb(int8_t service_id, uint8_t address[static 16], uint16_t port, uint8_t* pw, uint8_t *pw_len)
00019 {
00020     return 2;
00021 }
00022 
00023 int request_recv_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *request_ptr)
00024 {
00025     return 2;
00026 }
00027 
00028 int virtual_sock_send_cb(int8_t service_id, uint8_t destination_addr_ptr[static 16], uint16_t port, const uint8_t *data_ptr, uint16_t data_len)
00029 {
00030     return 2;
00031 }
00032 
00033 bool test_coap_service_initialize()
00034 {
00035     if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
00036         return false;
00037 
00038 
00039     nsdynmemlib_stub.returnCounter = 1;
00040     thread_conn_handler_stub.handler_obj = NULL;
00041     if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
00042         return false;
00043 
00044     thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
00045     memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
00046     coap_message_handler_stub.coap_ptr = NULL;
00047 
00048     nsdynmemlib_stub.returnCounter = 1;
00049     thread_conn_handler_stub.int_value = -1;
00050     if( -1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
00051             return false;
00052 
00053     thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
00054     memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
00055     nsdynmemlib_stub.returnCounter = 1;
00056     thread_conn_handler_stub.int_value = 0;
00057 
00058     if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
00059         return false;
00060 
00061     nsdynmemlib_stub.returnCounter = 1;
00062     if( 2 != coap_service_initialize(3, 4, 0, NULL, NULL ))
00063         return false;
00064 
00065     coap_service_delete(2);
00066     coap_service_delete(1);
00067 
00068     free( thread_conn_handler_stub.handler_obj );
00069     thread_conn_handler_stub.handler_obj = NULL;
00070     return true;
00071 }
00072 
00073 bool test_coap_service_delete()
00074 {
00075     coap_service_delete(1);
00076 
00077     thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
00078     memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
00079     nsdynmemlib_stub.returnCounter = 1;
00080     coap_message_handler_stub.coap_ptr = NULL;
00081 
00082     if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
00083         return false;
00084 
00085     coap_service_delete(1);
00086 
00087     free( thread_conn_handler_stub.handler_obj );
00088     thread_conn_handler_stub.handler_obj = NULL;
00089 
00090     return true;
00091 }
00092 
00093 bool test_coap_service_virtual_socket_recv()
00094 {
00095     uint8_t buf[16];
00096     if( -1 != coap_service_virtual_socket_recv(1, &buf, 10, NULL, 0) )
00097         return false;
00098 
00099     thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
00100     memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
00101     nsdynmemlib_stub.returnCounter = 1;
00102     coap_message_handler_stub.coap_ptr = NULL;
00103 
00104     if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
00105         return false;
00106 
00107     thread_conn_handler_stub.int_value = 5;
00108     if( 5 != coap_service_virtual_socket_recv(1, &buf, 10, NULL, 0) )
00109         return false;
00110 
00111     coap_service_delete(1);
00112 
00113     free( thread_conn_handler_stub.handler_obj );
00114     thread_conn_handler_stub.handler_obj = NULL;
00115 
00116     thread_conn_handler_stub.int_value = 0;
00117 
00118     return true;
00119 }
00120 
00121 bool test_coap_service_virtual_socket_set_cb()
00122 {
00123     if( -1 != coap_service_virtual_socket_set_cb(1, NULL) )
00124         return false;
00125 
00126     thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
00127     memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
00128     nsdynmemlib_stub.returnCounter = 1;
00129     coap_message_handler_stub.coap_ptr = NULL;
00130 
00131     if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
00132         return false;
00133 
00134     if( 0 != coap_service_virtual_socket_set_cb(1, NULL) )
00135         return false;
00136 
00137     coap_service_delete(1);
00138 
00139     free( thread_conn_handler_stub.handler_obj );
00140     thread_conn_handler_stub.handler_obj = NULL;
00141 
00142     return true;
00143 }
00144 
00145 bool test_coap_service_register_uri()
00146 {
00147     if( -1 != coap_service_register_uri(1, "as", 1, &request_recv_cb))
00148         return false;
00149 
00150     thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
00151     memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
00152     nsdynmemlib_stub.returnCounter = 1;
00153     coap_message_handler_stub.coap_ptr = NULL;
00154 
00155     if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
00156         return false;
00157 
00158     if( -2 != coap_service_register_uri(1, "as", 1, &request_recv_cb) )
00159         return false;
00160 
00161     nsdynmemlib_stub.returnCounter = 1;
00162     if( -2 != coap_service_register_uri(1, "as", 1, &request_recv_cb) )
00163         return false;
00164 
00165     nsdynmemlib_stub.returnCounter = 2;
00166     if( 0 != coap_service_register_uri(1, "as", 1, &request_recv_cb) )
00167         return false;
00168 
00169     coap_service_delete(1);
00170 
00171     free( thread_conn_handler_stub.handler_obj );
00172     thread_conn_handler_stub.handler_obj = NULL;
00173 
00174     return true;
00175 }
00176 
00177 bool test_coap_service_unregister_uri()
00178 {
00179     if( -1 != coap_service_unregister_uri(1, "as"))
00180         return false;
00181 
00182     thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
00183     memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
00184     nsdynmemlib_stub.returnCounter = 1;
00185     coap_message_handler_stub.coap_ptr = NULL;
00186     thread_conn_handler_stub.int_value = 0;
00187 
00188     if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
00189         return false;
00190 
00191     nsdynmemlib_stub.returnCounter = 2;
00192     if( 0 != coap_service_register_uri(1, "as", 1, &request_recv_cb) )
00193         return false;
00194 
00195     if( -2 != coap_service_unregister_uri(1, "ts") )
00196         return false;
00197 
00198     if( 0 != coap_service_unregister_uri(1, "as") )
00199         return false;
00200 
00201     coap_service_delete(1);
00202 
00203     free( thread_conn_handler_stub.handler_obj );
00204     thread_conn_handler_stub.handler_obj = NULL;
00205 
00206     return true;
00207 }
00208 
00209 bool test_coap_service_request_send()
00210 {
00211     uint8_t buf[16];
00212     coap_message_handler_stub.uint16_value = 6;
00213     if( 6 != coap_service_request_send(0,0,&buf,0,0,0,NULL, 0,NULL,0,NULL))
00214         return false;
00215     return true;
00216 }
00217 
00218 bool test_coap_service_response_send()
00219 {
00220     uint8_t buf[16];
00221     coap_message_handler_stub.int8_value = 6;
00222     if( 6 != coap_service_response_send(0,0,NULL, 65, 0,NULL, 0))
00223         return false;
00224     return true;
00225 }
00226 
00227 bool test_coap_callbacks()
00228 {
00229     thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
00230     memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
00231     nsdynmemlib_stub.returnCounter = 1;
00232     coap_message_handler_stub.coap_ptr = (coap_msg_handler_t *)malloc(sizeof(coap_msg_handler_t));
00233     memset(coap_message_handler_stub.coap_ptr, 0, sizeof(coap_msg_handler_t));
00234 
00235     if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
00236         return false;
00237 
00238     if( 0 != coap_message_handler_stub.coap_ptr->sn_coap_service_malloc(0))
00239         return false;
00240 
00241     nsdynmemlib_stub.returnCounter = 1;
00242     void *handle = coap_message_handler_stub.coap_ptr->sn_coap_service_malloc(5);
00243     if( 0 == handle )
00244         return false;
00245 
00246     coap_message_handler_stub.coap_ptr->sn_coap_service_free(handle);
00247 
00248     //coap_tx_function
00249     uint8_t data[14];
00250     memset(&data, 3, 14);
00251     sn_nsdl_addr_s addr;
00252     addr.addr_len = 2;
00253     addr.port = 4;
00254     addr.addr_ptr = &data;
00255     if( 255 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(NULL, 0, &addr, NULL))
00256         return false;
00257 
00258     coap_transaction_t *tr = (coap_transaction_t *)malloc(sizeof(coap_transaction_t));
00259     memset(tr, 0, sizeof(coap_transaction_t));
00260 
00261     if( 255 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 0, &addr, tr))
00262         return false;
00263 
00264     tr->service_id = 1;
00265     thread_conn_handler_stub.int_value = -2;
00266     if( 255 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 0, &addr, tr))
00267         return false;
00268 
00269     nsdynmemlib_stub.returnCounter = 1;
00270     if( 255 != coap_message_handler_stub.coap_ptr->sn_coap_tx_callback(&data, 2, &addr, tr))
00271         return false;
00272 
00273     free(tr->data_ptr);
00274     free(tr);
00275 
00276     coap_service_delete(1);
00277 
00278     free( coap_message_handler_stub.coap_ptr );
00279     coap_message_handler_stub.coap_ptr = NULL;
00280 
00281     free( thread_conn_handler_stub.handler_obj );
00282     thread_conn_handler_stub.handler_obj = NULL;
00283 
00284     return true;
00285 }
00286 
00287 #define COAP_TICK_TIMER 0xf1 //MUST BE SAME AS IN coap_service_api.c
00288 bool test_eventOS_callbacks()
00289 {
00290     thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
00291     memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
00292     nsdynmemlib_stub.returnCounter = 1;
00293     if( 1 != coap_service_initialize(1, 2, 0, NULL, NULL ))
00294         return false;
00295 
00296     if( eventOs_event_stub.event_ptr ){
00297         arm_event_s event;
00298         event.event_type = ARM_LIB_TASKLET_INIT_EVENT;
00299         eventOs_event_stub.event_ptr(&event);
00300 
00301         event.event_type = ARM_LIB_SYSTEM_TIMER_EVENT;
00302         event.event_id = COAP_TICK_TIMER;
00303         eventOs_event_stub.event_ptr(&event);
00304     }
00305 
00306     coap_service_delete(1);
00307     free( thread_conn_handler_stub.handler_obj );
00308     thread_conn_handler_stub.handler_obj = NULL;
00309     return true;
00310 }
00311 
00312 bool test_conn_handler_callbacks()
00313 {
00314     uint8_t buf[16];
00315     thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t));
00316     memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t));
00317     nsdynmemlib_stub.returnCounter = 1;
00318     if( 1 != coap_service_initialize(1, 2, COAP_SERVICE_OPTIONS_SECURE_BYPASS, &sec_start_cb, &sec_done_cb ))
00319         return false;
00320 
00321     if( thread_conn_handler_stub.send_to_sock_cb ){
00322         thread_conn_handler_stub.bool_value = true;
00323         coap_service_virtual_socket_set_cb(1, &virtual_sock_send_cb);
00324         if( 2 != thread_conn_handler_stub.send_to_sock_cb(1, buf, 12, NULL, 0))
00325             return false;
00326         thread_conn_handler_stub.bool_value = false;
00327         if( -1 != thread_conn_handler_stub.send_to_sock_cb(1, buf, 12, NULL, 0))
00328             return false;
00329     }
00330 
00331     if( thread_conn_handler_stub.receive_from_sock_cb ){
00332         coap_message_handler_stub.int16_value = 2;
00333         if( -1 != thread_conn_handler_stub.receive_from_sock_cb(1, buf, 12, NULL, NULL, 0))
00334             return false;
00335 
00336         nsdynmemlib_stub.returnCounter = 1;
00337         uint8_t * ptr = ns_dyn_mem_alloc(5);
00338         memset(ptr, 3, 5);
00339         nsdynmemlib_stub.returnCounter = 1;
00340         if( 2 != thread_conn_handler_stub.receive_from_sock_cb(1, buf, 12, NULL, ptr, 5))
00341             return false;
00342         ns_dyn_mem_free(ptr);
00343         coap_message_handler_stub.int16_value = 0;
00344 
00345         //This could be moved to own test function,
00346         //but thread_conn_handler_stub.receive_from_sock_cb must be called successfully
00347         if( coap_message_handler_stub.cb ){
00348             if( -1 != coap_message_handler_stub.cb(1, NULL, NULL) )
00349                 return false;
00350 
00351             sn_coap_hdr_s * coap = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00352             memset(coap, 0, sizeof(sn_coap_hdr_s));
00353 
00354             uint8_t  uri[2] = "as";
00355             coap->uri_path_ptr = &uri;
00356             coap->uri_path_len=2;
00357 
00358             if( -1 != coap_message_handler_stub.cb(1, coap, NULL) )
00359                 return false;
00360 
00361             thread_conn_handler_stub.bool_value = true;
00362             nsdynmemlib_stub.returnCounter = 2;
00363             if( 0 != coap_service_register_uri(1, "as", 1, &request_recv_cb) )
00364                 return false;
00365 
00366             if( -1 != coap_message_handler_stub.cb(1, coap, NULL) )
00367                 return false;
00368 
00369             coap_transaction_t *tr = (coap_transaction_t *)malloc(sizeof(coap_transaction_t));
00370             memset(tr, 0, sizeof(coap_transaction_t));
00371 
00372             if( 2 != coap_message_handler_stub.cb(1, coap, tr) )
00373                 return false;
00374 
00375             free(tr);
00376             tr = NULL;
00377 
00378             thread_conn_handler_stub.bool_value = false;
00379             free(coap);
00380             coap = NULL;
00381         }
00382     }
00383 
00384     if(thread_conn_handler_stub.get_passwd_cb){
00385         thread_conn_handler_stub.bool_value = true;
00386         if( 2 != thread_conn_handler_stub.get_passwd_cb(1, buf, 12, NULL, 0))
00387             return false;
00388         thread_conn_handler_stub.bool_value = false;
00389         if( -1 != thread_conn_handler_stub.get_passwd_cb(1, buf, 12, NULL, 0))
00390             return false;
00391     }
00392 
00393     if(thread_conn_handler_stub.sec_done_cb){
00394         uint8_t block[40];
00395         thread_conn_handler_stub.bool_value = true;
00396 
00397         coap_transaction_t *tr = (coap_transaction_t *)malloc(sizeof(coap_transaction_t));
00398         memset(tr, 0, sizeof(coap_transaction_t));
00399         nsdynmemlib_stub.returnCounter = 1;
00400         tr->data_ptr = ns_dyn_mem_alloc(1);
00401         tr->data_len = 1;
00402         coap_message_handler_stub.coap_tx_ptr = tr;
00403 
00404         thread_conn_handler_stub.sec_done_cb(1, buf, 12, block);
00405 
00406         free(tr);
00407         coap_message_handler_stub.coap_tx_ptr = NULL;
00408 
00409         thread_conn_handler_stub.bool_value = false;
00410 
00411     }
00412 
00413     coap_service_delete(1);
00414     free( thread_conn_handler_stub.handler_obj );
00415     thread_conn_handler_stub.handler_obj = NULL;
00416 
00417     return true;
00418 }