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 OmniWheels by
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 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00085 if( NULL == handle ) 00086 return false; 00087 free(sn_coap_protocol_stub.expectedCoap); 00088 sn_coap_protocol_stub.expectedCoap = NULL; 00089 free(handle); 00090 return true; 00091 } 00092 00093 bool test_coap_message_handler_destroy() 00094 { 00095 if( -1 != coap_message_handler_destroy(NULL) ) 00096 return false; 00097 retCounter = 1; 00098 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00099 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00100 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00101 00102 if( 0 != coap_message_handler_destroy(handle) ) 00103 return false; 00104 00105 free(sn_coap_protocol_stub.expectedCoap); 00106 return true; 00107 } 00108 00109 bool test_coap_message_handler_find_transaction() 00110 { 00111 if( NULL != coap_message_handler_find_transaction(NULL, 0)) 00112 return false; 00113 retCounter = 1; 00114 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00115 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00116 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00117 00118 uint8_t buf[16]; 00119 memset(&buf, 1, 16); 00120 char uri[3]; 00121 uri[0] = "r"; 00122 uri[1] = "s"; 00123 uri[2] = "\0"; 00124 00125 sn_coap_builder_stub.expectedUint16 = 1; 00126 nsdynmemlib_stub.returnCounter = 3; 00127 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00128 return false; 00129 00130 if( NULL == coap_message_handler_find_transaction(&buf, 24)) 00131 return false; 00132 00133 free(sn_coap_protocol_stub.expectedCoap); 00134 sn_coap_protocol_stub.expectedCoap = NULL; 00135 coap_message_handler_destroy(handle); 00136 return true; 00137 } 00138 00139 bool test_coap_message_handler_coap_msg_process() 00140 { 00141 uint8_t buf[16]; 00142 memset(&buf, 1, 16); 00143 /*Handler is null*/ 00144 if( -1 != coap_message_handler_coap_msg_process(NULL, 0, buf, 22, ns_in6addr_any, NULL, 0, NULL)) 00145 return false; 00146 00147 retCounter = 1; 00148 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00149 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00150 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00151 00152 sn_coap_protocol_stub.expectedHeader = NULL; 00153 /* Coap parse returns null */ 00154 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00155 return false; 00156 00157 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00158 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00159 sn_coap_protocol_stub.expectedHeader->coap_status = 66; 00160 /* Coap library responds */ 00161 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00162 return false; 00163 00164 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00165 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00166 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00167 sn_coap_protocol_stub.expectedHeader->msg_code = 1; 00168 retValue = 0; 00169 /* request received */ 00170 if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00171 return false; 00172 00173 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00174 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00175 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00176 sn_coap_protocol_stub.expectedHeader->msg_code = 1; 00177 nsdynmemlib_stub.returnCounter = 1; 00178 retValue = -1; 00179 if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00180 return false; 00181 00182 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00183 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00184 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00185 sn_coap_protocol_stub.expectedHeader->msg_code = 333; 00186 00187 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00188 return false; 00189 00190 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00191 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00192 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00193 sn_coap_protocol_stub.expectedHeader->msg_code = 333; 00194 00195 char uri[3]; 00196 uri[0] = "r"; 00197 uri[1] = "s"; 00198 uri[2] = "\0"; 00199 00200 sn_coap_builder_stub.expectedUint16 = 1; 00201 nsdynmemlib_stub.returnCounter = 3; 00202 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00203 return false; 00204 00205 sn_coap_protocol_stub.expectedHeader->msg_id = 2; 00206 // sn_coap_protocol_stub.expectedHeader->token_ptr = (uint8_t*)malloc(4); 00207 // memset(sn_coap_protocol_stub.expectedHeader->token_ptr, 1, 4); 00208 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00209 return false; 00210 00211 // free(sn_coap_protocol_stub.expectedHeader->token_ptr); 00212 00213 free(sn_coap_protocol_stub.expectedCoap); 00214 sn_coap_protocol_stub.expectedCoap = NULL; 00215 coap_message_handler_destroy(handle); 00216 return true; 00217 } 00218 00219 bool test_coap_message_handler_request_send() 00220 { 00221 retCounter = 1; 00222 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00223 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00224 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00225 00226 uint8_t buf[16]; 00227 memset(&buf, 1, 16); 00228 char uri[3]; 00229 uri[0] = "r"; 00230 uri[1] = "s"; 00231 uri[2] = "\0"; 00232 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00233 return false; 00234 00235 sn_coap_builder_stub.expectedUint16 = 1; 00236 nsdynmemlib_stub.returnCounter = 1; 00237 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00238 return false; 00239 00240 sn_coap_builder_stub.expectedUint16 = 1; 00241 nsdynmemlib_stub.returnCounter = 3; 00242 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00243 return false; 00244 00245 sn_coap_builder_stub.expectedUint16 = 1; 00246 nsdynmemlib_stub.returnCounter = 3; 00247 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00248 return false; 00249 00250 sn_coap_builder_stub.expectedUint16 = 1; 00251 nsdynmemlib_stub.returnCounter = 3; 00252 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00253 return false; 00254 00255 /* Clear all transactions */ 00256 if( 0 != coap_message_handler_exec(handle, 0xffffffff)) 00257 return false; 00258 00259 sn_coap_protocol_stub.expectedInt16 = -4; 00260 nsdynmemlib_stub.returnCounter = 3; 00261 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb)) 00262 return false; 00263 00264 transaction_cb = 0; 00265 sn_coap_protocol_stub.expectedInt8 = 0; 00266 if( 0 != coap_message_handler_exec(handle, 12)) 00267 return false; 00268 00269 if (transaction_cb != 1) 00270 return false; 00271 00272 sn_coap_protocol_stub.expectedInt16 = -2; 00273 nsdynmemlib_stub.returnCounter = 3; 00274 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb)) 00275 return false; 00276 00277 transaction_cb = 0; 00278 if( 0 != coap_message_handler_exec(handle, 2)) { 00279 return false; 00280 } 00281 if (transaction_cb != 1) 00282 return false; 00283 00284 00285 free(sn_coap_protocol_stub.expectedCoap); 00286 sn_coap_protocol_stub.expectedCoap = NULL; 00287 coap_message_handler_destroy(handle); 00288 return true; 00289 } 00290 00291 bool test_coap_message_handler_request_delete() 00292 { 00293 retCounter = 1; 00294 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00295 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00296 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00297 00298 uint8_t buf[16]; 00299 memset(&buf, 1, 16); 00300 char uri[3]; 00301 uri[0] = "r"; 00302 uri[1] = "s"; 00303 uri[2] = "\0"; 00304 if( 0 == coap_message_handler_request_delete(NULL, 1, 1)) 00305 return false; 00306 00307 if( 0 == coap_message_handler_request_delete(handle, 1, 1)) 00308 return false; 00309 00310 sn_coap_builder_stub.expectedUint16 = 1; 00311 nsdynmemlib_stub.returnCounter = 3; 00312 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00313 return false; 00314 00315 if( 0 != coap_message_handler_request_delete(handle, 1, 2)) 00316 return false; 00317 00318 free(sn_coap_protocol_stub.expectedCoap); 00319 sn_coap_protocol_stub.expectedCoap = NULL; 00320 coap_message_handler_destroy(handle); 00321 return true; 00322 } 00323 00324 bool test_coap_message_handler_response_send() 00325 { 00326 if( -1 != coap_message_handler_response_send(NULL, 2, 0, NULL, 1,3,NULL, 0)) 00327 return false; 00328 00329 retCounter = 1; 00330 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00331 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00332 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00333 sn_coap_hdr_s *header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00334 memset(header, 0, sizeof(sn_coap_hdr_s)); 00335 00336 if( -2 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00337 return false; 00338 00339 uint8_t buf[16]; 00340 memset(&buf, 1, 16); 00341 char uri[3]; 00342 uri[0] = "r"; 00343 uri[1] = "s"; 00344 uri[2] = "\0"; 00345 sn_coap_builder_stub.expectedUint16 = 1; 00346 nsdynmemlib_stub.returnCounter = 3; 00347 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00348 return false; 00349 00350 header->msg_id = 2; 00351 sn_coap_builder_stub.expectedUint16 = 2; 00352 coap_transaction_t * tx = coap_message_handler_find_transaction(&buf, 24); 00353 if( tx ){ 00354 tx->client_request = false; 00355 } 00356 sn_coap_builder_stub.expectedHeader = NULL; 00357 if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00358 return false; 00359 00360 sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00361 memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00362 nsdynmemlib_stub.returnCounter = 0; 00363 if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00364 return false; 00365 00366 sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00367 memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00368 nsdynmemlib_stub.returnCounter = 1; 00369 if( 0 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00370 return false; 00371 00372 free(header); 00373 free(sn_coap_protocol_stub.expectedCoap); 00374 sn_coap_protocol_stub.expectedCoap = NULL; 00375 coap_message_handler_destroy(handle); 00376 return true; 00377 } 00378 00379 bool test_coap_message_handler_exec() 00380 { 00381 /* Null as a parameter */ 00382 if( -1 != coap_message_handler_exec(NULL, 0)) 00383 return false; 00384 00385 retCounter = 1; 00386 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00387 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00388 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00389 00390 if( 0 != coap_message_handler_exec(handle, 0)) 00391 return false; 00392 00393 nsdynmemlib_stub.returnCounter = 1; 00394 coap_transaction_t *transact_ptr = transaction_create(); 00395 00396 /* Transaction not timed out*/ 00397 if( 0 != coap_message_handler_exec(handle, 0)) 00398 return false; 00399 00400 if (transaction_cb != 0) 00401 return false; 00402 00403 /* Timed out, no CB */ 00404 if( 0 != coap_message_handler_exec(handle, 300)) 00405 return false; 00406 00407 if (transaction_cb != 0) 00408 return false; 00409 00410 nsdynmemlib_stub.returnCounter = 1; 00411 transact_ptr = transaction_create(); 00412 transact_ptr->resp_cb = transaction_recv_cb; 00413 00414 /* Transaction not timed out */ 00415 if( 0 != coap_message_handler_exec(handle, 0)) 00416 return false; 00417 00418 if (transaction_cb != 0) 00419 return false; 00420 00421 /* Transaction timed out */ 00422 if( 0 != coap_message_handler_exec(handle, 300)) 00423 return false; 00424 00425 if (transaction_cb == 0) 00426 return false; 00427 00428 /* Teardown */ 00429 free(sn_coap_protocol_stub.expectedCoap); 00430 sn_coap_protocol_stub.expectedCoap = NULL; 00431 coap_message_handler_destroy(handle); 00432 return true; 00433 }
Generated on Fri Jul 22 2022 04:54:02 by
1.7.2
