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