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