update dev300-m57
[ooovba.git] / sal / qa / osl / socket / osl_Socket.cxx
blob27d881c48a2aa64ce80303a8e9afb383dadf9c78
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_Socket.cxx,v $
10 * $Revision: 1.12 $
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;
61 //------------------------------------------------------------------------
62 // include files
63 //------------------------------------------------------------------------
65 #ifndef _OSL_SOCKET_CONST_H_
66 #include <osl_Socket_Const_orig.h>
67 #endif
69 #include <cppunit/simpleheader.hxx>
71 using namespace osl;
72 using namespace rtl;
74 //------------------------------------------------------------------------
75 // helper functions
76 //------------------------------------------------------------------------
78 /** compare two OUString.
80 inline sal_Bool compareUString( const ::rtl::OUString & ustr1, const ::rtl::OUString & ustr2 )
82 sal_Bool bOk = ustr1.equalsIgnoreAsciiCase( ustr2 );
84 return bOk;
87 /** compare a OUString and an ASCII string.
89 inline sal_Bool compareUString( const ::rtl::OUString & ustr, const sal_Char *astr )
91 ::rtl::OUString ustr2 = rtl::OUString::createFromAscii( astr );
92 sal_Bool bOk = ustr.equalsIgnoreAsciiCase( ustr2 );
94 return bOk;
97 /** compare two socket address.
99 inline sal_Bool compareSocketAddr( const ::osl::SocketAddr & addr1 , const ::osl::SocketAddr & addr2 )
101 return ( ( sal_True == compareUString( addr1.getHostname( 0 ), addr2.getHostname( 0 ) ) ) && ( addr2.getPort( ) == addr2.getPort( ) ) );
104 inline char * oustring2char( const ::rtl::OUString & str )
106 rtl::OString aString;
107 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
108 return (char *)aString.getStr( );
111 /** print a UNI_CODE String. And also print some comments of the string.
113 inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
115 t_print("#%s #printUString_u# ", msg );
116 t_print("%s\n", oustring2char( str ) );
119 /** get the local host name.
120 mindy: gethostbyname( "localhost" ), on Linux, it returns the hostname in /etc/hosts + domain name,
121 if no entry in /etc/hosts, it returns "localhost" + domain name
123 inline ::rtl::OUString getHost( void )
125 struct hostent *hptr;
127 hptr = gethostbyname( "localhost" );
128 CPPUNIT_ASSERT_MESSAGE( "#In getHostname function, error on gethostbyname()", hptr != NULL );
129 ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name );
131 return aUString;
134 /** get the full host name of the current processor, such as "aegean.prc.sun.com" --mindyliu
136 inline ::rtl::OUString getThisHostname( void )
138 ::rtl::OUString aUString;
139 #ifdef WNT
140 struct hostent *hptr;
141 hptr = gethostbyname( "localhost" );
142 CPPUNIT_ASSERT_MESSAGE( "#In getHostname function, error on gethostbyname()", hptr != NULL );
143 aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name );
144 #else
145 char hostname[255];
146 CPPUNIT_ASSERT_MESSAGE( "#Error: gethostname failed.", gethostname(hostname, 255) == 0 );
148 struct hostent *hptr;
149 //first search /ets/hosts, then search from dns
150 hptr = gethostbyname( hostname);
151 if ( hptr != NULL )
153 strcpy( hostname, hptr->h_name );
156 t_print("hostname is %s \n", hostname );
157 aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hostname );
158 #endif
159 return aUString;
162 /** get IP by name, search /etc/hosts first, then search from dns, fail return OUString("")
164 inline ::rtl::OUString getIPbyName( rtl::OString const& str_name )
166 ::rtl::OUString aUString;
167 struct hostent *hptr;
168 //first search /ets/hosts, then search from dns
169 hptr = gethostbyname( str_name.getStr());
170 if ( hptr != NULL )
172 struct in_addr ** addrptr;
173 addrptr = (struct in_addr **) hptr->h_addr_list ;
174 //if there are more than one IPs on the same machine, we select one
175 for (; *addrptr; addrptr++)
177 t_print("#Local IP Address: %s\n", inet_ntoa(**addrptr));
178 aUString = ::rtl::OUString::createFromAscii( (sal_Char *) (inet_ntoa(**addrptr)) );
181 return aUString;
184 /** get local ethernet IP
186 inline ::rtl::OUString getLocalIP( )
188 char hostname[255];
189 gethostname(hostname, 255);
191 return getIPbyName( hostname );
194 /** construct error message
196 inline ::rtl::OUString outputError( const ::rtl::OUString & returnVal, const ::rtl::OUString & rightVal, const sal_Char * msg = "")
198 ::rtl::OUString aUString;
199 if ( returnVal.equals( rightVal ) )
200 return aUString;
201 aUString += ::rtl::OUString::createFromAscii(msg);
202 aUString += ::rtl::OUString::createFromAscii(": the returned value is '");
203 aUString += returnVal;
204 aUString += ::rtl::OUString::createFromAscii("', but the value should be '");
205 aUString += rightVal;
206 aUString += ::rtl::OUString::createFromAscii("'.");
207 return aUString;
210 /** wait _nSec seconds.
212 void thread_sleep( sal_Int32 _nSec )
214 /// print statement in thread process must use fflush() to force display.
215 t_print("# wait %d seconds. ", _nSec );
216 fflush(stdout);
218 #ifdef WNT //Windows
219 Sleep( _nSec * 100 );
220 #endif
221 #if ( defined UNX ) || ( defined OS2 ) //Unix
222 usleep(_nSec * 100000);
223 #endif
224 t_print("# done\n" );
227 /** print Boolean value.
229 inline void printBool( sal_Bool bOk )
231 t_print("#printBool# " );
232 ( sal_True == bOk ) ? t_print("YES!\n" ): t_print("NO!\n" );
235 /** print content of a ByteSequence.
237 inline void printByteSequence_IP( const ::rtl::ByteSequence & bsByteSeq, sal_Int32 nLen )
239 t_print("#ByteSequence is: " );
240 for ( int i = 0; i < nLen; i++ ){
241 if ( bsByteSeq[i] < 0 )
242 t_print("%d ", 256 + bsByteSeq[i] );
243 else
244 t_print("%d ", bsByteSeq[i] );
246 t_print(" .\n" );
249 /** convert an IP which is stored as a UString format to a ByteSequence array for later use.
251 inline ::rtl::ByteSequence UStringIPToByteSequence( ::rtl::OUString aUStr )
254 rtl::OString aString = ::rtl::OUStringToOString( aUStr, RTL_TEXTENCODING_ASCII_US );
255 const sal_Char *pChar = aString.getStr( ) ;
256 sal_Char tmpBuffer[4];
257 sal_Int32 nCharCounter = 0;
258 ::rtl::ByteSequence bsByteSequence( IP_VER );
259 sal_Int32 nByteSeqCounter = 0;
261 for ( int i = 0; i < aString.getLength( ) + 1 ; i++ )
263 if ( ( *pChar != '.' ) && ( i !=aString.getLength( ) ) )
264 tmpBuffer[nCharCounter++] = *pChar;
265 else
267 tmpBuffer[nCharCounter] = '\0';
268 nCharCounter = 0;
269 bsByteSequence[nByteSeqCounter++] = static_cast<sal_Int8>( atoi( tmpBuffer ) );
271 pChar++;
273 return bsByteSequence;
276 /** print a socket result name.
278 inline void printSocketResult( oslSocketResult eResult )
280 t_print("#printSocketResult# " );
281 if (!eResult)
282 switch (eResult)
284 case osl_Socket_Ok:
285 t_print("client connected\n");
286 break;
287 case osl_Socket_Error:
288 t_print("got an error ... exiting\r\n\r\n" );
289 break;
290 case osl_Socket_TimedOut:
291 t_print("timeout\n");
292 break;
294 case osl_Socket_FORCE_EQUAL_SIZE:
295 t_print("FORCE EQUAL SIZE\n");
296 break;
297 case osl_Socket_InProgress:
298 t_print("In Progress\n");
299 break;
300 case osl_Socket_Interrupted:
301 t_print("Interrupted\n");
302 break;
306 /** Client Socket Thread, served as a temp little client to communicate with server.
308 class ClientSocketThread : public Thread
310 protected:
311 oslThreadIdentifier m_id;
312 ::osl::SocketAddr saTargetSocketAddr;
313 ::osl::ConnectorSocket csConnectorSocket;
315 void SAL_CALL run( )
317 TimeValue *pTimeout;
318 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
319 pTimeout->Seconds = 5;
320 pTimeout->Nanosec = 0;
322 /// if the thread should terminate, schedule return false
323 //while ( schedule( ) == sal_True )
325 if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout ))
327 csConnectorSocket.send( pTestString1, 11 ); // "test socket"
328 csConnectorSocket.send( pTestString2, 10);
330 else
331 t_print("# ClientSocketThread: connect failed! \n");
332 // terminate();
334 csConnectorSocket.close();
335 free( pTimeout );
338 void SAL_CALL onTerminated( )
340 //t_print("# normally terminate this thread %d!\n", m_id );
343 public:
344 ClientSocketThread( ):
345 saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT9 ),
346 csConnectorSocket( )
348 m_id = getIdentifier( );
349 //t_print("# successfully creat this client thread %d!\n", m_id );
352 ~ClientSocketThread( )
354 if ( isRunning( ) )
355 t_print("# error: client thread not terminated.\n" );
361 /** Server Socket Thread, served as a temp little server to communicate with client.
363 class ServerSocketThread : public Thread
365 protected:
366 oslThreadIdentifier m_id;
368 void SAL_CALL run( )
370 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
371 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 );
372 ::osl::StreamSocket ssStreamConnection;
374 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
375 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
376 while ( schedule( ) == sal_True )
378 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
379 if ( sal_True != bOK1 )
381 t_print("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ;
382 break;
384 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
385 if ( sal_True != bOK2 )
387 t_print("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ;
388 break;
391 asAcceptorSocket.enableNonBlockingMode( sal_False );
393 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
394 if (eResult != osl_Socket_Ok )
396 t_print("ServerSocketThread: acceptConnection failed! \n");
397 break;
399 sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 );
400 sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 );
401 pReadBuffer[nReadNumber1 + nReadNumber2] = '\0';
402 //t_print("# read buffer content: %s\n", pReadBuffer );
403 break;
405 ssStreamConnection.close();
406 asAcceptorSocket.close();
410 void SAL_CALL onTerminated( )
412 //t_print("# normally terminate this server thread %d!\n", m_id );
415 public:
416 // public to check if data transmition is OK
417 sal_Char pReadBuffer[30];
418 ServerSocketThread( )
420 m_id = getIdentifier( );
421 //t_print("# successfully creat this server thread %d!\n", m_id );
424 ~ServerSocketThread( )
426 if ( isRunning( ) )
427 t_print("# error: server thread not terminated.\n" );
431 // -----------------------------------------------------------------------------
432 // Helper functions, to create buffers, check buffers
433 class ValueCheckProvider
435 bool m_bFoundFailure;
436 char *m_pBuffer;
437 sal_Int32 m_nBufferSize;
439 public:
440 ValueCheckProvider()
442 m_bFoundFailure(false),
443 m_pBuffer(NULL),
444 m_nBufferSize(0)
448 bool isFailure() {return m_bFoundFailure;}
450 const char* getBuffer() {return m_pBuffer;}
451 char* getWriteBuffer() {return m_pBuffer;}
453 sal_Int32 getBufferSize() {return m_nBufferSize;}
455 bool checkValues(sal_Int32 _nLength, int _nValue)
457 m_bFoundFailure = false;
458 for(sal_Int32 i=0;i<_nLength;i++)
460 if (m_pBuffer[i] != _nValue)
462 m_bFoundFailure = true;
465 return m_bFoundFailure;
468 void createBuffer(sal_Int32 _nLength, int _nValue)
470 m_nBufferSize = _nLength;
471 m_pBuffer = (char*) malloc(m_nBufferSize);
472 if (m_pBuffer)
474 memset(m_pBuffer, _nValue, m_nBufferSize);
478 void freeBuffer()
480 if (m_pBuffer) free(m_pBuffer);
485 // -----------------------------------------------------------------------------
486 /** Client Socket Thread, served as a temp little client to communicate with server.
489 class ReadSocketThread : public Thread
491 int m_nValue;
492 ValueCheckProvider m_aValues;
494 protected:
495 oslThreadIdentifier m_id;
496 ::osl::SocketAddr saTargetSocketAddr;
497 ::osl::ConnectorSocket csConnectorSocket;
499 void SAL_CALL run( )
501 TimeValue *pTimeout;
502 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
503 pTimeout->Seconds = 5;
504 pTimeout->Nanosec = 0;
506 /// if the thread should terminate, schedule return false
507 //while ( schedule( ) == sal_True )
509 if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout ))
511 sal_Int32 nReadCount = csConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() );
512 m_aValues.checkValues(nReadCount, m_nValue);
514 else
516 t_print("# ReadSocketThread: connect failed! \n");
518 // terminate();
520 //remove this line for deadlock on solaris( margritte.germany )
521 csConnectorSocket.close();
522 free( pTimeout );
525 void SAL_CALL onTerminated( )
527 //t_print("# normally terminate this thread %d!\n", m_id );
530 public:
531 sal_Int32 getCount() {return m_aValues.getBufferSize();}
532 bool isOk() {return m_aValues.isFailure() == true ? false : true;}
534 ReadSocketThread(sal_Int32 _nBufferSize, int _nValue )
536 m_nValue( _nValue ),
537 saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT10 ),
538 csConnectorSocket( )
540 m_id = getIdentifier( );
541 //t_print("# successfully creat this client thread %d!\n", m_id );
542 m_aValues.createBuffer(_nBufferSize, 0);
545 ~ReadSocketThread( )
547 if ( isRunning( ) )
548 t_print("# error: client thread not terminated.\n" );
549 m_aValues.freeBuffer();
554 /** Server Socket Thread, write a file which is large
556 class WriteSocketThread : public Thread
558 ValueCheckProvider m_aValues;
560 protected:
561 oslThreadIdentifier m_id;
563 void SAL_CALL run( )
565 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
566 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT10 );
567 ::osl::StreamSocket ssStreamConnection;
569 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
570 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
572 /// if the thread should terminate, schedule return false
573 while ( schedule( ) == sal_True )
575 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
576 if ( sal_True != bOK1 )
578 t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
579 break;
581 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
582 if ( sal_True != bOK2 )
584 t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
585 break;
587 // blocking mode, if read/recv failed, block until success
588 asAcceptorSocket.enableNonBlockingMode( sal_False);
590 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
591 if (eResult != osl_Socket_Ok )
593 t_print("WriteSocketThread: acceptConnection failed! \n");
594 break;
597 ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
598 break;
600 ssStreamConnection.close();
601 asAcceptorSocket.close();
604 void SAL_CALL onTerminated( )
606 //t_print("# normally terminate this server thread %d!\n", m_id );
609 public:
610 // public to check if data transmition is OK
611 WriteSocketThread(sal_Int32 _nBufferSize, int _nValue )
613 m_id = getIdentifier( );
614 //t_print("# successfully creat this server thread %d!\n", m_id );
616 m_aValues.createBuffer(_nBufferSize, _nValue);
619 ~WriteSocketThread( )
621 if ( isRunning( ) )
622 t_print("# error: server thread not terminated.\n" );
623 m_aValues.freeBuffer();
628 // -----------------------------------------------------------------------------
629 // just used to test socket::close() when accepting
630 class AcceptorThread : public Thread
632 ::osl::AcceptorSocket asAcceptorSocket;
633 ::rtl::OUString aHostIP;
634 sal_Bool bOK;
635 protected:
636 void SAL_CALL run( )
638 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
639 ::osl::StreamSocket ssStreamConnection;
641 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
642 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
643 if ( sal_True != bOK1 )
645 t_print("# AcceptorSocket bind address failed.\n" ) ;
646 return;
648 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
649 if ( sal_True != bOK2 )
651 t_print("# AcceptorSocket listen address failed.\n" ) ;
652 return;
655 asAcceptorSocket.enableNonBlockingMode( sal_False );
657 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
658 if (eResult != osl_Socket_Ok )
660 bOK = sal_True;
661 t_print("AcceptorThread: acceptConnection failed! \n");
664 public:
665 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString & aBindIP )
666 : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
668 bOK = sal_False;
671 sal_Bool isOK() { return bOK; }
673 ~AcceptorThread( )
675 if ( isRunning( ) )
677 asAcceptorSocket.shutdown();
678 t_print("# error: Acceptor thread not terminated.\n" );
683 class CloseSocketThread : public Thread
685 ::osl::Socket m_sSocket;
686 protected:
687 void SAL_CALL run( )
689 thread_sleep( 1 );
690 m_sSocket.close( );
692 public:
693 CloseSocketThread(::osl::Socket & sSocket )
694 : m_sSocket( sSocket )
698 ~CloseSocketThread( )
700 if ( isRunning( ) )
702 t_print("# error: CloseSocketThread not terminated.\n" );
707 //------------------------------------------------------------------------
708 // tests cases begins here
709 //------------------------------------------------------------------------
711 namespace osl_SocketAddr
714 /** testing the methods:
715 inline SocketAddr();
716 inline SocketAddr(const SocketAddr& Addr);
717 inline SocketAddr(const oslSocketAddr , __osl_socket_NoCopy nocopy );
718 inline SocketAddr(oslSocketAddr Addr);
719 inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort );
722 class ctors : public CppUnit::TestFixture
724 public:
726 void ctors_none()
728 /// SocketAddr constructor.
729 ::osl::SocketAddr saSocketAddr;
731 // oslSocketResult aResult;
732 // rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult);
734 // rtl::OUString suHost2 = getThisHostname();
736 CPPUNIT_ASSERT_MESSAGE("test for none parameter constructor function: check if the socket address was created successfully",
737 sal_True == saSocketAddr.is( ) );
740 void ctors_none_000()
742 /// SocketAddr constructor.
743 ::osl::SocketAddr saSocketAddr;
745 oslSocketResult aResult;
746 rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult);
747 rtl::OUString suHost2 = getThisHostname();
749 sal_Bool bOk = compareUString(suHost, suHost2);
751 rtl::OUString suError = rtl::OUString::createFromAscii("Host names should be the same. From SocketAddr.getLocalHostname() it is'");
752 suError += suHost;
753 suError += rtl::OUString::createFromAscii("', from getThisHostname() it is '");
754 suError += suHost2;
755 suError += rtl::OUString::createFromAscii("'.");
757 CPPUNIT_ASSERT_MESSAGE(suError, sal_True == bOk);
760 void ctors_copy()
762 /// SocketAddr copy constructor.
763 ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 );
764 ::osl::SocketAddr saCopySocketAddr( saSocketAddr );
766 sal_Int32 nPort = saCopySocketAddr.getPort( );
768 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy constructor function: copy constructor, do an action of copy construction then check the port with original set.",
769 ( sal_True == saCopySocketAddr.is( ) ) && ( nPort == IP_PORT_HTTP1 ) );
772 void ctors_copy_no_001()
774 #if 0
775 ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 );
776 oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( );
778 ::osl::SocketAddr saSocketAddrCopy( psaOSLSocketAddr, SAL_NO_COPY );
779 saSocketAddrCopy.setPort( IP_PORT_HTTP2 );
781 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.",
782 saSocketAddr.getPort( ) == IP_PORT_HTTP2 );
783 #endif
784 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 );
785 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
787 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
789 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
791 pSocketAddrCopy->setPort( IP_PORT_HTTP2 );
792 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.",
793 pSocketAddr->getPort( ) == IP_PORT_HTTP2 );
795 delete pSocketAddrCopy;
796 // LLA: don't do this also: delete pSocketAddr;
799 void ctors_copy_no_002()
801 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 );
802 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
803 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
804 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
806 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.",
807 pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) );
809 delete pSocketAddrCopy;
812 void ctors_copy_handle_001()
814 ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 );
815 ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) );
817 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, get its port to check copy effect.",
818 saSocketAddrCopy.getPort( ) == IP_PORT_HTTP1 );
821 void ctors_copy_handle_002()
823 ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 );
824 ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) );
825 saSocketAddrCopy.setPort( IP_PORT_HTTP2 );
827 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, the original one should not be changed.",
828 saSocketAddr.getPort( ) != IP_PORT_HTTP2 );
831 void ctors_hostname_port_001()
833 /// tcpip-specif constructor.
834 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
835 printUString(saSocketAddr.getHostname( ), "ctors_hostname_port_001:getHostname");
837 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: do a constructor using tcpip spec, check the result.",
838 saSocketAddr.is( ) == sal_True &&
839 ( saSocketAddr.getPort( ) == IP_PORT_FTP )/*&&
840 ( sal_True == compareUString( saSocketAddr.getHostname( ), aHostName1 ) ) */);
843 //same as is_002
844 void ctors_hostname_port_002()
846 /// tcpip-specif constructor.
847 ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT2 );
849 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: using an invalid IP address, the socketaddr ctors should fail", sal_False == saSocketAddr.is( ));
851 CPPUNIT_TEST_SUITE( ctors );
852 CPPUNIT_TEST( ctors_none );
853 CPPUNIT_TEST( ctors_none_000 );
854 CPPUNIT_TEST( ctors_copy );
855 CPPUNIT_TEST( ctors_copy_no_001 );
856 CPPUNIT_TEST( ctors_copy_no_002 );
857 CPPUNIT_TEST( ctors_copy_handle_001 );
858 CPPUNIT_TEST( ctors_copy_handle_002 );
859 CPPUNIT_TEST( ctors_hostname_port_001 );
860 CPPUNIT_TEST( ctors_hostname_port_002 );
861 CPPUNIT_TEST_SUITE_END();
863 }; // class ctors
866 /** testing the method:
867 inline sal_Bool is() const;
870 class is : public CppUnit::TestFixture
872 public:
873 void is_001()
875 ::osl::SocketAddr saSocketAddr;
877 CPPUNIT_ASSERT_MESSAGE("test for is() function: create an unknown type socket, it should be True when call is.",
878 sal_True == saSocketAddr.is( ) );
880 // refer to setPort_003()
881 void is_002()
883 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_INVAL );
885 CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid port number",
886 sal_True == saSocketAddr.is( ) );
889 void is_003()
891 ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT );
893 CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid Ip number",
894 sal_True != saSocketAddr.is( ) );
897 CPPUNIT_TEST_SUITE( is );
898 CPPUNIT_TEST( is_001 );
899 CPPUNIT_TEST( is_002 );
900 CPPUNIT_TEST( is_003 );
901 CPPUNIT_TEST_SUITE_END();
903 }; // class is
906 /** testing the method:
907 inline ::rtl::OUString SAL_CALL getHostname( oslSocketResult *pResult = 0 ) const;
910 class getHostname : public CppUnit::TestFixture
912 public:
913 void setUp()
917 void tearDown()
921 void getHostname_000()
923 ::osl::SocketAddr saSocketAddr( aHostIp4, IP_PORT_FTP );
927 /** it will search the Ip in current machine's /etc/hosts at first, if find, then return the
928 mapped hostname, otherwise, it will search via DNS server, and often return hostname+ Domain name
929 like "sceri.PRC.Sun.COM"
930 The process is same as Socket::getLocalHost(), but getLocalHost can only return hostname of the current machine.
932 void getHostname_001()
934 ::osl::SocketAddr saSocketAddr( aHostIp4, IP_PORT_FTP );
935 rtl::OUString suResult = saSocketAddr.getHostname( 0 );
936 rtl::OUString suError = outputError(suResult, aHostName4, "test for getHostname(0)");
937 sal_Bool bOK = compareUString( suResult, aHostName4 );
938 // search the returned hostname in /etc/hosts, if find, and the IP in the row is same as IP
939 // in the Addr, it's right also.
940 if ( bOK == sal_False)
942 if ( compareUString( getIPbyName( oustring2char( suResult ) ), aHostIp4 ) == sal_True )
943 bOK = sal_True;
945 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK);
948 // LLA: now we have to control, if this behaviour is right.
949 // LLA: this function does not work in company (Linux, Windows) but at home
950 void getHostname_002()
952 rtl::OUString suHostname = rtl::OUString::createFromAscii("cn-1.germany.sun.com");
953 rtl::OUString aHostIP = getIPbyName( oustring2char( suHostname ) );
955 ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_FTP );
956 sal_Bool bOK = saSocketAddr.setHostname( suHostname );
957 CPPUNIT_ASSERT_MESSAGE("#SocketAddr.setHostname failed", sal_True == bOK );
958 oslSocketResult aResult;
959 rtl::OUString suResult = saSocketAddr.getHostname( &aResult );
960 CPPUNIT_ASSERT_MESSAGE("SocketAddr.getHostname failed.", aResult == osl_Socket_Ok);
962 rtl::OUString suError = outputError(suResult, suHostname, "test for getHostname(0)");
963 bOK = compareUString( suResult, suHostname );
964 if ( bOK == sal_False)
966 rtl::OString aString = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US );
967 if ( compareUString( getIPbyName( aString) , aHostIp6 ) == sal_True )
969 bOK = sal_True;
973 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
977 CPPUNIT_TEST_SUITE( getHostname );
978 CPPUNIT_TEST( getHostname_001 );
979 CPPUNIT_TEST( getHostname_002 );
980 CPPUNIT_TEST_SUITE_END();
982 }; // class getHostname
985 /** testing the method:
986 inline sal_Int32 SAL_CALL getPort() const;
989 class getPort : public CppUnit::TestFixture
991 public:
992 void getPort_001()
994 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
996 CPPUNIT_ASSERT_MESSAGE( "test for getPort() function: get a normal port number.",
997 IP_PORT_FTP == saSocketAddr.getPort( ) );
1000 void getPort_002()
1002 ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_INVAL );
1004 //t_print("#getPort_002: Port number is %d \n", saSocketAddr.getPort( ));
1006 CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid port to a SocketAddr, get the port to see if it can detect. it did not pass in (W32).",
1007 saSocketAddr.getPort( )>=1 && saSocketAddr.getPort( ) <= 65535 );
1009 //two cases will return OSL_INVALID_PORT: 1. not valid SocketAddr
1010 //2. SocketAddr family is not osl_Socket_FamilyInet, but case 2 could not be constructed
1011 void getPort_003()
1013 ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT );
1015 CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid IP to a SocketAddr, get the port to see returned value. ",
1016 saSocketAddr.getPort( ) == OSL_INVALID_PORT );
1019 CPPUNIT_TEST_SUITE( getPort );
1020 CPPUNIT_TEST( getPort_001 );
1021 CPPUNIT_TEST( getPort_002 );
1022 CPPUNIT_TEST( getPort_003 );
1023 CPPUNIT_TEST_SUITE_END( );
1025 }; // class getPort
1028 /** testing the method:
1029 inline sal_Bool SAL_CALL setPort( sal_Int32 nPort );
1030 rfc1413.txt: TCP port numbers are from 1-65535
1031 rfc1700.txt: 0/tcp Reserved ; 0/udp Reserved
1034 class setPort : public CppUnit::TestFixture
1036 public:
1037 void setPort_001()
1039 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
1040 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_TELNET );
1042 CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: modify a port number setting, and check it.",
1043 ( sal_True == bOK ) &&
1044 ( IP_PORT_TELNET == saSocketAddr.getPort( ) ) );
1047 /** 0 to 1024 is known as the reserved port range (traditionally only root can assign programs to ports in
1048 this range) and the ephemeral port range from 1025 to 65535.
1049 As many of you programmers will know, when you specify the source port of 0 when you connect to a host,
1050 the OS automatically reassigns the port number to high numbered ephemeral port. The same happens if you
1051 try to bind a listening socket to port 0.
1052 http://www.securiteam.com/securityreviews/5XP0Q2AAKS.html
1053 another: http://www.muq.org/~cynbe/muq/mufref_564.html
1055 void setPort_002()
1057 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
1058 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_ZERO );
1060 oslSocket sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1061 ::osl::Socket sSocket(sHandle);
1062 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 );//sal_True);
1063 sal_Bool bOK1 = sSocket.bind( saSocketAddr );
1064 CPPUNIT_ASSERT_MESSAGE( "bind SocketAddr failed", bOK1 == sal_True );
1066 sal_Int32 newPort = sSocket.getLocalPort();
1067 //t_print("#new port is %d\n", newPort );
1069 CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: port number should be in 1 ~ 65535, set port 0, it should be converted to a port number between 1024~65535.",
1070 ( 1024 <= newPort ) && ( 65535 >= newPort ) && ( bOK == sal_True ) );
1074 void setPort_003()
1076 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP);
1077 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_INVAL );
1078 //on Linux, getPort return 34463
1079 //t_print("#Port number is %d \n", saSocketAddr.getPort( ));
1081 CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an address with invalid port. it should return error or convert it to a valid port.",
1082 ( ( 1 <= saSocketAddr.getPort( ) ) && ( 65535 >= saSocketAddr.getPort( ) ) &&( bOK == sal_True ) ) ||
1083 bOK == sal_False);
1086 /* this is not a inet-addr => can't set port */
1087 void setPort_004()
1089 ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_FTP);
1090 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_MYPORT );
1092 CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an invalid address with valid port. it should return error.",
1093 bOK == sal_False);
1097 CPPUNIT_TEST_SUITE( setPort );
1098 CPPUNIT_TEST( setPort_001 );
1099 CPPUNIT_TEST( setPort_002 );
1100 CPPUNIT_TEST( setPort_003 );
1101 CPPUNIT_TEST( setPort_004 );
1102 CPPUNIT_TEST_SUITE_END( );
1104 }; // class setPort
1107 /** tester comment:
1109 In the following two functions, it use ::rtl::ByteSequence as an intermediate storage for address,
1110 the ByteSequence object can hold sal_Int8 arrays, which is raged [-127, 127], in case of IP addr
1111 that is greater than 127, say 129.158.217.202, it will stored as -127, -98, -39, -54, it is unique
1112 in the range of sal_Int8, but lack of readability.
1113 so may be a sal_uInt8 array is better.
1117 /** testing the method:
1118 inline sal_Bool SAL_CALL setAddr( const ::rtl::ByteSequence & address );
1121 class setAddr : public CppUnit::TestFixture
1123 public:
1124 void setAddr_001()
1126 ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_FTP );
1127 saSocketAddr.setAddr( UStringIPToByteSequence( aHostIp1 ) );
1128 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 );
1129 sal_Bool bOK = sal_False;
1131 if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) && ( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1132 bOK = sal_True;
1134 CPPUNIT_ASSERT_MESSAGE( "test for setAddr() function: construct Addr with \"129.158.217.202\", set it to \"127.0.0.1\", and check the correctness ",
1135 sal_True == bOK );
1139 CPPUNIT_TEST_SUITE( setAddr );
1140 CPPUNIT_TEST( setAddr_001 );
1141 CPPUNIT_TEST_SUITE_END( );
1143 }; // class setAddr
1146 /** testing the method:
1147 inline ::rtl::ByteSequence SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const;
1150 class getAddr : public CppUnit::TestFixture
1152 public:
1153 void getAddr_001()
1155 oslSocketResult SocketResult;
1156 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
1157 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( &SocketResult );
1159 sal_Bool bOK = sal_False;
1161 if ( ( osl_Socket_Ok == SocketResult ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1162 bOK = sal_True;
1164 CPPUNIT_ASSERT_MESSAGE( "test for getAddr() function: construct a socketaddr with IP assigned, get the address to check correctness.Caught unknown exception on (Win32)",
1165 sal_True == bOK && SocketResult == osl_Socket_Ok);
1168 CPPUNIT_TEST_SUITE( getAddr );
1169 CPPUNIT_TEST( getAddr_001 );
1170 CPPUNIT_TEST_SUITE_END( );
1172 }; // class getAddr
1175 /** testing the methods:
1176 inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr);
1177 inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr);
1178 inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy );
1179 inline sal_Bool SAL_CALL operator== (oslSocketAddr Addr) const;
1180 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; /// not implemented.
1183 class operator_equal : public CppUnit::TestFixture
1185 public:
1186 void operator_equal_001()
1188 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1189 ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1191 saSocketAddrEqual = saSocketAddr;
1192 sal_Bool bOK = sal_False;
1193 ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 );
1195 if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1196 bOK = sal_True;
1198 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: use operator= to assign Ip1 to Ip2, check its modification.",
1199 sal_True == bOK );
1203 void operator_equal_002()
1205 ::osl::SocketAddr saSocketAddr( aHostIp3, IP_PORT_TELNET);
1206 ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1208 saSocketAddrEqual = saSocketAddr;
1209 CPPUNIT_ASSERT_MESSAGE( "after assign, the assigned SocketAddr is not same as the original Addr",
1210 IP_PORT_TELNET == saSocketAddrEqual.getPort( ) );
1211 saSocketAddrEqual.setPort( IP_PORT_MYPORT3 );
1212 saSocketAddr.setPort( IP_PORT_HTTP2 );
1214 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: perform an equal action, then try to change the original address's port. it should not be changed ( handle released), it did not pass in (W32), this is under discussion.",
1215 IP_PORT_MYPORT3 == saSocketAddrEqual.getPort( ) );
1218 void operator_equal_const_001()
1220 const ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1221 ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1223 saSocketAddrEqual = saSocketAddr;
1224 sal_Bool bOK = sal_False;
1225 ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 );
1227 if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1228 bOK = sal_True;
1230 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: use operator= const to assign Ip1 to Ip2, verify the change on the second one.",
1231 sal_True == bOK );
1234 void operator_equal_const_002()
1236 const ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1237 ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1239 saSocketAddrEqual = saSocketAddr;
1240 saSocketAddrEqual.setPort( IP_PORT_HTTP1 );
1242 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: change the second instance, the first one should not be altered, since it does not released the handle.",
1243 IP_PORT_HTTP1 != saSocketAddr.getPort( ) );
1246 void operator_equal_assign_001()
1248 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostIp1, IP_PORT_TELNET );
1249 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
1250 ::osl::SocketAddr* pSocketAddrAssign = new ::osl::SocketAddr( aHostIp2, IP_PORT_FTP );
1251 oslSocketAddr poslSocketAddr = pSocketAddr->getHandle( );
1252 //if( m_handle ) osl_destroySocketAddr( m_handle ); so pSocketAddrAssign had been destroyed and then point to pSocketAddr
1253 pSocketAddrAssign->assign(poslSocketAddr, SAL_NO_COPY);
1255 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.",
1256 pSocketAddrAssign->getPort( ) == IP_PORT_TELNET );
1258 delete pSocketAddrAssign;
1261 void operator_is_equal_001()
1263 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1264 ::osl::SocketAddr saSocketAddrequal( aHostIp1, IP_PORT_TELNET );
1266 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two identical Address.",
1267 sal_True == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) );
1270 void operator_is_equal_002()
1272 ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_FTP);
1273 ::osl::SocketAddr saSocketAddrequal( aHostIp1, IP_PORT_TELNET );
1275 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two different Address.",
1276 sal_False == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) );
1279 CPPUNIT_TEST_SUITE( operator_equal );
1280 CPPUNIT_TEST( operator_equal_001 );
1281 CPPUNIT_TEST( operator_equal_002 );
1282 CPPUNIT_TEST( operator_equal_const_001 );
1283 CPPUNIT_TEST( operator_equal_const_002 );
1284 CPPUNIT_TEST( operator_equal_assign_001 );
1285 CPPUNIT_TEST( operator_is_equal_001 );
1286 CPPUNIT_TEST( operator_is_equal_002 );
1287 CPPUNIT_TEST_SUITE_END( );
1289 }; // class operator_equal
1293 /** testing the method:
1294 inline oslSocketAddr SAL_CALL getHandle() const;
1297 class getSocketAddrHandle : public CppUnit::TestFixture
1299 public:
1301 void getSocketAddrHandle_001()
1303 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 );
1304 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
1305 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
1306 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
1308 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.",
1309 pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) );
1311 delete pSocketAddrCopy;
1314 void getSocketAddrHandle_002()
1316 ::osl::SocketAddr saSocketAddr( aHostName3, IP_PORT_MYPORT4 );
1317 oslSocketAddr poslSocketAddr = saSocketAddr.getHandle( );
1319 sal_Bool bOK = ( saSocketAddr == poslSocketAddr );
1320 //t_print("getSocketAddrHandle_002\n");
1321 CPPUNIT_ASSERT_MESSAGE( "test for getHandle() function: use getHandle() function as an intermediate way to create identical address.",
1322 sal_True == bOK );
1325 CPPUNIT_TEST_SUITE( getSocketAddrHandle );
1326 CPPUNIT_TEST( getSocketAddrHandle_001 );
1327 CPPUNIT_TEST( getSocketAddrHandle_002 );
1328 CPPUNIT_TEST_SUITE_END( );
1330 }; // class getSocketAddrHandle
1333 /** testing the method:
1334 static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0);
1337 class getLocalHostname : public CppUnit::TestFixture
1339 public:
1340 /* the process of getLocalHostname: 1.gethostname (same as /bin/hostname) returned name A
1341 2. search A in /etc/hosts, if there is an alias name is A, return the name in the same row
1344 void getLocalHostname_000()
1346 // _osl_getFullQualifiedDomainName( );
1347 oslSocketResult aResult = osl_Socket_Error;
1348 rtl::OUString suHostname = osl::SocketAddr::getLocalHostname(&aResult);
1349 CPPUNIT_ASSERT_MESSAGE("getLocalHostname failed", aResult == osl_Socket_Ok);
1352 void getLocalHostname_001()
1354 oslSocketResult *pResult = NULL;
1355 //printSocketResult(*pResult);
1356 ::rtl::OUString suResult = ::osl::SocketAddr::getLocalHostname( pResult );
1358 // LLA: IMHO localhost, or hostname by itself should be ok.
1359 rtl::OUString suThisHost = getThisHostname( );
1360 bool bOk = false;
1361 if (suThisHost.equals(rtl::OUString::createFromAscii("localhost")))
1363 bOk = true;
1365 else
1367 if (suThisHost.equals(suResult))
1369 bOk = true;
1373 ::rtl::OUString suError;
1374 suError = outputError(suResult, getThisHostname( ), "test for getLocalHostname() function");
1376 CPPUNIT_ASSERT_MESSAGE( suError, bOk == true );
1379 CPPUNIT_TEST_SUITE( getLocalHostname );
1380 CPPUNIT_TEST( getLocalHostname_000 );
1381 CPPUNIT_TEST( getLocalHostname_001 );
1382 CPPUNIT_TEST_SUITE_END( );
1384 }; // class getLocalHostname
1387 /** testing the method:
1388 static inline void SAL_CALL resolveHostname( const ::rtl::OUString & strHostName , SocketAddr & Addr );
1391 class resolveHostname : public CppUnit::TestFixture
1393 public:
1394 void resolveHostname_001()
1396 ::osl::SocketAddr saSocketAddr;
1397 ::osl::SocketAddr::resolveHostname( aHostIp1, saSocketAddr );
1398 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 );
1399 sal_Bool bOK = sal_False;
1401 if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1402 bOK = sal_True;
1404 CPPUNIT_ASSERT_MESSAGE( "test for resolveHostname() function: try to resolve localhost to 127.0.0.1.",
1405 sal_True == bOK );
1408 CPPUNIT_TEST_SUITE( resolveHostname );
1409 CPPUNIT_TEST( resolveHostname_001 );
1410 CPPUNIT_TEST_SUITE_END( );
1412 }; // class resolveHostname
1415 /** testing the method:
1416 static inline sal_Int32 SAL_CALL getServicePort(
1417 const ::rtl::OUString& strServiceName,
1418 const ::rtl::OUString & strProtocolName= ::rtl::OUString::createFromAscii( "tcp" ) );
1421 class gettheServicePort : public CppUnit::TestFixture
1423 public:
1424 void gettheServicePort_001()
1426 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get ftp service port on TCP protocol.",
1427 IP_PORT_FTP== ::osl::SocketAddr::getServicePort( aServiceFTP, aProtocolTCP ) );
1430 void gettheServicePort_002()
1432 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get telnet service port on TCP protocol.",
1433 IP_PORT_TELNET== ::osl::SocketAddr::getServicePort( aServiceTELNET, aProtocolTCP ) );
1436 void gettheServicePort_003()
1438 //Solaris has no service called "https", please see /etc/services
1439 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get netbios-ssn service port on UDP protocol.",
1440 IP_PORT_NETBIOS_DGM == ::osl::SocketAddr::getServicePort( aServiceNETBIOS, aProtocolUDP ) );
1443 void gettheServicePort_004()
1445 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get a service port which is not exist.",
1446 OSL_INVALID_PORT == ::osl::SocketAddr::getServicePort( ::rtl::OUString::createFromAscii( "notexist" ), aProtocolUDP ) );
1449 CPPUNIT_TEST_SUITE( gettheServicePort );
1450 CPPUNIT_TEST( gettheServicePort_001 );
1451 CPPUNIT_TEST( gettheServicePort_002 );
1452 CPPUNIT_TEST( gettheServicePort_003 );
1453 CPPUNIT_TEST( gettheServicePort_004 );
1454 CPPUNIT_TEST_SUITE_END( );
1456 }; // class gettheServicePort
1458 // -----------------------------------------------------------------------------
1461 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::ctors, "osl_SocketAddr");
1462 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::is, "osl_SocketAddr");
1463 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getHostname, "osl_SocketAddr");
1464 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getPort, "osl_SocketAddr");
1465 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setPort, "osl_SocketAddr");
1466 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setAddr, "osl_SocketAddr");
1467 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getAddr, "osl_SocketAddr");
1468 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::operator_equal, "osl_SocketAddr");
1469 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getSocketAddrHandle, "osl_SocketAddr");
1470 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getLocalHostname, "osl_SocketAddr");
1471 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::resolveHostname, "osl_SocketAddr");
1472 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::gettheServicePort, "osl_SocketAddr");
1475 } // namespace osl_SocketAddr
1479 namespace osl_Socket
1482 /** testing the methods:
1483 inline Socket( );
1484 inline Socket( const Socket & socket );
1485 inline Socket( oslSocket socketHandle );
1486 inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire );
1489 /** test writer's comment:
1491 class Socket can not be initialized by its protected constructor, though the protected
1492 constructor is the most convenient way to create a new socket.
1493 it only allow the method of C function osl_createSocket like:
1494 ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream,
1495 osl_Socket_ProtocolIp ) );
1496 the use of C method lost some of the transparent of tester using C++ wrapper.
1500 class ctors : public CppUnit::TestFixture
1502 public:
1503 oslSocket sHandle;
1504 // initialization
1505 void setUp( )
1507 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1510 void tearDown( )
1512 sHandle = NULL;
1516 void ctors_none()
1518 /// Socket constructor.
1519 // ::osl::Socket sSocket;
1521 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the socket was created successfully, if no exception occured",
1522 1 == 1 );
1525 void ctors_acquire()
1527 /// Socket constructor.
1528 ::osl::Socket sSocket( sHandle );
1530 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
1531 osl_Socket_TypeStream == sSocket.getType( ) );
1534 void ctors_no_acquire()
1536 /// Socket constructor.
1537 ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE );
1539 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
1540 osl_Socket_TypeStream == sSocket.getType( ) );
1543 void ctors_copy_ctor()
1545 ::osl::Socket sSocket( sHandle );
1546 /// Socket copy constructor.
1547 ::osl::Socket copySocket( sSocket );
1549 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
1550 osl_Socket_TypeStream == copySocket.getType( ) );
1553 void ctors_TypeRaw()
1555 #ifdef WNT
1556 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
1557 // LLA: ? ::osl::Socket sSocket( sHandleRaw );
1558 CPPUNIT_ASSERT_MESSAGE( " type osl_Socket_TypeRaw socket create failed on UNX ", sHandleRaw != NULL);
1559 #else
1560 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
1561 CPPUNIT_ASSERT_MESSAGE( " can't create socket with type osl_Socket_TypeRaw within UNX is ok.", sHandleRaw == NULL);
1562 #endif
1565 void ctors_family_Ipx()
1567 oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1568 CPPUNIT_ASSERT_MESSAGE( " family osl_Socket_FamilyIpx socket create failed! ", sHandleIpx != NULL);
1569 ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE );
1570 t_print("#Type is %d \n", sSocket.getType( ) );
1572 CPPUNIT_ASSERT_MESSAGE(" test for create new Socket instance that family is osl_Socket_FamilyIpx",
1573 osl_Socket_TypeStream == sSocket.getType( ) );
1578 CPPUNIT_TEST_SUITE( ctors );
1579 CPPUNIT_TEST( ctors_none );
1580 CPPUNIT_TEST( ctors_acquire );
1581 CPPUNIT_TEST( ctors_no_acquire );
1582 CPPUNIT_TEST( ctors_copy_ctor );
1583 CPPUNIT_TEST( ctors_TypeRaw );
1584 CPPUNIT_TEST( ctors_family_Ipx );
1585 CPPUNIT_TEST_SUITE_END();
1587 }; // class ctors
1590 /** testing the methods:
1591 inline Socket& SAL_CALL operator= ( oslSocket socketHandle);
1592 inline Socket& SAL_CALL operator= (const Socket& sock);
1593 inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ;
1594 inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const;
1597 class operators : public CppUnit::TestFixture
1599 public:
1600 oslSocket sHandle;
1601 // initialization
1602 void setUp( )
1604 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1607 void tearDown( )
1609 sHandle = NULL;
1613 /** test writer's comment:
1615 the assignment operator does not support direct assinment like:
1616 ::osl::Socket sSocket = sHandle.
1618 void operators_assignment_handle()
1620 ::osl::Socket sSocket(sHandle);
1621 ::osl::Socket assignSocket = sSocket.getHandle();
1623 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
1624 osl_Socket_TypeStream == assignSocket.getType( ) );
1627 void operators_assignment()
1629 ::osl::Socket sSocket( sHandle );
1630 ::osl::Socket assignSocket = sSocket;
1632 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
1633 osl_Socket_TypeStream == assignSocket.getType( ) );
1636 void operators_equal_handle_001()
1638 /// Socket constructor.
1639 ::osl::Socket sSocket( sHandle );
1640 ::osl::Socket equalSocket = sSocket;
1642 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check equal.",
1643 equalSocket == sHandle );
1646 void operators_equal_handle_002()
1648 /// Socket constructor.
1649 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
1651 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check unequal.",
1652 !( equalSocket == sHandle ) );
1655 void operators_equal_001()
1657 ::osl::Socket sSocket( sHandle );
1658 /// Socket copy constructor.
1659 ::osl::Socket equalSocket( sSocket );
1661 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal function: check equal.",
1662 equalSocket == sSocket );
1665 void operators_equal_002()
1667 ::osl::Socket sSocket( sHandle );
1668 /// Socket copy constructor.
1669 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
1671 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_002 function: check unequal.",
1672 !( equalSocket == sSocket ) );
1675 CPPUNIT_TEST_SUITE( operators );
1676 CPPUNIT_TEST( operators_assignment_handle );
1677 CPPUNIT_TEST( operators_assignment );
1678 CPPUNIT_TEST( operators_equal_handle_001 );
1679 CPPUNIT_TEST( operators_equal_handle_002 );
1680 CPPUNIT_TEST( operators_equal_001 );
1681 CPPUNIT_TEST( operators_equal_002 );
1682 CPPUNIT_TEST_SUITE_END();
1684 }; // class operators
1687 /** testing the methods:
1688 inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite );
1689 inline void SAL_CALL close();
1692 class close : public CppUnit::TestFixture
1694 public:
1695 oslSocket sHandle;
1696 // initialization
1697 void setUp( )
1699 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1702 void tearDown( )
1704 sHandle = NULL;
1708 void close_001()
1710 ::osl::Socket sSocket(sHandle);
1711 sSocket.close();
1713 CPPUNIT_ASSERT_MESSAGE( "test for close_001 function: this function is reserved for test.",
1714 sSocket.getHandle() == sHandle );
1717 void close_002()
1719 //#if defined(LINUX)
1720 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1721 AcceptorThread myAcceptorThread( asSocket, aHostIp1 );
1722 myAcceptorThread.create();
1724 thread_sleep( 1 );
1725 //when accepting, close the socket, the thread will not block for accepting
1726 //man close:Any locks held on the file it was associated with, and owned by the process, are removed
1727 asSocket.close();
1728 //thread_sleep( 2 );
1729 myAcceptorThread.join();
1731 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
1732 myAcceptorThread.isOK() == sal_True );
1733 //#endif
1736 // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( )
1737 void close_003()
1739 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1740 AcceptorThread myAcceptorThread( asSocket, aHostIpZero );
1741 myAcceptorThread.create();
1743 thread_sleep( 1 );
1744 asSocket.close();
1745 myAcceptorThread.join();
1747 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
1748 myAcceptorThread.isOK() == sal_True );
1751 CPPUNIT_TEST_SUITE( close );
1752 CPPUNIT_TEST( close_001 );
1753 CPPUNIT_TEST( close_002 );
1754 CPPUNIT_TEST( close_003 );
1755 CPPUNIT_TEST_SUITE_END();
1757 }; // class close
1759 /** testing the method:
1760 inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const;
1763 class getLocalAddr : public CppUnit::TestFixture
1765 public:
1766 oslSocket sHandle;
1767 // initialization
1768 void setUp( )
1770 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1773 void tearDown( )
1775 sHandle = NULL;
1778 // get the Address of the local end of the socket
1779 void getLocalAddr_001()
1781 ::osl::Socket sSocket(sHandle);
1782 ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT8 );
1783 ::osl::SocketAddr saLocalSocketAddr;
1785 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1787 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1788 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
1789 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
1791 sSocket.getLocalAddr( saLocalSocketAddr );
1793 sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ;
1795 CPPUNIT_ASSERT_MESSAGE( "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.",
1796 sal_True == bOK );
1800 CPPUNIT_TEST_SUITE( getLocalAddr );
1801 CPPUNIT_TEST( getLocalAddr_001 );
1802 CPPUNIT_TEST_SUITE_END();
1804 }; // class getLocalAddr
1807 /** testing the method:
1808 inline sal_Int32 SAL_CALL getLocalPort() const;
1811 class getLocalPort : public CppUnit::TestFixture
1813 public:
1814 oslSocket sHandle;
1815 // initialization
1816 void setUp( )
1818 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1821 void tearDown( )
1823 sHandle = NULL;
1827 void getLocalPort_001()
1829 ::osl::Socket sSocket(sHandle);
1830 ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT7 ); // aHostIp1 localhost
1831 ::osl::SocketAddr saLocalSocketAddr;
1833 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1835 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1836 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
1837 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
1838 sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) );
1840 CPPUNIT_ASSERT_MESSAGE( "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.",
1841 sal_True == bOK );
1844 /** test writer's comment:
1846 the invalid port number can not be set by giving invalid port number
1847 such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be
1848 valid, the only instance that the getLocalPort returns OSL_INVALID_PORT
1849 is when saSocketAddr itself is an invalid one, that is , the IP or host name
1850 can not be found, then the created socket address is not valid.
1852 void getLocalPort_002()
1854 ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_TELNET);
1855 #ifdef WNT
1856 ::osl::Socket sSocket(sHandle);
1857 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
1858 sSocket.bind( saBindSocketAddr );
1859 //Invalid IP, so bind should fail
1860 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
1861 ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT),
1862 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned.");
1863 sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) );
1864 (void)bOK;
1865 #else
1866 //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT
1867 ::rtl::OUString suError = ::rtl::OUString::createFromAscii( "on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT, but can not create Addr of that case");
1868 #endif
1869 CPPUNIT_ASSERT_MESSAGE( suError, sal_False );
1873 void getLocalPort_003()
1875 ::osl::Socket sSocket(sHandle);
1876 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL);
1878 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1880 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1881 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
1882 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
1883 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
1884 ::rtl::OUString::createFromAscii("34463"),
1885 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned");
1886 sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535);
1888 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
1891 CPPUNIT_TEST_SUITE( getLocalPort );
1892 CPPUNIT_TEST( getLocalPort_001 );
1893 // LLA: CPPUNIT_TEST( getLocalPort_002 );
1894 CPPUNIT_TEST( getLocalPort_003 );
1895 CPPUNIT_TEST_SUITE_END();
1897 }; // class getLocalPort
1900 /** testing the method:
1901 inline ::rtl::OUString SAL_CALL getLocalHost() const;
1903 Mindyliu: on Linux, at first it will check the binded in /etc/hosts, if it has the binded IP, it will return the hostname in it;
1904 else if the binded IP is "127.0.0.1", it will return "localhost", if it's the machine's ethernet ip such as "129.158.217.90", it
1905 will return hostname of current processor such as "aegean.PRC.Sun.COM"
1908 class getLocalHost : public CppUnit::TestFixture
1910 public:
1911 oslSocket sHandle;
1912 // initialization
1913 void setUp( )
1915 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1918 void tearDown( )
1920 sHandle = NULL;
1924 void getLocalHost_001()
1926 ::osl::Socket sSocket(sHandle);
1927 //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu
1928 ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT6 );
1930 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1932 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1933 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
1934 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
1935 sal_Bool bOK;
1936 ::rtl::OUString suError;
1937 #ifdef WNT
1938 bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
1939 suError = outputError(sSocket.getLocalHost( ), getThisHostname( ),
1940 "test for getLocalHost function: create localhost socket and check name");
1941 #else
1942 ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" );
1943 sal_Bool bRes1, bRes2;
1944 bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ;
1945 bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ;
1946 bOK = bRes1 || bRes2;
1947 suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name");
1948 #endif
1949 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
1952 void getLocalHost_002()
1954 ::osl::Socket sSocket(sHandle);
1955 ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_POP3);
1956 ::osl::SocketAddr saLocalSocketAddr;
1958 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1959 sSocket.bind( saBindSocketAddr );
1960 //Invalid IP, so bind should fail
1961 sal_Bool bOK = compareUString( sSocket.getLocalHost( ), aNullURL ) ;
1962 ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), aNullURL, "test for getLocalHost function: getLocalHost with invalid SocketAddr");
1964 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
1967 CPPUNIT_TEST_SUITE( getLocalHost );
1968 CPPUNIT_TEST( getLocalHost_001 );
1969 CPPUNIT_TEST( getLocalHost_002 );
1970 CPPUNIT_TEST_SUITE_END();
1972 }; // class getLocalHost
1975 /** testing the methods:
1976 inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const;
1977 inline sal_Int32 SAL_CALL getPeerPort() const;
1978 inline ::rtl::OUString SAL_CALL getPeerHost() const;
1980 class getPeer : public CppUnit::TestFixture
1982 public:
1983 oslSocket sHandle;
1984 TimeValue *pTimeout;
1985 ::osl::AcceptorSocket asAcceptorSocket;
1986 ::osl::ConnectorSocket csConnectorSocket;
1989 // initialization
1990 void setUp( )
1992 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
1993 pTimeout->Seconds = 3;
1994 pTimeout->Nanosec = 0;
1995 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1998 void tearDown( )
2000 free( pTimeout );
2001 sHandle = NULL;
2002 asAcceptorSocket.close( );
2003 csConnectorSocket.close( );
2007 void getPeer_001()
2009 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
2010 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT );
2011 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
2012 ::osl::StreamSocket ssConnection;
2013 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2014 /// launch server socket
2015 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
2016 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind '127.0.0.1' address failed.", sal_True == bOK1 );
2017 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
2018 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
2020 asAcceptorSocket.enableNonBlockingMode( sal_True );
2021 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
2023 /// launch client socket
2024 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
2026 /// get peer information
2027 csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
2028 sal_Int32 peerPort = csConnectorSocket.getPeerPort( );
2029 ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( );
2031 CPPUNIT_ASSERT_MESSAGE( "test for getPeer function: setup a connection and then get the peer address, port and host from client side.",
2032 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&&
2033 ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) &&
2034 ( peerPort == saLocalSocketAddr.getPort( ) ));
2038 CPPUNIT_TEST_SUITE( getPeer );
2039 CPPUNIT_TEST( getPeer_001 );
2040 CPPUNIT_TEST_SUITE_END();
2042 }; // class getPeer
2045 /** testing the methods:
2046 inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface);
2050 class bind : public CppUnit::TestFixture
2052 public:
2053 oslSocket sHandle;
2054 // initialization
2055 void setUp( )
2057 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2060 void tearDown( )
2062 sHandle = NULL;
2066 void bind_001()
2068 ::osl::Socket sSocket(sHandle);
2069 //bind must use local IP address ---mindyliu
2070 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 );
2072 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2073 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
2074 CPPUNIT_ASSERT_MESSAGE( "Socket bind fail.", sal_True == bOK1 );
2076 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ;
2078 sSocket.close();
2079 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", sal_True == bOK2 );
2082 void bind_002()
2084 ::osl::Socket sSocket(sHandle);
2085 ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_NETBIOS );
2086 ::osl::SocketAddr saLocalSocketAddr;
2088 sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True);
2089 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
2090 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
2092 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.",
2093 ( sal_False == bOK1 ) && ( sal_False == bOK2 ) );
2096 CPPUNIT_TEST_SUITE( bind );
2097 CPPUNIT_TEST( bind_001 );
2098 CPPUNIT_TEST( bind_002 );
2099 CPPUNIT_TEST_SUITE_END();
2101 }; // class bind
2104 /** testing the methods:
2105 inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const;
2108 class isRecvReady : public CppUnit::TestFixture
2110 public:
2111 oslSocket sHandle;
2112 TimeValue *pTimeout;
2113 ::osl::AcceptorSocket asAcceptorSocket;
2114 ::osl::ConnectorSocket csConnectorSocket;
2117 // initialization
2118 void setUp( )
2120 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
2121 pTimeout->Seconds = 3;
2122 pTimeout->Nanosec = 0;
2123 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2126 void tearDown( )
2128 free( pTimeout );
2129 sHandle = NULL;
2130 asAcceptorSocket.close( );
2131 csConnectorSocket.close( );
2135 void isRecvReady_001()
2137 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT1 );
2138 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT1 );
2139 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
2140 ::osl::StreamSocket ssConnection;
2141 /// launch server socket
2142 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
2143 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
2144 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
2145 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
2146 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
2147 asAcceptorSocket.enableNonBlockingMode( sal_True );
2148 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
2150 /// launch client socket
2151 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
2153 /// is receive ready?
2154 sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout );
2156 CPPUNIT_ASSERT_MESSAGE( "test for isRecvReady function: setup a connection and then check if it can transmit data.",
2157 ( sal_True == bOK3 ) );
2161 CPPUNIT_TEST_SUITE( isRecvReady );
2162 CPPUNIT_TEST( isRecvReady_001 );
2163 CPPUNIT_TEST_SUITE_END();
2165 }; // class isRecvReady
2168 /** testing the methods:
2169 inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const;
2171 class isSendReady : public CppUnit::TestFixture
2173 public:
2174 oslSocket sHandle;
2175 TimeValue *pTimeout;
2176 ::osl::AcceptorSocket asAcceptorSocket;
2177 ::osl::ConnectorSocket csConnectorSocket;
2180 // initialization
2181 void setUp( )
2183 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
2184 pTimeout->Seconds = 3;
2185 pTimeout->Nanosec = 0;
2186 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2189 void tearDown( )
2191 free( pTimeout );
2192 sHandle = NULL;
2193 asAcceptorSocket.close( );
2194 csConnectorSocket.close( );
2198 void isSendReady_001()
2200 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
2201 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT );
2202 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
2203 ::osl::StreamSocket ssConnection;
2205 /// launch server socket
2206 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2207 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
2208 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
2209 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
2210 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
2211 asAcceptorSocket.enableNonBlockingMode( sal_True );
2212 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
2214 /// launch client socket
2215 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
2217 /// is send ready?
2218 sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout );
2220 CPPUNIT_ASSERT_MESSAGE( "test for isSendReady function: setup a connection and then check if it can transmit data.",
2221 ( sal_True == bOK3 ) );
2225 CPPUNIT_TEST_SUITE( isSendReady );
2226 CPPUNIT_TEST( isSendReady_001 );
2227 CPPUNIT_TEST_SUITE_END();
2229 }; // class isSendReady
2232 /** testing the methods:
2233 inline oslSocketType SAL_CALL getType() const;
2237 class getType : public CppUnit::TestFixture
2239 public:
2240 oslSocket sHandle;
2241 // initialization
2242 void setUp( )
2247 void tearDown( )
2249 sHandle = NULL;
2253 void getType_001()
2255 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2256 ::osl::Socket sSocket(sHandle);
2258 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
2259 osl_Socket_TypeStream == sSocket.getType( ) );
2262 void getType_002()
2264 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2265 ::osl::Socket sSocket(sHandle);
2267 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
2268 osl_Socket_TypeDgram == sSocket.getType( ) );
2271 #ifdef UNX
2272 // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here
2273 // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw()
2274 void getType_003()
2276 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.this is not passed in (LINUX, SOLARIS), the osl_Socket_TypeRaw, type socket can not be created.",
2277 sal_True);
2279 #else
2280 void getType_003()
2282 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
2283 ::osl::Socket sSocket(sHandle);
2285 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
2286 osl_Socket_TypeRaw == sSocket.getType( ) );
2288 #endif
2290 CPPUNIT_TEST_SUITE( getType );
2291 CPPUNIT_TEST( getType_001 );
2292 CPPUNIT_TEST( getType_002 );
2293 CPPUNIT_TEST( getType_003 );
2294 CPPUNIT_TEST_SUITE_END();
2296 }; // class getType
2300 /** testing the methods:
2301 inline sal_Int32 SAL_CALL getOption(
2302 oslSocketOption Option,
2303 void* pBuffer,
2304 sal_uInt32 BufferLen,
2305 oslSocketOptionLevel Level= osl_Socket_LevelSocket) const;
2307 inline sal_Int32 getOption( oslSocketOption option ) const;
2311 class getOption : public CppUnit::TestFixture
2313 public:
2314 oslSocket sHandle;
2315 // initialization
2316 void setUp( )
2321 void tearDown( )
2323 sHandle = NULL;
2326 /** test writer's comment:
2328 in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream.
2329 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ
2330 in 1.
2333 void getOption_001()
2335 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2336 ::osl::Socket sSocket(sHandle);
2337 sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
2338 *pType = 0;
2339 sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) );
2340 sal_Bool bOK = ( SOCK_STREAM == *pType );
2341 // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream
2342 //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType );
2343 free( pType );
2345 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get type option of socket.",
2346 sal_True == bOK );
2349 // getsockopt error
2350 void getOption_004()
2352 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2353 ::osl::Socket sSocket(sHandle);
2355 sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
2356 sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) );
2357 free( pbDontRoute );
2359 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get invalid option of socket, should return -1.",
2360 nRes == -1 );
2363 void getOption_simple_001()
2365 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2366 ::osl::Socket sSocket(sHandle);
2368 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) );
2370 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
2371 sal_True == bOK );
2374 void getOption_simple_002()
2376 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2377 ::osl::Socket sSocket(sHandle);
2379 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) );
2381 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
2382 sal_True == bOK );
2385 CPPUNIT_TEST_SUITE( getOption );
2386 CPPUNIT_TEST( getOption_001 );
2387 CPPUNIT_TEST( getOption_004 );
2388 CPPUNIT_TEST( getOption_simple_001 );
2389 CPPUNIT_TEST( getOption_simple_002 );
2390 CPPUNIT_TEST_SUITE_END();
2392 }; // class getOption
2395 /** testing the methods:
2396 inline sal_Bool SAL_CALL setOption( oslSocketOption Option,
2397 void* pBuffer,
2398 sal_uInt32 BufferLen,
2399 oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const;
2402 class setOption : public CppUnit::TestFixture
2404 public:
2405 TimeValue *pTimeout;
2406 // LLA: maybe there is an error in the source,
2407 // as long as I remember, if a derived class do not overload all ctors there is a problem.
2409 ::osl::AcceptorSocket asAcceptorSocket;
2411 void setUp( )
2416 void tearDown( )
2418 asAcceptorSocket.close( );
2422 // LLA:
2423 // getSocketOption returns BufferLen, or -1 if something failed
2425 // setSocketOption returns sal_True, if option could stored
2426 // else sal_False
2428 void setOption_001()
2430 /// set and get option.
2431 int nBufferLen = sizeof ( sal_Int32);
2432 // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool!
2434 sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
2435 *pbDontRouteSet = 1; // sal_True;
2437 sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
2438 *pGetBuffer = 0;
2440 // maybe asAcceptorSocket is not right initialized
2441 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen );
2442 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b1 ) );
2443 sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
2444 CPPUNIT_ASSERT_MESSAGE( "getOption function failed.", ( n2 == nBufferLen ) );
2446 // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value,
2447 // just judge it is zero or not!
2448 sal_Bool bOK = ( 0 != *pGetBuffer );
2449 t_print("#setOption_001: getOption is %d \n", *pGetBuffer);
2451 // toggle check, set to 0
2452 *pbDontRouteSet = 0;
2454 sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) );
2455 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b3 ) );
2456 sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
2457 CPPUNIT_ASSERT_MESSAGE( "getOption (DONTROUTE) function failed.", ( n4 == nBufferLen ) );
2459 sal_Bool bOK2 = ( 0 == *pGetBuffer );
2461 t_print("#setOption_001: getOption is %d \n", *pGetBuffer);
2463 // LLA: sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
2464 // LLA: *pbDontTouteSet = sal_True;
2465 // LLA: sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
2466 // LLA: *pbDontTouteGet = sal_False;
2467 // LLA: asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontTouteSet, sizeof ( sal_Bool ) );
2468 // LLA: asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pbDontTouteGet, sizeof ( sal_Bool ) );
2469 // LLA: ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet),
2470 // LLA: ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet),
2471 // LLA: "test for setOption function: set osl_Socket_OptionDontRoute and then check");
2472 // LLA:
2473 // LLA: sal_Bool bOK = ( sal_True == *pbDontTouteGet );
2474 // LLA: free( pbDontTouteSet );
2475 // LLA: free( pbDontTouteGet );
2477 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
2478 ( sal_True == bOK ) && (sal_True == bOK2) );
2480 free( pbDontRouteSet );
2481 free( pGetBuffer );
2482 // LLA: CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
2485 void setOption_002()
2487 /// set and get option.
2489 // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen );
2490 // *pbLingerSet = 7;
2491 // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen );
2492 /* struct */linger aLingerSet;
2493 sal_Int32 nBufferLen = sizeof( struct linger );
2494 aLingerSet.l_onoff = 1;
2495 aLingerSet.l_linger = 7;
2497 linger aLingerGet;
2499 asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
2501 sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen );
2502 CPPUNIT_ASSERT_MESSAGE( "getOption (SO_LINGER) function failed.", ( n1 == nBufferLen ) );
2504 //t_print("#setOption_002: getOption is %d \n", aLingerGet.l_linger);
2505 sal_Bool bOK = ( 7 == aLingerGet.l_linger );
2506 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check. ",
2507 sal_True == bOK );
2511 void setOption_003()
2513 linger aLingerSet;
2514 aLingerSet.l_onoff = 1;
2515 aLingerSet.l_linger = 7;
2517 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 );
2518 printUString( asAcceptorSocket.getErrorAsString() );
2519 CPPUNIT_ASSERT_MESSAGE( "setOption (SO_LINGER) function failed for optlen is 0.",
2520 ( b1 == sal_False ) );
2523 void setOption_simple_001()
2525 /// set and get option.
2526 asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True );
2527 sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
2529 t_print("setOption_simple_001(): getoption is %d \n", asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
2530 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
2531 ( sal_True == bOK ) );
2534 void setOption_simple_002()
2536 /// set and get option.
2537 // LLA: this does not work, due to the fact that SO_LINGER is a structure
2538 // LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 );
2539 // LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) );
2541 // LLA: CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
2542 // LLA: ( sal_True == bOK ) );
2545 CPPUNIT_TEST_SUITE( setOption );
2546 CPPUNIT_TEST( setOption_001 );
2547 CPPUNIT_TEST( setOption_002 );
2548 CPPUNIT_TEST( setOption_003 );
2549 CPPUNIT_TEST( setOption_simple_001 );
2550 // LLA: CPPUNIT_TEST( setOption_simple_002 );
2551 CPPUNIT_TEST_SUITE_END();
2553 }; // class setOption
2557 /** testing the method:
2558 inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode);
2560 class enableNonBlockingMode : public CppUnit::TestFixture
2562 public:
2563 ::osl::AcceptorSocket asAcceptorSocket;
2565 void enableNonBlockingMode_001()
2567 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
2568 ::osl::StreamSocket ssConnection;
2570 /// launch server socket
2571 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2572 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
2573 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
2574 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
2575 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
2576 asAcceptorSocket.enableNonBlockingMode( sal_True );
2577 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
2579 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
2580 sal_Bool bOK = sal_True;
2581 asAcceptorSocket.close( );
2583 CPPUNIT_ASSERT_MESSAGE( "test for enableNonBlockingMode function: launch a server socket and make it non blocking. if it can pass the acceptConnection statement, it is non-blocking",
2584 ( sal_True == bOK ) );
2588 CPPUNIT_TEST_SUITE( enableNonBlockingMode );
2589 CPPUNIT_TEST( enableNonBlockingMode_001 );
2590 CPPUNIT_TEST_SUITE_END();
2592 }; // class enableNonBlockingMode
2595 /** testing the method:
2596 inline sal_Bool SAL_CALL isNonBlockingMode() const;
2598 class isNonBlockingMode : public CppUnit::TestFixture
2600 public:
2601 ::osl::AcceptorSocket asAcceptorSocket;
2603 void isNonBlockingMode_001()
2605 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
2606 ::osl::StreamSocket ssConnection;
2608 /// launch server socket
2609 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
2610 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
2611 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
2612 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
2613 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
2615 sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( );
2616 asAcceptorSocket.enableNonBlockingMode( sal_True );
2617 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
2619 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
2620 sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( );
2621 asAcceptorSocket.close( );
2623 CPPUNIT_ASSERT_MESSAGE( "test for isNonBlockingMode function: launch a server socket and make it non blocking. it is expected to change from blocking mode to non-blocking mode.",
2624 ( sal_False == bOK3 ) && ( sal_True == bOK4 ) );
2628 CPPUNIT_TEST_SUITE( isNonBlockingMode );
2629 CPPUNIT_TEST( isNonBlockingMode_001 );
2630 CPPUNIT_TEST_SUITE_END();
2632 }; // class isNonBlockingMode
2634 /** testing the method:
2635 inline void SAL_CALL clearError() const;
2637 class clearError : public CppUnit::TestFixture
2639 public:
2640 oslSocket sHandle;
2641 // initialization
2642 void setUp( )
2644 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2647 void tearDown( )
2649 sHandle = NULL;
2653 void clearError_001()
2655 ::osl::Socket sSocket(sHandle);
2656 ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_HTTP2 );
2657 ::osl::SocketAddr saLocalSocketAddr;
2658 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2659 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
2660 oslSocketError seBind = sSocket.getError( );
2661 sSocket.clearError( );
2663 CPPUNIT_ASSERT_MESSAGE( "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.",
2664 osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None );
2668 CPPUNIT_TEST_SUITE( clearError );
2669 CPPUNIT_TEST( clearError_001 );
2670 CPPUNIT_TEST_SUITE_END();
2672 }; // class clearError
2675 /** testing the methods:
2676 inline oslSocketError getError() const;
2677 inline ::rtl::OUString getErrorAsString( ) const;
2679 class getError : public CppUnit::TestFixture
2681 public:
2682 oslSocket sHandle;
2683 // initialization
2684 void setUp( )
2686 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2689 void tearDown( )
2691 sHandle = NULL;
2695 void getError_001()
2697 ::osl::Socket sSocket(sHandle);
2698 ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_FTP );
2699 ::osl::SocketAddr saLocalSocketAddr;
2701 CPPUNIT_ASSERT_MESSAGE( "test for getError function: should get no error.",
2702 osl_Socket_E_None == sSocket.getError( ) );
2705 void getError_002()
2707 ::osl::Socket sSocket(sHandle);
2708 ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_FTP );
2709 ::osl::SocketAddr saLocalSocketAddr;
2710 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2711 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
2712 //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError.
2713 #if defined(SOLARIS)
2714 CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. ",
2715 osl_Socket_E_InvalidError == sSocket.getError( ) );
2716 #else
2717 //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail.
2719 CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. Passed on Linux & Win32",
2720 osl_Socket_E_AddrNotAvail == sSocket.getError( ) );
2721 #endif
2724 CPPUNIT_TEST_SUITE( getError );
2725 CPPUNIT_TEST( getError_001 );
2726 CPPUNIT_TEST( getError_002 );
2727 CPPUNIT_TEST_SUITE_END();
2729 }; // class getError
2733 /** testing the methods:
2734 inline oslSocket getHandle() const;
2737 class getHandle : public CppUnit::TestFixture
2739 public:
2740 oslSocket sHandle;
2741 // initialization
2742 void setUp( )
2744 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2747 void tearDown( )
2749 sHandle = NULL;
2752 void getHandle_001()
2754 ::osl::Socket sSocket(sHandle);
2755 ::osl::Socket assignSocket = sSocket.getHandle();
2757 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
2758 osl_Socket_TypeStream == assignSocket.getType( ) );
2761 void getHandle_002()
2763 ::osl::Socket sSocket( sHandle );
2764 ::osl::Socket assignSocket ( sSocket.getHandle( ) );
2766 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
2767 osl_Socket_TypeStream == assignSocket.getType( ) );
2770 CPPUNIT_TEST_SUITE( getHandle );
2771 CPPUNIT_TEST( getHandle_001 );
2772 CPPUNIT_TEST( getHandle_002 );
2773 CPPUNIT_TEST_SUITE_END();
2775 }; // class getHandle
2778 // -----------------------------------------------------------------------------
2781 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::ctors, "osl_Socket");
2782 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::operators, "osl_Socket");
2783 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::close, "osl_Socket");
2784 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalAddr, "osl_Socket");
2785 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalPort, "osl_Socket");
2786 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalHost, "osl_Socket");
2787 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getPeer, "osl_Socket");
2788 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::bind, "osl_Socket");
2789 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isRecvReady, "osl_Socket");
2790 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isSendReady, "osl_Socket");
2791 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getType, "osl_Socket");
2792 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getOption, "osl_Socket");
2793 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::setOption, "osl_Socket");
2794 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::enableNonBlockingMode, "osl_Socket");
2795 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isNonBlockingMode, "osl_Socket");
2796 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::clearError, "osl_Socket");
2797 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getError, "osl_Socket");
2798 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getHandle, "osl_Socket");
2800 } // namespace osl_Socket
2804 namespace osl_StreamSocket
2807 /** testing the methods:
2808 inline StreamSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
2809 oslProtocol Protocol = osl_Socket_ProtocolIp,
2810 oslSocketType Type = osl_Socket_TypeStream);
2812 inline StreamSocket( const StreamSocket & );
2814 inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire );
2816 inline StreamSocket( oslSocket Socket );
2819 class ctors : public CppUnit::TestFixture
2821 public:
2822 oslSocket sHandle;
2823 // initialization
2824 void setUp( )
2826 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2829 void tearDown( )
2831 sHandle = NULL;
2835 void ctors_none()
2837 /// Socket constructor.
2838 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2840 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the stream socket was created successfully.",
2841 osl_Socket_TypeStream == ssSocket.getType( ) );
2844 void ctors_acquire()
2846 /// Socket constructor.
2847 ::osl::StreamSocket ssSocket( sHandle );
2849 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
2850 osl_Socket_TypeStream == ssSocket.getType( ) );
2853 void ctors_no_acquire()
2855 /// Socket constructor.
2856 ::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE );
2858 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
2859 osl_Socket_TypeStream == ssSocket.getType( ) );
2862 void ctors_copy_ctor()
2864 /// Socket constructor.
2865 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2866 /// Socket copy constructor.
2867 ::osl::StreamSocket copySocket( ssSocket );
2869 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
2870 osl_Socket_TypeStream == copySocket.getType( ) );
2873 CPPUNIT_TEST_SUITE( ctors );
2874 CPPUNIT_TEST( ctors_none );
2875 CPPUNIT_TEST( ctors_acquire );
2876 CPPUNIT_TEST( ctors_no_acquire );
2877 CPPUNIT_TEST( ctors_copy_ctor );
2878 CPPUNIT_TEST_SUITE_END();
2880 }; // class ctors
2882 class send_recv: public CppUnit::TestFixture
2884 public:
2885 // initialization
2886 void setUp( )
2890 void tearDown( )
2895 void send_recv1()
2897 //client sent two strings, and server received, check the order and value
2898 ServerSocketThread myServerThread;
2899 ClientSocketThread myClientThread;
2900 myServerThread.create( );
2901 myClientThread.create( );
2903 //wait until the thread terminate
2904 myClientThread.join( );
2905 myServerThread.join( );
2906 sal_Char myStr[30] = "";
2907 strcat( myStr, pTestString1 );
2908 strcat( myStr, pTestString2 );
2909 sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr );
2910 CPPUNIT_ASSERT_MESSAGE(" test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.",
2911 nRes == 0 );
2914 // error when recv
2915 void send_recv2()
2917 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2918 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 );
2919 ::osl::StreamSocket ssStreamConnection;
2920 sal_Char pReadBuffer[30] = "";
2922 ClientSocketThread myClientThread;
2923 myClientThread.create( );
2925 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
2927 asAcceptorSocket.bind( saLocalSocketAddr );
2928 asAcceptorSocket.listen( 1 );
2929 asAcceptorSocket.enableNonBlockingMode( sal_True );
2930 asAcceptorSocket.acceptConnection( ssStreamConnection );
2931 sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 );
2933 myClientThread.join( ) ;
2934 ssStreamConnection.close();
2935 asAcceptorSocket.close();
2936 CPPUNIT_ASSERT_MESSAGE(" test for send/recv, recv error!", nReadNumber == -1 );
2939 void write_read(sal_Int32 _nBufferSize, int _nValue)
2941 //client sent two strings, and server received, check the order and value
2942 WriteSocketThread myServerThread(_nBufferSize, _nValue);
2943 ReadSocketThread myClientThread(_nBufferSize, _nValue);
2944 myServerThread.create( );
2945 // thread_sleep( 1 );
2946 myClientThread.create( );
2948 //wait until the thread terminate
2949 myClientThread.join( );
2950 myServerThread.join( );
2952 //Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500
2953 // Proteon PRONET = 2046), so here test read 4000 bytes
2954 sal_Int32 nLength = myClientThread.getCount();
2955 bool bIsOk = myClientThread.isOk(); // check if the values are right.
2957 t_print("Length:=%d\n", nLength);
2958 t_print(" bIsOk:=%d\n", bIsOk);
2960 CPPUNIT_ASSERT_MESSAGE(" test for write/read values with two threads: send data from server, check readed data in client.",
2961 nLength == _nBufferSize && bIsOk == true);
2964 void write_read_001()
2966 write_read(50, 10);
2968 void write_read_002()
2970 write_read(1024, 20);
2972 void write_read_003()
2974 write_read(4000, 1);
2976 void write_read_004()
2978 write_read(8192, 3);
2981 CPPUNIT_TEST_SUITE( send_recv );
2982 CPPUNIT_TEST( write_read_001 );
2983 CPPUNIT_TEST( write_read_002 );
2984 CPPUNIT_TEST( write_read_003 );
2985 CPPUNIT_TEST( write_read_004 );
2986 CPPUNIT_TEST( send_recv1 );
2987 CPPUNIT_TEST( send_recv2 );
2988 // CPPUNIT_TEST( write_read );
2989 CPPUNIT_TEST_SUITE_END();
2990 }; // class send_recv
2992 class SendClientThread : public ClientSocketThread
2994 protected:
2996 void SAL_CALL run( )
2998 TimeValue *pTimeout;
2999 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
3000 pTimeout->Seconds = 5;
3001 pTimeout->Nanosec = 0;
3003 if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout ))
3005 sal_Int32 nWrite1 = csConnectorSocket.write( pTestString1, 11 ); // "test socket"
3007 sal_Int32 nWrite2 = csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
3008 thread_sleep( 2 );
3009 csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
3010 t_print("nWrite1 is %d, nWrite2 is %d\n", nWrite1, nWrite2 );
3011 //thread_sleep( 1 );
3013 else
3014 t_print("# SendClientThread: connect failed! \n");
3016 csConnectorSocket.close();
3017 free( pTimeout );
3022 class shutdown: public CppUnit::TestFixture
3024 public:
3025 // initialization
3026 void setUp( )
3030 void tearDown( )
3035 // similar to close_002
3036 void shutdown_001()
3038 #if defined(LINUX)
3039 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3040 AcceptorThread myAcceptorThread( asSocket, aHostIp1 );
3041 myAcceptorThread.create();
3043 thread_sleep( 1 );
3045 //when accepting, shutdown the socket, the thread will not block for accepting
3046 asSocket.shutdown();
3047 myAcceptorThread.join();
3049 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
3050 myAcceptorThread.isOK( ) == sal_True );
3051 #endif
3054 void shutdown_002()
3056 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3057 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9);
3058 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
3059 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
3060 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
3061 sal_Char pReadBuffer[40];
3062 SendClientThread mySendThread;
3063 mySendThread.create();
3065 asSocket.enableNonBlockingMode( sal_False );
3066 ::osl::StreamSocket ssConnectionSocket;
3067 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
3068 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
3070 /* set socket option SO_LINGER 0, so close immediatly */
3071 linger aLingerSet;
3072 sal_Int32 nBufferLen = sizeof( struct linger );
3073 aLingerSet.l_onoff = 0;
3074 aLingerSet.l_linger = 0;
3076 ssConnectionSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
3077 thread_sleep( 1 );
3078 //sal_uInt32 nRecv1 = 0;
3079 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
3081 //shutdown read after client the first send complete
3082 ssConnectionSocket.shutdown( osl_Socket_DirRead );
3084 sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 );
3085 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 );
3086 t_print("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", nRead1, nRead2, nRead3 );
3087 mySendThread.join();
3089 ssConnectionSocket.close();
3090 asSocket.close();
3092 /* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0,
3093 http://dbforums.com/arch/186/2002/12/586417
3094 While on Solaris, after shutdown(DirRead), all read will return 0
3096 #ifdef LINUX
3097 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
3098 nRead1 > 0 && nRead3 == 0 );
3099 #else
3100 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
3101 nRead1 > 0 && nRead2 == 0 && nRead3 == 0 );
3102 #endif
3106 void shutdown_003()
3108 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3109 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9);
3110 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
3111 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
3112 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
3113 sal_Char pReadBuffer[40];
3114 SendClientThread mySendThread;
3115 mySendThread.create();
3117 asSocket.enableNonBlockingMode( sal_False );
3118 ::osl::StreamSocket ssConnectionSocket;
3119 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
3120 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
3122 thread_sleep( 1 );
3123 //shutdown write after client the first send complete
3124 ssConnectionSocket.shutdown( osl_Socket_DirWrite );
3126 // recv should not shutdown
3127 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
3129 sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 );
3130 // still can read
3131 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 );
3132 t_print("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", nRead1, nWrite, nRead3 );
3133 mySendThread.join();
3134 ssConnectionSocket.close();
3135 asSocket.close();
3137 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not send(write).",
3138 nRead1 > 0 && nWrite == 0 && nRead3 > 0);
3142 CPPUNIT_TEST_SUITE( shutdown );
3143 CPPUNIT_TEST( shutdown_001 );
3144 CPPUNIT_TEST( shutdown_002 );
3145 CPPUNIT_TEST( shutdown_003 );
3146 CPPUNIT_TEST_SUITE_END();
3147 }; // class shutdown
3149 class isExceptionPending: public CppUnit::TestFixture
3151 public:
3152 void isExPending_001()
3154 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3155 TimeValue *pTimeout;
3156 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
3157 pTimeout->Seconds = 3;
3158 pTimeout->Nanosec = 0;
3159 sal_Bool bOk = asSocket.isExceptionPending( pTimeout );
3160 free( pTimeout );
3162 CPPUNIT_ASSERT_MESSAGE( "test for isExceptionPending.",
3163 bOk == sal_False );
3166 /**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/
3169 CPPUNIT_TEST_SUITE( isExceptionPending );
3170 CPPUNIT_TEST( isExPending_001 );
3171 CPPUNIT_TEST_SUITE_END();
3172 }; // class isExceptionPending
3174 // -----------------------------------------------------------------------------
3176 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::ctors, "osl_StreamSocket");
3177 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::send_recv, "osl_StreamSocket");
3178 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::shutdown, "osl_StreamSocket");
3179 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::isExceptionPending, "osl_StreamSocket");
3181 } // namespace osl_StreamSocket
3184 namespace osl_ConnectorSocket
3187 /** testing the method:
3188 ConnectorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
3189 oslProtocol Protocol = osl_Socket_ProtocolIp,
3190 oslSocketType Type = osl_Socket_TypeStream);
3193 class ctors : public CppUnit::TestFixture
3195 public:
3196 void ctors_001()
3198 /// Socket constructor.
3199 ::osl::ConnectorSocket csSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3201 CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the connector socket was created successfully.",
3202 osl_Socket_TypeStream == csSocket.getType( ) );
3205 CPPUNIT_TEST_SUITE( ctors );
3206 CPPUNIT_TEST( ctors_001 );
3207 CPPUNIT_TEST_SUITE_END();
3209 }; // class ctors
3211 /** testing the method:
3212 oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0);
3215 class connect : public CppUnit::TestFixture
3217 public:
3218 TimeValue *pTimeout;
3219 ::osl::AcceptorSocket asAcceptorSocket;
3220 ::osl::ConnectorSocket csConnectorSocket;
3223 // initialization
3224 void setUp( )
3226 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
3227 pTimeout->Seconds = 3;
3228 pTimeout->Nanosec = 0;
3229 // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
3232 void tearDown( )
3234 free( pTimeout );
3235 // sHandle = NULL;
3236 asAcceptorSocket.close( );
3237 csConnectorSocket.close( );
3241 void connect_001()
3243 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT2 );
3244 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT2 );
3245 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
3246 ::osl::StreamSocket ssConnection;
3248 /// launch server socket
3249 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
3250 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
3251 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
3252 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
3253 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
3255 //asAcceptorSocket.enableNonBlockingMode( sal_True );
3256 //oslSocketResult eResultAccept = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
3257 //CPPUNIT_ASSERT_MESSAGE( "accept failed.", osl_Socket_Ok == eResultAccept );
3258 /// launch client socket
3259 oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3260 CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_Ok == eResult );
3262 /// get peer information
3263 csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
3265 CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.",
3266 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) &&
3267 ( osl_Socket_Ok == eResult ));
3269 //non-blocking mode connect?
3270 void connect_002()
3272 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
3273 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
3274 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
3276 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
3277 asAcceptorSocket.enableNonBlockingMode( sal_True );
3278 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
3279 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
3280 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
3281 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
3283 csConnectorSocket.enableNonBlockingMode( sal_True );
3285 oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3286 CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_InProgress == eResult || osl_Socket_Ok == eResult );
3288 /// get peer information
3289 csConnectorSocket.getPeerAddr( saPeerSocketAddr );
3291 CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.",
3292 sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) ;
3294 // really an error or just delayed
3295 // how to design senarios that will return osl_Socket_Interrupted, osl_Socket_TimedOut
3296 void connect_003()
3298 ::osl::SocketAddr saTargetSocketAddr1( aHostIp1, IP_PORT_MYPORT3 );
3299 ::osl::SocketAddr saTargetSocketAddr2( aHostIpInval1, IP_PORT_MYPORT3 );
3301 csConnectorSocket.enableNonBlockingMode( sal_False );
3303 oslSocketResult eResult1 = csConnectorSocket.connect( saTargetSocketAddr1, pTimeout );
3304 oslSocketResult eResult2 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout );
3305 CloseSocketThread myCloseThread( csConnectorSocket );
3306 oslSocketResult eResult3 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout );
3307 myCloseThread.join();
3308 CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult1 &&
3309 osl_Socket_Error == eResult2 && osl_Socket_Error == eResult3 );
3313 // really an error in non-blocking mode
3314 void connect_004()
3316 ::osl::SocketAddr saTargetSocketAddr( aHostIpInval1, IP_PORT_MYPORT3 );
3318 csConnectorSocket.enableNonBlockingMode( sal_True );
3320 oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3321 CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult );
3323 /** here need a case: immediate connection, say in non-blocking mode connect return osl_Socket_Ok
3326 CPPUNIT_TEST_SUITE( connect );
3327 CPPUNIT_TEST( connect_001 );
3328 CPPUNIT_TEST( connect_002 );
3329 CPPUNIT_TEST( connect_003 );
3330 CPPUNIT_TEST( connect_004 );
3331 CPPUNIT_TEST_SUITE_END();
3333 }; // class connect
3336 // -----------------------------------------------------------------------------
3338 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::ctors, "osl_ConnectorSocket");
3339 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::connect, "osl_ConnectorSocket");
3341 } // namespace osl_ConnectorSocket
3345 namespace osl_AcceptorSocket
3348 /** testing the methods:
3349 inline AcceptorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
3350 oslProtocol Protocol = osl_Socket_ProtocolIp,
3351 oslSocketType Type = osl_Socket_TypeStream);
3354 class ctors : public CppUnit::TestFixture
3356 public:
3358 void ctors_001()
3360 /// Socket constructor.
3361 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3363 CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the acceptor socket was created successfully.",
3364 osl_Socket_TypeStream == asSocket.getType( ) );
3367 CPPUNIT_TEST_SUITE( ctors );
3368 CPPUNIT_TEST( ctors_001 );
3369 CPPUNIT_TEST_SUITE_END();
3371 }; // class ctors
3373 #if 0
3374 class operator_assign : public CppUnit::TestFixture
3376 public:
3378 void assign_001()
3380 #if defined(LINUX)
3381 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3382 ::osl::AcceptorSocket asSocketAssign( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3383 asSocket.setOption( osl_Socket_OptionReuseAddr, 1);
3384 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_MYPORT4 );
3385 asSocket.bind( saSocketAddr );
3387 AcceptorThread myAcceptorThread( asSocketAssign, aHostIp1 );
3388 myAcceptorThread.create();
3390 thread_sleep( 1 );
3391 //when accepting, assign another socket to the socket, the thread will not be closed, so is blocking
3392 asSocketAssign = asSocket;
3394 t_print("#asSocketAssign port number is %d\n", asSocketAssign.getLocalPort() );
3396 asSocketAssign.shutdown();
3397 myAcceptorThread.join();
3399 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
3400 myAcceptorThread.isOK() == sal_True );
3403 #endif /* LINUX */
3407 CPPUNIT_TEST_SUITE( operator_assign );
3408 CPPUNIT_TEST( assign_001 );
3409 CPPUNIT_TEST_SUITE_END();
3411 }; // class operator_assign
3412 #endif
3414 /** testing the method:
3415 inline sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1);
3416 inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection);
3417 inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr);
3420 class listen_accept : public CppUnit::TestFixture
3422 public:
3423 TimeValue *pTimeout;
3424 ::osl::AcceptorSocket asAcceptorSocket;
3425 ::osl::ConnectorSocket csConnectorSocket;
3428 // initialization
3429 void setUp( )
3431 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
3432 pTimeout->Seconds = 3;
3433 pTimeout->Nanosec = 0;
3434 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1);
3435 // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
3438 void tearDown( )
3440 free( pTimeout );
3441 // sHandle = NULL;
3442 asAcceptorSocket.close( );
3443 csConnectorSocket.close( );
3447 void listen_accept_001()
3449 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
3450 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
3451 ::osl::StreamSocket ssConnection;
3453 /// launch server socket
3454 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
3455 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
3456 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
3457 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
3458 asAcceptorSocket.enableNonBlockingMode( sal_True );
3460 /// launch client socket
3461 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3463 oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
3465 CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept.",
3466 ( osl_Socket_Ok == eResult ) );
3469 void listen_accept_002()
3471 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT4 );
3472 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT4 );
3473 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
3474 ::osl::StreamSocket ssConnection;
3476 /// launch server socket
3477 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
3478 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
3479 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
3480 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
3481 asAcceptorSocket.enableNonBlockingMode( sal_True );
3483 /// launch client socket
3484 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3486 oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection, saPeerSocketAddr); /// waiting for incoming connection...
3488 CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept, accept with peer address.",
3489 ( sal_True == bOK2 ) &&
3490 ( osl_Socket_Ok == eResult ) &&
3491 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) );
3494 void listen_accept_003()
3499 CPPUNIT_TEST_SUITE( listen_accept );
3500 CPPUNIT_TEST( listen_accept_001 );
3501 CPPUNIT_TEST( listen_accept_002 );
3502 CPPUNIT_TEST_SUITE_END();
3504 }; // class listen_accept
3507 // -----------------------------------------------------------------------------
3509 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::ctors, "osl_AcceptorSocket");
3510 //CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::operator_assign, "osl_AcceptorSocket");
3511 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::listen_accept, "osl_AcceptorSocket");
3513 } // namespace osl_AcceptorSocket
3516 namespace osl_DatagramSocket
3519 /** testing the methods:
3520 inline DatagramSocket(oslAddrFamily Family= osl_Socket_FamilyInet,
3521 oslProtocol Protocol= osl_Socket_ProtocolIp,
3522 oslSocketType Type= osl_Socket_TypeDgram);
3525 class ctors : public CppUnit::TestFixture
3527 public:
3529 void ctors_001()
3531 /// Socket constructor.
3532 ::osl::DatagramSocket dsSocket;
3534 CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the datagram socket was created successfully.",
3535 osl_Socket_TypeDgram == dsSocket.getType( ) );
3539 CPPUNIT_TEST_SUITE( ctors );
3540 CPPUNIT_TEST( ctors_001 );
3541 CPPUNIT_TEST_SUITE_END();
3543 }; // class ctors
3545 /**thread do sendTo, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml
3547 class TalkerThread : public Thread
3549 protected:
3550 ::osl::SocketAddr saTargetSocketAddr;
3551 ::osl::DatagramSocket dsSocket;
3553 void SAL_CALL run( )
3555 dsSocket.sendTo( saTargetSocketAddr, pTestString1, strlen( pTestString1 ) + 1 ); // "test socket"
3556 dsSocket.shutdown();
3559 void SAL_CALL onTerminated( )
3563 public:
3564 TalkerThread( ):
3565 saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT9 )
3569 ~TalkerThread( )
3571 if ( isRunning( ) )
3572 t_print("# error: TalkerThread not terminated normally.\n" );
3576 /**thread do listen, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml
3578 class ListenerThread : public Thread
3580 protected:
3581 ::osl::SocketAddr saTargetSocketAddr;
3582 ::osl::DatagramSocket dsSocket;
3584 void SAL_CALL run( )
3586 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT10 );
3587 dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
3588 if ( dsSocket.bind( saLocalSocketAddr ) == sal_False )
3590 t_print("DatagramSocket bind failed \n");
3591 return;
3593 //blocking mode: default
3594 sal_Int32 nRecv = dsSocket.recvFrom( pRecvBuffer, 30, &saTargetSocketAddr); //strlen( pTestString2 ) + 1
3595 t_print("After recvFrom, nRecv is %d\n", nRecv);
3598 void SAL_CALL onTerminated( )
3602 public:
3603 sal_Char pRecvBuffer[30];
3604 ListenerThread( ):
3605 saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT10 )
3607 pRecvBuffer[0] = '\0';
3610 ~ListenerThread( )
3612 if ( isRunning( ) )
3613 t_print("# error: ListenerThread not terminated normally.\n" );
3618 /** testing the methods:
3619 inline sal_Int32 DatagramSocket::recvFrom(void* pBuffer, sal_uInt32 BufferSize,
3620 SocketAddr* pSenderAddr, oslSocketMsgFlag Flag )
3621 inline sal_Int32 DatagramSocket::sendTo( const SocketAddr& ReceiverAddr,
3622 const void* pBuffer, sal_uInt32 BufferSize, oslSocketMsgFlag Flag )
3625 class sendTo_recvFrom : public CppUnit::TestFixture
3627 public:
3629 void sr_001()
3631 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 );
3632 ::osl::DatagramSocket dsSocket;
3633 dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
3634 dsSocket.bind( saLocalSocketAddr );
3636 sal_Char pReadBuffer[30];
3637 TalkerThread myTalkThread;
3638 myTalkThread.create();
3639 sal_Int32 nRecv = dsSocket.recvFrom( pReadBuffer, 30, &saLocalSocketAddr);
3640 myTalkThread.join();
3641 //t_print("#received buffer is %s# \n", pReadBuffer);
3643 sal_Bool bOk = ( strcmp(pReadBuffer, pTestString1) == 0 );
3645 CPPUNIT_ASSERT_MESSAGE( "test for sendTo/recvFrom function: create a talker thread and recvFrom in the main thread, check if the datagram socket can communicate successfully.",
3646 nRecv > 0 && bOk == sal_True );
3649 void sr_002()
3651 ::osl::SocketAddr saListenSocketAddr( aHostIp1, IP_PORT_MYPORT10 );
3652 ::osl::DatagramSocket dsSocket;
3654 //listener thread construct a DatagramSocket, recvFrom waiting for data, then main thread sendto data
3655 ListenerThread myListenThread;
3656 myListenThread.create();
3657 //to grantee the recvFrom is before sendTo
3658 thread_sleep( 1 );
3660 sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 );
3662 CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo failed: nSend <= 0.", nSend > 0);
3664 myListenThread.join();
3665 //t_print("#received buffer is %s# \n", myListenThread.pRecvBuffer);
3667 sal_Bool bOk = ( strcmp( myListenThread.pRecvBuffer, pTestString2) == 0 );
3669 CPPUNIT_ASSERT_MESSAGE( "test for sendTo/recvFrom function: create a listener thread and sendTo in the main thread, check if the datagram socket can communicate successfully.",
3670 bOk == sal_True );
3673 //sendTo error, return -1; recvFrom error, return -1
3674 void sr_003()
3676 ::osl::SocketAddr saListenSocketAddr( aHostIpInval1, IP_PORT_MYPORT10 );
3677 ::osl::DatagramSocket dsSocket;
3678 // Transport endpoint is not connected
3679 sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 );
3680 CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo should fail: nSend <= 0.",
3681 nSend == -1 );
3684 void sr_004()
3686 ::osl::SocketAddr saListenSocketAddr1( aHostIpInval1, IP_PORT_MYPORT10 );
3687 ::osl::SocketAddr saListenSocketAddr2( aHostIp2, IP_PORT_MYPORT10 );
3688 ::osl::DatagramSocket dsSocket;
3690 dsSocket.enableNonBlockingMode( sal_True );
3692 sal_Char pReadBuffer[30];
3693 //sal_Int32 nRecv1 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 );
3695 // will block ?
3696 CloseSocketThread myThread( dsSocket );
3697 myThread.create();
3698 sal_Int32 nRecv2 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 );
3699 myThread.join();
3700 //t_print("#nRecv1 is %d nRecv2 is %d\n", nRecv1, nRecv2 );
3701 CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo should fail: nSend <= 0.",
3702 nRecv2 == -1 );
3705 CPPUNIT_TEST_SUITE( sendTo_recvFrom );
3706 CPPUNIT_TEST( sr_001 );
3707 CPPUNIT_TEST( sr_002 );
3708 CPPUNIT_TEST( sr_003 );
3709 CPPUNIT_TEST( sr_004 );
3710 CPPUNIT_TEST_SUITE_END();
3712 }; // class sendTo_recvFrom
3714 // -----------------------------------------------------------------------------
3716 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DatagramSocket::ctors, "osl_DatagramSocket");
3717 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DatagramSocket::sendTo_recvFrom, "osl_DatagramSocket");
3719 } // namespace osl_DatagramSocket
3722 // -----------------------------------------------------------------------------
3724 // this macro creates an empty function, which will called by the RegisterAllFunctions()
3725 // to let the user the possibility to also register some functions by hand.
3726 NOADDITIONAL;