init
Embed:
(wiki syntax)
Show/hide line numbers
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, -1, 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, -1, 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, -1, 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, -1, 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, -1, 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, -1, 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, -1, 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 free(sn_coap_protocol_stub.expectedCoap); 00256 sn_coap_protocol_stub.expectedCoap = NULL; 00257 coap_message_handler_destroy(handle); 00258 return true; 00259 } 00260 00261 bool test_coap_message_handler_request_delete() 00262 { 00263 retCounter = 1; 00264 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00265 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00266 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00267 00268 uint8_t buf[16]; 00269 memset(&buf, 1, 16); 00270 char uri[3]; 00271 uri[0] = "r"; 00272 uri[1] = "s"; 00273 uri[2] = "\0"; 00274 if( 0 == coap_message_handler_request_delete(NULL, 1, 1)) 00275 return false; 00276 00277 if( 0 == coap_message_handler_request_delete(handle, 1, 1)) 00278 return false; 00279 00280 sn_coap_builder_stub.expectedUint16 = 1; 00281 nsdynmemlib_stub.returnCounter = 3; 00282 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00283 return false; 00284 00285 if( 0 != coap_message_handler_request_delete(handle, 1, 2)) 00286 return false; 00287 00288 free(sn_coap_protocol_stub.expectedCoap); 00289 sn_coap_protocol_stub.expectedCoap = NULL; 00290 coap_message_handler_destroy(handle); 00291 return true; 00292 } 00293 00294 bool test_coap_message_handler_response_send() 00295 { 00296 if( -1 != coap_message_handler_response_send(NULL, 2, 0, NULL, 1,3,NULL, 0)) 00297 return false; 00298 00299 retCounter = 1; 00300 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00301 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00302 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00303 sn_coap_hdr_s *header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00304 memset(header, 0, sizeof(sn_coap_hdr_s)); 00305 00306 if( -2 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00307 return false; 00308 00309 uint8_t buf[16]; 00310 memset(&buf, 1, 16); 00311 char uri[3]; 00312 uri[0] = "r"; 00313 uri[1] = "s"; 00314 uri[2] = "\0"; 00315 sn_coap_builder_stub.expectedUint16 = 1; 00316 nsdynmemlib_stub.returnCounter = 3; 00317 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00318 return false; 00319 00320 header->msg_id = 2; 00321 sn_coap_builder_stub.expectedUint16 = 2; 00322 coap_transaction_t * tx = coap_message_handler_find_transaction(&buf, 24); 00323 if( tx ){ 00324 tx->client_request = false; 00325 } 00326 sn_coap_builder_stub.expectedHeader = NULL; 00327 if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00328 return false; 00329 00330 sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00331 memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00332 nsdynmemlib_stub.returnCounter = 0; 00333 if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00334 return false; 00335 00336 sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00337 memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00338 nsdynmemlib_stub.returnCounter = 1; 00339 if( 0 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00340 return false; 00341 00342 free(header); 00343 free(sn_coap_protocol_stub.expectedCoap); 00344 sn_coap_protocol_stub.expectedCoap = NULL; 00345 coap_message_handler_destroy(handle); 00346 return true; 00347 } 00348 00349 bool test_coap_message_handler_exec() 00350 { 00351 /* Null as a parameter */ 00352 if( -1 != coap_message_handler_exec(NULL, 0)) 00353 return false; 00354 00355 retCounter = 1; 00356 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00357 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00358 coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); 00359 00360 if( 0 != coap_message_handler_exec(handle, 0)) 00361 return false; 00362 00363 nsdynmemlib_stub.returnCounter = 1; 00364 coap_transaction_t *transact_ptr = transaction_create(); 00365 00366 /* Transaction not timed out*/ 00367 if( 0 != coap_message_handler_exec(handle, 0)) 00368 return false; 00369 00370 if (transaction_cb != 0) 00371 return false; 00372 00373 /* Timed out, no CB */ 00374 if( 0 != coap_message_handler_exec(handle, 300)) 00375 return false; 00376 00377 if (transaction_cb != 0) 00378 return false; 00379 00380 nsdynmemlib_stub.returnCounter = 1; 00381 transact_ptr = transaction_create(); 00382 transact_ptr->resp_cb = transaction_recv_cb; 00383 00384 /* Transaction not timed out */ 00385 if( 0 != coap_message_handler_exec(handle, 0)) 00386 return false; 00387 00388 if (transaction_cb != 0) 00389 return false; 00390 00391 /* Transaction timed out */ 00392 if( 0 != coap_message_handler_exec(handle, 300)) 00393 return false; 00394 00395 if (transaction_cb == 0) 00396 return false; 00397 00398 /* Teardown */ 00399 free(sn_coap_protocol_stub.expectedCoap); 00400 sn_coap_protocol_stub.expectedCoap = NULL; 00401 coap_message_handler_destroy(handle); 00402 return true; 00403 }
Generated on Tue Jul 12 2022 13:25:14 by
1.7.2