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.
Dependents: cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more
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 00026 int retCounter = 0; 00027 int retValue = 0; 00028 00029 static void *own_alloc(uint16_t size) 00030 { 00031 if( retCounter > 0 ){ 00032 retCounter--; 00033 return malloc(size); 00034 } 00035 return NULL; 00036 } 00037 00038 static void own_free(void *ptr) 00039 { 00040 if (ptr) { 00041 free(ptr); 00042 } 00043 } 00044 00045 static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr, void *param) 00046 { 00047 return 0; 00048 } 00049 00050 int resp_recv(int8_t service_id, uint16_t msg_id, sn_coap_hdr_s *response_ptr){ 00051 return retValue; 00052 } 00053 00054 int16_t process_cb(int8_t a, sn_coap_hdr_s *b, coap_transaction_t *c) 00055 { 00056 return retValue; 00057 } 00058 00059 bool test_coap_message_handler_init() 00060 { 00061 if( NULL != coap_message_handler_init(NULL, NULL, NULL) ) 00062 return false; 00063 if( NULL != coap_message_handler_init(&own_alloc, NULL, NULL) ) 00064 return false; 00065 if( NULL != coap_message_handler_init(&own_alloc, &own_free, NULL) ) 00066 return false; 00067 if( NULL != coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function) ) 00068 return false; 00069 retCounter = 1; 00070 sn_coap_protocol_stub.expectedCoap = NULL; 00071 if( NULL != coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function) ) 00072 return false; 00073 retCounter = 1; 00074 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00075 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00076 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00077 if( NULL == handle ) 00078 return false; 00079 free(sn_coap_protocol_stub.expectedCoap); 00080 sn_coap_protocol_stub.expectedCoap = NULL; 00081 free(handle); 00082 return true; 00083 } 00084 00085 bool test_coap_message_handler_destroy() 00086 { 00087 if( -1 != coap_message_handler_destroy(NULL) ) 00088 return false; 00089 retCounter = 1; 00090 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00091 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00092 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00093 00094 if( 0 != coap_message_handler_destroy(handle) ) 00095 return false; 00096 00097 free(sn_coap_protocol_stub.expectedCoap); 00098 return true; 00099 } 00100 00101 bool test_coap_message_handler_find_transaction() 00102 { 00103 if( NULL != coap_message_handler_find_transaction(NULL, 0)) 00104 return false; 00105 retCounter = 1; 00106 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00107 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00108 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00109 00110 uint8_t buf[16]; 00111 memset(&buf, 1, 16); 00112 char uri[3]; 00113 uri[0] = "r"; 00114 uri[1] = "s"; 00115 uri[2] = "\0"; 00116 00117 sn_coap_builder_stub.expectedUint16 = 1; 00118 nsdynmemlib_stub.returnCounter = 3; 00119 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00120 return false; 00121 00122 if( NULL == coap_message_handler_find_transaction(&buf, 24)) 00123 return false; 00124 00125 free(sn_coap_protocol_stub.expectedCoap); 00126 sn_coap_protocol_stub.expectedCoap = NULL; 00127 coap_message_handler_destroy(handle); 00128 return true; 00129 } 00130 00131 bool test_coap_message_handler_coap_msg_process() 00132 { 00133 uint8_t buf[16]; 00134 memset(&buf, 1, 16); 00135 /*Handler is null*/ 00136 if( -1 != coap_message_handler_coap_msg_process(NULL, 0, buf, 22, ns_in6addr_any, NULL, 0, NULL)) 00137 return false; 00138 00139 retCounter = 1; 00140 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00141 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00142 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00143 00144 sn_coap_protocol_stub.expectedHeader = NULL; 00145 /* Coap parse returns null */ 00146 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00147 return false; 00148 00149 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00150 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00151 sn_coap_protocol_stub.expectedHeader->coap_status = 66; 00152 /* Coap library responds */ 00153 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00154 return false; 00155 00156 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00157 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00158 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00159 sn_coap_protocol_stub.expectedHeader->msg_code = 1; 00160 retValue = 0; 00161 /* request received */ 00162 if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00163 return false; 00164 00165 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00166 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00167 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00168 sn_coap_protocol_stub.expectedHeader->msg_code = 1; 00169 nsdynmemlib_stub.returnCounter = 1; 00170 retValue = -1; 00171 if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00172 return false; 00173 00174 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00175 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00176 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00177 sn_coap_protocol_stub.expectedHeader->msg_code = 333; 00178 00179 if( -1 != 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 char uri[3]; 00188 uri[0] = "r"; 00189 uri[1] = "s"; 00190 uri[2] = "\0"; 00191 00192 sn_coap_builder_stub.expectedUint16 = 1; 00193 nsdynmemlib_stub.returnCounter = 3; 00194 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00195 return false; 00196 00197 sn_coap_protocol_stub.expectedHeader->msg_id = 2; 00198 // sn_coap_protocol_stub.expectedHeader->token_ptr = (uint8_t*)malloc(4); 00199 // memset(sn_coap_protocol_stub.expectedHeader->token_ptr, 1, 4); 00200 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) 00201 return false; 00202 00203 // free(sn_coap_protocol_stub.expectedHeader->token_ptr); 00204 00205 free(sn_coap_protocol_stub.expectedCoap); 00206 sn_coap_protocol_stub.expectedCoap = NULL; 00207 coap_message_handler_destroy(handle); 00208 return true; 00209 } 00210 00211 bool test_coap_message_handler_request_send() 00212 { 00213 retCounter = 1; 00214 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00215 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00216 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00217 00218 uint8_t buf[16]; 00219 memset(&buf, 1, 16); 00220 char uri[3]; 00221 uri[0] = "r"; 00222 uri[1] = "s"; 00223 uri[2] = "\0"; 00224 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00225 return false; 00226 00227 sn_coap_builder_stub.expectedUint16 = 1; 00228 nsdynmemlib_stub.returnCounter = 1; 00229 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00230 return false; 00231 00232 sn_coap_builder_stub.expectedUint16 = 1; 00233 nsdynmemlib_stub.returnCounter = 3; 00234 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00235 return false; 00236 00237 sn_coap_builder_stub.expectedUint16 = 1; 00238 nsdynmemlib_stub.returnCounter = 3; 00239 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00240 return false; 00241 00242 sn_coap_builder_stub.expectedUint16 = 1; 00243 nsdynmemlib_stub.returnCounter = 3; 00244 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00245 return false; 00246 00247 free(sn_coap_protocol_stub.expectedCoap); 00248 sn_coap_protocol_stub.expectedCoap = NULL; 00249 coap_message_handler_destroy(handle); 00250 return true; 00251 } 00252 00253 bool test_coap_message_handler_request_delete() 00254 { 00255 retCounter = 1; 00256 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00257 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00258 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00259 00260 uint8_t buf[16]; 00261 memset(&buf, 1, 16); 00262 char uri[3]; 00263 uri[0] = "r"; 00264 uri[1] = "s"; 00265 uri[2] = "\0"; 00266 if( 0 == coap_message_handler_request_delete(NULL, 1, 1)) 00267 return false; 00268 00269 if( 0 == coap_message_handler_request_delete(handle, 1, 1)) 00270 return false; 00271 00272 sn_coap_builder_stub.expectedUint16 = 1; 00273 nsdynmemlib_stub.returnCounter = 3; 00274 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00275 return false; 00276 00277 if( 0 != coap_message_handler_request_delete(handle, 1, 2)) 00278 return false; 00279 00280 free(sn_coap_protocol_stub.expectedCoap); 00281 sn_coap_protocol_stub.expectedCoap = NULL; 00282 coap_message_handler_destroy(handle); 00283 return true; 00284 } 00285 00286 bool test_coap_message_handler_response_send() 00287 { 00288 if( -1 != coap_message_handler_response_send(NULL, 2, 0, NULL, 1,3,NULL, 0)) 00289 return false; 00290 00291 retCounter = 1; 00292 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00293 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00294 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00295 sn_coap_hdr_s *header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00296 memset(header, 0, sizeof(sn_coap_hdr_s)); 00297 00298 if( -2 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00299 return false; 00300 00301 uint8_t buf[16]; 00302 memset(&buf, 1, 16); 00303 char uri[3]; 00304 uri[0] = "r"; 00305 uri[1] = "s"; 00306 uri[2] = "\0"; 00307 sn_coap_builder_stub.expectedUint16 = 1; 00308 nsdynmemlib_stub.returnCounter = 3; 00309 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00310 return false; 00311 00312 header->msg_id = 2; 00313 sn_coap_builder_stub.expectedUint16 = 2; 00314 coap_transaction_t * tx = coap_message_handler_find_transaction(&buf, 24); 00315 if( tx ){ 00316 tx->client_request = false; 00317 } 00318 sn_coap_builder_stub.expectedHeader = NULL; 00319 if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00320 return false; 00321 00322 sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00323 memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00324 nsdynmemlib_stub.returnCounter = 0; 00325 if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00326 return false; 00327 00328 sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00329 memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00330 nsdynmemlib_stub.returnCounter = 1; 00331 if( 0 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00332 return false; 00333 00334 free(header); 00335 free(sn_coap_protocol_stub.expectedCoap); 00336 sn_coap_protocol_stub.expectedCoap = NULL; 00337 coap_message_handler_destroy(handle); 00338 return true; 00339 } 00340 00341 bool test_coap_message_handler_exec() 00342 { 00343 if( -1 != coap_message_handler_exec(NULL, 0)) 00344 return false; 00345 retCounter = 1; 00346 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00347 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00348 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00349 if( 0 != coap_message_handler_exec(handle, 0)) 00350 return false; 00351 00352 free(sn_coap_protocol_stub.expectedCoap); 00353 sn_coap_protocol_stub.expectedCoap = NULL; 00354 coap_message_handler_destroy(handle); 00355 return true; 00356 }
Generated on Tue Jul 12 2022 13:03:19 by
