1 // ============================================================================
3 * @file INET_Addr_Test_IPV6.cpp
5 * @brief Additional tests on the ACE_INET_Addr class above and beyond
6 * those found in INET_Addr_Test. These primarily focus on additional
7 * functionality and bug fixes to ACE_INET_Addr.
9 * @author John Aughey <jha@aughey.com>
10 * Brian Buesker <bbuesker@qualcomm.com>
12 // ============================================================================
14 #include "test_config.h"
15 #include "ace/OS_NS_string.h"
16 #include "ace/INET_Addr.h"
17 #include "ace/Log_Msg.h"
18 #include "ace/OS_NS_arpa_inet.h"
20 #if defined (ACE_HAS_IPV6)
22 #define LINK_LOCAL_ADDR ACE_TEXT ("fe80::")
23 #define SITE_LOCAL_ADDR ACE_TEXT ("fec0::")
24 #define THE_INTERFACE ("eth0")
26 // Make sure that ACE_Addr::addr_type_ is the same
27 // as the family of the inet_addr_.
28 static int check_type_consistency (const ACE_INET_Addr
&addr
)
32 if (addr
.get_type () == AF_INET
)
34 struct sockaddr_in
*sa4
= (struct sockaddr_in
*)addr
.get_addr();
35 family
= sa4
->sin_family
;
37 else if (addr
.get_type () == AF_INET6
)
39 struct sockaddr_in6
*sa6
= (struct sockaddr_in6
*)addr
.get_addr();
40 family
= sa6
->sin6_family
;
43 if (addr
.get_type () != family
)
46 ACE_TEXT ("Inconsistency between ACE_SOCK::addr_type_ (%d) ")
47 ACE_TEXT ("and the sockaddr family (%d)\n"),
55 // A test to ensure ACE_INET_Addr can select the protocol family.
56 static bool check_both_families()
60 if (-1 == a
.set(ACE_TEXT (""), ACE_TEXT ("www.google.com"), ACE_TEXT ("tcp")))
64 ACE_TEXT ("both test 4, www.google.com")));
70 a
.addr_to_string (str
, 1000, 1);
71 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("got type %d, addr %s\n"), a
.get_type(), str
));
72 // Should have selected IPv4.
73 if (a
.get_type() != AF_INET
)
76 ACE_TEXT ("Wrong address family, expecting IPv4\n")));
82 if (-1 == b
.set(ACE_TEXT(""), ACE_TEXT("www.google.com"), ACE_TEXT("tcp6")))
86 ACE_TEXT("both test 6, www.google.com")));
92 b
.addr_to_string(str
, 1000, 1);
93 ACE_DEBUG((LM_DEBUG
, ACE_TEXT("got type %d, addr %s\n"), b
.get_type(), str
));
94 // Should have selected IPv6.
95 if (b
.get_type() != AF_INET6
)
98 ACE_TEXT("Wrong address family, expecting IPv6\n")));
104 #endif /* ACE_HAS_IPV6 */
107 int run_main (int, ACE_TCHAR
*[])
109 ACE_START_TEST (ACE_TEXT ("INET_Addr_Test_IPV6"));
111 int status
= 0; // Innocent until proven guilty
113 #if defined (ACE_HAS_IPV6)
114 if (ACE::ipv6_enabled ())
116 ACE_INET_Addr
addr ("::");
118 // this should switch it back to an IPv4 address
121 if (AF_INET
!= addr
.get_type())
123 ACE_ERROR ((LM_ERROR
,
124 ACE_TEXT ("set failed: Address type %d != AF_INET\n"),
129 // this should get mapped to an IPv6 address
130 addr
.set (80, ACE_UINT32 (INADDR_ANY
), 1, 1);
132 if (AF_INET6
!= addr
.get_type())
134 ACE_ERROR ((LM_ERROR
,
135 ACE_TEXT ("set failed: Address type %d != AF_INET6\n"),
140 // Test for ACE_INET_Addr::set_addr().
141 struct in_addr addrv4
;
143 ACE_OS::inet_pton (AF_INET
, "127.0.0.1", &addrv4
);
145 struct sockaddr_in sa4
;
146 sa4
.sin_family
= AF_INET
;
147 sa4
.sin_addr
= addrv4
;
148 sa4
.sin_port
= ACE_HTONS (8080);
150 addr
.set (0, ACE_UINT32 (1), 1);
152 // test to make sure this doesn't get mapped to an IPv6 address
153 addr
.set_addr (&sa4
, sizeof(sa4
), 0);
155 if (addr
.get_type() != AF_INET
)
157 ACE_ERROR ((LM_ERROR
,
159 ("set_addr failed: Address type %d != AF_INET\n"),
164 // now test to make sure it does get mapped to an IPv6 address
165 addr
.set_addr (&sa4
, sizeof(sa4
), 1);
167 if (addr
.get_type() != AF_INET6
)
169 ACE_ERROR ((LM_ERROR
,
171 ("set_addr failed: Address type %d != AF_INET6\n"),
176 // test to make sure that the type gets set correctly when set is
177 // called with another ACE_INET_Addr
178 addr
.set (0, ACE_UINT32 (1), 1);
180 ACE_INET_Addr
addrIPv6 ((u_short
) 0, ACE_IPV6_LOCALHOST
);
184 status
|= check_type_consistency (addr
);
186 #if defined (ACE_LINUX)
187 // test a link local address to make sure the set_interface method works
188 ACE_INET_Addr
link_local_addr (80, LINK_LOCAL_ADDR
);
189 if (!link_local_addr
.is_linklocal())
191 ACE_ERROR ((LM_ERROR
,
192 ACE_TEXT ("IPv6 is_linklocal failed\n")));
196 if (0 != ACE_OS::strcmp (ACE_TEXT_CHAR_TO_TCHAR(link_local_addr
.get_host_addr ()),
199 ACE_ERROR ((LM_ERROR
,
200 ACE_TEXT ("IPv6 get_host_addr failed: %s != %s\n"),
201 link_local_addr
.get_host_addr (),
206 if (-1 == link_local_addr
.set_interface (THE_INTERFACE
))
208 ACE_ERROR ((LM_ERROR
,
210 ACE_TEXT ("IPv6 set_interface failed\n")));
214 // test a site local address
215 ACE_INET_Addr
site_local_addr (80, SITE_LOCAL_ADDR
);
216 if (!site_local_addr
.is_sitelocal())
218 ACE_ERROR ((LM_ERROR
,
219 ACE_TEXT ("IPv6 is_sitelocal failed\n")));
223 #endif /* ACE_LINUX */
226 if (!check_both_families())
229 #endif /* ACE_HAS_IPV6 */