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