update dev300-m57
[ooovba.git] / sal / qa / osl / socket / osl_ConnectorSocket.cxx
blobf18ac53787efc9473aebac1a8990961ee1a01d2b
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_ConnectorSocket.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_MYPORT2 8883
73 #define IP_PORT_FTP 21
74 #define IP_PORT_MYPORT3 8884
76 //------------------------------------------------------------------------
77 // helper functions
78 //------------------------------------------------------------------------
80 class CloseSocketThread : public Thread
82 ::osl::Socket &m_sSocket;
83 protected:
84 void SAL_CALL run( )
86 thread_sleep( 1 );
87 m_sSocket.close( );
89 public:
90 CloseSocketThread(::osl::Socket & sSocket )
91 : m_sSocket( sSocket )
95 ~CloseSocketThread( )
97 if ( isRunning( ) )
99 t_print("# error: CloseSocketThread not terminated.\n" );
104 namespace osl_ConnectorSocket
107 /** testing the method:
108 ConnectorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
109 oslProtocol Protocol = osl_Socket_ProtocolIp,
110 oslSocketType Type = osl_Socket_TypeStream);
113 class ctors : public CppUnit::TestFixture
115 public:
116 void ctors_001()
118 /// Socket constructor.
119 ::osl::ConnectorSocket csSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
121 CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the connector socket was created successfully.",
122 osl_Socket_TypeStream == csSocket.getType( ) );
125 CPPUNIT_TEST_SUITE( ctors );
126 CPPUNIT_TEST( ctors_001 );
127 CPPUNIT_TEST_SUITE_END();
129 }; // class ctors
131 /** testing the method:
132 oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0);
135 class connect : public CppUnit::TestFixture
137 public:
138 TimeValue *pTimeout;
139 ::osl::AcceptorSocket asAcceptorSocket;
140 ::osl::ConnectorSocket csConnectorSocket;
143 // initialization
144 void setUp( )
146 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
147 pTimeout->Seconds = 3;
148 pTimeout->Nanosec = 0;
149 // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
152 void tearDown( )
154 free( pTimeout );
155 // sHandle = NULL;
156 asAcceptorSocket.close( );
157 csConnectorSocket.close( );
161 void connect_001()
163 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT2 );
164 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT2 );
165 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
166 ::osl::StreamSocket ssConnection;
168 /// launch server socket
169 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
170 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
171 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
172 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
173 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
175 //asAcceptorSocket.enableNonBlockingMode( sal_True );
176 //oslSocketResult eResultAccept = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
177 //CPPUNIT_ASSERT_MESSAGE( "accept failed.", osl_Socket_Ok == eResultAccept );
178 /// launch client socket
179 oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
180 CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_Ok == eResult );
182 /// get peer information
183 csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
185 CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.",
186 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) &&
187 ( osl_Socket_Ok == eResult ));
189 //non-blocking mode connect?
190 void connect_002()
192 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 );
193 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 );
194 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
196 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
197 asAcceptorSocket.enableNonBlockingMode( sal_True );
198 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
199 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
200 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
201 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
203 csConnectorSocket.enableNonBlockingMode( sal_True );
205 oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
206 CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_InProgress == eResult || osl_Socket_Ok == eResult );
208 /// get peer information
209 csConnectorSocket.getPeerAddr( saPeerSocketAddr );
211 CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.",
212 sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) ;
214 // really an error or just delayed
215 // how to design senarios that will return osl_Socket_Interrupted, osl_Socket_TimedOut
216 void connect_003()
218 ::osl::SocketAddr saTargetSocketAddr1( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 );
219 ::osl::SocketAddr saTargetSocketAddr2( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT3 );
221 csConnectorSocket.enableNonBlockingMode( sal_False );
223 oslSocketResult eResult1 = csConnectorSocket.connect( saTargetSocketAddr1, pTimeout );
224 oslSocketResult eResult2 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout );
225 CloseSocketThread myCloseThread( csConnectorSocket );
226 oslSocketResult eResult3 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout );
227 myCloseThread.join();
228 CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult1 &&
229 osl_Socket_Error == eResult2 && osl_Socket_Error == eResult3 );
233 // really an error in non-blocking mode
234 void connect_004()
236 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT3 );
238 csConnectorSocket.enableNonBlockingMode( sal_True );
240 oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
241 CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult );
243 /** here need a case: immediate connection, say in non-blocking mode connect return osl_Socket_Ok
246 CPPUNIT_TEST_SUITE( connect );
247 CPPUNIT_TEST( connect_001 );
248 CPPUNIT_TEST( connect_002 );
249 CPPUNIT_TEST( connect_003 );
250 CPPUNIT_TEST( connect_004 );
251 CPPUNIT_TEST_SUITE_END();
253 }; // class connect
256 // -----------------------------------------------------------------------------
258 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::ctors, "osl_ConnectorSocket");
259 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::connect, "osl_ConnectorSocket");
261 } // namespace osl_ConnectorSocket
263 // -----------------------------------------------------------------------------
265 // this macro creates an empty function, which will called by the RegisterAllFunctions()
266 // to let the user the possibility to also register some functions by hand.
267 NOADDITIONAL;