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