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
|
// -*- c++ -*-
//------------------------------------------------------------------------------
// inet_address_test.cpp
//------------------------------------------------------------------------------
// $Id: inet_address_test.cpp,v 1.6 2006/07/20 02:30:56 vlg Exp $
//------------------------------------------------------------------------------
// Copyright (c) 2002,2004 by Vladislav Grinchenko
//
// 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.
//------------------------------------------------------------------------------
#include <vector>
#include <iostream>
using namespace std;
#include <assert.h>
#if !defined (WIN32)
# include <sys/utsname.h>
#endif
#include "assa/Logger.h"
#include "assa/INETAddress.h"
using namespace ASSA;
typedef std::vector<std::string>::const_iterator AliasesCIter;
std::vector<std::string> g_aliases;
void print_host_and_port (INETAddress& a_)
{
DL((APP,"\n"
"\tHost: \"%s\"\n"
"\tPort: %d\n",
a_.getHostName ().c_str (),
a_.getPort ()));
a_.dump ();
}
void log_test_header (u_int& index_, const char* message_)
{
DL ((ALL,"\n=======================================\n"));
DL((ALL,"TEST %d\n", index_));
cout << "Test " << index_ << " ...";
DL((ALL, "%d) %s\n", index_, message_));
index_++;
}
// Match the hostname_ against aliases list
// Return: true if match exists, false otherwise
bool
match_with_aliases (const std::string& hostname_)
{
if (g_aliases.size () == 0) {
return false;
}
AliasesCIter citer = g_aliases.begin ();
while (citer != g_aliases.end ()) {
if (*citer == hostname_) {
return true;
}
citer++;
}
return false;
}
int main (int argc, char* argv[])
{
char* self = argv[0];
u_int index = 1; // test counter
string logname (self);
logname += ".log";
unlink (logname.c_str());
Log::open_log_file (logname.c_str());
std::cout << "= Running inet_address_test Test =\n";
/* We are after fully-qualified host name. Kernel (uname(2)) knows
* nothing about network and cannot give us our DNS name.
* gethostbyname(3), however, can. It is equivalent to calling
* dnshostname (1).
*/
string hostname (INETAddress::get_fully_qualified_domain_name (g_aliases));
DL((ALL,"My host FQDN \"%s\"\n", hostname.c_str ()));
DL((ALL,"My host aliases:\n"));
if (g_aliases.size () == 0) {
DL((ALL," no aliases\n"));
}
else {
AliasesCIter it = g_aliases.begin ();
while (it != g_aliases.end ()) {
DL((ALL," \"%s\"\n", (*it).c_str ()));
it++;
}
DL((ALL," end-of-list\n"));
}
const int port = 18;
int errors = 0;
// string lochost ("localhost.localdomain");
string lochost ("localhost");
/**********************************************************************
* Test 1
*/
log_test_header (index, "create address with default constructor");
INETAddress addr1;
cout << " ok\n";
/**********************************************************************
* Test 2
*/
log_test_header (index, "create address from (host, port) pair");
DL((APP," (\"%s\", %d)\n", hostname.c_str (), port));
INETAddress addr2 (hostname.c_str (), port);
addr2.dump ();
DL((APP,"getHostName () = \"%s\"\n", addr2.getHostName ().c_str ()));
DL((ALL,"getPort () = %d\n", addr2.getPort ()));
if (addr2.getPort () == port &&
(addr2.getHostName () == lochost ||
addr2.getHostName () == hostname))
{
cout << " ok\n";
}
else {
cout << " failed!\n"; errors++;
}
print_host_and_port (addr2);
/**********************************************************************
* Test 3
*/
log_test_header (index,
"create address from service/tcp ('time','tcp') pair");
INETAddress addr3 ("time", INETAddress::TCP);
if (addr3.getPort () != 37) {
cout << " failed!\n"; errors++;
}
else {
cout << " ok\n";
}
print_host_and_port (addr3);
/**********************************************************************
* Test 4
*/
log_test_header (index, "create listen address from service ('time')");
INETAddress addr4 ("time");
if (addr4.getPort () != 37) {
cout << " failed!\n";
errors++;
}
else {
cout << " ok\n";
}
print_host_and_port (addr4);
/**********************************************************************
* Test 5
*/
log_test_header (index,
"create address from host:service (\"localhost:time\")");
INETAddress addr5 ("localhost:time");
if (addr5.getPort () != 37 || match_with_aliases (addr5.getHostName ())) {
cout << " failed!\n";
cout << "addr5.getPort() = " << addr5.getPort () << " (expected 37)\n"
<< "addr5.getHostName () = \"" << addr5.getHostName ()
<< "\" (expected one from the aliases list)\n";
errors++;
}
else {
cout << " ok\n";
}
print_host_and_port (addr5);
/**********************************************************************
* Test 6
*/
log_test_header (index, "create address from service[@hostname]");
DL((APP," = 'time@%s\n", hostname.c_str ()));
string isiaddr = "time@" + hostname;
INETAddress addr6 (isiaddr.c_str());
if (addr6.getPort () == 37 &&
(addr6.getHostName () == lochost || addr6.getHostName () == hostname))
{
cout << " ok\n";
}
else {
cout << " failed!\n";
errors++;
}
print_host_and_port (addr6);
/**********************************************************************
* Test 7
*/
log_test_header (index, "create address from 0@hostname (should fail)");
string nulladdr = "0@" + hostname;
INETAddress addr7 (nulladdr.c_str());
if (!addr7.bad()) { cout << "failed!\n"; errors++; }
else cout << " ok\n";
print_host_and_port (addr7);
/**********************************************************************
* Test 8
*/
log_test_header (index, "Address 10000@localhost should be ok");
INETAddress addr8 ("10000@localhost");
if (addr8) {
cout << " ok\n";
}
else {
cout << "failed\n";
errors++;
}
print_host_and_port (addr8);
/**********************************************************************
* Test 9
*/
log_test_header (index, "Address ('blahblah@localhost') should fail");
INETAddress addr9 ("blahblah@localhost");
if (addr9) {
cout << "failed!\n"; errors++;
}
else {
cout << " ok\n";
}
print_host_and_port (addr9);
/**********************************************************************
* Summary
*/
if (errors) {
cout << "Test failed! There were " << errors << " errors\n";
return 1;
}
cout << "Test completed successfully!\n";
return 0;
}
|