1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
/* nutclienttest - CppUnit nutclient unit test
Copyright (C)
2016 Emilien Kia <emilien.kia@gmail.com>
2020 - 2025 Jim Klimov <jimklimov+nut@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common.h"
/* Current CPPUnit offends the honor of C++98 and maybe later versions */
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_EXIT_TIME_DESTRUCTORS || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_GLOBAL_CONSTRUCTORS || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_DEPRECATED_DECLARATIONS || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_SUGGEST_OVERRIDE_BESIDEFUNC || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_SUGGEST_DESTRUCTOR_OVERRIDE_BESIDEFUNC || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_WEAK_VTABLES_BESIDEFUNC || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_DEPRECATED_DYNAMIC_EXCEPTION_SPEC_BESIDEFUNC || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_EXTRA_SEMI_BESIDEFUNC || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_OLD_STYLE_CAST_BESIDEFUNC)
#pragma GCC diagnostic push
# ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_GLOBAL_CONSTRUCTORS
# pragma GCC diagnostic ignored "-Wglobal-constructors"
# endif
# ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_EXIT_TIME_DESTRUCTORS
# pragma GCC diagnostic ignored "-Wexit-time-destructors"
# endif
# ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_DEPRECATED_DECLARATIONS
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
# endif
# ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_SUGGEST_OVERRIDE_BESIDEFUNC
# pragma GCC diagnostic ignored "-Wsuggest-override"
# endif
# ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_SUGGEST_DESTRUCTOR_OVERRIDE_BESIDEFUNC
# pragma GCC diagnostic ignored "-Wsuggest-destructor-override"
# endif
# ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_WEAK_VTABLES_BESIDEFUNC
# pragma GCC diagnostic ignored "-Wweak-vtables"
# endif
# ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_DEPRECATED_DYNAMIC_EXCEPTION_SPEC_BESIDEFUNC
# pragma GCC diagnostic ignored "-Wdeprecated-dynamic-exception-spec"
# endif
# ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_EXTRA_SEMI_BESIDEFUNC
# pragma GCC diagnostic ignored "-Wextra-semi"
# endif
# ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_OLD_STYLE_CAST_BESIDEFUNC
# pragma GCC diagnostic ignored "-Wold-style-cast"
# endif
#endif
#if (defined __clang__) && (defined HAVE_PRAGMA_CLANG_DIAGNOSTIC_IGNORED_DEPRECATED_DECLARATIONS)
# ifdef HAVE_PRAGMA_CLANG_DIAGNOSTIC_IGNORED_DEPRECATED_DECLARATIONS
# pragma clang diagnostic push "-Wdeprecated-declarations"
# endif
#endif
#include <cppunit/extensions/HelperMacros.h>
namespace nut {
class NutClientTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( NutClientTest );
CPPUNIT_TEST( test_strarr_alloc );
CPPUNIT_TEST( test_stringset_to_strarr );
CPPUNIT_TEST( test_stringvector_to_strarr );
CPPUNIT_TEST( test_copy_constructor_dev );
CPPUNIT_TEST( test_copy_assignment_dev );
CPPUNIT_TEST( test_copy_constructor_cmd );
CPPUNIT_TEST( test_copy_assignment_cmd );
CPPUNIT_TEST( test_copy_constructor_var );
CPPUNIT_TEST( test_copy_assignment_var );
CPPUNIT_TEST( test_nutclientstub_dev );
CPPUNIT_TEST_SUITE_END();
public:
void setUp() override;
void tearDown() override;
void test_strarr_alloc();
void test_stringset_to_strarr();
void test_stringvector_to_strarr();
void test_copy_constructor_dev();
void test_copy_assignment_dev();
void test_copy_constructor_cmd();
void test_copy_assignment_cmd();
void test_copy_constructor_var();
void test_copy_assignment_var();
void test_nutclientstub_dev();
};
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( NutClientTest );
} // namespace nut {}
#ifndef _NUTCLIENTTEST_BUILD
# define _NUTCLIENTTEST_BUILD 1
#endif
#include "../clients/nutclient.h"
#include "../clients/nutclientmem.h"
namespace nut {
extern "C" {
strarr stringset_to_strarr(const std::set<std::string>& strset);
strarr stringvector_to_strarr(const std::vector<std::string>& strset);
} // extern "C"
void NutClientTest::setUp()
{
}
void NutClientTest::tearDown()
{
}
void NutClientTest::test_strarr_alloc()
{
bool noException = true;
strarr arr = nullptr;
try {
arr = strarr_alloc(5);
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed strarr_alloc(...): throw exception",
noException);
CPPUNIT_ASSERT_MESSAGE(
"Failed strarr_alloc(...): result is null",
arr != nullptr);
strarr_free(arr);
}
void NutClientTest::test_stringset_to_strarr()
{
std::set<std::string> strset;
strset.insert("test");
strset.insert("hello");
strset.insert("world");
strarr arr = stringset_to_strarr(strset);
CPPUNIT_ASSERT_MESSAGE(
"stringset_to_strarr(...) result is null",
arr != nullptr);
std::set<std::string> res;
char** ptr = arr;
while(*ptr != nullptr)
{
res.insert(std::string(*ptr));
ptr++;
}
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"stringset_to_strarr(...) result has not 3 items",
static_cast<size_t>(3), res.size());
CPPUNIT_ASSERT_MESSAGE(
"stringset_to_strarr(...) result has not item \"test\"",
res.find("test") != res.end());
CPPUNIT_ASSERT_MESSAGE(
"stringset_to_strarr(...) result has not item \"hello\"",
res.find("hello") != res.end());
CPPUNIT_ASSERT_MESSAGE(
"stringset_to_strarr(...) result has not item \"world\"",
res.find("world") != res.end());
strarr_free(arr);
}
void NutClientTest::test_stringvector_to_strarr()
{
std::vector<std::string> strset;
strset.push_back("test");
strset.push_back("hello");
strset.push_back("world");
strarr arr = stringvector_to_strarr(strset);
CPPUNIT_ASSERT_MESSAGE(
"stringvector_to_strarr(...) result is null",
arr != nullptr);
char** ptr = arr;
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"stringvector_to_strarr(...) result has not item 0==\"test\"",
std::string("test"), std::string(*ptr));
++ptr;
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"stringvector_to_strarr(...) result has not item 1==\"hello\"",
std::string("hello"), std::string(*ptr));
++ptr;
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"stringvector_to_strarr(...) result has not item 2==\"world\"",
std::string("world"), std::string(*ptr));
++ptr;
/* https://stackoverflow.com/a/12565009/4715872
* Can not compare nullptr_t and another data type (char*)
* with CPPUNIT template assertEquals()
*/
CPPUNIT_ASSERT_MESSAGE(
"stringvector_to_strarr(...) result has not only 3 items",
nullptr == *ptr);
strarr_free(arr);
}
void NutClientTest::test_copy_constructor_dev() {
nut::TcpClient c;
nut::Device i(&c, "ups1");
nut::Device j(i);
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Failed to assign value of Device variable j by initializing from i",
i, j);
}
void NutClientTest::test_copy_assignment_dev() {
nut::TcpClient c;
nut::Device i(&c, "ups1");
nut::Device j(nullptr, "ups2");
CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE(
"Device variables i and j were initialized differently "
"but claim to be equal",
CPPUNIT_ASSERT_EQUAL(i, j) );
j = i;
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Failed to assign value of Device Command j by equating to i",
i, j);
}
void NutClientTest::test_copy_constructor_cmd() {
nut::TcpClient c;
nut::Device d(nullptr, "ups1");
nut::Command i(&d, "cmd1");
nut::Command j(i);
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Failed to assign value of Command variable j by initializing from i",
i, j);
}
void NutClientTest::test_copy_assignment_cmd() {
nut::TcpClient c;
nut::Device d(nullptr, "ups1");
nut::Command i(&d, "var1");
nut::Command j(nullptr, "var2");
CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE(
"Command variables i and j were initialized differently "
"but claim to be equal",
CPPUNIT_ASSERT_EQUAL(i, j) );
j = i;
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Failed to assign value of Command variable j by equating to i",
i, j);
}
void NutClientTest::test_copy_constructor_var() {
nut::TcpClient c;
nut::Device d(nullptr, "ups1");
nut::Variable i(&d, "var1");
nut::Variable j(i);
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Failed to assign value of Variable variable j by initializing from i",
i, j);
}
void NutClientTest::test_copy_assignment_var() {
nut::TcpClient c;
nut::Device d(nullptr, "ups1");
nut::Variable i(&d, "var1");
nut::Variable j(nullptr, "var2");
CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE(
"Variable variables i and j were initialized differently "
"but claim to be equal",
CPPUNIT_ASSERT_EQUAL(i, j) );
j = i;
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Failed to assign value of Variable variable j by equating to i",
i, j);
}
void NutClientTest::test_nutclientstub_dev() {
bool noException = true;
nut::MemClientStub c;
nut::Device d(nullptr, "ups_1");
try
{
// set mono value
c.setDeviceVariable("ups_1", "name_1", "value_1");
// get mono value
ListValue values = c.getDeviceVariableValue("ups_1", "name_1");
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: mono wrong values number",
values.size() == 1);
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: mono bad value",
values[0] == std::string("value_1"));
// set multi value
ListValue values_multi = { "multi_1", "multi_2" };
c.setDeviceVariable("ups_1", "name_multi_1", values_multi);
// get multi value
values = c.getDeviceVariableValue("ups_1", "name_multi_1");
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: multi wrong values number",
values.size() == 2);
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: multi first bad value",
values[0] == std::string("multi_1"));
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: multi second bad value",
values[1] == std::string("multi_2"));
// get object values
ListObject objects = c.getDeviceVariableValues("ups_1");
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: objects wrong values number",
objects.size() == 2);
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: objects mono wrong values number",
objects["name_1"].size() == 1);
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: objects mono bad value",
objects["name_1"][0] == std::string("value_1"));
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: objects multi wrong values number",
objects["name_multi_1"].size() == 2);
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: objects mono bad value",
objects["name_multi_1"][0] == std::string("multi_1"));
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: objects mono bad value",
objects["name_multi_1"][1] == std::string("multi_2"));
// get device values
std::set<std::string> devices_name = { "ups_1" };
ListDevice devices = c.getDevicesVariableValues(devices_name);
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: devices wrong values number",
devices.size() == 1);
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: devices mono wrong values number",
devices["ups_1"]["name_1"].size() == 1);
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: devices mono bad value",
devices["ups_1"]["name_1"][0] == std::string("value_1"));
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: devices multi wrong values number",
devices["ups_1"]["name_multi_1"].size() == 2);
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: devices mono bad value",
devices["ups_1"]["name_multi_1"][0] == std::string("multi_1"));
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: devices mono bad value",
devices["ups_1"]["name_multi_1"][1] == std::string("multi_2"));
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: throw exception",
noException);
// List of functions not implemented (should return exception)
noException = true;
try {
std::set<std::string> cmd = c.getDeviceCommandNames("ups-1");
CPPUNIT_ASSERT_MESSAGE(
"Variable not use",
cmd.size() == 0);
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: throw no exception",
!noException);
noException = true;
try {
std::string desc = c.getDeviceCommandDescription("ups-1", "cmd-1");
CPPUNIT_ASSERT_MESSAGE(
"Variable not use",
desc.empty());
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: throw no exception",
!noException);
noException = true;
try {
TrackingID id = c.executeDeviceCommand("ups-1", "cmd-1", "param-1");
CPPUNIT_ASSERT_MESSAGE(
"Variable not use",
id.empty());
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: throw no exception",
!noException);
noException = true;
try {
c.deviceLogin("ups-1");
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: throw no exception",
!noException);
noException = true;
try {
c.deviceMaster("ups-1");
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: throw no exception",
!noException);
noException = true;
try {
c.deviceForcedShutdown("ups-1");
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: throw no exception",
!noException);
noException = true;
try {
c.deviceGetNumLogins("ups-1");
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: throw no exception",
!noException);
noException = true;
try {
TrackingResult result = c.getTrackingResult("track-1");
CPPUNIT_ASSERT_MESSAGE(
"Variable not use",
result == TrackingResult::SUCCESS);
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: throw no exception",
!noException);
noException = true;
try {
bool status = c.isFeatureEnabled(Feature("feature-1"));
CPPUNIT_ASSERT_MESSAGE(
"Variable not use",
!status);
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: throw no exception",
!noException);
noException = true;
try {
c.setFeature(Feature("feature-1"), true);
}
catch(nut::NutException& ex)
{
NUT_UNUSED_VARIABLE(ex);
noException = false;
}
CPPUNIT_ASSERT_MESSAGE(
"Failed stub tcp client: throw no exception",
!noException);
}
} // namespace nut {}
#if (defined __clang__) && (defined HAVE_PRAGMA_CLANG_DIAGNOSTIC_IGNORED_DEPRECATED_DECLARATIONS)
# pragma clang diagnostic pop
#endif
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_EXIT_TIME_DESTRUCTORS || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_GLOBAL_CONSTRUCTORS || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_DEPRECATED_DECLARATIONS || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_SUGGEST_OVERRIDE_BESIDEFUNC || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_SUGGEST_DESTRUCTOR_OVERRIDE_BESIDEFUNC || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_WEAK_VTABLES_BESIDEFUNC || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_DEPRECATED_DYNAMIC_EXCEPTION_SPEC_BESIDEFUNC || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_EXTRA_SEMI_BESIDEFUNC || defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_OLD_STYLE_CAST_BESIDEFUNC)
# pragma GCC diagnostic pop
#endif
|