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 ARM Limited. All Rights Reserved. 00003 */ 00004 #include "test_coap_message_handler.h" 00005 #include <string.h> 00006 #include "coap_message_handler.h" 00007 #include "sn_coap_protocol_stub.h" 00008 #include "nsdynmemLIB_stub.h" 00009 #include "sn_coap_builder_stub.h" 00010 #include "sn_coap_parser_stub.h" 00011 00012 int retCounter = 0; 00013 int retValue = 0; 00014 00015 static void *own_alloc(uint16_t size) 00016 { 00017 if( retCounter > 0 ){ 00018 retCounter--; 00019 return malloc(size); 00020 } 00021 return NULL; 00022 } 00023 00024 static void own_free(void *ptr) 00025 { 00026 if (ptr) { 00027 free(ptr); 00028 } 00029 } 00030 00031 static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr, void *param) 00032 { 00033 return 0; 00034 } 00035 00036 int resp_recv(int8_t service_id, uint16_t msg_id, sn_coap_hdr_s *response_ptr){ 00037 return retValue; 00038 } 00039 00040 int16_t process_cb(int8_t a, sn_coap_hdr_s *b, coap_transaction_t *c) 00041 { 00042 return retValue; 00043 } 00044 00045 bool test_coap_message_handler_init() 00046 { 00047 if( NULL != coap_message_handler_init(NULL, NULL, NULL) ) 00048 return false; 00049 if( NULL != coap_message_handler_init(&own_alloc, NULL, NULL) ) 00050 return false; 00051 if( NULL != coap_message_handler_init(&own_alloc, &own_free, NULL) ) 00052 return false; 00053 if( NULL != coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function) ) 00054 return false; 00055 retCounter = 1; 00056 sn_coap_protocol_stub.expectedCoap = NULL; 00057 if( NULL != coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function) ) 00058 return false; 00059 retCounter = 1; 00060 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00061 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00062 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00063 if( NULL == handle ) 00064 return false; 00065 free(sn_coap_protocol_stub.expectedCoap); 00066 sn_coap_protocol_stub.expectedCoap = NULL; 00067 free(handle); 00068 return true; 00069 } 00070 00071 bool test_coap_message_handler_destroy() 00072 { 00073 if( -1 != coap_message_handler_destroy(NULL) ) 00074 return false; 00075 retCounter = 1; 00076 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00077 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00078 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00079 00080 if( 0 != coap_message_handler_destroy(handle) ) 00081 return false; 00082 00083 free(sn_coap_protocol_stub.expectedCoap); 00084 return true; 00085 } 00086 00087 bool test_coap_message_handler_find_transaction() 00088 { 00089 if( NULL != coap_message_handler_find_transaction(NULL, 0)) 00090 return false; 00091 retCounter = 1; 00092 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00093 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00094 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00095 00096 uint8_t buf[16]; 00097 memset(&buf, 1, 16); 00098 char uri[3]; 00099 uri[0] = "r"; 00100 uri[1] = "s"; 00101 uri[2] = "\0"; 00102 00103 sn_coap_builder_stub.expectedUint16 = 1; 00104 nsdynmemlib_stub.returnCounter = 3; 00105 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00106 return false; 00107 00108 if( NULL == coap_message_handler_find_transaction(&buf, 24)) 00109 return false; 00110 00111 free(sn_coap_protocol_stub.expectedCoap); 00112 sn_coap_protocol_stub.expectedCoap = NULL; 00113 coap_message_handler_destroy(handle); 00114 return true; 00115 } 00116 00117 bool test_coap_message_handler_coap_msg_process() 00118 { 00119 uint8_t buf[16]; 00120 memset(&buf, 1, 16); 00121 if( -1 != coap_message_handler_coap_msg_process(NULL, 0, buf, 22, NULL, 0, NULL)) 00122 return false; 00123 00124 retCounter = 1; 00125 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00126 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00127 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00128 00129 sn_coap_protocol_stub.expectedHeader = NULL; 00130 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, NULL, 0, process_cb)) 00131 return false; 00132 00133 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00134 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00135 sn_coap_protocol_stub.expectedHeader->coap_status = 66; 00136 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, NULL, 0, process_cb)) 00137 return false; 00138 00139 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00140 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00141 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00142 sn_coap_protocol_stub.expectedHeader->msg_code = 1; 00143 retValue = 0; 00144 if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, NULL, 0, process_cb)) 00145 return false; 00146 00147 nsdynmemlib_stub.returnCounter = 1; 00148 retValue = -1; 00149 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, NULL, 0, process_cb)) 00150 return false; 00151 00152 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00153 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00154 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00155 sn_coap_protocol_stub.expectedHeader->msg_code = 333; 00156 00157 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, NULL, 0, process_cb)) 00158 return false; 00159 00160 sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00161 memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00162 sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; 00163 sn_coap_protocol_stub.expectedHeader->msg_code = 333; 00164 00165 char uri[3]; 00166 uri[0] = "r"; 00167 uri[1] = "s"; 00168 uri[2] = "\0"; 00169 00170 sn_coap_builder_stub.expectedUint16 = 1; 00171 nsdynmemlib_stub.returnCounter = 3; 00172 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00173 return false; 00174 00175 sn_coap_protocol_stub.expectedHeader->msg_id = 2; 00176 // sn_coap_protocol_stub.expectedHeader->token_ptr = (uint8_t*)malloc(4); 00177 // memset(sn_coap_protocol_stub.expectedHeader->token_ptr, 1, 4); 00178 if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, NULL, 0, process_cb)) 00179 return false; 00180 00181 // free(sn_coap_protocol_stub.expectedHeader->token_ptr); 00182 00183 free(sn_coap_protocol_stub.expectedCoap); 00184 sn_coap_protocol_stub.expectedCoap = NULL; 00185 coap_message_handler_destroy(handle); 00186 return true; 00187 } 00188 00189 bool test_coap_message_handler_request_send() 00190 { 00191 retCounter = 1; 00192 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00193 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00194 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00195 00196 uint8_t buf[16]; 00197 memset(&buf, 1, 16); 00198 char uri[3]; 00199 uri[0] = "r"; 00200 uri[1] = "s"; 00201 uri[2] = "\0"; 00202 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00203 return false; 00204 00205 sn_coap_builder_stub.expectedUint16 = 1; 00206 nsdynmemlib_stub.returnCounter = 1; 00207 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00208 return false; 00209 00210 sn_coap_builder_stub.expectedUint16 = 1; 00211 nsdynmemlib_stub.returnCounter = 3; 00212 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00213 return false; 00214 00215 sn_coap_builder_stub.expectedUint16 = 1; 00216 nsdynmemlib_stub.returnCounter = 3; 00217 if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) 00218 return false; 00219 00220 sn_coap_builder_stub.expectedUint16 = 1; 00221 nsdynmemlib_stub.returnCounter = 3; 00222 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00223 return false; 00224 00225 free(sn_coap_protocol_stub.expectedCoap); 00226 sn_coap_protocol_stub.expectedCoap = NULL; 00227 coap_message_handler_destroy(handle); 00228 return true; 00229 } 00230 00231 bool test_coap_message_handler_response_send() 00232 { 00233 if( -1 != coap_message_handler_response_send(NULL, 2, 0, NULL, 1,3,NULL, 0)) 00234 return false; 00235 00236 retCounter = 1; 00237 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00238 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00239 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00240 sn_coap_hdr_s *header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00241 memset(header, 0, sizeof(sn_coap_hdr_s)); 00242 00243 if( -2 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00244 return false; 00245 00246 uint8_t buf[16]; 00247 memset(&buf, 1, 16); 00248 char uri[3]; 00249 uri[0] = "r"; 00250 uri[1] = "s"; 00251 uri[2] = "\0"; 00252 sn_coap_builder_stub.expectedUint16 = 1; 00253 nsdynmemlib_stub.returnCounter = 3; 00254 if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) 00255 return false; 00256 00257 header->msg_id = 2; 00258 sn_coap_builder_stub.expectedUint16 = 2; 00259 coap_transaction_t * tx = coap_message_handler_find_transaction(&buf, 24); 00260 if( tx ){ 00261 tx->client_request = false; 00262 } 00263 sn_coap_builder_stub.expectedHeader = NULL; 00264 if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00265 return false; 00266 00267 sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00268 memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00269 nsdynmemlib_stub.returnCounter = 0; 00270 if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00271 return false; 00272 00273 sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00274 memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); 00275 nsdynmemlib_stub.returnCounter = 1; 00276 if( 0 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0)) 00277 return false; 00278 00279 // free(header); 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_exec() 00287 { 00288 if( -1 != coap_message_handler_exec(NULL, 0)) 00289 return false; 00290 retCounter = 1; 00291 sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00292 memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); 00293 coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); 00294 if( 0 != coap_message_handler_exec(handle, 0)) 00295 return false; 00296 00297 free(sn_coap_protocol_stub.expectedCoap); 00298 sn_coap_protocol_stub.expectedCoap = NULL; 00299 coap_message_handler_destroy(handle); 00300 return true; 00301 }
Generated on Tue Jul 12 2022 17:34:57 by
1.7.2