1*87d2adbcSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*87d2adbcSAndrew Rist * distributed with this work for additional information 6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance 9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an 15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the 17*87d2adbcSAndrew Rist * specific language governing permissions and limitations 18*87d2adbcSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*87d2adbcSAndrew Rist *************************************************************/ 21*87d2adbcSAndrew Rist 22*87d2adbcSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_sal.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir /** test coder preface: 28cdf0e10cSrcweir 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform 29cdf0e10cSrcweir if you are not including ws2_32.lib in makefile.mk, the including format will be like this: 30cdf0e10cSrcweir 31cdf0e10cSrcweir .IF "$(GUI)" == "WNT" 32cdf0e10cSrcweir SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib 33cdf0e10cSrcweir SHL1STDLIBS += ws2_32.lib 34cdf0e10cSrcweir .ENDIF 35cdf0e10cSrcweir 36cdf0e10cSrcweir likewise on Solaris platform. 37cdf0e10cSrcweir .IF "$(GUI)" == "UNX" 38cdf0e10cSrcweir SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a 39cdf0e10cSrcweir SHL1STDLIBS += -lsocket -ldl -lnsl 40cdf0e10cSrcweir .ENDIF 41cdf0e10cSrcweir 42cdf0e10cSrcweir 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4 43cdf0e10cSrcweir category. 44cdf0e10cSrcweir 45cdf0e10cSrcweir 3. some fragment of Socket source implementation are lack of comment so it is hard for testers 46cdf0e10cSrcweir guess what the exact functionality or usage of a member. Hope the Socket section's comment 47cdf0e10cSrcweir will be added. 48cdf0e10cSrcweir 49cdf0e10cSrcweir 4. following functions are declared but not implemented: 50cdf0e10cSrcweir inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; 51cdf0e10cSrcweir */ 52cdf0e10cSrcweir 53cdf0e10cSrcweir //------------------------------------------------------------------------ 54cdf0e10cSrcweir // include files 55cdf0e10cSrcweir //------------------------------------------------------------------------ 56cdf0e10cSrcweir 57cdf0e10cSrcweir #include <testshl/simpleheader.hxx> 58cdf0e10cSrcweir 59cdf0e10cSrcweir //#include "osl_Socket_Const.h" 60cdf0e10cSrcweir #include "sockethelper.hxx" 61cdf0e10cSrcweir 62cdf0e10cSrcweir using namespace osl; 63cdf0e10cSrcweir using namespace rtl; 64cdf0e10cSrcweir 65cdf0e10cSrcweir #define IP_PORT_FTP 21 66cdf0e10cSrcweir #define IP_PORT_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: run()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 { 99cdf0e10cSrcweir t_print("# AcceptorSocket bind address failed.\n" ) ; 100cdf0e10cSrcweir return; 101cdf0e10cSrcweir } 102cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 103cdf0e10cSrcweir if ( sal_True != bOK2 ) 104cdf0e10cSrcweir { 105cdf0e10cSrcweir t_print("# 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; 115cdf0e10cSrcweir t_print("AcceptorThread: acceptConnection failed! \n"); 116cdf0e10cSrcweir } 117cdf0e10cSrcweir } 118cdf0e10cSrcweir public: AcceptorThread(::osl::AcceptorSocket & asSocket,::rtl::OUString const & aBindIP)119cdf0e10cSrcweir AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP ) 120cdf0e10cSrcweir : asAcceptorSocket( asSocket ), aHostIP( aBindIP ) 121cdf0e10cSrcweir { 122cdf0e10cSrcweir bOK = sal_False; 123cdf0e10cSrcweir } 124cdf0e10cSrcweir isOK()125cdf0e10cSrcweir sal_Bool isOK() { return bOK; } 126cdf0e10cSrcweir ~AcceptorThread()127cdf0e10cSrcweir ~AcceptorThread( ) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir if ( isRunning( ) ) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir asAcceptorSocket.shutdown(); 132cdf0e10cSrcweir t_print("# 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 158cdf0e10cSrcweir class ctors : public CppUnit::TestFixture 159cdf0e10cSrcweir { 160cdf0e10cSrcweir public: 161cdf0e10cSrcweir oslSocket sHandle; 162cdf0e10cSrcweir // initialization setUp()163cdf0e10cSrcweir void setUp( ) 164cdf0e10cSrcweir { 165cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 166cdf0e10cSrcweir } 167cdf0e10cSrcweir tearDown()168cdf0e10cSrcweir void tearDown( ) 169cdf0e10cSrcweir { 170cdf0e10cSrcweir sHandle = NULL; 171cdf0e10cSrcweir } 172cdf0e10cSrcweir 173cdf0e10cSrcweir ctors_none()174cdf0e10cSrcweir void ctors_none() 175cdf0e10cSrcweir { 176cdf0e10cSrcweir /// Socket constructor. 177cdf0e10cSrcweir // ::osl::Socket sSocket(); 178cdf0e10cSrcweir 179cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the socket was created successfully, if no exception occured", 180cdf0e10cSrcweir 1 == 1 ); 181cdf0e10cSrcweir } 182cdf0e10cSrcweir ctors_acquire()183cdf0e10cSrcweir void ctors_acquire() 184cdf0e10cSrcweir { 185cdf0e10cSrcweir /// Socket constructor. 186cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 187cdf0e10cSrcweir 188cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully", 189cdf0e10cSrcweir osl_Socket_TypeStream == sSocket.getType( ) ); 190cdf0e10cSrcweir } 191cdf0e10cSrcweir ctors_no_acquire()192cdf0e10cSrcweir void ctors_no_acquire() 193cdf0e10cSrcweir { 194cdf0e10cSrcweir /// Socket constructor. 195cdf0e10cSrcweir ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE ); 196cdf0e10cSrcweir 197cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully", 198cdf0e10cSrcweir osl_Socket_TypeStream == sSocket.getType( ) ); 199cdf0e10cSrcweir } 200cdf0e10cSrcweir ctors_copy_ctor()201cdf0e10cSrcweir void ctors_copy_ctor() 202cdf0e10cSrcweir { 203cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 204cdf0e10cSrcweir /// Socket copy constructor. 205cdf0e10cSrcweir ::osl::Socket copySocket( sSocket ); 206cdf0e10cSrcweir 207cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor", 208cdf0e10cSrcweir osl_Socket_TypeStream == copySocket.getType( ) ); 209cdf0e10cSrcweir } 210cdf0e10cSrcweir ctors_TypeRaw()211cdf0e10cSrcweir void ctors_TypeRaw() 212cdf0e10cSrcweir { 213cdf0e10cSrcweir #ifdef WNT 214cdf0e10cSrcweir oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 215cdf0e10cSrcweir // LLA: ? ::osl::Socket sSocket( sHandleRaw ); 216cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( " type osl_Socket_TypeRaw socket create failed on UNX ", sHandleRaw != NULL); 217cdf0e10cSrcweir #else 218cdf0e10cSrcweir oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 219cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( " can't create socket with type osl_Socket_TypeRaw within UNX is ok.", sHandleRaw == NULL); 220cdf0e10cSrcweir #endif 221cdf0e10cSrcweir } 222cdf0e10cSrcweir ctors_family_Ipx()223cdf0e10cSrcweir void ctors_family_Ipx() 224cdf0e10cSrcweir { 225cdf0e10cSrcweir oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 226cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( " family osl_Socket_FamilyIpx socket create failed! ", sHandleIpx != NULL); 227cdf0e10cSrcweir ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE ); 228cdf0e10cSrcweir t_print("#Type is %d \n", sSocket.getType( ) ); 229cdf0e10cSrcweir 230cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for create new Socket instance that family is osl_Socket_FamilyIpx", 231cdf0e10cSrcweir osl_Socket_TypeStream == sSocket.getType( ) ); 232cdf0e10cSrcweir } 233cdf0e10cSrcweir 234cdf0e10cSrcweir 235cdf0e10cSrcweir 236cdf0e10cSrcweir CPPUNIT_TEST_SUITE( ctors ); 237cdf0e10cSrcweir CPPUNIT_TEST( ctors_none ); 238cdf0e10cSrcweir CPPUNIT_TEST( ctors_acquire ); 239cdf0e10cSrcweir CPPUNIT_TEST( ctors_no_acquire ); 240cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy_ctor ); 241cdf0e10cSrcweir CPPUNIT_TEST( ctors_TypeRaw ); 242cdf0e10cSrcweir CPPUNIT_TEST( ctors_family_Ipx ); 243cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 244cdf0e10cSrcweir 245cdf0e10cSrcweir }; // class ctors 246cdf0e10cSrcweir 247cdf0e10cSrcweir 248cdf0e10cSrcweir /** testing the methods: 249cdf0e10cSrcweir inline Socket& SAL_CALL operator= ( oslSocket socketHandle); 250cdf0e10cSrcweir inline Socket& SAL_CALL operator= (const Socket& sock); 251cdf0e10cSrcweir inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ; 252cdf0e10cSrcweir inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const; 253cdf0e10cSrcweir */ 254cdf0e10cSrcweir 255cdf0e10cSrcweir class operators : public CppUnit::TestFixture 256cdf0e10cSrcweir { 257cdf0e10cSrcweir public: 258cdf0e10cSrcweir oslSocket sHandle; 259cdf0e10cSrcweir // initialization setUp()260cdf0e10cSrcweir void setUp( ) 261cdf0e10cSrcweir { 262cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 263cdf0e10cSrcweir } 264cdf0e10cSrcweir tearDown()265cdf0e10cSrcweir void tearDown( ) 266cdf0e10cSrcweir { 267cdf0e10cSrcweir sHandle = NULL; 268cdf0e10cSrcweir } 269cdf0e10cSrcweir 270cdf0e10cSrcweir 271cdf0e10cSrcweir /** test writer's comment: 272cdf0e10cSrcweir 273cdf0e10cSrcweir the assignment operator does not support direct assinment like: 274cdf0e10cSrcweir ::osl::Socket sSocket = sHandle. 275cdf0e10cSrcweir */ operators_assignment_handle()276cdf0e10cSrcweir void operators_assignment_handle() 277cdf0e10cSrcweir { 278cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 279cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket.getHandle(); 280cdf0e10cSrcweir 281cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.", 282cdf0e10cSrcweir osl_Socket_TypeStream == assignSocket.getType( ) ); 283cdf0e10cSrcweir } 284cdf0e10cSrcweir operators_assignment()285cdf0e10cSrcweir void operators_assignment() 286cdf0e10cSrcweir { 287cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 288cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket; 289cdf0e10cSrcweir 290cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator", 291cdf0e10cSrcweir osl_Socket_TypeStream == assignSocket.getType( ) ); 292cdf0e10cSrcweir } 293cdf0e10cSrcweir operators_equal_handle_001()294cdf0e10cSrcweir void operators_equal_handle_001() 295cdf0e10cSrcweir { 296cdf0e10cSrcweir /// Socket constructor. 297cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 298cdf0e10cSrcweir ::osl::Socket equalSocket = sSocket; 299cdf0e10cSrcweir 300cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check equal.", 301cdf0e10cSrcweir equalSocket == sHandle ); 302cdf0e10cSrcweir } 303cdf0e10cSrcweir operators_equal_handle_002()304cdf0e10cSrcweir void operators_equal_handle_002() 305cdf0e10cSrcweir { 306cdf0e10cSrcweir /// Socket constructor. 307cdf0e10cSrcweir ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); 308cdf0e10cSrcweir 309cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check unequal.", 310cdf0e10cSrcweir !( equalSocket == sHandle ) ); 311cdf0e10cSrcweir } 312cdf0e10cSrcweir operators_equal_001()313cdf0e10cSrcweir void operators_equal_001() 314cdf0e10cSrcweir { 315cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 316cdf0e10cSrcweir /// Socket copy constructor. 317cdf0e10cSrcweir ::osl::Socket equalSocket( sSocket ); 318cdf0e10cSrcweir 319cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for operators_equal function: check equal.", 320cdf0e10cSrcweir equalSocket == sSocket ); 321cdf0e10cSrcweir } 322cdf0e10cSrcweir operators_equal_002()323cdf0e10cSrcweir void operators_equal_002() 324cdf0e10cSrcweir { 325cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 326cdf0e10cSrcweir /// Socket copy constructor. 327cdf0e10cSrcweir ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); 328cdf0e10cSrcweir 329cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_002 function: check unequal.", 330cdf0e10cSrcweir !( equalSocket == sSocket ) ); 331cdf0e10cSrcweir } 332cdf0e10cSrcweir 333cdf0e10cSrcweir CPPUNIT_TEST_SUITE( operators ); 334cdf0e10cSrcweir CPPUNIT_TEST( operators_assignment_handle ); 335cdf0e10cSrcweir CPPUNIT_TEST( operators_assignment ); 336cdf0e10cSrcweir CPPUNIT_TEST( operators_equal_handle_001 ); 337cdf0e10cSrcweir CPPUNIT_TEST( operators_equal_handle_002 ); 338cdf0e10cSrcweir CPPUNIT_TEST( operators_equal_001 ); 339cdf0e10cSrcweir CPPUNIT_TEST( operators_equal_002 ); 340cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 341cdf0e10cSrcweir 342cdf0e10cSrcweir }; // class operators 343cdf0e10cSrcweir 344cdf0e10cSrcweir 345cdf0e10cSrcweir /** testing the methods: 346cdf0e10cSrcweir inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite ); 347cdf0e10cSrcweir inline void SAL_CALL close(); 348cdf0e10cSrcweir */ 349cdf0e10cSrcweir 350cdf0e10cSrcweir class close : public CppUnit::TestFixture 351cdf0e10cSrcweir { 352cdf0e10cSrcweir public: 353cdf0e10cSrcweir oslSocket sHandle; 354cdf0e10cSrcweir // initialization setUp()355cdf0e10cSrcweir void setUp( ) 356cdf0e10cSrcweir { 357cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 358cdf0e10cSrcweir } 359cdf0e10cSrcweir tearDown()360cdf0e10cSrcweir void tearDown( ) 361cdf0e10cSrcweir { 362cdf0e10cSrcweir sHandle = NULL; 363cdf0e10cSrcweir } 364cdf0e10cSrcweir 365cdf0e10cSrcweir close_001()366cdf0e10cSrcweir void close_001() 367cdf0e10cSrcweir { 368cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 369cdf0e10cSrcweir sSocket.close(); 370cdf0e10cSrcweir 371cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for close_001 function: this function is reserved for test.", 372cdf0e10cSrcweir sSocket.getHandle() == sHandle ); 373cdf0e10cSrcweir } 374cdf0e10cSrcweir close_002()375cdf0e10cSrcweir void close_002() 376cdf0e10cSrcweir { 377cdf0e10cSrcweir //#if defined(LINUX) 378cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 379cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") ); 380cdf0e10cSrcweir myAcceptorThread.create(); 381cdf0e10cSrcweir 382cdf0e10cSrcweir thread_sleep( 1 ); 383cdf0e10cSrcweir //when accepting, close the socket, the thread will not block for accepting 384cdf0e10cSrcweir //man close:Any locks held on the file it was associated with, and owned by the process, are removed 385cdf0e10cSrcweir asSocket.close(); 386cdf0e10cSrcweir //thread_sleep( 2 ); 387cdf0e10cSrcweir myAcceptorThread.join(); 388cdf0e10cSrcweir 389cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", 390cdf0e10cSrcweir myAcceptorThread.isOK() == sal_True ); 391cdf0e10cSrcweir //#endif 392cdf0e10cSrcweir } 393cdf0e10cSrcweir 394cdf0e10cSrcweir // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( ) close_003()395cdf0e10cSrcweir void close_003() 396cdf0e10cSrcweir { 397cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 398cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("0.0.0.0") ); 399cdf0e10cSrcweir myAcceptorThread.create(); 400cdf0e10cSrcweir 401cdf0e10cSrcweir thread_sleep( 1 ); 402cdf0e10cSrcweir asSocket.close(); 403cdf0e10cSrcweir myAcceptorThread.join(); 404cdf0e10cSrcweir 405cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", 406cdf0e10cSrcweir myAcceptorThread.isOK() == sal_True ); 407cdf0e10cSrcweir } 408cdf0e10cSrcweir 409cdf0e10cSrcweir CPPUNIT_TEST_SUITE( close ); 410cdf0e10cSrcweir CPPUNIT_TEST( close_001 ); 411cdf0e10cSrcweir CPPUNIT_TEST( close_002 ); 412cdf0e10cSrcweir CPPUNIT_TEST( close_003 ); 413cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 414cdf0e10cSrcweir 415cdf0e10cSrcweir }; // class close 416cdf0e10cSrcweir 417cdf0e10cSrcweir /** testing the method: 418cdf0e10cSrcweir inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const; 419cdf0e10cSrcweir */ 420cdf0e10cSrcweir 421cdf0e10cSrcweir class getLocalAddr : public CppUnit::TestFixture 422cdf0e10cSrcweir { 423cdf0e10cSrcweir public: 424cdf0e10cSrcweir oslSocket sHandle; 425cdf0e10cSrcweir // initialization setUp()426cdf0e10cSrcweir void setUp( ) 427cdf0e10cSrcweir { 428cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 429cdf0e10cSrcweir } 430cdf0e10cSrcweir tearDown()431cdf0e10cSrcweir void tearDown( ) 432cdf0e10cSrcweir { 433cdf0e10cSrcweir sHandle = NULL; 434cdf0e10cSrcweir } 435cdf0e10cSrcweir 436cdf0e10cSrcweir // get the Address of the local end of the socket getLocalAddr_001()437cdf0e10cSrcweir void getLocalAddr_001() 438cdf0e10cSrcweir { 439cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 440cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT8 ); 441cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 442cdf0e10cSrcweir 443cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 444cdf0e10cSrcweir 445cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 446cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 447cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 448cdf0e10cSrcweir 449cdf0e10cSrcweir sSocket.getLocalAddr( saLocalSocketAddr ); 450cdf0e10cSrcweir 451cdf0e10cSrcweir sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ; 452cdf0e10cSrcweir 453cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.", 454cdf0e10cSrcweir sal_True == bOK ); 455cdf0e10cSrcweir } 456cdf0e10cSrcweir 457cdf0e10cSrcweir 458cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getLocalAddr ); 459cdf0e10cSrcweir CPPUNIT_TEST( getLocalAddr_001 ); 460cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 461cdf0e10cSrcweir 462cdf0e10cSrcweir }; // class getLocalAddr 463cdf0e10cSrcweir 464cdf0e10cSrcweir 465cdf0e10cSrcweir /** testing the method: 466cdf0e10cSrcweir inline sal_Int32 SAL_CALL getLocalPort() const; 467cdf0e10cSrcweir */ 468cdf0e10cSrcweir 469cdf0e10cSrcweir class getLocalPort : public CppUnit::TestFixture 470cdf0e10cSrcweir { 471cdf0e10cSrcweir public: 472cdf0e10cSrcweir oslSocket sHandle; 473cdf0e10cSrcweir // initialization setUp()474cdf0e10cSrcweir void setUp( ) 475cdf0e10cSrcweir { 476cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 477cdf0e10cSrcweir } 478cdf0e10cSrcweir tearDown()479cdf0e10cSrcweir void tearDown( ) 480cdf0e10cSrcweir { 481cdf0e10cSrcweir sHandle = NULL; 482cdf0e10cSrcweir } 483cdf0e10cSrcweir 484cdf0e10cSrcweir getLocalPort_001()485cdf0e10cSrcweir void getLocalPort_001() 486cdf0e10cSrcweir { 487cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 488cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT7 ); // aHostIp1 localhost 489cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 490cdf0e10cSrcweir 491cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 492cdf0e10cSrcweir 493cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 494cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 495cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 496cdf0e10cSrcweir sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) ); 497cdf0e10cSrcweir 498cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.", 499cdf0e10cSrcweir sal_True == bOK ); 500cdf0e10cSrcweir } 501cdf0e10cSrcweir 502cdf0e10cSrcweir /** test writer's comment: 503cdf0e10cSrcweir 504cdf0e10cSrcweir the invalid port number can not be set by giving invalid port number 505cdf0e10cSrcweir such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be 506cdf0e10cSrcweir valid, the only instance that the getLocalPort returns OSL_INVALID_PORT 507cdf0e10cSrcweir is when saSocketAddr itself is an invalid one, that is , the IP or host name 508cdf0e10cSrcweir can not be found, then the created socket address is not valid. 509cdf0e10cSrcweir */ getLocalPort_002()510cdf0e10cSrcweir void getLocalPort_002() 511cdf0e10cSrcweir { 512cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_TELNET); 513cdf0e10cSrcweir #ifdef WNT 514cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 515cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 516cdf0e10cSrcweir sSocket.bind( saBindSocketAddr ); 517cdf0e10cSrcweir //Invalid IP, so bind should fail 518cdf0e10cSrcweir ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), 519cdf0e10cSrcweir ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT), 520cdf0e10cSrcweir "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned."); 521cdf0e10cSrcweir sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) ); 522cdf0e10cSrcweir (void)bOK; 523cdf0e10cSrcweir #else 524cdf0e10cSrcweir //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT 525cdf0e10cSrcweir ::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"); 526cdf0e10cSrcweir #endif 527cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_False ); 528cdf0e10cSrcweir 529cdf0e10cSrcweir } 530cdf0e10cSrcweir getLocalPort_003()531cdf0e10cSrcweir void getLocalPort_003() 532cdf0e10cSrcweir { 533cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 534cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL); 535cdf0e10cSrcweir 536cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 537cdf0e10cSrcweir 538cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 539cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 540cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 541cdf0e10cSrcweir ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), 542cdf0e10cSrcweir ::rtl::OUString::createFromAscii("34463"), 543cdf0e10cSrcweir "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned"); 544cdf0e10cSrcweir sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535); 545cdf0e10cSrcweir 546cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 547cdf0e10cSrcweir } 548cdf0e10cSrcweir 549cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getLocalPort ); 550cdf0e10cSrcweir CPPUNIT_TEST( getLocalPort_001 ); 551cdf0e10cSrcweir // LLA: CPPUNIT_TEST( getLocalPort_002 ); 552cdf0e10cSrcweir CPPUNIT_TEST( getLocalPort_003 ); 553cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 554cdf0e10cSrcweir 555cdf0e10cSrcweir }; // class getLocalPort 556cdf0e10cSrcweir 557cdf0e10cSrcweir 558cdf0e10cSrcweir /** testing the method: 559cdf0e10cSrcweir inline ::rtl::OUString SAL_CALL getLocalHost() const; 560cdf0e10cSrcweir 561cdf0e10cSrcweir 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; 562cdf0e10cSrcweir 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 563cdf0e10cSrcweir will return hostname of current processor such as "aegean.PRC.Sun.COM" 564cdf0e10cSrcweir */ 565cdf0e10cSrcweir 566cdf0e10cSrcweir class getLocalHost : public CppUnit::TestFixture 567cdf0e10cSrcweir { 568cdf0e10cSrcweir public: 569cdf0e10cSrcweir oslSocket sHandle; 570cdf0e10cSrcweir // initialization setUp()571cdf0e10cSrcweir void setUp( ) 572cdf0e10cSrcweir { 573cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 574cdf0e10cSrcweir } 575cdf0e10cSrcweir tearDown()576cdf0e10cSrcweir void tearDown( ) 577cdf0e10cSrcweir { 578cdf0e10cSrcweir sHandle = NULL; 579cdf0e10cSrcweir } 580cdf0e10cSrcweir 581cdf0e10cSrcweir getLocalHost_001()582cdf0e10cSrcweir void getLocalHost_001() 583cdf0e10cSrcweir { 584cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 585cdf0e10cSrcweir //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu 586cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT6 ); 587cdf0e10cSrcweir 588cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 589cdf0e10cSrcweir 590cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 591cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 592cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 593cdf0e10cSrcweir sal_Bool bOK; 594cdf0e10cSrcweir ::rtl::OUString suError; 595cdf0e10cSrcweir #ifdef WNT 596cdf0e10cSrcweir bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; 597cdf0e10cSrcweir suError = outputError(sSocket.getLocalHost( ), getThisHostname( ), 598cdf0e10cSrcweir "test for getLocalHost function: create localhost socket and check name"); 599cdf0e10cSrcweir #else 600cdf0e10cSrcweir ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" ); 601cdf0e10cSrcweir sal_Bool bRes1, bRes2; 602cdf0e10cSrcweir bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ; 603cdf0e10cSrcweir bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ; 604cdf0e10cSrcweir bOK = bRes1 || bRes2; 605cdf0e10cSrcweir suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name"); 606cdf0e10cSrcweir #endif 607cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 608cdf0e10cSrcweir } 609cdf0e10cSrcweir getLocalHost_002()610cdf0e10cSrcweir void getLocalHost_002() 611cdf0e10cSrcweir { 612cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 613cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_POP3); 614cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 615cdf0e10cSrcweir 616cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 617cdf0e10cSrcweir sSocket.bind( saBindSocketAddr ); 618cdf0e10cSrcweir //Invalid IP, so bind should fail 619cdf0e10cSrcweir sal_Bool bOK = compareUString( sSocket.getLocalHost( ), rtl::OUString::createFromAscii("") ) ; 620cdf0e10cSrcweir ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), rtl::OUString::createFromAscii(""), "test for getLocalHost function: getLocalHost with invalid SocketAddr"); 621cdf0e10cSrcweir 622cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 623cdf0e10cSrcweir } 624cdf0e10cSrcweir 625cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getLocalHost ); 626cdf0e10cSrcweir CPPUNIT_TEST( getLocalHost_001 ); 627cdf0e10cSrcweir CPPUNIT_TEST( getLocalHost_002 ); 628cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 629cdf0e10cSrcweir 630cdf0e10cSrcweir }; // class getLocalHost 631cdf0e10cSrcweir 632cdf0e10cSrcweir 633cdf0e10cSrcweir /** testing the methods: 634cdf0e10cSrcweir inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const; 635cdf0e10cSrcweir inline sal_Int32 SAL_CALL getPeerPort() const; 636cdf0e10cSrcweir inline ::rtl::OUString SAL_CALL getPeerHost() const; 637cdf0e10cSrcweir */ 638cdf0e10cSrcweir class getPeer : public CppUnit::TestFixture 639cdf0e10cSrcweir { 640cdf0e10cSrcweir public: 641cdf0e10cSrcweir oslSocket sHandle; 642cdf0e10cSrcweir TimeValue *pTimeout; 643cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 644cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 645cdf0e10cSrcweir 646cdf0e10cSrcweir 647cdf0e10cSrcweir // initialization setUp()648cdf0e10cSrcweir void setUp( ) 649cdf0e10cSrcweir { 650cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 651cdf0e10cSrcweir pTimeout->Seconds = 3; 652cdf0e10cSrcweir pTimeout->Nanosec = 0; 653cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 654cdf0e10cSrcweir } 655cdf0e10cSrcweir tearDown()656cdf0e10cSrcweir void tearDown( ) 657cdf0e10cSrcweir { 658cdf0e10cSrcweir free( pTimeout ); 659cdf0e10cSrcweir sHandle = NULL; 660cdf0e10cSrcweir asAcceptorSocket.close( ); 661cdf0e10cSrcweir csConnectorSocket.close( ); 662cdf0e10cSrcweir } 663cdf0e10cSrcweir 664cdf0e10cSrcweir getPeer_001()665cdf0e10cSrcweir void getPeer_001() 666cdf0e10cSrcweir { 667cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 668cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 669cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 670cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 671cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 672cdf0e10cSrcweir /// launch server socket 673cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 674cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind '127.0.0.1' address failed.", sal_True == bOK1 ); 675cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 676cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 677cdf0e10cSrcweir 678cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 679cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 680cdf0e10cSrcweir 681cdf0e10cSrcweir /// launch client socket 682cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 683cdf0e10cSrcweir 684cdf0e10cSrcweir /// get peer information 685cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected. 686cdf0e10cSrcweir sal_Int32 peerPort = csConnectorSocket.getPeerPort( ); 687cdf0e10cSrcweir ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( ); 688cdf0e10cSrcweir 689cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getPeer function: setup a connection and then get the peer address, port and host from client side.", 690cdf0e10cSrcweir ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&& 691cdf0e10cSrcweir ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) && 692cdf0e10cSrcweir ( peerPort == saLocalSocketAddr.getPort( ) )); 693cdf0e10cSrcweir } 694cdf0e10cSrcweir 695cdf0e10cSrcweir 696cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getPeer ); 697cdf0e10cSrcweir CPPUNIT_TEST( getPeer_001 ); 698cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 699cdf0e10cSrcweir 700cdf0e10cSrcweir }; // class getPeer 701cdf0e10cSrcweir 702cdf0e10cSrcweir 703cdf0e10cSrcweir /** testing the methods: 704cdf0e10cSrcweir inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface); 705cdf0e10cSrcweir */ 706cdf0e10cSrcweir 707cdf0e10cSrcweir 708cdf0e10cSrcweir class bind : public CppUnit::TestFixture 709cdf0e10cSrcweir { 710cdf0e10cSrcweir public: 711cdf0e10cSrcweir oslSocket sHandle; 712cdf0e10cSrcweir // initialization setUp()713cdf0e10cSrcweir void setUp( ) 714cdf0e10cSrcweir { 715cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 716cdf0e10cSrcweir } 717cdf0e10cSrcweir tearDown()718cdf0e10cSrcweir void tearDown( ) 719cdf0e10cSrcweir { 720cdf0e10cSrcweir sHandle = NULL; 721cdf0e10cSrcweir } 722cdf0e10cSrcweir 723cdf0e10cSrcweir bind_001()724cdf0e10cSrcweir void bind_001() 725cdf0e10cSrcweir { 726cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 727cdf0e10cSrcweir //bind must use local IP address ---mindyliu 728cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 ); 729cdf0e10cSrcweir 730cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 731cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 732cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "Socket bind fail.", sal_True == bOK1 ); 733cdf0e10cSrcweir 734cdf0e10cSrcweir sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ; 735cdf0e10cSrcweir 736cdf0e10cSrcweir sSocket.close(); 737cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", sal_True == bOK2 ); 738cdf0e10cSrcweir } 739cdf0e10cSrcweir bind_002()740cdf0e10cSrcweir void bind_002() 741cdf0e10cSrcweir { 742cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 743cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_NETBIOS ); 744cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 745cdf0e10cSrcweir 746cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True); 747cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 748cdf0e10cSrcweir sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; 749cdf0e10cSrcweir 750cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", 751cdf0e10cSrcweir ( sal_False == bOK1 ) && ( sal_False == bOK2 ) ); 752cdf0e10cSrcweir } 753cdf0e10cSrcweir 754cdf0e10cSrcweir CPPUNIT_TEST_SUITE( bind ); 755cdf0e10cSrcweir CPPUNIT_TEST( bind_001 ); 756cdf0e10cSrcweir CPPUNIT_TEST( bind_002 ); 757cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 758cdf0e10cSrcweir 759cdf0e10cSrcweir }; // class bind 760cdf0e10cSrcweir 761cdf0e10cSrcweir 762cdf0e10cSrcweir /** testing the methods: 763cdf0e10cSrcweir inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const; 764cdf0e10cSrcweir 765cdf0e10cSrcweir */ 766cdf0e10cSrcweir class isRecvReady : public CppUnit::TestFixture 767cdf0e10cSrcweir { 768cdf0e10cSrcweir public: 769cdf0e10cSrcweir oslSocket sHandle; 770cdf0e10cSrcweir TimeValue *pTimeout; 771cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 772cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 773cdf0e10cSrcweir 774cdf0e10cSrcweir 775cdf0e10cSrcweir // initialization setUp()776cdf0e10cSrcweir void setUp( ) 777cdf0e10cSrcweir { 778cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 779cdf0e10cSrcweir pTimeout->Seconds = 3; 780cdf0e10cSrcweir pTimeout->Nanosec = 0; 781cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 782cdf0e10cSrcweir } 783cdf0e10cSrcweir tearDown()784cdf0e10cSrcweir void tearDown( ) 785cdf0e10cSrcweir { 786cdf0e10cSrcweir free( pTimeout ); 787cdf0e10cSrcweir sHandle = NULL; 788cdf0e10cSrcweir asAcceptorSocket.close( ); 789cdf0e10cSrcweir csConnectorSocket.close( ); 790cdf0e10cSrcweir } 791cdf0e10cSrcweir 792cdf0e10cSrcweir isRecvReady_001()793cdf0e10cSrcweir void isRecvReady_001() 794cdf0e10cSrcweir { 795cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 ); 796cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 ); 797cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 798cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 799cdf0e10cSrcweir /// launch server socket 800cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 801cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 802cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 803cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 804cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 805cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 806cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 807cdf0e10cSrcweir 808cdf0e10cSrcweir /// launch client socket 809cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 810cdf0e10cSrcweir 811cdf0e10cSrcweir /// is receive ready? 812cdf0e10cSrcweir sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout ); 813cdf0e10cSrcweir 814cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for isRecvReady function: setup a connection and then check if it can transmit data.", 815cdf0e10cSrcweir ( sal_True == bOK3 ) ); 816cdf0e10cSrcweir } 817cdf0e10cSrcweir 818cdf0e10cSrcweir 819cdf0e10cSrcweir CPPUNIT_TEST_SUITE( isRecvReady ); 820cdf0e10cSrcweir CPPUNIT_TEST( isRecvReady_001 ); 821cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 822cdf0e10cSrcweir 823cdf0e10cSrcweir }; // class isRecvReady 824cdf0e10cSrcweir 825cdf0e10cSrcweir 826cdf0e10cSrcweir /** testing the methods: 827cdf0e10cSrcweir inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const; 828cdf0e10cSrcweir */ 829cdf0e10cSrcweir class isSendReady : public CppUnit::TestFixture 830cdf0e10cSrcweir { 831cdf0e10cSrcweir public: 832cdf0e10cSrcweir oslSocket sHandle; 833cdf0e10cSrcweir TimeValue *pTimeout; 834cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 835cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 836cdf0e10cSrcweir 837cdf0e10cSrcweir 838cdf0e10cSrcweir // initialization setUp()839cdf0e10cSrcweir void setUp( ) 840cdf0e10cSrcweir { 841cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 842cdf0e10cSrcweir pTimeout->Seconds = 3; 843cdf0e10cSrcweir pTimeout->Nanosec = 0; 844cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 845cdf0e10cSrcweir } 846cdf0e10cSrcweir tearDown()847cdf0e10cSrcweir void tearDown( ) 848cdf0e10cSrcweir { 849cdf0e10cSrcweir free( pTimeout ); 850cdf0e10cSrcweir sHandle = NULL; 851cdf0e10cSrcweir asAcceptorSocket.close( ); 852cdf0e10cSrcweir csConnectorSocket.close( ); 853cdf0e10cSrcweir } 854cdf0e10cSrcweir 855cdf0e10cSrcweir isSendReady_001()856cdf0e10cSrcweir void isSendReady_001() 857cdf0e10cSrcweir { 858cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 859cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 860cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 861cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 862cdf0e10cSrcweir 863cdf0e10cSrcweir /// launch server socket 864cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 865cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 866cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 867cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 868cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 869cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 870cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 871cdf0e10cSrcweir 872cdf0e10cSrcweir /// launch client socket 873cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 874cdf0e10cSrcweir 875cdf0e10cSrcweir /// is send ready? 876cdf0e10cSrcweir sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout ); 877cdf0e10cSrcweir 878cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for isSendReady function: setup a connection and then check if it can transmit data.", 879cdf0e10cSrcweir ( sal_True == bOK3 ) ); 880cdf0e10cSrcweir } 881cdf0e10cSrcweir 882cdf0e10cSrcweir 883cdf0e10cSrcweir CPPUNIT_TEST_SUITE( isSendReady ); 884cdf0e10cSrcweir CPPUNIT_TEST( isSendReady_001 ); 885cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 886cdf0e10cSrcweir 887cdf0e10cSrcweir }; // class isSendReady 888cdf0e10cSrcweir 889cdf0e10cSrcweir 890cdf0e10cSrcweir /** testing the methods: 891cdf0e10cSrcweir inline oslSocketType SAL_CALL getType() const; 892cdf0e10cSrcweir 893cdf0e10cSrcweir */ 894cdf0e10cSrcweir 895cdf0e10cSrcweir class getType : public CppUnit::TestFixture 896cdf0e10cSrcweir { 897cdf0e10cSrcweir public: 898cdf0e10cSrcweir oslSocket sHandle; 899cdf0e10cSrcweir // initialization setUp()900cdf0e10cSrcweir void setUp( ) 901cdf0e10cSrcweir { 902cdf0e10cSrcweir 903cdf0e10cSrcweir } 904cdf0e10cSrcweir tearDown()905cdf0e10cSrcweir void tearDown( ) 906cdf0e10cSrcweir { 907cdf0e10cSrcweir sHandle = NULL; 908cdf0e10cSrcweir } 909cdf0e10cSrcweir 910cdf0e10cSrcweir getType_001()911cdf0e10cSrcweir void getType_001() 912cdf0e10cSrcweir { 913cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 914cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 915cdf0e10cSrcweir 916cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", 917cdf0e10cSrcweir osl_Socket_TypeStream == sSocket.getType( ) ); 918cdf0e10cSrcweir } 919cdf0e10cSrcweir getType_002()920cdf0e10cSrcweir void getType_002() 921cdf0e10cSrcweir { 922cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 923cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 924cdf0e10cSrcweir 925cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", 926cdf0e10cSrcweir osl_Socket_TypeDgram == sSocket.getType( ) ); 927cdf0e10cSrcweir } 928cdf0e10cSrcweir 929cdf0e10cSrcweir #ifdef UNX 930cdf0e10cSrcweir // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here 931cdf0e10cSrcweir // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw() getType_003()932cdf0e10cSrcweir void getType_003() 933cdf0e10cSrcweir { 934cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.this is not passed in (LINUX, SOLARIS), the osl_Socket_TypeRaw, type socket can not be created.", 935cdf0e10cSrcweir sal_True); 936cdf0e10cSrcweir } 937cdf0e10cSrcweir #else getType_003()938cdf0e10cSrcweir void getType_003() 939cdf0e10cSrcweir { 940cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 941cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 942cdf0e10cSrcweir 943cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", 944cdf0e10cSrcweir osl_Socket_TypeRaw == sSocket.getType( ) ); 945cdf0e10cSrcweir } 946cdf0e10cSrcweir #endif 947cdf0e10cSrcweir 948cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getType ); 949cdf0e10cSrcweir CPPUNIT_TEST( getType_001 ); 950cdf0e10cSrcweir CPPUNIT_TEST( getType_002 ); 951cdf0e10cSrcweir CPPUNIT_TEST( getType_003 ); 952cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 953cdf0e10cSrcweir 954cdf0e10cSrcweir }; // class getType 955cdf0e10cSrcweir 956cdf0e10cSrcweir 957cdf0e10cSrcweir 958cdf0e10cSrcweir /** testing the methods: 959cdf0e10cSrcweir inline sal_Int32 SAL_CALL getOption( 960cdf0e10cSrcweir oslSocketOption Option, 961cdf0e10cSrcweir void* pBuffer, 962cdf0e10cSrcweir sal_uInt32 BufferLen, 963cdf0e10cSrcweir oslSocketOptionLevel Level= osl_Socket_LevelSocket) const; 964cdf0e10cSrcweir 965cdf0e10cSrcweir inline sal_Int32 getOption( oslSocketOption option ) const; 966cdf0e10cSrcweir 967cdf0e10cSrcweir */ 968cdf0e10cSrcweir 969cdf0e10cSrcweir class getOption : public CppUnit::TestFixture 970cdf0e10cSrcweir { 971cdf0e10cSrcweir public: 972cdf0e10cSrcweir oslSocket sHandle; 973cdf0e10cSrcweir // initialization setUp()974cdf0e10cSrcweir void setUp( ) 975cdf0e10cSrcweir { 976cdf0e10cSrcweir 977cdf0e10cSrcweir } 978cdf0e10cSrcweir tearDown()979cdf0e10cSrcweir void tearDown( ) 980cdf0e10cSrcweir { 981cdf0e10cSrcweir sHandle = NULL; 982cdf0e10cSrcweir } 983cdf0e10cSrcweir 984cdf0e10cSrcweir /** test writer's comment: 985cdf0e10cSrcweir 986cdf0e10cSrcweir in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream. 987cdf0e10cSrcweir 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ 988cdf0e10cSrcweir in 1. 989cdf0e10cSrcweir */ 990cdf0e10cSrcweir getOption_001()991cdf0e10cSrcweir void getOption_001() 992cdf0e10cSrcweir { 993cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 994cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 995cdf0e10cSrcweir sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 996cdf0e10cSrcweir *pType = 0; 997cdf0e10cSrcweir sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) ); 998cdf0e10cSrcweir sal_Bool bOK = ( SOCK_STREAM == *pType ); 999cdf0e10cSrcweir // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream 1000cdf0e10cSrcweir //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType ); 1001cdf0e10cSrcweir free( pType ); 1002cdf0e10cSrcweir 1003cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get type option of socket.", 1004cdf0e10cSrcweir sal_True == bOK ); 1005cdf0e10cSrcweir } 1006cdf0e10cSrcweir 1007cdf0e10cSrcweir // getsockopt error getOption_004()1008cdf0e10cSrcweir void getOption_004() 1009cdf0e10cSrcweir { 1010cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 1011cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1012cdf0e10cSrcweir 1013cdf0e10cSrcweir sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 1014cdf0e10cSrcweir sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) ); 1015cdf0e10cSrcweir free( pbDontRoute ); 1016cdf0e10cSrcweir 1017cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get invalid option of socket, should return -1.", 1018cdf0e10cSrcweir nRes == -1 ); 1019cdf0e10cSrcweir } 1020cdf0e10cSrcweir getOption_simple_001()1021cdf0e10cSrcweir void getOption_simple_001() 1022cdf0e10cSrcweir { 1023cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 1024cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1025cdf0e10cSrcweir 1026cdf0e10cSrcweir sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) ); 1027cdf0e10cSrcweir 1028cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.", 1029cdf0e10cSrcweir sal_True == bOK ); 1030cdf0e10cSrcweir } 1031cdf0e10cSrcweir getOption_simple_002()1032cdf0e10cSrcweir void getOption_simple_002() 1033cdf0e10cSrcweir { 1034cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 1035cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1036cdf0e10cSrcweir 1037cdf0e10cSrcweir sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) ); 1038cdf0e10cSrcweir 1039cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.", 1040cdf0e10cSrcweir sal_True == bOK ); 1041cdf0e10cSrcweir } 1042cdf0e10cSrcweir 1043cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getOption ); 1044cdf0e10cSrcweir CPPUNIT_TEST( getOption_001 ); 1045cdf0e10cSrcweir CPPUNIT_TEST( getOption_004 ); 1046cdf0e10cSrcweir CPPUNIT_TEST( getOption_simple_001 ); 1047cdf0e10cSrcweir CPPUNIT_TEST( getOption_simple_002 ); 1048cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1049cdf0e10cSrcweir 1050cdf0e10cSrcweir }; // class getOption 1051cdf0e10cSrcweir 1052cdf0e10cSrcweir 1053cdf0e10cSrcweir /** testing the methods: 1054cdf0e10cSrcweir inline sal_Bool SAL_CALL setOption( oslSocketOption Option, 1055cdf0e10cSrcweir void* pBuffer, 1056cdf0e10cSrcweir sal_uInt32 BufferLen, 1057cdf0e10cSrcweir oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const; 1058cdf0e10cSrcweir */ 1059cdf0e10cSrcweir 1060cdf0e10cSrcweir class setOption : public CppUnit::TestFixture 1061cdf0e10cSrcweir { 1062cdf0e10cSrcweir public: 1063cdf0e10cSrcweir TimeValue *pTimeout; 1064cdf0e10cSrcweir // LLA: maybe there is an error in the source, 1065cdf0e10cSrcweir // as long as I remember, if a derived class do not overload all ctors there is a problem. 1066cdf0e10cSrcweir 1067cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 1068cdf0e10cSrcweir setUp()1069cdf0e10cSrcweir void setUp( ) 1070cdf0e10cSrcweir { 1071cdf0e10cSrcweir 1072cdf0e10cSrcweir } 1073cdf0e10cSrcweir tearDown()1074cdf0e10cSrcweir void tearDown( ) 1075cdf0e10cSrcweir { 1076cdf0e10cSrcweir asAcceptorSocket.close( ); 1077cdf0e10cSrcweir } 1078cdf0e10cSrcweir 1079cdf0e10cSrcweir 1080cdf0e10cSrcweir // LLA: 1081cdf0e10cSrcweir // getSocketOption returns BufferLen, or -1 if something failed 1082cdf0e10cSrcweir 1083cdf0e10cSrcweir // setSocketOption returns sal_True, if option could stored 1084cdf0e10cSrcweir // else sal_False 1085cdf0e10cSrcweir setOption_001()1086cdf0e10cSrcweir void setOption_001() 1087cdf0e10cSrcweir { 1088cdf0e10cSrcweir /// set and get option. 1089cdf0e10cSrcweir int nBufferLen = sizeof ( sal_Int32); 1090cdf0e10cSrcweir // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool! 1091cdf0e10cSrcweir 1092cdf0e10cSrcweir sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 1093cdf0e10cSrcweir *pbDontRouteSet = 1; // sal_True; 1094cdf0e10cSrcweir 1095cdf0e10cSrcweir sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 1096cdf0e10cSrcweir *pGetBuffer = 0; 1097cdf0e10cSrcweir 1098cdf0e10cSrcweir // maybe asAcceptorSocket is not right initialized 1099cdf0e10cSrcweir sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen ); 1100cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b1 ) ); 1101cdf0e10cSrcweir sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); 1102cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "getOption function failed.", ( n2 == nBufferLen ) ); 1103cdf0e10cSrcweir 1104cdf0e10cSrcweir // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value, 1105cdf0e10cSrcweir // just judge it is zero or not! 1106cdf0e10cSrcweir sal_Bool bOK = ( 0 != *pGetBuffer ); 1107cdf0e10cSrcweir t_print("#setOption_001: getOption is %d \n", *pGetBuffer); 1108cdf0e10cSrcweir 1109cdf0e10cSrcweir // toggle check, set to 0 1110cdf0e10cSrcweir *pbDontRouteSet = 0; 1111cdf0e10cSrcweir 1112cdf0e10cSrcweir sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) ); 1113cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b3 ) ); 1114cdf0e10cSrcweir sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); 1115cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "getOption (DONTROUTE) function failed.", ( n4 == nBufferLen ) ); 1116cdf0e10cSrcweir 1117cdf0e10cSrcweir sal_Bool bOK2 = ( 0 == *pGetBuffer ); 1118cdf0e10cSrcweir 1119cdf0e10cSrcweir t_print("#setOption_001: getOption is %d \n", *pGetBuffer); 1120cdf0e10cSrcweir 1121cdf0e10cSrcweir // LLA: sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 1122cdf0e10cSrcweir // LLA: *pbDontTouteSet = sal_True; 1123cdf0e10cSrcweir // LLA: sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 1124cdf0e10cSrcweir // LLA: *pbDontTouteGet = sal_False; 1125cdf0e10cSrcweir // LLA: asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontTouteSet, sizeof ( sal_Bool ) ); 1126cdf0e10cSrcweir // LLA: asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pbDontTouteGet, sizeof ( sal_Bool ) ); 1127cdf0e10cSrcweir // LLA: ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet), 1128cdf0e10cSrcweir // LLA: ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet), 1129cdf0e10cSrcweir // LLA: "test for setOption function: set osl_Socket_OptionDontRoute and then check"); 1130cdf0e10cSrcweir // LLA: 1131cdf0e10cSrcweir // LLA: sal_Bool bOK = ( sal_True == *pbDontTouteGet ); 1132cdf0e10cSrcweir // LLA: free( pbDontTouteSet ); 1133cdf0e10cSrcweir // LLA: free( pbDontTouteGet ); 1134cdf0e10cSrcweir 1135cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", 1136cdf0e10cSrcweir ( sal_True == bOK ) && (sal_True == bOK2) ); 1137cdf0e10cSrcweir 1138cdf0e10cSrcweir free( pbDontRouteSet ); 1139cdf0e10cSrcweir free( pGetBuffer ); 1140cdf0e10cSrcweir // LLA: CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 1141cdf0e10cSrcweir } 1142cdf0e10cSrcweir setOption_002()1143cdf0e10cSrcweir void setOption_002() 1144cdf0e10cSrcweir { 1145cdf0e10cSrcweir /// set and get option. 1146cdf0e10cSrcweir 1147cdf0e10cSrcweir // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen ); 1148cdf0e10cSrcweir // *pbLingerSet = 7; 1149cdf0e10cSrcweir // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen ); 1150cdf0e10cSrcweir /* struct */linger aLingerSet; 1151cdf0e10cSrcweir sal_Int32 nBufferLen = sizeof( struct linger ); 1152cdf0e10cSrcweir aLingerSet.l_onoff = 1; 1153cdf0e10cSrcweir aLingerSet.l_linger = 7; 1154cdf0e10cSrcweir 1155cdf0e10cSrcweir linger aLingerGet; 1156cdf0e10cSrcweir 1157cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen ); 1158cdf0e10cSrcweir 1159cdf0e10cSrcweir sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen ); 1160cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "getOption (SO_LINGER) function failed.", ( n1 == nBufferLen ) ); 1161cdf0e10cSrcweir 1162cdf0e10cSrcweir //t_print("#setOption_002: getOption is %d \n", aLingerGet.l_linger); 1163cdf0e10cSrcweir sal_Bool bOK = ( 7 == aLingerGet.l_linger ); 1164cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check. ", 1165cdf0e10cSrcweir sal_True == bOK ); 1166cdf0e10cSrcweir 1167cdf0e10cSrcweir } 1168cdf0e10cSrcweir setOption_003()1169cdf0e10cSrcweir void setOption_003() 1170cdf0e10cSrcweir { 1171cdf0e10cSrcweir linger aLingerSet; 1172cdf0e10cSrcweir aLingerSet.l_onoff = 1; 1173cdf0e10cSrcweir aLingerSet.l_linger = 7; 1174cdf0e10cSrcweir 1175cdf0e10cSrcweir sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 ); 1176cdf0e10cSrcweir printUString( asAcceptorSocket.getErrorAsString( ) ); 1177cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "setOption (SO_LINGER) function failed for optlen is 0.", 1178cdf0e10cSrcweir ( b1 == sal_False ) ); 1179cdf0e10cSrcweir } 1180cdf0e10cSrcweir setOption_simple_001()1181cdf0e10cSrcweir void setOption_simple_001() 1182cdf0e10cSrcweir { 1183cdf0e10cSrcweir /// set and get option. 1184cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True ); 1185cdf0e10cSrcweir sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); 1186cdf0e10cSrcweir 1187cdf0e10cSrcweir t_print("setOption_simple_001(): getoption is %d \n", asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); 1188cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", 1189cdf0e10cSrcweir ( sal_True == bOK ) ); 1190cdf0e10cSrcweir } 1191cdf0e10cSrcweir setOption_simple_002()1192cdf0e10cSrcweir void setOption_simple_002() 1193cdf0e10cSrcweir { 1194cdf0e10cSrcweir /// set and get option. 1195cdf0e10cSrcweir // LLA: this does not work, due to the fact that SO_LINGER is a structure 1196cdf0e10cSrcweir // LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 ); 1197cdf0e10cSrcweir // LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) ); 1198cdf0e10cSrcweir 1199cdf0e10cSrcweir // LLA: CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", 1200cdf0e10cSrcweir // LLA: ( sal_True == bOK ) ); 1201cdf0e10cSrcweir } 1202cdf0e10cSrcweir 1203cdf0e10cSrcweir CPPUNIT_TEST_SUITE( setOption ); 1204cdf0e10cSrcweir CPPUNIT_TEST( setOption_001 ); 1205cdf0e10cSrcweir CPPUNIT_TEST( setOption_002 ); 1206cdf0e10cSrcweir CPPUNIT_TEST( setOption_003 ); 1207cdf0e10cSrcweir CPPUNIT_TEST( setOption_simple_001 ); 1208cdf0e10cSrcweir // LLA: CPPUNIT_TEST( setOption_simple_002 ); 1209cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1210cdf0e10cSrcweir 1211cdf0e10cSrcweir }; // class setOption 1212cdf0e10cSrcweir 1213cdf0e10cSrcweir 1214cdf0e10cSrcweir 1215cdf0e10cSrcweir /** testing the method: 1216cdf0e10cSrcweir inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode); 1217cdf0e10cSrcweir */ 1218cdf0e10cSrcweir class enableNonBlockingMode : public CppUnit::TestFixture 1219cdf0e10cSrcweir { 1220cdf0e10cSrcweir public: 1221cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 1222cdf0e10cSrcweir enableNonBlockingMode_001()1223cdf0e10cSrcweir void enableNonBlockingMode_001() 1224cdf0e10cSrcweir { 1225cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 1226cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 1227cdf0e10cSrcweir 1228cdf0e10cSrcweir /// launch server socket 1229cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1230cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 1231cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 1232cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 1233cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 1234cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 1235cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 1236cdf0e10cSrcweir 1237cdf0e10cSrcweir /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. 1238cdf0e10cSrcweir sal_Bool bOK = sal_True; 1239cdf0e10cSrcweir asAcceptorSocket.close( ); 1240cdf0e10cSrcweir 1241cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for enableNonBlockingMode function: launch a server socket and make it non blocking. if it can pass the acceptConnection statement, it is non-blocking", 1242cdf0e10cSrcweir ( sal_True == bOK ) ); 1243cdf0e10cSrcweir } 1244cdf0e10cSrcweir 1245cdf0e10cSrcweir 1246cdf0e10cSrcweir CPPUNIT_TEST_SUITE( enableNonBlockingMode ); 1247cdf0e10cSrcweir CPPUNIT_TEST( enableNonBlockingMode_001 ); 1248cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1249cdf0e10cSrcweir 1250cdf0e10cSrcweir }; // class enableNonBlockingMode 1251cdf0e10cSrcweir 1252cdf0e10cSrcweir 1253cdf0e10cSrcweir /** testing the method: 1254cdf0e10cSrcweir inline sal_Bool SAL_CALL isNonBlockingMode() const; 1255cdf0e10cSrcweir */ 1256cdf0e10cSrcweir class isNonBlockingMode : public CppUnit::TestFixture 1257cdf0e10cSrcweir { 1258cdf0e10cSrcweir public: 1259cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 1260cdf0e10cSrcweir isNonBlockingMode_001()1261cdf0e10cSrcweir void isNonBlockingMode_001() 1262cdf0e10cSrcweir { 1263cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 1264cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 1265cdf0e10cSrcweir 1266cdf0e10cSrcweir /// launch server socket 1267cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 1268cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 1269cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 1270cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 1271cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 1272cdf0e10cSrcweir 1273cdf0e10cSrcweir sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( ); 1274cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 1275cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 1276cdf0e10cSrcweir 1277cdf0e10cSrcweir /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. 1278cdf0e10cSrcweir sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( ); 1279cdf0e10cSrcweir asAcceptorSocket.close( ); 1280cdf0e10cSrcweir 1281cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for isNonBlockingMode function: launch a server socket and make it non blocking. it is expected to change from blocking mode to non-blocking mode.", 1282cdf0e10cSrcweir ( sal_False == bOK3 ) && ( sal_True == bOK4 ) ); 1283cdf0e10cSrcweir } 1284cdf0e10cSrcweir 1285cdf0e10cSrcweir 1286cdf0e10cSrcweir CPPUNIT_TEST_SUITE( isNonBlockingMode ); 1287cdf0e10cSrcweir CPPUNIT_TEST( isNonBlockingMode_001 ); 1288cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1289cdf0e10cSrcweir 1290cdf0e10cSrcweir }; // class isNonBlockingMode 1291cdf0e10cSrcweir 1292cdf0e10cSrcweir /** testing the method: 1293cdf0e10cSrcweir inline void SAL_CALL clearError() const; 1294cdf0e10cSrcweir */ 1295cdf0e10cSrcweir class clearError : public CppUnit::TestFixture 1296cdf0e10cSrcweir { 1297cdf0e10cSrcweir public: 1298cdf0e10cSrcweir oslSocket sHandle; 1299cdf0e10cSrcweir // initialization setUp()1300cdf0e10cSrcweir void setUp( ) 1301cdf0e10cSrcweir { 1302cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1303cdf0e10cSrcweir } 1304cdf0e10cSrcweir tearDown()1305cdf0e10cSrcweir void tearDown( ) 1306cdf0e10cSrcweir { 1307cdf0e10cSrcweir sHandle = NULL; 1308cdf0e10cSrcweir } 1309cdf0e10cSrcweir 1310cdf0e10cSrcweir clearError_001()1311cdf0e10cSrcweir void clearError_001() 1312cdf0e10cSrcweir { 1313cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1314cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_HTTP2 ); 1315cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 1316cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1317cdf0e10cSrcweir sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" 1318cdf0e10cSrcweir oslSocketError seBind = sSocket.getError( ); 1319cdf0e10cSrcweir sSocket.clearError( ); 1320cdf0e10cSrcweir 1321cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.", 1322cdf0e10cSrcweir osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None ); 1323cdf0e10cSrcweir } 1324cdf0e10cSrcweir 1325cdf0e10cSrcweir 1326cdf0e10cSrcweir CPPUNIT_TEST_SUITE( clearError ); 1327cdf0e10cSrcweir CPPUNIT_TEST( clearError_001 ); 1328cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1329cdf0e10cSrcweir 1330cdf0e10cSrcweir }; // class clearError 1331cdf0e10cSrcweir 1332cdf0e10cSrcweir 1333cdf0e10cSrcweir /** testing the methods: 1334cdf0e10cSrcweir inline oslSocketError getError() const; 1335cdf0e10cSrcweir inline ::rtl::OUString getErrorAsString( ) const; 1336cdf0e10cSrcweir */ 1337cdf0e10cSrcweir class getError : public CppUnit::TestFixture 1338cdf0e10cSrcweir { 1339cdf0e10cSrcweir public: 1340cdf0e10cSrcweir oslSocket sHandle; 1341cdf0e10cSrcweir // initialization setUp()1342cdf0e10cSrcweir void setUp( ) 1343cdf0e10cSrcweir { 1344cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1345cdf0e10cSrcweir } 1346cdf0e10cSrcweir tearDown()1347cdf0e10cSrcweir void tearDown( ) 1348cdf0e10cSrcweir { 1349cdf0e10cSrcweir sHandle = NULL; 1350cdf0e10cSrcweir } 1351cdf0e10cSrcweir 1352cdf0e10cSrcweir getError_001()1353cdf0e10cSrcweir void getError_001() 1354cdf0e10cSrcweir { 1355cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1356cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 1357cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 1358cdf0e10cSrcweir 1359cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getError function: should get no error.", 1360cdf0e10cSrcweir osl_Socket_E_None == sSocket.getError( ) ); 1361cdf0e10cSrcweir } 1362cdf0e10cSrcweir getError_002()1363cdf0e10cSrcweir void getError_002() 1364cdf0e10cSrcweir { 1365cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1366cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_FTP ); 1367cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 1368cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1369cdf0e10cSrcweir sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" 1370cdf0e10cSrcweir //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError. 1371cdf0e10cSrcweir #if defined(SOLARIS) 1372cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. ", 1373cdf0e10cSrcweir osl_Socket_E_InvalidError == sSocket.getError( ) ); 1374cdf0e10cSrcweir #else 1375cdf0e10cSrcweir //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail. 1376cdf0e10cSrcweir 1377cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. Passed on Linux & Win32", 1378cdf0e10cSrcweir osl_Socket_E_AddrNotAvail == sSocket.getError( ) ); 1379cdf0e10cSrcweir #endif 1380cdf0e10cSrcweir } 1381cdf0e10cSrcweir 1382cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getError ); 1383cdf0e10cSrcweir CPPUNIT_TEST( getError_001 ); 1384cdf0e10cSrcweir CPPUNIT_TEST( getError_002 ); 1385cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1386cdf0e10cSrcweir 1387cdf0e10cSrcweir }; // class getError 1388cdf0e10cSrcweir 1389cdf0e10cSrcweir 1390cdf0e10cSrcweir 1391cdf0e10cSrcweir /** testing the methods: 1392cdf0e10cSrcweir inline oslSocket getHandle() const; 1393cdf0e10cSrcweir */ 1394cdf0e10cSrcweir 1395cdf0e10cSrcweir class getHandle : public CppUnit::TestFixture 1396cdf0e10cSrcweir { 1397cdf0e10cSrcweir public: 1398cdf0e10cSrcweir oslSocket sHandle; 1399cdf0e10cSrcweir // initialization setUp()1400cdf0e10cSrcweir void setUp( ) 1401cdf0e10cSrcweir { 1402cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1403cdf0e10cSrcweir } 1404cdf0e10cSrcweir tearDown()1405cdf0e10cSrcweir void tearDown( ) 1406cdf0e10cSrcweir { 1407cdf0e10cSrcweir sHandle = NULL; 1408cdf0e10cSrcweir } 1409cdf0e10cSrcweir getHandle_001()1410cdf0e10cSrcweir void getHandle_001() 1411cdf0e10cSrcweir { 1412cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1413cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket.getHandle(); 1414cdf0e10cSrcweir 1415cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.", 1416cdf0e10cSrcweir osl_Socket_TypeStream == assignSocket.getType( ) ); 1417cdf0e10cSrcweir } 1418cdf0e10cSrcweir getHandle_002()1419cdf0e10cSrcweir void getHandle_002() 1420cdf0e10cSrcweir { 1421cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 1422cdf0e10cSrcweir ::osl::Socket assignSocket ( sSocket.getHandle( ) ); 1423cdf0e10cSrcweir 1424cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator", 1425cdf0e10cSrcweir osl_Socket_TypeStream == assignSocket.getType( ) ); 1426cdf0e10cSrcweir } 1427cdf0e10cSrcweir 1428cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getHandle ); 1429cdf0e10cSrcweir CPPUNIT_TEST( getHandle_001 ); 1430cdf0e10cSrcweir CPPUNIT_TEST( getHandle_002 ); 1431cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1432cdf0e10cSrcweir 1433cdf0e10cSrcweir }; // class getHandle 1434cdf0e10cSrcweir 1435cdf0e10cSrcweir 1436cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1437cdf0e10cSrcweir 1438cdf0e10cSrcweir 1439cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::ctors, "osl_Socket"); 1440cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::operators, "osl_Socket"); 1441cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::close, "osl_Socket"); 1442cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalAddr, "osl_Socket"); 1443cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalPort, "osl_Socket"); 1444cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalHost, "osl_Socket"); 1445cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getPeer, "osl_Socket"); 1446cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::bind, "osl_Socket"); 1447cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isRecvReady, "osl_Socket"); 1448cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isSendReady, "osl_Socket"); 1449cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getType, "osl_Socket"); 1450cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getOption, "osl_Socket"); 1451cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::setOption, "osl_Socket"); 1452cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::enableNonBlockingMode, "osl_Socket"); 1453cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isNonBlockingMode, "osl_Socket"); 1454cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::clearError, "osl_Socket"); 1455cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getError, "osl_Socket"); 1456cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getHandle, "osl_Socket"); 1457cdf0e10cSrcweir 1458cdf0e10cSrcweir } // namespace osl_Socket 1459cdf0e10cSrcweir 1460cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1461cdf0e10cSrcweir 1462cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions() 1463cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand. 1464cdf0e10cSrcweir NOADDITIONAL; 1465