diff options
Diffstat (limited to 'sal/qa/osl/socket')
21 files changed, 9775 insertions, 0 deletions
diff --git a/sal/qa/osl/socket/export.exp b/sal/qa/osl/socket/export.exp new file mode 100755 index 000000000000..a13529da5876 --- /dev/null +++ b/sal/qa/osl/socket/export.exp @@ -0,0 +1 @@ +registerAllTestFunction diff --git a/sal/qa/osl/socket/makefile.mk b/sal/qa/osl/socket/makefile.mk new file mode 100755 index 000000000000..bd92b5f40f3a --- /dev/null +++ b/sal/qa/osl/socket/makefile.mk @@ -0,0 +1,223 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=..$/..$/.. + +PRJNAME=sal +TARGET=qa_osl_socket + +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +CFLAGS+= $(LFS_CFLAGS) +CXXFLAGS+= $(LFS_CFLAGS) + +# BEGIN ---------------------------------------------------------------- +# auto generated Target:Socket by codegen.pl +SHL1OBJS= \ + $(SLO)$/osl_Socket.obj + +SHL1TARGET=osl_SocketOld +SHL1STDLIBS= $(SALLIB) $CPPUNITLIB) $(TESTSHL2LIB) + +.IF "$(GUI)" == "WNT" +SHL1STDLIBS+= $(WS2_32LIB) +.ENDIF + +.IF "$(GUI)" == "UNX" +SHL1STDLIBS+= -ldl -lnsl +.ENDIF + +SHL1IMPLIB= i$(SHL1TARGET) +# SHL1DEF= $(MISC)$/$(SHL1TARGET).def +DEF1NAME=$(SHL1TARGET) +# DEF1EXPORTFILE= export.exp +SHL1VERSIONMAP = $(PRJ)$/qa$/export.map +# auto generated Target:Socket +# END ------------------------------------------------------------------ + +# --- BEGIN -------------------------------------------------------- +SHL2OBJS= \ + $(SLO)$/osl_Socket_tests.obj +SHL2TARGET= osl_Socket_tests +SHL2STDLIBS= $(SALLIB) $CPPUNITLIB) $(TESTSHL2LIB) + +SHL2IMPLIB= i$(SHL2TARGET) +DEF2NAME= $(SHL2TARGET) +SHL2VERSIONMAP = $(PRJ)$/qa$/export.map + +# END -------------------------------------------------------------- + +# BEGIN ---------------------------------------------------------------- +# auto generated Target:Socket by codegen.pl +SHL3OBJS= \ + $(SLO)$/sockethelper.obj \ + $(SLO)$/osl_StreamSocket.obj + +SHL3TARGET= osl_StreamSocket +SHL3STDLIBS= $(SALLIB) $CPPUNITLIB) $(TESTSHL2LIB) + +.IF "$(GUI)" == "WNT" +SHL3STDLIBS += $(WS2_32LIB) +.ENDIF + +.IF "$(GUI)" == "UNX" +SHL3STDLIBS += -ldl -lnsl +.ENDIF + +SHL3IMPLIB= i$(SHL3TARGET) +DEF3NAME= $(SHL3TARGET) +SHL3VERSIONMAP = $(PRJ)$/qa$/export.map +# auto generated Target:Socket +# END ------------------------------------------------------------------ + +# BEGIN ---------------------------------------------------------------- +# auto generated Target:Socket by codegen.pl +SHL4OBJS= \ + $(SLO)$/sockethelper.obj \ + $(SLO)$/osl_DatagramSocket.obj + +SHL4TARGET= osl_DatagramSocket +SHL4STDLIBS= $(SALLIB) $(CPPUNITLIB) $(TESTSHL2LIB) + +.IF "$(GUI)" == "WNT" +SHL4STDLIBS += $(WS2_32LIB) +.ENDIF + +.IF "$(GUI)" == "UNX" +SHL4STDLIBS += -ldl -lnsl +.ENDIF + +SHL4IMPLIB= i$(SHL4TARGET) +DEF4NAME= $(SHL4TARGET) +SHL4VERSIONMAP = $(PRJ)$/qa$/export.map +# auto generated Target:Socket +# END ------------------------------------------------------------------ + +# BEGIN ---------------------------------------------------------------- +# auto generated Target:Socket by codegen.pl +SHL5OBJS= \ + $(SLO)$/sockethelper.obj \ + $(SLO)$/osl_SocketAddr.obj + +SHL5TARGET= osl_SocketAddr +SHL5STDLIBS= $(SALLIB) $(CPPUNITLIB) $(TESTSHL2LIB) + +.IF "$(GUI)" == "WNT" +SHL5STDLIBS += $(WS2_32LIB) +.ENDIF + +.IF "$(GUI)" == "UNX" +SHL5STDLIBS += -ldl -lnsl +.ENDIF + +SHL5IMPLIB= i$(SHL5TARGET) +DEF5NAME= $(SHL5TARGET) +SHL5VERSIONMAP = $(PRJ)$/qa$/export.map +# auto generated Target:Socket +# END ------------------------------------------------------------------ + +# BEGIN ---------------------------------------------------------------- +# auto generated Target:Socket by codegen.pl +SHL6OBJS= \ + $(SLO)$/sockethelper.obj \ + $(SLO)$/osl_Socket2.obj + +SHL6TARGET= osl_Socket2 +SHL6STDLIBS= $(SALLIB) $(CPPUNITLIB) $(TESTSHL2LIB) + +.IF "$(GUI)" == "WNT" +SHL6STDLIBS += $(WS2_32LIB) +.ENDIF + +.IF "$(GUI)" == "UNX" +SHL6STDLIBS += -ldl -lnsl +.ENDIF + +SHL6IMPLIB= i$(SHL6TARGET) +DEF6NAME= $(SHL6TARGET) +SHL6VERSIONMAP = $(PRJ)$/qa$/export.map +# auto generated Target:Socket +# END ------------------------------------------------------------------ + +# BEGIN ---------------------------------------------------------------- +# auto generated Target:Socket by codegen.pl +SHL7OBJS= \ + $(SLO)$/sockethelper.obj \ + $(SLO)$/osl_ConnectorSocket.obj + +SHL7TARGET= osl_ConnectorSocket +SHL7STDLIBS= $(SALLIB) $(CPPUNITLIB) $(TESTSHL2LIB) + +.IF "$(GUI)" == "WNT" +SHL7STDLIBS += $(WS2_32LIB) +.ENDIF + +.IF "$(GUI)" == "UNX" +SHL7STDLIBS += -ldl -lnsl +.ENDIF + +SHL7IMPLIB= i$(SHL7TARGET) +DEF7NAME= $(SHL7TARGET) +SHL7VERSIONMAP = $(PRJ)$/qa$/export.map +# auto generated Target:Socket +# END ------------------------------------------------------------------ + +# BEGIN ---------------------------------------------------------------- +# auto generated Target:Socket by codegen.pl +SHL8OBJS= \ + $(SLO)$/sockethelper.obj \ + $(SLO)$/osl_AcceptorSocket.obj + +SHL8TARGET= osl_AcceptorSocket +SHL8STDLIBS= $(SALLIB) $(CPPUNITLIB) $(TESTSHL2LIB) + +.IF "$(GUI)" == "WNT" +SHL8STDLIBS += $(WS2_32LIB) +.ENDIF + +.IF "$(GUI)" == "UNX" +SHL8STDLIBS += -ldl -lnsl +.ENDIF + +SHL8IMPLIB= i$(SHL8TARGET) +DEF8NAME= $(SHL8TARGET) +SHL8VERSIONMAP = $(PRJ)$/qa$/export.map +# auto generated Target:Socket +# END ------------------------------------------------------------------ + +#------------------------------- All object files ------------------------------- +# do this here, so we get right dependencies +# SLOFILES=$(SHL1OBJS) $(SHL2OBJS) + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk +.INCLUDE : _cppunit.mk diff --git a/sal/qa/osl/socket/osl_AcceptorSocket.cxx b/sal/qa/osl/socket/osl_AcceptorSocket.cxx new file mode 100644 index 000000000000..e98af5f9d3b0 --- /dev/null +++ b/sal/qa/osl/socket/osl_AcceptorSocket.cxx @@ -0,0 +1,302 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sal.hxx" + +/** test coder preface: + 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform + if you are not including ws2_32.lib in makefile.mk, the including format will be like this: + + .IF "$(GUI)" == "WNT" + SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib + SHL1STDLIBS += ws2_32.lib + .ENDIF + + likewise on Solaris platform. + .IF "$(GUI)" == "UNX" + SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a + SHL1STDLIBS += -lsocket -ldl -lnsl + .ENDIF + + 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4 + category. + + 3. some fragment of Socket source implementation are lack of comment so it is hard for testers + guess what the exact functionality or usage of a member. Hope the Socket section's comment + will be added. + + 4. following functions are declared but not implemented: + inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; + */ + +//------------------------------------------------------------------------ +// include files +//------------------------------------------------------------------------ + +#include <testshl/simpleheader.hxx> + +#include "osl_Socket_Const.h" +#include "sockethelper.hxx" + +using namespace osl; +using namespace rtl; + +#define IP_PORT_FTP 21 +#define IP_PORT_MYPORT9 8897 +#define IP_PORT_MYPORT4 8885 +#define IP_PORT_MYPORT3 8884 + +//------------------------------------------------------------------------ +// helper functions +//------------------------------------------------------------------------ + +// just used to test socket::close() when accepting +class AcceptorThread : public Thread +{ + ::osl::AcceptorSocket asAcceptorSocket; + ::rtl::OUString aHostIP; + sal_Bool bOK; +protected: + void SAL_CALL run( ) + { + ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 ); + ::osl::StreamSocket ssStreamConnection; + + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + if ( sal_True != bOK1 ) + { + t_print("# AcceptorSocket bind address failed.\n" ) ; + return; + } + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + if ( sal_True != bOK2 ) + { + t_print("# AcceptorSocket listen address failed.\n" ) ; + return; + } + + asAcceptorSocket.enableNonBlockingMode( sal_False ); + + oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); + if (eResult != osl_Socket_Ok ) + { + bOK = sal_True; + t_print("AcceptorThread: acceptConnection failed! \n"); + } + } +public: + AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP ) + : asAcceptorSocket( asSocket ), aHostIP( aBindIP ) + { + bOK = sal_False; + } + + sal_Bool isOK() { return bOK; } + + ~AcceptorThread( ) + { + if ( isRunning( ) ) + { + asAcceptorSocket.shutdown(); + t_print("# error: Acceptor thread not terminated.\n" ); + } + } +}; + +namespace osl_AcceptorSocket +{ + + /** testing the methods: + inline AcceptorSocket(oslAddrFamily Family = osl_Socket_FamilyInet, + oslProtocol Protocol = osl_Socket_ProtocolIp, + oslSocketType Type = osl_Socket_TypeStream); + */ + + class ctors : public CppUnit::TestFixture + { + public: + + void ctors_001() + { + /// Socket constructor. + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the acceptor socket was created successfully.", + osl_Socket_TypeStream == asSocket.getType( ) ); + } + + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + +#if 0 /* OBSOLETE */ + class operator_assign : public CppUnit::TestFixture + { + public: + + void assign_001() + { +#if defined(LINUX) + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::AcceptorSocket asSocketAssign( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + asSocket.setOption( osl_Socket_OptionReuseAddr, 1); + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT4 ); + asSocket.bind( saSocketAddr ); + + AcceptorThread myAcceptorThread( asSocketAssign, rtl::OUString::createFromAscii("127.0.0.1") ); + myAcceptorThread.create(); + + thread_sleep( 1 ); + //when accepting, assign another socket to the socket, the thread will not be closed, so is blocking + asSocketAssign = asSocket; + + t_print("#asSocketAssign port number is %d\n", asSocketAssign.getLocalPort() ); + + asSocketAssign.shutdown(); + myAcceptorThread.join(); + + CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", + myAcceptorThread.isOK() == sal_True ); + + +#endif /* LINUX */ + } + + + CPPUNIT_TEST_SUITE( operator_assign ); + CPPUNIT_TEST( assign_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class operator_assign +#endif /* OBSOLETE */ + + /** testing the method: + inline sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1); + inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection); + inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr); + */ + + class listen_accept : public CppUnit::TestFixture + { + public: + TimeValue *pTimeout; + ::osl::AcceptorSocket asAcceptorSocket; + ::osl::ConnectorSocket csConnectorSocket; + + + // initialization + void setUp( ) + { + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1); + // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + free( pTimeout ); + // sHandle = NULL; + asAcceptorSocket.close( ); + csConnectorSocket.close( ); + } + + + void listen_accept_001() + { + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 ); + ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + + /// launch client socket + csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + + oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept.", + ( osl_Socket_Ok == eResult ) ); + } + + void listen_accept_002() + { + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT4 ); + ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT4 ); + ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + + /// launch client socket + csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + + oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection, saPeerSocketAddr); /// waiting for incoming connection... + + CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept, accept with peer address.", + ( sal_True == bOK2 ) && + ( osl_Socket_Ok == eResult ) && + ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) ); + } + + + CPPUNIT_TEST_SUITE( listen_accept ); + CPPUNIT_TEST( listen_accept_001 ); + CPPUNIT_TEST( listen_accept_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class listen_accept + + +// ----------------------------------------------------------------------------- + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::ctors, "osl_AcceptorSocket"); +//CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::operator_assign, "osl_AcceptorSocket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::listen_accept, "osl_AcceptorSocket"); + +} // namespace osl_AcceptorSocket + +// ----------------------------------------------------------------------------- + +// this macro creates an empty function, which will called by the RegisterAllFunctions() +// to let the user the possibility to also register some functions by hand. +NOADDITIONAL; diff --git a/sal/qa/osl/socket/osl_ConnectorSocket.cxx b/sal/qa/osl/socket/osl_ConnectorSocket.cxx new file mode 100644 index 000000000000..4a5273e5a0e8 --- /dev/null +++ b/sal/qa/osl/socket/osl_ConnectorSocket.cxx @@ -0,0 +1,264 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sal.hxx" + +/** test coder preface: + 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform + if you are not including ws2_32.lib in makefile.mk, the including format will be like this: + + .IF "$(GUI)" == "WNT" + SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib + SHL1STDLIBS += ws2_32.lib + .ENDIF + + likewise on Solaris platform. + .IF "$(GUI)" == "UNX" + SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a + SHL1STDLIBS += -lsocket -ldl -lnsl + .ENDIF + + 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4 + category. + + 3. some fragment of Socket source implementation are lack of comment so it is hard for testers + guess what the exact functionality or usage of a member. Hope the Socket section's comment + will be added. + + 4. following functions are declared but not implemented: + inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; + */ + +//------------------------------------------------------------------------ +// include files +//------------------------------------------------------------------------ + +#include <testshl/simpleheader.hxx> + +#include "osl_Socket_Const.h" +#include "sockethelper.hxx" + +using namespace osl; +using namespace rtl; + +#define IP_PORT_MYPORT2 8883 +#define IP_PORT_FTP 21 +#define IP_PORT_MYPORT3 8884 + +//------------------------------------------------------------------------ +// helper functions +//------------------------------------------------------------------------ + +class CloseSocketThread : public Thread +{ + ::osl::Socket &m_sSocket; +protected: + void SAL_CALL run( ) + { + thread_sleep( 1 ); + m_sSocket.close( ); + } +public: + CloseSocketThread(::osl::Socket & sSocket ) + : m_sSocket( sSocket ) + { + } + + ~CloseSocketThread( ) + { + if ( isRunning( ) ) + { + t_print("# error: CloseSocketThread not terminated.\n" ); + } + } +}; + +namespace osl_ConnectorSocket +{ + + /** testing the method: + ConnectorSocket(oslAddrFamily Family = osl_Socket_FamilyInet, + oslProtocol Protocol = osl_Socket_ProtocolIp, + oslSocketType Type = osl_Socket_TypeStream); + */ + + class ctors : public CppUnit::TestFixture + { + public: + void ctors_001() + { + /// Socket constructor. + ::osl::ConnectorSocket csSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the connector socket was created successfully.", + osl_Socket_TypeStream == csSocket.getType( ) ); + } + + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + + /** testing the method: + oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0); + */ + + class connect : public CppUnit::TestFixture + { + public: + TimeValue *pTimeout; + ::osl::AcceptorSocket asAcceptorSocket; + ::osl::ConnectorSocket csConnectorSocket; + + + // initialization + void setUp( ) + { + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + free( pTimeout ); + // sHandle = NULL; + asAcceptorSocket.close( ); + csConnectorSocket.close( ); + } + + + void connect_001() + { + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT2 ); + ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT2 ); + ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + + //asAcceptorSocket.enableNonBlockingMode( sal_True ); + //oslSocketResult eResultAccept = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + //CPPUNIT_ASSERT_MESSAGE( "accept failed.", osl_Socket_Ok == eResultAccept ); + /// launch client socket + oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_Ok == eResult ); + + /// get peer information + csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected. + + CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.", + ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) && + ( osl_Socket_Ok == eResult )); + } + //non-blocking mode connect? + void connect_002() + { + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 ); + ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 ); + ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + + csConnectorSocket.enableNonBlockingMode( sal_True ); + + oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_InProgress == eResult || osl_Socket_Ok == eResult ); + + /// get peer information + csConnectorSocket.getPeerAddr( saPeerSocketAddr ); + + CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.", + sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) ; + } + // really an error or just delayed + // how to design senarios that will return osl_Socket_Interrupted, osl_Socket_TimedOut + void connect_003() + { + ::osl::SocketAddr saTargetSocketAddr1( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 ); + ::osl::SocketAddr saTargetSocketAddr2( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT3 ); + + csConnectorSocket.enableNonBlockingMode( sal_False ); + + oslSocketResult eResult1 = csConnectorSocket.connect( saTargetSocketAddr1, pTimeout ); + oslSocketResult eResult2 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout ); + CloseSocketThread myCloseThread( csConnectorSocket ); + oslSocketResult eResult3 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout ); + myCloseThread.join(); + CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult1 && + osl_Socket_Error == eResult2 && osl_Socket_Error == eResult3 ); + + } + + // really an error in non-blocking mode + void connect_004() + { + ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT3 ); + + csConnectorSocket.enableNonBlockingMode( sal_True ); + + oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult ); + } + /** here need a case: immediate connection, say in non-blocking mode connect return osl_Socket_Ok + */ + + CPPUNIT_TEST_SUITE( connect ); + CPPUNIT_TEST( connect_001 ); + CPPUNIT_TEST( connect_002 ); + CPPUNIT_TEST( connect_003 ); + CPPUNIT_TEST( connect_004 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class connect + + +// ----------------------------------------------------------------------------- + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::ctors, "osl_ConnectorSocket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::connect, "osl_ConnectorSocket"); + +} // namespace osl_ConnectorSocket + +// ----------------------------------------------------------------------------- + +// this macro creates an empty function, which will called by the RegisterAllFunctions() +// to let the user the possibility to also register some functions by hand. +NOADDITIONAL; diff --git a/sal/qa/osl/socket/osl_ConnectorSocket.xsce b/sal/qa/osl/socket/osl_ConnectorSocket.xsce new file mode 100644 index 000000000000..6c04ae2eb6f3 --- /dev/null +++ b/sal/qa/osl/socket/osl_ConnectorSocket.xsce @@ -0,0 +1 @@ +osl_ConnectorSocket.connect.connect_003 wntmsci diff --git a/sal/qa/osl/socket/osl_DatagramSocket.cxx b/sal/qa/osl/socket/osl_DatagramSocket.cxx new file mode 100644 index 000000000000..9a60bb8249b3 --- /dev/null +++ b/sal/qa/osl/socket/osl_DatagramSocket.cxx @@ -0,0 +1,316 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sal.hxx" + +/** test coder preface: + 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform + if you are not including ws2_32.lib in makefile.mk, the including format will be like this: + + .IF "$(GUI)" == "WNT" + SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib + SHL1STDLIBS += ws2_32.lib + .ENDIF + + likewise on Solaris platform. + .IF "$(GUI)" == "UNX" + SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a + SHL1STDLIBS += -lsocket -ldl -lnsl + .ENDIF + + 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4 + category. + + 3. some fragment of Socket source implementation are lack of comment so it is hard for testers + guess what the exact functionality or usage of a member. Hope the Socket section's comment + will be added. + + 4. following functions are declared but not implemented: + inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; + */ + +//------------------------------------------------------------------------ +// include files +//------------------------------------------------------------------------ + +#include <testshl/simpleheader.hxx> + +//#include "osl_Socket_Const.h" +#include "sockethelper.hxx" + +using namespace osl; +using namespace rtl; + +#define IP_PORT_MYPORT9 8897 +#define IP_PORT_MYPORT10 8898 + +const char * pTestString1 = "test socket"; +const char * pTestString2 = " Passed#OK"; + +//------------------------------------------------------------------------ +// helper functions +//------------------------------------------------------------------------ + +class CloseSocketThread : public Thread +{ + ::osl::Socket m_sSocket; +protected: + void SAL_CALL run( ) + { + thread_sleep( 1 ); + m_sSocket.close( ); + } +public: + CloseSocketThread(::osl::Socket & sSocket ) + : m_sSocket( sSocket ) + { + } + + ~CloseSocketThread( ) + { + if ( isRunning( ) ) + { + t_print("# error: CloseSocketThread not terminated.\n" ); + } + } +}; + +//------------------------------------------------------------------------ +// tests cases begins here +//------------------------------------------------------------------------ + +namespace osl_DatagramSocket +{ + + /** testing the methods: + inline DatagramSocket(oslAddrFamily Family= osl_Socket_FamilyInet, + oslProtocol Protocol= osl_Socket_ProtocolIp, + oslSocketType Type= osl_Socket_TypeDgram); + */ + + class ctors : public CppUnit::TestFixture + { + public: + + void ctors_001() + { + /// Socket constructor. + ::osl::DatagramSocket dsSocket; + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the datagram socket was created successfully.", + osl_Socket_TypeDgram == dsSocket.getType( ) ); + } + + + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + +/**thread do sendTo, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml +*/ +class TalkerThread : public Thread +{ +protected: + ::osl::SocketAddr saTargetSocketAddr; + ::osl::DatagramSocket dsSocket; + + void SAL_CALL run( ) + { + dsSocket.sendTo( saTargetSocketAddr, pTestString1, strlen( pTestString1 ) + 1 ); // "test socket" + dsSocket.shutdown(); + } + + void SAL_CALL onTerminated( ) + { + } + +public: + TalkerThread( ): + saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ) + { + } + + ~TalkerThread( ) + { + if ( isRunning( ) ) + t_print("# error: TalkerThread not terminated normally.\n" ); + } +}; + +/**thread do listen, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml +*/ +class ListenerThread : public Thread +{ +protected: + ::osl::SocketAddr saTargetSocketAddr; + ::osl::DatagramSocket dsSocket; + + void SAL_CALL run( ) + { + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 ); + dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); + if ( dsSocket.bind( saLocalSocketAddr ) == sal_False ) + { + t_print("DatagramSocket bind failed \n"); + return; + } + //blocking mode: default + sal_Int32 nRecv = dsSocket.recvFrom( pRecvBuffer, 30, &saTargetSocketAddr); //strlen( pTestString2 ) + 1 + t_print("After recvFrom, nRecv is %d\n", nRecv); + } + + void SAL_CALL onTerminated( ) + { + } + +public: + sal_Char pRecvBuffer[30]; + ListenerThread( ): + saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 ) + { + pRecvBuffer[0] = '\0'; + } + + ~ListenerThread( ) + { + if ( isRunning( ) ) + t_print("# error: ListenerThread not terminated normally.\n" ); + } + +}; + + /** testing the methods: + inline sal_Int32 DatagramSocket::recvFrom(void* pBuffer, sal_uInt32 BufferSize, + SocketAddr* pSenderAddr, oslSocketMsgFlag Flag ) + inline sal_Int32 DatagramSocket::sendTo( const SocketAddr& ReceiverAddr, + const void* pBuffer, sal_uInt32 BufferSize, oslSocketMsgFlag Flag ) + */ + + class sendTo_recvFrom : public CppUnit::TestFixture + { + public: + + void sr_001() + { + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ); + ::osl::DatagramSocket dsSocket; + dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); + dsSocket.bind( saLocalSocketAddr ); + + sal_Char pReadBuffer[30]; + TalkerThread myTalkThread; + myTalkThread.create(); + sal_Int32 nRecv = dsSocket.recvFrom( pReadBuffer, 30, &saLocalSocketAddr); + myTalkThread.join(); + //t_print("#received buffer is %s# \n", pReadBuffer); + + sal_Bool bOk = ( strcmp(pReadBuffer, pTestString1) == 0 ); + + CPPUNIT_ASSERT_MESSAGE( "test for sendTo/recvFrom function: create a talker thread and recvFrom in the main thread, check if the datagram socket can communicate successfully.", + nRecv > 0 && bOk == sal_True ); + } + + void sr_002() + { + ::osl::SocketAddr saListenSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 ); + ::osl::DatagramSocket dsSocket; + + //listener thread construct a DatagramSocket, recvFrom waiting for data, then main thread sendto data + ListenerThread myListenThread; + myListenThread.create(); + //to grantee the recvFrom is before sendTo + thread_sleep( 1 ); + + sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 ); + + CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo failed: nSend <= 0.", nSend > 0); + + myListenThread.join(); + //t_print("#received buffer is %s# \n", myListenThread.pRecvBuffer); + + sal_Bool bOk = ( strcmp( myListenThread.pRecvBuffer, pTestString2) == 0 ); + + CPPUNIT_ASSERT_MESSAGE( "test for sendTo/recvFrom function: create a listener thread and sendTo in the main thread, check if the datagram socket can communicate successfully.", + bOk == sal_True ); + } + + //sendTo error, return -1; recvFrom error, return -1 + void sr_003() + { + ::osl::SocketAddr saListenSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT10 ); + ::osl::DatagramSocket dsSocket; + // Transport endpoint is not connected + sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 ); + CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo should fail: nSend <= 0.", + nSend == -1 ); + } + + void sr_004() + { + ::osl::SocketAddr saListenSocketAddr1( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT10 ); + ::osl::SocketAddr saListenSocketAddr2( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_MYPORT10 ); + ::osl::DatagramSocket dsSocket; + + dsSocket.enableNonBlockingMode( sal_True ); + + sal_Char pReadBuffer[30]; + //sal_Int32 nRecv1 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 ); + + // will block ? + CloseSocketThread myThread( dsSocket ); + myThread.create(); + sal_Int32 nRecv2 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 ); + myThread.join(); + //t_print("#nRecv1 is %d nRecv2 is %d\n", nRecv1, nRecv2 ); + CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo should fail: nSend <= 0.", + nRecv2 == -1 ); + } + + CPPUNIT_TEST_SUITE( sendTo_recvFrom ); + CPPUNIT_TEST( sr_001 ); + CPPUNIT_TEST( sr_002 ); + CPPUNIT_TEST( sr_003 ); + CPPUNIT_TEST( sr_004 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class sendTo_recvFrom + +// ----------------------------------------------------------------------------- + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DatagramSocket::ctors, "osl_DatagramSocket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DatagramSocket::sendTo_recvFrom, "osl_DatagramSocket"); + +} // namespace osl_DatagramSocket + +// ----------------------------------------------------------------------------- + +// this macro creates an empty function, which will called by the RegisterAllFunctions() +// to let the user the possibility to also register some functions by hand. +NOADDITIONAL; diff --git a/sal/qa/osl/socket/osl_Socket.cxx b/sal/qa/osl/socket/osl_Socket.cxx new file mode 100755 index 000000000000..4a981bc7faa9 --- /dev/null +++ b/sal/qa/osl/socket/osl_Socket.cxx @@ -0,0 +1,3723 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sal.hxx" + +/** test coder preface: + 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform + if you are not including ws2_32.lib in makefile.mk, the including format will be like this: + + .IF "$(GUI)" == "WNT" + SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib + SHL1STDLIBS += ws2_32.lib + .ENDIF + + likewise on Solaris platform. + .IF "$(GUI)" == "UNX" + SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a + SHL1STDLIBS += -lsocket -ldl -lnsl + .ENDIF + + 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4 + category. + + 3. some fragment of Socket source implementation are lack of comment so it is hard for testers + guess what the exact functionality or usage of a member. Hope the Socket section's comment + will be added. + + 4. following functions are declared but not implemented: + inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; + */ + + +//------------------------------------------------------------------------ +// include files +//------------------------------------------------------------------------ + +#ifndef _OSL_SOCKET_CONST_H_ +#include <osl_Socket_Const_orig.h> +#endif + +#include <testshl/simpleheader.hxx> + +using namespace osl; +using namespace rtl; + +//------------------------------------------------------------------------ +// helper functions +//------------------------------------------------------------------------ + +/** compare two OUString. +*/ +inline sal_Bool compareUString( const ::rtl::OUString & ustr1, const ::rtl::OUString & ustr2 ) +{ + sal_Bool bOk = ustr1.equalsIgnoreAsciiCase( ustr2 ); + + return bOk; +} + +/** compare a OUString and an ASCII string. +*/ +inline sal_Bool compareUString( const ::rtl::OUString & ustr, const sal_Char *astr ) +{ + ::rtl::OUString ustr2 = rtl::OUString::createFromAscii( astr ); + sal_Bool bOk = ustr.equalsIgnoreAsciiCase( ustr2 ); + + return bOk; +} + +/** compare two socket address. +*/ +inline sal_Bool compareSocketAddr( const ::osl::SocketAddr & addr1 , const ::osl::SocketAddr & addr2 ) +{ + return ( ( sal_True == compareUString( addr1.getHostname( 0 ), addr2.getHostname( 0 ) ) ) && ( addr2.getPort( ) == addr2.getPort( ) ) ); +} + +inline char * oustring2char( const ::rtl::OUString & str ) +{ + rtl::OString aString; + aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ); + return (char *)aString.getStr( ); +} + +/** print a UNI_CODE String. And also print some comments of the string. +*/ +inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" ) +{ + t_print("#%s #printUString_u# ", msg ); + t_print("%s\n", oustring2char( str ) ); +} + +/** get the local host name. + mindy: gethostbyname( "localhost" ), on Linux, it returns the hostname in /etc/hosts + domain name, + if no entry in /etc/hosts, it returns "localhost" + domain name +*/ +inline ::rtl::OUString getHost( void ) +{ + struct hostent *hptr; + + hptr = gethostbyname( "localhost" ); + CPPUNIT_ASSERT_MESSAGE( "#In getHostname function, error on gethostbyname()", hptr != NULL ); + ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name ); + + return aUString; +} + +/** get the full host name of the current processor, such as "aegean.prc.sun.com" --mindyliu +*/ +inline ::rtl::OUString getThisHostname( void ) +{ + ::rtl::OUString aUString; +#ifdef WNT + struct hostent *hptr; + hptr = gethostbyname( "localhost" ); + CPPUNIT_ASSERT_MESSAGE( "#In getHostname function, error on gethostbyname()", hptr != NULL ); + aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name ); +#else + char hostname[255]; + CPPUNIT_ASSERT_MESSAGE( "#Error: gethostname failed.", gethostname(hostname, 255) == 0 ); + + struct hostent *hptr; + //first search /ets/hosts, then search from dns + hptr = gethostbyname( hostname); + if ( hptr != NULL ) + { + strcpy( hostname, hptr->h_name ); + } + + t_print("hostname is %s \n", hostname ); + aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hostname ); +#endif + return aUString; +} + +/** get IP by name, search /etc/hosts first, then search from dns, fail return OUString("") +*/ +inline ::rtl::OUString getIPbyName( rtl::OString const& str_name ) +{ + ::rtl::OUString aUString; + struct hostent *hptr; + //first search /ets/hosts, then search from dns + hptr = gethostbyname( str_name.getStr()); + if ( hptr != NULL ) + { + struct in_addr ** addrptr; + addrptr = (struct in_addr **) hptr->h_addr_list ; + //if there are more than one IPs on the same machine, we select one + for (; *addrptr; addrptr++) + { + t_print("#Local IP Address: %s\n", inet_ntoa(**addrptr)); + aUString = ::rtl::OUString::createFromAscii( (sal_Char *) (inet_ntoa(**addrptr)) ); + } + } + return aUString; +} + +/** get local ethernet IP +*/ +inline ::rtl::OUString getLocalIP( ) +{ + char hostname[255]; + gethostname(hostname, 255); + + return getIPbyName( hostname ); +} + +/** construct error message +*/ +inline ::rtl::OUString outputError( const ::rtl::OUString & returnVal, const ::rtl::OUString & rightVal, const sal_Char * msg = "") +{ + ::rtl::OUString aUString; + if ( returnVal.equals( rightVal ) ) + return aUString; + aUString += ::rtl::OUString::createFromAscii(msg); + aUString += ::rtl::OUString::createFromAscii(": the returned value is '"); + aUString += returnVal; + aUString += ::rtl::OUString::createFromAscii("', but the value should be '"); + aUString += rightVal; + aUString += ::rtl::OUString::createFromAscii("'."); + return aUString; +} + +/** wait _nSec seconds. +*/ +void thread_sleep( sal_Int32 _nSec ) +{ + /// print statement in thread process must use fflush() to force display. + t_print("# wait %d seconds. ", _nSec ); + fflush(stdout); + +#ifdef WNT //Windows + Sleep( _nSec * 100 ); +#endif +#if ( defined UNX ) || ( defined OS2 ) //Unix + usleep(_nSec * 100000); +#endif + t_print("# done\n" ); +} + +/** print Boolean value. +*/ +inline void printBool( sal_Bool bOk ) +{ + t_print("#printBool# " ); + ( sal_True == bOk ) ? t_print("YES!\n" ): t_print("NO!\n" ); +} + +/** print content of a ByteSequence. +*/ +inline void printByteSequence_IP( const ::rtl::ByteSequence & bsByteSeq, sal_Int32 nLen ) +{ + t_print("#ByteSequence is: " ); + for ( int i = 0; i < nLen; i++ ){ + if ( bsByteSeq[i] < 0 ) + t_print("%d ", 256 + bsByteSeq[i] ); + else + t_print("%d ", bsByteSeq[i] ); + } + t_print(" .\n" ); +} + +/** convert an IP which is stored as a UString format to a ByteSequence array for later use. +*/ +inline ::rtl::ByteSequence UStringIPToByteSequence( ::rtl::OUString aUStr ) +{ + + rtl::OString aString = ::rtl::OUStringToOString( aUStr, RTL_TEXTENCODING_ASCII_US ); + const sal_Char *pChar = aString.getStr( ) ; + sal_Char tmpBuffer[4]; + sal_Int32 nCharCounter = 0; + ::rtl::ByteSequence bsByteSequence( IP_VER ); + sal_Int32 nByteSeqCounter = 0; + + for ( int i = 0; i < aString.getLength( ) + 1 ; i++ ) + { + if ( ( *pChar != '.' ) && ( i !=aString.getLength( ) ) ) + tmpBuffer[nCharCounter++] = *pChar; + else + { + tmpBuffer[nCharCounter] = '\0'; + nCharCounter = 0; + bsByteSequence[nByteSeqCounter++] = static_cast<sal_Int8>( atoi( tmpBuffer ) ); + } + pChar++; + } + return bsByteSequence; +} + +/** print a socket result name. +*/ +inline void printSocketResult( oslSocketResult eResult ) +{ + t_print("#printSocketResult# " ); + if (!eResult) + switch (eResult) + { + case osl_Socket_Ok: + t_print("client connected\n"); + break; + case osl_Socket_Error: + t_print("got an error ... exiting\r\n\r\n" ); + break; + case osl_Socket_TimedOut: + t_print("timeout\n"); + break; + + case osl_Socket_FORCE_EQUAL_SIZE: + t_print("FORCE EQUAL SIZE\n"); + break; + case osl_Socket_InProgress: + t_print("In Progress\n"); + break; + case osl_Socket_Interrupted: + t_print("Interrupted\n"); + break; + } +} + +/** Client Socket Thread, served as a temp little client to communicate with server. +*/ +class ClientSocketThread : public Thread +{ +protected: + oslThreadIdentifier m_id; + ::osl::SocketAddr saTargetSocketAddr; + ::osl::ConnectorSocket csConnectorSocket; + + void SAL_CALL run( ) + { + TimeValue *pTimeout; + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 5; + pTimeout->Nanosec = 0; + + /// if the thread should terminate, schedule return false + //while ( schedule( ) == sal_True ) + //{ + if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout )) + { + csConnectorSocket.send( pTestString1, 11 ); // "test socket" + csConnectorSocket.send( pTestString2, 10); + } + else + t_print("# ClientSocketThread: connect failed! \n"); + // terminate(); + //} + csConnectorSocket.close(); + free( pTimeout ); + } + + void SAL_CALL onTerminated( ) + { + //t_print("# normally terminate this thread %d!\n", m_id ); + } + +public: + ClientSocketThread( ): + saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT9 ), + csConnectorSocket( ) + { + m_id = getIdentifier( ); + //t_print("# successfully creat this client thread %d!\n", m_id ); + } + + ~ClientSocketThread( ) + { + if ( isRunning( ) ) + t_print("# error: client thread not terminated.\n" ); + } + +}; + + +/** Server Socket Thread, served as a temp little server to communicate with client. +*/ +class ServerSocketThread : public Thread +{ +protected: + oslThreadIdentifier m_id; + + void SAL_CALL run( ) + { + ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 ); + ::osl::StreamSocket ssStreamConnection; + + //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); + while ( schedule( ) == sal_True ) + { + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + if ( sal_True != bOK1 ) + { + t_print("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ; + break; + } + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + if ( sal_True != bOK2 ) + { + t_print("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ; + break; + } + + asAcceptorSocket.enableNonBlockingMode( sal_False ); + + oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); + if (eResult != osl_Socket_Ok ) + { + t_print("ServerSocketThread: acceptConnection failed! \n"); + break; + } + sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 ); + sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 ); + pReadBuffer[nReadNumber1 + nReadNumber2] = '\0'; + //t_print("# read buffer content: %s\n", pReadBuffer ); + break; + } + ssStreamConnection.close(); + asAcceptorSocket.close(); + + } + + void SAL_CALL onTerminated( ) + { + //t_print("# normally terminate this server thread %d!\n", m_id ); + } + +public: + // public to check if data transmition is OK + sal_Char pReadBuffer[30]; + ServerSocketThread( ) + { + m_id = getIdentifier( ); + //t_print("# successfully creat this server thread %d!\n", m_id ); + } + + ~ServerSocketThread( ) + { + if ( isRunning( ) ) + t_print("# error: server thread not terminated.\n" ); + } +}; + +// ----------------------------------------------------------------------------- +// Helper functions, to create buffers, check buffers +class ValueCheckProvider +{ + bool m_bFoundFailure; + char *m_pBuffer; + sal_Int32 m_nBufferSize; + +public: + ValueCheckProvider() + : + m_bFoundFailure(false), + m_pBuffer(NULL), + m_nBufferSize(0) + { + } + + bool isFailure() {return m_bFoundFailure;} + + const char* getBuffer() {return m_pBuffer;} + char* getWriteBuffer() {return m_pBuffer;} + + sal_Int32 getBufferSize() {return m_nBufferSize;} + + bool checkValues(sal_Int32 _nLength, int _nValue) + { + m_bFoundFailure = false; + for(sal_Int32 i=0;i<_nLength;i++) + { + if (m_pBuffer[i] != _nValue) + { + m_bFoundFailure = true; + } + } + return m_bFoundFailure; + } + + void createBuffer(sal_Int32 _nLength, int _nValue) + { + m_nBufferSize = _nLength; + m_pBuffer = (char*) malloc(m_nBufferSize); + if (m_pBuffer) + { + memset(m_pBuffer, _nValue, m_nBufferSize); + } + } + + void freeBuffer() + { + if (m_pBuffer) free(m_pBuffer); + } + +}; + +// ----------------------------------------------------------------------------- +/** Client Socket Thread, served as a temp little client to communicate with server. +*/ + +class ReadSocketThread : public Thread +{ + int m_nValue; + ValueCheckProvider m_aValues; + +protected: + oslThreadIdentifier m_id; + ::osl::SocketAddr saTargetSocketAddr; + ::osl::ConnectorSocket csConnectorSocket; + + void SAL_CALL run( ) + { + TimeValue *pTimeout; + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 5; + pTimeout->Nanosec = 0; + + /// if the thread should terminate, schedule return false + //while ( schedule( ) == sal_True ) + //{ + if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout )) + { + sal_Int32 nReadCount = csConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() ); + m_aValues.checkValues(nReadCount, m_nValue); + } + else + { + t_print("# ReadSocketThread: connect failed! \n"); + } + // terminate(); + //} + //remove this line for deadlock on solaris( margritte.germany ) + csConnectorSocket.close(); + free( pTimeout ); + } + + void SAL_CALL onTerminated( ) + { + //t_print("# normally terminate this thread %d!\n", m_id ); + } + +public: + sal_Int32 getCount() {return m_aValues.getBufferSize();} + bool isOk() {return m_aValues.isFailure() == true ? false : true;} + + ReadSocketThread(sal_Int32 _nBufferSize, int _nValue ) + : + m_nValue( _nValue ), + saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT10 ), + csConnectorSocket( ) + { + m_id = getIdentifier( ); + //t_print("# successfully creat this client thread %d!\n", m_id ); + m_aValues.createBuffer(_nBufferSize, 0); + } + + ~ReadSocketThread( ) + { + if ( isRunning( ) ) + t_print("# error: client thread not terminated.\n" ); + m_aValues.freeBuffer(); + } + +}; + +/** Server Socket Thread, write a file which is large +*/ +class WriteSocketThread : public Thread +{ + ValueCheckProvider m_aValues; + +protected: + oslThreadIdentifier m_id; + + void SAL_CALL run( ) + { + ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT10 ); + ::osl::StreamSocket ssStreamConnection; + + //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + + /// if the thread should terminate, schedule return false + while ( schedule( ) == sal_True ) + { + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + if ( sal_True != bOK1 ) + { + t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ; + break; + } + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + if ( sal_True != bOK2 ) + { + t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ; + break; + } + // blocking mode, if read/recv failed, block until success + asAcceptorSocket.enableNonBlockingMode( sal_False); + + oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); + if (eResult != osl_Socket_Ok ) + { + t_print("WriteSocketThread: acceptConnection failed! \n"); + break; + } + + ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() ); + break; + } + ssStreamConnection.close(); + asAcceptorSocket.close(); + } + + void SAL_CALL onTerminated( ) + { + //t_print("# normally terminate this server thread %d!\n", m_id ); + } + +public: + // public to check if data transmition is OK + WriteSocketThread(sal_Int32 _nBufferSize, int _nValue ) + { + m_id = getIdentifier( ); + //t_print("# successfully creat this server thread %d!\n", m_id ); + + m_aValues.createBuffer(_nBufferSize, _nValue); + } + + ~WriteSocketThread( ) + { + if ( isRunning( ) ) + t_print("# error: server thread not terminated.\n" ); + m_aValues.freeBuffer(); + } + +}; + +// ----------------------------------------------------------------------------- +// just used to test socket::close() when accepting +class AcceptorThread : public Thread +{ + ::osl::AcceptorSocket asAcceptorSocket; + ::rtl::OUString aHostIP; + sal_Bool bOK; +protected: + void SAL_CALL run( ) + { + ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 ); + ::osl::StreamSocket ssStreamConnection; + + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + if ( sal_True != bOK1 ) + { + t_print("# AcceptorSocket bind address failed.\n" ) ; + return; + } + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + if ( sal_True != bOK2 ) + { + t_print("# AcceptorSocket listen address failed.\n" ) ; + return; + } + + asAcceptorSocket.enableNonBlockingMode( sal_False ); + + oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); + if (eResult != osl_Socket_Ok ) + { + bOK = sal_True; + t_print("AcceptorThread: acceptConnection failed! \n"); + } + } +public: + AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString & aBindIP ) + : asAcceptorSocket( asSocket ), aHostIP( aBindIP ) + { + bOK = sal_False; + } + + sal_Bool isOK() { return bOK; } + + ~AcceptorThread( ) + { + if ( isRunning( ) ) + { + asAcceptorSocket.shutdown(); + t_print("# error: Acceptor thread not terminated.\n" ); + } + } +}; + +class CloseSocketThread : public Thread +{ + ::osl::Socket m_sSocket; +protected: + void SAL_CALL run( ) + { + thread_sleep( 1 ); + m_sSocket.close( ); + } +public: + CloseSocketThread(::osl::Socket & sSocket ) + : m_sSocket( sSocket ) + { + } + + ~CloseSocketThread( ) + { + if ( isRunning( ) ) + { + t_print("# error: CloseSocketThread not terminated.\n" ); + } + } +}; + +//------------------------------------------------------------------------ +// tests cases begins here +//------------------------------------------------------------------------ + +namespace osl_SocketAddr +{ + + /** testing the methods: + inline SocketAddr(); + inline SocketAddr(const SocketAddr& Addr); + inline SocketAddr(const oslSocketAddr , __osl_socket_NoCopy nocopy ); + inline SocketAddr(oslSocketAddr Addr); + inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort ); + */ + + class ctors : public CppUnit::TestFixture + { + public: + + void ctors_none() + { + /// SocketAddr constructor. + ::osl::SocketAddr saSocketAddr; + + // oslSocketResult aResult; + // rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult); + + // rtl::OUString suHost2 = getThisHostname(); + + CPPUNIT_ASSERT_MESSAGE("test for none parameter constructor function: check if the socket address was created successfully", + sal_True == saSocketAddr.is( ) ); + } + + void ctors_none_000() + { + /// SocketAddr constructor. + ::osl::SocketAddr saSocketAddr; + + oslSocketResult aResult; + rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult); + rtl::OUString suHost2 = getThisHostname(); + + sal_Bool bOk = compareUString(suHost, suHost2); + + rtl::OUString suError = rtl::OUString::createFromAscii("Host names should be the same. From SocketAddr.getLocalHostname() it is'"); + suError += suHost; + suError += rtl::OUString::createFromAscii("', from getThisHostname() it is '"); + suError += suHost2; + suError += rtl::OUString::createFromAscii("'."); + + CPPUNIT_ASSERT_MESSAGE(suError, sal_True == bOk); + } + + void ctors_copy() + { + /// SocketAddr copy constructor. + ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 ); + ::osl::SocketAddr saCopySocketAddr( saSocketAddr ); + + sal_Int32 nPort = saCopySocketAddr.getPort( ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy constructor function: copy constructor, do an action of copy construction then check the port with original set.", + ( sal_True == saCopySocketAddr.is( ) ) && ( nPort == IP_PORT_HTTP1 ) ); + } + + void ctors_copy_no_001() + { +#if 0 + ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 ); + oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( ); + + ::osl::SocketAddr saSocketAddrCopy( psaOSLSocketAddr, SAL_NO_COPY ); + saSocketAddrCopy.setPort( IP_PORT_HTTP2 ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", + saSocketAddr.getPort( ) == IP_PORT_HTTP2 ); +#endif + ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 ); + CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); + + oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); + + ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); + + pSocketAddrCopy->setPort( IP_PORT_HTTP2 ); + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", + pSocketAddr->getPort( ) == IP_PORT_HTTP2 ); + + delete pSocketAddrCopy; + // LLA: don't do this also: delete pSocketAddr; + } + + void ctors_copy_no_002() + { + ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 ); + CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); + oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); + ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", + pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) ); + + delete pSocketAddrCopy; + } + + void ctors_copy_handle_001() + { + ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 ); + ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, get its port to check copy effect.", + saSocketAddrCopy.getPort( ) == IP_PORT_HTTP1 ); + } + + void ctors_copy_handle_002() + { + ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 ); + ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) ); + saSocketAddrCopy.setPort( IP_PORT_HTTP2 ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, the original one should not be changed.", + saSocketAddr.getPort( ) != IP_PORT_HTTP2 ); + } + + void ctors_hostname_port_001() + { + /// tcpip-specif constructor. + ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP ); + printUString(saSocketAddr.getHostname( ), "ctors_hostname_port_001:getHostname"); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: do a constructor using tcpip spec, check the result.", + saSocketAddr.is( ) == sal_True && + ( saSocketAddr.getPort( ) == IP_PORT_FTP )/*&& + ( sal_True == compareUString( saSocketAddr.getHostname( ), aHostName1 ) ) */); + } + + //same as is_002 + void ctors_hostname_port_002() + { + /// tcpip-specif constructor. + ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT2 ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: using an invalid IP address, the socketaddr ctors should fail", sal_False == saSocketAddr.is( )); + } + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_none ); + CPPUNIT_TEST( ctors_none_000 ); + CPPUNIT_TEST( ctors_copy ); + CPPUNIT_TEST( ctors_copy_no_001 ); + CPPUNIT_TEST( ctors_copy_no_002 ); + CPPUNIT_TEST( ctors_copy_handle_001 ); + CPPUNIT_TEST( ctors_copy_handle_002 ); + CPPUNIT_TEST( ctors_hostname_port_001 ); + CPPUNIT_TEST( ctors_hostname_port_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + + + /** testing the method: + inline sal_Bool is() const; + */ + + class is : public CppUnit::TestFixture + { + public: + void is_001() + { + ::osl::SocketAddr saSocketAddr; + + CPPUNIT_ASSERT_MESSAGE("test for is() function: create an unknown type socket, it should be True when call is.", + sal_True == saSocketAddr.is( ) ); + } + // refer to setPort_003() + void is_002() + { + ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_INVAL ); + + CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid port number", + sal_True == saSocketAddr.is( ) ); + } + + void is_003() + { + ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT ); + + CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid Ip number", + sal_True != saSocketAddr.is( ) ); + } + + CPPUNIT_TEST_SUITE( is ); + CPPUNIT_TEST( is_001 ); + CPPUNIT_TEST( is_002 ); + CPPUNIT_TEST( is_003 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class is + + + /** testing the method: + inline ::rtl::OUString SAL_CALL getHostname( oslSocketResult *pResult = 0 ) const; + */ + + class getHostname : public CppUnit::TestFixture + { + public: + void setUp() + { + } + + void tearDown() + { + } + + void getHostname_000() + { + ::osl::SocketAddr saSocketAddr( aHostIp4, IP_PORT_FTP ); + + } + + /** it will search the Ip in current machine's /etc/hosts at first, if find, then return the + mapped hostname, otherwise, it will search via DNS server, and often return hostname+ Domain name + like "sceri.PRC.Sun.COM" + The process is same as Socket::getLocalHost(), but getLocalHost can only return hostname of the current machine. + */ + void getHostname_001() + { + ::osl::SocketAddr saSocketAddr( aHostIp4, IP_PORT_FTP ); + rtl::OUString suResult = saSocketAddr.getHostname( 0 ); + rtl::OUString suError = outputError(suResult, aHostName4, "test for getHostname(0)"); + sal_Bool bOK = compareUString( suResult, aHostName4 ); + // search the returned hostname in /etc/hosts, if find, and the IP in the row is same as IP + // in the Addr, it's right also. + if ( bOK == sal_False) + { + if ( compareUString( getIPbyName( oustring2char( suResult ) ), aHostIp4 ) == sal_True ) + bOK = sal_True; + } + CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK); + } + +// LLA: now we have to control, if this behaviour is right. +// LLA: this function does not work in company (Linux, Windows) but at home + void getHostname_002() + { + rtl::OUString suHostname = rtl::OUString::createFromAscii("cn-1.germany.sun.com"); + rtl::OUString aHostIP = getIPbyName( oustring2char( suHostname ) ); + + ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_FTP ); + sal_Bool bOK = saSocketAddr.setHostname( suHostname ); + CPPUNIT_ASSERT_MESSAGE("#SocketAddr.setHostname failed", sal_True == bOK ); + oslSocketResult aResult; + rtl::OUString suResult = saSocketAddr.getHostname( &aResult ); + CPPUNIT_ASSERT_MESSAGE("SocketAddr.getHostname failed.", aResult == osl_Socket_Ok); + + rtl::OUString suError = outputError(suResult, suHostname, "test for getHostname(0)"); + bOK = compareUString( suResult, suHostname ); + if ( bOK == sal_False) + { + rtl::OString aString = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US ); + if ( compareUString( getIPbyName( aString) , aHostIp6 ) == sal_True ) + { + bOK = sal_True; + } + } + + CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); + } + + + CPPUNIT_TEST_SUITE( getHostname ); + CPPUNIT_TEST( getHostname_001 ); + CPPUNIT_TEST( getHostname_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getHostname + + + /** testing the method: + inline sal_Int32 SAL_CALL getPort() const; + */ + + class getPort : public CppUnit::TestFixture + { + public: + void getPort_001() + { + ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP ); + + CPPUNIT_ASSERT_MESSAGE( "test for getPort() function: get a normal port number.", + IP_PORT_FTP == saSocketAddr.getPort( ) ); + } + + void getPort_002() + { + ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_INVAL ); + + //t_print("#getPort_002: Port number is %d \n", saSocketAddr.getPort( )); + + CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid port to a SocketAddr, get the port to see if it can detect. it did not pass in (W32).", + saSocketAddr.getPort( )>=1 && saSocketAddr.getPort( ) <= 65535 ); + } + //two cases will return OSL_INVALID_PORT: 1. not valid SocketAddr + //2. SocketAddr family is not osl_Socket_FamilyInet, but case 2 could not be constructed + void getPort_003() + { + ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT ); + + CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid IP to a SocketAddr, get the port to see returned value. ", + saSocketAddr.getPort( ) == OSL_INVALID_PORT ); + } + + CPPUNIT_TEST_SUITE( getPort ); + CPPUNIT_TEST( getPort_001 ); + CPPUNIT_TEST( getPort_002 ); + CPPUNIT_TEST( getPort_003 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class getPort + + + /** testing the method: + inline sal_Bool SAL_CALL setPort( sal_Int32 nPort ); + rfc1413.txt: TCP port numbers are from 1-65535 + rfc1700.txt: 0/tcp Reserved ; 0/udp Reserved + */ + + class setPort : public CppUnit::TestFixture + { + public: + void setPort_001() + { + ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP ); + sal_Bool bOK = saSocketAddr.setPort( IP_PORT_TELNET ); + + CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: modify a port number setting, and check it.", + ( sal_True == bOK ) && + ( IP_PORT_TELNET == saSocketAddr.getPort( ) ) ); + } + + /** 0 to 1024 is known as the reserved port range (traditionally only root can assign programs to ports in + this range) and the ephemeral port range from 1025 to 65535. + As many of you programmers will know, when you specify the source port of 0 when you connect to a host, + the OS automatically reassigns the port number to high numbered ephemeral port. The same happens if you + try to bind a listening socket to port 0. + http://www.securiteam.com/securityreviews/5XP0Q2AAKS.html + another: http://www.muq.org/~cynbe/muq/mufref_564.html + */ + void setPort_002() + { + ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP ); + sal_Bool bOK = saSocketAddr.setPort( IP_PORT_ZERO ); + + oslSocket sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 );//sal_True); + sal_Bool bOK1 = sSocket.bind( saSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "bind SocketAddr failed", bOK1 == sal_True ); + + sal_Int32 newPort = sSocket.getLocalPort(); + //t_print("#new port is %d\n", newPort ); + + CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: port number should be in 1 ~ 65535, set port 0, it should be converted to a port number between 1024~65535.", + ( 1024 <= newPort ) && ( 65535 >= newPort ) && ( bOK == sal_True ) ); + + } + + void setPort_003() + { + ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP); + sal_Bool bOK = saSocketAddr.setPort( IP_PORT_INVAL ); + //on Linux, getPort return 34463 + //t_print("#Port number is %d \n", saSocketAddr.getPort( )); + + CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an address with invalid port. it should return error or convert it to a valid port.", + ( ( 1 <= saSocketAddr.getPort( ) ) && ( 65535 >= saSocketAddr.getPort( ) ) &&( bOK == sal_True ) ) || + bOK == sal_False); + } + + /* this is not a inet-addr => can't set port */ + void setPort_004() + { + ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_FTP); + sal_Bool bOK = saSocketAddr.setPort( IP_PORT_MYPORT ); + + CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an invalid address with valid port. it should return error.", + bOK == sal_False); + } + + + CPPUNIT_TEST_SUITE( setPort ); + CPPUNIT_TEST( setPort_001 ); + CPPUNIT_TEST( setPort_002 ); + CPPUNIT_TEST( setPort_003 ); + CPPUNIT_TEST( setPort_004 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class setPort + + + /** tester comment: + + In the following two functions, it use ::rtl::ByteSequence as an intermediate storage for address, + the ByteSequence object can hold sal_Int8 arrays, which is raged [-127, 127], in case of IP addr + that is greater than 127, say 129.158.217.202, it will stored as -127, -98, -39, -54, it is unique + in the range of sal_Int8, but lack of readability. + so may be a sal_uInt8 array is better. + */ + + + /** testing the method: + inline sal_Bool SAL_CALL setAddr( const ::rtl::ByteSequence & address ); + */ + + class setAddr : public CppUnit::TestFixture + { + public: + void setAddr_001() + { + ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_FTP ); + saSocketAddr.setAddr( UStringIPToByteSequence( aHostIp1 ) ); + ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 ); + sal_Bool bOK = sal_False; + + if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) && ( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) + bOK = sal_True; + + CPPUNIT_ASSERT_MESSAGE( "test for setAddr() function: construct Addr with \"129.158.217.202\", set it to \"127.0.0.1\", and check the correctness ", + sal_True == bOK ); + } + + + CPPUNIT_TEST_SUITE( setAddr ); + CPPUNIT_TEST( setAddr_001 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class setAddr + + + /** testing the method: + inline ::rtl::ByteSequence SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const; + */ + + class getAddr : public CppUnit::TestFixture + { + public: + void getAddr_001() + { + oslSocketResult SocketResult; + ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP ); + ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( &SocketResult ); + + sal_Bool bOK = sal_False; + + if ( ( osl_Socket_Ok == SocketResult ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) + bOK = sal_True; + + CPPUNIT_ASSERT_MESSAGE( "test for getAddr() function: construct a socketaddr with IP assigned, get the address to check correctness.Caught unknown exception on (Win32)", + sal_True == bOK && SocketResult == osl_Socket_Ok); + } + + CPPUNIT_TEST_SUITE( getAddr ); + CPPUNIT_TEST( getAddr_001 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class getAddr + + + /** testing the methods: + inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr); + inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr); + inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy ); + inline sal_Bool SAL_CALL operator== (oslSocketAddr Addr) const; + inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; /// not implemented. + */ + + class operator_equal : public CppUnit::TestFixture + { + public: + void operator_equal_001() + { + ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET); + ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP ); + + saSocketAddrEqual = saSocketAddr; + sal_Bool bOK = sal_False; + ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 ); + + if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) + bOK = sal_True; + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: use operator= to assign Ip1 to Ip2, check its modification.", + sal_True == bOK ); + } + + + void operator_equal_002() + { + ::osl::SocketAddr saSocketAddr( aHostIp3, IP_PORT_TELNET); + ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP ); + + saSocketAddrEqual = saSocketAddr; + CPPUNIT_ASSERT_MESSAGE( "after assign, the assigned SocketAddr is not same as the original Addr", + IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ); + saSocketAddrEqual.setPort( IP_PORT_MYPORT3 ); + saSocketAddr.setPort( IP_PORT_HTTP2 ); + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: perform an equal action, then try to change the original address's port. it should not be changed ( handle released), it did not pass in (W32), this is under discussion.", + IP_PORT_MYPORT3 == saSocketAddrEqual.getPort( ) ); + } + + void operator_equal_const_001() + { + const ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET); + ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP ); + + saSocketAddrEqual = saSocketAddr; + sal_Bool bOK = sal_False; + ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 ); + + if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) + bOK = sal_True; + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: use operator= const to assign Ip1 to Ip2, verify the change on the second one.", + sal_True == bOK ); + } + + void operator_equal_const_002() + { + const ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET); + ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP ); + + saSocketAddrEqual = saSocketAddr; + saSocketAddrEqual.setPort( IP_PORT_HTTP1 ); + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: change the second instance, the first one should not be altered, since it does not released the handle.", + IP_PORT_HTTP1 != saSocketAddr.getPort( ) ); + } + + void operator_equal_assign_001() + { + ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostIp1, IP_PORT_TELNET ); + CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); + ::osl::SocketAddr* pSocketAddrAssign = new ::osl::SocketAddr( aHostIp2, IP_PORT_FTP ); + oslSocketAddr poslSocketAddr = pSocketAddr->getHandle( ); + //if( m_handle ) osl_destroySocketAddr( m_handle ); so pSocketAddrAssign had been destroyed and then point to pSocketAddr + pSocketAddrAssign->assign(poslSocketAddr, SAL_NO_COPY); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", + pSocketAddrAssign->getPort( ) == IP_PORT_TELNET ); + + delete pSocketAddrAssign; + } + + void operator_is_equal_001() + { + ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET); + ::osl::SocketAddr saSocketAddrequal( aHostIp1, IP_PORT_TELNET ); + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two identical Address.", + sal_True == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) ); + } + + void operator_is_equal_002() + { + ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_FTP); + ::osl::SocketAddr saSocketAddrequal( aHostIp1, IP_PORT_TELNET ); + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two different Address.", + sal_False == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) ); + } + + CPPUNIT_TEST_SUITE( operator_equal ); + CPPUNIT_TEST( operator_equal_001 ); + CPPUNIT_TEST( operator_equal_002 ); + CPPUNIT_TEST( operator_equal_const_001 ); + CPPUNIT_TEST( operator_equal_const_002 ); + CPPUNIT_TEST( operator_equal_assign_001 ); + CPPUNIT_TEST( operator_is_equal_001 ); + CPPUNIT_TEST( operator_is_equal_002 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class operator_equal + + + + /** testing the method: + inline oslSocketAddr SAL_CALL getHandle() const; + */ + + class getSocketAddrHandle : public CppUnit::TestFixture + { + public: + + void getSocketAddrHandle_001() + { + ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 ); + CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); + oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); + ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", + pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) ); + + delete pSocketAddrCopy; + } + + void getSocketAddrHandle_002() + { + ::osl::SocketAddr saSocketAddr( aHostName3, IP_PORT_MYPORT4 ); + oslSocketAddr poslSocketAddr = saSocketAddr.getHandle( ); + + sal_Bool bOK = ( saSocketAddr == poslSocketAddr ); + //t_print("getSocketAddrHandle_002\n"); + CPPUNIT_ASSERT_MESSAGE( "test for getHandle() function: use getHandle() function as an intermediate way to create identical address.", + sal_True == bOK ); + } + + CPPUNIT_TEST_SUITE( getSocketAddrHandle ); + CPPUNIT_TEST( getSocketAddrHandle_001 ); + CPPUNIT_TEST( getSocketAddrHandle_002 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class getSocketAddrHandle + + + /** testing the method: + static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0); + */ + + class getLocalHostname : public CppUnit::TestFixture + { + public: + /* the process of getLocalHostname: 1.gethostname (same as /bin/hostname) returned name A + 2. search A in /etc/hosts, if there is an alias name is A, return the name in the same row + */ + + void getLocalHostname_000() + { + // _osl_getFullQualifiedDomainName( ); + oslSocketResult aResult = osl_Socket_Error; + rtl::OUString suHostname = osl::SocketAddr::getLocalHostname(&aResult); + CPPUNIT_ASSERT_MESSAGE("getLocalHostname failed", aResult == osl_Socket_Ok); + } + + void getLocalHostname_001() + { + oslSocketResult *pResult = NULL; + //printSocketResult(*pResult); + ::rtl::OUString suResult = ::osl::SocketAddr::getLocalHostname( pResult ); + + // LLA: IMHO localhost, or hostname by itself should be ok. + rtl::OUString suThisHost = getThisHostname( ); + bool bOk = false; + if (suThisHost.equals(rtl::OUString::createFromAscii("localhost"))) + { + bOk = true; + } + else + { + if (suThisHost.equals(suResult)) + { + bOk = true; + } + } + + ::rtl::OUString suError; + suError = outputError(suResult, getThisHostname( ), "test for getLocalHostname() function"); + + CPPUNIT_ASSERT_MESSAGE( suError, bOk == true ); + } + + CPPUNIT_TEST_SUITE( getLocalHostname ); + CPPUNIT_TEST( getLocalHostname_000 ); + CPPUNIT_TEST( getLocalHostname_001 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class getLocalHostname + + + /** testing the method: + static inline void SAL_CALL resolveHostname( const ::rtl::OUString & strHostName , SocketAddr & Addr ); + */ + + class resolveHostname : public CppUnit::TestFixture + { + public: + void resolveHostname_001() + { + ::osl::SocketAddr saSocketAddr; + ::osl::SocketAddr::resolveHostname( aHostIp1, saSocketAddr ); + ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 ); + sal_Bool bOK = sal_False; + + if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) + bOK = sal_True; + + CPPUNIT_ASSERT_MESSAGE( "test for resolveHostname() function: try to resolve localhost to 127.0.0.1.", + sal_True == bOK ); + } + + CPPUNIT_TEST_SUITE( resolveHostname ); + CPPUNIT_TEST( resolveHostname_001 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class resolveHostname + + + /** testing the method: + static inline sal_Int32 SAL_CALL getServicePort( + const ::rtl::OUString& strServiceName, + const ::rtl::OUString & strProtocolName= ::rtl::OUString::createFromAscii( "tcp" ) ); + */ + + class gettheServicePort : public CppUnit::TestFixture + { + public: + void gettheServicePort_001() + { + CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get ftp service port on TCP protocol.", + IP_PORT_FTP== ::osl::SocketAddr::getServicePort( aServiceFTP, aProtocolTCP ) ); + } + + void gettheServicePort_002() + { + CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get telnet service port on TCP protocol.", + IP_PORT_TELNET== ::osl::SocketAddr::getServicePort( aServiceTELNET, aProtocolTCP ) ); + } + + void gettheServicePort_003() + { + //Solaris has no service called "https", please see /etc/services + CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get netbios-ssn service port on UDP protocol.", + IP_PORT_NETBIOS_DGM == ::osl::SocketAddr::getServicePort( aServiceNETBIOS, aProtocolUDP ) ); + } + + void gettheServicePort_004() + { + CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get a service port which is not exist.", + OSL_INVALID_PORT == ::osl::SocketAddr::getServicePort( ::rtl::OUString::createFromAscii( "notexist" ), aProtocolUDP ) ); + } + + CPPUNIT_TEST_SUITE( gettheServicePort ); + CPPUNIT_TEST( gettheServicePort_001 ); + CPPUNIT_TEST( gettheServicePort_002 ); + CPPUNIT_TEST( gettheServicePort_003 ); + CPPUNIT_TEST( gettheServicePort_004 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class gettheServicePort + +// ----------------------------------------------------------------------------- + + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::ctors, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::is, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getHostname, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getPort, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setPort, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setAddr, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getAddr, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::operator_equal, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getSocketAddrHandle, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getLocalHostname, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::resolveHostname, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::gettheServicePort, "osl_SocketAddr"); + + +} // namespace osl_SocketAddr + + + +namespace osl_Socket +{ + + /** testing the methods: + inline Socket( ); + inline Socket( const Socket & socket ); + inline Socket( oslSocket socketHandle ); + inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire ); + */ + + /** test writer's comment: + + class Socket can not be initialized by its protected constructor, though the protected + constructor is the most convenient way to create a new socket. + it only allow the method of C function osl_createSocket like: + ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, + osl_Socket_ProtocolIp ) ); + the use of C method lost some of the transparent of tester using C++ wrapper. + */ + + + class ctors : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void ctors_none() + { + /// Socket constructor. + // ::osl::Socket sSocket; + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the socket was created successfully, if no exception occured", + 1 == 1 ); + } + + void ctors_acquire() + { + /// Socket constructor. + ::osl::Socket sSocket( sHandle ); + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully", + osl_Socket_TypeStream == sSocket.getType( ) ); + } + + void ctors_no_acquire() + { + /// Socket constructor. + ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE ); + + CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully", + osl_Socket_TypeStream == sSocket.getType( ) ); + } + + void ctors_copy_ctor() + { + ::osl::Socket sSocket( sHandle ); + /// Socket copy constructor. + ::osl::Socket copySocket( sSocket ); + + CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor", + osl_Socket_TypeStream == copySocket.getType( ) ); + } + + void ctors_TypeRaw() + { +#ifdef WNT + oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); +// LLA: ? ::osl::Socket sSocket( sHandleRaw ); + CPPUNIT_ASSERT_MESSAGE( " type osl_Socket_TypeRaw socket create failed on UNX ", sHandleRaw != NULL); +#else + oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); + CPPUNIT_ASSERT_MESSAGE( " can't create socket with type osl_Socket_TypeRaw within UNX is ok.", sHandleRaw == NULL); +#endif + } + + void ctors_family_Ipx() + { + oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + CPPUNIT_ASSERT_MESSAGE( " family osl_Socket_FamilyIpx socket create failed! ", sHandleIpx != NULL); + ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE ); + t_print("#Type is %d \n", sSocket.getType( ) ); + + CPPUNIT_ASSERT_MESSAGE(" test for create new Socket instance that family is osl_Socket_FamilyIpx", + osl_Socket_TypeStream == sSocket.getType( ) ); + } + + + + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_none ); + CPPUNIT_TEST( ctors_acquire ); + CPPUNIT_TEST( ctors_no_acquire ); + CPPUNIT_TEST( ctors_copy_ctor ); + CPPUNIT_TEST( ctors_TypeRaw ); + CPPUNIT_TEST( ctors_family_Ipx ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + + + /** testing the methods: + inline Socket& SAL_CALL operator= ( oslSocket socketHandle); + inline Socket& SAL_CALL operator= (const Socket& sock); + inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ; + inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const; + */ + + class operators : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + /** test writer's comment: + + the assignment operator does not support direct assinment like: + ::osl::Socket sSocket = sHandle. + */ + void operators_assignment_handle() + { + ::osl::Socket sSocket(sHandle); + ::osl::Socket assignSocket = sSocket.getHandle(); + + CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.", + osl_Socket_TypeStream == assignSocket.getType( ) ); + } + + void operators_assignment() + { + ::osl::Socket sSocket( sHandle ); + ::osl::Socket assignSocket = sSocket; + + CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator", + osl_Socket_TypeStream == assignSocket.getType( ) ); + } + + void operators_equal_handle_001() + { + /// Socket constructor. + ::osl::Socket sSocket( sHandle ); + ::osl::Socket equalSocket = sSocket; + + CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check equal.", + equalSocket == sHandle ); + } + + void operators_equal_handle_002() + { + /// Socket constructor. + ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); + + CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check unequal.", + !( equalSocket == sHandle ) ); + } + + void operators_equal_001() + { + ::osl::Socket sSocket( sHandle ); + /// Socket copy constructor. + ::osl::Socket equalSocket( sSocket ); + + CPPUNIT_ASSERT_MESSAGE(" test for operators_equal function: check equal.", + equalSocket == sSocket ); + } + + void operators_equal_002() + { + ::osl::Socket sSocket( sHandle ); + /// Socket copy constructor. + ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); + + CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_002 function: check unequal.", + !( equalSocket == sSocket ) ); + } + + CPPUNIT_TEST_SUITE( operators ); + CPPUNIT_TEST( operators_assignment_handle ); + CPPUNIT_TEST( operators_assignment ); + CPPUNIT_TEST( operators_equal_handle_001 ); + CPPUNIT_TEST( operators_equal_handle_002 ); + CPPUNIT_TEST( operators_equal_001 ); + CPPUNIT_TEST( operators_equal_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class operators + + + /** testing the methods: + inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite ); + inline void SAL_CALL close(); + */ + + class close : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void close_001() + { + ::osl::Socket sSocket(sHandle); + sSocket.close(); + + CPPUNIT_ASSERT_MESSAGE( "test for close_001 function: this function is reserved for test.", + sSocket.getHandle() == sHandle ); + } + + void close_002() + { +//#if defined(LINUX) + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + AcceptorThread myAcceptorThread( asSocket, aHostIp1 ); + myAcceptorThread.create(); + + thread_sleep( 1 ); + //when accepting, close the socket, the thread will not block for accepting + //man close:Any locks held on the file it was associated with, and owned by the process, are removed + asSocket.close(); + //thread_sleep( 2 ); + myAcceptorThread.join(); + + CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", + myAcceptorThread.isOK() == sal_True ); +//#endif + } + + // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( ) + void close_003() + { + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + AcceptorThread myAcceptorThread( asSocket, aHostIpZero ); + myAcceptorThread.create(); + + thread_sleep( 1 ); + asSocket.close(); + myAcceptorThread.join(); + + CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", + myAcceptorThread.isOK() == sal_True ); + } + + CPPUNIT_TEST_SUITE( close ); + CPPUNIT_TEST( close_001 ); + CPPUNIT_TEST( close_002 ); + CPPUNIT_TEST( close_003 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class close + + /** testing the method: + inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const; + */ + + class getLocalAddr : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + // get the Address of the local end of the socket + void getLocalAddr_001() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT8 ); + ::osl::SocketAddr saLocalSocketAddr; + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); + CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); + + sSocket.getLocalAddr( saLocalSocketAddr ); + + sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ; + + CPPUNIT_ASSERT_MESSAGE( "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.", + sal_True == bOK ); + } + + + CPPUNIT_TEST_SUITE( getLocalAddr ); + CPPUNIT_TEST( getLocalAddr_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getLocalAddr + + + /** testing the method: + inline sal_Int32 SAL_CALL getLocalPort() const; + */ + + class getLocalPort : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void getLocalPort_001() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT7 ); // aHostIp1 localhost + ::osl::SocketAddr saLocalSocketAddr; + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); + CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); + sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) ); + + CPPUNIT_ASSERT_MESSAGE( "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.", + sal_True == bOK ); + } + + /** test writer's comment: + + the invalid port number can not be set by giving invalid port number + such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be + valid, the only instance that the getLocalPort returns OSL_INVALID_PORT + is when saSocketAddr itself is an invalid one, that is , the IP or host name + can not be found, then the created socket address is not valid. + */ + void getLocalPort_002() + { + ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_TELNET); +#ifdef WNT + ::osl::Socket sSocket(sHandle); + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); + sSocket.bind( saBindSocketAddr ); + //Invalid IP, so bind should fail + ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), + ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT), + "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned."); + sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) ); + (void)bOK; +#else + //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT + ::rtl::OUString suError = ::rtl::OUString::createFromAscii( "on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT, but can not create Addr of that case"); +#endif + CPPUNIT_ASSERT_MESSAGE( suError, sal_False ); + + } + + void getLocalPort_003() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL); + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); + CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); + ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), + ::rtl::OUString::createFromAscii("34463"), + "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned"); + sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535); + + CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); + } + + CPPUNIT_TEST_SUITE( getLocalPort ); + CPPUNIT_TEST( getLocalPort_001 ); +// LLA: CPPUNIT_TEST( getLocalPort_002 ); + CPPUNIT_TEST( getLocalPort_003 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getLocalPort + + + /** testing the method: + inline ::rtl::OUString SAL_CALL getLocalHost() const; + + Mindyliu: on Linux, at first it will check the binded in /etc/hosts, if it has the binded IP, it will return the hostname in it; + else if the binded IP is "127.0.0.1", it will return "localhost", if it's the machine's ethernet ip such as "129.158.217.90", it + will return hostname of current processor such as "aegean.PRC.Sun.COM" + */ + + class getLocalHost : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void getLocalHost_001() + { + ::osl::Socket sSocket(sHandle); + //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu + ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT6 ); + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); + CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); + sal_Bool bOK; + ::rtl::OUString suError; +#ifdef WNT + bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; + suError = outputError(sSocket.getLocalHost( ), getThisHostname( ), +"test for getLocalHost function: create localhost socket and check name"); +#else + ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" ); + sal_Bool bRes1, bRes2; + bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ; + bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ; + bOK = bRes1 || bRes2; + suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name"); +#endif + CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); + } + + void getLocalHost_002() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_POP3); + ::osl::SocketAddr saLocalSocketAddr; + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sSocket.bind( saBindSocketAddr ); + //Invalid IP, so bind should fail + sal_Bool bOK = compareUString( sSocket.getLocalHost( ), aNullURL ) ; + ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), aNullURL, "test for getLocalHost function: getLocalHost with invalid SocketAddr"); + + CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); + } + + CPPUNIT_TEST_SUITE( getLocalHost ); + CPPUNIT_TEST( getLocalHost_001 ); + CPPUNIT_TEST( getLocalHost_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getLocalHost + + + /** testing the methods: + inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const; + inline sal_Int32 SAL_CALL getPeerPort() const; + inline ::rtl::OUString SAL_CALL getPeerHost() const; + */ + class getPeer : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + TimeValue *pTimeout; + ::osl::AcceptorSocket asAcceptorSocket; + ::osl::ConnectorSocket csConnectorSocket; + + + // initialization + void setUp( ) + { + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + free( pTimeout ); + sHandle = NULL; + asAcceptorSocket.close( ); + csConnectorSocket.close( ); + } + + + void getPeer_001() + { + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT ); + ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT ); + ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); + ::osl::StreamSocket ssConnection; + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + /// launch server socket + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind '127.0.0.1' address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + + asAcceptorSocket.enableNonBlockingMode( sal_True ); + asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + /// launch client socket + csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + + /// get peer information + csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected. + sal_Int32 peerPort = csConnectorSocket.getPeerPort( ); + ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( ); + + CPPUNIT_ASSERT_MESSAGE( "test for getPeer function: setup a connection and then get the peer address, port and host from client side.", + ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&& + ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) && + ( peerPort == saLocalSocketAddr.getPort( ) )); + } + + + CPPUNIT_TEST_SUITE( getPeer ); + CPPUNIT_TEST( getPeer_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getPeer + + + /** testing the methods: + inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface); + */ + + + class bind : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void bind_001() + { + ::osl::Socket sSocket(sHandle); + //bind must use local IP address ---mindyliu + ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 ); + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "Socket bind fail.", sal_True == bOK1 ); + + sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ; + + sSocket.close(); + CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", sal_True == bOK2 ); + } + + void bind_002() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_NETBIOS ); + ::osl::SocketAddr saLocalSocketAddr; + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True); + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; + + CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", + ( sal_False == bOK1 ) && ( sal_False == bOK2 ) ); + } + + CPPUNIT_TEST_SUITE( bind ); + CPPUNIT_TEST( bind_001 ); + CPPUNIT_TEST( bind_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class bind + + + /** testing the methods: + inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const; + + */ + class isRecvReady : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + TimeValue *pTimeout; + ::osl::AcceptorSocket asAcceptorSocket; + ::osl::ConnectorSocket csConnectorSocket; + + + // initialization + void setUp( ) + { + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + free( pTimeout ); + sHandle = NULL; + asAcceptorSocket.close( ); + csConnectorSocket.close( ); + } + + + void isRecvReady_001() + { + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT1 ); + ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT1 ); + ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); + ::osl::StreamSocket ssConnection; + /// launch server socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + /// launch client socket + csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + + /// is receive ready? + sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout ); + + CPPUNIT_ASSERT_MESSAGE( "test for isRecvReady function: setup a connection and then check if it can transmit data.", + ( sal_True == bOK3 ) ); + } + + + CPPUNIT_TEST_SUITE( isRecvReady ); + CPPUNIT_TEST( isRecvReady_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class isRecvReady + + + /** testing the methods: + inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const; + */ + class isSendReady : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + TimeValue *pTimeout; + ::osl::AcceptorSocket asAcceptorSocket; + ::osl::ConnectorSocket csConnectorSocket; + + + // initialization + void setUp( ) + { + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + free( pTimeout ); + sHandle = NULL; + asAcceptorSocket.close( ); + csConnectorSocket.close( ); + } + + + void isSendReady_001() + { + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT ); + ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT ); + ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + /// launch client socket + csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + + /// is send ready? + sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout ); + + CPPUNIT_ASSERT_MESSAGE( "test for isSendReady function: setup a connection and then check if it can transmit data.", + ( sal_True == bOK3 ) ); + } + + + CPPUNIT_TEST_SUITE( isSendReady ); + CPPUNIT_TEST( isSendReady_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class isSendReady + + + /** testing the methods: + inline oslSocketType SAL_CALL getType() const; + + */ + + class getType : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void getType_001() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", + osl_Socket_TypeStream == sSocket.getType( ) ); + } + + void getType_002() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", + osl_Socket_TypeDgram == sSocket.getType( ) ); + } + +#ifdef UNX + // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here + // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw() + void getType_003() + { + CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.this is not passed in (LINUX, SOLARIS), the osl_Socket_TypeRaw, type socket can not be created.", + sal_True); + } +#else + void getType_003() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", + osl_Socket_TypeRaw == sSocket.getType( ) ); + } +#endif + + CPPUNIT_TEST_SUITE( getType ); + CPPUNIT_TEST( getType_001 ); + CPPUNIT_TEST( getType_002 ); + CPPUNIT_TEST( getType_003 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getType + + + + /** testing the methods: + inline sal_Int32 SAL_CALL getOption( + oslSocketOption Option, + void* pBuffer, + sal_uInt32 BufferLen, + oslSocketOptionLevel Level= osl_Socket_LevelSocket) const; + + inline sal_Int32 getOption( oslSocketOption option ) const; + + */ + + class getOption : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + + } + + void tearDown( ) + { + sHandle = NULL; + } + + /** test writer's comment: + + in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream. + 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ + in 1. + */ + + void getOption_001() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); + *pType = 0; + sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) ); + sal_Bool bOK = ( SOCK_STREAM == *pType ); + // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream + //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType ); + free( pType ); + + CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get type option of socket.", + sal_True == bOK ); + } + + // getsockopt error + void getOption_004() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); + sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) ); + free( pbDontRoute ); + + CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get invalid option of socket, should return -1.", + nRes == -1 ); + } + + void getOption_simple_001() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) ); + + CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.", + sal_True == bOK ); + } + + void getOption_simple_002() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) ); + + CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.", + sal_True == bOK ); + } + + CPPUNIT_TEST_SUITE( getOption ); + CPPUNIT_TEST( getOption_001 ); + CPPUNIT_TEST( getOption_004 ); + CPPUNIT_TEST( getOption_simple_001 ); + CPPUNIT_TEST( getOption_simple_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getOption + + + /** testing the methods: + inline sal_Bool SAL_CALL setOption( oslSocketOption Option, + void* pBuffer, + sal_uInt32 BufferLen, + oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const; + */ + + class setOption : public CppUnit::TestFixture + { + public: + TimeValue *pTimeout; +// LLA: maybe there is an error in the source, +// as long as I remember, if a derived class do not overload all ctors there is a problem. + + ::osl::AcceptorSocket asAcceptorSocket; + + void setUp( ) + { + + } + + void tearDown( ) + { + asAcceptorSocket.close( ); + } + + + // LLA: + // getSocketOption returns BufferLen, or -1 if something failed + + // setSocketOption returns sal_True, if option could stored + // else sal_False + + void setOption_001() + { + /// set and get option. + int nBufferLen = sizeof ( sal_Int32); + // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool! + + sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); + *pbDontRouteSet = 1; // sal_True; + + sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); + *pGetBuffer = 0; + + // maybe asAcceptorSocket is not right initialized + sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen ); + CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b1 ) ); + sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); + CPPUNIT_ASSERT_MESSAGE( "getOption function failed.", ( n2 == nBufferLen ) ); + + // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value, + // just judge it is zero or not! + sal_Bool bOK = ( 0 != *pGetBuffer ); + t_print("#setOption_001: getOption is %d \n", *pGetBuffer); + + // toggle check, set to 0 + *pbDontRouteSet = 0; + + sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) ); + CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b3 ) ); + sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); + CPPUNIT_ASSERT_MESSAGE( "getOption (DONTROUTE) function failed.", ( n4 == nBufferLen ) ); + + sal_Bool bOK2 = ( 0 == *pGetBuffer ); + + t_print("#setOption_001: getOption is %d \n", *pGetBuffer); + +// LLA: sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); +// LLA: *pbDontTouteSet = sal_True; +// LLA: sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); +// LLA: *pbDontTouteGet = sal_False; +// LLA: asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontTouteSet, sizeof ( sal_Bool ) ); +// LLA: asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pbDontTouteGet, sizeof ( sal_Bool ) ); +// LLA: ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet), +// LLA: ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet), +// LLA: "test for setOption function: set osl_Socket_OptionDontRoute and then check"); +// LLA: +// LLA: sal_Bool bOK = ( sal_True == *pbDontTouteGet ); +// LLA: free( pbDontTouteSet ); +// LLA: free( pbDontTouteGet ); + + CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", + ( sal_True == bOK ) && (sal_True == bOK2) ); + + free( pbDontRouteSet ); + free( pGetBuffer ); +// LLA: CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); + } + + void setOption_002() + { + /// set and get option. + + // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen ); + // *pbLingerSet = 7; + // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen ); + /* struct */linger aLingerSet; + sal_Int32 nBufferLen = sizeof( struct linger ); + aLingerSet.l_onoff = 1; + aLingerSet.l_linger = 7; + + linger aLingerGet; + + asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen ); + + sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen ); + CPPUNIT_ASSERT_MESSAGE( "getOption (SO_LINGER) function failed.", ( n1 == nBufferLen ) ); + + //t_print("#setOption_002: getOption is %d \n", aLingerGet.l_linger); + sal_Bool bOK = ( 7 == aLingerGet.l_linger ); + CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check. ", + sal_True == bOK ); + + } + + void setOption_003() + { + linger aLingerSet; + aLingerSet.l_onoff = 1; + aLingerSet.l_linger = 7; + + sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 ); + printUString( asAcceptorSocket.getErrorAsString() ); + CPPUNIT_ASSERT_MESSAGE( "setOption (SO_LINGER) function failed for optlen is 0.", + ( b1 == sal_False ) ); + } + + void setOption_simple_001() + { + /// set and get option. + asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True ); + sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); + + t_print("setOption_simple_001(): getoption is %d \n", asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); + CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", + ( sal_True == bOK ) ); + } + + void setOption_simple_002() + { + /// set and get option. + // LLA: this does not work, due to the fact that SO_LINGER is a structure +// LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 ); +// LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) ); + +// LLA: CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", +// LLA: ( sal_True == bOK ) ); + } + + CPPUNIT_TEST_SUITE( setOption ); + CPPUNIT_TEST( setOption_001 ); + CPPUNIT_TEST( setOption_002 ); + CPPUNIT_TEST( setOption_003 ); + CPPUNIT_TEST( setOption_simple_001 ); +// LLA: CPPUNIT_TEST( setOption_simple_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class setOption + + + + /** testing the method: + inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode); + */ + class enableNonBlockingMode : public CppUnit::TestFixture + { + public: + ::osl::AcceptorSocket asAcceptorSocket; + + void enableNonBlockingMode_001() + { + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. + sal_Bool bOK = sal_True; + asAcceptorSocket.close( ); + + CPPUNIT_ASSERT_MESSAGE( "test for enableNonBlockingMode function: launch a server socket and make it non blocking. if it can pass the acceptConnection statement, it is non-blocking", + ( sal_True == bOK ) ); + } + + + CPPUNIT_TEST_SUITE( enableNonBlockingMode ); + CPPUNIT_TEST( enableNonBlockingMode_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class enableNonBlockingMode + + + /** testing the method: + inline sal_Bool SAL_CALL isNonBlockingMode() const; + */ + class isNonBlockingMode : public CppUnit::TestFixture + { + public: + ::osl::AcceptorSocket asAcceptorSocket; + + void isNonBlockingMode_001() + { + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + + sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. + sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( ); + asAcceptorSocket.close( ); + + CPPUNIT_ASSERT_MESSAGE( "test for isNonBlockingMode function: launch a server socket and make it non blocking. it is expected to change from blocking mode to non-blocking mode.", + ( sal_False == bOK3 ) && ( sal_True == bOK4 ) ); + } + + + CPPUNIT_TEST_SUITE( isNonBlockingMode ); + CPPUNIT_TEST( isNonBlockingMode_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class isNonBlockingMode + + /** testing the method: + inline void SAL_CALL clearError() const; + */ + class clearError : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void clearError_001() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_HTTP2 ); + ::osl::SocketAddr saLocalSocketAddr; + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" + oslSocketError seBind = sSocket.getError( ); + sSocket.clearError( ); + + CPPUNIT_ASSERT_MESSAGE( "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.", + osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None ); + } + + + CPPUNIT_TEST_SUITE( clearError ); + CPPUNIT_TEST( clearError_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class clearError + + + /** testing the methods: + inline oslSocketError getError() const; + inline ::rtl::OUString getErrorAsString( ) const; + */ + class getError : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void getError_001() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_FTP ); + ::osl::SocketAddr saLocalSocketAddr; + + CPPUNIT_ASSERT_MESSAGE( "test for getError function: should get no error.", + osl_Socket_E_None == sSocket.getError( ) ); + } + + void getError_002() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_FTP ); + ::osl::SocketAddr saLocalSocketAddr; + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" + //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError. +#if defined(SOLARIS) + CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. ", + osl_Socket_E_InvalidError == sSocket.getError( ) ); +#else + //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail. + + CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. Passed on Linux & Win32", + osl_Socket_E_AddrNotAvail == sSocket.getError( ) ); +#endif + } + + CPPUNIT_TEST_SUITE( getError ); + CPPUNIT_TEST( getError_001 ); + CPPUNIT_TEST( getError_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getError + + + + /** testing the methods: + inline oslSocket getHandle() const; + */ + + class getHandle : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + void getHandle_001() + { + ::osl::Socket sSocket(sHandle); + ::osl::Socket assignSocket = sSocket.getHandle(); + + CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.", + osl_Socket_TypeStream == assignSocket.getType( ) ); + } + + void getHandle_002() + { + ::osl::Socket sSocket( sHandle ); + ::osl::Socket assignSocket ( sSocket.getHandle( ) ); + + CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator", + osl_Socket_TypeStream == assignSocket.getType( ) ); + } + + CPPUNIT_TEST_SUITE( getHandle ); + CPPUNIT_TEST( getHandle_001 ); + CPPUNIT_TEST( getHandle_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getHandle + + +// ----------------------------------------------------------------------------- + + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::ctors, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::operators, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::close, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalAddr, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalPort, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalHost, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getPeer, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::bind, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isRecvReady, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isSendReady, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getType, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getOption, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::setOption, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::enableNonBlockingMode, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isNonBlockingMode, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::clearError, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getError, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getHandle, "osl_Socket"); + +} // namespace osl_Socket + + + +namespace osl_StreamSocket +{ + + /** testing the methods: + inline StreamSocket(oslAddrFamily Family = osl_Socket_FamilyInet, + oslProtocol Protocol = osl_Socket_ProtocolIp, + oslSocketType Type = osl_Socket_TypeStream); + + inline StreamSocket( const StreamSocket & ); + + inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire ); + + inline StreamSocket( oslSocket Socket ); + */ + + class ctors : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void ctors_none() + { + /// Socket constructor. + ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the stream socket was created successfully.", + osl_Socket_TypeStream == ssSocket.getType( ) ); + } + + void ctors_acquire() + { + /// Socket constructor. + ::osl::StreamSocket ssSocket( sHandle ); + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully", + osl_Socket_TypeStream == ssSocket.getType( ) ); + } + + void ctors_no_acquire() + { + /// Socket constructor. + ::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE ); + + CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully", + osl_Socket_TypeStream == ssSocket.getType( ) ); + } + + void ctors_copy_ctor() + { + /// Socket constructor. + ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + /// Socket copy constructor. + ::osl::StreamSocket copySocket( ssSocket ); + + CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor", + osl_Socket_TypeStream == copySocket.getType( ) ); + } + + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_none ); + CPPUNIT_TEST( ctors_acquire ); + CPPUNIT_TEST( ctors_no_acquire ); + CPPUNIT_TEST( ctors_copy_ctor ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + + class send_recv: public CppUnit::TestFixture + { + public: + // initialization + void setUp( ) + { + } + + void tearDown( ) + { + + } + + void send_recv1() + { + //client sent two strings, and server received, check the order and value + ServerSocketThread myServerThread; + ClientSocketThread myClientThread; + myServerThread.create( ); + myClientThread.create( ); + + //wait until the thread terminate + myClientThread.join( ); + myServerThread.join( ); + sal_Char myStr[30] = ""; + strcat( myStr, pTestString1 ); + strcat( myStr, pTestString2 ); + sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr ); + CPPUNIT_ASSERT_MESSAGE(" test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.", + nRes == 0 ); + } + + // error when recv + void send_recv2() + { + ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 ); + ::osl::StreamSocket ssStreamConnection; + sal_Char pReadBuffer[30] = ""; + + ClientSocketThread myClientThread; + myClientThread.create( ); + + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); + + asAcceptorSocket.bind( saLocalSocketAddr ); + asAcceptorSocket.listen( 1 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + asAcceptorSocket.acceptConnection( ssStreamConnection ); + sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 ); + + myClientThread.join( ) ; + ssStreamConnection.close(); + asAcceptorSocket.close(); + CPPUNIT_ASSERT_MESSAGE(" test for send/recv, recv error!", nReadNumber == -1 ); + } + + void write_read(sal_Int32 _nBufferSize, int _nValue) + { + //client sent two strings, and server received, check the order and value + WriteSocketThread myServerThread(_nBufferSize, _nValue); + ReadSocketThread myClientThread(_nBufferSize, _nValue); + myServerThread.create( ); +// thread_sleep( 1 ); + myClientThread.create( ); + + //wait until the thread terminate + myClientThread.join( ); + myServerThread.join( ); + + //Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500 + // Proteon PRONET = 2046), so here test read 4000 bytes + sal_Int32 nLength = myClientThread.getCount(); + bool bIsOk = myClientThread.isOk(); // check if the values are right. + + t_print("Length:=%d\n", nLength); + t_print(" bIsOk:=%d\n", bIsOk); + + CPPUNIT_ASSERT_MESSAGE(" test for write/read values with two threads: send data from server, check readed data in client.", + nLength == _nBufferSize && bIsOk == true); + } + + void write_read_001() + { + write_read(50, 10); + } + void write_read_002() + { + write_read(1024, 20); + } + void write_read_003() + { + write_read(4000, 1); + } + void write_read_004() + { + write_read(8192, 3); + } + + CPPUNIT_TEST_SUITE( send_recv ); + CPPUNIT_TEST( write_read_001 ); + CPPUNIT_TEST( write_read_002 ); + CPPUNIT_TEST( write_read_003 ); + CPPUNIT_TEST( write_read_004 ); + CPPUNIT_TEST( send_recv1 ); + CPPUNIT_TEST( send_recv2 ); +// CPPUNIT_TEST( write_read ); + CPPUNIT_TEST_SUITE_END(); + }; // class send_recv + +class SendClientThread : public ClientSocketThread +{ +protected: + + void SAL_CALL run( ) + { + TimeValue *pTimeout; + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 5; + pTimeout->Nanosec = 0; + + if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout )) + { + sal_Int32 nWrite1 = csConnectorSocket.write( pTestString1, 11 ); // "test socket" + + sal_Int32 nWrite2 = csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 ); + thread_sleep( 2 ); + csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 ); + t_print("nWrite1 is %d, nWrite2 is %d\n", nWrite1, nWrite2 ); + //thread_sleep( 1 ); + } + else + t_print("# SendClientThread: connect failed! \n"); + + csConnectorSocket.close(); + free( pTimeout ); + } + +}; + + class shutdown: public CppUnit::TestFixture + { + public: + // initialization + void setUp( ) + { + } + + void tearDown( ) + { + + } + + // similar to close_002 + void shutdown_001() + { +#if defined(LINUX) + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + AcceptorThread myAcceptorThread( asSocket, aHostIp1 ); + myAcceptorThread.create(); + + thread_sleep( 1 ); + + //when accepting, shutdown the socket, the thread will not block for accepting + asSocket.shutdown(); + myAcceptorThread.join(); + + CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", + myAcceptorThread.isOK( ) == sal_True ); +#endif + } + + void shutdown_002() + { + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9); + asSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True ); + sal_Char pReadBuffer[40]; + SendClientThread mySendThread; + mySendThread.create(); + + asSocket.enableNonBlockingMode( sal_False ); + ::osl::StreamSocket ssConnectionSocket; + oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket ); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok ); + + /* set socket option SO_LINGER 0, so close immediatly */ + linger aLingerSet; + sal_Int32 nBufferLen = sizeof( struct linger ); + aLingerSet.l_onoff = 0; + aLingerSet.l_linger = 0; + + ssConnectionSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen ); + thread_sleep( 1 ); + //sal_uInt32 nRecv1 = 0; + sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 ); + + //shutdown read after client the first send complete + ssConnectionSocket.shutdown( osl_Socket_DirRead ); + + sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 ); + sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 ); + t_print("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", nRead1, nRead2, nRead3 ); + mySendThread.join(); + + ssConnectionSocket.close(); + asSocket.close(); + + /* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0, + http://dbforums.com/arch/186/2002/12/586417 + While on Solaris, after shutdown(DirRead), all read will return 0 + */ +#ifdef LINUX + CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).", + nRead1 > 0 && nRead3 == 0 ); +#else + CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).", + nRead1 > 0 && nRead2 == 0 && nRead3 == 0 ); +#endif + + } + + void shutdown_003() + { + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9); + asSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True ); + sal_Char pReadBuffer[40]; + SendClientThread mySendThread; + mySendThread.create(); + + asSocket.enableNonBlockingMode( sal_False ); + ::osl::StreamSocket ssConnectionSocket; + oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket ); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok ); + + thread_sleep( 1 ); + //shutdown write after client the first send complete + ssConnectionSocket.shutdown( osl_Socket_DirWrite ); + + // recv should not shutdown + sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 ); + + sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 ); + // still can read + sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 ); + t_print("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", nRead1, nWrite, nRead3 ); + mySendThread.join(); + ssConnectionSocket.close(); + asSocket.close(); + + CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not send(write).", + nRead1 > 0 && nWrite == 0 && nRead3 > 0); + + } + + CPPUNIT_TEST_SUITE( shutdown ); + CPPUNIT_TEST( shutdown_001 ); + CPPUNIT_TEST( shutdown_002 ); + CPPUNIT_TEST( shutdown_003 ); + CPPUNIT_TEST_SUITE_END(); + }; // class shutdown + + class isExceptionPending: public CppUnit::TestFixture + { + public: + void isExPending_001() + { + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + TimeValue *pTimeout; + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + sal_Bool bOk = asSocket.isExceptionPending( pTimeout ); + free( pTimeout ); + + CPPUNIT_ASSERT_MESSAGE( "test for isExceptionPending.", + bOk == sal_False ); + } + + /**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/ + + + CPPUNIT_TEST_SUITE( isExceptionPending ); + CPPUNIT_TEST( isExPending_001 ); + CPPUNIT_TEST_SUITE_END(); + }; // class isExceptionPending + +// ----------------------------------------------------------------------------- + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::ctors, "osl_StreamSocket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::send_recv, "osl_StreamSocket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::shutdown, "osl_StreamSocket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::isExceptionPending, "osl_StreamSocket"); + +} // namespace osl_StreamSocket + + +namespace osl_ConnectorSocket +{ + + /** testing the method: + ConnectorSocket(oslAddrFamily Family = osl_Socket_FamilyInet, + oslProtocol Protocol = osl_Socket_ProtocolIp, + oslSocketType Type = osl_Socket_TypeStream); + */ + + class ctors : public CppUnit::TestFixture + { + public: + void ctors_001() + { + /// Socket constructor. + ::osl::ConnectorSocket csSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the connector socket was created successfully.", + osl_Socket_TypeStream == csSocket.getType( ) ); + } + + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + + /** testing the method: + oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0); + */ + + class connect : public CppUnit::TestFixture + { + public: + TimeValue *pTimeout; + ::osl::AcceptorSocket asAcceptorSocket; + ::osl::ConnectorSocket csConnectorSocket; + + + // initialization + void setUp( ) + { + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + free( pTimeout ); + // sHandle = NULL; + asAcceptorSocket.close( ); + csConnectorSocket.close( ); + } + + + void connect_001() + { + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT2 ); + ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT2 ); + ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + + //asAcceptorSocket.enableNonBlockingMode( sal_True ); + //oslSocketResult eResultAccept = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + //CPPUNIT_ASSERT_MESSAGE( "accept failed.", osl_Socket_Ok == eResultAccept ); + /// launch client socket + oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_Ok == eResult ); + + /// get peer information + csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected. + + CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.", + ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) && + ( osl_Socket_Ok == eResult )); + } + //non-blocking mode connect? + void connect_002() + { + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT3 ); + ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT3 ); + ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); + + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + + csConnectorSocket.enableNonBlockingMode( sal_True ); + + oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_InProgress == eResult || osl_Socket_Ok == eResult ); + + /// get peer information + csConnectorSocket.getPeerAddr( saPeerSocketAddr ); + + CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.", + sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) ; + } + // really an error or just delayed + // how to design senarios that will return osl_Socket_Interrupted, osl_Socket_TimedOut + void connect_003() + { + ::osl::SocketAddr saTargetSocketAddr1( aHostIp1, IP_PORT_MYPORT3 ); + ::osl::SocketAddr saTargetSocketAddr2( aHostIpInval1, IP_PORT_MYPORT3 ); + + csConnectorSocket.enableNonBlockingMode( sal_False ); + + oslSocketResult eResult1 = csConnectorSocket.connect( saTargetSocketAddr1, pTimeout ); + oslSocketResult eResult2 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout ); + CloseSocketThread myCloseThread( csConnectorSocket ); + oslSocketResult eResult3 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout ); + myCloseThread.join(); + CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult1 && + osl_Socket_Error == eResult2 && osl_Socket_Error == eResult3 ); + + } + + // really an error in non-blocking mode + void connect_004() + { + ::osl::SocketAddr saTargetSocketAddr( aHostIpInval1, IP_PORT_MYPORT3 ); + + csConnectorSocket.enableNonBlockingMode( sal_True ); + + oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult ); + } + /** here need a case: immediate connection, say in non-blocking mode connect return osl_Socket_Ok + */ + + CPPUNIT_TEST_SUITE( connect ); + CPPUNIT_TEST( connect_001 ); + CPPUNIT_TEST( connect_002 ); + CPPUNIT_TEST( connect_003 ); + CPPUNIT_TEST( connect_004 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class connect + + +// ----------------------------------------------------------------------------- + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::ctors, "osl_ConnectorSocket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::connect, "osl_ConnectorSocket"); + +} // namespace osl_ConnectorSocket + + + +namespace osl_AcceptorSocket +{ + + /** testing the methods: + inline AcceptorSocket(oslAddrFamily Family = osl_Socket_FamilyInet, + oslProtocol Protocol = osl_Socket_ProtocolIp, + oslSocketType Type = osl_Socket_TypeStream); + */ + + class ctors : public CppUnit::TestFixture + { + public: + + void ctors_001() + { + /// Socket constructor. + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the acceptor socket was created successfully.", + osl_Socket_TypeStream == asSocket.getType( ) ); + } + + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + +#if 0 + class operator_assign : public CppUnit::TestFixture + { + public: + + void assign_001() + { +#if defined(LINUX) + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::AcceptorSocket asSocketAssign( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + asSocket.setOption( osl_Socket_OptionReuseAddr, 1); + ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_MYPORT4 ); + asSocket.bind( saSocketAddr ); + + AcceptorThread myAcceptorThread( asSocketAssign, aHostIp1 ); + myAcceptorThread.create(); + + thread_sleep( 1 ); + //when accepting, assign another socket to the socket, the thread will not be closed, so is blocking + asSocketAssign = asSocket; + + t_print("#asSocketAssign port number is %d\n", asSocketAssign.getLocalPort() ); + + asSocketAssign.shutdown(); + myAcceptorThread.join(); + + CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", + myAcceptorThread.isOK() == sal_True ); + + +#endif /* LINUX */ + } + + + CPPUNIT_TEST_SUITE( operator_assign ); + CPPUNIT_TEST( assign_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class operator_assign +#endif + + /** testing the method: + inline sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1); + inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection); + inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr); + */ + + class listen_accept : public CppUnit::TestFixture + { + public: + TimeValue *pTimeout; + ::osl::AcceptorSocket asAcceptorSocket; + ::osl::ConnectorSocket csConnectorSocket; + + + // initialization + void setUp( ) + { + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1); + // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + free( pTimeout ); + // sHandle = NULL; + asAcceptorSocket.close( ); + csConnectorSocket.close( ); + } + + + void listen_accept_001() + { + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT3 ); + ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT3 ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + + /// launch client socket + csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + + oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept.", + ( osl_Socket_Ok == eResult ) ); + } + + void listen_accept_002() + { + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT4 ); + ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT4 ); + ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + + /// launch client socket + csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + + oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection, saPeerSocketAddr); /// waiting for incoming connection... + + CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept, accept with peer address.", + ( sal_True == bOK2 ) && + ( osl_Socket_Ok == eResult ) && + ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) ); + } + + void listen_accept_003() + { + + } + + CPPUNIT_TEST_SUITE( listen_accept ); + CPPUNIT_TEST( listen_accept_001 ); + CPPUNIT_TEST( listen_accept_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class listen_accept + + +// ----------------------------------------------------------------------------- + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::ctors, "osl_AcceptorSocket"); +//CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::operator_assign, "osl_AcceptorSocket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::listen_accept, "osl_AcceptorSocket"); + +} // namespace osl_AcceptorSocket + + +namespace osl_DatagramSocket +{ + + /** testing the methods: + inline DatagramSocket(oslAddrFamily Family= osl_Socket_FamilyInet, + oslProtocol Protocol= osl_Socket_ProtocolIp, + oslSocketType Type= osl_Socket_TypeDgram); + */ + + class ctors : public CppUnit::TestFixture + { + public: + + void ctors_001() + { + /// Socket constructor. + ::osl::DatagramSocket dsSocket; + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the datagram socket was created successfully.", + osl_Socket_TypeDgram == dsSocket.getType( ) ); + } + + + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + +/**thread do sendTo, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml +*/ +class TalkerThread : public Thread +{ +protected: + ::osl::SocketAddr saTargetSocketAddr; + ::osl::DatagramSocket dsSocket; + + void SAL_CALL run( ) + { + dsSocket.sendTo( saTargetSocketAddr, pTestString1, strlen( pTestString1 ) + 1 ); // "test socket" + dsSocket.shutdown(); + } + + void SAL_CALL onTerminated( ) + { + } + +public: + TalkerThread( ): + saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT9 ) + { + } + + ~TalkerThread( ) + { + if ( isRunning( ) ) + t_print("# error: TalkerThread not terminated normally.\n" ); + } +}; + +/**thread do listen, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml +*/ +class ListenerThread : public Thread +{ +protected: + ::osl::SocketAddr saTargetSocketAddr; + ::osl::DatagramSocket dsSocket; + + void SAL_CALL run( ) + { + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT10 ); + dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); + if ( dsSocket.bind( saLocalSocketAddr ) == sal_False ) + { + t_print("DatagramSocket bind failed \n"); + return; + } + //blocking mode: default + sal_Int32 nRecv = dsSocket.recvFrom( pRecvBuffer, 30, &saTargetSocketAddr); //strlen( pTestString2 ) + 1 + t_print("After recvFrom, nRecv is %d\n", nRecv); + } + + void SAL_CALL onTerminated( ) + { + } + +public: + sal_Char pRecvBuffer[30]; + ListenerThread( ): + saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT10 ) + { + pRecvBuffer[0] = '\0'; + } + + ~ListenerThread( ) + { + if ( isRunning( ) ) + t_print("# error: ListenerThread not terminated normally.\n" ); + } + +}; + + /** testing the methods: + inline sal_Int32 DatagramSocket::recvFrom(void* pBuffer, sal_uInt32 BufferSize, + SocketAddr* pSenderAddr, oslSocketMsgFlag Flag ) + inline sal_Int32 DatagramSocket::sendTo( const SocketAddr& ReceiverAddr, + const void* pBuffer, sal_uInt32 BufferSize, oslSocketMsgFlag Flag ) + */ + + class sendTo_recvFrom : public CppUnit::TestFixture + { + public: + + void sr_001() + { + ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 ); + ::osl::DatagramSocket dsSocket; + dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); + dsSocket.bind( saLocalSocketAddr ); + + sal_Char pReadBuffer[30]; + TalkerThread myTalkThread; + myTalkThread.create(); + sal_Int32 nRecv = dsSocket.recvFrom( pReadBuffer, 30, &saLocalSocketAddr); + myTalkThread.join(); + //t_print("#received buffer is %s# \n", pReadBuffer); + + sal_Bool bOk = ( strcmp(pReadBuffer, pTestString1) == 0 ); + + CPPUNIT_ASSERT_MESSAGE( "test for sendTo/recvFrom function: create a talker thread and recvFrom in the main thread, check if the datagram socket can communicate successfully.", + nRecv > 0 && bOk == sal_True ); + } + + void sr_002() + { + ::osl::SocketAddr saListenSocketAddr( aHostIp1, IP_PORT_MYPORT10 ); + ::osl::DatagramSocket dsSocket; + + //listener thread construct a DatagramSocket, recvFrom waiting for data, then main thread sendto data + ListenerThread myListenThread; + myListenThread.create(); + //to grantee the recvFrom is before sendTo + thread_sleep( 1 ); + + sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 ); + + CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo failed: nSend <= 0.", nSend > 0); + + myListenThread.join(); + //t_print("#received buffer is %s# \n", myListenThread.pRecvBuffer); + + sal_Bool bOk = ( strcmp( myListenThread.pRecvBuffer, pTestString2) == 0 ); + + CPPUNIT_ASSERT_MESSAGE( "test for sendTo/recvFrom function: create a listener thread and sendTo in the main thread, check if the datagram socket can communicate successfully.", + bOk == sal_True ); + } + + //sendTo error, return -1; recvFrom error, return -1 + void sr_003() + { + ::osl::SocketAddr saListenSocketAddr( aHostIpInval1, IP_PORT_MYPORT10 ); + ::osl::DatagramSocket dsSocket; + // Transport endpoint is not connected + sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 ); + CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo should fail: nSend <= 0.", + nSend == -1 ); + } + + void sr_004() + { + ::osl::SocketAddr saListenSocketAddr1( aHostIpInval1, IP_PORT_MYPORT10 ); + ::osl::SocketAddr saListenSocketAddr2( aHostIp2, IP_PORT_MYPORT10 ); + ::osl::DatagramSocket dsSocket; + + dsSocket.enableNonBlockingMode( sal_True ); + + sal_Char pReadBuffer[30]; + //sal_Int32 nRecv1 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 ); + + // will block ? + CloseSocketThread myThread( dsSocket ); + myThread.create(); + sal_Int32 nRecv2 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 ); + myThread.join(); + //t_print("#nRecv1 is %d nRecv2 is %d\n", nRecv1, nRecv2 ); + CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo should fail: nSend <= 0.", + nRecv2 == -1 ); + } + + CPPUNIT_TEST_SUITE( sendTo_recvFrom ); + CPPUNIT_TEST( sr_001 ); + CPPUNIT_TEST( sr_002 ); + CPPUNIT_TEST( sr_003 ); + CPPUNIT_TEST( sr_004 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class sendTo_recvFrom + +// ----------------------------------------------------------------------------- + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DatagramSocket::ctors, "osl_DatagramSocket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DatagramSocket::sendTo_recvFrom, "osl_DatagramSocket"); + +} // namespace osl_DatagramSocket + + +// ----------------------------------------------------------------------------- + +// this macro creates an empty function, which will called by the RegisterAllFunctions() +// to let the user the possibility to also register some functions by hand. +NOADDITIONAL; diff --git a/sal/qa/osl/socket/osl_Socket.xsce b/sal/qa/osl/socket/osl_Socket.xsce new file mode 100644 index 000000000000..38c02c8c75a3 --- /dev/null +++ b/sal/qa/osl/socket/osl_Socket.xsce @@ -0,0 +1,5 @@ +osl_Socket.ctors.ctors_family_Ipx +osl_SocketAddr.getHostname.getHostname_002 + +osl_StreamSocket.send_recv.write_read_003 unxsols +osl_StreamSocket.send_recv.write_read_004 unxsols diff --git a/sal/qa/osl/socket/osl_Socket2.cxx b/sal/qa/osl/socket/osl_Socket2.cxx new file mode 100644 index 000000000000..83ec82b5a77a --- /dev/null +++ b/sal/qa/osl/socket/osl_Socket2.cxx @@ -0,0 +1,1468 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sal.hxx" + +/** test coder preface: + 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform + if you are not including ws2_32.lib in makefile.mk, the including format will be like this: + + .IF "$(GUI)" == "WNT" + SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib + SHL1STDLIBS += ws2_32.lib + .ENDIF + + likewise on Solaris platform. + .IF "$(GUI)" == "UNX" + SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a + SHL1STDLIBS += -lsocket -ldl -lnsl + .ENDIF + + 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4 + category. + + 3. some fragment of Socket source implementation are lack of comment so it is hard for testers + guess what the exact functionality or usage of a member. Hope the Socket section's comment + will be added. + + 4. following functions are declared but not implemented: + inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; + */ + +//------------------------------------------------------------------------ +// include files +//------------------------------------------------------------------------ + +#include <testshl/simpleheader.hxx> + +//#include "osl_Socket_Const.h" +#include "sockethelper.hxx" + +using namespace osl; +using namespace rtl; + +#define IP_PORT_FTP 21 +#define IP_PORT_TELNET 23 +#define IP_PORT_HTTP2 8080 +#define IP_PORT_INVAL 99999 +#define IP_PORT_POP3 110 +#define IP_PORT_NETBIOS 139 +#define IP_PORT_MYPORT 8881 +#define IP_PORT_MYPORT1 8882 +#define IP_PORT_MYPORT5 8886 +#define IP_PORT_MYPORT6 8887 +#define IP_PORT_MYPORT7 8895 +#define IP_PORT_MYPORT8 8896 +#define IP_PORT_MYPORT9 8897 + +//------------------------------------------------------------------------ +// helper functions +//------------------------------------------------------------------------ + +// just used to test socket::close() when accepting +class AcceptorThread : public Thread +{ + ::osl::AcceptorSocket asAcceptorSocket; + ::rtl::OUString aHostIP; + sal_Bool bOK; +protected: + void SAL_CALL run( ) + { + ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 ); + ::osl::StreamSocket ssStreamConnection; + + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + if ( sal_True != bOK1 ) + { + t_print("# AcceptorSocket bind address failed.\n" ) ; + return; + } + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + if ( sal_True != bOK2 ) + { + t_print("# AcceptorSocket listen address failed.\n" ) ; + return; + } + + asAcceptorSocket.enableNonBlockingMode( sal_False ); + + oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); + if (eResult != osl_Socket_Ok ) + { + bOK = sal_True; + t_print("AcceptorThread: acceptConnection failed! \n"); + } + } +public: + AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP ) + : asAcceptorSocket( asSocket ), aHostIP( aBindIP ) + { + bOK = sal_False; + } + + sal_Bool isOK() { return bOK; } + + ~AcceptorThread( ) + { + if ( isRunning( ) ) + { + asAcceptorSocket.shutdown(); + t_print("# error: Acceptor thread not terminated.\n" ); + } + } +}; + +namespace osl_Socket +{ + + /** testing the methods: + inline Socket( ); + inline Socket( const Socket & socket ); + inline Socket( oslSocket socketHandle ); + inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire ); + */ + + /** test writer's comment: + + class Socket can not be initialized by its protected constructor, though the protected + constructor is the most convenient way to create a new socket. + it only allow the method of C function osl_createSocket like: + ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, + osl_Socket_ProtocolIp ) ); + the use of C method lost some of the transparent of tester using C++ wrapper. + */ + + + class ctors : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void ctors_none() + { + /// Socket constructor. + // ::osl::Socket sSocket(); + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the socket was created successfully, if no exception occured", + 1 == 1 ); + } + + void ctors_acquire() + { + /// Socket constructor. + ::osl::Socket sSocket( sHandle ); + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully", + osl_Socket_TypeStream == sSocket.getType( ) ); + } + + void ctors_no_acquire() + { + /// Socket constructor. + ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE ); + + CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully", + osl_Socket_TypeStream == sSocket.getType( ) ); + } + + void ctors_copy_ctor() + { + ::osl::Socket sSocket( sHandle ); + /// Socket copy constructor. + ::osl::Socket copySocket( sSocket ); + + CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor", + osl_Socket_TypeStream == copySocket.getType( ) ); + } + + void ctors_TypeRaw() + { +#ifdef WNT + oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); +// LLA: ? ::osl::Socket sSocket( sHandleRaw ); + CPPUNIT_ASSERT_MESSAGE( " type osl_Socket_TypeRaw socket create failed on UNX ", sHandleRaw != NULL); +#else + oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); + CPPUNIT_ASSERT_MESSAGE( " can't create socket with type osl_Socket_TypeRaw within UNX is ok.", sHandleRaw == NULL); +#endif + } + + void ctors_family_Ipx() + { + oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + CPPUNIT_ASSERT_MESSAGE( " family osl_Socket_FamilyIpx socket create failed! ", sHandleIpx != NULL); + ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE ); + t_print("#Type is %d \n", sSocket.getType( ) ); + + CPPUNIT_ASSERT_MESSAGE(" test for create new Socket instance that family is osl_Socket_FamilyIpx", + osl_Socket_TypeStream == sSocket.getType( ) ); + } + + + + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_none ); + CPPUNIT_TEST( ctors_acquire ); + CPPUNIT_TEST( ctors_no_acquire ); + CPPUNIT_TEST( ctors_copy_ctor ); + CPPUNIT_TEST( ctors_TypeRaw ); + CPPUNIT_TEST( ctors_family_Ipx ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + + + /** testing the methods: + inline Socket& SAL_CALL operator= ( oslSocket socketHandle); + inline Socket& SAL_CALL operator= (const Socket& sock); + inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ; + inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const; + */ + + class operators : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + /** test writer's comment: + + the assignment operator does not support direct assinment like: + ::osl::Socket sSocket = sHandle. + */ + void operators_assignment_handle() + { + ::osl::Socket sSocket(sHandle); + ::osl::Socket assignSocket = sSocket.getHandle(); + + CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.", + osl_Socket_TypeStream == assignSocket.getType( ) ); + } + + void operators_assignment() + { + ::osl::Socket sSocket( sHandle ); + ::osl::Socket assignSocket = sSocket; + + CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator", + osl_Socket_TypeStream == assignSocket.getType( ) ); + } + + void operators_equal_handle_001() + { + /// Socket constructor. + ::osl::Socket sSocket( sHandle ); + ::osl::Socket equalSocket = sSocket; + + CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check equal.", + equalSocket == sHandle ); + } + + void operators_equal_handle_002() + { + /// Socket constructor. + ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); + + CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check unequal.", + !( equalSocket == sHandle ) ); + } + + void operators_equal_001() + { + ::osl::Socket sSocket( sHandle ); + /// Socket copy constructor. + ::osl::Socket equalSocket( sSocket ); + + CPPUNIT_ASSERT_MESSAGE(" test for operators_equal function: check equal.", + equalSocket == sSocket ); + } + + void operators_equal_002() + { + ::osl::Socket sSocket( sHandle ); + /// Socket copy constructor. + ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); + + CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_002 function: check unequal.", + !( equalSocket == sSocket ) ); + } + + CPPUNIT_TEST_SUITE( operators ); + CPPUNIT_TEST( operators_assignment_handle ); + CPPUNIT_TEST( operators_assignment ); + CPPUNIT_TEST( operators_equal_handle_001 ); + CPPUNIT_TEST( operators_equal_handle_002 ); + CPPUNIT_TEST( operators_equal_001 ); + CPPUNIT_TEST( operators_equal_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class operators + + + /** testing the methods: + inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite ); + inline void SAL_CALL close(); + */ + + class close : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void close_001() + { + ::osl::Socket sSocket(sHandle); + sSocket.close(); + + CPPUNIT_ASSERT_MESSAGE( "test for close_001 function: this function is reserved for test.", + sSocket.getHandle() == sHandle ); + } + + void close_002() + { +//#if defined(LINUX) + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") ); + myAcceptorThread.create(); + + thread_sleep( 1 ); + //when accepting, close the socket, the thread will not block for accepting + //man close:Any locks held on the file it was associated with, and owned by the process, are removed + asSocket.close(); + //thread_sleep( 2 ); + myAcceptorThread.join(); + + CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", + myAcceptorThread.isOK() == sal_True ); +//#endif + } + + // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( ) + void close_003() + { + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("0.0.0.0") ); + myAcceptorThread.create(); + + thread_sleep( 1 ); + asSocket.close(); + myAcceptorThread.join(); + + CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", + myAcceptorThread.isOK() == sal_True ); + } + + CPPUNIT_TEST_SUITE( close ); + CPPUNIT_TEST( close_001 ); + CPPUNIT_TEST( close_002 ); + CPPUNIT_TEST( close_003 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class close + + /** testing the method: + inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const; + */ + + class getLocalAddr : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + // get the Address of the local end of the socket + void getLocalAddr_001() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT8 ); + ::osl::SocketAddr saLocalSocketAddr; + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); + CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); + + sSocket.getLocalAddr( saLocalSocketAddr ); + + sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ; + + CPPUNIT_ASSERT_MESSAGE( "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.", + sal_True == bOK ); + } + + + CPPUNIT_TEST_SUITE( getLocalAddr ); + CPPUNIT_TEST( getLocalAddr_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getLocalAddr + + + /** testing the method: + inline sal_Int32 SAL_CALL getLocalPort() const; + */ + + class getLocalPort : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void getLocalPort_001() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT7 ); // aHostIp1 localhost + ::osl::SocketAddr saLocalSocketAddr; + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); + CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); + sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) ); + + CPPUNIT_ASSERT_MESSAGE( "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.", + sal_True == bOK ); + } + + /** test writer's comment: + + the invalid port number can not be set by giving invalid port number + such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be + valid, the only instance that the getLocalPort returns OSL_INVALID_PORT + is when saSocketAddr itself is an invalid one, that is , the IP or host name + can not be found, then the created socket address is not valid. + */ + void getLocalPort_002() + { + ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_TELNET); +#ifdef WNT + ::osl::Socket sSocket(sHandle); + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); + sSocket.bind( saBindSocketAddr ); + //Invalid IP, so bind should fail + ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), + ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT), + "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned."); + sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) ); + (void)bOK; +#else + //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT + ::rtl::OUString suError = ::rtl::OUString::createFromAscii( "on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT, but can not create Addr of that case"); +#endif + CPPUNIT_ASSERT_MESSAGE( suError, sal_False ); + + } + + void getLocalPort_003() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL); + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); + CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); + ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), + ::rtl::OUString::createFromAscii("34463"), + "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned"); + sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535); + + CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); + } + + CPPUNIT_TEST_SUITE( getLocalPort ); + CPPUNIT_TEST( getLocalPort_001 ); +// LLA: CPPUNIT_TEST( getLocalPort_002 ); + CPPUNIT_TEST( getLocalPort_003 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getLocalPort + + + /** testing the method: + inline ::rtl::OUString SAL_CALL getLocalHost() const; + + Mindyliu: on Linux, at first it will check the binded in /etc/hosts, if it has the binded IP, it will return the hostname in it; + else if the binded IP is "127.0.0.1", it will return "localhost", if it's the machine's ethernet ip such as "129.158.217.90", it + will return hostname of current processor such as "aegean.PRC.Sun.COM" + */ + + class getLocalHost : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void getLocalHost_001() + { + ::osl::Socket sSocket(sHandle); + //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu + ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT6 ); + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); + CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); + sal_Bool bOK; + ::rtl::OUString suError; +#ifdef WNT + bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; + suError = outputError(sSocket.getLocalHost( ), getThisHostname( ), +"test for getLocalHost function: create localhost socket and check name"); +#else + ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" ); + sal_Bool bRes1, bRes2; + bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ; + bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ; + bOK = bRes1 || bRes2; + suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name"); +#endif + CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); + } + + void getLocalHost_002() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_POP3); + ::osl::SocketAddr saLocalSocketAddr; + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sSocket.bind( saBindSocketAddr ); + //Invalid IP, so bind should fail + sal_Bool bOK = compareUString( sSocket.getLocalHost( ), rtl::OUString::createFromAscii("") ) ; + ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), rtl::OUString::createFromAscii(""), "test for getLocalHost function: getLocalHost with invalid SocketAddr"); + + CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); + } + + CPPUNIT_TEST_SUITE( getLocalHost ); + CPPUNIT_TEST( getLocalHost_001 ); + CPPUNIT_TEST( getLocalHost_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getLocalHost + + + /** testing the methods: + inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const; + inline sal_Int32 SAL_CALL getPeerPort() const; + inline ::rtl::OUString SAL_CALL getPeerHost() const; + */ + class getPeer : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + TimeValue *pTimeout; + ::osl::AcceptorSocket asAcceptorSocket; + ::osl::ConnectorSocket csConnectorSocket; + + + // initialization + void setUp( ) + { + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + free( pTimeout ); + sHandle = NULL; + asAcceptorSocket.close( ); + csConnectorSocket.close( ); + } + + + void getPeer_001() + { + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); + ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); + ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + ::osl::StreamSocket ssConnection; + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + /// launch server socket + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind '127.0.0.1' address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + + asAcceptorSocket.enableNonBlockingMode( sal_True ); + asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + /// launch client socket + csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + + /// get peer information + csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected. + sal_Int32 peerPort = csConnectorSocket.getPeerPort( ); + ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( ); + + CPPUNIT_ASSERT_MESSAGE( "test for getPeer function: setup a connection and then get the peer address, port and host from client side.", + ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&& + ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) && + ( peerPort == saLocalSocketAddr.getPort( ) )); + } + + + CPPUNIT_TEST_SUITE( getPeer ); + CPPUNIT_TEST( getPeer_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getPeer + + + /** testing the methods: + inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface); + */ + + + class bind : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void bind_001() + { + ::osl::Socket sSocket(sHandle); + //bind must use local IP address ---mindyliu + ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 ); + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "Socket bind fail.", sal_True == bOK1 ); + + sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ; + + sSocket.close(); + CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", sal_True == bOK2 ); + } + + void bind_002() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_NETBIOS ); + ::osl::SocketAddr saLocalSocketAddr; + + sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True); + sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); + sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; + + CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", + ( sal_False == bOK1 ) && ( sal_False == bOK2 ) ); + } + + CPPUNIT_TEST_SUITE( bind ); + CPPUNIT_TEST( bind_001 ); + CPPUNIT_TEST( bind_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class bind + + + /** testing the methods: + inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const; + + */ + class isRecvReady : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + TimeValue *pTimeout; + ::osl::AcceptorSocket asAcceptorSocket; + ::osl::ConnectorSocket csConnectorSocket; + + + // initialization + void setUp( ) + { + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + free( pTimeout ); + sHandle = NULL; + asAcceptorSocket.close( ); + csConnectorSocket.close( ); + } + + + void isRecvReady_001() + { + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 ); + ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 ); + ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + ::osl::StreamSocket ssConnection; + /// launch server socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + /// launch client socket + csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + + /// is receive ready? + sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout ); + + CPPUNIT_ASSERT_MESSAGE( "test for isRecvReady function: setup a connection and then check if it can transmit data.", + ( sal_True == bOK3 ) ); + } + + + CPPUNIT_TEST_SUITE( isRecvReady ); + CPPUNIT_TEST( isRecvReady_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class isRecvReady + + + /** testing the methods: + inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const; + */ + class isSendReady : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + TimeValue *pTimeout; + ::osl::AcceptorSocket asAcceptorSocket; + ::osl::ConnectorSocket csConnectorSocket; + + + // initialization + void setUp( ) + { + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + free( pTimeout ); + sHandle = NULL; + asAcceptorSocket.close( ); + csConnectorSocket.close( ); + } + + + void isSendReady_001() + { + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); + ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); + ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + /// launch client socket + csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... + + /// is send ready? + sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout ); + + CPPUNIT_ASSERT_MESSAGE( "test for isSendReady function: setup a connection and then check if it can transmit data.", + ( sal_True == bOK3 ) ); + } + + + CPPUNIT_TEST_SUITE( isSendReady ); + CPPUNIT_TEST( isSendReady_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class isSendReady + + + /** testing the methods: + inline oslSocketType SAL_CALL getType() const; + + */ + + class getType : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void getType_001() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", + osl_Socket_TypeStream == sSocket.getType( ) ); + } + + void getType_002() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", + osl_Socket_TypeDgram == sSocket.getType( ) ); + } + +#ifdef UNX + // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here + // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw() + void getType_003() + { + CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.this is not passed in (LINUX, SOLARIS), the osl_Socket_TypeRaw, type socket can not be created.", + sal_True); + } +#else + void getType_003() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", + osl_Socket_TypeRaw == sSocket.getType( ) ); + } +#endif + + CPPUNIT_TEST_SUITE( getType ); + CPPUNIT_TEST( getType_001 ); + CPPUNIT_TEST( getType_002 ); + CPPUNIT_TEST( getType_003 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getType + + + + /** testing the methods: + inline sal_Int32 SAL_CALL getOption( + oslSocketOption Option, + void* pBuffer, + sal_uInt32 BufferLen, + oslSocketOptionLevel Level= osl_Socket_LevelSocket) const; + + inline sal_Int32 getOption( oslSocketOption option ) const; + + */ + + class getOption : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + + } + + void tearDown( ) + { + sHandle = NULL; + } + + /** test writer's comment: + + in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream. + 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ + in 1. + */ + + void getOption_001() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); + *pType = 0; + sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) ); + sal_Bool bOK = ( SOCK_STREAM == *pType ); + // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream + //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType ); + free( pType ); + + CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get type option of socket.", + sal_True == bOK ); + } + + // getsockopt error + void getOption_004() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); + sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) ); + free( pbDontRoute ); + + CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get invalid option of socket, should return -1.", + nRes == -1 ); + } + + void getOption_simple_001() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) ); + + CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.", + sal_True == bOK ); + } + + void getOption_simple_002() + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + + sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) ); + + CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.", + sal_True == bOK ); + } + + CPPUNIT_TEST_SUITE( getOption ); + CPPUNIT_TEST( getOption_001 ); + CPPUNIT_TEST( getOption_004 ); + CPPUNIT_TEST( getOption_simple_001 ); + CPPUNIT_TEST( getOption_simple_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getOption + + + /** testing the methods: + inline sal_Bool SAL_CALL setOption( oslSocketOption Option, + void* pBuffer, + sal_uInt32 BufferLen, + oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const; + */ + + class setOption : public CppUnit::TestFixture + { + public: + TimeValue *pTimeout; +// LLA: maybe there is an error in the source, +// as long as I remember, if a derived class do not overload all ctors there is a problem. + + ::osl::AcceptorSocket asAcceptorSocket; + + void setUp( ) + { + + } + + void tearDown( ) + { + asAcceptorSocket.close( ); + } + + + // LLA: + // getSocketOption returns BufferLen, or -1 if something failed + + // setSocketOption returns sal_True, if option could stored + // else sal_False + + void setOption_001() + { + /// set and get option. + int nBufferLen = sizeof ( sal_Int32); + // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool! + + sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); + *pbDontRouteSet = 1; // sal_True; + + sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); + *pGetBuffer = 0; + + // maybe asAcceptorSocket is not right initialized + sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen ); + CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b1 ) ); + sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); + CPPUNIT_ASSERT_MESSAGE( "getOption function failed.", ( n2 == nBufferLen ) ); + + // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value, + // just judge it is zero or not! + sal_Bool bOK = ( 0 != *pGetBuffer ); + t_print("#setOption_001: getOption is %d \n", *pGetBuffer); + + // toggle check, set to 0 + *pbDontRouteSet = 0; + + sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) ); + CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b3 ) ); + sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); + CPPUNIT_ASSERT_MESSAGE( "getOption (DONTROUTE) function failed.", ( n4 == nBufferLen ) ); + + sal_Bool bOK2 = ( 0 == *pGetBuffer ); + + t_print("#setOption_001: getOption is %d \n", *pGetBuffer); + +// LLA: sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); +// LLA: *pbDontTouteSet = sal_True; +// LLA: sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); +// LLA: *pbDontTouteGet = sal_False; +// LLA: asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontTouteSet, sizeof ( sal_Bool ) ); +// LLA: asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pbDontTouteGet, sizeof ( sal_Bool ) ); +// LLA: ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet), +// LLA: ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet), +// LLA: "test for setOption function: set osl_Socket_OptionDontRoute and then check"); +// LLA: +// LLA: sal_Bool bOK = ( sal_True == *pbDontTouteGet ); +// LLA: free( pbDontTouteSet ); +// LLA: free( pbDontTouteGet ); + + CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", + ( sal_True == bOK ) && (sal_True == bOK2) ); + + free( pbDontRouteSet ); + free( pGetBuffer ); +// LLA: CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); + } + + void setOption_002() + { + /// set and get option. + + // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen ); + // *pbLingerSet = 7; + // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen ); + /* struct */linger aLingerSet; + sal_Int32 nBufferLen = sizeof( struct linger ); + aLingerSet.l_onoff = 1; + aLingerSet.l_linger = 7; + + linger aLingerGet; + + asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen ); + + sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen ); + CPPUNIT_ASSERT_MESSAGE( "getOption (SO_LINGER) function failed.", ( n1 == nBufferLen ) ); + + //t_print("#setOption_002: getOption is %d \n", aLingerGet.l_linger); + sal_Bool bOK = ( 7 == aLingerGet.l_linger ); + CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check. ", + sal_True == bOK ); + + } + + void setOption_003() + { + linger aLingerSet; + aLingerSet.l_onoff = 1; + aLingerSet.l_linger = 7; + + sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 ); + printUString( asAcceptorSocket.getErrorAsString( ) ); + CPPUNIT_ASSERT_MESSAGE( "setOption (SO_LINGER) function failed for optlen is 0.", + ( b1 == sal_False ) ); + } + + void setOption_simple_001() + { + /// set and get option. + asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True ); + sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); + + t_print("setOption_simple_001(): getoption is %d \n", asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); + CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", + ( sal_True == bOK ) ); + } + + void setOption_simple_002() + { + /// set and get option. + // LLA: this does not work, due to the fact that SO_LINGER is a structure +// LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 ); +// LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) ); + +// LLA: CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", +// LLA: ( sal_True == bOK ) ); + } + + CPPUNIT_TEST_SUITE( setOption ); + CPPUNIT_TEST( setOption_001 ); + CPPUNIT_TEST( setOption_002 ); + CPPUNIT_TEST( setOption_003 ); + CPPUNIT_TEST( setOption_simple_001 ); +// LLA: CPPUNIT_TEST( setOption_simple_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class setOption + + + + /** testing the method: + inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode); + */ + class enableNonBlockingMode : public CppUnit::TestFixture + { + public: + ::osl::AcceptorSocket asAcceptorSocket; + + void enableNonBlockingMode_001() + { + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. + sal_Bool bOK = sal_True; + asAcceptorSocket.close( ); + + CPPUNIT_ASSERT_MESSAGE( "test for enableNonBlockingMode function: launch a server socket and make it non blocking. if it can pass the acceptConnection statement, it is non-blocking", + ( sal_True == bOK ) ); + } + + + CPPUNIT_TEST_SUITE( enableNonBlockingMode ); + CPPUNIT_TEST( enableNonBlockingMode_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class enableNonBlockingMode + + + /** testing the method: + inline sal_Bool SAL_CALL isNonBlockingMode() const; + */ + class isNonBlockingMode : public CppUnit::TestFixture + { + public: + ::osl::AcceptorSocket asAcceptorSocket; + + void isNonBlockingMode_001() + { + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); + ::osl::StreamSocket ssConnection; + + /// launch server socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); + + sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... + + /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. + sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( ); + asAcceptorSocket.close( ); + + CPPUNIT_ASSERT_MESSAGE( "test for isNonBlockingMode function: launch a server socket and make it non blocking. it is expected to change from blocking mode to non-blocking mode.", + ( sal_False == bOK3 ) && ( sal_True == bOK4 ) ); + } + + + CPPUNIT_TEST_SUITE( isNonBlockingMode ); + CPPUNIT_TEST( isNonBlockingMode_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class isNonBlockingMode + + /** testing the method: + inline void SAL_CALL clearError() const; + */ + class clearError : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void clearError_001() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_HTTP2 ); + ::osl::SocketAddr saLocalSocketAddr; + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" + oslSocketError seBind = sSocket.getError( ); + sSocket.clearError( ); + + CPPUNIT_ASSERT_MESSAGE( "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.", + osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None ); + } + + + CPPUNIT_TEST_SUITE( clearError ); + CPPUNIT_TEST( clearError_001 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class clearError + + + /** testing the methods: + inline oslSocketError getError() const; + inline ::rtl::OUString getErrorAsString( ) const; + */ + class getError : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void getError_001() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); + ::osl::SocketAddr saLocalSocketAddr; + + CPPUNIT_ASSERT_MESSAGE( "test for getError function: should get no error.", + osl_Socket_E_None == sSocket.getError( ) ); + } + + void getError_002() + { + ::osl::Socket sSocket(sHandle); + ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_FTP ); + ::osl::SocketAddr saLocalSocketAddr; + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" + //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError. +#if defined(SOLARIS) + CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. ", + osl_Socket_E_InvalidError == sSocket.getError( ) ); +#else + //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail. + + CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. Passed on Linux & Win32", + osl_Socket_E_AddrNotAvail == sSocket.getError( ) ); +#endif + } + + CPPUNIT_TEST_SUITE( getError ); + CPPUNIT_TEST( getError_001 ); + CPPUNIT_TEST( getError_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getError + + + + /** testing the methods: + inline oslSocket getHandle() const; + */ + + class getHandle : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + void getHandle_001() + { + ::osl::Socket sSocket(sHandle); + ::osl::Socket assignSocket = sSocket.getHandle(); + + CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.", + osl_Socket_TypeStream == assignSocket.getType( ) ); + } + + void getHandle_002() + { + ::osl::Socket sSocket( sHandle ); + ::osl::Socket assignSocket ( sSocket.getHandle( ) ); + + CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator", + osl_Socket_TypeStream == assignSocket.getType( ) ); + } + + CPPUNIT_TEST_SUITE( getHandle ); + CPPUNIT_TEST( getHandle_001 ); + CPPUNIT_TEST( getHandle_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getHandle + + +// ----------------------------------------------------------------------------- + + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::ctors, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::operators, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::close, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalAddr, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalPort, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalHost, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getPeer, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::bind, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isRecvReady, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isSendReady, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getType, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getOption, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::setOption, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::enableNonBlockingMode, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isNonBlockingMode, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::clearError, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getError, "osl_Socket"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getHandle, "osl_Socket"); + +} // namespace osl_Socket + +// ----------------------------------------------------------------------------- + +// this macro creates an empty function, which will called by the RegisterAllFunctions() +// to let the user the possibility to also register some functions by hand. +NOADDITIONAL; diff --git a/sal/qa/osl/socket/osl_Socket2.xsce b/sal/qa/osl/socket/osl_Socket2.xsce new file mode 100644 index 000000000000..66e3bf1379ec --- /dev/null +++ b/sal/qa/osl/socket/osl_Socket2.xsce @@ -0,0 +1,2 @@ +osl_Socket.ctors.ctors_family_Ipx +osl_Socket.getLocalHost.getLocalHost_001 wntmsci diff --git a/sal/qa/osl/socket/osl_SocketAddr.cxx b/sal/qa/osl/socket/osl_SocketAddr.cxx new file mode 100644 index 000000000000..6c68287ae748 --- /dev/null +++ b/sal/qa/osl/socket/osl_SocketAddr.cxx @@ -0,0 +1,895 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sal.hxx" + +/** test coder preface: + 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform + if you are not including ws2_32.lib in makefile.mk, the including format will be like this: + + .IF "$(GUI)" == "WNT" + SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib + SHL1STDLIBS += ws2_32.lib + .ENDIF + + likewise on Solaris platform. + .IF "$(GUI)" == "UNX" + SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a + SHL1STDLIBS += -lsocket -ldl -lnsl + .ENDIF + + 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4 + category. + + 3. some fragment of Socket source implementation are lack of comment so it is hard for testers + guess what the exact functionality or usage of a member. Hope the Socket section's comment + will be added. + + 4. following functions are declared but not implemented: + inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; + */ + +//------------------------------------------------------------------------ +// include files +//------------------------------------------------------------------------ + +#include <testshl/simpleheader.hxx> + +//#include "osl_Socket_Const.h" +#include "sockethelper.hxx" + +using namespace osl; +using namespace rtl; + +#define IP_PORT_ZERO 0 +#define IP_PORT_FTP 21 +#define IP_PORT_TELNET 23 +#define IP_PORT_HTTP1 80 +#define IP_PORT_HTTP2 8080 + +#define IP_PORT_MYPORT 8881 //8888 +#define IP_PORT_MYPORT2 8883 //8890 +#define IP_PORT_MYPORT3 8884 //8891 +#define IP_PORT_INVAL 99999 +#define IP_PORT_MYPORT4 8885 //8892 +#define IP_PORT_NETBIOS_DGM 138 + + +namespace osl_SocketAddr +{ + + /** testing the methods: + inline SocketAddr(); + inline SocketAddr(const SocketAddr& Addr); + inline SocketAddr(const oslSocketAddr , __osl_socket_NoCopy nocopy ); + inline SocketAddr(oslSocketAddr Addr); + inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort ); + */ + + class ctors : public CppUnit::TestFixture + { + public: + + void ctors_none() + { + /// SocketAddr constructor. + ::osl::SocketAddr saSocketAddr; + + // oslSocketResult aResult; + // rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult); + + // rtl::OUString suHost2 = getThisHostname(); + + CPPUNIT_ASSERT_MESSAGE("test for none parameter constructor function: check if the socket address was created successfully", + sal_True == saSocketAddr.is( ) ); + } + + void ctors_none_000() + { + /// SocketAddr constructor. + ::osl::SocketAddr saSocketAddr; + + oslSocketResult aResult; + rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult); + rtl::OUString suHost2 = getThisHostname(); + + sal_Bool bOk = compareUString(suHost, suHost2); + + rtl::OUString suError = rtl::OUString::createFromAscii("Host names should be the same. From SocketAddr.getLocalHostname() it is'"); + suError += suHost; + suError += rtl::OUString::createFromAscii("', from getThisHostname() it is '"); + suError += suHost2; + suError += rtl::OUString::createFromAscii("'."); + + CPPUNIT_ASSERT_MESSAGE(suError, sal_True == bOk); + } + + void ctors_copy() + { + /// SocketAddr copy constructor. + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); + ::osl::SocketAddr saCopySocketAddr( saSocketAddr ); + + sal_Int32 nPort = saCopySocketAddr.getPort( ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy constructor function: copy constructor, do an action of copy construction then check the port with original set.", + ( sal_True == saCopySocketAddr.is( ) ) && ( nPort == IP_PORT_HTTP1 ) ); + } + + void ctors_copy_no_001() + { +#if 0 + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); + oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( ); + + ::osl::SocketAddr saSocketAddrCopy( psaOSLSocketAddr, SAL_NO_COPY ); + saSocketAddrCopy.setPort( IP_PORT_HTTP2 ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", + saSocketAddr.getPort( ) == IP_PORT_HTTP2 ); +#endif + ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); + CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); + + oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); + + ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); + + pSocketAddrCopy->setPort( IP_PORT_HTTP2 ); + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", + pSocketAddr->getPort( ) == IP_PORT_HTTP2 ); + + delete pSocketAddrCopy; + // LLA: don't do this also: delete pSocketAddr; + } + + void ctors_copy_no_002() + { + ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); + CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); + oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); + ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", + pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) ); + + delete pSocketAddrCopy; + } + + void ctors_copy_handle_001() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); + ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, get its port to check copy effect.", + saSocketAddrCopy.getPort( ) == IP_PORT_HTTP1 ); + } + + void ctors_copy_handle_002() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); + ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) ); + saSocketAddrCopy.setPort( IP_PORT_HTTP2 ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, the original one should not be changed.", + saSocketAddr.getPort( ) != IP_PORT_HTTP2 ); + } + + void ctors_hostname_port_001() + { + /// tcpip-specif constructor. + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); + printUString( saSocketAddr.getHostname( ), "ctors_hostname_port_001:getHostname"); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: do a constructor using tcpip spec, check the result.", + saSocketAddr.is( ) == sal_True && + ( saSocketAddr.getPort( ) == IP_PORT_FTP )/*&& + ( sal_True == compareUString( saSocketAddr.getHostname( ), aHostName1 ) ) */); + } + + //same as is_002 + void ctors_hostname_port_002() + { + /// tcpip-specif constructor. + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT2 ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: using an invalid IP address, the socketaddr ctors should fail", sal_False == saSocketAddr.is( )); + } + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_none ); + CPPUNIT_TEST( ctors_none_000 ); + CPPUNIT_TEST( ctors_copy ); + CPPUNIT_TEST( ctors_copy_no_001 ); + CPPUNIT_TEST( ctors_copy_no_002 ); + CPPUNIT_TEST( ctors_copy_handle_001 ); + CPPUNIT_TEST( ctors_copy_handle_002 ); + CPPUNIT_TEST( ctors_hostname_port_001 ); + CPPUNIT_TEST( ctors_hostname_port_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + + + /** testing the method: + inline sal_Bool is() const; + */ + + class is : public CppUnit::TestFixture + { + public: + void is_001() + { + ::osl::SocketAddr saSocketAddr; + + CPPUNIT_ASSERT_MESSAGE("test for is() function: create an unknown type socket, it should be True when call is.", + sal_True == saSocketAddr.is( ) ); + } + // refer to setPort_003() + void is_002() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_INVAL ); + + CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid port number", + sal_True == saSocketAddr.is( ) ); + } + + void is_003() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT ); + + CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid Ip number", + sal_True != saSocketAddr.is( ) ); + } + + CPPUNIT_TEST_SUITE( is ); + CPPUNIT_TEST( is_001 ); + CPPUNIT_TEST( is_002 ); + CPPUNIT_TEST( is_003 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class is + + + /** testing the method: + inline ::rtl::OUString SAL_CALL getHostname( oslSocketResult *pResult = 0 ) const; + */ + + class getHostname : public CppUnit::TestFixture + { + public: + void setUp() + { + } + + void tearDown() + { + } + + void getHostname_000() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.107"), IP_PORT_FTP ); + rtl::OUString suResult = saSocketAddr.getHostname( 0 ); + + } + + /** it will search the Ip in current machine's /etc/hosts at first, if find, then return the + mapped hostname, otherwise, it will search via DNS server, and often return hostname+ Domain name + like "sceri.PRC.Sun.COM" + The process is same as Socket::getLocalHost(), but getLocalHost can only return hostname of the current machine. + */ + void getHostname_001() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.107"), IP_PORT_FTP ); + rtl::OUString suResult = saSocketAddr.getHostname( 0 ); + rtl::OUString suError = outputError(suResult, rtl::OUString::createFromAscii("sceri.PRC.Sun.COM"), "test for getHostname(0)"); + sal_Bool bOK = compareUString( suResult, rtl::OUString::createFromAscii("sceri.PRC.Sun.COM") ); + // search the returned hostname in /etc/hosts, if find, and the IP in the row is same as IP + // in the Addr, it's right also. + if ( bOK == sal_False) + { + rtl::OString aString = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US ); + if ( compareUString( getIPbyName( aString ), rtl::OUString::createFromAscii("129.158.217.107") ) == sal_True ) + bOK = sal_True; + } + CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK); + } + +// LLA: now we have to control, if this behaviour is right. +// LLA: this function does not work in company (Linux, Windows) but at home + void getHostname_002() + { + rtl::OUString suHostname = rtl::OUString::createFromAscii("cn-1.germany.sun.com"); + rtl::OString aString = ::rtl::OUStringToOString( suHostname, RTL_TEXTENCODING_ASCII_US ); + rtl::OUString aHostIP = getIPbyName( aString ); + + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_FTP ); + sal_Bool bOK = saSocketAddr.setHostname( suHostname ); + CPPUNIT_ASSERT_MESSAGE("#SocketAddr.setHostname failed", sal_True == bOK ); + oslSocketResult aResult; + rtl::OUString suResult = saSocketAddr.getHostname( &aResult ); + CPPUNIT_ASSERT_MESSAGE("SocketAddr.getHostname failed.", aResult == osl_Socket_Ok); + + rtl::OUString suError = outputError(suResult, suHostname, "test for getHostname(0)"); + bOK = compareUString( suResult, suHostname ); + if ( bOK == sal_False) + { + rtl::OString aStringResult = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US ); + rtl::OString aStringHostname = ::rtl::OUStringToOString( suHostname, RTL_TEXTENCODING_ASCII_US ); + if ( compareUString( getIPbyName( aStringResult ) , getIPbyName( aStringHostname ) ) == sal_True ) + { + bOK = sal_True; + } + } + + CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); + } + + + CPPUNIT_TEST_SUITE( getHostname ); + CPPUNIT_TEST( getHostname_001 ); + CPPUNIT_TEST( getHostname_002 ); + CPPUNIT_TEST_SUITE_END(); + + }; // class getHostname + + + /** testing the method: + inline sal_Int32 SAL_CALL getPort() const; + */ + + class getPort : public CppUnit::TestFixture + { + public: + void getPort_001() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); + + CPPUNIT_ASSERT_MESSAGE( "test for getPort() function: get a normal port number.", + IP_PORT_FTP == saSocketAddr.getPort( ) ); + } + + void getPort_002() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_INVAL ); + + //t_print("#getPort_002: Port number is %d \n", saSocketAddr.getPort( )); + + CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid port to a SocketAddr, get the port to see if it can detect. it did not pass in (W32).", + saSocketAddr.getPort( )>=1 && saSocketAddr.getPort( ) <= 65535 ); + } + //two cases will return OSL_INVALID_PORT: 1. not valid SocketAddr + //2. SocketAddr family is not osl_Socket_FamilyInet, but case 2 could not be constructed + void getPort_003() + { + rtl::OUString suInvalidIP = rtl::OUString::createFromAscii("123.345.67.89"); + ::osl::SocketAddr saSocketAddr( suInvalidIP, IP_PORT_MYPORT ); + + CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid IP to a SocketAddr, get the port to see returned value. ", + saSocketAddr.getPort( ) == OSL_INVALID_PORT ); + } + + CPPUNIT_TEST_SUITE( getPort ); + CPPUNIT_TEST( getPort_001 ); + CPPUNIT_TEST( getPort_002 ); + CPPUNIT_TEST( getPort_003 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class getPort + + + /** testing the method: + inline sal_Bool SAL_CALL setPort( sal_Int32 nPort ); + rfc1413.txt: TCP port numbers are from 1-65535 + rfc1700.txt: 0/tcp Reserved ; 0/udp Reserved + */ + + class setPort : public CppUnit::TestFixture + { + public: + void setPort_001() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); + sal_Bool bOK = saSocketAddr.setPort( IP_PORT_TELNET ); + + CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: modify a port number setting, and check it.", + ( sal_True == bOK ) && + ( IP_PORT_TELNET == saSocketAddr.getPort( ) ) ); + } + + /** 0 to 1024 is known as the reserved port range (traditionally only root can assign programs to ports in + this range) and the ephemeral port range from 1025 to 65535. + As many of you programmers will know, when you specify the source port of 0 when you connect to a host, + the OS automatically reassigns the port number to high numbered ephemeral port. The same happens if you + try to bind a listening socket to port 0. + http://www.securiteam.com/securityreviews/5XP0Q2AAKS.html + another: http://www.muq.org/~cynbe/muq/mufref_564.html + */ + void setPort_002() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); + sal_Bool bOK = saSocketAddr.setPort( IP_PORT_ZERO ); + + oslSocket sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + ::osl::Socket sSocket(sHandle); + sSocket.setOption( osl_Socket_OptionReuseAddr, 1 );//sal_True); + sal_Bool bOK1 = sSocket.bind( saSocketAddr ); + CPPUNIT_ASSERT_MESSAGE( "bind SocketAddr failed", bOK1 == sal_True ); + + sal_Int32 newPort = sSocket.getLocalPort(); + //t_print("#new port is %d\n", newPort ); + + CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: port number should be in 1 ~ 65535, set port 0, it should be converted to a port number between 1024~65535.", + ( 1024 <= newPort ) && ( 65535 >= newPort ) && ( bOK == sal_True ) ); + + } + + void setPort_003() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP); + sal_Bool bOK = saSocketAddr.setPort( IP_PORT_INVAL ); + //on Linux, getPort return 34463 + //t_print("#Port number is %d \n", saSocketAddr.getPort( )); + + CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an address with invalid port. it should return error or convert it to a valid port.", + ( ( 1 <= saSocketAddr.getPort( ) ) && ( 65535 >= saSocketAddr.getPort( ) ) &&( bOK == sal_True ) ) || + bOK == sal_False); + } + + /* this is not a inet-addr => can't set port */ + void setPort_004() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_FTP); + sal_Bool bOK = saSocketAddr.setPort( IP_PORT_MYPORT ); + + CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an invalid address with valid port. it should return error.", + bOK == sal_False); + } + + + CPPUNIT_TEST_SUITE( setPort ); + CPPUNIT_TEST( setPort_001 ); + CPPUNIT_TEST( setPort_002 ); + CPPUNIT_TEST( setPort_003 ); + CPPUNIT_TEST( setPort_004 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class setPort + + + /** tester comment: + + In the following two functions, it use ::rtl::ByteSequence as an intermediate storage for address, + the ByteSequence object can hold sal_Int8 arrays, which is raged [-127, 127], in case of IP addr + that is greater than 127, say 129.158.217.202, it will stored as -127, -98, -39, -54, it is unique + in the range of sal_Int8, but lack of readability. + so may be a sal_uInt8 array is better. + */ + + + /** testing the method: + inline sal_Bool SAL_CALL setAddr( const ::rtl::ByteSequence & address ); + */ + + class setAddr : public CppUnit::TestFixture + { + public: + void setAddr_001() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + saSocketAddr.setAddr( UStringIPToByteSequence( rtl::OUString::createFromAscii("127.0.0.1") ) ); + ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 ); + sal_Bool bOK = sal_False; + + // if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) && ( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) + // bOK = sal_True; + bOK = ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ); + + CPPUNIT_ASSERT_MESSAGE( "test for setAddr() function: construct Addr with \"129.158.217.202\", set it to \"127.0.0.1\", and check the correctness ", + sal_True == bOK ); + } + + + CPPUNIT_TEST_SUITE( setAddr ); + CPPUNIT_TEST( setAddr_001 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class setAddr + + + /** testing the method: + inline ::rtl::ByteSequence SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const; + */ + + class getAddr : public CppUnit::TestFixture + { + public: + void getAddr_001() + { + oslSocketResult SocketResult; + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); + ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( &SocketResult ); + + sal_Bool bOK = sal_False; + + //if ( ( osl_Socket_Ok == SocketResult ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) + // bOK = sal_True; + bOK = ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ); + + CPPUNIT_ASSERT_MESSAGE( "test for getAddr() function: construct a socketaddr with IP assigned, get the address to check correctness.Caught unknown exception on (Win32)", + sal_True == bOK && SocketResult == osl_Socket_Ok); + } + + CPPUNIT_TEST_SUITE( getAddr ); + CPPUNIT_TEST( getAddr_001 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class getAddr + + + /** testing the methods: + inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr); + inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr); + inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy ); + inline sal_Bool SAL_CALL operator== (oslSocketAddr Addr) const; + inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; /// not implemented. + */ + + class operator_equal : public CppUnit::TestFixture + { + public: + void operator_equal_001() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); + ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + + saSocketAddrEqual = saSocketAddr; + sal_Bool bOK = sal_False; + ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 ); + + // if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) + if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) && ( ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ) == sal_True ) ) + bOK = sal_True; + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: use operator= to assign Ip1 to Ip2, check its modification.", + sal_True == bOK ); + } + + + void operator_equal_002() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.199"), IP_PORT_TELNET); + ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + + saSocketAddrEqual = saSocketAddr; + CPPUNIT_ASSERT_MESSAGE( "after assign, the assigned SocketAddr is not same as the original Addr", + IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ); + saSocketAddrEqual.setPort( IP_PORT_MYPORT3 ); + saSocketAddr.setPort( IP_PORT_HTTP2 ); + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: perform an equal action, then try to change the original address's port. it should not be changed ( handle released), it did not pass in (W32), this is under discussion.", + IP_PORT_MYPORT3 == saSocketAddrEqual.getPort( ) ); + } + + void operator_equal_const_001() + { + const ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); + ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + + saSocketAddrEqual = saSocketAddr; + sal_Bool bOK = sal_False; + ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 ); + + // if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) + if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) && ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ) == sal_True ) + bOK = sal_True; + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: use operator= const to assign Ip1 to Ip2, verify the change on the second one.", + sal_True == bOK ); + } + + void operator_equal_const_002() + { + const ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); + ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + + saSocketAddrEqual = saSocketAddr; + saSocketAddrEqual.setPort( IP_PORT_HTTP1 ); + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: change the second instance, the first one should not be altered, since it does not released the handle.", + IP_PORT_HTTP1 != saSocketAddr.getPort( ) ); + } + + void operator_equal_assign_001() + { + ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET ); + CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); + ::osl::SocketAddr* pSocketAddrAssign = new ::osl::SocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); + oslSocketAddr poslSocketAddr = pSocketAddr->getHandle( ); + //if( m_handle ) osl_destroySocketAddr( m_handle ); so pSocketAddrAssign had been destroyed and then point to pSocketAddr + pSocketAddrAssign->assign(poslSocketAddr, SAL_NO_COPY); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", + pSocketAddrAssign->getPort( ) == IP_PORT_TELNET ); + + delete pSocketAddrAssign; + } + + void operator_is_equal_001() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); + ::osl::SocketAddr saSocketAddrequal( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET ); + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two identical Address.", + sal_True == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) ); + } + + void operator_is_equal_002() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP); + ::osl::SocketAddr saSocketAddrequal( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET ); + + CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two different Address.", + sal_False == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) ); + } + + CPPUNIT_TEST_SUITE( operator_equal ); + CPPUNIT_TEST( operator_equal_001 ); + CPPUNIT_TEST( operator_equal_002 ); + CPPUNIT_TEST( operator_equal_const_001 ); + CPPUNIT_TEST( operator_equal_const_002 ); + CPPUNIT_TEST( operator_equal_assign_001 ); + CPPUNIT_TEST( operator_is_equal_001 ); + CPPUNIT_TEST( operator_is_equal_002 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class operator_equal + + + + /** testing the method: + inline oslSocketAddr SAL_CALL getHandle() const; + */ + + class getSocketAddrHandle : public CppUnit::TestFixture + { + public: + + void getSocketAddrHandle_001() + { + ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); + CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); + oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); + ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); + + CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", + pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) ); + + delete pSocketAddrCopy; + } + + void getSocketAddrHandle_002() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("deuce.PRC.Sun.COM"), IP_PORT_MYPORT4 ); + oslSocketAddr poslSocketAddr = saSocketAddr.getHandle( ); + + sal_Bool bOK = ( saSocketAddr == poslSocketAddr ); + //t_print("getSocketAddrHandle_002\n"); + CPPUNIT_ASSERT_MESSAGE( "test for getHandle() function: use getHandle() function as an intermediate way to create identical address.", + sal_True == bOK ); + } + + CPPUNIT_TEST_SUITE( getSocketAddrHandle ); + CPPUNIT_TEST( getSocketAddrHandle_001 ); + CPPUNIT_TEST( getSocketAddrHandle_002 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class getSocketAddrHandle + + + /** testing the method: + static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0); + */ + + class getLocalHostname : public CppUnit::TestFixture + { + public: + /* the process of getLocalHostname: 1.gethostname (same as /bin/hostname) returned name A + 2. search A in /etc/hosts, if there is an alias name is A, return the name in the same row + */ + + void getLocalHostname_000() + { + // _osl_getFullQualifiedDomainName( ); + oslSocketResult aResult = osl_Socket_Error; + rtl::OUString suHostname = osl::SocketAddr::getLocalHostname(&aResult); + CPPUNIT_ASSERT_MESSAGE("getLocalHostname failed", aResult == osl_Socket_Ok); + } + + void getLocalHostname_001() + { + oslSocketResult *pResult = NULL; + //printSocketResult(*pResult); + ::rtl::OUString suResult = ::osl::SocketAddr::getLocalHostname( pResult ); + + // LLA: IMHO localhost, or hostname by itself should be ok. + rtl::OUString suThisHost = getThisHostname( ); + bool bOk = false; + if (suThisHost.equals(rtl::OUString::createFromAscii("localhost"))) + { + bOk = true; + } + else + { + if (suThisHost.equals(suResult)) + { + bOk = true; + } + } + + ::rtl::OUString suError; + suError = outputError(suResult, getThisHostname( ), "test for getLocalHostname() function"); + + CPPUNIT_ASSERT_MESSAGE( suError, bOk == true ); + } + + CPPUNIT_TEST_SUITE( getLocalHostname ); + CPPUNIT_TEST( getLocalHostname_000 ); + CPPUNIT_TEST( getLocalHostname_001 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class getLocalHostname + + + /** testing the method: + static inline void SAL_CALL resolveHostname( const ::rtl::OUString & strHostName , SocketAddr & Addr ); + */ + + class resolveHostname : public CppUnit::TestFixture + { + public: + void resolveHostname_001() + { + ::osl::SocketAddr saSocketAddr; + ::osl::SocketAddr::resolveHostname( rtl::OUString::createFromAscii("127.0.0.1"), saSocketAddr ); + ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 ); + sal_Bool bOK = sal_False; + + if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) + bOK = sal_True; + + CPPUNIT_ASSERT_MESSAGE( "test for resolveHostname() function: try to resolve localhost to 127.0.0.1.", + sal_True == bOK ); + } + + CPPUNIT_TEST_SUITE( resolveHostname ); + CPPUNIT_TEST( resolveHostname_001 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class resolveHostname + + + /** testing the method: + static inline sal_Int32 SAL_CALL getServicePort( + const ::rtl::OUString& strServiceName, + const ::rtl::OUString & strProtocolName= ::rtl::OUString::createFromAscii( "tcp" ) ); + */ + + class gettheServicePort : public CppUnit::TestFixture + { + public: + void gettheServicePort_001() + { + rtl::OUString suServiceFTP = rtl::OUString::createFromAscii( "ftp" ); + rtl::OUString suProtocolTCP = rtl::OUString::createFromAscii( "tcp" ); + + CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get ftp service port on TCP protocol.", + IP_PORT_FTP== ::osl::SocketAddr::getServicePort( suServiceFTP, suProtocolTCP ) ); + } + + void gettheServicePort_002() + { + rtl::OUString suServiceTELNET = rtl::OUString::createFromAscii( "telnet" ); + rtl::OUString suProtocolTCP = rtl::OUString::createFromAscii( "tcp" ); + CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get telnet service port on TCP protocol.", + IP_PORT_TELNET== ::osl::SocketAddr::getServicePort( suServiceTELNET, suProtocolTCP ) ); + } + + void gettheServicePort_003() + { + //Solaris has no service called "https", please see /etc/services + rtl::OUString suServiceNETBIOS = rtl::OUString::createFromAscii( "netbios-dgm" ); + rtl::OUString suProtocolUDP = rtl::OUString::createFromAscii( "udp" ); + CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get netbios-ssn service port on UDP protocol.", + IP_PORT_NETBIOS_DGM == ::osl::SocketAddr::getServicePort( suServiceNETBIOS, suProtocolUDP ) ); + } + + void gettheServicePort_004() + { + rtl::OUString suProtocolUDP = rtl::OUString::createFromAscii( "udp" ); + CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get a service port which is not exist.", + OSL_INVALID_PORT == ::osl::SocketAddr::getServicePort( ::rtl::OUString::createFromAscii( "notexist" ), suProtocolUDP ) ); + } + + CPPUNIT_TEST_SUITE( gettheServicePort ); + CPPUNIT_TEST( gettheServicePort_001 ); + CPPUNIT_TEST( gettheServicePort_002 ); + CPPUNIT_TEST( gettheServicePort_003 ); + CPPUNIT_TEST( gettheServicePort_004 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class gettheServicePort + + /** testing the method: + + */ + + class getFamilyOfSocketAddr : public CppUnit::TestFixture + { + public: + void getFamilyOfSocketAddr_001() + { + ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); + oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( ); + CPPUNIT_ASSERT_EQUAL( + osl_Socket_FamilyInet, + osl_getFamilyOfSocketAddr( psaOSLSocketAddr ) ); + + CPPUNIT_ASSERT_MESSAGE( "test for osl_getFamilyOfSocketAddr.", + osl_getFamilyOfSocketAddr( psaOSLSocketAddr ) == osl_Socket_FamilyInet ); + } + + CPPUNIT_TEST_SUITE( getFamilyOfSocketAddr ); + CPPUNIT_TEST( getFamilyOfSocketAddr_001 ); + CPPUNIT_TEST_SUITE_END( ); + + }; // class getFamilyOfSocketAddr + +// ----------------------------------------------------------------------------- + + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::ctors, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::is, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getHostname, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getPort, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setPort, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setAddr, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getAddr, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::operator_equal, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getSocketAddrHandle, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getLocalHostname, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::resolveHostname, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::gettheServicePort, "osl_SocketAddr"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getFamilyOfSocketAddr, "osl_SocketAddr"); + +} // namespace osl_SocketAddr + +// ----------------------------------------------------------------------------- + +// this macro creates an empty function, which will called by the RegisterAllFunctions() +// to let the user the possibility to also register some functions by hand. +NOADDITIONAL; diff --git a/sal/qa/osl/socket/osl_SocketAddr.xsce b/sal/qa/osl/socket/osl_SocketAddr.xsce new file mode 100644 index 000000000000..e69de29bb2d1 --- /dev/null +++ b/sal/qa/osl/socket/osl_SocketAddr.xsce diff --git a/sal/qa/osl/socket/osl_SocketOld.xsce b/sal/qa/osl/socket/osl_SocketOld.xsce new file mode 100644 index 000000000000..3f8120fc6943 --- /dev/null +++ b/sal/qa/osl/socket/osl_SocketOld.xsce @@ -0,0 +1,14 @@ +osl_Socket.ctors.ctors_family_Ipx +osl_SocketAddr.getHostname.getHostname_002 +osl_StreamSocket.send_recv.write_read_001 + +osl_ConnectorSocket.connect.connect_003 wntmsci +osl_Socket.getLocalHost.getLocalHost_001 wntmsci + +# hangs within solaris +osl_StreamSocket.send_recv.write_read_002 unxsols +osl_StreamSocket.send_recv.write_read_003 unxsols +osl_StreamSocket.send_recv.write_read_004 unxsols + +# hangs within solaris +osl_StreamSocket.send_recv.send_recv1 unxsols diff --git a/sal/qa/osl/socket/osl_Socket_Const.h b/sal/qa/osl/socket/osl_Socket_Const.h new file mode 100755 index 000000000000..554b188fa1ad --- /dev/null +++ b/sal/qa/osl/socket/osl_Socket_Const.h @@ -0,0 +1,196 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ + +#ifndef _OSL_SOCKET_CONST_H_ +#define _OSL_SOCKET_CONST_H_ + +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ +#include <sal/types.h> +#include <rtl/textenc.h> +#include <rtl/ustring.hxx> +#include <rtl/ustring.h> + +#ifndef _OSL_SOCLET_HXX_ +#include <osl/socket.hxx> +#endif +#include <osl/socket.h> + +#ifndef _OSL_THREAD_HXX +#include <osl/thread.hxx> +#endif + +#ifndef _OSL_FILE_HXX +#include <osl/file.hxx> +#endif + +#ifndef _OSL_MUTEX_HXX +#include <osl/mutex.hxx> +#endif +#include <osl/time.h> + +const char * pTestString1 = "test socket"; +const char * pTestString2 = " Passed#OK"; +//define read count +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ + +#ifdef __cplusplus +extern "C" +{ +#endif + +# include <stdio.h> +//------------------------------------------------------------------------ +// OS dependent declaration and includes +//------------------------------------------------------------------------ +#if ( defined UNX ) || ( defined OS2 ) //Unix +# include <unistd.h> +# include <limits.h> +# include <string.h> +# include <math.h> +# include <errno.h> +# include <fcntl.h> +# include <sys/stat.h> +# include <sys/statfs.h> +# include <sys/statvfs.h> +# include <sys/types.h> +# include <sys/socket.h> +# include <netdb.h> +# include <netinet/in.h> +# include <arpa/inet.h> +#endif +#if ( defined WNT ) // Windows +#include <tools/prewin.h> +// # include <windows.h> +# include <winsock.h> +# include <string.h> +#include <tools/postwin.h> +#endif + + +//------------------------------------------------------------------------ +// macro definition for the ASCII array/OUString declarations, +// we use p### for the ASCII array, +// a### for the OUString, +// n###Len for its length +//------------------------------------------------------------------------ + +#define OSLTEST_DECLARE( str_name, str_value ) \ + static const sal_Char p##str_name[] = str_value; \ + static const sal_Int32 n##str_name##Len = sizeof( p##str_name ) -1; \ + ::rtl::OUString a##str_name = rtl::OUString::createFromAscii( p##str_name ) + + +//------------------------------------------------------------------------ +// Ip version definition +//------------------------------------------------------------------------ +#define IP_VER 4 /// currently only IPv4 is considered. + +//------------------------------------------------------------------------ +// Ip port definition +//------------------------------------------------------------------------ +// #define IP_PORT_ZERO 0 +// #define IP_PORT_TELNET 23 +// #define IP_PORT_SMTP 25 +// #define IP_PORT_GOPHER 70 +// #define IP_PORT_POP3 110 +// #define IP_PORT_NETBIOS_DGM 138 +// #define IP_PORT_NETBIOS 139 +// #define IP_PORT_IMAP 143 +// #define IP_PORT_HTTPS 443 +// /**reference: http://www.iana.org/assignments/port-numbers */ +// #define IP_PORT_MYPORT 8881 //8888 +// #define IP_PORT_MYPORT1 8882 //8889 +// #define IP_PORT_MYPORT3 8884 //8891 +// #define IP_PORT_MYPORT4 8885 //8892 +// #define IP_PORT_MYPORT5 8886 //8893 +// #define IP_PORT_MYPORT6 8887 //8894 +// #define IP_PORT_MYPORT7 8895 +// #define IP_PORT_MYPORT8 8896 +// #define IP_PORT_MYPORT9 8897 +// #define IP_PORT_MYPORT10 8898 +// +// #define IP_PORT_TMP 9999 +// #define IP_PORT_INVAL 99999 + +//------------------------------------------------------------------------ +// service definitions. +//------------------------------------------------------------------------ +// OSLTEST_DECLARE( ServiceFTP, "ftp" ); +// OSLTEST_DECLARE( ServiceTELNET, "telnet" ); +// OSLTEST_DECLARE( ServiceGOPHER, "gopher" ); +// OSLTEST_DECLARE( ServiceIMAP, "imap" ); +// OSLTEST_DECLARE( ServiceHTTPS, "https" ); +// OSLTEST_DECLARE( ServiceNETBIOS, "netbios-dgm" ); + +//------------------------------------------------------------------------ +// protocol definitions. +//------------------------------------------------------------------------ +// OSLTEST_DECLARE( ProtocolTCP, "tcp" ); +// OSLTEST_DECLARE( ProtocolUDP, "udp" ); + +//------------------------------------------------------------------------ +// Hostnames. +//------------------------------------------------------------------------ +// OSLTEST_DECLARE( HostName1, "localhost" ); +// OSLTEST_DECLARE( HostIp1, "127.0.0.1" ); +OSLTEST_DECLARE( HostName2, "longshot.prc.sun.com" ); +OSLTEST_DECLARE( HostIp2, "129.158.217.202" ); +OSLTEST_DECLARE( HostName3, "deuce.prc.sun.com" ); +OSLTEST_DECLARE( HostIp3, "129.158.217.199" ); +OSLTEST_DECLARE( HostName4, "sceri.PRC.Sun.COM" ); //Beijing server for test +OSLTEST_DECLARE( HostIp4, "129.158.217.107" ); +OSLTEST_DECLARE( HostName5, "koori.SFBay.Sun.COM" ); //"grande.Germany.Sun.COM" ); //Germany server for test +OSLTEST_DECLARE( HostIp5, "10.5.32.20" ); +OSLTEST_DECLARE( HostName6, "crumple.SFBay.Sun.COM" ); //sfbay +OSLTEST_DECLARE( HostIp6, "10.6.103.83" ); +OSLTEST_DECLARE( HostIpInval, "123.45.67.89" ); //this is valid ip number,but can not arrive --mindy +// OSLTEST_DECLARE( HostIpInval1, "123.345.67.89" ); //this is real invalid ip number --mindy +OSLTEST_DECLARE( HostNameInval, "www.the_hostname_that_can_not_resolvable_to_an_IP_Address.com" ); +OSLTEST_DECLARE( HostIpZero, "0.0.0.0" ); + +//------------------------------------------------------------------------ +// OS independent file definition +//------------------------------------------------------------------------ +OSLTEST_DECLARE( NullURL, "" ); + +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ + +#ifdef __cplusplus +} +#endif + +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ + + +#endif /* _OSL_SOCKET_CONST_H_ */ diff --git a/sal/qa/osl/socket/osl_Socket_Const_orig.h b/sal/qa/osl/socket/osl_Socket_Const_orig.h new file mode 100644 index 000000000000..36272a5694d4 --- /dev/null +++ b/sal/qa/osl/socket/osl_Socket_Const_orig.h @@ -0,0 +1,200 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ + +#ifndef _OSL_SOCKET_CONST_H_ +#define _OSL_SOCKET_CONST_H_ + +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ +#include <sal/types.h> +#include <rtl/textenc.h> +#include <rtl/ustring.hxx> +#include <rtl/ustring.h> + +#ifndef _OSL_SOCLET_HXX_ +#include <osl/socket.hxx> +#endif +#include <osl/socket.h> + +#ifndef _OSL_THREAD_HXX +#include <osl/thread.hxx> +#endif + +#ifndef _OSL_FILE_HXX +#include <osl/file.hxx> +#endif + +#ifndef _OSL_MUTEX_HXX +#include <osl/mutex.hxx> +#endif +#include <osl/time.h> + +const char * pTestString1 = "test socket"; +const char * pTestString2 = " Passed#OK"; +//define read count +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ + +#ifdef __cplusplus +extern "C" +{ +#endif + +# include <stdio.h> +//------------------------------------------------------------------------ +// OS dependent declaration and includes +//------------------------------------------------------------------------ +#if ( defined UNX ) || ( defined OS2 ) //Unix +# include <unistd.h> +# include <limits.h> +# include <string.h> +# include <math.h> +# include <errno.h> +# include <fcntl.h> +# include <sys/stat.h> +# include <sys/statfs.h> +# include <sys/statvfs.h> +# include <sys/types.h> +# include <sys/socket.h> +# include <netdb.h> +# include <netinet/in.h> +# include <arpa/inet.h> +#endif +#if ( defined WNT ) // Windows +#include <tools/prewin.h> +// # include <windows.h> +# include <winsock.h> +# include <string.h> +#include <tools/postwin.h> +#endif + + +//------------------------------------------------------------------------ +// macro definition for the ASCII array/OUString declarations, +// we use p### for the ASCII array, +// a### for the OUString, +// n###Len for its length +//------------------------------------------------------------------------ + +#define OSLTEST_DECLARE( str_name, str_value ) \ + static const sal_Char p##str_name[] = str_value; \ + static const sal_Int32 n##str_name##Len = sizeof( p##str_name ) -1; \ + ::rtl::OUString a##str_name = rtl::OUString::createFromAscii( p##str_name ) + + +//------------------------------------------------------------------------ +// Ip version definition +//------------------------------------------------------------------------ +#define IP_VER 4 /// currently only IPv4 is considered. + +//------------------------------------------------------------------------ +// Ip port definition +//------------------------------------------------------------------------ +#define IP_PORT_ZERO 0 +#define IP_PORT_FTP 21 +#define IP_PORT_TELNET 23 +#define IP_PORT_SMTP 25 +#define IP_PORT_GOPHER 70 +#define IP_PORT_HTTP1 80 +#define IP_PORT_POP3 110 +#define IP_PORT_NETBIOS_DGM 138 +#define IP_PORT_NETBIOS 139 +#define IP_PORT_IMAP 143 +#define IP_PORT_HTTPS 443 +#define IP_PORT_HTTP2 8080 +/**reference: http://www.iana.org/assignments/port-numbers */ +#define IP_PORT_MYPORT 8881 //8888 +#define IP_PORT_MYPORT1 8882 //8889 +#define IP_PORT_MYPORT2 8883 //8890 +#define IP_PORT_MYPORT3 8884 //8891 +#define IP_PORT_MYPORT4 8885 //8892 +#define IP_PORT_MYPORT5 8886 //8893 +#define IP_PORT_MYPORT6 8887 //8894 +#define IP_PORT_MYPORT7 8895 +#define IP_PORT_MYPORT8 8896 +#define IP_PORT_MYPORT9 8897 +#define IP_PORT_MYPORT10 8898 + +#define IP_PORT_TMP 9999 +#define IP_PORT_INVAL 99999 + +//------------------------------------------------------------------------ +// service definitions. +//------------------------------------------------------------------------ +OSLTEST_DECLARE( ServiceFTP, "ftp" ); +OSLTEST_DECLARE( ServiceTELNET, "telnet" ); +OSLTEST_DECLARE( ServiceGOPHER, "gopher" ); +OSLTEST_DECLARE( ServiceIMAP, "imap" ); +OSLTEST_DECLARE( ServiceHTTPS, "https" ); +OSLTEST_DECLARE( ServiceNETBIOS, "netbios-dgm" ); + +//------------------------------------------------------------------------ +// protocol definitions. +//------------------------------------------------------------------------ +OSLTEST_DECLARE( ProtocolTCP, "tcp" ); +OSLTEST_DECLARE( ProtocolUDP, "udp" ); + +//------------------------------------------------------------------------ +// Hostnames. +//------------------------------------------------------------------------ +OSLTEST_DECLARE( HostName1, "localhost" ); +OSLTEST_DECLARE( HostIp1, "127.0.0.1" ); +OSLTEST_DECLARE( HostName2, "longshot.prc.sun.com" ); +OSLTEST_DECLARE( HostIp2, "129.158.217.202" ); +OSLTEST_DECLARE( HostName3, "deuce.prc.sun.com" ); +OSLTEST_DECLARE( HostIp3, "129.158.217.199" ); +OSLTEST_DECLARE( HostName4, "sceri.PRC.Sun.COM" ); //Beijing server for test +OSLTEST_DECLARE( HostIp4, "129.158.217.107" ); +OSLTEST_DECLARE( HostName5, "koori.SFBay.Sun.COM" ); //"grande.Germany.Sun.COM" ); //Germany server for test +OSLTEST_DECLARE( HostIp5, "10.5.32.20" ); +OSLTEST_DECLARE( HostName6, "crumple.SFBay.Sun.COM" ); //sfbay +OSLTEST_DECLARE( HostIp6, "10.6.103.83" ); +OSLTEST_DECLARE( HostIpInval, "123.45.67.89" ); //this is valid ip number,but can not arrive --mindy +OSLTEST_DECLARE( HostIpInval1, "123.345.67.89" ); //this is real invalid ip number --mindy +OSLTEST_DECLARE( HostNameInval, "www.the_hostname_that_can_not_resolvable_to_an_IP_Address.com" ); +OSLTEST_DECLARE( HostIpZero, "0.0.0.0" ); + +//------------------------------------------------------------------------ +// OS independent file definition +//------------------------------------------------------------------------ +OSLTEST_DECLARE( NullURL, "" ); + +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ + +#ifdef __cplusplus +} +#endif + +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ + + +#endif /* _OSL_SOCKET_CONST_H_ */ diff --git a/sal/qa/osl/socket/osl_Socket_tests.cxx b/sal/qa/osl/socket/osl_Socket_tests.cxx new file mode 100644 index 000000000000..3434d48203a0 --- /dev/null +++ b/sal/qa/osl/socket/osl_Socket_tests.cxx @@ -0,0 +1,88 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sal.hxx" + +//------------------------------------------------------------------------ +// include files +//------------------------------------------------------------------------ +#include <osl_Socket_Const.h> + +#include <testshl/simpleheader.hxx> +#include <osl/socket.hxx> +//------------------------------------------------------------------------ +// helper functions +//------------------------------------------------------------------------ + +//------------------------------------------------------------------------ +// tests cases begins here +//------------------------------------------------------------------------ + +namespace osl_Socket +{ + + class tests : public CppUnit::TestFixture + { + public: + + void test_001() + { + // _osl_getFullQualifiedDomainName( ); + oslSocketResult aResult; + rtl::OUString suHostname = osl::SocketAddr::getLocalHostname(&aResult); + CPPUNIT_ASSERT_MESSAGE("getLocalHostname failed", aResult == osl_Socket_Ok); + } + + CPPUNIT_TEST_SUITE( tests ); + CPPUNIT_TEST( test_001 ); + CPPUNIT_TEST_SUITE_END(); + }; + + CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::tests, "osl_SocketTest"); +} + + +// ----------------------------------------------------------------------------- + +// this macro creates an empty function, which will called by the RegisterAllFunctions() +// to let the user the possibility to also register some functions by hand. + +/*#if (defined LINUX) + +void RegisterAdditionalFunctions( FktRegFuncPtr _pFunc ) +{ + // for cover lines in _osl_getFullQualifiedDomainName( ) + // STAR_OVERRIDE_DOMAINNAME is more an internal HACK for 5.2, which should remove from sal + setenv( "STAR_OVERRIDE_DOMAINNAME", "PRC.Sun.COM", 0 ); +} + +#else*/ + +NOADDITIONAL; + +//#endif diff --git a/sal/qa/osl/socket/osl_Socket_tests.xsce b/sal/qa/osl/socket/osl_Socket_tests.xsce new file mode 100644 index 000000000000..d247b2365273 --- /dev/null +++ b/sal/qa/osl/socket/osl_Socket_tests.xsce @@ -0,0 +1 @@ +osl_SocketTest.tests.test_001 diff --git a/sal/qa/osl/socket/osl_StreamSocket.cxx b/sal/qa/osl/socket/osl_StreamSocket.cxx new file mode 100644 index 000000000000..9883511cb43e --- /dev/null +++ b/sal/qa/osl/socket/osl_StreamSocket.cxx @@ -0,0 +1,1506 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sal.hxx" + +/** test coder preface: + 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform + if you are not including ws2_32.lib in makefile.mk, the including format will be like this: + + .IF "$(GUI)" == "WNT" + SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib + SHL1STDLIBS += ws2_32.lib + .ENDIF + + likewise on Solaris platform. + .IF "$(GUI)" == "UNX" + SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a + SHL1STDLIBS += -lsocket -ldl -lnsl + .ENDIF + + 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4 + category. + + 3. some fragment of Socket source implementation are lack of comment so it is hard for testers + guess what the exact functionality or usage of a member. Hope the Socket section's comment + will be added. + + 4. following functions are declared but not implemented: + inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; +*/ + +//------------------------------------------------------------------------ +// include files +//------------------------------------------------------------------------ + +#include <testshl/simpleheader.hxx> + +//#include "osl_Socket_Const.h" +#include "sockethelper.hxx" +#include <osl/conditn.hxx> + +using namespace osl; +using namespace rtl; + +#define IP_PORT_MYPORT9 8897 +#define IP_PORT_MYPORT10 18900 + +const char * pTestString1 = "test socket"; +const char * pTestString2 = " Passed#OK"; + +//------------------------------------------------------------------------ +// helper functions +//------------------------------------------------------------------------ + +// just used to test socket::close() when accepting +class AcceptorThread : public Thread +{ + ::osl::AcceptorSocket asAcceptorSocket; + ::rtl::OUString aHostIP; + sal_Bool bOK; +protected: + void SAL_CALL run( ) + { + ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 ); + ::osl::StreamSocket ssStreamConnection; + + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + if ( sal_True != bOK1 ) + { + t_print("# AcceptorSocket bind address failed.\n" ) ; + return; + } + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + if ( sal_True != bOK2 ) + { + t_print("# AcceptorSocket listen address failed.\n" ) ; + return; + } + + asAcceptorSocket.enableNonBlockingMode( sal_False ); + + oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); + if (eResult != osl_Socket_Ok ) + { + bOK = sal_True; + t_print("AcceptorThread: acceptConnection failed! \n"); + } + } +public: + AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP ) + : asAcceptorSocket( asSocket ), aHostIP( aBindIP ) + { + bOK = sal_False; + } + + sal_Bool isOK() { return bOK; } + + ~AcceptorThread( ) + { + if ( isRunning( ) ) + { + asAcceptorSocket.shutdown(); + t_print("# error: Acceptor thread not terminated.\n" ); + } + } +}; + +/** Server Socket Thread, served as a temp little server to communicate with client. + */ +class ServerSocketThread : public Thread +{ + osl::Condition &m_aCondition; +protected: + oslThreadIdentifier m_id; + + void SAL_CALL run( ) + { + ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ); + ::osl::StreamSocket ssStreamConnection; + + //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); + while ( schedule( ) == sal_True ) + { + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + if ( sal_True != bOK1 ) + { + t_print("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ; + break; + } + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + if ( sal_True != bOK2 ) + { + t_print("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ; + break; + } + + asAcceptorSocket.enableNonBlockingMode( sal_False ); + m_aCondition.set(); + + oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); + if (eResult != osl_Socket_Ok ) + { + t_print("ServerSocketThread: acceptConnection failed! \n"); + break; + } + sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 ); + sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 ); + pReadBuffer[nReadNumber1 + nReadNumber2] = '\0'; + //t_print("# read buffer content: %s\n", pReadBuffer ); + break; + } + ssStreamConnection.close(); + asAcceptorSocket.close(); + + } + + void SAL_CALL onTerminated( ) + { + //t_print("# normally terminate this server thread %d!\n", m_id ); + } + +public: + // public to check if data transmition is OK + sal_Char pReadBuffer[30]; + ServerSocketThread( osl::Condition &_aCond ):m_aCondition(_aCond) + { + m_aCondition.reset(); + t_print("#init ServerSocketThread\n"); + m_id = getIdentifier( ); + //t_print("# successfully creat this ServerSocketThread %d!\n", m_id ); + } + + ~ServerSocketThread( ) + { + if ( isRunning( ) ) + t_print("# error: ServerSocketThread has not terminated.\n" ); + } +}; + +/** Client Socket Thread, served as a temp little client to communicate with server. + */ +class ClientSocketThread : public Thread +{ +protected: + osl::Condition &m_aCondition; + oslThreadIdentifier m_id; + ::osl::SocketAddr m_saTargetSocketAddr; + ::osl::ConnectorSocket m_csConnectorSocket; + + void SAL_CALL run( ) + { + TimeValue *pTimeout; + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 5; + pTimeout->Nanosec = 0; + + /// if the thread should terminate, schedule return false + //while ( schedule( ) == sal_True ) + //{ + if ( osl::Condition::result_ok != m_aCondition.wait( pTimeout ) ) + { + free( pTimeout ); + return; + } + + if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout )) + { + m_csConnectorSocket.send( pTestString1, 11 ); // "test socket" + m_csConnectorSocket.send( pTestString2, 10); + } + else + t_print("# ClientSocketThread: connect failed! \n"); + // terminate(); + //} + m_csConnectorSocket.close(); + free( pTimeout ); + } + + void SAL_CALL onTerminated( ) + { + //t_print("# normally terminate this thread %d!\n", m_id ); + } + +public: + ClientSocketThread( osl::Condition &_aCond ): + m_aCondition(_aCond), + m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ), + m_csConnectorSocket( ) + { + m_id = getIdentifier( ); + //t_print("# successfully creat this client thread %d!\n", m_id ); + } + + ~ClientSocketThread( ) + { + if ( isRunning( ) ) + t_print("# error: client thread not terminated.\n" ); + } + +}; + +// ----------------------------------------------------------------------------- +// Helper functions, to create buffers, check buffers +class ValueCheckProvider +{ + bool m_bFoundFailure; + char *m_pBuffer; + sal_Int32 m_nBufferSize; + +public: + ValueCheckProvider() + :m_bFoundFailure(false), + m_pBuffer(NULL), + m_nBufferSize(0) + { + } + + bool isFailure() {return m_bFoundFailure;} + + const char* getBuffer() {return m_pBuffer;} + char* getWriteBuffer() {return m_pBuffer;} + + sal_Int32 getBufferSize() {return m_nBufferSize;} + + bool checkValues(sal_Int32 _nLength, int _nValue) + { + m_bFoundFailure = false; + for(sal_Int32 i=0;i<_nLength;i++) + { + if (m_pBuffer[i] != _nValue) + { + m_bFoundFailure = true; + } + } + return m_bFoundFailure; + } + + void createBuffer(sal_Int32 _nLength, int _nValue) + { + m_nBufferSize = _nLength; + m_pBuffer = (char*) malloc(m_nBufferSize); + if (m_pBuffer) + { + memset(m_pBuffer, _nValue, m_nBufferSize); + } + } + + void freeBuffer() + { + if (m_pBuffer) free(m_pBuffer); + } + +}; + +// ----------------------------------------------------------------------------- +/** Client Socket Thread, served as a temp little client to communicate with server. + */ + +class ReadSocketThread : public Thread +{ + ValueCheckProvider m_aValues; + int m_nValue; + osl::Condition &m_aCondition; + +protected: + oslThreadIdentifier m_id; + + void SAL_CALL run( ) + { + ::osl::SocketAddr m_aTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 ); + ::osl::ConnectorSocket m_aConnectorSocket; + + if (! m_aTargetSocketAddr.is()) + { + t_print("# SocketAddr was NOT created successfully!\n"); + } + else + { + t_print("start ReadSocketThread\n"); + + TimeValue *pTimeout; + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 5; + pTimeout->Nanosec = 0; + + m_aCondition.wait(); + + t_print("connect()\n"); + + oslSocketResult eResult = m_aConnectorSocket.connect( m_aTargetSocketAddr, pTimeout ); + if ( osl_Socket_Ok == eResult ) + { + sal_Int32 nReadCount = m_aConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() ); + m_aValues.checkValues(nReadCount, m_nValue); + } + else + { + t_print("# ReadSocketThread: connect failed! \n"); + printSocketResult(eResult); + } + + //remove this line for deadlock on solaris( margritte.germany ) + m_aConnectorSocket.close(); + free( pTimeout ); + } + } + + void SAL_CALL onTerminated( ) + { + //t_print("# normally terminate this thread %d!\n", m_id ); + } + +public: + sal_Int32 getCount() {return m_aValues.getBufferSize();} + bool isOk() {return m_aValues.isFailure() == true ? false : true;} + + ReadSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond ) + : m_nValue( _nValue ), + m_aCondition(_aCond) + { + t_print("#init ReadSocketThread\n"); + m_id = getIdentifier( ); + + //t_print("# successfully creat this client thread %d!\n", m_id ); + m_aValues.createBuffer(_nBufferSize, 0); + } + + ~ReadSocketThread( ) + { + if ( isRunning( ) ) + t_print("# error: client thread not terminated.\n" ); + m_aValues.freeBuffer(); + } + +}; + +/** Server Socket Thread, write a file which is large + */ +class WriteSocketThread : public Thread +{ + ValueCheckProvider m_aValues; + osl::Condition &m_aCondition; + +protected: + oslThreadIdentifier m_id; + + void SAL_CALL run( ) + { + t_print("start WriteSocketThread\n"); + ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 ); + if (! saLocalSocketAddr.is()) + { + t_print("LocalSocketAddr was NOT created successfully!\n"); + } + + ::osl::StreamSocket ssStreamConnection; + + //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + + /// if the thread should terminate, schedule return false + // while ( schedule( ) == sal_True ) + // { + t_print("bind()\n"); + sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); + if ( sal_True != bOK1 ) + { + t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ; + } + else + { + t_print("listen()\n"); + sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); + if ( sal_True != bOK2 ) + { + t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ; + } + else + { + + // blocking mode, if read/recv failed, block until success + asAcceptorSocket.enableNonBlockingMode( sal_False); + t_print("acceptConnection()\n"); + m_aCondition.set(); + + oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); + if (eResult != osl_Socket_Ok ) + { + t_print("WriteSocketThread: acceptConnection failed! \n"); + } + else + { + +// LLA: removed, due to the fact, this is to error prone +// LLA: char * pSrcRoot = getenv("SOURCE_ROOT"); +// LLA: // LLA: This is absolute wrong! +// LLA: // strcat( pSrcRoot, "/sal/inc/osl/file.hxx"); +// LLA: rtl::OString sSrcRoot(pSrcRoot); +// LLA: sSrcRoot += "/sal/inc/osl/file.hxx"; +// LLA: +// LLA: ::rtl::OUString sFilePath = ::rtl::OUString::createFromAscii( sSrcRoot.getStr() ); +// LLA: #ifdef WNT +// LLA: while (sFilePath.lastIndexOf('/') != -1) +// LLA: sFilePath = sFilePath.replace('/',(sal_Unicode)'\\'); +// LLA: #endif +// LLA: FILE *stream; +// LLA: sal_uInt64 nCount_read; +// LLA: sal_Char buffer_read[FILE_READ]; +// LLA: +// LLA: if( (stream = fopen( oustring2char( sFilePath ), "r+t" )) != NULL ) +// LLA: { +// LLA: /* Attempt to read in 25 characters */ +// LLA: nCount_read = fread( buffer_read, sizeof( char ), FILE_READ, stream ); +// LLA: fclose( stream ); +// LLA: } +// LLA: else +// LLA: t_print("# File $SRC_ROOT/sal/inc/osl/file.hxx could not be opened\n" ); + + t_print("write()\n"); + + ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() ); + t_print("done written.\n"); + } + } + } + ssStreamConnection.close(); + asAcceptorSocket.close(); + } + + void SAL_CALL onTerminated( ) + { + //t_print("# normally terminate this server thread %d!\n", m_id ); + } + +public: + // public to check if data transmition is OK + WriteSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond ) + : m_aCondition(_aCond) + { + m_aCondition.reset(); + + t_print("#init WriteSocketThread\n"); + m_id = getIdentifier( ); + //t_print("# successfully creat this server thread %d!\n", m_id ); + + m_aValues.createBuffer(_nBufferSize, _nValue); + } + + ~WriteSocketThread( ) + { + if ( isRunning( ) ) + t_print("# error: server thread not terminated.\n" ); + m_aValues.freeBuffer(); + } +}; + +// ----------------------------------------------------------------------------- + +namespace osl_StreamSocket +{ + + /** testing the methods: + inline StreamSocket(oslAddrFamily Family = osl_Socket_FamilyInet, + oslProtocol Protocol = osl_Socket_ProtocolIp, + oslSocketType Type = osl_Socket_TypeStream); + + inline StreamSocket( const StreamSocket & ); + + inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire ); + + inline StreamSocket( oslSocket Socket ); + */ + + class ctors : public CppUnit::TestFixture + { + public: + oslSocket sHandle; + // initialization + void setUp( ) + { + sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); + } + + void tearDown( ) + { + sHandle = NULL; + } + + + void ctors_none() + { + /// Socket constructor. + ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the stream socket was created successfully.", + osl_Socket_TypeStream == ssSocket.getType( ) ); + } + + void ctors_acquire() + { + /// Socket constructor. + ::osl::StreamSocket ssSocket( sHandle ); + + CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully", + osl_Socket_TypeStream == ssSocket.getType( ) ); + } + + void ctors_no_acquire() + { + /// Socket constructor. + ::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE ); + + CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully", + osl_Socket_TypeStream == ssSocket.getType( ) ); + } + + void ctors_copy_ctor() + { + /// Socket constructor. + ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + /// Socket copy constructor. + ::osl::StreamSocket copySocket( ssSocket ); + + CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor", + osl_Socket_TypeStream == copySocket.getType( ) ); + } + + CPPUNIT_TEST_SUITE( ctors ); + CPPUNIT_TEST( ctors_none ); + CPPUNIT_TEST( ctors_acquire ); + CPPUNIT_TEST( ctors_no_acquire ); + CPPUNIT_TEST( ctors_copy_ctor ); + CPPUNIT_TEST_SUITE_END(); + + }; // class ctors + + class send_recv: public CppUnit::TestFixture + { + public: + // initialization + void setUp( ) + { + } + + void tearDown( ) + { + + } + + void send_recv1() + { + osl::Condition aCondition; + //client sent two strings, and server received, check the order and value + ServerSocketThread myServerThread( aCondition ); + ClientSocketThread myClientThread( aCondition ); + myServerThread.create( ); + myClientThread.create( ); + + //wait until the thread terminate + myClientThread.join( ); + myServerThread.join( ); + sal_Char myStr[30] = ""; + strcat( myStr, pTestString1 ); + strcat( myStr, pTestString2 ); + sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr ); + CPPUNIT_ASSERT_MESSAGE(" test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.", + nRes == 0 ); + } + + // error when recv + void send_recv2() + { + ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ); + ::osl::StreamSocket ssStreamConnection; + sal_Char pReadBuffer[30] = ""; + + osl::Condition aCondition; + aCondition.reset(); + ClientSocketThread myClientThread( aCondition ); + myClientThread.create( ); + + asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); + + asAcceptorSocket.bind( saLocalSocketAddr ); + asAcceptorSocket.listen( 1 ); + asAcceptorSocket.enableNonBlockingMode( sal_True ); + aCondition.set(); + + asAcceptorSocket.acceptConnection( ssStreamConnection ); + sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 ); + + myClientThread.join( ) ; + ssStreamConnection.close(); + asAcceptorSocket.close(); + CPPUNIT_ASSERT_MESSAGE(" test for send/recv, recv error!", nReadNumber == -1 ); + } + + // LLA: This is a helper function, which create 2 threads, a server and a client. + // the server writes the buffersize to the client. + + void write_read(sal_Int32 _nBufferSize, int _nValue) + { + //client sent two strings, and server received, check the order and value + osl::Condition aCondition; + WriteSocketThread myServerThread(_nBufferSize, _nValue, aCondition); + ReadSocketThread myClientThread(_nBufferSize, _nValue, aCondition); + myServerThread.create( ); +// thread_sleep( 1 ); + myClientThread.create( ); + + //wait until the thread terminate + myClientThread.join( ); + myServerThread.join( ); + + //Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500 + // Proteon PRONET = 2046), so here test read 4000 bytes + sal_Int32 nLength = myClientThread.getCount(); + bool bIsOk = myClientThread.isOk(); // check if the values are right. + + t_print("Length:=%d\n", nLength); + t_print(" bIsOk:=%d\n", bIsOk); + + CPPUNIT_ASSERT_MESSAGE(" test for write/read values with two threads: send data from server, check readed data in client.", + nLength == _nBufferSize && bIsOk == true); + } + + // Tests with different values and sizes + void write_read_001() + { + write_read(50, 10); + } + void write_read_002() + { + write_read(1024, 20); + } + void write_read_003() + { + write_read(4000, 1); + } + void write_read_004() + { + write_read(8192, 3); + } + void write_read_005() + { + write_read(32768, 3); + } + + CPPUNIT_TEST_SUITE( send_recv ); + CPPUNIT_TEST( write_read_001 ); + CPPUNIT_TEST( write_read_002 ); + CPPUNIT_TEST( write_read_003 ); + CPPUNIT_TEST( write_read_004 ); + CPPUNIT_TEST( write_read_005 ); + CPPUNIT_TEST( send_recv1 ); + CPPUNIT_TEST( send_recv2 ); +// CPPUNIT_TEST( write_read ); + CPPUNIT_TEST_SUITE_END(); + }; // class send_recv + +// ----------------------------------------------------------------------------- + + class SendClientThread : public Thread + { + protected: + ::osl::SocketAddr m_saTargetSocketAddr; + ::osl::ConnectorSocket m_csConnectorSocket; + void SAL_CALL run( ) + { + TimeValue *pTimeout; + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 5; + pTimeout->Nanosec = 0; + + if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout )) + { + sal_Int32 nWrite1 = m_csConnectorSocket.write( pTestString1, 11 ); // "test socket" + + sal_Int32 nWrite2 = m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 ); + thread_sleep( 2 ); + m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 ); + t_print("nWrite1 is %d, nWrite2 is %d\n", nWrite1, nWrite2 ); + //thread_sleep( 1 ); + } + else + t_print("# SendClientThread: connect failed! \n"); + + m_csConnectorSocket.close(); + free( pTimeout ); + } + public: + SendClientThread( ): + m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ), + m_csConnectorSocket( ) + { + //t_print("# successfully creat this SendClientThread %d!\n", m_id ); + } + + ~SendClientThread( ) + { + if ( isRunning( ) ) + t_print("# error: SendClientThread has not terminated.\n" ); + } + + }; + + class shutdown: public CppUnit::TestFixture + { + public: + // initialization + void setUp( ) + { + } + + void tearDown( ) + { + + } + + // similar to close_002 + void shutdown_001() + { +#if defined(LINUX) + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") ); + myAcceptorThread.create(); + + thread_sleep( 1 ); + + //when accepting, shutdown the socket, the thread will not block for accepting + asSocket.shutdown(); + myAcceptorThread.join(); + + CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", + myAcceptorThread.isOK( ) == sal_True ); +#endif + } + + void shutdown_002() + { + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9); + asSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True ); + sal_Char pReadBuffer[40]; +// osl::Condition aCondition; + SendClientThread mySendThread; + mySendThread.create(); + + asSocket.enableNonBlockingMode( sal_False ); + ::osl::StreamSocket ssConnectionSocket; + oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket ); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok ); + + /* set socket option SO_LINGER 0, so close immediatly */ + linger aLingerSet; + sal_Int32 nBufferLen = sizeof( struct linger ); + aLingerSet.l_onoff = 0; + aLingerSet.l_linger = 0; + + ssConnectionSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen ); + thread_sleep( 1 ); + //sal_uInt32 nRecv1 = 0; + sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 ); + + //shutdown read after client the first send complete + ssConnectionSocket.shutdown( osl_Socket_DirRead ); + + sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 ); + sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 ); + t_print("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", nRead1, nRead2, nRead3 ); + mySendThread.join(); + + ssConnectionSocket.close(); + asSocket.close(); + + /* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0, + http://dbforums.com/arch/186/2002/12/586417 + While on Solaris, after shutdown(DirRead), all read will return 0 + */ +#ifdef LINUX + CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).", + nRead1 > 0 && nRead3 == 0 ); +#else + CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).", + nRead1 > 0 && nRead2 == 0 && nRead3 == 0 ); +#endif + + } + + void shutdown_003() + { + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9); + asSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True ); + sal_Char pReadBuffer[40]; + osl::Condition aCondition; + SendClientThread mySendThread; + mySendThread.create(); + + asSocket.enableNonBlockingMode( sal_False ); + ::osl::StreamSocket ssConnectionSocket; + oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket ); + CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok ); + + thread_sleep( 1 ); + //shutdown write after client the first send complete + ssConnectionSocket.shutdown( osl_Socket_DirWrite ); + + // recv should not shutdown + sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 ); + + sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 ); + // still can read + sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 ); + t_print("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", nRead1, nWrite, nRead3 ); + mySendThread.join(); + ssConnectionSocket.close(); + asSocket.close(); + + CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not send(write).", + nRead1 > 0 && nWrite == 0 && nRead3 > 0); + + } + + CPPUNIT_TEST_SUITE( shutdown ); + CPPUNIT_TEST( shutdown_001 ); + CPPUNIT_TEST( shutdown_002 ); + CPPUNIT_TEST( shutdown_003 ); + CPPUNIT_TEST_SUITE_END(); + }; // class shutdown + + class isExceptionPending: public CppUnit::TestFixture + { + public: + void isExPending_001() + { + ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + TimeValue *pTimeout; + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + sal_Bool bOk = asSocket.isExceptionPending( pTimeout ); + free( pTimeout ); + + CPPUNIT_ASSERT_MESSAGE( "test for isExceptionPending.", + bOk == sal_False ); + } + + /**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/ + + + CPPUNIT_TEST_SUITE( isExceptionPending ); + CPPUNIT_TEST( isExPending_001 ); + CPPUNIT_TEST_SUITE_END(); + }; // class isExceptionPending + +// ----------------------------------------------------------------------------- +/** Server Socket Thread, write a file which is large + */ +// LLA: class WriteSocketThread : public Thread +// LLA: { +// LLA: ValueCheckProvider m_aValues; +// LLA: +// LLA: protected: +// LLA: oslThreadIdentifier m_id; +// LLA: +// LLA: void SAL_CALL run( ) +// LLA: { +// LLA: ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); +// LLA: ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("10.16.66.252"), 8888 ); +// LLA: ::osl::StreamSocket ssStreamConnection; +// LLA: +// LLA: //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket +// LLA: asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); +// LLA: +// LLA: /// if the thread should terminate, schedule return false +// LLA: while ( schedule( ) == sal_True ) +// LLA: { +// LLA: sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); +// LLA: if ( sal_True != bOK1 ) +// LLA: { +// LLA: t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ; +// LLA: break; +// LLA: } +// LLA: sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); +// LLA: if ( sal_True != bOK2 ) +// LLA: { +// LLA: t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ; +// LLA: break; +// LLA: } +// LLA: // blocking mode, if read/recv failed, block until success +// LLA: asAcceptorSocket.enableNonBlockingMode( sal_False); +// LLA: +// LLA: oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); +// LLA: if (eResult != osl_Socket_Ok ) +// LLA: { +// LLA: t_print("WriteSocketThread: acceptConnection failed! \n"); +// LLA: break; +// LLA: } +// LLA: +// LLA: +// LLA: sal_Int32 nReadNumber1 = ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() ); +// LLA: break; +// LLA: } +// LLA: ssStreamConnection.close(); +// LLA: asAcceptorSocket.close(); +// LLA: } +// LLA: } +// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- +/** Client Socket Thread, served as a temp little client to communicate with server. + */ + +#define IP_PORT_TEST 8900 + + class ReadSocket2Thread : public Thread + { + osl::Condition &m_aCondition; + char* m_pBuffer; + sal_Int32 m_nBufferSize; + sal_Int32 m_nReadCount; + rtl::OString m_sAddr; + + bool m_bOk; + + void setFailed() + { + m_bOk = false; + } + + protected: + oslThreadIdentifier m_id; + + void read() + { + if (m_sAddr.getLength() == 0) + { + setFailed(); + return; + } + + // 10.16.66.252 + ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(m_sAddr.getStr()), IP_PORT_TEST ); + ::osl::ConnectorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + + aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True; + + m_aCondition.wait(); + t_print("wait done\n"); + + TimeValue *pTimeout; + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 20; + pTimeout->Nanosec = 0; + + + // blocking mode, if read/recv failed, block until success + t_print("enableNonBlockingMode(false)\n"); + aSocket.enableNonBlockingMode( sal_False ); + + + t_print("connect()\n"); + oslSocketResult eResult = aSocket.connect( aSocketAddr, pTimeout ); + if ( osl_Socket_Ok == eResult) + { + if (m_pBuffer) + { + t_print("read()\n"); + m_nReadCount = aSocket.read( m_pBuffer, m_nBufferSize ); + t_print("%d bytes recived.\n", m_nReadCount); + } + } + else + { + t_print("# ReadSocket2Thread: connect failed! \n"); + printSocketResult(eResult); + setFailed(); + } + + //remove this line for deadlock on solaris( margritte.germany ) + aSocket.close(); + free( pTimeout ); + } + + void SAL_CALL run( ) + { + read(); + } + + void SAL_CALL onTerminated( ) + { + //t_print("# normally terminate this thread %d!\n", m_id ); + } + + public: + sal_Int32 getCount() {return m_nReadCount;} + bool isOk() {return m_nReadCount == 0 ? false : true;} + bool getFailed() {return m_bOk == false ? true : false;} + + ReadSocket2Thread(osl::Condition &_aCondition) + :m_aCondition(_aCondition), + m_nReadCount(0), + m_bOk( true ) + { + m_aCondition.reset(); + m_pBuffer = (char*) malloc(1024); + if (m_pBuffer) + { + m_nBufferSize = 1024; + } + + m_id = getIdentifier( ); + //t_print("# successfully creat this client thread %d!\n", m_id ); + } + + void setAddr(rtl::OString const& _sAddr) + { + m_sAddr = _sAddr; + } + + ~ReadSocket2Thread( ) + { + if ( isRunning( ) ) + t_print("# error: client thread not terminated.\n" ); + free(m_pBuffer); + } + + }; + + // ----------------------------------------------------------------------------- + + class justtest : public CppUnit::TestFixture + { + void send_Acceptor(rtl::OString const& _sAddr, osl::Condition &) + { + ::osl::AcceptorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr aSocketAddr; + + if (! aSocketAddr.setPort(IP_PORT_TEST)) + { + t_print("# cant set port\n" ); + } + + if (! aSocketAddr.setHostname(rtl::OUString::createFromAscii(_sAddr.getStr()))) + { + t_print("# cant set hostname/ip\n" ); + } + + rtl::OUString aHostname = aSocketAddr.getHostname(); + aSocketAddr.getPort(); + + + //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket + aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); + + /// if the thread should terminate, schedule return false + // while ( schedule( ) == sal_True ) + // { + if (! aSocket.bind( aSocketAddr )) + { + t_print("# can't bind.\n" ); + } + if (! aSocket.listen( )) + { + t_print("# can't listen. \n" ); + } + + // blocking mode, if read/recv failed, block until success + aSocket.enableNonBlockingMode( sal_False); + ::osl::StreamSocket ssStreamConnection; + + oslSocketResult eResult = aSocket.acceptConnection( ssStreamConnection ); + if (eResult != osl_Socket_Ok ) + { + t_print("WriteSocketThread: acceptConnection failed! \n"); + // break; + } + char const * pBuffer = "Test String\n"; + sal_Int32 nBufferSize = strlen(pBuffer); + ssStreamConnection.write( pBuffer, nBufferSize ); + // break; + // } + + // ssStreamConnection.close(); + aSocket.close(); + } + + // ----------------------------------------------------------------------------- + + void send_Connector(rtl::OString const& _sAddr, osl::Condition &/*_aCondition*/ ) + { + ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST ); + + if (! aSocketAddr.is()) + { + t_print("is failed.\n"); + return; + } + + //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket + aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True; + + oslSocketResult aResult = aSocket.connect( aSocketAddr ); + if ( aResult != osl_Socket_Ok ) + { + t_print("# send_Connector: connect failed. \n" ); + } + else + { + // blocking mode, if read/recv failed, block until success +// aSocket.enableNonBlockingMode( sal_False ); + +// _aCondition.set(); + + ::osl::StreamSocket ssStreamConnection(aSocket); + + char const * pBuffer = "GET / HTTP/1.0\015\012\015\012"; + sal_Int32 nBufferSize = strlen(pBuffer); + ssStreamConnection.write( pBuffer, nBufferSize ); + + char *pBufferPeek = (char*) malloc(1024); + sal_Int32 nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek); + free(pBufferPeek); + + char *pBuffer2 = (char*) malloc(nReadNumber + 1); + sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber ); + pBuffer2[nReadNumberReal] = '\0'; + + t_print("received: %s\n", pBuffer2); + + // char * pBuffer3 = "quit\n"; + // nBufferSize = strlen(pBuffer3); + // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize ); + + rtl::OUString suError = ssStreamConnection.getErrorAsString(); + free(pBuffer2); + // ssStreamConnection.close(); + + // ssStreamConnection.close(); + } + aSocket.shutdown(osl_Socket_DirReadWrite); + aSocket.close(); + } + + + public: +// LLA: orig void send_recv() +// LLA: orig { +// LLA: orig if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True ) +// LLA: orig t_print("margritte is alive ! \n"); +// LLA: orig if ( ifAvailable(rtl::OUString::createFromAscii("10.16.66.252")) == sal_False ) +// LLA: orig { +// LLA: orig t_print("ip 10.16.66.252 is not alive! \n"); +// LLA: orig return; +// LLA: orig } +// LLA: orig ReadSocket2Thread myReadThread; +// LLA: orig myReadThread.create(); +// LLA: orig +// LLA: orig thread_sleep( 2 ); +// LLA: orig // send_Acceptor(); +// LLA: orig send_Connector(); +// LLA: orig +// LLA: orig myReadThread.join(); +// LLA: orig +// LLA: orig // statistics +// LLA: orig sal_uInt32 nLength = myReadThread.getCount(); +// LLA: orig bool bIsOk = myReadThread.isOk(); // check if the values are right. +// LLA: orig +// LLA: orig t_print("Length:=%d\n", nLength); +// LLA: orig t_print(" bIsOk:=%d\n", bIsOk); +// LLA: orig } + + // ----------------------------------------------------------------------------- + + // LLA: send_Connector_2_margritte works, it send strings to echo server on margritte + // but can not receive anything + + void send_Connector_2_margritte(rtl::OString const& _sAddr) + { + ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST ); + + //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket + aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True; + + oslSocketResult aResult = aSocket.connect( aSocketAddr ); + if ( aResult != osl_Socket_Ok ) + { + t_print("# connect failed. \n" ); + } + else + { + // blocking mode, if read/recv failed, block until success + aSocket.enableNonBlockingMode( sal_False ); + + ::osl::StreamSocket ssStreamConnection(aSocket); + + char const * pBuffer = "Test String\n"; + sal_Int32 nBufferSize = strlen(pBuffer); + sal_Int32 nWriteNumber = ssStreamConnection.write( pBuffer, nBufferSize ); + + // char * pBuffer2 = " "; + // sal_Int32 nReadNumber = ssStreamConnection.read( pBuffer2, strlen(pBuffer2) ); + + char const * pBuffer3 = "quit\n"; + nBufferSize = strlen(pBuffer3); + nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize ); + + ssStreamConnection.close(); + } + aSocket.close(); + } + + void send_recv_2_margritte() + { + rtl::OString sAddr; + sAddr = "margritte.germany.sun.com"; + if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True ) + { + t_print("found %s!\n", sAddr.getStr()); + } + send_Connector_2_margritte(sAddr); + } + + // ----------------------------------------------------------------------------- + + void send_recv() + { + rtl::OString sAddr; + // if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True ) + // { + // t_print("margritte is alive ! \n"); + // sAddr = "margritte.germany"; + // } + + sAddr = "margritte.germany.sun.com"; + if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True ) + { + t_print("found %s!\n", sAddr.getStr()); + } +// else +// { +// if ( ifAvailable(rtl::OUString::createFromAscii("192.168.7.2")) == sal_True ) +// { +// sAddr = "192.168.7.2"; +// t_print("moon found ! \n"); +// } +// else +// { +// if ( ifAvailable(rtl::OUString::createFromAscii("moon.linux.bogus")) == sal_True ) +// { +// sAddr = "moon.linux.bogus"; +// t_print("moon found ! \n"); +// } +// else +// { +// if ( ifAvailable(rtl::OUString::createFromAscii("moon")) == sal_True ) +// { +// sAddr = "moon"; +// t_print("moon found ! \n"); +// } +// } +// } +// } + + // if ( ifAvailable(rtl::OUString::createFromAscii("10.16.64.196")) == sal_False ) + // { + // t_print("ip 10.16.64.196 is not alive! \n"); + // return; + // } + + osl::Condition aCondition; + ReadSocket2Thread myReadThread(aCondition); + myReadThread.setAddr(sAddr); +// myReadThread.create(); + + thread_sleep( 2 ); + if (! myReadThread.getFailed()) + { + // send_Acceptor(sAddr, aCondition); + send_Connector(sAddr, aCondition); + + thread_sleep( 2 ); + if (myReadThread.isRunning()) + { + myReadThread.join(); + } + // termAndJoinThread(&myReadThread); + + // statistics + sal_uInt32 nLength = myReadThread.getCount(); + bool bIsOk = myReadThread.isOk(); // check if the values are right. + + t_print("Length:=%d\n", nLength); + t_print(" bIsOk:=%d\n", bIsOk); + } + else + { + t_print("ERROR: No echo Server on %s found.\n", sAddr.getStr()); + } + } + + + void getPage(rtl::OString const& _sAddr); + void test_getPage() + { + // rtl::OString sPage("lla-1.germany.sun.com"); + // getPage(sPage); + + rtl::OString sPage("lla-1"); + getPage(sPage); + } + + CPPUNIT_TEST_SUITE( justtest ); + CPPUNIT_TEST( send_recv ); + CPPUNIT_TEST( test_getPage ); + CPPUNIT_TEST_SUITE_END(); + }; // class isExceptionPending + + + void justtest::getPage(rtl::OString const& _sAddr) + { + rtl::OUString suAddr = rtl::OUString::createFromAscii(_sAddr.getStr()); + ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr aSocketAddr( suAddr, 80 ); + + { + // some checks + aSocketAddr.getPort(); + oslSocketResult aResult; + rtl::ByteSequence aSeq = aSocketAddr.getAddr(&aResult); + if (aResult != osl_Socket_Ok) + { + t_print("problem with getAddr: "); + printSocketResult(aResult); + } + + rtl::OUString sStr = aSocketAddr.getHostname(&aResult); + if (aResult != osl_Socket_Ok) + { + t_print("problem with hostname: "); + printSocketResult(aResult); + } + } + + oslSocketResult aResult; + + // SocketAddr::resolveHostname(suAddr, aSocketAddr); + // if (! aSocketAddr.is()) + // { + // t_print("Can't resolve Hostname.\n"); + // return; + // } + // rtl::OUString sStr = aSocketAddr.getHostname(&aResult); + // if (aResult != osl_Socket_Ok) + // { + // t_print("problem with hostname: "); + // printSocketResult(aResult); + // + // } + + if (! aSocketAddr.is()) + { + t_print("SocketAddr::is() failed.\n"); + return; + } + + //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket + aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True; + + aResult = aSocket.connect( aSocketAddr ); + if ( aResult != osl_Socket_Ok ) + { + t_print("# send_Connector: connect failed. \n" ); + } + else + { + // blocking mode, if read/recv failed, block until success +// aSocket.enableNonBlockingMode( sal_False ); + +// _aCondition.set(); + + ::osl::StreamSocket ssStreamConnection(aSocket); + + char const * pBuffer = "GET / HTTP/1.0\015\012\015\012"; + sal_Int32 nBufferSize = strlen(pBuffer); + ssStreamConnection.write( pBuffer, nBufferSize ); + + + char *pBufferPeek = (char*) malloc(1024); + sal_Int32 nReadNumber = 1; + while ( nReadNumber != 0) + { + nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek); + if (nReadNumber > 0) + { + char *pBuffer2 = (char*) malloc(nReadNumber + 1); + sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber ); + pBuffer2[nReadNumberReal] = '\0'; + t_print("%s", pBuffer2); + free(pBuffer2); + } + } + free(pBufferPeek); + + // char * pBuffer3 = "quit\n"; + // nBufferSize = strlen(pBuffer3); + // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize ); + + rtl::OUString suError = ssStreamConnection.getErrorAsString(); + } + aSocket.shutdown(osl_Socket_DirReadWrite); + aSocket.close(); + } + +// ----------------------------------------------------------------------------- + + CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::ctors, "osl_StreamSocket"); + CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::send_recv, "osl_StreamSocket"); + CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::shutdown, "osl_StreamSocket"); + CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::isExceptionPending, "osl_StreamSocket"); + + CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::justtest, "osl_StreamSocket"); + +} // namespace osl_StreamSocket + +// ----------------------------------------------------------------------------- + +// this macro creates an empty function, which will called by the RegisterAllFunctions() +// to let the user the possibility to also register some functions by hand. +NOADDITIONAL; diff --git a/sal/qa/osl/socket/osl_StreamSocket.xsce b/sal/qa/osl/socket/osl_StreamSocket.xsce new file mode 100644 index 000000000000..9c9513969d4a --- /dev/null +++ b/sal/qa/osl/socket/osl_StreamSocket.xsce @@ -0,0 +1,4 @@ +osl_StreamSocket.send_recv.send_recv2 +osl_StreamSocket.send_recv.write_read_001 wntmsci unxsols +osl_StreamSocket.justtest.test_getPage wntmsci + diff --git a/sal/qa/osl/socket/sockethelper.cxx b/sal/qa/osl/socket/sockethelper.cxx new file mode 100644 index 000000000000..4568f042dc15 --- /dev/null +++ b/sal/qa/osl/socket/sockethelper.cxx @@ -0,0 +1,401 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sal.hxx" +#include "sockethelper.hxx" +#include <testshl/simpleheader.hxx> + +//------------------------------------------------------------------------ +// Ip version definition +//------------------------------------------------------------------------ +#define IP_VER 4 /// currently only IPv4 is considered. + +//------------------------------------------------------------------------ +// helper functions +//------------------------------------------------------------------------ + +/** compare two OUString. +*/ +sal_Bool compareUString( const ::rtl::OUString & ustr1, const ::rtl::OUString & ustr2 ) +{ + sal_Bool bOk = ustr1.equalsIgnoreAsciiCase( ustr2 ); + + return bOk; +} + +/** compare a OUString and an ASCII string. +*/ +sal_Bool compareUString( const ::rtl::OUString & ustr, const sal_Char *astr ) +{ + ::rtl::OUString ustr2 = rtl::OUString::createFromAscii( astr ); + sal_Bool bOk = ustr.equalsIgnoreAsciiCase( ustr2 ); + + return bOk; +} + +/** compare two socket address. +*/ +sal_Bool compareSocketAddr( const ::osl::SocketAddr & addr1 , const ::osl::SocketAddr & addr2 ) +{ + return ( ( sal_True == compareUString( addr1.getHostname( 0 ), addr2.getHostname( 0 ) ) ) && ( addr2.getPort( ) == addr2.getPort( ) ) ); +} + +/*char * oustring2char( const ::rtl::OUString & str ) +{ + rtl::OString aString; + aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ); + t_print("oustring2char %s\n", aString.getStr( ) ); + sal_Char * sStr = aString.getStr( ); + return (char *)sStr; +}*/ + +/** print a UNI_CODE String. And also print some comments of the string. +*/ +void printUString( const ::rtl::OUString & str, const char* msg) +{ + t_print("#%s #printUString_u# ", msg ); + rtl::OString aString; + aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ); + //char * sStr = aString.getStr( ); + t_print("%s\n", aString.getStr( ) ); +} + +/** get the local host name. + mindy: gethostbyname( "localhost" ), on Linux, it returns the hostname in /etc/hosts + domain name, + if no entry in /etc/hosts, it returns "localhost" + domain name +*/ +::rtl::OUString getHost( void ) +{ + struct hostent *hptr; + + hptr = gethostbyname( "localhost" ); + OSL_ENSURE( hptr != NULL, "#In getHostname function, error on gethostbyname()" ); + ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name ); + + return aUString; +} + +/** get the full host name of the current processor, such as "aegean.prc.sun.com" --mindyliu +*/ +::rtl::OUString getThisHostname( void ) +{ + ::rtl::OUString aUString; +#ifdef WNT + struct hostent *hptr; + hptr = gethostbyname( "localhost" ); + OSL_ENSURE( hptr != NULL, "#In getHostname function, error on gethostbyname()" ); + rtl::OString sHostname(hptr->h_name); + aUString = ::rtl::OStringToOUString(sHostname, RTL_TEXTENCODING_ASCII_US); +#else + char hostname[255]; + if (gethostname(hostname, 255) != 0) { + OSL_ENSURE( false, "#Error: gethostname failed." ); + } + + struct hostent *hptr; + //first search /ets/hosts, then search from dns + hptr = gethostbyname( hostname); + if ( hptr != NULL ) + { + strcpy( hostname, hptr->h_name ); + } + + t_print("hostname is %s \n", hostname ); + rtl::OString sHostname( hostname ); + aUString = ::rtl::OStringToOUString( sHostname, RTL_TEXTENCODING_ASCII_US ); + aUString.getLength(); +#endif + return aUString; +} + +/** get IP by name, search /etc/hosts first, then search from dns, fail return OUString("") +*/ +::rtl::OUString getIPbyName( rtl::OString const& str_name ) +{ + ::rtl::OUString aUString; + struct hostent *hptr; + //first search /ets/hosts, then search from dns + hptr = gethostbyname( str_name.getStr()); + if ( hptr != NULL ) + { + struct in_addr ** addrptr; + addrptr = (struct in_addr **) hptr->h_addr_list ; + //if there are more than one IPs on the same machine, we select one + for (; *addrptr; addrptr++) + { + t_print("#Local IP Address: %s\n", inet_ntoa(**addrptr)); + aUString = ::rtl::OUString::createFromAscii( (sal_Char *) (inet_ntoa(**addrptr)) ); + } + } + return aUString; +} + +/** get local ethernet IP +*/ +::rtl::OUString getLocalIP( ) +{ + char hostname[255]; + gethostname(hostname, 255); + + return getIPbyName( hostname ); +} + +/** construct error message +*/ +::rtl::OUString outputError( const ::rtl::OUString & returnVal, const ::rtl::OUString & rightVal, const sal_Char * msg ) +{ + ::rtl::OUString aUString; + if ( returnVal.equals( rightVal ) ) + return aUString; + aUString += ::rtl::OUString::createFromAscii(msg); + aUString += ::rtl::OUString::createFromAscii(": the returned value is '"); + aUString += returnVal; + aUString += ::rtl::OUString::createFromAscii("', but the value should be '"); + aUString += rightVal; + aUString += ::rtl::OUString::createFromAscii("'."); + return aUString; +} + +/** wait _nSec seconds. +*/ +void thread_sleep( sal_Int32 _nSec ) +{ + /// print statement in thread process must use fflush() to force display. + // printf("wait %d seconds. ", _nSec ); + // fflush(stdout); + +#ifdef WNT //Windows + Sleep( _nSec * 100 ); +#endif +#if ( defined UNX ) || ( defined OS2 ) //Unix + usleep(_nSec * 100000); +#endif + // t_print("# done\n" ); +} + +/** print Boolean value. +*/ +void printBool( sal_Bool bOk ) +{ + t_print("printBool " ); + ( sal_True == bOk ) ? t_print("YES!" ): t_print("NO!"); + t_print("\n"); +} + +/** print content of a ByteSequence. +*/ +void printByteSequence_IP( const ::rtl::ByteSequence & bsByteSeq, sal_Int32 nLen ) +{ + t_print("ByteSequence is: " ); + for ( int i = 0; i < nLen; i++ ){ + if ( bsByteSeq[i] < 0 ) + t_print("%d ", 256 + bsByteSeq[i] ); + else + t_print("%d ", bsByteSeq[i] ); + } + t_print(" .\n" ); +} + +/** convert an IP which is stored as a UString format to a ByteSequence array for later use. +*/ +::rtl::ByteSequence UStringIPToByteSequence( ::rtl::OUString aUStr ) +{ + + rtl::OString aString = ::rtl::OUStringToOString( aUStr, RTL_TEXTENCODING_ASCII_US ); + const sal_Char *pChar = aString.getStr( ) ; + sal_Char tmpBuffer[4]; + sal_Int32 nCharCounter = 0; + ::rtl::ByteSequence bsByteSequence( IP_VER ); + sal_Int32 nByteSeqCounter = 0; + + for ( int i = 0; i < aString.getLength( ) + 1 ; i++ ) + { + if ( ( *pChar != '.' ) && ( i !=aString.getLength( ) ) ) + tmpBuffer[nCharCounter++] = *pChar; + else + { + tmpBuffer[nCharCounter] = '\0'; + nCharCounter = 0; + bsByteSequence[nByteSeqCounter++] = static_cast<sal_Int8>(atoi( tmpBuffer )); + } + pChar++; + } + return bsByteSequence; +} + +/** print a socket result name. +*/ +void printSocketResult( oslSocketResult eResult ) +{ + t_print("printSocketResult: " ); + if (!eResult) + switch (eResult) + { + case osl_Socket_Ok: + t_print("client connected\n"); + break; + case osl_Socket_Error: + t_print("got an error ... exiting\r\n\r\n" ); + break; + case osl_Socket_TimedOut: + t_print("timeout\n"); + break; + case osl_Socket_Interrupted: + t_print("interrupted\n"); + break; + case osl_Socket_InProgress: + t_print("in progress\n"); + break; + default: + t_print("unknown result\n"); + break; + } +} + +/** if 4 parts of an IP addr are equal to specified values +*/ +sal_Bool ifIpv4is( const ::rtl::ByteSequence Ipaddr, sal_Int8 seq1, sal_Int8 seq2, sal_Int8 seq3, sal_Int8 seq4 ) +{ + if ( ( Ipaddr[0] == seq1 ) && ( Ipaddr[1] == seq2 ) && ( Ipaddr[2] == seq3 ) && ( Ipaddr[3] == seq4 ) ) + return sal_True; + return sal_False; +} + +/** if the IP or hostname is availble( alive ) +*/ +/*sal_Bool ifAvailable( const char * stringAddrOrHostName ) +{ + sal_Bool result; + int p[2]; + sal_Char buffer[2000]; + char stringhost[20]; + strcpy(stringhost, stringAddrOrHostName ); + + result = sal_False; + if (pipe (p) == 0) + { + pid_t pid; + int nStatus; + pid = fork(); + if (pid == 0) + { +#if ( defined LINUX ) + char *argv[] = + { + "/bin/ping", + "-c", "3", + "-W", "3", + stringhost, + NULL + }; +#endif +#if ( defined SOLARIS ) + char *argv[] = + { + "/usr/sbin/ping", + stringhost, + "3", + NULL + }; +#endif + close (p[0]); + dup2 (p[1], 1); + close (p[1]); +#if ( defined LINUX ) + execv ("/bin/ping", argv); +#endif +#if ( defined SOLARIS ) + execv ("/usr/sbin/ping", argv); +#endif + // arriving here means exec failed + _exit(-1); + } + else if (pid > 0) + { + sal_Int32 k = 0, n = 2000; + close (p[1]); + if ((k = read (p[0], buffer, n - 1)) > 0) + { + buffer[k] = 0; + if (buffer[k - 1] == '\n') + buffer[k - 1] = 0; +#if ( defined LINUX ) + char strOK[] = "bytes from"; +#endif +#if ( defined SOLARIS ) + char strOK[] = "is alive"; +#endif + if (strstr( buffer, strOK ) != NULL ) + result = sal_True; + t_print("buffer is %s\n", buffer ); + } + close (p[0]); + waitpid (pid, &nStatus, 0); + } + else + { + close (p[0]); + close (p[1]); + } + + } + return result; +}*/ + +sal_Bool ifAvailable( rtl::OUString const& strAddrOrHostName ) +{ + ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); + ::osl::SocketAddr aSocketAddr( strAddrOrHostName, 7 ); + + if (! aSocketAddr.is()) + { + aSocket.close(); + return sal_False; + } + + aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True; + + TimeValue *pTimeout; + pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); + pTimeout->Seconds = 3; + pTimeout->Nanosec = 0; + + oslSocketResult aResult = aSocket.connect( aSocketAddr, pTimeout ); + free( pTimeout ); + aSocket.close(); + if ( aResult != osl_Socket_Ok ) + { + t_print("Error: "); + printSocketResult(aResult); + t_print("\n"); + + return sal_False; + } + return sal_True; +} diff --git a/sal/qa/osl/socket/sockethelper.hxx b/sal/qa/osl/socket/sockethelper.hxx new file mode 100644 index 000000000000..414447cca003 --- /dev/null +++ b/sal/qa/osl/socket/sockethelper.hxx @@ -0,0 +1,165 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ + +#ifndef _SOCKETHELPER_HXX_ +#define _SOCKETHELPER_HXX_ + +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ +#include <sal/types.h> +#include <rtl/textenc.h> +#include <rtl/ustring.hxx> +#include <rtl/ustring.h> + +#ifndef _OSL_SOCLET_HXX_ +#include <osl/socket.hxx> +#endif +#include <osl/socket.h> + +#ifndef _OSL_THREAD_HXX +#include <osl/thread.hxx> +#endif + +#ifndef _OSL_FILE_HXX +#include <osl/file.hxx> +#endif + +#ifndef _OSL_MUTEX_HXX +#include <osl/mutex.hxx> +#endif +#include <osl/time.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +//------------------------------------------------------------------------ +// OS dependent declaration and includes +//------------------------------------------------------------------------ +#if ( defined UNX ) || ( defined OS2 ) //Unix + +#include <unistd.h> +#include <limits.h> +#include <string.h> +#include <math.h> +#include <errno.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/statfs.h> +#include <sys/statvfs.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <sys/wait.h> +#endif + +#ifndef _OSL_SOCKET_CONST_H_ + +#if ( defined WNT ) // Windows +#include <tools/prewin.h> +// #include <windows.h> +#include <winsock.h> +#include <string.h> +#include <tools/postwin.h> +#endif + +#endif + +//------------------------------------------------------------------------ +//------------------------------------------------------------------------ + +#ifdef __cplusplus +} +#endif + +/** compare two OUString. +*/ +sal_Bool compareUString( const ::rtl::OUString & ustr1, const ::rtl::OUString & ustr2 ); +/** compare a OUString and an ASCII string. +*/ +sal_Bool compareUString( const ::rtl::OUString & ustr, const sal_Char *astr ); +/** compare two socket address. +*/ +sal_Bool compareSocketAddr( const ::osl::SocketAddr & addr1 , const ::osl::SocketAddr & addr2 ); +//char * oustring2char( const ::rtl::OUString & str ); +/** print a UNI_CODE String. And also print some comments of the string. +*/ +void printUString( const ::rtl::OUString & str, const char * msg = "" ); +/** get the local host name. + mindy: gethostbyname( "localhost" ), on Linux, it returns the hostname in /etc/hosts + domain name, + if no entry in /etc/hosts, it returns "localhost" + domain name +*/ +::rtl::OUString getHost( void ); +/** get the full host name of the current processor, such as "aegean.prc.sun.com" --mindyliu +*/ +::rtl::OUString getThisHostname( void ); +/** get IP by name, search /etc/hosts first, then search from dns, fail return OUString("") +*/ +::rtl::OUString getIPbyName( rtl::OString const& str_name ); +/** get local ethernet IP +*/ +::rtl::OUString getLocalIP( ); +/** construct error message +*/ +::rtl::OUString outputError( const ::rtl::OUString & returnVal, const ::rtl::OUString & rightVal, const sal_Char * msg = ""); +void thread_sleep( sal_Int32 _nSec ); +/** print Boolean value. +*/ +void printBool( sal_Bool bOk ); +/** print content of a ByteSequence. +*/ +void printByteSequence_IP( const ::rtl::ByteSequence & bsByteSeq, sal_Int32 nLen ); +/** convert an IP which is stored as a UString format to a ByteSequence array for later use. +*/ +::rtl::ByteSequence UStringIPToByteSequence( ::rtl::OUString aUStr ); +/** print a socket result name. +*/ +void printSocketResult( oslSocketResult eResult ); +/** if 4 parts of an IP addr are equal to specified values +*/ +sal_Bool ifIpv4is( const ::rtl::ByteSequence Ipaddr, sal_Int8 seq1, sal_Int8 seq2, sal_Int8 seq3, sal_Int8 seq4 ); +/** if the IP or hostname is availble( alive ) +*/ +//sal_Bool ifAvailable( const char * stringAddrOrHostName ); +sal_Bool ifAvailable( rtl::OUString const& strAddrOrHostName ); +/* +class ClientSocketThread : public Thread +class ServerSocketThread : public Thread +class ValueCheckProvider +class ClientReadSocketThread : public Thread +class ServerWriteSocketThread : public Thread +class AcceptorThread : public Thread +class CloseSocketThread : public Thread + +*/ + +#endif |