xref: /trunk/main/sal/qa/osl/socket/osl_Socket2.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
30 
31 /**  test coder preface:
32     1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
33     if you are not including ws2_32.lib in makefile.mk,  the including format will be like this:
34 
35     .IF "$(GUI)" == "WNT"
36     SHL1STDLIBS +=  $(SOLARLIBDIR)$/cppunit.lib
37     SHL1STDLIBS +=  ws2_32.lib
38     .ENDIF
39 
40     likewise on Solaris platform.
41     .IF "$(GUI)" == "UNX"
42     SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
43     SHL1STDLIBS += -lsocket -ldl -lnsl
44     .ENDIF
45 
46     2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
47     category.
48 
49     3. some fragment of Socket source implementation are lack of comment so it is hard for testers
50     guess what the exact functionality or usage of a member.  Hope the Socket section's comment
51     will be added.
52 
53     4. following functions are declared but not implemented:
54     inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
55  */
56 
57 //------------------------------------------------------------------------
58 // include files
59 //------------------------------------------------------------------------
60 
61 #include <testshl/simpleheader.hxx>
62 
63 //#include "osl_Socket_Const.h"
64 #include "sockethelper.hxx"
65 
66 using namespace osl;
67 using namespace rtl;
68 
69 #define IP_PORT_FTP     21
70 #define IP_PORT_TELNET  23
71 #define IP_PORT_HTTP2   8080
72 #define IP_PORT_INVAL   99999
73 #define IP_PORT_POP3    110
74 #define IP_PORT_NETBIOS 139
75 #define IP_PORT_MYPORT  8881
76 #define IP_PORT_MYPORT1 8882
77 #define IP_PORT_MYPORT5 8886
78 #define IP_PORT_MYPORT6 8887
79 #define IP_PORT_MYPORT7 8895
80 #define IP_PORT_MYPORT8 8896
81 #define IP_PORT_MYPORT9 8897
82 
83 //------------------------------------------------------------------------
84 // helper functions
85 //------------------------------------------------------------------------
86 
87 // just used to test socket::close() when accepting
88 class AcceptorThread : public Thread
89 {
90     ::osl::AcceptorSocket asAcceptorSocket;
91     ::rtl::OUString aHostIP;
92     sal_Bool bOK;
93 protected:
94     void SAL_CALL run( )
95     {
96         ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
97         ::osl::StreamSocket ssStreamConnection;
98 
99         asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
100         sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
101         if  ( sal_True != bOK1 )
102         {
103             t_print("# AcceptorSocket bind address failed.\n" ) ;
104             return;
105         }
106         sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
107         if  ( sal_True != bOK2 )
108         {
109             t_print("# AcceptorSocket listen address failed.\n" ) ;
110             return;
111         }
112 
113         asAcceptorSocket.enableNonBlockingMode( sal_False );
114 
115         oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
116         if (eResult != osl_Socket_Ok )
117         {
118             bOK = sal_True;
119             t_print("AcceptorThread: acceptConnection failed! \n");
120         }
121     }
122 public:
123     AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
124         : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
125     {
126         bOK = sal_False;
127     }
128 
129     sal_Bool isOK() { return bOK; }
130 
131     ~AcceptorThread( )
132     {
133         if ( isRunning( ) )
134         {
135             asAcceptorSocket.shutdown();
136             t_print("# error: Acceptor thread not terminated.\n" );
137         }
138     }
139 };
140 
141 namespace osl_Socket
142 {
143 
144     /** testing the methods:
145         inline Socket( );
146         inline Socket( const Socket & socket );
147         inline Socket( oslSocket socketHandle );
148         inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire );
149     */
150 
151     /**  test writer's comment:
152 
153         class Socket can not be initialized by its protected constructor, though the protected
154         constructor is the most convenient way to create a new socket.
155         it only allow the method of C function osl_createSocket like:
156         ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream,
157                                           osl_Socket_ProtocolIp ) );
158         the use of C method lost some of the transparent of tester using C++ wrapper.
159     */
160 
161 
162     class ctors : public CppUnit::TestFixture
163     {
164     public:
165         oslSocket sHandle;
166         // initialization
167         void setUp( )
168         {
169             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
170         }
171 
172         void tearDown( )
173         {
174             sHandle = NULL;
175         }
176 
177 
178         void ctors_none()
179         {
180             /// Socket constructor.
181             // ::osl::Socket sSocket();
182 
183             CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the socket was created successfully, if no exception occured",
184                                     1 == 1 );
185         }
186 
187         void ctors_acquire()
188         {
189             /// Socket constructor.
190             ::osl::Socket sSocket( sHandle );
191 
192             CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
193                                     osl_Socket_TypeStream == sSocket.getType( ) );
194         }
195 
196         void ctors_no_acquire()
197         {
198             /// Socket constructor.
199             ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE );
200 
201             CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
202                                     osl_Socket_TypeStream == sSocket.getType( ) );
203         }
204 
205         void ctors_copy_ctor()
206         {
207             ::osl::Socket sSocket( sHandle );
208             /// Socket copy constructor.
209             ::osl::Socket copySocket( sSocket );
210 
211             CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
212                                     osl_Socket_TypeStream == copySocket.getType( ) );
213         }
214 
215         void ctors_TypeRaw()
216         {
217 #ifdef WNT
218             oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
219 // LLA: ?           ::osl::Socket sSocket( sHandleRaw );
220             CPPUNIT_ASSERT_MESSAGE( " type osl_Socket_TypeRaw socket create failed on UNX ", sHandleRaw != NULL);
221 #else
222             oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
223             CPPUNIT_ASSERT_MESSAGE( " can't create socket with type osl_Socket_TypeRaw within UNX is ok.", sHandleRaw == NULL);
224 #endif
225         }
226 
227         void ctors_family_Ipx()
228         {
229             oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
230             CPPUNIT_ASSERT_MESSAGE( " family osl_Socket_FamilyIpx socket create failed! ", sHandleIpx != NULL);
231             ::osl::Socket sSocket( sHandleIpx );        //, SAL_NO_ACQUIRE );
232             t_print("#Type is %d \n", sSocket.getType( ) );
233 
234             CPPUNIT_ASSERT_MESSAGE(" test for create new Socket instance that family is osl_Socket_FamilyIpx",
235                                     osl_Socket_TypeStream == sSocket.getType( ) );
236         }
237 
238 
239 
240         CPPUNIT_TEST_SUITE( ctors );
241         CPPUNIT_TEST( ctors_none );
242         CPPUNIT_TEST( ctors_acquire );
243         CPPUNIT_TEST( ctors_no_acquire );
244         CPPUNIT_TEST( ctors_copy_ctor );
245         CPPUNIT_TEST( ctors_TypeRaw );
246         CPPUNIT_TEST( ctors_family_Ipx );
247         CPPUNIT_TEST_SUITE_END();
248 
249     }; // class ctors
250 
251 
252     /** testing the methods:
253         inline Socket& SAL_CALL operator= ( oslSocket socketHandle);
254         inline Socket& SAL_CALL operator= (const Socket& sock);
255         inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ;
256         inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const;
257     */
258 
259     class operators : public CppUnit::TestFixture
260     {
261     public:
262         oslSocket sHandle;
263         // initialization
264         void setUp( )
265         {
266             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
267         }
268 
269         void tearDown( )
270         {
271             sHandle = NULL;
272         }
273 
274 
275     /**  test writer's comment:
276 
277         the assignment operator does not support direct assinment like:
278         ::osl::Socket sSocket = sHandle.
279     */
280         void operators_assignment_handle()
281         {
282             ::osl::Socket sSocket(sHandle);
283             ::osl::Socket assignSocket = sSocket.getHandle();
284 
285             CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
286                                     osl_Socket_TypeStream == assignSocket.getType( )  );
287         }
288 
289         void operators_assignment()
290         {
291             ::osl::Socket sSocket( sHandle );
292             ::osl::Socket assignSocket = sSocket;
293 
294             CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
295                                     osl_Socket_TypeStream == assignSocket.getType( ) );
296         }
297 
298         void operators_equal_handle_001()
299         {
300             /// Socket constructor.
301             ::osl::Socket sSocket( sHandle );
302             ::osl::Socket equalSocket = sSocket;
303 
304             CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check equal.",
305                                     equalSocket == sHandle );
306         }
307 
308         void operators_equal_handle_002()
309         {
310             /// Socket constructor.
311             ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
312 
313             CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check unequal.",
314                                     !( equalSocket == sHandle ) );
315         }
316 
317         void operators_equal_001()
318         {
319             ::osl::Socket sSocket( sHandle );
320             /// Socket copy constructor.
321             ::osl::Socket equalSocket( sSocket );
322 
323             CPPUNIT_ASSERT_MESSAGE(" test for operators_equal function: check equal.",
324                                     equalSocket == sSocket );
325         }
326 
327         void operators_equal_002()
328         {
329             ::osl::Socket sSocket( sHandle );
330             /// Socket copy constructor.
331             ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
332 
333             CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_002 function: check unequal.",
334                                     !( equalSocket == sSocket ) );
335         }
336 
337         CPPUNIT_TEST_SUITE( operators );
338         CPPUNIT_TEST( operators_assignment_handle );
339         CPPUNIT_TEST( operators_assignment );
340         CPPUNIT_TEST( operators_equal_handle_001 );
341         CPPUNIT_TEST( operators_equal_handle_002 );
342         CPPUNIT_TEST( operators_equal_001 );
343         CPPUNIT_TEST( operators_equal_002 );
344         CPPUNIT_TEST_SUITE_END();
345 
346     }; // class operators
347 
348 
349     /** testing the methods:
350         inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite );
351         inline void SAL_CALL close();
352     */
353 
354     class close : public CppUnit::TestFixture
355     {
356     public:
357         oslSocket sHandle;
358         // initialization
359         void setUp( )
360         {
361             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
362         }
363 
364         void tearDown( )
365         {
366             sHandle = NULL;
367         }
368 
369 
370         void close_001()
371         {
372             ::osl::Socket sSocket(sHandle);
373             sSocket.close();
374 
375             CPPUNIT_ASSERT_MESSAGE( "test for close_001 function: this function is reserved for test.",
376                                     sSocket.getHandle() == sHandle );
377         }
378 
379         void close_002()
380         {
381 //#if defined(LINUX)
382             ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
383             AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") );
384             myAcceptorThread.create();
385 
386             thread_sleep( 1 );
387             //when accepting, close the socket, the thread will not block for accepting
388             //man close:Any locks held on the file it was associated with, and owned by the process, are removed
389             asSocket.close();
390             //thread_sleep( 2 );
391             myAcceptorThread.join();
392 
393             CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
394                                 myAcceptorThread.isOK() == sal_True );
395 //#endif
396         }
397 
398         // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( )
399         void close_003()
400         {
401             ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
402             AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("0.0.0.0") );
403             myAcceptorThread.create();
404 
405             thread_sleep( 1 );
406             asSocket.close();
407             myAcceptorThread.join();
408 
409             CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
410                                 myAcceptorThread.isOK() == sal_True );
411         }
412 
413         CPPUNIT_TEST_SUITE( close );
414         CPPUNIT_TEST( close_001 );
415         CPPUNIT_TEST( close_002 );
416         CPPUNIT_TEST( close_003 );
417         CPPUNIT_TEST_SUITE_END();
418 
419     }; // class close
420 
421     /** testing the method:
422         inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const;
423     */
424 
425     class getLocalAddr : public CppUnit::TestFixture
426     {
427     public:
428         oslSocket sHandle;
429         // initialization
430         void setUp( )
431         {
432             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
433         }
434 
435         void tearDown( )
436         {
437             sHandle = NULL;
438         }
439 
440         // get the Address of the local end of the socket
441         void getLocalAddr_001()
442         {
443             ::osl::Socket sSocket(sHandle);
444             ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT8 );
445             ::osl::SocketAddr saLocalSocketAddr;
446 
447             sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
448 
449             sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
450             ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
451             CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
452 
453             sSocket.getLocalAddr( saLocalSocketAddr );
454 
455             sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ;
456 
457             CPPUNIT_ASSERT_MESSAGE( "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.",
458                                     sal_True == bOK );
459         }
460 
461 
462         CPPUNIT_TEST_SUITE( getLocalAddr );
463         CPPUNIT_TEST( getLocalAddr_001 );
464         CPPUNIT_TEST_SUITE_END();
465 
466     }; // class getLocalAddr
467 
468 
469     /** testing the method:
470         inline sal_Int32    SAL_CALL getLocalPort() const;
471     */
472 
473     class getLocalPort : public CppUnit::TestFixture
474     {
475     public:
476         oslSocket sHandle;
477         // initialization
478         void setUp( )
479         {
480             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
481         }
482 
483         void tearDown( )
484         {
485             sHandle = NULL;
486         }
487 
488 
489         void getLocalPort_001()
490         {
491             ::osl::Socket sSocket(sHandle);
492             ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT7 );  // aHostIp1 localhost
493             ::osl::SocketAddr saLocalSocketAddr;
494 
495             sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
496 
497             sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
498             ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
499             CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
500             sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( )  );
501 
502             CPPUNIT_ASSERT_MESSAGE( "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.",
503                                     sal_True == bOK );
504         }
505 
506     /**  test writer's comment:
507 
508         the invalid port number can not be set by giving invalid port number
509         such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be
510         valid,  the only instance that the getLocalPort returns OSL_INVALID_PORT
511         is when saSocketAddr itself is an invalid one, that is , the IP or host name
512         can not be found, then the created socket address is not valid.
513     */
514         void getLocalPort_002()
515         {
516             ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_TELNET);
517 #ifdef WNT
518             ::osl::Socket sSocket(sHandle);
519             sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
520             sSocket.bind( saBindSocketAddr );
521             //Invalid IP, so bind should fail
522             ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
523                 ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT),
524                 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned.");
525             sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) );
526             (void)bOK;
527 #else
528             //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT
529             ::rtl::OUString suError = ::rtl::OUString::createFromAscii( "on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT, but can not create Addr of that case");
530 #endif
531             CPPUNIT_ASSERT_MESSAGE( suError, sal_False );
532 
533         }
534 
535         void getLocalPort_003()
536         {
537             ::osl::Socket sSocket(sHandle);
538             ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL);
539 
540             sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
541 
542             sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
543             ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
544             CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
545             ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
546                 ::rtl::OUString::createFromAscii("34463"),
547                 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned");
548             sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 &&  sSocket.getLocalPort( ) <= 65535);
549 
550             CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
551         }
552 
553         CPPUNIT_TEST_SUITE( getLocalPort );
554         CPPUNIT_TEST( getLocalPort_001 );
555 // LLA:     CPPUNIT_TEST( getLocalPort_002 );
556         CPPUNIT_TEST( getLocalPort_003 );
557         CPPUNIT_TEST_SUITE_END();
558 
559     }; // class getLocalPort
560 
561 
562     /** testing the method:
563         inline ::rtl::OUString SAL_CALL getLocalHost() const;
564 
565         Mindyliu: on Linux, at first it will check the binded in /etc/hosts, if it has the binded IP, it will return the hostname in it;
566         else if the binded IP is "127.0.0.1", it will return "localhost", if it's the machine's ethernet ip such as "129.158.217.90", it
567         will return hostname of current processor such as "aegean.PRC.Sun.COM"
568     */
569 
570     class getLocalHost : public CppUnit::TestFixture
571     {
572     public:
573         oslSocket sHandle;
574         // initialization
575         void setUp( )
576         {
577             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
578         }
579 
580         void tearDown( )
581         {
582             sHandle = NULL;
583         }
584 
585 
586         void getLocalHost_001()
587         {
588             ::osl::Socket sSocket(sHandle);
589             //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu
590             ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT6 );
591 
592             sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
593 
594             sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
595             ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
596             CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
597             sal_Bool bOK;
598             ::rtl::OUString suError;
599 #ifdef WNT
600             bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
601             suError = outputError(sSocket.getLocalHost( ), getThisHostname( ),
602 "test for getLocalHost function: create localhost socket and check name");
603 #else
604             ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" );
605             sal_Bool bRes1, bRes2;
606             bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ;
607             bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ;
608             bOK = bRes1 || bRes2;
609             suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name");
610 #endif
611             CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
612         }
613 
614         void getLocalHost_002()
615         {
616             ::osl::Socket sSocket(sHandle);
617             ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_POP3);
618             ::osl::SocketAddr saLocalSocketAddr;
619 
620             sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
621             sSocket.bind( saBindSocketAddr );
622             //Invalid IP, so bind should fail
623             sal_Bool bOK = compareUString( sSocket.getLocalHost( ), rtl::OUString::createFromAscii("") ) ;
624             ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), rtl::OUString::createFromAscii(""), "test for getLocalHost function: getLocalHost with invalid SocketAddr");
625 
626             CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
627         }
628 
629         CPPUNIT_TEST_SUITE( getLocalHost );
630         CPPUNIT_TEST( getLocalHost_001 );
631         CPPUNIT_TEST( getLocalHost_002 );
632         CPPUNIT_TEST_SUITE_END();
633 
634     }; // class getLocalHost
635 
636 
637     /** testing the methods:
638         inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const;
639         inline sal_Int32    SAL_CALL getPeerPort() const;
640         inline ::rtl::OUString SAL_CALL getPeerHost() const;
641     */
642     class getPeer : public CppUnit::TestFixture
643     {
644     public:
645         oslSocket sHandle;
646         TimeValue *pTimeout;
647         ::osl::AcceptorSocket asAcceptorSocket;
648         ::osl::ConnectorSocket csConnectorSocket;
649 
650 
651         // initialization
652         void setUp( )
653         {
654             pTimeout  = ( TimeValue* )malloc( sizeof( TimeValue ) );
655             pTimeout->Seconds = 3;
656             pTimeout->Nanosec = 0;
657             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
658         }
659 
660         void tearDown( )
661         {
662             free( pTimeout );
663             sHandle = NULL;
664             asAcceptorSocket.close( );
665             csConnectorSocket.close( );
666         }
667 
668 
669         void getPeer_001()
670         {
671             ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
672             ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
673             ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
674             ::osl::StreamSocket ssConnection;
675             asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
676             /// launch server socket
677             sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
678             CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind '127.0.0.1' address failed.", sal_True == bOK1 );
679             sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
680             CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.",  sal_True == bOK2 );
681 
682             asAcceptorSocket.enableNonBlockingMode( sal_True );
683             asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
684 
685             /// launch client socket
686             csConnectorSocket.connect( saTargetSocketAddr, pTimeout );   /// connecting to server...
687 
688             /// get peer information
689             csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
690             sal_Int32 peerPort = csConnectorSocket.getPeerPort( );
691             ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( );
692 
693             CPPUNIT_ASSERT_MESSAGE( "test for getPeer function: setup a connection and then get the peer address, port and host from client side.",
694                                     ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&&
695                                     ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) &&
696                                     ( peerPort == saLocalSocketAddr.getPort( ) ));
697         }
698 
699 
700         CPPUNIT_TEST_SUITE( getPeer );
701         CPPUNIT_TEST( getPeer_001 );
702         CPPUNIT_TEST_SUITE_END();
703 
704     }; // class getPeer
705 
706 
707     /** testing the methods:
708         inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface);
709     */
710 
711 
712     class bind : public CppUnit::TestFixture
713     {
714     public:
715         oslSocket sHandle;
716         // initialization
717         void setUp( )
718         {
719             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
720         }
721 
722         void tearDown( )
723         {
724             sHandle = NULL;
725         }
726 
727 
728         void bind_001()
729         {
730             ::osl::Socket sSocket(sHandle);
731             //bind must use local IP address ---mindyliu
732             ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 );
733 
734             sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
735             sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
736             CPPUNIT_ASSERT_MESSAGE( "Socket bind fail.", sal_True == bOK1 );
737 
738             sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ;
739 
740             sSocket.close();
741             CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", sal_True == bOK2 );
742         }
743 
744         void bind_002()
745         {
746             ::osl::Socket sSocket(sHandle);
747             ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_NETBIOS );
748             ::osl::SocketAddr saLocalSocketAddr;
749 
750             sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True);
751             sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
752             sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
753 
754             CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.",
755                                     ( sal_False == bOK1 ) && ( sal_False == bOK2 ) );
756         }
757 
758         CPPUNIT_TEST_SUITE( bind );
759         CPPUNIT_TEST( bind_001 );
760         CPPUNIT_TEST( bind_002 );
761         CPPUNIT_TEST_SUITE_END();
762 
763     }; // class bind
764 
765 
766     /** testing the methods:
767         inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const;
768 
769     */
770     class isRecvReady : public CppUnit::TestFixture
771     {
772     public:
773         oslSocket sHandle;
774         TimeValue *pTimeout;
775         ::osl::AcceptorSocket asAcceptorSocket;
776         ::osl::ConnectorSocket csConnectorSocket;
777 
778 
779         // initialization
780         void setUp( )
781         {
782             pTimeout  = ( TimeValue* )malloc( sizeof( TimeValue ) );
783             pTimeout->Seconds = 3;
784             pTimeout->Nanosec = 0;
785             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
786         }
787 
788         void tearDown( )
789         {
790             free( pTimeout );
791             sHandle = NULL;
792             asAcceptorSocket.close( );
793             csConnectorSocket.close( );
794         }
795 
796 
797         void isRecvReady_001()
798         {
799             ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 );
800             ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 );
801             ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
802             ::osl::StreamSocket ssConnection;
803             /// launch server socket
804             asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
805             sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
806             CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
807             sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
808             CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.",  sal_True == bOK2 );
809             asAcceptorSocket.enableNonBlockingMode( sal_True );
810             asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
811 
812             /// launch client socket
813             csConnectorSocket.connect( saTargetSocketAddr, pTimeout );   /// connecting to server...
814 
815             /// is receive ready?
816             sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout );
817 
818             CPPUNIT_ASSERT_MESSAGE( "test for isRecvReady function: setup a connection and then check if it can transmit data.",
819                                     ( sal_True == bOK3 ) );
820         }
821 
822 
823         CPPUNIT_TEST_SUITE( isRecvReady );
824         CPPUNIT_TEST( isRecvReady_001 );
825         CPPUNIT_TEST_SUITE_END();
826 
827     }; // class isRecvReady
828 
829 
830     /** testing the methods:
831         inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const;
832     */
833     class isSendReady : public CppUnit::TestFixture
834     {
835     public:
836         oslSocket sHandle;
837         TimeValue *pTimeout;
838         ::osl::AcceptorSocket asAcceptorSocket;
839         ::osl::ConnectorSocket csConnectorSocket;
840 
841 
842         // initialization
843         void setUp( )
844         {
845             pTimeout  = ( TimeValue* )malloc( sizeof( TimeValue ) );
846             pTimeout->Seconds = 3;
847             pTimeout->Nanosec = 0;
848             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
849         }
850 
851         void tearDown( )
852         {
853             free( pTimeout );
854             sHandle = NULL;
855             asAcceptorSocket.close( );
856             csConnectorSocket.close( );
857         }
858 
859 
860         void isSendReady_001()
861         {
862             ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
863             ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
864             ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
865             ::osl::StreamSocket ssConnection;
866 
867             /// launch server socket
868             asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
869             sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
870             CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
871             sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
872             CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.",  sal_True == bOK2 );
873             asAcceptorSocket.enableNonBlockingMode( sal_True );
874             asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
875 
876             /// launch client socket
877             csConnectorSocket.connect( saTargetSocketAddr, pTimeout );   /// connecting to server...
878 
879             /// is send ready?
880             sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout );
881 
882             CPPUNIT_ASSERT_MESSAGE( "test for isSendReady function: setup a connection and then check if it can transmit data.",
883                                     ( sal_True == bOK3 ) );
884         }
885 
886 
887         CPPUNIT_TEST_SUITE( isSendReady );
888         CPPUNIT_TEST( isSendReady_001 );
889         CPPUNIT_TEST_SUITE_END();
890 
891     }; // class isSendReady
892 
893 
894     /** testing the methods:
895         inline oslSocketType    SAL_CALL getType() const;
896 
897     */
898 
899     class getType : public CppUnit::TestFixture
900     {
901     public:
902         oslSocket sHandle;
903         // initialization
904         void setUp( )
905         {
906 
907         }
908 
909         void tearDown( )
910         {
911             sHandle = NULL;
912         }
913 
914 
915         void getType_001()
916         {
917             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
918             ::osl::Socket sSocket(sHandle);
919 
920             CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
921                                     osl_Socket_TypeStream ==  sSocket.getType( ) );
922         }
923 
924         void getType_002()
925         {
926             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
927             ::osl::Socket sSocket(sHandle);
928 
929             CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
930                                     osl_Socket_TypeDgram ==  sSocket.getType( ) );
931         }
932 
933 #ifdef UNX
934         // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here
935         // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw()
936         void getType_003()
937         {
938             CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.this is not passed in (LINUX, SOLARIS), the osl_Socket_TypeRaw, type socket can not be created.",
939                                     sal_True);
940         }
941 #else
942         void getType_003()
943         {
944             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
945             ::osl::Socket sSocket(sHandle);
946 
947             CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
948                                     osl_Socket_TypeRaw ==  sSocket.getType( ) );
949         }
950 #endif
951 
952         CPPUNIT_TEST_SUITE( getType );
953         CPPUNIT_TEST( getType_001 );
954         CPPUNIT_TEST( getType_002 );
955         CPPUNIT_TEST( getType_003 );
956         CPPUNIT_TEST_SUITE_END();
957 
958     }; // class getType
959 
960 
961 
962     /** testing the methods:
963         inline sal_Int32 SAL_CALL getOption(
964             oslSocketOption Option,
965             void* pBuffer,
966             sal_uInt32 BufferLen,
967             oslSocketOptionLevel Level= osl_Socket_LevelSocket) const;
968 
969         inline sal_Int32 getOption( oslSocketOption option ) const;
970 
971     */
972 
973     class getOption : public CppUnit::TestFixture
974     {
975     public:
976         oslSocket sHandle;
977         // initialization
978         void setUp( )
979         {
980 
981         }
982 
983         void tearDown( )
984         {
985             sHandle = NULL;
986         }
987 
988         /**  test writer's comment:
989 
990             in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream.
991             2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ
992             in 1.
993         */
994 
995         void getOption_001()
996         {
997             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
998             ::osl::Socket sSocket(sHandle);
999             sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
1000             *pType = 0;
1001             sSocket.getOption( osl_Socket_OptionType,  pType, sizeof ( sal_Int32 ) );
1002             sal_Bool bOK = ( SOCK_STREAM ==  *pType );
1003             // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream
1004             //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) ==  *pType );
1005             free( pType );
1006 
1007             CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get type option of socket.",
1008                                     sal_True == bOK );
1009         }
1010 
1011         // getsockopt error
1012         void getOption_004()
1013         {
1014             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
1015             ::osl::Socket sSocket(sHandle);
1016 
1017             sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
1018             sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid,  pbDontRoute, sizeof ( sal_Bool ) );
1019             free( pbDontRoute );
1020 
1021             CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get invalid option of socket, should return -1.",
1022                                      nRes  ==  -1 );
1023         }
1024 
1025         void getOption_simple_001()
1026         {
1027             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
1028             ::osl::Socket sSocket(sHandle);
1029 
1030             sal_Bool bOK = ( sal_False  ==  sSocket.getOption( osl_Socket_OptionDontRoute ) );
1031 
1032             CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
1033                                     sal_True == bOK );
1034         }
1035 
1036         void getOption_simple_002()
1037         {
1038             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
1039             ::osl::Socket sSocket(sHandle);
1040 
1041             sal_Bool bOK = ( sal_False  ==  sSocket.getOption( osl_Socket_OptionDebug ) );
1042 
1043             CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
1044                                     sal_True == bOK );
1045         }
1046 
1047         CPPUNIT_TEST_SUITE( getOption );
1048         CPPUNIT_TEST( getOption_001 );
1049         CPPUNIT_TEST( getOption_004 );
1050         CPPUNIT_TEST( getOption_simple_001 );
1051         CPPUNIT_TEST( getOption_simple_002 );
1052         CPPUNIT_TEST_SUITE_END();
1053 
1054     }; // class getOption
1055 
1056 
1057     /** testing the methods:
1058         inline sal_Bool SAL_CALL setOption( oslSocketOption Option,
1059                                             void* pBuffer,
1060                                             sal_uInt32 BufferLen,
1061                                             oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const;
1062     */
1063 
1064     class setOption : public CppUnit::TestFixture
1065     {
1066     public:
1067         TimeValue *pTimeout;
1068 // LLA: maybe there is an error in the source,
1069 //      as long as I remember, if a derived class do not overload all ctors there is a problem.
1070 
1071         ::osl::AcceptorSocket asAcceptorSocket;
1072 
1073         void setUp( )
1074         {
1075 
1076         }
1077 
1078         void tearDown( )
1079         {
1080             asAcceptorSocket.close( );
1081         }
1082 
1083 
1084         // LLA:
1085         // getSocketOption returns BufferLen, or -1 if something failed
1086 
1087         // setSocketOption returns sal_True, if option could stored
1088         // else sal_False
1089 
1090         void setOption_001()
1091         {
1092             /// set and get option.
1093             int nBufferLen = sizeof ( sal_Int32);
1094             // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool!
1095 
1096             sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
1097             *pbDontRouteSet = 1; // sal_True;
1098 
1099             sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
1100             *pGetBuffer = 0;
1101 
1102             // maybe asAcceptorSocket is not right initialized
1103             sal_Bool  b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute,  pbDontRouteSet, nBufferLen );
1104             CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b1 ) );
1105             sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute,  pGetBuffer, nBufferLen );
1106             CPPUNIT_ASSERT_MESSAGE( "getOption function failed.", ( n2 == nBufferLen ) );
1107 
1108             // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value,
1109             // just judge it is zero or not!
1110             sal_Bool bOK = ( 0  !=  *pGetBuffer );
1111             t_print("#setOption_001: getOption is %d \n", *pGetBuffer);
1112 
1113             // toggle check, set to 0
1114             *pbDontRouteSet = 0;
1115 
1116             sal_Bool  b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute,  pbDontRouteSet, sizeof ( sal_Int32 ) );
1117             CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b3 ) );
1118             sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute,  pGetBuffer, nBufferLen );
1119             CPPUNIT_ASSERT_MESSAGE( "getOption (DONTROUTE) function failed.", ( n4 == nBufferLen ) );
1120 
1121             sal_Bool bOK2 = ( 0  ==  *pGetBuffer );
1122 
1123             t_print("#setOption_001: getOption is %d \n", *pGetBuffer);
1124 
1125 // LLA:             sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
1126 // LLA:             *pbDontTouteSet = sal_True;
1127 // LLA:             sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
1128 // LLA:             *pbDontTouteGet = sal_False;
1129 // LLA:             asAcceptorSocket.setOption( osl_Socket_OptionDontRoute,  pbDontTouteSet, sizeof ( sal_Bool ) );
1130 // LLA:             asAcceptorSocket.getOption( osl_Socket_OptionDontRoute,  pbDontTouteGet, sizeof ( sal_Bool ) );
1131 // LLA:             ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet),
1132 // LLA:                 ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet),
1133 // LLA:                 "test for setOption function: set osl_Socket_OptionDontRoute and then check");
1134 // LLA:
1135 // LLA:             sal_Bool bOK = ( sal_True  ==  *pbDontTouteGet );
1136 // LLA:             free( pbDontTouteSet );
1137 // LLA:             free( pbDontTouteGet );
1138 
1139             CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1140                                     ( sal_True == bOK ) && (sal_True == bOK2) );
1141 
1142             free( pbDontRouteSet );
1143             free( pGetBuffer );
1144 // LLA:             CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
1145         }
1146 
1147         void setOption_002()
1148         {
1149             /// set and get option.
1150 
1151             // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen );
1152             // *pbLingerSet = 7;
1153             // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen );
1154                     /* struct */linger aLingerSet;
1155                     sal_Int32 nBufferLen = sizeof( struct linger );
1156                     aLingerSet.l_onoff = 1;
1157                     aLingerSet.l_linger = 7;
1158 
1159                 linger aLingerGet;
1160 
1161             asAcceptorSocket.setOption( osl_Socket_OptionLinger,  &aLingerSet, nBufferLen );
1162 
1163             sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger,  &aLingerGet, nBufferLen );
1164                     CPPUNIT_ASSERT_MESSAGE( "getOption (SO_LINGER) function failed.", ( n1 == nBufferLen ) );
1165 
1166             //t_print("#setOption_002: getOption is %d \n", aLingerGet.l_linger);
1167             sal_Bool bOK = ( 7  ==  aLingerGet.l_linger );
1168             CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check. ",
1169                 sal_True == bOK );
1170 
1171         }
1172 
1173         void setOption_003()
1174         {
1175             linger aLingerSet;
1176                 aLingerSet.l_onoff = 1;
1177                     aLingerSet.l_linger = 7;
1178 
1179             sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger,  &aLingerSet, 0 );
1180                     printUString( asAcceptorSocket.getErrorAsString( ) );
1181             CPPUNIT_ASSERT_MESSAGE( "setOption (SO_LINGER) function failed for optlen is 0.",
1182                 ( b1 == sal_False ) );
1183         }
1184 
1185         void setOption_simple_001()
1186         {
1187             /// set and get option.
1188             asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True );
1189             sal_Bool bOK = ( 0  !=  asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
1190 
1191             t_print("setOption_simple_001(): getoption is %d \n", asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
1192             CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1193                                     ( sal_True == bOK ) );
1194         }
1195 
1196         void setOption_simple_002()
1197         {
1198             /// set and get option.
1199             // LLA: this does not work, due to the fact that SO_LINGER is a structure
1200 // LLA:         asAcceptorSocket.setOption( osl_Socket_OptionLinger,  7 );
1201 // LLA:         sal_Bool bOK = ( 7  ==  asAcceptorSocket.getOption( osl_Socket_OptionLinger ) );
1202 
1203 // LLA:         CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1204 // LLA:                                     ( sal_True == bOK ) );
1205         }
1206 
1207         CPPUNIT_TEST_SUITE( setOption );
1208         CPPUNIT_TEST( setOption_001 );
1209         CPPUNIT_TEST( setOption_002 );
1210         CPPUNIT_TEST( setOption_003 );
1211         CPPUNIT_TEST( setOption_simple_001 );
1212 // LLA:     CPPUNIT_TEST( setOption_simple_002 );
1213         CPPUNIT_TEST_SUITE_END();
1214 
1215     }; // class setOption
1216 
1217 
1218 
1219     /** testing the method:
1220         inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode);
1221     */
1222     class enableNonBlockingMode : public CppUnit::TestFixture
1223     {
1224     public:
1225         ::osl::AcceptorSocket asAcceptorSocket;
1226 
1227         void enableNonBlockingMode_001()
1228         {
1229             ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
1230             ::osl::StreamSocket ssConnection;
1231 
1232             /// launch server socket
1233             asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1234             sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
1235             CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
1236             sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
1237             CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.",  sal_True == bOK2 );
1238             asAcceptorSocket.enableNonBlockingMode( sal_True );
1239             asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1240 
1241             /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
1242             sal_Bool bOK  = sal_True;
1243             asAcceptorSocket.close( );
1244 
1245             CPPUNIT_ASSERT_MESSAGE( "test for enableNonBlockingMode function: launch a server socket and make it non blocking. if it can pass the acceptConnection statement, it is non-blocking",
1246                                     ( sal_True == bOK  ) );
1247         }
1248 
1249 
1250         CPPUNIT_TEST_SUITE( enableNonBlockingMode );
1251         CPPUNIT_TEST( enableNonBlockingMode_001 );
1252         CPPUNIT_TEST_SUITE_END();
1253 
1254     }; // class enableNonBlockingMode
1255 
1256 
1257     /** testing the method:
1258         inline sal_Bool SAL_CALL isNonBlockingMode() const;
1259     */
1260     class isNonBlockingMode : public CppUnit::TestFixture
1261     {
1262     public:
1263         ::osl::AcceptorSocket asAcceptorSocket;
1264 
1265         void isNonBlockingMode_001()
1266         {
1267             ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
1268             ::osl::StreamSocket ssConnection;
1269 
1270             /// launch server socket
1271             asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
1272             sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
1273             CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
1274             sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
1275             CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.",  sal_True == bOK2 );
1276 
1277             sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( );
1278             asAcceptorSocket.enableNonBlockingMode( sal_True );
1279             asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1280 
1281             /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
1282             sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( );
1283             asAcceptorSocket.close( );
1284 
1285             CPPUNIT_ASSERT_MESSAGE( "test for isNonBlockingMode function: launch a server socket and make it non blocking. it is expected to change from blocking mode to non-blocking mode.",
1286                                     ( sal_False == bOK3 ) && ( sal_True == bOK4 ) );
1287         }
1288 
1289 
1290         CPPUNIT_TEST_SUITE( isNonBlockingMode );
1291         CPPUNIT_TEST( isNonBlockingMode_001 );
1292         CPPUNIT_TEST_SUITE_END();
1293 
1294     }; // class isNonBlockingMode
1295 
1296     /** testing the method:
1297         inline void SAL_CALL clearError() const;
1298     */
1299     class clearError : public CppUnit::TestFixture
1300     {
1301     public:
1302         oslSocket sHandle;
1303         // initialization
1304         void setUp( )
1305         {
1306             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1307         }
1308 
1309         void tearDown( )
1310         {
1311             sHandle = NULL;
1312         }
1313 
1314 
1315         void clearError_001()
1316         {
1317             ::osl::Socket sSocket(sHandle);
1318             ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_HTTP2 );
1319             ::osl::SocketAddr saLocalSocketAddr;
1320             sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1321             sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
1322             oslSocketError seBind = sSocket.getError( );
1323             sSocket.clearError( );
1324 
1325             CPPUNIT_ASSERT_MESSAGE( "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.",
1326                                     osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None  );
1327         }
1328 
1329 
1330         CPPUNIT_TEST_SUITE( clearError );
1331         CPPUNIT_TEST( clearError_001 );
1332         CPPUNIT_TEST_SUITE_END();
1333 
1334     }; // class clearError
1335 
1336 
1337     /** testing the methods:
1338         inline oslSocketError getError() const;
1339         inline ::rtl::OUString getErrorAsString( ) const;
1340     */
1341     class getError : public CppUnit::TestFixture
1342     {
1343     public:
1344         oslSocket sHandle;
1345         // initialization
1346         void setUp( )
1347         {
1348             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1349         }
1350 
1351         void tearDown( )
1352         {
1353             sHandle = NULL;
1354         }
1355 
1356 
1357         void getError_001()
1358         {
1359             ::osl::Socket sSocket(sHandle);
1360             ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
1361             ::osl::SocketAddr saLocalSocketAddr;
1362 
1363             CPPUNIT_ASSERT_MESSAGE( "test for getError function: should get no error.",
1364                                     osl_Socket_E_None == sSocket.getError( )  );
1365         }
1366 
1367         void getError_002()
1368         {
1369             ::osl::Socket sSocket(sHandle);
1370             ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_FTP );
1371             ::osl::SocketAddr saLocalSocketAddr;
1372             sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1373             sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
1374             //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError.
1375 #if defined(SOLARIS)
1376             CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. ",
1377                                     osl_Socket_E_InvalidError == sSocket.getError( )  );
1378 #else
1379             //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail.
1380 
1381             CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. Passed on Linux & Win32",
1382                                     osl_Socket_E_AddrNotAvail == sSocket.getError( )  );
1383 #endif
1384         }
1385 
1386         CPPUNIT_TEST_SUITE( getError );
1387         CPPUNIT_TEST( getError_001 );
1388         CPPUNIT_TEST( getError_002 );
1389         CPPUNIT_TEST_SUITE_END();
1390 
1391     }; // class getError
1392 
1393 
1394 
1395     /** testing the methods:
1396         inline oslSocket getHandle() const;
1397     */
1398 
1399     class getHandle : public CppUnit::TestFixture
1400     {
1401     public:
1402         oslSocket sHandle;
1403         // initialization
1404         void setUp( )
1405         {
1406             sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1407         }
1408 
1409         void tearDown( )
1410         {
1411             sHandle = NULL;
1412         }
1413 
1414         void getHandle_001()
1415         {
1416             ::osl::Socket sSocket(sHandle);
1417             ::osl::Socket assignSocket = sSocket.getHandle();
1418 
1419             CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
1420                                     osl_Socket_TypeStream == assignSocket.getType( )  );
1421         }
1422 
1423         void getHandle_002()
1424         {
1425             ::osl::Socket sSocket( sHandle );
1426             ::osl::Socket assignSocket ( sSocket.getHandle( ) );
1427 
1428             CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
1429                                     osl_Socket_TypeStream == assignSocket.getType( ) );
1430         }
1431 
1432         CPPUNIT_TEST_SUITE( getHandle );
1433         CPPUNIT_TEST( getHandle_001 );
1434         CPPUNIT_TEST( getHandle_002 );
1435         CPPUNIT_TEST_SUITE_END();
1436 
1437     }; // class getHandle
1438 
1439 
1440 // -----------------------------------------------------------------------------
1441 
1442 
1443 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::ctors, "osl_Socket");
1444 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::operators, "osl_Socket");
1445 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::close, "osl_Socket");
1446 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalAddr, "osl_Socket");
1447 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalPort, "osl_Socket");
1448 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalHost, "osl_Socket");
1449 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getPeer, "osl_Socket");
1450 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::bind, "osl_Socket");
1451 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isRecvReady, "osl_Socket");
1452 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isSendReady, "osl_Socket");
1453 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getType, "osl_Socket");
1454 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getOption, "osl_Socket");
1455 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::setOption, "osl_Socket");
1456 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::enableNonBlockingMode, "osl_Socket");
1457 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isNonBlockingMode, "osl_Socket");
1458 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::clearError, "osl_Socket");
1459 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getError, "osl_Socket");
1460 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getHandle, "osl_Socket");
1461 
1462 } // namespace osl_Socket
1463 
1464 // -----------------------------------------------------------------------------
1465 
1466 // this macro creates an empty function, which will called by the RegisterAllFunctions()
1467 // to let the user the possibility to also register some functions by hand.
1468 NOADDITIONAL;
1469