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