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_m2mbase.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 "test_m2mbase.h" 00018 00019 #include "m2mreportobserver.h" 00020 #include "m2mreporthandler.h" 00021 #include "m2mreporthandler_stub.h" 00022 #include "nsdlaccesshelper_stub.h" 00023 #include "m2mresource_stub.h" 00024 #include "m2mobject_stub.h" 00025 #include "m2mobjectinstance_stub.h" 00026 #include "m2mresource_stub.h" 00027 00028 static bool value_update_called = false; 00029 static void value_updated_function(const char* name) { 00030 value_update_called = true; 00031 } 00032 00033 class MyTest{ 00034 public: 00035 void value_updated_function(const char* name) { 00036 visited = true; 00037 } 00038 00039 bool visited; 00040 }; 00041 00042 00043 class Observer : public M2MReportObserver { 00044 public: 00045 Observer(){} 00046 ~Observer(){} 00047 00048 void observation_to_be_sent(m2m::Vector<uint16_t>,bool){} 00049 }; 00050 00051 Test_M2MBase::Test_M2MBase(char* path, Handler *handler) 00052 : M2MBase("name",M2MBase::Dynamic, "type", path, false) 00053 00054 { 00055 obsHandler = handler; 00056 } 00057 00058 Test_M2MBase::~Test_M2MBase() 00059 { 00060 } 00061 00062 void Test_M2MBase::test_set_operation() 00063 { 00064 this->_sn_resource->dynamic_resource_params->static_resource_parameters->mode = M2MBase::Static; 00065 M2MBase::Operation test = M2MBase::GET_ALLOWED; 00066 set_operation(test); 00067 00068 CHECK(test == this->operation()); 00069 00070 this->_sn_resource->dynamic_resource_params->static_resource_parameters->mode = M2MBase::Dynamic; 00071 test = M2MBase::PUT_ALLOWED; 00072 set_operation(test); 00073 00074 CHECK(test == this->operation()); 00075 } 00076 00077 void Test_M2MBase::test_set_base_type() 00078 { 00079 set_base_type(M2MBase::ObjectInstance); 00080 CHECK(M2MBase::ObjectInstance == this->base_type()); 00081 } 00082 00083 void Test_M2MBase::test_set_interface_description() 00084 { 00085 String test = "interface_description"; 00086 set_interface_description(test); 00087 00088 CHECK(test == this->interface_description()); 00089 } 00090 00091 void Test_M2MBase::test_uri_path() 00092 { 00093 // Default value in ctor 00094 String test = "test"; 00095 CHECK(test == uri_path()); 00096 } 00097 00098 void Test_M2MBase::test_set_resource_type() 00099 { 00100 String test = "resource_type"; 00101 set_resource_type(test); 00102 00103 CHECK(test == this->_sn_resource->dynamic_resource_params->static_resource_parameters->resource_type_ptr); 00104 } 00105 00106 void Test_M2MBase::test_set_coap_content_type() 00107 { 00108 u_int8_t test = 1; 00109 set_coap_content_type(test); 00110 00111 CHECK(test == this->coap_content_type()); 00112 } 00113 00114 void Test_M2MBase::test_set_instance_id() 00115 { 00116 u_int16_t test = 1; 00117 set_instance_id(test); 00118 00119 CHECK(test == this->instance_id()); 00120 } 00121 00122 void Test_M2MBase::test_set_observable() 00123 { 00124 bool test = true; 00125 set_observable(test); 00126 00127 CHECK(test == this->is_observable()); 00128 } 00129 00130 void Test_M2MBase::test_add_observation_level() 00131 { 00132 add_observation_level(M2MBase::R_Attribute); 00133 CHECK(M2MBase::R_Attribute == this->_observation_level); 00134 00135 add_observation_level(M2MBase::O_Attribute); 00136 CHECK(M2MBase::OR_Attribute == this->_observation_level); 00137 } 00138 00139 void Test_M2MBase::test_remove_observation_level() 00140 { 00141 this->_observation_level = M2MBase::OR_Attribute; 00142 remove_observation_level(M2MBase::R_Attribute); 00143 CHECK(M2MBase::O_Attribute == this->_observation_level); 00144 00145 remove_observation_level(M2MBase::O_Attribute); 00146 CHECK(M2MBase::None == this->_observation_level); 00147 00148 this->_observation_level = M2MBase::OI_Attribute; 00149 remove_observation_level(M2MBase::R_Attribute); 00150 CHECK(M2MBase::OI_Attribute == this->_observation_level); 00151 00152 remove_observation_level(M2MBase::OI_Attribute); 00153 CHECK(M2MBase::None == this->_observation_level); 00154 remove_observation_level(M2MBase::OI_Attribute); 00155 CHECK(M2MBase::None == this->_observation_level); 00156 } 00157 00158 void Test_M2MBase::test_set_under_observation() 00159 { 00160 this->set_base_type(M2MBase::ObjectInstance); 00161 00162 bool test = true; 00163 set_under_observation(test,NULL); 00164 set_under_observation(test,obsHandler); 00165 00166 CHECK(obsHandler == this->_observation_handler); 00167 00168 set_under_observation(test,NULL); 00169 00170 test = false; 00171 set_under_observation(test,NULL); 00172 00173 test = false; 00174 set_under_observation(test,obsHandler); 00175 00176 set_under_observation(test,obsHandler); 00177 } 00178 00179 void Test_M2MBase::test_set_observation_token() 00180 { 00181 this->_token_length = 4; 00182 this->_token = (u_int8_t *)malloc(this->_token_length); 00183 String test = "token"; 00184 set_observation_token((const u_int8_t*)test.c_str(), (u_int8_t)test.size()); 00185 00186 CHECK(this->_token_length == 5); 00187 } 00188 00189 void Test_M2MBase::test_observation_level() 00190 { 00191 this->_observation_level = M2MBase::OR_Attribute; 00192 CHECK(M2MBase::OR_Attribute == this->observation_level()); 00193 } 00194 00195 void Test_M2MBase::test_get_observation_token() 00196 { 00197 u_int8_t test_value[] = {"val"}; 00198 u_int32_t value_length((u_int32_t)sizeof(test_value)); 00199 00200 u_int8_t* out_value = (u_int8_t *)malloc(value_length); 00201 u_int32_t out_size = value_length; 00202 memcpy((u_int8_t *)out_value, (u_int8_t *)test_value, value_length); 00203 00204 u_int8_t test[] = {"token"}; 00205 this->_token_length = (u_int8_t)sizeof(test); 00206 this->_token = (u_int8_t *)malloc(this->_token_length); 00207 memcpy((u_int8_t *)this->_token, (u_int8_t *)test, this->_token_length); 00208 00209 get_observation_token(out_value,out_size); 00210 00211 CHECK(out_size == 6); 00212 00213 free(out_value); 00214 } 00215 00216 void Test_M2MBase::test_mode() 00217 { 00218 CHECK(M2MBase::Dynamic == mode()); 00219 } 00220 00221 void Test_M2MBase::test_observation_number() 00222 { 00223 u_int8_t test = 1; 00224 this->_observation_number = test; 00225 00226 CHECK(test == observation_number()); 00227 } 00228 00229 void Test_M2MBase::test_name() 00230 { 00231 // Default value in ctor 00232 String test = "name"; 00233 CHECK(test == name()); 00234 } 00235 00236 void Test_M2MBase::test_name_id() 00237 { 00238 int id = 10; 00239 this->_sn_resource->name_id = id; 00240 CHECK(id == name_id()); 00241 } 00242 00243 void Test_M2MBase::test_handle_observation_attribute() 00244 { 00245 char *s = "wrong"; 00246 bool ret = handle_observation_attribute(s); 00247 CHECK(ret == false); 00248 delete this->_report_handler; 00249 00250 Observer obs; 00251 this->_report_handler = new M2MReportHandler(obs); 00252 00253 m2mreporthandler_stub::bool_return = true; 00254 ret = handle_observation_attribute(s); 00255 CHECK(ret == true); 00256 00257 this->_is_under_observation = true; 00258 ret = handle_observation_attribute(s); 00259 CHECK(ret == true); 00260 00261 this->_is_under_observation = true; 00262 m2mreporthandler_stub::bool_return = false; 00263 ret = handle_observation_attribute(s); 00264 CHECK(ret == false); 00265 } 00266 00267 void Test_M2MBase::test_observation_to_be_sent() 00268 { 00269 Vector<uint16_t> list; 00270 observation_to_be_sent(list); 00271 CHECK(obsHandler->visited == false); 00272 this->set_base_type(M2MBase::ObjectInstance); 00273 00274 bool test = true; 00275 set_under_observation(test,obsHandler); 00276 observation_to_be_sent(list); 00277 CHECK(obsHandler->visited == true); 00278 } 00279 00280 void Test_M2MBase::test_handle_get_request() 00281 { 00282 CHECK(this->handle_get_request(NULL,NULL,NULL) == NULL); 00283 } 00284 00285 void Test_M2MBase::test_handle_put_request() 00286 { 00287 bool execute = false; 00288 CHECK(this->handle_put_request(NULL,NULL,NULL, execute) == NULL); 00289 } 00290 00291 void Test_M2MBase::test_handle_post_request() 00292 { 00293 bool execute = false; 00294 CHECK(this->handle_post_request(NULL,NULL,NULL, execute) == NULL); 00295 } 00296 00297 void Test_M2MBase::test_memory_alloc() 00298 { 00299 CHECK(memory_alloc(0) == 0); 00300 uint8_t *ptr = 0; 00301 ptr = (uint8_t*)memory_alloc(sizeof(uint8_t)); 00302 CHECK(ptr != NULL); 00303 memory_free(ptr); 00304 } 00305 00306 void Test_M2MBase::test_memory_free() 00307 { 00308 uint8_t *ptr = (uint8_t*)memory_alloc(sizeof(uint8_t)); 00309 memory_free((void*)ptr); 00310 //memory leak test will fail, if there is a leak, so no need for CHECK 00311 } 00312 00313 void Test_M2MBase::test_report_handler() 00314 { 00315 CHECK(report_handler() == NULL); 00316 } 00317 00318 void Test_M2MBase::test_observation_handler() 00319 { 00320 CHECK(observation_handler() == NULL); 00321 } 00322 00323 void Test_M2MBase::test_id_number() 00324 { 00325 char* path = (char*)malloc(3); 00326 strcpy(path, "10"); 00327 M2MBase* b = new M2MBase("10", M2MBase::Static, "", path, false); 00328 CHECK(b->name_id() == 10); 00329 delete b; 00330 00331 char* path1 = (char*)malloc(6); 00332 strcpy(path1, "66567"); 00333 00334 M2MBase * test1 = new M2MBase("66567",M2MBase::Static, "", path1, false); 00335 CHECK(test1->name_id() == -1); 00336 delete test1; 00337 } 00338 00339 void Test_M2MBase::test_set_register_uri() 00340 { 00341 this->set_register_uri(false); 00342 CHECK(this->register_uri() == false); 00343 } 00344 00345 void Test_M2MBase::test_set_observation_number() 00346 { 00347 set_observation_number(0); 00348 CHECK(0 == this->_observation_number); 00349 } 00350 00351 void Test_M2MBase::test_set_max_age() 00352 { 00353 this->set_max_age(10000); 00354 CHECK(this->max_age() == 10000); 00355 } 00356 00357 void Test_M2MBase::test_is_under_observation() 00358 { 00359 CHECK(false == is_under_observation()); 00360 this->_is_under_observation = true; 00361 CHECK(true == is_under_observation()); 00362 } 00363 00364 void Test_M2MBase::test_value_updated_function() 00365 { 00366 MyTest test; 00367 test.visited = false; 00368 00369 CHECK(this->is_value_updated_function_set() == false); 00370 00371 this->set_value_updated_function(value_updated_callback (&test,&MyTest::value_updated_function)); 00372 this->execute_value_updated("test"); 00373 CHECK(this->is_value_updated_function_set() == true); 00374 CHECK(test.visited == true); 00375 00376 value_update_called = false; 00377 this->set_value_updated_function(value_updated_callback2(value_updated_function)); 00378 this->execute_value_updated("test"); 00379 CHECK(value_update_called == true); 00380 } 00381 00382 void Test_M2MBase::test_build_path() 00383 { 00384 StringBuffer<MAX_PATH_SIZE> buffer; 00385 CHECK(build_path(buffer, "0123456789012345678901234567890123456789012345678901234567891234", 10000, "2123456789012345678901234567890123456789012345678901234567891234", 20000)); 00386 CHECK(!build_path(buffer, "01234567890123456789012345678901234567890123456789012345678912345", 10000, "21234567890123456789012345678901234567890123456789012345678912345", 20000)); 00387 00388 StringBuffer<MAX_PATH_SIZE_2> buffer2; 00389 CHECK(build_path(buffer2, "0123456789012345678901234567890123456789012345678901234567891234", 10000, "2123456789012345678901234567890123456789012345678901234567891234")); 00390 CHECK(!build_path(buffer2, "01234567890123456789012345678901234567890123456789012345678912345", 10000, "21234567890123456789012345678901234567890123456789012345678912345")); 00391 00392 StringBuffer<MAX_PATH_SIZE_3> buffer3; 00393 CHECK(build_path(buffer3, "0123456789012345678901234567890123456789012345678901234567891234", 10000, 20000)); 00394 CHECK(!build_path(buffer3, "01234567890123456789012345678901234567890123456789012345678912345", 10000, 20000)); 00395 00396 StringBuffer<MAX_PATH_SIZE_4> buffer4; 00397 CHECK(build_path(buffer4, "0123456789012345678901234567890123456789012345678901234567891234", 10000)); 00398 CHECK(!build_path(buffer4, "01234567890123456789012345678901234567890123456789012345678912345", 10000)); 00399 } 00400 00401 void Test_M2MBase::test_set_observation_handler() 00402 { 00403 set_observation_handler(obsHandler); 00404 CHECK(observation_handler() == obsHandler); 00405 } 00406 00407 void Test_M2MBase::test_resource_type() 00408 { 00409 // Default value in ctor 00410 String test = "type"; 00411 CHECK(test == resource_type()); 00412 } 00413 00414 void Test_M2MBase::test_resource_name_length() 00415 { 00416 CHECK(4 == resource_name_length()); 00417 } 00418 00419 void Test_M2MBase::test_get_nsdl_resource() 00420 { 00421 sn_nsdl_dynamic_resource_parameters_s *res = get_nsdl_resource(); 00422 CHECK(res != NULL); 00423 CHECK(res->static_resource_parameters != NULL); 00424 } 00425 00426 void Test_M2MBase::test_create_path() 00427 { 00428 char* path1 = (char*)malloc(5); 00429 strcpy(path1, "name"); 00430 M2MObject* object = new M2MObject("name", path1); 00431 00432 char* path2 = (char*)malloc(7); 00433 strcpy(path2, "name/0"); 00434 m2mresource_stub::object_instance = 00435 new M2MObjectInstance(*object, "name","", path2); 00436 00437 String path = "name/1"; 00438 String res_path = "name/0/resource"; 00439 String res_path_instance = "name/0/resource/1"; 00440 00441 char* result = create_path(*object, "1"); 00442 CHECK(path == result); 00443 free(result); 00444 00445 result = create_path(*object, 1); 00446 CHECK(path == result); 00447 free(result); 00448 00449 result = create_path(*m2mresource_stub::object_instance, "resource"); 00450 CHECK(res_path == result); 00451 free(result); 00452 00453 char* path3 = (char*)malloc(9); 00454 strcpy(path3, "resource"); 00455 00456 M2MResource* res = new M2MResource(*m2mresource_stub::object_instance, 00457 "resource", 00458 "type", 00459 M2MResourceInstance::INTEGER, 00460 false, 00461 path3); 00462 00463 result = create_path(*res, 1); 00464 CHECK(res_path_instance == result); 00465 free(result); 00466 00467 delete m2mresource_stub::object_instance; 00468 delete object; 00469 delete res; 00470 } 00471 00472 void Test_M2MBase::test_create_report_handler() 00473 { 00474 M2MReportHandler* report_handler = create_report_handler(); 00475 CHECK(report_handler != NULL); 00476 } 00477 00478 void Test_M2MBase::test_validate_string_length() 00479 { 00480 String test = "stringlengthabc"; 00481 CHECK(validate_string_length(test, 1, 20) == true); 00482 CHECK(validate_string_length(test, 1, 2) == false); 00483 CHECK(validate_string_length(test, 15, 15) == true); 00484 CHECK(validate_string_length(test, 16, 15) == false); 00485 00486 CHECK(validate_string_length(test.c_str(), 15, 15) == true); 00487 CHECK(validate_string_length(test.c_str(), 16, 15) == false); 00488 } 00489 00490 void Test_M2MBase::test_is_integer() 00491 { 00492 CHECK(is_integer("") == false); 00493 CHECK(is_integer("-1a") == false); 00494 CHECK(is_integer("10") == true); 00495 CHECK(is_integer("+10") == true); 00496 CHECK(is_integer("-10") == true); 00497 } 00498 00499 void Test_M2MBase::test_alloc_copy() 00500 { 00501 uint8_t* test_ptr = (uint8_t *)malloc(10); 00502 memset(test_ptr,'a', 10); 00503 uint8_t* result = alloc_copy(test_ptr, 10); 00504 CHECK(memcmp(test_ptr, result, 10) == 0); 00505 free(test_ptr); 00506 free(result); 00507 } 00508 00509 void Test_M2MBase::test_alloc_string_copy() 00510 { 00511 char* test = "testi"; 00512 char* result = alloc_string_copy(test); 00513 STRCMP_EQUAL(test,result); 00514 free(result); 00515 } 00516 00517 void Test_M2MBase::test_ctor() 00518 { 00519 static sn_nsdl_static_resource_parameters_s params_static = { 00520 (char*)"", // resource_type_ptr 00521 (char*)"", // interface_description_ptr 00522 (uint8_t*)"", // path 00523 (uint8_t*)"", // resource 00524 0, // pathlen 00525 0, // resourcelen 00526 false, // external_memory_block 00527 SN_GRS_DYNAMIC, // mode 00528 false // free_on_delete 00529 }; 00530 00531 static sn_nsdl_dynamic_resource_parameters_s params_dynamic = { 00532 __nsdl_c_callback, 00533 ¶ms_static, 00534 {NULL, NULL}, // link 00535 0, // coap_content_type 00536 M2MBase::PUT_ALLOWED, // access 00537 0, // registered 00538 false, // publish_uri 00539 false, // free_on_delete 00540 true // observable 00541 }; 00542 00543 const static M2MBase::lwm2m_parameters params = { 00544 0, // max_age 00545 0, // instance_id 00546 0, // name_id 00547 (char*)"", // name 00548 ¶ms_dynamic, 00549 M2MBase::Resource, // base_type 00550 false // free_on_delete 00551 }; 00552 M2MBase* base = new M2MBase(¶ms); 00553 delete base; 00554 }
Generated on Tue Jul 12 2022 21:20:29 by
 1.7.2
 1.7.2