Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
test_m2mserver.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_m2mserver.h" 00018 #include "m2mobject_stub.h" 00019 #include "m2mobjectinstance_stub.h" 00020 #include "m2mresource_stub.h" 00021 #include "m2mresourceinstance_stub.h" 00022 #include "m2mbase_stub.h" 00023 #include "nsdlaccesshelper_stub.h" 00024 00025 Test_M2MServer::Test_M2MServer() 00026 { 00027 M2MObject* obj = new M2MObject("name", "name"); 00028 m2mobject_stub::inst = new M2MObjectInstance(*obj,"name", "type", "name"); 00029 m2mresource_stub::bool_value = true; 00030 m2mobjectinstance_stub::create_resource = new M2MResource(*m2mobject_stub::inst, 00031 "name", 00032 "type", 00033 M2MResourceInstance::STRING, 00034 false, 00035 "name"); 00036 server = new M2MServer(); 00037 00038 delete m2mobjectinstance_stub::create_resource; 00039 m2mobjectinstance_stub::create_resource = NULL; 00040 delete obj; 00041 } 00042 00043 Test_M2MServer::~Test_M2MServer() 00044 { 00045 delete m2mobject_stub::inst; 00046 m2mobject_stub::inst = NULL; 00047 00048 m2mobject_stub::clear(); 00049 m2mobjectinstance_stub::clear(); 00050 m2mresource_stub::clear(); 00051 m2mbase_stub::clear(); 00052 delete server; 00053 } 00054 00055 void Test_M2MServer::test_create_resource_int() 00056 { 00057 m2mobjectinstance_stub::create_resource = new M2MResource(*m2mobject_stub::inst, 00058 "name", 00059 "type", 00060 M2MResourceInstance::STRING, 00061 false, 00062 "name"); 00063 00064 CHECK(server->create_resource(M2MServer::DefaultMinPeriod,10) != NULL); 00065 CHECK(server->create_resource(M2MServer::DefaultMaxPeriod,10) != NULL); 00066 CHECK(server->create_resource(M2MServer::DisableTimeout,10) != NULL); 00067 00068 CHECK(server->create_resource(M2MServer::ShortServerID,10) == NULL); 00069 CHECK(server->create_resource(M2MServer::Lifetime,10) == NULL); 00070 CHECK(server->create_resource(M2MServer::Disable,10) == NULL); 00071 CHECK(server->create_resource(M2MServer::Binding,10) == NULL); 00072 CHECK(server->create_resource(M2MServer::NotificationStorage,10) == NULL); 00073 CHECK(server->create_resource(M2MServer::Binding,10) == NULL); 00074 CHECK(server->create_resource(M2MServer::RegistrationUpdate,10) == NULL); 00075 00076 delete m2mobjectinstance_stub::create_resource; 00077 m2mobjectinstance_stub::create_resource = NULL; 00078 } 00079 00080 void Test_M2MServer::test_create_resource() 00081 { 00082 m2mobjectinstance_stub::create_resource = new M2MResource(*m2mobject_stub::inst, 00083 "name", 00084 "type", 00085 M2MResourceInstance::STRING, 00086 false, 00087 "name"); 00088 00089 CHECK(server->create_resource(M2MServer::Disable) != NULL); 00090 00091 CHECK(server->create_resource(M2MServer::DefaultMinPeriod) == NULL); 00092 CHECK(server->create_resource(M2MServer::DefaultMaxPeriod) == NULL); 00093 CHECK(server->create_resource(M2MServer::DefaultMaxPeriod) == NULL); 00094 CHECK(server->create_resource(M2MServer::ShortServerID) == NULL); 00095 CHECK(server->create_resource(M2MServer::Lifetime) == NULL); 00096 CHECK(server->create_resource(M2MServer::DisableTimeout) == NULL); 00097 CHECK(server->create_resource(M2MServer::Binding) == NULL); 00098 CHECK(server->create_resource(M2MServer::NotificationStorage) == NULL); 00099 CHECK(server->create_resource(M2MServer::RegistrationUpdate) == NULL); 00100 00101 delete m2mobjectinstance_stub::create_resource; 00102 m2mobjectinstance_stub::create_resource = NULL; 00103 } 00104 00105 void Test_M2MServer::test_delete_resource() 00106 { 00107 m2mobjectinstance_stub::bool_value = true; 00108 00109 CHECK(server->delete_resource(M2MServer::ShortServerID) == false); 00110 CHECK(server->delete_resource(M2MServer::Binding) == false); 00111 CHECK(server->delete_resource(M2MServer::NotificationStorage) == false); 00112 CHECK(server->delete_resource(M2MServer::Lifetime) == false); 00113 CHECK(server->delete_resource(M2MServer::RegistrationUpdate) == false); 00114 00115 CHECK(server->delete_resource(M2MServer::DefaultMinPeriod) == true); 00116 CHECK(server->delete_resource(M2MServer::DefaultMaxPeriod) == true); 00117 CHECK(server->delete_resource(M2MServer::DisableTimeout) == true); 00118 CHECK(server->delete_resource(M2MServer::Disable) == true); 00119 } 00120 00121 void Test_M2MServer::test_set_resource_value_int() 00122 { 00123 m2mbase_stub::bool_value = true; 00124 00125 m2mobjectinstance_stub::resource = new M2MResource(*m2mobject_stub::inst, 00126 "name", 00127 "type", 00128 M2MResourceInstance::STRING, 00129 false, 00130 "name"); 00131 00132 CHECK(server->set_resource_value(M2MServer::DefaultMinPeriod,10) == true); 00133 CHECK(server->set_resource_value(M2MServer::DefaultMaxPeriod,10) == true); 00134 CHECK(server->set_resource_value(M2MServer::DisableTimeout,10) == true); 00135 CHECK(server->set_resource_value(M2MServer::ShortServerID,10) == true); 00136 CHECK(server->set_resource_value(M2MServer::NotificationStorage,10) == true); 00137 CHECK(server->set_resource_value(M2MServer::Lifetime,10) == true); 00138 00139 CHECK(server->set_resource_value(M2MServer::Binding,10) == false); 00140 CHECK(server->set_resource_value(M2MServer::RegistrationUpdate,10) == false); 00141 CHECK(server->set_resource_value(M2MServer::Disable,10) == false); 00142 00143 delete m2mobjectinstance_stub::resource; 00144 m2mobjectinstance_stub::resource = NULL; 00145 } 00146 00147 void Test_M2MServer::test_set_resource_value_string() 00148 { 00149 m2mresourceinstance_stub::bool_value = true; 00150 00151 m2mobjectinstance_stub::resource = new M2MResource(*m2mobject_stub::inst, 00152 "name", 00153 "type", 00154 M2MResourceInstance::STRING, 00155 false, 00156 "name"); 00157 00158 CHECK(server->set_resource_value(M2MServer::Binding,"test") == true); 00159 00160 CHECK(server->set_resource_value(M2MServer::DefaultMinPeriod,"test") == false); 00161 CHECK(server->set_resource_value(M2MServer::DefaultMaxPeriod,"test") == false); 00162 CHECK(server->set_resource_value(M2MServer::DisableTimeout,"test") == false); 00163 CHECK(server->set_resource_value(M2MServer::ShortServerID,"test") == false); 00164 CHECK(server->set_resource_value(M2MServer::NotificationStorage,"test") == false); 00165 CHECK(server->set_resource_value(M2MServer::Lifetime,"test") == false); 00166 CHECK(server->set_resource_value(M2MServer::RegistrationUpdate,"test") == false); 00167 CHECK(server->set_resource_value(M2MServer::Disable,"test") == false); 00168 00169 delete m2mobjectinstance_stub::resource; 00170 m2mobjectinstance_stub::resource = NULL; 00171 } 00172 00173 void Test_M2MServer::test_resource_value_int() 00174 { 00175 uint8_t value[] = {"10"}; 00176 m2mresourceinstance_stub::value = (uint8_t*)malloc((uint32_t)sizeof(value)); 00177 memset(m2mresourceinstance_stub::value,0,(uint32_t)sizeof(value)); 00178 memcpy(m2mresourceinstance_stub::value,value,sizeof(value)); 00179 m2mresourceinstance_stub::int_value = (uint32_t)sizeof(value); 00180 00181 m2mobjectinstance_stub::resource = new M2MResource(*m2mobject_stub::inst, 00182 "name", 00183 "type", 00184 M2MResourceInstance::STRING, 00185 false, 00186 "name"); 00187 00188 CHECK(server->resource_value_int(M2MServer::ShortServerID) == 10); 00189 CHECK(server->resource_value_int(M2MServer::DefaultMinPeriod) == 10); 00190 CHECK(server->resource_value_int(M2MServer::DefaultMaxPeriod) == 10); 00191 CHECK(server->resource_value_int(M2MServer::DisableTimeout) == 10); 00192 CHECK(server->resource_value_int(M2MServer::NotificationStorage) == 10); 00193 CHECK(server->resource_value_int(M2MServer::Lifetime) == 10); 00194 CHECK(server->resource_value_int(M2MServer::Binding) == 0); 00195 CHECK(server->resource_value_int(M2MServer::RegistrationUpdate) == 0); 00196 CHECK(server->resource_value_int(M2MServer::Disable) == 0); 00197 00198 delete m2mobjectinstance_stub::resource; 00199 m2mobjectinstance_stub::resource = NULL; 00200 00201 free(m2mresourceinstance_stub::value); 00202 m2mresourceinstance_stub::value = NULL; 00203 } 00204 00205 void Test_M2MServer::test_resource_value_string() 00206 { 00207 String test = "string"; 00208 uint8_t value[] = {"string"}; 00209 00210 m2mresourceinstance_stub::value = (uint8_t*)malloc((uint32_t)sizeof(value)); 00211 memset(m2mresourceinstance_stub::value,0,(uint32_t)sizeof(value)); 00212 memcpy(m2mresourceinstance_stub::value,value,sizeof(value)); 00213 m2mresourceinstance_stub::int_value = (uint32_t)sizeof(value); 00214 00215 m2mobjectinstance_stub::resource = new M2MResource(*m2mobject_stub::inst, 00216 "name", 00217 "type", 00218 M2MResourceInstance::STRING, 00219 false, 00220 "name"); 00221 00222 CHECK(server->resource_value_string(M2MServer::Binding) == test); 00223 00224 CHECK(server->resource_value_string(M2MServer::Disable) == ""); 00225 CHECK(server->resource_value_string(M2MServer::RegistrationUpdate) == ""); 00226 CHECK(server->resource_value_string(M2MServer::Lifetime) == ""); 00227 CHECK(server->resource_value_string(M2MServer::NotificationStorage) == ""); 00228 CHECK(server->resource_value_string(M2MServer::DisableTimeout) == ""); 00229 CHECK(server->resource_value_string(M2MServer::DefaultMaxPeriod) == ""); 00230 CHECK(server->resource_value_string(M2MServer::DefaultMinPeriod) == ""); 00231 CHECK(server->resource_value_string(M2MServer::ShortServerID) == ""); 00232 00233 delete m2mobjectinstance_stub::resource; 00234 m2mobjectinstance_stub::resource = NULL; 00235 00236 free(m2mresourceinstance_stub::value); 00237 m2mresourceinstance_stub::value = NULL; 00238 } 00239 00240 void Test_M2MServer::test_is_resource_present() 00241 { 00242 m2mobjectinstance_stub::resource = new M2MResource(*m2mobject_stub::inst, 00243 "name", 00244 "type", 00245 M2MResourceInstance::STRING, 00246 false, 00247 "name"); 00248 00249 CHECK(server->is_resource_present(M2MServer::ShortServerID) == true); 00250 00251 delete m2mobjectinstance_stub::resource; 00252 m2mobjectinstance_stub::resource = NULL; 00253 00254 CHECK(server->is_resource_present(M2MServer::DefaultMinPeriod) == false); 00255 } 00256 00257 void Test_M2MServer::test_total_resource_count() 00258 { 00259 M2MResource *res = new M2MResource(*m2mobject_stub::inst, 00260 "name", 00261 "type", 00262 M2MResourceInstance::STRING, 00263 false, 00264 "name"); 00265 00266 m2mobjectinstance_stub::resource_list.push_back(res); 00267 M2MResource *res2 = new M2MResource(*m2mobject_stub::inst, 00268 "name", 00269 "type", 00270 M2MResourceInstance::STRING, 00271 false, 00272 "name"); 00273 00274 m2mobjectinstance_stub::resource_list.push_back(res2); 00275 00276 CHECK(server->total_resource_count() == 2); 00277 00278 m2mobjectinstance_stub::resource_list.clear(); 00279 00280 delete res2; 00281 delete res; 00282 }
Generated on Tue Jul 12 2022 21:20:30 by
