Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / SOCK_Connector_Test.cpp
blob2db953b4c2188fa3dc87309dc49351bef549518b
2 //=============================================================================
3 /**
4 * @file SOCK_Connector_Test.cpp
6 * This is a test of ACE_SOCK_Connector, focusing on failure cases more
7 * than on success cases.
9 * @author Steve Huston <shuston@riverace.com>
11 //=============================================================================
14 #include "test_config.h"
15 #include "ace/OS_NS_string.h"
16 #include "ace/INET_Addr.h"
17 #include "ace/SOCK_Connector.h"
18 #include "ace/Time_Value.h"
19 #include "ace/SOCK_Stream.h"
20 #include "ace/OS_NS_sys_utsname.h"
21 #include "ace/OS_NS_netdb.h"
23 #if defined(ACE_LACKS_GETHOSTENT) || defined(ACE_LACKS_SETHOSTENT) || defined(ACE_LACKS_ENDHOSTENT)
24 # define MISSING_HOSTENT_FUNCTIONS
25 #endif
27 // Host candidate list
28 struct Host_Candidate
30 ACE_TCHAR host_name[MAXHOSTNAMELEN];
33 const int MAX_CANDIDATES = 50;
34 Host_Candidate candidate[MAX_CANDIDATES];
36 #ifndef MISSING_HOSTENT_FUNCTIONS
37 // Determine if a host exists, is reachable, and is up. Attempt a
38 // blocking connection to it; if it succeeds, then the host exists, is
39 // reachable, and is up.
41 static u_int
42 host_is_up (ACE_TCHAR hostname[])
44 ACE_SOCK_Connector con;
45 ACE_SOCK_Stream sock;
47 // The ACE_INET_Addr construction causes gethostbyname_r to be
48 // called, so we need to copy the hostname.
49 ACE_TCHAR test_host[MAXHOSTNAMELEN];
50 ACE_OS::strcpy (test_host, hostname);
52 ACE_INET_Addr another_host ((u_short) 7, test_host);
53 ACE_Time_Value timeout_value (5);
54 int const status = con.connect (sock,
55 another_host,
56 &timeout_value);
57 sock.close ();
58 return status == 0 ? 1 : 0;
60 #endif /* ! ACE_LACKS_GETHOSTENT */
62 // The original problem this program tested for was incorrectly saying
63 // a non-blocking connect completed successfully when it didn't. The
64 // test doesn't always work when done to localhost
65 // (platform-dependent) so we look around for another host - any other
66 // one will do.
68 static void
69 find_another_host (ACE_TCHAR other_host[])
71 static ACE_TCHAR cached_other_host[MAXHOSTNAMELEN] = {'\0'};
73 if (cached_other_host[0] == '\0')
75 ACE_OS::strcpy (other_host,
76 ACE_DEFAULT_SERVER_HOST); // If all else fails
78 #ifndef MISSING_HOSTENT_FUNCTIONS
79 // These gethost-type things don't work everywhere.
80 struct hostent *h = 0;
81 ACE_utsname un;
83 ACE_OS::uname (&un);
85 h = ACE_OS::gethostbyname (un.nodename);
87 if (h == 0)
88 ACE_OS::strcpy (other_host, ACE_LOCALHOST);
89 else
90 // Use me if can't find another
91 ACE_OS::strcpy (other_host, ACE_TEXT_CHAR_TO_TCHAR (h->h_name));
93 // @@ We really need to add wrappers for these hostent methods.
95 // Optimize for sequential access of DNS or hosts file.
96 sethostent (1);
98 int candidate_count = 0;
100 // Accumulate candidates first. There is some interaction on
101 // Linux systems between <gethostent> and <gethostbyname_r>
102 // (called by ACE_INET_Addr in host_is_up) This otherwise causes
103 // an infinite loop on Linux --mas 03-08-2001
104 while ((h = gethostent ()) != 0)
106 if (ACE_OS::strcmp (h->h_name, ACE_TEXT_ALWAYS_CHAR (ACE_DEFAULT_SERVER_HOST)) == 0)
107 continue;
109 if (ACE_OS::strcmp (h->h_name, "loopback") == 0)
110 continue;
112 // If not me.
113 if (ACE_OS::strcmp
114 (h->h_name, ACE_TEXT_ALWAYS_CHAR (other_host)) != 0
115 && ACE_OS::strcmp (h->h_name, un.nodename) != 0)
117 ACE_OS::strcpy (candidate[candidate_count].host_name,
118 ACE_TEXT_CHAR_TO_TCHAR (h->h_name));
119 if (++candidate_count >= MAX_CANDIDATES)
120 break;
124 // Now try to connect to candidates
125 for (int i = 0; i < candidate_count; i++)
126 if (host_is_up (candidate[i].host_name))
128 ACE_OS::strcpy (other_host, candidate[i].host_name);
129 break;
132 sethostent (0);
133 endhostent ();
134 #endif /* ! ACE_LACKS_GETHOSTENT */
136 ACE_OS::strcpy (cached_other_host, other_host);
138 else
139 ACE_OS::strcpy (other_host, cached_other_host);
142 static int
143 fail_no_listener_nonblocking ()
145 ACE_TCHAR test_host[MAXHOSTNAMELEN], test_addr[MAXHOSTNAMELEN + 8];
146 int status;
147 ACE_INET_Addr nobody_home;
148 ACE_SOCK_Connector con;
149 ACE_SOCK_Stream sock;
150 ACE_Time_Value nonblock (0, 0);
152 find_another_host (test_host);
153 if (nobody_home.set ((u_short) 42000, test_host) == -1)
155 ACE_ERROR ((LM_ERROR,
156 ACE_TEXT ("Host lookup for %s %p\n"),
157 test_host,
158 ACE_TEXT ("failed")));
159 return -1;
161 nobody_home.addr_to_string (test_addr, MAXHOSTNAMELEN + 8);
162 ACE_DEBUG ((LM_DEBUG,
163 ACE_TEXT ("Testing to host \"%s\" (%s)\n"),
164 test_host, test_addr));
166 status = con.connect (sock, nobody_home, &nonblock);
168 // Need a port that will fail.
169 if (status == 0)
171 ACE_ERROR ((LM_ERROR,
172 ACE_TEXT ("Connect which should fail didn't\n")));
173 status = -1;
176 // On some systems, a failed connect to localhost will return
177 // ECONNREFUSED or ENETUNREACH directly, instead of
178 // EWOULDBLOCK. That is also fine.
180 else if (errno == EWOULDBLOCK ||
181 errno == ECONNREFUSED ||
182 errno == ENETUNREACH)
184 if (sock.get_handle () != ACE_INVALID_HANDLE)
185 status = con.complete (sock);
187 if (status != -1)
189 ACE_ERROR ((LM_ERROR,
190 ACE_TEXT ("Connect which should fail didn't\n")));
191 status = -1;
193 else
195 ACE_DEBUG ((LM_DEBUG,
196 ACE_TEXT ("%p\n"),
197 ACE_TEXT ("Proper fail")));
198 status = 0;
201 else
203 ACE_DEBUG ((LM_WARNING,
204 ACE_TEXT ("Test not executed fully; ")
205 ACE_TEXT ("expected EWOULDBLOCK, %p (%d)\n"),
206 ACE_TEXT ("not"), ACE_ERRNO_GET));
207 status = -1;
210 // Just in case.
211 sock.close ();
213 return status;
217 // This test tries to hit a port that's listening. Echo (7) is pretty
218 // popular. Just in case, though, it won't report a failure if it
219 // gets "refused" (no listener) since the real fixed bug this is
220 // testing is a returned error of EWOULDBLOCK when the connect really
221 // did work. That was a side-affect of how
222 // <ACE::handle_timed_complete> does checks on some systems.
224 static int
225 succeed_nonblocking ()
227 ACE_TCHAR test_host[MAXHOSTNAMELEN], test_addr[MAXHOSTNAMELEN + 8];
228 int status;
229 ACE_INET_Addr echo_server;
230 ACE_SOCK_Connector con;
231 ACE_SOCK_Stream sock;
232 ACE_Time_Value nonblock (0, 0);
233 u_short test_port = 7; // Echo
235 find_another_host (test_host);
236 if (ACE_OS::strcmp (ACE_TEXT ("localhost"), test_host) == 0)
238 #if defined (ACE_WIN32)
239 test_port = 80; // Echo not available on Win32; try web server
240 #endif /* ACE_WIN32 */
242 if (echo_server.set (test_port, test_host) == -1)
244 ACE_ERROR ((LM_ERROR,
245 ACE_TEXT ("Host lookup for %s %p\n"),
246 test_host,
247 ACE_TEXT ("failed")));
248 return -1;
250 echo_server.addr_to_string (test_addr, MAXHOSTNAMELEN + 8);
251 ACE_DEBUG ((LM_DEBUG,
252 ACE_TEXT ("Testing to host \"%s\", port %d (%s)\n"),
253 test_host, test_port, test_addr));
254 status = con.connect (sock, echo_server, &nonblock);
256 // Need to test the call to 'complete' really.
257 if (status == 0 || (status == -1 && errno != EWOULDBLOCK))
259 ACE_DEBUG((LM_WARNING,
260 ACE_TEXT ("Immediate success/fail; test not completed\n")));
261 status = 0;
263 else
265 if (sock.get_handle () != ACE_INVALID_HANDLE)
267 status = con.complete (sock);
270 if (status == -1)
272 // Reset the status _before_ doing the printout, in case the
273 // printout overwrites errno.
274 if (errno == ECONNREFUSED)
276 status = 0;
277 ACE_DEBUG ((LM_DEBUG,
278 ACE_TEXT ("Should succeed, but refused: ok\n")));
280 else
282 ACE_ERROR ((LM_ERROR,
283 ACE_TEXT("Errno <%d>: %p\n"),
284 ACE_ERRNO_GET,
285 ACE_TEXT("connect should succeed, but")));
288 else
289 ACE_DEBUG((LM_DEBUG,
290 ACE_TEXT("Connect which should succeed, did\n")));
293 // Just in case.
294 sock.close ();
296 return status;
300 run_main (int, ACE_TCHAR *[])
302 ACE_START_TEST (ACE_TEXT ("SOCK_Connector_Test"));
304 int status = 0;
306 if (fail_no_listener_nonblocking () == -1)
307 status = 1;
309 if (succeed_nonblocking () == -1)
310 status = 1;
312 ACE_END_TEST;
313 return status;