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.
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
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_libNsdl\Test.c 00018 * 00019 * \brief Unit tests for libNsdl 00020 * NOTE!! Needs libCoap.a from ../../nsdl-c/libCoap/x86_gcc 00021 * 00022 * */ 00023 00024 #include "ns_types.h" 00025 00026 #include "sn_nsdl.h" 00027 #include "sn_coap_header.h" 00028 #include "sn_coap_protocol.h" 00029 #include "sn_nsdl_lib.h" 00030 00031 #include "unity.h" 00032 #include "string.h" 00033 #include "stdlib.h" 00034 00035 /* non-test function declarations */ 00036 void fill_with_random(uint8_t *ptr, uint16_t len); 00037 void *own_alloc(uint16_t size); 00038 void own_free(void *ptr); 00039 uint8_t nsdl_tx_dummy(struct nsdl_s *, sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *); 00040 uint8_t nsdl_rx_dummy(struct nsdl_s *, sn_coap_hdr_s *, sn_nsdl_addr_s *); 00041 uint8_t dynamic_callback(struct nsdl_s *handle, sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *address_ptr, sn_nsdl_capab_e protocol); 00042 static void init_temp_variables(void); 00043 00044 static uint8_t res1_path[] = {"test/res1"}; 00045 static uint8_t res1_content[] = {"res1 content"}; 00046 00047 static uint8_t res2_path[] = {"test/res2"}; 00048 static uint8_t res2_content[] = {"res2 content"}; 00049 00050 static uint8_t res3_path[] = {"test/res3"}; 00051 static uint8_t res3_content[] = {"res3 content"}; 00052 00053 static uint8_t res4_path[] = {"test/res4"}; 00054 static uint8_t res4_content[] = {"res4 content"}; 00055 00056 static uint8_t res5_path[] = {"test/res5"}; 00057 static uint8_t res5_content[] = {"res5 content"}; 00058 00059 static uint8_t res_negative_path[] = {"test/negative"}; 00060 static uint8_t res_negative_content[] = {"negative_test content"}; 00061 00062 static uint8_t address[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; 00063 00064 /* Change byte 13 to differend resources */ 00065 static uint8_t get_message[14] = {0x40, 0x01, 0x12, 0x34, 0xb4, 0x74, 0x65, 0x73, 0x74, 0x04, 0x72, 0x65, 0x73, 0x31}; 00066 static uint8_t put_message[18] = {0x40, 0x03, 0x12, 0x34, 0xb4, 0x74, 0x65, 0x73, 0x74, 0x04, 0x72, 0x65, 0x73, 0x32, 0xff, 0x61, 0x62, 0x63}; 00067 static uint8_t post_message[18] = {0x40, 0x02, 0x12, 0x34, 0xb4, 0x74, 0x65, 0x73, 0x74, 0x04, 0x72, 0x65, 0x73, 0x33, 0xff, 0x61, 0x62, 0x63}; 00068 static uint8_t delete_message[14] = {0x40, 0x04, 0x12, 0x34, 0xb4, 0x74, 0x65, 0x73, 0x74, 0x04, 0x72, 0x65, 0x73, 0x34}; 00069 00070 static uint8_t message_payload[3] = {0x61, 0x62, 0x63}; 00071 00072 static uint8_t get_response[17] = {0x60, 0x45, 0x12, 0x34, 0xff, 0x72, 0x65, 0x73, 0x31, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74}; 00073 static uint8_t response_not_allowed[4] = {0x60, 0x85, 0x12, 0x34}; 00074 static uint8_t response_changed[4] = {0x60, 0x44, 0x12, 0x34}; 00075 static uint8_t response_created[4] = {0x60, 0x41, 0x12, 0x34}; 00076 static uint8_t response_deleted[4] = {0x60, 0x42, 0x12, 0x34}; 00077 00078 #define NSP_ADDRESS_LEN 4 00079 #define NSP_PORT 5683 00080 00081 #define NSP_REGISTRATION_MESSAGE_LEN 72 00082 static uint8_t registration_message[NSP_REGISTRATION_MESSAGE_LEN] = { 00083 0x40, 0x02, 0x01, 0x47, 0xb2, 0x72, 0x64, 0xff, 0x3c, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x72, 00084 0x65, 0x73, 0x35, 0x3e, 0x2c, 0x3c, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x72, 0x65, 0x73, 0x34, 00085 0x3e, 0x2c, 0x3c, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x72, 0x65, 0x73, 0x33, 0x3e, 0x2c, 0x3c, 00086 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x72, 0x65, 0x73, 0x32, 0x3e, 0x2c, 0x3c, 0x2f, 0x74, 0x65, 00087 0x73, 0x74, 0x2f, 0x72, 0x65, 0x73, 0x31, 0x3e 00088 }; 00089 00090 static uint16_t message_temp_len = 0; 00091 static uint8_t *message_temp_ptr = 0; 00092 static sn_nsdl_addr_s *address_temp_ptr = 0; 00093 00094 struct nsdl_s *handle; 00095 00096 /* Unity test code starts */ 00097 void setUp(void) 00098 { 00099 //This is run before EACH TEST 00100 00101 } 00102 00103 void tearDown(void) 00104 { 00105 00106 } 00107 00108 /** 00109 * \fn void test_libnsdl_init(void) 00110 * 00111 * \brief Tests sn_nsdl_init - function. Function must return failure (-1) if initialization fails. 00112 * 00113 * Negative test cases: 00114 * - Memory structure contains null pointer(s) 00115 * - One of the parameter is null 00116 * 00117 */ 00118 void test_libnsdl_init(void) 00119 { 00120 /* Memory struct contains null */ 00121 handle = sn_nsdl_init(&nsdl_tx_dummy, &nsdl_rx_dummy, NULL, NULL); 00122 TEST_ASSERT_NULL(handle); 00123 00124 handle = sn_nsdl_init(&nsdl_tx_dummy, &nsdl_rx_dummy, &own_alloc, NULL); 00125 TEST_ASSERT_NULL(handle); 00126 00127 handle = sn_nsdl_init(&nsdl_tx_dummy, &nsdl_rx_dummy, NULL, &own_free); 00128 TEST_ASSERT_NULL(handle); 00129 00130 /* One of parameters is null */ 00131 handle = sn_nsdl_init(NULL, &nsdl_rx_dummy, &own_alloc, &own_free); 00132 TEST_ASSERT_NULL(handle); 00133 00134 handle = sn_nsdl_init(&nsdl_tx_dummy, NULL, &own_alloc, &own_free); 00135 TEST_ASSERT_NULL(handle); 00136 00137 /* OK case */ 00138 handle = sn_nsdl_init(&nsdl_tx_dummy, &nsdl_rx_dummy, &own_alloc, &own_free); 00139 TEST_ASSERT_NOT_NULL(handle); 00140 } 00141 00142 /** 00143 * \fn void test_libnsdl_create_resource(void) 00144 * 00145 * \brief Tests sn_nsdl_create_resource - function. Creates five resources. 00146 * 00147 * STATIC RESOURCES: 00148 * - Resource 1 00149 * * Get allowed 00150 * - Resource 2 00151 * * Put allowed 00152 * - Resource 3 00153 * * Post allowed 00154 * - Resource 4 00155 * * Delete allowed 00156 * 00157 * DYNAMIC RESOURCES: 00158 * - Resource 5 00159 * * All allowed 00160 * 00161 * Negative test cases: 00162 * - Resource already exists 00163 * - Null pointer as a parameter 00164 * 00165 */ 00166 void test_libnsdl_create_resource(void) 00167 { 00168 sn_nsdl_resource_info_s resource; 00169 sn_nsdl_resource_parameters_s resource_parameters; 00170 int8_t ret_val; 00171 00172 memset(&resource, 0, sizeof(sn_nsdl_resource_info_s)); 00173 memset(&resource_parameters, 0, sizeof(sn_nsdl_resource_parameters_s)); 00174 00175 00176 /* Resource 1*/ 00177 resource.access = SN_GRS_GET_ALLOWED; 00178 resource.mode = SN_GRS_STATIC; 00179 resource.path = res1_path; 00180 resource.pathlen = sizeof(res1_path) - 1; 00181 resource.resource = res1_content; 00182 resource.resourcelen = sizeof(res1_content) - 1; 00183 resource.resource_parameters_ptr = &resource_parameters; 00184 00185 ret_val = sn_nsdl_create_resource(handle, &resource); 00186 TEST_ASSERT_EQUAL(0, ret_val); 00187 00188 00189 /* Resource 2 */ 00190 resource.access = SN_GRS_PUT_ALLOWED; 00191 resource.path = res2_path; 00192 resource.pathlen = sizeof(res2_path) - 1; 00193 resource.resource = res2_content; 00194 resource.resourcelen = sizeof(res2_content) - 1; 00195 00196 ret_val = sn_nsdl_create_resource(handle, &resource); 00197 TEST_ASSERT_EQUAL(0, ret_val); 00198 00199 00200 /* Resource 3 */ 00201 resource.access = SN_GRS_POST_ALLOWED; 00202 resource.path = res3_path; 00203 resource.pathlen = sizeof(res3_path) - 1; 00204 resource.resource = res3_content; 00205 resource.resourcelen = sizeof(res3_content) - 1; 00206 00207 ret_val = sn_nsdl_create_resource(handle, &resource); 00208 TEST_ASSERT_EQUAL(0, ret_val); 00209 00210 00211 /* Resource 4 */ 00212 resource.access = SN_GRS_DELETE_ALLOWED; 00213 resource.path = res4_path; 00214 resource.pathlen = sizeof(res4_path) - 1; 00215 resource.resource = res4_content; 00216 resource.resourcelen = sizeof(res4_content) - 1; 00217 00218 ret_val = sn_nsdl_create_resource(handle, &resource); 00219 TEST_ASSERT_EQUAL(0, ret_val); 00220 00221 00222 /* Resource 5 - Dynamic*/ 00223 resource.mode = SN_GRS_DYNAMIC; 00224 00225 resource.access = 0xFF; 00226 resource.path = res5_path; 00227 resource.pathlen = sizeof(res5_path) - 1; 00228 resource.resource = res5_content; 00229 resource.resourcelen = sizeof(res5_content) - 1; 00230 resource.sn_grs_dyn_res_callback = &dynamic_callback; 00231 00232 ret_val = sn_nsdl_create_resource(handle, &resource); 00233 TEST_ASSERT_EQUAL(0, ret_val); 00234 00235 /*** Negative test cases ***/ 00236 00237 /* Already exists */ 00238 ret_val = sn_nsdl_create_resource(handle, &resource); 00239 TEST_ASSERT_EQUAL(-2, ret_val); 00240 00241 /* Null pointer */ 00242 ret_val = sn_nsdl_create_resource(handle, NULL); 00243 TEST_ASSERT_EQUAL(-1, ret_val); 00244 00245 ret_val = sn_nsdl_create_resource(NULL, &resource); 00246 TEST_ASSERT_EQUAL(-1, ret_val); 00247 00248 resource.path = NULL; 00249 resource.pathlen = sizeof(res_negative_path) - 1; 00250 resource.resource = res_negative_content; 00251 resource.resourcelen = sizeof(res_negative_content) - 1; 00252 00253 ret_val = sn_nsdl_create_resource(handle, &resource); 00254 TEST_ASSERT_EQUAL(-3, ret_val); 00255 00256 resource.path = res_negative_path; 00257 resource.pathlen = 0; 00258 00259 ret_val = sn_nsdl_create_resource(handle, &resource); 00260 TEST_ASSERT_EQUAL(-3, ret_val); 00261 00262 } 00263 00264 /** 00265 * \fn test_libnsdl_list_resources(void) 00266 * 00267 * \brief Calls sn_nsdl_list_resource - function 00268 * Function must return list of early created resources 00269 */ 00270 00271 void test_libnsdl_list_resources(void) 00272 { 00273 sn_grs_resource_list_s *resource_list_ptr; 00274 00275 resource_list_ptr = sn_nsdl_list_resource(handle, 0, 0); 00276 00277 TEST_ASSERT_EQUAL(5, resource_list_ptr->res_count); 00278 00279 TEST_ASSERT_EQUAL_INT8_ARRAY(resource_list_ptr->res[4].path, res1_path, resource_list_ptr->res[4].pathlen); 00280 TEST_ASSERT_EQUAL_INT8_ARRAY(resource_list_ptr->res[3].path, res2_path, resource_list_ptr->res[3].pathlen); 00281 TEST_ASSERT_EQUAL_INT8_ARRAY(resource_list_ptr->res[2].path, res3_path, resource_list_ptr->res[2].pathlen); 00282 TEST_ASSERT_EQUAL_INT8_ARRAY(resource_list_ptr->res[1].path, res4_path, resource_list_ptr->res[1].pathlen); 00283 TEST_ASSERT_EQUAL_INT8_ARRAY(resource_list_ptr->res[0].path, res5_path, resource_list_ptr->res[0].pathlen); 00284 00285 } 00286 00287 /** 00288 * \fn test_libnsdl_get_resource(void) 00289 * 00290 * \brief Calls sn_nsdl_get_resource - function 00291 * Function must return pointer to created resource 00292 * 00293 * Negative test cases: 00294 * - call function with null parameter 00295 * 00296 */ 00297 00298 void test_libnsdl_get_resource(void) 00299 { 00300 sn_nsdl_resource_info_s *res_ptr; 00301 00302 /* With null pointer */ 00303 res_ptr = sn_nsdl_get_resource(handle, 0, res1_path); 00304 TEST_ASSERT_NULL(res_ptr); 00305 00306 res_ptr = sn_nsdl_get_resource(handle, sizeof(res1_path) - 1, NULL); 00307 TEST_ASSERT_NULL(res_ptr); 00308 00309 res_ptr = sn_nsdl_get_resource(NULL, sizeof(res1_path) - 1, res1_path); 00310 TEST_ASSERT_NULL(res_ptr); 00311 00312 /* Ok case, gets resource 1 and checks that path and content are ok */ 00313 res_ptr = sn_nsdl_get_resource(handle, sizeof(res1_path) - 1, res1_path); 00314 TEST_ASSERT_NOT_NULL(res_ptr); 00315 00316 TEST_ASSERT_EQUAL_INT8_ARRAY(res_ptr->path, res1_path, sizeof(res1_path) - 1); 00317 TEST_ASSERT_EQUAL_INT8_ARRAY(res_ptr->resource, res1_content, sizeof(res1_content) - 1); 00318 } 00319 00320 /** 00321 * \fn test_libnsdl_set_nsp_address(void) 00322 * 00323 * \brief Sets NSP address to nsdl-library 00324 * 00325 * Negative test cases: 00326 * - call function with null parameter 00327 */ 00328 void test_libnsdl_set_nsp_address(void) 00329 { 00330 int8_t ret_val = 0; 00331 00332 /* with null pointer */ 00333 ret_val = set_NSP_address(handle, NULL, NSP_PORT, SN_NSDL_ADDRESS_TYPE_IPV4); 00334 TEST_ASSERT_EQUAL(-1, ret_val); 00335 00336 ret_val = set_NSP_address(NULL, address, NSP_PORT, SN_NSDL_ADDRESS_TYPE_IPV4); 00337 TEST_ASSERT_EQUAL(-1, ret_val); 00338 00339 /* OK case */ 00340 ret_val = set_NSP_address(handle, address, NSP_PORT, SN_NSDL_ADDRESS_TYPE_IPV4); 00341 TEST_ASSERT_EQUAL(0, ret_val); 00342 } 00343 00344 /** 00345 * \fn test_libnsdl_set_nsp_address_2(void) 00346 * 00347 * \brief Sets NSP address to nsdl-library 00348 * 00349 * Negative test cases: 00350 * - call function with null parameter 00351 */ 00352 void test_libnsdl_set_nsp_address_2(void) 00353 { 00354 int8_t ret_val = 0; 00355 00356 /* with null pointer */ 00357 ret_val = set_NSP_address_2(handle, NULL, NSP_ADDRESS_LEN, NSP_PORT, SN_NSDL_ADDRESS_TYPE_IPV4); 00358 TEST_ASSERT_EQUAL(-1, ret_val); 00359 00360 ret_val = set_NSP_address_2(NULL, address, NSP_ADDRESS_LEN, NSP_PORT, SN_NSDL_ADDRESS_TYPE_IPV4); 00361 TEST_ASSERT_EQUAL(-1, ret_val); 00362 00363 /* OK case */ 00364 ret_val = set_NSP_address_2(handle, address, NSP_ADDRESS_LEN, NSP_PORT, SN_NSDL_ADDRESS_TYPE_IPV4); 00365 TEST_ASSERT_EQUAL(0, ret_val); 00366 } 00367 00368 /** 00369 * \fn test_libnsdl_register(void) 00370 * 00371 * \brief Calls sn_nsdl_get_resource - function 00372 * 00373 * Negative test cases: 00374 * - call function with null parameter 00375 * 00376 */ 00377 00378 void test_libnsdl_register(void) 00379 { 00380 int8_t ret_val = 0; 00381 sn_nsdl_ep_parameters_s endpoint_info; 00382 00383 memset(&endpoint_info, 0, sizeof(sn_nsdl_ep_parameters_s)); 00384 00385 /* With null pointer */ 00386 ret_val = sn_nsdl_register_endpoint(handle, NULL); 00387 TEST_ASSERT_EQUAL(0, ret_val); 00388 00389 ret_val = sn_nsdl_register_endpoint(NULL, &endpoint_info); 00390 TEST_ASSERT_EQUAL(0, ret_val); 00391 00392 /* OK case */ 00393 ret_val = sn_nsdl_register_endpoint(handle, &endpoint_info); 00394 TEST_ASSERT_NOT_EQUAL(0, ret_val); 00395 00396 /* Check address */ 00397 TEST_ASSERT_EQUAL(NSP_PORT, address_temp_ptr->port); 00398 TEST_ASSERT_EQUAL_INT8_ARRAY(address, address_temp_ptr->addr_ptr, address_temp_ptr->addr_len); 00399 00400 /* Check registration message !Note, message ID [bytes 2-3] is random in every message! */ 00401 TEST_ASSERT_EQUAL(NSP_REGISTRATION_MESSAGE_LEN, message_temp_len); 00402 TEST_ASSERT_EQUAL_INT8_ARRAY(message_temp_ptr, registration_message, 2); //message header 00403 TEST_ASSERT_EQUAL_INT8_ARRAY(message_temp_ptr + 4, registration_message + 4, message_temp_len - 4); //Rest of the message 00404 00405 init_temp_variables(); 00406 } 00407 00408 /** 00409 * \fn void test_libnsdl_get_requests_to_resources(void) 00410 * 00411 * \brief Process CoAP message "GET" 00412 * 00413 * - GET to resource that is allowed 00414 * - GET to resource that is not allowed 00415 * 00416 */ 00417 00418 void test_libnsdl_get_requests_to_resources(void) 00419 { 00420 sn_nsdl_addr_s address_struct; 00421 int8_t ret_val = 0; 00422 00423 address_struct.addr_ptr = address; 00424 address_struct.addr_len = 16; 00425 address_struct.port = NSP_PORT; 00426 00427 /* Get to resource that is allowed */ 00428 ret_val = sn_nsdl_process_coap(handle, get_message, sizeof(get_message), &address_struct); 00429 00430 TEST_ASSERT_EQUAL(0, ret_val); 00431 TEST_ASSERT_EQUAL(sizeof(get_response), message_temp_len); 00432 TEST_ASSERT_EQUAL_INT8_ARRAY(message_temp_ptr, get_response, message_temp_len); 00433 00434 init_temp_variables(); 00435 00436 /* Get to resource that is not allowed */ 00437 get_message[13] = 0x32; 00438 ret_val = sn_nsdl_process_coap(handle, get_message, sizeof(get_message), &address_struct); 00439 get_message[13] = 0x31; 00440 00441 TEST_ASSERT_EQUAL(0, ret_val); 00442 00443 TEST_ASSERT_EQUAL(sizeof(response_not_allowed), message_temp_len); 00444 TEST_ASSERT_EQUAL_INT8_ARRAY(message_temp_ptr, response_not_allowed, message_temp_len); 00445 00446 init_temp_variables(); 00447 } 00448 00449 /** 00450 * \fn void test_libnsdl_put_requests_to_resources(void) 00451 * 00452 * \brief Process CoAP message "PUT" 00453 * 00454 * - PUT to resource that is allowed 00455 * - PUT to resource that is not allowed 00456 */ 00457 void test_libnsdl_put_requests_to_resources(void) 00458 { 00459 sn_nsdl_addr_s address_struct; 00460 int8_t ret_val = 0; 00461 sn_nsdl_resource_info_s *res_ptr; 00462 00463 address_struct.addr_ptr = address; 00464 address_struct.addr_len = 16; 00465 address_struct.port = NSP_PORT; 00466 00467 /* Put to resource that is allowed */ 00468 ret_val = sn_nsdl_process_coap(handle, put_message, sizeof(put_message), &address_struct); 00469 00470 TEST_ASSERT_EQUAL(0, ret_val); 00471 TEST_ASSERT_EQUAL(sizeof(response_changed), message_temp_len); 00472 TEST_ASSERT_EQUAL_INT8_ARRAY(response_changed, message_temp_ptr, message_temp_len); 00473 00474 //get resource and read res value 00475 res_ptr = sn_nsdl_get_resource(handle, sizeof(res2_path) - 1, res2_path); 00476 TEST_ASSERT_EQUAL_INT8_ARRAY(message_payload, res_ptr->resource, sizeof(message_payload)); 00477 00478 init_temp_variables(); 00479 00480 /* Put to resource that is not allowed */ 00481 put_message[13] = 0x31; 00482 ret_val = sn_nsdl_process_coap(handle, put_message, sizeof(put_message), &address_struct); 00483 put_message[13] = 0x32; 00484 00485 TEST_ASSERT_EQUAL(0, ret_val); 00486 TEST_ASSERT_EQUAL(sizeof(response_not_allowed), message_temp_len); 00487 TEST_ASSERT_EQUAL_INT8_ARRAY(response_not_allowed, message_temp_ptr, message_temp_len); 00488 00489 init_temp_variables(); 00490 } 00491 00492 /** 00493 * \fn void test_libnsdl_post_requests_to_resources(void) 00494 * 00495 * \brief Process CoAP message "POST" 00496 * 00497 * - POST to resource that is allowed 00498 * - POST to resource that is not allowed 00499 * 00500 */ 00501 void test_libnsdl_post_requests_to_resources(void) 00502 { 00503 sn_nsdl_addr_s address_struct; 00504 int8_t ret_val = 0; 00505 sn_nsdl_resource_info_s *res_ptr; 00506 00507 address_struct.addr_ptr = address; 00508 address_struct.addr_len = 16; 00509 address_struct.port = NSP_PORT; 00510 00511 /* Post to resource that is allowed */ 00512 ret_val = sn_nsdl_process_coap(handle, post_message, sizeof(post_message), &address_struct); 00513 00514 TEST_ASSERT_EQUAL(0, ret_val); 00515 TEST_ASSERT_EQUAL(sizeof(response_changed), message_temp_len); 00516 TEST_ASSERT_EQUAL_INT8_ARRAY(response_changed, message_temp_ptr, message_temp_len); 00517 00518 //get resource and read res value 00519 res_ptr = sn_nsdl_get_resource(handle, sizeof(res3_path) - 1, res3_path); 00520 TEST_ASSERT_EQUAL_INT8_ARRAY(message_payload, res_ptr->resource, sizeof(message_payload)); 00521 00522 init_temp_variables(); 00523 00524 /* Post to resource that is not allowed */ 00525 post_message[13] = 0x32; 00526 ret_val = sn_nsdl_process_coap(handle, post_message, sizeof(post_message), &address_struct); 00527 post_message[13] = 0x33; 00528 00529 TEST_ASSERT_EQUAL(0, ret_val); 00530 TEST_ASSERT_EQUAL(sizeof(response_not_allowed), message_temp_len); 00531 TEST_ASSERT_EQUAL_INT8_ARRAY(response_not_allowed, message_temp_ptr, message_temp_len); 00532 00533 init_temp_variables(); 00534 } 00535 00536 /** 00537 * \fn void test_libnsdl_delete_requests_to_resources(void) 00538 * 00539 * \brief Process CoAP message "DELETE" 00540 * 00541 * - DELETE to resource that is allowed 00542 * - DELETE to resource that is not allowed 00543 * 00544 */ 00545 void test_libnsdl_delete_requests_to_resources(void) 00546 { 00547 sn_nsdl_addr_s address_struct; 00548 int8_t ret_val = 0; 00549 sn_nsdl_resource_info_s *res_ptr; 00550 00551 address_struct.addr_ptr = address; 00552 address_struct.addr_len = 16; 00553 address_struct.port = NSP_PORT; 00554 00555 /* Delete to resource that is not allowed */ 00556 delete_message[13] = 0x33; 00557 ret_val = sn_nsdl_process_coap(handle, delete_message, sizeof(delete_message), &address_struct); 00558 delete_message[13] = 0x34; 00559 00560 TEST_ASSERT_EQUAL(0, ret_val); 00561 TEST_ASSERT_EQUAL(sizeof(response_not_allowed), message_temp_len); 00562 TEST_ASSERT_EQUAL_INT8_ARRAY(response_not_allowed, message_temp_ptr, message_temp_len); 00563 00564 init_temp_variables(); 00565 } 00566 00567 /** 00568 * \fn void test_libnsdl_update_resource_value(void) 00569 * 00570 * \brief Updates resource value from application 00571 * 00572 */ 00573 void test_libnsdl_update_resource_value(void) 00574 { 00575 sn_nsdl_resource_info_s resource; 00576 sn_nsdl_resource_info_s *res_ptr; 00577 sn_nsdl_resource_parameters_s resource_parameters; 00578 int8_t ret_val; 00579 00580 memset(&resource, 0, sizeof(sn_nsdl_resource_info_s)); 00581 memset(&resource_parameters, 0, sizeof(sn_nsdl_resource_parameters_s)); 00582 00583 /* Resource 1*/ 00584 resource.access = SN_GRS_GET_ALLOWED; 00585 resource.mode = SN_GRS_STATIC; 00586 resource.path = res1_path; 00587 resource.pathlen = sizeof(res1_path) - 1; 00588 resource.resource = message_payload; 00589 resource.resourcelen = sizeof(message_payload); 00590 resource.resource_parameters_ptr = &resource_parameters; 00591 00592 ret_val = sn_nsdl_update_resource(handle, &resource); 00593 00594 TEST_ASSERT_EQUAL(0, ret_val); 00595 00596 //get resource and read res value 00597 res_ptr = sn_nsdl_get_resource(handle, sizeof(res1_path) - 1, res1_path); 00598 TEST_ASSERT_NOT_NULL(res_ptr); 00599 TEST_ASSERT_EQUAL_INT8_ARRAY(message_payload, res_ptr->resource, sizeof(message_payload)); 00600 00601 } 00602 00603 /** 00604 * \fn void test_libnsdl_delete_resource(void) 00605 * 00606 * \brief Delete resource from application 00607 * 00608 */ 00609 void test_libnsdl_delete_resource(void) 00610 { 00611 sn_nsdl_resource_info_s *res_ptr; 00612 int8_t ret_val; 00613 00614 ret_val = sn_nsdl_delete_resource(handle, sizeof(res4_path) - 1, res4_path); 00615 00616 TEST_ASSERT_EQUAL(0, ret_val); 00617 00618 //get resource and read res value 00619 res_ptr = sn_nsdl_get_resource(handle, sizeof(res4_path) - 1, res4_path); 00620 TEST_ASSERT_NULL(res_ptr); 00621 } 00622 00623 00624 /*******************************/ 00625 /*** non-test functions ***/ 00626 /*******************************/ 00627 00628 void fill_with_random(uint8_t *ptr, uint16_t len) 00629 { 00630 while (len--) { 00631 *(ptr + len) = 'a'; 00632 } 00633 } 00634 00635 void *own_alloc(uint16_t size) 00636 { 00637 return malloc(size); 00638 } 00639 00640 void own_free(void *ptr) 00641 { 00642 free(ptr); 00643 } 00644 00645 uint8_t nsdl_tx_dummy(struct nsdl_s *handle, sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) 00646 { 00647 message_temp_ptr = malloc(data_len); 00648 memcpy(message_temp_ptr, data_ptr, data_len); 00649 00650 message_temp_len = data_len; 00651 00652 address_temp_ptr = malloc(sizeof(sn_nsdl_addr_s)); 00653 memcpy(address_temp_ptr, address_ptr, sizeof(sn_nsdl_addr_s)); 00654 00655 #if 0 00656 uint8_t i; 00657 printf("\nlength = %d\n", data_len); 00658 for (i = 0; i < data_len; i++) { 00659 printf("0x"); 00660 if (*(data_ptr + i) <= 0x0F) { 00661 printf("0"); 00662 } 00663 printf("%x, ", *(data_ptr + i)); 00664 if (!((i + 1) % 16)) { 00665 printf("\n"); 00666 } 00667 } 00668 printf("\n"); 00669 #endif 00670 } 00671 00672 uint8_t nsdl_rx_dummy(struct nsdl_s *handle, sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *address_ptr) 00673 { 00674 00675 } 00676 00677 uint8_t dynamic_callback(struct nsdl_s *handle, sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *address_ptr, sn_nsdl_capab_e protocol) 00678 { 00679 00680 } 00681 00682 static void init_temp_variables(void) 00683 { 00684 if (message_temp_ptr) { 00685 free(message_temp_ptr); 00686 message_temp_ptr = 0; 00687 } 00688 if (address_temp_ptr) { 00689 free(address_temp_ptr); 00690 address_temp_ptr = 0; 00691 } 00692 message_temp_len = 0; 00693 } 00694 00695 00696 00697 00698 00699 00700 00701
Generated on Tue Jul 12 2022 11:02:54 by
