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 //------------------------------------------------------------------------ 59*cdf0e10cSrcweir // include files 60*cdf0e10cSrcweir //------------------------------------------------------------------------ 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir #ifndef _OSL_SOCKET_CONST_H_ 63*cdf0e10cSrcweir #include <osl_Socket_Const_orig.h> 64*cdf0e10cSrcweir #endif 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir #include <testshl/simpleheader.hxx> 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir using namespace osl; 69*cdf0e10cSrcweir using namespace rtl; 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir //------------------------------------------------------------------------ 72*cdf0e10cSrcweir // helper functions 73*cdf0e10cSrcweir //------------------------------------------------------------------------ 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir /** compare two OUString. 76*cdf0e10cSrcweir */ 77*cdf0e10cSrcweir inline sal_Bool compareUString( const ::rtl::OUString & ustr1, const ::rtl::OUString & ustr2 ) 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir sal_Bool bOk = ustr1.equalsIgnoreAsciiCase( ustr2 ); 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir return bOk; 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir /** compare a OUString and an ASCII string. 85*cdf0e10cSrcweir */ 86*cdf0e10cSrcweir inline sal_Bool compareUString( const ::rtl::OUString & ustr, const sal_Char *astr ) 87*cdf0e10cSrcweir { 88*cdf0e10cSrcweir ::rtl::OUString ustr2 = rtl::OUString::createFromAscii( astr ); 89*cdf0e10cSrcweir sal_Bool bOk = ustr.equalsIgnoreAsciiCase( ustr2 ); 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir return bOk; 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir /** compare two socket address. 95*cdf0e10cSrcweir */ 96*cdf0e10cSrcweir inline sal_Bool compareSocketAddr( const ::osl::SocketAddr & addr1 , const ::osl::SocketAddr & addr2 ) 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir return ( ( sal_True == compareUString( addr1.getHostname( 0 ), addr2.getHostname( 0 ) ) ) && ( addr2.getPort( ) == addr2.getPort( ) ) ); 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir inline char * oustring2char( const ::rtl::OUString & str ) 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir rtl::OString aString; 104*cdf0e10cSrcweir aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ); 105*cdf0e10cSrcweir return (char *)aString.getStr( ); 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir /** print a UNI_CODE String. And also print some comments of the string. 109*cdf0e10cSrcweir */ 110*cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" ) 111*cdf0e10cSrcweir { 112*cdf0e10cSrcweir t_print("#%s #printUString_u# ", msg ); 113*cdf0e10cSrcweir t_print("%s\n", oustring2char( str ) ); 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir /** get the local host name. 117*cdf0e10cSrcweir mindy: gethostbyname( "localhost" ), on Linux, it returns the hostname in /etc/hosts + domain name, 118*cdf0e10cSrcweir if no entry in /etc/hosts, it returns "localhost" + domain name 119*cdf0e10cSrcweir */ 120*cdf0e10cSrcweir inline ::rtl::OUString getHost( void ) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir struct hostent *hptr; 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir hptr = gethostbyname( "localhost" ); 125*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "#In getHostname function, error on gethostbyname()", hptr != NULL ); 126*cdf0e10cSrcweir ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name ); 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir return aUString; 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir /** get the full host name of the current processor, such as "aegean.prc.sun.com" --mindyliu 132*cdf0e10cSrcweir */ 133*cdf0e10cSrcweir inline ::rtl::OUString getThisHostname( void ) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir ::rtl::OUString aUString; 136*cdf0e10cSrcweir #ifdef WNT 137*cdf0e10cSrcweir struct hostent *hptr; 138*cdf0e10cSrcweir hptr = gethostbyname( "localhost" ); 139*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "#In getHostname function, error on gethostbyname()", hptr != NULL ); 140*cdf0e10cSrcweir aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name ); 141*cdf0e10cSrcweir #else 142*cdf0e10cSrcweir char hostname[255]; 143*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "#Error: gethostname failed.", gethostname(hostname, 255) == 0 ); 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir struct hostent *hptr; 146*cdf0e10cSrcweir //first search /ets/hosts, then search from dns 147*cdf0e10cSrcweir hptr = gethostbyname( hostname); 148*cdf0e10cSrcweir if ( hptr != NULL ) 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir strcpy( hostname, hptr->h_name ); 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir t_print("hostname is %s \n", hostname ); 154*cdf0e10cSrcweir aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hostname ); 155*cdf0e10cSrcweir #endif 156*cdf0e10cSrcweir return aUString; 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir /** get IP by name, search /etc/hosts first, then search from dns, fail return OUString("") 160*cdf0e10cSrcweir */ 161*cdf0e10cSrcweir inline ::rtl::OUString getIPbyName( rtl::OString const& str_name ) 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir ::rtl::OUString aUString; 164*cdf0e10cSrcweir struct hostent *hptr; 165*cdf0e10cSrcweir //first search /ets/hosts, then search from dns 166*cdf0e10cSrcweir hptr = gethostbyname( str_name.getStr()); 167*cdf0e10cSrcweir if ( hptr != NULL ) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir struct in_addr ** addrptr; 170*cdf0e10cSrcweir addrptr = (struct in_addr **) hptr->h_addr_list ; 171*cdf0e10cSrcweir //if there are more than one IPs on the same machine, we select one 172*cdf0e10cSrcweir for (; *addrptr; addrptr++) 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir t_print("#Local IP Address: %s\n", inet_ntoa(**addrptr)); 175*cdf0e10cSrcweir aUString = ::rtl::OUString::createFromAscii( (sal_Char *) (inet_ntoa(**addrptr)) ); 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir return aUString; 179*cdf0e10cSrcweir } 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir /** get local ethernet IP 182*cdf0e10cSrcweir */ 183*cdf0e10cSrcweir inline ::rtl::OUString getLocalIP( ) 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir char hostname[255]; 186*cdf0e10cSrcweir gethostname(hostname, 255); 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir return getIPbyName( hostname ); 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir /** construct error message 192*cdf0e10cSrcweir */ 193*cdf0e10cSrcweir inline ::rtl::OUString outputError( const ::rtl::OUString & returnVal, const ::rtl::OUString & rightVal, const sal_Char * msg = "") 194*cdf0e10cSrcweir { 195*cdf0e10cSrcweir ::rtl::OUString aUString; 196*cdf0e10cSrcweir if ( returnVal.equals( rightVal ) ) 197*cdf0e10cSrcweir return aUString; 198*cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii(msg); 199*cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii(": the returned value is '"); 200*cdf0e10cSrcweir aUString += returnVal; 201*cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii("', but the value should be '"); 202*cdf0e10cSrcweir aUString += rightVal; 203*cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii("'."); 204*cdf0e10cSrcweir return aUString; 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir /** wait _nSec seconds. 208*cdf0e10cSrcweir */ 209*cdf0e10cSrcweir void thread_sleep( sal_Int32 _nSec ) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir /// print statement in thread process must use fflush() to force display. 212*cdf0e10cSrcweir t_print("# wait %d seconds. ", _nSec ); 213*cdf0e10cSrcweir fflush(stdout); 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir #ifdef WNT //Windows 216*cdf0e10cSrcweir Sleep( _nSec * 100 ); 217*cdf0e10cSrcweir #endif 218*cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 ) //Unix 219*cdf0e10cSrcweir usleep(_nSec * 100000); 220*cdf0e10cSrcweir #endif 221*cdf0e10cSrcweir t_print("# done\n" ); 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir /** print Boolean value. 225*cdf0e10cSrcweir */ 226*cdf0e10cSrcweir inline void printBool( sal_Bool bOk ) 227*cdf0e10cSrcweir { 228*cdf0e10cSrcweir t_print("#printBool# " ); 229*cdf0e10cSrcweir ( sal_True == bOk ) ? t_print("YES!\n" ): t_print("NO!\n" ); 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir /** print content of a ByteSequence. 233*cdf0e10cSrcweir */ 234*cdf0e10cSrcweir inline void printByteSequence_IP( const ::rtl::ByteSequence & bsByteSeq, sal_Int32 nLen ) 235*cdf0e10cSrcweir { 236*cdf0e10cSrcweir t_print("#ByteSequence is: " ); 237*cdf0e10cSrcweir for ( int i = 0; i < nLen; i++ ){ 238*cdf0e10cSrcweir if ( bsByteSeq[i] < 0 ) 239*cdf0e10cSrcweir t_print("%d ", 256 + bsByteSeq[i] ); 240*cdf0e10cSrcweir else 241*cdf0e10cSrcweir t_print("%d ", bsByteSeq[i] ); 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir t_print(" .\n" ); 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir /** convert an IP which is stored as a UString format to a ByteSequence array for later use. 247*cdf0e10cSrcweir */ 248*cdf0e10cSrcweir inline ::rtl::ByteSequence UStringIPToByteSequence( ::rtl::OUString aUStr ) 249*cdf0e10cSrcweir { 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir rtl::OString aString = ::rtl::OUStringToOString( aUStr, RTL_TEXTENCODING_ASCII_US ); 252*cdf0e10cSrcweir const sal_Char *pChar = aString.getStr( ) ; 253*cdf0e10cSrcweir sal_Char tmpBuffer[4]; 254*cdf0e10cSrcweir sal_Int32 nCharCounter = 0; 255*cdf0e10cSrcweir ::rtl::ByteSequence bsByteSequence( IP_VER ); 256*cdf0e10cSrcweir sal_Int32 nByteSeqCounter = 0; 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir for ( int i = 0; i < aString.getLength( ) + 1 ; i++ ) 259*cdf0e10cSrcweir { 260*cdf0e10cSrcweir if ( ( *pChar != '.' ) && ( i !=aString.getLength( ) ) ) 261*cdf0e10cSrcweir tmpBuffer[nCharCounter++] = *pChar; 262*cdf0e10cSrcweir else 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir tmpBuffer[nCharCounter] = '\0'; 265*cdf0e10cSrcweir nCharCounter = 0; 266*cdf0e10cSrcweir bsByteSequence[nByteSeqCounter++] = static_cast<sal_Int8>( atoi( tmpBuffer ) ); 267*cdf0e10cSrcweir } 268*cdf0e10cSrcweir pChar++; 269*cdf0e10cSrcweir } 270*cdf0e10cSrcweir return bsByteSequence; 271*cdf0e10cSrcweir } 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir /** print a socket result name. 274*cdf0e10cSrcweir */ 275*cdf0e10cSrcweir inline void printSocketResult( oslSocketResult eResult ) 276*cdf0e10cSrcweir { 277*cdf0e10cSrcweir t_print("#printSocketResult# " ); 278*cdf0e10cSrcweir if (!eResult) 279*cdf0e10cSrcweir switch (eResult) 280*cdf0e10cSrcweir { 281*cdf0e10cSrcweir case osl_Socket_Ok: 282*cdf0e10cSrcweir t_print("client connected\n"); 283*cdf0e10cSrcweir break; 284*cdf0e10cSrcweir case osl_Socket_Error: 285*cdf0e10cSrcweir t_print("got an error ... exiting\r\n\r\n" ); 286*cdf0e10cSrcweir break; 287*cdf0e10cSrcweir case osl_Socket_TimedOut: 288*cdf0e10cSrcweir t_print("timeout\n"); 289*cdf0e10cSrcweir break; 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir case osl_Socket_FORCE_EQUAL_SIZE: 292*cdf0e10cSrcweir t_print("FORCE EQUAL SIZE\n"); 293*cdf0e10cSrcweir break; 294*cdf0e10cSrcweir case osl_Socket_InProgress: 295*cdf0e10cSrcweir t_print("In Progress\n"); 296*cdf0e10cSrcweir break; 297*cdf0e10cSrcweir case osl_Socket_Interrupted: 298*cdf0e10cSrcweir t_print("Interrupted\n"); 299*cdf0e10cSrcweir break; 300*cdf0e10cSrcweir } 301*cdf0e10cSrcweir } 302*cdf0e10cSrcweir 303*cdf0e10cSrcweir /** Client Socket Thread, served as a temp little client to communicate with server. 304*cdf0e10cSrcweir */ 305*cdf0e10cSrcweir class ClientSocketThread : public Thread 306*cdf0e10cSrcweir { 307*cdf0e10cSrcweir protected: 308*cdf0e10cSrcweir oslThreadIdentifier m_id; 309*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr; 310*cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 311*cdf0e10cSrcweir 312*cdf0e10cSrcweir void SAL_CALL run( ) 313*cdf0e10cSrcweir { 314*cdf0e10cSrcweir TimeValue *pTimeout; 315*cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 316*cdf0e10cSrcweir pTimeout->Seconds = 5; 317*cdf0e10cSrcweir pTimeout->Nanosec = 0; 318*cdf0e10cSrcweir 319*cdf0e10cSrcweir /// if the thread should terminate, schedule return false 320*cdf0e10cSrcweir //while ( schedule( ) == sal_True ) 321*cdf0e10cSrcweir //{ 322*cdf0e10cSrcweir if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout )) 323*cdf0e10cSrcweir { 324*cdf0e10cSrcweir csConnectorSocket.send( pTestString1, 11 ); // "test socket" 325*cdf0e10cSrcweir csConnectorSocket.send( pTestString2, 10); 326*cdf0e10cSrcweir } 327*cdf0e10cSrcweir else 328*cdf0e10cSrcweir t_print("# ClientSocketThread: connect failed! \n"); 329*cdf0e10cSrcweir // terminate(); 330*cdf0e10cSrcweir //} 331*cdf0e10cSrcweir csConnectorSocket.close(); 332*cdf0e10cSrcweir free( pTimeout ); 333*cdf0e10cSrcweir } 334*cdf0e10cSrcweir 335*cdf0e10cSrcweir void SAL_CALL onTerminated( ) 336*cdf0e10cSrcweir { 337*cdf0e10cSrcweir //t_print("# normally terminate this thread %d!\n", m_id ); 338*cdf0e10cSrcweir } 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir public: 341*cdf0e10cSrcweir ClientSocketThread( ): 342*cdf0e10cSrcweir saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT9 ), 343*cdf0e10cSrcweir csConnectorSocket( ) 344*cdf0e10cSrcweir { 345*cdf0e10cSrcweir m_id = getIdentifier( ); 346*cdf0e10cSrcweir //t_print("# successfully creat this client thread %d!\n", m_id ); 347*cdf0e10cSrcweir } 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir ~ClientSocketThread( ) 350*cdf0e10cSrcweir { 351*cdf0e10cSrcweir if ( isRunning( ) ) 352*cdf0e10cSrcweir t_print("# error: client thread not terminated.\n" ); 353*cdf0e10cSrcweir } 354*cdf0e10cSrcweir 355*cdf0e10cSrcweir }; 356*cdf0e10cSrcweir 357*cdf0e10cSrcweir 358*cdf0e10cSrcweir /** Server Socket Thread, served as a temp little server to communicate with client. 359*cdf0e10cSrcweir */ 360*cdf0e10cSrcweir class ServerSocketThread : public Thread 361*cdf0e10cSrcweir { 362*cdf0e10cSrcweir protected: 363*cdf0e10cSrcweir oslThreadIdentifier m_id; 364*cdf0e10cSrcweir 365*cdf0e10cSrcweir void SAL_CALL run( ) 366*cdf0e10cSrcweir { 367*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 368*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 ); 369*cdf0e10cSrcweir ::osl::StreamSocket ssStreamConnection; 370*cdf0e10cSrcweir 371*cdf0e10cSrcweir //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket 372*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); 373*cdf0e10cSrcweir while ( schedule( ) == sal_True ) 374*cdf0e10cSrcweir { 375*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 376*cdf0e10cSrcweir if ( sal_True != bOK1 ) 377*cdf0e10cSrcweir { 378*cdf0e10cSrcweir t_print("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ; 379*cdf0e10cSrcweir break; 380*cdf0e10cSrcweir } 381*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 382*cdf0e10cSrcweir if ( sal_True != bOK2 ) 383*cdf0e10cSrcweir { 384*cdf0e10cSrcweir t_print("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ; 385*cdf0e10cSrcweir break; 386*cdf0e10cSrcweir } 387*cdf0e10cSrcweir 388*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_False ); 389*cdf0e10cSrcweir 390*cdf0e10cSrcweir oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); 391*cdf0e10cSrcweir if (eResult != osl_Socket_Ok ) 392*cdf0e10cSrcweir { 393*cdf0e10cSrcweir t_print("ServerSocketThread: acceptConnection failed! \n"); 394*cdf0e10cSrcweir break; 395*cdf0e10cSrcweir } 396*cdf0e10cSrcweir sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 ); 397*cdf0e10cSrcweir sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 ); 398*cdf0e10cSrcweir pReadBuffer[nReadNumber1 + nReadNumber2] = '\0'; 399*cdf0e10cSrcweir //t_print("# read buffer content: %s\n", pReadBuffer ); 400*cdf0e10cSrcweir break; 401*cdf0e10cSrcweir } 402*cdf0e10cSrcweir ssStreamConnection.close(); 403*cdf0e10cSrcweir asAcceptorSocket.close(); 404*cdf0e10cSrcweir 405*cdf0e10cSrcweir } 406*cdf0e10cSrcweir 407*cdf0e10cSrcweir void SAL_CALL onTerminated( ) 408*cdf0e10cSrcweir { 409*cdf0e10cSrcweir //t_print("# normally terminate this server thread %d!\n", m_id ); 410*cdf0e10cSrcweir } 411*cdf0e10cSrcweir 412*cdf0e10cSrcweir public: 413*cdf0e10cSrcweir // public to check if data transmition is OK 414*cdf0e10cSrcweir sal_Char pReadBuffer[30]; 415*cdf0e10cSrcweir ServerSocketThread( ) 416*cdf0e10cSrcweir { 417*cdf0e10cSrcweir m_id = getIdentifier( ); 418*cdf0e10cSrcweir //t_print("# successfully creat this server thread %d!\n", m_id ); 419*cdf0e10cSrcweir } 420*cdf0e10cSrcweir 421*cdf0e10cSrcweir ~ServerSocketThread( ) 422*cdf0e10cSrcweir { 423*cdf0e10cSrcweir if ( isRunning( ) ) 424*cdf0e10cSrcweir t_print("# error: server thread not terminated.\n" ); 425*cdf0e10cSrcweir } 426*cdf0e10cSrcweir }; 427*cdf0e10cSrcweir 428*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 429*cdf0e10cSrcweir // Helper functions, to create buffers, check buffers 430*cdf0e10cSrcweir class ValueCheckProvider 431*cdf0e10cSrcweir { 432*cdf0e10cSrcweir bool m_bFoundFailure; 433*cdf0e10cSrcweir char *m_pBuffer; 434*cdf0e10cSrcweir sal_Int32 m_nBufferSize; 435*cdf0e10cSrcweir 436*cdf0e10cSrcweir public: 437*cdf0e10cSrcweir ValueCheckProvider() 438*cdf0e10cSrcweir : 439*cdf0e10cSrcweir m_bFoundFailure(false), 440*cdf0e10cSrcweir m_pBuffer(NULL), 441*cdf0e10cSrcweir m_nBufferSize(0) 442*cdf0e10cSrcweir { 443*cdf0e10cSrcweir } 444*cdf0e10cSrcweir 445*cdf0e10cSrcweir bool isFailure() {return m_bFoundFailure;} 446*cdf0e10cSrcweir 447*cdf0e10cSrcweir const char* getBuffer() {return m_pBuffer;} 448*cdf0e10cSrcweir char* getWriteBuffer() {return m_pBuffer;} 449*cdf0e10cSrcweir 450*cdf0e10cSrcweir sal_Int32 getBufferSize() {return m_nBufferSize;} 451*cdf0e10cSrcweir 452*cdf0e10cSrcweir bool checkValues(sal_Int32 _nLength, int _nValue) 453*cdf0e10cSrcweir { 454*cdf0e10cSrcweir m_bFoundFailure = false; 455*cdf0e10cSrcweir for(sal_Int32 i=0;i<_nLength;i++) 456*cdf0e10cSrcweir { 457*cdf0e10cSrcweir if (m_pBuffer[i] != _nValue) 458*cdf0e10cSrcweir { 459*cdf0e10cSrcweir m_bFoundFailure = true; 460*cdf0e10cSrcweir } 461*cdf0e10cSrcweir } 462*cdf0e10cSrcweir return m_bFoundFailure; 463*cdf0e10cSrcweir } 464*cdf0e10cSrcweir 465*cdf0e10cSrcweir void createBuffer(sal_Int32 _nLength, int _nValue) 466*cdf0e10cSrcweir { 467*cdf0e10cSrcweir m_nBufferSize = _nLength; 468*cdf0e10cSrcweir m_pBuffer = (char*) malloc(m_nBufferSize); 469*cdf0e10cSrcweir if (m_pBuffer) 470*cdf0e10cSrcweir { 471*cdf0e10cSrcweir memset(m_pBuffer, _nValue, m_nBufferSize); 472*cdf0e10cSrcweir } 473*cdf0e10cSrcweir } 474*cdf0e10cSrcweir 475*cdf0e10cSrcweir void freeBuffer() 476*cdf0e10cSrcweir { 477*cdf0e10cSrcweir if (m_pBuffer) free(m_pBuffer); 478*cdf0e10cSrcweir } 479*cdf0e10cSrcweir 480*cdf0e10cSrcweir }; 481*cdf0e10cSrcweir 482*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 483*cdf0e10cSrcweir /** Client Socket Thread, served as a temp little client to communicate with server. 484*cdf0e10cSrcweir */ 485*cdf0e10cSrcweir 486*cdf0e10cSrcweir class ReadSocketThread : public Thread 487*cdf0e10cSrcweir { 488*cdf0e10cSrcweir int m_nValue; 489*cdf0e10cSrcweir ValueCheckProvider m_aValues; 490*cdf0e10cSrcweir 491*cdf0e10cSrcweir protected: 492*cdf0e10cSrcweir oslThreadIdentifier m_id; 493*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr; 494*cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 495*cdf0e10cSrcweir 496*cdf0e10cSrcweir void SAL_CALL run( ) 497*cdf0e10cSrcweir { 498*cdf0e10cSrcweir TimeValue *pTimeout; 499*cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 500*cdf0e10cSrcweir pTimeout->Seconds = 5; 501*cdf0e10cSrcweir pTimeout->Nanosec = 0; 502*cdf0e10cSrcweir 503*cdf0e10cSrcweir /// if the thread should terminate, schedule return false 504*cdf0e10cSrcweir //while ( schedule( ) == sal_True ) 505*cdf0e10cSrcweir //{ 506*cdf0e10cSrcweir if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout )) 507*cdf0e10cSrcweir { 508*cdf0e10cSrcweir sal_Int32 nReadCount = csConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() ); 509*cdf0e10cSrcweir m_aValues.checkValues(nReadCount, m_nValue); 510*cdf0e10cSrcweir } 511*cdf0e10cSrcweir else 512*cdf0e10cSrcweir { 513*cdf0e10cSrcweir t_print("# ReadSocketThread: connect failed! \n"); 514*cdf0e10cSrcweir } 515*cdf0e10cSrcweir // terminate(); 516*cdf0e10cSrcweir //} 517*cdf0e10cSrcweir //remove this line for deadlock on solaris( margritte.germany ) 518*cdf0e10cSrcweir csConnectorSocket.close(); 519*cdf0e10cSrcweir free( pTimeout ); 520*cdf0e10cSrcweir } 521*cdf0e10cSrcweir 522*cdf0e10cSrcweir void SAL_CALL onTerminated( ) 523*cdf0e10cSrcweir { 524*cdf0e10cSrcweir //t_print("# normally terminate this thread %d!\n", m_id ); 525*cdf0e10cSrcweir } 526*cdf0e10cSrcweir 527*cdf0e10cSrcweir public: 528*cdf0e10cSrcweir sal_Int32 getCount() {return m_aValues.getBufferSize();} 529*cdf0e10cSrcweir bool isOk() {return m_aValues.isFailure() == true ? false : true;} 530*cdf0e10cSrcweir 531*cdf0e10cSrcweir ReadSocketThread(sal_Int32 _nBufferSize, int _nValue ) 532*cdf0e10cSrcweir : 533*cdf0e10cSrcweir m_nValue( _nValue ), 534*cdf0e10cSrcweir saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT10 ), 535*cdf0e10cSrcweir csConnectorSocket( ) 536*cdf0e10cSrcweir { 537*cdf0e10cSrcweir m_id = getIdentifier( ); 538*cdf0e10cSrcweir //t_print("# successfully creat this client thread %d!\n", m_id ); 539*cdf0e10cSrcweir m_aValues.createBuffer(_nBufferSize, 0); 540*cdf0e10cSrcweir } 541*cdf0e10cSrcweir 542*cdf0e10cSrcweir ~ReadSocketThread( ) 543*cdf0e10cSrcweir { 544*cdf0e10cSrcweir if ( isRunning( ) ) 545*cdf0e10cSrcweir t_print("# error: client thread not terminated.\n" ); 546*cdf0e10cSrcweir m_aValues.freeBuffer(); 547*cdf0e10cSrcweir } 548*cdf0e10cSrcweir 549*cdf0e10cSrcweir }; 550*cdf0e10cSrcweir 551*cdf0e10cSrcweir /** Server Socket Thread, write a file which is large 552*cdf0e10cSrcweir */ 553*cdf0e10cSrcweir class WriteSocketThread : public Thread 554*cdf0e10cSrcweir { 555*cdf0e10cSrcweir ValueCheckProvider m_aValues; 556*cdf0e10cSrcweir 557*cdf0e10cSrcweir protected: 558*cdf0e10cSrcweir oslThreadIdentifier m_id; 559*cdf0e10cSrcweir 560*cdf0e10cSrcweir void SAL_CALL run( ) 561*cdf0e10cSrcweir { 562*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 563*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT10 ); 564*cdf0e10cSrcweir ::osl::StreamSocket ssStreamConnection; 565*cdf0e10cSrcweir 566*cdf0e10cSrcweir //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket 567*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 568*cdf0e10cSrcweir 569*cdf0e10cSrcweir /// if the thread should terminate, schedule return false 570*cdf0e10cSrcweir while ( schedule( ) == sal_True ) 571*cdf0e10cSrcweir { 572*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 573*cdf0e10cSrcweir if ( sal_True != bOK1 ) 574*cdf0e10cSrcweir { 575*cdf0e10cSrcweir t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ; 576*cdf0e10cSrcweir break; 577*cdf0e10cSrcweir } 578*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 579*cdf0e10cSrcweir if ( sal_True != bOK2 ) 580*cdf0e10cSrcweir { 581*cdf0e10cSrcweir t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ; 582*cdf0e10cSrcweir break; 583*cdf0e10cSrcweir } 584*cdf0e10cSrcweir // blocking mode, if read/recv failed, block until success 585*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_False); 586*cdf0e10cSrcweir 587*cdf0e10cSrcweir oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); 588*cdf0e10cSrcweir if (eResult != osl_Socket_Ok ) 589*cdf0e10cSrcweir { 590*cdf0e10cSrcweir t_print("WriteSocketThread: acceptConnection failed! \n"); 591*cdf0e10cSrcweir break; 592*cdf0e10cSrcweir } 593*cdf0e10cSrcweir 594*cdf0e10cSrcweir ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() ); 595*cdf0e10cSrcweir break; 596*cdf0e10cSrcweir } 597*cdf0e10cSrcweir ssStreamConnection.close(); 598*cdf0e10cSrcweir asAcceptorSocket.close(); 599*cdf0e10cSrcweir } 600*cdf0e10cSrcweir 601*cdf0e10cSrcweir void SAL_CALL onTerminated( ) 602*cdf0e10cSrcweir { 603*cdf0e10cSrcweir //t_print("# normally terminate this server thread %d!\n", m_id ); 604*cdf0e10cSrcweir } 605*cdf0e10cSrcweir 606*cdf0e10cSrcweir public: 607*cdf0e10cSrcweir // public to check if data transmition is OK 608*cdf0e10cSrcweir WriteSocketThread(sal_Int32 _nBufferSize, int _nValue ) 609*cdf0e10cSrcweir { 610*cdf0e10cSrcweir m_id = getIdentifier( ); 611*cdf0e10cSrcweir //t_print("# successfully creat this server thread %d!\n", m_id ); 612*cdf0e10cSrcweir 613*cdf0e10cSrcweir m_aValues.createBuffer(_nBufferSize, _nValue); 614*cdf0e10cSrcweir } 615*cdf0e10cSrcweir 616*cdf0e10cSrcweir ~WriteSocketThread( ) 617*cdf0e10cSrcweir { 618*cdf0e10cSrcweir if ( isRunning( ) ) 619*cdf0e10cSrcweir t_print("# error: server thread not terminated.\n" ); 620*cdf0e10cSrcweir m_aValues.freeBuffer(); 621*cdf0e10cSrcweir } 622*cdf0e10cSrcweir 623*cdf0e10cSrcweir }; 624*cdf0e10cSrcweir 625*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 626*cdf0e10cSrcweir // just used to test socket::close() when accepting 627*cdf0e10cSrcweir class AcceptorThread : public Thread 628*cdf0e10cSrcweir { 629*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 630*cdf0e10cSrcweir ::rtl::OUString aHostIP; 631*cdf0e10cSrcweir sal_Bool bOK; 632*cdf0e10cSrcweir protected: 633*cdf0e10cSrcweir void SAL_CALL run( ) 634*cdf0e10cSrcweir { 635*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 ); 636*cdf0e10cSrcweir ::osl::StreamSocket ssStreamConnection; 637*cdf0e10cSrcweir 638*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); 639*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 640*cdf0e10cSrcweir if ( sal_True != bOK1 ) 641*cdf0e10cSrcweir { 642*cdf0e10cSrcweir t_print("# AcceptorSocket bind address failed.\n" ) ; 643*cdf0e10cSrcweir return; 644*cdf0e10cSrcweir } 645*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 646*cdf0e10cSrcweir if ( sal_True != bOK2 ) 647*cdf0e10cSrcweir { 648*cdf0e10cSrcweir t_print("# AcceptorSocket listen address failed.\n" ) ; 649*cdf0e10cSrcweir return; 650*cdf0e10cSrcweir } 651*cdf0e10cSrcweir 652*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_False ); 653*cdf0e10cSrcweir 654*cdf0e10cSrcweir oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); 655*cdf0e10cSrcweir if (eResult != osl_Socket_Ok ) 656*cdf0e10cSrcweir { 657*cdf0e10cSrcweir bOK = sal_True; 658*cdf0e10cSrcweir t_print("AcceptorThread: acceptConnection failed! \n"); 659*cdf0e10cSrcweir } 660*cdf0e10cSrcweir } 661*cdf0e10cSrcweir public: 662*cdf0e10cSrcweir AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString & aBindIP ) 663*cdf0e10cSrcweir : asAcceptorSocket( asSocket ), aHostIP( aBindIP ) 664*cdf0e10cSrcweir { 665*cdf0e10cSrcweir bOK = sal_False; 666*cdf0e10cSrcweir } 667*cdf0e10cSrcweir 668*cdf0e10cSrcweir sal_Bool isOK() { return bOK; } 669*cdf0e10cSrcweir 670*cdf0e10cSrcweir ~AcceptorThread( ) 671*cdf0e10cSrcweir { 672*cdf0e10cSrcweir if ( isRunning( ) ) 673*cdf0e10cSrcweir { 674*cdf0e10cSrcweir asAcceptorSocket.shutdown(); 675*cdf0e10cSrcweir t_print("# error: Acceptor thread not terminated.\n" ); 676*cdf0e10cSrcweir } 677*cdf0e10cSrcweir } 678*cdf0e10cSrcweir }; 679*cdf0e10cSrcweir 680*cdf0e10cSrcweir class CloseSocketThread : public Thread 681*cdf0e10cSrcweir { 682*cdf0e10cSrcweir ::osl::Socket m_sSocket; 683*cdf0e10cSrcweir protected: 684*cdf0e10cSrcweir void SAL_CALL run( ) 685*cdf0e10cSrcweir { 686*cdf0e10cSrcweir thread_sleep( 1 ); 687*cdf0e10cSrcweir m_sSocket.close( ); 688*cdf0e10cSrcweir } 689*cdf0e10cSrcweir public: 690*cdf0e10cSrcweir CloseSocketThread(::osl::Socket & sSocket ) 691*cdf0e10cSrcweir : m_sSocket( sSocket ) 692*cdf0e10cSrcweir { 693*cdf0e10cSrcweir } 694*cdf0e10cSrcweir 695*cdf0e10cSrcweir ~CloseSocketThread( ) 696*cdf0e10cSrcweir { 697*cdf0e10cSrcweir if ( isRunning( ) ) 698*cdf0e10cSrcweir { 699*cdf0e10cSrcweir t_print("# error: CloseSocketThread not terminated.\n" ); 700*cdf0e10cSrcweir } 701*cdf0e10cSrcweir } 702*cdf0e10cSrcweir }; 703*cdf0e10cSrcweir 704*cdf0e10cSrcweir //------------------------------------------------------------------------ 705*cdf0e10cSrcweir // tests cases begins here 706*cdf0e10cSrcweir //------------------------------------------------------------------------ 707*cdf0e10cSrcweir 708*cdf0e10cSrcweir namespace osl_SocketAddr 709*cdf0e10cSrcweir { 710*cdf0e10cSrcweir 711*cdf0e10cSrcweir /** testing the methods: 712*cdf0e10cSrcweir inline SocketAddr(); 713*cdf0e10cSrcweir inline SocketAddr(const SocketAddr& Addr); 714*cdf0e10cSrcweir inline SocketAddr(const oslSocketAddr , __osl_socket_NoCopy nocopy ); 715*cdf0e10cSrcweir inline SocketAddr(oslSocketAddr Addr); 716*cdf0e10cSrcweir inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort ); 717*cdf0e10cSrcweir */ 718*cdf0e10cSrcweir 719*cdf0e10cSrcweir class ctors : public CppUnit::TestFixture 720*cdf0e10cSrcweir { 721*cdf0e10cSrcweir public: 722*cdf0e10cSrcweir 723*cdf0e10cSrcweir void ctors_none() 724*cdf0e10cSrcweir { 725*cdf0e10cSrcweir /// SocketAddr constructor. 726*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr; 727*cdf0e10cSrcweir 728*cdf0e10cSrcweir // oslSocketResult aResult; 729*cdf0e10cSrcweir // rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult); 730*cdf0e10cSrcweir 731*cdf0e10cSrcweir // rtl::OUString suHost2 = getThisHostname(); 732*cdf0e10cSrcweir 733*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for none parameter constructor function: check if the socket address was created successfully", 734*cdf0e10cSrcweir sal_True == saSocketAddr.is( ) ); 735*cdf0e10cSrcweir } 736*cdf0e10cSrcweir 737*cdf0e10cSrcweir void ctors_none_000() 738*cdf0e10cSrcweir { 739*cdf0e10cSrcweir /// SocketAddr constructor. 740*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr; 741*cdf0e10cSrcweir 742*cdf0e10cSrcweir oslSocketResult aResult; 743*cdf0e10cSrcweir rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult); 744*cdf0e10cSrcweir rtl::OUString suHost2 = getThisHostname(); 745*cdf0e10cSrcweir 746*cdf0e10cSrcweir sal_Bool bOk = compareUString(suHost, suHost2); 747*cdf0e10cSrcweir 748*cdf0e10cSrcweir rtl::OUString suError = rtl::OUString::createFromAscii("Host names should be the same. From SocketAddr.getLocalHostname() it is'"); 749*cdf0e10cSrcweir suError += suHost; 750*cdf0e10cSrcweir suError += rtl::OUString::createFromAscii("', from getThisHostname() it is '"); 751*cdf0e10cSrcweir suError += suHost2; 752*cdf0e10cSrcweir suError += rtl::OUString::createFromAscii("'."); 753*cdf0e10cSrcweir 754*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(suError, sal_True == bOk); 755*cdf0e10cSrcweir } 756*cdf0e10cSrcweir 757*cdf0e10cSrcweir void ctors_copy() 758*cdf0e10cSrcweir { 759*cdf0e10cSrcweir /// SocketAddr copy constructor. 760*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 ); 761*cdf0e10cSrcweir ::osl::SocketAddr saCopySocketAddr( saSocketAddr ); 762*cdf0e10cSrcweir 763*cdf0e10cSrcweir sal_Int32 nPort = saCopySocketAddr.getPort( ); 764*cdf0e10cSrcweir 765*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy constructor function: copy constructor, do an action of copy construction then check the port with original set.", 766*cdf0e10cSrcweir ( sal_True == saCopySocketAddr.is( ) ) && ( nPort == IP_PORT_HTTP1 ) ); 767*cdf0e10cSrcweir } 768*cdf0e10cSrcweir 769*cdf0e10cSrcweir void ctors_copy_no_001() 770*cdf0e10cSrcweir { 771*cdf0e10cSrcweir #if 0 772*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 ); 773*cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( ); 774*cdf0e10cSrcweir 775*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrCopy( psaOSLSocketAddr, SAL_NO_COPY ); 776*cdf0e10cSrcweir saSocketAddrCopy.setPort( IP_PORT_HTTP2 ); 777*cdf0e10cSrcweir 778*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", 779*cdf0e10cSrcweir saSocketAddr.getPort( ) == IP_PORT_HTTP2 ); 780*cdf0e10cSrcweir #endif 781*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 ); 782*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 783*cdf0e10cSrcweir 784*cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); 785*cdf0e10cSrcweir 786*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); 787*cdf0e10cSrcweir 788*cdf0e10cSrcweir pSocketAddrCopy->setPort( IP_PORT_HTTP2 ); 789*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", 790*cdf0e10cSrcweir pSocketAddr->getPort( ) == IP_PORT_HTTP2 ); 791*cdf0e10cSrcweir 792*cdf0e10cSrcweir delete pSocketAddrCopy; 793*cdf0e10cSrcweir // LLA: don't do this also: delete pSocketAddr; 794*cdf0e10cSrcweir } 795*cdf0e10cSrcweir 796*cdf0e10cSrcweir void ctors_copy_no_002() 797*cdf0e10cSrcweir { 798*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 ); 799*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 800*cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); 801*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); 802*cdf0e10cSrcweir 803*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", 804*cdf0e10cSrcweir pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) ); 805*cdf0e10cSrcweir 806*cdf0e10cSrcweir delete pSocketAddrCopy; 807*cdf0e10cSrcweir } 808*cdf0e10cSrcweir 809*cdf0e10cSrcweir void ctors_copy_handle_001() 810*cdf0e10cSrcweir { 811*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 ); 812*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) ); 813*cdf0e10cSrcweir 814*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, get its port to check copy effect.", 815*cdf0e10cSrcweir saSocketAddrCopy.getPort( ) == IP_PORT_HTTP1 ); 816*cdf0e10cSrcweir } 817*cdf0e10cSrcweir 818*cdf0e10cSrcweir void ctors_copy_handle_002() 819*cdf0e10cSrcweir { 820*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 ); 821*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) ); 822*cdf0e10cSrcweir saSocketAddrCopy.setPort( IP_PORT_HTTP2 ); 823*cdf0e10cSrcweir 824*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, the original one should not be changed.", 825*cdf0e10cSrcweir saSocketAddr.getPort( ) != IP_PORT_HTTP2 ); 826*cdf0e10cSrcweir } 827*cdf0e10cSrcweir 828*cdf0e10cSrcweir void ctors_hostname_port_001() 829*cdf0e10cSrcweir { 830*cdf0e10cSrcweir /// tcpip-specif constructor. 831*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP ); 832*cdf0e10cSrcweir printUString(saSocketAddr.getHostname( ), "ctors_hostname_port_001:getHostname"); 833*cdf0e10cSrcweir 834*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: do a constructor using tcpip spec, check the result.", 835*cdf0e10cSrcweir saSocketAddr.is( ) == sal_True && 836*cdf0e10cSrcweir ( saSocketAddr.getPort( ) == IP_PORT_FTP )/*&& 837*cdf0e10cSrcweir ( sal_True == compareUString( saSocketAddr.getHostname( ), aHostName1 ) ) */); 838*cdf0e10cSrcweir } 839*cdf0e10cSrcweir 840*cdf0e10cSrcweir //same as is_002 841*cdf0e10cSrcweir void ctors_hostname_port_002() 842*cdf0e10cSrcweir { 843*cdf0e10cSrcweir /// tcpip-specif constructor. 844*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT2 ); 845*cdf0e10cSrcweir 846*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: using an invalid IP address, the socketaddr ctors should fail", sal_False == saSocketAddr.is( )); 847*cdf0e10cSrcweir } 848*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( ctors ); 849*cdf0e10cSrcweir CPPUNIT_TEST( ctors_none ); 850*cdf0e10cSrcweir CPPUNIT_TEST( ctors_none_000 ); 851*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy ); 852*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy_no_001 ); 853*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy_no_002 ); 854*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy_handle_001 ); 855*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy_handle_002 ); 856*cdf0e10cSrcweir CPPUNIT_TEST( ctors_hostname_port_001 ); 857*cdf0e10cSrcweir CPPUNIT_TEST( ctors_hostname_port_002 ); 858*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 859*cdf0e10cSrcweir 860*cdf0e10cSrcweir }; // class ctors 861*cdf0e10cSrcweir 862*cdf0e10cSrcweir 863*cdf0e10cSrcweir /** testing the method: 864*cdf0e10cSrcweir inline sal_Bool is() const; 865*cdf0e10cSrcweir */ 866*cdf0e10cSrcweir 867*cdf0e10cSrcweir class is : public CppUnit::TestFixture 868*cdf0e10cSrcweir { 869*cdf0e10cSrcweir public: 870*cdf0e10cSrcweir void is_001() 871*cdf0e10cSrcweir { 872*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr; 873*cdf0e10cSrcweir 874*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for is() function: create an unknown type socket, it should be True when call is.", 875*cdf0e10cSrcweir sal_True == saSocketAddr.is( ) ); 876*cdf0e10cSrcweir } 877*cdf0e10cSrcweir // refer to setPort_003() 878*cdf0e10cSrcweir void is_002() 879*cdf0e10cSrcweir { 880*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_INVAL ); 881*cdf0e10cSrcweir 882*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid port number", 883*cdf0e10cSrcweir sal_True == saSocketAddr.is( ) ); 884*cdf0e10cSrcweir } 885*cdf0e10cSrcweir 886*cdf0e10cSrcweir void is_003() 887*cdf0e10cSrcweir { 888*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT ); 889*cdf0e10cSrcweir 890*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid Ip number", 891*cdf0e10cSrcweir sal_True != saSocketAddr.is( ) ); 892*cdf0e10cSrcweir } 893*cdf0e10cSrcweir 894*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( is ); 895*cdf0e10cSrcweir CPPUNIT_TEST( is_001 ); 896*cdf0e10cSrcweir CPPUNIT_TEST( is_002 ); 897*cdf0e10cSrcweir CPPUNIT_TEST( is_003 ); 898*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 899*cdf0e10cSrcweir 900*cdf0e10cSrcweir }; // class is 901*cdf0e10cSrcweir 902*cdf0e10cSrcweir 903*cdf0e10cSrcweir /** testing the method: 904*cdf0e10cSrcweir inline ::rtl::OUString SAL_CALL getHostname( oslSocketResult *pResult = 0 ) const; 905*cdf0e10cSrcweir */ 906*cdf0e10cSrcweir 907*cdf0e10cSrcweir class getHostname : public CppUnit::TestFixture 908*cdf0e10cSrcweir { 909*cdf0e10cSrcweir public: 910*cdf0e10cSrcweir void setUp() 911*cdf0e10cSrcweir { 912*cdf0e10cSrcweir } 913*cdf0e10cSrcweir 914*cdf0e10cSrcweir void tearDown() 915*cdf0e10cSrcweir { 916*cdf0e10cSrcweir } 917*cdf0e10cSrcweir 918*cdf0e10cSrcweir void getHostname_000() 919*cdf0e10cSrcweir { 920*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp4, IP_PORT_FTP ); 921*cdf0e10cSrcweir 922*cdf0e10cSrcweir } 923*cdf0e10cSrcweir 924*cdf0e10cSrcweir /** it will search the Ip in current machine's /etc/hosts at first, if find, then return the 925*cdf0e10cSrcweir mapped hostname, otherwise, it will search via DNS server, and often return hostname+ Domain name 926*cdf0e10cSrcweir like "sceri.PRC.Sun.COM" 927*cdf0e10cSrcweir The process is same as Socket::getLocalHost(), but getLocalHost can only return hostname of the current machine. 928*cdf0e10cSrcweir */ 929*cdf0e10cSrcweir void getHostname_001() 930*cdf0e10cSrcweir { 931*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp4, IP_PORT_FTP ); 932*cdf0e10cSrcweir rtl::OUString suResult = saSocketAddr.getHostname( 0 ); 933*cdf0e10cSrcweir rtl::OUString suError = outputError(suResult, aHostName4, "test for getHostname(0)"); 934*cdf0e10cSrcweir sal_Bool bOK = compareUString( suResult, aHostName4 ); 935*cdf0e10cSrcweir // search the returned hostname in /etc/hosts, if find, and the IP in the row is same as IP 936*cdf0e10cSrcweir // in the Addr, it's right also. 937*cdf0e10cSrcweir if ( bOK == sal_False) 938*cdf0e10cSrcweir { 939*cdf0e10cSrcweir if ( compareUString( getIPbyName( oustring2char( suResult ) ), aHostIp4 ) == sal_True ) 940*cdf0e10cSrcweir bOK = sal_True; 941*cdf0e10cSrcweir } 942*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK); 943*cdf0e10cSrcweir } 944*cdf0e10cSrcweir 945*cdf0e10cSrcweir // LLA: now we have to control, if this behaviour is right. 946*cdf0e10cSrcweir // LLA: this function does not work in company (Linux, Windows) but at home 947*cdf0e10cSrcweir void getHostname_002() 948*cdf0e10cSrcweir { 949*cdf0e10cSrcweir rtl::OUString suHostname = rtl::OUString::createFromAscii("cn-1.germany.sun.com"); 950*cdf0e10cSrcweir rtl::OUString aHostIP = getIPbyName( oustring2char( suHostname ) ); 951*cdf0e10cSrcweir 952*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_FTP ); 953*cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setHostname( suHostname ); 954*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("#SocketAddr.setHostname failed", sal_True == bOK ); 955*cdf0e10cSrcweir oslSocketResult aResult; 956*cdf0e10cSrcweir rtl::OUString suResult = saSocketAddr.getHostname( &aResult ); 957*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("SocketAddr.getHostname failed.", aResult == osl_Socket_Ok); 958*cdf0e10cSrcweir 959*cdf0e10cSrcweir rtl::OUString suError = outputError(suResult, suHostname, "test for getHostname(0)"); 960*cdf0e10cSrcweir bOK = compareUString( suResult, suHostname ); 961*cdf0e10cSrcweir if ( bOK == sal_False) 962*cdf0e10cSrcweir { 963*cdf0e10cSrcweir rtl::OString aString = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US ); 964*cdf0e10cSrcweir if ( compareUString( getIPbyName( aString) , aHostIp6 ) == sal_True ) 965*cdf0e10cSrcweir { 966*cdf0e10cSrcweir bOK = sal_True; 967*cdf0e10cSrcweir } 968*cdf0e10cSrcweir } 969*cdf0e10cSrcweir 970*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 971*cdf0e10cSrcweir } 972*cdf0e10cSrcweir 973*cdf0e10cSrcweir 974*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getHostname ); 975*cdf0e10cSrcweir CPPUNIT_TEST( getHostname_001 ); 976*cdf0e10cSrcweir CPPUNIT_TEST( getHostname_002 ); 977*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 978*cdf0e10cSrcweir 979*cdf0e10cSrcweir }; // class getHostname 980*cdf0e10cSrcweir 981*cdf0e10cSrcweir 982*cdf0e10cSrcweir /** testing the method: 983*cdf0e10cSrcweir inline sal_Int32 SAL_CALL getPort() const; 984*cdf0e10cSrcweir */ 985*cdf0e10cSrcweir 986*cdf0e10cSrcweir class getPort : public CppUnit::TestFixture 987*cdf0e10cSrcweir { 988*cdf0e10cSrcweir public: 989*cdf0e10cSrcweir void getPort_001() 990*cdf0e10cSrcweir { 991*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP ); 992*cdf0e10cSrcweir 993*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getPort() function: get a normal port number.", 994*cdf0e10cSrcweir IP_PORT_FTP == saSocketAddr.getPort( ) ); 995*cdf0e10cSrcweir } 996*cdf0e10cSrcweir 997*cdf0e10cSrcweir void getPort_002() 998*cdf0e10cSrcweir { 999*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_INVAL ); 1000*cdf0e10cSrcweir 1001*cdf0e10cSrcweir //t_print("#getPort_002: Port number is %d \n", saSocketAddr.getPort( )); 1002*cdf0e10cSrcweir 1003*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid port to a SocketAddr, get the port to see if it can detect. it did not pass in (W32).", 1004*cdf0e10cSrcweir saSocketAddr.getPort( )>=1 && saSocketAddr.getPort( ) <= 65535 ); 1005*cdf0e10cSrcweir } 1006*cdf0e10cSrcweir //two cases will return OSL_INVALID_PORT: 1. not valid SocketAddr 1007*cdf0e10cSrcweir //2. SocketAddr family is not osl_Socket_FamilyInet, but case 2 could not be constructed 1008*cdf0e10cSrcweir void getPort_003() 1009*cdf0e10cSrcweir { 1010*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT ); 1011*cdf0e10cSrcweir 1012*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid IP to a SocketAddr, get the port to see returned value. ", 1013*cdf0e10cSrcweir saSocketAddr.getPort( ) == OSL_INVALID_PORT ); 1014*cdf0e10cSrcweir } 1015*cdf0e10cSrcweir 1016*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getPort ); 1017*cdf0e10cSrcweir CPPUNIT_TEST( getPort_001 ); 1018*cdf0e10cSrcweir CPPUNIT_TEST( getPort_002 ); 1019*cdf0e10cSrcweir CPPUNIT_TEST( getPort_003 ); 1020*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 1021*cdf0e10cSrcweir 1022*cdf0e10cSrcweir }; // class getPort 1023*cdf0e10cSrcweir 1024*cdf0e10cSrcweir 1025*cdf0e10cSrcweir /** testing the method: 1026*cdf0e10cSrcweir inline sal_Bool SAL_CALL setPort( sal_Int32 nPort ); 1027*cdf0e10cSrcweir rfc1413.txt: TCP port numbers are from 1-65535 1028*cdf0e10cSrcweir rfc1700.txt: 0/tcp Reserved ; 0/udp Reserved 1029*cdf0e10cSrcweir */ 1030*cdf0e10cSrcweir 1031*cdf0e10cSrcweir class setPort : public CppUnit::TestFixture 1032*cdf0e10cSrcweir { 1033*cdf0e10cSrcweir public: 1034*cdf0e10cSrcweir void setPort_001() 1035*cdf0e10cSrcweir { 1036*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP ); 1037*cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_TELNET ); 1038*cdf0e10cSrcweir 1039*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: modify a port number setting, and check it.", 1040*cdf0e10cSrcweir ( sal_True == bOK ) && 1041*cdf0e10cSrcweir ( IP_PORT_TELNET == saSocketAddr.getPort( ) ) ); 1042*cdf0e10cSrcweir } 1043*cdf0e10cSrcweir 1044*cdf0e10cSrcweir /** 0 to 1024 is known as the reserved port range (traditionally only root can assign programs to ports in 1045*cdf0e10cSrcweir this range) and the ephemeral port range from 1025 to 65535. 1046*cdf0e10cSrcweir As many of you programmers will know, when you specify the source port of 0 when you connect to a host, 1047*cdf0e10cSrcweir the OS automatically reassigns the port number to high numbered ephemeral port. The same happens if you 1048*cdf0e10cSrcweir try to bind a listening socket to port 0. 1049*cdf0e10cSrcweir http://www.securiteam.com/securityreviews/5XP0Q2AAKS.html 1050*cdf0e10cSrcweir another: http://www.muq.org/~cynbe/muq/mufref_564.html 1051*cdf0e10cSrcweir */ 1052*cdf0e10cSrcweir void setPort_002() 1053*cdf0e10cSrcweir { 1054*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP ); 1055*cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_ZERO ); 1056*cdf0e10cSrcweir 1057*cdf0e10cSrcweir oslSocket sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1058*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1059*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 );//sal_True); 1060*cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saSocketAddr ); 1061*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "bind SocketAddr failed", bOK1 == sal_True ); 1062*cdf0e10cSrcweir 1063*cdf0e10cSrcweir sal_Int32 newPort = sSocket.getLocalPort(); 1064*cdf0e10cSrcweir //t_print("#new port is %d\n", newPort ); 1065*cdf0e10cSrcweir 1066*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: port number should be in 1 ~ 65535, set port 0, it should be converted to a port number between 1024~65535.", 1067*cdf0e10cSrcweir ( 1024 <= newPort ) && ( 65535 >= newPort ) && ( bOK == sal_True ) ); 1068*cdf0e10cSrcweir 1069*cdf0e10cSrcweir } 1070*cdf0e10cSrcweir 1071*cdf0e10cSrcweir void setPort_003() 1072*cdf0e10cSrcweir { 1073*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP); 1074*cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_INVAL ); 1075*cdf0e10cSrcweir //on Linux, getPort return 34463 1076*cdf0e10cSrcweir //t_print("#Port number is %d \n", saSocketAddr.getPort( )); 1077*cdf0e10cSrcweir 1078*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an address with invalid port. it should return error or convert it to a valid port.", 1079*cdf0e10cSrcweir ( ( 1 <= saSocketAddr.getPort( ) ) && ( 65535 >= saSocketAddr.getPort( ) ) &&( bOK == sal_True ) ) || 1080*cdf0e10cSrcweir bOK == sal_False); 1081*cdf0e10cSrcweir } 1082*cdf0e10cSrcweir 1083*cdf0e10cSrcweir /* this is not a inet-addr => can't set port */ 1084*cdf0e10cSrcweir void setPort_004() 1085*cdf0e10cSrcweir { 1086*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_FTP); 1087*cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_MYPORT ); 1088*cdf0e10cSrcweir 1089*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an invalid address with valid port. it should return error.", 1090*cdf0e10cSrcweir bOK == sal_False); 1091*cdf0e10cSrcweir } 1092*cdf0e10cSrcweir 1093*cdf0e10cSrcweir 1094*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( setPort ); 1095*cdf0e10cSrcweir CPPUNIT_TEST( setPort_001 ); 1096*cdf0e10cSrcweir CPPUNIT_TEST( setPort_002 ); 1097*cdf0e10cSrcweir CPPUNIT_TEST( setPort_003 ); 1098*cdf0e10cSrcweir CPPUNIT_TEST( setPort_004 ); 1099*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 1100*cdf0e10cSrcweir 1101*cdf0e10cSrcweir }; // class setPort 1102*cdf0e10cSrcweir 1103*cdf0e10cSrcweir 1104*cdf0e10cSrcweir /** tester comment: 1105*cdf0e10cSrcweir 1106*cdf0e10cSrcweir In the following two functions, it use ::rtl::ByteSequence as an intermediate storage for address, 1107*cdf0e10cSrcweir the ByteSequence object can hold sal_Int8 arrays, which is raged [-127, 127], in case of IP addr 1108*cdf0e10cSrcweir that is greater than 127, say 129.158.217.202, it will stored as -127, -98, -39, -54, it is unique 1109*cdf0e10cSrcweir in the range of sal_Int8, but lack of readability. 1110*cdf0e10cSrcweir so may be a sal_uInt8 array is better. 1111*cdf0e10cSrcweir */ 1112*cdf0e10cSrcweir 1113*cdf0e10cSrcweir 1114*cdf0e10cSrcweir /** testing the method: 1115*cdf0e10cSrcweir inline sal_Bool SAL_CALL setAddr( const ::rtl::ByteSequence & address ); 1116*cdf0e10cSrcweir */ 1117*cdf0e10cSrcweir 1118*cdf0e10cSrcweir class setAddr : public CppUnit::TestFixture 1119*cdf0e10cSrcweir { 1120*cdf0e10cSrcweir public: 1121*cdf0e10cSrcweir void setAddr_001() 1122*cdf0e10cSrcweir { 1123*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_FTP ); 1124*cdf0e10cSrcweir saSocketAddr.setAddr( UStringIPToByteSequence( aHostIp1 ) ); 1125*cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 ); 1126*cdf0e10cSrcweir sal_Bool bOK = sal_False; 1127*cdf0e10cSrcweir 1128*cdf0e10cSrcweir if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) && ( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 1129*cdf0e10cSrcweir bOK = sal_True; 1130*cdf0e10cSrcweir 1131*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setAddr() function: construct Addr with \"129.158.217.202\", set it to \"127.0.0.1\", and check the correctness ", 1132*cdf0e10cSrcweir sal_True == bOK ); 1133*cdf0e10cSrcweir } 1134*cdf0e10cSrcweir 1135*cdf0e10cSrcweir 1136*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( setAddr ); 1137*cdf0e10cSrcweir CPPUNIT_TEST( setAddr_001 ); 1138*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 1139*cdf0e10cSrcweir 1140*cdf0e10cSrcweir }; // class setAddr 1141*cdf0e10cSrcweir 1142*cdf0e10cSrcweir 1143*cdf0e10cSrcweir /** testing the method: 1144*cdf0e10cSrcweir inline ::rtl::ByteSequence SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const; 1145*cdf0e10cSrcweir */ 1146*cdf0e10cSrcweir 1147*cdf0e10cSrcweir class getAddr : public CppUnit::TestFixture 1148*cdf0e10cSrcweir { 1149*cdf0e10cSrcweir public: 1150*cdf0e10cSrcweir void getAddr_001() 1151*cdf0e10cSrcweir { 1152*cdf0e10cSrcweir oslSocketResult SocketResult; 1153*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP ); 1154*cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( &SocketResult ); 1155*cdf0e10cSrcweir 1156*cdf0e10cSrcweir sal_Bool bOK = sal_False; 1157*cdf0e10cSrcweir 1158*cdf0e10cSrcweir if ( ( osl_Socket_Ok == SocketResult ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 1159*cdf0e10cSrcweir bOK = sal_True; 1160*cdf0e10cSrcweir 1161*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getAddr() function: construct a socketaddr with IP assigned, get the address to check correctness.Caught unknown exception on (Win32)", 1162*cdf0e10cSrcweir sal_True == bOK && SocketResult == osl_Socket_Ok); 1163*cdf0e10cSrcweir } 1164*cdf0e10cSrcweir 1165*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getAddr ); 1166*cdf0e10cSrcweir CPPUNIT_TEST( getAddr_001 ); 1167*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 1168*cdf0e10cSrcweir 1169*cdf0e10cSrcweir }; // class getAddr 1170*cdf0e10cSrcweir 1171*cdf0e10cSrcweir 1172*cdf0e10cSrcweir /** testing the methods: 1173*cdf0e10cSrcweir inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr); 1174*cdf0e10cSrcweir inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr); 1175*cdf0e10cSrcweir inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy ); 1176*cdf0e10cSrcweir inline sal_Bool SAL_CALL operator== (oslSocketAddr Addr) const; 1177*cdf0e10cSrcweir inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; /// not implemented. 1178*cdf0e10cSrcweir */ 1179*cdf0e10cSrcweir 1180*cdf0e10cSrcweir class operator_equal : public CppUnit::TestFixture 1181*cdf0e10cSrcweir { 1182*cdf0e10cSrcweir public: 1183*cdf0e10cSrcweir void operator_equal_001() 1184*cdf0e10cSrcweir { 1185*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET); 1186*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP ); 1187*cdf0e10cSrcweir 1188*cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr; 1189*cdf0e10cSrcweir sal_Bool bOK = sal_False; 1190*cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 ); 1191*cdf0e10cSrcweir 1192*cdf0e10cSrcweir if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 1193*cdf0e10cSrcweir bOK = sal_True; 1194*cdf0e10cSrcweir 1195*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: use operator= to assign Ip1 to Ip2, check its modification.", 1196*cdf0e10cSrcweir sal_True == bOK ); 1197*cdf0e10cSrcweir } 1198*cdf0e10cSrcweir 1199*cdf0e10cSrcweir 1200*cdf0e10cSrcweir void operator_equal_002() 1201*cdf0e10cSrcweir { 1202*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp3, IP_PORT_TELNET); 1203*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP ); 1204*cdf0e10cSrcweir 1205*cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr; 1206*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "after assign, the assigned SocketAddr is not same as the original Addr", 1207*cdf0e10cSrcweir IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ); 1208*cdf0e10cSrcweir saSocketAddrEqual.setPort( IP_PORT_MYPORT3 ); 1209*cdf0e10cSrcweir saSocketAddr.setPort( IP_PORT_HTTP2 ); 1210*cdf0e10cSrcweir 1211*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: perform an equal action, then try to change the original address's port. it should not be changed ( handle released), it did not pass in (W32), this is under discussion.", 1212*cdf0e10cSrcweir IP_PORT_MYPORT3 == saSocketAddrEqual.getPort( ) ); 1213*cdf0e10cSrcweir } 1214*cdf0e10cSrcweir 1215*cdf0e10cSrcweir void operator_equal_const_001() 1216*cdf0e10cSrcweir { 1217*cdf0e10cSrcweir const ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET); 1218*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP ); 1219*cdf0e10cSrcweir 1220*cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr; 1221*cdf0e10cSrcweir sal_Bool bOK = sal_False; 1222*cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 ); 1223*cdf0e10cSrcweir 1224*cdf0e10cSrcweir if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 1225*cdf0e10cSrcweir bOK = sal_True; 1226*cdf0e10cSrcweir 1227*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: use operator= const to assign Ip1 to Ip2, verify the change on the second one.", 1228*cdf0e10cSrcweir sal_True == bOK ); 1229*cdf0e10cSrcweir } 1230*cdf0e10cSrcweir 1231*cdf0e10cSrcweir void operator_equal_const_002() 1232*cdf0e10cSrcweir { 1233*cdf0e10cSrcweir const ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET); 1234*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP ); 1235*cdf0e10cSrcweir 1236*cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr; 1237*cdf0e10cSrcweir saSocketAddrEqual.setPort( IP_PORT_HTTP1 ); 1238*cdf0e10cSrcweir 1239*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: change the second instance, the first one should not be altered, since it does not released the handle.", 1240*cdf0e10cSrcweir IP_PORT_HTTP1 != saSocketAddr.getPort( ) ); 1241*cdf0e10cSrcweir } 1242*cdf0e10cSrcweir 1243*cdf0e10cSrcweir void operator_equal_assign_001() 1244*cdf0e10cSrcweir { 1245*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostIp1, IP_PORT_TELNET ); 1246*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 1247*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrAssign = new ::osl::SocketAddr( aHostIp2, IP_PORT_FTP ); 1248*cdf0e10cSrcweir oslSocketAddr poslSocketAddr = pSocketAddr->getHandle( ); 1249*cdf0e10cSrcweir //if( m_handle ) osl_destroySocketAddr( m_handle ); so pSocketAddrAssign had been destroyed and then point to pSocketAddr 1250*cdf0e10cSrcweir pSocketAddrAssign->assign(poslSocketAddr, SAL_NO_COPY); 1251*cdf0e10cSrcweir 1252*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", 1253*cdf0e10cSrcweir pSocketAddrAssign->getPort( ) == IP_PORT_TELNET ); 1254*cdf0e10cSrcweir 1255*cdf0e10cSrcweir delete pSocketAddrAssign; 1256*cdf0e10cSrcweir } 1257*cdf0e10cSrcweir 1258*cdf0e10cSrcweir void operator_is_equal_001() 1259*cdf0e10cSrcweir { 1260*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET); 1261*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrequal( aHostIp1, IP_PORT_TELNET ); 1262*cdf0e10cSrcweir 1263*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two identical Address.", 1264*cdf0e10cSrcweir sal_True == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) ); 1265*cdf0e10cSrcweir } 1266*cdf0e10cSrcweir 1267*cdf0e10cSrcweir void operator_is_equal_002() 1268*cdf0e10cSrcweir { 1269*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_FTP); 1270*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrequal( aHostIp1, IP_PORT_TELNET ); 1271*cdf0e10cSrcweir 1272*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two different Address.", 1273*cdf0e10cSrcweir sal_False == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) ); 1274*cdf0e10cSrcweir } 1275*cdf0e10cSrcweir 1276*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( operator_equal ); 1277*cdf0e10cSrcweir CPPUNIT_TEST( operator_equal_001 ); 1278*cdf0e10cSrcweir CPPUNIT_TEST( operator_equal_002 ); 1279*cdf0e10cSrcweir CPPUNIT_TEST( operator_equal_const_001 ); 1280*cdf0e10cSrcweir CPPUNIT_TEST( operator_equal_const_002 ); 1281*cdf0e10cSrcweir CPPUNIT_TEST( operator_equal_assign_001 ); 1282*cdf0e10cSrcweir CPPUNIT_TEST( operator_is_equal_001 ); 1283*cdf0e10cSrcweir CPPUNIT_TEST( operator_is_equal_002 ); 1284*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 1285*cdf0e10cSrcweir 1286*cdf0e10cSrcweir }; // class operator_equal 1287*cdf0e10cSrcweir 1288*cdf0e10cSrcweir 1289*cdf0e10cSrcweir 1290*cdf0e10cSrcweir /** testing the method: 1291*cdf0e10cSrcweir inline oslSocketAddr SAL_CALL getHandle() const; 1292*cdf0e10cSrcweir */ 1293*cdf0e10cSrcweir 1294*cdf0e10cSrcweir class getSocketAddrHandle : public CppUnit::TestFixture 1295*cdf0e10cSrcweir { 1296*cdf0e10cSrcweir public: 1297*cdf0e10cSrcweir 1298*cdf0e10cSrcweir void getSocketAddrHandle_001() 1299*cdf0e10cSrcweir { 1300*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 ); 1301*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 1302*cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); 1303*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); 1304*cdf0e10cSrcweir 1305*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.", 1306*cdf0e10cSrcweir pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) ); 1307*cdf0e10cSrcweir 1308*cdf0e10cSrcweir delete pSocketAddrCopy; 1309*cdf0e10cSrcweir } 1310*cdf0e10cSrcweir 1311*cdf0e10cSrcweir void getSocketAddrHandle_002() 1312*cdf0e10cSrcweir { 1313*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName3, IP_PORT_MYPORT4 ); 1314*cdf0e10cSrcweir oslSocketAddr poslSocketAddr = saSocketAddr.getHandle( ); 1315*cdf0e10cSrcweir 1316*cdf0e10cSrcweir sal_Bool bOK = ( saSocketAddr == poslSocketAddr ); 1317*cdf0e10cSrcweir //t_print("getSocketAddrHandle_002\n"); 1318*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getHandle() function: use getHandle() function as an intermediate way to create identical address.", 1319*cdf0e10cSrcweir sal_True == bOK ); 1320*cdf0e10cSrcweir } 1321*cdf0e10cSrcweir 1322*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getSocketAddrHandle ); 1323*cdf0e10cSrcweir CPPUNIT_TEST( getSocketAddrHandle_001 ); 1324*cdf0e10cSrcweir CPPUNIT_TEST( getSocketAddrHandle_002 ); 1325*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 1326*cdf0e10cSrcweir 1327*cdf0e10cSrcweir }; // class getSocketAddrHandle 1328*cdf0e10cSrcweir 1329*cdf0e10cSrcweir 1330*cdf0e10cSrcweir /** testing the method: 1331*cdf0e10cSrcweir static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0); 1332*cdf0e10cSrcweir */ 1333*cdf0e10cSrcweir 1334*cdf0e10cSrcweir class getLocalHostname : public CppUnit::TestFixture 1335*cdf0e10cSrcweir { 1336*cdf0e10cSrcweir public: 1337*cdf0e10cSrcweir /* the process of getLocalHostname: 1.gethostname (same as /bin/hostname) returned name A 1338*cdf0e10cSrcweir 2. search A in /etc/hosts, if there is an alias name is A, return the name in the same row 1339*cdf0e10cSrcweir */ 1340*cdf0e10cSrcweir 1341*cdf0e10cSrcweir void getLocalHostname_000() 1342*cdf0e10cSrcweir { 1343*cdf0e10cSrcweir // _osl_getFullQualifiedDomainName( ); 1344*cdf0e10cSrcweir oslSocketResult aResult = osl_Socket_Error; 1345*cdf0e10cSrcweir rtl::OUString suHostname = osl::SocketAddr::getLocalHostname(&aResult); 1346*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("getLocalHostname failed", aResult == osl_Socket_Ok); 1347*cdf0e10cSrcweir } 1348*cdf0e10cSrcweir 1349*cdf0e10cSrcweir void getLocalHostname_001() 1350*cdf0e10cSrcweir { 1351*cdf0e10cSrcweir oslSocketResult *pResult = NULL; 1352*cdf0e10cSrcweir //printSocketResult(*pResult); 1353*cdf0e10cSrcweir ::rtl::OUString suResult = ::osl::SocketAddr::getLocalHostname( pResult ); 1354*cdf0e10cSrcweir 1355*cdf0e10cSrcweir // LLA: IMHO localhost, or hostname by itself should be ok. 1356*cdf0e10cSrcweir rtl::OUString suThisHost = getThisHostname( ); 1357*cdf0e10cSrcweir bool bOk = false; 1358*cdf0e10cSrcweir if (suThisHost.equals(rtl::OUString::createFromAscii("localhost"))) 1359*cdf0e10cSrcweir { 1360*cdf0e10cSrcweir bOk = true; 1361*cdf0e10cSrcweir } 1362*cdf0e10cSrcweir else 1363*cdf0e10cSrcweir { 1364*cdf0e10cSrcweir if (suThisHost.equals(suResult)) 1365*cdf0e10cSrcweir { 1366*cdf0e10cSrcweir bOk = true; 1367*cdf0e10cSrcweir } 1368*cdf0e10cSrcweir } 1369*cdf0e10cSrcweir 1370*cdf0e10cSrcweir ::rtl::OUString suError; 1371*cdf0e10cSrcweir suError = outputError(suResult, getThisHostname( ), "test for getLocalHostname() function"); 1372*cdf0e10cSrcweir 1373*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, bOk == true ); 1374*cdf0e10cSrcweir } 1375*cdf0e10cSrcweir 1376*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getLocalHostname ); 1377*cdf0e10cSrcweir CPPUNIT_TEST( getLocalHostname_000 ); 1378*cdf0e10cSrcweir CPPUNIT_TEST( getLocalHostname_001 ); 1379*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 1380*cdf0e10cSrcweir 1381*cdf0e10cSrcweir }; // class getLocalHostname 1382*cdf0e10cSrcweir 1383*cdf0e10cSrcweir 1384*cdf0e10cSrcweir /** testing the method: 1385*cdf0e10cSrcweir static inline void SAL_CALL resolveHostname( const ::rtl::OUString & strHostName , SocketAddr & Addr ); 1386*cdf0e10cSrcweir */ 1387*cdf0e10cSrcweir 1388*cdf0e10cSrcweir class resolveHostname : public CppUnit::TestFixture 1389*cdf0e10cSrcweir { 1390*cdf0e10cSrcweir public: 1391*cdf0e10cSrcweir void resolveHostname_001() 1392*cdf0e10cSrcweir { 1393*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr; 1394*cdf0e10cSrcweir ::osl::SocketAddr::resolveHostname( aHostIp1, saSocketAddr ); 1395*cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 ); 1396*cdf0e10cSrcweir sal_Bool bOK = sal_False; 1397*cdf0e10cSrcweir 1398*cdf0e10cSrcweir if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 1399*cdf0e10cSrcweir bOK = sal_True; 1400*cdf0e10cSrcweir 1401*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for resolveHostname() function: try to resolve localhost to 127.0.0.1.", 1402*cdf0e10cSrcweir sal_True == bOK ); 1403*cdf0e10cSrcweir } 1404*cdf0e10cSrcweir 1405*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( resolveHostname ); 1406*cdf0e10cSrcweir CPPUNIT_TEST( resolveHostname_001 ); 1407*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 1408*cdf0e10cSrcweir 1409*cdf0e10cSrcweir }; // class resolveHostname 1410*cdf0e10cSrcweir 1411*cdf0e10cSrcweir 1412*cdf0e10cSrcweir /** testing the method: 1413*cdf0e10cSrcweir static inline sal_Int32 SAL_CALL getServicePort( 1414*cdf0e10cSrcweir const ::rtl::OUString& strServiceName, 1415*cdf0e10cSrcweir const ::rtl::OUString & strProtocolName= ::rtl::OUString::createFromAscii( "tcp" ) ); 1416*cdf0e10cSrcweir */ 1417*cdf0e10cSrcweir 1418*cdf0e10cSrcweir class gettheServicePort : public CppUnit::TestFixture 1419*cdf0e10cSrcweir { 1420*cdf0e10cSrcweir public: 1421*cdf0e10cSrcweir void gettheServicePort_001() 1422*cdf0e10cSrcweir { 1423*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get ftp service port on TCP protocol.", 1424*cdf0e10cSrcweir IP_PORT_FTP== ::osl::SocketAddr::getServicePort( aServiceFTP, aProtocolTCP ) ); 1425*cdf0e10cSrcweir } 1426*cdf0e10cSrcweir 1427*cdf0e10cSrcweir void gettheServicePort_002() 1428*cdf0e10cSrcweir { 1429*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get telnet service port on TCP protocol.", 1430*cdf0e10cSrcweir IP_PORT_TELNET== ::osl::SocketAddr::getServicePort( aServiceTELNET, aProtocolTCP ) ); 1431*cdf0e10cSrcweir } 1432*cdf0e10cSrcweir 1433*cdf0e10cSrcweir void gettheServicePort_003() 1434*cdf0e10cSrcweir { 1435*cdf0e10cSrcweir //Solaris has no service called "https", please see /etc/services 1436*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get netbios-ssn service port on UDP protocol.", 1437*cdf0e10cSrcweir IP_PORT_NETBIOS_DGM == ::osl::SocketAddr::getServicePort( aServiceNETBIOS, aProtocolUDP ) ); 1438*cdf0e10cSrcweir } 1439*cdf0e10cSrcweir 1440*cdf0e10cSrcweir void gettheServicePort_004() 1441*cdf0e10cSrcweir { 1442*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get a service port which is not exist.", 1443*cdf0e10cSrcweir OSL_INVALID_PORT == ::osl::SocketAddr::getServicePort( ::rtl::OUString::createFromAscii( "notexist" ), aProtocolUDP ) ); 1444*cdf0e10cSrcweir } 1445*cdf0e10cSrcweir 1446*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( gettheServicePort ); 1447*cdf0e10cSrcweir CPPUNIT_TEST( gettheServicePort_001 ); 1448*cdf0e10cSrcweir CPPUNIT_TEST( gettheServicePort_002 ); 1449*cdf0e10cSrcweir CPPUNIT_TEST( gettheServicePort_003 ); 1450*cdf0e10cSrcweir CPPUNIT_TEST( gettheServicePort_004 ); 1451*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 1452*cdf0e10cSrcweir 1453*cdf0e10cSrcweir }; // class gettheServicePort 1454*cdf0e10cSrcweir 1455*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1456*cdf0e10cSrcweir 1457*cdf0e10cSrcweir 1458*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::ctors, "osl_SocketAddr"); 1459*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::is, "osl_SocketAddr"); 1460*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getHostname, "osl_SocketAddr"); 1461*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getPort, "osl_SocketAddr"); 1462*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setPort, "osl_SocketAddr"); 1463*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setAddr, "osl_SocketAddr"); 1464*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getAddr, "osl_SocketAddr"); 1465*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::operator_equal, "osl_SocketAddr"); 1466*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getSocketAddrHandle, "osl_SocketAddr"); 1467*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getLocalHostname, "osl_SocketAddr"); 1468*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::resolveHostname, "osl_SocketAddr"); 1469*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::gettheServicePort, "osl_SocketAddr"); 1470*cdf0e10cSrcweir 1471*cdf0e10cSrcweir 1472*cdf0e10cSrcweir } // namespace osl_SocketAddr 1473*cdf0e10cSrcweir 1474*cdf0e10cSrcweir 1475*cdf0e10cSrcweir 1476*cdf0e10cSrcweir namespace osl_Socket 1477*cdf0e10cSrcweir { 1478*cdf0e10cSrcweir 1479*cdf0e10cSrcweir /** testing the methods: 1480*cdf0e10cSrcweir inline Socket( ); 1481*cdf0e10cSrcweir inline Socket( const Socket & socket ); 1482*cdf0e10cSrcweir inline Socket( oslSocket socketHandle ); 1483*cdf0e10cSrcweir inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire ); 1484*cdf0e10cSrcweir */ 1485*cdf0e10cSrcweir 1486*cdf0e10cSrcweir /** test writer's comment: 1487*cdf0e10cSrcweir 1488*cdf0e10cSrcweir class Socket can not be initialized by its protected constructor, though the protected 1489*cdf0e10cSrcweir constructor is the most convenient way to create a new socket. 1490*cdf0e10cSrcweir it only allow the method of C function osl_createSocket like: 1491*cdf0e10cSrcweir ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, 1492*cdf0e10cSrcweir osl_Socket_ProtocolIp ) ); 1493*cdf0e10cSrcweir the use of C method lost some of the transparent of tester using C++ wrapper. 1494*cdf0e10cSrcweir */ 1495*cdf0e10cSrcweir 1496*cdf0e10cSrcweir 1497*cdf0e10cSrcweir class ctors : public CppUnit::TestFixture 1498*cdf0e10cSrcweir { 1499*cdf0e10cSrcweir public: 1500*cdf0e10cSrcweir oslSocket sHandle; 1501*cdf0e10cSrcweir // initialization 1502*cdf0e10cSrcweir void setUp( ) 1503*cdf0e10cSrcweir { 1504*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1505*cdf0e10cSrcweir } 1506*cdf0e10cSrcweir 1507*cdf0e10cSrcweir void tearDown( ) 1508*cdf0e10cSrcweir { 1509*cdf0e10cSrcweir sHandle = NULL; 1510*cdf0e10cSrcweir } 1511*cdf0e10cSrcweir 1512*cdf0e10cSrcweir 1513*cdf0e10cSrcweir void ctors_none() 1514*cdf0e10cSrcweir { 1515*cdf0e10cSrcweir /// Socket constructor. 1516*cdf0e10cSrcweir // ::osl::Socket sSocket; 1517*cdf0e10cSrcweir 1518*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the socket was created successfully, if no exception occured", 1519*cdf0e10cSrcweir 1 == 1 ); 1520*cdf0e10cSrcweir } 1521*cdf0e10cSrcweir 1522*cdf0e10cSrcweir void ctors_acquire() 1523*cdf0e10cSrcweir { 1524*cdf0e10cSrcweir /// Socket constructor. 1525*cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 1526*cdf0e10cSrcweir 1527*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully", 1528*cdf0e10cSrcweir osl_Socket_TypeStream == sSocket.getType( ) ); 1529*cdf0e10cSrcweir } 1530*cdf0e10cSrcweir 1531*cdf0e10cSrcweir void ctors_no_acquire() 1532*cdf0e10cSrcweir { 1533*cdf0e10cSrcweir /// Socket constructor. 1534*cdf0e10cSrcweir ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE ); 1535*cdf0e10cSrcweir 1536*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully", 1537*cdf0e10cSrcweir osl_Socket_TypeStream == sSocket.getType( ) ); 1538*cdf0e10cSrcweir } 1539*cdf0e10cSrcweir 1540*cdf0e10cSrcweir void ctors_copy_ctor() 1541*cdf0e10cSrcweir { 1542*cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 1543*cdf0e10cSrcweir /// Socket copy constructor. 1544*cdf0e10cSrcweir ::osl::Socket copySocket( sSocket ); 1545*cdf0e10cSrcweir 1546*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor", 1547*cdf0e10cSrcweir osl_Socket_TypeStream == copySocket.getType( ) ); 1548*cdf0e10cSrcweir } 1549*cdf0e10cSrcweir 1550*cdf0e10cSrcweir void ctors_TypeRaw() 1551*cdf0e10cSrcweir { 1552*cdf0e10cSrcweir #ifdef WNT 1553*cdf0e10cSrcweir oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 1554*cdf0e10cSrcweir // LLA: ? ::osl::Socket sSocket( sHandleRaw ); 1555*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( " type osl_Socket_TypeRaw socket create failed on UNX ", sHandleRaw != NULL); 1556*cdf0e10cSrcweir #else 1557*cdf0e10cSrcweir oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 1558*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( " can't create socket with type osl_Socket_TypeRaw within UNX is ok.", sHandleRaw == NULL); 1559*cdf0e10cSrcweir #endif 1560*cdf0e10cSrcweir } 1561*cdf0e10cSrcweir 1562*cdf0e10cSrcweir void ctors_family_Ipx() 1563*cdf0e10cSrcweir { 1564*cdf0e10cSrcweir oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1565*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( " family osl_Socket_FamilyIpx socket create failed! ", sHandleIpx != NULL); 1566*cdf0e10cSrcweir ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE ); 1567*cdf0e10cSrcweir t_print("#Type is %d \n", sSocket.getType( ) ); 1568*cdf0e10cSrcweir 1569*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for create new Socket instance that family is osl_Socket_FamilyIpx", 1570*cdf0e10cSrcweir osl_Socket_TypeStream == sSocket.getType( ) ); 1571*cdf0e10cSrcweir } 1572*cdf0e10cSrcweir 1573*cdf0e10cSrcweir 1574*cdf0e10cSrcweir 1575*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( ctors ); 1576*cdf0e10cSrcweir CPPUNIT_TEST( ctors_none ); 1577*cdf0e10cSrcweir CPPUNIT_TEST( ctors_acquire ); 1578*cdf0e10cSrcweir CPPUNIT_TEST( ctors_no_acquire ); 1579*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy_ctor ); 1580*cdf0e10cSrcweir CPPUNIT_TEST( ctors_TypeRaw ); 1581*cdf0e10cSrcweir CPPUNIT_TEST( ctors_family_Ipx ); 1582*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1583*cdf0e10cSrcweir 1584*cdf0e10cSrcweir }; // class ctors 1585*cdf0e10cSrcweir 1586*cdf0e10cSrcweir 1587*cdf0e10cSrcweir /** testing the methods: 1588*cdf0e10cSrcweir inline Socket& SAL_CALL operator= ( oslSocket socketHandle); 1589*cdf0e10cSrcweir inline Socket& SAL_CALL operator= (const Socket& sock); 1590*cdf0e10cSrcweir inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ; 1591*cdf0e10cSrcweir inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const; 1592*cdf0e10cSrcweir */ 1593*cdf0e10cSrcweir 1594*cdf0e10cSrcweir class operators : public CppUnit::TestFixture 1595*cdf0e10cSrcweir { 1596*cdf0e10cSrcweir public: 1597*cdf0e10cSrcweir oslSocket sHandle; 1598*cdf0e10cSrcweir // initialization 1599*cdf0e10cSrcweir void setUp( ) 1600*cdf0e10cSrcweir { 1601*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1602*cdf0e10cSrcweir } 1603*cdf0e10cSrcweir 1604*cdf0e10cSrcweir void tearDown( ) 1605*cdf0e10cSrcweir { 1606*cdf0e10cSrcweir sHandle = NULL; 1607*cdf0e10cSrcweir } 1608*cdf0e10cSrcweir 1609*cdf0e10cSrcweir 1610*cdf0e10cSrcweir /** test writer's comment: 1611*cdf0e10cSrcweir 1612*cdf0e10cSrcweir the assignment operator does not support direct assinment like: 1613*cdf0e10cSrcweir ::osl::Socket sSocket = sHandle. 1614*cdf0e10cSrcweir */ 1615*cdf0e10cSrcweir void operators_assignment_handle() 1616*cdf0e10cSrcweir { 1617*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1618*cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket.getHandle(); 1619*cdf0e10cSrcweir 1620*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.", 1621*cdf0e10cSrcweir osl_Socket_TypeStream == assignSocket.getType( ) ); 1622*cdf0e10cSrcweir } 1623*cdf0e10cSrcweir 1624*cdf0e10cSrcweir void operators_assignment() 1625*cdf0e10cSrcweir { 1626*cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 1627*cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket; 1628*cdf0e10cSrcweir 1629*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator", 1630*cdf0e10cSrcweir osl_Socket_TypeStream == assignSocket.getType( ) ); 1631*cdf0e10cSrcweir } 1632*cdf0e10cSrcweir 1633*cdf0e10cSrcweir void operators_equal_handle_001() 1634*cdf0e10cSrcweir { 1635*cdf0e10cSrcweir /// Socket constructor. 1636*cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 1637*cdf0e10cSrcweir ::osl::Socket equalSocket = sSocket; 1638*cdf0e10cSrcweir 1639*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check equal.", 1640*cdf0e10cSrcweir equalSocket == sHandle ); 1641*cdf0e10cSrcweir } 1642*cdf0e10cSrcweir 1643*cdf0e10cSrcweir void operators_equal_handle_002() 1644*cdf0e10cSrcweir { 1645*cdf0e10cSrcweir /// Socket constructor. 1646*cdf0e10cSrcweir ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); 1647*cdf0e10cSrcweir 1648*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check unequal.", 1649*cdf0e10cSrcweir !( equalSocket == sHandle ) ); 1650*cdf0e10cSrcweir } 1651*cdf0e10cSrcweir 1652*cdf0e10cSrcweir void operators_equal_001() 1653*cdf0e10cSrcweir { 1654*cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 1655*cdf0e10cSrcweir /// Socket copy constructor. 1656*cdf0e10cSrcweir ::osl::Socket equalSocket( sSocket ); 1657*cdf0e10cSrcweir 1658*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for operators_equal function: check equal.", 1659*cdf0e10cSrcweir equalSocket == sSocket ); 1660*cdf0e10cSrcweir } 1661*cdf0e10cSrcweir 1662*cdf0e10cSrcweir void operators_equal_002() 1663*cdf0e10cSrcweir { 1664*cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 1665*cdf0e10cSrcweir /// Socket copy constructor. 1666*cdf0e10cSrcweir ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); 1667*cdf0e10cSrcweir 1668*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_002 function: check unequal.", 1669*cdf0e10cSrcweir !( equalSocket == sSocket ) ); 1670*cdf0e10cSrcweir } 1671*cdf0e10cSrcweir 1672*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( operators ); 1673*cdf0e10cSrcweir CPPUNIT_TEST( operators_assignment_handle ); 1674*cdf0e10cSrcweir CPPUNIT_TEST( operators_assignment ); 1675*cdf0e10cSrcweir CPPUNIT_TEST( operators_equal_handle_001 ); 1676*cdf0e10cSrcweir CPPUNIT_TEST( operators_equal_handle_002 ); 1677*cdf0e10cSrcweir CPPUNIT_TEST( operators_equal_001 ); 1678*cdf0e10cSrcweir CPPUNIT_TEST( operators_equal_002 ); 1679*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1680*cdf0e10cSrcweir 1681*cdf0e10cSrcweir }; // class operators 1682*cdf0e10cSrcweir 1683*cdf0e10cSrcweir 1684*cdf0e10cSrcweir /** testing the methods: 1685*cdf0e10cSrcweir inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite ); 1686*cdf0e10cSrcweir inline void SAL_CALL close(); 1687*cdf0e10cSrcweir */ 1688*cdf0e10cSrcweir 1689*cdf0e10cSrcweir class close : public CppUnit::TestFixture 1690*cdf0e10cSrcweir { 1691*cdf0e10cSrcweir public: 1692*cdf0e10cSrcweir oslSocket sHandle; 1693*cdf0e10cSrcweir // initialization 1694*cdf0e10cSrcweir void setUp( ) 1695*cdf0e10cSrcweir { 1696*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1697*cdf0e10cSrcweir } 1698*cdf0e10cSrcweir 1699*cdf0e10cSrcweir void tearDown( ) 1700*cdf0e10cSrcweir { 1701*cdf0e10cSrcweir sHandle = NULL; 1702*cdf0e10cSrcweir } 1703*cdf0e10cSrcweir 1704*cdf0e10cSrcweir 1705*cdf0e10cSrcweir void close_001() 1706*cdf0e10cSrcweir { 1707*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1708*cdf0e10cSrcweir sSocket.close(); 1709*cdf0e10cSrcweir 1710*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for close_001 function: this function is reserved for test.", 1711*cdf0e10cSrcweir sSocket.getHandle() == sHandle ); 1712*cdf0e10cSrcweir } 1713*cdf0e10cSrcweir 1714*cdf0e10cSrcweir void close_002() 1715*cdf0e10cSrcweir { 1716*cdf0e10cSrcweir //#if defined(LINUX) 1717*cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 1718*cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocket, aHostIp1 ); 1719*cdf0e10cSrcweir myAcceptorThread.create(); 1720*cdf0e10cSrcweir 1721*cdf0e10cSrcweir thread_sleep( 1 ); 1722*cdf0e10cSrcweir //when accepting, close the socket, the thread will not block for accepting 1723*cdf0e10cSrcweir //man close:Any locks held on the file it was associated with, and owned by the process, are removed 1724*cdf0e10cSrcweir asSocket.close(); 1725*cdf0e10cSrcweir //thread_sleep( 2 ); 1726*cdf0e10cSrcweir myAcceptorThread.join(); 1727*cdf0e10cSrcweir 1728*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", 1729*cdf0e10cSrcweir myAcceptorThread.isOK() == sal_True ); 1730*cdf0e10cSrcweir //#endif 1731*cdf0e10cSrcweir } 1732*cdf0e10cSrcweir 1733*cdf0e10cSrcweir // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( ) 1734*cdf0e10cSrcweir void close_003() 1735*cdf0e10cSrcweir { 1736*cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 1737*cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocket, aHostIpZero ); 1738*cdf0e10cSrcweir myAcceptorThread.create(); 1739*cdf0e10cSrcweir 1740*cdf0e10cSrcweir thread_sleep( 1 ); 1741*cdf0e10cSrcweir asSocket.close(); 1742*cdf0e10cSrcweir myAcceptorThread.join(); 1743*cdf0e10cSrcweir 1744*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", 1745*cdf0e10cSrcweir myAcceptorThread.isOK() == sal_True ); 1746*cdf0e10cSrcweir } 1747*cdf0e10cSrcweir 1748*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( close ); 1749*cdf0e10cSrcweir CPPUNIT_TEST( close_001 ); 1750*cdf0e10cSrcweir CPPUNIT_TEST( close_002 ); 1751*cdf0e10cSrcweir CPPUNIT_TEST( close_003 ); 1752*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1753*cdf0e10cSrcweir 1754*cdf0e10cSrcweir }; // class close 1755*cdf0e10cSrcweir 1756*cdf0e10cSrcweir /** testing the method: 1757*cdf0e10cSrcweir inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const; 1758*cdf0e10cSrcweir */ 1759*cdf0e10cSrcweir 1760*cdf0e10cSrcweir class getLocalAddr : public CppUnit::TestFixture 1761*cdf0e10cSrcweir { 1762*cdf0e10cSrcweir public: 1763*cdf0e10cSrcweir oslSocket sHandle; 1764*cdf0e10cSrcweir // initialization 1765*cdf0e10cSrcweir void setUp( ) 1766*cdf0e10cSrcweir { 1767*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1768*cdf0e10cSrcweir } 1769*cdf0e10cSrcweir 1770*cdf0e10cSrcweir void tearDown( ) 1771*cdf0e10cSrcweir { 1772*cdf0e10cSrcweir sHandle = NULL; 1773*cdf0e10cSrcweir } 1774*cdf0e10cSrcweir 1775*cdf0e10cSrcweir // get the Address of the local end of the socket 1776*cdf0e10cSrcweir void getLocalAddr_001() 1777*cdf0e10cSrcweir { 1778*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1779*cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT8 ); 1780*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 1781*cdf0e10cSrcweir 1782*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1783*cdf0e10cSrcweir 1784*cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 1785*cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 1786*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 1787*cdf0e10cSrcweir 1788*cdf0e10cSrcweir sSocket.getLocalAddr( saLocalSocketAddr ); 1789*cdf0e10cSrcweir 1790*cdf0e10cSrcweir sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ; 1791*cdf0e10cSrcweir 1792*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.", 1793*cdf0e10cSrcweir sal_True == bOK ); 1794*cdf0e10cSrcweir } 1795*cdf0e10cSrcweir 1796*cdf0e10cSrcweir 1797*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getLocalAddr ); 1798*cdf0e10cSrcweir CPPUNIT_TEST( getLocalAddr_001 ); 1799*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1800*cdf0e10cSrcweir 1801*cdf0e10cSrcweir }; // class getLocalAddr 1802*cdf0e10cSrcweir 1803*cdf0e10cSrcweir 1804*cdf0e10cSrcweir /** testing the method: 1805*cdf0e10cSrcweir inline sal_Int32 SAL_CALL getLocalPort() const; 1806*cdf0e10cSrcweir */ 1807*cdf0e10cSrcweir 1808*cdf0e10cSrcweir class getLocalPort : public CppUnit::TestFixture 1809*cdf0e10cSrcweir { 1810*cdf0e10cSrcweir public: 1811*cdf0e10cSrcweir oslSocket sHandle; 1812*cdf0e10cSrcweir // initialization 1813*cdf0e10cSrcweir void setUp( ) 1814*cdf0e10cSrcweir { 1815*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1816*cdf0e10cSrcweir } 1817*cdf0e10cSrcweir 1818*cdf0e10cSrcweir void tearDown( ) 1819*cdf0e10cSrcweir { 1820*cdf0e10cSrcweir sHandle = NULL; 1821*cdf0e10cSrcweir } 1822*cdf0e10cSrcweir 1823*cdf0e10cSrcweir 1824*cdf0e10cSrcweir void getLocalPort_001() 1825*cdf0e10cSrcweir { 1826*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1827*cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT7 ); // aHostIp1 localhost 1828*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 1829*cdf0e10cSrcweir 1830*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1831*cdf0e10cSrcweir 1832*cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 1833*cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 1834*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 1835*cdf0e10cSrcweir sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) ); 1836*cdf0e10cSrcweir 1837*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.", 1838*cdf0e10cSrcweir sal_True == bOK ); 1839*cdf0e10cSrcweir } 1840*cdf0e10cSrcweir 1841*cdf0e10cSrcweir /** test writer's comment: 1842*cdf0e10cSrcweir 1843*cdf0e10cSrcweir the invalid port number can not be set by giving invalid port number 1844*cdf0e10cSrcweir such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be 1845*cdf0e10cSrcweir valid, the only instance that the getLocalPort returns OSL_INVALID_PORT 1846*cdf0e10cSrcweir is when saSocketAddr itself is an invalid one, that is , the IP or host name 1847*cdf0e10cSrcweir can not be found, then the created socket address is not valid. 1848*cdf0e10cSrcweir */ 1849*cdf0e10cSrcweir void getLocalPort_002() 1850*cdf0e10cSrcweir { 1851*cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_TELNET); 1852*cdf0e10cSrcweir #ifdef WNT 1853*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1854*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 1855*cdf0e10cSrcweir sSocket.bind( saBindSocketAddr ); 1856*cdf0e10cSrcweir //Invalid IP, so bind should fail 1857*cdf0e10cSrcweir ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), 1858*cdf0e10cSrcweir ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT), 1859*cdf0e10cSrcweir "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned."); 1860*cdf0e10cSrcweir sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) ); 1861*cdf0e10cSrcweir (void)bOK; 1862*cdf0e10cSrcweir #else 1863*cdf0e10cSrcweir //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT 1864*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"); 1865*cdf0e10cSrcweir #endif 1866*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_False ); 1867*cdf0e10cSrcweir 1868*cdf0e10cSrcweir } 1869*cdf0e10cSrcweir 1870*cdf0e10cSrcweir void getLocalPort_003() 1871*cdf0e10cSrcweir { 1872*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1873*cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL); 1874*cdf0e10cSrcweir 1875*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1876*cdf0e10cSrcweir 1877*cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 1878*cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 1879*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 1880*cdf0e10cSrcweir ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), 1881*cdf0e10cSrcweir ::rtl::OUString::createFromAscii("34463"), 1882*cdf0e10cSrcweir "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned"); 1883*cdf0e10cSrcweir sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535); 1884*cdf0e10cSrcweir 1885*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 1886*cdf0e10cSrcweir } 1887*cdf0e10cSrcweir 1888*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getLocalPort ); 1889*cdf0e10cSrcweir CPPUNIT_TEST( getLocalPort_001 ); 1890*cdf0e10cSrcweir // LLA: CPPUNIT_TEST( getLocalPort_002 ); 1891*cdf0e10cSrcweir CPPUNIT_TEST( getLocalPort_003 ); 1892*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1893*cdf0e10cSrcweir 1894*cdf0e10cSrcweir }; // class getLocalPort 1895*cdf0e10cSrcweir 1896*cdf0e10cSrcweir 1897*cdf0e10cSrcweir /** testing the method: 1898*cdf0e10cSrcweir inline ::rtl::OUString SAL_CALL getLocalHost() const; 1899*cdf0e10cSrcweir 1900*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; 1901*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 1902*cdf0e10cSrcweir will return hostname of current processor such as "aegean.PRC.Sun.COM" 1903*cdf0e10cSrcweir */ 1904*cdf0e10cSrcweir 1905*cdf0e10cSrcweir class getLocalHost : public CppUnit::TestFixture 1906*cdf0e10cSrcweir { 1907*cdf0e10cSrcweir public: 1908*cdf0e10cSrcweir oslSocket sHandle; 1909*cdf0e10cSrcweir // initialization 1910*cdf0e10cSrcweir void setUp( ) 1911*cdf0e10cSrcweir { 1912*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1913*cdf0e10cSrcweir } 1914*cdf0e10cSrcweir 1915*cdf0e10cSrcweir void tearDown( ) 1916*cdf0e10cSrcweir { 1917*cdf0e10cSrcweir sHandle = NULL; 1918*cdf0e10cSrcweir } 1919*cdf0e10cSrcweir 1920*cdf0e10cSrcweir 1921*cdf0e10cSrcweir void getLocalHost_001() 1922*cdf0e10cSrcweir { 1923*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1924*cdf0e10cSrcweir //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu 1925*cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT6 ); 1926*cdf0e10cSrcweir 1927*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1928*cdf0e10cSrcweir 1929*cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 1930*cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 1931*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 1932*cdf0e10cSrcweir sal_Bool bOK; 1933*cdf0e10cSrcweir ::rtl::OUString suError; 1934*cdf0e10cSrcweir #ifdef WNT 1935*cdf0e10cSrcweir bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; 1936*cdf0e10cSrcweir suError = outputError(sSocket.getLocalHost( ), getThisHostname( ), 1937*cdf0e10cSrcweir "test for getLocalHost function: create localhost socket and check name"); 1938*cdf0e10cSrcweir #else 1939*cdf0e10cSrcweir ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" ); 1940*cdf0e10cSrcweir sal_Bool bRes1, bRes2; 1941*cdf0e10cSrcweir bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ; 1942*cdf0e10cSrcweir bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ; 1943*cdf0e10cSrcweir bOK = bRes1 || bRes2; 1944*cdf0e10cSrcweir suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name"); 1945*cdf0e10cSrcweir #endif 1946*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 1947*cdf0e10cSrcweir } 1948*cdf0e10cSrcweir 1949*cdf0e10cSrcweir void getLocalHost_002() 1950*cdf0e10cSrcweir { 1951*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 1952*cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_POP3); 1953*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 1954*cdf0e10cSrcweir 1955*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1956*cdf0e10cSrcweir sSocket.bind( saBindSocketAddr ); 1957*cdf0e10cSrcweir //Invalid IP, so bind should fail 1958*cdf0e10cSrcweir sal_Bool bOK = compareUString( sSocket.getLocalHost( ), aNullURL ) ; 1959*cdf0e10cSrcweir ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), aNullURL, "test for getLocalHost function: getLocalHost with invalid SocketAddr"); 1960*cdf0e10cSrcweir 1961*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 1962*cdf0e10cSrcweir } 1963*cdf0e10cSrcweir 1964*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getLocalHost ); 1965*cdf0e10cSrcweir CPPUNIT_TEST( getLocalHost_001 ); 1966*cdf0e10cSrcweir CPPUNIT_TEST( getLocalHost_002 ); 1967*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1968*cdf0e10cSrcweir 1969*cdf0e10cSrcweir }; // class getLocalHost 1970*cdf0e10cSrcweir 1971*cdf0e10cSrcweir 1972*cdf0e10cSrcweir /** testing the methods: 1973*cdf0e10cSrcweir inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const; 1974*cdf0e10cSrcweir inline sal_Int32 SAL_CALL getPeerPort() const; 1975*cdf0e10cSrcweir inline ::rtl::OUString SAL_CALL getPeerHost() const; 1976*cdf0e10cSrcweir */ 1977*cdf0e10cSrcweir class getPeer : public CppUnit::TestFixture 1978*cdf0e10cSrcweir { 1979*cdf0e10cSrcweir public: 1980*cdf0e10cSrcweir oslSocket sHandle; 1981*cdf0e10cSrcweir TimeValue *pTimeout; 1982*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 1983*cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 1984*cdf0e10cSrcweir 1985*cdf0e10cSrcweir 1986*cdf0e10cSrcweir // initialization 1987*cdf0e10cSrcweir void setUp( ) 1988*cdf0e10cSrcweir { 1989*cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 1990*cdf0e10cSrcweir pTimeout->Seconds = 3; 1991*cdf0e10cSrcweir pTimeout->Nanosec = 0; 1992*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1993*cdf0e10cSrcweir } 1994*cdf0e10cSrcweir 1995*cdf0e10cSrcweir void tearDown( ) 1996*cdf0e10cSrcweir { 1997*cdf0e10cSrcweir free( pTimeout ); 1998*cdf0e10cSrcweir sHandle = NULL; 1999*cdf0e10cSrcweir asAcceptorSocket.close( ); 2000*cdf0e10cSrcweir csConnectorSocket.close( ); 2001*cdf0e10cSrcweir } 2002*cdf0e10cSrcweir 2003*cdf0e10cSrcweir 2004*cdf0e10cSrcweir void getPeer_001() 2005*cdf0e10cSrcweir { 2006*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT ); 2007*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT ); 2008*cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); 2009*cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 2010*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 2011*cdf0e10cSrcweir /// launch server socket 2012*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 2013*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind '127.0.0.1' address failed.", sal_True == bOK1 ); 2014*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 2015*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 2016*cdf0e10cSrcweir 2017*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 2018*cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 2019*cdf0e10cSrcweir 2020*cdf0e10cSrcweir /// launch client socket 2021*cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 2022*cdf0e10cSrcweir 2023*cdf0e10cSrcweir /// get peer information 2024*cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected. 2025*cdf0e10cSrcweir sal_Int32 peerPort = csConnectorSocket.getPeerPort( ); 2026*cdf0e10cSrcweir ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( ); 2027*cdf0e10cSrcweir 2028*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getPeer function: setup a connection and then get the peer address, port and host from client side.", 2029*cdf0e10cSrcweir ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&& 2030*cdf0e10cSrcweir ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) && 2031*cdf0e10cSrcweir ( peerPort == saLocalSocketAddr.getPort( ) )); 2032*cdf0e10cSrcweir } 2033*cdf0e10cSrcweir 2034*cdf0e10cSrcweir 2035*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getPeer ); 2036*cdf0e10cSrcweir CPPUNIT_TEST( getPeer_001 ); 2037*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2038*cdf0e10cSrcweir 2039*cdf0e10cSrcweir }; // class getPeer 2040*cdf0e10cSrcweir 2041*cdf0e10cSrcweir 2042*cdf0e10cSrcweir /** testing the methods: 2043*cdf0e10cSrcweir inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface); 2044*cdf0e10cSrcweir */ 2045*cdf0e10cSrcweir 2046*cdf0e10cSrcweir 2047*cdf0e10cSrcweir class bind : public CppUnit::TestFixture 2048*cdf0e10cSrcweir { 2049*cdf0e10cSrcweir public: 2050*cdf0e10cSrcweir oslSocket sHandle; 2051*cdf0e10cSrcweir // initialization 2052*cdf0e10cSrcweir void setUp( ) 2053*cdf0e10cSrcweir { 2054*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 2055*cdf0e10cSrcweir } 2056*cdf0e10cSrcweir 2057*cdf0e10cSrcweir void tearDown( ) 2058*cdf0e10cSrcweir { 2059*cdf0e10cSrcweir sHandle = NULL; 2060*cdf0e10cSrcweir } 2061*cdf0e10cSrcweir 2062*cdf0e10cSrcweir 2063*cdf0e10cSrcweir void bind_001() 2064*cdf0e10cSrcweir { 2065*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2066*cdf0e10cSrcweir //bind must use local IP address ---mindyliu 2067*cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 ); 2068*cdf0e10cSrcweir 2069*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 2070*cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 2071*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "Socket bind fail.", sal_True == bOK1 ); 2072*cdf0e10cSrcweir 2073*cdf0e10cSrcweir sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ; 2074*cdf0e10cSrcweir 2075*cdf0e10cSrcweir sSocket.close(); 2076*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", sal_True == bOK2 ); 2077*cdf0e10cSrcweir } 2078*cdf0e10cSrcweir 2079*cdf0e10cSrcweir void bind_002() 2080*cdf0e10cSrcweir { 2081*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2082*cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_NETBIOS ); 2083*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 2084*cdf0e10cSrcweir 2085*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True); 2086*cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 2087*cdf0e10cSrcweir sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; 2088*cdf0e10cSrcweir 2089*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", 2090*cdf0e10cSrcweir ( sal_False == bOK1 ) && ( sal_False == bOK2 ) ); 2091*cdf0e10cSrcweir } 2092*cdf0e10cSrcweir 2093*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( bind ); 2094*cdf0e10cSrcweir CPPUNIT_TEST( bind_001 ); 2095*cdf0e10cSrcweir CPPUNIT_TEST( bind_002 ); 2096*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2097*cdf0e10cSrcweir 2098*cdf0e10cSrcweir }; // class bind 2099*cdf0e10cSrcweir 2100*cdf0e10cSrcweir 2101*cdf0e10cSrcweir /** testing the methods: 2102*cdf0e10cSrcweir inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const; 2103*cdf0e10cSrcweir 2104*cdf0e10cSrcweir */ 2105*cdf0e10cSrcweir class isRecvReady : public CppUnit::TestFixture 2106*cdf0e10cSrcweir { 2107*cdf0e10cSrcweir public: 2108*cdf0e10cSrcweir oslSocket sHandle; 2109*cdf0e10cSrcweir TimeValue *pTimeout; 2110*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 2111*cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 2112*cdf0e10cSrcweir 2113*cdf0e10cSrcweir 2114*cdf0e10cSrcweir // initialization 2115*cdf0e10cSrcweir void setUp( ) 2116*cdf0e10cSrcweir { 2117*cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 2118*cdf0e10cSrcweir pTimeout->Seconds = 3; 2119*cdf0e10cSrcweir pTimeout->Nanosec = 0; 2120*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 2121*cdf0e10cSrcweir } 2122*cdf0e10cSrcweir 2123*cdf0e10cSrcweir void tearDown( ) 2124*cdf0e10cSrcweir { 2125*cdf0e10cSrcweir free( pTimeout ); 2126*cdf0e10cSrcweir sHandle = NULL; 2127*cdf0e10cSrcweir asAcceptorSocket.close( ); 2128*cdf0e10cSrcweir csConnectorSocket.close( ); 2129*cdf0e10cSrcweir } 2130*cdf0e10cSrcweir 2131*cdf0e10cSrcweir 2132*cdf0e10cSrcweir void isRecvReady_001() 2133*cdf0e10cSrcweir { 2134*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT1 ); 2135*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT1 ); 2136*cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); 2137*cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 2138*cdf0e10cSrcweir /// launch server socket 2139*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 2140*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 2141*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 2142*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 2143*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 2144*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 2145*cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 2146*cdf0e10cSrcweir 2147*cdf0e10cSrcweir /// launch client socket 2148*cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 2149*cdf0e10cSrcweir 2150*cdf0e10cSrcweir /// is receive ready? 2151*cdf0e10cSrcweir sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout ); 2152*cdf0e10cSrcweir 2153*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for isRecvReady function: setup a connection and then check if it can transmit data.", 2154*cdf0e10cSrcweir ( sal_True == bOK3 ) ); 2155*cdf0e10cSrcweir } 2156*cdf0e10cSrcweir 2157*cdf0e10cSrcweir 2158*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( isRecvReady ); 2159*cdf0e10cSrcweir CPPUNIT_TEST( isRecvReady_001 ); 2160*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2161*cdf0e10cSrcweir 2162*cdf0e10cSrcweir }; // class isRecvReady 2163*cdf0e10cSrcweir 2164*cdf0e10cSrcweir 2165*cdf0e10cSrcweir /** testing the methods: 2166*cdf0e10cSrcweir inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const; 2167*cdf0e10cSrcweir */ 2168*cdf0e10cSrcweir class isSendReady : public CppUnit::TestFixture 2169*cdf0e10cSrcweir { 2170*cdf0e10cSrcweir public: 2171*cdf0e10cSrcweir oslSocket sHandle; 2172*cdf0e10cSrcweir TimeValue *pTimeout; 2173*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 2174*cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 2175*cdf0e10cSrcweir 2176*cdf0e10cSrcweir 2177*cdf0e10cSrcweir // initialization 2178*cdf0e10cSrcweir void setUp( ) 2179*cdf0e10cSrcweir { 2180*cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 2181*cdf0e10cSrcweir pTimeout->Seconds = 3; 2182*cdf0e10cSrcweir pTimeout->Nanosec = 0; 2183*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 2184*cdf0e10cSrcweir } 2185*cdf0e10cSrcweir 2186*cdf0e10cSrcweir void tearDown( ) 2187*cdf0e10cSrcweir { 2188*cdf0e10cSrcweir free( pTimeout ); 2189*cdf0e10cSrcweir sHandle = NULL; 2190*cdf0e10cSrcweir asAcceptorSocket.close( ); 2191*cdf0e10cSrcweir csConnectorSocket.close( ); 2192*cdf0e10cSrcweir } 2193*cdf0e10cSrcweir 2194*cdf0e10cSrcweir 2195*cdf0e10cSrcweir void isSendReady_001() 2196*cdf0e10cSrcweir { 2197*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT ); 2198*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT ); 2199*cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); 2200*cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 2201*cdf0e10cSrcweir 2202*cdf0e10cSrcweir /// launch server socket 2203*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 2204*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 2205*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 2206*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 2207*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 2208*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 2209*cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 2210*cdf0e10cSrcweir 2211*cdf0e10cSrcweir /// launch client socket 2212*cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 2213*cdf0e10cSrcweir 2214*cdf0e10cSrcweir /// is send ready? 2215*cdf0e10cSrcweir sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout ); 2216*cdf0e10cSrcweir 2217*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for isSendReady function: setup a connection and then check if it can transmit data.", 2218*cdf0e10cSrcweir ( sal_True == bOK3 ) ); 2219*cdf0e10cSrcweir } 2220*cdf0e10cSrcweir 2221*cdf0e10cSrcweir 2222*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( isSendReady ); 2223*cdf0e10cSrcweir CPPUNIT_TEST( isSendReady_001 ); 2224*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2225*cdf0e10cSrcweir 2226*cdf0e10cSrcweir }; // class isSendReady 2227*cdf0e10cSrcweir 2228*cdf0e10cSrcweir 2229*cdf0e10cSrcweir /** testing the methods: 2230*cdf0e10cSrcweir inline oslSocketType SAL_CALL getType() const; 2231*cdf0e10cSrcweir 2232*cdf0e10cSrcweir */ 2233*cdf0e10cSrcweir 2234*cdf0e10cSrcweir class getType : public CppUnit::TestFixture 2235*cdf0e10cSrcweir { 2236*cdf0e10cSrcweir public: 2237*cdf0e10cSrcweir oslSocket sHandle; 2238*cdf0e10cSrcweir // initialization 2239*cdf0e10cSrcweir void setUp( ) 2240*cdf0e10cSrcweir { 2241*cdf0e10cSrcweir 2242*cdf0e10cSrcweir } 2243*cdf0e10cSrcweir 2244*cdf0e10cSrcweir void tearDown( ) 2245*cdf0e10cSrcweir { 2246*cdf0e10cSrcweir sHandle = NULL; 2247*cdf0e10cSrcweir } 2248*cdf0e10cSrcweir 2249*cdf0e10cSrcweir 2250*cdf0e10cSrcweir void getType_001() 2251*cdf0e10cSrcweir { 2252*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 2253*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2254*cdf0e10cSrcweir 2255*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", 2256*cdf0e10cSrcweir osl_Socket_TypeStream == sSocket.getType( ) ); 2257*cdf0e10cSrcweir } 2258*cdf0e10cSrcweir 2259*cdf0e10cSrcweir void getType_002() 2260*cdf0e10cSrcweir { 2261*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 2262*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2263*cdf0e10cSrcweir 2264*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", 2265*cdf0e10cSrcweir osl_Socket_TypeDgram == sSocket.getType( ) ); 2266*cdf0e10cSrcweir } 2267*cdf0e10cSrcweir 2268*cdf0e10cSrcweir #ifdef UNX 2269*cdf0e10cSrcweir // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here 2270*cdf0e10cSrcweir // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw() 2271*cdf0e10cSrcweir void getType_003() 2272*cdf0e10cSrcweir { 2273*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.", 2274*cdf0e10cSrcweir sal_True); 2275*cdf0e10cSrcweir } 2276*cdf0e10cSrcweir #else 2277*cdf0e10cSrcweir void getType_003() 2278*cdf0e10cSrcweir { 2279*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 2280*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2281*cdf0e10cSrcweir 2282*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", 2283*cdf0e10cSrcweir osl_Socket_TypeRaw == sSocket.getType( ) ); 2284*cdf0e10cSrcweir } 2285*cdf0e10cSrcweir #endif 2286*cdf0e10cSrcweir 2287*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getType ); 2288*cdf0e10cSrcweir CPPUNIT_TEST( getType_001 ); 2289*cdf0e10cSrcweir CPPUNIT_TEST( getType_002 ); 2290*cdf0e10cSrcweir CPPUNIT_TEST( getType_003 ); 2291*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2292*cdf0e10cSrcweir 2293*cdf0e10cSrcweir }; // class getType 2294*cdf0e10cSrcweir 2295*cdf0e10cSrcweir 2296*cdf0e10cSrcweir 2297*cdf0e10cSrcweir /** testing the methods: 2298*cdf0e10cSrcweir inline sal_Int32 SAL_CALL getOption( 2299*cdf0e10cSrcweir oslSocketOption Option, 2300*cdf0e10cSrcweir void* pBuffer, 2301*cdf0e10cSrcweir sal_uInt32 BufferLen, 2302*cdf0e10cSrcweir oslSocketOptionLevel Level= osl_Socket_LevelSocket) const; 2303*cdf0e10cSrcweir 2304*cdf0e10cSrcweir inline sal_Int32 getOption( oslSocketOption option ) const; 2305*cdf0e10cSrcweir 2306*cdf0e10cSrcweir */ 2307*cdf0e10cSrcweir 2308*cdf0e10cSrcweir class getOption : public CppUnit::TestFixture 2309*cdf0e10cSrcweir { 2310*cdf0e10cSrcweir public: 2311*cdf0e10cSrcweir oslSocket sHandle; 2312*cdf0e10cSrcweir // initialization 2313*cdf0e10cSrcweir void setUp( ) 2314*cdf0e10cSrcweir { 2315*cdf0e10cSrcweir 2316*cdf0e10cSrcweir } 2317*cdf0e10cSrcweir 2318*cdf0e10cSrcweir void tearDown( ) 2319*cdf0e10cSrcweir { 2320*cdf0e10cSrcweir sHandle = NULL; 2321*cdf0e10cSrcweir } 2322*cdf0e10cSrcweir 2323*cdf0e10cSrcweir /** test writer's comment: 2324*cdf0e10cSrcweir 2325*cdf0e10cSrcweir in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream. 2326*cdf0e10cSrcweir 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ 2327*cdf0e10cSrcweir in 1. 2328*cdf0e10cSrcweir */ 2329*cdf0e10cSrcweir 2330*cdf0e10cSrcweir void getOption_001() 2331*cdf0e10cSrcweir { 2332*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 2333*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2334*cdf0e10cSrcweir sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 2335*cdf0e10cSrcweir *pType = 0; 2336*cdf0e10cSrcweir sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) ); 2337*cdf0e10cSrcweir sal_Bool bOK = ( SOCK_STREAM == *pType ); 2338*cdf0e10cSrcweir // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream 2339*cdf0e10cSrcweir //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType ); 2340*cdf0e10cSrcweir free( pType ); 2341*cdf0e10cSrcweir 2342*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get type option of socket.", 2343*cdf0e10cSrcweir sal_True == bOK ); 2344*cdf0e10cSrcweir } 2345*cdf0e10cSrcweir 2346*cdf0e10cSrcweir // getsockopt error 2347*cdf0e10cSrcweir void getOption_004() 2348*cdf0e10cSrcweir { 2349*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 2350*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2351*cdf0e10cSrcweir 2352*cdf0e10cSrcweir sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 2353*cdf0e10cSrcweir sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) ); 2354*cdf0e10cSrcweir free( pbDontRoute ); 2355*cdf0e10cSrcweir 2356*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get invalid option of socket, should return -1.", 2357*cdf0e10cSrcweir nRes == -1 ); 2358*cdf0e10cSrcweir } 2359*cdf0e10cSrcweir 2360*cdf0e10cSrcweir void getOption_simple_001() 2361*cdf0e10cSrcweir { 2362*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 2363*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2364*cdf0e10cSrcweir 2365*cdf0e10cSrcweir sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) ); 2366*cdf0e10cSrcweir 2367*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.", 2368*cdf0e10cSrcweir sal_True == bOK ); 2369*cdf0e10cSrcweir } 2370*cdf0e10cSrcweir 2371*cdf0e10cSrcweir void getOption_simple_002() 2372*cdf0e10cSrcweir { 2373*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 2374*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2375*cdf0e10cSrcweir 2376*cdf0e10cSrcweir sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) ); 2377*cdf0e10cSrcweir 2378*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.", 2379*cdf0e10cSrcweir sal_True == bOK ); 2380*cdf0e10cSrcweir } 2381*cdf0e10cSrcweir 2382*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getOption ); 2383*cdf0e10cSrcweir CPPUNIT_TEST( getOption_001 ); 2384*cdf0e10cSrcweir CPPUNIT_TEST( getOption_004 ); 2385*cdf0e10cSrcweir CPPUNIT_TEST( getOption_simple_001 ); 2386*cdf0e10cSrcweir CPPUNIT_TEST( getOption_simple_002 ); 2387*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2388*cdf0e10cSrcweir 2389*cdf0e10cSrcweir }; // class getOption 2390*cdf0e10cSrcweir 2391*cdf0e10cSrcweir 2392*cdf0e10cSrcweir /** testing the methods: 2393*cdf0e10cSrcweir inline sal_Bool SAL_CALL setOption( oslSocketOption Option, 2394*cdf0e10cSrcweir void* pBuffer, 2395*cdf0e10cSrcweir sal_uInt32 BufferLen, 2396*cdf0e10cSrcweir oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const; 2397*cdf0e10cSrcweir */ 2398*cdf0e10cSrcweir 2399*cdf0e10cSrcweir class setOption : public CppUnit::TestFixture 2400*cdf0e10cSrcweir { 2401*cdf0e10cSrcweir public: 2402*cdf0e10cSrcweir TimeValue *pTimeout; 2403*cdf0e10cSrcweir // LLA: maybe there is an error in the source, 2404*cdf0e10cSrcweir // as long as I remember, if a derived class do not overload all ctors there is a problem. 2405*cdf0e10cSrcweir 2406*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 2407*cdf0e10cSrcweir 2408*cdf0e10cSrcweir void setUp( ) 2409*cdf0e10cSrcweir { 2410*cdf0e10cSrcweir 2411*cdf0e10cSrcweir } 2412*cdf0e10cSrcweir 2413*cdf0e10cSrcweir void tearDown( ) 2414*cdf0e10cSrcweir { 2415*cdf0e10cSrcweir asAcceptorSocket.close( ); 2416*cdf0e10cSrcweir } 2417*cdf0e10cSrcweir 2418*cdf0e10cSrcweir 2419*cdf0e10cSrcweir // LLA: 2420*cdf0e10cSrcweir // getSocketOption returns BufferLen, or -1 if something failed 2421*cdf0e10cSrcweir 2422*cdf0e10cSrcweir // setSocketOption returns sal_True, if option could stored 2423*cdf0e10cSrcweir // else sal_False 2424*cdf0e10cSrcweir 2425*cdf0e10cSrcweir void setOption_001() 2426*cdf0e10cSrcweir { 2427*cdf0e10cSrcweir /// set and get option. 2428*cdf0e10cSrcweir int nBufferLen = sizeof ( sal_Int32); 2429*cdf0e10cSrcweir // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool! 2430*cdf0e10cSrcweir 2431*cdf0e10cSrcweir sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 2432*cdf0e10cSrcweir *pbDontRouteSet = 1; // sal_True; 2433*cdf0e10cSrcweir 2434*cdf0e10cSrcweir sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 2435*cdf0e10cSrcweir *pGetBuffer = 0; 2436*cdf0e10cSrcweir 2437*cdf0e10cSrcweir // maybe asAcceptorSocket is not right initialized 2438*cdf0e10cSrcweir sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen ); 2439*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b1 ) ); 2440*cdf0e10cSrcweir sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); 2441*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "getOption function failed.", ( n2 == nBufferLen ) ); 2442*cdf0e10cSrcweir 2443*cdf0e10cSrcweir // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value, 2444*cdf0e10cSrcweir // just judge it is zero or not! 2445*cdf0e10cSrcweir sal_Bool bOK = ( 0 != *pGetBuffer ); 2446*cdf0e10cSrcweir t_print("#setOption_001: getOption is %d \n", *pGetBuffer); 2447*cdf0e10cSrcweir 2448*cdf0e10cSrcweir // toggle check, set to 0 2449*cdf0e10cSrcweir *pbDontRouteSet = 0; 2450*cdf0e10cSrcweir 2451*cdf0e10cSrcweir sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) ); 2452*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b3 ) ); 2453*cdf0e10cSrcweir sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); 2454*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "getOption (DONTROUTE) function failed.", ( n4 == nBufferLen ) ); 2455*cdf0e10cSrcweir 2456*cdf0e10cSrcweir sal_Bool bOK2 = ( 0 == *pGetBuffer ); 2457*cdf0e10cSrcweir 2458*cdf0e10cSrcweir t_print("#setOption_001: getOption is %d \n", *pGetBuffer); 2459*cdf0e10cSrcweir 2460*cdf0e10cSrcweir // LLA: sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 2461*cdf0e10cSrcweir // LLA: *pbDontTouteSet = sal_True; 2462*cdf0e10cSrcweir // LLA: sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 2463*cdf0e10cSrcweir // LLA: *pbDontTouteGet = sal_False; 2464*cdf0e10cSrcweir // LLA: asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontTouteSet, sizeof ( sal_Bool ) ); 2465*cdf0e10cSrcweir // LLA: asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pbDontTouteGet, sizeof ( sal_Bool ) ); 2466*cdf0e10cSrcweir // LLA: ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet), 2467*cdf0e10cSrcweir // LLA: ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet), 2468*cdf0e10cSrcweir // LLA: "test for setOption function: set osl_Socket_OptionDontRoute and then check"); 2469*cdf0e10cSrcweir // LLA: 2470*cdf0e10cSrcweir // LLA: sal_Bool bOK = ( sal_True == *pbDontTouteGet ); 2471*cdf0e10cSrcweir // LLA: free( pbDontTouteSet ); 2472*cdf0e10cSrcweir // LLA: free( pbDontTouteGet ); 2473*cdf0e10cSrcweir 2474*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", 2475*cdf0e10cSrcweir ( sal_True == bOK ) && (sal_True == bOK2) ); 2476*cdf0e10cSrcweir 2477*cdf0e10cSrcweir free( pbDontRouteSet ); 2478*cdf0e10cSrcweir free( pGetBuffer ); 2479*cdf0e10cSrcweir // LLA: CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 2480*cdf0e10cSrcweir } 2481*cdf0e10cSrcweir 2482*cdf0e10cSrcweir void setOption_002() 2483*cdf0e10cSrcweir { 2484*cdf0e10cSrcweir /// set and get option. 2485*cdf0e10cSrcweir 2486*cdf0e10cSrcweir // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen ); 2487*cdf0e10cSrcweir // *pbLingerSet = 7; 2488*cdf0e10cSrcweir // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen ); 2489*cdf0e10cSrcweir /* struct */linger aLingerSet; 2490*cdf0e10cSrcweir sal_Int32 nBufferLen = sizeof( struct linger ); 2491*cdf0e10cSrcweir aLingerSet.l_onoff = 1; 2492*cdf0e10cSrcweir aLingerSet.l_linger = 7; 2493*cdf0e10cSrcweir 2494*cdf0e10cSrcweir linger aLingerGet; 2495*cdf0e10cSrcweir 2496*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen ); 2497*cdf0e10cSrcweir 2498*cdf0e10cSrcweir sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen ); 2499*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "getOption (SO_LINGER) function failed.", ( n1 == nBufferLen ) ); 2500*cdf0e10cSrcweir 2501*cdf0e10cSrcweir //t_print("#setOption_002: getOption is %d \n", aLingerGet.l_linger); 2502*cdf0e10cSrcweir sal_Bool bOK = ( 7 == aLingerGet.l_linger ); 2503*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check. ", 2504*cdf0e10cSrcweir sal_True == bOK ); 2505*cdf0e10cSrcweir 2506*cdf0e10cSrcweir } 2507*cdf0e10cSrcweir 2508*cdf0e10cSrcweir void setOption_003() 2509*cdf0e10cSrcweir { 2510*cdf0e10cSrcweir linger aLingerSet; 2511*cdf0e10cSrcweir aLingerSet.l_onoff = 1; 2512*cdf0e10cSrcweir aLingerSet.l_linger = 7; 2513*cdf0e10cSrcweir 2514*cdf0e10cSrcweir sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 ); 2515*cdf0e10cSrcweir printUString( asAcceptorSocket.getErrorAsString() ); 2516*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "setOption (SO_LINGER) function failed for optlen is 0.", 2517*cdf0e10cSrcweir ( b1 == sal_False ) ); 2518*cdf0e10cSrcweir } 2519*cdf0e10cSrcweir 2520*cdf0e10cSrcweir void setOption_simple_001() 2521*cdf0e10cSrcweir { 2522*cdf0e10cSrcweir /// set and get option. 2523*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True ); 2524*cdf0e10cSrcweir sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); 2525*cdf0e10cSrcweir 2526*cdf0e10cSrcweir t_print("setOption_simple_001(): getoption is %d \n", asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); 2527*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", 2528*cdf0e10cSrcweir ( sal_True == bOK ) ); 2529*cdf0e10cSrcweir } 2530*cdf0e10cSrcweir 2531*cdf0e10cSrcweir void setOption_simple_002() 2532*cdf0e10cSrcweir { 2533*cdf0e10cSrcweir /// set and get option. 2534*cdf0e10cSrcweir // LLA: this does not work, due to the fact that SO_LINGER is a structure 2535*cdf0e10cSrcweir // LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 ); 2536*cdf0e10cSrcweir // LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) ); 2537*cdf0e10cSrcweir 2538*cdf0e10cSrcweir // LLA: CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", 2539*cdf0e10cSrcweir // LLA: ( sal_True == bOK ) ); 2540*cdf0e10cSrcweir } 2541*cdf0e10cSrcweir 2542*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( setOption ); 2543*cdf0e10cSrcweir CPPUNIT_TEST( setOption_001 ); 2544*cdf0e10cSrcweir CPPUNIT_TEST( setOption_002 ); 2545*cdf0e10cSrcweir CPPUNIT_TEST( setOption_003 ); 2546*cdf0e10cSrcweir CPPUNIT_TEST( setOption_simple_001 ); 2547*cdf0e10cSrcweir // LLA: CPPUNIT_TEST( setOption_simple_002 ); 2548*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2549*cdf0e10cSrcweir 2550*cdf0e10cSrcweir }; // class setOption 2551*cdf0e10cSrcweir 2552*cdf0e10cSrcweir 2553*cdf0e10cSrcweir 2554*cdf0e10cSrcweir /** testing the method: 2555*cdf0e10cSrcweir inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode); 2556*cdf0e10cSrcweir */ 2557*cdf0e10cSrcweir class enableNonBlockingMode : public CppUnit::TestFixture 2558*cdf0e10cSrcweir { 2559*cdf0e10cSrcweir public: 2560*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 2561*cdf0e10cSrcweir 2562*cdf0e10cSrcweir void enableNonBlockingMode_001() 2563*cdf0e10cSrcweir { 2564*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT ); 2565*cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 2566*cdf0e10cSrcweir 2567*cdf0e10cSrcweir /// launch server socket 2568*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 2569*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 2570*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 2571*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 2572*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 2573*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 2574*cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 2575*cdf0e10cSrcweir 2576*cdf0e10cSrcweir /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. 2577*cdf0e10cSrcweir sal_Bool bOK = sal_True; 2578*cdf0e10cSrcweir asAcceptorSocket.close( ); 2579*cdf0e10cSrcweir 2580*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", 2581*cdf0e10cSrcweir ( sal_True == bOK ) ); 2582*cdf0e10cSrcweir } 2583*cdf0e10cSrcweir 2584*cdf0e10cSrcweir 2585*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( enableNonBlockingMode ); 2586*cdf0e10cSrcweir CPPUNIT_TEST( enableNonBlockingMode_001 ); 2587*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2588*cdf0e10cSrcweir 2589*cdf0e10cSrcweir }; // class enableNonBlockingMode 2590*cdf0e10cSrcweir 2591*cdf0e10cSrcweir 2592*cdf0e10cSrcweir /** testing the method: 2593*cdf0e10cSrcweir inline sal_Bool SAL_CALL isNonBlockingMode() const; 2594*cdf0e10cSrcweir */ 2595*cdf0e10cSrcweir class isNonBlockingMode : public CppUnit::TestFixture 2596*cdf0e10cSrcweir { 2597*cdf0e10cSrcweir public: 2598*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 2599*cdf0e10cSrcweir 2600*cdf0e10cSrcweir void isNonBlockingMode_001() 2601*cdf0e10cSrcweir { 2602*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT ); 2603*cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 2604*cdf0e10cSrcweir 2605*cdf0e10cSrcweir /// launch server socket 2606*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 2607*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 2608*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 2609*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 2610*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 2611*cdf0e10cSrcweir 2612*cdf0e10cSrcweir sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( ); 2613*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 2614*cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 2615*cdf0e10cSrcweir 2616*cdf0e10cSrcweir /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. 2617*cdf0e10cSrcweir sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( ); 2618*cdf0e10cSrcweir asAcceptorSocket.close( ); 2619*cdf0e10cSrcweir 2620*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.", 2621*cdf0e10cSrcweir ( sal_False == bOK3 ) && ( sal_True == bOK4 ) ); 2622*cdf0e10cSrcweir } 2623*cdf0e10cSrcweir 2624*cdf0e10cSrcweir 2625*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( isNonBlockingMode ); 2626*cdf0e10cSrcweir CPPUNIT_TEST( isNonBlockingMode_001 ); 2627*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2628*cdf0e10cSrcweir 2629*cdf0e10cSrcweir }; // class isNonBlockingMode 2630*cdf0e10cSrcweir 2631*cdf0e10cSrcweir /** testing the method: 2632*cdf0e10cSrcweir inline void SAL_CALL clearError() const; 2633*cdf0e10cSrcweir */ 2634*cdf0e10cSrcweir class clearError : public CppUnit::TestFixture 2635*cdf0e10cSrcweir { 2636*cdf0e10cSrcweir public: 2637*cdf0e10cSrcweir oslSocket sHandle; 2638*cdf0e10cSrcweir // initialization 2639*cdf0e10cSrcweir void setUp( ) 2640*cdf0e10cSrcweir { 2641*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 2642*cdf0e10cSrcweir } 2643*cdf0e10cSrcweir 2644*cdf0e10cSrcweir void tearDown( ) 2645*cdf0e10cSrcweir { 2646*cdf0e10cSrcweir sHandle = NULL; 2647*cdf0e10cSrcweir } 2648*cdf0e10cSrcweir 2649*cdf0e10cSrcweir 2650*cdf0e10cSrcweir void clearError_001() 2651*cdf0e10cSrcweir { 2652*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2653*cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_HTTP2 ); 2654*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 2655*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 2656*cdf0e10cSrcweir sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" 2657*cdf0e10cSrcweir oslSocketError seBind = sSocket.getError( ); 2658*cdf0e10cSrcweir sSocket.clearError( ); 2659*cdf0e10cSrcweir 2660*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.", 2661*cdf0e10cSrcweir osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None ); 2662*cdf0e10cSrcweir } 2663*cdf0e10cSrcweir 2664*cdf0e10cSrcweir 2665*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( clearError ); 2666*cdf0e10cSrcweir CPPUNIT_TEST( clearError_001 ); 2667*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2668*cdf0e10cSrcweir 2669*cdf0e10cSrcweir }; // class clearError 2670*cdf0e10cSrcweir 2671*cdf0e10cSrcweir 2672*cdf0e10cSrcweir /** testing the methods: 2673*cdf0e10cSrcweir inline oslSocketError getError() const; 2674*cdf0e10cSrcweir inline ::rtl::OUString getErrorAsString( ) const; 2675*cdf0e10cSrcweir */ 2676*cdf0e10cSrcweir class getError : public CppUnit::TestFixture 2677*cdf0e10cSrcweir { 2678*cdf0e10cSrcweir public: 2679*cdf0e10cSrcweir oslSocket sHandle; 2680*cdf0e10cSrcweir // initialization 2681*cdf0e10cSrcweir void setUp( ) 2682*cdf0e10cSrcweir { 2683*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 2684*cdf0e10cSrcweir } 2685*cdf0e10cSrcweir 2686*cdf0e10cSrcweir void tearDown( ) 2687*cdf0e10cSrcweir { 2688*cdf0e10cSrcweir sHandle = NULL; 2689*cdf0e10cSrcweir } 2690*cdf0e10cSrcweir 2691*cdf0e10cSrcweir 2692*cdf0e10cSrcweir void getError_001() 2693*cdf0e10cSrcweir { 2694*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2695*cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_FTP ); 2696*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 2697*cdf0e10cSrcweir 2698*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getError function: should get no error.", 2699*cdf0e10cSrcweir osl_Socket_E_None == sSocket.getError( ) ); 2700*cdf0e10cSrcweir } 2701*cdf0e10cSrcweir 2702*cdf0e10cSrcweir void getError_002() 2703*cdf0e10cSrcweir { 2704*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2705*cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_FTP ); 2706*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr; 2707*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 2708*cdf0e10cSrcweir sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" 2709*cdf0e10cSrcweir //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError. 2710*cdf0e10cSrcweir #if defined(SOLARIS) 2711*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. ", 2712*cdf0e10cSrcweir osl_Socket_E_InvalidError == sSocket.getError( ) ); 2713*cdf0e10cSrcweir #else 2714*cdf0e10cSrcweir //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail. 2715*cdf0e10cSrcweir 2716*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", 2717*cdf0e10cSrcweir osl_Socket_E_AddrNotAvail == sSocket.getError( ) ); 2718*cdf0e10cSrcweir #endif 2719*cdf0e10cSrcweir } 2720*cdf0e10cSrcweir 2721*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getError ); 2722*cdf0e10cSrcweir CPPUNIT_TEST( getError_001 ); 2723*cdf0e10cSrcweir CPPUNIT_TEST( getError_002 ); 2724*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2725*cdf0e10cSrcweir 2726*cdf0e10cSrcweir }; // class getError 2727*cdf0e10cSrcweir 2728*cdf0e10cSrcweir 2729*cdf0e10cSrcweir 2730*cdf0e10cSrcweir /** testing the methods: 2731*cdf0e10cSrcweir inline oslSocket getHandle() const; 2732*cdf0e10cSrcweir */ 2733*cdf0e10cSrcweir 2734*cdf0e10cSrcweir class getHandle : public CppUnit::TestFixture 2735*cdf0e10cSrcweir { 2736*cdf0e10cSrcweir public: 2737*cdf0e10cSrcweir oslSocket sHandle; 2738*cdf0e10cSrcweir // initialization 2739*cdf0e10cSrcweir void setUp( ) 2740*cdf0e10cSrcweir { 2741*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 2742*cdf0e10cSrcweir } 2743*cdf0e10cSrcweir 2744*cdf0e10cSrcweir void tearDown( ) 2745*cdf0e10cSrcweir { 2746*cdf0e10cSrcweir sHandle = NULL; 2747*cdf0e10cSrcweir } 2748*cdf0e10cSrcweir 2749*cdf0e10cSrcweir void getHandle_001() 2750*cdf0e10cSrcweir { 2751*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 2752*cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket.getHandle(); 2753*cdf0e10cSrcweir 2754*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.", 2755*cdf0e10cSrcweir osl_Socket_TypeStream == assignSocket.getType( ) ); 2756*cdf0e10cSrcweir } 2757*cdf0e10cSrcweir 2758*cdf0e10cSrcweir void getHandle_002() 2759*cdf0e10cSrcweir { 2760*cdf0e10cSrcweir ::osl::Socket sSocket( sHandle ); 2761*cdf0e10cSrcweir ::osl::Socket assignSocket ( sSocket.getHandle( ) ); 2762*cdf0e10cSrcweir 2763*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator", 2764*cdf0e10cSrcweir osl_Socket_TypeStream == assignSocket.getType( ) ); 2765*cdf0e10cSrcweir } 2766*cdf0e10cSrcweir 2767*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getHandle ); 2768*cdf0e10cSrcweir CPPUNIT_TEST( getHandle_001 ); 2769*cdf0e10cSrcweir CPPUNIT_TEST( getHandle_002 ); 2770*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2771*cdf0e10cSrcweir 2772*cdf0e10cSrcweir }; // class getHandle 2773*cdf0e10cSrcweir 2774*cdf0e10cSrcweir 2775*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 2776*cdf0e10cSrcweir 2777*cdf0e10cSrcweir 2778*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::ctors, "osl_Socket"); 2779*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::operators, "osl_Socket"); 2780*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::close, "osl_Socket"); 2781*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalAddr, "osl_Socket"); 2782*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalPort, "osl_Socket"); 2783*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalHost, "osl_Socket"); 2784*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getPeer, "osl_Socket"); 2785*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::bind, "osl_Socket"); 2786*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isRecvReady, "osl_Socket"); 2787*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isSendReady, "osl_Socket"); 2788*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getType, "osl_Socket"); 2789*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getOption, "osl_Socket"); 2790*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::setOption, "osl_Socket"); 2791*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::enableNonBlockingMode, "osl_Socket"); 2792*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isNonBlockingMode, "osl_Socket"); 2793*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::clearError, "osl_Socket"); 2794*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getError, "osl_Socket"); 2795*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getHandle, "osl_Socket"); 2796*cdf0e10cSrcweir 2797*cdf0e10cSrcweir } // namespace osl_Socket 2798*cdf0e10cSrcweir 2799*cdf0e10cSrcweir 2800*cdf0e10cSrcweir 2801*cdf0e10cSrcweir namespace osl_StreamSocket 2802*cdf0e10cSrcweir { 2803*cdf0e10cSrcweir 2804*cdf0e10cSrcweir /** testing the methods: 2805*cdf0e10cSrcweir inline StreamSocket(oslAddrFamily Family = osl_Socket_FamilyInet, 2806*cdf0e10cSrcweir oslProtocol Protocol = osl_Socket_ProtocolIp, 2807*cdf0e10cSrcweir oslSocketType Type = osl_Socket_TypeStream); 2808*cdf0e10cSrcweir 2809*cdf0e10cSrcweir inline StreamSocket( const StreamSocket & ); 2810*cdf0e10cSrcweir 2811*cdf0e10cSrcweir inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire ); 2812*cdf0e10cSrcweir 2813*cdf0e10cSrcweir inline StreamSocket( oslSocket Socket ); 2814*cdf0e10cSrcweir */ 2815*cdf0e10cSrcweir 2816*cdf0e10cSrcweir class ctors : public CppUnit::TestFixture 2817*cdf0e10cSrcweir { 2818*cdf0e10cSrcweir public: 2819*cdf0e10cSrcweir oslSocket sHandle; 2820*cdf0e10cSrcweir // initialization 2821*cdf0e10cSrcweir void setUp( ) 2822*cdf0e10cSrcweir { 2823*cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 2824*cdf0e10cSrcweir } 2825*cdf0e10cSrcweir 2826*cdf0e10cSrcweir void tearDown( ) 2827*cdf0e10cSrcweir { 2828*cdf0e10cSrcweir sHandle = NULL; 2829*cdf0e10cSrcweir } 2830*cdf0e10cSrcweir 2831*cdf0e10cSrcweir 2832*cdf0e10cSrcweir void ctors_none() 2833*cdf0e10cSrcweir { 2834*cdf0e10cSrcweir /// Socket constructor. 2835*cdf0e10cSrcweir ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 2836*cdf0e10cSrcweir 2837*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the stream socket was created successfully.", 2838*cdf0e10cSrcweir osl_Socket_TypeStream == ssSocket.getType( ) ); 2839*cdf0e10cSrcweir } 2840*cdf0e10cSrcweir 2841*cdf0e10cSrcweir void ctors_acquire() 2842*cdf0e10cSrcweir { 2843*cdf0e10cSrcweir /// Socket constructor. 2844*cdf0e10cSrcweir ::osl::StreamSocket ssSocket( sHandle ); 2845*cdf0e10cSrcweir 2846*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully", 2847*cdf0e10cSrcweir osl_Socket_TypeStream == ssSocket.getType( ) ); 2848*cdf0e10cSrcweir } 2849*cdf0e10cSrcweir 2850*cdf0e10cSrcweir void ctors_no_acquire() 2851*cdf0e10cSrcweir { 2852*cdf0e10cSrcweir /// Socket constructor. 2853*cdf0e10cSrcweir ::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE ); 2854*cdf0e10cSrcweir 2855*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully", 2856*cdf0e10cSrcweir osl_Socket_TypeStream == ssSocket.getType( ) ); 2857*cdf0e10cSrcweir } 2858*cdf0e10cSrcweir 2859*cdf0e10cSrcweir void ctors_copy_ctor() 2860*cdf0e10cSrcweir { 2861*cdf0e10cSrcweir /// Socket constructor. 2862*cdf0e10cSrcweir ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 2863*cdf0e10cSrcweir /// Socket copy constructor. 2864*cdf0e10cSrcweir ::osl::StreamSocket copySocket( ssSocket ); 2865*cdf0e10cSrcweir 2866*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor", 2867*cdf0e10cSrcweir osl_Socket_TypeStream == copySocket.getType( ) ); 2868*cdf0e10cSrcweir } 2869*cdf0e10cSrcweir 2870*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( ctors ); 2871*cdf0e10cSrcweir CPPUNIT_TEST( ctors_none ); 2872*cdf0e10cSrcweir CPPUNIT_TEST( ctors_acquire ); 2873*cdf0e10cSrcweir CPPUNIT_TEST( ctors_no_acquire ); 2874*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy_ctor ); 2875*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2876*cdf0e10cSrcweir 2877*cdf0e10cSrcweir }; // class ctors 2878*cdf0e10cSrcweir 2879*cdf0e10cSrcweir class send_recv: public CppUnit::TestFixture 2880*cdf0e10cSrcweir { 2881*cdf0e10cSrcweir public: 2882*cdf0e10cSrcweir // initialization 2883*cdf0e10cSrcweir void setUp( ) 2884*cdf0e10cSrcweir { 2885*cdf0e10cSrcweir } 2886*cdf0e10cSrcweir 2887*cdf0e10cSrcweir void tearDown( ) 2888*cdf0e10cSrcweir { 2889*cdf0e10cSrcweir 2890*cdf0e10cSrcweir } 2891*cdf0e10cSrcweir 2892*cdf0e10cSrcweir void send_recv1() 2893*cdf0e10cSrcweir { 2894*cdf0e10cSrcweir //client sent two strings, and server received, check the order and value 2895*cdf0e10cSrcweir ServerSocketThread myServerThread; 2896*cdf0e10cSrcweir ClientSocketThread myClientThread; 2897*cdf0e10cSrcweir myServerThread.create( ); 2898*cdf0e10cSrcweir myClientThread.create( ); 2899*cdf0e10cSrcweir 2900*cdf0e10cSrcweir //wait until the thread terminate 2901*cdf0e10cSrcweir myClientThread.join( ); 2902*cdf0e10cSrcweir myServerThread.join( ); 2903*cdf0e10cSrcweir sal_Char myStr[30] = ""; 2904*cdf0e10cSrcweir strcat( myStr, pTestString1 ); 2905*cdf0e10cSrcweir strcat( myStr, pTestString2 ); 2906*cdf0e10cSrcweir sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr ); 2907*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.", 2908*cdf0e10cSrcweir nRes == 0 ); 2909*cdf0e10cSrcweir } 2910*cdf0e10cSrcweir 2911*cdf0e10cSrcweir // error when recv 2912*cdf0e10cSrcweir void send_recv2() 2913*cdf0e10cSrcweir { 2914*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 2915*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 ); 2916*cdf0e10cSrcweir ::osl::StreamSocket ssStreamConnection; 2917*cdf0e10cSrcweir sal_Char pReadBuffer[30] = ""; 2918*cdf0e10cSrcweir 2919*cdf0e10cSrcweir ClientSocketThread myClientThread; 2920*cdf0e10cSrcweir myClientThread.create( ); 2921*cdf0e10cSrcweir 2922*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); 2923*cdf0e10cSrcweir 2924*cdf0e10cSrcweir asAcceptorSocket.bind( saLocalSocketAddr ); 2925*cdf0e10cSrcweir asAcceptorSocket.listen( 1 ); 2926*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 2927*cdf0e10cSrcweir asAcceptorSocket.acceptConnection( ssStreamConnection ); 2928*cdf0e10cSrcweir sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 ); 2929*cdf0e10cSrcweir 2930*cdf0e10cSrcweir myClientThread.join( ) ; 2931*cdf0e10cSrcweir ssStreamConnection.close(); 2932*cdf0e10cSrcweir asAcceptorSocket.close(); 2933*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for send/recv, recv error!", nReadNumber == -1 ); 2934*cdf0e10cSrcweir } 2935*cdf0e10cSrcweir 2936*cdf0e10cSrcweir void write_read(sal_Int32 _nBufferSize, int _nValue) 2937*cdf0e10cSrcweir { 2938*cdf0e10cSrcweir //client sent two strings, and server received, check the order and value 2939*cdf0e10cSrcweir WriteSocketThread myServerThread(_nBufferSize, _nValue); 2940*cdf0e10cSrcweir ReadSocketThread myClientThread(_nBufferSize, _nValue); 2941*cdf0e10cSrcweir myServerThread.create( ); 2942*cdf0e10cSrcweir // thread_sleep( 1 ); 2943*cdf0e10cSrcweir myClientThread.create( ); 2944*cdf0e10cSrcweir 2945*cdf0e10cSrcweir //wait until the thread terminate 2946*cdf0e10cSrcweir myClientThread.join( ); 2947*cdf0e10cSrcweir myServerThread.join( ); 2948*cdf0e10cSrcweir 2949*cdf0e10cSrcweir //Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500 2950*cdf0e10cSrcweir // Proteon PRONET = 2046), so here test read 4000 bytes 2951*cdf0e10cSrcweir sal_Int32 nLength = myClientThread.getCount(); 2952*cdf0e10cSrcweir bool bIsOk = myClientThread.isOk(); // check if the values are right. 2953*cdf0e10cSrcweir 2954*cdf0e10cSrcweir t_print("Length:=%d\n", nLength); 2955*cdf0e10cSrcweir t_print(" bIsOk:=%d\n", bIsOk); 2956*cdf0e10cSrcweir 2957*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(" test for write/read values with two threads: send data from server, check readed data in client.", 2958*cdf0e10cSrcweir nLength == _nBufferSize && bIsOk == true); 2959*cdf0e10cSrcweir } 2960*cdf0e10cSrcweir 2961*cdf0e10cSrcweir void write_read_001() 2962*cdf0e10cSrcweir { 2963*cdf0e10cSrcweir write_read(50, 10); 2964*cdf0e10cSrcweir } 2965*cdf0e10cSrcweir void write_read_002() 2966*cdf0e10cSrcweir { 2967*cdf0e10cSrcweir write_read(1024, 20); 2968*cdf0e10cSrcweir } 2969*cdf0e10cSrcweir void write_read_003() 2970*cdf0e10cSrcweir { 2971*cdf0e10cSrcweir write_read(4000, 1); 2972*cdf0e10cSrcweir } 2973*cdf0e10cSrcweir void write_read_004() 2974*cdf0e10cSrcweir { 2975*cdf0e10cSrcweir write_read(8192, 3); 2976*cdf0e10cSrcweir } 2977*cdf0e10cSrcweir 2978*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( send_recv ); 2979*cdf0e10cSrcweir CPPUNIT_TEST( write_read_001 ); 2980*cdf0e10cSrcweir CPPUNIT_TEST( write_read_002 ); 2981*cdf0e10cSrcweir CPPUNIT_TEST( write_read_003 ); 2982*cdf0e10cSrcweir CPPUNIT_TEST( write_read_004 ); 2983*cdf0e10cSrcweir CPPUNIT_TEST( send_recv1 ); 2984*cdf0e10cSrcweir CPPUNIT_TEST( send_recv2 ); 2985*cdf0e10cSrcweir // CPPUNIT_TEST( write_read ); 2986*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 2987*cdf0e10cSrcweir }; // class send_recv 2988*cdf0e10cSrcweir 2989*cdf0e10cSrcweir class SendClientThread : public ClientSocketThread 2990*cdf0e10cSrcweir { 2991*cdf0e10cSrcweir protected: 2992*cdf0e10cSrcweir 2993*cdf0e10cSrcweir void SAL_CALL run( ) 2994*cdf0e10cSrcweir { 2995*cdf0e10cSrcweir TimeValue *pTimeout; 2996*cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 2997*cdf0e10cSrcweir pTimeout->Seconds = 5; 2998*cdf0e10cSrcweir pTimeout->Nanosec = 0; 2999*cdf0e10cSrcweir 3000*cdf0e10cSrcweir if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout )) 3001*cdf0e10cSrcweir { 3002*cdf0e10cSrcweir sal_Int32 nWrite1 = csConnectorSocket.write( pTestString1, 11 ); // "test socket" 3003*cdf0e10cSrcweir 3004*cdf0e10cSrcweir sal_Int32 nWrite2 = csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 ); 3005*cdf0e10cSrcweir thread_sleep( 2 ); 3006*cdf0e10cSrcweir csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 ); 3007*cdf0e10cSrcweir t_print("nWrite1 is %d, nWrite2 is %d\n", nWrite1, nWrite2 ); 3008*cdf0e10cSrcweir //thread_sleep( 1 ); 3009*cdf0e10cSrcweir } 3010*cdf0e10cSrcweir else 3011*cdf0e10cSrcweir t_print("# SendClientThread: connect failed! \n"); 3012*cdf0e10cSrcweir 3013*cdf0e10cSrcweir csConnectorSocket.close(); 3014*cdf0e10cSrcweir free( pTimeout ); 3015*cdf0e10cSrcweir } 3016*cdf0e10cSrcweir 3017*cdf0e10cSrcweir }; 3018*cdf0e10cSrcweir 3019*cdf0e10cSrcweir class shutdown: public CppUnit::TestFixture 3020*cdf0e10cSrcweir { 3021*cdf0e10cSrcweir public: 3022*cdf0e10cSrcweir // initialization 3023*cdf0e10cSrcweir void setUp( ) 3024*cdf0e10cSrcweir { 3025*cdf0e10cSrcweir } 3026*cdf0e10cSrcweir 3027*cdf0e10cSrcweir void tearDown( ) 3028*cdf0e10cSrcweir { 3029*cdf0e10cSrcweir 3030*cdf0e10cSrcweir } 3031*cdf0e10cSrcweir 3032*cdf0e10cSrcweir // similar to close_002 3033*cdf0e10cSrcweir void shutdown_001() 3034*cdf0e10cSrcweir { 3035*cdf0e10cSrcweir #if defined(LINUX) 3036*cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 3037*cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocket, aHostIp1 ); 3038*cdf0e10cSrcweir myAcceptorThread.create(); 3039*cdf0e10cSrcweir 3040*cdf0e10cSrcweir thread_sleep( 1 ); 3041*cdf0e10cSrcweir 3042*cdf0e10cSrcweir //when accepting, shutdown the socket, the thread will not block for accepting 3043*cdf0e10cSrcweir asSocket.shutdown(); 3044*cdf0e10cSrcweir myAcceptorThread.join(); 3045*cdf0e10cSrcweir 3046*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", 3047*cdf0e10cSrcweir myAcceptorThread.isOK( ) == sal_True ); 3048*cdf0e10cSrcweir #endif 3049*cdf0e10cSrcweir } 3050*cdf0e10cSrcweir 3051*cdf0e10cSrcweir void shutdown_002() 3052*cdf0e10cSrcweir { 3053*cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 3054*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9); 3055*cdf0e10cSrcweir asSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); 3056*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True); 3057*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True ); 3058*cdf0e10cSrcweir sal_Char pReadBuffer[40]; 3059*cdf0e10cSrcweir SendClientThread mySendThread; 3060*cdf0e10cSrcweir mySendThread.create(); 3061*cdf0e10cSrcweir 3062*cdf0e10cSrcweir asSocket.enableNonBlockingMode( sal_False ); 3063*cdf0e10cSrcweir ::osl::StreamSocket ssConnectionSocket; 3064*cdf0e10cSrcweir oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket ); 3065*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok ); 3066*cdf0e10cSrcweir 3067*cdf0e10cSrcweir /* set socket option SO_LINGER 0, so close immediatly */ 3068*cdf0e10cSrcweir linger aLingerSet; 3069*cdf0e10cSrcweir sal_Int32 nBufferLen = sizeof( struct linger ); 3070*cdf0e10cSrcweir aLingerSet.l_onoff = 0; 3071*cdf0e10cSrcweir aLingerSet.l_linger = 0; 3072*cdf0e10cSrcweir 3073*cdf0e10cSrcweir ssConnectionSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen ); 3074*cdf0e10cSrcweir thread_sleep( 1 ); 3075*cdf0e10cSrcweir //sal_uInt32 nRecv1 = 0; 3076*cdf0e10cSrcweir sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 ); 3077*cdf0e10cSrcweir 3078*cdf0e10cSrcweir //shutdown read after client the first send complete 3079*cdf0e10cSrcweir ssConnectionSocket.shutdown( osl_Socket_DirRead ); 3080*cdf0e10cSrcweir 3081*cdf0e10cSrcweir sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 ); 3082*cdf0e10cSrcweir sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 ); 3083*cdf0e10cSrcweir t_print("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", nRead1, nRead2, nRead3 ); 3084*cdf0e10cSrcweir mySendThread.join(); 3085*cdf0e10cSrcweir 3086*cdf0e10cSrcweir ssConnectionSocket.close(); 3087*cdf0e10cSrcweir asSocket.close(); 3088*cdf0e10cSrcweir 3089*cdf0e10cSrcweir /* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0, 3090*cdf0e10cSrcweir http://dbforums.com/arch/186/2002/12/586417 3091*cdf0e10cSrcweir While on Solaris, after shutdown(DirRead), all read will return 0 3092*cdf0e10cSrcweir */ 3093*cdf0e10cSrcweir #ifdef LINUX 3094*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).", 3095*cdf0e10cSrcweir nRead1 > 0 && nRead3 == 0 ); 3096*cdf0e10cSrcweir #else 3097*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).", 3098*cdf0e10cSrcweir nRead1 > 0 && nRead2 == 0 && nRead3 == 0 ); 3099*cdf0e10cSrcweir #endif 3100*cdf0e10cSrcweir 3101*cdf0e10cSrcweir } 3102*cdf0e10cSrcweir 3103*cdf0e10cSrcweir void shutdown_003() 3104*cdf0e10cSrcweir { 3105*cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 3106*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9); 3107*cdf0e10cSrcweir asSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); 3108*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True); 3109*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True ); 3110*cdf0e10cSrcweir sal_Char pReadBuffer[40]; 3111*cdf0e10cSrcweir SendClientThread mySendThread; 3112*cdf0e10cSrcweir mySendThread.create(); 3113*cdf0e10cSrcweir 3114*cdf0e10cSrcweir asSocket.enableNonBlockingMode( sal_False ); 3115*cdf0e10cSrcweir ::osl::StreamSocket ssConnectionSocket; 3116*cdf0e10cSrcweir oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket ); 3117*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok ); 3118*cdf0e10cSrcweir 3119*cdf0e10cSrcweir thread_sleep( 1 ); 3120*cdf0e10cSrcweir //shutdown write after client the first send complete 3121*cdf0e10cSrcweir ssConnectionSocket.shutdown( osl_Socket_DirWrite ); 3122*cdf0e10cSrcweir 3123*cdf0e10cSrcweir // recv should not shutdown 3124*cdf0e10cSrcweir sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 ); 3125*cdf0e10cSrcweir 3126*cdf0e10cSrcweir sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 ); 3127*cdf0e10cSrcweir // still can read 3128*cdf0e10cSrcweir sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 ); 3129*cdf0e10cSrcweir t_print("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", nRead1, nWrite, nRead3 ); 3130*cdf0e10cSrcweir mySendThread.join(); 3131*cdf0e10cSrcweir ssConnectionSocket.close(); 3132*cdf0e10cSrcweir asSocket.close(); 3133*cdf0e10cSrcweir 3134*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not send(write).", 3135*cdf0e10cSrcweir nRead1 > 0 && nWrite == 0 && nRead3 > 0); 3136*cdf0e10cSrcweir 3137*cdf0e10cSrcweir } 3138*cdf0e10cSrcweir 3139*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( shutdown ); 3140*cdf0e10cSrcweir CPPUNIT_TEST( shutdown_001 ); 3141*cdf0e10cSrcweir CPPUNIT_TEST( shutdown_002 ); 3142*cdf0e10cSrcweir CPPUNIT_TEST( shutdown_003 ); 3143*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 3144*cdf0e10cSrcweir }; // class shutdown 3145*cdf0e10cSrcweir 3146*cdf0e10cSrcweir class isExceptionPending: public CppUnit::TestFixture 3147*cdf0e10cSrcweir { 3148*cdf0e10cSrcweir public: 3149*cdf0e10cSrcweir void isExPending_001() 3150*cdf0e10cSrcweir { 3151*cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 3152*cdf0e10cSrcweir TimeValue *pTimeout; 3153*cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 3154*cdf0e10cSrcweir pTimeout->Seconds = 3; 3155*cdf0e10cSrcweir pTimeout->Nanosec = 0; 3156*cdf0e10cSrcweir sal_Bool bOk = asSocket.isExceptionPending( pTimeout ); 3157*cdf0e10cSrcweir free( pTimeout ); 3158*cdf0e10cSrcweir 3159*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for isExceptionPending.", 3160*cdf0e10cSrcweir bOk == sal_False ); 3161*cdf0e10cSrcweir } 3162*cdf0e10cSrcweir 3163*cdf0e10cSrcweir /**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/ 3164*cdf0e10cSrcweir 3165*cdf0e10cSrcweir 3166*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( isExceptionPending ); 3167*cdf0e10cSrcweir CPPUNIT_TEST( isExPending_001 ); 3168*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 3169*cdf0e10cSrcweir }; // class isExceptionPending 3170*cdf0e10cSrcweir 3171*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 3172*cdf0e10cSrcweir 3173*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::ctors, "osl_StreamSocket"); 3174*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::send_recv, "osl_StreamSocket"); 3175*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::shutdown, "osl_StreamSocket"); 3176*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::isExceptionPending, "osl_StreamSocket"); 3177*cdf0e10cSrcweir 3178*cdf0e10cSrcweir } // namespace osl_StreamSocket 3179*cdf0e10cSrcweir 3180*cdf0e10cSrcweir 3181*cdf0e10cSrcweir namespace osl_ConnectorSocket 3182*cdf0e10cSrcweir { 3183*cdf0e10cSrcweir 3184*cdf0e10cSrcweir /** testing the method: 3185*cdf0e10cSrcweir ConnectorSocket(oslAddrFamily Family = osl_Socket_FamilyInet, 3186*cdf0e10cSrcweir oslProtocol Protocol = osl_Socket_ProtocolIp, 3187*cdf0e10cSrcweir oslSocketType Type = osl_Socket_TypeStream); 3188*cdf0e10cSrcweir */ 3189*cdf0e10cSrcweir 3190*cdf0e10cSrcweir class ctors : public CppUnit::TestFixture 3191*cdf0e10cSrcweir { 3192*cdf0e10cSrcweir public: 3193*cdf0e10cSrcweir void ctors_001() 3194*cdf0e10cSrcweir { 3195*cdf0e10cSrcweir /// Socket constructor. 3196*cdf0e10cSrcweir ::osl::ConnectorSocket csSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 3197*cdf0e10cSrcweir 3198*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the connector socket was created successfully.", 3199*cdf0e10cSrcweir osl_Socket_TypeStream == csSocket.getType( ) ); 3200*cdf0e10cSrcweir } 3201*cdf0e10cSrcweir 3202*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( ctors ); 3203*cdf0e10cSrcweir CPPUNIT_TEST( ctors_001 ); 3204*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 3205*cdf0e10cSrcweir 3206*cdf0e10cSrcweir }; // class ctors 3207*cdf0e10cSrcweir 3208*cdf0e10cSrcweir /** testing the method: 3209*cdf0e10cSrcweir oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0); 3210*cdf0e10cSrcweir */ 3211*cdf0e10cSrcweir 3212*cdf0e10cSrcweir class connect : public CppUnit::TestFixture 3213*cdf0e10cSrcweir { 3214*cdf0e10cSrcweir public: 3215*cdf0e10cSrcweir TimeValue *pTimeout; 3216*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 3217*cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 3218*cdf0e10cSrcweir 3219*cdf0e10cSrcweir 3220*cdf0e10cSrcweir // initialization 3221*cdf0e10cSrcweir void setUp( ) 3222*cdf0e10cSrcweir { 3223*cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 3224*cdf0e10cSrcweir pTimeout->Seconds = 3; 3225*cdf0e10cSrcweir pTimeout->Nanosec = 0; 3226*cdf0e10cSrcweir // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 3227*cdf0e10cSrcweir } 3228*cdf0e10cSrcweir 3229*cdf0e10cSrcweir void tearDown( ) 3230*cdf0e10cSrcweir { 3231*cdf0e10cSrcweir free( pTimeout ); 3232*cdf0e10cSrcweir // sHandle = NULL; 3233*cdf0e10cSrcweir asAcceptorSocket.close( ); 3234*cdf0e10cSrcweir csConnectorSocket.close( ); 3235*cdf0e10cSrcweir } 3236*cdf0e10cSrcweir 3237*cdf0e10cSrcweir 3238*cdf0e10cSrcweir void connect_001() 3239*cdf0e10cSrcweir { 3240*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT2 ); 3241*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT2 ); 3242*cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); 3243*cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 3244*cdf0e10cSrcweir 3245*cdf0e10cSrcweir /// launch server socket 3246*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 3247*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 3248*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 3249*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 3250*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 3251*cdf0e10cSrcweir 3252*cdf0e10cSrcweir //asAcceptorSocket.enableNonBlockingMode( sal_True ); 3253*cdf0e10cSrcweir //oslSocketResult eResultAccept = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 3254*cdf0e10cSrcweir //CPPUNIT_ASSERT_MESSAGE( "accept failed.", osl_Socket_Ok == eResultAccept ); 3255*cdf0e10cSrcweir /// launch client socket 3256*cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 3257*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_Ok == eResult ); 3258*cdf0e10cSrcweir 3259*cdf0e10cSrcweir /// get peer information 3260*cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected. 3261*cdf0e10cSrcweir 3262*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.", 3263*cdf0e10cSrcweir ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) && 3264*cdf0e10cSrcweir ( osl_Socket_Ok == eResult )); 3265*cdf0e10cSrcweir } 3266*cdf0e10cSrcweir //non-blocking mode connect? 3267*cdf0e10cSrcweir void connect_002() 3268*cdf0e10cSrcweir { 3269*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT3 ); 3270*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT3 ); 3271*cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); 3272*cdf0e10cSrcweir 3273*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 3274*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 3275*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 3276*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 3277*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 3278*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 3279*cdf0e10cSrcweir 3280*cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_True ); 3281*cdf0e10cSrcweir 3282*cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 3283*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_InProgress == eResult || osl_Socket_Ok == eResult ); 3284*cdf0e10cSrcweir 3285*cdf0e10cSrcweir /// get peer information 3286*cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr ); 3287*cdf0e10cSrcweir 3288*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.", 3289*cdf0e10cSrcweir sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) ; 3290*cdf0e10cSrcweir } 3291*cdf0e10cSrcweir // really an error or just delayed 3292*cdf0e10cSrcweir // how to design senarios that will return osl_Socket_Interrupted, osl_Socket_TimedOut 3293*cdf0e10cSrcweir void connect_003() 3294*cdf0e10cSrcweir { 3295*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr1( aHostIp1, IP_PORT_MYPORT3 ); 3296*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr2( aHostIpInval1, IP_PORT_MYPORT3 ); 3297*cdf0e10cSrcweir 3298*cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_False ); 3299*cdf0e10cSrcweir 3300*cdf0e10cSrcweir oslSocketResult eResult1 = csConnectorSocket.connect( saTargetSocketAddr1, pTimeout ); 3301*cdf0e10cSrcweir oslSocketResult eResult2 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout ); 3302*cdf0e10cSrcweir CloseSocketThread myCloseThread( csConnectorSocket ); 3303*cdf0e10cSrcweir oslSocketResult eResult3 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout ); 3304*cdf0e10cSrcweir myCloseThread.join(); 3305*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult1 && 3306*cdf0e10cSrcweir osl_Socket_Error == eResult2 && osl_Socket_Error == eResult3 ); 3307*cdf0e10cSrcweir 3308*cdf0e10cSrcweir } 3309*cdf0e10cSrcweir 3310*cdf0e10cSrcweir // really an error in non-blocking mode 3311*cdf0e10cSrcweir void connect_004() 3312*cdf0e10cSrcweir { 3313*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIpInval1, IP_PORT_MYPORT3 ); 3314*cdf0e10cSrcweir 3315*cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_True ); 3316*cdf0e10cSrcweir 3317*cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 3318*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult ); 3319*cdf0e10cSrcweir } 3320*cdf0e10cSrcweir /** here need a case: immediate connection, say in non-blocking mode connect return osl_Socket_Ok 3321*cdf0e10cSrcweir */ 3322*cdf0e10cSrcweir 3323*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( connect ); 3324*cdf0e10cSrcweir CPPUNIT_TEST( connect_001 ); 3325*cdf0e10cSrcweir CPPUNIT_TEST( connect_002 ); 3326*cdf0e10cSrcweir CPPUNIT_TEST( connect_003 ); 3327*cdf0e10cSrcweir CPPUNIT_TEST( connect_004 ); 3328*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 3329*cdf0e10cSrcweir 3330*cdf0e10cSrcweir }; // class connect 3331*cdf0e10cSrcweir 3332*cdf0e10cSrcweir 3333*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 3334*cdf0e10cSrcweir 3335*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::ctors, "osl_ConnectorSocket"); 3336*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::connect, "osl_ConnectorSocket"); 3337*cdf0e10cSrcweir 3338*cdf0e10cSrcweir } // namespace osl_ConnectorSocket 3339*cdf0e10cSrcweir 3340*cdf0e10cSrcweir 3341*cdf0e10cSrcweir 3342*cdf0e10cSrcweir namespace osl_AcceptorSocket 3343*cdf0e10cSrcweir { 3344*cdf0e10cSrcweir 3345*cdf0e10cSrcweir /** testing the methods: 3346*cdf0e10cSrcweir inline AcceptorSocket(oslAddrFamily Family = osl_Socket_FamilyInet, 3347*cdf0e10cSrcweir oslProtocol Protocol = osl_Socket_ProtocolIp, 3348*cdf0e10cSrcweir oslSocketType Type = osl_Socket_TypeStream); 3349*cdf0e10cSrcweir */ 3350*cdf0e10cSrcweir 3351*cdf0e10cSrcweir class ctors : public CppUnit::TestFixture 3352*cdf0e10cSrcweir { 3353*cdf0e10cSrcweir public: 3354*cdf0e10cSrcweir 3355*cdf0e10cSrcweir void ctors_001() 3356*cdf0e10cSrcweir { 3357*cdf0e10cSrcweir /// Socket constructor. 3358*cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 3359*cdf0e10cSrcweir 3360*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the acceptor socket was created successfully.", 3361*cdf0e10cSrcweir osl_Socket_TypeStream == asSocket.getType( ) ); 3362*cdf0e10cSrcweir } 3363*cdf0e10cSrcweir 3364*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( ctors ); 3365*cdf0e10cSrcweir CPPUNIT_TEST( ctors_001 ); 3366*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 3367*cdf0e10cSrcweir 3368*cdf0e10cSrcweir }; // class ctors 3369*cdf0e10cSrcweir 3370*cdf0e10cSrcweir #if 0 3371*cdf0e10cSrcweir class operator_assign : public CppUnit::TestFixture 3372*cdf0e10cSrcweir { 3373*cdf0e10cSrcweir public: 3374*cdf0e10cSrcweir 3375*cdf0e10cSrcweir void assign_001() 3376*cdf0e10cSrcweir { 3377*cdf0e10cSrcweir #if defined(LINUX) 3378*cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 3379*cdf0e10cSrcweir ::osl::AcceptorSocket asSocketAssign( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 3380*cdf0e10cSrcweir asSocket.setOption( osl_Socket_OptionReuseAddr, 1); 3381*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_MYPORT4 ); 3382*cdf0e10cSrcweir asSocket.bind( saSocketAddr ); 3383*cdf0e10cSrcweir 3384*cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocketAssign, aHostIp1 ); 3385*cdf0e10cSrcweir myAcceptorThread.create(); 3386*cdf0e10cSrcweir 3387*cdf0e10cSrcweir thread_sleep( 1 ); 3388*cdf0e10cSrcweir //when accepting, assign another socket to the socket, the thread will not be closed, so is blocking 3389*cdf0e10cSrcweir asSocketAssign = asSocket; 3390*cdf0e10cSrcweir 3391*cdf0e10cSrcweir t_print("#asSocketAssign port number is %d\n", asSocketAssign.getLocalPort() ); 3392*cdf0e10cSrcweir 3393*cdf0e10cSrcweir asSocketAssign.shutdown(); 3394*cdf0e10cSrcweir myAcceptorThread.join(); 3395*cdf0e10cSrcweir 3396*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", 3397*cdf0e10cSrcweir myAcceptorThread.isOK() == sal_True ); 3398*cdf0e10cSrcweir 3399*cdf0e10cSrcweir 3400*cdf0e10cSrcweir #endif /* LINUX */ 3401*cdf0e10cSrcweir } 3402*cdf0e10cSrcweir 3403*cdf0e10cSrcweir 3404*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( operator_assign ); 3405*cdf0e10cSrcweir CPPUNIT_TEST( assign_001 ); 3406*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 3407*cdf0e10cSrcweir 3408*cdf0e10cSrcweir }; // class operator_assign 3409*cdf0e10cSrcweir #endif 3410*cdf0e10cSrcweir 3411*cdf0e10cSrcweir /** testing the method: 3412*cdf0e10cSrcweir inline sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1); 3413*cdf0e10cSrcweir inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection); 3414*cdf0e10cSrcweir inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr); 3415*cdf0e10cSrcweir */ 3416*cdf0e10cSrcweir 3417*cdf0e10cSrcweir class listen_accept : public CppUnit::TestFixture 3418*cdf0e10cSrcweir { 3419*cdf0e10cSrcweir public: 3420*cdf0e10cSrcweir TimeValue *pTimeout; 3421*cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket; 3422*cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket; 3423*cdf0e10cSrcweir 3424*cdf0e10cSrcweir 3425*cdf0e10cSrcweir // initialization 3426*cdf0e10cSrcweir void setUp( ) 3427*cdf0e10cSrcweir { 3428*cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 3429*cdf0e10cSrcweir pTimeout->Seconds = 3; 3430*cdf0e10cSrcweir pTimeout->Nanosec = 0; 3431*cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1); 3432*cdf0e10cSrcweir // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 3433*cdf0e10cSrcweir } 3434*cdf0e10cSrcweir 3435*cdf0e10cSrcweir void tearDown( ) 3436*cdf0e10cSrcweir { 3437*cdf0e10cSrcweir free( pTimeout ); 3438*cdf0e10cSrcweir // sHandle = NULL; 3439*cdf0e10cSrcweir asAcceptorSocket.close( ); 3440*cdf0e10cSrcweir csConnectorSocket.close( ); 3441*cdf0e10cSrcweir } 3442*cdf0e10cSrcweir 3443*cdf0e10cSrcweir 3444*cdf0e10cSrcweir void listen_accept_001() 3445*cdf0e10cSrcweir { 3446*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT3 ); 3447*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT3 ); 3448*cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 3449*cdf0e10cSrcweir 3450*cdf0e10cSrcweir /// launch server socket 3451*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 3452*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 3453*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 3454*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 3455*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 3456*cdf0e10cSrcweir 3457*cdf0e10cSrcweir /// launch client socket 3458*cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 3459*cdf0e10cSrcweir 3460*cdf0e10cSrcweir oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 3461*cdf0e10cSrcweir 3462*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept.", 3463*cdf0e10cSrcweir ( osl_Socket_Ok == eResult ) ); 3464*cdf0e10cSrcweir } 3465*cdf0e10cSrcweir 3466*cdf0e10cSrcweir void listen_accept_002() 3467*cdf0e10cSrcweir { 3468*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT4 ); 3469*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT4 ); 3470*cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP ); 3471*cdf0e10cSrcweir ::osl::StreamSocket ssConnection; 3472*cdf0e10cSrcweir 3473*cdf0e10cSrcweir /// launch server socket 3474*cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 3475*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 3476*cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 3477*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 3478*cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True ); 3479*cdf0e10cSrcweir 3480*cdf0e10cSrcweir /// launch client socket 3481*cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 3482*cdf0e10cSrcweir 3483*cdf0e10cSrcweir oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection, saPeerSocketAddr); /// waiting for incoming connection... 3484*cdf0e10cSrcweir 3485*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept, accept with peer address.", 3486*cdf0e10cSrcweir ( sal_True == bOK2 ) && 3487*cdf0e10cSrcweir ( osl_Socket_Ok == eResult ) && 3488*cdf0e10cSrcweir ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) ); 3489*cdf0e10cSrcweir } 3490*cdf0e10cSrcweir 3491*cdf0e10cSrcweir void listen_accept_003() 3492*cdf0e10cSrcweir { 3493*cdf0e10cSrcweir 3494*cdf0e10cSrcweir } 3495*cdf0e10cSrcweir 3496*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( listen_accept ); 3497*cdf0e10cSrcweir CPPUNIT_TEST( listen_accept_001 ); 3498*cdf0e10cSrcweir CPPUNIT_TEST( listen_accept_002 ); 3499*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 3500*cdf0e10cSrcweir 3501*cdf0e10cSrcweir }; // class listen_accept 3502*cdf0e10cSrcweir 3503*cdf0e10cSrcweir 3504*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 3505*cdf0e10cSrcweir 3506*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::ctors, "osl_AcceptorSocket"); 3507*cdf0e10cSrcweir //CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::operator_assign, "osl_AcceptorSocket"); 3508*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::listen_accept, "osl_AcceptorSocket"); 3509*cdf0e10cSrcweir 3510*cdf0e10cSrcweir } // namespace osl_AcceptorSocket 3511*cdf0e10cSrcweir 3512*cdf0e10cSrcweir 3513*cdf0e10cSrcweir namespace osl_DatagramSocket 3514*cdf0e10cSrcweir { 3515*cdf0e10cSrcweir 3516*cdf0e10cSrcweir /** testing the methods: 3517*cdf0e10cSrcweir inline DatagramSocket(oslAddrFamily Family= osl_Socket_FamilyInet, 3518*cdf0e10cSrcweir oslProtocol Protocol= osl_Socket_ProtocolIp, 3519*cdf0e10cSrcweir oslSocketType Type= osl_Socket_TypeDgram); 3520*cdf0e10cSrcweir */ 3521*cdf0e10cSrcweir 3522*cdf0e10cSrcweir class ctors : public CppUnit::TestFixture 3523*cdf0e10cSrcweir { 3524*cdf0e10cSrcweir public: 3525*cdf0e10cSrcweir 3526*cdf0e10cSrcweir void ctors_001() 3527*cdf0e10cSrcweir { 3528*cdf0e10cSrcweir /// Socket constructor. 3529*cdf0e10cSrcweir ::osl::DatagramSocket dsSocket; 3530*cdf0e10cSrcweir 3531*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the datagram socket was created successfully.", 3532*cdf0e10cSrcweir osl_Socket_TypeDgram == dsSocket.getType( ) ); 3533*cdf0e10cSrcweir } 3534*cdf0e10cSrcweir 3535*cdf0e10cSrcweir 3536*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( ctors ); 3537*cdf0e10cSrcweir CPPUNIT_TEST( ctors_001 ); 3538*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 3539*cdf0e10cSrcweir 3540*cdf0e10cSrcweir }; // class ctors 3541*cdf0e10cSrcweir 3542*cdf0e10cSrcweir /**thread do sendTo, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml 3543*cdf0e10cSrcweir */ 3544*cdf0e10cSrcweir class TalkerThread : public Thread 3545*cdf0e10cSrcweir { 3546*cdf0e10cSrcweir protected: 3547*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr; 3548*cdf0e10cSrcweir ::osl::DatagramSocket dsSocket; 3549*cdf0e10cSrcweir 3550*cdf0e10cSrcweir void SAL_CALL run( ) 3551*cdf0e10cSrcweir { 3552*cdf0e10cSrcweir dsSocket.sendTo( saTargetSocketAddr, pTestString1, strlen( pTestString1 ) + 1 ); // "test socket" 3553*cdf0e10cSrcweir dsSocket.shutdown(); 3554*cdf0e10cSrcweir } 3555*cdf0e10cSrcweir 3556*cdf0e10cSrcweir void SAL_CALL onTerminated( ) 3557*cdf0e10cSrcweir { 3558*cdf0e10cSrcweir } 3559*cdf0e10cSrcweir 3560*cdf0e10cSrcweir public: 3561*cdf0e10cSrcweir TalkerThread( ): 3562*cdf0e10cSrcweir saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT9 ) 3563*cdf0e10cSrcweir { 3564*cdf0e10cSrcweir } 3565*cdf0e10cSrcweir 3566*cdf0e10cSrcweir ~TalkerThread( ) 3567*cdf0e10cSrcweir { 3568*cdf0e10cSrcweir if ( isRunning( ) ) 3569*cdf0e10cSrcweir t_print("# error: TalkerThread not terminated normally.\n" ); 3570*cdf0e10cSrcweir } 3571*cdf0e10cSrcweir }; 3572*cdf0e10cSrcweir 3573*cdf0e10cSrcweir /**thread do listen, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml 3574*cdf0e10cSrcweir */ 3575*cdf0e10cSrcweir class ListenerThread : public Thread 3576*cdf0e10cSrcweir { 3577*cdf0e10cSrcweir protected: 3578*cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr; 3579*cdf0e10cSrcweir ::osl::DatagramSocket dsSocket; 3580*cdf0e10cSrcweir 3581*cdf0e10cSrcweir void SAL_CALL run( ) 3582*cdf0e10cSrcweir { 3583*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT10 ); 3584*cdf0e10cSrcweir dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); 3585*cdf0e10cSrcweir if ( dsSocket.bind( saLocalSocketAddr ) == sal_False ) 3586*cdf0e10cSrcweir { 3587*cdf0e10cSrcweir t_print("DatagramSocket bind failed \n"); 3588*cdf0e10cSrcweir return; 3589*cdf0e10cSrcweir } 3590*cdf0e10cSrcweir //blocking mode: default 3591*cdf0e10cSrcweir sal_Int32 nRecv = dsSocket.recvFrom( pRecvBuffer, 30, &saTargetSocketAddr); //strlen( pTestString2 ) + 1 3592*cdf0e10cSrcweir t_print("After recvFrom, nRecv is %d\n", nRecv); 3593*cdf0e10cSrcweir } 3594*cdf0e10cSrcweir 3595*cdf0e10cSrcweir void SAL_CALL onTerminated( ) 3596*cdf0e10cSrcweir { 3597*cdf0e10cSrcweir } 3598*cdf0e10cSrcweir 3599*cdf0e10cSrcweir public: 3600*cdf0e10cSrcweir sal_Char pRecvBuffer[30]; 3601*cdf0e10cSrcweir ListenerThread( ): 3602*cdf0e10cSrcweir saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT10 ) 3603*cdf0e10cSrcweir { 3604*cdf0e10cSrcweir pRecvBuffer[0] = '\0'; 3605*cdf0e10cSrcweir } 3606*cdf0e10cSrcweir 3607*cdf0e10cSrcweir ~ListenerThread( ) 3608*cdf0e10cSrcweir { 3609*cdf0e10cSrcweir if ( isRunning( ) ) 3610*cdf0e10cSrcweir t_print("# error: ListenerThread not terminated normally.\n" ); 3611*cdf0e10cSrcweir } 3612*cdf0e10cSrcweir 3613*cdf0e10cSrcweir }; 3614*cdf0e10cSrcweir 3615*cdf0e10cSrcweir /** testing the methods: 3616*cdf0e10cSrcweir inline sal_Int32 DatagramSocket::recvFrom(void* pBuffer, sal_uInt32 BufferSize, 3617*cdf0e10cSrcweir SocketAddr* pSenderAddr, oslSocketMsgFlag Flag ) 3618*cdf0e10cSrcweir inline sal_Int32 DatagramSocket::sendTo( const SocketAddr& ReceiverAddr, 3619*cdf0e10cSrcweir const void* pBuffer, sal_uInt32 BufferSize, oslSocketMsgFlag Flag ) 3620*cdf0e10cSrcweir */ 3621*cdf0e10cSrcweir 3622*cdf0e10cSrcweir class sendTo_recvFrom : public CppUnit::TestFixture 3623*cdf0e10cSrcweir { 3624*cdf0e10cSrcweir public: 3625*cdf0e10cSrcweir 3626*cdf0e10cSrcweir void sr_001() 3627*cdf0e10cSrcweir { 3628*cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 ); 3629*cdf0e10cSrcweir ::osl::DatagramSocket dsSocket; 3630*cdf0e10cSrcweir dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); 3631*cdf0e10cSrcweir dsSocket.bind( saLocalSocketAddr ); 3632*cdf0e10cSrcweir 3633*cdf0e10cSrcweir sal_Char pReadBuffer[30]; 3634*cdf0e10cSrcweir TalkerThread myTalkThread; 3635*cdf0e10cSrcweir myTalkThread.create(); 3636*cdf0e10cSrcweir sal_Int32 nRecv = dsSocket.recvFrom( pReadBuffer, 30, &saLocalSocketAddr); 3637*cdf0e10cSrcweir myTalkThread.join(); 3638*cdf0e10cSrcweir //t_print("#received buffer is %s# \n", pReadBuffer); 3639*cdf0e10cSrcweir 3640*cdf0e10cSrcweir sal_Bool bOk = ( strcmp(pReadBuffer, pTestString1) == 0 ); 3641*cdf0e10cSrcweir 3642*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for sendTo/recvFrom function: create a talker thread and recvFrom in the main thread, check if the datagram socket can communicate successfully.", 3643*cdf0e10cSrcweir nRecv > 0 && bOk == sal_True ); 3644*cdf0e10cSrcweir } 3645*cdf0e10cSrcweir 3646*cdf0e10cSrcweir void sr_002() 3647*cdf0e10cSrcweir { 3648*cdf0e10cSrcweir ::osl::SocketAddr saListenSocketAddr( aHostIp1, IP_PORT_MYPORT10 ); 3649*cdf0e10cSrcweir ::osl::DatagramSocket dsSocket; 3650*cdf0e10cSrcweir 3651*cdf0e10cSrcweir //listener thread construct a DatagramSocket, recvFrom waiting for data, then main thread sendto data 3652*cdf0e10cSrcweir ListenerThread myListenThread; 3653*cdf0e10cSrcweir myListenThread.create(); 3654*cdf0e10cSrcweir //to grantee the recvFrom is before sendTo 3655*cdf0e10cSrcweir thread_sleep( 1 ); 3656*cdf0e10cSrcweir 3657*cdf0e10cSrcweir sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 ); 3658*cdf0e10cSrcweir 3659*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo failed: nSend <= 0.", nSend > 0); 3660*cdf0e10cSrcweir 3661*cdf0e10cSrcweir myListenThread.join(); 3662*cdf0e10cSrcweir //t_print("#received buffer is %s# \n", myListenThread.pRecvBuffer); 3663*cdf0e10cSrcweir 3664*cdf0e10cSrcweir sal_Bool bOk = ( strcmp( myListenThread.pRecvBuffer, pTestString2) == 0 ); 3665*cdf0e10cSrcweir 3666*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for sendTo/recvFrom function: create a listener thread and sendTo in the main thread, check if the datagram socket can communicate successfully.", 3667*cdf0e10cSrcweir bOk == sal_True ); 3668*cdf0e10cSrcweir } 3669*cdf0e10cSrcweir 3670*cdf0e10cSrcweir //sendTo error, return -1; recvFrom error, return -1 3671*cdf0e10cSrcweir void sr_003() 3672*cdf0e10cSrcweir { 3673*cdf0e10cSrcweir ::osl::SocketAddr saListenSocketAddr( aHostIpInval1, IP_PORT_MYPORT10 ); 3674*cdf0e10cSrcweir ::osl::DatagramSocket dsSocket; 3675*cdf0e10cSrcweir // Transport endpoint is not connected 3676*cdf0e10cSrcweir sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 ); 3677*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo should fail: nSend <= 0.", 3678*cdf0e10cSrcweir nSend == -1 ); 3679*cdf0e10cSrcweir } 3680*cdf0e10cSrcweir 3681*cdf0e10cSrcweir void sr_004() 3682*cdf0e10cSrcweir { 3683*cdf0e10cSrcweir ::osl::SocketAddr saListenSocketAddr1( aHostIpInval1, IP_PORT_MYPORT10 ); 3684*cdf0e10cSrcweir ::osl::SocketAddr saListenSocketAddr2( aHostIp2, IP_PORT_MYPORT10 ); 3685*cdf0e10cSrcweir ::osl::DatagramSocket dsSocket; 3686*cdf0e10cSrcweir 3687*cdf0e10cSrcweir dsSocket.enableNonBlockingMode( sal_True ); 3688*cdf0e10cSrcweir 3689*cdf0e10cSrcweir sal_Char pReadBuffer[30]; 3690*cdf0e10cSrcweir //sal_Int32 nRecv1 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 ); 3691*cdf0e10cSrcweir 3692*cdf0e10cSrcweir // will block ? 3693*cdf0e10cSrcweir CloseSocketThread myThread( dsSocket ); 3694*cdf0e10cSrcweir myThread.create(); 3695*cdf0e10cSrcweir sal_Int32 nRecv2 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 ); 3696*cdf0e10cSrcweir myThread.join(); 3697*cdf0e10cSrcweir //t_print("#nRecv1 is %d nRecv2 is %d\n", nRecv1, nRecv2 ); 3698*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo should fail: nSend <= 0.", 3699*cdf0e10cSrcweir nRecv2 == -1 ); 3700*cdf0e10cSrcweir } 3701*cdf0e10cSrcweir 3702*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( sendTo_recvFrom ); 3703*cdf0e10cSrcweir CPPUNIT_TEST( sr_001 ); 3704*cdf0e10cSrcweir CPPUNIT_TEST( sr_002 ); 3705*cdf0e10cSrcweir CPPUNIT_TEST( sr_003 ); 3706*cdf0e10cSrcweir CPPUNIT_TEST( sr_004 ); 3707*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 3708*cdf0e10cSrcweir 3709*cdf0e10cSrcweir }; // class sendTo_recvFrom 3710*cdf0e10cSrcweir 3711*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 3712*cdf0e10cSrcweir 3713*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DatagramSocket::ctors, "osl_DatagramSocket"); 3714*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DatagramSocket::sendTo_recvFrom, "osl_DatagramSocket"); 3715*cdf0e10cSrcweir 3716*cdf0e10cSrcweir } // namespace osl_DatagramSocket 3717*cdf0e10cSrcweir 3718*cdf0e10cSrcweir 3719*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 3720*cdf0e10cSrcweir 3721*cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions() 3722*cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand. 3723*cdf0e10cSrcweir NOADDITIONAL; 3724