bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / osl / socket / osl_Socket.cxx
blob2c9e1ff58f9843ef77d739a08d5ed682867ef669
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 /** test coder preface:
21 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
22 if you are not including ws2_32.lib in makefile.mk, the including format will be like this:
24 .IF "$(OS)" == "WNT"
25 SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
26 SHL1STDLIBS += ws2_32.lib
27 .ENDIF
29 likewise on Solaris platform.
30 .IF "$(OS)" != "WNT"
31 SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
32 SHL1STDLIBS += -lsocket -ldl -lnsl
33 .ENDIF
35 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
36 category.
38 3. some fragment of Socket source implementation are lack of comment so it is hard for testers
39 guess what the exact functionality or usage of a member. Hope the Socket section's comment
40 will be added.
42 4. following functions are declared but not implemented:
43 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
46 // include files
48 #include <sal/types.h>
49 #include <osl_Socket_Const_orig.h>
51 #include <cppunit/TestFixture.h>
52 #include <cppunit/extensions/HelperMacros.h>
53 #include <cppunit/plugin/TestPlugIn.h>
55 /** test output if SILENT_TEST is 0
57 #if OSL_DEBUG_LEVEL > 0
58 # define SILENT_TEST 0
59 #else
60 # define SILENT_TEST 1
61 #endif
63 #if SILENT_TEST
64 # define t_print(...) { }
65 #else
66 # define t_print printf
67 #endif
69 /** convert UString and OUString to std::string
71 #define STD_STRING(s) (std::string((const char *)s.getStr()))
73 using namespace osl;
75 using ::rtl::OUString;
76 using ::rtl::OUStringToOString;
77 using ::rtl::OString;
79 // helper functions
81 /** compare two OUString.
83 inline sal_Bool compareUString( const ::rtl::OUString & ustr1, const ::rtl::OUString & ustr2 )
85 sal_Bool bOk = ustr1.equalsIgnoreAsciiCase( ustr2 );
87 return bOk;
90 /** compare a OUString and an ASCII string.
92 inline sal_Bool compareUString( const ::rtl::OUString & ustr, const sal_Char *astr )
94 ::rtl::OUString ustr2 = rtl::OUString::createFromAscii( astr );
95 sal_Bool bOk = ustr.equalsIgnoreAsciiCase( ustr2 );
97 return bOk;
100 /** compare two socket address.
102 inline sal_Bool compareSocketAddr( const ::osl::SocketAddr & addr1 , const ::osl::SocketAddr & addr2 )
104 return ( ( sal_True == compareUString( addr1.getHostname( 0 ), addr2.getHostname( 0 ) ) ) && ( addr2.getPort( ) == addr2.getPort( ) ) );
107 inline char * oustring2char( const ::rtl::OUString & str )
109 rtl::OString aString;
110 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
111 return (char *)aString.getStr( );
114 /** print a UNI_CODE String. And also print some comments of the string.
116 inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
118 #if SILENT_TEST
119 (void)str;
120 (void)msg;
121 #else
122 t_print("#%s #printUString_u# ", msg );
123 t_print("%s\n", oustring2char( str ) );
124 #endif
127 /** get the local host name.
128 mindy: gethostbyname( "localhost" ), on Linux, it returns the hostname in /etc/hosts + domain name,
129 if no entry in /etc/hosts, it returns "localhost" + domain name
131 inline ::rtl::OUString getHost()
133 struct hostent *hptr;
135 hptr = gethostbyname( "localhost" );
136 CPPUNIT_ASSERT_MESSAGE( "#In getHostname function, error on gethostbyname()", hptr != NULL );
137 ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name );
139 return aUString;
142 /** get the full host name of the current processor, such as "aegean.prc.sun.com" --mindyliu
144 inline ::rtl::OUString getThisHostname()
146 ::rtl::OUString aUString;
147 #ifdef WNT
148 struct hostent *hptr;
149 hptr = gethostbyname( "localhost" );
150 CPPUNIT_ASSERT_MESSAGE( "#In getHostname function, error on gethostbyname()", hptr != NULL );
151 aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name );
152 #else
153 char hostname[255];
154 CPPUNIT_ASSERT_MESSAGE( "#Error: gethostname failed.", gethostname(hostname, 255) == 0 );
156 struct hostent *hptr;
157 //first search /ets/hosts, then search from dns
158 hptr = gethostbyname( hostname);
159 if ( hptr != NULL )
161 strcpy( hostname, hptr->h_name );
164 t_print("hostname is %s \n", hostname );
165 aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hostname );
166 #endif
167 return aUString;
170 /** get IP by name, search /etc/hosts first, then search from dns, fail return OUString("")
172 inline ::rtl::OUString getIPbyName( rtl::OString const& str_name )
174 ::rtl::OUString aUString;
175 struct hostent *hptr;
176 //first search /ets/hosts, then search from dns
177 hptr = gethostbyname( str_name.getStr());
178 if ( hptr != NULL )
180 struct in_addr ** addrptr;
181 addrptr = (struct in_addr **) hptr->h_addr_list ;
182 //if there are more than one IPs on the same machine, we select one
183 for (; *addrptr; addrptr++)
185 t_print("#Local IP Address: %s\n", inet_ntoa(**addrptr));
186 aUString = ::rtl::OUString::createFromAscii( (sal_Char *) (inet_ntoa(**addrptr)) );
189 return aUString;
192 /** get local ethernet IP
194 inline ::rtl::OUString getLocalIP( )
196 char hostname[255];
197 gethostname(hostname, 255);
199 return getIPbyName( hostname );
202 /** construct error message
204 inline ::rtl::OUString outputError( const ::rtl::OUString & returnVal, const ::rtl::OUString & rightVal, const sal_Char * msg = "")
206 ::rtl::OUString aUString;
207 if ( returnVal.equals( rightVal ) )
208 return aUString;
209 aUString += ::rtl::OUString::createFromAscii(msg);
210 aUString += ::rtl::OUString(": the returned value is '");
211 aUString += returnVal;
212 aUString += ::rtl::OUString("', but the value should be '");
213 aUString += rightVal;
214 aUString += ::rtl::OUString("'.");
215 return aUString;
218 /** wait _nSec seconds.
220 void thread_sleep( sal_Int32 _nSec )
222 /// print statement in thread process must use fflush() to force display.
223 t_print("# wait %d seconds. ", (int) _nSec );
224 fflush(stdout);
226 #ifdef WNT //Windows
227 Sleep( _nSec * 100 );
228 #endif
229 #if ( defined UNX ) //Unix
230 usleep(_nSec * 100000);
231 #endif
232 t_print("# done\n" );
235 /** print Boolean value.
237 inline void printBool( sal_Bool bOk )
239 #if SILENT_TEST
240 (void)bOk;
241 #else
242 t_print("#printBool# " );
243 t_print ("%s", (sal_True == bOk) ? "YES!\n" : "NO!\n");
244 #endif
247 /** print content of a ByteSequence.
249 inline void printByteSequence_IP( const ::rtl::ByteSequence & bsByteSeq, sal_Int32 nLen )
251 #if SILENT_TEST
252 (void)bsByteSeq;
253 (void)nLen;
254 #else
255 t_print("#ByteSequence is: " );
256 for ( int i = 0; i < nLen; i++ ){
257 if ( bsByteSeq[i] < 0 )
258 t_print("%d ", 256 + bsByteSeq[i] );
259 else
260 t_print("%d ", bsByteSeq[i] );
262 t_print(" .\n" );
263 #endif
266 /** convert an IP which is stored as a UString format to a ByteSequence array for later use.
268 inline ::rtl::ByteSequence UStringIPToByteSequence( ::rtl::OUString aUStr )
271 rtl::OString aString = ::rtl::OUStringToOString( aUStr, RTL_TEXTENCODING_ASCII_US );
272 const sal_Char *pChar = aString.getStr( ) ;
273 sal_Char tmpBuffer[4];
274 sal_Int32 nCharCounter = 0;
275 ::rtl::ByteSequence bsByteSequence( IP_VER );
276 sal_Int32 nByteSeqCounter = 0;
278 for ( int i = 0; i < aString.getLength( ) + 1 ; i++ )
280 if ( ( *pChar != '.' ) && ( i !=aString.getLength( ) ) )
281 tmpBuffer[nCharCounter++] = *pChar;
282 else
284 tmpBuffer[nCharCounter] = '\0';
285 nCharCounter = 0;
286 bsByteSequence[nByteSeqCounter++] = static_cast<sal_Int8>( atoi( tmpBuffer ) );
288 pChar++;
290 return bsByteSequence;
293 /** print a socket result name.
295 inline void printSocketResult( oslSocketResult eResult )
297 t_print("#printSocketResult# " );
298 if (!eResult)
299 switch (eResult)
301 case osl_Socket_Ok:
302 t_print("client connected\n");
303 break;
304 case osl_Socket_Error:
305 t_print("got an error ... exiting\r\n\r\n" );
306 break;
307 case osl_Socket_TimedOut:
308 t_print("timeout\n");
309 break;
311 case osl_Socket_FORCE_EQUAL_SIZE:
312 t_print("FORCE EQUAL SIZE\n");
313 break;
314 case osl_Socket_InProgress:
315 t_print("In Progress\n");
316 break;
317 case osl_Socket_Interrupted:
318 t_print("Interrupted\n");
319 break;
323 /** Client Socket Thread, served as a temp little client to communicate with server.
325 class ClientSocketThread : public Thread
327 protected:
328 oslThreadIdentifier m_id;
329 ::osl::SocketAddr saTargetSocketAddr;
330 ::osl::ConnectorSocket csConnectorSocket;
332 void SAL_CALL run( )
334 TimeValue *pTimeout;
335 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
336 pTimeout->Seconds = 5;
337 pTimeout->Nanosec = 0;
339 /// if the thread should terminate, schedule return false
340 //while ( schedule( ) == sal_True )
342 if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout ))
344 csConnectorSocket.send( pTestString1, 11 ); // "test socket"
345 csConnectorSocket.send( pTestString2, 10);
347 else
348 t_print("# ClientSocketThread: connect failed! \n");
349 // terminate();
351 csConnectorSocket.close();
352 free( pTimeout );
355 void SAL_CALL onTerminated( )
357 //t_print("# normally terminate this thread %d!\n", m_id );
360 public:
361 ClientSocketThread( ):
362 saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT9 ),
363 csConnectorSocket( )
365 m_id = getIdentifier( );
366 //t_print("# successfully create this client thread %d!\n", m_id );
369 ~ClientSocketThread( )
371 if ( isRunning( ) )
372 t_print("# error: client thread not terminated.\n" );
377 /** Server Socket Thread, served as a temp little server to communicate with client.
379 class ServerSocketThread : public Thread
381 protected:
382 oslThreadIdentifier m_id;
384 void SAL_CALL run( )
386 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
387 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 );
388 ::osl::StreamSocket ssStreamConnection;
390 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
391 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
392 while ( schedule( ) == sal_True )
394 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
395 if ( sal_True != bOK1 )
397 t_print("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ;
398 break;
400 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
401 if ( sal_True != bOK2 )
403 t_print("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ;
404 break;
407 asAcceptorSocket.enableNonBlockingMode( sal_False );
409 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
410 if (eResult != osl_Socket_Ok )
412 t_print("ServerSocketThread: acceptConnection failed! \n");
413 break;
415 sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 );
416 sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 );
417 pReadBuffer[nReadNumber1 + nReadNumber2] = '\0';
418 //t_print("# read buffer content: %s\n", pReadBuffer );
419 break;
421 ssStreamConnection.close();
422 asAcceptorSocket.close();
426 void SAL_CALL onTerminated( )
428 //t_print("# normally terminate this server thread %d!\n", m_id );
431 public:
432 // public to check if data transmition is OK
433 sal_Char pReadBuffer[30];
434 ServerSocketThread( )
436 m_id = getIdentifier( );
437 //t_print("# successfully create this server thread %d!\n", m_id );
440 ~ServerSocketThread( )
442 if ( isRunning( ) )
443 t_print("# error: server thread not terminated.\n" );
447 // Helper functions, to create buffers, check buffers
448 class ValueCheckProvider
450 bool m_bFoundFailure;
451 char *m_pBuffer;
452 sal_Int32 m_nBufferSize;
454 public:
455 ValueCheckProvider()
457 m_bFoundFailure(false),
458 m_pBuffer(NULL),
459 m_nBufferSize(0)
463 bool isFailure() {return m_bFoundFailure;}
465 const char* getBuffer() {return m_pBuffer;}
466 char* getWriteBuffer() {return m_pBuffer;}
468 sal_Int32 getBufferSize() {return m_nBufferSize;}
470 bool checkValues(sal_Int32 _nLength, int _nValue)
472 m_bFoundFailure = false;
473 for(sal_Int32 i=0;i<_nLength;i++)
475 if (m_pBuffer[i] != _nValue)
477 m_bFoundFailure = true;
480 return m_bFoundFailure;
483 void createBuffer(sal_Int32 _nLength, int _nValue)
485 m_nBufferSize = _nLength;
486 m_pBuffer = (char*) malloc(m_nBufferSize);
487 if (m_pBuffer)
489 memset(m_pBuffer, _nValue, m_nBufferSize);
493 void freeBuffer()
495 if (m_pBuffer) free(m_pBuffer);
500 /** Client Socket Thread, served as a temp little client to communicate with server.
503 class ReadSocketThread : public Thread
505 int m_nValue;
506 ValueCheckProvider m_aValues;
508 protected:
509 oslThreadIdentifier m_id;
510 ::osl::SocketAddr saTargetSocketAddr;
511 ::osl::ConnectorSocket csConnectorSocket;
513 void SAL_CALL run( )
515 TimeValue *pTimeout;
516 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
517 pTimeout->Seconds = 5;
518 pTimeout->Nanosec = 0;
520 /// if the thread should terminate, schedule return false
521 //while ( schedule( ) == sal_True )
523 if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout ))
525 sal_Int32 nReadCount = csConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() );
526 m_aValues.checkValues(nReadCount, m_nValue);
528 else
530 t_print("# ReadSocketThread: connect failed! \n");
532 // terminate();
534 //remove this line for deadlock on solaris( margritte.germany )
535 csConnectorSocket.close();
536 free( pTimeout );
539 void SAL_CALL onTerminated( )
541 //t_print("# normally terminate this thread %d!\n", m_id );
544 public:
545 sal_Int32 getCount() {return m_aValues.getBufferSize();}
546 bool isOk() {return m_aValues.isFailure() == true ? false : true;}
548 ReadSocketThread(sal_Int32 _nBufferSize, int _nValue )
550 m_nValue( _nValue ),
551 saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT10 ),
552 csConnectorSocket( )
554 m_id = getIdentifier( );
555 //t_print("# successfully create this client thread %d!\n", m_id );
556 m_aValues.createBuffer(_nBufferSize, 0);
559 ~ReadSocketThread( )
561 if ( isRunning( ) )
562 t_print("# error: client thread not terminated.\n" );
563 m_aValues.freeBuffer();
568 /** Server Socket Thread, write a file which is large
570 class WriteSocketThread : public Thread
572 ValueCheckProvider m_aValues;
574 protected:
575 oslThreadIdentifier m_id;
577 void SAL_CALL run( )
579 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
580 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT10 );
581 ::osl::StreamSocket ssStreamConnection;
583 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
584 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
586 /// if the thread should terminate, schedule return false
587 while ( schedule( ) == sal_True )
589 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
590 if ( sal_True != bOK1 )
592 t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
593 break;
595 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
596 if ( sal_True != bOK2 )
598 t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
599 break;
601 // blocking mode, if read/recv failed, block until success
602 asAcceptorSocket.enableNonBlockingMode( sal_False);
604 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
605 if (eResult != osl_Socket_Ok )
607 t_print("WriteSocketThread: acceptConnection failed! \n");
608 break;
611 ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
612 break;
614 ssStreamConnection.close();
615 asAcceptorSocket.close();
618 void SAL_CALL onTerminated( )
620 //t_print("# normally terminate this server thread %d!\n", m_id );
623 public:
624 // public to check if data transmition is OK
625 WriteSocketThread(sal_Int32 _nBufferSize, int _nValue )
627 m_id = getIdentifier( );
628 //t_print("# successfully create this server thread %d!\n", m_id );
630 m_aValues.createBuffer(_nBufferSize, _nValue);
633 ~WriteSocketThread( )
635 if ( isRunning( ) )
636 t_print("# error: server thread not terminated.\n" );
637 m_aValues.freeBuffer();
642 // just used to test socket::close() when accepting
643 class AcceptorThread : public Thread
645 ::osl::AcceptorSocket asAcceptorSocket;
646 ::rtl::OUString aHostIP;
647 sal_Bool bOK;
648 protected:
649 void SAL_CALL run( )
651 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
652 ::osl::StreamSocket ssStreamConnection;
654 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
655 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
656 if ( sal_True != bOK1 )
658 t_print("# AcceptorSocket bind address failed.\n" ) ;
659 return;
661 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
662 if ( sal_True != bOK2 )
664 t_print("# AcceptorSocket listen address failed.\n" ) ;
665 return;
668 asAcceptorSocket.enableNonBlockingMode( sal_False );
670 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
671 if (eResult != osl_Socket_Ok )
673 bOK = sal_True;
674 t_print("AcceptorThread: acceptConnection failed! \n");
677 public:
678 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString & aBindIP )
679 : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
681 bOK = sal_False;
684 sal_Bool isOK() { return bOK; }
686 ~AcceptorThread( )
688 if ( isRunning( ) )
690 asAcceptorSocket.shutdown();
691 t_print("# error: Acceptor thread not terminated.\n" );
696 class CloseSocketThread : public Thread
698 ::osl::Socket m_sSocket;
699 protected:
700 void SAL_CALL run( )
702 thread_sleep( 1 );
703 m_sSocket.close( );
705 public:
706 CloseSocketThread(::osl::Socket & sSocket )
707 : m_sSocket( sSocket )
711 ~CloseSocketThread( )
713 if ( isRunning( ) )
715 t_print("# error: CloseSocketThread not terminated.\n" );
720 // tests cases begins here
722 namespace osl_SocketAddr
725 /** testing the methods:
726 inline SocketAddr();
727 inline SocketAddr(const SocketAddr& Addr);
728 inline SocketAddr(const oslSocketAddr , __osl_socket_NoCopy nocopy );
729 inline SocketAddr(oslSocketAddr Addr);
730 inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort );
733 class ctors : public CppUnit::TestFixture
735 public:
737 void ctors_none()
739 /// SocketAddr constructor.
740 ::osl::SocketAddr saSocketAddr;
742 // oslSocketResult aResult;
743 // rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult);
745 // rtl::OUString suHost2 = getThisHostname();
747 CPPUNIT_ASSERT_MESSAGE("test for none parameter constructor function: check if the socket address was created successfully",
748 sal_True == saSocketAddr.is( ) );
751 void ctors_none_000()
753 /// SocketAddr constructor.
754 ::osl::SocketAddr saSocketAddr;
756 oslSocketResult aResult;
757 rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult);
758 rtl::OUString suHost2 = getThisHostname();
760 sal_Bool bOk = compareUString(suHost, suHost2);
762 rtl::OUString suError ("Host names should be the same. From SocketAddr.getLocalHostname() it is'");
763 suError += suHost;
764 suError += rtl::OUString("', from getThisHostname() it is '");
765 suError += suHost2;
766 suError += rtl::OUString("'.");
768 CPPUNIT_ASSERT_MESSAGE(STD_STRING(suError), sal_True == bOk);
771 void ctors_copy()
773 /// SocketAddr copy constructor.
774 ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 );
775 ::osl::SocketAddr saCopySocketAddr( saSocketAddr );
777 sal_Int32 nPort = saCopySocketAddr.getPort( );
779 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy constructor function: copy constructor, do an action of copy construction then check the port with original set.",
780 ( sal_True == saCopySocketAddr.is( ) ) && ( nPort == IP_PORT_HTTP1 ) );
783 void ctors_copy_no_001()
786 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 );
787 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
789 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
791 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
793 pSocketAddrCopy->setPort( IP_PORT_HTTP2 );
794 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.",
795 pSocketAddr->getPort( ) == IP_PORT_HTTP2 );
797 delete pSocketAddrCopy;
798 // LLA: don't do this also: delete pSocketAddr;
801 void ctors_copy_no_002()
803 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 );
804 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
805 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
806 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
808 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.",
809 pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) );
811 delete pSocketAddrCopy;
814 void ctors_copy_handle_001()
816 ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 );
817 ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) );
819 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, get its port to check copy effect.",
820 saSocketAddrCopy.getPort( ) == IP_PORT_HTTP1 );
823 void ctors_copy_handle_002()
825 ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 );
826 ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) );
827 saSocketAddrCopy.setPort( IP_PORT_HTTP2 );
829 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, the original one should not be changed.",
830 saSocketAddr.getPort( ) != IP_PORT_HTTP2 );
833 void ctors_hostname_port_001()
835 /// tcpip-specific constructor.
836 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
837 printUString(saSocketAddr.getHostname( ), "ctors_hostname_port_001:getHostname");
839 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specificy constructor function: do a constructor using tcpip spec, check the result.",
840 saSocketAddr.is( ) == sal_True &&
841 ( saSocketAddr.getPort( ) == IP_PORT_FTP )/*&&
842 ( sal_True == compareUString( saSocketAddr.getHostname( ), aHostName1 ) ) */);
845 //same as is_002
846 void ctors_hostname_port_002()
848 /// tcpip-specific constructor.
849 ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT2 );
851 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specificy constructor function: using an invalid IP address, the socketaddr ctors should fail", sal_False == saSocketAddr.is( ));
853 CPPUNIT_TEST_SUITE( ctors );
854 CPPUNIT_TEST( ctors_none );
855 CPPUNIT_TEST( ctors_none_000 );
856 CPPUNIT_TEST( ctors_copy );
857 CPPUNIT_TEST( ctors_copy_no_001 );
858 CPPUNIT_TEST( ctors_copy_no_002 );
859 CPPUNIT_TEST( ctors_copy_handle_001 );
860 CPPUNIT_TEST( ctors_copy_handle_002 );
861 CPPUNIT_TEST( ctors_hostname_port_001 );
862 CPPUNIT_TEST( ctors_hostname_port_002 );
863 CPPUNIT_TEST_SUITE_END();
865 }; // class ctors
867 /** testing the method:
868 inline sal_Bool is() const;
871 class is : public CppUnit::TestFixture
873 public:
874 void is_001()
876 ::osl::SocketAddr saSocketAddr;
878 CPPUNIT_ASSERT_MESSAGE("test for is() function: create an unknown type socket, it should be True when call is.",
879 sal_True == saSocketAddr.is( ) );
881 // refer to setPort_003()
882 void is_002()
884 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_INVAL );
886 CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid port number",
887 sal_True == saSocketAddr.is( ) );
890 void is_003()
892 ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT );
894 CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid Ip number",
895 sal_True != saSocketAddr.is( ) );
898 CPPUNIT_TEST_SUITE( is );
899 CPPUNIT_TEST( is_001 );
900 CPPUNIT_TEST( is_002 );
901 CPPUNIT_TEST( is_003 );
902 CPPUNIT_TEST_SUITE_END();
904 }; // 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( STD_STRING(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 ("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( STD_STRING(suError), sal_True == bOK );
976 CPPUNIT_TEST_SUITE( getHostname );
977 CPPUNIT_TEST( getHostname_001 );
978 CPPUNIT_TEST( getHostname_002 );
979 CPPUNIT_TEST_SUITE_END();
981 }; // class getHostname
983 /** testing the method:
984 inline sal_Int32 SAL_CALL getPort() const;
987 class getPort : public CppUnit::TestFixture
989 public:
990 void getPort_001()
992 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
994 CPPUNIT_ASSERT_MESSAGE( "test for getPort() function: get a normal port number.",
995 IP_PORT_FTP == saSocketAddr.getPort( ) );
998 void getPort_002()
1000 ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_INVAL );
1002 //t_print("#getPort_002: Port number is %d \n", saSocketAddr.getPort( ));
1004 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).",
1005 saSocketAddr.getPort( )>=1 && saSocketAddr.getPort( ) <= 65535 );
1007 //two cases will return OSL_INVALID_PORT: 1. not valid SocketAddr
1008 //2. SocketAddr family is not osl_Socket_FamilyInet, but case 2 could not be constructed
1009 void getPort_003()
1011 ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT );
1013 CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid IP to a SocketAddr, get the port to see returned value. ",
1014 saSocketAddr.getPort( ) == OSL_INVALID_PORT );
1017 CPPUNIT_TEST_SUITE( getPort );
1018 CPPUNIT_TEST( getPort_001 );
1019 CPPUNIT_TEST( getPort_002 );
1020 CPPUNIT_TEST( getPort_003 );
1021 CPPUNIT_TEST_SUITE_END( );
1023 }; // class getPort
1025 /** testing the method:
1026 inline sal_Bool SAL_CALL setPort( sal_Int32 nPort );
1027 rfc1413.txt: TCP port numbers are from 1-65535
1028 rfc1700.txt: 0/tcp Reserved ; 0/udp Reserved
1031 class setPort : public CppUnit::TestFixture
1033 public:
1034 void setPort_001()
1036 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
1037 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_TELNET );
1039 CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: modify a port number setting, and check it.",
1040 ( sal_True == bOK ) &&
1041 ( IP_PORT_TELNET == saSocketAddr.getPort( ) ) );
1044 /** 0 to 1024 is known as the reserved port range (traditionally only root can assign programs to ports in
1045 this range) and the ephemeral port range from 1025 to 65535.
1046 As many of you programmers will know, when you specify the source port of 0 when you connect to a host,
1047 the OS automatically reassigns the port number to high numbered ephemeral port. The same happens if you
1048 try to bind a listening socket to port 0.
1049 http://www.securiteam.com/securityreviews/5XP0Q2AAKS.html
1050 another: http://www.muq.org/~cynbe/muq/mufref_564.html
1052 void setPort_002()
1054 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
1055 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_ZERO );
1057 oslSocket sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1058 ::osl::Socket sSocket(sHandle);
1059 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 );//sal_True);
1060 sal_Bool bOK1 = sSocket.bind( saSocketAddr );
1061 CPPUNIT_ASSERT_MESSAGE( "bind SocketAddr failed", bOK1 == sal_True );
1063 sal_Int32 newPort = sSocket.getLocalPort();
1064 //t_print("#new port is %d\n", newPort );
1066 CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: port number should be in 1 ~ 65535, set port 0, it should be converted to a port number between 1024~65535.",
1067 ( 1024 <= newPort ) && ( 65535 >= newPort ) && ( bOK == sal_True ) );
1071 void setPort_003()
1073 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP);
1074 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_INVAL );
1075 //on Linux, getPort return 34463
1076 //t_print("#Port number is %d \n", saSocketAddr.getPort( ));
1078 CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an address with invalid port. it should return error or convert it to a valid port.",
1079 ( ( 1 <= saSocketAddr.getPort( ) ) && ( 65535 >= saSocketAddr.getPort( ) ) &&( bOK == sal_True ) ) ||
1080 bOK == sal_False);
1083 /* this is not a inet-addr => can't set port */
1084 void setPort_004()
1086 ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_FTP);
1087 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_MYPORT );
1089 CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an invalid address with valid port. it should return error.",
1090 bOK == sal_False);
1093 CPPUNIT_TEST_SUITE( setPort );
1094 CPPUNIT_TEST( setPort_001 );
1095 CPPUNIT_TEST( setPort_002 );
1096 CPPUNIT_TEST( setPort_003 );
1097 CPPUNIT_TEST( setPort_004 );
1098 CPPUNIT_TEST_SUITE_END( );
1100 }; // class setPort
1102 /** tester comment:
1104 In the following two functions, it use ::rtl::ByteSequence as an intermediate storage for address,
1105 the ByteSequence object can hold sal_Int8 arrays, which is raged [-127, 127], in case of IP addr
1106 that is greater than 127, say 129.158.217.202, it will stored as -127, -98, -39, -54, it is unique
1107 in the range of sal_Int8, but lack of readability.
1108 so may be a sal_uInt8 array is better.
1111 /** testing the method:
1112 inline sal_Bool SAL_CALL setAddr( const ::rtl::ByteSequence & address );
1115 class setAddr : public CppUnit::TestFixture
1117 public:
1118 void setAddr_001()
1120 ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_FTP );
1121 saSocketAddr.setAddr( UStringIPToByteSequence( aHostIp1 ) );
1122 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 );
1123 sal_Bool bOK = sal_False;
1125 if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) && ( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1126 bOK = sal_True;
1128 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 ",
1129 sal_True == bOK );
1132 CPPUNIT_TEST_SUITE( setAddr );
1133 CPPUNIT_TEST( setAddr_001 );
1134 CPPUNIT_TEST_SUITE_END( );
1136 }; // class setAddr
1138 /** testing the method:
1139 inline ::rtl::ByteSequence SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const;
1142 class getAddr : public CppUnit::TestFixture
1144 public:
1145 void getAddr_001()
1147 oslSocketResult SocketResult;
1148 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
1149 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( &SocketResult );
1151 sal_Bool bOK = sal_False;
1153 if ( ( osl_Socket_Ok == SocketResult ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1154 bOK = sal_True;
1156 CPPUNIT_ASSERT_MESSAGE( "test for getAddr() function: construct a socketaddr with IP assigned, get the address to check correctness.Caught unknown exception on (Win32)",
1157 sal_True == bOK && SocketResult == osl_Socket_Ok);
1160 CPPUNIT_TEST_SUITE( getAddr );
1161 CPPUNIT_TEST( getAddr_001 );
1162 CPPUNIT_TEST_SUITE_END( );
1164 }; // class getAddr
1166 /** testing the methods:
1167 inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr);
1168 inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr);
1169 inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy );
1170 inline sal_Bool SAL_CALL operator== (oslSocketAddr Addr) const;
1171 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; /// not implemented.
1174 class operator_equal : public CppUnit::TestFixture
1176 public:
1177 void operator_equal_001()
1179 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1180 ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1182 saSocketAddrEqual = saSocketAddr;
1183 sal_Bool bOK = sal_False;
1184 ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 );
1186 if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1187 bOK = sal_True;
1189 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: use operator= to assign Ip1 to Ip2, check its modification.",
1190 sal_True == bOK );
1193 void operator_equal_002()
1195 ::osl::SocketAddr saSocketAddr( aHostIp3, IP_PORT_TELNET);
1196 ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1198 saSocketAddrEqual = saSocketAddr;
1199 CPPUNIT_ASSERT_MESSAGE( "after assign, the assigned SocketAddr is not same as the original Addr",
1200 IP_PORT_TELNET == saSocketAddrEqual.getPort( ) );
1201 saSocketAddrEqual.setPort( IP_PORT_MYPORT3 );
1202 saSocketAddr.setPort( IP_PORT_HTTP2 );
1204 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.",
1205 IP_PORT_MYPORT3 == saSocketAddrEqual.getPort( ) );
1208 void operator_equal_const_001()
1210 const ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1211 ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1213 saSocketAddrEqual = saSocketAddr;
1214 sal_Bool bOK = sal_False;
1215 ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 );
1217 if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1218 bOK = sal_True;
1220 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: use operator= const to assign Ip1 to Ip2, verify the change on the second one.",
1221 sal_True == bOK );
1224 void operator_equal_const_002()
1226 const ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1227 ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1229 saSocketAddrEqual = saSocketAddr;
1230 saSocketAddrEqual.setPort( IP_PORT_HTTP1 );
1232 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.",
1233 IP_PORT_HTTP1 != saSocketAddr.getPort( ) );
1236 void operator_equal_assign_001()
1238 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostIp1, IP_PORT_TELNET );
1239 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
1240 ::osl::SocketAddr* pSocketAddrAssign = new ::osl::SocketAddr( aHostIp2, IP_PORT_FTP );
1241 oslSocketAddr poslSocketAddr = pSocketAddr->getHandle( );
1242 //if( m_handle ) osl_destroySocketAddr( m_handle ); so pSocketAddrAssign had been destroyed and then point to pSocketAddr
1243 pSocketAddrAssign->assign(poslSocketAddr, SAL_NO_COPY);
1245 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.",
1246 pSocketAddrAssign->getPort( ) == IP_PORT_TELNET );
1248 delete pSocketAddrAssign;
1251 void operator_is_equal_001()
1253 ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1254 ::osl::SocketAddr saSocketAddrequal( aHostIp1, IP_PORT_TELNET );
1256 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two identical Address.",
1257 sal_True == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) );
1260 void operator_is_equal_002()
1262 ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_FTP);
1263 ::osl::SocketAddr saSocketAddrequal( aHostIp1, IP_PORT_TELNET );
1265 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two different Address.",
1266 sal_False == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) );
1269 CPPUNIT_TEST_SUITE( operator_equal );
1270 CPPUNIT_TEST( operator_equal_001 );
1271 CPPUNIT_TEST( operator_equal_002 );
1272 CPPUNIT_TEST( operator_equal_const_001 );
1273 CPPUNIT_TEST( operator_equal_const_002 );
1274 CPPUNIT_TEST( operator_equal_assign_001 );
1275 CPPUNIT_TEST( operator_is_equal_001 );
1276 CPPUNIT_TEST( operator_is_equal_002 );
1277 CPPUNIT_TEST_SUITE_END( );
1279 }; // class operator_equal
1281 /** testing the method:
1282 inline oslSocketAddr SAL_CALL getHandle() const;
1285 class getSocketAddrHandle : public CppUnit::TestFixture
1287 public:
1289 void getSocketAddrHandle_001()
1291 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 );
1292 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
1293 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
1294 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
1296 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.",
1297 pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) );
1299 delete pSocketAddrCopy;
1302 void getSocketAddrHandle_002()
1304 ::osl::SocketAddr saSocketAddr( aHostName3, IP_PORT_MYPORT4 );
1305 oslSocketAddr poslSocketAddr = saSocketAddr.getHandle( );
1307 sal_Bool bOK = ( saSocketAddr == poslSocketAddr );
1308 //t_print("getSocketAddrHandle_002\n");
1309 CPPUNIT_ASSERT_MESSAGE( "test for getHandle() function: use getHandle() function as an intermediate way to create identical address.",
1310 sal_True == bOK );
1313 CPPUNIT_TEST_SUITE( getSocketAddrHandle );
1314 CPPUNIT_TEST( getSocketAddrHandle_001 );
1315 CPPUNIT_TEST( getSocketAddrHandle_002 );
1316 CPPUNIT_TEST_SUITE_END( );
1318 }; // class getSocketAddrHandle
1320 /** testing the method:
1321 static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0);
1324 class getLocalHostname : public CppUnit::TestFixture
1326 public:
1327 /* the process of getLocalHostname: 1.gethostname (same as /bin/hostname) returned name A
1328 2. search A in /etc/hosts, if there is an alias name is A, return the name in the same row
1331 void getLocalHostname_000()
1333 // _osl_getFullQualifiedDomainName( );
1334 oslSocketResult aResult = osl_Socket_Error;
1335 rtl::OUString suHostname = osl::SocketAddr::getLocalHostname(&aResult);
1336 CPPUNIT_ASSERT_MESSAGE("getLocalHostname failed", aResult == osl_Socket_Ok);
1339 void getLocalHostname_001()
1341 oslSocketResult *pResult = NULL;
1342 //printSocketResult(*pResult);
1343 ::rtl::OUString suResult = ::osl::SocketAddr::getLocalHostname( pResult );
1345 // LLA: IMHO localhost, or hostname by itself should be ok.
1346 rtl::OUString suThisHost = getThisHostname( );
1347 bool bOk = false;
1348 if ( suThisHost == "localhost" )
1350 bOk = true;
1352 else
1354 if (suThisHost.equals(suResult))
1356 bOk = true;
1360 ::rtl::OUString suError;
1361 suError = outputError(suResult, getThisHostname( ), "test for getLocalHostname() function");
1363 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError), bOk == true );
1366 CPPUNIT_TEST_SUITE( getLocalHostname );
1367 CPPUNIT_TEST( getLocalHostname_000 );
1368 CPPUNIT_TEST( getLocalHostname_001 );
1369 CPPUNIT_TEST_SUITE_END( );
1371 }; // class getLocalHostname
1373 /** testing the method:
1374 static inline void SAL_CALL resolveHostname( const ::rtl::OUString & strHostName , SocketAddr & Addr );
1377 class resolveHostname : public CppUnit::TestFixture
1379 public:
1380 void resolveHostname_001()
1382 ::osl::SocketAddr saSocketAddr;
1383 ::osl::SocketAddr::resolveHostname( aHostIp1, saSocketAddr );
1384 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 );
1385 sal_Bool bOK = sal_False;
1387 if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1388 bOK = sal_True;
1390 CPPUNIT_ASSERT_MESSAGE( "test for resolveHostname() function: try to resolve localhost to 127.0.0.1.",
1391 sal_True == bOK );
1394 CPPUNIT_TEST_SUITE( resolveHostname );
1395 CPPUNIT_TEST( resolveHostname_001 );
1396 CPPUNIT_TEST_SUITE_END( );
1398 }; // class resolveHostname
1400 /** testing the method:
1401 static inline sal_Int32 SAL_CALL getServicePort(
1402 const ::rtl::OUString& strServiceName,
1403 const ::rtl::OUString & strProtocolName= ::rtl::OUString("tcp") );
1406 class gettheServicePort : public CppUnit::TestFixture
1408 public:
1409 void gettheServicePort_001()
1411 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get ftp service port on TCP protocol.",
1412 IP_PORT_FTP== ::osl::SocketAddr::getServicePort( aServiceFTP, aProtocolTCP ) );
1415 void gettheServicePort_002()
1417 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get telnet service port on TCP protocol.",
1418 IP_PORT_TELNET== ::osl::SocketAddr::getServicePort( aServiceTELNET, aProtocolTCP ) );
1421 void gettheServicePort_003()
1423 //Solaris has no service called "https", please see /etc/services
1424 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get netbios-ssn service port on UDP protocol.",
1425 IP_PORT_NETBIOS_DGM == ::osl::SocketAddr::getServicePort( aServiceNETBIOS, aProtocolUDP ) );
1428 void gettheServicePort_004()
1430 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get a service port which is not exist.",
1431 OSL_INVALID_PORT == ::osl::SocketAddr::getServicePort( ::rtl::OUString("notexist"), aProtocolUDP ) );
1434 CPPUNIT_TEST_SUITE( gettheServicePort );
1435 CPPUNIT_TEST( gettheServicePort_001 );
1436 CPPUNIT_TEST( gettheServicePort_002 );
1437 CPPUNIT_TEST( gettheServicePort_003 );
1438 CPPUNIT_TEST( gettheServicePort_004 );
1439 CPPUNIT_TEST_SUITE_END( );
1441 }; // class gettheServicePort
1443 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::ctors, "osl_SocketAddr");
1444 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::is, "osl_SocketAddr");
1445 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getHostname, "osl_SocketAddr");
1446 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getPort, "osl_SocketAddr");
1447 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setPort, "osl_SocketAddr");
1448 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setAddr, "osl_SocketAddr");
1449 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getAddr, "osl_SocketAddr");
1450 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::operator_equal, "osl_SocketAddr");
1451 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getSocketAddrHandle, "osl_SocketAddr");
1452 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getLocalHostname, "osl_SocketAddr");
1453 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::resolveHostname, "osl_SocketAddr");
1454 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::gettheServicePort, "osl_SocketAddr");
1456 } // namespace osl_SocketAddr
1458 namespace osl_Socket
1461 /** testing the methods:
1462 inline Socket( );
1463 inline Socket( const Socket & socket );
1464 inline Socket( oslSocket socketHandle );
1465 inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire );
1468 /** test writer's comment:
1470 class Socket can not be initialized by its protected constructor, though the protected
1471 constructor is the most convenient way to create a new socket.
1472 it only allow the method of C function osl_createSocket like:
1473 ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream,
1474 osl_Socket_ProtocolIp ) );
1475 the use of C method lost some of the transparent of tester using C++ wrapper.
1478 class ctors : public CppUnit::TestFixture
1480 public:
1481 oslSocket sHandle;
1482 // initialization
1483 void setUp( )
1485 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1488 void tearDown( )
1490 sHandle = NULL;
1493 void ctors_none()
1495 /// Socket constructor.
1496 // ::osl::Socket sSocket;
1498 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the socket was created successfully, if no exception occurred",
1499 1 == 1 );
1502 void ctors_acquire()
1504 /// Socket constructor.
1505 ::osl::Socket sSocket( sHandle );
1507 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
1508 osl_Socket_TypeStream == sSocket.getType( ) );
1511 void ctors_no_acquire()
1513 /// Socket constructor.
1514 ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE );
1516 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
1517 osl_Socket_TypeStream == sSocket.getType( ) );
1520 void ctors_copy_ctor()
1522 ::osl::Socket sSocket( sHandle );
1523 /// Socket copy constructor.
1524 ::osl::Socket copySocket( sSocket );
1526 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
1527 osl_Socket_TypeStream == copySocket.getType( ) );
1530 void ctors_TypeRaw()
1532 #ifdef WNT
1533 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
1534 // LLA: ? ::osl::Socket sSocket( sHandleRaw );
1535 CPPUNIT_ASSERT_MESSAGE( " type osl_Socket_TypeRaw socket create failed on UNX ", sHandleRaw != NULL);
1536 #else
1537 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
1538 CPPUNIT_ASSERT_MESSAGE( " can't create socket with type osl_Socket_TypeRaw within UNX is ok.", sHandleRaw == NULL);
1539 #endif
1542 void ctors_family_Ipx()
1544 oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1545 CPPUNIT_ASSERT_MESSAGE( " family osl_Socket_FamilyIpx socket create failed! ", sHandleIpx != NULL);
1546 ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE );
1547 t_print("#Type is %d \n", sSocket.getType( ) );
1549 CPPUNIT_ASSERT_MESSAGE(" test for create new Socket instance that family is osl_Socket_FamilyIpx",
1550 osl_Socket_TypeStream == sSocket.getType( ) );
1553 CPPUNIT_TEST_SUITE( ctors );
1554 CPPUNIT_TEST( ctors_none );
1555 CPPUNIT_TEST( ctors_acquire );
1556 CPPUNIT_TEST( ctors_no_acquire );
1557 CPPUNIT_TEST( ctors_copy_ctor );
1558 CPPUNIT_TEST( ctors_TypeRaw );
1559 CPPUNIT_TEST( ctors_family_Ipx );
1560 CPPUNIT_TEST_SUITE_END();
1562 }; // class ctors
1564 /** testing the methods:
1565 inline Socket& SAL_CALL operator= ( oslSocket socketHandle);
1566 inline Socket& SAL_CALL operator= (const Socket& sock);
1567 inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ;
1568 inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const;
1571 class operators : public CppUnit::TestFixture
1573 public:
1574 oslSocket sHandle;
1575 // initialization
1576 void setUp( )
1578 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1581 void tearDown( )
1583 sHandle = NULL;
1586 /** test writer's comment:
1588 the assignment operator does not support direct assinment like:
1589 ::osl::Socket sSocket = sHandle.
1591 void operators_assignment_handle()
1593 ::osl::Socket sSocket(sHandle);
1594 ::osl::Socket assignSocket = sSocket.getHandle();
1596 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
1597 osl_Socket_TypeStream == assignSocket.getType( ) );
1600 void operators_assignment()
1602 ::osl::Socket sSocket( sHandle );
1603 ::osl::Socket assignSocket = sSocket;
1605 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
1606 osl_Socket_TypeStream == assignSocket.getType( ) );
1609 void operators_equal_handle_001()
1611 /// Socket constructor.
1612 ::osl::Socket sSocket( sHandle );
1613 ::osl::Socket equalSocket = sSocket;
1615 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check equal.",
1616 equalSocket == sHandle );
1619 void operators_equal_handle_002()
1621 /// Socket constructor.
1622 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
1624 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check unequal.",
1625 !( equalSocket == sHandle ) );
1628 void operators_equal_001()
1630 ::osl::Socket sSocket( sHandle );
1631 /// Socket copy constructor.
1632 ::osl::Socket equalSocket( sSocket );
1634 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal function: check equal.",
1635 equalSocket == sSocket );
1638 void operators_equal_002()
1640 ::osl::Socket sSocket( sHandle );
1641 /// Socket copy constructor.
1642 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
1644 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_002 function: check unequal.",
1645 !( equalSocket == sSocket ) );
1648 CPPUNIT_TEST_SUITE( operators );
1649 CPPUNIT_TEST( operators_assignment_handle );
1650 CPPUNIT_TEST( operators_assignment );
1651 CPPUNIT_TEST( operators_equal_handle_001 );
1652 CPPUNIT_TEST( operators_equal_handle_002 );
1653 CPPUNIT_TEST( operators_equal_001 );
1654 CPPUNIT_TEST( operators_equal_002 );
1655 CPPUNIT_TEST_SUITE_END();
1657 }; // class operators
1659 /** testing the methods:
1660 inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite );
1661 inline void SAL_CALL close();
1664 class close : public CppUnit::TestFixture
1666 public:
1667 oslSocket sHandle;
1668 // initialization
1669 void setUp( )
1671 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1674 void tearDown( )
1676 sHandle = NULL;
1679 void close_001()
1681 ::osl::Socket sSocket(sHandle);
1682 sSocket.close();
1684 CPPUNIT_ASSERT_MESSAGE( "test for close_001 function: this function is reserved for test.",
1685 sSocket.getHandle() == sHandle );
1688 void close_002()
1690 //#if defined(LINUX)
1691 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1692 AcceptorThread myAcceptorThread( asSocket, aHostIp1 );
1693 myAcceptorThread.create();
1695 thread_sleep( 1 );
1696 //when accepting, close the socket, the thread will not block for accepting
1697 //man close:Any locks held on the file it was associated with, and owned by the process, are removed
1698 asSocket.close();
1699 //thread_sleep( 2 );
1700 myAcceptorThread.join();
1702 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
1703 myAcceptorThread.isOK() == sal_True );
1704 //#endif
1707 // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( )
1708 void close_003()
1710 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1711 AcceptorThread myAcceptorThread( asSocket, aHostIpZero );
1712 myAcceptorThread.create();
1714 thread_sleep( 1 );
1715 asSocket.close();
1716 myAcceptorThread.join();
1718 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
1719 myAcceptorThread.isOK() == sal_True );
1722 CPPUNIT_TEST_SUITE( close );
1723 CPPUNIT_TEST( close_001 );
1724 CPPUNIT_TEST( close_002 );
1725 CPPUNIT_TEST( close_003 );
1726 CPPUNIT_TEST_SUITE_END();
1728 }; // class close
1730 /** testing the method:
1731 inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const;
1734 class getLocalAddr : public CppUnit::TestFixture
1736 public:
1737 oslSocket sHandle;
1738 // initialization
1739 void setUp( )
1741 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1744 void tearDown( )
1746 sHandle = NULL;
1749 // get the Address of the local end of the socket
1750 void getLocalAddr_001()
1752 ::osl::Socket sSocket(sHandle);
1753 ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT8 );
1754 ::osl::SocketAddr saLocalSocketAddr;
1756 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1758 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1759 ::rtl::OUString suError1 = ::rtl::OUString("Socket bind fail:") + sSocket.getErrorAsString();
1760 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError1), sal_True == bOK1 );
1762 sSocket.getLocalAddr( saLocalSocketAddr );
1764 sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ;
1766 CPPUNIT_ASSERT_MESSAGE( "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.",
1767 sal_True == bOK );
1770 CPPUNIT_TEST_SUITE( getLocalAddr );
1771 CPPUNIT_TEST( getLocalAddr_001 );
1772 CPPUNIT_TEST_SUITE_END();
1774 }; // class getLocalAddr
1776 /** testing the method:
1777 inline sal_Int32 SAL_CALL getLocalPort() const;
1780 class getLocalPort : public CppUnit::TestFixture
1782 public:
1783 oslSocket sHandle;
1784 // initialization
1785 void setUp( )
1787 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1790 void tearDown( )
1792 sHandle = NULL;
1795 void getLocalPort_001()
1797 ::osl::Socket sSocket(sHandle);
1798 ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT7 ); // aHostIp1 localhost
1799 ::osl::SocketAddr saLocalSocketAddr;
1801 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1803 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1804 ::rtl::OUString suError1 = ::rtl::OUString("Socket bind fail:") + sSocket.getErrorAsString();
1805 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError1), sal_True == bOK1 );
1806 sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) );
1808 CPPUNIT_ASSERT_MESSAGE( "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.",
1809 sal_True == bOK );
1812 /** test writer's comment:
1814 the invalid port number can not be set by giving invalid port number
1815 such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be
1816 valid, the only instance that the getLocalPort returns OSL_INVALID_PORT
1817 is when saSocketAddr itself is an invalid one, that is , the IP or host name
1818 can not be found, then the created socket address is not valid.
1820 void getLocalPort_002()
1822 ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_TELNET);
1823 #ifdef WNT
1824 ::osl::Socket sSocket(sHandle);
1825 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
1826 sSocket.bind( saBindSocketAddr );
1827 //Invalid IP, so bind should fail
1828 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
1829 ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT),
1830 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned.");
1831 sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) );
1832 (void)bOK;
1833 #else
1834 //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT
1835 ::rtl::OUString suError ("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");
1836 #endif
1837 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError), sal_False );
1841 void getLocalPort_003()
1843 ::osl::Socket sSocket(sHandle);
1844 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL);
1846 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1848 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1849 ::rtl::OUString suError1 = ::rtl::OUString("Socket bind fail:") + sSocket.getErrorAsString();
1850 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError1), sal_True == bOK1 );
1851 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
1852 ::rtl::OUString("34463"),
1853 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned");
1854 sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535);
1856 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError), sal_True == bOK );
1859 CPPUNIT_TEST_SUITE( getLocalPort );
1860 CPPUNIT_TEST( getLocalPort_001 );
1861 // LLA: CPPUNIT_TEST( getLocalPort_002 );
1862 CPPUNIT_TEST( getLocalPort_003 );
1863 CPPUNIT_TEST_SUITE_END();
1865 }; // class getLocalPort
1867 /** testing the method:
1868 inline ::rtl::OUString SAL_CALL getLocalHost() const;
1870 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;
1871 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
1872 will return hostname of current processor such as "aegean.PRC.Sun.COM"
1875 class getLocalHost : public CppUnit::TestFixture
1877 public:
1878 oslSocket sHandle;
1879 // initialization
1880 void setUp( )
1882 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1885 void tearDown( )
1887 sHandle = NULL;
1890 void getLocalHost_001()
1892 ::osl::Socket sSocket(sHandle);
1893 //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu
1894 ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT6 );
1896 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1898 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1899 ::rtl::OUString suError1 = ::rtl::OUString("Socket bind fail:") + sSocket.getErrorAsString();
1900 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError1), sal_True == bOK1 );
1901 sal_Bool bOK;
1902 ::rtl::OUString suError;
1903 #ifdef WNT
1904 bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
1905 suError = outputError(sSocket.getLocalHost( ), getThisHostname( ),
1906 "test for getLocalHost function: create localhost socket and check name");
1907 #else
1908 ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" );
1909 sal_Bool bRes1, bRes2;
1910 bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ;
1911 bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ;
1912 bOK = bRes1 || bRes2;
1913 suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name");
1914 #endif
1915 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError), sal_True == bOK );
1918 void getLocalHost_002()
1920 ::osl::Socket sSocket(sHandle);
1921 ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_POP3);
1922 ::osl::SocketAddr saLocalSocketAddr;
1924 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1925 sSocket.bind( saBindSocketAddr );
1926 //Invalid IP, so bind should fail
1927 sal_Bool bOK = compareUString( sSocket.getLocalHost( ), aNullURL ) ;
1928 ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), aNullURL, "test for getLocalHost function: getLocalHost with invalid SocketAddr");
1930 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError), sal_True == bOK );
1933 CPPUNIT_TEST_SUITE( getLocalHost );
1934 CPPUNIT_TEST( getLocalHost_001 );
1935 CPPUNIT_TEST( getLocalHost_002 );
1936 CPPUNIT_TEST_SUITE_END();
1938 }; // class getLocalHost
1940 /** testing the methods:
1941 inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const;
1942 inline sal_Int32 SAL_CALL getPeerPort() const;
1943 inline ::rtl::OUString SAL_CALL getPeerHost() const;
1945 class getPeer : public CppUnit::TestFixture
1947 public:
1948 oslSocket sHandle;
1949 TimeValue *pTimeout;
1950 ::osl::AcceptorSocket asAcceptorSocket;
1951 ::osl::ConnectorSocket csConnectorSocket;
1953 // initialization
1954 void setUp( )
1956 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
1957 pTimeout->Seconds = 3;
1958 pTimeout->Nanosec = 0;
1959 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1962 void tearDown( )
1964 free( pTimeout );
1965 sHandle = NULL;
1966 asAcceptorSocket.close( );
1967 csConnectorSocket.close( );
1970 void getPeer_001()
1972 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
1973 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT );
1974 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
1975 ::osl::StreamSocket ssConnection;
1976 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1977 /// launch server socket
1978 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
1979 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind '127.0.0.1' address failed.", sal_True == bOK1 );
1980 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
1981 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
1983 asAcceptorSocket.enableNonBlockingMode( sal_True );
1984 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1986 /// launch client socket
1987 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
1989 /// get peer information
1990 csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
1991 sal_Int32 peerPort = csConnectorSocket.getPeerPort( );
1992 ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( );
1994 CPPUNIT_ASSERT_MESSAGE( "test for getPeer function: setup a connection and then get the peer address, port and host from client side.",
1995 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&&
1996 ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) &&
1997 ( peerPort == saLocalSocketAddr.getPort( ) ));
2000 CPPUNIT_TEST_SUITE( getPeer );
2001 CPPUNIT_TEST( getPeer_001 );
2002 CPPUNIT_TEST_SUITE_END();
2004 }; // class getPeer
2006 /** testing the methods:
2007 inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface);
2010 class bind : public CppUnit::TestFixture
2012 public:
2013 oslSocket sHandle;
2014 // initialization
2015 void setUp( )
2017 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2020 void tearDown( )
2022 sHandle = NULL;
2025 void bind_001()
2027 ::osl::Socket sSocket(sHandle);
2028 //bind must use local IP address ---mindyliu
2029 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 );
2031 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2032 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
2033 CPPUNIT_ASSERT_MESSAGE( "Socket bind fail.", sal_True == bOK1 );
2035 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ;
2037 sSocket.close();
2038 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", sal_True == bOK2 );
2041 void bind_002()
2043 ::osl::Socket sSocket(sHandle);
2044 ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_NETBIOS );
2045 ::osl::SocketAddr saLocalSocketAddr;
2047 sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True);
2048 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
2049 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
2051 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.",
2052 ( sal_False == bOK1 ) && ( sal_False == bOK2 ) );
2055 CPPUNIT_TEST_SUITE( bind );
2056 CPPUNIT_TEST( bind_001 );
2057 CPPUNIT_TEST( bind_002 );
2058 CPPUNIT_TEST_SUITE_END();
2060 }; // class bind
2062 /** testing the methods:
2063 inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const;
2066 class isRecvReady : public CppUnit::TestFixture
2068 public:
2069 oslSocket sHandle;
2070 TimeValue *pTimeout;
2071 ::osl::AcceptorSocket asAcceptorSocket;
2072 ::osl::ConnectorSocket csConnectorSocket;
2074 // initialization
2075 void setUp( )
2077 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
2078 pTimeout->Seconds = 3;
2079 pTimeout->Nanosec = 0;
2080 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2083 void tearDown( )
2085 free( pTimeout );
2086 sHandle = NULL;
2087 asAcceptorSocket.close( );
2088 csConnectorSocket.close( );
2091 void isRecvReady_001()
2093 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT1 );
2094 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT1 );
2095 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
2096 ::osl::StreamSocket ssConnection;
2097 /// launch server socket
2098 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
2099 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
2100 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
2101 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
2102 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
2103 asAcceptorSocket.enableNonBlockingMode( sal_True );
2104 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
2106 /// launch client socket
2107 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
2109 /// is receive ready?
2110 sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout );
2112 CPPUNIT_ASSERT_MESSAGE( "test for isRecvReady function: setup a connection and then check if it can transmit data.",
2113 ( sal_True == bOK3 ) );
2116 CPPUNIT_TEST_SUITE( isRecvReady );
2117 CPPUNIT_TEST( isRecvReady_001 );
2118 CPPUNIT_TEST_SUITE_END();
2120 }; // class isRecvReady
2122 /** testing the methods:
2123 inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const;
2125 class isSendReady : public CppUnit::TestFixture
2127 public:
2128 oslSocket sHandle;
2129 TimeValue *pTimeout;
2130 ::osl::AcceptorSocket asAcceptorSocket;
2131 ::osl::ConnectorSocket csConnectorSocket;
2133 // initialization
2134 void setUp( )
2136 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
2137 pTimeout->Seconds = 3;
2138 pTimeout->Nanosec = 0;
2139 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2142 void tearDown( )
2144 free( pTimeout );
2145 sHandle = NULL;
2146 asAcceptorSocket.close( );
2147 csConnectorSocket.close( );
2150 void isSendReady_001()
2152 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
2153 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT );
2154 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
2155 ::osl::StreamSocket ssConnection;
2157 /// launch server socket
2158 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2159 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
2160 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
2161 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
2162 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
2163 asAcceptorSocket.enableNonBlockingMode( sal_True );
2164 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
2166 /// launch client socket
2167 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
2169 /// is send ready?
2170 sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout );
2172 CPPUNIT_ASSERT_MESSAGE( "test for isSendReady function: setup a connection and then check if it can transmit data.",
2173 ( sal_True == bOK3 ) );
2176 CPPUNIT_TEST_SUITE( isSendReady );
2177 CPPUNIT_TEST( isSendReady_001 );
2178 CPPUNIT_TEST_SUITE_END();
2180 }; // class isSendReady
2182 /** testing the methods:
2183 inline oslSocketType SAL_CALL getType() const;
2187 class getType : public CppUnit::TestFixture
2189 public:
2190 oslSocket sHandle;
2191 // initialization
2192 void setUp( )
2197 void tearDown( )
2199 sHandle = NULL;
2202 void getType_001()
2204 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2205 ::osl::Socket sSocket(sHandle);
2207 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
2208 osl_Socket_TypeStream == sSocket.getType( ) );
2211 void getType_002()
2213 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2214 ::osl::Socket sSocket(sHandle);
2216 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
2217 osl_Socket_TypeDgram == sSocket.getType( ) );
2220 #ifdef UNX
2221 // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here
2222 // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw()
2223 void getType_003()
2225 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.",
2226 sal_True);
2228 #else
2229 void getType_003()
2231 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
2232 ::osl::Socket sSocket(sHandle);
2234 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
2235 osl_Socket_TypeRaw == sSocket.getType( ) );
2237 #endif
2239 CPPUNIT_TEST_SUITE( getType );
2240 CPPUNIT_TEST( getType_001 );
2241 CPPUNIT_TEST( getType_002 );
2242 CPPUNIT_TEST( getType_003 );
2243 CPPUNIT_TEST_SUITE_END();
2245 }; // class getType
2247 /** testing the methods:
2248 inline sal_Int32 SAL_CALL getOption(
2249 oslSocketOption Option,
2250 void* pBuffer,
2251 sal_uInt32 BufferLen,
2252 oslSocketOptionLevel Level= osl_Socket_LevelSocket) const;
2254 inline sal_Int32 getOption( oslSocketOption option ) const;
2258 class getOption : public CppUnit::TestFixture
2260 public:
2261 oslSocket sHandle;
2262 // initialization
2263 void setUp( )
2268 void tearDown( )
2270 sHandle = NULL;
2273 /** test writer's comment:
2275 in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream.
2276 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ
2277 in 1.
2280 void getOption_001()
2282 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2283 ::osl::Socket sSocket(sHandle);
2284 sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
2285 *pType = 0;
2286 sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) );
2287 sal_Bool bOK = ( SOCK_STREAM == *pType );
2288 // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream
2289 //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType );
2290 free( pType );
2292 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get type option of socket.",
2293 sal_True == bOK );
2296 // getsockopt error
2297 void getOption_004()
2299 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2300 ::osl::Socket sSocket(sHandle);
2302 sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
2303 sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) );
2304 free( pbDontRoute );
2306 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get invalid option of socket, should return -1.",
2307 nRes == -1 );
2310 void getOption_simple_001()
2312 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2313 ::osl::Socket sSocket(sHandle);
2315 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) );
2317 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
2318 sal_True == bOK );
2321 void getOption_simple_002()
2323 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2324 ::osl::Socket sSocket(sHandle);
2326 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) );
2328 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
2329 sal_True == bOK );
2332 CPPUNIT_TEST_SUITE( getOption );
2333 CPPUNIT_TEST( getOption_001 );
2334 CPPUNIT_TEST( getOption_004 );
2335 CPPUNIT_TEST( getOption_simple_001 );
2336 CPPUNIT_TEST( getOption_simple_002 );
2337 CPPUNIT_TEST_SUITE_END();
2339 }; // class getOption
2341 /** testing the methods:
2342 inline sal_Bool SAL_CALL setOption( oslSocketOption Option,
2343 void* pBuffer,
2344 sal_uInt32 BufferLen,
2345 oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const;
2348 class setOption : public CppUnit::TestFixture
2350 public:
2351 TimeValue *pTimeout;
2352 // LLA: maybe there is an error in the source,
2353 // as long as I remember, if a derived class do not overload all ctors there is a problem.
2355 ::osl::AcceptorSocket asAcceptorSocket;
2357 void setUp( )
2362 void tearDown( )
2364 asAcceptorSocket.close( );
2367 // LLA:
2368 // getSocketOption returns BufferLen, or -1 if something failed
2370 // setSocketOption returns sal_True, if option could stored
2371 // else sal_False
2373 void setOption_001()
2375 /// set and get option.
2376 int nBufferLen = sizeof ( sal_Int32);
2377 // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool!
2379 sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
2380 *pbDontRouteSet = 1; // sal_True;
2382 sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
2383 *pGetBuffer = 0;
2385 // maybe asAcceptorSocket is not right initialized
2386 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen );
2387 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b1 ) );
2388 sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
2389 CPPUNIT_ASSERT_MESSAGE( "getOption function failed.", ( n2 == nBufferLen ) );
2391 // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value,
2392 // just judge it is zero or not!
2393 sal_Bool bOK = ( 0 != *pGetBuffer );
2394 t_print("#setOption_001: getOption is %" SAL_PRIdINT32 " \n", *pGetBuffer);
2396 // toggle check, set to 0
2397 *pbDontRouteSet = 0;
2399 sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) );
2400 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b3 ) );
2401 sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
2402 CPPUNIT_ASSERT_MESSAGE( "getOption (DONTROUTE) function failed.", ( n4 == nBufferLen ) );
2404 sal_Bool bOK2 = ( 0 == *pGetBuffer );
2406 t_print("#setOption_001: getOption is %" SAL_PRIdINT32 " \n", *pGetBuffer);
2408 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
2409 ( sal_True == bOK ) && (sal_True == bOK2) );
2411 free( pbDontRouteSet );
2412 free( pGetBuffer );
2415 void setOption_002()
2417 /// set and get option.
2419 // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen );
2420 // *pbLingerSet = 7;
2421 // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen );
2422 /* struct */linger aLingerSet;
2423 sal_Int32 nBufferLen = sizeof( struct linger );
2424 aLingerSet.l_onoff = 1;
2425 aLingerSet.l_linger = 7;
2427 linger aLingerGet;
2429 asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
2431 sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen );
2432 CPPUNIT_ASSERT_MESSAGE( "getOption (SO_LINGER) function failed.", ( n1 == nBufferLen ) );
2434 //t_print("#setOption_002: getOption is %d \n", aLingerGet.l_linger);
2435 sal_Bool bOK = ( 7 == aLingerGet.l_linger );
2436 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check. ",
2437 sal_True == bOK );
2441 void setOption_003()
2443 linger aLingerSet;
2444 aLingerSet.l_onoff = 1;
2445 aLingerSet.l_linger = 7;
2447 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 );
2448 printUString( asAcceptorSocket.getErrorAsString() );
2449 CPPUNIT_ASSERT_MESSAGE( "setOption (SO_LINGER) function failed for optlen is 0.",
2450 ( b1 == sal_False ) );
2453 void setOption_simple_001()
2455 /// set and get option.
2456 asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True );
2457 sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
2459 t_print("setOption_simple_001(): getoption is %d \n", (int) asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
2460 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
2461 ( sal_True == bOK ) );
2464 void setOption_simple_002()
2466 /// set and get option.
2467 // LLA: this does not work, due to the fact that SO_LINGER is a structure
2468 // LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 );
2469 // LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) );
2471 // LLA: CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
2472 // LLA: ( sal_True == bOK ) );
2475 CPPUNIT_TEST_SUITE( setOption );
2476 CPPUNIT_TEST( setOption_001 );
2477 CPPUNIT_TEST( setOption_002 );
2478 CPPUNIT_TEST( setOption_003 );
2479 CPPUNIT_TEST( setOption_simple_001 );
2480 // LLA: CPPUNIT_TEST( setOption_simple_002 );
2481 CPPUNIT_TEST_SUITE_END();
2483 }; // class setOption
2485 /** testing the method:
2486 inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode);
2488 class enableNonBlockingMode : public CppUnit::TestFixture
2490 public:
2491 ::osl::AcceptorSocket asAcceptorSocket;
2493 void enableNonBlockingMode_001()
2495 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
2496 ::osl::StreamSocket ssConnection;
2498 /// launch server socket
2499 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2500 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
2501 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
2502 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
2503 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
2504 asAcceptorSocket.enableNonBlockingMode( sal_True );
2505 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
2507 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
2508 sal_Bool bOK = sal_True;
2509 asAcceptorSocket.close( );
2511 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",
2512 ( sal_True == bOK ) );
2515 CPPUNIT_TEST_SUITE( enableNonBlockingMode );
2516 CPPUNIT_TEST( enableNonBlockingMode_001 );
2517 CPPUNIT_TEST_SUITE_END();
2519 }; // class enableNonBlockingMode
2521 /** testing the method:
2522 inline sal_Bool SAL_CALL isNonBlockingMode() const;
2524 class isNonBlockingMode : public CppUnit::TestFixture
2526 public:
2527 ::osl::AcceptorSocket asAcceptorSocket;
2529 void isNonBlockingMode_001()
2531 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
2532 ::osl::StreamSocket ssConnection;
2534 /// launch server socket
2535 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
2536 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
2537 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
2538 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
2539 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
2541 sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( );
2542 asAcceptorSocket.enableNonBlockingMode( sal_True );
2543 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
2545 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
2546 sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( );
2547 asAcceptorSocket.close( );
2549 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.",
2550 ( sal_False == bOK3 ) && ( sal_True == bOK4 ) );
2553 CPPUNIT_TEST_SUITE( isNonBlockingMode );
2554 CPPUNIT_TEST( isNonBlockingMode_001 );
2555 CPPUNIT_TEST_SUITE_END();
2557 }; // class isNonBlockingMode
2559 /** testing the method:
2560 inline void SAL_CALL clearError() const;
2562 class clearError : public CppUnit::TestFixture
2564 public:
2565 oslSocket sHandle;
2566 // initialization
2567 void setUp( )
2569 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2572 void tearDown( )
2574 sHandle = NULL;
2577 void clearError_001()
2579 ::osl::Socket sSocket(sHandle);
2580 ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_HTTP2 );
2581 ::osl::SocketAddr saLocalSocketAddr;
2582 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2583 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
2584 oslSocketError seBind = sSocket.getError( );
2585 sSocket.clearError( );
2587 CPPUNIT_ASSERT_MESSAGE( "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.",
2588 osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None );
2591 CPPUNIT_TEST_SUITE( clearError );
2592 CPPUNIT_TEST( clearError_001 );
2593 CPPUNIT_TEST_SUITE_END();
2595 }; // class clearError
2597 /** testing the methods:
2598 inline oslSocketError getError() const;
2599 inline ::rtl::OUString getErrorAsString( ) const;
2601 class getError : public CppUnit::TestFixture
2603 public:
2604 oslSocket sHandle;
2605 // initialization
2606 void setUp( )
2608 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2611 void tearDown( )
2613 sHandle = NULL;
2616 void getError_001()
2618 ::osl::Socket sSocket(sHandle);
2619 ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_FTP );
2620 ::osl::SocketAddr saLocalSocketAddr;
2622 CPPUNIT_ASSERT_MESSAGE( "test for getError function: should get no error.",
2623 osl_Socket_E_None == sSocket.getError( ) );
2626 void getError_002()
2628 ::osl::Socket sSocket(sHandle);
2629 ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_FTP );
2630 ::osl::SocketAddr saLocalSocketAddr;
2631 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2632 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
2633 //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError.
2634 #if defined(SOLARIS)
2635 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. ",
2636 osl_Socket_E_InvalidError == sSocket.getError( ) );
2637 #else
2638 //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail.
2640 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",
2641 osl_Socket_E_AddrNotAvail == sSocket.getError( ) );
2642 #endif
2645 CPPUNIT_TEST_SUITE( getError );
2646 CPPUNIT_TEST( getError_001 );
2647 CPPUNIT_TEST( getError_002 );
2648 CPPUNIT_TEST_SUITE_END();
2650 }; // class getError
2652 /** testing the methods:
2653 inline oslSocket getHandle() const;
2656 class getHandle : public CppUnit::TestFixture
2658 public:
2659 oslSocket sHandle;
2660 // initialization
2661 void setUp( )
2663 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2666 void tearDown( )
2668 sHandle = NULL;
2671 void getHandle_001()
2673 ::osl::Socket sSocket(sHandle);
2674 ::osl::Socket assignSocket = sSocket.getHandle();
2676 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
2677 osl_Socket_TypeStream == assignSocket.getType( ) );
2680 void getHandle_002()
2682 ::osl::Socket sSocket( sHandle );
2683 ::osl::Socket assignSocket ( sSocket.getHandle( ) );
2685 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
2686 osl_Socket_TypeStream == assignSocket.getType( ) );
2689 CPPUNIT_TEST_SUITE( getHandle );
2690 CPPUNIT_TEST( getHandle_001 );
2691 CPPUNIT_TEST( getHandle_002 );
2692 CPPUNIT_TEST_SUITE_END();
2694 }; // class getHandle
2696 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::ctors, "osl_Socket");
2697 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::operators, "osl_Socket");
2698 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::close, "osl_Socket");
2699 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalAddr, "osl_Socket");
2700 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalPort, "osl_Socket");
2701 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalHost, "osl_Socket");
2702 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getPeer, "osl_Socket");
2703 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::bind, "osl_Socket");
2704 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isRecvReady, "osl_Socket");
2705 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isSendReady, "osl_Socket");
2706 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getType, "osl_Socket");
2707 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getOption, "osl_Socket");
2708 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::setOption, "osl_Socket");
2709 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::enableNonBlockingMode, "osl_Socket");
2710 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isNonBlockingMode, "osl_Socket");
2711 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::clearError, "osl_Socket");
2712 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getError, "osl_Socket");
2713 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getHandle, "osl_Socket");
2715 } // namespace osl_Socket
2717 namespace osl_StreamSocket
2720 /** testing the methods:
2721 inline StreamSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
2722 oslProtocol Protocol = osl_Socket_ProtocolIp,
2723 oslSocketType Type = osl_Socket_TypeStream);
2725 inline StreamSocket( const StreamSocket & );
2727 inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire );
2729 inline StreamSocket( oslSocket Socket );
2732 class ctors : public CppUnit::TestFixture
2734 public:
2735 oslSocket sHandle;
2736 // initialization
2737 void setUp( )
2739 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2742 void tearDown( )
2744 sHandle = NULL;
2747 void ctors_none()
2749 /// Socket constructor.
2750 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2752 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the stream socket was created successfully.",
2753 osl_Socket_TypeStream == ssSocket.getType( ) );
2756 void ctors_acquire()
2758 /// Socket constructor.
2759 ::osl::StreamSocket ssSocket( sHandle );
2761 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
2762 osl_Socket_TypeStream == ssSocket.getType( ) );
2765 void ctors_no_acquire()
2767 /// Socket constructor.
2768 ::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE );
2770 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
2771 osl_Socket_TypeStream == ssSocket.getType( ) );
2774 void ctors_copy_ctor()
2776 /// Socket constructor.
2777 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2778 /// Socket copy constructor.
2779 ::osl::StreamSocket copySocket( ssSocket );
2781 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
2782 osl_Socket_TypeStream == copySocket.getType( ) );
2785 CPPUNIT_TEST_SUITE( ctors );
2786 CPPUNIT_TEST( ctors_none );
2787 CPPUNIT_TEST( ctors_acquire );
2788 CPPUNIT_TEST( ctors_no_acquire );
2789 CPPUNIT_TEST( ctors_copy_ctor );
2790 CPPUNIT_TEST_SUITE_END();
2792 }; // class ctors
2794 class send_recv: public CppUnit::TestFixture
2796 public:
2797 // initialization
2798 void setUp( )
2802 void tearDown( )
2807 void send_recv1()
2809 //client sent two strings, and server received, check the order and value
2810 ServerSocketThread myServerThread;
2811 ClientSocketThread myClientThread;
2812 myServerThread.create( );
2813 myClientThread.create( );
2815 //wait until the thread terminate
2816 myClientThread.join( );
2817 myServerThread.join( );
2818 sal_Char myStr[30] = "";
2819 strcat( myStr, pTestString1 );
2820 strcat( myStr, pTestString2 );
2821 sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr );
2822 CPPUNIT_ASSERT_MESSAGE(" test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.",
2823 nRes == 0 );
2826 // error when recv
2827 void send_recv2()
2829 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2830 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 );
2831 ::osl::StreamSocket ssStreamConnection;
2832 sal_Char pReadBuffer[30] = "";
2834 ClientSocketThread myClientThread;
2835 myClientThread.create( );
2837 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
2839 asAcceptorSocket.bind( saLocalSocketAddr );
2840 asAcceptorSocket.listen( 1 );
2841 asAcceptorSocket.enableNonBlockingMode( sal_True );
2842 asAcceptorSocket.acceptConnection( ssStreamConnection );
2843 sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 );
2845 myClientThread.join( ) ;
2846 ssStreamConnection.close();
2847 asAcceptorSocket.close();
2848 CPPUNIT_ASSERT_MESSAGE(" test for send/recv, recv error!", nReadNumber == -1 );
2851 void write_read(sal_Int32 _nBufferSize, int _nValue)
2853 //client sent two strings, and server received, check the order and value
2854 WriteSocketThread myServerThread(_nBufferSize, _nValue);
2855 ReadSocketThread myClientThread(_nBufferSize, _nValue);
2856 myServerThread.create( );
2857 // thread_sleep( 1 );
2858 myClientThread.create( );
2860 //wait until the thread terminate
2861 myClientThread.join( );
2862 myServerThread.join( );
2864 //Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500
2865 // Proteon PRONET = 2046), so here test read 4000 bytes
2866 sal_Int32 nLength = myClientThread.getCount();
2867 bool bIsOk = myClientThread.isOk(); // check if the values are right.
2869 t_print("Length:=%d\n", (int) nLength);
2870 t_print(" bIsOk:=%d\n", bIsOk);
2872 CPPUNIT_ASSERT_MESSAGE(" test for write/read values with two threads: send data from server, check readed data in client.",
2873 nLength == _nBufferSize && bIsOk == true);
2876 void write_read_001()
2878 write_read(50, 10);
2880 void write_read_002()
2882 write_read(1024, 20);
2884 void write_read_003()
2886 write_read(4000, 1);
2888 void write_read_004()
2890 write_read(8192, 3);
2893 CPPUNIT_TEST_SUITE( send_recv );
2894 CPPUNIT_TEST( write_read_001 );
2895 CPPUNIT_TEST( write_read_002 );
2896 CPPUNIT_TEST( write_read_003 );
2897 CPPUNIT_TEST( write_read_004 );
2898 CPPUNIT_TEST( send_recv1 );
2899 CPPUNIT_TEST( send_recv2 );
2900 // CPPUNIT_TEST( write_read );
2901 CPPUNIT_TEST_SUITE_END();
2902 }; // class send_recv
2904 class SendClientThread : public ClientSocketThread
2906 protected:
2908 void SAL_CALL run( )
2910 TimeValue *pTimeout;
2911 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
2912 pTimeout->Seconds = 5;
2913 pTimeout->Nanosec = 0;
2915 if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout ))
2917 #if !SILENT_TEST
2918 sal_Int32 nWrite1 =
2919 #endif
2920 csConnectorSocket.write( pTestString1, 11 ); // "test socket"
2922 #if !SILENT_TEST
2923 sal_Int32 nWrite2 =
2924 #endif
2925 csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
2926 thread_sleep( 2 );
2927 csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
2928 t_print("nWrite1 is %d, nWrite2 is %d\n", (int)nWrite1, (int)nWrite2 );
2929 //thread_sleep( 1 );
2931 else
2932 t_print("# SendClientThread: connect failed! \n");
2934 csConnectorSocket.close();
2935 free( pTimeout );
2940 class shutdown: public CppUnit::TestFixture
2942 public:
2943 // initialization
2944 void setUp( )
2948 void tearDown( )
2953 // similar to close_002
2954 void shutdown_001()
2956 #if defined(LINUX)
2957 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2958 AcceptorThread myAcceptorThread( asSocket, aHostIp1 );
2959 myAcceptorThread.create();
2961 thread_sleep( 1 );
2963 //when accepting, shutdown the socket, the thread will not block for accepting
2964 asSocket.shutdown();
2965 myAcceptorThread.join();
2967 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
2968 myAcceptorThread.isOK( ) == sal_True );
2969 #endif
2972 void shutdown_002()
2974 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2975 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9);
2976 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
2977 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
2978 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
2979 sal_Char pReadBuffer[40];
2980 SendClientThread mySendThread;
2981 mySendThread.create();
2983 asSocket.enableNonBlockingMode( sal_False );
2984 ::osl::StreamSocket ssConnectionSocket;
2985 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
2986 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
2988 /* set socket option SO_LINGER 0, so close immediately */
2989 linger aLingerSet;
2990 sal_Int32 nBufferLen = sizeof( struct linger );
2991 aLingerSet.l_onoff = 0;
2992 aLingerSet.l_linger = 0;
2994 ssConnectionSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
2995 thread_sleep( 1 );
2996 //sal_uInt32 nRecv1 = 0;
2997 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
2999 //shutdown read after client the first send complete
3000 ssConnectionSocket.shutdown( osl_Socket_DirRead );
3002 sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 );
3003 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 );
3004 t_print("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", (int) nRead1, (int) nRead2, (int) nRead3 );
3005 mySendThread.join();
3007 ssConnectionSocket.close();
3008 asSocket.close();
3010 /* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0,
3011 http://dbforums.com/arch/186/2002/12/586417
3012 While on Solaris, after shutdown(DirRead), all read will return 0
3014 #ifdef LINUX
3015 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
3016 nRead1 > 0 && nRead3 == 0 );
3017 #else
3018 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
3019 nRead1 > 0 && nRead2 == 0 && nRead3 == 0 );
3020 #endif
3024 void shutdown_003()
3026 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3027 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9);
3028 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
3029 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
3030 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
3031 sal_Char pReadBuffer[40];
3032 SendClientThread mySendThread;
3033 mySendThread.create();
3035 asSocket.enableNonBlockingMode( sal_False );
3036 ::osl::StreamSocket ssConnectionSocket;
3037 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
3038 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
3040 thread_sleep( 1 );
3041 //shutdown write after client the first send complete
3042 ssConnectionSocket.shutdown( osl_Socket_DirWrite );
3044 // recv should not shutdown
3045 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
3047 sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 );
3048 // still can read
3049 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 );
3050 t_print("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", (int) nRead1, (int) nWrite, (int) nRead3 );
3051 mySendThread.join();
3052 ssConnectionSocket.close();
3053 asSocket.close();
3055 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not send(write).",
3056 nRead1 > 0 && nWrite == 0 && nRead3 > 0);
3060 CPPUNIT_TEST_SUITE( shutdown );
3061 CPPUNIT_TEST( shutdown_001 );
3062 CPPUNIT_TEST( shutdown_002 );
3063 CPPUNIT_TEST( shutdown_003 );
3064 CPPUNIT_TEST_SUITE_END();
3065 }; // class shutdown
3067 class isExceptionPending: public CppUnit::TestFixture
3069 public:
3070 void isExPending_001()
3072 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3073 TimeValue *pTimeout;
3074 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
3075 pTimeout->Seconds = 3;
3076 pTimeout->Nanosec = 0;
3077 sal_Bool bOk = asSocket.isExceptionPending( pTimeout );
3078 free( pTimeout );
3080 CPPUNIT_ASSERT_MESSAGE( "test for isExceptionPending.",
3081 bOk == sal_False );
3084 /**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/
3086 CPPUNIT_TEST_SUITE( isExceptionPending );
3087 CPPUNIT_TEST( isExPending_001 );
3088 CPPUNIT_TEST_SUITE_END();
3089 }; // class isExceptionPending
3091 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::ctors, "osl_StreamSocket");
3092 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::send_recv, "osl_StreamSocket");
3093 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::shutdown, "osl_StreamSocket");
3094 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::isExceptionPending, "osl_StreamSocket");
3096 } // namespace osl_StreamSocket
3098 namespace osl_ConnectorSocket
3101 /** testing the method:
3102 ConnectorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
3103 oslProtocol Protocol = osl_Socket_ProtocolIp,
3104 oslSocketType Type = osl_Socket_TypeStream);
3107 class ctors : public CppUnit::TestFixture
3109 public:
3110 void ctors_001()
3112 /// Socket constructor.
3113 ::osl::ConnectorSocket csSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3115 CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the connector socket was created successfully.",
3116 osl_Socket_TypeStream == csSocket.getType( ) );
3119 CPPUNIT_TEST_SUITE( ctors );
3120 CPPUNIT_TEST( ctors_001 );
3121 CPPUNIT_TEST_SUITE_END();
3123 }; // class ctors
3125 /** testing the method:
3126 oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0);
3129 class connect : public CppUnit::TestFixture
3131 public:
3132 TimeValue *pTimeout;
3133 ::osl::AcceptorSocket asAcceptorSocket;
3134 ::osl::ConnectorSocket csConnectorSocket;
3136 // initialization
3137 void setUp( )
3139 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
3140 pTimeout->Seconds = 3;
3141 pTimeout->Nanosec = 0;
3142 // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
3145 void tearDown( )
3147 free( pTimeout );
3148 // sHandle = NULL;
3149 asAcceptorSocket.close( );
3150 csConnectorSocket.close( );
3153 void connect_001()
3155 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT2 );
3156 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT2 );
3157 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
3158 ::osl::StreamSocket ssConnection;
3160 /// launch server socket
3161 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
3162 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
3163 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
3164 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
3165 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
3167 //asAcceptorSocket.enableNonBlockingMode( sal_True );
3168 //oslSocketResult eResultAccept = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
3169 //CPPUNIT_ASSERT_MESSAGE( "accept failed.", osl_Socket_Ok == eResultAccept );
3170 /// launch client socket
3171 oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3172 CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_Ok == eResult );
3174 /// get peer information
3175 csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
3177 CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.",
3178 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) &&
3179 ( osl_Socket_Ok == eResult ));
3181 //non-blocking mode connect?
3182 void connect_002()
3184 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
3185 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
3186 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
3188 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
3189 asAcceptorSocket.enableNonBlockingMode( sal_True );
3190 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
3191 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
3192 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
3193 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
3195 csConnectorSocket.enableNonBlockingMode( sal_True );
3197 oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3198 CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_InProgress == eResult || osl_Socket_Ok == eResult );
3200 /// get peer information
3201 csConnectorSocket.getPeerAddr( saPeerSocketAddr );
3203 CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.",
3204 sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) ;
3206 // really an error or just delayed
3207 // how to design scenarios that will return osl_Socket_Interrupted, osl_Socket_TimedOut
3208 void connect_003()
3210 ::osl::SocketAddr saTargetSocketAddr1( aHostIp1, IP_PORT_MYPORT3 );
3211 ::osl::SocketAddr saTargetSocketAddr2( aHostIpInval1, IP_PORT_MYPORT3 );
3213 csConnectorSocket.enableNonBlockingMode( sal_False );
3215 oslSocketResult eResult1 = csConnectorSocket.connect( saTargetSocketAddr1, pTimeout );
3216 oslSocketResult eResult2 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout );
3217 CloseSocketThread myCloseThread( csConnectorSocket );
3218 oslSocketResult eResult3 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout );
3219 myCloseThread.join();
3220 CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult1 &&
3221 osl_Socket_Error == eResult2 && osl_Socket_Error == eResult3 );
3225 // really an error in non-blocking mode
3226 void connect_004()
3228 ::osl::SocketAddr saTargetSocketAddr( aHostIpInval1, IP_PORT_MYPORT3 );
3230 csConnectorSocket.enableNonBlockingMode( sal_True );
3232 oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3233 CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error == eResult );
3235 /** here need a case: immediate connection, say in non-blocking mode connect return osl_Socket_Ok
3238 CPPUNIT_TEST_SUITE( connect );
3239 CPPUNIT_TEST( connect_001 );
3240 CPPUNIT_TEST( connect_002 );
3241 CPPUNIT_TEST( connect_003 );
3242 CPPUNIT_TEST( connect_004 );
3243 CPPUNIT_TEST_SUITE_END();
3245 }; // class connect
3247 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::ctors, "osl_ConnectorSocket");
3248 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ConnectorSocket::connect, "osl_ConnectorSocket");
3250 } // namespace osl_ConnectorSocket
3252 namespace osl_AcceptorSocket
3255 /** testing the methods:
3256 inline AcceptorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
3257 oslProtocol Protocol = osl_Socket_ProtocolIp,
3258 oslSocketType Type = osl_Socket_TypeStream);
3261 class ctors : public CppUnit::TestFixture
3263 public:
3265 void ctors_001()
3267 /// Socket constructor.
3268 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
3270 CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the acceptor socket was created successfully.",
3271 osl_Socket_TypeStream == asSocket.getType( ) );
3274 CPPUNIT_TEST_SUITE( ctors );
3275 CPPUNIT_TEST( ctors_001 );
3276 CPPUNIT_TEST_SUITE_END();
3278 }; // class ctors
3280 /** testing the method:
3281 inline sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1);
3282 inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection);
3283 inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr);
3286 class listen_accept : public CppUnit::TestFixture
3288 public:
3289 TimeValue *pTimeout;
3290 ::osl::AcceptorSocket asAcceptorSocket;
3291 ::osl::ConnectorSocket csConnectorSocket;
3293 // initialization
3294 void setUp( )
3296 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
3297 pTimeout->Seconds = 3;
3298 pTimeout->Nanosec = 0;
3299 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1);
3300 // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
3303 void tearDown( )
3305 free( pTimeout );
3306 // sHandle = NULL;
3307 asAcceptorSocket.close( );
3308 csConnectorSocket.close( );
3311 void listen_accept_001()
3313 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
3314 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
3315 ::osl::StreamSocket ssConnection;
3317 /// launch server socket
3318 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
3319 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
3320 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
3321 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
3322 asAcceptorSocket.enableNonBlockingMode( sal_True );
3324 /// launch client socket
3325 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3327 oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
3329 CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept.",
3330 ( osl_Socket_Ok == eResult ) );
3333 void listen_accept_002()
3335 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT4 );
3336 ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT4 );
3337 ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
3338 ::osl::StreamSocket ssConnection;
3340 /// launch server socket
3341 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
3342 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
3343 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
3344 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
3345 asAcceptorSocket.enableNonBlockingMode( sal_True );
3347 /// launch client socket
3348 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3350 oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection, saPeerSocketAddr); /// waiting for incoming connection...
3352 CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept, accept with peer address.",
3353 ( sal_True == bOK2 ) &&
3354 ( osl_Socket_Ok == eResult ) &&
3355 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) );
3358 void listen_accept_003()
3363 CPPUNIT_TEST_SUITE( listen_accept );
3364 CPPUNIT_TEST( listen_accept_001 );
3365 CPPUNIT_TEST( listen_accept_002 );
3366 CPPUNIT_TEST_SUITE_END();
3368 }; // class listen_accept
3370 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::ctors, "osl_AcceptorSocket");
3371 //CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::operator_assign, "osl_AcceptorSocket");
3372 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::listen_accept, "osl_AcceptorSocket");
3374 } // namespace osl_AcceptorSocket
3376 namespace osl_DatagramSocket
3379 /** testing the methods:
3380 inline DatagramSocket(oslAddrFamily Family= osl_Socket_FamilyInet,
3381 oslProtocol Protocol= osl_Socket_ProtocolIp,
3382 oslSocketType Type= osl_Socket_TypeDgram);
3385 class ctors : public CppUnit::TestFixture
3387 public:
3389 void ctors_001()
3391 /// Socket constructor.
3392 ::osl::DatagramSocket dsSocket;
3394 CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the datagram socket was created successfully.",
3395 osl_Socket_TypeDgram == dsSocket.getType( ) );
3398 CPPUNIT_TEST_SUITE( ctors );
3399 CPPUNIT_TEST( ctors_001 );
3400 CPPUNIT_TEST_SUITE_END();
3402 }; // class ctors
3404 /**thread do sendTo, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml
3406 class TalkerThread : public Thread
3408 protected:
3409 ::osl::SocketAddr saTargetSocketAddr;
3410 ::osl::DatagramSocket dsSocket;
3412 void SAL_CALL run( )
3414 dsSocket.sendTo( saTargetSocketAddr, pTestString1, strlen( pTestString1 ) + 1 ); // "test socket"
3415 dsSocket.shutdown();
3418 void SAL_CALL onTerminated( )
3422 public:
3423 TalkerThread( ):
3424 saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT9 )
3428 ~TalkerThread( )
3430 if ( isRunning( ) )
3431 t_print("# error: TalkerThread not terminated normally.\n" );
3435 /**thread do listen, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml
3437 class ListenerThread : public Thread
3439 protected:
3440 ::osl::SocketAddr saTargetSocketAddr;
3441 ::osl::DatagramSocket dsSocket;
3443 void SAL_CALL run( )
3445 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT10 );
3446 dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
3447 if ( dsSocket.bind( saLocalSocketAddr ) == sal_False )
3449 t_print("DatagramSocket bind failed \n");
3450 return;
3452 //blocking mode: default
3453 #if !SILENT_TEST
3454 sal_Int32 nRecv =
3455 #endif
3456 dsSocket.recvFrom( pRecvBuffer, 30, &saTargetSocketAddr);
3457 t_print("After recvFrom, nRecv is %d\n", (int) nRecv);
3460 void SAL_CALL onTerminated( )
3464 public:
3465 sal_Char pRecvBuffer[30];
3466 ListenerThread( ):
3467 saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT10 )
3469 pRecvBuffer[0] = '\0';
3472 ~ListenerThread( )
3474 if ( isRunning( ) )
3475 t_print("# error: ListenerThread not terminated normally.\n" );
3480 /** testing the methods:
3481 inline sal_Int32 DatagramSocket::recvFrom(void* pBuffer, sal_uInt32 BufferSize,
3482 SocketAddr* pSenderAddr, oslSocketMsgFlag Flag )
3483 inline sal_Int32 DatagramSocket::sendTo( const SocketAddr& ReceiverAddr,
3484 const void* pBuffer, sal_uInt32 BufferSize, oslSocketMsgFlag Flag )
3487 class sendTo_recvFrom : public CppUnit::TestFixture
3489 public:
3491 void sr_001()
3493 ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 );
3494 ::osl::DatagramSocket dsSocket;
3495 dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
3496 dsSocket.bind( saLocalSocketAddr );
3498 sal_Char pReadBuffer[30];
3499 TalkerThread myTalkThread;
3500 myTalkThread.create();
3501 sal_Int32 nRecv = dsSocket.recvFrom( pReadBuffer, 30, &saLocalSocketAddr);
3502 myTalkThread.join();
3503 //t_print("#received buffer is %s# \n", pReadBuffer);
3505 sal_Bool bOk = ( strcmp(pReadBuffer, pTestString1) == 0 );
3507 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.",
3508 nRecv > 0 && bOk == sal_True );
3511 void sr_002()
3513 ::osl::SocketAddr saListenSocketAddr( aHostIp1, IP_PORT_MYPORT10 );
3514 ::osl::DatagramSocket dsSocket;
3516 //listener thread construct a DatagramSocket, recvFrom waiting for data, then main thread sendto data
3517 ListenerThread myListenThread;
3518 myListenThread.create();
3519 //to grantee the recvFrom is before sendTo
3520 thread_sleep( 1 );
3522 sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 );
3524 CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo failed: nSend <= 0.", nSend > 0);
3526 myListenThread.join();
3527 //t_print("#received buffer is %s# \n", myListenThread.pRecvBuffer);
3529 sal_Bool bOk = ( strcmp( myListenThread.pRecvBuffer, pTestString2) == 0 );
3531 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.",
3532 bOk == sal_True );
3535 //sendTo error, return -1; recvFrom error, return -1
3536 void sr_003()
3538 ::osl::SocketAddr saListenSocketAddr( aHostIpInval1, IP_PORT_MYPORT10 );
3539 ::osl::DatagramSocket dsSocket;
3540 // Transport endpoint is not connected
3541 sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 );
3542 CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo should fail: nSend <= 0.",
3543 nSend == -1 );
3546 void sr_004()
3548 ::osl::SocketAddr saListenSocketAddr1( aHostIpInval1, IP_PORT_MYPORT10 );
3549 ::osl::SocketAddr saListenSocketAddr2( aHostIp2, IP_PORT_MYPORT10 );
3550 ::osl::DatagramSocket dsSocket;
3552 dsSocket.enableNonBlockingMode( sal_True );
3554 sal_Char pReadBuffer[30];
3555 //sal_Int32 nRecv1 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 );
3557 // will block ?
3558 CloseSocketThread myThread( dsSocket );
3559 myThread.create();
3560 sal_Int32 nRecv2 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 );
3561 myThread.join();
3562 //t_print("#nRecv1 is %d nRecv2 is %d\n", nRecv1, nRecv2 );
3563 CPPUNIT_ASSERT_MESSAGE( "DatagramSocket sendTo should fail: nSend <= 0.",
3564 nRecv2 == -1 );
3567 CPPUNIT_TEST_SUITE( sendTo_recvFrom );
3568 CPPUNIT_TEST( sr_001 );
3569 CPPUNIT_TEST( sr_002 );
3570 CPPUNIT_TEST( sr_003 );
3571 CPPUNIT_TEST( sr_004 );
3572 CPPUNIT_TEST_SUITE_END();
3574 }; // class sendTo_recvFrom
3576 CPPUNIT_TEST_SUITE_REGISTRATION(osl_DatagramSocket::ctors);
3577 CPPUNIT_TEST_SUITE_REGISTRATION(osl_DatagramSocket::sendTo_recvFrom);
3579 } // namespace osl_DatagramSocket
3581 // this macro creates an empty function, which will called by the RegisterAllFunctions()
3582 // to let the user the possibility to also register some functions by hand.
3583 CPPUNIT_PLUGIN_IMPLEMENT();
3585 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */