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_m2mreporthandler.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 "CppUTestExt/MockSupport.h" 00018 #include "m2mtimer_stub.h" 00019 #include "test_m2mreporthandler.h" 00020 #include "m2mobservationhandler.h" 00021 #include "m2mtimer.h" 00022 #include "m2mtimerobserver.h" 00023 #include "m2mbase.h" 00024 00025 class Observer : public M2MReportObserver{ 00026 00027 public: 00028 00029 Observer() : visited(false) {} 00030 virtual ~Observer(){} 00031 void observation_to_be_sent(m2m::Vector<uint16_t>,bool){ 00032 visited = true; 00033 } 00034 bool visited; 00035 }; 00036 00037 class TimerObserver : public M2MTimerObserver 00038 { 00039 public: 00040 TimerObserver() : visited(false) {} 00041 virtual ~TimerObserver(){} 00042 00043 virtual void timer_expired(M2MTimerObserver::Type ){ 00044 visited = true; 00045 } 00046 00047 bool visited; 00048 }; 00049 00050 Test_M2MReportHandler::Test_M2MReportHandler() 00051 { 00052 _observer = new Observer(); 00053 M2MReportHandler handler(*_observer); 00054 _handler = new M2MReportHandler(*_observer); 00055 } 00056 00057 Test_M2MReportHandler::~Test_M2MReportHandler() 00058 { 00059 delete _handler; 00060 delete _observer; 00061 m2mtimer_stub::clear(); 00062 } 00063 00064 void Test_M2MReportHandler::test_set_under_observation() 00065 { 00066 m2mtimer_stub::enable_mock = true; 00067 00068 _observer->visited = false; 00069 00070 mock().clear(); 00071 mock().expectOneCall("stop_timer").onObject(&_handler->_pmin_timer); 00072 mock().expectOneCall("stop_timer").onObject(&_handler->_pmax_timer); 00073 _handler->set_under_observation(true); 00074 CHECK(false == _observer->visited); 00075 00076 mock().expectOneCall("stop_timer").onObject(&_handler->_pmin_timer); 00077 mock().expectOneCall("stop_timer").onObject(&_handler->_pmax_timer); 00078 _observer->visited = false; 00079 _handler->set_under_observation(false); 00080 CHECK(false == _observer->visited); 00081 mock().checkExpectations(); 00082 00083 m2mtimer_stub::enable_mock = false; 00084 } 00085 00086 void Test_M2MReportHandler::test_parse_notification_attribute() 00087 { 00088 char* val = {"value"}; 00089 CHECK(false == _handler->parse_notification_attribute(val, M2MBase::ObjectInstance )); 00090 00091 char* val2 = {"value&eval"}; 00092 CHECK(false == _handler->parse_notification_attribute(val2, M2MBase::ObjectInstance )); 00093 00094 char* val3 = {"toolongvaluevaluevaluevaluevalue&toolongvaluevaluevaluevaluevalue"}; 00095 CHECK(false == _handler->parse_notification_attribute(val3, M2MBase::ObjectInstance )); 00096 00097 char* val_real = {"st=6&pmax=3<=1>=100"}; 00098 CHECK(true == _handler->parse_notification_attribute(val_real, M2MBase::Resource, 00099 M2MResourceInstance::INTEGER )); 00100 00101 char* val_real1 = {"a=1&pmin=2&pmax=3>=4<=5&st=6"}; 00102 CHECK(false == _handler->parse_notification_attribute(val_real1, M2MBase::Resource, 00103 M2MResourceInstance::INTEGER )); 00104 00105 char* val2_real = {"st=6<=50>=1"}; 00106 CHECK(false == _handler->parse_notification_attribute(val2_real, M2MBase::Resource, 00107 M2MResourceInstance::INTEGER )); 00108 00109 char* val3_real = {"gt=40<=5&st=6"}; 00110 CHECK(true == _handler->parse_notification_attribute(val3_real, M2MBase::Resource, 00111 M2MResourceInstance::INTEGER )); 00112 00113 _handler->set_default_values(); 00114 00115 char* val5_real = {"pmin=10"}; 00116 CHECK(true == _handler->parse_notification_attribute(val5_real, M2MBase::Resource, 00117 M2MResourceInstance::INTEGER )); 00118 00119 char* val6_real = {"pmin=100&pmax=5"}; 00120 CHECK(false == _handler->parse_notification_attribute(val6_real, M2MBase::Resource, 00121 M2MResourceInstance::INTEGER )); 00122 00123 char* val7_real = {"st=6&pmax=30<=1>=100&pmin=0"}; 00124 CHECK(true == _handler->parse_notification_attribute(val7_real, M2MBase::Resource, 00125 M2MResourceInstance::INTEGER )); 00126 00127 char* val8_real = {"pmax=30<=10>=5&pmin=1"}; 00128 CHECK(false == _handler->parse_notification_attribute(val8_real, M2MBase::Resource, 00129 M2MResourceInstance::INTEGER )); 00130 00131 // low = lt + 2 * st = 18 00132 char* val9_real = {"pmax=30<=10>=17&pmin=1&st=4"}; 00133 CHECK(false == _handler->parse_notification_attribute(val9_real, M2MBase::Resource, 00134 M2MResourceInstance::INTEGER )); 00135 00136 // low = lt + 2 * st = 18 00137 char* val10_real = {"pmax=30<=10>=19&pmin=1&st=4"}; 00138 CHECK(true == _handler->parse_notification_attribute(val10_real, M2MBase::Resource, 00139 M2MResourceInstance::INTEGER )); 00140 00141 char* val11_real = {"pmax=30&pmin=30"}; 00142 CHECK(true == _handler->parse_notification_attribute(val11_real, M2MBase::Resource, 00143 M2MResourceInstance::INTEGER )); 00144 00145 _handler->set_default_values(); 00146 00147 char* inst_real = {"st=6&pmax=3<=1>=100"}; 00148 CHECK(true == _handler->parse_notification_attribute(inst_real, M2MBase::Resource, 00149 M2MResourceInstance::INTEGER )); 00150 00151 char* inst1_real1 = {"a=1&pmin=2&pmax=3>=4<=5&st=6"}; 00152 CHECK(false == _handler->parse_notification_attribute(inst1_real1, M2MBase::Resource, 00153 M2MResourceInstance::INTEGER )); 00154 00155 char* inst2_real = {"st=6<=50>=1"}; 00156 CHECK(false == _handler->parse_notification_attribute(inst2_real, M2MBase::Resource, 00157 M2MResourceInstance::INTEGER )); 00158 00159 char* inst3_real = {"gt=40<=5&st=6"}; 00160 CHECK(true == _handler->parse_notification_attribute(inst3_real, M2MBase::Resource, 00161 M2MResourceInstance::INTEGER )); 00162 00163 _handler->set_default_values(); 00164 char* inst5_real = {"pmin=10"}; 00165 CHECK(true == _handler->parse_notification_attribute(inst5_real, M2MBase::Resource, 00166 M2MResourceInstance::INTEGER )); 00167 00168 char* inst6_real = {"pmin=100&pmax=5"}; 00169 CHECK(false == _handler->parse_notification_attribute(inst6_real, M2MBase::Resource, 00170 M2MResourceInstance::INTEGER )); 00171 00172 char* inst7_real = {"st=6&pmax=30<=1>=100&pmin=0"}; 00173 CHECK(true == _handler->parse_notification_attribute(inst7_real, M2MBase::Resource, 00174 M2MResourceInstance::INTEGER )); 00175 00176 char* inst8_real = {"pmax=30<=10>=5&pmin=1"}; 00177 CHECK(false == _handler->parse_notification_attribute(inst8_real, M2MBase::Resource, 00178 M2MResourceInstance::INTEGER )); 00179 00180 // low = lt + 2 * st = 18 00181 char* inst9_real = {"pmax=30<=10>=17&pmin=1&st=4"}; 00182 CHECK(false == _handler->parse_notification_attribute(inst9_real, M2MBase::Resource, 00183 M2MResourceInstance::INTEGER )); 00184 00185 // low = lt + 2 * st = 18 00186 char* inst10_real = {"pmax=30<=10>=19&pmin=1&st=4"}; 00187 CHECK(true == _handler->parse_notification_attribute(inst10_real, M2MBase::Resource, 00188 M2MResourceInstance::INTEGER )); 00189 00190 char* inst11_real = {"pmax=30&pmin=30"}; 00191 CHECK(true == _handler->parse_notification_attribute(inst11_real, M2MBase::Resource, 00192 M2MResourceInstance::INTEGER )); 00193 00194 char* inst12_real = {"pmax=&pmin=30"}; 00195 CHECK(false == _handler->parse_notification_attribute(inst12_real, M2MBase::Resource, 00196 M2MResourceInstance::INTEGER )); 00197 char* inst13_real = {"st="}; 00198 CHECK(false == _handler->parse_notification_attribute(inst13_real, M2MBase::Resource, 00199 M2MResourceInstance::INTEGER )); 00200 00201 _handler->set_default_values(); 00202 00203 char* inst14_real = {"stp=10"}; 00204 CHECK(true == _handler->parse_notification_attribute(inst14_real, M2MBase::Resource, 00205 M2MResourceInstance::INTEGER )); 00206 char* inst15_real = {"stp=10&st=15"}; 00207 CHECK(true == _handler->parse_notification_attribute(inst15_real, M2MBase::Resource, 00208 M2MResourceInstance::INTEGER )); 00209 00210 _handler->set_default_values(); 00211 00212 DOUBLES_EQUAL(0,_handler->_lt,0); 00213 DOUBLES_EQUAL(0,_handler->_gt,0); 00214 DOUBLES_EQUAL(-1,_handler->_pmax,0); 00215 DOUBLES_EQUAL(1,_handler->_pmin,0); 00216 DOUBLES_EQUAL(0,_handler->_st,0); 00217 DOUBLES_EQUAL(0,_handler->_high_step,0); 00218 DOUBLES_EQUAL(0,_handler->_low_step,0); 00219 DOUBLES_EQUAL(-1,_handler->_last_value,0); 00220 DOUBLES_EQUAL(0,_handler->_attribute_state,0); 00221 CHECK_FALSE(_handler->_pmin_exceeded); 00222 CHECK_FALSE(_handler->_pmax_exceeded); 00223 } 00224 00225 void Test_M2MReportHandler::test_timer_expired() 00226 { 00227 _handler->timer_expired(M2MTimerObserver::Notdefined); 00228 CHECK(_observer->visited == false); 00229 00230 _handler->_notify = true; 00231 _handler->_pmin_exceeded = true; 00232 _handler->timer_expired(M2MTimerObserver::PMaxTimer); 00233 CHECK(_observer->visited == true); 00234 00235 _handler->_pmin_exceeded = false; 00236 _handler->_notify = false; 00237 _handler->_attribute_state = M2MReportHandler::Pmax; 00238 _handler->_current_value = 100; 00239 _handler->timer_expired(M2MTimerObserver::PMinTimer); 00240 CHECK(_handler->_pmin_exceeded == true); 00241 00242 _observer->visited = false; 00243 _handler->_notify = true; 00244 _handler->timer_expired(M2MTimerObserver::PMinTimer); 00245 CHECK(_observer->visited == true); 00246 00247 _handler->_notify = true; 00248 _handler->_pmin_exceeded = true; 00249 _handler->timer_expired(M2MTimerObserver::PMinTimer); 00250 CHECK(_handler->_pmin_exceeded == true); 00251 00252 _handler->_notify = true; 00253 _handler->_pmin_exceeded = false; 00254 _handler->_attribute_state = M2MReportHandler::Pmax; 00255 _handler->timer_expired(M2MTimerObserver::PMaxTimer); 00256 CHECK(_handler->_pmax_exceeded == true); 00257 } 00258 00259 void Test_M2MReportHandler::test_set_value() 00260 { 00261 _handler->_notify = true; 00262 _handler->_pmin_exceeded = false; 00263 _observer->visited = false; 00264 00265 _handler->set_value(1); 00266 _handler->set_value(10); 00267 CHECK(_observer->visited == true); 00268 00269 char* query = {"st=6"}; 00270 _handler->_attribute_state = 0; 00271 CHECK(true == _handler->parse_notification_attribute(query, 00272 M2MBase::Resource, 00273 M2MResourceInstance::INTEGER)); 00274 _observer->visited = false; 00275 _handler->set_value(15); 00276 CHECK(_observer->visited == false); 00277 00278 _observer->visited = false; 00279 _handler->set_value(21); 00280 CHECK(_observer->visited == true); 00281 00282 _handler->set_value(10); 00283 char* query2 = {"st=3<=10>=100"}; 00284 _handler->_attribute_state = 0; 00285 CHECK(true == _handler->parse_notification_attribute(query2, 00286 M2MBase::Resource, 00287 M2MResourceInstance::INTEGER)); 00288 00289 _observer->visited = false; 00290 _handler->set_value(12); 00291 CHECK(_observer->visited == false); 00292 00293 _observer->visited = false; 00294 _handler->set_value(15); 00295 CHECK(_observer->visited == true); 00296 00297 _observer->visited = false; 00298 _handler->set_value(5); 00299 CHECK(_observer->visited == true); 00300 00301 _observer->visited = false; 00302 _handler->set_value(4); 00303 CHECK(_observer->visited == true); 00304 00305 _observer->visited = false; 00306 _handler->set_value(101); 00307 CHECK(_observer->visited == true); 00308 00309 _observer->visited = false; 00310 _handler->set_value(102); 00311 CHECK(_observer->visited == true); 00312 00313 char* query3 = {"lt=10"}; 00314 _handler->set_default_values(); 00315 CHECK(true == _handler->parse_notification_attribute(query3, M2MBase::Resource, M2MResourceInstance::INTEGER)); 00316 _observer->visited = false; 00317 _handler->set_value(9); 00318 CHECK(_observer->visited == true); 00319 00320 _observer->visited = false; 00321 _handler->set_value(15); 00322 CHECK(_observer->visited == false); 00323 00324 char* query4 = {"gt=10"}; 00325 _handler->set_default_values(); 00326 CHECK(true == _handler->parse_notification_attribute(query4, M2MBase::Resource, M2MResourceInstance::INTEGER)); 00327 00328 // Instantiate timers 00329 _handler->_attribute_state |= M2MReportHandler::Pmin; 00330 _handler->handle_timers(); 00331 _observer->visited = false; 00332 _handler->set_value(9); 00333 CHECK(_observer->visited == false); 00334 // Stop timers and reset flag to previous state 00335 _handler->stop_timers(); 00336 _handler->_attribute_state = M2MReportHandler::Gt; 00337 00338 _observer->visited = false; 00339 _handler->set_value(15); 00340 CHECK(_observer->visited == true); 00341 00342 _observer->visited = false; 00343 _handler->set_value(16); 00344 CHECK(_observer->visited == true); 00345 00346 _observer->visited = false; 00347 char* query5 = {"gt=10"}; 00348 _handler->set_default_values(); 00349 CHECK(false == _handler->parse_notification_attribute(query5, M2MBase::Resource, M2MResourceInstance::STRING)); 00350 00351 _observer->visited = false; 00352 char* query6 = {"pmin=10"}; 00353 _handler->set_default_values(); 00354 CHECK(true == _handler->parse_notification_attribute(query6, M2MBase::Resource, M2MResourceInstance::STRING)); 00355 00356 _observer->visited = false; 00357 char* query7 = {"pmin=10&pmax=20"}; 00358 _handler->set_default_values(); 00359 CHECK(true == _handler->parse_notification_attribute(query7, M2MBase::Resource, M2MResourceInstance::OPAQUE)); 00360 00361 _observer->visited = false; 00362 _handler->handle_timers(); 00363 _handler->set_value(26); 00364 _handler->_pmin_exceeded = true; 00365 _handler->timer_expired(M2MTimerObserver::PMaxTimer); 00366 CHECK(_observer->visited == true); 00367 } 00368 00369 void Test_M2MReportHandler::test_trigger_object_notification() 00370 { 00371 _handler->_notify = true; 00372 _handler->_pmin_exceeded = true; 00373 _handler->set_notification_trigger(); 00374 CHECK(_handler->_pmin_exceeded == false); 00375 CHECK(_observer->visited == true); 00376 00377 _handler->_changed_instance_ids.push_back(0); 00378 _handler->_changed_instance_ids.push_back(1); 00379 _handler->_changed_instance_ids.push_back(2); 00380 00381 _handler->set_notification_trigger(); 00382 CHECK(_handler->_pmin_exceeded == false); 00383 CHECK(_observer->visited == true); 00384 00385 } 00386 00387 void Test_M2MReportHandler::test_set_string_notification_trigger() 00388 { 00389 _handler->_notify = true; 00390 _handler->_pmin_exceeded = true; 00391 _handler->set_notification_trigger(); 00392 CHECK(_handler->_pmin_exceeded == false); 00393 } 00394 00395 void Test_M2MReportHandler::test_timers() 00396 { 00397 m2mtimer_stub::enable_mock = true; 00398 // No timers should start when _attribute_state == 0 00399 _handler->handle_timers(); 00400 mock().checkExpectations(); 00401 00402 // pmin starts when _attribute_state |= pmin 00403 mock().clear(); 00404 mock().expectOneCall("start_timer").onObject(&_handler->_pmin_timer); 00405 _handler->_attribute_state |= M2MReportHandler::Pmin; 00406 _handler->handle_timers(); 00407 mock().checkExpectations(); 00408 00409 // pmin gets stopped 00410 mock().clear(); 00411 mock().expectOneCall("stop_timer").onObject(&_handler->_pmin_timer); 00412 mock().expectOneCall("stop_timer").onObject(&_handler->_pmax_timer); 00413 _handler->stop_timers(); 00414 mock().checkExpectations(); 00415 00416 // pmin starts when _attribute_state |= pmin & pmax but _pmax value is default -1 00417 // pmax should not start 00418 mock().clear(); 00419 mock().expectOneCall("start_timer").onObject(&_handler->_pmin_timer); 00420 _handler->_attribute_state |= M2MReportHandler::Pmax; 00421 _handler->handle_timers(); 00422 mock().checkExpectations(); 00423 00424 // pmin and pmax get stopped 00425 mock().clear(); 00426 mock().expectOneCall("stop_timer").onObject(&_handler->_pmin_timer); 00427 mock().expectOneCall("stop_timer").onObject(&_handler->_pmax_timer); 00428 _handler->stop_timers(); 00429 mock().checkExpectations(); 00430 00431 // max timer is started when pmax == pmin 00432 mock().clear(); 00433 mock().expectOneCall("start_timer").onObject(&_handler->_pmax_timer); 00434 _handler->_pmax = 2; 00435 _handler->_pmin = 2; 00436 _handler->handle_timers(); 00437 mock().checkExpectations(); 00438 CHECK(_handler->_pmin_exceeded == true); 00439 00440 /* 00441 mock().clear(); 00442 mock().expectOneCall("stop_timer").onObject(&_handler->_pmin_timer); 00443 mock().expectOneCall("stop_timer").onObject(&_handler->_pmax_timer); 00444 _handler->stop_timers(); 00445 mock().checkExpectations(); 00446 */ 00447 m2mtimer_stub::enable_mock = false; 00448 } 00449 00450 void Test_M2MReportHandler::test_attribute_flags() 00451 { 00452 CHECK(_handler->attribute_flags() == 0); 00453 _handler->_attribute_state = M2MReportHandler::Pmax | M2MReportHandler::Pmin | 00454 M2MReportHandler::St | M2MReportHandler::Gt | M2MReportHandler::Lt | M2MReportHandler::Cancel; 00455 CHECK(_handler->attribute_flags() == (1 << 6) - 1); 00456 }
Generated on Tue Jul 12 2022 21:20:30 by
1.7.2