xref: /aoo42x/main/sal/qa/osl/socket/osl_SocketAddr.cxx (revision 30acf5e8)
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_ZERO   0
66 #define IP_PORT_FTP    21
67 #define IP_PORT_TELNET 23
68 #define IP_PORT_HTTP1  80
69 #define IP_PORT_HTTP2  8080
70 
71 #define IP_PORT_MYPORT  8881 	//8888
72 #define IP_PORT_MYPORT2  8883	//8890
73 #define IP_PORT_MYPORT3  8884	//8891
74 #define IP_PORT_INVAL  99999
75 #define IP_PORT_MYPORT4  8885	//8892
76 #define IP_PORT_NETBIOS_DGM  138
77 
78 
79 namespace osl_SocketAddr
80 {
81 
82 	/** testing the methods:
83 		inline SocketAddr();
84 		inline SocketAddr(const SocketAddr& Addr);
85 		inline SocketAddr(const oslSocketAddr , __osl_socket_NoCopy nocopy );
86 		inline SocketAddr(oslSocketAddr Addr);
87 		inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort );
88 	*/
89 
90 	class ctors : public ::testing::Test
91 	{
92 	public:
93 	}; // class ctors
94 
TEST_F(ctors,ctors_none)95     TEST_F(ctors, ctors_none)
96     {
97         /// SocketAddr constructor.
98         ::osl::SocketAddr saSocketAddr;
99 
100         // oslSocketResult aResult;
101         // rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult);
102 
103         // rtl::OUString suHost2 = getThisHostname();
104 
105         ASSERT_TRUE(sal_True == saSocketAddr.is( )) << "test for none parameter constructor function: check if the socket address was created successfully";
106     }
107 
TEST_F(ctors,ctors_none_000)108     TEST_F(ctors, ctors_none_000)
109     {
110         /// SocketAddr constructor.
111         ::osl::SocketAddr saSocketAddr;
112 
113         oslSocketResult aResult;
114         rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult);
115         rtl::OUString suHost2 = getThisHostname();
116 
117         sal_Bool bOk = compareUString(suHost, suHost2);
118 
119         rtl::OUString suError = rtl::OUString::createFromAscii("Host names should be the same. From SocketAddr.getLocalHostname() it is'");
120         suError += suHost;
121         suError += rtl::OUString::createFromAscii("', from getThisHostname() it is '");
122         suError += suHost2;
123         suError += rtl::OUString::createFromAscii("'.");
124 
125         ASSERT_TRUE(sal_True == bOk) << suError.pData;
126     }
127 
TEST_F(ctors,ctors_copy)128     TEST_F(ctors, ctors_copy)
129     {
130         /// SocketAddr copy constructor.
131         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
132         ::osl::SocketAddr saCopySocketAddr( saSocketAddr );
133 
134         sal_Int32 nPort = saCopySocketAddr.getPort( );
135 
136         ASSERT_TRUE(( sal_True == saCopySocketAddr.is( ) ) && ( nPort == IP_PORT_HTTP1 )) << "test for SocketAddr copy constructor function: copy constructor, do an action of copy construction then check the port with original set.";
137     }
138 
TEST_F(ctors,ctors_copy_no_001)139     TEST_F(ctors, ctors_copy_no_001)
140     {
141 #if 0
142         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
143         oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( );
144 
145         ::osl::SocketAddr saSocketAddrCopy( psaOSLSocketAddr, SAL_NO_COPY );
146         saSocketAddrCopy.setPort( IP_PORT_HTTP2 );
147 
148         ASSERT_TRUE(saSocketAddr.getPort( ) == IP_PORT_HTTP2) << "test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.";
149 #endif
150         ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
151         ASSERT_TRUE(pSocketAddr != NULL) << "check for new SocketAddr";
152 
153         oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
154 
155         ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
156 
157         pSocketAddrCopy->setPort( IP_PORT_HTTP2 );
158         ASSERT_TRUE(pSocketAddr->getPort( ) == IP_PORT_HTTP2) << "test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.";
159 
160         delete pSocketAddrCopy;
161         // LLA: don't do this also:           delete pSocketAddr;
162     }
163 
TEST_F(ctors,ctors_copy_no_002)164     TEST_F(ctors, ctors_copy_no_002)
165     {
166         ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
167             ASSERT_TRUE(pSocketAddr != NULL) << "check for new SocketAddr";
168             oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
169             ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
170 
171             ASSERT_TRUE(pSocketAddr->getHandle( ) ==  pSocketAddrCopy->getHandle( )) << "test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.";
172 
173             delete pSocketAddrCopy;
174     }
175 
TEST_F(ctors,ctors_copy_handle_001)176     TEST_F(ctors, ctors_copy_handle_001)
177     {
178         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
179         ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) );
180 
181         ASSERT_TRUE(saSocketAddrCopy.getPort( ) == IP_PORT_HTTP1) << "test for SocketAddr copy handle constructor function: copy another Socket's handle, get its port to check copy effect.";
182     }
183 
TEST_F(ctors,ctors_copy_handle_002)184     TEST_F(ctors, ctors_copy_handle_002)
185     {
186         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
187         ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) );
188         saSocketAddrCopy.setPort( IP_PORT_HTTP2 );
189 
190         ASSERT_TRUE(saSocketAddr.getPort( ) != IP_PORT_HTTP2) << "test for SocketAddr copy handle constructor function: copy another Socket's handle, the original one should not be changed.";
191     }
192 
TEST_F(ctors,ctors_hostname_port_001)193     TEST_F(ctors, ctors_hostname_port_001)
194     {
195         /// tcpip-specif constructor.
196         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
197         printUString( saSocketAddr.getHostname( ), "ctors_hostname_port_001:getHostname");
198 
199         ASSERT_TRUE(saSocketAddr.is( ) == sal_True &&
200                                 ( saSocketAddr.getPort( ) == IP_PORT_FTP )/*&&
201                                 ( sal_True == compareUString( saSocketAddr.getHostname( ), aHostName1 ) ) */) << "test for SocketAddr tcpip specific constructor function: do a constructor using tcpip spec, check the result.";
202     }
203 
204     //same as is_002
TEST_F(ctors,ctors_hostname_port_002)205     TEST_F(ctors, ctors_hostname_port_002)
206     {
207         /// tcpip-specif constructor.
208         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT2 );
209 
210         ASSERT_TRUE(sal_False == saSocketAddr.is( )) << "test for SocketAddr tcpip specific constructor function: using an invalid IP address, the socketaddr ctors should fail";
211     }
212 
213 
214 	/** testing the method:
215 		inline sal_Bool is() const;
216 	*/
217 
218 	class is : public ::testing::Test
219 	{
220 	public:
221 	}; // class is
222 
TEST_F(is,is_001)223     TEST_F(is, is_001)
224     {
225         ::osl::SocketAddr saSocketAddr;
226 
227         ASSERT_TRUE(sal_True == saSocketAddr.is( )) << "test for is() function: create an unknown type socket, it should be True when call is.";
228     }
229     // refer to setPort_003()
TEST_F(is,is_002)230     TEST_F(is, is_002)
231     {
232         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_INVAL );
233 
234         ASSERT_TRUE(sal_True == saSocketAddr.is( )) << "test for is() function: create a tcp-ip socket using invalid port number";
235     }
236 
TEST_F(is,is_003)237     TEST_F(is, is_003)
238     {
239         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT );
240 
241         ASSERT_TRUE(sal_True != saSocketAddr.is( )) << "test for is() function: create a tcp-ip socket using invalid Ip number";
242     }
243 
244 	/** testing the method:
245 		inline ::rtl::OUString SAL_CALL getHostname( oslSocketResult *pResult = 0 ) const;
246 	*/
247 
248 	class getHostname : public ::testing::Test
249 	{
250 	public:
SetUp()251 		void SetUp()
252 		{
253 		}
254 
TearDown()255 		void TearDown()
256 		{
257 		}
258 	}; // class getHostname
259 
TEST_F(getHostname,getHostname_000)260     TEST_F(getHostname, getHostname_000)
261         {
262             ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.107"), IP_PORT_FTP );
263             rtl::OUString suResult = saSocketAddr.getHostname( 0 );
264 
265         }
266 
267     /** it will search the Ip in current machine's /etc/hosts at first, if find, then return the
268         mapped hostname, otherwise, it will search via DNS server, and often return hostname+ Domain name
269         like "sceri.PRC.Sun.COM"
270         The process is same as Socket::getLocalHost(), but getLocalHost can only return hostname of the current machine.
271     */
TEST_F(getHostname,getHostname_001)272     TEST_F(getHostname, getHostname_001)
273     {
274         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.107"), IP_PORT_FTP );
275         rtl::OUString suResult = saSocketAddr.getHostname( 0 );
276         rtl::OUString suError = outputError(suResult, rtl::OUString::createFromAscii("sceri.PRC.Sun.COM"), "test for getHostname(0)");
277         sal_Bool bOK = compareUString( suResult, rtl::OUString::createFromAscii("sceri.PRC.Sun.COM") );
278         // search the returned hostname in /etc/hosts, if find, and the IP in the row is same as IP
279         // in the Addr, it's right also.
280         if ( bOK == sal_False)
281         {
282             rtl::OString aString = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US );
283             if ( compareUString( getIPbyName( aString ), rtl::OUString::createFromAscii("129.158.217.107") ) == sal_True )
284                 bOK = sal_True;
285         }
286         ASSERT_TRUE(sal_True == bOK) << suError.pData;
287     }
288 
289 // LLA: now we have to control, if this behaviour is right.
290 // LLA: this function does not work in company (Linux, Windows) but at home
TEST_F(getHostname,getHostname_002)291     TEST_F(getHostname, getHostname_002)
292     {
293         rtl::OUString suHostname = rtl::OUString::createFromAscii("cn-1.germany.sun.com");
294         rtl::OString aString = ::rtl::OUStringToOString( suHostname, RTL_TEXTENCODING_ASCII_US );
295         rtl::OUString aHostIP    = getIPbyName( aString );
296 
297         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_FTP );
298         sal_Bool bOK = saSocketAddr.setHostname( suHostname );
299         ASSERT_TRUE(sal_True == bOK) << "#SocketAddr.setHostname failed";
300         oslSocketResult aResult;
301         rtl::OUString suResult = saSocketAddr.getHostname( &aResult );
302         ASSERT_TRUE(aResult == osl_Socket_Ok) << "SocketAddr.getHostname failed.";
303 
304         rtl::OUString suError = outputError(suResult, suHostname, "test for getHostname(0)");
305         bOK = compareUString( suResult, suHostname );
306         if ( bOK == sal_False)
307         {
308             rtl::OString aStringResult = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US );
309             rtl::OString aStringHostname = ::rtl::OUStringToOString( suHostname, RTL_TEXTENCODING_ASCII_US );
310             if ( compareUString( getIPbyName( aStringResult ) ,  getIPbyName( aStringHostname ) ) == sal_True )
311             {
312                 bOK = sal_True;
313             }
314         }
315 
316         ASSERT_TRUE(sal_True == bOK) << suError.pData;
317     }
318 
319 	/** testing the method:
320 		inline sal_Int32 SAL_CALL getPort() const;
321 	*/
322 
323 	class getPort : public ::testing::Test
324 	{
325 	public:
326 	}; // class getPort
327 
TEST_F(getPort,getPort_001)328     TEST_F(getPort, getPort_001)
329     {
330         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
331 
332         ASSERT_TRUE(IP_PORT_FTP == saSocketAddr.getPort( )) << "test for getPort() function: get a normal port number.";
333     }
334 
TEST_F(getPort,getPort_002)335     TEST_F(getPort, getPort_002)
336     {
337         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_INVAL );
338 
339         //t_print("#getPort_002: Port number is %d \n", saSocketAddr.getPort( ));
340 
341         ASSERT_TRUE(saSocketAddr.getPort( )>=1 && saSocketAddr.getPort( ) <= 65535) << "test for getPort( ) function: give an invalid port to a SocketAddr, get the port to see if it can detect. it did not pass in (W32).";
342     }
343     //two cases will return OSL_INVALID_PORT: 1. not valid SocketAddr
344     //2. SocketAddr family is not osl_Socket_FamilyInet, but case 2 could not be constructed
TEST_F(getPort,getPort_003)345     TEST_F(getPort, getPort_003)
346     {
347         rtl::OUString suInvalidIP = rtl::OUString::createFromAscii("123.345.67.89");
348         ::osl::SocketAddr saSocketAddr( suInvalidIP, IP_PORT_MYPORT );
349 
350         ASSERT_TRUE(saSocketAddr.getPort( ) == OSL_INVALID_PORT) << "test for getPort( ) function: give an invalid IP to a SocketAddr, get the port to see returned value. ";
351     }
352 
353 	/** testing the method:
354 		inline sal_Bool SAL_CALL setPort( sal_Int32 nPort );
355 	    rfc1413.txt: TCP port numbers are from 1-65535
356 	    rfc1700.txt: 0/tcp    Reserved ;  0/udp    Reserved
357 	*/
358 
359 	class setPort : public ::testing::Test
360 	{
361 	public:
362 	}; // class setPort
363 
TEST_F(setPort,setPort_001)364     TEST_F(setPort, setPort_001)
365     {
366         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
367         sal_Bool bOK = saSocketAddr.setPort( IP_PORT_TELNET );
368 
369         ASSERT_TRUE(( sal_True == bOK ) &&
370                                 ( IP_PORT_TELNET == saSocketAddr.getPort( ) )) << "test for setPort() function: modify a port number setting, and check it.";
371     }
372 
373     /** 0 to 1024 is known as the reserved port range (traditionally only root can assign programs to ports in
374         this range) and the ephemeral port range from 1025 to 65535.
375         As many of you programmers will know, when you specify the source port of 0 when you connect to a host,
376         the OS automatically reassigns the port number to high numbered ephemeral port. The same happens if you
377         try to bind a listening socket to port 0.
378         http://www.securiteam.com/securityreviews/5XP0Q2AAKS.html
379         another: http://www.muq.org/~cynbe/muq/mufref_564.html
380     */
TEST_F(setPort,setPort_002)381     TEST_F(setPort, setPort_002)
382     {
383         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
384         sal_Bool bOK = saSocketAddr.setPort( IP_PORT_ZERO );
385 
386         oslSocket sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
387         ::osl::Socket sSocket(sHandle);
388         sSocket.setOption( osl_Socket_OptionReuseAddr, 1 );//sal_True);
389         sal_Bool bOK1 = sSocket.bind( saSocketAddr );
390         ASSERT_TRUE(bOK1 == sal_True) << "bind SocketAddr failed";
391 
392         sal_Int32 newPort = sSocket.getLocalPort();
393         //t_print("#new port is %d\n", newPort );
394 
395         ASSERT_TRUE(( 1024 <= newPort ) && ( 65535 >= newPort ) && ( bOK == sal_True )) << "test for setPort() function: port number should be in 1 ~ 65535, set port 0, it should be converted to a port number between 1024~65535.";
396 
397     }
398 
TEST_F(setPort,setPort_003)399     TEST_F(setPort, setPort_003)
400     {
401         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP);
402         sal_Bool bOK = saSocketAddr.setPort( IP_PORT_INVAL );
403         //on Linux, getPort return 34463
404         //t_print("#Port number is %d \n", saSocketAddr.getPort( ));
405 
406         ASSERT_TRUE(( ( 1 <= saSocketAddr.getPort( ) ) && ( 65535 >= saSocketAddr.getPort( ) ) &&( bOK == sal_True ) ) ||
407                                  bOK == sal_False) << "test for setPort( ) function: set an address with invalid port. it should return error or convert it to a valid port.";
408     }
409 
410     /* this is not a inet-addr => can't set port */
TEST_F(setPort,setPort_004)411     TEST_F(setPort, setPort_004)
412     {
413         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_FTP);
414         sal_Bool bOK = saSocketAddr.setPort( IP_PORT_MYPORT );
415 
416         ASSERT_TRUE(bOK == sal_False) << "test for setPort( ) function: set an invalid address with valid port. it should return error.";
417     }
418 
419 	/**  tester comment:
420 
421 		In the following two functions, it use ::rtl::ByteSequence as an intermediate storage for address,
422 		the ByteSequence object can hold sal_Int8 arrays, which is raged [-127, 127], in case of IP addr
423 		that is greater than 127, say 129.158.217.202, it will stored as -127, -98, -39, -54,  it is unique
424 		in the range of sal_Int8, but lack of readability.
425 		so may be a sal_uInt8 array is better.
426 	*/
427 
428 
429 	/** testing the method:
430 		inline sal_Bool SAL_CALL setAddr( const ::rtl::ByteSequence & address );
431 	*/
432 
433 	class setAddr : public ::testing::Test
434 	{
435 	public:
436 	}; // class setAddr
437 
TEST_F(setAddr,setAddr_001)438     TEST_F(setAddr, setAddr_001)
439     {
440         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
441         saSocketAddr.setAddr( UStringIPToByteSequence( rtl::OUString::createFromAscii("127.0.0.1") ) );
442         ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 );
443         sal_Bool bOK = sal_False;
444 
445         // if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) && ( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
446         //  bOK = sal_True;
447         bOK = ifIpv4is( bsSocketAddr, 127, 0, 0, 1 );
448 
449         ASSERT_TRUE(sal_True == bOK) << "test for setAddr() function: construct Addr with  \"129.158.217.202\", set it to \"127.0.0.1\",  and check the correctness ";
450     }
451 
452 	/** testing the method:
453 		inline ::rtl::ByteSequence  SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const;
454 	*/
455 
456 	class getAddr : public ::testing::Test
457 	{
458 	public:
459 	}; // class getAddr
460 
TEST_F(getAddr,getAddr_001)461     TEST_F(getAddr, getAddr_001)
462     {
463         oslSocketResult SocketResult;
464         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
465         ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( &SocketResult );
466 
467         sal_Bool bOK = sal_False;
468 
469         //if ( ( osl_Socket_Ok == SocketResult ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
470         //  bOK = sal_True;
471         bOK = ifIpv4is( bsSocketAddr, 127, 0, 0, 1 );
472 
473         ASSERT_TRUE(sal_True == bOK && SocketResult == osl_Socket_Ok) << "test for getAddr() function: construct a socketaddr with IP assigned, get the address to check correctness.Caught unknown exception on (Win32)";
474     }
475 
476 	/** testing the methods:
477 		inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr);
478 		inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr);
479 		inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy );
480 		inline sal_Bool SAL_CALL operator== (oslSocketAddr Addr) const;
481 		inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;    /// not implemented.
482 	*/
483 
484 	class operator_equal : public ::testing::Test
485 	{
486 	public:
487 	}; // class operator_equal
488 
TEST_F(operator_equal,operator_equal_001)489     TEST_F(operator_equal, operator_equal_001)
490     {
491         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET);
492         ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
493 
494         saSocketAddrEqual = saSocketAddr;
495         sal_Bool bOK = sal_False;
496         ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 );
497 
498         // if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
499         if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&  ( ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ) == sal_True ) )
500             bOK = sal_True;
501 
502         ASSERT_TRUE(sal_True == bOK) << "test for operator_equal() function: use operator= to assign Ip1 to Ip2, check its modification.";
503     }
504 
505 
TEST_F(operator_equal,operator_equal_002)506     TEST_F(operator_equal, operator_equal_002)
507     {
508         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.199"), IP_PORT_TELNET);
509         ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
510 
511         saSocketAddrEqual = saSocketAddr;
512         ASSERT_TRUE(IP_PORT_TELNET == saSocketAddrEqual.getPort( )) << "after assign, the assigned SocketAddr is not same as the original Addr";
513         saSocketAddrEqual.setPort( IP_PORT_MYPORT3 );
514         saSocketAddr.setPort( IP_PORT_HTTP2 );
515 
516         ASSERT_TRUE(IP_PORT_MYPORT3 == saSocketAddrEqual.getPort( )) << "test for operator_equal() function: perform an equal action, then try to change the original address's port. it should not be changed ( handle released), it did not pass in (W32), this is under discussion.";
517     }
518 
TEST_F(operator_equal,operator_equal_const_001)519     TEST_F(operator_equal, operator_equal_const_001)
520     {
521         const ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET);
522         ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
523 
524         saSocketAddrEqual = saSocketAddr;
525         sal_Bool bOK = sal_False;
526         ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 );
527 
528         // if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
529         if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) && ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ) == sal_True )
530             bOK = sal_True;
531 
532         ASSERT_TRUE(sal_True == bOK) << "test for operator_equal_const() function: use operator= const to assign Ip1 to Ip2, verify the change on the second one.";
533     }
534 
TEST_F(operator_equal,operator_equal_const_002)535     TEST_F(operator_equal, operator_equal_const_002)
536     {
537         const ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET);
538         ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
539 
540         saSocketAddrEqual = saSocketAddr;
541         saSocketAddrEqual.setPort( IP_PORT_HTTP1 );
542 
543         ASSERT_TRUE(IP_PORT_HTTP1 != saSocketAddr.getPort( )) << "test for operator_equal_const() function: change the second instance, the first one should not be altered, since it does not released the handle.";
544     }
545 
TEST_F(operator_equal,operator_equal_assign_001)546     TEST_F(operator_equal, operator_equal_assign_001)
547     {
548         ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET );
549             ASSERT_TRUE(pSocketAddr != NULL) << "check for new SocketAddr";
550             ::osl::SocketAddr* pSocketAddrAssign = new ::osl::SocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
551             oslSocketAddr poslSocketAddr = pSocketAddr->getHandle( );
552             //if( m_handle ) osl_destroySocketAddr( m_handle ); so pSocketAddrAssign had been destroyed and then point to pSocketAddr
553             pSocketAddrAssign->assign(poslSocketAddr, SAL_NO_COPY);
554 
555             ASSERT_TRUE(pSocketAddrAssign->getPort( ) == IP_PORT_TELNET) << "test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.";
556 
557             delete pSocketAddrAssign;
558     }
559 
TEST_F(operator_equal,operator_is_equal_001)560     TEST_F(operator_equal, operator_is_equal_001)
561     {
562         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET);
563         ::osl::SocketAddr saSocketAddrequal( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET );
564 
565         ASSERT_TRUE(sal_True == ( saSocketAddrequal == saSocketAddr.getHandle( ) )) << "test for operator_equal_equal() function: check two identical Address.";
566     }
567 
TEST_F(operator_equal,operator_is_equal_002)568     TEST_F(operator_equal, operator_is_equal_002)
569     {
570         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP);
571         ::osl::SocketAddr saSocketAddrequal( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET );
572 
573         ASSERT_TRUE(sal_False == ( saSocketAddrequal == saSocketAddr.getHandle( ) )) << "test for operator_equal_equal() function: check two different Address.";
574     }
575 
576 
577 	/** testing the method:
578 		inline oslSocketAddr SAL_CALL getHandle() const;
579 	*/
580 
581 	class getSocketAddrHandle : public ::testing::Test
582 	{
583 	public:
584 	}; // class getSocketAddrHandle
585 
TEST_F(getSocketAddrHandle,getSocketAddrHandle_001)586     TEST_F(getSocketAddrHandle, getSocketAddrHandle_001)
587     {
588         ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
589             ASSERT_TRUE(pSocketAddr != NULL) << "check for new SocketAddr";
590             oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
591             ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
592 
593             ASSERT_TRUE(pSocketAddr->getHandle( ) ==  pSocketAddrCopy->getHandle( )) << "test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.";
594 
595             delete pSocketAddrCopy;
596     }
597 
TEST_F(getSocketAddrHandle,getSocketAddrHandle_002)598     TEST_F(getSocketAddrHandle, getSocketAddrHandle_002)
599     {
600         ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("deuce.PRC.Sun.COM"), IP_PORT_MYPORT4 );
601         oslSocketAddr poslSocketAddr = saSocketAddr.getHandle( );
602 
603         sal_Bool bOK = ( saSocketAddr == poslSocketAddr );
604         //t_print("getSocketAddrHandle_002\n");
605         ASSERT_TRUE(sal_True == bOK) << "test for getHandle() function: use getHandle() function as an intermediate way to create identical address.";
606     }
607 
608 	/** testing the method:
609 		static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0);
610 	*/
611 
612 	class getLocalHostname : public ::testing::Test
613 	{
614 	public:
615 	}; // class getLocalHostname
616 
617     /* the process of getLocalHostname: 1.gethostname (same as /bin/hostname) returned name A
618        2. search A in /etc/hosts, if there is an alias name is A, return the name in the same row
619     */
620 
TEST_F(getLocalHostname,getLocalHostname_000)621     TEST_F(getLocalHostname, getLocalHostname_000)
622         {
623             // _osl_getFullQualifiedDomainName( );
624             oslSocketResult aResult = osl_Socket_Error;
625             rtl::OUString suHostname = osl::SocketAddr::getLocalHostname(&aResult);
626             ASSERT_TRUE(aResult == osl_Socket_Ok) << "getLocalHostname failed";
627         }
628 
TEST_F(getLocalHostname,getLocalHostname_001)629     TEST_F(getLocalHostname, getLocalHostname_001)
630     {
631         oslSocketResult *pResult = NULL;
632         //printSocketResult(*pResult);
633         ::rtl::OUString suResult = ::osl::SocketAddr::getLocalHostname( pResult );
634 
635         // LLA: IMHO localhost, or hostname by itself should be ok.
636         rtl::OUString suThisHost = getThisHostname( );
637         bool bOk = false;
638         if (suThisHost.equals(rtl::OUString::createFromAscii("localhost")))
639         {
640             bOk = true;
641         }
642         else
643         {
644             if (suThisHost.equals(suResult))
645             {
646                 bOk = true;
647             }
648         }
649 
650         ::rtl::OUString suError;
651         suError = outputError(suResult, getThisHostname( ), "test for getLocalHostname() function");
652 
653         ASSERT_TRUE(bOk == true) << suError.pData;
654     }
655 
656 	/** testing the method:
657 		static inline void SAL_CALL resolveHostname( const ::rtl::OUString & strHostName , SocketAddr & Addr );
658 	*/
659 
660 	class resolveHostname : public ::testing::Test
661 	{
662 	public:
663 	}; // class resolveHostname
664 
TEST_F(resolveHostname,resolveHostname_001)665     TEST_F(resolveHostname, resolveHostname_001)
666     {
667         ::osl::SocketAddr saSocketAddr;
668         ::osl::SocketAddr::resolveHostname( rtl::OUString::createFromAscii("127.0.0.1"), saSocketAddr );
669         ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 );
670         sal_Bool bOK = sal_False;
671 
672          if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
673             bOK = sal_True;
674 
675         ASSERT_TRUE(sal_True == bOK) << "test for resolveHostname() function: try to resolve localhost to 127.0.0.1.";
676     }
677 
678 	/** testing the method:
679 		static inline sal_Int32 SAL_CALL getServicePort(
680 			const ::rtl::OUString& strServiceName,
681 			const ::rtl::OUString & strProtocolName= ::rtl::OUString::createFromAscii( "tcp" ) );
682 	*/
683 
684 	class gettheServicePort : public ::testing::Test
685 	{
686 	public:
687 	}; // class gettheServicePort
688 
TEST_F(gettheServicePort,gettheServicePort_001)689     TEST_F(gettheServicePort, gettheServicePort_001)
690     {
691         rtl::OUString suServiceFTP  = rtl::OUString::createFromAscii( "ftp" );
692         rtl::OUString suProtocolTCP = rtl::OUString::createFromAscii( "tcp" );
693 
694         ASSERT_TRUE(IP_PORT_FTP== ::osl::SocketAddr::getServicePort( suServiceFTP, suProtocolTCP )) << "test for getServicePort() function: try to get ftp service port on TCP protocol.";
695     }
696 
TEST_F(gettheServicePort,gettheServicePort_002)697     TEST_F(gettheServicePort, gettheServicePort_002)
698     {
699         rtl::OUString suServiceTELNET  = rtl::OUString::createFromAscii( "telnet" );
700         rtl::OUString suProtocolTCP    = rtl::OUString::createFromAscii( "tcp" );
701         ASSERT_TRUE(IP_PORT_TELNET== ::osl::SocketAddr::getServicePort( suServiceTELNET, suProtocolTCP )) << "test for getServicePort() function: try to get telnet service port on TCP protocol.";
702     }
703 
TEST_F(gettheServicePort,gettheServicePort_003)704     TEST_F(gettheServicePort, gettheServicePort_003)
705     {
706     //Solaris has no service called "https", please see /etc/services
707         rtl::OUString suServiceNETBIOS = rtl::OUString::createFromAscii( "netbios-dgm" );
708         rtl::OUString suProtocolUDP    = rtl::OUString::createFromAscii( "udp" );
709         ASSERT_TRUE(IP_PORT_NETBIOS_DGM == ::osl::SocketAddr::getServicePort( suServiceNETBIOS, suProtocolUDP )) << "test for getServicePort() function: try to get netbios-ssn service port on UDP protocol.";
710     }
711 
TEST_F(gettheServicePort,gettheServicePort_004)712     TEST_F(gettheServicePort, gettheServicePort_004)
713     {
714         rtl::OUString suProtocolUDP    = rtl::OUString::createFromAscii( "udp" );
715         ASSERT_TRUE(OSL_INVALID_PORT == ::osl::SocketAddr::getServicePort( ::rtl::OUString::createFromAscii( "notexist" ), suProtocolUDP )) << "test for getServicePort() function: try to get a service port which is not exist.";
716     }
717 
718 	/** testing the method:
719 
720 	*/
721 
722 	class getFamilyOfSocketAddr : public ::testing::Test
723 	{
724 	public:
725 	}; // class getFamilyOfSocketAddr
726 
TEST_F(getFamilyOfSocketAddr,getFamilyOfSocketAddr_001)727     TEST_F(getFamilyOfSocketAddr, getFamilyOfSocketAddr_001)
728     {
729             ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
730             oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( );
731             ASSERT_EQ(
732                 osl_Socket_FamilyInet,
733                 osl_getFamilyOfSocketAddr( psaOSLSocketAddr ) );
734 
735         ASSERT_TRUE(osl_getFamilyOfSocketAddr( psaOSLSocketAddr ) == osl_Socket_FamilyInet) << "test for osl_getFamilyOfSocketAddr.";
736     }
737 
738 } // namespace osl_SocketAddr
739 
main(int argc,char ** argv)740 int main(int argc, char **argv)
741 {
742     ::testing::InitGoogleTest(&argc, argv);
743     return RUN_ALL_TESTS();
744 }
745