2 //=============================================================================
4 * @file INET_Addr_Test.cpp
6 * Performs several tests on the ACE_INET_Addr class. It creates several
7 * IPv4 and IPv6 addresses and checks that the address formed by the
10 * @author John Aughey (jha@aughey.com)
12 //=============================================================================
15 #include "test_config.h"
16 #include "ace/OS_NS_string.h"
17 #include "ace/INET_Addr.h"
18 #include "ace/Log_Msg.h"
19 #include "ace/OS_NS_arpa_inet.h"
20 #include "ace/SString.h"
21 #include "ace/OS_NS_unistd.h"
23 // Make sure that ACE_Addr::addr_type_ is the same
24 // as the family of the inet_addr_.
25 static int check_type_consistency (const ACE_INET_Addr
&addr
)
29 if (addr
.get_type () == AF_INET
)
31 struct sockaddr_in
*sa4
= (struct sockaddr_in
*)addr
.get_addr();
32 family
= sa4
->sin_family
;
34 #if defined (ACE_HAS_IPV6)
35 else if (addr
.get_type () == AF_INET6
)
37 struct sockaddr_in6
*sa6
= (struct sockaddr_in6
*)addr
.get_addr();
38 family
= sa6
->sin6_family
;
42 if (addr
.get_type () != family
)
45 ACE_TEXT ("Inconsistency between ACE_SOCK::addr_type_ (%d) ")
46 ACE_TEXT ("and the sockaddr family (%d)\n"),
54 static bool test_tao_use ()
57 if (::gethostname (host
, 255) != 0)
60 ACE_TEXT ("Test TAO Use fail %p\n"),
61 ACE_TEXT ("gethostname")));
66 addr
.set ((unsigned short)0, host
);
68 ACE_CString
full (host
);
71 addr
.set (full
.c_str ());
73 u_short p
= addr
.get_port_number ();
78 ACE_TEXT ("Test TAO Use expected port 12345 got %d\n"),
84 ACE_TEXT ("Test TAO Use passed\n")));
88 static bool test_port_assignment ()
90 #if defined (ACE_HAS_IPV6)
91 ACE_INET_Addr
addr1 (static_cast<unsigned short> (0), ACE_IPV6_ANY
, AF_INET6
);
94 addr1
.set_port_number (12345);
96 if (addr1
.get_port_number () != addr2
.get_port_number ())
99 ACE_TEXT ("port number not properly copied. ")
100 ACE_TEXT ("addr1 port = %d addr2 port = %d\n"),
101 addr1
.get_port_number (), addr2
.get_port_number ()));
104 ACE_DEBUG ((LM_DEBUG
,
105 ACE_TEXT ("Test Port Assignment passed\n")));
107 ACE_DEBUG ((LM_DEBUG
,
108 ACE_TEXT ("Test Port Assignment is IPv6 only\n")));
109 #endif /* ACE_HAS_IPV6 */
113 static bool test_multiple ()
117 // Check the behavior when there are multiple addresses assigned to a name.
118 // The NTP pool should always return multiples, though always different.
120 if (ntp
.set (123, ACE_TEXT ("pool.ntp.org")) == -1)
122 // This is just a warning to prevent fails on lookups on hosts with no
123 // DNS service. The real value of this test is to accurately get
124 // multiples from the result.
125 ACE_ERROR ((LM_WARNING
, ACE_TEXT ("%p\n"), ACE_TEXT ("pool.ntp.org")));
129 ACE_TCHAR addr_string
[256];
132 ++count
; // If lookup succeeded, there's at least one
133 ntp
.addr_to_string (addr_string
, sizeof (addr_string
));
134 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("IPv4 %B: %s\n"), count
, addr_string
));
139 #if defined (ACE_HAS_IPV6)
141 if (ntp6
.set (123, ACE_TEXT ("2.pool.ntp.org"), 1, AF_INET6
) == -1)
143 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("2.pool.ntp.org")));
149 ++count
; // If lookup succeeded, there's at least one
150 ntp6
.addr_to_string (addr_string
, sizeof (addr_string
));
151 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("IPv6 %B: %s\n"), count
, addr_string
));
153 while (ntp6
.next ());
156 #endif /* ACE_HAS_IPV6 */
161 static bool test_port_names()
167 #if defined (ACE_LACKS_GETSERVBYNAME)
168 // Since we don't have getservbyname, the call to set should fail.
169 // Check that the call does in fact fail.
170 if (addr
.set("telnet") == 0)
172 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("set with 'telnet' succeeded (expected failure)\n")));
176 if (addr
.set("telnet") != 0)
178 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("set with 'telnet' failed (expected success)\n")));
182 if (addr
!= ACE_INET_Addr("0.0.0.0:23"))
184 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("set with 'telnet' failed to yield correct address\n")));
187 #endif /* ACE_LACKS_GETSERVBYNAME */
189 if (addr
.set("not a port name") == 0)
191 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("set with 'not a port name' succeeded (expected failure)\n")));
203 int run_main (int, ACE_TCHAR
*[])
205 ACE_START_TEST (ACE_TEXT ("INET_Addr_Test"));
207 int status
= 0; // Innocent until proven guilty
209 // Try to set up known IP and port.
211 ACE_UINT32
const ia_any
= INADDR_ANY
;
212 ACE_INET_Addr
local_addr(port
, ia_any
);
213 status
|= check_type_consistency (local_addr
);
214 if (local_addr
.get_port_number () != 80)
216 ACE_ERROR ((LM_ERROR
,
217 ACE_TEXT ("Got port %d, expecting 80\n"),
218 (int)(local_addr
.get_port_number ())));
221 if (local_addr
.get_ip_address () != ia_any
)
223 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Mismatch on local IP addr\n")));
227 // Assignment constructor
228 ACE_INET_Addr
local_addr2 (local_addr
);
229 status
|= check_type_consistency (local_addr2
);
230 if (local_addr2
.get_port_number () != 80)
232 ACE_ERROR ((LM_ERROR
,
233 ACE_TEXT ("Copy got port %d, expecting 80\n"),
234 (int)(local_addr2
.get_port_number ())));
237 if (local_addr2
.get_ip_address () != ia_any
)
239 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Mismatch on copy local IP addr\n")));
242 if (local_addr
!= local_addr2
)
244 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Copy local addr mismatch\n")));
248 // Try to parse out a simple address:port string. Intentionally reuse
249 // the ACE_INET_Addr to ensure resetting an address works.
250 const char *addr_ports
[] =
252 "127.0.0.1:80", "www.dre.vanderbilt.edu:80", 0
254 ACE_INET_Addr addr_port
;
255 for (int i
= 0; addr_ports
[i
] != 0; ++i
)
257 if (addr_port
.set (addr_ports
[i
]) == 0)
259 status
|= check_type_consistency (addr_port
);
260 if (addr_port
.get_port_number () != 80)
262 ACE_ERROR ((LM_ERROR
,
263 ACE_TEXT ("Got port %d from %s\n"),
264 (int)(addr_port
.get_port_number ()),
268 ACE_INET_Addr
check (addr_ports
[i
]);
269 if (addr_port
!= check
)
271 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Reset on iter %d failed\n"), i
));
273 if (addr_port
.get_type() != check
.get_type()) {
274 ACE_ERROR ((LM_ERROR
, ACE_TEXT (" addr_port.get_type()= %d, check.get_type()=%d\n")
275 , addr_port
.get_type(), check
.get_type()));
277 if (addr_port
.get_size() != check
.get_size()) {
278 ACE_ERROR ((LM_ERROR
, ACE_TEXT (" addr_port.get_size()= %d, check.get_size()=%d\n")
279 , addr_port
.get_size(), check
.get_size()));
281 #if defined(ACE_HAS_IPV6)
282 if (addr_port
.get_type() == check
.get_type() && addr_port
.get_size() == check
.get_size()){
283 if (addr_port
.get_type() == AF_INET6
) {
284 const struct sockaddr_in6
*addr_port_in6
=
285 static_cast<const struct sockaddr_in6
*> (addr_port
.get_addr());
286 const struct sockaddr_in6
*check_in6
=
287 static_cast<const struct sockaddr_in6
*> (check
.get_addr());
289 ACE_ERROR((LM_ERROR
, ACE_TEXT (" addr_port_in6->sin6_family=%d, check_in6->sin6_family=%d\n")
290 , (int)addr_port_in6
->sin6_family
, (int)check_in6
->sin6_family
));
292 ACE_ERROR((LM_ERROR
, ACE_TEXT (" addr_port_in6->sin6_port=%d, check_in6->sin6_port=%d\n")
293 , (int)addr_port_in6
->sin6_port
, (int)check_in6
->sin6_port
));
295 ACE_ERROR((LM_ERROR
, ACE_TEXT (" addr_port_in6->sin6_flowinfo=%d, check_in6->sin6_flowinfo=%d\n")
296 , (int)addr_port_in6
->sin6_flowinfo
, (int)check_in6
->sin6_flowinfo
));
298 ACE_ERROR((LM_ERROR
, ACE_TEXT (" addr_port_in6->sin6_scope_id=%d, check_in6->sin6_scope_id=%d\n")
299 , (int)addr_port_in6
->sin6_scope_id
, (int)check_in6
->sin6_scope_id
));
301 ACE_ERROR((LM_DEBUG
, ACE_TEXT (" addr_port_in6->sin6_addr=")));
302 ACE_HEX_DUMP((LM_DEBUG
, reinterpret_cast<const char*>(&addr_port_in6
->sin6_addr
), sizeof(addr_port_in6
->sin6_addr
)));
303 ACE_ERROR((LM_DEBUG
, ACE_TEXT (" check_in6->sin6_addr=")));
304 ACE_HEX_DUMP((LM_DEBUG
, reinterpret_cast<const char*>(&check_in6
->sin6_addr
), sizeof(check_in6
->sin6_addr
)));
313 // Sometimes this fails because the run-time host lacks the capability to
314 // resolve a name. But it shouldn't fail on the first one, 127.0.0.1.
317 ACE_ERROR ((LM_ERROR
,
318 ACE_TEXT ("%C: %p\n"),
320 ACE_TEXT ("lookup")));
325 ACE_ERROR ((LM_WARNING
,
326 ACE_TEXT ("%C: %p\n"),
328 ACE_TEXT ("lookup")));
333 const char *ipv4_addresses
[] =
335 "127.0.0.1", "138.38.180.251", "64.219.54.121", "192.0.0.1", "10.0.0.1", 0
339 status
|= check_type_consistency (addr
);
342 for (int i
=0; ipv4_addresses
[i
] != 0; i
++)
344 struct in_addr addrv4
;
345 ACE_OS::memset ((void *) &addrv4
, 0, sizeof addrv4
);
348 ACE_OS::inet_pton (AF_INET
, ipv4_addresses
[i
], &addrv4
);
350 ACE_OS::memcpy (&addr32
, &addrv4
, sizeof (addr32
));
352 status
|= !(addr
.set (80, ipv4_addresses
[i
]) == 0);
353 status
|= check_type_consistency (addr
);
356 ** Now check to make sure get_ip_address matches and get_host_addr
359 if (addr
.get_ip_address () != ACE_HTONL (addr32
))
361 ACE_ERROR ((LM_ERROR
,
362 ACE_TEXT ("Error: %C failed get_ip_address() check\n")
363 ACE_TEXT ("0x%x != 0x%x\n"),
365 addr
.get_ip_address (),
366 ACE_HTONL (addr32
)));
370 if (addr
.get_host_addr () != 0 &&
371 ACE_OS::strcmp (addr
.get_host_addr(), ipv4_addresses
[i
]) != 0)
373 ACE_ERROR ((LM_ERROR
,
374 ACE_TEXT ("%C failed get_host_addr() check\n")
375 ACE_TEXT ("%C != %C\n"),
377 addr
.get_host_addr (),
382 // Now we check the operation of get_host_addr(char*,int)
383 const char* haddr
= addr
.get_host_addr (&hostaddr
[0], sizeof(hostaddr
));
385 ACE_OS::strcmp (&hostaddr
[0], haddr
) != 0)
387 ACE_ERROR ((LM_ERROR
,
388 ACE_TEXT ("%C failed get_host_addr(char* buf,int) check\n")
389 ACE_TEXT ("buf ['%C'] != return value ['%C']\n"),
395 if (ACE_OS::strcmp (&hostaddr
[0], ipv4_addresses
[i
]) != 0)
397 ACE_ERROR ((LM_ERROR
,
398 ACE_TEXT ("%C failed get_host_addr(char*,int) check\n")
399 ACE_TEXT ("buf ['%C'] != expected value ['%C']\n"),
406 // Clear out the address by setting it to 1 and check
407 addr
.set (0, ACE_UINT32 (1), 1);
408 status
|= check_type_consistency (addr
);
409 if (addr
.get_ip_address () != 1)
411 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Failed to set address to 1\n")));
415 // Now set the address using a 32 bit number and check that we get
416 // the right string out of get_host_addr().
417 addr
.set (80, addr32
, 0); // addr32 is already in network byte order
418 status
|= check_type_consistency(addr
);
420 if (addr
.get_host_addr () != 0 &&
421 ACE_OS::strcmp (addr
.get_host_addr (), ipv4_addresses
[i
]) != 0)
423 ACE_ERROR ((LM_ERROR
,
424 ACE_TEXT ("%C failed second get_host_addr() check\n")
425 ACE_TEXT ("return value ['%C'] != expected value ['%C']\n"),
427 addr
.get_host_addr (),
432 // Test for ACE_INET_Addr::set_addr().
433 struct sockaddr_in sa4
;
434 sa4
.sin_family
= AF_INET
;
435 sa4
.sin_addr
= addrv4
;
436 sa4
.sin_port
= ACE_HTONS (8080);
438 addr
.set (0, ACE_UINT32 (1), 1);
439 addr
.set_addr (&sa4
, sizeof(sa4
));
440 status
|= check_type_consistency (addr
);
442 if (addr
.get_port_number () != 8080)
444 ACE_ERROR ((LM_ERROR
,
445 ACE_TEXT ("ACE_INET_Addr::set_addr() ")
446 ACE_TEXT ("failed to update port number.\n")));
450 if (addr
.get_ip_address () != ACE_HTONL (addr32
))
452 ACE_ERROR ((LM_ERROR
,
453 ACE_TEXT ("ACE_INET_Addr::set_addr() ")
454 ACE_TEXT ("failed to update address.\n")));
460 #if defined (ACE_HAS_IPV6)
461 if (ACE::ipv6_enabled ())
463 char local_host_name
[1024];
464 ACE_OS::hostname(local_host_name
, 1024);
465 const char* local_names
[] = {"localhost", local_host_name
};
467 for (int i
= 0; i
< 2; ++i
)
470 int old_type
= addr
.get_type();
471 if (addr
.set(12345, local_names
[i
]) == 0) {
472 if (addr
.get_type() != old_type
) {
473 ACE_ERROR ((LM_ERROR
,
474 ACE_TEXT ("IPv6 set failed: addr.set(12345, \"%C\"), old addr.type() = %d, new addr_type()= %d\n"),
482 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("IPv6 set failed: addr.set(12345, \"%C\") returns nonzero\n"), local_names
[i
]));
486 const char *ipv6_addresses
[] = {
487 "1080::8:800:200c:417a", // unicast address
488 "ff01::101", // multicast address
489 "::1", // loopback address
490 "::", // unspecified addresses
494 for (int i
=0; ipv6_addresses
[i
] != 0; i
++)
496 ACE_INET_Addr
addr (80, ipv6_addresses
[i
]);
497 status
|= check_type_consistency (addr
);
499 if (0 != ACE_OS::strcmp (addr
.get_host_addr (), ipv6_addresses
[i
]))
501 ACE_ERROR ((LM_ERROR
,
502 ACE_TEXT ("IPv6 get_host_addr failed: %C != %C\n"),
503 addr
.get_host_addr (),
509 const char *ipv6_names
[] = {
510 "naboo.dre.vanderbilt.edu",
514 for (int i
=0; ipv6_names
[i
] != 0; i
++)
516 ACE_INET_Addr
addr (80, ipv6_names
[i
]);
517 status
|= check_type_consistency (addr
);
519 if (0 != ACE_OS::strcmp (addr
.get_host_name (), ipv6_names
[i
]))
521 // Alias? Check lookup on the reverse.
522 ACE_INET_Addr alias_check
;
523 if (alias_check
.set (80, addr
.get_host_name ()) == 0)
525 if (addr
!= alias_check
)
526 ACE_ERROR ((LM_WARNING
,
527 ACE_TEXT ("IPv6 name mismatch: %s (%s) != %s\n"),
528 addr
.get_host_name (),
529 addr
.get_host_addr (),
534 ACE_ERROR ((LM_WARNING
,
535 ACE_TEXT ("IPv6 reverse lookup mismatch: %s (%s) != %s\n"),
536 addr
.get_host_name (),
537 addr
.get_host_addr (),
543 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("IPv6 tests done\n")));
545 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("ACE_HAS_IPV6 not set; no IPv6 tests run\n")));
548 struct Address loopback_addresses
[] =
549 { {"127.0.0.1", true}, {"127.1.2.3", true}
550 , {"127.0.0.0", true}, {"127.255.255.255", true}
551 , {"126.255.255.255", false}, {"128.0.0.0", false}, {0, true}
554 for (int i
=0; loopback_addresses
[i
].name
!= 0; i
++)
556 struct in_addr addrv4
;
557 ACE_UINT32 addr32
= 0;
559 ACE_OS::inet_pton (AF_INET
, loopback_addresses
[i
].name
, &addrv4
);
561 ACE_OS::memcpy (&addr32
, &addrv4
, sizeof (addr32
));
563 addr
.set (80, loopback_addresses
[i
].name
);
565 if (addr
.is_loopback() != loopback_addresses
[i
].loopback
)
567 ACE_ERROR ((LM_ERROR
,
568 ACE_TEXT ("ACE_INET_Addr::is_loopback() ")
569 ACE_TEXT ("failed to distinguish loopback address. %C\n")
570 , loopback_addresses
[i
].name
));
575 if (addr
.string_to_addr ("127.0.0.1:72000", AF_INET
) != -1)
577 ACE_ERROR ((LM_ERROR
,
578 ACE_TEXT ("ACE_INET_Addr::string_to_addr() ")
579 ACE_TEXT ("failed to detect port number overflow\n")));
583 if (!test_tao_use ())
586 if (!test_multiple ())
589 if (!test_port_assignment ())
592 ACE_INET_Addr
a1 (80, "127.0.0.1");
593 ACE_INET_Addr a2
= a1
;
596 ACE_ERROR ((LM_ERROR
,
597 ACE_TEXT ("Address equality check failed after assignment\n")));
601 if (!test_port_names ())