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