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