1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_sal.hxx" 30 31 /** test coder preface: 32 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform 33 if you are not including ws2_32.lib in makefile.mk, the including format will be like this: 34 35 .IF "$(GUI)" == "WNT" 36 SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib 37 SHL1STDLIBS += ws2_32.lib 38 .ENDIF 39 40 likewise on Solaris platform. 41 .IF "$(GUI)" == "UNX" 42 SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a 43 SHL1STDLIBS += -lsocket -ldl -lnsl 44 .ENDIF 45 46 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4 47 category. 48 49 3. some fragment of Socket source implementation are lack of comment so it is hard for testers 50 guess what the exact functionality or usage of a member. Hope the Socket section's comment 51 will be added. 52 53 4. following functions are declared but not implemented: 54 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; 55 */ 56 57 //------------------------------------------------------------------------ 58 // include files 59 //------------------------------------------------------------------------ 60 61 #include <testshl/simpleheader.hxx> 62 63 //#include "osl_Socket_Const.h" 64 #include "sockethelper.hxx" 65 #include <osl/conditn.hxx> 66 67 using namespace osl; 68 using namespace rtl; 69 70 #define IP_PORT_MYPORT9 8897 71 #define IP_PORT_MYPORT10 18900 72 73 const char * pTestString1 = "test socket"; 74 const char * pTestString2 = " Passed#OK"; 75 76 //------------------------------------------------------------------------ 77 // helper functions 78 //------------------------------------------------------------------------ 79 80 // just used to test socket::close() when accepting 81 class AcceptorThread : public Thread 82 { 83 ::osl::AcceptorSocket asAcceptorSocket; 84 ::rtl::OUString aHostIP; 85 sal_Bool bOK; 86 protected: 87 void SAL_CALL run( ) 88 { 89 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 ); 90 ::osl::StreamSocket ssStreamConnection; 91 92 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); 93 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 94 if ( sal_True != bOK1 ) 95 { 96 t_print("# AcceptorSocket bind address failed.\n" ) ; 97 return; 98 } 99 sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 100 if ( sal_True != bOK2 ) 101 { 102 t_print("# AcceptorSocket listen address failed.\n" ) ; 103 return; 104 } 105 106 asAcceptorSocket.enableNonBlockingMode( sal_False ); 107 108 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); 109 if (eResult != osl_Socket_Ok ) 110 { 111 bOK = sal_True; 112 t_print("AcceptorThread: acceptConnection failed! \n"); 113 } 114 } 115 public: 116 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP ) 117 : asAcceptorSocket( asSocket ), aHostIP( aBindIP ) 118 { 119 bOK = sal_False; 120 } 121 122 sal_Bool isOK() { return bOK; } 123 124 ~AcceptorThread( ) 125 { 126 if ( isRunning( ) ) 127 { 128 asAcceptorSocket.shutdown(); 129 t_print("# error: Acceptor thread not terminated.\n" ); 130 } 131 } 132 }; 133 134 /** Server Socket Thread, served as a temp little server to communicate with client. 135 */ 136 class ServerSocketThread : public Thread 137 { 138 osl::Condition &m_aCondition; 139 protected: 140 oslThreadIdentifier m_id; 141 142 void SAL_CALL run( ) 143 { 144 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 145 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ); 146 ::osl::StreamSocket ssStreamConnection; 147 148 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket 149 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); 150 while ( schedule( ) == sal_True ) 151 { 152 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 153 if ( sal_True != bOK1 ) 154 { 155 t_print("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ; 156 break; 157 } 158 sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 159 if ( sal_True != bOK2 ) 160 { 161 t_print("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ; 162 break; 163 } 164 165 asAcceptorSocket.enableNonBlockingMode( sal_False ); 166 m_aCondition.set(); 167 168 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); 169 if (eResult != osl_Socket_Ok ) 170 { 171 t_print("ServerSocketThread: acceptConnection failed! \n"); 172 break; 173 } 174 sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 ); 175 sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 ); 176 pReadBuffer[nReadNumber1 + nReadNumber2] = '\0'; 177 //t_print("# read buffer content: %s\n", pReadBuffer ); 178 break; 179 } 180 ssStreamConnection.close(); 181 asAcceptorSocket.close(); 182 183 } 184 185 void SAL_CALL onTerminated( ) 186 { 187 //t_print("# normally terminate this server thread %d!\n", m_id ); 188 } 189 190 public: 191 // public to check if data transmition is OK 192 sal_Char pReadBuffer[30]; 193 ServerSocketThread( osl::Condition &_aCond ):m_aCondition(_aCond) 194 { 195 m_aCondition.reset(); 196 t_print("#init ServerSocketThread\n"); 197 m_id = getIdentifier( ); 198 //t_print("# successfully creat this ServerSocketThread %d!\n", m_id ); 199 } 200 201 ~ServerSocketThread( ) 202 { 203 if ( isRunning( ) ) 204 t_print("# error: ServerSocketThread has not terminated.\n" ); 205 } 206 }; 207 208 /** Client Socket Thread, served as a temp little client to communicate with server. 209 */ 210 class ClientSocketThread : public Thread 211 { 212 protected: 213 osl::Condition &m_aCondition; 214 oslThreadIdentifier m_id; 215 ::osl::SocketAddr m_saTargetSocketAddr; 216 ::osl::ConnectorSocket m_csConnectorSocket; 217 218 void SAL_CALL run( ) 219 { 220 TimeValue *pTimeout; 221 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 222 pTimeout->Seconds = 5; 223 pTimeout->Nanosec = 0; 224 225 /// if the thread should terminate, schedule return false 226 //while ( schedule( ) == sal_True ) 227 //{ 228 if ( osl::Condition::result_ok != m_aCondition.wait( pTimeout ) ) 229 { 230 free( pTimeout ); 231 return; 232 } 233 234 if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout )) 235 { 236 m_csConnectorSocket.send( pTestString1, 11 ); // "test socket" 237 m_csConnectorSocket.send( pTestString2, 10); 238 } 239 else 240 t_print("# ClientSocketThread: connect failed! \n"); 241 // terminate(); 242 //} 243 m_csConnectorSocket.close(); 244 free( pTimeout ); 245 } 246 247 void SAL_CALL onTerminated( ) 248 { 249 //t_print("# normally terminate this thread %d!\n", m_id ); 250 } 251 252 public: 253 ClientSocketThread( osl::Condition &_aCond ): 254 m_aCondition(_aCond), 255 m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ), 256 m_csConnectorSocket( ) 257 { 258 m_id = getIdentifier( ); 259 //t_print("# successfully creat this client thread %d!\n", m_id ); 260 } 261 262 ~ClientSocketThread( ) 263 { 264 if ( isRunning( ) ) 265 t_print("# error: client thread not terminated.\n" ); 266 } 267 268 }; 269 270 // ----------------------------------------------------------------------------- 271 // Helper functions, to create buffers, check buffers 272 class ValueCheckProvider 273 { 274 bool m_bFoundFailure; 275 char *m_pBuffer; 276 sal_Int32 m_nBufferSize; 277 278 public: 279 ValueCheckProvider() 280 :m_bFoundFailure(false), 281 m_pBuffer(NULL), 282 m_nBufferSize(0) 283 { 284 } 285 286 bool isFailure() {return m_bFoundFailure;} 287 288 const char* getBuffer() {return m_pBuffer;} 289 char* getWriteBuffer() {return m_pBuffer;} 290 291 sal_Int32 getBufferSize() {return m_nBufferSize;} 292 293 bool checkValues(sal_Int32 _nLength, int _nValue) 294 { 295 m_bFoundFailure = false; 296 for(sal_Int32 i=0;i<_nLength;i++) 297 { 298 if (m_pBuffer[i] != _nValue) 299 { 300 m_bFoundFailure = true; 301 } 302 } 303 return m_bFoundFailure; 304 } 305 306 void createBuffer(sal_Int32 _nLength, int _nValue) 307 { 308 m_nBufferSize = _nLength; 309 m_pBuffer = (char*) malloc(m_nBufferSize); 310 if (m_pBuffer) 311 { 312 memset(m_pBuffer, _nValue, m_nBufferSize); 313 } 314 } 315 316 void freeBuffer() 317 { 318 if (m_pBuffer) free(m_pBuffer); 319 } 320 321 }; 322 323 // ----------------------------------------------------------------------------- 324 /** Client Socket Thread, served as a temp little client to communicate with server. 325 */ 326 327 class ReadSocketThread : public Thread 328 { 329 ValueCheckProvider m_aValues; 330 int m_nValue; 331 osl::Condition &m_aCondition; 332 333 protected: 334 oslThreadIdentifier m_id; 335 336 void SAL_CALL run( ) 337 { 338 ::osl::SocketAddr m_aTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 ); 339 ::osl::ConnectorSocket m_aConnectorSocket; 340 341 if (! m_aTargetSocketAddr.is()) 342 { 343 t_print("# SocketAddr was NOT created successfully!\n"); 344 } 345 else 346 { 347 t_print("start ReadSocketThread\n"); 348 349 TimeValue *pTimeout; 350 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 351 pTimeout->Seconds = 5; 352 pTimeout->Nanosec = 0; 353 354 m_aCondition.wait(); 355 356 t_print("connect()\n"); 357 358 oslSocketResult eResult = m_aConnectorSocket.connect( m_aTargetSocketAddr, pTimeout ); 359 if ( osl_Socket_Ok == eResult ) 360 { 361 sal_Int32 nReadCount = m_aConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() ); 362 m_aValues.checkValues(nReadCount, m_nValue); 363 } 364 else 365 { 366 t_print("# ReadSocketThread: connect failed! \n"); 367 printSocketResult(eResult); 368 } 369 370 //remove this line for deadlock on solaris( margritte.germany ) 371 m_aConnectorSocket.close(); 372 free( pTimeout ); 373 } 374 } 375 376 void SAL_CALL onTerminated( ) 377 { 378 //t_print("# normally terminate this thread %d!\n", m_id ); 379 } 380 381 public: 382 sal_Int32 getCount() {return m_aValues.getBufferSize();} 383 bool isOk() {return m_aValues.isFailure() == true ? false : true;} 384 385 ReadSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond ) 386 : m_nValue( _nValue ), 387 m_aCondition(_aCond) 388 { 389 t_print("#init ReadSocketThread\n"); 390 m_id = getIdentifier( ); 391 392 //t_print("# successfully creat this client thread %d!\n", m_id ); 393 m_aValues.createBuffer(_nBufferSize, 0); 394 } 395 396 ~ReadSocketThread( ) 397 { 398 if ( isRunning( ) ) 399 t_print("# error: client thread not terminated.\n" ); 400 m_aValues.freeBuffer(); 401 } 402 403 }; 404 405 /** Server Socket Thread, write a file which is large 406 */ 407 class WriteSocketThread : public Thread 408 { 409 ValueCheckProvider m_aValues; 410 osl::Condition &m_aCondition; 411 412 protected: 413 oslThreadIdentifier m_id; 414 415 void SAL_CALL run( ) 416 { 417 t_print("start WriteSocketThread\n"); 418 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 419 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 ); 420 if (! saLocalSocketAddr.is()) 421 { 422 t_print("LocalSocketAddr was NOT created successfully!\n"); 423 } 424 425 ::osl::StreamSocket ssStreamConnection; 426 427 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket 428 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 429 430 /// if the thread should terminate, schedule return false 431 // while ( schedule( ) == sal_True ) 432 // { 433 t_print("bind()\n"); 434 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 435 if ( sal_True != bOK1 ) 436 { 437 t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ; 438 } 439 else 440 { 441 t_print("listen()\n"); 442 sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 443 if ( sal_True != bOK2 ) 444 { 445 t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ; 446 } 447 else 448 { 449 450 // blocking mode, if read/recv failed, block until success 451 asAcceptorSocket.enableNonBlockingMode( sal_False); 452 t_print("acceptConnection()\n"); 453 m_aCondition.set(); 454 455 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); 456 if (eResult != osl_Socket_Ok ) 457 { 458 t_print("WriteSocketThread: acceptConnection failed! \n"); 459 } 460 else 461 { 462 463 // LLA: removed, due to the fact, this is to error prone 464 // LLA: char * pSrcRoot = getenv("SOURCE_ROOT"); 465 // LLA: // LLA: This is absolute wrong! 466 // LLA: // strcat( pSrcRoot, "/sal/inc/osl/file.hxx"); 467 // LLA: rtl::OString sSrcRoot(pSrcRoot); 468 // LLA: sSrcRoot += "/sal/inc/osl/file.hxx"; 469 // LLA: 470 // LLA: ::rtl::OUString sFilePath = ::rtl::OUString::createFromAscii( sSrcRoot.getStr() ); 471 // LLA: #ifdef WNT 472 // LLA: while (sFilePath.lastIndexOf('/') != -1) 473 // LLA: sFilePath = sFilePath.replace('/',(sal_Unicode)'\\'); 474 // LLA: #endif 475 // LLA: FILE *stream; 476 // LLA: sal_uInt64 nCount_read; 477 // LLA: sal_Char buffer_read[FILE_READ]; 478 // LLA: 479 // LLA: if( (stream = fopen( oustring2char( sFilePath ), "r+t" )) != NULL ) 480 // LLA: { 481 // LLA: /* Attempt to read in 25 characters */ 482 // LLA: nCount_read = fread( buffer_read, sizeof( char ), FILE_READ, stream ); 483 // LLA: fclose( stream ); 484 // LLA: } 485 // LLA: else 486 // LLA: t_print("# File $SRC_ROOT/sal/inc/osl/file.hxx could not be opened\n" ); 487 488 t_print("write()\n"); 489 490 ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() ); 491 t_print("done written.\n"); 492 } 493 } 494 } 495 ssStreamConnection.close(); 496 asAcceptorSocket.close(); 497 } 498 499 void SAL_CALL onTerminated( ) 500 { 501 //t_print("# normally terminate this server thread %d!\n", m_id ); 502 } 503 504 public: 505 // public to check if data transmition is OK 506 WriteSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond ) 507 : m_aCondition(_aCond) 508 { 509 m_aCondition.reset(); 510 511 t_print("#init WriteSocketThread\n"); 512 m_id = getIdentifier( ); 513 //t_print("# successfully creat this server thread %d!\n", m_id ); 514 515 m_aValues.createBuffer(_nBufferSize, _nValue); 516 } 517 518 ~WriteSocketThread( ) 519 { 520 if ( isRunning( ) ) 521 t_print("# error: server thread not terminated.\n" ); 522 m_aValues.freeBuffer(); 523 } 524 }; 525 526 // ----------------------------------------------------------------------------- 527 528 namespace osl_StreamSocket 529 { 530 531 /** testing the methods: 532 inline StreamSocket(oslAddrFamily Family = osl_Socket_FamilyInet, 533 oslProtocol Protocol = osl_Socket_ProtocolIp, 534 oslSocketType Type = osl_Socket_TypeStream); 535 536 inline StreamSocket( const StreamSocket & ); 537 538 inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire ); 539 540 inline StreamSocket( oslSocket Socket ); 541 */ 542 543 class ctors : public CppUnit::TestFixture 544 { 545 public: 546 oslSocket sHandle; 547 // initialization 548 void setUp( ) 549 { 550 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 551 } 552 553 void tearDown( ) 554 { 555 sHandle = NULL; 556 } 557 558 559 void ctors_none() 560 { 561 /// Socket constructor. 562 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 563 564 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the stream socket was created successfully.", 565 osl_Socket_TypeStream == ssSocket.getType( ) ); 566 } 567 568 void ctors_acquire() 569 { 570 /// Socket constructor. 571 ::osl::StreamSocket ssSocket( sHandle ); 572 573 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully", 574 osl_Socket_TypeStream == ssSocket.getType( ) ); 575 } 576 577 void ctors_no_acquire() 578 { 579 /// Socket constructor. 580 ::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE ); 581 582 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully", 583 osl_Socket_TypeStream == ssSocket.getType( ) ); 584 } 585 586 void ctors_copy_ctor() 587 { 588 /// Socket constructor. 589 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 590 /// Socket copy constructor. 591 ::osl::StreamSocket copySocket( ssSocket ); 592 593 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor", 594 osl_Socket_TypeStream == copySocket.getType( ) ); 595 } 596 597 CPPUNIT_TEST_SUITE( ctors ); 598 CPPUNIT_TEST( ctors_none ); 599 CPPUNIT_TEST( ctors_acquire ); 600 CPPUNIT_TEST( ctors_no_acquire ); 601 CPPUNIT_TEST( ctors_copy_ctor ); 602 CPPUNIT_TEST_SUITE_END(); 603 604 }; // class ctors 605 606 class send_recv: public CppUnit::TestFixture 607 { 608 public: 609 // initialization 610 void setUp( ) 611 { 612 } 613 614 void tearDown( ) 615 { 616 617 } 618 619 void send_recv1() 620 { 621 osl::Condition aCondition; 622 //client sent two strings, and server received, check the order and value 623 ServerSocketThread myServerThread( aCondition ); 624 ClientSocketThread myClientThread( aCondition ); 625 myServerThread.create( ); 626 myClientThread.create( ); 627 628 //wait until the thread terminate 629 myClientThread.join( ); 630 myServerThread.join( ); 631 sal_Char myStr[30] = ""; 632 strcat( myStr, pTestString1 ); 633 strcat( myStr, pTestString2 ); 634 sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr ); 635 CPPUNIT_ASSERT_MESSAGE(" test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.", 636 nRes == 0 ); 637 } 638 639 // error when recv 640 void send_recv2() 641 { 642 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 643 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ); 644 ::osl::StreamSocket ssStreamConnection; 645 sal_Char pReadBuffer[30] = ""; 646 647 osl::Condition aCondition; 648 aCondition.reset(); 649 ClientSocketThread myClientThread( aCondition ); 650 myClientThread.create( ); 651 652 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); 653 654 asAcceptorSocket.bind( saLocalSocketAddr ); 655 asAcceptorSocket.listen( 1 ); 656 asAcceptorSocket.enableNonBlockingMode( sal_True ); 657 aCondition.set(); 658 659 asAcceptorSocket.acceptConnection( ssStreamConnection ); 660 sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 ); 661 662 myClientThread.join( ) ; 663 ssStreamConnection.close(); 664 asAcceptorSocket.close(); 665 CPPUNIT_ASSERT_MESSAGE(" test for send/recv, recv error!", nReadNumber == -1 ); 666 } 667 668 // LLA: This is a helper function, which create 2 threads, a server and a client. 669 // the server writes the buffersize to the client. 670 671 void write_read(sal_Int32 _nBufferSize, int _nValue) 672 { 673 //client sent two strings, and server received, check the order and value 674 osl::Condition aCondition; 675 WriteSocketThread myServerThread(_nBufferSize, _nValue, aCondition); 676 ReadSocketThread myClientThread(_nBufferSize, _nValue, aCondition); 677 myServerThread.create( ); 678 // thread_sleep( 1 ); 679 myClientThread.create( ); 680 681 //wait until the thread terminate 682 myClientThread.join( ); 683 myServerThread.join( ); 684 685 //Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500 686 // Proteon PRONET = 2046), so here test read 4000 bytes 687 sal_Int32 nLength = myClientThread.getCount(); 688 bool bIsOk = myClientThread.isOk(); // check if the values are right. 689 690 t_print("Length:=%d\n", nLength); 691 t_print(" bIsOk:=%d\n", bIsOk); 692 693 CPPUNIT_ASSERT_MESSAGE(" test for write/read values with two threads: send data from server, check readed data in client.", 694 nLength == _nBufferSize && bIsOk == true); 695 } 696 697 // Tests with different values and sizes 698 void write_read_001() 699 { 700 write_read(50, 10); 701 } 702 void write_read_002() 703 { 704 write_read(1024, 20); 705 } 706 void write_read_003() 707 { 708 write_read(4000, 1); 709 } 710 void write_read_004() 711 { 712 write_read(8192, 3); 713 } 714 void write_read_005() 715 { 716 write_read(32768, 3); 717 } 718 719 CPPUNIT_TEST_SUITE( send_recv ); 720 CPPUNIT_TEST( write_read_001 ); 721 CPPUNIT_TEST( write_read_002 ); 722 CPPUNIT_TEST( write_read_003 ); 723 CPPUNIT_TEST( write_read_004 ); 724 CPPUNIT_TEST( write_read_005 ); 725 CPPUNIT_TEST( send_recv1 ); 726 CPPUNIT_TEST( send_recv2 ); 727 // CPPUNIT_TEST( write_read ); 728 CPPUNIT_TEST_SUITE_END(); 729 }; // class send_recv 730 731 // ----------------------------------------------------------------------------- 732 733 class SendClientThread : public Thread 734 { 735 protected: 736 ::osl::SocketAddr m_saTargetSocketAddr; 737 ::osl::ConnectorSocket m_csConnectorSocket; 738 void SAL_CALL run( ) 739 { 740 TimeValue *pTimeout; 741 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 742 pTimeout->Seconds = 5; 743 pTimeout->Nanosec = 0; 744 745 if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout )) 746 { 747 sal_Int32 nWrite1 = m_csConnectorSocket.write( pTestString1, 11 ); // "test socket" 748 749 sal_Int32 nWrite2 = m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 ); 750 thread_sleep( 2 ); 751 m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 ); 752 t_print("nWrite1 is %d, nWrite2 is %d\n", nWrite1, nWrite2 ); 753 //thread_sleep( 1 ); 754 } 755 else 756 t_print("# SendClientThread: connect failed! \n"); 757 758 m_csConnectorSocket.close(); 759 free( pTimeout ); 760 } 761 public: 762 SendClientThread( ): 763 m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ), 764 m_csConnectorSocket( ) 765 { 766 //t_print("# successfully creat this SendClientThread %d!\n", m_id ); 767 } 768 769 ~SendClientThread( ) 770 { 771 if ( isRunning( ) ) 772 t_print("# error: SendClientThread has not terminated.\n" ); 773 } 774 775 }; 776 777 class shutdown: public CppUnit::TestFixture 778 { 779 public: 780 // initialization 781 void setUp( ) 782 { 783 } 784 785 void tearDown( ) 786 { 787 788 } 789 790 // similar to close_002 791 void shutdown_001() 792 { 793 #if defined(LINUX) 794 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 795 AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") ); 796 myAcceptorThread.create(); 797 798 thread_sleep( 1 ); 799 800 //when accepting, shutdown the socket, the thread will not block for accepting 801 asSocket.shutdown(); 802 myAcceptorThread.join(); 803 804 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", 805 myAcceptorThread.isOK( ) == sal_True ); 806 #endif 807 } 808 809 void shutdown_002() 810 { 811 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 812 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9); 813 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); 814 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True); 815 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True ); 816 sal_Char pReadBuffer[40]; 817 // osl::Condition aCondition; 818 SendClientThread mySendThread; 819 mySendThread.create(); 820 821 asSocket.enableNonBlockingMode( sal_False ); 822 ::osl::StreamSocket ssConnectionSocket; 823 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket ); 824 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok ); 825 826 /* set socket option SO_LINGER 0, so close immediatly */ 827 linger aLingerSet; 828 sal_Int32 nBufferLen = sizeof( struct linger ); 829 aLingerSet.l_onoff = 0; 830 aLingerSet.l_linger = 0; 831 832 ssConnectionSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen ); 833 thread_sleep( 1 ); 834 //sal_uInt32 nRecv1 = 0; 835 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 ); 836 837 //shutdown read after client the first send complete 838 ssConnectionSocket.shutdown( osl_Socket_DirRead ); 839 840 sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 ); 841 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 ); 842 t_print("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", nRead1, nRead2, nRead3 ); 843 mySendThread.join(); 844 845 ssConnectionSocket.close(); 846 asSocket.close(); 847 848 /* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0, 849 http://dbforums.com/arch/186/2002/12/586417 850 While on Solaris, after shutdown(DirRead), all read will return 0 851 */ 852 #ifdef LINUX 853 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).", 854 nRead1 > 0 && nRead3 == 0 ); 855 #else 856 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).", 857 nRead1 > 0 && nRead2 == 0 && nRead3 == 0 ); 858 #endif 859 860 } 861 862 void shutdown_003() 863 { 864 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 865 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9); 866 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); 867 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True); 868 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True ); 869 sal_Char pReadBuffer[40]; 870 osl::Condition aCondition; 871 SendClientThread mySendThread; 872 mySendThread.create(); 873 874 asSocket.enableNonBlockingMode( sal_False ); 875 ::osl::StreamSocket ssConnectionSocket; 876 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket ); 877 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok ); 878 879 thread_sleep( 1 ); 880 //shutdown write after client the first send complete 881 ssConnectionSocket.shutdown( osl_Socket_DirWrite ); 882 883 // recv should not shutdown 884 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 ); 885 886 sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 ); 887 // still can read 888 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 ); 889 t_print("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", nRead1, nWrite, nRead3 ); 890 mySendThread.join(); 891 ssConnectionSocket.close(); 892 asSocket.close(); 893 894 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not send(write).", 895 nRead1 > 0 && nWrite == 0 && nRead3 > 0); 896 897 } 898 899 CPPUNIT_TEST_SUITE( shutdown ); 900 CPPUNIT_TEST( shutdown_001 ); 901 CPPUNIT_TEST( shutdown_002 ); 902 CPPUNIT_TEST( shutdown_003 ); 903 CPPUNIT_TEST_SUITE_END(); 904 }; // class shutdown 905 906 class isExceptionPending: public CppUnit::TestFixture 907 { 908 public: 909 void isExPending_001() 910 { 911 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 912 TimeValue *pTimeout; 913 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 914 pTimeout->Seconds = 3; 915 pTimeout->Nanosec = 0; 916 sal_Bool bOk = asSocket.isExceptionPending( pTimeout ); 917 free( pTimeout ); 918 919 CPPUNIT_ASSERT_MESSAGE( "test for isExceptionPending.", 920 bOk == sal_False ); 921 } 922 923 /**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/ 924 925 926 CPPUNIT_TEST_SUITE( isExceptionPending ); 927 CPPUNIT_TEST( isExPending_001 ); 928 CPPUNIT_TEST_SUITE_END(); 929 }; // class isExceptionPending 930 931 // ----------------------------------------------------------------------------- 932 /** Server Socket Thread, write a file which is large 933 */ 934 // LLA: class WriteSocketThread : public Thread 935 // LLA: { 936 // LLA: ValueCheckProvider m_aValues; 937 // LLA: 938 // LLA: protected: 939 // LLA: oslThreadIdentifier m_id; 940 // LLA: 941 // LLA: void SAL_CALL run( ) 942 // LLA: { 943 // LLA: ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 944 // LLA: ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("10.16.66.252"), 8888 ); 945 // LLA: ::osl::StreamSocket ssStreamConnection; 946 // LLA: 947 // LLA: //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket 948 // LLA: asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 949 // LLA: 950 // LLA: /// if the thread should terminate, schedule return false 951 // LLA: while ( schedule( ) == sal_True ) 952 // LLA: { 953 // LLA: sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 954 // LLA: if ( sal_True != bOK1 ) 955 // LLA: { 956 // LLA: t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ; 957 // LLA: break; 958 // LLA: } 959 // LLA: sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 960 // LLA: if ( sal_True != bOK2 ) 961 // LLA: { 962 // LLA: t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ; 963 // LLA: break; 964 // LLA: } 965 // LLA: // blocking mode, if read/recv failed, block until success 966 // LLA: asAcceptorSocket.enableNonBlockingMode( sal_False); 967 // LLA: 968 // LLA: oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); 969 // LLA: if (eResult != osl_Socket_Ok ) 970 // LLA: { 971 // LLA: t_print("WriteSocketThread: acceptConnection failed! \n"); 972 // LLA: break; 973 // LLA: } 974 // LLA: 975 // LLA: 976 // LLA: sal_Int32 nReadNumber1 = ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() ); 977 // LLA: break; 978 // LLA: } 979 // LLA: ssStreamConnection.close(); 980 // LLA: asAcceptorSocket.close(); 981 // LLA: } 982 // LLA: } 983 // ----------------------------------------------------------------------------- 984 // ----------------------------------------------------------------------------- 985 /** Client Socket Thread, served as a temp little client to communicate with server. 986 */ 987 988 #define IP_PORT_TEST 8900 989 990 class ReadSocket2Thread : public Thread 991 { 992 osl::Condition &m_aCondition; 993 char* m_pBuffer; 994 sal_Int32 m_nBufferSize; 995 sal_Int32 m_nReadCount; 996 rtl::OString m_sAddr; 997 998 bool m_bOk; 999 1000 void setFailed() 1001 { 1002 m_bOk = false; 1003 } 1004 1005 protected: 1006 oslThreadIdentifier m_id; 1007 1008 void read() 1009 { 1010 if (m_sAddr.getLength() == 0) 1011 { 1012 setFailed(); 1013 return; 1014 } 1015 1016 // 10.16.66.252 1017 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(m_sAddr.getStr()), IP_PORT_TEST ); 1018 ::osl::ConnectorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 1019 1020 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True; 1021 1022 m_aCondition.wait(); 1023 t_print("wait done\n"); 1024 1025 TimeValue *pTimeout; 1026 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 1027 pTimeout->Seconds = 20; 1028 pTimeout->Nanosec = 0; 1029 1030 1031 // blocking mode, if read/recv failed, block until success 1032 t_print("enableNonBlockingMode(false)\n"); 1033 aSocket.enableNonBlockingMode( sal_False ); 1034 1035 1036 t_print("connect()\n"); 1037 oslSocketResult eResult = aSocket.connect( aSocketAddr, pTimeout ); 1038 if ( osl_Socket_Ok == eResult) 1039 { 1040 if (m_pBuffer) 1041 { 1042 t_print("read()\n"); 1043 m_nReadCount = aSocket.read( m_pBuffer, m_nBufferSize ); 1044 t_print("%d bytes recived.\n", m_nReadCount); 1045 } 1046 } 1047 else 1048 { 1049 t_print("# ReadSocket2Thread: connect failed! \n"); 1050 printSocketResult(eResult); 1051 setFailed(); 1052 } 1053 1054 //remove this line for deadlock on solaris( margritte.germany ) 1055 aSocket.close(); 1056 free( pTimeout ); 1057 } 1058 1059 void SAL_CALL run( ) 1060 { 1061 read(); 1062 } 1063 1064 void SAL_CALL onTerminated( ) 1065 { 1066 //t_print("# normally terminate this thread %d!\n", m_id ); 1067 } 1068 1069 public: 1070 sal_Int32 getCount() {return m_nReadCount;} 1071 bool isOk() {return m_nReadCount == 0 ? false : true;} 1072 bool getFailed() {return m_bOk == false ? true : false;} 1073 1074 ReadSocket2Thread(osl::Condition &_aCondition) 1075 :m_aCondition(_aCondition), 1076 m_nReadCount(0), 1077 m_bOk( true ) 1078 { 1079 m_aCondition.reset(); 1080 m_pBuffer = (char*) malloc(1024); 1081 if (m_pBuffer) 1082 { 1083 m_nBufferSize = 1024; 1084 } 1085 1086 m_id = getIdentifier( ); 1087 //t_print("# successfully creat this client thread %d!\n", m_id ); 1088 } 1089 1090 void setAddr(rtl::OString const& _sAddr) 1091 { 1092 m_sAddr = _sAddr; 1093 } 1094 1095 ~ReadSocket2Thread( ) 1096 { 1097 if ( isRunning( ) ) 1098 t_print("# error: client thread not terminated.\n" ); 1099 free(m_pBuffer); 1100 } 1101 1102 }; 1103 1104 // ----------------------------------------------------------------------------- 1105 1106 class justtest : public CppUnit::TestFixture 1107 { 1108 void send_Acceptor(rtl::OString const& _sAddr, osl::Condition &) 1109 { 1110 ::osl::AcceptorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 1111 ::osl::SocketAddr aSocketAddr; 1112 1113 if (! aSocketAddr.setPort(IP_PORT_TEST)) 1114 { 1115 t_print("# cant set port\n" ); 1116 } 1117 1118 if (! aSocketAddr.setHostname(rtl::OUString::createFromAscii(_sAddr.getStr()))) 1119 { 1120 t_print("# cant set hostname/ip\n" ); 1121 } 1122 1123 rtl::OUString aHostname = aSocketAddr.getHostname(); 1124 aSocketAddr.getPort(); 1125 1126 1127 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket 1128 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1129 1130 /// if the thread should terminate, schedule return false 1131 // while ( schedule( ) == sal_True ) 1132 // { 1133 if (! aSocket.bind( aSocketAddr )) 1134 { 1135 t_print("# can't bind.\n" ); 1136 } 1137 if (! aSocket.listen( )) 1138 { 1139 t_print("# can't listen. \n" ); 1140 } 1141 1142 // blocking mode, if read/recv failed, block until success 1143 aSocket.enableNonBlockingMode( sal_False); 1144 ::osl::StreamSocket ssStreamConnection; 1145 1146 oslSocketResult eResult = aSocket.acceptConnection( ssStreamConnection ); 1147 if (eResult != osl_Socket_Ok ) 1148 { 1149 t_print("WriteSocketThread: acceptConnection failed! \n"); 1150 // break; 1151 } 1152 char const * pBuffer = "Test String\n"; 1153 sal_Int32 nBufferSize = strlen(pBuffer); 1154 ssStreamConnection.write( pBuffer, nBufferSize ); 1155 // break; 1156 // } 1157 1158 // ssStreamConnection.close(); 1159 aSocket.close(); 1160 } 1161 1162 // ----------------------------------------------------------------------------- 1163 1164 void send_Connector(rtl::OString const& _sAddr, osl::Condition &/*_aCondition*/ ) 1165 { 1166 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 1167 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST ); 1168 1169 if (! aSocketAddr.is()) 1170 { 1171 t_print("is failed.\n"); 1172 return; 1173 } 1174 1175 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket 1176 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True; 1177 1178 oslSocketResult aResult = aSocket.connect( aSocketAddr ); 1179 if ( aResult != osl_Socket_Ok ) 1180 { 1181 t_print("# send_Connector: connect failed. \n" ); 1182 } 1183 else 1184 { 1185 // blocking mode, if read/recv failed, block until success 1186 // aSocket.enableNonBlockingMode( sal_False ); 1187 1188 // _aCondition.set(); 1189 1190 ::osl::StreamSocket ssStreamConnection(aSocket); 1191 1192 char const * pBuffer = "GET / HTTP/1.0\015\012\015\012"; 1193 sal_Int32 nBufferSize = strlen(pBuffer); 1194 ssStreamConnection.write( pBuffer, nBufferSize ); 1195 1196 char *pBufferPeek = (char*) malloc(1024); 1197 sal_Int32 nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek); 1198 free(pBufferPeek); 1199 1200 char *pBuffer2 = (char*) malloc(nReadNumber + 1); 1201 sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber ); 1202 pBuffer2[nReadNumberReal] = '\0'; 1203 1204 t_print("received: %s\n", pBuffer2); 1205 1206 // char * pBuffer3 = "quit\n"; 1207 // nBufferSize = strlen(pBuffer3); 1208 // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize ); 1209 1210 rtl::OUString suError = ssStreamConnection.getErrorAsString(); 1211 free(pBuffer2); 1212 // ssStreamConnection.close(); 1213 1214 // ssStreamConnection.close(); 1215 } 1216 aSocket.shutdown(osl_Socket_DirReadWrite); 1217 aSocket.close(); 1218 } 1219 1220 1221 public: 1222 // LLA: orig void send_recv() 1223 // LLA: orig { 1224 // LLA: orig if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True ) 1225 // LLA: orig t_print("margritte is alive ! \n"); 1226 // LLA: orig if ( ifAvailable(rtl::OUString::createFromAscii("10.16.66.252")) == sal_False ) 1227 // LLA: orig { 1228 // LLA: orig t_print("ip 10.16.66.252 is not alive! \n"); 1229 // LLA: orig return; 1230 // LLA: orig } 1231 // LLA: orig ReadSocket2Thread myReadThread; 1232 // LLA: orig myReadThread.create(); 1233 // LLA: orig 1234 // LLA: orig thread_sleep( 2 ); 1235 // LLA: orig // send_Acceptor(); 1236 // LLA: orig send_Connector(); 1237 // LLA: orig 1238 // LLA: orig myReadThread.join(); 1239 // LLA: orig 1240 // LLA: orig // statistics 1241 // LLA: orig sal_uInt32 nLength = myReadThread.getCount(); 1242 // LLA: orig bool bIsOk = myReadThread.isOk(); // check if the values are right. 1243 // LLA: orig 1244 // LLA: orig t_print("Length:=%d\n", nLength); 1245 // LLA: orig t_print(" bIsOk:=%d\n", bIsOk); 1246 // LLA: orig } 1247 1248 // ----------------------------------------------------------------------------- 1249 1250 // LLA: send_Connector_2_margritte works, it send strings to echo server on margritte 1251 // but can not receive anything 1252 1253 void send_Connector_2_margritte(rtl::OString const& _sAddr) 1254 { 1255 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 1256 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST ); 1257 1258 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket 1259 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True; 1260 1261 oslSocketResult aResult = aSocket.connect( aSocketAddr ); 1262 if ( aResult != osl_Socket_Ok ) 1263 { 1264 t_print("# connect failed. \n" ); 1265 } 1266 else 1267 { 1268 // blocking mode, if read/recv failed, block until success 1269 aSocket.enableNonBlockingMode( sal_False ); 1270 1271 ::osl::StreamSocket ssStreamConnection(aSocket); 1272 1273 char const * pBuffer = "Test String\n"; 1274 sal_Int32 nBufferSize = strlen(pBuffer); 1275 sal_Int32 nWriteNumber = ssStreamConnection.write( pBuffer, nBufferSize ); 1276 1277 // char * pBuffer2 = " "; 1278 // sal_Int32 nReadNumber = ssStreamConnection.read( pBuffer2, strlen(pBuffer2) ); 1279 1280 char const * pBuffer3 = "quit\n"; 1281 nBufferSize = strlen(pBuffer3); 1282 nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize ); 1283 1284 ssStreamConnection.close(); 1285 } 1286 aSocket.close(); 1287 } 1288 1289 void send_recv_2_margritte() 1290 { 1291 rtl::OString sAddr; 1292 sAddr = "margritte.germany.sun.com"; 1293 if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True ) 1294 { 1295 t_print("found %s!\n", sAddr.getStr()); 1296 } 1297 send_Connector_2_margritte(sAddr); 1298 } 1299 1300 // ----------------------------------------------------------------------------- 1301 1302 void send_recv() 1303 { 1304 rtl::OString sAddr; 1305 // if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True ) 1306 // { 1307 // t_print("margritte is alive ! \n"); 1308 // sAddr = "margritte.germany"; 1309 // } 1310 1311 sAddr = "margritte.germany.sun.com"; 1312 if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True ) 1313 { 1314 t_print("found %s!\n", sAddr.getStr()); 1315 } 1316 // else 1317 // { 1318 // if ( ifAvailable(rtl::OUString::createFromAscii("192.168.7.2")) == sal_True ) 1319 // { 1320 // sAddr = "192.168.7.2"; 1321 // t_print("moon found ! \n"); 1322 // } 1323 // else 1324 // { 1325 // if ( ifAvailable(rtl::OUString::createFromAscii("moon.linux.bogus")) == sal_True ) 1326 // { 1327 // sAddr = "moon.linux.bogus"; 1328 // t_print("moon found ! \n"); 1329 // } 1330 // else 1331 // { 1332 // if ( ifAvailable(rtl::OUString::createFromAscii("moon")) == sal_True ) 1333 // { 1334 // sAddr = "moon"; 1335 // t_print("moon found ! \n"); 1336 // } 1337 // } 1338 // } 1339 // } 1340 1341 // if ( ifAvailable(rtl::OUString::createFromAscii("10.16.64.196")) == sal_False ) 1342 // { 1343 // t_print("ip 10.16.64.196 is not alive! \n"); 1344 // return; 1345 // } 1346 1347 osl::Condition aCondition; 1348 ReadSocket2Thread myReadThread(aCondition); 1349 myReadThread.setAddr(sAddr); 1350 // myReadThread.create(); 1351 1352 thread_sleep( 2 ); 1353 if (! myReadThread.getFailed()) 1354 { 1355 // send_Acceptor(sAddr, aCondition); 1356 send_Connector(sAddr, aCondition); 1357 1358 thread_sleep( 2 ); 1359 if (myReadThread.isRunning()) 1360 { 1361 myReadThread.join(); 1362 } 1363 // termAndJoinThread(&myReadThread); 1364 1365 // statistics 1366 sal_uInt32 nLength = myReadThread.getCount(); 1367 bool bIsOk = myReadThread.isOk(); // check if the values are right. 1368 1369 t_print("Length:=%d\n", nLength); 1370 t_print(" bIsOk:=%d\n", bIsOk); 1371 } 1372 else 1373 { 1374 t_print("ERROR: No echo Server on %s found.\n", sAddr.getStr()); 1375 } 1376 } 1377 1378 1379 void getPage(rtl::OString const& _sAddr); 1380 void test_getPage() 1381 { 1382 // rtl::OString sPage("lla-1.germany.sun.com"); 1383 // getPage(sPage); 1384 1385 rtl::OString sPage("lla-1"); 1386 getPage(sPage); 1387 } 1388 1389 CPPUNIT_TEST_SUITE( justtest ); 1390 CPPUNIT_TEST( send_recv ); 1391 CPPUNIT_TEST( test_getPage ); 1392 CPPUNIT_TEST_SUITE_END(); 1393 }; // class isExceptionPending 1394 1395 1396 void justtest::getPage(rtl::OString const& _sAddr) 1397 { 1398 rtl::OUString suAddr = rtl::OUString::createFromAscii(_sAddr.getStr()); 1399 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 1400 ::osl::SocketAddr aSocketAddr( suAddr, 80 ); 1401 1402 { 1403 // some checks 1404 aSocketAddr.getPort(); 1405 oslSocketResult aResult; 1406 rtl::ByteSequence aSeq = aSocketAddr.getAddr(&aResult); 1407 if (aResult != osl_Socket_Ok) 1408 { 1409 t_print("problem with getAddr: "); 1410 printSocketResult(aResult); 1411 } 1412 1413 rtl::OUString sStr = aSocketAddr.getHostname(&aResult); 1414 if (aResult != osl_Socket_Ok) 1415 { 1416 t_print("problem with hostname: "); 1417 printSocketResult(aResult); 1418 } 1419 } 1420 1421 oslSocketResult aResult; 1422 1423 // SocketAddr::resolveHostname(suAddr, aSocketAddr); 1424 // if (! aSocketAddr.is()) 1425 // { 1426 // t_print("Can't resolve Hostname.\n"); 1427 // return; 1428 // } 1429 // rtl::OUString sStr = aSocketAddr.getHostname(&aResult); 1430 // if (aResult != osl_Socket_Ok) 1431 // { 1432 // t_print("problem with hostname: "); 1433 // printSocketResult(aResult); 1434 // 1435 // } 1436 1437 if (! aSocketAddr.is()) 1438 { 1439 t_print("SocketAddr::is() failed.\n"); 1440 return; 1441 } 1442 1443 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket 1444 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True; 1445 1446 aResult = aSocket.connect( aSocketAddr ); 1447 if ( aResult != osl_Socket_Ok ) 1448 { 1449 t_print("# send_Connector: connect failed. \n" ); 1450 } 1451 else 1452 { 1453 // blocking mode, if read/recv failed, block until success 1454 // aSocket.enableNonBlockingMode( sal_False ); 1455 1456 // _aCondition.set(); 1457 1458 ::osl::StreamSocket ssStreamConnection(aSocket); 1459 1460 char const * pBuffer = "GET / HTTP/1.0\015\012\015\012"; 1461 sal_Int32 nBufferSize = strlen(pBuffer); 1462 ssStreamConnection.write( pBuffer, nBufferSize ); 1463 1464 1465 char *pBufferPeek = (char*) malloc(1024); 1466 sal_Int32 nReadNumber = 1; 1467 while ( nReadNumber != 0) 1468 { 1469 nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek); 1470 if (nReadNumber > 0) 1471 { 1472 char *pBuffer2 = (char*) malloc(nReadNumber + 1); 1473 sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber ); 1474 pBuffer2[nReadNumberReal] = '\0'; 1475 t_print("%s", pBuffer2); 1476 free(pBuffer2); 1477 } 1478 } 1479 free(pBufferPeek); 1480 1481 // char * pBuffer3 = "quit\n"; 1482 // nBufferSize = strlen(pBuffer3); 1483 // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize ); 1484 1485 rtl::OUString suError = ssStreamConnection.getErrorAsString(); 1486 } 1487 aSocket.shutdown(osl_Socket_DirReadWrite); 1488 aSocket.close(); 1489 } 1490 1491 // ----------------------------------------------------------------------------- 1492 1493 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::ctors, "osl_StreamSocket"); 1494 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::send_recv, "osl_StreamSocket"); 1495 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::shutdown, "osl_StreamSocket"); 1496 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::isExceptionPending, "osl_StreamSocket"); 1497 1498 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::justtest, "osl_StreamSocket"); 1499 1500 } // namespace osl_StreamSocket 1501 1502 // ----------------------------------------------------------------------------- 1503 1504 // this macro creates an empty function, which will called by the RegisterAllFunctions() 1505 // to let the user the possibility to also register some functions by hand. 1506 NOADDITIONAL; 1507