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.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
Test.c
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 /** 00017 * \file \test_libCoap\Test.c 00018 * 00019 * \brief Unit tests for libCoap 00020 */ 00021 #include "ns_types.h" 00022 #include "sn_nsdl.h" 00023 #include "sn_coap_header.h" 00024 #include "sn_coap_protocol.h" 00025 #include "sn_coap_header_internal.h" 00026 00027 #include "unity.h" 00028 #include "string.h" 00029 #include "stdlib.h" 00030 00031 static uint8_t *message_ptr = 0; 00032 static uint16_t message_len = 0; 00033 static sn_coap_hdr_s coap_header; 00034 sn_nsdl_addr_s coap_address; 00035 static uint8_t address[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; 00036 00037 /* CoAP parameters to be used */ 00038 static uint8_t option_short[3] = {0x61, 0x62, 0x63}; 00039 static uint8_t option_path[5] = {0x62, 0x63, 0x2f, 0x61, 0x62}; 00040 static uint8_t option_30[30]; 00041 static uint8_t option_300[300]; 00042 static uint8_t option_600[600]; 00043 static uint8_t option_800[800]; 00044 00045 /* Resultat sju rätt är: */ 00046 static uint8_t coap_message_no_options[4] = {0x60, 0x44, 0x12, 0x34}; 00047 static uint8_t coap_message_token_payload[11] = {0x63, 0x44, 0x12, 0x34, 0x61, 0x62, 0x63, 0xff, 0x61, 0x62, 0x63}; 00048 static uint8_t coap_message_wrong_version[4] = {0xe0, 0x44, 0x12, 0x34}; 00049 static uint8_t coap_message_malformed[4] = {0x60, 0xae, 0x43, 0x11}; 00050 static uint8_t coap_message_option_short[9] = {0x60, 0x44, 0x12, 0x34, 0xd3, 0x16, 0x61, 0x62, 0x63}; // option number 35 00051 static uint8_t coap_message_option_30[7] = {0x60, 0x44, 0x12, 0x34, 0xdd, 0x16, 0x11}; // option number 35, length 30 00052 static uint8_t coap_message_option_300[8] = {0x60, 0x44, 0x12, 0x34, 0xde, 0x16, 0x00, 0x1f}; // option number 35, length 300 00053 static uint8_t coap_message_option_600[8] = {0x60, 0x44, 0x12, 0x34, 0xde, 0x16, 0x01, 0x4b}; // option number 35, length 600 00054 static uint8_t coap_message_option_800[8] = {0x60, 0x44, 0x12, 0x34, 0xde, 0x16, 0x02, 0x13}; // option number 35, length 800 00055 /* Options = Token - 3 bytes, max age - 14 - 3 bytes, Uri path - 11 - 2 x 2 bytes, Uri host - 3 - 3 bytes */ 00056 static uint8_t coap_message_multiple_options[21] = {0x63, 0x44, 0x12, 0x34, 0x61, 0x62, 0x63, 0x33, 0x61, 0x62, 0x63, 0x82, 0x62, 0x63, 0x02, 0x61, 0x62, 0x33, 0x61, 0x62, 0x63}; 00057 00058 static uint8_t coap_message_empty_con[4] = {0x40, 0x00, 0x12, 0x34}; 00059 static uint8_t coap_message_wrong_code_1[4] = {0x40, 0x24, 0x12, 0x34}; 00060 static uint8_t coap_message_wrong_code_6[4] = {0x40, 0xc4, 0x12, 0x34}; 00061 static uint8_t coap_message_wrong_code_7[4] = {0x40, 0xe4, 0x12, 0x34}; 00062 00063 00064 void *own_alloc(uint16_t size); 00065 void own_free(void *ptr); 00066 00067 struct coap_s *handle; 00068 00069 uint8_t tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr, void *param); 00070 int8_t rx_callback(sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *address_ptr, void *param); 00071 00072 /* non-test function declarations */ 00073 void fill_with_random(uint8_t *ptr, uint16_t len); 00074 00075 /* Unity test code starts */ 00076 void setUp(void) 00077 { 00078 //This is run before EACH TEST 00079 00080 } 00081 00082 void tearDown(void) 00083 { 00084 00085 } 00086 00087 void test_libcoap_init(void) 00088 { 00089 handle = sn_coap_protocol_init(&own_alloc, &own_free, &tx_function, &rx_callback); 00090 00091 TEST_ASSERT_NOT_NULL(handle); 00092 } 00093 00094 00095 /** 00096 * \fn test_libcoap_builder_no_options(void) 00097 * 00098 * \brief Build CoAP message - ACK - Changed 00099 * 00100 */ 00101 void test_libcoap_builder_no_options(void) 00102 { 00103 memset(&coap_header, 0, sizeof(sn_coap_hdr_s)); 00104 coap_header.msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT; 00105 coap_header.msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; 00106 coap_header.msg_id = 0x1234; 00107 00108 coap_header.options_list_ptr = malloc(sizeof(sn_coap_options_list_s)); 00109 memset(coap_header.options_list_ptr, 0, sizeof(sn_coap_options_list_s)); 00110 00111 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00112 message_ptr = malloc(message_len); 00113 sn_coap_builder(message_ptr, &coap_header); 00114 00115 TEST_ASSERT_EQUAL(sizeof(coap_message_no_options), message_len); 00116 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_no_options, message_ptr, sizeof(coap_message_no_options)); 00117 00118 free(message_ptr); 00119 message_ptr = 0; 00120 } 00121 00122 /** 00123 * \fn test_libcoap_builder_token_and_payload(void) 00124 * 00125 * \brief Build CoAP message - ACK - changed - token - payload 00126 * 00127 */ 00128 void test_libcoap_builder_token_and_payload(void) 00129 { 00130 memset(&coap_header, 0, sizeof(sn_coap_hdr_s)); 00131 coap_header.msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT; 00132 coap_header.msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; 00133 coap_header.msg_id = 0x1234; 00134 coap_header.token_len = sizeof(option_short); 00135 coap_header.token_ptr = option_short; 00136 coap_header.payload_len = sizeof(option_short); 00137 coap_header.payload_ptr = option_short; 00138 00139 coap_header.options_list_ptr = malloc(sizeof(sn_coap_options_list_s)); 00140 memset(coap_header.options_list_ptr, 0, sizeof(sn_coap_options_list_s)); 00141 00142 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00143 message_ptr = malloc(message_len); 00144 sn_coap_builder(message_ptr, &coap_header); 00145 00146 TEST_ASSERT_EQUAL(sizeof(coap_message_token_payload), message_len); 00147 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_token_payload, message_ptr, sizeof(coap_message_token_payload)); 00148 00149 free(message_ptr); 00150 message_ptr = 0; 00151 } 00152 00153 /** 00154 * \fn test_libcoap_builder_short_option(void) 00155 * 00156 * \brief Build CoAP message - ACK - changed - short proxy URI option 00157 * 00158 */ 00159 void test_libcoap_builder_short_option(void) 00160 { 00161 coap_header.token_len = 0; 00162 coap_header.token_ptr = 0; 00163 coap_header.payload_len = 0; 00164 coap_header.payload_ptr = 0; 00165 00166 coap_header.options_list_ptr->proxy_uri_len = sizeof(option_short); 00167 coap_header.options_list_ptr->proxy_uri_ptr = option_short; 00168 00169 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00170 message_ptr = malloc(message_len); 00171 sn_coap_builder(message_ptr, &coap_header); 00172 00173 TEST_ASSERT_EQUAL(sizeof(coap_message_option_short), message_len); 00174 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_option_short, message_ptr, sizeof(coap_message_option_short)); 00175 00176 free(message_ptr); 00177 message_ptr = 0; 00178 } 00179 00180 /** 00181 * \fn test_libcoap_builder_option_30(void) 00182 * 00183 * \brief Build CoAP message - ACK - changed - proxy URI option, length 30 bytes 00184 * 00185 */ 00186 void test_libcoap_builder_option_30(void) 00187 { 00188 fill_with_random(option_30, sizeof(option_30)); 00189 coap_header.options_list_ptr->proxy_uri_len = sizeof(option_30); 00190 coap_header.options_list_ptr->proxy_uri_ptr = option_30; 00191 00192 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00193 message_ptr = malloc(message_len); 00194 sn_coap_builder(message_ptr, &coap_header); 00195 00196 TEST_ASSERT_EQUAL((sizeof(coap_message_option_30) + sizeof(option_30)), message_len); 00197 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_option_30, message_ptr, sizeof(coap_message_option_30)); 00198 00199 free(message_ptr); 00200 message_ptr = 0; 00201 00202 } 00203 00204 /** 00205 * \fn test_libcoap_builder_option_300(void) 00206 * 00207 * \brief Build CoAP message - ACK - changed - proxy URI option, length 300 bytes 00208 * 00209 */ 00210 void test_libcoap_builder_option_300(void) 00211 { 00212 fill_with_random(option_300, sizeof(option_300)); 00213 coap_header.options_list_ptr->proxy_uri_len = sizeof(option_300); 00214 coap_header.options_list_ptr->proxy_uri_ptr = option_300; 00215 00216 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00217 message_ptr = malloc(message_len); 00218 sn_coap_builder(message_ptr, &coap_header); 00219 00220 TEST_ASSERT_EQUAL(sizeof(coap_message_option_300) + sizeof(option_300), message_len); 00221 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_option_300, message_ptr, sizeof(coap_message_option_300)); 00222 00223 free(message_ptr); 00224 message_ptr = 0; 00225 } 00226 00227 /** 00228 * \fn test_libcoap_builder_option_600(void) 00229 * 00230 * \brief Build CoAP message - ACK - changed - proxy URI option, length 600 bytes 00231 * 00232 */ 00233 00234 void test_libcoap_builder_option_600(void) 00235 { 00236 fill_with_random(option_600, sizeof(option_600)); 00237 coap_header.options_list_ptr->proxy_uri_len = sizeof(option_600); 00238 coap_header.options_list_ptr->proxy_uri_ptr = option_600; 00239 00240 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00241 message_ptr = malloc(message_len); 00242 sn_coap_builder(message_ptr, &coap_header); 00243 00244 TEST_ASSERT_EQUAL(sizeof(coap_message_option_600) + sizeof(option_600), message_len); 00245 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_option_600, message_ptr, sizeof(coap_message_option_600)); 00246 00247 free(message_ptr); 00248 message_ptr = 0; 00249 } 00250 00251 /** 00252 * \fn test_libcoap_builder_option_800(void) 00253 * 00254 * \brief Build CoAP message - ACK - changed - proxy URI option, length 800 bytes 00255 * 00256 */ 00257 void test_libcoap_builder_option_800(void) 00258 { 00259 fill_with_random(option_800, sizeof(option_800)); 00260 coap_header.options_list_ptr->proxy_uri_len = sizeof(option_800); 00261 coap_header.options_list_ptr->proxy_uri_ptr = option_800; 00262 00263 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00264 message_ptr = malloc(message_len); 00265 sn_coap_builder(message_ptr, &coap_header); 00266 00267 TEST_ASSERT_EQUAL(sizeof(coap_message_option_800) + sizeof(option_800), message_len); 00268 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_option_800, message_ptr, sizeof(coap_message_option_800)); 00269 00270 free(message_ptr); 00271 message_ptr = 0; 00272 } 00273 00274 /** 00275 * \fn test_libcoap_builder_message_with_multiple_options(void) 00276 * 00277 * \brief Build CoAP message - ACK - changed - URI host - URI path - max age - token 00278 * 00279 */ 00280 void test_libcoap_builder_message_with_multiple_options(void) 00281 { 00282 coap_header.options_list_ptr->proxy_uri_len = 0; 00283 coap_header.options_list_ptr->proxy_uri_ptr = 0; 00284 00285 coap_header.options_list_ptr->uri_host_len = sizeof(option_short); 00286 coap_header.options_list_ptr->uri_host_ptr = option_short; 00287 00288 coap_header.uri_path_len = sizeof(option_path); 00289 coap_header.uri_path_ptr = option_path; 00290 00291 coap_header.options_list_ptr->max_age_len = sizeof(option_short); 00292 coap_header.options_list_ptr->max_age_ptr = option_short; 00293 00294 coap_header.token_len = sizeof(option_short); 00295 coap_header.token_ptr = option_short; 00296 00297 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00298 message_ptr = malloc(message_len); 00299 sn_coap_builder(message_ptr, &coap_header); 00300 00301 TEST_ASSERT_EQUAL(sizeof(coap_message_multiple_options), message_len); 00302 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_multiple_options, message_ptr, sizeof(coap_message_multiple_options)); 00303 00304 free(message_ptr); 00305 message_ptr = 0; 00306 } 00307 00308 /*******************************************************************************/ 00309 /*** CoAP PARSER TEST ***/ 00310 /*******************************************************************************/ 00311 00312 /** 00313 * \fn test_libcoap_parser_parse_message_without_options(void) 00314 * 00315 * \brief call coap protocol parser with message without options (ACK - changed) 00316 * 00317 */ 00318 void test_libcoap_parser_parse_message_without_options(void) 00319 { 00320 coap_address.addr_ptr = address; 00321 00322 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_no_options), coap_message_no_options, NULL); 00323 00324 TEST_ASSERT_NOT_NULL(coap_header_ptr); 00325 00326 TEST_ASSERT_EQUAL(COAP_MSG_TYPE_ACKNOWLEDGEMENT, coap_header_ptr->msg_type); 00327 TEST_ASSERT_EQUAL(COAP_MSG_CODE_RESPONSE_CHANGED, coap_header_ptr->msg_code); 00328 TEST_ASSERT_EQUAL(0x1234, coap_header_ptr->msg_id); 00329 00330 sn_coap_parser_release_allocated_coap_msg_mem(handle, coap_header_ptr); 00331 } 00332 00333 /** 00334 * \fn test_libcoap_parser_parse_message_with_payload_and_token(void) 00335 * 00336 * \brief call coap protocol parser with message with token option and small payload (ACK - changed) 00337 * 00338 */ 00339 void test_libcoap_parser_parse_message_with_payload_and_token(void) 00340 { 00341 coap_address.addr_ptr = address; 00342 00343 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_token_payload), coap_message_token_payload, NULL); 00344 00345 TEST_ASSERT_NOT_NULL(coap_header_ptr); 00346 00347 TEST_ASSERT_EQUAL(COAP_MSG_TYPE_ACKNOWLEDGEMENT, coap_header_ptr->msg_type); 00348 TEST_ASSERT_EQUAL(COAP_MSG_CODE_RESPONSE_CHANGED, coap_header_ptr->msg_code); 00349 TEST_ASSERT_EQUAL(0x1234, coap_header_ptr->msg_id); 00350 00351 TEST_ASSERT_EQUAL(3, coap_header_ptr->token_len); 00352 TEST_ASSERT_EQUAL_INT8_ARRAY(option_short, coap_header_ptr->token_ptr, sizeof(option_short)); 00353 00354 TEST_ASSERT_EQUAL(3, coap_header_ptr->payload_len); 00355 TEST_ASSERT_EQUAL_INT8_ARRAY(option_short, coap_header_ptr->payload_ptr, sizeof(option_short)); 00356 00357 sn_coap_parser_release_allocated_coap_msg_mem(handle, coap_header_ptr); 00358 } 00359 00360 /** 00361 * \fn test_libcoap_parser_parse_message_with_small_option(void) 00362 * 00363 * \brief call coap protocol parser with message with small option (ACK - changed - URI query) 00364 * 00365 */ 00366 void test_libcoap_parser_parse_message_with_small_option(void) 00367 { 00368 coap_address.addr_ptr = address; 00369 00370 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_option_short), coap_message_option_short, NULL); 00371 00372 TEST_ASSERT_NOT_NULL(coap_header_ptr); 00373 TEST_ASSERT_NOT_NULL(coap_header_ptr->options_list_ptr); 00374 00375 TEST_ASSERT_EQUAL(COAP_MSG_TYPE_ACKNOWLEDGEMENT, coap_header_ptr->msg_type); 00376 TEST_ASSERT_EQUAL(COAP_MSG_CODE_RESPONSE_CHANGED, coap_header_ptr->msg_code); 00377 TEST_ASSERT_EQUAL(0x1234, coap_header_ptr->msg_id); 00378 00379 TEST_ASSERT_EQUAL(sizeof(option_short), coap_header_ptr->options_list_ptr->proxy_uri_len); 00380 TEST_ASSERT_EQUAL_INT8_ARRAY(option_short, coap_header_ptr->options_list_ptr->proxy_uri_ptr, sizeof(option_short)); 00381 00382 sn_coap_parser_release_allocated_coap_msg_mem(handle, coap_header_ptr); 00383 } 00384 00385 00386 /** 00387 * \fn test_libcoap_parser_parse_message_with_multiple_options(void) 00388 * 00389 * \brief call coap protocol parser with message with multiple options (ACK - changed - token - max age - URI path - URI host) 00390 * 00391 */ 00392 void test_libcoap_parser_parse_message_with_multiple_options(void) 00393 { 00394 coap_address.addr_ptr = address; 00395 00396 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_multiple_options), coap_message_multiple_options, NULL); 00397 00398 TEST_ASSERT_NOT_NULL(coap_header_ptr); 00399 TEST_ASSERT_NOT_NULL(coap_header_ptr->options_list_ptr); 00400 00401 TEST_ASSERT_EQUAL(COAP_MSG_TYPE_ACKNOWLEDGEMENT, coap_header_ptr->msg_type); 00402 TEST_ASSERT_EQUAL(COAP_MSG_CODE_RESPONSE_CHANGED, coap_header_ptr->msg_code); 00403 TEST_ASSERT_EQUAL(0x1234, coap_header_ptr->msg_id); 00404 00405 00406 TEST_ASSERT_EQUAL(sizeof(option_short), coap_header_ptr->options_list_ptr->uri_host_len); 00407 TEST_ASSERT_EQUAL_INT8_ARRAY(option_short, coap_header_ptr->options_list_ptr->uri_host_ptr, sizeof(option_short)); 00408 00409 TEST_ASSERT_EQUAL(sizeof(option_path), coap_header_ptr->uri_path_len); 00410 TEST_ASSERT_EQUAL_INT8_ARRAY(option_path, coap_header_ptr->uri_path_ptr, sizeof(option_path)); 00411 00412 TEST_ASSERT_EQUAL(sizeof(option_short), coap_header_ptr->options_list_ptr->max_age_len); 00413 TEST_ASSERT_EQUAL_INT8_ARRAY(option_short, coap_header_ptr->options_list_ptr->max_age_ptr, sizeof(option_short)); 00414 00415 TEST_ASSERT_EQUAL(sizeof(option_short), coap_header_ptr->token_len); 00416 TEST_ASSERT_EQUAL_INT8_ARRAY(option_short, coap_header_ptr->token_ptr, sizeof(option_short)); 00417 00418 00419 sn_coap_parser_release_allocated_coap_msg_mem(handle, coap_header_ptr); 00420 } 00421 00422 /*******************************************************************************/ 00423 /*** NEGATIVE TEST CASES ***/ 00424 /*******************************************************************************/ 00425 00426 /** 00427 * \fn test_libcoap_negative_build_with_null_pointer(void) 00428 * 00429 * \brief - Call sn_coap_builder_calc_needed_packet_data_size with null pointer parameter 00430 * - Call sn_coap_builder with null pointer parameter 00431 * 00432 */ 00433 void test_libcoap_negative_build_with_null_pointer(void) 00434 { 00435 int16_t ret_val_1 = 0; 00436 int16_t ret_val_2 = 0; 00437 00438 message_len = sn_coap_builder_calc_needed_packet_data_size(NULL); 00439 ret_val_1 = sn_coap_builder(message_ptr, NULL); 00440 ret_val_2 = sn_coap_builder(NULL, &coap_header); 00441 00442 TEST_ASSERT_EQUAL(0, message_len); 00443 TEST_ASSERT_EQUAL(-2, ret_val_1); 00444 TEST_ASSERT_EQUAL(-2, ret_val_2); 00445 } 00446 00447 /** 00448 * \fn test_libcoap_negative_parse_with_null_pointer(void) 00449 * 00450 * \brief - Call sn_coap_protocol_parse with null pointer parameter 00451 * 00452 */ 00453 void test_libcoap_negative_parse_with_null_pointer(void) 00454 { 00455 coap_address.addr_ptr = address; 00456 00457 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, NULL, sizeof(coap_message_no_options), coap_message_no_options, NULL); 00458 TEST_ASSERT_NULL(coap_header_ptr); 00459 00460 coap_header_ptr = sn_coap_protocol_parse(NULL, &coap_address, sizeof(coap_message_no_options), coap_message_no_options, NULL); 00461 TEST_ASSERT_NULL(coap_header_ptr); 00462 00463 coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_no_options), NULL, NULL); 00464 TEST_ASSERT_NULL(coap_header_ptr); 00465 } 00466 00467 /** 00468 * \fn test_libcoap_negative_parse_with_coap_packet_len_null(void) 00469 * 00470 * \brief - Call sn_coap_protocol_parse with parameter length = 0 00471 * 00472 */ 00473 void test_libcoap_negative_parse_with_coap_packet_len_null(void) 00474 { 00475 coap_address.addr_ptr = address; 00476 00477 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, 0, coap_message_no_options, NULL); 00478 TEST_ASSERT_NULL(coap_header_ptr); 00479 } 00480 00481 /** 00482 * \fn test_libcoap_negative_parse_with_wrong_coap_packet_len(void) 00483 * 00484 * \brief - Call sn_coap_protocol_parse with wrong packet length 00485 * 00486 */ 00487 void test_libcoap_negative_parse_with_wrong_coap_packet_len(void) 00488 { 00489 coap_address.addr_ptr = address; 00490 00491 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, (sizeof(coap_message_multiple_options) - 1), coap_message_multiple_options, NULL); 00492 TEST_ASSERT_NULL(coap_header_ptr); 00493 } 00494 00495 /** 00496 * \fn test_libcoap_negative_parse_with_wrong_coap_version(void) 00497 * 00498 * \brief - Call sn_coap_protocol_parse with wrong CoAP version 00499 * 00500 */ 00501 void test_libcoap_negative_parse_with_wrong_coap_version(void) 00502 { 00503 coap_address.addr_ptr = address; 00504 00505 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_wrong_version), coap_message_wrong_version, NULL); 00506 TEST_ASSERT_NULL(coap_header_ptr); 00507 } 00508 00509 /** 00510 * \fn test_libcoap_negative_parse_malformed_coap(void) 00511 * 00512 * \brief - Call sn_coap_protocol_parse with malformed CoAP message 00513 * 00514 */ 00515 void test_libcoap_negative_parse_malformed_coap(void) 00516 { 00517 coap_address.addr_ptr = address; 00518 00519 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_malformed), coap_message_malformed, NULL); 00520 TEST_ASSERT_NULL(coap_header_ptr); 00521 } 00522 00523 /** 00524 * \fn test_libcoap_negative_parse_empty_con(void) 00525 * 00526 * \brief - Call sn_coap_protocol_parse with "CoAP ping" message. Should return RST 00527 * 00528 */ 00529 void test_libcoap_negative_parse_empty_con(void) 00530 { 00531 coap_address.addr_ptr = address; 00532 00533 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_empty_con), coap_message_empty_con, NULL); 00534 TEST_ASSERT_NULL(coap_header_ptr); 00535 00536 } 00537 00538 /** 00539 * \fn test_libcoap_negative_parse_wrong_code_1(void) 00540 * 00541 * \brief - Call sn_coap_protocol_parse with wrong message code. Should return RST 00542 * 00543 */ 00544 void test_libcoap_negative_parse_wrong_code_1(void) 00545 { 00546 coap_address.addr_ptr = address; 00547 00548 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_wrong_code_1), coap_message_wrong_code_1, NULL); 00549 TEST_ASSERT_NULL(coap_header_ptr); 00550 00551 } 00552 00553 /** 00554 * \fn test_libcoap_negative_parse_wrong_code_6(void) 00555 * 00556 * \brief - Call sn_coap_protocol_parse with wrong message code. Should return RST 00557 * 00558 */ 00559 void test_libcoap_negative_parse_wrong_code_6(void) 00560 { 00561 coap_address.addr_ptr = address; 00562 00563 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_wrong_code_6), coap_message_wrong_code_6, NULL); 00564 TEST_ASSERT_NULL(coap_header_ptr); 00565 00566 } 00567 00568 /** 00569 * \fn test_libcoap_negative_parse_wrong_code_7(void) 00570 * 00571 * \brief - Call sn_coap_protocol_parse with wrong message code. Should return RST 00572 * 00573 */ 00574 void test_libcoap_negative_parse_wrong_code_7(void) 00575 { 00576 coap_address.addr_ptr = address; 00577 00578 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_wrong_code_7), coap_message_wrong_code_7, NULL); 00579 TEST_ASSERT_NULL(coap_header_ptr); 00580 00581 } 00582 00583 00584 /*******************************/ 00585 /*** non-test functions ***/ 00586 /*******************************/ 00587 00588 void fill_with_random(uint8_t *ptr, uint16_t len) 00589 { 00590 while (len--) { 00591 *(ptr + len) = 'a'; 00592 } 00593 } 00594 00595 void *own_alloc(uint16_t size) 00596 { 00597 return malloc(size); 00598 } 00599 00600 void own_free(void *ptr) 00601 { 00602 free(ptr); 00603 } 00604 00605 uint8_t tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr, void *param) 00606 { 00607 return 0; 00608 } 00609 00610 int8_t rx_callback(sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *address_ptr, void *param) 00611 { 00612 return 0; 00613 } 00614 00615 00616 00617 00618 00619 00620 00621 00622
Generated on Tue Jul 12 2022 12:28:52 by
