bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / osl / socket / osl_Socket2.cxx
blob33f928b82dce2dff442c4e8b4ccf496c56df3167
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 /** test coder preface:
21 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
22 if you are not including ws2_32.lib in makefile.mk, the including format will be like this:
24 .IF "$(OS)" == "WNT"
25 SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
26 SHL1STDLIBS += ws2_32.lib
27 .ENDIF
29 likewise on Solaris platform.
30 .IF "$(OS)" != "WNT"
31 SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
32 SHL1STDLIBS += -lsocket -ldl -lnsl
33 .ENDIF
35 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
36 category.
38 3. some fragment of Socket source implementation are lack of comment so it is hard for testers
39 guess what the exact functionality or usage of a member. Hope the Socket section's comment
40 will be added.
42 4. following functions are declared but not implemented:
43 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
46 #include <sal/types.h>
47 #include <cppunit/TestFixture.h>
48 #include <cppunit/extensions/HelperMacros.h>
49 #include <cppunit/plugin/TestPlugIn.h>
51 #include "sockethelper.hxx"
53 using namespace osl;
54 using ::rtl::OUString;
56 #define IP_PORT_FTP 21
57 #define IP_PORT_TELNET 23
58 #define IP_PORT_HTTP2 8080
59 #define IP_PORT_INVAL 99999
60 #define IP_PORT_POP3 110
61 #define IP_PORT_NETBIOS 139
62 #define IP_PORT_MYPORT 8881
63 #define IP_PORT_MYPORT1 8882
64 #define IP_PORT_MYPORT5 8886
65 #define IP_PORT_MYPORT6 8887
66 #define IP_PORT_MYPORT7 8895
67 #define IP_PORT_MYPORT8 8896
68 #define IP_PORT_MYPORT9 8897
70 // helper functions
72 // just used to test socket::close() when accepting
73 class AcceptorThread : public Thread
75 ::osl::AcceptorSocket asAcceptorSocket;
76 ::rtl::OUString aHostIP;
77 sal_Bool bOK;
78 protected:
79 void SAL_CALL run( )
81 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
82 ::osl::StreamSocket ssStreamConnection;
84 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
85 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
86 if ( sal_True != bOK1 )
88 t_print("# AcceptorSocket bind address failed.\n" ) ;
89 return;
91 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
92 if ( sal_True != bOK2 )
94 t_print("# AcceptorSocket listen address failed.\n" ) ;
95 return;
98 asAcceptorSocket.enableNonBlockingMode( sal_False );
100 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
101 if (eResult != osl_Socket_Ok )
103 bOK = sal_True;
104 t_print("AcceptorThread: acceptConnection failed! \n");
107 public:
108 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
109 : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
111 bOK = sal_False;
114 sal_Bool isOK() { return bOK; }
116 ~AcceptorThread( )
118 if ( isRunning( ) )
120 asAcceptorSocket.shutdown();
121 t_print("# error: Acceptor thread not terminated.\n" );
126 namespace osl_Socket
129 /** testing the methods:
130 inline Socket( );
131 inline Socket( const Socket & socket );
132 inline Socket( oslSocket socketHandle );
133 inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire );
136 /** test writer's comment:
138 class Socket can not be initialized by its protected constructor, though the protected
139 constructor is the most convenient way to create a new socket.
140 it only allow the method of C function osl_createSocket like:
141 ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream,
142 osl_Socket_ProtocolIp ) );
143 the use of C method lost some of the transparent of tester using C++ wrapper.
146 class ctors : public CppUnit::TestFixture
148 public:
149 oslSocket sHandle;
150 // initialization
151 void setUp( )
153 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
156 void tearDown( )
158 sHandle = NULL;
161 void ctors_none()
163 /// Socket constructor.
164 // ::osl::Socket sSocket();
166 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the socket was created successfully, if no exception occurred",
167 1 == 1 );
170 void ctors_acquire()
172 /// Socket constructor.
173 ::osl::Socket sSocket( sHandle );
175 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
176 osl_Socket_TypeStream == sSocket.getType( ) );
179 void ctors_no_acquire()
181 /// Socket constructor.
182 ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE );
184 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
185 osl_Socket_TypeStream == sSocket.getType( ) );
188 void ctors_copy_ctor()
190 ::osl::Socket sSocket( sHandle );
191 /// Socket copy constructor.
192 ::osl::Socket copySocket( sSocket );
194 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
195 osl_Socket_TypeStream == copySocket.getType( ) );
198 void ctors_TypeRaw()
200 #ifdef WNT
201 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
202 // LLA: ? ::osl::Socket sSocket( sHandleRaw );
203 CPPUNIT_ASSERT_MESSAGE( " type osl_Socket_TypeRaw socket create failed on UNX ", sHandleRaw != NULL);
204 #else
205 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
206 CPPUNIT_ASSERT_MESSAGE( " can't create socket with type osl_Socket_TypeRaw within UNX is ok.", sHandleRaw == NULL);
207 #endif
210 void ctors_family_Ipx()
212 oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
213 CPPUNIT_ASSERT_MESSAGE( " family osl_Socket_FamilyIpx socket create failed! ", sHandleIpx != NULL);
214 ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE );
215 t_print("#Type is %d \n", sSocket.getType( ) );
217 CPPUNIT_ASSERT_MESSAGE(" test for create new Socket instance that family is osl_Socket_FamilyIpx",
218 osl_Socket_TypeStream == sSocket.getType( ) );
221 CPPUNIT_TEST_SUITE( ctors );
222 CPPUNIT_TEST( ctors_none );
223 CPPUNIT_TEST( ctors_acquire );
224 CPPUNIT_TEST( ctors_no_acquire );
225 CPPUNIT_TEST( ctors_copy_ctor );
226 CPPUNIT_TEST( ctors_TypeRaw );
227 //TODO: Check if family_Ipx is still in use?
228 // CPPUNIT_TEST( ctors_family_Ipx );
229 CPPUNIT_TEST_SUITE_END();
231 }; // class ctors
233 /** testing the methods:
234 inline Socket& SAL_CALL operator= ( oslSocket socketHandle);
235 inline Socket& SAL_CALL operator= (const Socket& sock);
236 inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ;
237 inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const;
240 class operators : public CppUnit::TestFixture
242 public:
243 oslSocket sHandle;
244 // initialization
245 void setUp( )
247 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
250 void tearDown( )
252 sHandle = NULL;
255 /** test writer's comment:
257 the assignment operator does not support direct assinment like:
258 ::osl::Socket sSocket = sHandle.
260 void operators_assignment_handle()
262 ::osl::Socket sSocket(sHandle);
263 ::osl::Socket assignSocket = sSocket.getHandle();
265 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
266 osl_Socket_TypeStream == assignSocket.getType( ) );
269 void operators_assignment()
271 ::osl::Socket sSocket( sHandle );
272 ::osl::Socket assignSocket = sSocket;
274 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
275 osl_Socket_TypeStream == assignSocket.getType( ) );
278 void operators_equal_handle_001()
280 /// Socket constructor.
281 ::osl::Socket sSocket( sHandle );
282 ::osl::Socket equalSocket = sSocket;
284 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check equal.",
285 equalSocket == sHandle );
288 void operators_equal_handle_002()
290 /// Socket constructor.
291 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
293 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check unequal.",
294 !( equalSocket == sHandle ) );
297 void operators_equal_001()
299 ::osl::Socket sSocket( sHandle );
300 /// Socket copy constructor.
301 ::osl::Socket equalSocket( sSocket );
303 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal function: check equal.",
304 equalSocket == sSocket );
307 void operators_equal_002()
309 ::osl::Socket sSocket( sHandle );
310 /// Socket copy constructor.
311 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
313 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_002 function: check unequal.",
314 !( equalSocket == sSocket ) );
317 CPPUNIT_TEST_SUITE( operators );
318 CPPUNIT_TEST( operators_assignment_handle );
319 CPPUNIT_TEST( operators_assignment );
320 CPPUNIT_TEST( operators_equal_handle_001 );
321 CPPUNIT_TEST( operators_equal_handle_002 );
322 CPPUNIT_TEST( operators_equal_001 );
323 CPPUNIT_TEST( operators_equal_002 );
324 CPPUNIT_TEST_SUITE_END();
326 }; // class operators
328 /** testing the methods:
329 inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite );
330 inline void SAL_CALL close();
333 class close : public CppUnit::TestFixture
335 public:
336 oslSocket sHandle;
337 // initialization
338 void setUp( )
340 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
343 void tearDown( )
345 sHandle = NULL;
348 void close_001()
350 ::osl::Socket sSocket(sHandle);
351 sSocket.close();
353 CPPUNIT_ASSERT_MESSAGE( "test for close_001 function: this function is reserved for test.",
354 sSocket.getHandle() == sHandle );
357 void close_002()
359 //#if defined(LINUX)
360 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
361 AcceptorThread myAcceptorThread( asSocket, rtl::OUString("127.0.0.1") );
362 myAcceptorThread.create();
364 thread_sleep( 1 );
365 //when accepting, close the socket, the thread will not block for accepting
366 //man close:Any locks held on the file it was associated with, and owned by the process, are removed
367 asSocket.close();
368 //thread_sleep( 2 );
369 myAcceptorThread.join();
371 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
372 myAcceptorThread.isOK() == sal_True );
373 //#endif
376 // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( )
377 void close_003()
379 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
380 AcceptorThread myAcceptorThread( asSocket, rtl::OUString("0.0.0.0") );
381 myAcceptorThread.create();
383 thread_sleep( 1 );
384 asSocket.close();
385 myAcceptorThread.join();
387 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
388 myAcceptorThread.isOK() == sal_True );
391 CPPUNIT_TEST_SUITE( close );
392 CPPUNIT_TEST( close_001 );
393 CPPUNIT_TEST( close_002 );
394 CPPUNIT_TEST( close_003 );
395 CPPUNIT_TEST_SUITE_END();
397 }; // class close
399 /** testing the method:
400 inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const;
403 class getLocalAddr : public CppUnit::TestFixture
405 public:
406 oslSocket sHandle;
407 // initialization
408 void setUp( )
410 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
413 void tearDown( )
415 sHandle = NULL;
418 // get the Address of the local end of the socket
419 void getLocalAddr_001()
421 ::osl::Socket sSocket(sHandle);
422 ::osl::SocketAddr saBindSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT8 );
423 ::osl::SocketAddr saLocalSocketAddr;
425 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
427 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
428 ::rtl::OUString suError1 = ::rtl::OUString("Socket bind fail:") + sSocket.getErrorAsString();
429 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError1), sal_True == bOK1 );
431 sSocket.getLocalAddr( saLocalSocketAddr );
433 sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ;
435 CPPUNIT_ASSERT_MESSAGE( "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.",
436 sal_True == bOK );
439 CPPUNIT_TEST_SUITE( getLocalAddr );
440 CPPUNIT_TEST( getLocalAddr_001 );
441 CPPUNIT_TEST_SUITE_END();
443 }; // class getLocalAddr
445 /** testing the method:
446 inline sal_Int32 SAL_CALL getLocalPort() const;
449 class getLocalPort : public CppUnit::TestFixture
451 public:
452 oslSocket sHandle;
453 // initialization
454 void setUp( )
456 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
459 void tearDown( )
461 sHandle = NULL;
464 void getLocalPort_001()
466 ::osl::Socket sSocket(sHandle);
467 ::osl::SocketAddr saBindSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT7 ); // aHostIp1 localhost
468 ::osl::SocketAddr saLocalSocketAddr;
470 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
472 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
473 ::rtl::OUString suError1 = ::rtl::OUString("Socket bind fail:") + sSocket.getErrorAsString();
474 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError1), sal_True == bOK1 );
475 sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) );
477 CPPUNIT_ASSERT_MESSAGE( "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.",
478 sal_True == bOK );
481 /** test writer's comment:
483 the invalid port number can not be set by giving invalid port number
484 such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be
485 valid, the only instance that the getLocalPort returns OSL_INVALID_PORT
486 is when saSocketAddr itself is an invalid one, that is , the IP or host name
487 can not be found, then the created socket address is not valid.
489 void getLocalPort_002()
491 ::osl::SocketAddr saBindSocketAddr( rtl::OUString("123.45.67.89"), IP_PORT_TELNET);
492 #ifdef WNT
493 ::osl::Socket sSocket(sHandle);
494 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
495 sSocket.bind( saBindSocketAddr );
496 //Invalid IP, so bind should fail
497 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
498 ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT),
499 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned.");
500 sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) );
501 (void)bOK;
502 #else
503 //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT
504 ::rtl::OUString suError ("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");
505 #endif
506 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError), sal_False );
510 void getLocalPort_003()
512 ::osl::Socket sSocket(sHandle);
513 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL);
515 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
517 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
518 ::rtl::OUString suError1 = ::rtl::OUString("Socket bind fail:") + sSocket.getErrorAsString();
519 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError1), sal_True == bOK1 );
520 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
521 ::rtl::OUString("34463"),
522 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned");
523 sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535);
525 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError), sal_True == bOK );
528 CPPUNIT_TEST_SUITE( getLocalPort );
529 CPPUNIT_TEST( getLocalPort_001 );
530 // LLA: CPPUNIT_TEST( getLocalPort_002 );
531 CPPUNIT_TEST( getLocalPort_003 );
532 CPPUNIT_TEST_SUITE_END();
534 }; // class getLocalPort
536 /** testing the method:
537 inline ::rtl::OUString SAL_CALL getLocalHost() const;
539 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;
540 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
541 will return hostname of current processor such as "aegean.PRC.Sun.COM"
544 class getLocalHost : public CppUnit::TestFixture
546 public:
547 oslSocket sHandle;
548 // initialization
549 void setUp( )
551 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
554 void tearDown( )
556 sHandle = NULL;
559 void getLocalHost_001()
561 ::osl::Socket sSocket(sHandle);
562 //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu
563 ::osl::SocketAddr saBindSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT6 );
565 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
567 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
568 ::rtl::OUString suError1 = ::rtl::OUString("Socket bind fail:") + sSocket.getErrorAsString();
569 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError1), sal_True == bOK1 );
570 sal_Bool bOK;
571 ::rtl::OUString suError;
572 #ifdef WNT
573 bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
574 suError = outputError(sSocket.getLocalHost( ), getThisHostname( ),
575 "test for getLocalHost function: create localhost socket and check name");
576 #else
577 ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" );
578 sal_Bool bRes1, bRes2;
579 bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ;
580 bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ;
581 bOK = bRes1 || bRes2;
582 suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name");
583 #endif
584 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError), sal_True == bOK );
587 void getLocalHost_002()
589 ::osl::Socket sSocket(sHandle);
590 ::osl::SocketAddr saBindSocketAddr( rtl::OUString("123.45.67.89"), IP_PORT_POP3);
591 ::osl::SocketAddr saLocalSocketAddr;
593 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
594 sSocket.bind( saBindSocketAddr );
595 //Invalid IP, so bind should fail
596 sal_Bool bOK = compareUString( sSocket.getLocalHost( ), rtl::OUString("") ) ;
597 ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), rtl::OUString(), "test for getLocalHost function: getLocalHost with invalid SocketAddr");
599 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError), sal_True == bOK );
602 CPPUNIT_TEST_SUITE( getLocalHost );
603 CPPUNIT_TEST( getLocalHost_001 );
604 CPPUNIT_TEST( getLocalHost_002 );
605 CPPUNIT_TEST_SUITE_END();
607 }; // class getLocalHost
609 /** testing the methods:
610 inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const;
611 inline sal_Int32 SAL_CALL getPeerPort() const;
612 inline ::rtl::OUString SAL_CALL getPeerHost() const;
614 class getPeer : public CppUnit::TestFixture
616 public:
617 oslSocket sHandle;
618 TimeValue *pTimeout;
619 ::osl::AcceptorSocket asAcceptorSocket;
620 ::osl::ConnectorSocket csConnectorSocket;
622 // initialization
623 void setUp( )
625 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
626 pTimeout->Seconds = 3;
627 pTimeout->Nanosec = 0;
628 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
631 void tearDown( )
633 free( pTimeout );
634 sHandle = NULL;
635 asAcceptorSocket.close( );
636 csConnectorSocket.close( );
639 void getPeer_001()
641 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT );
642 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT );
643 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString("129.158.217.202"), IP_PORT_FTP );
644 ::osl::StreamSocket ssConnection;
645 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
646 /// launch server socket
647 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
648 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind '127.0.0.1' address failed.", sal_True == bOK1 );
649 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
650 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
652 asAcceptorSocket.enableNonBlockingMode( sal_True );
653 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
655 /// launch client socket
656 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
658 /// get peer information
659 csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
660 sal_Int32 peerPort = csConnectorSocket.getPeerPort( );
661 ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( );
663 CPPUNIT_ASSERT_MESSAGE( "test for getPeer function: setup a connection and then get the peer address, port and host from client side.",
664 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&&
665 ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) &&
666 ( peerPort == saLocalSocketAddr.getPort( ) ));
669 CPPUNIT_TEST_SUITE( getPeer );
670 CPPUNIT_TEST( getPeer_001 );
671 CPPUNIT_TEST_SUITE_END();
673 }; // class getPeer
675 /** testing the methods:
676 inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface);
679 class bind : public CppUnit::TestFixture
681 public:
682 oslSocket sHandle;
683 // initialization
684 void setUp( )
686 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
689 void tearDown( )
691 sHandle = NULL;
694 void bind_001()
696 ::osl::Socket sSocket(sHandle);
697 //bind must use local IP address ---mindyliu
698 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 );
700 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
701 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
702 CPPUNIT_ASSERT_MESSAGE( "Socket bind fail.", sal_True == bOK1 );
704 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ;
706 sSocket.close();
707 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", sal_True == bOK2 );
710 void bind_002()
712 ::osl::Socket sSocket(sHandle);
713 ::osl::SocketAddr saBindSocketAddr( rtl::OUString("123.45.67.89"), IP_PORT_NETBIOS );
714 ::osl::SocketAddr saLocalSocketAddr;
716 sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True);
717 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
718 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
720 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.",
721 ( sal_False == bOK1 ) && ( sal_False == bOK2 ) );
724 CPPUNIT_TEST_SUITE( bind );
725 CPPUNIT_TEST( bind_001 );
726 CPPUNIT_TEST( bind_002 );
727 CPPUNIT_TEST_SUITE_END();
729 }; // class bind
731 /** testing the methods:
732 inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const;
735 class isRecvReady : public CppUnit::TestFixture
737 public:
738 oslSocket sHandle;
739 TimeValue *pTimeout;
740 ::osl::AcceptorSocket asAcceptorSocket;
741 ::osl::ConnectorSocket csConnectorSocket;
743 // initialization
744 void setUp( )
746 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
747 pTimeout->Seconds = 3;
748 pTimeout->Nanosec = 0;
749 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
752 void tearDown( )
754 free( pTimeout );
755 sHandle = NULL;
756 asAcceptorSocket.close( );
757 csConnectorSocket.close( );
760 void isRecvReady_001()
762 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT1 );
763 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT1 );
764 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString("129.158.217.202"), IP_PORT_FTP );
765 ::osl::StreamSocket ssConnection;
766 /// launch server socket
767 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
768 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
769 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
770 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
771 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
772 asAcceptorSocket.enableNonBlockingMode( sal_True );
773 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
775 /// launch client socket
776 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
778 /// is receive ready?
779 sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout );
781 CPPUNIT_ASSERT_MESSAGE( "test for isRecvReady function: setup a connection and then check if it can transmit data.",
782 ( sal_True == bOK3 ) );
785 CPPUNIT_TEST_SUITE( isRecvReady );
786 CPPUNIT_TEST( isRecvReady_001 );
787 CPPUNIT_TEST_SUITE_END();
789 }; // class isRecvReady
791 /** testing the methods:
792 inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const;
794 class isSendReady : public CppUnit::TestFixture
796 public:
797 oslSocket sHandle;
798 TimeValue *pTimeout;
799 ::osl::AcceptorSocket asAcceptorSocket;
800 ::osl::ConnectorSocket csConnectorSocket;
802 // initialization
803 void setUp( )
805 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
806 pTimeout->Seconds = 3;
807 pTimeout->Nanosec = 0;
808 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
811 void tearDown( )
813 free( pTimeout );
814 sHandle = NULL;
815 asAcceptorSocket.close( );
816 csConnectorSocket.close( );
819 void isSendReady_001()
821 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT );
822 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT );
823 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString("129.158.217.202"), IP_PORT_FTP );
824 ::osl::StreamSocket ssConnection;
826 /// launch server socket
827 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
828 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
829 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
830 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
831 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
832 asAcceptorSocket.enableNonBlockingMode( sal_True );
833 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
835 /// launch client socket
836 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
838 /// is send ready?
839 sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout );
841 CPPUNIT_ASSERT_MESSAGE( "test for isSendReady function: setup a connection and then check if it can transmit data.",
842 ( sal_True == bOK3 ) );
845 CPPUNIT_TEST_SUITE( isSendReady );
846 CPPUNIT_TEST( isSendReady_001 );
847 CPPUNIT_TEST_SUITE_END();
849 }; // class isSendReady
851 /** testing the methods:
852 inline oslSocketType SAL_CALL getType() const;
856 class getType : public CppUnit::TestFixture
858 public:
859 oslSocket sHandle;
860 // initialization
861 void setUp( )
866 void tearDown( )
868 sHandle = NULL;
871 void getType_001()
873 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
874 ::osl::Socket sSocket(sHandle);
876 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
877 osl_Socket_TypeStream == sSocket.getType( ) );
880 void getType_002()
882 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
883 ::osl::Socket sSocket(sHandle);
885 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
886 osl_Socket_TypeDgram == sSocket.getType( ) );
889 #ifdef UNX
890 // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here
891 // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw()
892 void getType_003()
894 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.",
895 sal_True);
897 #else
898 void getType_003()
900 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
901 ::osl::Socket sSocket(sHandle);
903 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
904 osl_Socket_TypeRaw == sSocket.getType( ) );
906 #endif
908 CPPUNIT_TEST_SUITE( getType );
909 CPPUNIT_TEST( getType_001 );
910 CPPUNIT_TEST( getType_002 );
911 CPPUNIT_TEST( getType_003 );
912 CPPUNIT_TEST_SUITE_END();
914 }; // class getType
916 /** testing the methods:
917 inline sal_Int32 SAL_CALL getOption(
918 oslSocketOption Option,
919 void* pBuffer,
920 sal_uInt32 BufferLen,
921 oslSocketOptionLevel Level= osl_Socket_LevelSocket) const;
923 inline sal_Int32 getOption( oslSocketOption option ) const;
927 class getOption : public CppUnit::TestFixture
929 public:
930 oslSocket sHandle;
931 // initialization
932 void setUp( )
937 void tearDown( )
939 sHandle = NULL;
942 /** test writer's comment:
944 in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream.
945 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ
946 in 1.
949 void getOption_001()
951 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
952 ::osl::Socket sSocket(sHandle);
953 sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
954 *pType = 0;
955 sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) );
956 sal_Bool bOK = ( SOCK_STREAM == *pType );
957 // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream
958 //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType );
959 free( pType );
961 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get type option of socket.",
962 sal_True == bOK );
965 // getsockopt error
966 void getOption_004()
968 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
969 ::osl::Socket sSocket(sHandle);
971 sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
972 sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) );
973 free( pbDontRoute );
975 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get invalid option of socket, should return -1.",
976 nRes == -1 );
979 void getOption_simple_001()
981 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
982 ::osl::Socket sSocket(sHandle);
984 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) );
986 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
987 sal_True == bOK );
990 void getOption_simple_002()
992 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
993 ::osl::Socket sSocket(sHandle);
995 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) );
997 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
998 sal_True == bOK );
1001 CPPUNIT_TEST_SUITE( getOption );
1002 CPPUNIT_TEST( getOption_001 );
1003 CPPUNIT_TEST( getOption_004 );
1004 CPPUNIT_TEST( getOption_simple_001 );
1005 CPPUNIT_TEST( getOption_simple_002 );
1006 CPPUNIT_TEST_SUITE_END();
1008 }; // class getOption
1010 /** testing the methods:
1011 inline sal_Bool SAL_CALL setOption( oslSocketOption Option,
1012 void* pBuffer,
1013 sal_uInt32 BufferLen,
1014 oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const;
1017 class setOption : public CppUnit::TestFixture
1019 public:
1020 TimeValue *pTimeout;
1021 // LLA: maybe there is an error in the source,
1022 // as long as I remember, if a derived class do not overload all ctors there is a problem.
1024 ::osl::AcceptorSocket asAcceptorSocket;
1026 void setUp( )
1031 void tearDown( )
1033 asAcceptorSocket.close( );
1036 // LLA:
1037 // getSocketOption returns BufferLen, or -1 if something failed
1039 // setSocketOption returns sal_True, if option could stored
1040 // else sal_False
1042 void setOption_001()
1044 /// set and get option.
1045 int nBufferLen = sizeof ( sal_Int32);
1046 // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool!
1048 sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
1049 *pbDontRouteSet = 1; // sal_True;
1051 sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
1052 *pGetBuffer = 0;
1054 // maybe asAcceptorSocket is not right initialized
1055 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen );
1056 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b1 ) );
1057 sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
1058 CPPUNIT_ASSERT_MESSAGE( "getOption function failed.", ( n2 == nBufferLen ) );
1060 // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value,
1061 // just judge it is zero or not!
1062 sal_Bool bOK = ( 0 != *pGetBuffer );
1063 t_print("#setOption_001: getOption is %" SAL_PRIdINT32 " \n", *pGetBuffer);
1065 // toggle check, set to 0
1066 *pbDontRouteSet = 0;
1068 sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) );
1069 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b3 ) );
1070 sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
1071 CPPUNIT_ASSERT_MESSAGE( "getOption (DONTROUTE) function failed.", ( n4 == nBufferLen ) );
1073 sal_Bool bOK2 = ( 0 == *pGetBuffer );
1075 t_print("#setOption_001: getOption is %" SAL_PRIdINT32 " \n", *pGetBuffer);
1077 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1078 ( sal_True == bOK ) && (sal_True == bOK2) );
1080 free( pbDontRouteSet );
1081 free( pGetBuffer );
1084 void setOption_002()
1086 /// set and get option.
1088 // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen );
1089 // *pbLingerSet = 7;
1090 // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen );
1091 /* struct */linger aLingerSet;
1092 sal_Int32 nBufferLen = sizeof( struct linger );
1093 aLingerSet.l_onoff = 1;
1094 aLingerSet.l_linger = 7;
1096 linger aLingerGet;
1098 asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
1100 sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen );
1101 CPPUNIT_ASSERT_MESSAGE( "getOption (SO_LINGER) function failed.", ( n1 == nBufferLen ) );
1103 //t_print("#setOption_002: getOption is %d \n", aLingerGet.l_linger);
1104 sal_Bool bOK = ( 7 == aLingerGet.l_linger );
1105 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check. ",
1106 sal_True == bOK );
1110 void setOption_003()
1112 linger aLingerSet;
1113 aLingerSet.l_onoff = 1;
1114 aLingerSet.l_linger = 7;
1116 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 );
1117 printUString( asAcceptorSocket.getErrorAsString( ) );
1118 CPPUNIT_ASSERT_MESSAGE( "setOption (SO_LINGER) function failed for optlen is 0.",
1119 ( b1 == sal_False ) );
1122 void setOption_simple_001()
1124 /// set and get option.
1125 asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True );
1126 sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
1128 t_print("setOption_simple_001(): getoption is %d \n", (int) asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
1129 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1130 ( sal_True == bOK ) );
1133 void setOption_simple_002()
1135 /// set and get option.
1136 // LLA: this does not work, due to the fact that SO_LINGER is a structure
1137 // LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 );
1138 // LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) );
1140 // LLA: CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1141 // LLA: ( sal_True == bOK ) );
1144 CPPUNIT_TEST_SUITE( setOption );
1145 // CPPUNIT_TEST( setOption_001 );
1146 CPPUNIT_TEST( setOption_002 );
1147 CPPUNIT_TEST( setOption_003 );
1148 //TODO: Check this test
1149 // CPPUNIT_TEST( setOption_simple_001 );
1150 // LLA: CPPUNIT_TEST( setOption_simple_002 );
1151 CPPUNIT_TEST_SUITE_END();
1153 }; // class setOption
1155 /** testing the method:
1156 inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode);
1158 class enableNonBlockingMode : public CppUnit::TestFixture
1160 public:
1161 ::osl::AcceptorSocket asAcceptorSocket;
1163 void enableNonBlockingMode_001()
1165 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT );
1166 ::osl::StreamSocket ssConnection;
1168 /// launch server socket
1169 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1170 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
1171 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
1172 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
1173 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
1174 asAcceptorSocket.enableNonBlockingMode( sal_True );
1175 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1177 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
1178 sal_Bool bOK = sal_True;
1179 asAcceptorSocket.close( );
1181 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",
1182 ( sal_True == bOK ) );
1185 CPPUNIT_TEST_SUITE( enableNonBlockingMode );
1186 CPPUNIT_TEST( enableNonBlockingMode_001 );
1187 CPPUNIT_TEST_SUITE_END();
1189 }; // class enableNonBlockingMode
1191 /** testing the method:
1192 inline sal_Bool SAL_CALL isNonBlockingMode() const;
1194 class isNonBlockingMode : public CppUnit::TestFixture
1196 public:
1197 ::osl::AcceptorSocket asAcceptorSocket;
1199 void isNonBlockingMode_001()
1201 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT );
1202 ::osl::StreamSocket ssConnection;
1204 /// launch server socket
1205 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
1206 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
1207 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
1208 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
1209 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
1211 sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( );
1212 asAcceptorSocket.enableNonBlockingMode( sal_True );
1213 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1215 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
1216 sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( );
1217 asAcceptorSocket.close( );
1219 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.",
1220 ( sal_False == bOK3 ) && ( sal_True == bOK4 ) );
1223 CPPUNIT_TEST_SUITE( isNonBlockingMode );
1224 CPPUNIT_TEST( isNonBlockingMode_001 );
1225 CPPUNIT_TEST_SUITE_END();
1227 }; // class isNonBlockingMode
1229 /** testing the method:
1230 inline void SAL_CALL clearError() const;
1232 class clearError : public CppUnit::TestFixture
1234 public:
1235 oslSocket sHandle;
1236 // initialization
1237 void setUp( )
1239 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1242 void tearDown( )
1244 sHandle = NULL;
1247 void clearError_001()
1249 ::osl::Socket sSocket(sHandle);
1250 ::osl::SocketAddr saBindSocketAddr( rtl::OUString("123.45.67.89"), IP_PORT_HTTP2 );
1251 ::osl::SocketAddr saLocalSocketAddr;
1252 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1253 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
1254 oslSocketError seBind = sSocket.getError( );
1255 sSocket.clearError( );
1257 CPPUNIT_ASSERT_MESSAGE( "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.",
1258 osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None );
1261 CPPUNIT_TEST_SUITE( clearError );
1262 CPPUNIT_TEST( clearError_001 );
1263 CPPUNIT_TEST_SUITE_END();
1265 }; // class clearError
1267 /** testing the methods:
1268 inline oslSocketError getError() const;
1269 inline ::rtl::OUString getErrorAsString( ) const;
1271 class getError : public CppUnit::TestFixture
1273 public:
1274 oslSocket sHandle;
1275 // initialization
1276 void setUp( )
1278 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1281 void tearDown( )
1283 sHandle = NULL;
1286 void getError_001()
1288 ::osl::Socket sSocket(sHandle);
1289 ::osl::SocketAddr saBindSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_FTP );
1290 ::osl::SocketAddr saLocalSocketAddr;
1292 CPPUNIT_ASSERT_MESSAGE( "test for getError function: should get no error.",
1293 osl_Socket_E_None == sSocket.getError( ) );
1296 void getError_002()
1298 ::osl::Socket sSocket(sHandle);
1299 ::osl::SocketAddr saBindSocketAddr( rtl::OUString("123.45.67.89"), IP_PORT_FTP );
1300 ::osl::SocketAddr saLocalSocketAddr;
1301 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1302 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
1303 //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError.
1304 #if defined(SOLARIS)
1305 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. ",
1306 osl_Socket_E_InvalidError == sSocket.getError( ) );
1307 #else
1308 //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail.
1310 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",
1311 osl_Socket_E_AddrNotAvail == sSocket.getError( ) );
1312 #endif
1315 CPPUNIT_TEST_SUITE( getError );
1316 CPPUNIT_TEST( getError_001 );
1317 CPPUNIT_TEST( getError_002 );
1318 CPPUNIT_TEST_SUITE_END();
1320 }; // class getError
1322 /** testing the methods:
1323 inline oslSocket getHandle() const;
1326 class getHandle : public CppUnit::TestFixture
1328 public:
1329 oslSocket sHandle;
1330 // initialization
1331 void setUp( )
1333 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1336 void tearDown( )
1338 sHandle = NULL;
1341 void getHandle_001()
1343 ::osl::Socket sSocket(sHandle);
1344 ::osl::Socket assignSocket = sSocket.getHandle();
1346 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
1347 osl_Socket_TypeStream == assignSocket.getType( ) );
1350 void getHandle_002()
1352 ::osl::Socket sSocket( sHandle );
1353 ::osl::Socket assignSocket ( sSocket.getHandle( ) );
1355 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
1356 osl_Socket_TypeStream == assignSocket.getType( ) );
1359 CPPUNIT_TEST_SUITE( getHandle );
1360 CPPUNIT_TEST( getHandle_001 );
1361 CPPUNIT_TEST( getHandle_002 );
1362 CPPUNIT_TEST_SUITE_END();
1364 }; // class getHandle
1366 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::ctors);
1367 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::operators);
1368 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::close);
1369 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::getLocalAddr);
1370 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::getLocalPort);
1371 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::getLocalHost);
1372 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::getPeer);
1373 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::bind);
1374 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::isRecvReady);
1375 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::isSendReady);
1376 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::getType);
1377 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::getOption);
1378 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::setOption);
1379 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::enableNonBlockingMode);
1380 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::isNonBlockingMode);
1381 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::clearError);
1382 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::getError);
1383 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Socket::getHandle);
1385 } // namespace osl_Socket
1387 // this macro creates an empty function, which will called by the RegisterAllFunctions()
1388 // to let the user the possibility to also register some functions by hand.
1389 CPPUNIT_PLUGIN_IMPLEMENT();
1391 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */