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
|
// -*- c++ -*-
// Generated by assa-genesis
//------------------------------------------------------------------------------
// $Id: echoxdr_testc.cpp,v 1.12 2006/07/26 00:27:32 vlg Exp $
//------------------------------------------------------------------------------
// EchoXDR_Client_Test.cpp
//------------------------------------------------------------------------------
// Copyright (c) 1999,2005 by Vladislav Grinchenko
//
// Permission to use, copy, modify, and distribute this software
// and its documentation for any purpose and without fee is hereby
// granted, provided that the above copyright notice appear in all
// copies. The author makes no representations about the suitability
// of this software for any purpose. It is provided "as is" without
// express or implied warranty.
//------------------------------------------------------------------------------
// Date: Jul 12, 1999
//------------------------------------------------------------------------------
static const char help_msg[]=
" \n"
" NAME: \n"
" \n"
" echoxdr_testc - test XDR encoding/decoding. \n"
" \n"
" DESCRIPTION: \n"
" \n"
" This is the client side of the test intended to exercise IPv4Socket \n"
" class' I/O capabilities to transfer XDR-encoded basic data types. \n"
" \n"
" The test is two-stage: \n"
" \n"
" 1) Connect to the daytime server on [hostname] and read current time. \n"
" \n"
" 2) Connect to 'echoxdr_tests' server running on $ASSAPORT@localhost \n"
" or $ASSAPORT@hostname and send/receive/compare data values of every \n"
" basic type IPv4Socket can handle. \n"
" \n"
" If [hostname] is ommited, localhost is assumed. \n"
" \n"
" USAGE: \n"
" \n"
" shell> echoxdr_testc [OPTIONS] \n"
" \n"
" OPTIONS: \n"
" \n"
" --daytime-host NAME - Hostname of 'daytime' service \n"
" --build-dir STRING - Directory where executables are located. \n"
" \n"
" -D, --log-file NAME - Write debug to NAME file \n"
" -d, --log-stdout - Write debug to standard output \n"
" -z, --log-size NUM - Maximum size debug file can reach (dfl: is 10Mb) \n"
" \n"
" -m, --mask MASK - Mask (default: ALL = 0x7fffffff) \n"
" -p, --port NAME - The tcp/ip port NAME (default - procname) \n"
" \n"
" -h, --help - Print this messag \n"
" -v, --version - Print version number \n";
//------------------------------------------------------------------------------
#if !defined (WIN32)
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/utsname.h> // uname(2)
#include <netdb.h> // gethostbyname(3)
#include <unistd.h> // getcwd(3)
#include <iostream>
#include <vector>
#include <string>
using std::string;
#include <assa/GenServer.h>
#include <assa/Singleton.h>
#include <assa/TimeVal.h>
#include <assa/Fork.h>
#include <assa/IPv4Socket.h>
#include <assa/INETAddress.h>
#include <assa/MemDump.h>
#include <assa/CommonUtils.h>
using namespace ASSA;
/*******************************************************************************
Class EchoXDR_Client_Test
*******************************************************************************/
class EchoXDR_Client_Test :
public GenServer,
public Singleton<EchoXDR_Client_Test>
{
public:
EchoXDR_Client_Test ();
virtual void init_service ();
virtual void process_events ();
int get_exit_value () const { return m_exit_value; }
void set_exit_value (int v_) { m_exit_value = v_; }
string get_build_dir () const { return m_build_dir; }
private:
int m_exit_value; // Return status of the process
string m_daytime_host;
string m_my_hostname;
string m_build_dir;
};
/* Useful definitions */
#define CLIENT EchoXDR_Client_Test::get_instance()
#define REACTOR CLIENT->get_reactor()
// Static declarations mandated by Singleton class
ASSA_DECL_SINGLETON(EchoXDR_Client_Test);
/*******************************************************************************
Local helpers
*******************************************************************************/
template <class T>
void
compare_and_report (T in_, T out_)
{
if (in_ == out_) {
std::cout << " ok";
}
else {
std::cout << " error:\n";
std::cout << "\tdata in (lhs) = " << in_ << '\n'
<< "\tdata out (rhs) = " << out_ << std::endl;
CLIENT->set_exit_value (1);
}
std::cout << std::endl;
}
void
dump_var (const char* annotation_, std::string& var_)
{
MemDump::dump_to_log (APP, annotation_, var_.c_str (), var_.length ());
}
template <class T>
void
dump_var (const char* annotation_, T& var_)
{
MemDump::dump_to_log (APP, annotation_,
reinterpret_cast<const char*>(&var_),
sizeof (T));
}
/*******************************************************************************
Class EchoXDR_Client_Test member functions
*******************************************************************************/
EchoXDR_Client_Test::
EchoXDR_Client_Test () :
m_exit_value (0)
{
add_opt (0, "daytime-host", &m_daytime_host);
add_opt (0, "build-dir", &m_build_dir);
// ---Configuration---
rm_opt ('f', "config-file" );
rm_opt ('n', "instance" );
// ---Process bookkeeping---
rm_opt ('b', "daemon" );
rm_opt ('l', "pidfile" );
rm_opt ('L', "ommit-pidfile");
/*---
* Disable all debugging
*---*/
// m_debug_mask = 0x0;
m_log_file = "echoxdr_testc.log";
}
void
EchoXDR_Client_Test::
init_service ()
{
trace("Client::init_service");
Log::disable_timestamp ();
/* Get fully-quialified host name */
struct utsname u;
::uname (&u);
struct hostent* hent_ptr;
hent_ptr = ::gethostbyname (u.nodename);
m_my_hostname = hent_ptr->h_name;
DL((APP,"my_hostname = \"%s\"\n", m_my_hostname.c_str ()));
/* Figure out what directory we have been started from. */
if (m_build_dir.length () == 0) {
m_build_dir = ASSA::Utils::get_cwd_name ();
}
DL((APP,"build-dir = \"%s\"\n", m_build_dir.c_str ()));
DL((APP,"Service \"%s\" has been initialized\n",
get_cmdline_name ().c_str ()));
}
void
EchoXDR_Client_Test::
process_events ()
{
static const char self[]="Client::process_events";
trace(self);
std::cout << "= Running echoxdr Test =\n";
//------------------------------------------------------------------------------
// Test 1: Connect to date server and get current time
//
// We try all possible combinations:
//
// No. Host Port Action Note
// --- ---------------- ----------- ------------------------- ------------------
// 1 --daytime-host "daytime" connect() remote
// --- ---------------- ----------- ------------------------- ------------------
// 2 gethostbyname(3) "daytime" connect() local
// --- ---------------- ----------- ------------------------- ------------------
// 3 gethostbyname(3) $ASSAPORT fork()/exec()/connect() local replacmnt
// --- ---------------- ----------- ------------------------- ------------------
// 4 gethostbyname(3) 10000 fork()/exec()/connect() local replacmnt
//------------------------------------------------------------------------------
DL((APP,"===============================\n"));
DL((APP,"| Test 1: |\n"));
DL((APP,"| |\n"));
DL((APP,"| Read time from daytime ... |\n"));
DL((APP,"| |\n"));
DL((APP,"===============================\n"));
std::cout << ">>> Testing ability to read input from daytime ..."
<< std::flush;
std::string assaport;
char* ap;
char page [4096];
IPv4Socket stream;
std::vector<INETAddress> address_list;
DL((APP,"1. --daytime-host, daytime\n"));
INETAddress dt_1 (m_daytime_host.c_str (), "daytime");
address_list.push_back (dt_1);
DL((APP,"2. gethostbyname, daytime\n"));
INETAddress dt_2 (m_my_hostname.c_str (), "daytime");
address_list.push_back (dt_2);
if ((ap = ::getenv("ASSAPORT")) != 0) {
assaport = ap;
}
if (assaport.length () == 0) {
assaport = "10000";
}
/* [$ASSAPORT|10000]@my-fully-quaified-host-name
*/
assaport += "@" + m_my_hostname;
DL((APP,"3./4. gethostbyname, $ASSAPORT/%s\n", assaport.c_str ()));
INETAddress dt_3 (assaport.c_str ());
address_list.push_back (dt_3);
Assure_exit (stream.open (AF_INET));
/*
* By default, IPv4Socket is in non-blocking mode. We switch
* to the blocking mode to get blocking connection complete.
* Otherwise, connect() returns EINPROGRESS.
*/
stream.turnOptionOff (Socket::nonblocking);
std::string exec_name;
int j = 0;
int ret = 0;
std::vector<INETAddress>::iterator cit = address_list.begin ();
while (cit != address_list.end ()) {
DL((APP,"[%02d] Trying address: %d@%s\n", j+1,
(*cit).getPort (), (*cit).getHostName ().c_str ()));
(*cit).dump ();
if (j == 2) { // fork daytime replacement
DL((APP,"Starting daytime replacement server ...\n"));
exec_name = CLIENT->get_build_dir () + "/daytime";
Fork f (Fork::KILL_ON_EXIT, Fork::IGNORE_STATUS);
if (f.isChild ()) {
ret = execlp (exec_name.c_str (),
exec_name.c_str (),
"--log-file=daytime.log", "--mask=0x2",
"--one-shot",
NULL);
// point of no return
if (ret == -1) {
EL((ASSA::ASSAERR,"execlp(\"daytime\") failed\n"));
}
Assure_exit (false);
}
DL((APP,"Wait for daytime to come up\n"));
ASSA::Utils::sleep_for_seconds (1);
}
if (stream.connect (*cit)) {
DL((APP,"Connection established with daytime service.\n"));
break;
}
j++, cit++;
}
if (cit == address_list.end ()) {
std::cout << "Test failed\n";
exit (1);
}
while ( stream && stream.read (page, 4096) > 0 ) {
DL((APP,"Got the date: \"%s\"\n", page));
}
stream.close();
std::cout << " ok\n";
//------------------------------------------------------------------------------
// Test 2: Connect to echoxdr server, send base types,
// receive replies and compare sent and received values
//------------------------------------------------------------------------------
DL((APP,"===============================\n"));
DL((APP,"| Test 2: |\n"));
DL((APP,"| |\n"));
DL((APP,"| Starting echoxdr server ... |\n"));
DL((APP,"| |\n"));
DL((APP,"===============================\n"));
exec_name = CLIENT->get_build_dir () + "/echoxdr_tests";
Fork sf (Fork::KILL_ON_EXIT, Fork::IGNORE_STATUS);
if (sf.isChild ()) {
ret = execlp (exec_name.c_str (),
exec_name.c_str (),
"--log-file=echoxdr_tests.log", "--mask=0x7fffffff",
"--port", assaport.c_str (),
NULL);
// point of no return
if (ret == -1) {
EL((ASSA::ASSAERR,"execlp(\"echoxdr_tests\") failed\n"));
}
exit (1);
}
DL((APP,"Wait for echoxdr server to come up\n"));
ASSA::Utils::sleep_for_seconds (1);
assert (stream.open (AF_INET));
stream.turnOptionOff (Socket::nonblocking);
stream.connect (dt_3);
DL((APP,"Connection established with echoxdr server\n"));
bool b = true, br;
/*----------------------------------------------------------------------*/
char c = 'G', cr;
std::cout << ">>> Testing char c = \""<< c << "\" ..." << std::flush;
dump_var ("char sent", c);
stream << c << flush;
stream >> cr;
dump_var ("char received", cr);
compare_and_report (c, cr);
/*----------------------------------------------------------------------*/
// 01234578901234578901234578901
std::string s = "ASSA is a pretty cool library";
std::string sr;
std::cout << ">>> Testing string = \""<< s << "\" ..." << std::flush;
dump_var ("string sent", s);
stream << s << flush;
stream >> sr;
dump_var ("string received", sr);
compare_and_report (s, sr);
/*----------------------------------------------------------------------*/
short sh = 120, shr;
std::cout << ">>> Testing short sh = "<< sh <<" ..." << std::flush;
dump_var ("short sent", sh);
stream << sh << flush;
stream >> shr;
dump_var ("short received", shr);
compare_and_report (sh, shr);
/*----------------------------------------------------------------------*/
int i = -210, ir;
std::cout << ">>> Testing int i="<< i << " ..." << std::flush;
dump_var ("int sent", i);
stream << i << flush;
stream >> ir;
dump_var ("int received", ir);
compare_and_report (i, ir);
/*----------------------------------------------------------------------*/
u_int ui = 32, uir;
std::cout << ">>> Testing u_int ui="<< ui <<" ...";
dump_var ("u_int sent", ui);
stream << ui << flush;
stream >> uir;
dump_var ("u_int received", uir);
compare_and_report (ui, uir);
/*----------------------------------------------------------------------*/
long l = INT_MIN + 1, lr;
std::cout << ">>> Testing long l="<< l <<" ...";
dump_var ("long sent", l);
stream << l << flush;
stream >> lr;
dump_var ("long received", lr);
compare_and_report (l, lr);
/*----------------------------------------------------------------------*/
u_long ul = INT_MAX - 1, ulr;
std::cout << ">>> Testing u_long ul=" << ul << " ...";
dump_var ("u_long sent", ul);
stream << ul << flush;
stream >> ulr;
dump_var ("u_long received", ulr);
compare_and_report (ul, ulr);
/*----------------------------------------------------------------------*/
float f = 123.4334453545, fr;
std::cout << ">>> Testing float f=" << f << " ...";
dump_var ("float sent", f);
stream << f << flush;
stream >> fr;
dump_var ("float received", fr);
compare_and_report (f, fr);
/*----------------------------------------------------------------------*/
double d = -123456678.234324234, dr;
std::cout << ">>> Testing double d=" << d << " ...";
dump_var ("double sent", d);
stream << d << flush;
stream >> dr;
dump_var ("double received", dr);
compare_and_report (d, dr);
/*----------------------------------------------------------------------*/
stream.close ();
if (CLIENT->get_exit_value ()) {
std::cout << "Test failed";
}
else {
std::cout << "Test passed";
}
std::cout << std::endl;
m_reactor.stopReactor ();
DL((APP,"Service stopped!\n"));
}
#endif /* !defined WIN32 */
/*******************************************************************************
Main
*******************************************************************************/
int
main (int argc, char* argv[])
{
#if !defined (WIN32)
static const char release[] = "VERSION";
int patch_level = 0;
CLIENT->set_version (release, patch_level);
CLIENT->set_author ("Vladislav Grinchenko");
CLIENT->set_flags (GenServer::RMLOG);
CLIENT->init (&argc, argv, help_msg);
CLIENT->init_service ();
CLIENT->process_events ();
return CLIENT->get_exit_value ();
#endif /* !def WIN32 */
return 0;
}
|