Compile fixes
[ACE_TAO.git] / ACE / tests / SOCK_Acceptor_Test.cpp
blobc433656c80333fef3e1d5ca5030699dc34ebe32c
2 //=============================================================================
3 /**
4 * @file SOCK_Acceptor_Test.cpp
6 * This is a test of the <ACE_SOCK_Acceptor> class.
8 * @author Steve Huston <shuston@riverace.com>
9 */
10 //=============================================================================
12 #include "test_config.h"
13 #include "ace/OS_NS_unistd.h"
14 #include "ace/OS_NS_sys_wait.h"
15 #include "ace/Time_Value.h"
16 #include "ace/SOCK_Connector.h"
17 #include "ace/SOCK_Acceptor.h"
19 static int
20 test_accept (ACE_Addr listen_addr, int v6_only)
22 int status = 0;
24 ACE_DEBUG ((LM_DEBUG,
25 ACE_TEXT ("(%P|%t) starting listen, ipv6-only %d\n"),
26 v6_only));
27 // Bind listener to any port and then find out what the port was.
28 ACE_SOCK_Acceptor acceptor;
29 ACE_INET_Addr listening_at;
30 if (acceptor.open (listen_addr,
32 PF_UNSPEC,
33 ACE_DEFAULT_BACKLOG,
35 v6_only) == -1
36 || acceptor.get_local_addr (listening_at) == -1)
37 ACE_ERROR_RETURN ((LM_ERROR,
38 ACE_TEXT ("(%P|%t) %p\n"),
39 ACE_TEXT ("open")),
40 -1);
41 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) listening at port %d\n"),
42 listening_at.get_port_number ()));
44 // Try IPv4 and, if v6 is available, v6. If v6-capable and v6-only
45 // is selected, v4 should NOT work and v6 should. If not v6-capable
46 // just try v4 and it should work regardless of v6-only.
47 ACE_SOCK_Connector connector;
49 ACE_INET_Addr v4_addr (listening_at.get_port_number(),
50 ACE_LOCALHOST,
51 PF_INET);
52 ACE_SOCK_Stream v4_stream;
53 bool v4_ok = (connector.connect (v4_stream, v4_addr) == 0);
54 ACE_DEBUG ((LM_DEBUG,
55 ACE_TEXT ("v4 connect %s\n"),
56 v4_ok ? ACE_TEXT ("OK") : ACE_TEXT ("FAIL")));
57 v4_stream.close();
59 #if defined (ACE_HAS_IPV6)
60 ACE_INET_Addr v6_addr (listening_at.get_port_number(),
61 ACE_IPV6_LOCALHOST,
62 PF_INET6);
63 ACE_SOCK_Stream v6_stream;
64 bool v6_ok = (connector.connect (v6_stream, v6_addr) == 0);
65 ACE_DEBUG ((LM_DEBUG,
66 ACE_TEXT ("v6 connect %s\n"),
67 v6_ok ? ACE_TEXT ("OK") : ACE_TEXT ("FAIL")));
68 v6_stream.close();
70 if (v6_only && v4_ok)
71 status = 1;
72 if (!v6_only && !v4_ok)
73 status = 1;
74 if (!v6_ok)
75 status = 1;
76 #else
77 status = v4_ok ? 0 : 1;
78 #endif /* ACE_HAS_IPV6 */
79 acceptor.close ();
80 return status;
83 int
84 run_main (int, ACE_TCHAR *[])
86 ACE_START_TEST (ACE_TEXT ("SOCK_Acceptor_Test"));
88 int status = 0;
90 if (test_accept (ACE_Addr::sap_any, 1) != 0)
91 status = 1;
92 if (test_accept (ACE_Addr::sap_any, 0) != 0)
93 status = 1;
95 ACE_END_TEST;
96 return status;