Doxygen changes
[ACE_TAO.git] / ACE / tests / INET_Addr_Test.cpp
blobcfcc9dce1329627e70dc382e17f097ee932b4486
2 //=============================================================================
3 /**
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
8 * class is valid.
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)
27 int family = -1;
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;
40 #endif
42 if (addr.get_type () != family)
44 ACE_ERROR ((LM_ERROR,
45 ACE_TEXT ("Inconsistency between ACE_SOCK::addr_type_ (%d) ")
46 ACE_TEXT ("and the sockaddr family (%d)\n"),
47 addr.get_type (),
48 family));
49 return 1;
51 return 0;
54 static bool test_tao_use (void)
56 char host[256];
57 if (::gethostname (host, 255) != 0)
59 ACE_ERROR ((LM_ERROR,
60 ACE_TEXT ("Test TAO Use fail %p\n"),
61 ACE_TEXT ("gethostname")));
62 return false;
65 ACE_INET_Addr addr;
66 addr.set ((unsigned short)0, host);
68 ACE_CString full (host);
69 full += ":12345";
71 addr.set (full.c_str ());
73 u_short p = addr.get_port_number ();
75 if (p != 12345)
77 ACE_ERROR ((LM_ERROR,
78 ACE_TEXT ("Test TAO Use expected port 12345 got %d\n"),
79 p));
80 return false;
83 ACE_DEBUG ((LM_DEBUG,
84 ACE_TEXT ("Test TAO Use passed\n")));
85 return true;
88 static bool test_port_assignment (void)
90 #if defined (ACE_HAS_IPV6)
91 ACE_INET_Addr addr1 (static_cast<unsigned short> (0), ACE_IPV6_ANY, AF_INET6);
92 ACE_INET_Addr addr2;
94 addr1.set_port_number (12345);
95 addr2.set (addr1);
96 if (addr1.get_port_number () != addr2.get_port_number ())
98 ACE_ERROR ((LM_ERROR,
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 ()));
102 return false;
104 ACE_DEBUG ((LM_DEBUG,
105 ACE_TEXT ("Test Port Assignment passed\n")));
106 #else
107 ACE_DEBUG ((LM_DEBUG,
108 ACE_TEXT ("Test Port Assignment is IPv6 only\n")));
109 #endif /* ACE_HAS_IPV6 */
110 return true;
113 static bool test_multiple (void)
115 bool success = true;
117 // Check the behavior when there are multiple addresses assigned to a name.
118 // The NTP pool should always return multiples, though always different.
119 ACE_INET_Addr ntp;
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")));
126 return true;
128 size_t count = 0;
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));
136 while (ntp.next ());
137 success = count > 1;
139 #if defined (ACE_HAS_IPV6)
140 ACE_INET_Addr ntp6;
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")));
144 return false;
146 count = 0;
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 ());
154 if (count <= 1)
155 success = false;
156 #endif /* ACE_HAS_IPV6 */
158 return success;
161 struct Address {
162 const char* name;
163 bool loopback;
166 int run_main (int, ACE_TCHAR *[])
168 ACE_START_TEST (ACE_TEXT ("INET_Addr_Test"));
170 int status = 0; // Innocent until proven guilty
172 // Try to set up known IP and port.
173 u_short port (80);
174 ACE_UINT32 const ia_any = INADDR_ANY;
175 ACE_INET_Addr local_addr(port, ia_any);
176 status |= check_type_consistency (local_addr);
177 if (local_addr.get_port_number () != 80)
179 ACE_ERROR ((LM_ERROR,
180 ACE_TEXT ("Got port %d, expecting 80\n"),
181 (int)(local_addr.get_port_number ())));
182 status = 1;
184 if (local_addr.get_ip_address () != ia_any)
186 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Mismatch on local IP addr\n")));
187 status = 1;
190 // Assignment constructor
191 ACE_INET_Addr local_addr2 (local_addr);
192 status |= check_type_consistency (local_addr2);
193 if (local_addr2.get_port_number () != 80)
195 ACE_ERROR ((LM_ERROR,
196 ACE_TEXT ("Copy got port %d, expecting 80\n"),
197 (int)(local_addr2.get_port_number ())));
198 status = 1;
200 if (local_addr2.get_ip_address () != ia_any)
202 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Mismatch on copy local IP addr\n")));
203 status = 1;
205 if (local_addr != local_addr2)
207 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Copy local addr mismatch\n")));
208 status = 1;
211 // Try to parse out a simple address:port string. Intentionally reuse
212 // the ACE_INET_Addr to ensure resetting an address works.
213 const char *addr_ports[] =
215 "127.0.0.1:80", "www.dre.vanderbilt.edu:80", 0
217 ACE_INET_Addr addr_port;
218 for (int i = 0; addr_ports[i] != 0; ++i)
220 if (addr_port.set (addr_ports[i]) == 0)
222 status |= check_type_consistency (addr_port);
223 if (addr_port.get_port_number () != 80)
225 ACE_ERROR ((LM_ERROR,
226 ACE_TEXT ("Got port %d from %s\n"),
227 (int)(addr_port.get_port_number ()),
228 addr_ports[i]));
229 status = 1;
231 ACE_INET_Addr check (addr_ports[i]);
232 if (addr_port != check)
234 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Reset on iter %d failed\n"), i));
236 if (addr_port.get_type() != check.get_type()) {
237 ACE_ERROR ((LM_ERROR, ACE_TEXT (" addr_port.get_type()= %d, check.get_type()=%d\n")
238 , addr_port.get_type(), check.get_type()));
240 if (addr_port.get_size() != check.get_size()) {
241 ACE_ERROR ((LM_ERROR, ACE_TEXT (" addr_port.get_size()= %d, check.get_size()=%d\n")
242 , addr_port.get_size(), check.get_size()));
244 #if defined(ACE_HAS_IPV6)
245 if (addr_port.get_type() == check.get_type() && addr_port.get_size() == check.get_size()){
246 if (addr_port.get_type() == AF_INET6) {
247 const struct sockaddr_in6 *addr_port_in6 =
248 static_cast<const struct sockaddr_in6*> (addr_port.get_addr());
249 const struct sockaddr_in6 *check_in6 =
250 static_cast<const struct sockaddr_in6*> (check.get_addr());
251 # if defined(AIX)
253 ACE_ERROR((LM_ERROR, ACE_TEXT (" addr_port_in6->sin6_len=%d, check_in6->sin6_len=%d\n")
254 , (int)addr_port_in6->sin6_len, (int)check_in6->sin6_len));
255 # endif
257 ACE_ERROR((LM_ERROR, ACE_TEXT (" addr_port_in6->sin6_family=%d, check_in6->sin6_family=%d\n")
258 , (int)addr_port_in6->sin6_family, (int)check_in6->sin6_family));
260 ACE_ERROR((LM_ERROR, ACE_TEXT (" addr_port_in6->sin6_port=%d, check_in6->sin6_port=%d\n")
261 , (int)addr_port_in6->sin6_port, (int)check_in6->sin6_port));
263 ACE_ERROR((LM_ERROR, ACE_TEXT (" addr_port_in6->sin6_flowinfo=%d, check_in6->sin6_flowinfo=%d\n")
264 , (int)addr_port_in6->sin6_flowinfo, (int)check_in6->sin6_flowinfo));
266 ACE_ERROR((LM_ERROR, ACE_TEXT (" addr_port_in6->sin6_scope_id=%d, check_in6->sin6_scope_id=%d\n")
267 , (int)addr_port_in6->sin6_scope_id, (int)check_in6->sin6_scope_id));
269 ACE_ERROR((LM_DEBUG, ACE_TEXT (" addr_port_in6->sin6_addr=")));
270 ACE_HEX_DUMP((LM_DEBUG, reinterpret_cast<const char*>(&addr_port_in6->sin6_addr), sizeof(addr_port_in6->sin6_addr)));
271 ACE_ERROR((LM_DEBUG, ACE_TEXT (" check_in6->sin6_addr=")));
272 ACE_HEX_DUMP((LM_DEBUG, reinterpret_cast<const char*>(&check_in6->sin6_addr), sizeof(check_in6->sin6_addr)));
275 #endif
276 status = 1;
279 else
281 // Sometimes this fails because the run-time host lacks the capability to
282 // resolve a name. But it shouldn't fail on the first one, 127.0.0.1.
283 if (i == 0)
285 ACE_ERROR ((LM_ERROR,
286 ACE_TEXT ("%C: %p\n"),
287 addr_ports[i],
288 ACE_TEXT ("lookup")));
289 status = 1;
291 else
293 ACE_ERROR ((LM_WARNING,
294 ACE_TEXT ("%C: %p\n"),
295 addr_ports[i],
296 ACE_TEXT ("lookup")));
301 const char *ipv4_addresses[] =
303 "127.0.0.1", "138.38.180.251", "64.219.54.121", "192.0.0.1", "10.0.0.1", 0
306 ACE_INET_Addr addr;
307 status |= check_type_consistency (addr);
308 char hostaddr[1024];
310 for (int i=0; ipv4_addresses[i] != 0; i++)
312 struct in_addr addrv4;
313 ACE_OS::memset ((void *) &addrv4, 0, sizeof addrv4);
314 ACE_UINT32 addr32;
316 ACE_OS::inet_pton (AF_INET, ipv4_addresses[i], &addrv4);
318 ACE_OS::memcpy (&addr32, &addrv4, sizeof (addr32));
320 status |= !(addr.set (80, ipv4_addresses[i]) == 0);
321 status |= check_type_consistency (addr);
324 ** Now check to make sure get_ip_address matches and get_host_addr
325 ** matches.
327 if (addr.get_ip_address () != ACE_HTONL (addr32))
329 ACE_ERROR ((LM_ERROR,
330 ACE_TEXT ("Error: %C failed get_ip_address() check\n")
331 ACE_TEXT ("0x%x != 0x%x\n"),
332 ipv4_addresses[i],
333 addr.get_ip_address (),
334 ACE_HTONL (addr32)));
335 status = 1;
338 if (addr.get_host_addr () != 0 &&
339 ACE_OS::strcmp (addr.get_host_addr(), ipv4_addresses[i]) != 0)
341 ACE_ERROR ((LM_ERROR,
342 ACE_TEXT ("%C failed get_host_addr() check\n")
343 ACE_TEXT ("%C != %C\n"),
344 ipv4_addresses[i],
345 addr.get_host_addr (),
346 ipv4_addresses[i]));
347 status = 1;
350 // Now we check the operation of get_host_addr(char*,int)
351 const char* haddr = addr.get_host_addr (&hostaddr[0], sizeof(hostaddr));
352 if (haddr != 0 &&
353 ACE_OS::strcmp (&hostaddr[0], haddr) != 0)
355 ACE_ERROR ((LM_ERROR,
356 ACE_TEXT ("%C failed get_host_addr(char* buf,int) check\n")
357 ACE_TEXT ("buf ['%C'] != return value ['%C']\n"),
358 ipv4_addresses[i],
359 &hostaddr[0],
360 haddr));
361 status = 1;
363 if (ACE_OS::strcmp (&hostaddr[0], ipv4_addresses[i]) != 0)
365 ACE_ERROR ((LM_ERROR,
366 ACE_TEXT ("%C failed get_host_addr(char*,int) check\n")
367 ACE_TEXT ("buf ['%C'] != expected value ['%C']\n"),
368 ipv4_addresses[i],
369 &hostaddr[0],
370 ipv4_addresses[i]));
371 status = 1;
374 // Clear out the address by setting it to 1 and check
375 addr.set (0, ACE_UINT32 (1), 1);
376 status |= check_type_consistency (addr);
377 if (addr.get_ip_address () != 1)
379 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Failed to set address to 1\n")));
380 status = 1;
383 // Now set the address using a 32 bit number and check that we get
384 // the right string out of get_host_addr().
385 addr.set (80, addr32, 0); // addr32 is already in network byte order
386 status |= check_type_consistency(addr);
388 if (addr.get_host_addr () != 0 &&
389 ACE_OS::strcmp (addr.get_host_addr (), ipv4_addresses[i]) != 0)
391 ACE_ERROR ((LM_ERROR,
392 ACE_TEXT ("%C failed second get_host_addr() check\n")
393 ACE_TEXT ("return value ['%C'] != expected value ['%C']\n"),
394 ipv4_addresses[i],
395 addr.get_host_addr (),
396 ipv4_addresses[i]));
397 status = 1;
400 // Test for ACE_INET_Addr::set_addr().
401 struct sockaddr_in sa4;
402 sa4.sin_family = AF_INET;
403 sa4.sin_addr = addrv4;
404 sa4.sin_port = ACE_HTONS (8080);
406 addr.set (0, ACE_UINT32 (1), 1);
407 addr.set_addr (&sa4, sizeof(sa4));
408 status |= check_type_consistency (addr);
410 if (addr.get_port_number () != 8080)
412 ACE_ERROR ((LM_ERROR,
413 ACE_TEXT ("ACE_INET_Addr::set_addr() ")
414 ACE_TEXT ("failed to update port number.\n")));
415 status = 1;
418 if (addr.get_ip_address () != ACE_HTONL (addr32))
420 ACE_ERROR ((LM_ERROR,
421 ACE_TEXT ("ACE_INET_Addr::set_addr() ")
422 ACE_TEXT ("failed to update address.\n")));
423 status = 1;
428 #if defined (ACE_HAS_IPV6)
429 if (ACE::ipv6_enabled ())
431 char local_host_name[1024];
432 ACE_OS::hostname(local_host_name, 1024);
433 const char* local_names[] = {"localhost", local_host_name};
435 for (int i = 0; i < 2; ++i)
437 ACE_INET_Addr addr;
438 int old_type = addr.get_type();
439 if (addr.set(12345, local_names[i]) == 0) {
440 if (addr.get_type() != old_type) {
441 ACE_ERROR ((LM_ERROR,
442 ACE_TEXT ("IPv6 set failed: addr.set(12345, \"%C\"), old addr.type() = %d, new addr_type()= %d\n"),
443 local_names[i],
444 old_type,
445 addr.get_type ()));
446 status = 1;
449 else {
450 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("IPv6 set failed: addr.set(12345, \"%C\") returns nonzero\n"), local_names[i]));
454 const char *ipv6_addresses[] = {
455 "1080::8:800:200c:417a", // unicast address
456 "ff01::101", // multicast address
457 "::1", // loopback address
458 "::", // unspecified addresses
462 for (int i=0; ipv6_addresses[i] != 0; i++)
464 ACE_INET_Addr addr (80, ipv6_addresses[i]);
465 status |= check_type_consistency (addr);
467 if (0 != ACE_OS::strcmp (addr.get_host_addr (), ipv6_addresses[i]))
469 ACE_ERROR ((LM_ERROR,
470 ACE_TEXT ("IPv6 get_host_addr failed: %C != %C\n"),
471 addr.get_host_addr (),
472 ipv6_addresses[i]));
473 status = 1;
477 const char *ipv6_names[] = {
478 "naboo.dre.vanderbilt.edu",
479 "v6.ipv6-test.com",
482 for (int i=0; ipv6_names[i] != 0; i++)
484 ACE_INET_Addr addr (80, ipv6_names[i]);
485 status |= check_type_consistency (addr);
487 if (0 != ACE_OS::strcmp (addr.get_host_name (), ipv6_names[i]))
489 // Alias? Check lookup on the reverse.
490 ACE_INET_Addr alias_check;
491 if (alias_check.set (80, addr.get_host_name ()) == 0)
493 if (addr != alias_check)
494 ACE_ERROR ((LM_WARNING,
495 ACE_TEXT ("IPv6 name mismatch: %s (%s) != %s\n"),
496 addr.get_host_name (),
497 addr.get_host_addr (),
498 ipv6_names[i]));
500 else
502 ACE_ERROR ((LM_WARNING,
503 ACE_TEXT ("IPv6 reverse lookup mismatch: %s (%s) != %s\n"),
504 addr.get_host_name (),
505 addr.get_host_addr (),
506 ipv6_names[i]));
511 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("IPv6 tests done\n")));
512 #else
513 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_HAS_IPV6 not set; no IPv6 tests run\n")));
514 #endif
516 struct Address loopback_addresses[] =
517 { {"127.0.0.1", true}, {"127.1.2.3", true}
518 , {"127.0.0.0", true}, {"127.255.255.255", true}
519 , {"126.255.255.255", false}, {"128.0.0.0", false}, {0, true}
522 for (int i=0; loopback_addresses[i].name != 0; i++)
524 struct in_addr addrv4;
525 ACE_UINT32 addr32 = 0;
527 ACE_OS::inet_pton (AF_INET, loopback_addresses[i].name, &addrv4);
529 ACE_OS::memcpy (&addr32, &addrv4, sizeof (addr32));
531 addr.set (80, loopback_addresses[i].name);
533 if (addr.is_loopback() != loopback_addresses[i].loopback)
535 ACE_ERROR ((LM_ERROR,
536 ACE_TEXT ("ACE_INET_Addr::is_loopback() ")
537 ACE_TEXT ("failed to distinguish loopback address. %C\n")
538 , loopback_addresses[i].name));
539 status = 1;
543 if (addr.string_to_addr ("127.0.0.1:72000", AF_INET) != -1)
545 ACE_ERROR ((LM_ERROR,
546 ACE_TEXT ("ACE_INET_Addr::string_to_addr() ")
547 ACE_TEXT ("failed to detect port number overflow\n")));
548 status = 1;
551 if (!test_tao_use ())
552 status = 1;
554 if (!test_multiple ())
555 status = 1;
557 if (!test_port_assignment ())
558 status = 1;
560 ACE_INET_Addr a1 (80, "127.0.0.1");
561 ACE_INET_Addr a2 = a1;
562 if (a1 != a2)
564 ACE_ERROR ((LM_ERROR,
565 ACE_TEXT ("Address equality check failed after assignment\n")));
566 status = 1;
569 ACE_END_TEST;
571 return status;