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