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 5763d99982SDamjan 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_FTP 21 66cdf0e10cSrcweir #define IP_PORT_TELNET 23 67cdf0e10cSrcweir #define IP_PORT_HTTP2 8080 68cdf0e10cSrcweir #define IP_PORT_INVAL 99999 69cdf0e10cSrcweir #define IP_PORT_POP3 110 70cdf0e10cSrcweir #define IP_PORT_NETBIOS 139 71cdf0e10cSrcweir #define IP_PORT_MYPORT 8881 72cdf0e10cSrcweir #define IP_PORT_MYPORT1 8882 73cdf0e10cSrcweir #define IP_PORT_MYPORT5 8886 74cdf0e10cSrcweir #define IP_PORT_MYPORT6 8887 75cdf0e10cSrcweir #define IP_PORT_MYPORT7 8895 76cdf0e10cSrcweir #define IP_PORT_MYPORT8 8896 77cdf0e10cSrcweir #define IP_PORT_MYPORT9 8897 78cdf0e10cSrcweir 79cdf0e10cSrcweir //------------------------------------------------------------------------ 80cdf0e10cSrcweir // helper functions 81cdf0e10cSrcweir //------------------------------------------------------------------------ 82cdf0e10cSrcweir 83cdf0e10cSrcweir // just used to test socket::close() when accepting 84cdf0e10cSrcweir class AcceptorThread : public Thread 85cdf0e10cSrcweir { 86cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 87cdf0e10cSrcweir ::rtl::OUString aHostIP; 88cdf0e10cSrcweir sal_Bool bOK; 89cdf0e10cSrcweir protected: 90cdf0e10cSrcweir void SAL_CALL run( ) 91cdf0e10cSrcweir { 92cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 ); 93cdf0e10cSrcweir ::osl::StreamSocket ssStreamConnection; 94cdf0e10cSrcweir 95cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); 96cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 97cdf0e10cSrcweir if ( sal_True != bOK1 ) 98cdf0e10cSrcweir { 9963d99982SDamjan Jovanovic printf("# AcceptorSocket bind address failed.\n" ) ; 100cdf0e10cSrcweir return; 101cdf0e10cSrcweir } 102cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 103cdf0e10cSrcweir if ( sal_True != bOK2 ) 104cdf0e10cSrcweir { 10563d99982SDamjan Jovanovic printf("# AcceptorSocket listen address failed.\n" ) ; 106cdf0e10cSrcweir return; 107cdf0e10cSrcweir } 108cdf0e10cSrcweir 109cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_False ); 110cdf0e10cSrcweir 111cdf0e10cSrcweir oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); 112cdf0e10cSrcweir if (eResult != osl_Socket_Ok ) 113cdf0e10cSrcweir { 114cdf0e10cSrcweir bOK = sal_True; 11563d99982SDamjan Jovanovic printf("AcceptorThread: acceptConnection failed! \n"); 116cdf0e10cSrcweir } 117cdf0e10cSrcweir } 118cdf0e10cSrcweir public: 119cdf0e10cSrcweir AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP ) 120cdf0e10cSrcweir : asAcceptorSocket( asSocket ), aHostIP( aBindIP ) 121cdf0e10cSrcweir { 122cdf0e10cSrcweir bOK = sal_False; 123cdf0e10cSrcweir } 124cdf0e10cSrcweir 125cdf0e10cSrcweir sal_Bool isOK() { return bOK; } 126cdf0e10cSrcweir 127cdf0e10cSrcweir ~AcceptorThread( ) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir if ( isRunning( ) ) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir asAcceptorSocket.shutdown(); 13263d99982SDamjan Jovanovic printf("# error: Acceptor thread not terminated.\n" ); 133cdf0e10cSrcweir } 134cdf0e10cSrcweir } 135cdf0e10cSrcweir }; 136cdf0e10cSrcweir 137cdf0e10cSrcweir namespace osl_Socket 138cdf0e10cSrcweir { 139cdf0e10cSrcweir 140cdf0e10cSrcweir /** testing the methods: 141cdf0e10cSrcweir inline Socket( ); 142cdf0e10cSrcweir inline Socket( const Socket & socket ); 143cdf0e10cSrcweir inline Socket( oslSocket socketHandle ); 144cdf0e10cSrcweir inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire ); 145cdf0e10cSrcweir */ 146cdf0e10cSrcweir 147cdf0e10cSrcweir /** test writer's comment: 148cdf0e10cSrcweir 149cdf0e10cSrcweir class Socket can not be initialized by its protected constructor, though the protected 150cdf0e10cSrcweir constructor is the most convenient way to create a new socket. 151cdf0e10cSrcweir it only allow the method of C function osl_createSocket like: 152cdf0e10cSrcweir ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, 153cdf0e10cSrcweir osl_Socket_ProtocolIp ) ); 154cdf0e10cSrcweir the use of C method lost some of the transparent of tester using C++ wrapper. 155cdf0e10cSrcweir */ 156cdf0e10cSrcweir 157cdf0e10cSrcweir 15863d99982SDamjan Jovanovic class ctors : public ::testing::Test 159cdf0e10cSrcweir { 160cdf0e10cSrcweir public: 161cdf0e10cSrcweir oslSocket sHandle; 162cdf0e10cSrcweir // initialization 16363d99982SDamjan Jovanovic void SetUp( ) 164cdf0e10cSrcweir { 165cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 166cdf0e10cSrcweir } 167cdf0e10cSrcweir 16863d99982SDamjan Jovanovic void TearDown( ) 169cdf0e10cSrcweir { 170cdf0e10cSrcweir sHandle = NULL; 171cdf0e10cSrcweir } 17263d99982SDamjan Jovanovic }; // class ctors 173cdf0e10cSrcweir 17463d99982SDamjan Jovanovic TEST_F(ctors, ctors_none) 175cdf0e10cSrcweir { 176cdf0e10cSrcweir /// Socket constructor. 177cdf0e10cSrcweir // ::osl::Socket sSocket(); 178cdf0e10cSrcweir 17963d99982SDamjan Jovanovic ASSERT_TRUE(1 == 1) << "test for ctors_none constructor function: check if the socket was created successfully, if no exception occurred"; 180cdf0e10cSrcweir } 181cdf0e10cSrcweir 18263d99982SDamjan Jovanovic TEST_F(ctors, ctors_acquire) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir /// Socket constructor. 185cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 186cdf0e10cSrcweir 18763d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << "test for ctors_acquire constructor function: check if the socket was created successfully"; 188cdf0e10cSrcweir } 189cdf0e10cSrcweir 19063d99982SDamjan Jovanovic TEST_F(ctors, ctors_no_acquire) 191cdf0e10cSrcweir { 192cdf0e10cSrcweir /// Socket constructor. 193cdf0e10cSrcweir ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE ); 194cdf0e10cSrcweir 19563d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << " test for ctors_no_acquire constructor function: check if the socket was created successfully"; 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 19863d99982SDamjan Jovanovic TEST_F(ctors, ctors_copy_ctor) 199cdf0e10cSrcweir { 200cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 201cdf0e10cSrcweir /// Socket copy constructor. 202cdf0e10cSrcweir ::osl::Socket copySocket( sSocket ); 203cdf0e10cSrcweir 20463d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == copySocket.getType( )) << " test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor"; 205cdf0e10cSrcweir } 206cdf0e10cSrcweir 20763d99982SDamjan Jovanovic TEST_F(ctors, ctors_TypeRaw) 208cdf0e10cSrcweir { 209cdf0e10cSrcweir #ifdef WNT 210cdf0e10cSrcweir oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 211cdf0e10cSrcweir // LLA: ? ::osl::Socket sSocket( sHandleRaw ); 21263d99982SDamjan Jovanovic ASSERT_TRUE(sHandleRaw != NULL) << " type osl_Socket_TypeRaw socket create failed on UNX "; 213cdf0e10cSrcweir #else 214cdf0e10cSrcweir oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 21563d99982SDamjan Jovanovic ASSERT_TRUE(sHandleRaw == NULL) << " can't create socket with type osl_Socket_TypeRaw within UNX is ok."; 216cdf0e10cSrcweir #endif 217cdf0e10cSrcweir } 218cdf0e10cSrcweir 21963d99982SDamjan Jovanovic TEST_F(ctors, ctors_family_Ipx) 220cdf0e10cSrcweir { 221cdf0e10cSrcweir oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 22263d99982SDamjan Jovanovic ASSERT_TRUE(sHandleIpx != NULL) << " family osl_Socket_FamilyIpx socket create failed! "; 223cdf0e10cSrcweir ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE ); 22463d99982SDamjan Jovanovic printf("#Type is %d \n", sSocket.getType( ) ); 225cdf0e10cSrcweir 22663d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << " test for create new Socket instance that family is osl_Socket_FamilyIpx"; 227cdf0e10cSrcweir } 228cdf0e10cSrcweir 229cdf0e10cSrcweir /** testing the methods: 230cdf0e10cSrcweir inline Socket& SAL_CALL operator= ( oslSocket socketHandle); 231cdf0e10cSrcweir inline Socket& SAL_CALL operator= (const Socket& sock); 232cdf0e10cSrcweir inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ; 233cdf0e10cSrcweir inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const; 234cdf0e10cSrcweir */ 235cdf0e10cSrcweir 23663d99982SDamjan Jovanovic class operators : public ::testing::Test 237cdf0e10cSrcweir { 238cdf0e10cSrcweir public: 239cdf0e10cSrcweir oslSocket sHandle; 240cdf0e10cSrcweir // initialization 24163d99982SDamjan Jovanovic void SetUp( ) 242cdf0e10cSrcweir { 243cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 244cdf0e10cSrcweir } 245cdf0e10cSrcweir 24663d99982SDamjan Jovanovic void TearDown( ) 247cdf0e10cSrcweir { 248cdf0e10cSrcweir sHandle = NULL; 249cdf0e10cSrcweir } 25063d99982SDamjan Jovanovic }; // class operators 251cdf0e10cSrcweir 252cdf0e10cSrcweir /** test writer's comment: 253cdf0e10cSrcweir 254*99ad85ffSJohn Bampton the assignment operator does not support direct assignment like: 255cdf0e10cSrcweir ::osl::Socket sSocket = sHandle. 256cdf0e10cSrcweir */ 25763d99982SDamjan Jovanovic TEST_F(operators, operators_assignment_handle) 258cdf0e10cSrcweir { 259cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 260cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket.getHandle(); 261cdf0e10cSrcweir 26263d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment_handle function: test the assignment operator."; 263cdf0e10cSrcweir } 264cdf0e10cSrcweir 26563d99982SDamjan Jovanovic TEST_F(operators, operators_assignment) 266cdf0e10cSrcweir { 267cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 268cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket; 269cdf0e10cSrcweir 27063d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment function: assignment operator"; 271cdf0e10cSrcweir } 272cdf0e10cSrcweir 27363d99982SDamjan Jovanovic TEST_F(operators, operators_equal_handle_001) 274cdf0e10cSrcweir { 275cdf0e10cSrcweir /// Socket constructor. 276cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 277cdf0e10cSrcweir ::osl::Socket equalSocket = sSocket; 278cdf0e10cSrcweir 27963d99982SDamjan Jovanovic ASSERT_TRUE(equalSocket == sHandle) << " test for operators_equal_handle_001 function: check equal."; 280cdf0e10cSrcweir } 281cdf0e10cSrcweir 28263d99982SDamjan Jovanovic TEST_F(operators, operators_equal_handle_002) 283cdf0e10cSrcweir { 284cdf0e10cSrcweir /// Socket constructor. 285cdf0e10cSrcweir ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); 286cdf0e10cSrcweir 28763d99982SDamjan Jovanovic ASSERT_TRUE(!( equalSocket == sHandle )) << " test for operators_equal_handle_001 function: check unequal."; 288cdf0e10cSrcweir } 289cdf0e10cSrcweir 29063d99982SDamjan Jovanovic TEST_F(operators, operators_equal_001) 291cdf0e10cSrcweir { 292cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 293cdf0e10cSrcweir /// Socket copy constructor. 294cdf0e10cSrcweir ::osl::Socket equalSocket( sSocket ); 295cdf0e10cSrcweir 29663d99982SDamjan Jovanovic ASSERT_TRUE(equalSocket == sSocket) << " test for operators_equal function: check equal."; 297cdf0e10cSrcweir } 298cdf0e10cSrcweir 29963d99982SDamjan Jovanovic TEST_F(operators, operators_equal_002) 300cdf0e10cSrcweir { 301cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 302cdf0e10cSrcweir /// Socket copy constructor. 303cdf0e10cSrcweir ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); 304cdf0e10cSrcweir 30563d99982SDamjan Jovanovic ASSERT_TRUE(!( equalSocket == sSocket )) << " test for operators_equal_002 function: check unequal."; 306cdf0e10cSrcweir } 307cdf0e10cSrcweir 308cdf0e10cSrcweir /** testing the methods: 309cdf0e10cSrcweir inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite ); 310cdf0e10cSrcweir inline void SAL_CALL close(); 311cdf0e10cSrcweir */ 312cdf0e10cSrcweir 31363d99982SDamjan Jovanovic class close : public ::testing::Test 314cdf0e10cSrcweir { 315cdf0e10cSrcweir public: 316cdf0e10cSrcweir oslSocket sHandle; 317cdf0e10cSrcweir // initialization 31863d99982SDamjan Jovanovic void SetUp( ) 319cdf0e10cSrcweir { 320cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 321cdf0e10cSrcweir } 322cdf0e10cSrcweir 32363d99982SDamjan Jovanovic void TearDown( ) 324cdf0e10cSrcweir { 325cdf0e10cSrcweir sHandle = NULL; 326cdf0e10cSrcweir } 32763d99982SDamjan Jovanovic }; // class close 328cdf0e10cSrcweir 32963d99982SDamjan Jovanovic TEST_F(close, close_001) 330cdf0e10cSrcweir { 331cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 332cdf0e10cSrcweir sSocket.close(); 333cdf0e10cSrcweir 33463d99982SDamjan Jovanovic ASSERT_TRUE(sSocket.getHandle() == sHandle) << "test for close_001 function: this function is reserved for test."; 335cdf0e10cSrcweir } 336cdf0e10cSrcweir 33763d99982SDamjan Jovanovic TEST_F(close, close_002) 338cdf0e10cSrcweir { 33963d99982SDamjan Jovanovic // This blocks forever on FreeBSD 34063d99982SDamjan Jovanovic #if defined(LINUX) 341cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 342cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") ); 343cdf0e10cSrcweir myAcceptorThread.create(); 344cdf0e10cSrcweir 345cdf0e10cSrcweir thread_sleep( 1 ); 346cdf0e10cSrcweir //when accepting, close the socket, the thread will not block for accepting 347cdf0e10cSrcweir //man close:Any locks held on the file it was associated with, and owned by the process, are removed 348cdf0e10cSrcweir asSocket.close(); 349cdf0e10cSrcweir //thread_sleep( 2 ); 350cdf0e10cSrcweir myAcceptorThread.join(); 351cdf0e10cSrcweir 35263d99982SDamjan Jovanovic ASSERT_TRUE(myAcceptorThread.isOK() == sal_True) << "test for close when is accepting: the socket will quit accepting status."; 35363d99982SDamjan Jovanovic #endif 354cdf0e10cSrcweir } 355cdf0e10cSrcweir 356cdf0e10cSrcweir // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( ) 35763d99982SDamjan Jovanovic TEST_F(close, close_003) 358cdf0e10cSrcweir { 35963d99982SDamjan Jovanovic // This blocks forever on FreeBSD 36063d99982SDamjan Jovanovic #if defined(LINUX) 361cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 362cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("0.0.0.0") ); 363cdf0e10cSrcweir myAcceptorThread.create(); 364cdf0e10cSrcweir 365cdf0e10cSrcweir thread_sleep( 1 ); 366cdf0e10cSrcweir asSocket.close(); 367cdf0e10cSrcweir myAcceptorThread.join(); 368cdf0e10cSrcweir 36963d99982SDamjan Jovanovic ASSERT_TRUE(myAcceptorThread.isOK() == sal_True) << "test for close when is accepting: the socket will quit accepting status."; 37063d99982SDamjan Jovanovic #endif 371cdf0e10cSrcweir } 372cdf0e10cSrcweir 373cdf0e10cSrcweir /** testing the method: 374cdf0e10cSrcweir inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const; 375cdf0e10cSrcweir */ 376cdf0e10cSrcweir 37763d99982SDamjan Jovanovic class getLocalAddr : public ::testing::Test 378cdf0e10cSrcweir { 379cdf0e10cSrcweir public: 380cdf0e10cSrcweir oslSocket sHandle; 381cdf0e10cSrcweir // initialization 38263d99982SDamjan Jovanovic void SetUp( ) 383cdf0e10cSrcweir { 384cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 385cdf0e10cSrcweir } 386cdf0e10cSrcweir 38763d99982SDamjan Jovanovic void TearDown( ) 388cdf0e10cSrcweir { 389cdf0e10cSrcweir sHandle = NULL; 390cdf0e10cSrcweir } 39163d99982SDamjan Jovanovic }; // class getLocalAddr 392cdf0e10cSrcweir 393cdf0e10cSrcweir // get the Address of the local end of the socket 39463d99982SDamjan Jovanovic TEST_F(getLocalAddr, getLocalAddr_001) 395cdf0e10cSrcweir { 396cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 397cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT8 ); 398cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 399cdf0e10cSrcweir 400cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 401cdf0e10cSrcweir 402cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 403cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 40463d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << suError1.pData; 405cdf0e10cSrcweir 406cdf0e10cSrcweir sSocket.getLocalAddr( saLocalSocketAddr ); 407cdf0e10cSrcweir 408cdf0e10cSrcweir sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ; 409cdf0e10cSrcweir 41063d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address."; 411cdf0e10cSrcweir } 412cdf0e10cSrcweir 413cdf0e10cSrcweir /** testing the method: 414cdf0e10cSrcweir inline sal_Int32 SAL_CALL getLocalPort() const; 415cdf0e10cSrcweir */ 416cdf0e10cSrcweir 41763d99982SDamjan Jovanovic class getLocalPort : public ::testing::Test 418cdf0e10cSrcweir { 419cdf0e10cSrcweir public: 420cdf0e10cSrcweir oslSocket sHandle; 421cdf0e10cSrcweir // initialization 42263d99982SDamjan Jovanovic void SetUp( ) 423cdf0e10cSrcweir { 424cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 425cdf0e10cSrcweir } 426cdf0e10cSrcweir 42763d99982SDamjan Jovanovic void TearDown( ) 428cdf0e10cSrcweir { 429cdf0e10cSrcweir sHandle = NULL; 430cdf0e10cSrcweir } 43163d99982SDamjan Jovanovic }; // class getLocalPort 432cdf0e10cSrcweir 43363d99982SDamjan Jovanovic TEST_F(getLocalPort, getLocalPort_001) 434cdf0e10cSrcweir { 435cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 436cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT7 ); // aHostIp1 localhost 437cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 438cdf0e10cSrcweir 439cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 440cdf0e10cSrcweir 441cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 442cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 44363d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << suError1.pData; 444cdf0e10cSrcweir sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) ); 445cdf0e10cSrcweir 44663d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port."; 447cdf0e10cSrcweir } 448cdf0e10cSrcweir 449cdf0e10cSrcweir /** test writer's comment: 450cdf0e10cSrcweir 451cdf0e10cSrcweir the invalid port number can not be set by giving invalid port number 452cdf0e10cSrcweir such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be 453cdf0e10cSrcweir valid, the only instance that the getLocalPort returns OSL_INVALID_PORT 454cdf0e10cSrcweir is when saSocketAddr itself is an invalid one, that is , the IP or host name 455cdf0e10cSrcweir can not be found, then the created socket address is not valid. 456cdf0e10cSrcweir */ 45763d99982SDamjan Jovanovic TEST_F(getLocalPort, getLocalPort_002) 458cdf0e10cSrcweir { 459cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_TELNET); 460cdf0e10cSrcweir #ifdef WNT 461cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 462cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 463cdf0e10cSrcweir sSocket.bind( saBindSocketAddr ); 464cdf0e10cSrcweir //Invalid IP, so bind should fail 465cdf0e10cSrcweir ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), 466cdf0e10cSrcweir ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT), 467cdf0e10cSrcweir "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned."); 468cdf0e10cSrcweir sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) ); 469cdf0e10cSrcweir (void)bOK; 470cdf0e10cSrcweir #else 471cdf0e10cSrcweir //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT 472cdf0e10cSrcweir ::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"); 473cdf0e10cSrcweir #endif 47463d99982SDamjan Jovanovic ASSERT_TRUE(sal_False) << suError.pData; 475cdf0e10cSrcweir 476cdf0e10cSrcweir } 477cdf0e10cSrcweir 47863d99982SDamjan Jovanovic TEST_F(getLocalPort, getLocalPort_003) 479cdf0e10cSrcweir { 480cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 481cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL); 482cdf0e10cSrcweir 483cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 484cdf0e10cSrcweir 485cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 486cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 48763d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << suError1.pData; 488cdf0e10cSrcweir ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), 489cdf0e10cSrcweir ::rtl::OUString::createFromAscii("34463"), 490cdf0e10cSrcweir "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned"); 491cdf0e10cSrcweir sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535); 492cdf0e10cSrcweir 49363d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << suError.pData; 494cdf0e10cSrcweir } 495cdf0e10cSrcweir 496cdf0e10cSrcweir /** testing the method: 497cdf0e10cSrcweir inline ::rtl::OUString SAL_CALL getLocalHost() const; 498cdf0e10cSrcweir 499cdf0e10cSrcweir 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; 500cdf0e10cSrcweir 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 501cdf0e10cSrcweir will return hostname of current processor such as "aegean.PRC.Sun.COM" 502cdf0e10cSrcweir */ 503cdf0e10cSrcweir 50463d99982SDamjan Jovanovic class getLocalHost : public ::testing::Test 505cdf0e10cSrcweir { 506cdf0e10cSrcweir public: 507cdf0e10cSrcweir oslSocket sHandle; 508cdf0e10cSrcweir // initialization 50963d99982SDamjan Jovanovic void SetUp( ) 510cdf0e10cSrcweir { 511cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 512cdf0e10cSrcweir } 513cdf0e10cSrcweir 51463d99982SDamjan Jovanovic void TearDown( ) 515cdf0e10cSrcweir { 516cdf0e10cSrcweir sHandle = NULL; 517cdf0e10cSrcweir } 51863d99982SDamjan Jovanovic }; // class getLocalHost 519cdf0e10cSrcweir 52063d99982SDamjan Jovanovic TEST_F(getLocalHost, getLocalHost_001) 521cdf0e10cSrcweir { 522cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 523cdf0e10cSrcweir //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu 524cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT6 ); 525cdf0e10cSrcweir 526cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 527cdf0e10cSrcweir 528cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 529cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 53063d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << suError1.pData; 531cdf0e10cSrcweir sal_Bool bOK; 532cdf0e10cSrcweir ::rtl::OUString suError; 533cdf0e10cSrcweir #ifdef WNT 534cdf0e10cSrcweir bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; 535cdf0e10cSrcweir suError = outputError(sSocket.getLocalHost( ), getThisHostname( ), 536cdf0e10cSrcweir "test for getLocalHost function: create localhost socket and check name"); 537cdf0e10cSrcweir #else 538cdf0e10cSrcweir ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" ); 539cdf0e10cSrcweir sal_Bool bRes1, bRes2; 540cdf0e10cSrcweir bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ; 541cdf0e10cSrcweir bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ; 542cdf0e10cSrcweir bOK = bRes1 || bRes2; 543cdf0e10cSrcweir suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name"); 544cdf0e10cSrcweir #endif 54563d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << suError.pData; 546cdf0e10cSrcweir } 547cdf0e10cSrcweir 54863d99982SDamjan Jovanovic TEST_F(getLocalHost, getLocalHost_002) 549cdf0e10cSrcweir { 550cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 551cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_POP3); 552cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 553cdf0e10cSrcweir 554cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 555cdf0e10cSrcweir sSocket.bind( saBindSocketAddr ); 556cdf0e10cSrcweir //Invalid IP, so bind should fail 557cdf0e10cSrcweir sal_Bool bOK = compareUString( sSocket.getLocalHost( ), rtl::OUString::createFromAscii("") ) ; 558cdf0e10cSrcweir ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), rtl::OUString::createFromAscii(""), "test for getLocalHost function: getLocalHost with invalid SocketAddr"); 559cdf0e10cSrcweir 56063d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << suError.pData; 561cdf0e10cSrcweir } 562cdf0e10cSrcweir 563cdf0e10cSrcweir /** testing the methods: 564cdf0e10cSrcweir inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const; 565cdf0e10cSrcweir inline sal_Int32 SAL_CALL getPeerPort() const; 566cdf0e10cSrcweir inline ::rtl::OUString SAL_CALL getPeerHost() const; 567cdf0e10cSrcweir */ 56863d99982SDamjan Jovanovic class getPeer : public ::testing::Test 569cdf0e10cSrcweir { 570cdf0e10cSrcweir public: 571cdf0e10cSrcweir oslSocket sHandle; 572cdf0e10cSrcweir TimeValue *pTimeout; 573cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 574cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 575cdf0e10cSrcweir 576cdf0e10cSrcweir 577cdf0e10cSrcweir // initialization 57863d99982SDamjan Jovanovic void SetUp( ) 579cdf0e10cSrcweir { 580cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 581cdf0e10cSrcweir pTimeout->Seconds = 3; 582cdf0e10cSrcweir pTimeout->Nanosec = 0; 583cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 584cdf0e10cSrcweir } 585cdf0e10cSrcweir 58663d99982SDamjan Jovanovic void TearDown( ) 587cdf0e10cSrcweir { 588cdf0e10cSrcweir free( pTimeout ); 589cdf0e10cSrcweir sHandle = NULL; 590cdf0e10cSrcweir asAcceptorSocket.close( ); 591cdf0e10cSrcweir csConnectorSocket.close( ); 592cdf0e10cSrcweir } 59363d99982SDamjan Jovanovic }; // class getPeer 594cdf0e10cSrcweir 59563d99982SDamjan Jovanovic TEST_F(getPeer, getPeer_001) 596cdf0e10cSrcweir { 597cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 598cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 599cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 600cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 601cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 602cdf0e10cSrcweir /// launch server socket 603cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 60463d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind '127.0.0.1' address failed."; 605cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 60663d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed."; 607cdf0e10cSrcweir 608cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 609cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 610cdf0e10cSrcweir 611cdf0e10cSrcweir /// launch client socket 612cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 613cdf0e10cSrcweir 614cdf0e10cSrcweir /// get peer information 615cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected. 616cdf0e10cSrcweir sal_Int32 peerPort = csConnectorSocket.getPeerPort( ); 617cdf0e10cSrcweir ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( ); 618cdf0e10cSrcweir 61963d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&& 620cdf0e10cSrcweir ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) && 62163d99982SDamjan Jovanovic ( peerPort == saLocalSocketAddr.getPort( ) )) << "test for getPeer function: setup a connection and then get the peer address, port and host from client side."; 622cdf0e10cSrcweir } 623cdf0e10cSrcweir 624cdf0e10cSrcweir /** testing the methods: 625cdf0e10cSrcweir inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface); 626cdf0e10cSrcweir */ 627cdf0e10cSrcweir 628cdf0e10cSrcweir 62963d99982SDamjan Jovanovic class bind : public ::testing::Test 630cdf0e10cSrcweir { 631cdf0e10cSrcweir public: 632cdf0e10cSrcweir oslSocket sHandle; 633cdf0e10cSrcweir // initialization 63463d99982SDamjan Jovanovic void SetUp( ) 635cdf0e10cSrcweir { 636cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 637cdf0e10cSrcweir } 638cdf0e10cSrcweir 63963d99982SDamjan Jovanovic void TearDown( ) 640cdf0e10cSrcweir { 641cdf0e10cSrcweir sHandle = NULL; 642cdf0e10cSrcweir } 64363d99982SDamjan Jovanovic }; // class bind 644cdf0e10cSrcweir 64563d99982SDamjan Jovanovic TEST_F(bind, bind_001) 646cdf0e10cSrcweir { 647cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 648cdf0e10cSrcweir //bind must use local IP address ---mindyliu 649cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 ); 650cdf0e10cSrcweir 651cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 652cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 65363d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "Socket bind fail."; 654cdf0e10cSrcweir 655cdf0e10cSrcweir sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ; 656cdf0e10cSrcweir 657cdf0e10cSrcweir sSocket.close(); 65863d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "test for bind function: bind a valid address."; 659cdf0e10cSrcweir } 660cdf0e10cSrcweir 66163d99982SDamjan Jovanovic TEST_F(bind, bind_002) 662cdf0e10cSrcweir { 663cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 664cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_NETBIOS ); 665cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 666cdf0e10cSrcweir 667cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True); 668cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 669cdf0e10cSrcweir sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; 670cdf0e10cSrcweir 67163d99982SDamjan Jovanovic ASSERT_TRUE(( sal_False == bOK1 ) && ( sal_False == bOK2 )) << "test for bind function: bind a valid address."; 672cdf0e10cSrcweir } 673cdf0e10cSrcweir 674cdf0e10cSrcweir /** testing the methods: 675cdf0e10cSrcweir inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const; 676cdf0e10cSrcweir 677cdf0e10cSrcweir */ 67863d99982SDamjan Jovanovic class isRecvReady : public ::testing::Test 679cdf0e10cSrcweir { 680cdf0e10cSrcweir public: 681cdf0e10cSrcweir oslSocket sHandle; 682cdf0e10cSrcweir TimeValue *pTimeout; 683cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 684cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 685cdf0e10cSrcweir 686cdf0e10cSrcweir 687cdf0e10cSrcweir // initialization 68863d99982SDamjan Jovanovic void SetUp( ) 689cdf0e10cSrcweir { 690cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 691cdf0e10cSrcweir pTimeout->Seconds = 3; 692cdf0e10cSrcweir pTimeout->Nanosec = 0; 693cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 694cdf0e10cSrcweir } 695cdf0e10cSrcweir 69663d99982SDamjan Jovanovic void TearDown( ) 697cdf0e10cSrcweir { 698cdf0e10cSrcweir free( pTimeout ); 699cdf0e10cSrcweir sHandle = NULL; 700cdf0e10cSrcweir asAcceptorSocket.close( ); 701cdf0e10cSrcweir csConnectorSocket.close( ); 702cdf0e10cSrcweir } 70363d99982SDamjan Jovanovic }; // class isRecvReady 704cdf0e10cSrcweir 70563d99982SDamjan Jovanovic TEST_F(isRecvReady, isRecvReady_001) 706cdf0e10cSrcweir { 707cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 ); 708cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 ); 709cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 710cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 711cdf0e10cSrcweir /// launch server socket 712cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 713cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 71463d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed."; 715cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 71663d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed."; 717cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 718cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 719cdf0e10cSrcweir 720cdf0e10cSrcweir /// launch client socket 721cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 722cdf0e10cSrcweir 723cdf0e10cSrcweir /// is receive ready? 724cdf0e10cSrcweir sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout ); 725cdf0e10cSrcweir 72663d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK3 )) << "test for isRecvReady function: setup a connection and then check if it can transmit data."; 727cdf0e10cSrcweir } 728cdf0e10cSrcweir 729cdf0e10cSrcweir /** testing the methods: 730cdf0e10cSrcweir inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const; 731cdf0e10cSrcweir */ 73263d99982SDamjan Jovanovic class isSendReady : public ::testing::Test 733cdf0e10cSrcweir { 734cdf0e10cSrcweir public: 735cdf0e10cSrcweir oslSocket sHandle; 736cdf0e10cSrcweir TimeValue *pTimeout; 737cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 738cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 739cdf0e10cSrcweir 740cdf0e10cSrcweir 741cdf0e10cSrcweir // initialization 74263d99982SDamjan Jovanovic void SetUp( ) 743cdf0e10cSrcweir { 744cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 745cdf0e10cSrcweir pTimeout->Seconds = 3; 746cdf0e10cSrcweir pTimeout->Nanosec = 0; 747cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 748cdf0e10cSrcweir } 749cdf0e10cSrcweir 75063d99982SDamjan Jovanovic void TearDown( ) 751cdf0e10cSrcweir { 752cdf0e10cSrcweir free( pTimeout ); 753cdf0e10cSrcweir sHandle = NULL; 754cdf0e10cSrcweir asAcceptorSocket.close( ); 755cdf0e10cSrcweir csConnectorSocket.close( ); 756cdf0e10cSrcweir } 75763d99982SDamjan Jovanovic }; // class isSendReady 758cdf0e10cSrcweir 75963d99982SDamjan Jovanovic TEST_F(isSendReady, isSendReady_001) 760cdf0e10cSrcweir { 761cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 762cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 763cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 764cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 765cdf0e10cSrcweir 766cdf0e10cSrcweir /// launch server socket 767cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 768cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 76963d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed."; 770cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 77163d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed."; 772cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 773cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 774cdf0e10cSrcweir 775cdf0e10cSrcweir /// launch client socket 776cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 777cdf0e10cSrcweir 778cdf0e10cSrcweir /// is send ready? 779cdf0e10cSrcweir sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout ); 780cdf0e10cSrcweir 78163d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK3 )) << "test for isSendReady function: setup a connection and then check if it can transmit data."; 782cdf0e10cSrcweir } 783cdf0e10cSrcweir 784cdf0e10cSrcweir /** testing the methods: 785cdf0e10cSrcweir inline oslSocketType SAL_CALL getType() const; 786cdf0e10cSrcweir 787cdf0e10cSrcweir */ 788cdf0e10cSrcweir 78963d99982SDamjan Jovanovic class getType : public ::testing::Test 790cdf0e10cSrcweir { 791cdf0e10cSrcweir public: 792cdf0e10cSrcweir oslSocket sHandle; 793cdf0e10cSrcweir // initialization 79463d99982SDamjan Jovanovic void SetUp( ) 795cdf0e10cSrcweir { 796cdf0e10cSrcweir 797cdf0e10cSrcweir } 798cdf0e10cSrcweir 79963d99982SDamjan Jovanovic void TearDown( ) 800cdf0e10cSrcweir { 801cdf0e10cSrcweir sHandle = NULL; 802cdf0e10cSrcweir } 80363d99982SDamjan Jovanovic }; // class getType 804cdf0e10cSrcweir 80563d99982SDamjan Jovanovic TEST_F(getType, getType_001) 806cdf0e10cSrcweir { 807cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 808cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 809cdf0e10cSrcweir 81063d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << "test for getType function: get type of socket."; 811cdf0e10cSrcweir } 812cdf0e10cSrcweir 81363d99982SDamjan Jovanovic TEST_F(getType, getType_002) 814cdf0e10cSrcweir { 815cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 816cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 817cdf0e10cSrcweir 81863d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeDgram == sSocket.getType( )) << "test for getType function: get type of socket."; 819cdf0e10cSrcweir } 820cdf0e10cSrcweir 821cdf0e10cSrcweir #ifdef UNX 822cdf0e10cSrcweir // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here 823cdf0e10cSrcweir // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw() 82463d99982SDamjan Jovanovic TEST_F(getType, getType_003) 825cdf0e10cSrcweir { 82663d99982SDamjan Jovanovic ASSERT_TRUE(sal_True) << "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."; 827cdf0e10cSrcweir } 828cdf0e10cSrcweir #else 82963d99982SDamjan Jovanovic TEST_F(getType, getType_003) 830cdf0e10cSrcweir { 831cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 832cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 833cdf0e10cSrcweir 83463d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeRaw == sSocket.getType( )) << "test for getType function: get type of socket."; 835cdf0e10cSrcweir } 836cdf0e10cSrcweir #endif 837cdf0e10cSrcweir 838cdf0e10cSrcweir 839cdf0e10cSrcweir /** testing the methods: 840cdf0e10cSrcweir inline sal_Int32 SAL_CALL getOption( 841cdf0e10cSrcweir oslSocketOption Option, 842cdf0e10cSrcweir void* pBuffer, 843cdf0e10cSrcweir sal_uInt32 BufferLen, 844cdf0e10cSrcweir oslSocketOptionLevel Level= osl_Socket_LevelSocket) const; 845cdf0e10cSrcweir 846cdf0e10cSrcweir inline sal_Int32 getOption( oslSocketOption option ) const; 847cdf0e10cSrcweir 848cdf0e10cSrcweir */ 849cdf0e10cSrcweir 85063d99982SDamjan Jovanovic class getOption : public ::testing::Test 851cdf0e10cSrcweir { 852cdf0e10cSrcweir public: 853cdf0e10cSrcweir oslSocket sHandle; 854cdf0e10cSrcweir // initialization 85563d99982SDamjan Jovanovic void SetUp( ) 856cdf0e10cSrcweir { 857cdf0e10cSrcweir 858cdf0e10cSrcweir } 859cdf0e10cSrcweir 86063d99982SDamjan Jovanovic void TearDown( ) 861cdf0e10cSrcweir { 862cdf0e10cSrcweir sHandle = NULL; 863cdf0e10cSrcweir } 86463d99982SDamjan Jovanovic }; // class getOption 86563d99982SDamjan Jovanovic 866cdf0e10cSrcweir 867cdf0e10cSrcweir /** test writer's comment: 868cdf0e10cSrcweir 869cdf0e10cSrcweir in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream. 870cdf0e10cSrcweir 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ 871cdf0e10cSrcweir in 1. 872cdf0e10cSrcweir */ 873cdf0e10cSrcweir 87463d99982SDamjan Jovanovic TEST_F(getOption, getOption_001) 875cdf0e10cSrcweir { 876cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 877cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 878cdf0e10cSrcweir sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 879cdf0e10cSrcweir *pType = 0; 880cdf0e10cSrcweir sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) ); 881cdf0e10cSrcweir sal_Bool bOK = ( SOCK_STREAM == *pType ); 882cdf0e10cSrcweir // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream 883cdf0e10cSrcweir //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType ); 884cdf0e10cSrcweir free( pType ); 885cdf0e10cSrcweir 88663d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for getOption function: get type option of socket."; 887cdf0e10cSrcweir } 888cdf0e10cSrcweir 889cdf0e10cSrcweir // getsockopt error 89063d99982SDamjan Jovanovic TEST_F(getOption, getOption_004) 891cdf0e10cSrcweir { 892cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 893cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 894cdf0e10cSrcweir 895cdf0e10cSrcweir sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 896cdf0e10cSrcweir sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) ); 897cdf0e10cSrcweir free( pbDontRoute ); 898cdf0e10cSrcweir 89963d99982SDamjan Jovanovic ASSERT_TRUE(nRes == -1) << "test for getOption function: get invalid option of socket, should return -1."; 900cdf0e10cSrcweir } 901cdf0e10cSrcweir 90263d99982SDamjan Jovanovic TEST_F(getOption, getOption_simple_001) 903cdf0e10cSrcweir { 904cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 905cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 906cdf0e10cSrcweir 907cdf0e10cSrcweir sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) ); 908cdf0e10cSrcweir 90963d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for getOption function: get debug option of socket."; 910cdf0e10cSrcweir } 911cdf0e10cSrcweir 91263d99982SDamjan Jovanovic TEST_F(getOption, getOption_simple_002) 913cdf0e10cSrcweir { 914cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 915cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 916cdf0e10cSrcweir 917cdf0e10cSrcweir sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) ); 918cdf0e10cSrcweir 91963d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for getOption function: get debug option of socket."; 920cdf0e10cSrcweir } 921cdf0e10cSrcweir 922cdf0e10cSrcweir /** testing the methods: 923cdf0e10cSrcweir inline sal_Bool SAL_CALL setOption( oslSocketOption Option, 924cdf0e10cSrcweir void* pBuffer, 925cdf0e10cSrcweir sal_uInt32 BufferLen, 926cdf0e10cSrcweir oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const; 927cdf0e10cSrcweir */ 928cdf0e10cSrcweir 92963d99982SDamjan Jovanovic class setOption : public ::testing::Test 930cdf0e10cSrcweir { 931cdf0e10cSrcweir public: 932cdf0e10cSrcweir TimeValue *pTimeout; 933cdf0e10cSrcweir // LLA: maybe there is an error in the source, 934cdf0e10cSrcweir // as long as I remember, if a derived class do not overload all ctors there is a problem. 935cdf0e10cSrcweir 936cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 937cdf0e10cSrcweir 93863d99982SDamjan Jovanovic void SetUp( ) 939cdf0e10cSrcweir { 940cdf0e10cSrcweir 941cdf0e10cSrcweir } 942cdf0e10cSrcweir 94363d99982SDamjan Jovanovic void TearDown( ) 944cdf0e10cSrcweir { 945cdf0e10cSrcweir asAcceptorSocket.close( ); 946cdf0e10cSrcweir } 94763d99982SDamjan Jovanovic }; // class setOption 948cdf0e10cSrcweir 949cdf0e10cSrcweir // LLA: 950cdf0e10cSrcweir // getSocketOption returns BufferLen, or -1 if something failed 951cdf0e10cSrcweir 952cdf0e10cSrcweir // setSocketOption returns sal_True, if option could stored 953cdf0e10cSrcweir // else sal_False 954cdf0e10cSrcweir 95563d99982SDamjan Jovanovic TEST_F(setOption, setOption_001) 956cdf0e10cSrcweir { 957cdf0e10cSrcweir /// set and get option. 958cdf0e10cSrcweir int nBufferLen = sizeof ( sal_Int32); 959cdf0e10cSrcweir // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool! 960cdf0e10cSrcweir 961cdf0e10cSrcweir sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 962cdf0e10cSrcweir *pbDontRouteSet = 1; // sal_True; 963cdf0e10cSrcweir 964cdf0e10cSrcweir sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 965cdf0e10cSrcweir *pGetBuffer = 0; 966cdf0e10cSrcweir 967cdf0e10cSrcweir // maybe asAcceptorSocket is not right initialized 968cdf0e10cSrcweir sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen ); 96963d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == b1 )) << "setOption function failed."; 970cdf0e10cSrcweir sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); 97163d99982SDamjan Jovanovic ASSERT_TRUE(( n2 == nBufferLen )) << "getOption function failed."; 972cdf0e10cSrcweir 973cdf0e10cSrcweir // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value, 974cdf0e10cSrcweir // just judge it is zero or not! 975cdf0e10cSrcweir sal_Bool bOK = ( 0 != *pGetBuffer ); 97663d99982SDamjan Jovanovic printf("#setOption_001: getOption is %d \n", *pGetBuffer); 977cdf0e10cSrcweir 978cdf0e10cSrcweir // toggle check, set to 0 979cdf0e10cSrcweir *pbDontRouteSet = 0; 980cdf0e10cSrcweir 981cdf0e10cSrcweir sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) ); 98263d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == b3 )) << "setOption function failed."; 983cdf0e10cSrcweir sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); 98463d99982SDamjan Jovanovic ASSERT_TRUE(( n4 == nBufferLen )) << "getOption (DONTROUTE) function failed."; 985cdf0e10cSrcweir 986cdf0e10cSrcweir sal_Bool bOK2 = ( 0 == *pGetBuffer ); 987cdf0e10cSrcweir 98863d99982SDamjan Jovanovic printf("#setOption_001: getOption is %d \n", *pGetBuffer); 989cdf0e10cSrcweir 990cdf0e10cSrcweir // LLA: sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 991cdf0e10cSrcweir // LLA: *pbDontTouteSet = sal_True; 992cdf0e10cSrcweir // LLA: sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 993cdf0e10cSrcweir // LLA: *pbDontTouteGet = sal_False; 994cdf0e10cSrcweir // LLA: asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontTouteSet, sizeof ( sal_Bool ) ); 995cdf0e10cSrcweir // LLA: asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pbDontTouteGet, sizeof ( sal_Bool ) ); 996cdf0e10cSrcweir // LLA: ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet), 997cdf0e10cSrcweir // LLA: ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet), 998cdf0e10cSrcweir // LLA: "test for setOption function: set osl_Socket_OptionDontRoute and then check"); 999cdf0e10cSrcweir // LLA: 1000cdf0e10cSrcweir // LLA: sal_Bool bOK = ( sal_True == *pbDontTouteGet ); 1001cdf0e10cSrcweir // LLA: free( pbDontTouteSet ); 1002cdf0e10cSrcweir // LLA: free( pbDontTouteGet ); 1003cdf0e10cSrcweir 100463d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK ) && (sal_True == bOK2)) << "test for setOption function: set option of a socket and then check."; 1005cdf0e10cSrcweir 1006cdf0e10cSrcweir free( pbDontRouteSet ); 1007cdf0e10cSrcweir free( pGetBuffer ); 100863d99982SDamjan Jovanovic // LLA: ASSERT_TRUE(sal_True == bOK) << suError; 1009cdf0e10cSrcweir } 1010cdf0e10cSrcweir 101163d99982SDamjan Jovanovic TEST_F(setOption, setOption_002) 1012cdf0e10cSrcweir { 1013cdf0e10cSrcweir /// set and get option. 1014cdf0e10cSrcweir 1015cdf0e10cSrcweir // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen ); 1016cdf0e10cSrcweir // *pbLingerSet = 7; 1017cdf0e10cSrcweir // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen ); 1018cdf0e10cSrcweir /* struct */linger aLingerSet; 1019cdf0e10cSrcweir sal_Int32 nBufferLen = sizeof( struct linger ); 1020cdf0e10cSrcweir aLingerSet.l_onoff = 1; 1021cdf0e10cSrcweir aLingerSet.l_linger = 7; 1022cdf0e10cSrcweir 1023cdf0e10cSrcweir linger aLingerGet; 1024cdf0e10cSrcweir 1025cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen ); 1026cdf0e10cSrcweir 1027cdf0e10cSrcweir sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen ); 102863d99982SDamjan Jovanovic ASSERT_TRUE(( n1 == nBufferLen )) << "getOption (SO_LINGER) function failed."; 1029cdf0e10cSrcweir 103063d99982SDamjan Jovanovic //printf("#setOption_002: getOption is %d \n", aLingerGet.l_linger); 1031cdf0e10cSrcweir sal_Bool bOK = ( 7 == aLingerGet.l_linger ); 103263d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for setOption function: set option of a socket and then check. "; 1033cdf0e10cSrcweir 1034cdf0e10cSrcweir } 1035cdf0e10cSrcweir 103663d99982SDamjan Jovanovic TEST_F(setOption, setOption_003) 1037cdf0e10cSrcweir { 1038cdf0e10cSrcweir linger aLingerSet; 1039cdf0e10cSrcweir aLingerSet.l_onoff = 1; 1040cdf0e10cSrcweir aLingerSet.l_linger = 7; 1041cdf0e10cSrcweir 1042cdf0e10cSrcweir sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 ); 1043cdf0e10cSrcweir printUString( asAcceptorSocket.getErrorAsString( ) ); 104463d99982SDamjan Jovanovic ASSERT_TRUE(( b1 == sal_False )) << "setOption (SO_LINGER) function failed for optlen is 0."; 1045cdf0e10cSrcweir } 1046cdf0e10cSrcweir 104763d99982SDamjan Jovanovic TEST_F(setOption, setOption_simple_001) 1048cdf0e10cSrcweir { 1049cdf0e10cSrcweir /// set and get option. 1050cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True ); 1051cdf0e10cSrcweir sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); 1052cdf0e10cSrcweir 105363d99982SDamjan Jovanovic printf("setOption_simple_001(): getoption is %d \n", asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); 105463d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK )) << "test for setOption function: set option of a socket and then check."; 1055cdf0e10cSrcweir } 1056cdf0e10cSrcweir 105763d99982SDamjan Jovanovic TEST_F(setOption, setOption_simple_002) 1058cdf0e10cSrcweir { 1059cdf0e10cSrcweir /// set and get option. 1060cdf0e10cSrcweir // LLA: this does not work, due to the fact that SO_LINGER is a structure 1061cdf0e10cSrcweir // LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 ); 1062cdf0e10cSrcweir // LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) ); 1063cdf0e10cSrcweir 106463d99982SDamjan Jovanovic // LLA: ASSERT_TRUE(// LLA: ( sal_True == bOK )) << "test for setOption function: set option of a socket and then check."; 1065cdf0e10cSrcweir } 1066cdf0e10cSrcweir 1067cdf0e10cSrcweir 1068cdf0e10cSrcweir /** testing the method: 1069cdf0e10cSrcweir inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode); 1070cdf0e10cSrcweir */ 107163d99982SDamjan Jovanovic class enableNonBlockingMode : public ::testing::Test 1072cdf0e10cSrcweir { 1073cdf0e10cSrcweir public: 1074cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 107563d99982SDamjan Jovanovic }; // class enableNonBlockingMode 1076cdf0e10cSrcweir 107763d99982SDamjan Jovanovic TEST_F(enableNonBlockingMode, enableNonBlockingMode_001) 1078cdf0e10cSrcweir { 1079cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 1080cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 1081cdf0e10cSrcweir 1082cdf0e10cSrcweir /// launch server socket 1083cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1084cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 108563d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed."; 1086cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 108763d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed."; 1088cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 1089cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 1090cdf0e10cSrcweir 1091cdf0e10cSrcweir /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. 1092cdf0e10cSrcweir sal_Bool bOK = sal_True; 1093cdf0e10cSrcweir asAcceptorSocket.close( ); 1094cdf0e10cSrcweir 109563d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK )) << "test for enableNonBlockingMode function: launch a server socket and make it non blocking. if it can pass the acceptConnection statement, it is non-blocking"; 1096cdf0e10cSrcweir } 1097cdf0e10cSrcweir 1098cdf0e10cSrcweir 1099cdf0e10cSrcweir /** testing the method: 1100cdf0e10cSrcweir inline sal_Bool SAL_CALL isNonBlockingMode() const; 1101cdf0e10cSrcweir */ 110263d99982SDamjan Jovanovic class isNonBlockingMode : public ::testing::Test 1103cdf0e10cSrcweir { 1104cdf0e10cSrcweir public: 1105cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 110663d99982SDamjan Jovanovic }; // class isNonBlockingMode 1107cdf0e10cSrcweir 110863d99982SDamjan Jovanovic TEST_F(isNonBlockingMode, isNonBlockingMode_001) 1109cdf0e10cSrcweir { 1110cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 1111cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 1112cdf0e10cSrcweir 1113cdf0e10cSrcweir /// launch server socket 1114cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 1115cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 111663d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed."; 1117cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 111863d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed."; 1119cdf0e10cSrcweir 1120cdf0e10cSrcweir sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( ); 1121cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 1122cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 1123cdf0e10cSrcweir 1124cdf0e10cSrcweir /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. 1125cdf0e10cSrcweir sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( ); 1126cdf0e10cSrcweir asAcceptorSocket.close( ); 1127cdf0e10cSrcweir 112863d99982SDamjan Jovanovic ASSERT_TRUE(( sal_False == bOK3 ) && ( sal_True == bOK4 )) << "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."; 1129cdf0e10cSrcweir } 1130cdf0e10cSrcweir 1131cdf0e10cSrcweir /** testing the method: 1132cdf0e10cSrcweir inline void SAL_CALL clearError() const; 1133cdf0e10cSrcweir */ 113463d99982SDamjan Jovanovic class clearError : public ::testing::Test 1135cdf0e10cSrcweir { 1136cdf0e10cSrcweir public: 1137cdf0e10cSrcweir oslSocket sHandle; 1138cdf0e10cSrcweir // initialization 113963d99982SDamjan Jovanovic void SetUp( ) 1140cdf0e10cSrcweir { 1141cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1142cdf0e10cSrcweir } 1143cdf0e10cSrcweir 114463d99982SDamjan Jovanovic void TearDown( ) 1145cdf0e10cSrcweir { 1146cdf0e10cSrcweir sHandle = NULL; 1147cdf0e10cSrcweir } 114863d99982SDamjan Jovanovic }; // class clearError 1149cdf0e10cSrcweir 115063d99982SDamjan Jovanovic TEST_F(clearError, clearError_001) 1151cdf0e10cSrcweir { 1152cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1153cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_HTTP2 ); 1154cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 1155cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1156cdf0e10cSrcweir sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" 1157cdf0e10cSrcweir oslSocketError seBind = sSocket.getError( ); 1158cdf0e10cSrcweir sSocket.clearError( ); 1159cdf0e10cSrcweir 116063d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None) << "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result."; 1161cdf0e10cSrcweir } 1162cdf0e10cSrcweir 1163cdf0e10cSrcweir /** testing the methods: 1164cdf0e10cSrcweir inline oslSocketError getError() const; 1165cdf0e10cSrcweir inline ::rtl::OUString getErrorAsString( ) const; 1166cdf0e10cSrcweir */ 116763d99982SDamjan Jovanovic class getError : public ::testing::Test 1168cdf0e10cSrcweir { 1169cdf0e10cSrcweir public: 1170cdf0e10cSrcweir oslSocket sHandle; 1171cdf0e10cSrcweir // initialization 117263d99982SDamjan Jovanovic void SetUp( ) 1173cdf0e10cSrcweir { 1174cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1175cdf0e10cSrcweir } 1176cdf0e10cSrcweir 117763d99982SDamjan Jovanovic void TearDown( ) 1178cdf0e10cSrcweir { 1179cdf0e10cSrcweir sHandle = NULL; 1180cdf0e10cSrcweir } 118163d99982SDamjan Jovanovic }; // class getError 1182cdf0e10cSrcweir 118363d99982SDamjan Jovanovic TEST_F(getError, getError_001) 1184cdf0e10cSrcweir { 1185cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1186cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 1187cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 1188cdf0e10cSrcweir 118963d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_E_None == sSocket.getError( )) << "test for getError function: should get no error."; 1190cdf0e10cSrcweir } 1191cdf0e10cSrcweir 119263d99982SDamjan Jovanovic TEST_F(getError, getError_002) 1193cdf0e10cSrcweir { 1194cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1195cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_FTP ); 1196cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 1197cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1198cdf0e10cSrcweir sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" 1199cdf0e10cSrcweir //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError. 1200cdf0e10cSrcweir #if defined(SOLARIS) 120163d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_E_InvalidError == sSocket.getError( )) << "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. "; 1202cdf0e10cSrcweir #else 1203cdf0e10cSrcweir //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail. 1204cdf0e10cSrcweir 120563d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_E_AddrNotAvail == sSocket.getError( )) << "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"; 1206cdf0e10cSrcweir #endif 1207cdf0e10cSrcweir } 1208cdf0e10cSrcweir 1209cdf0e10cSrcweir 1210cdf0e10cSrcweir /** testing the methods: 1211cdf0e10cSrcweir inline oslSocket getHandle() const; 1212cdf0e10cSrcweir */ 1213cdf0e10cSrcweir 121463d99982SDamjan Jovanovic class getHandle : public ::testing::Test 1215cdf0e10cSrcweir { 1216cdf0e10cSrcweir public: 1217cdf0e10cSrcweir oslSocket sHandle; 1218cdf0e10cSrcweir // initialization 121963d99982SDamjan Jovanovic void SetUp( ) 1220cdf0e10cSrcweir { 1221cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1222cdf0e10cSrcweir } 1223cdf0e10cSrcweir 122463d99982SDamjan Jovanovic void TearDown( ) 1225cdf0e10cSrcweir { 1226cdf0e10cSrcweir sHandle = NULL; 1227cdf0e10cSrcweir } 122863d99982SDamjan Jovanovic }; // class getHandle 1229cdf0e10cSrcweir 123063d99982SDamjan Jovanovic TEST_F(getHandle, getHandle_001) 1231cdf0e10cSrcweir { 1232cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1233cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket.getHandle(); 1234cdf0e10cSrcweir 123563d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment_handle function: test the assignment operator."; 1236cdf0e10cSrcweir } 1237cdf0e10cSrcweir 123863d99982SDamjan Jovanovic TEST_F(getHandle, getHandle_002) 1239cdf0e10cSrcweir { 1240cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 1241cdf0e10cSrcweir ::osl::Socket assignSocket ( sSocket.getHandle( ) ); 1242cdf0e10cSrcweir 124363d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment function: assignment operator"; 1244cdf0e10cSrcweir } 1245cdf0e10cSrcweir 1246cdf0e10cSrcweir 1247cdf0e10cSrcweir } // namespace osl_Socket 1248cdf0e10cSrcweir 124963d99982SDamjan Jovanovic int main(int argc, char **argv) 125063d99982SDamjan Jovanovic { 125163d99982SDamjan Jovanovic ::testing::InitGoogleTest(&argc, argv); 125263d99982SDamjan Jovanovic return RUN_ALL_TESTS(); 125363d99982SDamjan Jovanovic } 1254