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_m2mresourceinstance.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_m2mresourceinstance.h" 00018 #include "CppUTest/MemoryLeakDetectorMallocMacros.h" 00019 #include "m2mbase_stub.h" 00020 #include "m2mobservationhandler.h" 00021 #include "m2mreporthandler.h" 00022 #include "m2mreporthandler_stub.h" 00023 #include "common_stub.h" 00024 #include "m2mtlvdeserializer_stub.h" 00025 static bool cb_visited = false; 00026 static void callback_function(void *args) 00027 { 00028 cb_visited = true; 00029 } 00030 00031 class MyTest{ 00032 public: 00033 void execute_function(void */*argument*/) { 00034 visited = true; 00035 } 00036 00037 bool visited; 00038 }; 00039 00040 class TestReportObserver : public M2MReportObserver{ 00041 public : 00042 TestReportObserver() {} 00043 ~TestReportObserver() {} 00044 void observation_to_be_sent(m2m::Vector<uint16_t>,bool){ } 00045 }; 00046 00047 class ResourceCallback : public M2MResourceCallback { 00048 00049 public: 00050 00051 ResourceCallback() : visited(false) {} 00052 ~ResourceCallback(){} 00053 void notification_update() { 00054 visited = true; 00055 } 00056 00057 void clear() {visited = false;} 00058 bool visited; 00059 }; 00060 00061 class Handler : public M2MObservationHandler { 00062 00063 public: 00064 00065 Handler(){} 00066 ~Handler(){} 00067 void observation_to_be_sent(M2MBase *, uint16_t, m2m::Vector<uint16_t>, bool){ 00068 visited = true; 00069 } 00070 void send_delayed_response(M2MBase *){} 00071 void resource_to_be_deleted(const String &){visited=true;} 00072 void remove_object(M2MBase *){visited = true;} 00073 void value_updated(M2MBase *,const String&){visited = true;} 00074 00075 void clear() {visited = false;} 00076 bool visited; 00077 }; 00078 00079 class Callback : public M2MObjectInstanceCallback { 00080 00081 public: 00082 00083 Callback(){} 00084 ~Callback(){} 00085 void notification_update(M2MBase::Observation) { 00086 visited = true; 00087 } 00088 00089 void clear() {visited = false;} 00090 bool visited; 00091 }; 00092 00093 00094 Test_M2MResourceInstance::Test_M2MResourceInstance() 00095 { 00096 callback = new Callback(); 00097 handler = new Handler(); 00098 resource_instance = new M2MResourceInstance("name", 00099 "resource_type", 00100 M2MResourceInstance::STRING, 00101 *callback); 00102 //resource_instance->_resource_callback = new ResourceCallback(); 00103 } 00104 00105 Test_M2MResourceInstance::~Test_M2MResourceInstance() 00106 { 00107 delete resource_instance; 00108 delete handler; 00109 delete callback; 00110 } 00111 00112 void Test_M2MResourceInstance::test_copy_constructor() 00113 { 00114 u_int8_t value[] = {"value"}; 00115 resource_instance->set_value(value,(u_int32_t)sizeof(value)); 00116 00117 M2MResourceInstance* copy = new M2MResourceInstance(*resource_instance); 00118 u_int8_t* out_value = (u_int8_t*)malloc(sizeof(u_int8_t)); 00119 u_int32_t out_size; 00120 00121 copy->get_value(out_value,out_size); 00122 CHECK(out_size == sizeof(value)); 00123 free(out_value); 00124 00125 delete copy; 00126 } 00127 00128 void Test_M2MResourceInstance::test_assignment_constructor() 00129 { 00130 u_int8_t value[] = {"value"}; 00131 resource_instance->set_value(value,(u_int32_t)sizeof(value)); 00132 00133 M2MResourceInstance* res = new M2MResourceInstance("name", 00134 "resource_type", 00135 M2MResourceInstance::STRING, 00136 *callback); 00137 00138 u_int8_t value1[] = {"value1"}; 00139 res->set_value(value1,(u_int32_t)sizeof(value1)); 00140 00141 *res = *resource_instance; 00142 00143 u_int8_t* out_value = (u_int8_t*)malloc(sizeof(u_int8_t)); 00144 u_int32_t out_size; 00145 00146 res->get_value(out_value,out_size); 00147 00148 CHECK(out_size == sizeof(value)); 00149 free(out_value); 00150 00151 delete res; 00152 } 00153 00154 00155 void Test_M2MResourceInstance::test_resource_instance_type() 00156 { 00157 CHECK(resource_instance->resource_instance_type() == M2MResourceInstance::STRING); 00158 } 00159 00160 void Test_M2MResourceInstance::test_static_resource_instance() 00161 { 00162 u_int8_t value[] = {"value"}; 00163 M2MResourceInstance *res = new M2MResourceInstance("name1", "type1", 00164 M2MResourceInstance::INTEGER, 00165 value, (uint32_t)sizeof(value), 00166 *callback); 00167 00168 CHECK(res != NULL); 00169 delete res; 00170 } 00171 00172 void Test_M2MResourceInstance::test_base_type() 00173 { 00174 m2mbase_stub::base_type = M2MBase::Resource; 00175 CHECK(M2MBase::Resource == resource_instance->base_type()); 00176 } 00177 00178 void Test_M2MResourceInstance::test_handle_observation_attribute() 00179 { 00180 char *d = "s"; 00181 TestReportObserver obs; 00182 m2mbase_stub::report = new M2MReportHandler(obs); 00183 00184 CHECK(false == resource_instance->handle_observation_attribute(d)); 00185 00186 resource_instance->_resource_type = M2MResourceInstance::INTEGER; 00187 CHECK(false == resource_instance->handle_observation_attribute(d)); 00188 00189 resource_instance->_resource_type = M2MResourceInstance::FLOAT; 00190 CHECK(false == resource_instance->handle_observation_attribute(d)); 00191 00192 m2mreporthandler_stub::bool_return = true; 00193 CHECK(true == resource_instance->handle_observation_attribute(d)); 00194 00195 m2mbase_stub::bool_value = false; 00196 CHECK(true == resource_instance->handle_observation_attribute(d)); 00197 00198 delete m2mbase_stub::report; 00199 m2mbase_stub::report = NULL; 00200 } 00201 00202 void Test_M2MResourceInstance::test_set_execute_function() 00203 { 00204 MyTest test; 00205 resource_instance->set_execute_function(execute_callback (&test,&MyTest::execute_function)); 00206 resource_instance->set_execute_function(callback_function); 00207 } 00208 00209 void Test_M2MResourceInstance::test_execute() 00210 { 00211 MyTest test; 00212 void *args = NULL; 00213 00214 resource_instance->set_execute_function(execute_callback (&test,&MyTest::execute_function)); 00215 resource_instance->execute(args); 00216 00217 cb_visited = false; 00218 resource_instance->set_execute_function(callback_function); 00219 resource_instance->execute(args); 00220 CHECK(true == cb_visited); 00221 00222 // Check delete 00223 cb_visited = false; 00224 resource_instance->set_execute_function(callback_function); 00225 resource_instance->execute(args); 00226 CHECK(true == cb_visited); 00227 } 00228 00229 void Test_M2MResourceInstance::test_set_value() 00230 { 00231 u_int8_t value[] = {"value2"}; 00232 resource_instance->_value = (u_int8_t*)malloc(sizeof(u_int8_t)); 00233 m2mbase_stub::bool_value = true; 00234 00235 CHECK(resource_instance->set_value(value,(u_int32_t)sizeof(value)) == true); 00236 CHECK( resource_instance->_value_length == sizeof(value)); 00237 CHECK( *resource_instance->_value == *value); 00238 00239 m2mbase_stub::observe = (M2MObservationHandler*)handler; 00240 00241 u_int8_t value2[] = {"12"}; 00242 CHECK(resource_instance->set_value(value2,(u_int32_t)sizeof(value2)) == true); 00243 00244 u_int8_t value3[] = {"13"}; 00245 CHECK(resource_instance->set_value(value3,(u_int32_t)sizeof(value3)) == true); 00246 00247 CHECK(resource_instance->set_value(123456789) == true); 00248 CHECK(memcmp(resource_instance->_value, "123456789", 9) == 0); 00249 00250 // verify int value helper 00251 CHECK(resource_instance->get_value_int() == 123456789); 00252 00253 // verify string value helper 00254 CHECK(resource_instance->get_value_string() == "123456789"); 00255 00256 free(resource_instance->_value); 00257 resource_instance->_value_length = 0; 00258 00259 CHECK(resource_instance->set_value(NULL,0) == false); 00260 00261 CHECK(resource_instance->set_value(NULL,0) == false); 00262 00263 m2mbase_stub::observation_level_value = M2MBase::R_Attribute; 00264 resource_instance->_value = (u_int8_t*)malloc(sizeof(value)+1); 00265 memset(resource_instance->_value,0,sizeof(value)+1); 00266 memcpy(resource_instance->_value,value,sizeof(value)); 00267 resource_instance->_value_length = sizeof(value); 00268 TestReportObserver obs; 00269 m2mbase_stub::report = new M2MReportHandler(obs); 00270 00271 u_int8_t value4[] = {"value4"}; 00272 CHECK(resource_instance->set_value(value4,(u_int32_t)sizeof(value4)) == true); 00273 00274 00275 m2mbase_stub::base_type = M2MBase::ResourceInstance; 00276 m2mbase_stub::observation_level_value = M2MBase::O_Attribute; 00277 resource_instance->_resource_type = M2MResourceInstance::INTEGER; 00278 m2mbase_stub::mode_value = M2MBase::Dynamic; 00279 ResourceCallback *resource_cb = new ResourceCallback(); 00280 resource_instance->set_resource_observer(resource_cb); 00281 CHECK(resource_instance->set_value(value2,(u_int32_t)sizeof(value2)) == true); 00282 00283 // XXX: the callback will not be called on current code with combination of 00284 // M2MBase::Dynamic and M2MBase::R_Attribute. 00285 CHECK(resource_cb->visited == false); 00286 00287 resource_cb->visited = false; 00288 m2mbase_stub::observation_level_value = M2MBase::R_Attribute; 00289 CHECK(resource_instance->set_value(value3,(u_int32_t)sizeof(value3)) == true); 00290 CHECK(resource_cb->visited == true); 00291 00292 resource_instance->set_resource_observer(NULL); 00293 resource_cb->visited = false; 00294 m2mbase_stub::observation_level_value = M2MBase::R_Attribute; 00295 CHECK(resource_instance->set_value(value2,(u_int32_t)sizeof(value2)) == true); 00296 CHECK(resource_cb->visited == false); 00297 00298 00299 CHECK(resource_instance->set_value(value3,(u_int32_t)sizeof(value3)) == true); 00300 00301 m2mbase_stub::observation_level_value = M2MBase::OI_Attribute; 00302 00303 resource_instance->_resource_type = M2MResourceInstance::INTEGER; 00304 00305 m2mbase_stub::mode_value = M2MBase::Dynamic; 00306 00307 CHECK(resource_instance->set_value(value2,(u_int32_t)sizeof(value2)) == true); 00308 00309 m2mbase_stub::observation_level_value = M2MBase::OOI_Attribute; 00310 00311 resource_instance->_resource_type = M2MResourceInstance::INTEGER; 00312 00313 m2mbase_stub::mode_value = M2MBase::Dynamic; 00314 00315 CHECK(resource_instance->set_value(value2,(u_int32_t)sizeof(value2)) == true); 00316 00317 delete m2mbase_stub::report; 00318 m2mbase_stub::report = NULL; 00319 delete resource_cb; 00320 } 00321 00322 void Test_M2MResourceInstance::test_clear_value() 00323 { 00324 u_int8_t value[] = {"value"}; 00325 resource_instance->_value = (u_int8_t*)malloc(sizeof(u_int8_t)); 00326 00327 m2mbase_stub::observe = handler; 00328 TestReportObserver obs; 00329 m2mbase_stub::report = new M2MReportHandler(obs); 00330 00331 CHECK(resource_instance->set_value(value,(u_int32_t)sizeof(value)) == true); 00332 CHECK( resource_instance->_value_length == sizeof(value)); 00333 CHECK( *resource_instance->_value == *value); 00334 resource_instance->clear_value(); 00335 00336 CHECK( resource_instance->_value_length == 0); 00337 CHECK( resource_instance->_value == NULL); 00338 00339 m2mbase_stub::bool_value = true; 00340 m2mbase_stub::mode_value = M2MBase::Dynamic; 00341 m2mbase_stub::observation_level_value = M2MBase::R_Attribute; 00342 resource_instance->_resource_type = M2MResourceInstance::INTEGER; 00343 resource_instance->clear_value(); 00344 00345 CHECK( resource_instance->_value_length == 0); 00346 CHECK( resource_instance->_value == NULL); 00347 00348 delete m2mbase_stub::report; 00349 m2mbase_stub::report = NULL; 00350 } 00351 00352 void Test_M2MResourceInstance::test_get_value() 00353 { 00354 u_int8_t test_value[] = {"value3"}; 00355 u_int32_t value_length((u_int32_t)sizeof(test_value)); 00356 00357 resource_instance->_value = (u_int8_t *)malloc(value_length); 00358 resource_instance->_value_length = value_length; 00359 memcpy((u_int8_t *)resource_instance->_value, (u_int8_t *)test_value, value_length); 00360 00361 resource_instance->clear_value(); 00362 00363 CHECK(resource_instance->_value == NULL); 00364 00365 } 00366 00367 void Test_M2MResourceInstance::test_value() 00368 { 00369 CHECK(resource_instance->value() == NULL); 00370 } 00371 00372 void Test_M2MResourceInstance::test_value_length() 00373 { 00374 CHECK(resource_instance->value_length() == 0); 00375 } 00376 00377 void Test_M2MResourceInstance::test_handle_get_request() 00378 { 00379 uint8_t value[] = {"name"}; 00380 sn_coap_hdr_s *coap_header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00381 memset(coap_header, 0, sizeof(sn_coap_hdr_s)); 00382 00383 coap_header->uri_path_ptr = value; 00384 coap_header->uri_path_len = sizeof(value); 00385 00386 coap_header->msg_code = COAP_MSG_CODE_REQUEST_GET; 00387 00388 String *name = new String("name"); 00389 common_stub::int_value = 0; 00390 m2mbase_stub::string_value = name; 00391 00392 m2mbase_stub::operation = M2MBase::GET_ALLOWED; 00393 m2mbase_stub::uint8_value = 200; 00394 common_stub::coap_header = (sn_coap_hdr_ *)malloc(sizeof(sn_coap_hdr_)); 00395 memset(common_stub::coap_header,0,sizeof(sn_coap_hdr_)); 00396 00397 coap_header->token_ptr = (uint8_t*)malloc(sizeof(value)); 00398 memcpy(coap_header->token_ptr, value, sizeof(value)); 00399 00400 coap_header->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); 00401 coap_header->options_list_ptr->observe = 0; 00402 00403 coap_header->content_type_ptr = (uint8_t*)malloc(1); 00404 coap_header->content_type_len = 1; 00405 *coap_header->content_type_ptr = 110; 00406 00407 CHECK(resource_instance->handle_get_request(NULL,coap_header,handler) != NULL); 00408 00409 if(coap_header->content_type_ptr) { 00410 free(coap_header->content_type_ptr); 00411 coap_header->content_type_ptr = NULL; 00412 } 00413 00414 if(common_stub::coap_header->content_type_ptr) { 00415 free(common_stub::coap_header->content_type_ptr); 00416 common_stub::coap_header->content_type_ptr = NULL; 00417 } 00418 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00419 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00420 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00421 } 00422 if(common_stub::coap_header->options_list_ptr) { 00423 free(common_stub::coap_header->options_list_ptr); 00424 common_stub::coap_header->options_list_ptr = NULL; 00425 } 00426 00427 // OMA OPAQUE 00428 resource_instance->_resource_type = M2MResourceInstance::OPAQUE; 00429 00430 CHECK(resource_instance->handle_get_request(NULL,coap_header,handler) != NULL); 00431 00432 if(common_stub::coap_header->content_type_ptr) { 00433 free(common_stub::coap_header->content_type_ptr); 00434 common_stub::coap_header->content_type_ptr = NULL; 00435 } 00436 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00437 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00438 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00439 } 00440 if(common_stub::coap_header->options_list_ptr) { 00441 free(common_stub::coap_header->options_list_ptr); 00442 common_stub::coap_header->options_list_ptr = NULL; 00443 } 00444 00445 // Not OMA TLV or JSON 00446 m2mbase_stub::uint8_value = 110; 00447 CHECK(resource_instance->handle_get_request(NULL,coap_header,handler) != NULL); 00448 00449 if(common_stub::coap_header->content_type_ptr) { 00450 free(common_stub::coap_header->content_type_ptr); 00451 common_stub::coap_header->content_type_ptr = NULL; 00452 } 00453 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00454 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00455 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00456 } 00457 if(common_stub::coap_header->options_list_ptr) { 00458 free(common_stub::coap_header->options_list_ptr); 00459 common_stub::coap_header->options_list_ptr = NULL; 00460 } 00461 00462 // OMA TLV 00463 m2mbase_stub::uint8_value = 99; 00464 CHECK(resource_instance->handle_get_request(NULL,coap_header,handler) != NULL); 00465 00466 if(common_stub::coap_header->content_type_ptr) { 00467 free(common_stub::coap_header->content_type_ptr); 00468 common_stub::coap_header->content_type_ptr = NULL; 00469 } 00470 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00471 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00472 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00473 } 00474 if(common_stub::coap_header->options_list_ptr) { 00475 free(common_stub::coap_header->options_list_ptr); 00476 common_stub::coap_header->options_list_ptr = NULL; 00477 } 00478 00479 // OMA JSON 00480 m2mbase_stub::uint8_value = 100; 00481 CHECK(resource_instance->handle_get_request(NULL,coap_header,handler) != NULL); 00482 00483 if(common_stub::coap_header->content_type_ptr) { 00484 free(common_stub::coap_header->content_type_ptr); 00485 common_stub::coap_header->content_type_ptr = NULL; 00486 } 00487 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00488 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00489 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00490 } 00491 if(common_stub::coap_header->options_list_ptr) { 00492 free(common_stub::coap_header->options_list_ptr); 00493 common_stub::coap_header->options_list_ptr = NULL; 00494 } 00495 00496 coap_header->options_list_ptr->observe = 1; 00497 00498 uint8_t obs = 0; 00499 coap_header->options_list_ptr->observe_ptr = (uint8_t*)malloc(sizeof(obs)); 00500 memcpy(coap_header->options_list_ptr->observe_ptr,&obs,sizeof(obs)); 00501 coap_header->options_list_ptr->observe_len = 0; 00502 m2mbase_stub::uint16_value = 0x1c1c; 00503 m2mbase_stub::bool_value = true; 00504 00505 CHECK(resource_instance->handle_get_request(NULL,coap_header,handler) != NULL); 00506 00507 if(common_stub::coap_header->content_type_ptr) { 00508 free(common_stub::coap_header->content_type_ptr); 00509 common_stub::coap_header->content_type_ptr = NULL; 00510 } 00511 if(common_stub::coap_header->options_list_ptr->observe_ptr) { 00512 free(common_stub::coap_header->options_list_ptr->observe_ptr); 00513 common_stub::coap_header->options_list_ptr->observe_ptr = NULL; 00514 } 00515 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00516 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00517 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00518 } 00519 if(common_stub::coap_header->options_list_ptr) { 00520 free(common_stub::coap_header->options_list_ptr); 00521 common_stub::coap_header->options_list_ptr = NULL; 00522 } 00523 00524 // Not observable 00525 m2mbase_stub::bool_value = false; 00526 CHECK(resource_instance->handle_get_request(NULL,coap_header,handler) != NULL); 00527 00528 if(common_stub::coap_header->content_type_ptr) { 00529 free(common_stub::coap_header->content_type_ptr); 00530 common_stub::coap_header->content_type_ptr = NULL; 00531 } 00532 if(common_stub::coap_header->options_list_ptr->observe_ptr) { 00533 free(common_stub::coap_header->options_list_ptr->observe_ptr); 00534 common_stub::coap_header->options_list_ptr->observe_ptr = NULL; 00535 } 00536 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00537 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00538 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00539 } 00540 if(common_stub::coap_header->options_list_ptr) { 00541 free(common_stub::coap_header->options_list_ptr); 00542 common_stub::coap_header->options_list_ptr = NULL; 00543 } 00544 00545 m2mbase_stub::bool_value = true; 00546 00547 coap_header->options_list_ptr->observe_len = 1; 00548 00549 CHECK(resource_instance->handle_get_request(NULL,coap_header,handler) != NULL); 00550 00551 if(common_stub::coap_header->content_type_ptr) { 00552 free(common_stub::coap_header->content_type_ptr); 00553 common_stub::coap_header->content_type_ptr = NULL; 00554 } 00555 00556 if(common_stub::coap_header->options_list_ptr->observe_ptr) { 00557 free(common_stub::coap_header->options_list_ptr->observe_ptr); 00558 common_stub::coap_header->options_list_ptr->observe_ptr = NULL; 00559 } 00560 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00561 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00562 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00563 } 00564 if(common_stub::coap_header->options_list_ptr) { 00565 free(common_stub::coap_header->options_list_ptr); 00566 common_stub::coap_header->options_list_ptr = NULL; 00567 } 00568 00569 00570 obs = 1; 00571 memcpy(coap_header->options_list_ptr->observe_ptr,&obs,sizeof(obs)); 00572 CHECK(resource_instance->handle_get_request(NULL,coap_header,handler) != NULL); 00573 00574 if(common_stub::coap_header->content_type_ptr) { 00575 free(common_stub::coap_header->content_type_ptr); 00576 common_stub::coap_header->content_type_ptr = NULL; 00577 } 00578 00579 if(common_stub::coap_header->options_list_ptr->observe_ptr) { 00580 free(common_stub::coap_header->options_list_ptr->observe_ptr); 00581 common_stub::coap_header->options_list_ptr->observe_ptr = NULL; 00582 } 00583 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00584 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00585 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00586 } 00587 if(common_stub::coap_header->options_list_ptr) { 00588 free(common_stub::coap_header->options_list_ptr); 00589 common_stub::coap_header->options_list_ptr = NULL; 00590 } 00591 00592 m2mbase_stub::operation = M2MBase::NOT_ALLOWED; 00593 CHECK(resource_instance->handle_get_request(NULL,coap_header,handler) != NULL); 00594 00595 CHECK(resource_instance->handle_get_request(NULL,NULL,handler) != NULL); 00596 00597 if(coap_header->token_ptr) { 00598 free(coap_header->token_ptr); 00599 coap_header->token_ptr = NULL; 00600 } 00601 if(coap_header->content_type_ptr) { 00602 free(coap_header->content_type_ptr); 00603 coap_header->content_type_ptr = NULL; 00604 } 00605 if(coap_header->options_list_ptr->observe_ptr) { 00606 free(coap_header->options_list_ptr->observe_ptr); 00607 coap_header->options_list_ptr->observe_ptr = NULL; 00608 } 00609 if(coap_header->options_list_ptr) { 00610 free(coap_header->options_list_ptr); 00611 coap_header->options_list_ptr = NULL; 00612 } 00613 00614 if(common_stub::coap_header){ 00615 if(common_stub::coap_header->content_type_ptr) { 00616 free(common_stub::coap_header->content_type_ptr); 00617 common_stub::coap_header->content_type_ptr = NULL; 00618 } 00619 if(common_stub::coap_header->options_list_ptr) { 00620 free(common_stub::coap_header->options_list_ptr); 00621 common_stub::coap_header->options_list_ptr = NULL; 00622 } 00623 free(common_stub::coap_header); 00624 common_stub::coap_header = NULL; 00625 } 00626 free(coap_header); 00627 coap_header = NULL; 00628 00629 delete name; 00630 name = NULL; 00631 00632 m2mbase_stub::clear(); 00633 common_stub::clear(); 00634 } 00635 00636 void Test_M2MResourceInstance::test_handle_put_request() 00637 { 00638 uint8_t value[] = {"name"}; 00639 bool execute_value_updated = false; 00640 sn_coap_hdr_s *coap_header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00641 memset(coap_header, 0, sizeof(sn_coap_hdr_s)); 00642 00643 coap_header->uri_path_ptr = value; 00644 coap_header->uri_path_len = sizeof(value); 00645 00646 coap_header->msg_code = COAP_MSG_CODE_REQUEST_PUT; 00647 00648 String *name = new String("name"); 00649 common_stub::int_value = 0; 00650 m2mbase_stub::string_value = name; 00651 00652 m2mbase_stub::operation = M2MBase::PUT_ALLOWED; 00653 m2mbase_stub::uint8_value = 200; 00654 00655 common_stub::coap_header = (sn_coap_hdr_ *)malloc(sizeof(sn_coap_hdr_)); 00656 memset(common_stub::coap_header,0,sizeof(sn_coap_hdr_)); 00657 00658 coap_header->payload_ptr = (uint8_t*)malloc(1); 00659 00660 coap_header->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); 00661 coap_header->options_list_ptr->uri_query_ptr = value; 00662 coap_header->options_list_ptr->uri_query_len = sizeof(value); 00663 00664 coap_header->content_type_ptr = (uint8_t*)malloc(1); 00665 coap_header->content_type_len = 1; 00666 *coap_header->content_type_ptr = 99; 00667 m2mtlvdeserializer_stub::bool_value = true; 00668 00669 m2mbase_stub::bool_value = false; 00670 00671 sn_coap_hdr_s *coap_response = NULL; 00672 coap_response = resource_instance->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00673 CHECK( coap_response != NULL); 00674 if(coap_response) { 00675 if(coap_response->content_type_ptr) { 00676 free(coap_response->content_type_ptr); 00677 coap_response->content_type_ptr = NULL; 00678 } 00679 } 00680 00681 free(coap_header->options_list_ptr); 00682 coap_header->options_list_ptr = NULL; 00683 00684 coap_response = resource_instance->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00685 CHECK( coap_response != NULL); 00686 if(coap_response) { 00687 if(coap_response->content_type_ptr) { 00688 free(coap_response->content_type_ptr); 00689 coap_response->content_type_ptr = NULL; 00690 } 00691 } 00692 00693 m2mtlvdeserializer_stub::bool_value = false; 00694 00695 coap_response = resource_instance->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00696 00697 CHECK( coap_response != NULL); 00698 if(coap_response) { 00699 if(coap_response->content_type_ptr) { 00700 free(coap_response->content_type_ptr); 00701 coap_response->content_type_ptr = NULL; 00702 } 00703 } 00704 00705 *coap_header->content_type_ptr = 100; 00706 00707 coap_response = resource_instance->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00708 00709 CHECK( coap_response != NULL); 00710 if(coap_response) { 00711 if(coap_response->content_type_ptr) { 00712 free(coap_response->content_type_ptr); 00713 coap_response->content_type_ptr = NULL; 00714 } 00715 } 00716 00717 m2mbase_stub::bool_value = true; 00718 00719 coap_response = resource_instance->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00720 00721 CHECK( coap_response != NULL); 00722 if(coap_response) { 00723 if(coap_response->content_type_ptr) { 00724 free(coap_response->content_type_ptr); 00725 coap_response->content_type_ptr = NULL; 00726 } 00727 } 00728 00729 m2mbase_stub::operation = M2MBase::NOT_ALLOWED; 00730 00731 coap_response = resource_instance->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00732 00733 CHECK( coap_response != NULL); 00734 if(coap_response) { 00735 if(coap_response->content_type_ptr) { 00736 free(coap_response->content_type_ptr); 00737 coap_response->content_type_ptr = NULL; 00738 } 00739 } 00740 00741 coap_response = resource_instance->handle_put_request(NULL,NULL,handler,execute_value_updated); 00742 00743 CHECK( coap_response != NULL); 00744 if(coap_response) { 00745 if(coap_response->content_type_ptr) { 00746 free(coap_response->content_type_ptr); 00747 coap_response->content_type_ptr = NULL; 00748 } 00749 } 00750 00751 free(coap_header->content_type_ptr); 00752 free(coap_header->options_list_ptr); 00753 free(coap_header->payload_ptr); 00754 free(common_stub::coap_header); 00755 delete name; 00756 free(coap_header); 00757 00758 m2mtlvdeserializer_stub::clear(); 00759 common_stub::clear(); 00760 m2mbase_stub::clear(); 00761 } 00762 00763 void Test_M2MResourceInstance::test_set_resource_observer() 00764 { 00765 ResourceCallback *resource_cb = new ResourceCallback(); 00766 resource_instance->set_resource_observer(resource_cb); 00767 CHECK(resource_instance->_resource_callback == resource_cb) 00768 delete resource_cb; 00769 } 00770 00771 00772 void Test_M2MResourceInstance::test_get_object_name() 00773 { 00774 resource_instance->_object_name = "object"; 00775 CHECK(resource_instance->object_name() == "object"); 00776 } 00777 00778 void Test_M2MResourceInstance::test_get_object_instance_id() 00779 { 00780 resource_instance->_object_instance_id = 100; 00781 CHECK(resource_instance->object_instance_id() == 100); 00782 }
Generated on Tue Jul 12 2022 12:28:54 by
