ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_sn_coap_parser.c Source File

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