187d2adbcSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 387d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 487d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file 587d2adbcSAndrew Rist * distributed with this work for additional information 687d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file 787d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the 887d2adbcSAndrew Rist * "License"); you may not use this file except in compliance 987d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 1187d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 1387d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing, 1487d2adbcSAndrew Rist * software distributed under the License is distributed on an 1587d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1687d2adbcSAndrew Rist * KIND, either express or implied. See the License for the 1787d2adbcSAndrew Rist * specific language governing permissions and limitations 1887d2adbcSAndrew Rist * under the License. 19cdf0e10cSrcweir * 2087d2adbcSAndrew Rist *************************************************************/ 2187d2adbcSAndrew Rist 2287d2adbcSAndrew 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 57*63d99982SDamjan Jovanovic #include "gtest/gtest.h" 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 { 92*63d99982SDamjan Jovanovic printf("# 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 106*63d99982SDamjan Jovanovic class ctors : public ::testing::Test 107cdf0e10cSrcweir { 108cdf0e10cSrcweir public: 109*63d99982SDamjan Jovanovic }; // class ctors 110*63d99982SDamjan Jovanovic 111*63d99982SDamjan Jovanovic TEST_F(ctors, ctors_001) 112cdf0e10cSrcweir { 113cdf0e10cSrcweir /// Socket constructor. 114cdf0e10cSrcweir ::osl::ConnectorSocket csSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 115cdf0e10cSrcweir 116*63d99982SDamjan Jovanovic ASSERT_TRUE( osl_Socket_TypeStream == csSocket.getType( ) ) 117*63d99982SDamjan Jovanovic << "test for ctors_001 constructor function: check if the connector socket was created successfully."; 118cdf0e10cSrcweir } 119cdf0e10cSrcweir 120cdf0e10cSrcweir /** testing the method: 121cdf0e10cSrcweir oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0); 122cdf0e10cSrcweir */ 123cdf0e10cSrcweir 124*63d99982SDamjan Jovanovic class connect : public ::testing::Test 125cdf0e10cSrcweir { 126cdf0e10cSrcweir public: 127cdf0e10cSrcweir TimeValue *pTimeout; 128cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 129cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 130cdf0e10cSrcweir 131cdf0e10cSrcweir 132cdf0e10cSrcweir // initialization 133*63d99982SDamjan Jovanovic void SetUp( ) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 136cdf0e10cSrcweir pTimeout->Seconds = 3; 137cdf0e10cSrcweir pTimeout->Nanosec = 0; 138cdf0e10cSrcweir // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir 141*63d99982SDamjan Jovanovic void TearDown( ) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir free( pTimeout ); 144cdf0e10cSrcweir // sHandle = NULL; 145cdf0e10cSrcweir asAcceptorSocket.close( ); 146cdf0e10cSrcweir csConnectorSocket.close( ); 147cdf0e10cSrcweir } 148*63d99982SDamjan Jovanovic }; // class connect 149cdf0e10cSrcweir 150*63d99982SDamjan Jovanovic TEST_F(connect, connect_001) 151cdf0e10cSrcweir { 152cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT2 ); 153cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT2 ); 154cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 155cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 156cdf0e10cSrcweir 157cdf0e10cSrcweir /// launch server socket 158cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 159cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 160*63d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed."; 161cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 162*63d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed."; 163cdf0e10cSrcweir 164cdf0e10cSrcweir //asAcceptorSocket.enableNonBlockingMode( sal_True ); 165cdf0e10cSrcweir //oslSocketResult eResultAccept = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 166*63d99982SDamjan Jovanovic //ASSERT_TRUE(osl_Socket_Ok == eResultAccept) << "accept failed."; 167cdf0e10cSrcweir /// launch client socket 168cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 169*63d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_Ok == eResult) << "connect failed."; 170cdf0e10cSrcweir 171cdf0e10cSrcweir /// get peer information 172cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected. 173cdf0e10cSrcweir 174*63d99982SDamjan Jovanovic ASSERT_TRUE( 175cdf0e10cSrcweir ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) && 176*63d99982SDamjan Jovanovic ( osl_Socket_Ok == eResult )) 177*63d99982SDamjan Jovanovic << "test for connect function: try to create a connection with remote host. and check the setup address."; 178cdf0e10cSrcweir } 179cdf0e10cSrcweir //non-blocking mode connect? 180*63d99982SDamjan Jovanovic TEST_F(connect, connect_002) 181cdf0e10cSrcweir { 182cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 ); 183cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 ); 184cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 185cdf0e10cSrcweir 186cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 187cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 188cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 189*63d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed."; 190cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 191*63d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed."; 192cdf0e10cSrcweir 193cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_True ); 194cdf0e10cSrcweir 195cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 196*63d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_InProgress == eResult || osl_Socket_Ok == eResult) << "connect failed."; 197cdf0e10cSrcweir 198cdf0e10cSrcweir /// get peer information 199cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr ); 200cdf0e10cSrcweir 201*63d99982SDamjan Jovanovic ASSERT_TRUE( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) 202*63d99982SDamjan Jovanovic << "test for connect function: try to create a connection with remote host. and check the setup address."; 203cdf0e10cSrcweir } 204cdf0e10cSrcweir // really an error or just delayed 20586e1cf34SPedro Giffuni // how to design scenarios that will return osl_Socket_Interrupted, osl_Socket_TimedOut 206*63d99982SDamjan Jovanovic TEST_F(connect, connect_003) 207cdf0e10cSrcweir { 208cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr1( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 ); 209cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr2( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT3 ); 210cdf0e10cSrcweir 211cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_False ); 212cdf0e10cSrcweir 213cdf0e10cSrcweir oslSocketResult eResult1 = csConnectorSocket.connect( saTargetSocketAddr1, pTimeout ); 214cdf0e10cSrcweir oslSocketResult eResult2 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout ); 215cdf0e10cSrcweir CloseSocketThread myCloseThread( csConnectorSocket ); 216cdf0e10cSrcweir oslSocketResult eResult3 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout ); 217cdf0e10cSrcweir myCloseThread.join(); 218*63d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_Error == eResult1 && 219*63d99982SDamjan Jovanovic osl_Socket_Error == eResult2 && osl_Socket_Error == eResult3) << "connect should failed."; 220cdf0e10cSrcweir 221cdf0e10cSrcweir } 222cdf0e10cSrcweir 223cdf0e10cSrcweir // really an error in non-blocking mode 224*63d99982SDamjan Jovanovic TEST_F(connect, connect_004) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT3 ); 227cdf0e10cSrcweir 228cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_True ); 229cdf0e10cSrcweir 230cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 231*63d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_Error == eResult) << "connect should failed."; 232cdf0e10cSrcweir } 233cdf0e10cSrcweir /** here need a case: immediate connection, say in non-blocking mode connect return osl_Socket_Ok 234cdf0e10cSrcweir */ 235cdf0e10cSrcweir 236cdf0e10cSrcweir } // namespace osl_ConnectorSocket 237cdf0e10cSrcweir 238*63d99982SDamjan Jovanovic int main(int argc, char **argv) 239*63d99982SDamjan Jovanovic { 240*63d99982SDamjan Jovanovic ::testing::InitGoogleTest(&argc, argv); 241*63d99982SDamjan Jovanovic return RUN_ALL_TESTS(); 242*63d99982SDamjan Jovanovic } 243