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