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
|
/* cpputest-client - CppUnit libnutclient active test
Module for NUT `cpputest` runner, to check client-server interactions
as part of NIT (NUT Integration Testing) suite and similar scenarios.
This is an "active" NUT client talking to an `upsd` on $NUT_PORT,
as opposed to nutclienttest.cpp which unit-tests the class API etc.
in isolated-binary fashion.
Copyright (C)
2022-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"
#include <stdexcept>
#include <cstdint>
#include <cstdlib>
#include <stdlib.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 NutActiveClientTest : public CppUnit::TestFixture
{
/* Note: is "friend" of nut::TcpClient class
* to test a few of its protected methods */
CPPUNIT_TEST_SUITE( NutActiveClientTest );
CPPUNIT_TEST( test_query_ver );
CPPUNIT_TEST( test_list_ups );
CPPUNIT_TEST( test_list_ups_clients );
CPPUNIT_TEST( test_auth_user );
CPPUNIT_TEST( test_auth_primary );
CPPUNIT_TEST_SUITE_END();
private:
/* Fed by caller via envvars: */
uint16_t NUT_PORT = 0;
std::string NUT_USER = "";
std::string NUT_PASS = "";
std::string NUT_PRIMARY_DEVICE = "";
std::string NUT_SETVAR_DEVICE = "";
public:
void setUp() override;
void tearDown() override;
void test_query_ver();
void test_list_ups();
void test_list_ups_clients();
void test_auth_user();
void test_auth_primary();
};
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( NutActiveClientTest );
} // 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 NutActiveClientTest::setUp()
{
/* NUT_PORT etc. are provided by external test suite driver */
char * s;
s = std::getenv("NUT_PORT");
if (s) {
long l = atol(s);
if (l < 1 || l > 65535) {
throw std::runtime_error("NUT_PORT specified by caller is out of range");
}
NUT_PORT = static_cast<uint16_t>(l);
} else {
throw std::runtime_error("NUT_PORT not specified by caller, NIT should call this test");
}
s = std::getenv("NUT_USER");
if (s) {
NUT_USER = s;
} // else stays empty
s = std::getenv("NUT_PASS");
if (s) {
NUT_PASS = s;
} // else stays empty
s = std::getenv("NUT_PRIMARY_DEVICE");
if (s) {
NUT_PRIMARY_DEVICE = s;
} // else stays empty
s = std::getenv("NUT_SETVAR_DEVICE");
if (s) {
NUT_SETVAR_DEVICE = s;
} // else stays empty
}
void NutActiveClientTest::tearDown()
{
}
void NutActiveClientTest::test_query_ver() {
nut::TcpClient c("localhost", NUT_PORT);
std::string s;
std::cerr << "[D] C++ NUT Client lib test running against Data Server at: "
<< c.getHost() << ':' << c.getPort() << std::endl;
CPPUNIT_ASSERT_MESSAGE(
"TcpClient is not connected after constructor",
c.isConnected());
/* Note: generic client code can not use protected methods
* like low-level sendQuery(), list(), get() and some more,
* but this NutActiveClientTest is a friend of TcpClient:
*/
s = c.sendQuery("VER");
std::cerr << "[D] Got Data Server VER: " << s << std::endl;
try {
s = c.sendQuery("PROTVER");
std::cerr << "[D] Got PROTVER: " << s << std::endl;
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Did not get PROTVER: " << ex.what() << std::endl;
}
try {
s = c.sendQuery("NETVER");
std::cerr << "[D] Got NETVER (obsolete): " << s << std::endl;
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Did not get NETVER (obsolete): " << ex.what() << std::endl;
}
try {
c.logout();
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Could not get LOGOUT: " << ex.what() << std::endl;
}
try {
c.disconnect();
}
catch(nut::NutException& ex)
{
/* NUT_UNUSED_VARIABLE(ex); */
std::cerr << "[D] Could not get disconnect(): " << ex.what() << std::endl;
}
}
void NutActiveClientTest::test_list_ups() {
nut::TcpClient c("localhost", NUT_PORT);
std::set<std::string> devs;
bool noException = true;
try {
devs = c.getDeviceNames();
std::cerr << "[D] Got device list (" << devs.size() << "): [";
for (std::set<std::string>::iterator it = devs.begin();
it != devs.end(); it++
) {
if (it != devs.begin()) {
std::cerr << ", ";
}
std::cerr << '"' << *it << '"';
}
std::cerr << "]" << std::endl;
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Could not device list: " << ex.what() << std::endl;
noException = false;
}
c.logout();
c.disconnect();
CPPUNIT_ASSERT_MESSAGE(
"Failed to list UPS with TcpClient: threw NutException",
noException);
}
void NutActiveClientTest::test_list_ups_clients() {
nut::TcpClient c("localhost", NUT_PORT);
std::map<std::string, std::set<std::string>> deviceClients;
bool noException = true;
try {
c.authenticate(NUT_USER, NUT_PASS);
std::cerr << "[D] Authenticated without exceptions" << std::endl;
/* Note: no high hopes here, credentials are checked by server
* when running critical commands, not at auth request itself */
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Could not authenticate as a simple user: " << ex.what() << std::endl;
/* no failure here */
}
try {
c.deviceLogin(NUT_PRIMARY_DEVICE);
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Could not log into primary device (envvars not set?) so test below should return empty client lists: " << ex.what() << std::endl;
/* no failure here */
}
try {
deviceClients = c.listDeviceClients();
std::cerr << "[D] Got device client list (" << deviceClients.size() << "): [";
for (std::map<std::string, std::set<std::string>>::iterator itM = deviceClients.begin();
itM != deviceClients.end(); itM++
) {
if (itM != deviceClients.begin()) {
std::cerr << "," << std::endl;
}
std::cerr << "{ \"" << itM->first << "\" : ";
for (std::set<std::string>::iterator it = itM->second.begin();
it != itM->second.end(); it++
) {
if (it != itM->second.begin()) {
std::cerr << ", ";
}
std::cerr << '"' << *it << '"';
}
std::cerr << " }";
}
std::cerr << " ]" << std::endl;
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Could not device client list: " << ex.what() << std::endl;
noException = false;
}
c.logout();
c.disconnect();
CPPUNIT_ASSERT_MESSAGE(
"Failed to list UPS with TcpClient: threw NutException",
noException);
}
void NutActiveClientTest::test_auth_user() {
if (NUT_USER.empty()) {
std::cerr << "[D] SKIPPING test_auth_user()" << std::endl;
return;
}
nut::TcpClient c("localhost", NUT_PORT);
bool noException = true;
try {
c.authenticate(NUT_USER, NUT_PASS);
std::cerr << "[D] Authenticated without exceptions" << std::endl;
/* Note: no high hopes here, credentials are checked by server
* when running critical commands, not at auth request itself */
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Could not authenticate as a simple user: " << ex.what() << std::endl;
noException = false;
}
if (!NUT_SETVAR_DEVICE.empty()) {
try {
TrackingResult tres;
TrackingID tid;
int i;
std::string nutVar = "ups.status"; /* Has a risk of flip-flop with NIT dummy setup */
std::string s1 = c.getDeviceVariableValue(NUT_SETVAR_DEVICE, nutVar)[0];
std::string sTest = s1 + "-test";
std::cerr << "[D] Got initial device '" << NUT_SETVAR_DEVICE
<< "' variable '" << nutVar << "' value: " << s1 << std::endl;
CPPUNIT_ASSERT_MESSAGE(
"Did not expect empty value here",
!s1.empty());
tid = c.setDeviceVariable(NUT_SETVAR_DEVICE, nutVar, sTest);
while ( (tres = c.getTrackingResult(tid)) == PENDING) {
usleep(100);
}
if (tres != SUCCESS) {
std::cerr << "[D] Failed to set device variable: "
<< "tracking result is " << tres << std::endl;
noException = false;
}
/* Check what we got after set */
/* Note that above we told the server to tell the driver
* to set a dstate entry; below we ask the server to ask
* the driver and relay the answer to us. The dummy-ups
* driver may also be in a sleeping state between cycles.
* Data propagation may be not instantaneous, so we loop
* for a while to see the expected value (or give up).
*/
std::string s2;
for (i = 0; i < 100 ; i++) {
s2 = c.getDeviceVariableValue(NUT_SETVAR_DEVICE, nutVar)[0];
if (s2 == sTest)
break;
usleep(100000);
}
std::cerr << "[D] Read back: " << s2
<< " after " << (100 * i) << "msec"
<< std::endl;
/* Fix it back */
tid = c.setDeviceVariable(NUT_SETVAR_DEVICE, nutVar, s1);
while ( (tres = c.getTrackingResult(tid)) == PENDING) {
usleep(100);
}
if (tres != SUCCESS) {
std::cerr << "[D] Failed to set device variable: "
<< "tracking result is " << tres << std::endl;
noException = false;
}
std::string s3;
for (i = 0; i < 100 ; i++) {
s3 = c.getDeviceVariableValue(NUT_SETVAR_DEVICE, nutVar)[0];
if (s3 == s1)
break;
usleep(100000);
}
std::cerr << "[D] Read back: " << s3
<< " after " << (100 * i) << "msec"
<< std::endl;
if (s3 != s1) {
std::cerr << "[D] Final device variable value '" << s3
<< "' differs from original '" << s1
<< "'" << std::endl;
noException = false;
}
if (s2 == s1) {
std::cerr << "[D] Tweaked device variable value '" << s2
<< "' does not differ from original '" << s1
<< "'" << std::endl;
noException = false;
}
if (noException) {
std::cerr << "[D] Tweaked device variable value OK" << std::endl;
}
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Failed to set device variable: "
<< ex.what() << std::endl;
noException = false;
}
} else {
std::cerr << "[D] SKIPPING test_auth_user() active test "
<< "(got no NUT_SETVAR_DEVICE to poke)" << std::endl;
}
c.logout();
c.disconnect();
CPPUNIT_ASSERT_MESSAGE(
"Failed to auth as user with TcpClient or tweak device variable",
noException);
}
void NutActiveClientTest::test_auth_primary() {
if (NUT_USER.empty() || NUT_PRIMARY_DEVICE.empty()) {
std::cerr << "[D] SKIPPING test_auth_primary()" << std::endl;
return;
}
nut::TcpClient c("localhost", NUT_PORT);
bool noException = true;
try {
c.authenticate(NUT_USER, NUT_PASS);
std::cerr << "[D] Authenticated without exceptions" << std::endl;
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Could not authenticate as an upsmon primary user: "
<< ex.what() << std::endl;
noException = false;
}
try {
Device d = c.getDevice(NUT_PRIMARY_DEVICE);
bool gotPrimary = false;
bool gotMaster = false;
try {
c.deviceMaster(NUT_PRIMARY_DEVICE);
gotMaster = true;
std::cerr << "[D] Elevated as MASTER without exceptions" << std::endl;
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Could not elevate as MASTER for "
<< "NUT_PRIMARY_DEVICE " << NUT_PRIMARY_DEVICE << ": "
<< ex.what() << std::endl;
}
try {
c.devicePrimary(NUT_PRIMARY_DEVICE);
gotPrimary = true;
std::cerr << "[D] Elevated as PRIMARY without exceptions" << std::endl;
}
catch(nut::NutException& ex)
{
std::cerr << "[D] Could not elevate as PRIMARY for "
<< "NUT_PRIMARY_DEVICE " << NUT_PRIMARY_DEVICE << ": "
<< ex.what() << std::endl;
}
if (!gotMaster && !gotPrimary)
noException = false;
}
catch(nut::NutException& ex)
{
std::cerr << "[D] NUT_PRIMARY_DEVICE " << NUT_PRIMARY_DEVICE
<< " not found on Data Server: "
<< ex.what() << std::endl;
noException = false;
}
c.logout();
c.disconnect();
CPPUNIT_ASSERT_MESSAGE(
"Failed to auth as user with TcpClient: threw NutException",
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
|