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 libCoap_builder_test.cpp Source File

libCoap_builder_test.cpp

00001 /*
00002  * Copyright (c) 2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "CppUTest/TestHarness.h"
00017 #include <string.h>
00018 #include <stdint.h>
00019 #include "sn_nsdl.h"
00020 #include "sn_coap_header.h"
00021 #include "sn_coap_protocol_internal.h"
00022 
00023 #include "sn_coap_header_check_stub.h"
00024 #include "sn_coap_parser_stub.h"
00025 
00026 sn_coap_hdr_s coap_header;
00027 sn_coap_options_list_s option_list;
00028 uint8_t buffer[256];
00029 uint8_t temp[10];
00030 
00031 uint8_t retCounter = 0;
00032 
00033 static void *own_alloc(uint16_t size)
00034 {
00035     if( retCounter > 0 ){
00036         retCounter--;
00037         return malloc(size);
00038     }
00039     return NULL;
00040 }
00041 
00042 static void own_free(void *ptr)
00043 {
00044     free(ptr);
00045 }
00046 
00047 TEST_GROUP(libCoap_builder)
00048 {
00049     void setup() {
00050         sn_coap_header_check_stub.expectedInt8 = 0;
00051         retCounter = 0;
00052         memset(&coap_header, 0, sizeof(sn_coap_hdr_s));
00053         memset(&option_list, 0, sizeof(sn_coap_options_list_s));
00054 
00055         coap_header.options_list_ptr = &option_list;
00056         coap_header.msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
00057         coap_header.msg_code = COAP_MSG_CODE_RESPONSE_CREATED;
00058         coap_header.msg_id = 12;
00059     }
00060 
00061     void teardown() {
00062 
00063     }
00064 };
00065 
00066 TEST(libCoap_builder, build_confirmable_response)
00067 {
00068     struct coap_s handle;
00069     sn_coap_hdr_s *response = NULL;
00070 
00071     handle.sn_coap_protocol_malloc = &own_alloc;
00072     handle.sn_coap_protocol_free = &own_free;
00073 
00074     coap_header.msg_type = COAP_MSG_TYPE_RESET;
00075     coap_header.msg_code = COAP_MSG_CODE_REQUEST_GET;
00076     coap_header.msg_id = 12;
00077 
00078     //NULL pointer
00079     CHECK(sn_coap_build_response(NULL, NULL, 0) == NULL);
00080     CHECK(sn_coap_build_response(&handle, NULL, 0) == NULL);
00081     CHECK(sn_coap_build_response(NULL, &coap_header, 0) == NULL);
00082     CHECK(sn_coap_build_response(&handle, &coap_header, COAP_MSG_CODE_RESPONSE_CONTENT) == NULL);
00083 
00084     retCounter = 1;
00085     CHECK(sn_coap_build_response(&handle, &coap_header, COAP_MSG_CODE_RESPONSE_CONTENT) == NULL);
00086 
00087     coap_header.msg_type = COAP_MSG_TYPE_CONFIRMABLE;
00088 
00089     retCounter = 2;
00090     response = sn_coap_build_response(&handle, &coap_header, COAP_MSG_CODE_RESPONSE_CONTENT);
00091 
00092     CHECK(response != NULL);
00093     CHECK(response->msg_type == COAP_MSG_TYPE_ACKNOWLEDGEMENT);
00094     CHECK(response->msg_id == 12);
00095     CHECK(response->msg_code == COAP_MSG_CODE_RESPONSE_CONTENT);
00096 
00097     own_free(response);
00098 }
00099 
00100 TEST(libCoap_builder, build_non_confirmable_response)
00101 {
00102     struct coap_s handle;
00103     sn_coap_hdr_s *response = NULL;
00104     uint8_t token_val = 0x99;
00105 
00106     handle.sn_coap_protocol_malloc = &own_alloc;
00107     handle.sn_coap_protocol_free = &own_free;
00108 
00109     coap_header.msg_type = COAP_MSG_TYPE_NON_CONFIRMABLE;
00110     coap_header.msg_code = COAP_MSG_CODE_REQUEST_GET;
00111     coap_header.msg_id = 12;
00112     coap_header.token_len = 1;
00113     coap_header.token_ptr = &token_val;
00114 
00115     retCounter = 1;
00116     CHECK( NULL == sn_coap_build_response(&handle, &coap_header, COAP_MSG_CODE_RESPONSE_CONTENT));
00117 
00118     retCounter = 2;
00119     response = sn_coap_build_response(&handle, &coap_header, COAP_MSG_CODE_RESPONSE_CONTENT);
00120 
00121     CHECK(response != NULL);
00122     CHECK(response->msg_type == COAP_MSG_TYPE_NON_CONFIRMABLE);
00123     CHECK(response->msg_code == COAP_MSG_CODE_RESPONSE_CONTENT);
00124     CHECK(response->token_ptr != NULL);
00125     CHECK(memcmp(response->token_ptr, coap_header.token_ptr, coap_header.token_len) == 0);
00126     CHECK(response->token_len == coap_header.token_len);
00127 
00128     own_free(response->token_ptr);
00129     own_free(response);
00130 }
00131 
00132 
00133 TEST(libCoap_builder, build_message_negative_cases)
00134 {
00135     // Null pointers as a parameter
00136     CHECK(sn_coap_builder(NULL, NULL) == -2);
00137     CHECK(sn_coap_builder(NULL, &coap_header) == -2);
00138     CHECK(sn_coap_builder(buffer, NULL) == -2);
00139 
00140     // Invalid option length
00141     coap_header.token_ptr = temp;
00142     CHECK(sn_coap_builder(buffer, &coap_header) == -1);
00143 }
00144 
00145 TEST(libCoap_builder, build_message_ok_cases)
00146 {
00147     CHECK(sn_coap_builder(buffer, &coap_header) == 4);
00148 }
00149 
00150 TEST(libCoap_builder, build_message_options_token)
00151 {
00152     coap_header.token_ptr = temp;
00153     coap_header.token_len = 2;
00154     CHECK(sn_coap_builder(buffer, &coap_header) == 6);
00155 }
00156 
00157 TEST(libCoap_builder, build_message_options_uri_path)
00158 {
00159     coap_header.uri_path_ptr = temp;
00160     coap_header.uri_path_len = 2;
00161     CHECK(sn_coap_builder(buffer, &coap_header) == 7);
00162 }
00163 
00164 TEST(libCoap_builder, build_message_options_content_type)
00165 {
00166     coap_header.content_type_ptr = temp;
00167     coap_header.content_type_len = 2;
00168     CHECK(sn_coap_builder(buffer, &coap_header) == 7);
00169 }
00170 
00171 TEST(libCoap_builder, build_message_options_max_age)
00172 {
00173     coap_header.options_list_ptr->max_age_ptr = temp;
00174     coap_header.options_list_ptr->max_age_len = 2;
00175     CHECK(sn_coap_builder(buffer, &coap_header) == 8);
00176 }
00177 
00178 TEST(libCoap_builder, build_message_options_proxy_uri)
00179 {
00180     coap_header.options_list_ptr->proxy_uri_ptr = temp;
00181     coap_header.options_list_ptr->proxy_uri_len = 2;
00182     CHECK(sn_coap_builder(buffer, &coap_header) == 8);
00183 }
00184 
00185 TEST(libCoap_builder, build_message_options_etag)
00186 {
00187     coap_header.options_list_ptr->etag_ptr = temp;
00188     coap_header.options_list_ptr->etag_len = 2;
00189     CHECK(sn_coap_builder(buffer, &coap_header) == 7);
00190 }
00191 
00192 TEST(libCoap_builder, build_message_options_uri_host)
00193 {
00194     coap_header.options_list_ptr->uri_host_ptr = temp;
00195     coap_header.options_list_ptr->uri_host_len = 2;
00196     CHECK(sn_coap_builder(buffer, &coap_header) == 7);
00197 }
00198 
00199 TEST(libCoap_builder, build_message_options_location_path)
00200 {
00201     coap_header.options_list_ptr->location_path_ptr = temp;
00202     coap_header.options_list_ptr->location_path_len = 2;
00203     CHECK(sn_coap_builder(buffer, &coap_header) == 7);
00204 }
00205 
00206 TEST(libCoap_builder, build_message_options_uri_port)
00207 {
00208     coap_header.options_list_ptr->uri_port_ptr = temp;
00209     coap_header.options_list_ptr->uri_port_len = 2;
00210     CHECK(sn_coap_builder(buffer, &coap_header) == 7);
00211 }
00212 
00213 TEST(libCoap_builder, build_message_options_location_query)
00214 {
00215     coap_header.options_list_ptr->location_query_ptr = temp;
00216     coap_header.options_list_ptr->location_query_len = 2;
00217     CHECK(sn_coap_builder(buffer, &coap_header) == 8);
00218 }
00219 
00220 TEST(libCoap_builder, build_message_options_observe)
00221 {
00222     coap_header.options_list_ptr->observe_ptr = temp;
00223     coap_header.options_list_ptr->observe_len = 2;
00224     CHECK(sn_coap_builder(buffer, &coap_header) == 7);
00225 }
00226 
00227 
00228 TEST(libCoap_builder, build_message_options_accept)
00229 {
00230     coap_header.options_list_ptr->accept_ptr = temp;
00231     coap_header.options_list_ptr->accept_len = 2;
00232     CHECK(sn_coap_builder(buffer, &coap_header) == 8);
00233 }
00234 
00235 
00236 TEST(libCoap_builder, build_message_options_uri_query)
00237 {
00238     coap_header.options_list_ptr->uri_query_ptr = temp;
00239     coap_header.options_list_ptr->uri_query_len = 2;
00240     CHECK(sn_coap_builder(buffer, &coap_header) == 8);
00241 }
00242 
00243 
00244 TEST(libCoap_builder, build_message_options_block1)
00245 {
00246     coap_header.options_list_ptr->block1_ptr = temp;
00247     coap_header.options_list_ptr->block1_len = 2;
00248     CHECK(sn_coap_builder(buffer, &coap_header) == 8);
00249 }
00250 
00251 TEST(libCoap_builder, build_message_options_block2)
00252 {
00253     coap_header.options_list_ptr->block2_ptr = temp;
00254     coap_header.options_list_ptr->block2_len = 2;
00255 
00256     sn_coap_header_check_stub.expectedInt8 = 44;
00257     CHECK(sn_coap_builder(buffer, &coap_header) == -1);
00258 
00259     sn_coap_header_check_stub.expectedInt8 = 0;
00260     CHECK(sn_coap_builder(buffer, &coap_header) == 8);
00261 
00262     coap_header.options_list_ptr = NULL; //return from sn_coap_builder_options_build immediately
00263     sn_coap_header_check_stub.expectedInt8 = 0;
00264     CHECK( 4 == sn_coap_builder(buffer, &coap_header) );
00265 }
00266 
00267 TEST(libCoap_builder, sn_coap_builder_calc_needed_packet_data_size)
00268 {
00269     CHECK(sn_coap_builder_calc_needed_packet_data_size(NULL) == 0);
00270 
00271     sn_coap_hdr_s header;
00272     memset(&header, 0, sizeof(sn_coap_hdr_s));
00273     header.msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
00274     header.token_ptr = (uint8_t*)malloc(10);
00275     header.token_len = 10;
00276     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00277     free(header.token_ptr);
00278     header.token_ptr = (uint8_t*)malloc(6);
00279     header.token_len = 6;
00280 
00281     //Test variations of sn_coap_builder_options_calc_option_size here -->
00282     header.uri_path_ptr = (uint8_t*)malloc(290);
00283     memset(header.uri_path_ptr, '1', 290);
00284     header.uri_path_len = 290;
00285     header.uri_path_ptr[5] = '/';
00286     header.uri_path_ptr[285] = '/';
00287 
00288     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00289 
00290     header.uri_path_ptr[285] = '1';
00291     header.uri_path_ptr[170] = '/';
00292 
00293     header.content_type_ptr = (uint8_t*)malloc(6);
00294     header.content_type_len = 6;
00295 
00296     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00297 
00298     header.content_type_len = 2;
00299     sn_coap_options_list_s opt_list;
00300     memset(&opt_list, 0, sizeof(sn_coap_options_list_s));
00301     header.options_list_ptr = &opt_list;
00302     header.options_list_ptr->accept_ptr = (uint8_t*)malloc(6);
00303     header.options_list_ptr->accept_len = 6;
00304     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00305 
00306     header.options_list_ptr->accept_len = 2;
00307     header.options_list_ptr->max_age_ptr = (uint8_t*)malloc(6);
00308     header.options_list_ptr->max_age_len = 6;
00309     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00310 
00311     //proxy uri tests (4)
00312     header.options_list_ptr->max_age_len = 2;
00313     header.options_list_ptr->proxy_uri_ptr = (uint8_t*)malloc(270);
00314     header.options_list_ptr->proxy_uri_len = 1800;
00315 
00316     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00317     header.options_list_ptr->proxy_uri_len = 6;
00318     header.options_list_ptr->etag_ptr = (uint8_t*)malloc(6);
00319     header.options_list_ptr->etag_len = 0;
00320     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00321 
00322     header.options_list_ptr->proxy_uri_len = 14;
00323     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00324 
00325     header.options_list_ptr->proxy_uri_len = 281;
00326     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00327 
00328     header.options_list_ptr->etag_len = 4;
00329     header.options_list_ptr->uri_host_ptr = (uint8_t*)malloc(6);
00330     header.options_list_ptr->uri_host_len = 0;
00331     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00332 
00333     header.options_list_ptr->uri_host_len = 4;
00334     header.options_list_ptr->location_path_ptr = (uint8_t*)malloc(6);
00335     header.options_list_ptr->location_path_len = 270;
00336     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00337 
00338     header.options_list_ptr->uri_host_len = 44;
00339     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00340 
00341     header.options_list_ptr->location_path_len = 27;
00342     header.options_list_ptr->uri_port_ptr = (uint8_t*)malloc(6);
00343     header.options_list_ptr->uri_port_len = 6;
00344     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00345 
00346     header.options_list_ptr->uri_port_len = 2;
00347     header.options_list_ptr->location_query_ptr = (uint8_t*)malloc(6);
00348     header.options_list_ptr->location_query_len = 277;
00349     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00350 
00351     header.options_list_ptr->location_query_len = 27;
00352     header.options_list_ptr->observe_ptr = (uint8_t*)malloc(6);
00353     header.options_list_ptr->observe_len = 6;
00354     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00355 
00356     header.options_list_ptr->observe_len = 2;
00357     header.options_list_ptr->uri_query_ptr = (uint8_t*)malloc(6);
00358     header.options_list_ptr->uri_query_len = 0;
00359     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00360 
00361     header.options_list_ptr->observe = 1;
00362 
00363     header.options_list_ptr->uri_query_len = 4;
00364     header.options_list_ptr->block2_ptr = (uint8_t*)malloc(6);
00365     header.options_list_ptr->block2_len = 0;
00366     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00367 
00368     header.options_list_ptr->block2_len = 2;
00369     header.options_list_ptr->block1_ptr = (uint8_t*)malloc(6);
00370     header.options_list_ptr->block1_len = 0;
00371     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00372 
00373     header.options_list_ptr->block1_len = 2;
00374     header.payload_len = 1;
00375     CHECK(431 == sn_coap_builder_calc_needed_packet_data_size(&header));
00376 
00377 
00378     header.options_list_ptr->size1_ptr = (uint8_t*)malloc(6);
00379     header.options_list_ptr->size1_len = 6;
00380     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00381 
00382     header.options_list_ptr->size2_ptr = (uint8_t*)malloc(6);
00383     header.options_list_ptr->size1_len = 2;
00384     header.options_list_ptr->size2_len = 6;
00385     CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
00386 
00387     free(header.options_list_ptr->observe_ptr);
00388     header.options_list_ptr->observe_ptr = NULL;
00389     header.options_list_ptr->observe_len = 0;
00390     header.options_list_ptr->block1_len = 2;
00391     header.options_list_ptr->size1_len = 2;
00392     header.options_list_ptr->size2_len = 2;
00393     header.payload_len = 1;
00394     CHECK(729 == sn_coap_builder_calc_needed_packet_data_size(&header));
00395 
00396     // <--
00397 
00398     //free(header.options_list_ptr->observe_ptr); Called earlier!
00399     free(header.options_list_ptr->location_query_ptr);
00400     free(header.options_list_ptr->uri_port_ptr);
00401     free(header.options_list_ptr->location_path_ptr);
00402     free(header.options_list_ptr->uri_host_ptr);
00403     free(header.options_list_ptr->etag_ptr);
00404     free(header.options_list_ptr->proxy_uri_ptr);
00405     free(header.options_list_ptr->max_age_ptr);
00406     free(header.options_list_ptr->accept_ptr);
00407     free(header.options_list_ptr->size1_ptr);
00408     free(header.options_list_ptr->block1_ptr);
00409     free(header.options_list_ptr->block2_ptr);
00410     header.options_list_ptr->location_query_ptr = NULL;
00411     header.options_list_ptr->uri_port_ptr = NULL;
00412     header.options_list_ptr->location_path_ptr = NULL;
00413     header.options_list_ptr->uri_host_ptr = NULL;
00414     header.options_list_ptr->etag_ptr = NULL;
00415     header.options_list_ptr->proxy_uri_ptr = NULL;
00416     header.options_list_ptr->max_age_ptr = NULL;
00417     header.options_list_ptr->accept_ptr = NULL;
00418     header.options_list_ptr->size1_ptr = NULL;
00419     header.options_list_ptr->block1_ptr = NULL;
00420     header.options_list_ptr->block2_ptr = NULL;
00421 
00422     CHECK(318 == sn_coap_builder_calc_needed_packet_data_size(&header));
00423     free(header.options_list_ptr->size2_ptr);
00424     free(header.options_list_ptr->uri_query_ptr);
00425 
00426     //Test sn_coap_builder_options_calculate_jump_need "else" case
00427     header.options_list_ptr = NULL;
00428     uint16_t val = sn_coap_builder_calc_needed_packet_data_size(&header);
00429     CHECK( 308 == val );
00430 
00431     free(header.content_type_ptr);
00432     free(header.uri_path_ptr);
00433     free(header.token_ptr);
00434 }
00435 
00436 TEST(libCoap_builder, sn_coap_builder_options_build_add_one_option)
00437 {
00438     coap_header.options_list_ptr->proxy_uri_ptr = (uint8_t*)malloc(280);
00439     memset(coap_header.options_list_ptr->proxy_uri_ptr, '1', 280);
00440     coap_header.options_list_ptr->proxy_uri_len = 2;
00441     sn_coap_header_check_stub.expectedInt8 = 0;
00442     CHECK(sn_coap_builder(buffer, &coap_header) == 8);
00443 
00444     coap_header.options_list_ptr->proxy_uri_len = 27;
00445     sn_coap_header_check_stub.expectedInt8 = 0;
00446     CHECK(34 == sn_coap_builder(buffer, &coap_header));
00447 
00448     coap_header.options_list_ptr->proxy_uri_len = 277;
00449     sn_coap_header_check_stub.expectedInt8 = 0;
00450     CHECK(285 == sn_coap_builder(buffer, &coap_header));
00451 
00452     free(coap_header.options_list_ptr->proxy_uri_ptr);
00453     coap_header.options_list_ptr->proxy_uri_ptr = NULL;
00454 }
00455 
00456 TEST(libCoap_builder, sn_coap_builder_options_build_add_zero_length_option)
00457 {
00458     coap_header.options_list_ptr->proxy_uri_ptr = (uint8_t*)malloc(280);
00459     memset(coap_header.options_list_ptr->proxy_uri_ptr, '1', 280);
00460     coap_header.options_list_ptr->proxy_uri_len = 2;
00461     sn_coap_header_check_stub.expectedInt8 = 0;
00462     coap_header.options_list_ptr->observe = 1;
00463     int16_t val = sn_coap_builder(buffer, &coap_header);
00464     CHECK(val == 9);
00465 
00466     free(coap_header.options_list_ptr->proxy_uri_ptr);
00467 }
00468 
00469 TEST(libCoap_builder, sn_coap_builder_options_get_option_part_position)
00470 {
00471     sn_coap_hdr_s header;
00472     memset(&header, 0, sizeof(sn_coap_hdr_s));
00473     sn_coap_options_list_s opt_list;
00474     memset(&opt_list, 0, sizeof(sn_coap_options_list_s));
00475     header.options_list_ptr = &opt_list;
00476     header.options_list_ptr->accept_ptr = (uint8_t*)malloc(20);
00477     memset(header.options_list_ptr->accept_ptr, '&', 20);
00478     header.options_list_ptr->accept_len = 20;
00479     uint16_t val = sn_coap_builder(buffer, &header);
00480     CHECK(val == 24);
00481 
00482     header.options_list_ptr->accept_ptr[0] = 'a';
00483     header.options_list_ptr->accept_ptr[1] = 'n';
00484     header.options_list_ptr->accept_ptr[19] = 'n';
00485     val = sn_coap_builder(buffer, &header);
00486     CHECK(val == 42);
00487 
00488     free(header.options_list_ptr->accept_ptr);
00489     header.options_list_ptr->accept_ptr = NULL;
00490 }
00491 
00492 TEST(libCoap_builder, sn_coap_builder_payload_build)
00493 {
00494     sn_coap_hdr_s header;
00495     memset(&header, 0, sizeof(sn_coap_hdr_s));
00496     header.payload_ptr = (uint8_t*)malloc(5);
00497     header.payload_len = 5;
00498     sn_coap_options_list_s opt_list;
00499     memset(&opt_list, 0, sizeof(sn_coap_options_list_s));
00500     header.options_list_ptr = &opt_list;
00501     header.options_list_ptr->accept_ptr = (uint8_t*)malloc(20);
00502     memset(header.options_list_ptr->accept_ptr, '&', 20);
00503     header.options_list_ptr->accept_len = 20;
00504     uint16_t val = sn_coap_builder(buffer, &header);
00505     CHECK(val == 30);
00506 
00507     free(header.payload_ptr);
00508     free(header.options_list_ptr->accept_ptr);
00509     header.options_list_ptr->accept_ptr = NULL;
00510 }