1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: osl_Socket2.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 /** test coder preface:
35 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
36 if you are not including ws2_32.lib in makefile.mk, the including format will be like this:
39 SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
40 SHL1STDLIBS += ws2_32.lib
43 likewise on Solaris platform.
45 SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
46 SHL1STDLIBS += -lsocket -ldl -lnsl
49 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
52 3. some fragment of Socket source implementation are lack of comment so it is hard for testers
53 guess what the exact functionality or usage of a member. Hope the Socket section's comment
56 4. following functions are declared but not implemented:
57 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
60 //------------------------------------------------------------------------
62 //------------------------------------------------------------------------
64 #include <cppunit/simpleheader.hxx>
66 //#include "osl_Socket_Const.h"
67 #include "sockethelper.hxx"
72 #define IP_PORT_FTP 21
73 #define IP_PORT_TELNET 23
74 #define IP_PORT_HTTP2 8080
75 #define IP_PORT_INVAL 99999
76 #define IP_PORT_POP3 110
77 #define IP_PORT_NETBIOS 139
78 #define IP_PORT_MYPORT 8881
79 #define IP_PORT_MYPORT1 8882
80 #define IP_PORT_MYPORT5 8886
81 #define IP_PORT_MYPORT6 8887
82 #define IP_PORT_MYPORT7 8895
83 #define IP_PORT_MYPORT8 8896
84 #define IP_PORT_MYPORT9 8897
86 //------------------------------------------------------------------------
88 //------------------------------------------------------------------------
90 // just used to test socket::close() when accepting
91 class AcceptorThread
: public Thread
93 ::osl::AcceptorSocket asAcceptorSocket
;
94 ::rtl::OUString aHostIP
;
99 ::osl::SocketAddr
saLocalSocketAddr( aHostIP
, IP_PORT_MYPORT9
);
100 ::osl::StreamSocket ssStreamConnection
;
102 asAcceptorSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //integer not sal_Bool : sal_True);
103 sal_Bool bOK1
= asAcceptorSocket
.bind( saLocalSocketAddr
);
104 if ( sal_True
!= bOK1
)
106 t_print("# AcceptorSocket bind address failed.\n" ) ;
109 sal_Bool bOK2
= asAcceptorSocket
.listen( 1 );
110 if ( sal_True
!= bOK2
)
112 t_print("# AcceptorSocket listen address failed.\n" ) ;
116 asAcceptorSocket
.enableNonBlockingMode( sal_False
);
118 oslSocketResult eResult
= asAcceptorSocket
.acceptConnection( ssStreamConnection
);
119 if (eResult
!= osl_Socket_Ok
)
122 t_print("AcceptorThread: acceptConnection failed! \n");
126 AcceptorThread(::osl::AcceptorSocket
& asSocket
, ::rtl::OUString
const& aBindIP
)
127 : asAcceptorSocket( asSocket
), aHostIP( aBindIP
)
132 sal_Bool
isOK() { return bOK
; }
138 asAcceptorSocket
.shutdown();
139 t_print("# error: Acceptor thread not terminated.\n" );
147 /** testing the methods:
149 inline Socket( const Socket & socket );
150 inline Socket( oslSocket socketHandle );
151 inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire );
154 /** test writer's comment:
156 class Socket can not be initialized by its protected constructor, though the protected
157 constructor is the most convenient way to create a new socket.
158 it only allow the method of C function osl_createSocket like:
159 ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream,
160 osl_Socket_ProtocolIp ) );
161 the use of C method lost some of the transparent of tester using C++ wrapper.
165 class ctors
: public CppUnit::TestFixture
172 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
183 /// Socket constructor.
184 // ::osl::Socket sSocket();
186 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the socket was created successfully, if no exception occured",
192 /// Socket constructor.
193 ::osl::Socket
sSocket( sHandle
);
195 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
196 osl_Socket_TypeStream
== sSocket
.getType( ) );
199 void ctors_no_acquire()
201 /// Socket constructor.
202 ::osl::Socket
sSocket( sHandle
, SAL_NO_ACQUIRE
);
204 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
205 osl_Socket_TypeStream
== sSocket
.getType( ) );
208 void ctors_copy_ctor()
210 ::osl::Socket
sSocket( sHandle
);
211 /// Socket copy constructor.
212 ::osl::Socket
copySocket( sSocket
);
214 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
215 osl_Socket_TypeStream
== copySocket
.getType( ) );
221 oslSocket sHandleRaw
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeRaw
, osl_Socket_ProtocolIp
);
222 // LLA: ? ::osl::Socket sSocket( sHandleRaw );
223 CPPUNIT_ASSERT_MESSAGE( " type osl_Socket_TypeRaw socket create failed on UNX ", sHandleRaw
!= NULL
);
225 oslSocket sHandleRaw
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeRaw
, osl_Socket_ProtocolIp
);
226 CPPUNIT_ASSERT_MESSAGE( " can't create socket with type osl_Socket_TypeRaw within UNX is ok.", sHandleRaw
== NULL
);
230 void ctors_family_Ipx()
232 oslSocket sHandleIpx
= osl_createSocket( osl_Socket_FamilyIpx
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
233 CPPUNIT_ASSERT_MESSAGE( " family osl_Socket_FamilyIpx socket create failed! ", sHandleIpx
!= NULL
);
234 ::osl::Socket
sSocket( sHandleIpx
); //, SAL_NO_ACQUIRE );
235 t_print("#Type is %d \n", sSocket
.getType( ) );
237 CPPUNIT_ASSERT_MESSAGE(" test for create new Socket instance that family is osl_Socket_FamilyIpx",
238 osl_Socket_TypeStream
== sSocket
.getType( ) );
243 CPPUNIT_TEST_SUITE( ctors
);
244 CPPUNIT_TEST( ctors_none
);
245 CPPUNIT_TEST( ctors_acquire
);
246 CPPUNIT_TEST( ctors_no_acquire
);
247 CPPUNIT_TEST( ctors_copy_ctor
);
248 CPPUNIT_TEST( ctors_TypeRaw
);
249 CPPUNIT_TEST( ctors_family_Ipx
);
250 CPPUNIT_TEST_SUITE_END();
255 /** testing the methods:
256 inline Socket& SAL_CALL operator= ( oslSocket socketHandle);
257 inline Socket& SAL_CALL operator= (const Socket& sock);
258 inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ;
259 inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const;
262 class operators
: public CppUnit::TestFixture
269 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
278 /** test writer's comment:
280 the assignment operator does not support direct assinment like:
281 ::osl::Socket sSocket = sHandle.
283 void operators_assignment_handle()
285 ::osl::Socket
sSocket(sHandle
);
286 ::osl::Socket assignSocket
= sSocket
.getHandle();
288 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
289 osl_Socket_TypeStream
== assignSocket
.getType( ) );
292 void operators_assignment()
294 ::osl::Socket
sSocket( sHandle
);
295 ::osl::Socket assignSocket
= sSocket
;
297 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
298 osl_Socket_TypeStream
== assignSocket
.getType( ) );
301 void operators_equal_handle_001()
303 /// Socket constructor.
304 ::osl::Socket
sSocket( sHandle
);
305 ::osl::Socket equalSocket
= sSocket
;
307 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check equal.",
308 equalSocket
== sHandle
);
311 void operators_equal_handle_002()
313 /// Socket constructor.
314 ::osl::Socket
equalSocket( osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeDgram
, osl_Socket_ProtocolIp
) );
316 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check unequal.",
317 !( equalSocket
== sHandle
) );
320 void operators_equal_001()
322 ::osl::Socket
sSocket( sHandle
);
323 /// Socket copy constructor.
324 ::osl::Socket
equalSocket( sSocket
);
326 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal function: check equal.",
327 equalSocket
== sSocket
);
330 void operators_equal_002()
332 ::osl::Socket
sSocket( sHandle
);
333 /// Socket copy constructor.
334 ::osl::Socket
equalSocket( osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeDgram
, osl_Socket_ProtocolIp
) );
336 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_002 function: check unequal.",
337 !( equalSocket
== sSocket
) );
340 CPPUNIT_TEST_SUITE( operators
);
341 CPPUNIT_TEST( operators_assignment_handle
);
342 CPPUNIT_TEST( operators_assignment
);
343 CPPUNIT_TEST( operators_equal_handle_001
);
344 CPPUNIT_TEST( operators_equal_handle_002
);
345 CPPUNIT_TEST( operators_equal_001
);
346 CPPUNIT_TEST( operators_equal_002
);
347 CPPUNIT_TEST_SUITE_END();
349 }; // class operators
352 /** testing the methods:
353 inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite );
354 inline void SAL_CALL close();
357 class close
: public CppUnit::TestFixture
364 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
375 ::osl::Socket
sSocket(sHandle
);
378 CPPUNIT_ASSERT_MESSAGE( "test for close_001 function: this function is reserved for test.",
379 sSocket
.getHandle() == sHandle
);
385 ::osl::AcceptorSocket
asSocket( osl_Socket_FamilyInet
, osl_Socket_ProtocolIp
, osl_Socket_TypeStream
);
386 AcceptorThread
myAcceptorThread( asSocket
, rtl::OUString::createFromAscii("127.0.0.1") );
387 myAcceptorThread
.create();
390 //when accepting, close the socket, the thread will not block for accepting
391 //man close:Any locks held on the file it was associated with, and owned by the process, are removed
394 myAcceptorThread
.join();
396 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
397 myAcceptorThread
.isOK() == sal_True
);
401 // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( )
404 ::osl::AcceptorSocket
asSocket( osl_Socket_FamilyInet
, osl_Socket_ProtocolIp
, osl_Socket_TypeStream
);
405 AcceptorThread
myAcceptorThread( asSocket
, rtl::OUString::createFromAscii("0.0.0.0") );
406 myAcceptorThread
.create();
410 myAcceptorThread
.join();
412 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
413 myAcceptorThread
.isOK() == sal_True
);
416 CPPUNIT_TEST_SUITE( close
);
417 CPPUNIT_TEST( close_001
);
418 CPPUNIT_TEST( close_002
);
419 CPPUNIT_TEST( close_003
);
420 CPPUNIT_TEST_SUITE_END();
424 /** testing the method:
425 inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const;
428 class getLocalAddr
: public CppUnit::TestFixture
435 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
443 // get the Address of the local end of the socket
444 void getLocalAddr_001()
446 ::osl::Socket
sSocket(sHandle
);
447 ::osl::SocketAddr
saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT8
);
448 ::osl::SocketAddr saLocalSocketAddr
;
450 sSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
452 sal_Bool bOK1
= sSocket
.bind( saBindSocketAddr
);
453 ::rtl::OUString suError1
= ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket
.getErrorAsString();
454 CPPUNIT_ASSERT_MESSAGE( suError1
, sal_True
== bOK1
);
456 sSocket
.getLocalAddr( saLocalSocketAddr
);
458 sal_Bool bOK
= compareUString( saLocalSocketAddr
.getHostname( 0 ), sSocket
.getLocalHost() ) ;
460 CPPUNIT_ASSERT_MESSAGE( "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.",
465 CPPUNIT_TEST_SUITE( getLocalAddr
);
466 CPPUNIT_TEST( getLocalAddr_001
);
467 CPPUNIT_TEST_SUITE_END();
469 }; // class getLocalAddr
472 /** testing the method:
473 inline sal_Int32 SAL_CALL getLocalPort() const;
476 class getLocalPort
: public CppUnit::TestFixture
483 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
492 void getLocalPort_001()
494 ::osl::Socket
sSocket(sHandle
);
495 ::osl::SocketAddr
saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT7
); // aHostIp1 localhost
496 ::osl::SocketAddr saLocalSocketAddr
;
498 sSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
500 sal_Bool bOK1
= sSocket
.bind( saBindSocketAddr
);
501 ::rtl::OUString suError1
= ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket
.getErrorAsString();
502 CPPUNIT_ASSERT_MESSAGE( suError1
, sal_True
== bOK1
);
503 sal_Bool bOK
= ( IP_PORT_MYPORT7
== sSocket
.getLocalPort( ) );
505 CPPUNIT_ASSERT_MESSAGE( "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.",
509 /** test writer's comment:
511 the invalid port number can not be set by giving invalid port number
512 such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be
513 valid, the only instance that the getLocalPort returns OSL_INVALID_PORT
514 is when saSocketAddr itself is an invalid one, that is , the IP or host name
515 can not be found, then the created socket address is not valid.
517 void getLocalPort_002()
519 ::osl::SocketAddr
saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_TELNET
);
521 ::osl::Socket
sSocket(sHandle
);
522 sSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); // sal_True);
523 sSocket
.bind( saBindSocketAddr
);
524 //Invalid IP, so bind should fail
525 ::rtl::OUString suError
= outputError(::rtl::OUString::valueOf(sSocket
.getLocalPort( )),
526 ::rtl::OUString::valueOf((sal_Int32
)OSL_INVALID_PORT
),
527 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned.");
528 sal_Bool bOK
= ( OSL_INVALID_PORT
== sSocket
.getLocalPort( ) );
531 //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT
532 ::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");
534 CPPUNIT_ASSERT_MESSAGE( suError
, sal_False
);
538 void getLocalPort_003()
540 ::osl::Socket
sSocket(sHandle
);
541 ::osl::SocketAddr
saBindSocketAddr( getLocalIP(), IP_PORT_INVAL
);
543 sSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
545 sal_Bool bOK1
= sSocket
.bind( saBindSocketAddr
);
546 ::rtl::OUString suError1
= ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket
.getErrorAsString();
547 CPPUNIT_ASSERT_MESSAGE( suError1
, sal_True
== bOK1
);
548 ::rtl::OUString suError
= outputError(::rtl::OUString::valueOf(sSocket
.getLocalPort( )),
549 ::rtl::OUString::createFromAscii("34463"),
550 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned");
551 sal_Bool bOK
= ( sSocket
.getLocalPort( ) >= 1 && sSocket
.getLocalPort( ) <= 65535);
553 CPPUNIT_ASSERT_MESSAGE( suError
, sal_True
== bOK
);
556 CPPUNIT_TEST_SUITE( getLocalPort
);
557 CPPUNIT_TEST( getLocalPort_001
);
558 // LLA: CPPUNIT_TEST( getLocalPort_002 );
559 CPPUNIT_TEST( getLocalPort_003
);
560 CPPUNIT_TEST_SUITE_END();
562 }; // class getLocalPort
565 /** testing the method:
566 inline ::rtl::OUString SAL_CALL getLocalHost() const;
568 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;
569 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
570 will return hostname of current processor such as "aegean.PRC.Sun.COM"
573 class getLocalHost
: public CppUnit::TestFixture
580 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
589 void getLocalHost_001()
591 ::osl::Socket
sSocket(sHandle
);
592 //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu
593 ::osl::SocketAddr
saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT6
);
595 sSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
597 sal_Bool bOK1
= sSocket
.bind( saBindSocketAddr
);
598 ::rtl::OUString suError1
= ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket
.getErrorAsString();
599 CPPUNIT_ASSERT_MESSAGE( suError1
, sal_True
== bOK1
);
601 ::rtl::OUString suError
;
603 bOK
= compareUString( sSocket
.getLocalHost( ), getThisHostname( ) ) ;
604 suError
= outputError(sSocket
.getLocalHost( ), getThisHostname( ),
605 "test for getLocalHost function: create localhost socket and check name");
607 ::rtl::OUString aUString
= ::rtl::OUString::createFromAscii( (const sal_Char
*) "localhost" );
608 sal_Bool bRes1
, bRes2
;
609 bRes1
= compareUString( sSocket
.getLocalHost( ), aUString
) ;
610 bRes2
= compareUString( sSocket
.getLocalHost( ), saBindSocketAddr
.getHostname(0) ) ;
611 bOK
= bRes1
|| bRes2
;
612 suError
= outputError(sSocket
.getLocalHost( ), aUString
, "test for getLocalHost function: create localhost socket and check name");
614 CPPUNIT_ASSERT_MESSAGE( suError
, sal_True
== bOK
);
617 void getLocalHost_002()
619 ::osl::Socket
sSocket(sHandle
);
620 ::osl::SocketAddr
saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_POP3
);
621 ::osl::SocketAddr saLocalSocketAddr
;
623 sSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
624 sSocket
.bind( saBindSocketAddr
);
625 //Invalid IP, so bind should fail
626 sal_Bool bOK
= compareUString( sSocket
.getLocalHost( ), rtl::OUString::createFromAscii("") ) ;
627 ::rtl::OUString suError
= outputError(sSocket
.getLocalHost( ), rtl::OUString::createFromAscii(""), "test for getLocalHost function: getLocalHost with invalid SocketAddr");
629 CPPUNIT_ASSERT_MESSAGE( suError
, sal_True
== bOK
);
632 CPPUNIT_TEST_SUITE( getLocalHost
);
633 CPPUNIT_TEST( getLocalHost_001
);
634 CPPUNIT_TEST( getLocalHost_002
);
635 CPPUNIT_TEST_SUITE_END();
637 }; // class getLocalHost
640 /** testing the methods:
641 inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const;
642 inline sal_Int32 SAL_CALL getPeerPort() const;
643 inline ::rtl::OUString SAL_CALL getPeerHost() const;
645 class getPeer
: public CppUnit::TestFixture
650 ::osl::AcceptorSocket asAcceptorSocket
;
651 ::osl::ConnectorSocket csConnectorSocket
;
657 pTimeout
= ( TimeValue
* )malloc( sizeof( TimeValue
) );
658 pTimeout
->Seconds
= 3;
659 pTimeout
->Nanosec
= 0;
660 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
667 asAcceptorSocket
.close( );
668 csConnectorSocket
.close( );
674 ::osl::SocketAddr
saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT
);
675 ::osl::SocketAddr
saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT
);
676 ::osl::SocketAddr
saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP
);
677 ::osl::StreamSocket ssConnection
;
678 asAcceptorSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
679 /// launch server socket
680 sal_Bool bOK1
= asAcceptorSocket
.bind( saLocalSocketAddr
);
681 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind '127.0.0.1' address failed.", sal_True
== bOK1
);
682 sal_Bool bOK2
= asAcceptorSocket
.listen( 1 );
683 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True
== bOK2
);
685 asAcceptorSocket
.enableNonBlockingMode( sal_True
);
686 asAcceptorSocket
.acceptConnection(ssConnection
); /// waiting for incoming connection...
688 /// launch client socket
689 csConnectorSocket
.connect( saTargetSocketAddr
, pTimeout
); /// connecting to server...
691 /// get peer information
692 csConnectorSocket
.getPeerAddr( saPeerSocketAddr
);/// connected.
693 sal_Int32 peerPort
= csConnectorSocket
.getPeerPort( );
694 ::rtl::OUString peerHost
= csConnectorSocket
.getPeerHost( );
696 CPPUNIT_ASSERT_MESSAGE( "test for getPeer function: setup a connection and then get the peer address, port and host from client side.",
697 ( sal_True
== compareSocketAddr( saPeerSocketAddr
, saLocalSocketAddr
) )&&
698 ( sal_True
== compareUString( peerHost
, saLocalSocketAddr
.getHostname( 0 ) ) ) &&
699 ( peerPort
== saLocalSocketAddr
.getPort( ) ));
703 CPPUNIT_TEST_SUITE( getPeer
);
704 CPPUNIT_TEST( getPeer_001
);
705 CPPUNIT_TEST_SUITE_END();
710 /** testing the methods:
711 inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface);
715 class bind
: public CppUnit::TestFixture
722 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
733 ::osl::Socket
sSocket(sHandle
);
734 //bind must use local IP address ---mindyliu
735 ::osl::SocketAddr
saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5
);
737 sSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
738 sal_Bool bOK1
= sSocket
.bind( saBindSocketAddr
);
739 CPPUNIT_ASSERT_MESSAGE( "Socket bind fail.", sal_True
== bOK1
);
741 sal_Bool bOK2
= compareUString( sSocket
.getLocalHost( ), saBindSocketAddr
.getHostname( ) ) ;
744 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", sal_True
== bOK2
);
749 ::osl::Socket
sSocket(sHandle
);
750 ::osl::SocketAddr
saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_NETBIOS
);
751 ::osl::SocketAddr saLocalSocketAddr
;
753 sSocket
.setOption( osl_Socket_OptionReuseAddr
, 1); // sal_True);
754 sal_Bool bOK1
= sSocket
.bind( saBindSocketAddr
);
755 sal_Bool bOK2
= compareUString( sSocket
.getLocalHost( ), getThisHostname( ) ) ;
757 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.",
758 ( sal_False
== bOK1
) && ( sal_False
== bOK2
) );
761 CPPUNIT_TEST_SUITE( bind
);
762 CPPUNIT_TEST( bind_001
);
763 CPPUNIT_TEST( bind_002
);
764 CPPUNIT_TEST_SUITE_END();
769 /** testing the methods:
770 inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const;
773 class isRecvReady
: public CppUnit::TestFixture
778 ::osl::AcceptorSocket asAcceptorSocket
;
779 ::osl::ConnectorSocket csConnectorSocket
;
785 pTimeout
= ( TimeValue
* )malloc( sizeof( TimeValue
) );
786 pTimeout
->Seconds
= 3;
787 pTimeout
->Nanosec
= 0;
788 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
795 asAcceptorSocket
.close( );
796 csConnectorSocket
.close( );
800 void isRecvReady_001()
802 ::osl::SocketAddr
saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1
);
803 ::osl::SocketAddr
saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1
);
804 ::osl::SocketAddr
saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP
);
805 ::osl::StreamSocket ssConnection
;
806 /// launch server socket
807 asAcceptorSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); // sal_True);
808 sal_Bool bOK1
= asAcceptorSocket
.bind( saLocalSocketAddr
);
809 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True
== bOK1
);
810 sal_Bool bOK2
= asAcceptorSocket
.listen( 1 );
811 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True
== bOK2
);
812 asAcceptorSocket
.enableNonBlockingMode( sal_True
);
813 asAcceptorSocket
.acceptConnection(ssConnection
); /// waiting for incoming connection...
815 /// launch client socket
816 csConnectorSocket
.connect( saTargetSocketAddr
, pTimeout
); /// connecting to server...
818 /// is receive ready?
819 sal_Bool bOK3
= asAcceptorSocket
.isRecvReady( pTimeout
);
821 CPPUNIT_ASSERT_MESSAGE( "test for isRecvReady function: setup a connection and then check if it can transmit data.",
822 ( sal_True
== bOK3
) );
826 CPPUNIT_TEST_SUITE( isRecvReady
);
827 CPPUNIT_TEST( isRecvReady_001
);
828 CPPUNIT_TEST_SUITE_END();
830 }; // class isRecvReady
833 /** testing the methods:
834 inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const;
836 class isSendReady
: public CppUnit::TestFixture
841 ::osl::AcceptorSocket asAcceptorSocket
;
842 ::osl::ConnectorSocket csConnectorSocket
;
848 pTimeout
= ( TimeValue
* )malloc( sizeof( TimeValue
) );
849 pTimeout
->Seconds
= 3;
850 pTimeout
->Nanosec
= 0;
851 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
858 asAcceptorSocket
.close( );
859 csConnectorSocket
.close( );
863 void isSendReady_001()
865 ::osl::SocketAddr
saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT
);
866 ::osl::SocketAddr
saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT
);
867 ::osl::SocketAddr
saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP
);
868 ::osl::StreamSocket ssConnection
;
870 /// launch server socket
871 asAcceptorSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
872 sal_Bool bOK1
= asAcceptorSocket
.bind( saLocalSocketAddr
);
873 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True
== bOK1
);
874 sal_Bool bOK2
= asAcceptorSocket
.listen( 1 );
875 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True
== bOK2
);
876 asAcceptorSocket
.enableNonBlockingMode( sal_True
);
877 asAcceptorSocket
.acceptConnection(ssConnection
); /// waiting for incoming connection...
879 /// launch client socket
880 csConnectorSocket
.connect( saTargetSocketAddr
, pTimeout
); /// connecting to server...
883 sal_Bool bOK3
= csConnectorSocket
.isSendReady( pTimeout
);
885 CPPUNIT_ASSERT_MESSAGE( "test for isSendReady function: setup a connection and then check if it can transmit data.",
886 ( sal_True
== bOK3
) );
890 CPPUNIT_TEST_SUITE( isSendReady
);
891 CPPUNIT_TEST( isSendReady_001
);
892 CPPUNIT_TEST_SUITE_END();
894 }; // class isSendReady
897 /** testing the methods:
898 inline oslSocketType SAL_CALL getType() const;
902 class getType
: public CppUnit::TestFixture
920 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
921 ::osl::Socket
sSocket(sHandle
);
923 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
924 osl_Socket_TypeStream
== sSocket
.getType( ) );
929 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeDgram
, osl_Socket_ProtocolIp
);
930 ::osl::Socket
sSocket(sHandle
);
932 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
933 osl_Socket_TypeDgram
== sSocket
.getType( ) );
937 // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here
938 // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw()
941 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.",
947 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeRaw
, osl_Socket_ProtocolIp
);
948 ::osl::Socket
sSocket(sHandle
);
950 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
951 osl_Socket_TypeRaw
== sSocket
.getType( ) );
955 CPPUNIT_TEST_SUITE( getType
);
956 CPPUNIT_TEST( getType_001
);
957 CPPUNIT_TEST( getType_002
);
958 CPPUNIT_TEST( getType_003
);
959 CPPUNIT_TEST_SUITE_END();
965 /** testing the methods:
966 inline sal_Int32 SAL_CALL getOption(
967 oslSocketOption Option,
969 sal_uInt32 BufferLen,
970 oslSocketOptionLevel Level= osl_Socket_LevelSocket) const;
972 inline sal_Int32 getOption( oslSocketOption option ) const;
976 class getOption
: public CppUnit::TestFixture
991 /** test writer's comment:
993 in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream.
994 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ
1000 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
1001 ::osl::Socket
sSocket(sHandle
);
1002 sal_Int32
* pType
= ( sal_Int32
* )malloc( sizeof ( sal_Int32
) );
1004 sSocket
.getOption( osl_Socket_OptionType
, pType
, sizeof ( sal_Int32
) );
1005 sal_Bool bOK
= ( SOCK_STREAM
== *pType
);
1006 // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream
1007 //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType );
1010 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get type option of socket.",
1015 void getOption_004()
1017 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeDgram
, osl_Socket_ProtocolIp
);
1018 ::osl::Socket
sSocket(sHandle
);
1020 sal_Bool
* pbDontRoute
= ( sal_Bool
* )malloc( sizeof ( sal_Bool
) );
1021 sal_Int32 nRes
= sSocket
.getOption( osl_Socket_OptionInvalid
, pbDontRoute
, sizeof ( sal_Bool
) );
1022 free( pbDontRoute
);
1024 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get invalid option of socket, should return -1.",
1028 void getOption_simple_001()
1030 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeDgram
, osl_Socket_ProtocolIp
);
1031 ::osl::Socket
sSocket(sHandle
);
1033 sal_Bool bOK
= ( sal_False
== sSocket
.getOption( osl_Socket_OptionDontRoute
) );
1035 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
1039 void getOption_simple_002()
1041 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeDgram
, osl_Socket_ProtocolIp
);
1042 ::osl::Socket
sSocket(sHandle
);
1044 sal_Bool bOK
= ( sal_False
== sSocket
.getOption( osl_Socket_OptionDebug
) );
1046 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
1050 CPPUNIT_TEST_SUITE( getOption
);
1051 CPPUNIT_TEST( getOption_001
);
1052 CPPUNIT_TEST( getOption_004
);
1053 CPPUNIT_TEST( getOption_simple_001
);
1054 CPPUNIT_TEST( getOption_simple_002
);
1055 CPPUNIT_TEST_SUITE_END();
1057 }; // class getOption
1060 /** testing the methods:
1061 inline sal_Bool SAL_CALL setOption( oslSocketOption Option,
1063 sal_uInt32 BufferLen,
1064 oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const;
1067 class setOption
: public CppUnit::TestFixture
1070 TimeValue
*pTimeout
;
1071 // LLA: maybe there is an error in the source,
1072 // as long as I remember, if a derived class do not overload all ctors there is a problem.
1074 ::osl::AcceptorSocket asAcceptorSocket
;
1083 asAcceptorSocket
.close( );
1088 // getSocketOption returns BufferLen, or -1 if something failed
1090 // setSocketOption returns sal_True, if option could stored
1093 void setOption_001()
1095 /// set and get option.
1096 int nBufferLen
= sizeof ( sal_Int32
);
1097 // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool!
1099 sal_Int32
* pbDontRouteSet
= ( sal_Int32
* )malloc( sizeof ( sal_Int32
) );
1100 *pbDontRouteSet
= 1; // sal_True;
1102 sal_Int32
* pGetBuffer
= ( sal_Int32
* )malloc( sizeof ( sal_Int32
) );
1105 // maybe asAcceptorSocket is not right initialized
1106 sal_Bool b1
= asAcceptorSocket
.setOption( osl_Socket_OptionDontRoute
, pbDontRouteSet
, nBufferLen
);
1107 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True
== b1
) );
1108 sal_Int32 n2
= asAcceptorSocket
.getOption( osl_Socket_OptionDontRoute
, pGetBuffer
, nBufferLen
);
1109 CPPUNIT_ASSERT_MESSAGE( "getOption function failed.", ( n2
== nBufferLen
) );
1111 // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value,
1112 // just judge it is zero or not!
1113 sal_Bool bOK
= ( 0 != *pGetBuffer
);
1114 t_print("#setOption_001: getOption is %d \n", *pGetBuffer
);
1116 // toggle check, set to 0
1117 *pbDontRouteSet
= 0;
1119 sal_Bool b3
= asAcceptorSocket
.setOption( osl_Socket_OptionDontRoute
, pbDontRouteSet
, sizeof ( sal_Int32
) );
1120 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True
== b3
) );
1121 sal_Int32 n4
= asAcceptorSocket
.getOption( osl_Socket_OptionDontRoute
, pGetBuffer
, nBufferLen
);
1122 CPPUNIT_ASSERT_MESSAGE( "getOption (DONTROUTE) function failed.", ( n4
== nBufferLen
) );
1124 sal_Bool bOK2
= ( 0 == *pGetBuffer
);
1126 t_print("#setOption_001: getOption is %d \n", *pGetBuffer
);
1128 // LLA: sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
1129 // LLA: *pbDontTouteSet = sal_True;
1130 // LLA: sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
1131 // LLA: *pbDontTouteGet = sal_False;
1132 // LLA: asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontTouteSet, sizeof ( sal_Bool ) );
1133 // LLA: asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pbDontTouteGet, sizeof ( sal_Bool ) );
1134 // LLA: ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet),
1135 // LLA: ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet),
1136 // LLA: "test for setOption function: set osl_Socket_OptionDontRoute and then check");
1138 // LLA: sal_Bool bOK = ( sal_True == *pbDontTouteGet );
1139 // LLA: free( pbDontTouteSet );
1140 // LLA: free( pbDontTouteGet );
1142 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1143 ( sal_True
== bOK
) && (sal_True
== bOK2
) );
1145 free( pbDontRouteSet
);
1147 // LLA: CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
1150 void setOption_002()
1152 /// set and get option.
1154 // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen );
1155 // *pbLingerSet = 7;
1156 // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen );
1157 /* struct */linger aLingerSet
;
1158 sal_Int32 nBufferLen
= sizeof( struct linger
);
1159 aLingerSet
.l_onoff
= 1;
1160 aLingerSet
.l_linger
= 7;
1164 asAcceptorSocket
.setOption( osl_Socket_OptionLinger
, &aLingerSet
, nBufferLen
);
1166 sal_Int32 n1
= asAcceptorSocket
.getOption( osl_Socket_OptionLinger
, &aLingerGet
, nBufferLen
);
1167 CPPUNIT_ASSERT_MESSAGE( "getOption (SO_LINGER) function failed.", ( n1
== nBufferLen
) );
1169 //t_print("#setOption_002: getOption is %d \n", aLingerGet.l_linger);
1170 sal_Bool bOK
= ( 7 == aLingerGet
.l_linger
);
1171 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check. ",
1176 void setOption_003()
1179 aLingerSet
.l_onoff
= 1;
1180 aLingerSet
.l_linger
= 7;
1182 sal_Bool b1
= asAcceptorSocket
.setOption( osl_Socket_OptionLinger
, &aLingerSet
, 0 );
1183 printUString( asAcceptorSocket
.getErrorAsString( ) );
1184 CPPUNIT_ASSERT_MESSAGE( "setOption (SO_LINGER) function failed for optlen is 0.",
1185 ( b1
== sal_False
) );
1188 void setOption_simple_001()
1190 /// set and get option.
1191 asAcceptorSocket
.setOption( osl_Socket_OptionDontRoute
, 1 ); //sal_True );
1192 sal_Bool bOK
= ( 0 != asAcceptorSocket
.getOption( osl_Socket_OptionDontRoute
) );
1194 t_print("setOption_simple_001(): getoption is %d \n", asAcceptorSocket
.getOption( osl_Socket_OptionDontRoute
) );
1195 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1196 ( sal_True
== bOK
) );
1199 void setOption_simple_002()
1201 /// set and get option.
1202 // LLA: this does not work, due to the fact that SO_LINGER is a structure
1203 // LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 );
1204 // LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) );
1206 // LLA: CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1207 // LLA: ( sal_True == bOK ) );
1210 CPPUNIT_TEST_SUITE( setOption
);
1211 CPPUNIT_TEST( setOption_001
);
1212 CPPUNIT_TEST( setOption_002
);
1213 CPPUNIT_TEST( setOption_003
);
1214 CPPUNIT_TEST( setOption_simple_001
);
1215 // LLA: CPPUNIT_TEST( setOption_simple_002 );
1216 CPPUNIT_TEST_SUITE_END();
1218 }; // class setOption
1222 /** testing the method:
1223 inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode);
1225 class enableNonBlockingMode
: public CppUnit::TestFixture
1228 ::osl::AcceptorSocket asAcceptorSocket
;
1230 void enableNonBlockingMode_001()
1232 ::osl::SocketAddr
saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT
);
1233 ::osl::StreamSocket ssConnection
;
1235 /// launch server socket
1236 asAcceptorSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
1237 sal_Bool bOK1
= asAcceptorSocket
.bind( saLocalSocketAddr
);
1238 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True
== bOK1
);
1239 sal_Bool bOK2
= asAcceptorSocket
.listen( 1 );
1240 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True
== bOK2
);
1241 asAcceptorSocket
.enableNonBlockingMode( sal_True
);
1242 asAcceptorSocket
.acceptConnection(ssConnection
); /// waiting for incoming connection...
1244 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
1245 sal_Bool bOK
= sal_True
;
1246 asAcceptorSocket
.close( );
1248 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",
1249 ( sal_True
== bOK
) );
1253 CPPUNIT_TEST_SUITE( enableNonBlockingMode
);
1254 CPPUNIT_TEST( enableNonBlockingMode_001
);
1255 CPPUNIT_TEST_SUITE_END();
1257 }; // class enableNonBlockingMode
1260 /** testing the method:
1261 inline sal_Bool SAL_CALL isNonBlockingMode() const;
1263 class isNonBlockingMode
: public CppUnit::TestFixture
1266 ::osl::AcceptorSocket asAcceptorSocket
;
1268 void isNonBlockingMode_001()
1270 ::osl::SocketAddr
saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT
);
1271 ::osl::StreamSocket ssConnection
;
1273 /// launch server socket
1274 asAcceptorSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); // sal_True);
1275 sal_Bool bOK1
= asAcceptorSocket
.bind( saLocalSocketAddr
);
1276 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True
== bOK1
);
1277 sal_Bool bOK2
= asAcceptorSocket
.listen( 1 );
1278 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True
== bOK2
);
1280 sal_Bool bOK3
= asAcceptorSocket
.isNonBlockingMode( );
1281 asAcceptorSocket
.enableNonBlockingMode( sal_True
);
1282 asAcceptorSocket
.acceptConnection(ssConnection
); /// waiting for incoming connection...
1284 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
1285 sal_Bool bOK4
= asAcceptorSocket
.isNonBlockingMode( );
1286 asAcceptorSocket
.close( );
1288 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.",
1289 ( sal_False
== bOK3
) && ( sal_True
== bOK4
) );
1293 CPPUNIT_TEST_SUITE( isNonBlockingMode
);
1294 CPPUNIT_TEST( isNonBlockingMode_001
);
1295 CPPUNIT_TEST_SUITE_END();
1297 }; // class isNonBlockingMode
1299 /** testing the method:
1300 inline void SAL_CALL clearError() const;
1302 class clearError
: public CppUnit::TestFixture
1309 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
1318 void clearError_001()
1320 ::osl::Socket
sSocket(sHandle
);
1321 ::osl::SocketAddr
saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_HTTP2
);
1322 ::osl::SocketAddr saLocalSocketAddr
;
1323 sSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
1324 sSocket
.bind( saBindSocketAddr
);//build an error "osl_Socket_E_AddrNotAvail"
1325 oslSocketError seBind
= sSocket
.getError( );
1326 sSocket
.clearError( );
1328 CPPUNIT_ASSERT_MESSAGE( "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.",
1329 osl_Socket_E_None
== sSocket
.getError( ) && seBind
!= osl_Socket_E_None
);
1333 CPPUNIT_TEST_SUITE( clearError
);
1334 CPPUNIT_TEST( clearError_001
);
1335 CPPUNIT_TEST_SUITE_END();
1337 }; // class clearError
1340 /** testing the methods:
1341 inline oslSocketError getError() const;
1342 inline ::rtl::OUString getErrorAsString( ) const;
1344 class getError
: public CppUnit::TestFixture
1351 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
1362 ::osl::Socket
sSocket(sHandle
);
1363 ::osl::SocketAddr
saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP
);
1364 ::osl::SocketAddr saLocalSocketAddr
;
1366 CPPUNIT_ASSERT_MESSAGE( "test for getError function: should get no error.",
1367 osl_Socket_E_None
== sSocket
.getError( ) );
1372 ::osl::Socket
sSocket(sHandle
);
1373 ::osl::SocketAddr
saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_FTP
);
1374 ::osl::SocketAddr saLocalSocketAddr
;
1375 sSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
1376 sSocket
.bind( saBindSocketAddr
);//build an error "osl_Socket_E_AddrNotAvail"
1377 //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError.
1378 #if defined(SOLARIS)
1379 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. ",
1380 osl_Socket_E_InvalidError
== sSocket
.getError( ) );
1382 //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail.
1384 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",
1385 osl_Socket_E_AddrNotAvail
== sSocket
.getError( ) );
1389 CPPUNIT_TEST_SUITE( getError
);
1390 CPPUNIT_TEST( getError_001
);
1391 CPPUNIT_TEST( getError_002
);
1392 CPPUNIT_TEST_SUITE_END();
1394 }; // class getError
1398 /** testing the methods:
1399 inline oslSocket getHandle() const;
1402 class getHandle
: public CppUnit::TestFixture
1409 sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
1417 void getHandle_001()
1419 ::osl::Socket
sSocket(sHandle
);
1420 ::osl::Socket assignSocket
= sSocket
.getHandle();
1422 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
1423 osl_Socket_TypeStream
== assignSocket
.getType( ) );
1426 void getHandle_002()
1428 ::osl::Socket
sSocket( sHandle
);
1429 ::osl::Socket
assignSocket ( sSocket
.getHandle( ) );
1431 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
1432 osl_Socket_TypeStream
== assignSocket
.getType( ) );
1435 CPPUNIT_TEST_SUITE( getHandle
);
1436 CPPUNIT_TEST( getHandle_001
);
1437 CPPUNIT_TEST( getHandle_002
);
1438 CPPUNIT_TEST_SUITE_END();
1440 }; // class getHandle
1443 // -----------------------------------------------------------------------------
1446 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::ctors
, "osl_Socket");
1447 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::operators
, "osl_Socket");
1448 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::close
, "osl_Socket");
1449 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalAddr
, "osl_Socket");
1450 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalPort
, "osl_Socket");
1451 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalHost
, "osl_Socket");
1452 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getPeer
, "osl_Socket");
1453 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::bind
, "osl_Socket");
1454 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isRecvReady
, "osl_Socket");
1455 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isSendReady
, "osl_Socket");
1456 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getType
, "osl_Socket");
1457 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getOption
, "osl_Socket");
1458 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::setOption
, "osl_Socket");
1459 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::enableNonBlockingMode
, "osl_Socket");
1460 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isNonBlockingMode
, "osl_Socket");
1461 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::clearError
, "osl_Socket");
1462 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getError
, "osl_Socket");
1463 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getHandle
, "osl_Socket");
1465 } // namespace osl_Socket
1467 // -----------------------------------------------------------------------------
1469 // this macro creates an empty function, which will called by the RegisterAllFunctions()
1470 // to let the user the possibility to also register some functions by hand.