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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
|
//=============================================================================
/**
* @file INET_Addr_Test.cpp
*
* Performs several tests on the ACE_INET_Addr class. It creates several
* IPv4 and IPv6 addresses and checks that the address formed by the
* class is valid.
*
* @author John Aughey (jha@aughey.com)
*/
//=============================================================================
#include "test_config.h"
#include "ace/OS_NS_string.h"
#include "ace/INET_Addr.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_arpa_inet.h"
#include "ace/SString.h"
#include "ace/OS_NS_unistd.h"
// Make sure that ACE_Addr::addr_type_ is the same
// as the family of the inet_addr_.
static int check_type_consistency (const ACE_INET_Addr &addr)
{
int family = -1;
if (addr.get_type () == AF_INET)
{
struct sockaddr_in *sa4 = (struct sockaddr_in *)addr.get_addr();
family = sa4->sin_family;
}
#if defined (ACE_HAS_IPV6)
else if (addr.get_type () == AF_INET6)
{
struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)addr.get_addr();
family = sa6->sin6_family;
}
#endif
if (addr.get_type () != family)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Inconsistency between ACE_SOCK::addr_type_ (%d) ")
ACE_TEXT ("and the sockaddr family (%d)\n"),
addr.get_type (),
family));
return 1;
}
return 0;
}
static bool test_tao_use ()
{
char host[256];
if (::gethostname (host, 255) != 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Test TAO Use fail %p\n"),
ACE_TEXT ("gethostname")));
return false;
}
ACE_INET_Addr addr;
addr.set ((unsigned short)0, host);
ACE_CString full (host);
full += ":12345";
addr.set (full.c_str ());
u_short p = addr.get_port_number ();
if (p != 12345)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Test TAO Use expected port 12345 got %d\n"),
p));
return false;
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Test TAO Use passed\n")));
return true;
}
static bool test_port_assignment ()
{
#if defined (ACE_HAS_IPV6)
ACE_INET_Addr addr1 (static_cast<unsigned short> (0), ACE_IPV6_ANY, AF_INET6);
ACE_INET_Addr addr2;
addr1.set_port_number (12345);
addr2.set (addr1);
if (addr1.get_port_number () != addr2.get_port_number ())
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("port number not properly copied. ")
ACE_TEXT ("addr1 port = %d addr2 port = %d\n"),
addr1.get_port_number (), addr2.get_port_number ()));
return false;
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Test Port Assignment passed\n")));
#else
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Test Port Assignment is IPv6 only\n")));
#endif /* ACE_HAS_IPV6 */
return true;
}
static bool test_multiple ()
{
bool success = true;
// Check the behavior when there are multiple addresses assigned to a name.
// The NTP pool should always return multiples, though always different.
ACE_INET_Addr ntp;
if (ntp.set (123, ACE_TEXT ("pool.ntp.org")) == -1)
{
// This is just a warning to prevent fails on lookups on hosts with no
// DNS service. The real value of this test is to accurately get
// multiples from the result.
ACE_ERROR ((LM_WARNING, ACE_TEXT ("%p\n"), ACE_TEXT ("pool.ntp.org")));
return true;
}
size_t count = 0;
ACE_TCHAR addr_string[256];
do
{
++count; // If lookup succeeded, there's at least one
ntp.addr_to_string (addr_string, sizeof (addr_string));
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("IPv4 %B: %s\n"), count, addr_string));
}
while (ntp.next ());
success = count > 1;
#if defined (ACE_HAS_IPV6)
ACE_INET_Addr ntp6;
if (ntp6.set (123, ACE_TEXT ("2.pool.ntp.org"), 1, AF_INET6) == -1)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("2.pool.ntp.org")));
return false;
}
count = 0;
do
{
++count; // If lookup succeeded, there's at least one
ntp6.addr_to_string (addr_string, sizeof (addr_string));
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("IPv6 %B: %s\n"), count, addr_string));
}
while (ntp6.next ());
if (count <= 1)
success = false;
#endif /* ACE_HAS_IPV6 */
return success;
}
struct Address {
const char* name;
bool loopback;
};
int run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("INET_Addr_Test"));
int status = 0; // Innocent until proven guilty
// Try to set up known IP and port.
u_short port (80);
ACE_UINT32 const ia_any = INADDR_ANY;
ACE_INET_Addr local_addr(port, ia_any);
status |= check_type_consistency (local_addr);
if (local_addr.get_port_number () != 80)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Got port %d, expecting 80\n"),
(int)(local_addr.get_port_number ())));
status = 1;
}
if (local_addr.get_ip_address () != ia_any)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Mismatch on local IP addr\n")));
status = 1;
}
// Assignment constructor
ACE_INET_Addr local_addr2 (local_addr);
status |= check_type_consistency (local_addr2);
if (local_addr2.get_port_number () != 80)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Copy got port %d, expecting 80\n"),
(int)(local_addr2.get_port_number ())));
status = 1;
}
if (local_addr2.get_ip_address () != ia_any)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Mismatch on copy local IP addr\n")));
status = 1;
}
if (local_addr != local_addr2)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Copy local addr mismatch\n")));
status = 1;
}
// Try to parse out a simple address:port string. Intentionally reuse
// the ACE_INET_Addr to ensure resetting an address works.
const char *addr_ports[] =
{
"127.0.0.1:80", "www.dre.vanderbilt.edu:80", 0
};
ACE_INET_Addr addr_port;
for (int i = 0; addr_ports[i] != 0; ++i)
{
if (addr_port.set (addr_ports[i]) == 0)
{
status |= check_type_consistency (addr_port);
if (addr_port.get_port_number () != 80)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Got port %d from %s\n"),
(int)(addr_port.get_port_number ()),
addr_ports[i]));
status = 1;
}
ACE_INET_Addr check (addr_ports[i]);
if (addr_port != check)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Reset on iter %d failed\n"), i));
if (addr_port.get_type() != check.get_type()) {
ACE_ERROR ((LM_ERROR, ACE_TEXT (" addr_port.get_type()= %d, check.get_type()=%d\n")
, addr_port.get_type(), check.get_type()));
}
if (addr_port.get_size() != check.get_size()) {
ACE_ERROR ((LM_ERROR, ACE_TEXT (" addr_port.get_size()= %d, check.get_size()=%d\n")
, addr_port.get_size(), check.get_size()));
}
#if defined(ACE_HAS_IPV6)
if (addr_port.get_type() == check.get_type() && addr_port.get_size() == check.get_size()){
if (addr_port.get_type() == AF_INET6) {
const struct sockaddr_in6 *addr_port_in6 =
static_cast<const struct sockaddr_in6*> (addr_port.get_addr());
const struct sockaddr_in6 *check_in6 =
static_cast<const struct sockaddr_in6*> (check.get_addr());
# if defined(AIX)
ACE_ERROR((LM_ERROR, ACE_TEXT (" addr_port_in6->sin6_len=%d, check_in6->sin6_len=%d\n")
, (int)addr_port_in6->sin6_len, (int)check_in6->sin6_len));
# endif
ACE_ERROR((LM_ERROR, ACE_TEXT (" addr_port_in6->sin6_family=%d, check_in6->sin6_family=%d\n")
, (int)addr_port_in6->sin6_family, (int)check_in6->sin6_family));
ACE_ERROR((LM_ERROR, ACE_TEXT (" addr_port_in6->sin6_port=%d, check_in6->sin6_port=%d\n")
, (int)addr_port_in6->sin6_port, (int)check_in6->sin6_port));
ACE_ERROR((LM_ERROR, ACE_TEXT (" addr_port_in6->sin6_flowinfo=%d, check_in6->sin6_flowinfo=%d\n")
, (int)addr_port_in6->sin6_flowinfo, (int)check_in6->sin6_flowinfo));
ACE_ERROR((LM_ERROR, ACE_TEXT (" addr_port_in6->sin6_scope_id=%d, check_in6->sin6_scope_id=%d\n")
, (int)addr_port_in6->sin6_scope_id, (int)check_in6->sin6_scope_id));
ACE_ERROR((LM_DEBUG, ACE_TEXT (" addr_port_in6->sin6_addr=")));
ACE_HEX_DUMP((LM_DEBUG, reinterpret_cast<const char*>(&addr_port_in6->sin6_addr), sizeof(addr_port_in6->sin6_addr)));
ACE_ERROR((LM_DEBUG, ACE_TEXT (" check_in6->sin6_addr=")));
ACE_HEX_DUMP((LM_DEBUG, reinterpret_cast<const char*>(&check_in6->sin6_addr), sizeof(check_in6->sin6_addr)));
}
}
#endif
status = 1;
}
}
else
{
// Sometimes this fails because the run-time host lacks the capability to
// resolve a name. But it shouldn't fail on the first one, 127.0.0.1.
if (i == 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%C: %p\n"),
addr_ports[i],
ACE_TEXT ("lookup")));
status = 1;
}
else
{
ACE_ERROR ((LM_WARNING,
ACE_TEXT ("%C: %p\n"),
addr_ports[i],
ACE_TEXT ("lookup")));
}
}
}
const char *ipv4_addresses[] =
{
"127.0.0.1", "138.38.180.251", "64.219.54.121", "192.0.0.1", "10.0.0.1", 0
};
ACE_INET_Addr addr;
status |= check_type_consistency (addr);
char hostaddr[1024];
for (int i=0; ipv4_addresses[i] != 0; i++)
{
struct in_addr addrv4;
ACE_OS::memset ((void *) &addrv4, 0, sizeof addrv4);
ACE_UINT32 addr32;
ACE_OS::inet_pton (AF_INET, ipv4_addresses[i], &addrv4);
ACE_OS::memcpy (&addr32, &addrv4, sizeof (addr32));
status |= !(addr.set (80, ipv4_addresses[i]) == 0);
status |= check_type_consistency (addr);
/*
** Now check to make sure get_ip_address matches and get_host_addr
** matches.
*/
if (addr.get_ip_address () != ACE_HTONL (addr32))
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Error: %C failed get_ip_address() check\n")
ACE_TEXT ("0x%x != 0x%x\n"),
ipv4_addresses[i],
addr.get_ip_address (),
ACE_HTONL (addr32)));
status = 1;
}
if (addr.get_host_addr () != 0 &&
ACE_OS::strcmp (addr.get_host_addr(), ipv4_addresses[i]) != 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%C failed get_host_addr() check\n")
ACE_TEXT ("%C != %C\n"),
ipv4_addresses[i],
addr.get_host_addr (),
ipv4_addresses[i]));
status = 1;
}
// Now we check the operation of get_host_addr(char*,int)
const char* haddr = addr.get_host_addr (&hostaddr[0], sizeof(hostaddr));
if (haddr != 0 &&
ACE_OS::strcmp (&hostaddr[0], haddr) != 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%C failed get_host_addr(char* buf,int) check\n")
ACE_TEXT ("buf ['%C'] != return value ['%C']\n"),
ipv4_addresses[i],
&hostaddr[0],
haddr));
status = 1;
}
if (ACE_OS::strcmp (&hostaddr[0], ipv4_addresses[i]) != 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%C failed get_host_addr(char*,int) check\n")
ACE_TEXT ("buf ['%C'] != expected value ['%C']\n"),
ipv4_addresses[i],
&hostaddr[0],
ipv4_addresses[i]));
status = 1;
}
// Clear out the address by setting it to 1 and check
addr.set (0, ACE_UINT32 (1), 1);
status |= check_type_consistency (addr);
if (addr.get_ip_address () != 1)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("Failed to set address to 1\n")));
status = 1;
}
// Now set the address using a 32 bit number and check that we get
// the right string out of get_host_addr().
addr.set (80, addr32, 0); // addr32 is already in network byte order
status |= check_type_consistency(addr);
if (addr.get_host_addr () != 0 &&
ACE_OS::strcmp (addr.get_host_addr (), ipv4_addresses[i]) != 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%C failed second get_host_addr() check\n")
ACE_TEXT ("return value ['%C'] != expected value ['%C']\n"),
ipv4_addresses[i],
addr.get_host_addr (),
ipv4_addresses[i]));
status = 1;
}
// Test for ACE_INET_Addr::set_addr().
struct sockaddr_in sa4;
sa4.sin_family = AF_INET;
sa4.sin_addr = addrv4;
sa4.sin_port = ACE_HTONS (8080);
addr.set (0, ACE_UINT32 (1), 1);
addr.set_addr (&sa4, sizeof(sa4));
status |= check_type_consistency (addr);
if (addr.get_port_number () != 8080)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_INET_Addr::set_addr() ")
ACE_TEXT ("failed to update port number.\n")));
status = 1;
}
if (addr.get_ip_address () != ACE_HTONL (addr32))
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_INET_Addr::set_addr() ")
ACE_TEXT ("failed to update address.\n")));
status = 1;
}
}
#if defined (ACE_HAS_IPV6)
if (ACE::ipv6_enabled ())
{
char local_host_name[1024];
ACE_OS::hostname(local_host_name, 1024);
const char* local_names[] = {"localhost", local_host_name};
for (int i = 0; i < 2; ++i)
{
ACE_INET_Addr addr;
int old_type = addr.get_type();
if (addr.set(12345, local_names[i]) == 0) {
if (addr.get_type() != old_type) {
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("IPv6 set failed: addr.set(12345, \"%C\"), old addr.type() = %d, new addr_type()= %d\n"),
local_names[i],
old_type,
addr.get_type ()));
status = 1;
}
}
else {
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("IPv6 set failed: addr.set(12345, \"%C\") returns nonzero\n"), local_names[i]));
}
}
const char *ipv6_addresses[] = {
"1080::8:800:200c:417a", // unicast address
"ff01::101", // multicast address
"::1", // loopback address
"::", // unspecified addresses
0
};
for (int i=0; ipv6_addresses[i] != 0; i++)
{
ACE_INET_Addr addr (80, ipv6_addresses[i]);
status |= check_type_consistency (addr);
if (0 != ACE_OS::strcmp (addr.get_host_addr (), ipv6_addresses[i]))
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("IPv6 get_host_addr failed: %C != %C\n"),
addr.get_host_addr (),
ipv6_addresses[i]));
status = 1;
}
}
const char *ipv6_names[] = {
"naboo.dre.vanderbilt.edu",
"v6.ipv6-test.com",
0
};
for (int i=0; ipv6_names[i] != 0; i++)
{
ACE_INET_Addr addr (80, ipv6_names[i]);
status |= check_type_consistency (addr);
if (0 != ACE_OS::strcmp (addr.get_host_name (), ipv6_names[i]))
{
// Alias? Check lookup on the reverse.
ACE_INET_Addr alias_check;
if (alias_check.set (80, addr.get_host_name ()) == 0)
{
if (addr != alias_check)
ACE_ERROR ((LM_WARNING,
ACE_TEXT ("IPv6 name mismatch: %s (%s) != %s\n"),
addr.get_host_name (),
addr.get_host_addr (),
ipv6_names[i]));
}
else
{
ACE_ERROR ((LM_WARNING,
ACE_TEXT ("IPv6 reverse lookup mismatch: %s (%s) != %s\n"),
addr.get_host_name (),
addr.get_host_addr (),
ipv6_names[i]));
}
}
}
}
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("IPv6 tests done\n")));
#else
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_HAS_IPV6 not set; no IPv6 tests run\n")));
#endif
struct Address loopback_addresses[] =
{ {"127.0.0.1", true}, {"127.1.2.3", true}
, {"127.0.0.0", true}, {"127.255.255.255", true}
, {"126.255.255.255", false}, {"128.0.0.0", false}, {0, true}
};
for (int i=0; loopback_addresses[i].name != 0; i++)
{
struct in_addr addrv4;
ACE_UINT32 addr32 = 0;
ACE_OS::inet_pton (AF_INET, loopback_addresses[i].name, &addrv4);
ACE_OS::memcpy (&addr32, &addrv4, sizeof (addr32));
addr.set (80, loopback_addresses[i].name);
if (addr.is_loopback() != loopback_addresses[i].loopback)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_INET_Addr::is_loopback() ")
ACE_TEXT ("failed to distinguish loopback address. %C\n")
, loopback_addresses[i].name));
status = 1;
}
}
if (addr.string_to_addr ("127.0.0.1:72000", AF_INET) != -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("ACE_INET_Addr::string_to_addr() ")
ACE_TEXT ("failed to detect port number overflow\n")));
status = 1;
}
if (!test_tao_use ())
status = 1;
if (!test_multiple ())
status = 1;
if (!test_port_assignment ())
status = 1;
ACE_INET_Addr a1 (80, "127.0.0.1");
ACE_INET_Addr a2 = a1;
if (a1 != a2)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Address equality check failed after assignment\n")));
status = 1;
}
ACE_END_TEST;
return status;
}
|