merge the formfield patch from ooo-build
[ooovba.git] / sal / qa / osl / socket / osl_AcceptorSocket.cxx
blob830feb6457d28982b9df28e27c56a7691d55351b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: osl_AcceptorSocket.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 /** test coder preface:
35 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
36 if you are not including ws2_32.lib in makefile.mk, the including format will be like this:
38 .IF "$(GUI)" == "WNT"
39 SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
40 SHL1STDLIBS += ws2_32.lib
41 .ENDIF
43 likewise on Solaris platform.
44 .IF "$(GUI)" == "UNX"
45 SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
46 SHL1STDLIBS += -lsocket -ldl -lnsl
47 .ENDIF
49 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
50 category.
52 3. some fragment of Socket source implementation are lack of comment so it is hard for testers
53 guess what the exact functionality or usage of a member. Hope the Socket section's comment
54 will be added.
56 4. following functions are declared but not implemented:
57 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
60 //------------------------------------------------------------------------
61 // include files
62 //------------------------------------------------------------------------
64 #include <cppunit/simpleheader.hxx>
66 #include "osl_Socket_Const.h"
67 #include "sockethelper.hxx"
69 using namespace osl;
70 using namespace rtl;
72 #define IP_PORT_FTP 21
73 #define IP_PORT_MYPORT9 8897
74 #define IP_PORT_MYPORT4 8885
75 #define IP_PORT_MYPORT3 8884
77 //------------------------------------------------------------------------
78 // helper functions
79 //------------------------------------------------------------------------
81 // just used to test socket::close() when accepting
82 class AcceptorThread : public Thread
84 ::osl::AcceptorSocket asAcceptorSocket;
85 ::rtl::OUString aHostIP;
86 sal_Bool bOK;
87 protected:
88 void SAL_CALL run( )
90 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
91 ::osl::StreamSocket ssStreamConnection;
93 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
94 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
95 if ( sal_True != bOK1 )
97 t_print("# AcceptorSocket bind address failed.\n" ) ;
98 return;
100 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
101 if ( sal_True != bOK2 )
103 t_print("# AcceptorSocket listen address failed.\n" ) ;
104 return;
107 asAcceptorSocket.enableNonBlockingMode( sal_False );
109 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
110 if (eResult != osl_Socket_Ok )
112 bOK = sal_True;
113 t_print("AcceptorThread: acceptConnection failed! \n");
116 public:
117 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
118 : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
120 bOK = sal_False;
123 sal_Bool isOK() { return bOK; }
125 ~AcceptorThread( )
127 if ( isRunning( ) )
129 asAcceptorSocket.shutdown();
130 t_print("# error: Acceptor thread not terminated.\n" );
135 namespace osl_AcceptorSocket
138 /** testing the methods:
139 inline AcceptorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
140 oslProtocol Protocol = osl_Socket_ProtocolIp,
141 oslSocketType Type = osl_Socket_TypeStream);
144 class ctors : public CppUnit::TestFixture
146 public:
148 void ctors_001()
150 /// Socket constructor.
151 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
153 CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the acceptor socket was created successfully.",
154 osl_Socket_TypeStream == asSocket.getType( ) );
157 CPPUNIT_TEST_SUITE( ctors );
158 CPPUNIT_TEST( ctors_001 );
159 CPPUNIT_TEST_SUITE_END();
161 }; // class ctors
163 #if 0 /* OBSOLETE */
164 class operator_assign : public CppUnit::TestFixture
166 public:
168 void assign_001()
170 #if defined(LINUX)
171 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
172 ::osl::AcceptorSocket asSocketAssign( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
173 asSocket.setOption( osl_Socket_OptionReuseAddr, 1);
174 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT4 );
175 asSocket.bind( saSocketAddr );
177 AcceptorThread myAcceptorThread( asSocketAssign, rtl::OUString::createFromAscii("127.0.0.1") );
178 myAcceptorThread.create();
180 thread_sleep( 1 );
181 //when accepting, assign another socket to the socket, the thread will not be closed, so is blocking
182 asSocketAssign = asSocket;
184 t_print("#asSocketAssign port number is %d\n", asSocketAssign.getLocalPort() );
186 asSocketAssign.shutdown();
187 myAcceptorThread.join();
189 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
190 myAcceptorThread.isOK() == sal_True );
193 #endif /* LINUX */
197 CPPUNIT_TEST_SUITE( operator_assign );
198 CPPUNIT_TEST( assign_001 );
199 CPPUNIT_TEST_SUITE_END();
201 }; // class operator_assign
202 #endif /* OBSOLETE */
204 /** testing the method:
205 inline sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1);
206 inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection);
207 inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr);
210 class listen_accept : public CppUnit::TestFixture
212 public:
213 TimeValue *pTimeout;
214 ::osl::AcceptorSocket asAcceptorSocket;
215 ::osl::ConnectorSocket csConnectorSocket;
218 // initialization
219 void setUp( )
221 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
222 pTimeout->Seconds = 3;
223 pTimeout->Nanosec = 0;
224 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1);
225 // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
228 void tearDown( )
230 free( pTimeout );
231 // sHandle = NULL;
232 asAcceptorSocket.close( );
233 csConnectorSocket.close( );
237 void listen_accept_001()
239 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 );
240 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 );
241 ::osl::StreamSocket ssConnection;
243 /// launch server socket
244 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
245 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
246 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
247 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
248 asAcceptorSocket.enableNonBlockingMode( sal_True );
250 /// launch client socket
251 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
253 oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
255 CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept.",
256 ( osl_Socket_Ok == eResult ) );
259 void listen_accept_002()
261 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT4 );
262 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT4 );
263 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
264 ::osl::StreamSocket ssConnection;
266 /// launch server socket
267 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
268 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
269 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
270 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
271 asAcceptorSocket.enableNonBlockingMode( sal_True );
273 /// launch client socket
274 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
276 oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection, saPeerSocketAddr); /// waiting for incoming connection...
278 CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept, accept with peer address.",
279 ( sal_True == bOK2 ) &&
280 ( osl_Socket_Ok == eResult ) &&
281 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) );
285 CPPUNIT_TEST_SUITE( listen_accept );
286 CPPUNIT_TEST( listen_accept_001 );
287 CPPUNIT_TEST( listen_accept_002 );
288 CPPUNIT_TEST_SUITE_END();
290 }; // class listen_accept
293 // -----------------------------------------------------------------------------
295 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::ctors, "osl_AcceptorSocket");
296 //CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::operator_assign, "osl_AcceptorSocket");
297 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::listen_accept, "osl_AcceptorSocket");
299 } // namespace osl_AcceptorSocket
301 // -----------------------------------------------------------------------------
303 // this macro creates an empty function, which will called by the RegisterAllFunctions()
304 // to let the user the possibility to also register some functions by hand.
305 NOADDITIONAL;