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_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 #include "m2mobservationhandler.h" 00019 #include "m2mreportobserver.h" 00020 #include "m2mreporthandler.h" 00021 #include "m2mreporthandler_stub.h" 00022 static bool value_update_called = false; 00023 static void value_updated_function(const char* name) { 00024 value_update_called = true; 00025 } 00026 00027 class MyTest{ 00028 public: 00029 void value_updated_function(const char* name) { 00030 visited = true; 00031 } 00032 00033 bool visited; 00034 }; 00035 00036 class Handler : public M2MObservationHandler { 00037 00038 public: 00039 00040 Handler() : visited(false) {} 00041 ~Handler(){} 00042 void observation_to_be_sent(M2MBase *, uint16_t, m2m::Vector<uint16_t>, bool){ 00043 visited = true; 00044 } 00045 void send_delayed_response(M2MBase *){} 00046 void resource_to_be_deleted(const String &){visited=true;} 00047 void remove_object(M2MBase *){visited = true;} 00048 void value_updated(M2MBase *,const String&){visited = true;} 00049 00050 void clear() {visited = false;} 00051 bool visited; 00052 }; 00053 00054 class Observer : public M2MReportObserver { 00055 public: 00056 Observer(){} 00057 ~Observer(){} 00058 00059 void observation_to_be_sent(m2m::Vector<uint16_t>,bool){} 00060 }; 00061 00062 Test_M2MBase::Test_M2MBase() 00063 :M2MBase("name",M2MBase::Static) 00064 { 00065 00066 } 00067 00068 Test_M2MBase::~Test_M2MBase() 00069 { 00070 } 00071 00072 void Test_M2MBase::test_copy_constructor() 00073 { 00074 String test_string = "test"; 00075 //Test stack constructor 00076 Test_M2MBase b; 00077 Test_M2MBase c(b); 00078 Test_M2MBase a; 00079 a = b; 00080 00081 //Test heap constructor 00082 Test_M2MBase* test = new Test_M2MBase(); 00083 test->_interface_description = test_string; 00084 00085 test->_token_length = 3; 00086 test->_token = (u_int8_t *)malloc(test->_token_length); 00087 00088 Observer obs; 00089 test->_report_handler = new M2MReportHandler(obs); 00090 00091 Test_M2MBase* copy = new Test_M2MBase(*test); 00092 00093 CHECK(copy->_interface_description.compare(0,test_string.size(),test_string) == 0); 00094 00095 CHECK(copy->_token != NULL); 00096 00097 CHECK(copy->_report_handler != NULL); 00098 00099 delete test; 00100 delete copy; 00101 } 00102 00103 void Test_M2MBase::test_assignment_operator() 00104 { 00105 //Test stack 00106 M2MBase b("name", M2MBase::Static); 00107 Test_M2MBase a; 00108 M2MBase c = a; 00109 c = b; 00110 00111 //Test heap 00112 Test_M2MBase* test = new Test_M2MBase(); 00113 Test_M2MBase* test2 = new Test_M2MBase(); 00114 Test_M2MBase* test3 = new Test_M2MBase(); 00115 00116 test->operator=(*test3); 00117 delete test3; 00118 00119 test->_token_length = 3; 00120 test->_token = (u_int8_t *)malloc(test->_token_length); 00121 00122 Observer obs; 00123 test->_report_handler = new M2MReportHandler(obs); 00124 00125 test2->_token_length = 8; 00126 test2->_token = (u_int8_t *)malloc(test2->_token_length); 00127 00128 Observer obs2; 00129 test2->_report_handler = new M2MReportHandler(obs2); 00130 00131 *test = *test2; 00132 00133 CHECK(test->_token != NULL); 00134 00135 CHECK(test->_report_handler != NULL); 00136 00137 delete test2; 00138 delete test; 00139 } 00140 00141 void Test_M2MBase::test_set_operation() 00142 { 00143 M2MBase::Operation test = M2MBase::GET_ALLOWED; 00144 set_operation(test); 00145 00146 CHECK(test == this->_operation); 00147 00148 this->_mode = M2MBase::Dynamic; 00149 test = M2MBase::PUT_ALLOWED; 00150 set_operation(test); 00151 00152 CHECK(test == this->_operation); 00153 } 00154 00155 void Test_M2MBase::test_set_base_type() 00156 { 00157 set_base_type(M2MBase::ObjectInstance); 00158 00159 CHECK(M2MBase::ObjectInstance == this->_base_type); 00160 } 00161 00162 void Test_M2MBase::test_set_interface_description() 00163 { 00164 String test = "interface_description"; 00165 set_interface_description(test); 00166 00167 CHECK(test == this->_interface_description); 00168 } 00169 00170 void Test_M2MBase::test_set_uri_path() 00171 { 00172 String test = "10/0/1"; 00173 set_uri_path(test); 00174 CHECK(test == this->_uri_path); 00175 } 00176 00177 void Test_M2MBase::test_uri_path() 00178 { 00179 String test = "interface_description"; 00180 this->_uri_path = test; 00181 00182 CHECK(test == uri_path()); 00183 } 00184 00185 void Test_M2MBase::test_set_resource_type() 00186 { 00187 String test = "resource_type"; 00188 set_resource_type(test); 00189 00190 CHECK(test == this->_resource_type); 00191 } 00192 00193 void Test_M2MBase::test_set_coap_content_type() 00194 { 00195 u_int8_t test = 1; 00196 set_coap_content_type(test); 00197 00198 CHECK(test == this->_coap_content_type); 00199 } 00200 00201 void Test_M2MBase::test_set_instance_id() 00202 { 00203 u_int16_t test = 1; 00204 set_instance_id(test); 00205 00206 CHECK(test == this->_instance_id); 00207 } 00208 00209 void Test_M2MBase::test_set_observable() 00210 { 00211 bool test = true; 00212 set_observable(test); 00213 00214 CHECK(test == this->_observable); 00215 } 00216 00217 void Test_M2MBase::test_add_observation_level() 00218 { 00219 add_observation_level(M2MBase::R_Attribute); 00220 CHECK(M2MBase::R_Attribute == this->_observation_level); 00221 00222 add_observation_level(M2MBase::O_Attribute); 00223 CHECK(M2MBase::OR_Attribute == this->_observation_level); 00224 } 00225 00226 void Test_M2MBase::test_remove_observation_level() 00227 { 00228 this->_observation_level = M2MBase::OR_Attribute; 00229 remove_observation_level(M2MBase::R_Attribute); 00230 CHECK(M2MBase::O_Attribute == this->_observation_level); 00231 00232 remove_observation_level(M2MBase::O_Attribute); 00233 CHECK(M2MBase::None == this->_observation_level); 00234 00235 this->_observation_level = M2MBase::OI_Attribute; 00236 remove_observation_level(M2MBase::R_Attribute); 00237 CHECK(M2MBase::OI_Attribute == this->_observation_level); 00238 00239 remove_observation_level(M2MBase::OI_Attribute); 00240 CHECK(M2MBase::None == this->_observation_level); 00241 remove_observation_level(M2MBase::OI_Attribute); 00242 CHECK(M2MBase::None == this->_observation_level); 00243 } 00244 00245 void Test_M2MBase::test_set_under_observation() 00246 { 00247 Handler handler; 00248 00249 this->_base_type = M2MBase::ObjectInstance; 00250 00251 bool test = true; 00252 set_under_observation(test,NULL); 00253 set_under_observation(test,&handler); 00254 00255 CHECK(&handler == this->_observation_handler); 00256 00257 set_under_observation(test,NULL); 00258 00259 test = false; 00260 set_under_observation(test,NULL); 00261 00262 test = false; 00263 set_under_observation(test,&handler); 00264 00265 set_under_observation(test,&handler); 00266 } 00267 00268 void Test_M2MBase::test_set_observation_token() 00269 { 00270 this->_token_length = 4; 00271 this->_token = (u_int8_t *)malloc(this->_token_length); 00272 String test = "token"; 00273 set_observation_token((const u_int8_t*)test.c_str(), (u_int8_t)test.size()); 00274 00275 CHECK(this->_token_length == 5); 00276 } 00277 00278 void Test_M2MBase::test_is_observable() 00279 { 00280 bool test = false; 00281 this->_observable = test; 00282 00283 CHECK(test == is_observable()); 00284 } 00285 00286 void Test_M2MBase::test_observation_level() 00287 { 00288 this->_observation_level = M2MBase::OR_Attribute; 00289 CHECK(M2MBase::OR_Attribute == this->observation_level()); 00290 } 00291 00292 void Test_M2MBase::test_get_observation_token() 00293 { 00294 u_int8_t test_value[] = {"val"}; 00295 u_int32_t value_length((u_int32_t)sizeof(test_value)); 00296 00297 u_int8_t* out_value = (u_int8_t *)malloc(value_length); 00298 u_int32_t out_size = value_length; 00299 memcpy((u_int8_t *)out_value, (u_int8_t *)test_value, value_length); 00300 00301 u_int8_t test[] = {"token"}; 00302 this->_token_length = (u_int8_t)sizeof(test); 00303 this->_token = (u_int8_t *)malloc(this->_token_length); 00304 memcpy((u_int8_t *)this->_token, (u_int8_t *)test, this->_token_length); 00305 00306 get_observation_token(out_value,out_size); 00307 00308 CHECK(out_size == 6); 00309 00310 free(out_value); 00311 } 00312 00313 void Test_M2MBase::test_mode() 00314 { 00315 CHECK(M2MBase::Static == mode()); 00316 } 00317 00318 void Test_M2MBase::test_observation_number() 00319 { 00320 u_int8_t test = 1; 00321 this->_observation_number = test; 00322 00323 CHECK(test == observation_number()); 00324 } 00325 00326 void Test_M2MBase::test_operation() 00327 { 00328 M2MBase::Operation test = M2MBase::DELETE_ALLOWED; 00329 this->_operation = test; 00330 00331 CHECK(test == operation()); 00332 } 00333 00334 void Test_M2MBase::test_name() 00335 { 00336 String test = "name"; 00337 this->_name = test; 00338 00339 CHECK(test == name()); 00340 } 00341 00342 void Test_M2MBase::test_name_id() 00343 { 00344 int id = 10; 00345 this->_name_id = id; 00346 00347 CHECK(id == name_id()); 00348 } 00349 00350 void Test_M2MBase::test_instance_id() 00351 { 00352 u_int16_t test = 1; 00353 this->_instance_id = test; 00354 00355 CHECK(test == instance_id()); 00356 } 00357 00358 void Test_M2MBase::test_interface_description() 00359 { 00360 String test = "interface_description"; 00361 this->_interface_description = test; 00362 00363 CHECK(test == interface_description()); 00364 } 00365 00366 void Test_M2MBase::test_resource_type() 00367 { 00368 String test = "resource_type"; 00369 this->_resource_type = test; 00370 00371 CHECK(test == resource_type()); 00372 } 00373 00374 void Test_M2MBase::test_coap_content_type() 00375 { 00376 u_int8_t test = 1; 00377 this->_coap_content_type = test; 00378 00379 CHECK(test == coap_content_type()); 00380 } 00381 00382 void Test_M2MBase::test_base_type() 00383 { 00384 this->_base_type = M2MBase::ObjectInstance; 00385 00386 CHECK(M2MBase::ObjectInstance == base_type()); 00387 } 00388 00389 //void Test_M2MBase::test_set_value() 00390 //{ 00391 // u_int8_t value[] = {"value2"}; 00392 // this->_value = (u_int8_t*)malloc(sizeof(u_int8_t)); 00393 00394 // CHECK(set_value(value,(u_int32_t)sizeof(value)) == true); 00395 // CHECK( this->_value_length == sizeof(value)); 00396 // CHECK( *this->_value == *value); 00397 00398 // Observer obs; 00399 // this->_report_handler = new M2MReportHandler(obs); 00400 00401 // u_int8_t value2[] = {"12"}; 00402 // CHECK(set_value(value2,(u_int32_t)sizeof(value2), true) == true); 00403 //} 00404 00405 //void Test_M2MBase::test_get_value() 00406 //{ 00407 // u_int8_t test_value[] = {"value3"}; 00408 // u_int32_t value_length((u_int32_t)sizeof(test_value)); 00409 00410 // u_int8_t* out_value = (u_int8_t *)malloc(1); 00411 // u_int32_t out_size = 1; 00412 00413 // this->_value = (u_int8_t *)malloc(value_length); 00414 // this->_value_length = value_length; 00415 // memcpy((u_int8_t *)this->_value, (u_int8_t *)test_value, value_length); 00416 00417 // get_value(out_value,out_size); 00418 00419 // CHECK(out_size == value_length); 00420 00421 // free(out_value); 00422 //} 00423 00424 void Test_M2MBase::test_handle_observation_attribute() 00425 { 00426 char query[] = "wrong"; 00427 char* s = query; 00428 bool ret = handle_observation_attribute(s); 00429 CHECK(ret == false); 00430 00431 Observer obs; 00432 this->_report_handler = new M2MReportHandler(obs); 00433 00434 m2mreporthandler_stub::bool_return = true; 00435 ret = handle_observation_attribute(s); 00436 CHECK(ret == true); 00437 00438 this->_is_under_observation = true; 00439 ret = handle_observation_attribute(s); 00440 CHECK(ret == true); 00441 00442 this->_is_under_observation = true; 00443 m2mreporthandler_stub::bool_return = false; 00444 ret = handle_observation_attribute(s); 00445 CHECK(ret == false); 00446 00447 } 00448 00449 void Test_M2MBase::test_observation_to_be_sent() 00450 { 00451 Handler handler; 00452 Vector<uint16_t> list; 00453 observation_to_be_sent(list); 00454 CHECK(handler.visited == false); 00455 00456 this->_base_type = M2MBase::ObjectInstance; 00457 00458 bool test = true; 00459 set_under_observation(test,&handler); 00460 00461 observation_to_be_sent(list); 00462 00463 CHECK(handler.visited == true); 00464 } 00465 00466 void Test_M2MBase::test_remove_resource_from_coap() 00467 { 00468 Handler handler; 00469 00470 this->_base_type = M2MBase::ObjectInstance; 00471 00472 const String s = "test"; 00473 remove_resource_from_coap(s); 00474 CHECK(handler.visited == false); 00475 00476 bool test = true; 00477 set_under_observation(test,&handler); 00478 remove_resource_from_coap(s); 00479 00480 CHECK(handler.visited == true); 00481 } 00482 00483 void Test_M2MBase::test_remove_object_from_coap() 00484 { 00485 Handler handler; 00486 00487 this->_base_type = M2MBase::ObjectInstance; 00488 00489 remove_object_from_coap(); 00490 CHECK(handler.visited == false); 00491 00492 bool test = true; 00493 set_under_observation(test,&handler); 00494 remove_object_from_coap(); 00495 00496 CHECK(handler.visited == true); 00497 } 00498 00499 void Test_M2MBase::test_handle_get_request() 00500 { 00501 CHECK(this->handle_get_request(NULL,NULL,NULL) == NULL); 00502 } 00503 00504 void Test_M2MBase::test_handle_put_request() 00505 { 00506 bool execute = false; 00507 CHECK(this->handle_put_request(NULL,NULL,NULL, execute) == NULL); 00508 } 00509 00510 void Test_M2MBase::test_handle_post_request() 00511 { 00512 bool execute = false; 00513 CHECK(this->handle_post_request(NULL,NULL,NULL, execute) == NULL); 00514 } 00515 00516 void Test_M2MBase::test_memory_alloc() 00517 { 00518 CHECK(memory_alloc(0) == 0); 00519 uint8_t *ptr = 0; 00520 ptr = (uint8_t*)memory_alloc(sizeof(uint8_t)); 00521 CHECK(ptr != NULL); 00522 memory_free(ptr); 00523 } 00524 00525 void Test_M2MBase::test_memory_free() 00526 { 00527 uint8_t *ptr = (uint8_t*)memory_alloc(sizeof(uint8_t)); 00528 memory_free((void*)ptr); 00529 //memory leak test will fail, if there is a leak, so no need for CHECK 00530 } 00531 00532 void Test_M2MBase::test_report_handler() 00533 { 00534 CHECK(report_handler() == NULL); 00535 } 00536 00537 void Test_M2MBase::test_observation_handler() 00538 { 00539 CHECK(observation_handler() == NULL); 00540 } 00541 00542 void Test_M2MBase::test_id_number() 00543 { 00544 M2MBase b("10", M2MBase::Static); 00545 CHECK(b.name_id() == 10); 00546 M2MBase * test1 = new M2MBase("66567",M2MBase::Static); 00547 CHECK(test1->name_id() == -1); 00548 delete test1; 00549 00550 } 00551 00552 void Test_M2MBase::test_set_register_uri() 00553 { 00554 this->set_register_uri(false); 00555 CHECK(this->_register_uri == false); 00556 } 00557 00558 void Test_M2MBase::test_register_uri() 00559 { 00560 this->_register_uri = false; 00561 CHECK(this->register_uri() == false); 00562 } 00563 00564 void Test_M2MBase::test_set_observation_number() 00565 { 00566 set_observation_number(0); 00567 CHECK(0 == this->_observation_number); 00568 } 00569 00570 void Test_M2MBase::test_set_max_age() 00571 { 00572 this->set_max_age(10000); 00573 CHECK(this->_max_age == 10000); 00574 } 00575 00576 void Test_M2MBase::test_max_age() 00577 { 00578 this->_max_age = 10000; 00579 CHECK(this->max_age() == 10000); 00580 } 00581 00582 void Test_M2MBase::test_is_under_observation() 00583 { 00584 CHECK(false == is_under_observation()); 00585 this->_is_under_observation = true; 00586 CHECK(true == is_under_observation()); 00587 } 00588 00589 void Test_M2MBase::test_value_updated_function() 00590 { 00591 MyTest test; 00592 test.visited = false; 00593 00594 CHECK(this->is_value_updated_function_set() == false); 00595 00596 this->set_value_updated_function(value_updated_callback (&test,&MyTest::value_updated_function)); 00597 this->execute_value_updated("test"); 00598 CHECK(this->is_value_updated_function_set() == true); 00599 CHECK(test.visited == true); 00600 00601 value_update_called = false; 00602 this->set_value_updated_function(value_updated_callback2(value_updated_function)); 00603 this->execute_value_updated("test"); 00604 CHECK(value_update_called == true); 00605 } 00606
Generated on Tue Jul 12 2022 12:28:53 by
1.7.2
