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.
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 uint32_t option_short_value = 0x616263; 00040 static uint8_t option_path[5] = {0x62, 0x63, 0x2f, 0x61, 0x62}; 00041 static uint8_t option_30[30]; 00042 static uint8_t option_300[300]; 00043 static uint8_t option_600[600]; 00044 static uint8_t option_800[800]; 00045 00046 /* Resultat sju rätt är: */ 00047 static uint8_t coap_message_no_options[4] = {0x60, 0x44, 0x12, 0x34}; 00048 static uint8_t coap_message_token_payload[11] = {0x63, 0x44, 0x12, 0x34, 0x61, 0x62, 0x63, 0xff, 0x61, 0x62, 0x63}; 00049 static uint8_t coap_message_wrong_version[4] = {0xe0, 0x44, 0x12, 0x34}; 00050 static uint8_t coap_message_malformed[4] = {0x60, 0xae, 0x43, 0x11}; 00051 static uint8_t coap_message_option_short[9] = {0x60, 0x44, 0x12, 0x34, 0xd3, 0x16, 0x61, 0x62, 0x63}; // option number 35 00052 static uint8_t coap_message_option_30[7] = {0x60, 0x44, 0x12, 0x34, 0xdd, 0x16, 0x11}; // option number 35, length 30 00053 static uint8_t coap_message_option_300[8] = {0x60, 0x44, 0x12, 0x34, 0xde, 0x16, 0x00, 0x1f}; // option number 35, length 300 00054 static uint8_t coap_message_option_600[8] = {0x60, 0x44, 0x12, 0x34, 0xde, 0x16, 0x01, 0x4b}; // option number 35, length 600 00055 static uint8_t coap_message_option_800[8] = {0x60, 0x44, 0x12, 0x34, 0xde, 0x16, 0x02, 0x13}; // option number 35, length 800 00056 /* Options = Token - 3 bytes, max age - 14 - 3 bytes, Uri path - 11 - 2 x 2 bytes, Uri host - 3 - 3 bytes */ 00057 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}; 00058 00059 static uint8_t coap_message_empty_con[4] = {0x40, 0x00, 0x12, 0x34}; 00060 static uint8_t coap_message_wrong_code_1[4] = {0x40, 0x24, 0x12, 0x34}; 00061 static uint8_t coap_message_wrong_code_6[4] = {0x40, 0xc4, 0x12, 0x34}; 00062 static uint8_t coap_message_wrong_code_7[4] = {0x40, 0xe4, 0x12, 0x34}; 00063 00064 00065 void *own_alloc(uint16_t size); 00066 void own_free(void *ptr); 00067 00068 struct coap_s *handle; 00069 00070 uint8_t tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr, void *param); 00071 int8_t rx_callback(sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *address_ptr, void *param); 00072 00073 /* non-test function declarations */ 00074 void fill_with_random(uint8_t *ptr, uint16_t len); 00075 00076 /* Unity test code starts */ 00077 void setUp(void) 00078 { 00079 //This is run before EACH TEST 00080 00081 } 00082 00083 void tearDown(void) 00084 { 00085 00086 } 00087 00088 void test_libcoap_init(void) 00089 { 00090 handle = sn_coap_protocol_init(&own_alloc, &own_free, &tx_function, &rx_callback); 00091 00092 TEST_ASSERT_NOT_NULL(handle); 00093 } 00094 00095 00096 /** 00097 * \fn test_libcoap_builder_no_options(void) 00098 * 00099 * \brief Build CoAP message - ACK - Changed 00100 * 00101 */ 00102 void test_libcoap_builder_no_options(void) 00103 { 00104 memset(&coap_header, 0, sizeof(sn_coap_hdr_s)); 00105 coap_header.msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT; 00106 coap_header.msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; 00107 coap_header.msg_id = 0x1234; 00108 00109 coap_header.options_list_ptr = malloc(sizeof(sn_coap_options_list_s)); 00110 memset(coap_header.options_list_ptr, 0, sizeof(sn_coap_options_list_s)); 00111 00112 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00113 message_ptr = malloc(message_len); 00114 sn_coap_builder(message_ptr, &coap_header); 00115 00116 TEST_ASSERT_EQUAL(sizeof(coap_message_no_options), message_len); 00117 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_no_options, message_ptr, sizeof(coap_message_no_options)); 00118 00119 free(message_ptr); 00120 message_ptr = 0; 00121 } 00122 00123 /** 00124 * \fn test_libcoap_builder_token_and_payload(void) 00125 * 00126 * \brief Build CoAP message - ACK - changed - token - payload 00127 * 00128 */ 00129 void test_libcoap_builder_token_and_payload(void) 00130 { 00131 memset(&coap_header, 0, sizeof(sn_coap_hdr_s)); 00132 coap_header.msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT; 00133 coap_header.msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; 00134 coap_header.msg_id = 0x1234; 00135 coap_header.token_len = sizeof(option_short); 00136 coap_header.token_ptr = option_short; 00137 coap_header.payload_len = sizeof(option_short); 00138 coap_header.payload_ptr = option_short; 00139 00140 coap_header.options_list_ptr = malloc(sizeof(sn_coap_options_list_s)); 00141 memset(coap_header.options_list_ptr, 0, sizeof(sn_coap_options_list_s)); 00142 00143 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00144 message_ptr = malloc(message_len); 00145 sn_coap_builder(message_ptr, &coap_header); 00146 00147 TEST_ASSERT_EQUAL(sizeof(coap_message_token_payload), message_len); 00148 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_token_payload, message_ptr, sizeof(coap_message_token_payload)); 00149 00150 free(message_ptr); 00151 message_ptr = 0; 00152 } 00153 00154 /** 00155 * \fn test_libcoap_builder_short_option(void) 00156 * 00157 * \brief Build CoAP message - ACK - changed - short proxy URI option 00158 * 00159 */ 00160 void test_libcoap_builder_short_option(void) 00161 { 00162 coap_header.token_len = 0; 00163 coap_header.token_ptr = 0; 00164 coap_header.payload_len = 0; 00165 coap_header.payload_ptr = 0; 00166 00167 coap_header.options_list_ptr->proxy_uri_len = sizeof(option_short); 00168 coap_header.options_list_ptr->proxy_uri_ptr = option_short; 00169 00170 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00171 message_ptr = malloc(message_len); 00172 sn_coap_builder(message_ptr, &coap_header); 00173 00174 TEST_ASSERT_EQUAL(sizeof(coap_message_option_short), message_len); 00175 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_option_short, message_ptr, sizeof(coap_message_option_short)); 00176 00177 free(message_ptr); 00178 message_ptr = 0; 00179 } 00180 00181 /** 00182 * \fn test_libcoap_builder_option_30(void) 00183 * 00184 * \brief Build CoAP message - ACK - changed - proxy URI option, length 30 bytes 00185 * 00186 */ 00187 void test_libcoap_builder_option_30(void) 00188 { 00189 fill_with_random(option_30, sizeof(option_30)); 00190 coap_header.options_list_ptr->proxy_uri_len = sizeof(option_30); 00191 coap_header.options_list_ptr->proxy_uri_ptr = option_30; 00192 00193 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00194 message_ptr = malloc(message_len); 00195 sn_coap_builder(message_ptr, &coap_header); 00196 00197 TEST_ASSERT_EQUAL((sizeof(coap_message_option_30) + sizeof(option_30)), message_len); 00198 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_option_30, message_ptr, sizeof(coap_message_option_30)); 00199 00200 free(message_ptr); 00201 message_ptr = 0; 00202 00203 } 00204 00205 /** 00206 * \fn test_libcoap_builder_option_300(void) 00207 * 00208 * \brief Build CoAP message - ACK - changed - proxy URI option, length 300 bytes 00209 * 00210 */ 00211 void test_libcoap_builder_option_300(void) 00212 { 00213 fill_with_random(option_300, sizeof(option_300)); 00214 coap_header.options_list_ptr->proxy_uri_len = sizeof(option_300); 00215 coap_header.options_list_ptr->proxy_uri_ptr = option_300; 00216 00217 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00218 message_ptr = malloc(message_len); 00219 sn_coap_builder(message_ptr, &coap_header); 00220 00221 TEST_ASSERT_EQUAL(sizeof(coap_message_option_300) + sizeof(option_300), message_len); 00222 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_option_300, message_ptr, sizeof(coap_message_option_300)); 00223 00224 free(message_ptr); 00225 message_ptr = 0; 00226 } 00227 00228 /** 00229 * \fn test_libcoap_builder_option_600(void) 00230 * 00231 * \brief Build CoAP message - ACK - changed - proxy URI option, length 600 bytes 00232 * 00233 */ 00234 00235 void test_libcoap_builder_option_600(void) 00236 { 00237 fill_with_random(option_600, sizeof(option_600)); 00238 coap_header.options_list_ptr->proxy_uri_len = sizeof(option_600); 00239 coap_header.options_list_ptr->proxy_uri_ptr = option_600; 00240 00241 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00242 message_ptr = malloc(message_len); 00243 sn_coap_builder(message_ptr, &coap_header); 00244 00245 TEST_ASSERT_EQUAL(sizeof(coap_message_option_600) + sizeof(option_600), message_len); 00246 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_option_600, message_ptr, sizeof(coap_message_option_600)); 00247 00248 free(message_ptr); 00249 message_ptr = 0; 00250 } 00251 00252 /** 00253 * \fn test_libcoap_builder_option_800(void) 00254 * 00255 * \brief Build CoAP message - ACK - changed - proxy URI option, length 800 bytes 00256 * 00257 */ 00258 void test_libcoap_builder_option_800(void) 00259 { 00260 fill_with_random(option_800, sizeof(option_800)); 00261 coap_header.options_list_ptr->proxy_uri_len = sizeof(option_800); 00262 coap_header.options_list_ptr->proxy_uri_ptr = option_800; 00263 00264 message_len = sn_coap_builder_calc_needed_packet_data_size(&coap_header); 00265 message_ptr = malloc(message_len); 00266 sn_coap_builder(message_ptr, &coap_header); 00267 00268 TEST_ASSERT_EQUAL(sizeof(coap_message_option_800) + sizeof(option_800), message_len); 00269 TEST_ASSERT_EQUAL_INT8_ARRAY(coap_message_option_800, message_ptr, sizeof(coap_message_option_800)); 00270 00271 free(message_ptr); 00272 message_ptr = 0; 00273 } 00274 00275 /** 00276 * \fn test_libcoap_builder_message_with_multiple_options(void) 00277 * 00278 * \brief Build CoAP message - ACK - changed - URI host - URI path - max age - token 00279 * 00280 */ 00281 void test_libcoap_builder_message_with_multiple_options(void) 00282 { 00283 coap_header.options_list_ptr->proxy_uri_len = 0; 00284 coap_header.options_list_ptr->proxy_uri_ptr = 0; 00285 00286 coap_header.options_list_ptr->uri_host_len = sizeof(option_short); 00287 coap_header.options_list_ptr->uri_host_ptr = option_short; 00288 00289 coap_header.uri_path_len = sizeof(option_path); 00290 coap_header.uri_path_ptr = option_path; 00291 00292 coap_header.options_list_ptr->max_age = option_short_value; 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(option_short_value, coap_header_ptr->options_list_ptr->max_age); 00413 00414 TEST_ASSERT_EQUAL(sizeof(option_short), coap_header_ptr->token_len); 00415 TEST_ASSERT_EQUAL_INT8_ARRAY(option_short, coap_header_ptr->token_ptr, sizeof(option_short)); 00416 00417 00418 sn_coap_parser_release_allocated_coap_msg_mem(handle, coap_header_ptr); 00419 } 00420 00421 /*******************************************************************************/ 00422 /*** NEGATIVE TEST CASES ***/ 00423 /*******************************************************************************/ 00424 00425 /** 00426 * \fn test_libcoap_negative_build_with_null_pointer(void) 00427 * 00428 * \brief - Call sn_coap_builder_calc_needed_packet_data_size with null pointer parameter 00429 * - Call sn_coap_builder with null pointer parameter 00430 * 00431 */ 00432 void test_libcoap_negative_build_with_null_pointer(void) 00433 { 00434 int16_t ret_val_1 = 0; 00435 int16_t ret_val_2 = 0; 00436 00437 message_len = sn_coap_builder_calc_needed_packet_data_size(NULL); 00438 ret_val_1 = sn_coap_builder(message_ptr, NULL); 00439 ret_val_2 = sn_coap_builder(NULL, &coap_header); 00440 00441 TEST_ASSERT_EQUAL(0, message_len); 00442 TEST_ASSERT_EQUAL(-2, ret_val_1); 00443 TEST_ASSERT_EQUAL(-2, ret_val_2); 00444 } 00445 00446 /** 00447 * \fn test_libcoap_negative_parse_with_null_pointer(void) 00448 * 00449 * \brief - Call sn_coap_protocol_parse with null pointer parameter 00450 * 00451 */ 00452 void test_libcoap_negative_parse_with_null_pointer(void) 00453 { 00454 coap_address.addr_ptr = address; 00455 00456 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, NULL, sizeof(coap_message_no_options), coap_message_no_options, NULL); 00457 TEST_ASSERT_NULL(coap_header_ptr); 00458 00459 coap_header_ptr = sn_coap_protocol_parse(NULL, &coap_address, sizeof(coap_message_no_options), coap_message_no_options, NULL); 00460 TEST_ASSERT_NULL(coap_header_ptr); 00461 00462 coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_no_options), NULL, NULL); 00463 TEST_ASSERT_NULL(coap_header_ptr); 00464 } 00465 00466 /** 00467 * \fn test_libcoap_negative_parse_with_coap_packet_len_null(void) 00468 * 00469 * \brief - Call sn_coap_protocol_parse with parameter length = 0 00470 * 00471 */ 00472 void test_libcoap_negative_parse_with_coap_packet_len_null(void) 00473 { 00474 coap_address.addr_ptr = address; 00475 00476 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, 0, coap_message_no_options, NULL); 00477 TEST_ASSERT_NULL(coap_header_ptr); 00478 } 00479 00480 /** 00481 * \fn test_libcoap_negative_parse_with_wrong_coap_packet_len(void) 00482 * 00483 * \brief - Call sn_coap_protocol_parse with wrong packet length 00484 * 00485 */ 00486 void test_libcoap_negative_parse_with_wrong_coap_packet_len(void) 00487 { 00488 coap_address.addr_ptr = address; 00489 00490 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); 00491 TEST_ASSERT_NULL(coap_header_ptr); 00492 } 00493 00494 /** 00495 * \fn test_libcoap_negative_parse_with_wrong_coap_version(void) 00496 * 00497 * \brief - Call sn_coap_protocol_parse with wrong CoAP version 00498 * 00499 */ 00500 void test_libcoap_negative_parse_with_wrong_coap_version(void) 00501 { 00502 coap_address.addr_ptr = address; 00503 00504 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_wrong_version), coap_message_wrong_version, NULL); 00505 TEST_ASSERT_NULL(coap_header_ptr); 00506 } 00507 00508 /** 00509 * \fn test_libcoap_negative_parse_malformed_coap(void) 00510 * 00511 * \brief - Call sn_coap_protocol_parse with malformed CoAP message 00512 * 00513 */ 00514 void test_libcoap_negative_parse_malformed_coap(void) 00515 { 00516 coap_address.addr_ptr = address; 00517 00518 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_malformed), coap_message_malformed, NULL); 00519 TEST_ASSERT_NULL(coap_header_ptr); 00520 } 00521 00522 /** 00523 * \fn test_libcoap_negative_parse_empty_con(void) 00524 * 00525 * \brief - Call sn_coap_protocol_parse with "CoAP ping" message. Should return RST 00526 * 00527 */ 00528 void test_libcoap_negative_parse_empty_con(void) 00529 { 00530 coap_address.addr_ptr = address; 00531 00532 sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_empty_con), coap_message_empty_con, NULL); 00533 TEST_ASSERT_NULL(coap_header_ptr); 00534 00535 } 00536 00537 /** 00538 * \fn test_libcoap_negative_parse_wrong_code_1(void) 00539 * 00540 * \brief - Call sn_coap_protocol_parse with wrong message code. Should return RST 00541 * 00542 */ 00543 void test_libcoap_negative_parse_wrong_code_1(void) 00544 { 00545 coap_address.addr_ptr = address; 00546 00547 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); 00548 TEST_ASSERT_NULL(coap_header_ptr); 00549 00550 } 00551 00552 /** 00553 * \fn test_libcoap_negative_parse_wrong_code_6(void) 00554 * 00555 * \brief - Call sn_coap_protocol_parse with wrong message code. Should return RST 00556 * 00557 */ 00558 void test_libcoap_negative_parse_wrong_code_6(void) 00559 { 00560 coap_address.addr_ptr = address; 00561 00562 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); 00563 TEST_ASSERT_NULL(coap_header_ptr); 00564 00565 } 00566 00567 /** 00568 * \fn test_libcoap_negative_parse_wrong_code_7(void) 00569 * 00570 * \brief - Call sn_coap_protocol_parse with wrong message code. Should return RST 00571 * 00572 */ 00573 void test_libcoap_negative_parse_wrong_code_7(void) 00574 { 00575 coap_address.addr_ptr = address; 00576 00577 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); 00578 TEST_ASSERT_NULL(coap_header_ptr); 00579 00580 } 00581 00582 00583 /*******************************/ 00584 /*** non-test functions ***/ 00585 /*******************************/ 00586 00587 void fill_with_random(uint8_t *ptr, uint16_t len) 00588 { 00589 while (len--) { 00590 *(ptr + len) = 'a'; 00591 } 00592 } 00593 00594 void *own_alloc(uint16_t size) 00595 { 00596 return malloc(size); 00597 } 00598 00599 void own_free(void *ptr) 00600 { 00601 free(ptr); 00602 } 00603 00604 uint8_t tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr, void *param) 00605 { 00606 return 0; 00607 } 00608 00609 int8_t rx_callback(sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *address_ptr, void *param) 00610 { 00611 return 0; 00612 } 00613 00614 00615 00616 00617 00618 00619 00620 00621
Generated on Tue Jul 12 2022 17:34:56 by
1.7.2