joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_coap_message_handler.c Source File

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 
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 = -1;
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     if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, NULL, 0, process_cb))
00149         return false;
00150 
00151     sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00152     memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s));
00153     sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK;
00154     sn_coap_protocol_stub.expectedHeader->msg_code = 333;
00155 
00156     if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, NULL, 0, process_cb))
00157         return false;
00158 
00159     sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00160     memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s));
00161     sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK;
00162     sn_coap_protocol_stub.expectedHeader->msg_code = 333;
00163 
00164     char uri[3];
00165     uri[0] = "r";
00166     uri[1] = "s";
00167     uri[2] = "\0";
00168 
00169     sn_coap_builder_stub.expectedUint16 = 1;
00170     nsdynmemlib_stub.returnCounter = 3;
00171     if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv))
00172         return false;
00173 
00174     sn_coap_protocol_stub.expectedHeader->msg_id = 2;
00175 //    sn_coap_protocol_stub.expectedHeader->token_ptr = (uint8_t*)malloc(4);
00176 //    memset(sn_coap_protocol_stub.expectedHeader->token_ptr, 1, 4);
00177     if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, NULL, 0, process_cb))
00178         return false;
00179 
00180 //    free(sn_coap_protocol_stub.expectedHeader->token_ptr);
00181 
00182     free(sn_coap_protocol_stub.expectedCoap);
00183     sn_coap_protocol_stub.expectedCoap = NULL;
00184     coap_message_handler_destroy(handle);
00185     return true;
00186 }
00187 
00188 bool test_coap_message_handler_request_send()
00189 {
00190     retCounter = 1;
00191     sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s));
00192     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00193     coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function);
00194 
00195     uint8_t buf[16];
00196     memset(&buf, 1, 16);
00197     char uri[3];
00198     uri[0] = "r";
00199     uri[1] = "s";
00200     uri[2] = "\0";
00201     if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL))
00202         return false;
00203 
00204     sn_coap_builder_stub.expectedUint16 = 1;
00205     nsdynmemlib_stub.returnCounter = 1;
00206     if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL))
00207         return false;
00208 
00209     sn_coap_builder_stub.expectedUint16 = 1;
00210     nsdynmemlib_stub.returnCounter = 3;
00211     if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL))
00212         return false;
00213 
00214     sn_coap_builder_stub.expectedUint16 = 1;
00215     nsdynmemlib_stub.returnCounter = 3;
00216     if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL))
00217         return false;
00218 
00219     sn_coap_builder_stub.expectedUint16 = 1;
00220     nsdynmemlib_stub.returnCounter = 3;
00221     if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv))
00222         return false;
00223 
00224     free(sn_coap_protocol_stub.expectedCoap);
00225     sn_coap_protocol_stub.expectedCoap = NULL;
00226     coap_message_handler_destroy(handle);
00227     return true;
00228 }
00229 
00230 bool test_coap_message_handler_response_send()
00231 {
00232     if( -1 != coap_message_handler_response_send(NULL, 2, 0, NULL, 1,3,NULL, 0))
00233         return false;
00234 
00235     retCounter = 1;
00236     sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s));
00237     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00238     coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function);
00239     sn_coap_hdr_s *header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00240     memset(header, 0, sizeof(sn_coap_hdr_s));
00241 
00242     if( -2 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0))
00243         return false;
00244 
00245     uint8_t buf[16];
00246     memset(&buf, 1, 16);
00247     char uri[3];
00248     uri[0] = "r";
00249     uri[1] = "s";
00250     uri[2] = "\0";
00251     sn_coap_builder_stub.expectedUint16 = 1;
00252     nsdynmemlib_stub.returnCounter = 3;
00253     if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv))
00254         return false;
00255 
00256     header->msg_id = 2;
00257     sn_coap_builder_stub.expectedUint16 = 2;
00258     coap_transaction_t * tx = coap_message_handler_find_transaction(&buf, 24);
00259     if( tx ){
00260         tx->client_request = false;
00261     }
00262     sn_coap_builder_stub.expectedHeader = NULL;
00263     if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0))
00264         return false;
00265 
00266     sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00267     memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s));
00268     nsdynmemlib_stub.returnCounter = 1;
00269     if( -1 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0))
00270         return false;
00271 
00272     sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00273     memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s));
00274     nsdynmemlib_stub.returnCounter = 2;
00275     if( 0 != coap_message_handler_response_send(handle, 2, 0, header, 1,3,NULL, 0))
00276         return false;
00277 
00278 //    free(header);
00279     free(sn_coap_protocol_stub.expectedCoap);
00280     sn_coap_protocol_stub.expectedCoap = NULL;
00281     coap_message_handler_destroy(handle);
00282     return true;
00283 }
00284 
00285 bool test_coap_message_handler_exec()
00286 {
00287     if( -1 != coap_message_handler_exec(NULL, 0))
00288         return false;
00289     retCounter = 1;
00290     sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s));
00291     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00292     coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function);
00293     if( 0 != coap_message_handler_exec(handle, 0))
00294         return false;
00295 
00296     free(sn_coap_protocol_stub.expectedCoap);
00297     sn_coap_protocol_stub.expectedCoap = NULL;
00298     coap_message_handler_destroy(handle);
00299     return true;
00300 }