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