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_m2mobjectinstance.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_m2mobjectinstance.h" 00018 #include "m2mresource_stub.h" 00019 #include "common_stub.h" 00020 #include "m2mresourceinstance_stub.h" 00021 #include "m2mbase_stub.h" 00022 #include "m2mtlvdeserializer_stub.h" 00023 #include "m2mtlvserializer_stub.h" 00024 #include "m2mreporthandler_stub.h" 00025 00026 // Length is 65 00027 String max_length = "65656565656565656565656565656565656565656565656565656565656565656"; 00028 00029 class Handler : public M2MObservationHandler { 00030 00031 public: 00032 00033 Handler(){} 00034 ~Handler(){} 00035 void observation_to_be_sent(M2MBase *, uint16_t, m2m::Vector<uint16_t>,bool){ 00036 visited = true; 00037 } 00038 void send_delayed_response(M2MBase *){} 00039 void resource_to_be_deleted(const String &){visited=true;} 00040 void remove_object(M2MBase *){visited = true;} 00041 void value_updated(M2MBase *,const String&){visited = true;} 00042 00043 void clear() {visited = false;} 00044 bool visited; 00045 }; 00046 00047 class TestReportObserver : public M2MReportObserver{ 00048 public : 00049 TestReportObserver() {} 00050 ~TestReportObserver() {} 00051 void observation_to_be_sent(m2m::Vector<uint16_t>,bool){ } 00052 }; 00053 00054 class Callback : public M2MObjectCallback { 00055 00056 public: 00057 00058 Callback() : visited(false){} 00059 ~Callback(){} 00060 void notification_update(uint16_t obj_instance_id) { 00061 visited = true; 00062 } 00063 00064 void clear() {visited = false;} 00065 bool visited; 00066 }; 00067 00068 Test_M2MObjectInstance::Test_M2MObjectInstance() 00069 { 00070 handler = new Handler(); 00071 callback = new Callback(); 00072 object = new M2MObjectInstance("name",*callback); 00073 } 00074 00075 void Test_M2MObjectInstance::test_copy_constructor() 00076 { 00077 M2MResource *res = new M2MResource(*object,"name","type",M2MResourceInstance::STRING,M2MBase::Static); 00078 m2mbase_stub::string_value = new String("name"); 00079 object->_resource_list.push_back(res); 00080 00081 M2MObjectInstance* copy = new M2MObjectInstance(*object); 00082 00083 CHECK(1 == copy->_resource_list.size()); 00084 delete copy; 00085 } 00086 00087 00088 Test_M2MObjectInstance::~Test_M2MObjectInstance() 00089 { 00090 delete object; 00091 delete callback; 00092 delete handler; 00093 delete m2mbase_stub::string_value; 00094 m2mresource_stub::clear(); 00095 m2mbase_stub::clear(); 00096 m2mtlvdeserializer_stub::clear(); 00097 m2mtlvserializer_stub::clear(); 00098 00099 } 00100 00101 void Test_M2MObjectInstance::test_create_static_resource() 00102 { 00103 m2mbase_stub::string_value = new String("name"); 00104 u_int8_t value[] = {"value"}; 00105 00106 m2mbase_stub::bool_value = true; 00107 00108 m2mbase_stub::uint8_value = 99; 00109 M2MResource * res = object->create_static_resource("name","type",M2MResourceInstance::STRING,value,(u_int32_t)sizeof(value),false); 00110 00111 CHECK(res != NULL); 00112 res = object->create_static_resource("name2","type",M2MResourceInstance::STRING,value,(u_int32_t)sizeof(value),true); 00113 00114 CHECK(res != NULL); 00115 CHECK(2 == object->_resource_list.size()); 00116 00117 res = object->create_static_resource("","type",M2MResourceInstance::STRING,value,(u_int32_t)sizeof(value)); 00118 CHECK(res == NULL); 00119 00120 res = object->create_static_resource(max_length,"type",M2MResourceInstance::STRING,value,(u_int32_t)sizeof(value)); 00121 CHECK(res == NULL); 00122 } 00123 00124 void Test_M2MObjectInstance::test_create_static_resource_instance() 00125 { 00126 m2mbase_stub::string_value = new String("name"); 00127 u_int8_t value[] = {"value"}; 00128 00129 m2mbase_stub::bool_value = true; 00130 m2mresource_stub::bool_value = true; 00131 m2mbase_stub::uint8_value = 99; 00132 M2MResourceInstance *ins = object->create_static_resource_instance("name","type", 00133 M2MResourceInstance::STRING, 00134 value,(u_int32_t)sizeof(value), 00135 0); 00136 00137 CHECK(ins != NULL); 00138 CHECK(1 == object->_resource_list.size()); 00139 00140 delete ins; 00141 00142 ins = object->create_static_resource_instance("name","type", 00143 M2MResourceInstance::STRING, 00144 value,(u_int32_t)sizeof(value), 00145 1); 00146 00147 CHECK(ins != NULL); 00148 CHECK(1 == object->_resource_list.size()); 00149 00150 delete ins; 00151 00152 ins = object->create_static_resource_instance("","type", 00153 M2MResourceInstance::STRING,value, 00154 (u_int32_t)sizeof(value),0); 00155 CHECK(ins == NULL); 00156 00157 delete ins; 00158 00159 ins = object->create_static_resource_instance(max_length,"type", 00160 M2MResourceInstance::STRING,value, 00161 (u_int32_t)sizeof(value),0); 00162 CHECK(ins == NULL); 00163 } 00164 00165 void Test_M2MObjectInstance::test_create_dynamic_resource_instance() 00166 { 00167 m2mbase_stub::string_value = new String("name"); 00168 00169 m2mresource_stub::bool_value = true; 00170 m2mbase_stub::uint8_value = 99; 00171 00172 M2MResourceInstance * ins = object->create_dynamic_resource_instance("name","type", 00173 M2MResourceInstance::STRING, 00174 false,0); 00175 CHECK(ins != NULL); 00176 CHECK(1 == object->_resource_list.size()); 00177 00178 delete ins; 00179 00180 ins = object->create_dynamic_resource_instance("name","type", 00181 M2MResourceInstance::STRING, 00182 false,1); 00183 CHECK(ins != NULL); 00184 CHECK(1 == object->_resource_list.size()); 00185 00186 delete ins; 00187 00188 ins = object->create_dynamic_resource_instance("","type", 00189 M2MResourceInstance::STRING, 00190 false,1); 00191 00192 CHECK(ins == NULL); 00193 00194 ins = object->create_dynamic_resource_instance(max_length,"type", 00195 M2MResourceInstance::STRING, 00196 false,1); 00197 CHECK(ins == NULL); 00198 } 00199 00200 void Test_M2MObjectInstance::test_create_dynamic_resource() 00201 { 00202 m2mbase_stub::string_value = new String("name"); 00203 m2mbase_stub::uint8_value = 99; 00204 00205 M2MResource * res = object->create_dynamic_resource("name","type",M2MResourceInstance::STRING,false,false); 00206 CHECK(res != NULL); 00207 CHECK(1 == object->_resource_list.size()); 00208 00209 res = object->create_dynamic_resource("multiple","type",M2MResourceInstance::STRING,false,true); 00210 CHECK(res != NULL); 00211 CHECK(2 == object->_resource_list.size()); 00212 00213 M2MResource * res1 = object->create_dynamic_resource("name1","type",M2MResourceInstance::STRING,false,false); 00214 CHECK(res1 != NULL); 00215 CHECK(3 == object->_resource_list.size()); 00216 00217 M2MResource * res2 = object->create_dynamic_resource("","type",M2MResourceInstance::STRING,false,false); 00218 CHECK(res2 == NULL); 00219 00220 M2MResource * res3 = object->create_dynamic_resource(max_length,"type",M2MResourceInstance::STRING,false,false); 00221 CHECK(res3 == NULL); 00222 } 00223 00224 void Test_M2MObjectInstance::test_remove_resource() 00225 { 00226 CHECK(false == object->remove_resource("name")); 00227 00228 M2MResource *res = new M2MResource(*object,"name","type",M2MResourceInstance::STRING,M2MBase::Static,true); 00229 object->_resource_list.push_back(res); 00230 00231 m2mbase_stub::string_value = new String("name"); 00232 m2mbase_stub::int_value = 0; 00233 m2mbase_stub::void_value = malloc(20); 00234 00235 m2mresource_stub::bool_value = true; 00236 CHECK(true == object->remove_resource("name")); 00237 CHECK(0 == object->_resource_list.size()); 00238 00239 free(m2mbase_stub::void_value); 00240 } 00241 00242 void Test_M2MObjectInstance::test_remove_resource_instance() 00243 { 00244 CHECK(false == object->remove_resource_instance("name",0)); 00245 00246 M2MResource *res = new M2MResource(*object,"name","type",M2MResourceInstance::STRING,M2MBase::Static,true); 00247 object->_resource_list.push_back(res); 00248 00249 m2mbase_stub::string_value = new String("name"); 00250 m2mbase_stub::int_value = 0; 00251 m2mbase_stub::void_value = malloc(20); 00252 00253 m2mresource_stub::bool_value = true; 00254 00255 M2MResourceInstance *ins = new M2MResourceInstance("name","type",M2MResourceInstance::STRING,*object); 00256 00257 m2mresource_stub::list.push_back(ins); 00258 00259 CHECK(true == object->remove_resource_instance("name",0)); 00260 CHECK(0 == object->_resource_list.size()); 00261 00262 free(m2mbase_stub::void_value); 00263 00264 delete ins; 00265 ins = NULL; 00266 } 00267 00268 00269 void Test_M2MObjectInstance::test_resource() 00270 { 00271 M2MResource *res = new M2MResource(*object,"name","type",M2MResourceInstance::STRING,M2MBase::Static,true); 00272 object->_resource_list.push_back(res); 00273 00274 m2mbase_stub::string_value = new String("name"); 00275 m2mbase_stub::int_value = 0; 00276 00277 M2MResource *result = object->resource("name"); 00278 CHECK(result != NULL); 00279 00280 res = new M2MResource(*object,"name","type",M2MResourceInstance::STRING,M2MBase::Static,true); 00281 object->_resource_list.push_back(res); 00282 00283 m2mbase_stub::int_value = 1; 00284 00285 result = object->resource("name"); 00286 CHECK(result != NULL); 00287 } 00288 00289 void Test_M2MObjectInstance::test_resources() 00290 { 00291 M2MResource *res = new M2MResource(*object,"name","type",M2MResourceInstance::STRING,M2MBase::Static,true); 00292 m2mbase_stub::string_value = new String("name"); 00293 object->_resource_list.push_back(res); 00294 00295 res = new M2MResource(*object,"name","type",M2MResourceInstance::STRING,M2MBase::Static,true); 00296 object->_resource_list.push_back(res); 00297 00298 M2MResourceList resources =object->resources(); 00299 00300 CHECK(2 == resources.size()); 00301 } 00302 00303 void Test_M2MObjectInstance::test_resource_count() 00304 { 00305 M2MResource *res = new M2MResource(*object,"name","type",M2MResourceInstance::STRING,M2MBase::Static,true); 00306 m2mbase_stub::string_value = new String("name"); 00307 object->_resource_list.push_back(res); 00308 00309 res = new M2MResource(*object,"name","type",M2MResourceInstance::STRING,M2MBase::Static,true); 00310 object->_resource_list.push_back(res); 00311 00312 00313 m2mbase_stub::int_value = 0; 00314 m2mresource_stub::bool_value = true; 00315 m2mresource_stub::int_value = 1; 00316 00317 CHECK(2 == object->resource_count("name")); 00318 00319 m2mresource_stub::bool_value = false; 00320 00321 CHECK(2 == object->resource_count("name")); 00322 } 00323 00324 void Test_M2MObjectInstance::test_total_resource_count() 00325 { 00326 M2MResource *res = new M2MResource(*object,"name","type",M2MResourceInstance::STRING,M2MBase::Static,true); 00327 m2mbase_stub::string_value = new String("name"); 00328 object->_resource_list.push_back(res); 00329 00330 res = new M2MResource(*object,"name1","type1",M2MResourceInstance::STRING,M2MBase::Static,true); 00331 object->_resource_list.push_back(res); 00332 00333 m2mresource_stub::bool_value = true; 00334 m2mresource_stub::int_value = 1; 00335 00336 CHECK(2 == object->resource_count()); 00337 00338 m2mresource_stub::bool_value = false; 00339 00340 CHECK(2 == object->resource_count()); 00341 } 00342 00343 void Test_M2MObjectInstance::test_base_type() 00344 { 00345 m2mbase_stub::base_type = M2MBase::ObjectInstance; 00346 CHECK(M2MBase::ObjectInstance == object->base_type()); 00347 } 00348 00349 void Test_M2MObjectInstance::test_handle_get_request() 00350 { 00351 M2MResource *res = new M2MResource(*object,"name1","type1",M2MResourceInstance::STRING,M2MBase::Static,true); 00352 object->_resource_list.push_back(res); 00353 m2mbase_stub::string_value = new String("name1"); 00354 uint8_t value[] = {"name"}; 00355 sn_coap_hdr_s *coap_header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00356 memset(coap_header, 0, sizeof(sn_coap_hdr_s)); 00357 00358 coap_header->uri_path_ptr = value; 00359 coap_header->uri_path_len = sizeof(value); 00360 00361 coap_header->msg_code = COAP_MSG_CODE_REQUEST_GET; 00362 00363 common_stub::int_value = 0; 00364 00365 m2mbase_stub::operation = M2MBase::GET_ALLOWED; 00366 m2mbase_stub::uint8_value = 200; 00367 00368 common_stub::coap_header = (sn_coap_hdr_ *)malloc(sizeof(sn_coap_hdr_)); 00369 memset(common_stub::coap_header,0,sizeof(sn_coap_hdr_)); 00370 00371 m2mtlvserializer_stub::uint8_value = (uint8_t*)malloc(1); 00372 00373 coap_header->token_ptr = (uint8_t*)malloc(sizeof(value)); 00374 memcpy(coap_header->token_ptr, value, sizeof(value)); 00375 00376 coap_header->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); 00377 coap_header->options_list_ptr->observe = 0; 00378 00379 coap_header->content_type_ptr = (uint8_t*)malloc(1); 00380 coap_header->content_type_len = 1; 00381 *coap_header->content_type_ptr = 110; 00382 00383 CHECK(object->handle_get_request(NULL,coap_header,handler) != NULL); 00384 00385 if(coap_header->content_type_ptr) { 00386 free(coap_header->content_type_ptr); 00387 coap_header->content_type_ptr = NULL; 00388 } 00389 00390 if(common_stub::coap_header->content_type_ptr) { 00391 free(common_stub::coap_header->content_type_ptr); 00392 common_stub::coap_header->content_type_ptr = NULL; 00393 } 00394 if(common_stub::coap_header->options_list_ptr) { 00395 free(common_stub::coap_header->options_list_ptr); 00396 common_stub::coap_header->options_list_ptr = NULL; 00397 } 00398 00399 // Not OMA TLV or JSON 00400 m2mbase_stub::uint8_value = 110; 00401 CHECK(object->handle_get_request(NULL,coap_header,handler) != NULL); 00402 00403 if(common_stub::coap_header->content_type_ptr) { 00404 free(common_stub::coap_header->content_type_ptr); 00405 common_stub::coap_header->content_type_ptr = NULL; 00406 } 00407 if(common_stub::coap_header->options_list_ptr) { 00408 free(common_stub::coap_header->options_list_ptr); 00409 common_stub::coap_header->options_list_ptr = NULL; 00410 } 00411 00412 // OMA TLV 00413 m2mbase_stub::uint8_value = 99; 00414 CHECK(object->handle_get_request(NULL,coap_header,handler) != NULL); 00415 00416 if(common_stub::coap_header->content_type_ptr) { 00417 free(common_stub::coap_header->content_type_ptr); 00418 common_stub::coap_header->content_type_ptr = NULL; 00419 } 00420 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00421 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00422 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00423 } 00424 if(common_stub::coap_header->options_list_ptr) { 00425 free(common_stub::coap_header->options_list_ptr); 00426 common_stub::coap_header->options_list_ptr = NULL; 00427 } 00428 00429 // OMA JSON 00430 m2mbase_stub::uint8_value = 100; 00431 CHECK(object->handle_get_request(NULL,coap_header,handler) != NULL); 00432 00433 if(common_stub::coap_header->content_type_ptr) { 00434 free(common_stub::coap_header->content_type_ptr); 00435 common_stub::coap_header->content_type_ptr = NULL; 00436 } 00437 if(common_stub::coap_header->options_list_ptr) { 00438 free(common_stub::coap_header->options_list_ptr); 00439 common_stub::coap_header->options_list_ptr = NULL; 00440 } 00441 00442 coap_header->options_list_ptr->observe = 1; 00443 00444 uint8_t obs = 0; 00445 coap_header->options_list_ptr->observe_ptr = (uint8_t*)malloc(sizeof(obs)); 00446 memcpy(coap_header->options_list_ptr->observe_ptr,&obs,sizeof(obs)); 00447 coap_header->options_list_ptr->observe_len = 0; 00448 m2mbase_stub::uint16_value = 0x1c1c; 00449 m2mbase_stub::uint8_value = 99; 00450 m2mbase_stub::bool_value = true; 00451 00452 CHECK(object->handle_get_request(NULL,coap_header,handler) != NULL); 00453 00454 if(common_stub::coap_header->content_type_ptr) { 00455 free(common_stub::coap_header->content_type_ptr); 00456 common_stub::coap_header->content_type_ptr = NULL; 00457 } 00458 00459 if(common_stub::coap_header->options_list_ptr->observe_ptr) { 00460 free(common_stub::coap_header->options_list_ptr->observe_ptr); 00461 common_stub::coap_header->options_list_ptr->observe_ptr = NULL; 00462 } 00463 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00464 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00465 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00466 } 00467 if(common_stub::coap_header->options_list_ptr) { 00468 free(common_stub::coap_header->options_list_ptr); 00469 common_stub::coap_header->options_list_ptr = NULL; 00470 } 00471 00472 m2mbase_stub::uint16_value = 10; 00473 00474 CHECK(object->handle_get_request(NULL,coap_header,handler) != NULL); 00475 00476 if(common_stub::coap_header->content_type_ptr) { 00477 free(common_stub::coap_header->content_type_ptr); 00478 common_stub::coap_header->content_type_ptr = NULL; 00479 } 00480 00481 if(common_stub::coap_header->options_list_ptr->observe_ptr) { 00482 free(common_stub::coap_header->options_list_ptr->observe_ptr); 00483 common_stub::coap_header->options_list_ptr->observe_ptr = NULL; 00484 } 00485 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00486 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00487 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00488 } 00489 if(common_stub::coap_header->options_list_ptr) { 00490 free(common_stub::coap_header->options_list_ptr); 00491 common_stub::coap_header->options_list_ptr = NULL; 00492 } 00493 00494 // Not observable 00495 m2mbase_stub::bool_value = false; 00496 CHECK(object->handle_get_request(NULL,coap_header,handler) != NULL); 00497 00498 m2mbase_stub::bool_value = true; 00499 00500 if(common_stub::coap_header->content_type_ptr) { 00501 free(common_stub::coap_header->content_type_ptr); 00502 common_stub::coap_header->content_type_ptr = NULL; 00503 } 00504 00505 if(common_stub::coap_header->options_list_ptr->observe_ptr) { 00506 free(common_stub::coap_header->options_list_ptr->observe_ptr); 00507 common_stub::coap_header->options_list_ptr->observe_ptr = NULL; 00508 } 00509 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00510 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00511 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00512 } 00513 if(common_stub::coap_header->options_list_ptr) { 00514 free(common_stub::coap_header->options_list_ptr); 00515 common_stub::coap_header->options_list_ptr = NULL; 00516 } 00517 00518 coap_header->options_list_ptr->observe_len = 1; 00519 00520 CHECK(object->handle_get_request(NULL,coap_header,handler) != NULL); 00521 00522 if(common_stub::coap_header->content_type_ptr) { 00523 free(common_stub::coap_header->content_type_ptr); 00524 common_stub::coap_header->content_type_ptr = NULL; 00525 } 00526 00527 if(common_stub::coap_header->options_list_ptr->observe_ptr) { 00528 free(common_stub::coap_header->options_list_ptr->observe_ptr); 00529 common_stub::coap_header->options_list_ptr->observe_ptr = NULL; 00530 } 00531 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00532 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00533 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00534 } 00535 if(common_stub::coap_header->options_list_ptr) { 00536 free(common_stub::coap_header->options_list_ptr); 00537 common_stub::coap_header->options_list_ptr = NULL; 00538 } 00539 00540 00541 obs = 1; 00542 memcpy(coap_header->options_list_ptr->observe_ptr,&obs,sizeof(obs)); 00543 m2mbase_stub::uint8_value = 99; 00544 CHECK(object->handle_get_request(NULL,coap_header,handler) != NULL); 00545 00546 if(common_stub::coap_header->content_type_ptr) { 00547 free(common_stub::coap_header->content_type_ptr); 00548 common_stub::coap_header->content_type_ptr = NULL; 00549 } 00550 00551 if(common_stub::coap_header->options_list_ptr->observe_ptr) { 00552 free(common_stub::coap_header->options_list_ptr->observe_ptr); 00553 common_stub::coap_header->options_list_ptr->observe_ptr = NULL; 00554 } 00555 if(common_stub::coap_header->options_list_ptr->max_age_ptr) { 00556 free(common_stub::coap_header->options_list_ptr->max_age_ptr); 00557 common_stub::coap_header->options_list_ptr->max_age_ptr = NULL; 00558 } 00559 if(common_stub::coap_header->options_list_ptr) { 00560 free(common_stub::coap_header->options_list_ptr); 00561 common_stub::coap_header->options_list_ptr = NULL; 00562 } 00563 00564 m2mbase_stub::operation = M2MBase::NOT_ALLOWED; 00565 CHECK(object->handle_get_request(NULL,coap_header,handler) != NULL); 00566 00567 CHECK(object->handle_get_request(NULL,NULL,handler) != NULL); 00568 00569 if(coap_header->token_ptr) { 00570 free(coap_header->token_ptr); 00571 coap_header->token_ptr = NULL; 00572 } 00573 if(coap_header->content_type_ptr) { 00574 free(coap_header->content_type_ptr); 00575 coap_header->content_type_ptr = NULL; 00576 } 00577 if(coap_header->options_list_ptr->observe_ptr) { 00578 free(coap_header->options_list_ptr->observe_ptr); 00579 coap_header->options_list_ptr->observe_ptr = NULL; 00580 } 00581 if(coap_header->options_list_ptr) { 00582 free(coap_header->options_list_ptr); 00583 coap_header->options_list_ptr = NULL; 00584 } 00585 00586 if(common_stub::coap_header){ 00587 if(common_stub::coap_header->content_type_ptr) { 00588 free(common_stub::coap_header->content_type_ptr); 00589 common_stub::coap_header->content_type_ptr = NULL; 00590 } 00591 if(common_stub::coap_header->options_list_ptr) { 00592 free(common_stub::coap_header->options_list_ptr); 00593 common_stub::coap_header->options_list_ptr = NULL; 00594 } 00595 free(common_stub::coap_header); 00596 common_stub::coap_header = NULL; 00597 } 00598 free(coap_header); 00599 coap_header = NULL; 00600 00601 if(m2mtlvserializer_stub::uint8_value) { 00602 free(m2mtlvserializer_stub::uint8_value); 00603 } 00604 m2mtlvserializer_stub::clear(); 00605 common_stub::clear(); 00606 } 00607 00608 void Test_M2MObjectInstance::test_handle_put_request() 00609 { 00610 uint8_t value[] = {"name"}; 00611 bool execute_value_updated = false; 00612 sn_coap_hdr_s *coap_header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00613 memset(coap_header, 0, sizeof(sn_coap_hdr_s)); 00614 00615 coap_header->uri_path_ptr = value; 00616 coap_header->uri_path_len = sizeof(value); 00617 00618 coap_header->msg_code = COAP_MSG_CODE_REQUEST_PUT; 00619 00620 common_stub::int_value = 0; 00621 m2mbase_stub::string_value = new String("name"); 00622 00623 m2mbase_stub::operation = M2MBase::PUT_ALLOWED; 00624 m2mbase_stub::uint8_value = 200; 00625 00626 common_stub::coap_header = (sn_coap_hdr_ *)malloc(sizeof(sn_coap_hdr_)); 00627 memset(common_stub::coap_header,0,sizeof(sn_coap_hdr_)); 00628 00629 coap_header->payload_ptr = (uint8_t*)malloc(1); 00630 00631 sn_coap_hdr_s *coap_response = NULL; 00632 m2mbase_stub::uint8_value = 99; 00633 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00634 CHECK( coap_response != NULL); 00635 00636 m2mbase_stub::uint8_value = 0; 00637 00638 coap_header->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); 00639 coap_header->options_list_ptr->uri_query_ptr = value; 00640 coap_header->options_list_ptr->uri_query_len = sizeof(value); 00641 00642 coap_header->content_type_ptr = (uint8_t*)malloc(1); 00643 coap_header->content_type_len = 1; 00644 *coap_header->content_type_ptr = 99; 00645 m2mtlvdeserializer_stub::bool_value = true; 00646 m2mtlvdeserializer_stub::error = M2MTLVDeserializer::None; 00647 00648 m2mbase_stub::bool_value = false; 00649 00650 00651 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00652 CHECK( coap_response != NULL); 00653 if(coap_response) { 00654 if(coap_response->content_type_ptr) { 00655 free(coap_response->content_type_ptr); 00656 coap_response->content_type_ptr = NULL; 00657 } 00658 } 00659 00660 m2mbase_stub::bool_value = true; 00661 00662 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00663 CHECK( coap_response != NULL); 00664 if(coap_response) { 00665 if(coap_response->content_type_ptr) { 00666 free(coap_response->content_type_ptr); 00667 coap_response->content_type_ptr = NULL; 00668 } 00669 } 00670 00671 free(coap_header->options_list_ptr); 00672 coap_header->options_list_ptr = NULL; 00673 00674 m2mbase_stub::bool_value = false; 00675 00676 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00677 CHECK( coap_response != NULL); 00678 if(coap_response) { 00679 if(coap_response->content_type_ptr) { 00680 free(coap_response->content_type_ptr); 00681 coap_response->content_type_ptr = NULL; 00682 } 00683 } 00684 00685 m2mtlvdeserializer_stub::error = M2MTLVDeserializer::NotFound; 00686 00687 m2mbase_stub::bool_value = false; 00688 00689 00690 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00691 CHECK( coap_response != NULL); 00692 if(coap_response) { 00693 if(coap_response->content_type_ptr) { 00694 free(coap_response->content_type_ptr); 00695 coap_response->content_type_ptr = NULL; 00696 } 00697 } 00698 00699 m2mtlvdeserializer_stub::error = M2MTLVDeserializer::NotValid; 00700 00701 m2mbase_stub::bool_value = false; 00702 00703 00704 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00705 CHECK( coap_response != NULL); 00706 if(coap_response) { 00707 if(coap_response->content_type_ptr) { 00708 free(coap_response->content_type_ptr); 00709 coap_response->content_type_ptr = NULL; 00710 } 00711 } 00712 00713 m2mtlvdeserializer_stub::error = M2MTLVDeserializer::NotAllowed; 00714 00715 m2mbase_stub::bool_value = false; 00716 00717 00718 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00719 CHECK( coap_response != NULL); 00720 if(coap_response) { 00721 if(coap_response->content_type_ptr) { 00722 free(coap_response->content_type_ptr); 00723 coap_response->content_type_ptr = NULL; 00724 } 00725 } 00726 00727 m2mtlvdeserializer_stub::bool_value = false; 00728 00729 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00730 CHECK( coap_response != NULL); 00731 if(coap_response) { 00732 if(coap_response->content_type_ptr) { 00733 free(coap_response->content_type_ptr); 00734 coap_response->content_type_ptr = NULL; 00735 } 00736 } 00737 00738 *coap_header->content_type_ptr = 100; 00739 00740 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00741 CHECK( coap_response != NULL); 00742 if(coap_response) { 00743 if(coap_response->content_type_ptr) { 00744 free(coap_response->content_type_ptr); 00745 coap_response->content_type_ptr = NULL; 00746 } 00747 } 00748 00749 free(coap_header->payload_ptr); 00750 coap_header->payload_ptr = NULL; 00751 00752 m2mbase_stub::bool_value = true; 00753 00754 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00755 CHECK( coap_response != NULL); 00756 if(coap_response) { 00757 if(coap_response->content_type_ptr) { 00758 free(coap_response->content_type_ptr); 00759 coap_response->content_type_ptr = NULL; 00760 } 00761 } 00762 00763 m2mbase_stub::bool_value = false; 00764 00765 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00766 CHECK( coap_response != NULL); 00767 if(coap_response) { 00768 if(coap_response->content_type_ptr) { 00769 free(coap_response->content_type_ptr); 00770 coap_response->content_type_ptr = NULL; 00771 } 00772 } 00773 00774 m2mbase_stub::operation = M2MBase::NOT_ALLOWED; 00775 00776 coap_response = object->handle_put_request(NULL,coap_header,handler,execute_value_updated); 00777 CHECK( coap_response != NULL); 00778 if(coap_response) { 00779 if(coap_response->content_type_ptr) { 00780 free(coap_response->content_type_ptr); 00781 coap_response->content_type_ptr = NULL; 00782 } 00783 } 00784 00785 coap_response = object->handle_put_request(NULL,NULL,handler,execute_value_updated); 00786 00787 CHECK( coap_response != NULL); 00788 if(coap_response) { 00789 if(coap_response->content_type_ptr) { 00790 free(coap_response->content_type_ptr); 00791 coap_response->content_type_ptr = NULL; 00792 } 00793 } 00794 00795 00796 free(coap_header->content_type_ptr); 00797 //free(coap_header->options_list_ptr); 00798 free(common_stub::coap_header); 00799 delete m2mbase_stub::string_value; 00800 free(coap_header); 00801 00802 m2mtlvdeserializer_stub::clear(); 00803 common_stub::clear(); 00804 m2mbase_stub::clear(); 00805 } 00806 00807 void Test_M2MObjectInstance::test_handle_post_request() 00808 { 00809 uint8_t value[] = {"name"}; 00810 bool execute_value_updated = false; 00811 sn_coap_hdr_s *coap_header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); 00812 memset(coap_header, 0, sizeof(sn_coap_hdr_s)); 00813 00814 coap_header->uri_path_ptr = value; 00815 coap_header->uri_path_len = sizeof(value); 00816 00817 coap_header->msg_code = COAP_MSG_CODE_REQUEST_POST; 00818 00819 common_stub::int_value = 0; 00820 m2mbase_stub::string_value = new String("name"); 00821 00822 m2mbase_stub::operation = M2MBase::POST_ALLOWED; 00823 m2mbase_stub::uint8_value = 200; 00824 00825 common_stub::coap_header = (sn_coap_hdr_ *)malloc(sizeof(sn_coap_hdr_)); 00826 memset(common_stub::coap_header,0,sizeof(sn_coap_hdr_)); 00827 00828 m2mbase_stub::bool_value = false; 00829 00830 sn_coap_hdr_s * coap_response = NULL; 00831 m2mbase_stub::uint8_value = 99; 00832 00833 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 00834 CHECK( coap_response != NULL); 00835 if(coap_response) { 00836 if(coap_response->content_type_ptr) { 00837 free(coap_response->content_type_ptr); 00838 coap_response->content_type_ptr = NULL; 00839 } 00840 if (coap_response->options_list_ptr) { 00841 if (coap_response->options_list_ptr->location_path_ptr) { 00842 free(coap_response->options_list_ptr->location_path_ptr); 00843 coap_response->options_list_ptr->location_path_ptr = NULL; 00844 } 00845 free(coap_response->options_list_ptr); 00846 coap_response->options_list_ptr = NULL; 00847 } 00848 } 00849 00850 m2mbase_stub::uint8_value = 100; 00851 00852 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 00853 CHECK( coap_response != NULL); 00854 if(coap_response) { 00855 if(coap_response->content_type_ptr) { 00856 free(coap_response->content_type_ptr); 00857 coap_response->content_type_ptr = NULL; 00858 } 00859 if (coap_response->options_list_ptr) { 00860 if (coap_response->options_list_ptr->location_path_ptr) { 00861 free(coap_response->options_list_ptr->location_path_ptr); 00862 coap_response->options_list_ptr->location_path_ptr = NULL; 00863 } 00864 free(coap_response->options_list_ptr); 00865 coap_response->options_list_ptr = NULL; 00866 } 00867 } 00868 coap_header->payload_ptr = (uint8_t*)malloc(1); 00869 00870 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 00871 CHECK( coap_response != NULL); 00872 if(coap_response) { 00873 if(coap_response->content_type_ptr) { 00874 free(coap_response->content_type_ptr); 00875 coap_response->content_type_ptr = NULL; 00876 } 00877 if (coap_response->options_list_ptr) { 00878 if (coap_response->options_list_ptr->location_path_ptr) { 00879 free(coap_response->options_list_ptr->location_path_ptr); 00880 coap_response->options_list_ptr->location_path_ptr = NULL; 00881 } 00882 free(coap_response->options_list_ptr); 00883 coap_response->options_list_ptr = NULL; 00884 } 00885 } 00886 m2mbase_stub::uint8_value = 99; 00887 00888 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 00889 CHECK( coap_response != NULL); 00890 if(coap_response) { 00891 if(coap_response->content_type_ptr) { 00892 free(coap_response->content_type_ptr); 00893 coap_response->content_type_ptr = NULL; 00894 } 00895 if (coap_response->options_list_ptr) { 00896 if (coap_response->options_list_ptr->location_path_ptr) { 00897 free(coap_response->options_list_ptr->location_path_ptr); 00898 coap_response->options_list_ptr->location_path_ptr = NULL; 00899 } 00900 free(coap_response->options_list_ptr); 00901 coap_response->options_list_ptr = NULL; 00902 } 00903 } 00904 00905 m2mbase_stub::uint8_value = 0; 00906 00907 coap_header->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); 00908 coap_header->options_list_ptr->uri_query_ptr = value; 00909 coap_header->options_list_ptr->uri_query_len = sizeof(value); 00910 00911 coap_header->content_type_ptr = (uint8_t*)malloc(1); 00912 coap_header->content_type_len = 1; 00913 *coap_header->content_type_ptr = 99; 00914 m2mtlvdeserializer_stub::is_object_bool_value = true; 00915 m2mtlvdeserializer_stub::bool_value = false; 00916 m2mbase_stub::bool_value = false; 00917 00918 m2mtlvdeserializer_stub::error = M2MTLVDeserializer::None; 00919 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 00920 00921 CHECK( coap_response != NULL); 00922 if(coap_response) { 00923 if(coap_response->content_type_ptr) { 00924 free(coap_response->content_type_ptr); 00925 coap_response->content_type_ptr = NULL; 00926 } 00927 if (coap_response->options_list_ptr) { 00928 if (coap_response->options_list_ptr->location_path_ptr) { 00929 free(coap_response->options_list_ptr->location_path_ptr); 00930 coap_response->options_list_ptr->location_path_ptr = NULL; 00931 } 00932 free(coap_response->options_list_ptr); 00933 coap_response->options_list_ptr = NULL; 00934 } 00935 } 00936 00937 m2mbase_stub::operation = M2MBase::POST_ALLOWED; 00938 m2mtlvdeserializer_stub::is_object_bool_value = true; 00939 m2mtlvdeserializer_stub::bool_value = false; 00940 m2mbase_stub::bool_value = false; 00941 00942 m2mtlvdeserializer_stub::error = M2MTLVDeserializer::NotAllowed; 00943 00944 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 00945 00946 CHECK( coap_response != NULL); 00947 if(coap_response) { 00948 if(coap_response->content_type_ptr) { 00949 free(coap_response->content_type_ptr); 00950 coap_response->content_type_ptr = NULL; 00951 } 00952 if (coap_response->options_list_ptr) { 00953 if (coap_response->options_list_ptr->location_path_ptr) { 00954 free(coap_response->options_list_ptr->location_path_ptr); 00955 coap_response->options_list_ptr->location_path_ptr = NULL; 00956 } 00957 free(coap_response->options_list_ptr); 00958 coap_response->options_list_ptr = NULL; 00959 } 00960 } 00961 00962 m2mtlvdeserializer_stub::error = M2MTLVDeserializer::NotValid; 00963 00964 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 00965 00966 CHECK( coap_response != NULL); 00967 if(coap_response) { 00968 if(coap_response->content_type_ptr) { 00969 free(coap_response->content_type_ptr); 00970 coap_response->content_type_ptr = NULL; 00971 } 00972 if (coap_response->options_list_ptr) { 00973 if (coap_response->options_list_ptr->location_path_ptr) { 00974 free(coap_response->options_list_ptr->location_path_ptr); 00975 coap_response->options_list_ptr->location_path_ptr = NULL; 00976 } 00977 free(coap_response->options_list_ptr); 00978 coap_response->options_list_ptr = NULL; 00979 } 00980 } 00981 00982 00983 m2mbase_stub::operation = M2MBase::POST_ALLOWED; 00984 m2mtlvdeserializer_stub::is_object_bool_value = false; 00985 m2mtlvdeserializer_stub::bool_value = true; 00986 m2mbase_stub::bool_value = false; 00987 m2mtlvdeserializer_stub::error = M2MTLVDeserializer::None; 00988 00989 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 00990 00991 CHECK( coap_response != NULL); 00992 if(coap_response) { 00993 if(coap_response->content_type_ptr) { 00994 free(coap_response->content_type_ptr); 00995 coap_response->content_type_ptr = NULL; 00996 } 00997 if (coap_response->options_list_ptr) { 00998 if (coap_response->options_list_ptr->location_path_ptr) { 00999 free(coap_response->options_list_ptr->location_path_ptr); 01000 coap_response->options_list_ptr->location_path_ptr = NULL; 01001 } 01002 free(coap_response->options_list_ptr); 01003 coap_response->options_list_ptr = NULL; 01004 } 01005 } 01006 01007 m2mbase_stub::operation = M2MBase::POST_ALLOWED; 01008 m2mtlvdeserializer_stub::is_object_bool_value = false; 01009 m2mtlvdeserializer_stub::bool_value = true; 01010 m2mbase_stub::bool_value = false; 01011 m2mtlvdeserializer_stub::error = M2MTLVDeserializer::NotFound; 01012 01013 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 01014 01015 CHECK( coap_response != NULL); 01016 if(coap_response) { 01017 if(coap_response->content_type_ptr) { 01018 free(coap_response->content_type_ptr); 01019 coap_response->content_type_ptr = NULL; 01020 } 01021 if (coap_response->options_list_ptr) { 01022 if (coap_response->options_list_ptr->location_path_ptr) { 01023 free(coap_response->options_list_ptr->location_path_ptr); 01024 coap_response->options_list_ptr->location_path_ptr = NULL; 01025 } 01026 free(coap_response->options_list_ptr); 01027 coap_response->options_list_ptr = NULL; 01028 } 01029 } 01030 01031 m2mbase_stub::operation = M2MBase::POST_ALLOWED; 01032 m2mtlvdeserializer_stub::bool_value = false; 01033 01034 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 01035 01036 CHECK( coap_response != NULL); 01037 if(coap_response) { 01038 if(coap_response->content_type_ptr) { 01039 free(coap_response->content_type_ptr); 01040 coap_response->content_type_ptr = NULL; 01041 } 01042 if (coap_response->options_list_ptr) { 01043 if (coap_response->options_list_ptr->location_path_ptr) { 01044 free(coap_response->options_list_ptr->location_path_ptr); 01045 coap_response->options_list_ptr->location_path_ptr = NULL; 01046 } 01047 free(coap_response->options_list_ptr); 01048 coap_response->options_list_ptr = NULL; 01049 } 01050 } 01051 01052 01053 *coap_header->content_type_ptr = 100; 01054 01055 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 01056 01057 CHECK( coap_response != NULL); 01058 if(coap_response) { 01059 if(coap_response->content_type_ptr) { 01060 free(coap_response->content_type_ptr); 01061 coap_response->content_type_ptr = NULL; 01062 } 01063 if (coap_response->options_list_ptr) { 01064 if (coap_response->options_list_ptr->location_path_ptr) { 01065 free(coap_response->options_list_ptr->location_path_ptr); 01066 coap_response->options_list_ptr->location_path_ptr = NULL; 01067 } 01068 free(coap_response->options_list_ptr); 01069 coap_response->options_list_ptr = NULL; 01070 } 01071 } 01072 01073 01074 m2mbase_stub::bool_value = true; 01075 01076 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 01077 01078 CHECK( coap_response != NULL); 01079 if(coap_response) { 01080 if(coap_response->content_type_ptr) { 01081 free(coap_response->content_type_ptr); 01082 coap_response->content_type_ptr = NULL; 01083 } 01084 if (coap_response->options_list_ptr) { 01085 if (coap_response->options_list_ptr->location_path_ptr) { 01086 free(coap_response->options_list_ptr->location_path_ptr); 01087 coap_response->options_list_ptr->location_path_ptr = NULL; 01088 } 01089 free(coap_response->options_list_ptr); 01090 coap_response->options_list_ptr = NULL; 01091 } 01092 } 01093 01094 01095 m2mbase_stub::operation = M2MBase::NOT_ALLOWED; 01096 01097 coap_response = object->handle_post_request(NULL,coap_header,handler,execute_value_updated); 01098 01099 CHECK( coap_response != NULL); 01100 if(coap_response) { 01101 if(coap_response->content_type_ptr) { 01102 free(coap_response->content_type_ptr); 01103 coap_response->content_type_ptr = NULL; 01104 } 01105 if (coap_response->options_list_ptr) { 01106 if (coap_response->options_list_ptr->location_path_ptr) { 01107 free(coap_response->options_list_ptr->location_path_ptr); 01108 coap_response->options_list_ptr->location_path_ptr = NULL; 01109 } 01110 free(coap_response->options_list_ptr); 01111 coap_response->options_list_ptr = NULL; 01112 } 01113 } 01114 01115 coap_response = object->handle_post_request(NULL,NULL,handler,execute_value_updated); 01116 01117 CHECK( coap_response != NULL); 01118 if(coap_response) { 01119 if(coap_response->content_type_ptr) { 01120 free(coap_response->content_type_ptr); 01121 coap_response->content_type_ptr = NULL; 01122 } 01123 if (coap_response->options_list_ptr) { 01124 if (coap_response->options_list_ptr->location_path_ptr) { 01125 free(coap_response->options_list_ptr->location_path_ptr); 01126 coap_response->options_list_ptr->location_path_ptr = NULL; 01127 } 01128 free(coap_response->options_list_ptr); 01129 coap_response->options_list_ptr = NULL; 01130 } 01131 } 01132 01133 free(coap_header->content_type_ptr); 01134 free(coap_header->options_list_ptr); 01135 free(coap_header->payload_ptr); 01136 free(common_stub::coap_header); 01137 free(coap_header); 01138 delete m2mbase_stub::string_value; 01139 m2mtlvdeserializer_stub::clear(); 01140 common_stub::clear(); 01141 m2mbase_stub::clear(); 01142 01143 } 01144 01145 void Test_M2MObjectInstance::test_notification_update() 01146 { 01147 M2MBase::Observation obs_level = M2MBase::O_Attribute; 01148 01149 object->notification_update(obs_level); 01150 CHECK(callback->visited == true); 01151 01152 obs_level = M2MBase::OI_Attribute; 01153 01154 TestReportObserver obs; 01155 m2mbase_stub::report = new M2MReportHandler(obs); 01156 m2mbase_stub::bool_value = true; 01157 01158 object->notification_update(obs_level); 01159 01160 delete m2mbase_stub::report; 01161 m2mbase_stub::report = NULL; 01162 }
Generated on Tue Jul 12 2022 12:28:54 by
