Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / INET_Addr_Test_IPV6.cpp
blob097e03c9d262b84f64cb6b338975dbf045c322fc
1 // ============================================================================
2 /**
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)
30 int family = -1;
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)
45 ACE_ERROR ((LM_ERROR,
46 ACE_TEXT ("Inconsistency between ACE_SOCK::addr_type_ (%d) ")
47 ACE_TEXT ("and the sockaddr family (%d)\n"),
48 addr.get_type (),
49 family));
50 return 1;
52 return 0;
55 // A test to ensure ACE_INET_Addr can select the protocol family.
56 static bool check_both_families()
58 bool good = true;
59 ACE_INET_Addr a;
60 if (-1 == a.set(ACE_TEXT (""), ACE_TEXT ("www.google.com"), ACE_TEXT ("tcp")))
62 ACE_ERROR ((LM_ERROR,
63 ACE_TEXT ("%p\n"),
64 ACE_TEXT ("both test 4, www.google.com")));
65 good = false;
67 else
69 ACE_TCHAR str[1000];
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)
75 ACE_ERROR ((LM_ERROR,
76 ACE_TEXT ("Wrong address family, expecting IPv4\n")));
77 good = false;
81 ACE_INET_Addr b;
82 if (-1 == b.set(ACE_TEXT(""), ACE_TEXT("www.google.com"), ACE_TEXT("tcp6")))
84 ACE_ERROR((LM_ERROR,
85 ACE_TEXT("%p\n"),
86 ACE_TEXT("both test 6, www.google.com")));
87 good = false;
89 else
91 ACE_TCHAR str[1000];
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)
97 ACE_ERROR((LM_ERROR,
98 ACE_TEXT("Wrong address family, expecting IPv6\n")));
99 good = false;
102 return good;
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
119 addr.set (80);
121 if (AF_INET != addr.get_type())
123 ACE_ERROR ((LM_ERROR,
124 ACE_TEXT ("set failed: Address type %d != AF_INET\n"),
125 addr.get_type()));
126 status = 1;
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"),
136 addr.get_type()));
137 status = 1;
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,
158 ACE_TEXT
159 ("set_addr failed: Address type %d != AF_INET\n"),
160 addr.get_type()));
161 status = 1;
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,
170 ACE_TEXT
171 ("set_addr failed: Address type %d != AF_INET6\n"),
172 addr.get_type()));
173 status = 1;
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);
182 addr.set (addrIPv6);
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")));
193 status = 1;
196 if (0 != ACE_OS::strcmp (ACE_TEXT_CHAR_TO_TCHAR(link_local_addr.get_host_addr ()),
197 LINK_LOCAL_ADDR))
199 ACE_ERROR ((LM_ERROR,
200 ACE_TEXT ("IPv6 get_host_addr failed: %s != %s\n"),
201 link_local_addr.get_host_addr (),
202 LINK_LOCAL_ADDR));
203 status = 1;
206 if (-1 == link_local_addr.set_interface (THE_INTERFACE))
208 ACE_ERROR ((LM_ERROR,
209 ACE_TEXT ("%p\n"),
210 ACE_TEXT ("IPv6 set_interface failed\n")));
211 status = 1;
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")));
220 status = 1;
223 #endif /* ACE_LINUX */
226 if (!check_both_families())
227 status = 1;
229 #endif /* ACE_HAS_IPV6 */
231 ACE_END_TEST;
233 return status;