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 mbed-os by
test_sn_coap_parser.c
00001 /* 00002 * Copyright (c) 2015 ARM. All rights reserved. 00003 */ 00004 #include "test_sn_coap_parser.h" 00005 #include <string.h> 00006 #include "stdint.h" 00007 #include "sn_nsdl.h" 00008 #include "sn_coap_header.h" 00009 #include "sn_coap_header_internal.h" 00010 #include "sn_coap_protocol_internal.h" 00011 00012 int retCounter = 0; 00013 00014 void* myMalloc(uint16_t size) 00015 { 00016 if( retCounter > 0 ){ 00017 retCounter--; 00018 return malloc(size); 00019 }else { 00020 return NULL; 00021 } 00022 } 00023 00024 void myFree(void* ptr){ 00025 free(ptr); 00026 } 00027 00028 bool test_sn_coap_parser() 00029 { 00030 if( sn_coap_parser_alloc_message(NULL) ) { 00031 return false; 00032 } 00033 00034 if( sn_coap_parser_alloc_options(NULL, NULL) ) { 00035 return false; 00036 } 00037 00038 retCounter = 0; 00039 bool ret = true; 00040 uint8_t* ptr = (uint8_t*)malloc(20); 00041 sn_coap_hdr_s * hdr = sn_coap_parser(NULL, 8, ptr, NULL); 00042 if( hdr != NULL ){ 00043 free(hdr); 00044 ret = false; 00045 } 00046 00047 if( ret ){ 00048 struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00049 coap->sn_coap_protocol_malloc = myMalloc; 00050 coap->sn_coap_protocol_free = myFree; 00051 retCounter = 0; 00052 coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); 00053 hdr = sn_coap_parser(coap, 8, ptr, ver); 00054 if( hdr != NULL ){ 00055 free(hdr); 00056 ret = false; 00057 } 00058 00059 if( ret ){ 00060 retCounter = 1; 00061 hdr = sn_coap_parser(coap, 8, ptr, ver); 00062 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00063 free(hdr); 00064 ret = false; 00065 } 00066 if (hdr) 00067 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00068 } 00069 free(ver); 00070 free(coap); 00071 00072 } 00073 00074 free(ptr); 00075 return ret; 00076 } 00077 00078 bool test_sn_coap_parser_options_parsing() 00079 { 00080 bool ret = true; 00081 uint8_t* ptr = (uint8_t*)malloc(20); 00082 memset(ptr, 0, 20); 00083 ptr[0] = 9; 00084 struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00085 coap->sn_coap_protocol_malloc = myMalloc; 00086 coap->sn_coap_protocol_free = myFree; 00087 00088 retCounter = 1; 00089 coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); 00090 sn_coap_hdr_s * hdr = sn_coap_parser(coap, 8, ptr, ver); 00091 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00092 ret = false; 00093 }else{ 00094 if (hdr) 00095 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00096 ptr[0] = 1; 00097 retCounter = 1; 00098 hdr = sn_coap_parser(coap, 8, ptr, ver); 00099 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00100 ret = false; 00101 }else{ 00102 if (hdr) 00103 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00104 ptr[5] = 255; //this point is sufficient to test parsing of payload 00105 retCounter = 2; 00106 hdr = sn_coap_parser(coap, 6, ptr, ver); 00107 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00108 ret = false; 00109 }else{ 00110 if (hdr) 00111 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00112 ptr[5] = 239; 00113 retCounter = 2; 00114 hdr = sn_coap_parser(coap, 8, ptr, ver); 00115 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00116 ret = false; 00117 }else{ 00118 if (hdr) 00119 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00120 ptr[5] = 254; //15 | 14 00121 retCounter = 2; 00122 hdr = sn_coap_parser(coap, 8, ptr, ver); 00123 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00124 ret = false; 00125 }else{ 00126 if (hdr) 00127 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00128 ptr[5] = 238; //14 | 14 00129 ptr[6] = 6; 00130 ptr[7] = 7; 00131 retCounter = 2; 00132 hdr = sn_coap_parser(coap, 8, ptr, ver); 00133 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00134 ret = false; 00135 }else{ 00136 if (hdr) 00137 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00138 ptr[5] = 221; //13 | 13 00139 ptr[6] = 6; 00140 retCounter = 2; 00141 hdr = sn_coap_parser(coap, 8, ptr, ver); 00142 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00143 ret = false; 00144 } else { 00145 if (hdr) 00146 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00147 } 00148 } 00149 } 00150 } 00151 } 00152 } 00153 } 00154 00155 free(ver); 00156 free(coap); 00157 free(ptr); 00158 return ret; 00159 } 00160 00161 bool test_sn_coap_parser_options_parsing_switches() 00162 { 00163 bool ret = true; 00164 uint8_t* ptr = (uint8_t*)malloc(20); 00165 memset(ptr, 0, 20); 00166 struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00167 coap->sn_coap_protocol_malloc = myMalloc; 00168 coap->sn_coap_protocol_free = myFree; 00169 coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); 00170 00171 //These should be tested: 00172 /* 00173 COAP_OPTION_IF_MATCH = 1, 00174 COAP_OPTION_URI_HOST = 3, 00175 COAP_OPTION_ETAG = 4, 00176 COAP_OPTION_IF_NONE_MATCH = 5, 00177 COAP_OPTION_OBSERVE = 6, 00178 COAP_OPTION_URI_PORT = 7, 00179 COAP_OPTION_LOCATION_PATH = 8, 00180 COAP_OPTION_URI_PATH = 11, 00181 COAP_OPTION_CONTENT_FORMAT = 12, 00182 COAP_OPTION_MAX_AGE = 14, 00183 COAP_OPTION_URI_QUERY = 15, 00184 COAP_OPTION_ACCEPT = 17, 00185 COAP_OPTION_LOCATION_QUERY = 20, 00186 COAP_OPTION_BLOCK2 = 23, 00187 COAP_OPTION_BLOCK1 = 27, 00188 COAP_OPTION_SIZE2 = 28, 00189 COAP_OPTION_PROXY_URI = 35, 00190 COAP_OPTION_PROXY_SCHEME = 39, 00191 COAP_OPTION_SIZE1 = 60 00192 */ 00193 00194 ptr[0] = 1; 00195 00196 retCounter = 2; 00197 ptr[5] = 17; //1 | 1 (number | length) 00198 sn_coap_hdr_s * hdr = sn_coap_parser(coap, 8, ptr, ver); 00199 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00200 return false; 00201 } 00202 00203 if (hdr) 00204 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00205 ptr[5] = 51; //1 | 3 00206 retCounter = 2; 00207 hdr = sn_coap_parser(coap, 8, ptr, ver); 00208 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00209 return false; 00210 } 00211 00212 if (hdr) 00213 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00214 ptr[5] = 48; //3 | 0 00215 retCounter = 3; 00216 hdr = sn_coap_parser(coap, 8, ptr, ver); 00217 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00218 return false; 00219 } 00220 00221 if (hdr) 00222 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00223 ptr[5] = 51; //3 | 3 00224 retCounter = 3; 00225 hdr = sn_coap_parser(coap, 8, ptr, ver); 00226 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00227 return false; 00228 } 00229 00230 if (hdr) 00231 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00232 ptr[5] = 51; //3 | 3 00233 retCounter = 4; 00234 //overflows, so not valid data 00235 hdr = sn_coap_parser(coap, 8, ptr, ver); 00236 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00237 return false; 00238 } 00239 if (hdr) 00240 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00241 ptr[5] = 68; //4 | 4 00242 retCounter = 3; 00243 hdr = sn_coap_parser(coap, 8, ptr, ver); 00244 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00245 return false; 00246 } 00247 if (hdr) 00248 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00249 ptr[5] = 68; //4 | 4 00250 retCounter = 4; 00251 //This should test if (ret_status >= 0) {} 00252 hdr = sn_coap_parser(coap, 8, ptr, ver); 00253 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00254 return false; 00255 } 00256 if (hdr) 00257 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00258 ptr[5] = 85; //5 | 5 00259 retCounter = 2; 00260 hdr = sn_coap_parser(coap, 8, ptr, ver); 00261 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00262 return false; 00263 } 00264 if (hdr) 00265 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00266 ptr[5] = 102; //6 | 6 00267 retCounter = 3; 00268 hdr = sn_coap_parser(coap, 8, ptr, ver); 00269 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00270 return false; 00271 } 00272 if (hdr) 00273 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00274 ptr[5] = 97; //6 | 1 00275 retCounter = 3; 00276 hdr = sn_coap_parser(coap, 8, ptr, ver); 00277 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00278 return false; 00279 } 00280 if (hdr) 00281 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00282 ptr[5] = 97; //6 | 1 00283 retCounter = 4; 00284 hdr = sn_coap_parser(coap, 8, ptr, ver); 00285 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00286 return false; 00287 } 00288 if (hdr) 00289 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00290 ptr[5] = 119; //7 | 7 00291 retCounter = 3; 00292 hdr = sn_coap_parser(coap, 8, ptr, ver); 00293 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00294 return false; 00295 } 00296 if (hdr) 00297 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00298 ptr[5] = 113; //7 | 1 00299 retCounter = 3; 00300 hdr = sn_coap_parser(coap, 8, ptr, ver); 00301 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00302 return false; 00303 } 00304 if (hdr) 00305 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00306 ptr[5] = 113; //7 | 1 00307 retCounter = 4; 00308 hdr = sn_coap_parser(coap, 8, ptr, ver); 00309 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00310 return false; 00311 } 00312 if (hdr) 00313 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00314 ptr[5] = 128; //8 | 8 00315 retCounter = 3; 00316 hdr = sn_coap_parser(coap, 8, ptr, ver); 00317 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00318 return false; 00319 } 00320 if (hdr) 00321 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00322 ptr[5] = 136; //8 | 8 00323 retCounter = 4; 00324 //This should test if (ret_status >= 0) {} 00325 hdr = sn_coap_parser(coap, 8, ptr, ver); 00326 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00327 return false; 00328 } 00329 if (hdr) 00330 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00331 ptr[5] = 187; //11 | 11 00332 retCounter = 2; 00333 hdr = sn_coap_parser(coap, 8, ptr, ver); 00334 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00335 return false; 00336 } 00337 if (hdr) 00338 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00339 ptr[5] = 187; //11 | 11 00340 retCounter = 3; 00341 //This should test if (ret_status >= 0) {} 00342 hdr = sn_coap_parser(coap, 8, ptr, ver); 00343 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00344 return false; 00345 } 00346 if (hdr) 00347 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00348 ptr[5] = 204; //12 | 12 00349 retCounter = 2; 00350 hdr = sn_coap_parser(coap, 8, ptr, ver); 00351 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00352 return false; 00353 } 00354 if (hdr) 00355 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00356 ptr[5] = 193; //12 | 1 00357 retCounter = 2; 00358 hdr = sn_coap_parser(coap, 8, ptr, ver); 00359 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00360 return false; 00361 } 00362 if (hdr) 00363 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00364 ptr[5] = 193; //12 | 1 00365 retCounter = 3; 00366 hdr = sn_coap_parser(coap, 8, ptr, ver); 00367 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00368 return false; 00369 } 00370 if (hdr) 00371 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00372 ptr[5] = 216; //13 | 8 00373 ptr[6] = 1; //1 -> 14 00374 retCounter = 3; 00375 hdr = sn_coap_parser(coap, 8, ptr, ver); 00376 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00377 return false; 00378 } 00379 if (hdr) 00380 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00381 ptr[5] = 209; //13 | 1 00382 ptr[6] = 1; //1 -> 14 00383 retCounter = 2; 00384 hdr = sn_coap_parser(coap, 8, ptr, ver); 00385 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00386 return false; 00387 } 00388 if (hdr) 00389 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00390 ptr[5] = 210; //13 | 2 00391 ptr[6] = 1; //1 -> 14 00392 retCounter = 3; 00393 hdr = sn_coap_parser(coap, 6, ptr, ver); 00394 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00395 return false; 00396 } 00397 if (hdr) 00398 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00399 ptr[5] = 208; //13 | 0 00400 ptr[6] = 2; //2 -> 15 ??? 00401 retCounter = 3; 00402 hdr = sn_coap_parser(coap, 8, ptr, ver); 00403 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00404 return false; 00405 } 00406 if (hdr) 00407 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00408 ptr[5] = 209; //13 | 1 00409 ptr[6] = 2; //2 -> 15 ??? 00410 retCounter = 5; 00411 //This should test if (ret_status >= 0) {} 00412 hdr = sn_coap_parser(coap, 7, ptr, ver); 00413 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00414 return false; 00415 } 00416 if (hdr) 00417 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00418 ptr[5] = 208; //13 | 0 00419 ptr[6] = 4; 00420 retCounter = 3; 00421 hdr = sn_coap_parser(coap, 8, ptr, ver); 00422 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00423 return false; 00424 } 00425 if (hdr) 00426 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00427 ptr[5] = 209; //13 | 1 00428 ptr[6] = 4; 00429 retCounter = 5; 00430 //This should test if (ret_status >= 0) {} 00431 hdr = sn_coap_parser(coap, 7, ptr, ver); 00432 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00433 return false; 00434 } 00435 if (hdr) 00436 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00437 ptr[5] = 208; //13 | 0 00438 ptr[6] = 7; 00439 retCounter = 3; 00440 hdr = sn_coap_parser(coap, 8, ptr, ver); 00441 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00442 return false; 00443 } 00444 if (hdr) 00445 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00446 ptr[5] = 209; //13 | 1 00447 ptr[6] = 7; 00448 retCounter = 5; 00449 //This should test if (ret_status >= 0) {} 00450 hdr = sn_coap_parser(coap, 7, ptr, ver); 00451 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00452 return false; 00453 } 00454 if (hdr) 00455 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00456 ptr[5] = 216; //13 | 8 00457 ptr[6] = 10; 00458 retCounter = 3; 00459 hdr = sn_coap_parser(coap, 8, ptr, ver); 00460 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00461 return false; 00462 } 00463 if (hdr) 00464 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00465 ptr[5] = 209; //13 | 1 00466 ptr[6] = 10; 00467 retCounter = 2; 00468 hdr = sn_coap_parser(coap, 8, ptr, ver); 00469 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00470 return false; 00471 } 00472 if (hdr) 00473 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00474 ptr[5] = 210; //13 | 2 00475 ptr[6] = 10; 00476 retCounter = 3; 00477 hdr = sn_coap_parser(coap, 8, ptr, ver); 00478 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00479 return false; 00480 } 00481 if (hdr) 00482 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00483 ptr[5] = 216; //13 | 8 00484 ptr[6] = 14; 00485 retCounter = 3; 00486 hdr = sn_coap_parser(coap, 8, ptr, ver); 00487 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00488 return false; 00489 } 00490 if (hdr) 00491 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00492 ptr[5] = 209; //13 | 1 00493 ptr[6] = 14; 00494 retCounter = 2; 00495 hdr = sn_coap_parser(coap, 8, ptr, ver); 00496 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00497 return false; 00498 } 00499 if (hdr) 00500 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00501 ptr[5] = 210; //13 | 2 00502 ptr[6] = 14; 00503 retCounter = 3; 00504 hdr = sn_coap_parser(coap, 8, ptr, ver); 00505 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00506 return false; 00507 } 00508 if (hdr) 00509 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00510 ptr[5] = 208; //13 | 0 00511 ptr[6] = 22; 00512 retCounter = 3; 00513 hdr = sn_coap_parser(coap, 8, ptr, ver); 00514 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00515 return false; 00516 } 00517 if (hdr) 00518 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00519 ptr[5] = 209; //13 | 1 00520 ptr[6] = 22; 00521 retCounter = 3; 00522 hdr = sn_coap_parser(coap, 8, ptr, ver); 00523 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00524 return false; 00525 } 00526 if (hdr) 00527 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00528 ptr[5] = 209; //13 | 1 00529 ptr[6] = 22; 00530 retCounter = 4; 00531 hdr = sn_coap_parser(coap, 7, ptr, ver); 00532 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00533 return false; 00534 } 00535 if (hdr) 00536 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00537 ptr[5] = 208; //13 | 0 00538 ptr[6] = 26; 00539 retCounter = 2; 00540 hdr = sn_coap_parser(coap, 8, ptr, ver); 00541 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00542 return false; 00543 } 00544 if (hdr) 00545 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00546 ptr[5] = 208; //13 | 0 00547 ptr[6] = 47; 00548 retCounter = 2; 00549 hdr = sn_coap_parser(coap, 8, ptr, ver); 00550 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00551 return false; 00552 } 00553 if (hdr) 00554 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00555 ptr[5] = 216; //13 | 8 00556 ptr[6] = 47; 00557 retCounter = 3; 00558 hdr = sn_coap_parser(coap, 8, ptr, ver); 00559 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00560 return false; 00561 } 00562 if (hdr) 00563 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00564 ptr[5] = 216; //13 | 8 00565 ptr[6] = 47; 00566 retCounter = 3; 00567 hdr = sn_coap_parser(coap, 8, ptr, ver); 00568 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00569 return false; 00570 } 00571 if (hdr) 00572 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00573 ptr[5] = 210; //13 | 2 00574 ptr[6] = 47; 00575 retCounter = 4; 00576 hdr = sn_coap_parser(coap, 8, ptr, ver); 00577 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00578 return false; 00579 } 00580 if (hdr) 00581 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00582 ptr[5] = 216; //13 | 8 00583 ptr[6] = 15; 00584 retCounter = 3; 00585 hdr = sn_coap_parser(coap, 8, ptr, ver); 00586 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00587 return false; 00588 } 00589 if (hdr) 00590 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00591 ptr[5] = 210; //13 | 2 00592 ptr[6] = 15; 00593 retCounter = 4; 00594 hdr = sn_coap_parser(coap, 8, ptr, ver); 00595 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00596 return false; 00597 } 00598 if (hdr) 00599 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00600 ptr[5] = 216; //13 | 8 00601 ptr[6] = 15; 00602 retCounter = 3; 00603 hdr = sn_coap_parser(coap, 8, ptr, ver); 00604 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00605 return false; 00606 } 00607 if (hdr) 00608 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00609 00610 ptr[5] = 209; //13 | 1 00611 ptr[6] = 10; 00612 retCounter = 4; 00613 hdr = sn_coap_parser(coap, 8, ptr, ver); 00614 if( hdr == NULL ){ 00615 return false; 00616 } 00617 00618 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00619 00620 00621 free(ver); 00622 free(coap); 00623 free(ptr); 00624 return ret; 00625 } 00626 00627 bool test_sn_coap_parser_options_count_needed_memory_multiple_option() 00628 { 00629 bool ret = true; 00630 uint8_t* ptr = (uint8_t*)malloc(65635); 00631 memset(ptr, 0, 65635); 00632 struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00633 coap->sn_coap_protocol_malloc = myMalloc; 00634 coap->sn_coap_protocol_free = myFree; 00635 coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); 00636 00637 //Some of these should be tested: 00638 //These 6 will test sn_coap_parser_options_count_needed_memory_multiple_option overflows 00639 ptr[0] = 1; 00640 ptr[5] = 0x4d; //4 | 13 00641 ptr[6] = 254; 00642 retCounter = 4; 00643 //This should test if (ret_status >= 0) {} 00644 sn_coap_hdr_s * hdr = sn_coap_parser(coap, 8, ptr, ver); 00645 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00646 ret = false; 00647 goto end; 00648 } 00649 if (hdr) 00650 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00651 00652 ptr[0] = 1; 00653 ptr[5] = 0x8d; //4 | 13 00654 ptr[6] = 254; 00655 retCounter = 4; 00656 //This should test if (ret_status >= 0) {} 00657 hdr = sn_coap_parser(coap, 8, ptr, ver); 00658 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00659 ret = false; 00660 goto end; 00661 } 00662 if (hdr) 00663 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00664 00665 ptr[0] = 1; 00666 ptr[5] = 0xbd; 00667 ptr[6] = 254; 00668 retCounter = 4; 00669 //This should test if (ret_status >= 0) {} 00670 hdr = sn_coap_parser(coap, 8, ptr, ver); 00671 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00672 ret = false; 00673 goto end; 00674 } 00675 if (hdr) 00676 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00677 00678 ptr[0] = 1; 00679 ptr[5] = 0xdd; 00680 ptr[6] = 2; 00681 ptr[7] = 254; 00682 retCounter = 4; 00683 //This should test if (ret_status >= 0) {} 00684 hdr = sn_coap_parser(coap, 8, ptr, ver); 00685 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00686 ret = false; 00687 goto end; 00688 } 00689 if (hdr) 00690 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00691 00692 ptr[0] = 1; 00693 ptr[5] = 0xdd; 00694 ptr[6] = 4; 00695 ptr[7] = 254; 00696 retCounter = 4; 00697 //This should test if (ret_status >= 0) {} 00698 hdr = sn_coap_parser(coap, 8, ptr, ver); 00699 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00700 ret = false; 00701 goto end; 00702 } 00703 if (hdr) 00704 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00705 00706 ptr[0] = 1; 00707 ptr[5] = 0xdd; 00708 ptr[6] = 7; 00709 ptr[7] = 254; 00710 retCounter = 4; 00711 //This should test if (ret_status >= 0) {} 00712 hdr = sn_coap_parser(coap, 8, ptr, ver); 00713 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00714 ret = false; 00715 goto end; 00716 } 00717 if (hdr) 00718 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00719 00720 ptr[0] = 1; 00721 ptr[5] = 0x81; 00722 ptr[6] = 0x00; 00723 ptr[7] = 0x20; 00724 retCounter = 4; 00725 //This should test if (ret_status >= 0) {} 00726 hdr = sn_coap_parser(coap, 8, ptr, ver); 00727 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00728 ret = false; 00729 goto end; 00730 } 00731 if (hdr) 00732 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00733 00734 ptr[0] = 1; 00735 ptr[5] = 0x81; 00736 ptr[6] = 0x00; 00737 ptr[7] = 0x00; 00738 retCounter = 4; 00739 //This should test if (ret_status >= 0) {} 00740 hdr = sn_coap_parser(coap, 6, ptr, ver); 00741 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00742 ret = false; 00743 goto end; 00744 } 00745 if (hdr) 00746 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00747 00748 ptr[0] = 1; 00749 ptr[5] = 0x81; 00750 ptr[6] = 0x00; 00751 ptr[7] = 0x0d; 00752 retCounter = 4; 00753 //This should test if (ret_status >= 0) {} 00754 hdr = sn_coap_parser(coap, 8, ptr, ver); 00755 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00756 ret = false; 00757 goto end; 00758 } 00759 if (hdr) 00760 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00761 00762 ptr[0] = 1; 00763 ptr[5] = 0x81; 00764 ptr[6] = 0x00; 00765 ptr[7] = 0x0e; 00766 retCounter = 4; 00767 //This should test if (ret_status >= 0) {} 00768 hdr = sn_coap_parser(coap, 8, ptr, ver); 00769 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00770 ret = false; 00771 goto end; 00772 } 00773 if (hdr) 00774 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00775 00776 ptr[0] = 1; 00777 ptr[5] = 0x81; 00778 ptr[6] = 0x00; 00779 ptr[7] = 0x0f; 00780 retCounter = 4; 00781 //This should test if (ret_status >= 0) {} 00782 hdr = sn_coap_parser(coap, 8, ptr, ver); 00783 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00784 ret = false; 00785 goto end; 00786 } 00787 if (hdr) 00788 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00789 00790 end: 00791 free(ver); 00792 free(coap); 00793 free(ptr); 00794 return ret; 00795 } 00796 00797 bool test_sn_coap_parser_options_parse_multiple_options() 00798 { 00799 bool ret = true; 00800 uint8_t* ptr = (uint8_t*)malloc(33); 00801 memset(ptr, 0, 33); 00802 struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00803 coap->sn_coap_protocol_malloc = myMalloc; 00804 coap->sn_coap_protocol_free = myFree; 00805 coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); 00806 00807 ptr[0] = 0x60; 00808 ptr[4] = 0x82; //opt 8 & len 2 00809 ptr[5] = 0x00; 00810 ptr[6] = 0x00; 00811 ptr[7] = 0x0d; 00812 ptr[8] = 0x00; 00813 ptr[9] = 0x6f; 00814 ptr[10] = 0x6d; 00815 ptr[11] = 0x61; 00816 ptr[12] = 0x69; 00817 ptr[13] = 0x6e; 00818 retCounter = 4; 00819 sn_coap_hdr_s *hdr = sn_coap_parser(coap, 14, ptr, ver); 00820 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00821 ret = false; 00822 goto end2; 00823 } 00824 if (hdr) 00825 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00826 00827 00828 ptr[7] = 0x0e; 00829 ptr[8] = 0xff; 00830 ptr[9] = 0x00; 00831 retCounter = 4; 00832 hdr = sn_coap_parser(coap, 14, ptr, ver); 00833 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00834 ret = false; 00835 goto end2; 00836 } 00837 if (hdr) 00838 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00839 00840 ptr[0] = 0x60; 00841 ptr[4] = 0x42; 00842 ptr[5] = 0x00; 00843 ptr[6] = 0x00; 00844 ptr[7] = 0x06; 00845 ptr[8] = 0x00; 00846 ptr[9] = 0x6f; 00847 ptr[10] = 0x6d; 00848 ptr[11] = 0x61; 00849 ptr[12] = 0x69; 00850 ptr[13] = 0x6e; 00851 retCounter = 4; 00852 hdr = sn_coap_parser(coap, 14, ptr, ver); 00853 if( hdr == NULL ){ 00854 ret = false; 00855 goto end2; 00856 }else{ 00857 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00858 } 00859 00860 free(ptr); 00861 ptr = (uint8_t*)malloc(10); 00862 memset(ptr, 0, 10); 00863 00864 //this should be checked after bug http://jira.arm.com/browse/IOTCLT-244 is fixed 00865 //This is illegal options message, but gets parsed though 00866 //this test passes because parsing of data still fails (expected) 00867 ptr[0] = 0x60; 00868 ptr[4] = 0x82; 00869 ptr[5] = 0x00; 00870 ptr[6] = 0x00; 00871 ptr[7] = 0x06; 00872 ptr[8] = 0x00; 00873 ptr[9] = 0x6f; 00874 retCounter = 4; 00875 hdr = sn_coap_parser(coap, 10, ptr, ver); 00876 if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ 00877 ret = false; 00878 goto end2; 00879 } 00880 if (hdr) 00881 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00882 00883 end2: 00884 free(ver); 00885 free(coap); 00886 free(ptr); 00887 return ret; 00888 } 00889 00890 bool test_sn_coap_parser_parsing() 00891 { 00892 bool ret = true; 00893 uint8_t* ptr = (uint8_t*)malloc(33); 00894 memset(ptr, 0, 33); 00895 struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00896 coap->sn_coap_protocol_malloc = myMalloc; 00897 coap->sn_coap_protocol_free = myFree; 00898 coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); 00899 00900 ptr[0] = 0x60; 00901 ptr[4] = 0x42; 00902 ptr[5] = 0x00; 00903 ptr[6] = 0x00; 00904 retCounter = 4; 00905 00906 //TODO: add sn_coap_parser_parsing related stuff 00907 ptr[7] = 0x20; 00908 ptr[8] = 0x00; 00909 00910 sn_coap_hdr_s *hdr = NULL; 00911 00912 //this should test parsing without payload marker, but it is not possible 00913 // hdr = sn_coap_parser(coap, 9, ptr, ver); 00914 00915 // if( hdr != NULL ){ 00916 // ret = false; 00917 // goto end3; 00918 // } 00919 // sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00920 00921 ptr[0] = 0x60; 00922 ptr[4] = 0x42; 00923 ptr[5] = 0x00; 00924 ptr[6] = 0x00; 00925 ptr[7] = 0x06; 00926 ptr[8] = 0x00; 00927 ptr[9] = 0x6f; 00928 ptr[10] = 0x6d; 00929 ptr[11] = 0x61; 00930 ptr[12] = 0x69; 00931 ptr[13] = 0x6e; 00932 ptr[14] = 0xff; 00933 ptr[15] = 0x1; 00934 retCounter = 4; 00935 00936 hdr = sn_coap_parser(coap, 16, ptr, ver); 00937 00938 if( hdr == NULL ){ 00939 ret = false; 00940 goto end3; 00941 } 00942 00943 sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); 00944 00945 end3: 00946 free(ver); 00947 free(coap); 00948 free(ptr); 00949 return ret; 00950 } 00951 00952 bool test_sn_coap_parser_release_allocated_coap_msg_mem() 00953 { 00954 struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); 00955 coap->sn_coap_protocol_malloc = myMalloc; 00956 coap->sn_coap_protocol_free = myFree; 00957 retCounter = 99; 00958 00959 sn_coap_parser_release_allocated_coap_msg_mem( NULL, NULL ); 00960 00961 sn_coap_hdr_s* ptr = (sn_coap_hdr_s*)myMalloc(sizeof(sn_coap_hdr_s)); 00962 ptr->uri_path_ptr = (uint8_t*)malloc(sizeof(uint8_t)); 00963 ptr->token_ptr = (uint8_t*)malloc(sizeof(uint8_t)); 00964 //ptr->payload_ptr = (uint8_t*)malloc(sizeof(uint8_t)); 00965 ptr->options_list_ptr = (sn_coap_options_list_s*)myMalloc(sizeof(sn_coap_options_list_s)); 00966 00967 ptr->options_list_ptr->max_age = 1; 00968 ptr->options_list_ptr->proxy_uri_ptr = (uint8_t*)malloc(sizeof(uint8_t)); 00969 ptr->options_list_ptr->etag_ptr = (uint8_t*)malloc(sizeof(uint8_t)); 00970 ptr->options_list_ptr->uri_host_ptr = (uint8_t*)malloc(sizeof(uint8_t)); 00971 ptr->options_list_ptr->location_path_ptr = (uint8_t*)malloc(sizeof(uint8_t)); 00972 ptr->options_list_ptr->uri_port = 8; 00973 ptr->options_list_ptr->location_query_ptr = (uint8_t*)malloc(sizeof(uint8_t)); 00974 ptr->options_list_ptr->observe = 0; 00975 ptr->options_list_ptr->uri_query_ptr = (uint8_t*)malloc(sizeof(uint8_t)); 00976 00977 sn_coap_parser_release_allocated_coap_msg_mem( coap, ptr ); 00978 00979 free(coap); 00980 return true; //this is a memory leak check, so that will pass/fail 00981 } 00982
Generated on Tue Jul 12 2022 13:16:15 by
