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