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