merge the formfield patch from ooo-build
[ooovba.git] / sal / qa / osl / socket / osl_StreamSocket.cxx
blob6f65afd31ee72f887f6402577fbc19da5cbe1433
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: osl_StreamSocket.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 /** test coder preface:
35 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
36 if you are not including ws2_32.lib in makefile.mk, the including format will be like this:
38 .IF "$(GUI)" == "WNT"
39 SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
40 SHL1STDLIBS += ws2_32.lib
41 .ENDIF
43 likewise on Solaris platform.
44 .IF "$(GUI)" == "UNX"
45 SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
46 SHL1STDLIBS += -lsocket -ldl -lnsl
47 .ENDIF
49 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
50 category.
52 3. some fragment of Socket source implementation are lack of comment so it is hard for testers
53 guess what the exact functionality or usage of a member. Hope the Socket section's comment
54 will be added.
56 4. following functions are declared but not implemented:
57 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
60 //------------------------------------------------------------------------
61 // include files
62 //------------------------------------------------------------------------
64 #include <cppunit/simpleheader.hxx>
66 //#include "osl_Socket_Const.h"
67 #include "sockethelper.hxx"
68 #include <osl/conditn.hxx>
70 using namespace osl;
71 using namespace rtl;
73 #define IP_PORT_MYPORT9 8897
74 #define IP_PORT_MYPORT10 18900
76 const char * pTestString1 = "test socket";
77 const char * pTestString2 = " Passed#OK";
79 //------------------------------------------------------------------------
80 // helper functions
81 //------------------------------------------------------------------------
83 // just used to test socket::close() when accepting
84 class AcceptorThread : public Thread
86 ::osl::AcceptorSocket asAcceptorSocket;
87 ::rtl::OUString aHostIP;
88 sal_Bool bOK;
89 protected:
90 void SAL_CALL run( )
92 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
93 ::osl::StreamSocket ssStreamConnection;
95 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
96 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
97 if ( sal_True != bOK1 )
99 t_print("# AcceptorSocket bind address failed.\n" ) ;
100 return;
102 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
103 if ( sal_True != bOK2 )
105 t_print("# AcceptorSocket listen address failed.\n" ) ;
106 return;
109 asAcceptorSocket.enableNonBlockingMode( sal_False );
111 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
112 if (eResult != osl_Socket_Ok )
114 bOK = sal_True;
115 t_print("AcceptorThread: acceptConnection failed! \n");
118 public:
119 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
120 : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
122 bOK = sal_False;
125 sal_Bool isOK() { return bOK; }
127 ~AcceptorThread( )
129 if ( isRunning( ) )
131 asAcceptorSocket.shutdown();
132 t_print("# error: Acceptor thread not terminated.\n" );
137 /** Server Socket Thread, served as a temp little server to communicate with client.
139 class ServerSocketThread : public Thread
141 osl::Condition &m_aCondition;
142 protected:
143 oslThreadIdentifier m_id;
145 void SAL_CALL run( )
147 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
148 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 );
149 ::osl::StreamSocket ssStreamConnection;
151 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
152 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
153 while ( schedule( ) == sal_True )
155 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
156 if ( sal_True != bOK1 )
158 t_print("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ;
159 break;
161 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
162 if ( sal_True != bOK2 )
164 t_print("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ;
165 break;
168 asAcceptorSocket.enableNonBlockingMode( sal_False );
169 m_aCondition.set();
171 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
172 if (eResult != osl_Socket_Ok )
174 t_print("ServerSocketThread: acceptConnection failed! \n");
175 break;
177 sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 );
178 sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 );
179 pReadBuffer[nReadNumber1 + nReadNumber2] = '\0';
180 //t_print("# read buffer content: %s\n", pReadBuffer );
181 break;
183 ssStreamConnection.close();
184 asAcceptorSocket.close();
188 void SAL_CALL onTerminated( )
190 //t_print("# normally terminate this server thread %d!\n", m_id );
193 public:
194 // public to check if data transmition is OK
195 sal_Char pReadBuffer[30];
196 ServerSocketThread( osl::Condition &_aCond ):m_aCondition(_aCond)
198 m_aCondition.reset();
199 t_print("#init ServerSocketThread\n");
200 m_id = getIdentifier( );
201 //t_print("# successfully creat this ServerSocketThread %d!\n", m_id );
204 ~ServerSocketThread( )
206 if ( isRunning( ) )
207 t_print("# error: ServerSocketThread has not terminated.\n" );
211 /** Client Socket Thread, served as a temp little client to communicate with server.
213 class ClientSocketThread : public Thread
215 protected:
216 osl::Condition &m_aCondition;
217 oslThreadIdentifier m_id;
218 ::osl::SocketAddr m_saTargetSocketAddr;
219 ::osl::ConnectorSocket m_csConnectorSocket;
221 void SAL_CALL run( )
223 TimeValue *pTimeout;
224 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
225 pTimeout->Seconds = 5;
226 pTimeout->Nanosec = 0;
228 /// if the thread should terminate, schedule return false
229 //while ( schedule( ) == sal_True )
231 if ( osl::Condition::result_ok != m_aCondition.wait( pTimeout ) )
233 free( pTimeout );
234 return;
237 if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout ))
239 m_csConnectorSocket.send( pTestString1, 11 ); // "test socket"
240 m_csConnectorSocket.send( pTestString2, 10);
242 else
243 t_print("# ClientSocketThread: connect failed! \n");
244 // terminate();
246 m_csConnectorSocket.close();
247 free( pTimeout );
250 void SAL_CALL onTerminated( )
252 //t_print("# normally terminate this thread %d!\n", m_id );
255 public:
256 ClientSocketThread( osl::Condition &_aCond ):
257 m_aCondition(_aCond),
258 m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ),
259 m_csConnectorSocket( )
261 m_id = getIdentifier( );
262 //t_print("# successfully creat this client thread %d!\n", m_id );
265 ~ClientSocketThread( )
267 if ( isRunning( ) )
268 t_print("# error: client thread not terminated.\n" );
273 // -----------------------------------------------------------------------------
274 // Helper functions, to create buffers, check buffers
275 class ValueCheckProvider
277 bool m_bFoundFailure;
278 char *m_pBuffer;
279 sal_Int32 m_nBufferSize;
281 public:
282 ValueCheckProvider()
283 :m_bFoundFailure(false),
284 m_pBuffer(NULL),
285 m_nBufferSize(0)
289 bool isFailure() {return m_bFoundFailure;}
291 const char* getBuffer() {return m_pBuffer;}
292 char* getWriteBuffer() {return m_pBuffer;}
294 sal_Int32 getBufferSize() {return m_nBufferSize;}
296 bool checkValues(sal_Int32 _nLength, int _nValue)
298 m_bFoundFailure = false;
299 for(sal_Int32 i=0;i<_nLength;i++)
301 if (m_pBuffer[i] != _nValue)
303 m_bFoundFailure = true;
306 return m_bFoundFailure;
309 void createBuffer(sal_Int32 _nLength, int _nValue)
311 m_nBufferSize = _nLength;
312 m_pBuffer = (char*) malloc(m_nBufferSize);
313 if (m_pBuffer)
315 memset(m_pBuffer, _nValue, m_nBufferSize);
319 void freeBuffer()
321 if (m_pBuffer) free(m_pBuffer);
326 // -----------------------------------------------------------------------------
327 /** Client Socket Thread, served as a temp little client to communicate with server.
330 class ReadSocketThread : public Thread
332 ValueCheckProvider m_aValues;
333 int m_nValue;
334 osl::Condition &m_aCondition;
336 protected:
337 oslThreadIdentifier m_id;
339 void SAL_CALL run( )
341 ::osl::SocketAddr m_aTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 );
342 ::osl::ConnectorSocket m_aConnectorSocket;
344 if (! m_aTargetSocketAddr.is())
346 t_print("# SocketAddr was NOT created successfully!\n");
348 else
350 t_print("start ReadSocketThread\n");
352 TimeValue *pTimeout;
353 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
354 pTimeout->Seconds = 5;
355 pTimeout->Nanosec = 0;
357 m_aCondition.wait();
359 t_print("connect()\n");
361 oslSocketResult eResult = m_aConnectorSocket.connect( m_aTargetSocketAddr, pTimeout );
362 if ( osl_Socket_Ok == eResult )
364 sal_Int32 nReadCount = m_aConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() );
365 m_aValues.checkValues(nReadCount, m_nValue);
367 else
369 t_print("# ReadSocketThread: connect failed! \n");
370 printSocketResult(eResult);
373 //remove this line for deadlock on solaris( margritte.germany )
374 m_aConnectorSocket.close();
375 free( pTimeout );
379 void SAL_CALL onTerminated( )
381 //t_print("# normally terminate this thread %d!\n", m_id );
384 public:
385 sal_Int32 getCount() {return m_aValues.getBufferSize();}
386 bool isOk() {return m_aValues.isFailure() == true ? false : true;}
388 ReadSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond )
389 : m_nValue( _nValue ),
390 m_aCondition(_aCond)
392 t_print("#init ReadSocketThread\n");
393 m_id = getIdentifier( );
395 //t_print("# successfully creat this client thread %d!\n", m_id );
396 m_aValues.createBuffer(_nBufferSize, 0);
399 ~ReadSocketThread( )
401 if ( isRunning( ) )
402 t_print("# error: client thread not terminated.\n" );
403 m_aValues.freeBuffer();
408 /** Server Socket Thread, write a file which is large
410 class WriteSocketThread : public Thread
412 ValueCheckProvider m_aValues;
413 osl::Condition &m_aCondition;
415 protected:
416 oslThreadIdentifier m_id;
418 void SAL_CALL run( )
420 t_print("start WriteSocketThread\n");
421 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
422 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 );
423 if (! saLocalSocketAddr.is())
425 t_print("LocalSocketAddr was NOT created successfully!\n");
428 ::osl::StreamSocket ssStreamConnection;
430 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
431 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
433 /// if the thread should terminate, schedule return false
434 // while ( schedule( ) == sal_True )
435 // {
436 t_print("bind()\n");
437 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
438 if ( sal_True != bOK1 )
440 t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
442 else
444 t_print("listen()\n");
445 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
446 if ( sal_True != bOK2 )
448 t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
450 else
453 // blocking mode, if read/recv failed, block until success
454 asAcceptorSocket.enableNonBlockingMode( sal_False);
455 t_print("acceptConnection()\n");
456 m_aCondition.set();
458 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
459 if (eResult != osl_Socket_Ok )
461 t_print("WriteSocketThread: acceptConnection failed! \n");
463 else
466 // LLA: removed, due to the fact, this is to error prone
467 // LLA: char * pSrcRoot = getenv("SOURCE_ROOT");
468 // LLA: // LLA: This is absolute wrong!
469 // LLA: // strcat( pSrcRoot, "/sal/inc/osl/file.hxx");
470 // LLA: rtl::OString sSrcRoot(pSrcRoot);
471 // LLA: sSrcRoot += "/sal/inc/osl/file.hxx";
472 // LLA:
473 // LLA: ::rtl::OUString sFilePath = ::rtl::OUString::createFromAscii( sSrcRoot.getStr() );
474 // LLA: #ifdef WNT
475 // LLA: while (sFilePath.lastIndexOf('/') != -1)
476 // LLA: sFilePath = sFilePath.replace('/',(sal_Unicode)'\\');
477 // LLA: #endif
478 // LLA: FILE *stream;
479 // LLA: sal_uInt64 nCount_read;
480 // LLA: sal_Char buffer_read[FILE_READ];
481 // LLA:
482 // LLA: if( (stream = fopen( oustring2char( sFilePath ), "r+t" )) != NULL )
483 // LLA: {
484 // LLA: /* Attempt to read in 25 characters */
485 // LLA: nCount_read = fread( buffer_read, sizeof( char ), FILE_READ, stream );
486 // LLA: fclose( stream );
487 // LLA: }
488 // LLA: else
489 // LLA: t_print("# File $SRC_ROOT/sal/inc/osl/file.hxx could not be opened\n" );
491 t_print("write()\n");
493 ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
494 t_print("done written.\n");
498 ssStreamConnection.close();
499 asAcceptorSocket.close();
502 void SAL_CALL onTerminated( )
504 //t_print("# normally terminate this server thread %d!\n", m_id );
507 public:
508 // public to check if data transmition is OK
509 WriteSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond )
510 : m_aCondition(_aCond)
512 m_aCondition.reset();
514 t_print("#init WriteSocketThread\n");
515 m_id = getIdentifier( );
516 //t_print("# successfully creat this server thread %d!\n", m_id );
518 m_aValues.createBuffer(_nBufferSize, _nValue);
521 ~WriteSocketThread( )
523 if ( isRunning( ) )
524 t_print("# error: server thread not terminated.\n" );
525 m_aValues.freeBuffer();
529 // -----------------------------------------------------------------------------
531 namespace osl_StreamSocket
534 /** testing the methods:
535 inline StreamSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
536 oslProtocol Protocol = osl_Socket_ProtocolIp,
537 oslSocketType Type = osl_Socket_TypeStream);
539 inline StreamSocket( const StreamSocket & );
541 inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire );
543 inline StreamSocket( oslSocket Socket );
546 class ctors : public CppUnit::TestFixture
548 public:
549 oslSocket sHandle;
550 // initialization
551 void setUp( )
553 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
556 void tearDown( )
558 sHandle = NULL;
562 void ctors_none()
564 /// Socket constructor.
565 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
567 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the stream socket was created successfully.",
568 osl_Socket_TypeStream == ssSocket.getType( ) );
571 void ctors_acquire()
573 /// Socket constructor.
574 ::osl::StreamSocket ssSocket( sHandle );
576 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
577 osl_Socket_TypeStream == ssSocket.getType( ) );
580 void ctors_no_acquire()
582 /// Socket constructor.
583 ::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE );
585 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
586 osl_Socket_TypeStream == ssSocket.getType( ) );
589 void ctors_copy_ctor()
591 /// Socket constructor.
592 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
593 /// Socket copy constructor.
594 ::osl::StreamSocket copySocket( ssSocket );
596 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
597 osl_Socket_TypeStream == copySocket.getType( ) );
600 CPPUNIT_TEST_SUITE( ctors );
601 CPPUNIT_TEST( ctors_none );
602 CPPUNIT_TEST( ctors_acquire );
603 CPPUNIT_TEST( ctors_no_acquire );
604 CPPUNIT_TEST( ctors_copy_ctor );
605 CPPUNIT_TEST_SUITE_END();
607 }; // class ctors
609 class send_recv: public CppUnit::TestFixture
611 public:
612 // initialization
613 void setUp( )
617 void tearDown( )
622 void send_recv1()
624 osl::Condition aCondition;
625 //client sent two strings, and server received, check the order and value
626 ServerSocketThread myServerThread( aCondition );
627 ClientSocketThread myClientThread( aCondition );
628 myServerThread.create( );
629 myClientThread.create( );
631 //wait until the thread terminate
632 myClientThread.join( );
633 myServerThread.join( );
634 sal_Char myStr[30] = "";
635 strcat( myStr, pTestString1 );
636 strcat( myStr, pTestString2 );
637 sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr );
638 CPPUNIT_ASSERT_MESSAGE(" test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.",
639 nRes == 0 );
642 // error when recv
643 void send_recv2()
645 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
646 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 );
647 ::osl::StreamSocket ssStreamConnection;
648 sal_Char pReadBuffer[30] = "";
650 osl::Condition aCondition;
651 aCondition.reset();
652 ClientSocketThread myClientThread( aCondition );
653 myClientThread.create( );
655 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
657 asAcceptorSocket.bind( saLocalSocketAddr );
658 asAcceptorSocket.listen( 1 );
659 asAcceptorSocket.enableNonBlockingMode( sal_True );
660 aCondition.set();
662 asAcceptorSocket.acceptConnection( ssStreamConnection );
663 sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 );
665 myClientThread.join( ) ;
666 ssStreamConnection.close();
667 asAcceptorSocket.close();
668 CPPUNIT_ASSERT_MESSAGE(" test for send/recv, recv error!", nReadNumber == -1 );
671 // LLA: This is a helper function, which create 2 threads, a server and a client.
672 // the server writes the buffersize to the client.
674 void write_read(sal_Int32 _nBufferSize, int _nValue)
676 //client sent two strings, and server received, check the order and value
677 osl::Condition aCondition;
678 WriteSocketThread myServerThread(_nBufferSize, _nValue, aCondition);
679 ReadSocketThread myClientThread(_nBufferSize, _nValue, aCondition);
680 myServerThread.create( );
681 // thread_sleep( 1 );
682 myClientThread.create( );
684 //wait until the thread terminate
685 myClientThread.join( );
686 myServerThread.join( );
688 //Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500
689 // Proteon PRONET = 2046), so here test read 4000 bytes
690 sal_Int32 nLength = myClientThread.getCount();
691 bool bIsOk = myClientThread.isOk(); // check if the values are right.
693 t_print("Length:=%d\n", nLength);
694 t_print(" bIsOk:=%d\n", bIsOk);
696 CPPUNIT_ASSERT_MESSAGE(" test for write/read values with two threads: send data from server, check readed data in client.",
697 nLength == _nBufferSize && bIsOk == true);
700 // Tests with different values and sizes
701 void write_read_001()
703 write_read(50, 10);
705 void write_read_002()
707 write_read(1024, 20);
709 void write_read_003()
711 write_read(4000, 1);
713 void write_read_004()
715 write_read(8192, 3);
717 void write_read_005()
719 write_read(32768, 3);
722 CPPUNIT_TEST_SUITE( send_recv );
723 CPPUNIT_TEST( write_read_001 );
724 CPPUNIT_TEST( write_read_002 );
725 CPPUNIT_TEST( write_read_003 );
726 CPPUNIT_TEST( write_read_004 );
727 CPPUNIT_TEST( write_read_005 );
728 CPPUNIT_TEST( send_recv1 );
729 CPPUNIT_TEST( send_recv2 );
730 // CPPUNIT_TEST( write_read );
731 CPPUNIT_TEST_SUITE_END();
732 }; // class send_recv
734 // -----------------------------------------------------------------------------
736 class SendClientThread : public Thread
738 protected:
739 ::osl::SocketAddr m_saTargetSocketAddr;
740 ::osl::ConnectorSocket m_csConnectorSocket;
741 void SAL_CALL run( )
743 TimeValue *pTimeout;
744 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
745 pTimeout->Seconds = 5;
746 pTimeout->Nanosec = 0;
748 if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout ))
750 sal_Int32 nWrite1 = m_csConnectorSocket.write( pTestString1, 11 ); // "test socket"
752 sal_Int32 nWrite2 = m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
753 thread_sleep( 2 );
754 m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
755 t_print("nWrite1 is %d, nWrite2 is %d\n", nWrite1, nWrite2 );
756 //thread_sleep( 1 );
758 else
759 t_print("# SendClientThread: connect failed! \n");
761 m_csConnectorSocket.close();
762 free( pTimeout );
764 public:
765 SendClientThread( ):
766 m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ),
767 m_csConnectorSocket( )
769 //t_print("# successfully creat this SendClientThread %d!\n", m_id );
772 ~SendClientThread( )
774 if ( isRunning( ) )
775 t_print("# error: SendClientThread has not terminated.\n" );
780 class shutdown: public CppUnit::TestFixture
782 public:
783 // initialization
784 void setUp( )
788 void tearDown( )
793 // similar to close_002
794 void shutdown_001()
796 #if defined(LINUX)
797 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
798 AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") );
799 myAcceptorThread.create();
801 thread_sleep( 1 );
803 //when accepting, shutdown the socket, the thread will not block for accepting
804 asSocket.shutdown();
805 myAcceptorThread.join();
807 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
808 myAcceptorThread.isOK( ) == sal_True );
809 #endif
812 void shutdown_002()
814 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
815 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9);
816 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
817 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
818 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
819 sal_Char pReadBuffer[40];
820 // osl::Condition aCondition;
821 SendClientThread mySendThread;
822 mySendThread.create();
824 asSocket.enableNonBlockingMode( sal_False );
825 ::osl::StreamSocket ssConnectionSocket;
826 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
827 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
829 /* set socket option SO_LINGER 0, so close immediatly */
830 linger aLingerSet;
831 sal_Int32 nBufferLen = sizeof( struct linger );
832 aLingerSet.l_onoff = 0;
833 aLingerSet.l_linger = 0;
835 ssConnectionSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
836 thread_sleep( 1 );
837 //sal_uInt32 nRecv1 = 0;
838 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
840 //shutdown read after client the first send complete
841 ssConnectionSocket.shutdown( osl_Socket_DirRead );
843 sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 );
844 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 );
845 t_print("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", nRead1, nRead2, nRead3 );
846 mySendThread.join();
848 ssConnectionSocket.close();
849 asSocket.close();
851 /* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0,
852 http://dbforums.com/arch/186/2002/12/586417
853 While on Solaris, after shutdown(DirRead), all read will return 0
855 #ifdef LINUX
856 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
857 nRead1 > 0 && nRead3 == 0 );
858 #else
859 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
860 nRead1 > 0 && nRead2 == 0 && nRead3 == 0 );
861 #endif
865 void shutdown_003()
867 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
868 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9);
869 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
870 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
871 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
872 sal_Char pReadBuffer[40];
873 osl::Condition aCondition;
874 SendClientThread mySendThread;
875 mySendThread.create();
877 asSocket.enableNonBlockingMode( sal_False );
878 ::osl::StreamSocket ssConnectionSocket;
879 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
880 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
882 thread_sleep( 1 );
883 //shutdown write after client the first send complete
884 ssConnectionSocket.shutdown( osl_Socket_DirWrite );
886 // recv should not shutdown
887 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
889 sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 );
890 // still can read
891 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 );
892 t_print("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", nRead1, nWrite, nRead3 );
893 mySendThread.join();
894 ssConnectionSocket.close();
895 asSocket.close();
897 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not send(write).",
898 nRead1 > 0 && nWrite == 0 && nRead3 > 0);
902 CPPUNIT_TEST_SUITE( shutdown );
903 CPPUNIT_TEST( shutdown_001 );
904 CPPUNIT_TEST( shutdown_002 );
905 CPPUNIT_TEST( shutdown_003 );
906 CPPUNIT_TEST_SUITE_END();
907 }; // class shutdown
909 class isExceptionPending: public CppUnit::TestFixture
911 public:
912 void isExPending_001()
914 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
915 TimeValue *pTimeout;
916 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
917 pTimeout->Seconds = 3;
918 pTimeout->Nanosec = 0;
919 sal_Bool bOk = asSocket.isExceptionPending( pTimeout );
920 free( pTimeout );
922 CPPUNIT_ASSERT_MESSAGE( "test for isExceptionPending.",
923 bOk == sal_False );
926 /**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/
929 CPPUNIT_TEST_SUITE( isExceptionPending );
930 CPPUNIT_TEST( isExPending_001 );
931 CPPUNIT_TEST_SUITE_END();
932 }; // class isExceptionPending
934 // -----------------------------------------------------------------------------
935 /** Server Socket Thread, write a file which is large
937 // LLA: class WriteSocketThread : public Thread
938 // LLA: {
939 // LLA: ValueCheckProvider m_aValues;
940 // LLA:
941 // LLA: protected:
942 // LLA: oslThreadIdentifier m_id;
943 // LLA:
944 // LLA: void SAL_CALL run( )
945 // LLA: {
946 // LLA: ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
947 // LLA: ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("10.16.66.252"), 8888 );
948 // LLA: ::osl::StreamSocket ssStreamConnection;
949 // LLA:
950 // LLA: //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
951 // LLA: asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
952 // LLA:
953 // LLA: /// if the thread should terminate, schedule return false
954 // LLA: while ( schedule( ) == sal_True )
955 // LLA: {
956 // LLA: sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
957 // LLA: if ( sal_True != bOK1 )
958 // LLA: {
959 // LLA: t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
960 // LLA: break;
961 // LLA: }
962 // LLA: sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
963 // LLA: if ( sal_True != bOK2 )
964 // LLA: {
965 // LLA: t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
966 // LLA: break;
967 // LLA: }
968 // LLA: // blocking mode, if read/recv failed, block until success
969 // LLA: asAcceptorSocket.enableNonBlockingMode( sal_False);
970 // LLA:
971 // LLA: oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
972 // LLA: if (eResult != osl_Socket_Ok )
973 // LLA: {
974 // LLA: t_print("WriteSocketThread: acceptConnection failed! \n");
975 // LLA: break;
976 // LLA: }
977 // LLA:
978 // LLA:
979 // LLA: sal_Int32 nReadNumber1 = ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
980 // LLA: break;
981 // LLA: }
982 // LLA: ssStreamConnection.close();
983 // LLA: asAcceptorSocket.close();
984 // LLA: }
985 // LLA: }
986 // -----------------------------------------------------------------------------
987 // -----------------------------------------------------------------------------
988 /** Client Socket Thread, served as a temp little client to communicate with server.
991 #define IP_PORT_TEST 8900
993 class ReadSocket2Thread : public Thread
995 osl::Condition &m_aCondition;
996 char* m_pBuffer;
997 sal_Int32 m_nBufferSize;
998 sal_Int32 m_nReadCount;
999 rtl::OString m_sAddr;
1001 bool m_bOk;
1003 void setFailed()
1005 m_bOk = false;
1008 protected:
1009 oslThreadIdentifier m_id;
1011 void read()
1013 if (m_sAddr.getLength() == 0)
1015 setFailed();
1016 return;
1019 // 10.16.66.252
1020 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(m_sAddr.getStr()), IP_PORT_TEST );
1021 ::osl::ConnectorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1023 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1025 m_aCondition.wait();
1026 t_print("wait done\n");
1028 TimeValue *pTimeout;
1029 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
1030 pTimeout->Seconds = 20;
1031 pTimeout->Nanosec = 0;
1034 // blocking mode, if read/recv failed, block until success
1035 t_print("enableNonBlockingMode(false)\n");
1036 aSocket.enableNonBlockingMode( sal_False );
1039 t_print("connect()\n");
1040 oslSocketResult eResult = aSocket.connect( aSocketAddr, pTimeout );
1041 if ( osl_Socket_Ok == eResult)
1043 if (m_pBuffer)
1045 t_print("read()\n");
1046 m_nReadCount = aSocket.read( m_pBuffer, m_nBufferSize );
1047 t_print("%d bytes recived.\n", m_nReadCount);
1050 else
1052 t_print("# ReadSocket2Thread: connect failed! \n");
1053 printSocketResult(eResult);
1054 setFailed();
1057 //remove this line for deadlock on solaris( margritte.germany )
1058 aSocket.close();
1059 free( pTimeout );
1062 void SAL_CALL run( )
1064 read();
1067 void SAL_CALL onTerminated( )
1069 //t_print("# normally terminate this thread %d!\n", m_id );
1072 public:
1073 sal_Int32 getCount() {return m_nReadCount;}
1074 bool isOk() {return m_nReadCount == 0 ? false : true;}
1075 bool getFailed() {return m_bOk == false ? true : false;}
1077 ReadSocket2Thread(osl::Condition &_aCondition)
1078 :m_aCondition(_aCondition),
1079 m_nReadCount(0),
1080 m_bOk( true )
1082 m_aCondition.reset();
1083 m_pBuffer = (char*) malloc(1024);
1084 if (m_pBuffer)
1086 m_nBufferSize = 1024;
1089 m_id = getIdentifier( );
1090 //t_print("# successfully creat this client thread %d!\n", m_id );
1093 void setAddr(rtl::OString const& _sAddr)
1095 m_sAddr = _sAddr;
1098 ~ReadSocket2Thread( )
1100 if ( isRunning( ) )
1101 t_print("# error: client thread not terminated.\n" );
1102 free(m_pBuffer);
1107 // -----------------------------------------------------------------------------
1109 class justtest : public CppUnit::TestFixture
1111 void send_Acceptor(rtl::OString const& _sAddr, osl::Condition &)
1113 ::osl::AcceptorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1114 ::osl::SocketAddr aSocketAddr;
1116 if (! aSocketAddr.setPort(IP_PORT_TEST))
1118 t_print("# cant set port\n" );
1121 if (! aSocketAddr.setHostname(rtl::OUString::createFromAscii(_sAddr.getStr())))
1123 t_print("# cant set hostname/ip\n" );
1126 rtl::OUString aHostname = aSocketAddr.getHostname();
1127 aSocketAddr.getPort();
1130 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1131 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1133 /// if the thread should terminate, schedule return false
1134 // while ( schedule( ) == sal_True )
1135 // {
1136 if (! aSocket.bind( aSocketAddr ))
1138 t_print("# can't bind.\n" );
1140 if (! aSocket.listen( ))
1142 t_print("# can't listen. \n" );
1145 // blocking mode, if read/recv failed, block until success
1146 aSocket.enableNonBlockingMode( sal_False);
1147 ::osl::StreamSocket ssStreamConnection;
1149 oslSocketResult eResult = aSocket.acceptConnection( ssStreamConnection );
1150 if (eResult != osl_Socket_Ok )
1152 t_print("WriteSocketThread: acceptConnection failed! \n");
1153 // break;
1155 char const * pBuffer = "Test String\n";
1156 sal_Int32 nBufferSize = strlen(pBuffer);
1157 ssStreamConnection.write( pBuffer, nBufferSize );
1158 // break;
1159 // }
1161 // ssStreamConnection.close();
1162 aSocket.close();
1165 // -----------------------------------------------------------------------------
1167 void send_Connector(rtl::OString const& _sAddr, osl::Condition &/*_aCondition*/ )
1169 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1170 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST );
1172 if (! aSocketAddr.is())
1174 t_print("is failed.\n");
1175 return;
1178 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1179 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1181 oslSocketResult aResult = aSocket.connect( aSocketAddr );
1182 if ( aResult != osl_Socket_Ok )
1184 t_print("# send_Connector: connect failed. \n" );
1186 else
1188 // blocking mode, if read/recv failed, block until success
1189 // aSocket.enableNonBlockingMode( sal_False );
1191 // _aCondition.set();
1193 ::osl::StreamSocket ssStreamConnection(aSocket);
1195 char const * pBuffer = "GET / HTTP/1.0\015\012\015\012";
1196 sal_Int32 nBufferSize = strlen(pBuffer);
1197 ssStreamConnection.write( pBuffer, nBufferSize );
1199 char *pBufferPeek = (char*) malloc(1024);
1200 sal_Int32 nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek);
1201 free(pBufferPeek);
1203 char *pBuffer2 = (char*) malloc(nReadNumber + 1);
1204 sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber );
1205 pBuffer2[nReadNumberReal] = '\0';
1207 t_print("received: %s\n", pBuffer2);
1209 // char * pBuffer3 = "quit\n";
1210 // nBufferSize = strlen(pBuffer3);
1211 // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
1213 rtl::OUString suError = ssStreamConnection.getErrorAsString();
1214 free(pBuffer2);
1215 // ssStreamConnection.close();
1217 // ssStreamConnection.close();
1219 aSocket.shutdown(osl_Socket_DirReadWrite);
1220 aSocket.close();
1224 public:
1225 // LLA: orig void send_recv()
1226 // LLA: orig {
1227 // LLA: orig if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True )
1228 // LLA: orig t_print("margritte is alive ! \n");
1229 // LLA: orig if ( ifAvailable(rtl::OUString::createFromAscii("10.16.66.252")) == sal_False )
1230 // LLA: orig {
1231 // LLA: orig t_print("ip 10.16.66.252 is not alive! \n");
1232 // LLA: orig return;
1233 // LLA: orig }
1234 // LLA: orig ReadSocket2Thread myReadThread;
1235 // LLA: orig myReadThread.create();
1236 // LLA: orig
1237 // LLA: orig thread_sleep( 2 );
1238 // LLA: orig // send_Acceptor();
1239 // LLA: orig send_Connector();
1240 // LLA: orig
1241 // LLA: orig myReadThread.join();
1242 // LLA: orig
1243 // LLA: orig // statistics
1244 // LLA: orig sal_uInt32 nLength = myReadThread.getCount();
1245 // LLA: orig bool bIsOk = myReadThread.isOk(); // check if the values are right.
1246 // LLA: orig
1247 // LLA: orig t_print("Length:=%d\n", nLength);
1248 // LLA: orig t_print(" bIsOk:=%d\n", bIsOk);
1249 // LLA: orig }
1251 // -----------------------------------------------------------------------------
1253 // LLA: send_Connector_2_margritte works, it send strings to echo server on margritte
1254 // but can not receive anything
1256 void send_Connector_2_margritte(rtl::OString const& _sAddr)
1258 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1259 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST );
1261 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1262 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1264 oslSocketResult aResult = aSocket.connect( aSocketAddr );
1265 if ( aResult != osl_Socket_Ok )
1267 t_print("# connect failed. \n" );
1269 else
1271 // blocking mode, if read/recv failed, block until success
1272 aSocket.enableNonBlockingMode( sal_False );
1274 ::osl::StreamSocket ssStreamConnection(aSocket);
1276 char const * pBuffer = "Test String\n";
1277 sal_Int32 nBufferSize = strlen(pBuffer);
1278 sal_Int32 nWriteNumber = ssStreamConnection.write( pBuffer, nBufferSize );
1280 // char * pBuffer2 = " ";
1281 // sal_Int32 nReadNumber = ssStreamConnection.read( pBuffer2, strlen(pBuffer2) );
1283 char const * pBuffer3 = "quit\n";
1284 nBufferSize = strlen(pBuffer3);
1285 nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
1287 ssStreamConnection.close();
1289 aSocket.close();
1292 void send_recv_2_margritte()
1294 rtl::OString sAddr;
1295 sAddr = "margritte.germany.sun.com";
1296 if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True )
1298 t_print("found %s!\n", sAddr.getStr());
1300 send_Connector_2_margritte(sAddr);
1303 // -----------------------------------------------------------------------------
1305 void send_recv()
1307 rtl::OString sAddr;
1308 // if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True )
1309 // {
1310 // t_print("margritte is alive ! \n");
1311 // sAddr = "margritte.germany";
1312 // }
1314 sAddr = "margritte.germany.sun.com";
1315 if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True )
1317 t_print("found %s!\n", sAddr.getStr());
1319 // else
1320 // {
1321 // if ( ifAvailable(rtl::OUString::createFromAscii("192.168.7.2")) == sal_True )
1322 // {
1323 // sAddr = "192.168.7.2";
1324 // t_print("moon found ! \n");
1325 // }
1326 // else
1327 // {
1328 // if ( ifAvailable(rtl::OUString::createFromAscii("moon.linux.bogus")) == sal_True )
1329 // {
1330 // sAddr = "moon.linux.bogus";
1331 // t_print("moon found ! \n");
1332 // }
1333 // else
1334 // {
1335 // if ( ifAvailable(rtl::OUString::createFromAscii("moon")) == sal_True )
1336 // {
1337 // sAddr = "moon";
1338 // t_print("moon found ! \n");
1339 // }
1340 // }
1341 // }
1342 // }
1344 // if ( ifAvailable(rtl::OUString::createFromAscii("10.16.64.196")) == sal_False )
1345 // {
1346 // t_print("ip 10.16.64.196 is not alive! \n");
1347 // return;
1348 // }
1350 osl::Condition aCondition;
1351 ReadSocket2Thread myReadThread(aCondition);
1352 myReadThread.setAddr(sAddr);
1353 // myReadThread.create();
1355 thread_sleep( 2 );
1356 if (! myReadThread.getFailed())
1358 // send_Acceptor(sAddr, aCondition);
1359 send_Connector(sAddr, aCondition);
1361 thread_sleep( 2 );
1362 if (myReadThread.isRunning())
1364 myReadThread.join();
1366 // termAndJoinThread(&myReadThread);
1368 // statistics
1369 sal_uInt32 nLength = myReadThread.getCount();
1370 bool bIsOk = myReadThread.isOk(); // check if the values are right.
1372 t_print("Length:=%d\n", nLength);
1373 t_print(" bIsOk:=%d\n", bIsOk);
1375 else
1377 t_print("ERROR: No echo Server on %s found.\n", sAddr.getStr());
1382 void getPage(rtl::OString const& _sAddr);
1383 void test_getPage()
1385 // rtl::OString sPage("lla-1.germany.sun.com");
1386 // getPage(sPage);
1388 rtl::OString sPage("lla-1");
1389 getPage(sPage);
1392 CPPUNIT_TEST_SUITE( justtest );
1393 CPPUNIT_TEST( send_recv );
1394 CPPUNIT_TEST( test_getPage );
1395 CPPUNIT_TEST_SUITE_END();
1396 }; // class isExceptionPending
1399 void justtest::getPage(rtl::OString const& _sAddr)
1401 rtl::OUString suAddr = rtl::OUString::createFromAscii(_sAddr.getStr());
1402 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1403 ::osl::SocketAddr aSocketAddr( suAddr, 80 );
1406 // some checks
1407 aSocketAddr.getPort();
1408 oslSocketResult aResult;
1409 rtl::ByteSequence aSeq = aSocketAddr.getAddr(&aResult);
1410 if (aResult != osl_Socket_Ok)
1412 t_print("problem with getAddr: ");
1413 printSocketResult(aResult);
1416 rtl::OUString sStr = aSocketAddr.getHostname(&aResult);
1417 if (aResult != osl_Socket_Ok)
1419 t_print("problem with hostname: ");
1420 printSocketResult(aResult);
1424 oslSocketResult aResult;
1426 // SocketAddr::resolveHostname(suAddr, aSocketAddr);
1427 // if (! aSocketAddr.is())
1428 // {
1429 // t_print("Can't resolve Hostname.\n");
1430 // return;
1431 // }
1432 // rtl::OUString sStr = aSocketAddr.getHostname(&aResult);
1433 // if (aResult != osl_Socket_Ok)
1434 // {
1435 // t_print("problem with hostname: ");
1436 // printSocketResult(aResult);
1438 // }
1440 if (! aSocketAddr.is())
1442 t_print("SocketAddr::is() failed.\n");
1443 return;
1446 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1447 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1449 aResult = aSocket.connect( aSocketAddr );
1450 if ( aResult != osl_Socket_Ok )
1452 t_print("# send_Connector: connect failed. \n" );
1454 else
1456 // blocking mode, if read/recv failed, block until success
1457 // aSocket.enableNonBlockingMode( sal_False );
1459 // _aCondition.set();
1461 ::osl::StreamSocket ssStreamConnection(aSocket);
1463 char const * pBuffer = "GET / HTTP/1.0\015\012\015\012";
1464 sal_Int32 nBufferSize = strlen(pBuffer);
1465 ssStreamConnection.write( pBuffer, nBufferSize );
1468 char *pBufferPeek = (char*) malloc(1024);
1469 sal_Int32 nReadNumber = 1;
1470 while ( nReadNumber != 0)
1472 nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek);
1473 if (nReadNumber > 0)
1475 char *pBuffer2 = (char*) malloc(nReadNumber + 1);
1476 sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber );
1477 pBuffer2[nReadNumberReal] = '\0';
1478 t_print("%s", pBuffer2);
1479 free(pBuffer2);
1482 free(pBufferPeek);
1484 // char * pBuffer3 = "quit\n";
1485 // nBufferSize = strlen(pBuffer3);
1486 // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
1488 rtl::OUString suError = ssStreamConnection.getErrorAsString();
1490 aSocket.shutdown(osl_Socket_DirReadWrite);
1491 aSocket.close();
1494 // -----------------------------------------------------------------------------
1496 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::ctors, "osl_StreamSocket");
1497 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::send_recv, "osl_StreamSocket");
1498 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::shutdown, "osl_StreamSocket");
1499 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::isExceptionPending, "osl_StreamSocket");
1501 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::justtest, "osl_StreamSocket");
1503 } // namespace osl_StreamSocket
1505 // -----------------------------------------------------------------------------
1507 // this macro creates an empty function, which will called by the RegisterAllFunctions()
1508 // to let the user the possibility to also register some functions by hand.
1509 NOADDITIONAL;