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.
test_coap_message_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_message_handler.h" 00018 #include <string.h> 00019 #include "coap_message_handler.h" 00020 #include "sn_coap_protocol_stub.h" 00021 #include "nsdynmemLIB_stub.h" 00022 #include "sn_coap_builder_stub.h" 00023 #include "sn_coap_parser_stub.h" 00024 #include "socket_api.h" 00025 #include "coap_message_handler.c" 00026 00027 int retCounter = 0; 00028 int retValue = 0; 00029 int transaction_cb = 0; 00030 00031 static void *test_own_alloc(uint16_t size) 00032 { 00033 if( retCounter > 0 ){ 00034 retCounter--; 00035 return malloc(size); 00036 } 00037 return NULL; 00038 } 00039 00040 static void test_own_free(void *ptr) 00041 { 00042 if (ptr) { 00043 free(ptr); 00044 } 00045 } 00046 00047 static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr, void *param) 00048 { 00049 return 0; 00050 } 00051 00052 int resp_recv(int8_t service_id, uint16_t msg_id, sn_coap_hdr_s *response_ptr){ 00053 return retValue; 00054 } 00055 00056 int16_t process_cb(int8_t a, sn_coap_hdr_s *b, coap_transaction_t *c) 00057 { 00058 return retValue; 00059 } 00060 00061 static int transaction_recv_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *response_ptr) 00062 { 00063 transaction_cb = 1; 00064 return 1; 00065 } 00066 00067 bool test_coap_message_handler_init() 00068 { 00069 if( NULL != coap_message_handler_init(NULL, NULL, NULL) ) 00070 return false; 00071 if( NULL != coap_message_handler_init(&test_own_alloc, NULL, NULL) ) 00072 return false; 00073 if( NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, NULL) ) 00074 return false; 00075 if( NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function) ) 00076 return false; 00077 retCounter = 1; 00078 sn_coap_protocol_stub.expectedCoap = NULL; 00079 if( NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function) ) 00080 return false; 00081 retCounter = 1; 00082 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00083 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00084 nsdynmemlib_stub.returnCounter = 1; 00085 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00086 if( NULL == handle ) 00087 return false; 00088 free(sn_coap_protocol_stub.expectedCoap); 00089 sn_coap_protocol_stub.expectedCoap = NULL; 00090 free(handle); 00091 return true; 00092 } 00093 00094 bool test_coap_message_handler_destroy() 00095 { 00096 if( -1 != coap_message_handler_destroy(NULL) ) 00097 return false; 00098 retCounter = 1; 00099 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00100 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00101 nsdynmemlib_stub.returnCounter = 1; 00102 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00103 00104 if( 0 != coap_message_handler_destroy(handle) ) 00105 return false; 00106 00107 free(sn_coap_protocol_stub.expectedCoap); 00108 return true; 00109 } 00110 00111 bool test_coap_message_handler_find_transaction() 00112 { 00113 if( NULL != coap_message_handler_find_transaction(NULL, 0)) 00114 return false; 00115 retCounter = 1; 00116 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00117 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00118 nsdynmemlib_stub.returnCounter = 1; 00119 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00120 00121 uint8_t buf[16]; 00122 memset(&buf, 1, 16); 00123 char uri[3]; 00124 uri[0] = "r"; 00125 uri[1] = "s"; 00126 uri[2] = "\0"; 00127 00128 sn_coap_builder_stub.expectedUint16 = 1; 00129 nsdynmemlib_stub.returnCounter = 3; 00130 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00131 return false; 00132 00133 if( NULL == coap_message_handler_find_transaction(&buf, 24)) 00134 return false; 00135 00136 free(sn_coap_protocol_stub.expectedCoap); 00137 sn_coap_protocol_stub.expectedCoap = NULL; 00138 coap_message_handler_destroy(handle); 00139 return true; 00140 } 00141 00142 bool test_coap_message_handler_coap_msg_process() 00143 { 00144 uint8_t buf[16]; 00145 memset(&buf, 1, 16); 00146 bool ret_val = false; 00147 /*Handler is null*/ 00148 if( -1 != coap_message_handler_coap_msg_process(NULL, 0, buf, 22, ns_in6addr_any, NULL, 0, NULL)) 00149 goto exit; 00150 00151 retCounter = 1; 00152 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00153 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00154 nsdynmemlib_stub.returnCounter = 1; 00155 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00156 00157 sn_coap_protocol_stub.expectedHeader = NULL; 00158 /* Coap parse returns null */ 00159 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00160 goto exit; 00161 00162 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00163 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00164 sn_coap_protocol_stub.expectedHeader->coap_status = 66; 00165 nsdynmemlib_stub.returnCounter = 1; 00166 /* Coap library responds */ 00167 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00168 goto exit; 00169 00170 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00171 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00172 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00173 sn_coap_protocol_stub.expectedHeader->msg_code = 1; 00174 retValue = 0; 00175 /* request received */ 00176 nsdynmemlib_stub.returnCounter = 1; 00177 if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00178 goto exit; 00179 00180 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00181 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00182 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00183 sn_coap_protocol_stub.expectedHeader->msg_code = 1; 00184 nsdynmemlib_stub.returnCounter = 1; 00185 retValue = -1; 00186 if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00187 goto exit; 00188 00189 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00190 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00191 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00192 sn_coap_protocol_stub.expectedHeader->msg_code = 333; 00193 nsdynmemlib_stub.returnCounter = 1; 00194 00195 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00196 goto exit; 00197 00198 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00199 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00200 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00201 sn_coap_protocol_stub.expectedHeader->msg_code = 333; 00202 00203 char uri[3]; 00204 uri[0] = "r"; 00205 uri[1] = "s"; 00206 uri[2] = "\0"; 00207 00208 sn_coap_builder_stub.expectedUint16 = 1; 00209 nsdynmemlib_stub.returnCounter = 3; 00210 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00211 goto exit; 00212 00213 sn_coap_protocol_stub.expectedHeader->msg_id = 2; 00214 00215 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00216 goto exit; 00217 00218 ret_val = true; 00219 exit: 00220 free(sn_coap_protocol_stub.expectedCoap); 00221 sn_coap_protocol_stub.expectedCoap = NULL; 00222 coap_message_handler_destroy(handle); 00223 return ret_val; 00224 } 00225 00226 bool test_coap_message_handler_request_send() 00227 { 00228 retCounter = 1; 00229 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00230 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00231 nsdynmemlib_stub.returnCounter = 1; 00232 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00233 00234 uint8_t buf[16]; 00235 memset(&buf, 1, 16); 00236 char uri[3]; 00237 uri[0] = "r"; 00238 uri[1] = "s"; 00239 uri[2] = "\0"; 00240 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00241 return false; 00242 00243 sn_coap_builder_stub.expectedUint16 = 1; 00244 nsdynmemlib_stub.returnCounter = 1; 00245 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00246 return false; 00247 00248 sn_coap_builder_stub.expectedUint16 = 1; 00249 nsdynmemlib_stub.returnCounter = 3; 00250 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00251 return false; 00252 00253 sn_coap_builder_stub.expectedUint16 = 1; 00254 nsdynmemlib_stub.returnCounter = 3; 00255 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00256 return false; 00257 00258 sn_coap_builder_stub.expectedUint16 = 1; 00259 nsdynmemlib_stub.returnCounter = 3; 00260 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00261 return false; 00262 00263 /* Clear all transactions */ 00264 if( 0 != coap_message_handler_exec(handle, 0xffffffff)) 00265 return false; 00266 00267 sn_coap_protocol_stub.expectedInt16 = -4; 00268 nsdynmemlib_stub.returnCounter = 3; 00269 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb)) 00270 return false; 00271 00272 transaction_cb = 0; 00273 sn_coap_protocol_stub.expectedInt8 = 0; 00274 if( 0 != coap_message_handler_exec(handle, 12)) 00275 return false; 00276 00277 if (transaction_cb != 1) 00278 return false; 00279 00280 sn_coap_protocol_stub.expectedInt16 = -2; 00281 nsdynmemlib_stub.returnCounter = 3; 00282 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb)) 00283 return false; 00284 00285 transaction_cb = 0; 00286 if( 0 != coap_message_handler_exec(handle, 2)) { 00287 return false; 00288 } 00289 if (transaction_cb != 1) 00290 return false; 00291 00292 00293 free(sn_coap_protocol_stub.expectedCoap); 00294 sn_coap_protocol_stub.expectedCoap = NULL; 00295 coap_message_handler_destroy(handle); 00296 return true; 00297 } 00298 00299 bool test_coap_message_handler_request_delete() 00300 { 00301 retCounter = 1; 00302 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00303 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00304 nsdynmemlib_stub.returnCounter = 1; 00305 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00306 00307 uint8_t buf[16]; 00308 memset(&buf, 1, 16); 00309 char uri[3]; 00310 uri[0] = "r"; 00311 uri[1] = "s"; 00312 uri[2] = "\0"; 00313 if( 0 == coap_message_handler_request_delete(NULL, 1, 1)) 00314 return false; 00315 00316 if( 0 == coap_message_handler_request_delete(handle, 1, 1)) 00317 return false; 00318 00319 sn_coap_builder_stub.expectedUint16 = 1; 00320 nsdynmemlib_stub.returnCounter = 3; 00321 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00322 return false; 00323 00324 if( 0 != coap_message_handler_request_delete(handle, 1, 2)) 00325 return false; 00326 00327 free(sn_coap_protocol_stub.expectedCoap); 00328 sn_coap_protocol_stub.expectedCoap = NULL; 00329 coap_message_handler_destroy(handle); 00330 return true; 00331 } 00332 00333 bool test_coap_message_handler_response_send() 00334 { 00335 if( -1 != coap_message_handler_response_send(NULL, 2, 0, NULL, 1,3,NULL, 0)) 00336 return false; 00337 00338 retCounter = 1; 00339 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00340 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00341 nsdynmemlib_stub.returnCounter = 1; 00342 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00343 sn_coap_hdr_s *header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00344 memset(header, 0, sizeof(sn_coap_hdr_s)); 00345 00346 if( -2 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00347 return false; 00348 00349 uint8_t buf[16]; 00350 memset(&buf, 1, 16); 00351 char uri[3]; 00352 uri[0] = "r"; 00353 uri[1] = "s"; 00354 uri[2] = "\0"; 00355 sn_coap_builder_stub.expectedUint16 = 1; 00356 nsdynmemlib_stub.returnCounter = 3; 00357 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00358 return false; 00359 00360 header->msg_id = 2; 00361 sn_coap_builder_stub.expectedUint16 = 2; 00362 coap_transaction_t * tx = coap_message_handler_find_transaction(&buf, 24); 00363 if( tx ){ 00364 tx->client_request = false; 00365 } 00366 sn_coap_builder_stub.expectedHeader = NULL; 00367 if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00368 return false; 00369 00370 sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00371 memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00372 nsdynmemlib_stub.returnCounter = 0; 00373 if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00374 return false; 00375 00376 sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00377 memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00378 nsdynmemlib_stub.returnCounter = 1; 00379 if( 0 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00380 return false; 00381 00382 free(header); 00383 free(sn_coap_protocol_stub.expectedCoap); 00384 sn_coap_protocol_stub.expectedCoap = NULL; 00385 coap_message_handler_destroy(handle); 00386 return true; 00387 } 00388 00389 bool test_coap_message_handler_exec() 00390 { 00391 /* Null as a parameter */ 00392 if( -1 != coap_message_handler_exec(NULL, 0)) 00393 return false; 00394 00395 retCounter = 1; 00396 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00397 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00398 nsdynmemlib_stub.returnCounter = 1; 00399 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00400 00401 if( 0 != coap_message_handler_exec(handle, 0)) 00402 return false; 00403 00404 nsdynmemlib_stub.returnCounter = 1; 00405 coap_transaction_t *transact_ptr = transaction_create(); 00406 00407 /* Transaction not timed out*/ 00408 if( 0 != coap_message_handler_exec(handle, 0)) 00409 return false; 00410 00411 if (transaction_cb != 0) 00412 return false; 00413 00414 /* Timed out, no CB */ 00415 if( 0 != coap_message_handler_exec(handle, 300)) 00416 return false; 00417 00418 if (transaction_cb != 0) 00419 return false; 00420 00421 nsdynmemlib_stub.returnCounter = 1; 00422 transact_ptr = transaction_create(); 00423 transact_ptr->resp_cb = transaction_recv_cb; 00424 00425 /* Transaction not timed out */ 00426 if( 0 != coap_message_handler_exec(handle, 0)) 00427 return false; 00428 00429 if (transaction_cb != 0) 00430 return false; 00431 00432 /* Transaction timed out */ 00433 if( 0 != coap_message_handler_exec(handle, 300)) 00434 return false; 00435 00436 if (transaction_cb == 0) 00437 return false; 00438 00439 /* Teardown */ 00440 free(sn_coap_protocol_stub.expectedCoap); 00441 sn_coap_protocol_stub.expectedCoap = NULL; 00442 coap_message_handler_destroy(handle); 00443 return true; 00444 }
Generated on Tue Aug 9 2022 00:37:21 by
1.7.2