2 //=============================================================================
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
27 // Host candidate list
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.
42 host_is_up (ACE_TCHAR hostname
[])
44 ACE_SOCK_Connector con
;
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
,
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
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;
85 h
= ACE_OS::gethostbyname (un
.nodename
);
88 ACE_OS::strcpy (other_host
, ACE_LOCALHOST
);
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.
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)
109 if (ACE_OS::strcmp (h
->h_name
, "loopback") == 0)
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
)
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
);
134 #endif /* ! ACE_LACKS_GETHOSTENT */
136 ACE_OS::strcpy (cached_other_host
, other_host
);
139 ACE_OS::strcpy (other_host
, cached_other_host
);
143 fail_no_listener_nonblocking ()
145 ACE_TCHAR test_host
[MAXHOSTNAMELEN
], test_addr
[MAXHOSTNAMELEN
+ 8];
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"),
158 ACE_TEXT ("failed")));
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.
171 ACE_ERROR ((LM_ERROR
,
172 ACE_TEXT ("Connect which should fail didn't\n")));
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
);
189 ACE_ERROR ((LM_ERROR
,
190 ACE_TEXT ("Connect which should fail didn't\n")));
195 ACE_DEBUG ((LM_DEBUG
,
197 ACE_TEXT ("Proper fail")));
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
));
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.
225 succeed_nonblocking ()
227 ACE_TCHAR test_host
[MAXHOSTNAMELEN
], test_addr
[MAXHOSTNAMELEN
+ 8];
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"),
247 ACE_TEXT ("failed")));
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")));
265 if (sock
.get_handle () != ACE_INVALID_HANDLE
)
267 status
= con
.complete (sock
);
272 // Reset the status _before_ doing the printout, in case the
273 // printout overwrites errno.
274 if (errno
== ECONNREFUSED
)
277 ACE_DEBUG ((LM_DEBUG
,
278 ACE_TEXT ("Should succeed, but refused: ok\n")));
282 ACE_ERROR ((LM_ERROR
,
283 ACE_TEXT("Errno <%d>: %p\n"),
285 ACE_TEXT("connect should succeed, but")));
290 ACE_TEXT("Connect which should succeed, did\n")));
300 run_main (int, ACE_TCHAR
*[])
302 ACE_START_TEST (ACE_TEXT ("SOCK_Connector_Test"));
306 if (fail_no_listener_nonblocking () == -1)
309 if (succeed_nonblocking () == -1)