update dev300-m57
[ooovba.git] / sal / qa / osl / socket / osl_SocketAddr.cxx
blob8942e3a7b42092dc28e1ec7aa30b52d8100da77c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: osl_SocketAddr.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 /** test coder preface:
35 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
36 if you are not including ws2_32.lib in makefile.mk, the including format will be like this:
38 .IF "$(GUI)" == "WNT"
39 SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
40 SHL1STDLIBS += ws2_32.lib
41 .ENDIF
43 likewise on Solaris platform.
44 .IF "$(GUI)" == "UNX"
45 SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
46 SHL1STDLIBS += -lsocket -ldl -lnsl
47 .ENDIF
49 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
50 category.
52 3. some fragment of Socket source implementation are lack of comment so it is hard for testers
53 guess what the exact functionality or usage of a member. Hope the Socket section's comment
54 will be added.
56 4. following functions are declared but not implemented:
57 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
60 //------------------------------------------------------------------------
61 // include files
62 //------------------------------------------------------------------------
64 #include <cppunit/simpleheader.hxx>
66 //#include "osl_Socket_Const.h"
67 #include "sockethelper.hxx"
69 using namespace osl;
70 using namespace rtl;
72 #define IP_PORT_ZERO 0
73 #define IP_PORT_FTP 21
74 #define IP_PORT_TELNET 23
75 #define IP_PORT_HTTP1 80
76 #define IP_PORT_HTTP2 8080
78 #define IP_PORT_MYPORT 8881 //8888
79 #define IP_PORT_MYPORT2 8883 //8890
80 #define IP_PORT_MYPORT3 8884 //8891
81 #define IP_PORT_INVAL 99999
82 #define IP_PORT_MYPORT4 8885 //8892
83 #define IP_PORT_NETBIOS_DGM 138
86 namespace osl_SocketAddr
89 /** testing the methods:
90 inline SocketAddr();
91 inline SocketAddr(const SocketAddr& Addr);
92 inline SocketAddr(const oslSocketAddr , __osl_socket_NoCopy nocopy );
93 inline SocketAddr(oslSocketAddr Addr);
94 inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort );
97 class ctors : public CppUnit::TestFixture
99 public:
101 void ctors_none()
103 /// SocketAddr constructor.
104 ::osl::SocketAddr saSocketAddr;
106 // oslSocketResult aResult;
107 // rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult);
109 // rtl::OUString suHost2 = getThisHostname();
111 CPPUNIT_ASSERT_MESSAGE("test for none parameter constructor function: check if the socket address was created successfully",
112 sal_True == saSocketAddr.is( ) );
115 void ctors_none_000()
117 /// SocketAddr constructor.
118 ::osl::SocketAddr saSocketAddr;
120 oslSocketResult aResult;
121 rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult);
122 rtl::OUString suHost2 = getThisHostname();
124 sal_Bool bOk = compareUString(suHost, suHost2);
126 rtl::OUString suError = rtl::OUString::createFromAscii("Host names should be the same. From SocketAddr.getLocalHostname() it is'");
127 suError += suHost;
128 suError += rtl::OUString::createFromAscii("', from getThisHostname() it is '");
129 suError += suHost2;
130 suError += rtl::OUString::createFromAscii("'.");
132 CPPUNIT_ASSERT_MESSAGE(suError, sal_True == bOk);
135 void ctors_copy()
137 /// SocketAddr copy constructor.
138 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
139 ::osl::SocketAddr saCopySocketAddr( saSocketAddr );
141 sal_Int32 nPort = saCopySocketAddr.getPort( );
143 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy constructor function: copy constructor, do an action of copy construction then check the port with original set.",
144 ( sal_True == saCopySocketAddr.is( ) ) && ( nPort == IP_PORT_HTTP1 ) );
147 void ctors_copy_no_001()
149 #if 0
150 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
151 oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( );
153 ::osl::SocketAddr saSocketAddrCopy( psaOSLSocketAddr, SAL_NO_COPY );
154 saSocketAddrCopy.setPort( IP_PORT_HTTP2 );
156 CPPUNIT_ASSERT_MESSAGE("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.",
157 saSocketAddr.getPort( ) == IP_PORT_HTTP2 );
158 #endif
159 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
160 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
162 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
164 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
166 pSocketAddrCopy->setPort( IP_PORT_HTTP2 );
167 CPPUNIT_ASSERT_MESSAGE("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.",
168 pSocketAddr->getPort( ) == IP_PORT_HTTP2 );
170 delete pSocketAddrCopy;
171 // LLA: don't do this also: delete pSocketAddr;
174 void ctors_copy_no_002()
176 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
177 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
178 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
179 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
181 CPPUNIT_ASSERT_MESSAGE("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.",
182 pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) );
184 delete pSocketAddrCopy;
187 void ctors_copy_handle_001()
189 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
190 ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) );
192 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, get its port to check copy effect.",
193 saSocketAddrCopy.getPort( ) == IP_PORT_HTTP1 );
196 void ctors_copy_handle_002()
198 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
199 ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) );
200 saSocketAddrCopy.setPort( IP_PORT_HTTP2 );
202 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, the original one should not be changed.",
203 saSocketAddr.getPort( ) != IP_PORT_HTTP2 );
206 void ctors_hostname_port_001()
208 /// tcpip-specif constructor.
209 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
210 printUString( saSocketAddr.getHostname( ), "ctors_hostname_port_001:getHostname");
212 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: do a constructor using tcpip spec, check the result.",
213 saSocketAddr.is( ) == sal_True &&
214 ( saSocketAddr.getPort( ) == IP_PORT_FTP )/*&&
215 ( sal_True == compareUString( saSocketAddr.getHostname( ), aHostName1 ) ) */);
218 //same as is_002
219 void ctors_hostname_port_002()
221 /// tcpip-specif constructor.
222 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT2 );
224 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: using an invalid IP address, the socketaddr ctors should fail", sal_False == saSocketAddr.is( ));
226 CPPUNIT_TEST_SUITE( ctors );
227 CPPUNIT_TEST( ctors_none );
228 CPPUNIT_TEST( ctors_none_000 );
229 CPPUNIT_TEST( ctors_copy );
230 CPPUNIT_TEST( ctors_copy_no_001 );
231 CPPUNIT_TEST( ctors_copy_no_002 );
232 CPPUNIT_TEST( ctors_copy_handle_001 );
233 CPPUNIT_TEST( ctors_copy_handle_002 );
234 CPPUNIT_TEST( ctors_hostname_port_001 );
235 CPPUNIT_TEST( ctors_hostname_port_002 );
236 CPPUNIT_TEST_SUITE_END();
238 }; // class ctors
241 /** testing the method:
242 inline sal_Bool is() const;
245 class is : public CppUnit::TestFixture
247 public:
248 void is_001()
250 ::osl::SocketAddr saSocketAddr;
252 CPPUNIT_ASSERT_MESSAGE("test for is() function: create an unknown type socket, it should be True when call is.",
253 sal_True == saSocketAddr.is( ) );
255 // refer to setPort_003()
256 void is_002()
258 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_INVAL );
260 CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid port number",
261 sal_True == saSocketAddr.is( ) );
264 void is_003()
266 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT );
268 CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid Ip number",
269 sal_True != saSocketAddr.is( ) );
272 CPPUNIT_TEST_SUITE( is );
273 CPPUNIT_TEST( is_001 );
274 CPPUNIT_TEST( is_002 );
275 CPPUNIT_TEST( is_003 );
276 CPPUNIT_TEST_SUITE_END();
278 }; // class is
281 /** testing the method:
282 inline ::rtl::OUString SAL_CALL getHostname( oslSocketResult *pResult = 0 ) const;
285 class getHostname : public CppUnit::TestFixture
287 public:
288 void setUp()
292 void tearDown()
296 void getHostname_000()
298 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.107"), IP_PORT_FTP );
299 rtl::OUString suResult = saSocketAddr.getHostname( 0 );
303 /** it will search the Ip in current machine's /etc/hosts at first, if find, then return the
304 mapped hostname, otherwise, it will search via DNS server, and often return hostname+ Domain name
305 like "sceri.PRC.Sun.COM"
306 The process is same as Socket::getLocalHost(), but getLocalHost can only return hostname of the current machine.
308 void getHostname_001()
310 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.107"), IP_PORT_FTP );
311 rtl::OUString suResult = saSocketAddr.getHostname( 0 );
312 rtl::OUString suError = outputError(suResult, rtl::OUString::createFromAscii("sceri.PRC.Sun.COM"), "test for getHostname(0)");
313 sal_Bool bOK = compareUString( suResult, rtl::OUString::createFromAscii("sceri.PRC.Sun.COM") );
314 // search the returned hostname in /etc/hosts, if find, and the IP in the row is same as IP
315 // in the Addr, it's right also.
316 if ( bOK == sal_False)
318 rtl::OString aString = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US );
319 if ( compareUString( getIPbyName( aString ), rtl::OUString::createFromAscii("129.158.217.107") ) == sal_True )
320 bOK = sal_True;
322 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK);
325 // LLA: now we have to control, if this behaviour is right.
326 // LLA: this function does not work in company (Linux, Windows) but at home
327 void getHostname_002()
329 rtl::OUString suHostname = rtl::OUString::createFromAscii("cn-1.germany.sun.com");
330 rtl::OString aString = ::rtl::OUStringToOString( suHostname, RTL_TEXTENCODING_ASCII_US );
331 rtl::OUString aHostIP = getIPbyName( aString );
333 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_FTP );
334 sal_Bool bOK = saSocketAddr.setHostname( suHostname );
335 CPPUNIT_ASSERT_MESSAGE("#SocketAddr.setHostname failed", sal_True == bOK );
336 oslSocketResult aResult;
337 rtl::OUString suResult = saSocketAddr.getHostname( &aResult );
338 CPPUNIT_ASSERT_MESSAGE("SocketAddr.getHostname failed.", aResult == osl_Socket_Ok);
340 rtl::OUString suError = outputError(suResult, suHostname, "test for getHostname(0)");
341 bOK = compareUString( suResult, suHostname );
342 if ( bOK == sal_False)
344 rtl::OString aStringResult = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US );
345 rtl::OString aStringHostname = ::rtl::OUStringToOString( suHostname, RTL_TEXTENCODING_ASCII_US );
346 if ( compareUString( getIPbyName( aStringResult ) , getIPbyName( aStringHostname ) ) == sal_True )
348 bOK = sal_True;
352 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
356 CPPUNIT_TEST_SUITE( getHostname );
357 CPPUNIT_TEST( getHostname_001 );
358 CPPUNIT_TEST( getHostname_002 );
359 CPPUNIT_TEST_SUITE_END();
361 }; // class getHostname
364 /** testing the method:
365 inline sal_Int32 SAL_CALL getPort() const;
368 class getPort : public CppUnit::TestFixture
370 public:
371 void getPort_001()
373 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
375 CPPUNIT_ASSERT_MESSAGE( "test for getPort() function: get a normal port number.",
376 IP_PORT_FTP == saSocketAddr.getPort( ) );
379 void getPort_002()
381 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_INVAL );
383 //t_print("#getPort_002: Port number is %d \n", saSocketAddr.getPort( ));
385 CPPUNIT_ASSERT_MESSAGE( "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).",
386 saSocketAddr.getPort( )>=1 && saSocketAddr.getPort( ) <= 65535 );
388 //two cases will return OSL_INVALID_PORT: 1. not valid SocketAddr
389 //2. SocketAddr family is not osl_Socket_FamilyInet, but case 2 could not be constructed
390 void getPort_003()
392 rtl::OUString suInvalidIP = rtl::OUString::createFromAscii("123.345.67.89");
393 ::osl::SocketAddr saSocketAddr( suInvalidIP, IP_PORT_MYPORT );
395 CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid IP to a SocketAddr, get the port to see returned value. ",
396 saSocketAddr.getPort( ) == OSL_INVALID_PORT );
399 CPPUNIT_TEST_SUITE( getPort );
400 CPPUNIT_TEST( getPort_001 );
401 CPPUNIT_TEST( getPort_002 );
402 CPPUNIT_TEST( getPort_003 );
403 CPPUNIT_TEST_SUITE_END( );
405 }; // class getPort
408 /** testing the method:
409 inline sal_Bool SAL_CALL setPort( sal_Int32 nPort );
410 rfc1413.txt: TCP port numbers are from 1-65535
411 rfc1700.txt: 0/tcp Reserved ; 0/udp Reserved
414 class setPort : public CppUnit::TestFixture
416 public:
417 void setPort_001()
419 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
420 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_TELNET );
422 CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: modify a port number setting, and check it.",
423 ( sal_True == bOK ) &&
424 ( IP_PORT_TELNET == saSocketAddr.getPort( ) ) );
427 /** 0 to 1024 is known as the reserved port range (traditionally only root can assign programs to ports in
428 this range) and the ephemeral port range from 1025 to 65535.
429 As many of you programmers will know, when you specify the source port of 0 when you connect to a host,
430 the OS automatically reassigns the port number to high numbered ephemeral port. The same happens if you
431 try to bind a listening socket to port 0.
432 http://www.securiteam.com/securityreviews/5XP0Q2AAKS.html
433 another: http://www.muq.org/~cynbe/muq/mufref_564.html
435 void setPort_002()
437 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
438 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_ZERO );
440 oslSocket sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
441 ::osl::Socket sSocket(sHandle);
442 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 );//sal_True);
443 sal_Bool bOK1 = sSocket.bind( saSocketAddr );
444 CPPUNIT_ASSERT_MESSAGE( "bind SocketAddr failed", bOK1 == sal_True );
446 sal_Int32 newPort = sSocket.getLocalPort();
447 //t_print("#new port is %d\n", newPort );
449 CPPUNIT_ASSERT_MESSAGE( "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.",
450 ( 1024 <= newPort ) && ( 65535 >= newPort ) && ( bOK == sal_True ) );
454 void setPort_003()
456 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP);
457 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_INVAL );
458 //on Linux, getPort return 34463
459 //t_print("#Port number is %d \n", saSocketAddr.getPort( ));
461 CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an address with invalid port. it should return error or convert it to a valid port.",
462 ( ( 1 <= saSocketAddr.getPort( ) ) && ( 65535 >= saSocketAddr.getPort( ) ) &&( bOK == sal_True ) ) ||
463 bOK == sal_False);
466 /* this is not a inet-addr => can't set port */
467 void setPort_004()
469 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_FTP);
470 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_MYPORT );
472 CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an invalid address with valid port. it should return error.",
473 bOK == sal_False);
477 CPPUNIT_TEST_SUITE( setPort );
478 CPPUNIT_TEST( setPort_001 );
479 CPPUNIT_TEST( setPort_002 );
480 CPPUNIT_TEST( setPort_003 );
481 CPPUNIT_TEST( setPort_004 );
482 CPPUNIT_TEST_SUITE_END( );
484 }; // class setPort
487 /** tester comment:
489 In the following two functions, it use ::rtl::ByteSequence as an intermediate storage for address,
490 the ByteSequence object can hold sal_Int8 arrays, which is raged [-127, 127], in case of IP addr
491 that is greater than 127, say 129.158.217.202, it will stored as -127, -98, -39, -54, it is unique
492 in the range of sal_Int8, but lack of readability.
493 so may be a sal_uInt8 array is better.
497 /** testing the method:
498 inline sal_Bool SAL_CALL setAddr( const ::rtl::ByteSequence & address );
501 class setAddr : public CppUnit::TestFixture
503 public:
504 void setAddr_001()
506 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
507 saSocketAddr.setAddr( UStringIPToByteSequence( rtl::OUString::createFromAscii("127.0.0.1") ) );
508 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 );
509 sal_Bool bOK = sal_False;
511 // if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) && ( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
512 // bOK = sal_True;
513 bOK = ifIpv4is( bsSocketAddr, 127, 0, 0, 1 );
515 CPPUNIT_ASSERT_MESSAGE( "test for setAddr() function: construct Addr with \"129.158.217.202\", set it to \"127.0.0.1\", and check the correctness ",
516 sal_True == bOK );
520 CPPUNIT_TEST_SUITE( setAddr );
521 CPPUNIT_TEST( setAddr_001 );
522 CPPUNIT_TEST_SUITE_END( );
524 }; // class setAddr
527 /** testing the method:
528 inline ::rtl::ByteSequence SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const;
531 class getAddr : public CppUnit::TestFixture
533 public:
534 void getAddr_001()
536 oslSocketResult SocketResult;
537 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
538 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( &SocketResult );
540 sal_Bool bOK = sal_False;
542 //if ( ( osl_Socket_Ok == SocketResult ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
543 // bOK = sal_True;
544 bOK = ifIpv4is( bsSocketAddr, 127, 0, 0, 1 );
546 CPPUNIT_ASSERT_MESSAGE( "test for getAddr() function: construct a socketaddr with IP assigned, get the address to check correctness.Caught unknown exception on (Win32)",
547 sal_True == bOK && SocketResult == osl_Socket_Ok);
550 CPPUNIT_TEST_SUITE( getAddr );
551 CPPUNIT_TEST( getAddr_001 );
552 CPPUNIT_TEST_SUITE_END( );
554 }; // class getAddr
557 /** testing the methods:
558 inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr);
559 inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr);
560 inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy );
561 inline sal_Bool SAL_CALL operator== (oslSocketAddr Addr) const;
562 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; /// not implemented.
565 class operator_equal : public CppUnit::TestFixture
567 public:
568 void operator_equal_001()
570 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET);
571 ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
573 saSocketAddrEqual = saSocketAddr;
574 sal_Bool bOK = sal_False;
575 ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 );
577 // if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
578 if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) && ( ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ) == sal_True ) )
579 bOK = sal_True;
581 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: use operator= to assign Ip1 to Ip2, check its modification.",
582 sal_True == bOK );
586 void operator_equal_002()
588 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.199"), IP_PORT_TELNET);
589 ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
591 saSocketAddrEqual = saSocketAddr;
592 CPPUNIT_ASSERT_MESSAGE( "after assign, the assigned SocketAddr is not same as the original Addr",
593 IP_PORT_TELNET == saSocketAddrEqual.getPort( ) );
594 saSocketAddrEqual.setPort( IP_PORT_MYPORT3 );
595 saSocketAddr.setPort( IP_PORT_HTTP2 );
597 CPPUNIT_ASSERT_MESSAGE( "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.",
598 IP_PORT_MYPORT3 == saSocketAddrEqual.getPort( ) );
601 void operator_equal_const_001()
603 const ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET);
604 ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
606 saSocketAddrEqual = saSocketAddr;
607 sal_Bool bOK = sal_False;
608 ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 );
610 // if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
611 if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) && ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ) == sal_True )
612 bOK = sal_True;
614 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: use operator= const to assign Ip1 to Ip2, verify the change on the second one.",
615 sal_True == bOK );
618 void operator_equal_const_002()
620 const ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET);
621 ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
623 saSocketAddrEqual = saSocketAddr;
624 saSocketAddrEqual.setPort( IP_PORT_HTTP1 );
626 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: change the second instance, the first one should not be altered, since it does not released the handle.",
627 IP_PORT_HTTP1 != saSocketAddr.getPort( ) );
630 void operator_equal_assign_001()
632 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET );
633 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
634 ::osl::SocketAddr* pSocketAddrAssign = new ::osl::SocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
635 oslSocketAddr poslSocketAddr = pSocketAddr->getHandle( );
636 //if( m_handle ) osl_destroySocketAddr( m_handle ); so pSocketAddrAssign had been destroyed and then point to pSocketAddr
637 pSocketAddrAssign->assign(poslSocketAddr, SAL_NO_COPY);
639 CPPUNIT_ASSERT_MESSAGE("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.",
640 pSocketAddrAssign->getPort( ) == IP_PORT_TELNET );
642 delete pSocketAddrAssign;
645 void operator_is_equal_001()
647 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET);
648 ::osl::SocketAddr saSocketAddrequal( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET );
650 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two identical Address.",
651 sal_True == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) );
654 void operator_is_equal_002()
656 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP);
657 ::osl::SocketAddr saSocketAddrequal( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET );
659 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two different Address.",
660 sal_False == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) );
663 CPPUNIT_TEST_SUITE( operator_equal );
664 CPPUNIT_TEST( operator_equal_001 );
665 CPPUNIT_TEST( operator_equal_002 );
666 CPPUNIT_TEST( operator_equal_const_001 );
667 CPPUNIT_TEST( operator_equal_const_002 );
668 CPPUNIT_TEST( operator_equal_assign_001 );
669 CPPUNIT_TEST( operator_is_equal_001 );
670 CPPUNIT_TEST( operator_is_equal_002 );
671 CPPUNIT_TEST_SUITE_END( );
673 }; // class operator_equal
677 /** testing the method:
678 inline oslSocketAddr SAL_CALL getHandle() const;
681 class getSocketAddrHandle : public CppUnit::TestFixture
683 public:
685 void getSocketAddrHandle_001()
687 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
688 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL);
689 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
690 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
692 CPPUNIT_ASSERT_MESSAGE("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.",
693 pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) );
695 delete pSocketAddrCopy;
698 void getSocketAddrHandle_002()
700 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("deuce.PRC.Sun.COM"), IP_PORT_MYPORT4 );
701 oslSocketAddr poslSocketAddr = saSocketAddr.getHandle( );
703 sal_Bool bOK = ( saSocketAddr == poslSocketAddr );
704 //t_print("getSocketAddrHandle_002\n");
705 CPPUNIT_ASSERT_MESSAGE( "test for getHandle() function: use getHandle() function as an intermediate way to create identical address.",
706 sal_True == bOK );
709 CPPUNIT_TEST_SUITE( getSocketAddrHandle );
710 CPPUNIT_TEST( getSocketAddrHandle_001 );
711 CPPUNIT_TEST( getSocketAddrHandle_002 );
712 CPPUNIT_TEST_SUITE_END( );
714 }; // class getSocketAddrHandle
717 /** testing the method:
718 static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0);
721 class getLocalHostname : public CppUnit::TestFixture
723 public:
724 /* the process of getLocalHostname: 1.gethostname (same as /bin/hostname) returned name A
725 2. search A in /etc/hosts, if there is an alias name is A, return the name in the same row
728 void getLocalHostname_000()
730 // _osl_getFullQualifiedDomainName( );
731 oslSocketResult aResult = osl_Socket_Error;
732 rtl::OUString suHostname = osl::SocketAddr::getLocalHostname(&aResult);
733 CPPUNIT_ASSERT_MESSAGE("getLocalHostname failed", aResult == osl_Socket_Ok);
736 void getLocalHostname_001()
738 oslSocketResult *pResult = NULL;
739 //printSocketResult(*pResult);
740 ::rtl::OUString suResult = ::osl::SocketAddr::getLocalHostname( pResult );
742 // LLA: IMHO localhost, or hostname by itself should be ok.
743 rtl::OUString suThisHost = getThisHostname( );
744 bool bOk = false;
745 if (suThisHost.equals(rtl::OUString::createFromAscii("localhost")))
747 bOk = true;
749 else
751 if (suThisHost.equals(suResult))
753 bOk = true;
757 ::rtl::OUString suError;
758 suError = outputError(suResult, getThisHostname( ), "test for getLocalHostname() function");
760 CPPUNIT_ASSERT_MESSAGE( suError, bOk == true );
763 CPPUNIT_TEST_SUITE( getLocalHostname );
764 CPPUNIT_TEST( getLocalHostname_000 );
765 CPPUNIT_TEST( getLocalHostname_001 );
766 CPPUNIT_TEST_SUITE_END( );
768 }; // class getLocalHostname
771 /** testing the method:
772 static inline void SAL_CALL resolveHostname( const ::rtl::OUString & strHostName , SocketAddr & Addr );
775 class resolveHostname : public CppUnit::TestFixture
777 public:
778 void resolveHostname_001()
780 ::osl::SocketAddr saSocketAddr;
781 ::osl::SocketAddr::resolveHostname( rtl::OUString::createFromAscii("127.0.0.1"), saSocketAddr );
782 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 );
783 sal_Bool bOK = sal_False;
785 if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
786 bOK = sal_True;
788 CPPUNIT_ASSERT_MESSAGE( "test for resolveHostname() function: try to resolve localhost to 127.0.0.1.",
789 sal_True == bOK );
792 CPPUNIT_TEST_SUITE( resolveHostname );
793 CPPUNIT_TEST( resolveHostname_001 );
794 CPPUNIT_TEST_SUITE_END( );
796 }; // class resolveHostname
799 /** testing the method:
800 static inline sal_Int32 SAL_CALL getServicePort(
801 const ::rtl::OUString& strServiceName,
802 const ::rtl::OUString & strProtocolName= ::rtl::OUString::createFromAscii( "tcp" ) );
805 class gettheServicePort : public CppUnit::TestFixture
807 public:
808 void gettheServicePort_001()
810 rtl::OUString suServiceFTP = rtl::OUString::createFromAscii( "ftp" );
811 rtl::OUString suProtocolTCP = rtl::OUString::createFromAscii( "tcp" );
813 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get ftp service port on TCP protocol.",
814 IP_PORT_FTP== ::osl::SocketAddr::getServicePort( suServiceFTP, suProtocolTCP ) );
817 void gettheServicePort_002()
819 rtl::OUString suServiceTELNET = rtl::OUString::createFromAscii( "telnet" );
820 rtl::OUString suProtocolTCP = rtl::OUString::createFromAscii( "tcp" );
821 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get telnet service port on TCP protocol.",
822 IP_PORT_TELNET== ::osl::SocketAddr::getServicePort( suServiceTELNET, suProtocolTCP ) );
825 void gettheServicePort_003()
827 //Solaris has no service called "https", please see /etc/services
828 rtl::OUString suServiceNETBIOS = rtl::OUString::createFromAscii( "netbios-dgm" );
829 rtl::OUString suProtocolUDP = rtl::OUString::createFromAscii( "udp" );
830 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get netbios-ssn service port on UDP protocol.",
831 IP_PORT_NETBIOS_DGM == ::osl::SocketAddr::getServicePort( suServiceNETBIOS, suProtocolUDP ) );
834 void gettheServicePort_004()
836 rtl::OUString suProtocolUDP = rtl::OUString::createFromAscii( "udp" );
837 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get a service port which is not exist.",
838 OSL_INVALID_PORT == ::osl::SocketAddr::getServicePort( ::rtl::OUString::createFromAscii( "notexist" ), suProtocolUDP ) );
841 CPPUNIT_TEST_SUITE( gettheServicePort );
842 CPPUNIT_TEST( gettheServicePort_001 );
843 CPPUNIT_TEST( gettheServicePort_002 );
844 CPPUNIT_TEST( gettheServicePort_003 );
845 CPPUNIT_TEST( gettheServicePort_004 );
846 CPPUNIT_TEST_SUITE_END( );
848 }; // class gettheServicePort
850 /** testing the method:
854 class getFamilyOfSocketAddr : public CppUnit::TestFixture
856 public:
857 void getFamilyOfSocketAddr_001()
859 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 );
860 oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( );
861 CPPUNIT_ASSERT_EQUAL(
862 osl_Socket_FamilyInet,
863 osl_getFamilyOfSocketAddr( psaOSLSocketAddr ) );
865 CPPUNIT_ASSERT_MESSAGE( "test for osl_getFamilyOfSocketAddr.",
866 osl_getFamilyOfSocketAddr( psaOSLSocketAddr ) == osl_Socket_FamilyInet );
869 CPPUNIT_TEST_SUITE( getFamilyOfSocketAddr );
870 CPPUNIT_TEST( getFamilyOfSocketAddr_001 );
871 CPPUNIT_TEST_SUITE_END( );
873 }; // class getFamilyOfSocketAddr
875 // -----------------------------------------------------------------------------
878 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::ctors, "osl_SocketAddr");
879 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::is, "osl_SocketAddr");
880 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getHostname, "osl_SocketAddr");
881 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getPort, "osl_SocketAddr");
882 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setPort, "osl_SocketAddr");
883 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setAddr, "osl_SocketAddr");
884 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getAddr, "osl_SocketAddr");
885 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::operator_equal, "osl_SocketAddr");
886 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getSocketAddrHandle, "osl_SocketAddr");
887 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getLocalHostname, "osl_SocketAddr");
888 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::resolveHostname, "osl_SocketAddr");
889 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::gettheServicePort, "osl_SocketAddr");
890 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getFamilyOfSocketAddr, "osl_SocketAddr");
892 } // namespace osl_SocketAddr
894 // -----------------------------------------------------------------------------
896 // this macro creates an empty function, which will called by the RegisterAllFunctions()
897 // to let the user the possibility to also register some functions by hand.
898 NOADDITIONAL;