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_MYPORT2 8883 70*cdf0e10cSrcweir #define IP_PORT_FTP 21 71*cdf0e10cSrcweir #define IP_PORT_MYPORT3 8884 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir //------------------------------------------------------------------------ 74*cdf0e10cSrcweir // helper functions 75*cdf0e10cSrcweir //------------------------------------------------------------------------ 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir class CloseSocketThread : public Thread 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir ::osl::Socket &m_sSocket; 80*cdf0e10cSrcweir protected: 81*cdf0e10cSrcweir void SAL_CALL run( ) 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir thread_sleep( 1 ); 84*cdf0e10cSrcweir m_sSocket.close( ); 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir public: 87*cdf0e10cSrcweir CloseSocketThread(::osl::Socket & sSocket ) 88*cdf0e10cSrcweir : m_sSocket( sSocket ) 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir ~CloseSocketThread( ) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir if ( isRunning( ) ) 95*cdf0e10cSrcweir { 96*cdf0e10cSrcweir t_print("# error: CloseSocketThread not terminated.\n" ); 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir }; 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir namespace osl_ConnectorSocket 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir /** testing the method: 105*cdf0e10cSrcweir ConnectorSocket(oslAddrFamily Family = osl_Socket_FamilyInet, 106*cdf0e10cSrcweir oslProtocol Protocol = osl_Socket_ProtocolIp, 107*cdf0e10cSrcweir oslSocketType Type = osl_Socket_TypeStream); 108*cdf0e10cSrcweir */ 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir class ctors : public CppUnit::TestFixture 111*cdf0e10cSrcweir { 112*cdf0e10cSrcweir public: 113*cdf0e10cSrcweir void ctors_001() 114*cdf0e10cSrcweir { 115*cdf0e10cSrcweir /// Socket constructor. 116*cdf0e10cSrcweir ::osl::ConnectorSocket csSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the connector socket was created successfully.", 119*cdf0e10cSrcweir osl_Socket_TypeStream == csSocket.getType( ) ); 120*cdf0e10cSrcweir } 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( ctors ); 123*cdf0e10cSrcweir CPPUNIT_TEST( ctors_001 ); 124*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir }; // class ctors 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir /** testing the method: 129*cdf0e10cSrcweir oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0); 130*cdf0e10cSrcweir */ 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir class connect : public CppUnit::TestFixture 133*cdf0e10cSrcweir { 134*cdf0e10cSrcweir public: 135*cdf0e10cSrcweir TimeValue *pTimeout; 136*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 137*cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir // initialization 141*cdf0e10cSrcweir void setUp( ) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 144*cdf0e10cSrcweir pTimeout->Seconds = 3; 145*cdf0e10cSrcweir pTimeout->Nanosec = 0; 146*cdf0e10cSrcweir // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir void tearDown( ) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir free( pTimeout ); 152*cdf0e10cSrcweir // sHandle = NULL; 153*cdf0e10cSrcweir asAcceptorSocket.close( ); 154*cdf0e10cSrcweir csConnectorSocket.close( ); 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir void connect_001() 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT2 ); 161*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT2 ); 162*cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 163*cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir /// launch server socket 166*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 167*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 168*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 169*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 170*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir //asAcceptorSocket.enableNonBlockingMode( sal_True ); 173*cdf0e10cSrcweir //oslSocketResult eResultAccept = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 174*cdf0e10cSrcweir //CPPUNIT_ASSERT_MESSAGE( "accept failed.", osl_Socket_Ok == eResultAccept ); 175*cdf0e10cSrcweir /// launch client socket 176*cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 177*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_Ok == eResult ); 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir /// get peer information 180*cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected. 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.", 183*cdf0e10cSrcweir ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) && 184*cdf0e10cSrcweir ( osl_Socket_Ok == eResult )); 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir //non-blocking mode connect? 187*cdf0e10cSrcweir void connect_002() 188*cdf0e10cSrcweir { 189*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 ); 190*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 ); 191*cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 194*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 195*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 196*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 197*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 198*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_True ); 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 203*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_InProgress == eResult || osl_Socket_Ok == eResult ); 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir /// get peer information 206*cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr ); 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.", 209*cdf0e10cSrcweir sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) ; 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir // really an error or just delayed 212*cdf0e10cSrcweir // how to design senarios that will return osl_Socket_Interrupted, osl_Socket_TimedOut 213*cdf0e10cSrcweir void connect_003() 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr1( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 ); 216*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr2( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT3 ); 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_False ); 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir oslSocketResult eResult1 = csConnectorSocket.connect( saTargetSocketAddr1, pTimeout ); 221*cdf0e10cSrcweir oslSocketResult eResult2 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout ); 222*cdf0e10cSrcweir CloseSocketThread myCloseThread( csConnectorSocket ); 223*cdf0e10cSrcweir oslSocketResult eResult3 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout ); 224*cdf0e10cSrcweir myCloseThread.join(); 225*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult1 && 226*cdf0e10cSrcweir osl_Socket_Error == eResult2 && osl_Socket_Error == eResult3 ); 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir // really an error in non-blocking mode 231*cdf0e10cSrcweir void connect_004() 232*cdf0e10cSrcweir { 233*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT3 ); 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_True ); 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 238*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult ); 239*cdf0e10cSrcweir } 240*cdf0e10cSrcweir /** here need a case: immediate connection, say in non-blocking mode connect return osl_Socket_Ok 241*cdf0e10cSrcweir */ 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( connect ); 244*cdf0e10cSrcweir CPPUNIT_TEST( connect_001 ); 245*cdf0e10cSrcweir CPPUNIT_TEST( connect_002 ); 246*cdf0e10cSrcweir CPPUNIT_TEST( connect_003 ); 247*cdf0e10cSrcweir CPPUNIT_TEST( connect_004 ); 248*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir }; // class connect 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::ctors, "osl_ConnectorSocket"); 256*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::connect, "osl_ConnectorSocket"); 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir } // namespace osl_ConnectorSocket 259*cdf0e10cSrcweir 260*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions() 263*cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand. 264*cdf0e10cSrcweir NOADDITIONAL; 265