xref: /trunk/main/sal/qa/osl/socket/osl_AcceptorSocket.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
187d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
387d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
487d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
587d2adbcSAndrew Rist  * distributed with this work for additional information
687d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
787d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
887d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
987d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1187d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1387d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1487d2adbcSAndrew Rist  * software distributed under the License is distributed on an
1587d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1687d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
1787d2adbcSAndrew Rist  * specific language governing permissions and limitations
1887d2adbcSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2087d2adbcSAndrew Rist  *************************************************************/
2187d2adbcSAndrew Rist 
2287d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /**  test coder preface:
28cdf0e10cSrcweir     1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
29cdf0e10cSrcweir     if you are not including ws2_32.lib in makefile.mk,  the including format will be like this:
30cdf0e10cSrcweir 
31cdf0e10cSrcweir     .IF "$(GUI)" == "WNT"
32cdf0e10cSrcweir     SHL1STDLIBS +=  $(SOLARLIBDIR)$/cppunit.lib
33cdf0e10cSrcweir     SHL1STDLIBS +=  ws2_32.lib
34cdf0e10cSrcweir     .ENDIF
35cdf0e10cSrcweir 
36cdf0e10cSrcweir     likewise on Solaris platform.
37cdf0e10cSrcweir     .IF "$(GUI)" == "UNX"
38cdf0e10cSrcweir     SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
39cdf0e10cSrcweir     SHL1STDLIBS += -lsocket -ldl -lnsl
40cdf0e10cSrcweir     .ENDIF
41cdf0e10cSrcweir 
42cdf0e10cSrcweir     2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
43cdf0e10cSrcweir     category.
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     3. some fragment of Socket source implementation are lack of comment so it is hard for testers
46cdf0e10cSrcweir     guess what the exact functionality or usage of a member.  Hope the Socket section's comment
47cdf0e10cSrcweir     will be added.
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     4. following functions are declared but not implemented:
50cdf0e10cSrcweir     inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
51cdf0e10cSrcweir  */
52cdf0e10cSrcweir 
53cdf0e10cSrcweir //------------------------------------------------------------------------
54cdf0e10cSrcweir // include files
55cdf0e10cSrcweir //------------------------------------------------------------------------
56cdf0e10cSrcweir 
57*63d99982SDamjan Jovanovic #include "gtest/gtest.h"
58cdf0e10cSrcweir 
59cdf0e10cSrcweir #include "osl_Socket_Const.h"
60cdf0e10cSrcweir #include "sockethelper.hxx"
61cdf0e10cSrcweir 
62cdf0e10cSrcweir using namespace osl;
63cdf0e10cSrcweir using namespace rtl;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir #define IP_PORT_FTP     21
66cdf0e10cSrcweir #define IP_PORT_MYPORT9 8897
67cdf0e10cSrcweir #define IP_PORT_MYPORT4 8885
68cdf0e10cSrcweir #define IP_PORT_MYPORT3 8884
69cdf0e10cSrcweir 
70cdf0e10cSrcweir //------------------------------------------------------------------------
71cdf0e10cSrcweir // helper functions
72cdf0e10cSrcweir //------------------------------------------------------------------------
73cdf0e10cSrcweir 
74cdf0e10cSrcweir // just used to test socket::close() when accepting
75cdf0e10cSrcweir class AcceptorThread : public Thread
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     ::osl::AcceptorSocket asAcceptorSocket;
78cdf0e10cSrcweir     ::rtl::OUString aHostIP;
79cdf0e10cSrcweir     sal_Bool bOK;
80cdf0e10cSrcweir protected:
run()81cdf0e10cSrcweir     void SAL_CALL run( )
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
84cdf0e10cSrcweir         ::osl::StreamSocket ssStreamConnection;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
87cdf0e10cSrcweir         sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
88cdf0e10cSrcweir         if  ( sal_True != bOK1 )
89cdf0e10cSrcweir         {
90*63d99982SDamjan Jovanovic             printf("# AcceptorSocket bind address failed.\n" ) ;
91cdf0e10cSrcweir             return;
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir         sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
94cdf0e10cSrcweir         if  ( sal_True != bOK2 )
95cdf0e10cSrcweir         {
96*63d99982SDamjan Jovanovic             printf("# AcceptorSocket listen address failed.\n" ) ;
97cdf0e10cSrcweir             return;
98cdf0e10cSrcweir         }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         asAcceptorSocket.enableNonBlockingMode( sal_False );
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
103cdf0e10cSrcweir         if (eResult != osl_Socket_Ok )
104cdf0e10cSrcweir         {
105cdf0e10cSrcweir             bOK = sal_True;
106*63d99982SDamjan Jovanovic             printf("AcceptorThread: acceptConnection failed! \n");
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir public:
AcceptorThread(::osl::AcceptorSocket & asSocket,::rtl::OUString const & aBindIP)110cdf0e10cSrcweir     AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
111cdf0e10cSrcweir         : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         bOK = sal_False;
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
isOK()116cdf0e10cSrcweir     sal_Bool isOK() { return bOK; }
117cdf0e10cSrcweir 
~AcceptorThread()118cdf0e10cSrcweir     ~AcceptorThread( )
119cdf0e10cSrcweir     {
120cdf0e10cSrcweir         if ( isRunning( ) )
121cdf0e10cSrcweir         {
122cdf0e10cSrcweir             asAcceptorSocket.shutdown();
123*63d99982SDamjan Jovanovic             printf("# error: Acceptor thread not terminated.\n" );
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir };
127cdf0e10cSrcweir 
128cdf0e10cSrcweir namespace osl_AcceptorSocket
129cdf0e10cSrcweir {
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     /** testing the methods:
132cdf0e10cSrcweir         inline AcceptorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
133cdf0e10cSrcweir                               oslProtocol   Protocol = osl_Socket_ProtocolIp,
134cdf0e10cSrcweir                               oslSocketType Type = osl_Socket_TypeStream);
135cdf0e10cSrcweir     */
136cdf0e10cSrcweir 
137*63d99982SDamjan Jovanovic     class ctors : public ::testing::Test
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir     public:
140*63d99982SDamjan Jovanovic     }; // class ctors
141cdf0e10cSrcweir 
TEST_F(ctors,ctors_001)142*63d99982SDamjan Jovanovic     TEST_F(ctors, ctors_001)
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         /// Socket constructor.
145cdf0e10cSrcweir         ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
146cdf0e10cSrcweir 
147*63d99982SDamjan Jovanovic         ASSERT_TRUE(osl_Socket_TypeStream ==  asSocket.getType( )) << "test for ctors_001 constructor function: check if the acceptor socket was created successfully.";
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir #if 0  /* OBSOLETE */
151*63d99982SDamjan Jovanovic     class operator_assign : public ::testing::Test
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir     public:
154*63d99982SDamjan Jovanovic     }; // class operator_assign
155cdf0e10cSrcweir 
156*63d99982SDamjan Jovanovic     TEST_F(assign, assign_001)
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir #if defined(LINUX)
159cdf0e10cSrcweir         ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
160cdf0e10cSrcweir         ::osl::AcceptorSocket asSocketAssign( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
161cdf0e10cSrcweir         asSocket.setOption( osl_Socket_OptionReuseAddr, 1);
162cdf0e10cSrcweir         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT4 );
163cdf0e10cSrcweir         asSocket.bind( saSocketAddr );
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         AcceptorThread myAcceptorThread( asSocketAssign, rtl::OUString::createFromAscii("127.0.0.1") );
166cdf0e10cSrcweir         myAcceptorThread.create();
167cdf0e10cSrcweir 
168cdf0e10cSrcweir         thread_sleep( 1 );
169cdf0e10cSrcweir         //when accepting, assign another socket to the socket, the thread will not be closed, so is blocking
170cdf0e10cSrcweir         asSocketAssign = asSocket;
171cdf0e10cSrcweir 
172*63d99982SDamjan Jovanovic         printf("#asSocketAssign port number is %d\n", asSocketAssign.getLocalPort() );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         asSocketAssign.shutdown();
175cdf0e10cSrcweir         myAcceptorThread.join();
176cdf0e10cSrcweir 
177*63d99982SDamjan Jovanovic         ASSERT_TRUE(myAcceptorThread.isOK() == sal_True) << "test for close when is accepting: the socket will quit accepting status.";
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 
180cdf0e10cSrcweir #endif /* LINUX */
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir #endif /* OBSOLETE */
183cdf0e10cSrcweir 
184cdf0e10cSrcweir     /** testing the method:
185cdf0e10cSrcweir         inline sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1);
186cdf0e10cSrcweir         inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection);
187cdf0e10cSrcweir         inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr);
188cdf0e10cSrcweir     */
189cdf0e10cSrcweir 
190*63d99982SDamjan Jovanovic     class listen_accept : public ::testing::Test
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir     public:
193cdf0e10cSrcweir         TimeValue *pTimeout;
194cdf0e10cSrcweir         ::osl::AcceptorSocket asAcceptorSocket;
195cdf0e10cSrcweir         ::osl::ConnectorSocket csConnectorSocket;
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 
198cdf0e10cSrcweir         // initialization
SetUp()199*63d99982SDamjan Jovanovic         void SetUp( )
200cdf0e10cSrcweir         {
201cdf0e10cSrcweir             pTimeout  = ( TimeValue* )malloc( sizeof( TimeValue ) );
202cdf0e10cSrcweir             pTimeout->Seconds = 3;
203cdf0e10cSrcweir             pTimeout->Nanosec = 0;
204cdf0e10cSrcweir             asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1);
205cdf0e10cSrcweir         //  sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
206cdf0e10cSrcweir         }
207cdf0e10cSrcweir 
TearDown()208*63d99982SDamjan Jovanovic         void TearDown( )
209cdf0e10cSrcweir         {
210cdf0e10cSrcweir             free( pTimeout );
211cdf0e10cSrcweir         //  sHandle = NULL;
212cdf0e10cSrcweir             asAcceptorSocket.close( );
213cdf0e10cSrcweir             csConnectorSocket.close( );
214cdf0e10cSrcweir         }
215*63d99982SDamjan Jovanovic     }; // class listen_accept
216cdf0e10cSrcweir 
TEST_F(listen_accept,listen_accept_001)217*63d99982SDamjan Jovanovic     TEST_F(listen_accept, listen_accept_001)
218cdf0e10cSrcweir     {
219cdf0e10cSrcweir         ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 );
220cdf0e10cSrcweir         ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 );
221cdf0e10cSrcweir         ::osl::StreamSocket ssConnection;
222cdf0e10cSrcweir 
223cdf0e10cSrcweir         /// launch server socket
224cdf0e10cSrcweir         sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
225*63d99982SDamjan Jovanovic         ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
226cdf0e10cSrcweir         sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
227*63d99982SDamjan Jovanovic         ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
228cdf0e10cSrcweir         asAcceptorSocket.enableNonBlockingMode( sal_True );
229cdf0e10cSrcweir 
230cdf0e10cSrcweir         /// launch client socket
231cdf0e10cSrcweir         csConnectorSocket.connect( saTargetSocketAddr, pTimeout );   /// connecting to server...
232cdf0e10cSrcweir 
233cdf0e10cSrcweir         oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
234cdf0e10cSrcweir 
235*63d99982SDamjan Jovanovic         ASSERT_TRUE( ( osl_Socket_Ok == eResult ) )
236*63d99982SDamjan Jovanovic             << "test for listen_accept function: try to create a connection with remote host, using listen and accept.";
237cdf0e10cSrcweir     }
238cdf0e10cSrcweir 
TEST_F(listen_accept,listen_accept_002)239*63d99982SDamjan Jovanovic     TEST_F(listen_accept, listen_accept_002)
240cdf0e10cSrcweir     {
241cdf0e10cSrcweir         ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT4 );
242cdf0e10cSrcweir         ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT4 );
243cdf0e10cSrcweir         ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
244cdf0e10cSrcweir         ::osl::StreamSocket ssConnection;
245cdf0e10cSrcweir 
246cdf0e10cSrcweir         /// launch server socket
247cdf0e10cSrcweir         sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
248*63d99982SDamjan Jovanovic         ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
249cdf0e10cSrcweir         sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
250*63d99982SDamjan Jovanovic         ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
251cdf0e10cSrcweir         asAcceptorSocket.enableNonBlockingMode( sal_True );
252cdf0e10cSrcweir 
253cdf0e10cSrcweir         /// launch client socket
254cdf0e10cSrcweir         csConnectorSocket.connect( saTargetSocketAddr, pTimeout );   /// connecting to server...
255cdf0e10cSrcweir 
256cdf0e10cSrcweir         oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection, saPeerSocketAddr); /// waiting for incoming connection...
257cdf0e10cSrcweir 
258*63d99982SDamjan Jovanovic         ASSERT_TRUE(
259cdf0e10cSrcweir                                 ( sal_True == bOK2 ) &&
260cdf0e10cSrcweir                                 ( osl_Socket_Ok == eResult ) &&
261*63d99982SDamjan Jovanovic                                 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) )
262*63d99982SDamjan Jovanovic             << "test for listen_accept function: try to create a connection with remote host, using listen and accept, accept with peer address.";
263cdf0e10cSrcweir     }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir } // namespace osl_AcceptorSocket
266cdf0e10cSrcweir 
main(int argc,char ** argv)267*63d99982SDamjan Jovanovic int main(int argc, char **argv)
268*63d99982SDamjan Jovanovic {
269*63d99982SDamjan Jovanovic     ::testing::InitGoogleTest(&argc, argv);
270*63d99982SDamjan Jovanovic     return RUN_ALL_TESTS();
271*63d99982SDamjan Jovanovic }
272