1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 /** test coder preface:
21 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
22 if you are not including ws2_32.lib in makefile.mk, the including format will be like this:
25 SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
26 SHL1STDLIBS += ws2_32.lib
29 likewise on Solaris platform.
31 SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
32 SHL1STDLIBS += -lsocket -ldl -lnsl
35 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
38 3. some fragment of Socket source implementation are lack of comment so it is hard for testers
39 guess what the exact functionality or usage of a member. Hope the Socket section's comment
42 4. following functions are declared but not implemented:
43 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
46 #include <sal/types.h>
47 #include <cppunit/TestFixture.h>
48 #include <cppunit/extensions/HelperMacros.h>
49 #include <cppunit/plugin/TestPlugIn.h>
51 #include "sockethelper.hxx"
55 using ::rtl::OUString
;
56 using ::rtl::OUStringToOString
;
59 #define IP_PORT_ZERO 0
60 #define IP_PORT_FTP 21
61 #define IP_PORT_TELNET 23
62 #define IP_PORT_HTTP1 80
63 #define IP_PORT_HTTP2 8080
65 #define IP_PORT_MYPORT 8881 //8888
66 #define IP_PORT_MYPORT2 8883 //8890
67 #define IP_PORT_MYPORT3 8884 //8891
68 #define IP_PORT_INVAL 99999
69 #define IP_PORT_MYPORT4 8885 //8892
70 #define IP_PORT_NETBIOS_DGM 138
72 namespace osl_SocketAddr
75 /** testing the methods:
77 inline SocketAddr(const SocketAddr& Addr);
78 inline SocketAddr(const oslSocketAddr , __osl_socket_NoCopy nocopy );
79 inline SocketAddr(oslSocketAddr Addr);
80 inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort );
83 class ctors
: public CppUnit::TestFixture
89 /// SocketAddr constructor.
90 ::osl::SocketAddr saSocketAddr
;
92 // oslSocketResult aResult;
93 // rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult);
95 // rtl::OUString suHost2 = getThisHostname();
97 CPPUNIT_ASSERT_MESSAGE("test for none parameter constructor function: check if the socket address was created successfully",
98 sal_True
== saSocketAddr
.is( ) );
101 void ctors_none_000()
103 /// SocketAddr constructor.
104 ::osl::SocketAddr saSocketAddr
;
106 oslSocketResult aResult
;
107 rtl::OUString suHost
= saSocketAddr
.getLocalHostname( &aResult
);
108 rtl::OUString suHost2
= getThisHostname();
110 sal_Bool bOk
= compareUString(suHost
, suHost2
);
112 rtl::OUString
suError ("Host names should be the same. From SocketAddr.getLocalHostname() it is'");
114 suError
+= rtl::OUString("', from getThisHostname() it is '");
116 suError
+= rtl::OUString("'.");
118 CPPUNIT_ASSERT_MESSAGE(STD_STRING(suError
), sal_True
== bOk
);
123 /// SocketAddr copy constructor.
124 ::osl::SocketAddr
saSocketAddr( rtl::OUString("localhost"), IP_PORT_HTTP1
);
125 ::osl::SocketAddr
saCopySocketAddr( saSocketAddr
);
127 sal_Int32 nPort
= saCopySocketAddr
.getPort( );
129 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy constructor function: copy constructor, do an action of copy construction then check the port with original set.",
130 ( sal_True
== saCopySocketAddr
.is( ) ) && ( nPort
== IP_PORT_HTTP1
) );
133 void ctors_copy_no_001()
135 ::osl::SocketAddr
* pSocketAddr
= new ::osl::SocketAddr( rtl::OUString("localhost"), IP_PORT_HTTP1
);
136 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr
!= NULL
);
138 oslSocketAddr psaOSLSocketAddr
= pSocketAddr
->getHandle( );
140 ::osl::SocketAddr
* pSocketAddrCopy
= new ::osl::SocketAddr( psaOSLSocketAddr
, SAL_NO_COPY
);
142 pSocketAddrCopy
->setPort( IP_PORT_HTTP2
);
143 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.",
144 pSocketAddr
->getPort( ) == IP_PORT_HTTP2
);
146 delete pSocketAddrCopy
;
147 // LLA: don't do this also: delete pSocketAddr;
150 void ctors_copy_no_002()
152 ::osl::SocketAddr
* pSocketAddr
= new ::osl::SocketAddr( rtl::OUString("localhost"), IP_PORT_HTTP1
);
153 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr
!= NULL
);
154 oslSocketAddr psaOSLSocketAddr
= pSocketAddr
->getHandle( );
155 ::osl::SocketAddr
* pSocketAddrCopy
= new ::osl::SocketAddr( psaOSLSocketAddr
, SAL_NO_COPY
);
157 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.",
158 pSocketAddr
->getHandle( ) == pSocketAddrCopy
->getHandle( ) );
160 delete pSocketAddrCopy
;
163 void ctors_copy_handle_001()
165 ::osl::SocketAddr
saSocketAddr( rtl::OUString("localhost"), IP_PORT_HTTP1
);
166 ::osl::SocketAddr
saSocketAddrCopy( saSocketAddr
.getHandle( ) );
168 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, get its port to check copy effect.",
169 saSocketAddrCopy
.getPort( ) == IP_PORT_HTTP1
);
172 void ctors_copy_handle_002()
174 ::osl::SocketAddr
saSocketAddr( rtl::OUString("localhost"), IP_PORT_HTTP1
);
175 ::osl::SocketAddr
saSocketAddrCopy( saSocketAddr
.getHandle( ) );
176 saSocketAddrCopy
.setPort( IP_PORT_HTTP2
);
178 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, the original one should not be changed.",
179 saSocketAddr
.getPort( ) != IP_PORT_HTTP2
);
182 void ctors_hostname_port_001()
184 /// tcpip-specific constructor.
185 ::osl::SocketAddr
saSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_FTP
);
186 printUString( saSocketAddr
.getHostname( ), "ctors_hostname_port_001:getHostname");
188 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specificy constructor function: do a constructor using tcpip spec, check the result.",
189 saSocketAddr
.is( ) == sal_True
&&
190 ( saSocketAddr
.getPort( ) == IP_PORT_FTP
)
195 void ctors_hostname_port_002()
197 /// tcpip-specific constructor.
198 ::osl::SocketAddr
saSocketAddr( rtl::OUString("123.345.67.89"), IP_PORT_MYPORT2
);
200 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specificy constructor function: using an invalid IP address, the socketaddr ctors should fail", sal_False
== saSocketAddr
.is( ));
202 CPPUNIT_TEST_SUITE( ctors
);
203 CPPUNIT_TEST( ctors_none
);
204 CPPUNIT_TEST( ctors_none_000
);
205 CPPUNIT_TEST( ctors_copy
);
206 CPPUNIT_TEST( ctors_copy_no_001
);
207 CPPUNIT_TEST( ctors_copy_no_002
);
208 CPPUNIT_TEST( ctors_copy_handle_001
);
209 CPPUNIT_TEST( ctors_copy_handle_002
);
210 CPPUNIT_TEST( ctors_hostname_port_001
);
211 CPPUNIT_TEST( ctors_hostname_port_002
);
212 CPPUNIT_TEST_SUITE_END();
216 /** testing the method:
217 inline sal_Bool is() const;
220 class is
: public CppUnit::TestFixture
225 ::osl::SocketAddr saSocketAddr
;
227 CPPUNIT_ASSERT_MESSAGE("test for is() function: create an unknown type socket, it should be True when call is.",
228 sal_True
== saSocketAddr
.is( ) );
230 // refer to setPort_003()
233 ::osl::SocketAddr
saSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_INVAL
);
235 CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid port number",
236 sal_True
== saSocketAddr
.is( ) );
241 ::osl::SocketAddr
saSocketAddr( rtl::OUString("123.345.67.89"), IP_PORT_MYPORT
);
243 CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid Ip number",
244 sal_True
!= saSocketAddr
.is( ) );
247 CPPUNIT_TEST_SUITE( is
);
248 CPPUNIT_TEST( is_001
);
249 CPPUNIT_TEST( is_002
);
250 CPPUNIT_TEST( is_003
);
251 CPPUNIT_TEST_SUITE_END();
255 /** testing the method:
256 inline ::rtl::OUString SAL_CALL getHostname( oslSocketResult *pResult = 0 ) const;
259 class getHostname
: public CppUnit::TestFixture
270 void getHostname_000()
272 ::osl::SocketAddr
saSocketAddr( rtl::OUString("129.158.217.107"), IP_PORT_FTP
);
273 rtl::OUString suResult
= saSocketAddr
.getHostname( 0 );
277 /** it will search the Ip in current machine's /etc/hosts at first, if find, then return the
278 mapped hostname, otherwise, it will search via DNS server, and often return hostname+ Domain name
279 like "sceri.PRC.Sun.COM"
280 The process is same as Socket::getLocalHost(), but getLocalHost can only return hostname of the current machine.
282 void getHostname_001()
284 ::osl::SocketAddr
saSocketAddr( rtl::OUString("129.158.217.107"), IP_PORT_FTP
);
285 rtl::OUString suResult
= saSocketAddr
.getHostname( 0 );
286 rtl::OUString suError
= outputError(suResult
, rtl::OUString("sceri.PRC.Sun.COM"), "test for getHostname(0)");
287 sal_Bool bOK
= compareUString( suResult
, rtl::OUString("sceri.PRC.Sun.COM") );
288 // search the returned hostname in /etc/hosts, if find, and the IP in the row is same as IP
289 // in the Addr, it's right also.
290 if ( bOK
== sal_False
)
292 rtl::OString aString
= ::rtl::OUStringToOString( suResult
, RTL_TEXTENCODING_ASCII_US
);
293 if ( compareUString( getIPbyName( aString
), rtl::OUString("129.158.217.107") ) == sal_True
)
296 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError
), sal_True
== bOK
);
299 // LLA: now we have to control, if this behaviour is right.
300 // LLA: this function does not work in company (Linux, Windows) but at home
301 void getHostname_002()
303 rtl::OUString
suHostname ("cn-1.germany.sun.com");
304 rtl::OString aString
= ::rtl::OUStringToOString( suHostname
, RTL_TEXTENCODING_ASCII_US
);
305 rtl::OUString aHostIP
= getIPbyName( aString
);
307 ::osl::SocketAddr
saSocketAddr( rtl::OUString("localhost"), IP_PORT_FTP
);
308 sal_Bool bOK
= saSocketAddr
.setHostname( suHostname
);
309 CPPUNIT_ASSERT_MESSAGE("#SocketAddr.setHostname failed", sal_True
== bOK
);
310 oslSocketResult aResult
;
311 rtl::OUString suResult
= saSocketAddr
.getHostname( &aResult
);
312 CPPUNIT_ASSERT_MESSAGE("SocketAddr.getHostname failed.", aResult
== osl_Socket_Ok
);
314 rtl::OUString suError
= outputError(suResult
, suHostname
, "test for getHostname(0)");
315 bOK
= compareUString( suResult
, suHostname
);
316 if ( bOK
== sal_False
)
318 rtl::OString aStringResult
= ::rtl::OUStringToOString( suResult
, RTL_TEXTENCODING_ASCII_US
);
319 rtl::OString aStringHostname
= ::rtl::OUStringToOString( suHostname
, RTL_TEXTENCODING_ASCII_US
);
320 if ( compareUString( getIPbyName( aStringResult
) , getIPbyName( aStringHostname
) ) == sal_True
)
326 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError
), sal_True
== bOK
);
329 CPPUNIT_TEST_SUITE( getHostname
);
330 CPPUNIT_TEST( getHostname_001
);
331 CPPUNIT_TEST( getHostname_002
);
332 CPPUNIT_TEST_SUITE_END();
334 }; // class getHostname
336 /** testing the method:
337 inline sal_Int32 SAL_CALL getPort() const;
340 class getPort
: public CppUnit::TestFixture
345 ::osl::SocketAddr
saSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_FTP
);
347 CPPUNIT_ASSERT_MESSAGE( "test for getPort() function: get a normal port number.",
348 IP_PORT_FTP
== saSocketAddr
.getPort( ) );
353 ::osl::SocketAddr
saSocketAddr( rtl::OUString("129.158.217.202"), IP_PORT_INVAL
);
355 //t_print("#getPort_002: Port number is %d \n", saSocketAddr.getPort( ));
357 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).",
358 saSocketAddr
.getPort( )>=1 && saSocketAddr
.getPort( ) <= 65535 );
360 //two cases will return OSL_INVALID_PORT: 1. not valid SocketAddr
361 //2. SocketAddr family is not osl_Socket_FamilyInet, but case 2 could not be constructed
364 rtl::OUString
suInvalidIP ("123.345.67.89");
365 ::osl::SocketAddr
saSocketAddr( suInvalidIP
, IP_PORT_MYPORT
);
367 CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid IP to a SocketAddr, get the port to see returned value. ",
368 saSocketAddr
.getPort( ) == OSL_INVALID_PORT
);
371 CPPUNIT_TEST_SUITE( getPort
);
372 CPPUNIT_TEST( getPort_001
);
373 CPPUNIT_TEST( getPort_002
);
374 CPPUNIT_TEST( getPort_003
);
375 CPPUNIT_TEST_SUITE_END( );
379 /** testing the method:
380 inline sal_Bool SAL_CALL setPort( sal_Int32 nPort );
381 rfc1413.txt: TCP port numbers are from 1-65535
382 rfc1700.txt: 0/tcp Reserved ; 0/udp Reserved
385 class setPort
: public CppUnit::TestFixture
390 ::osl::SocketAddr
saSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_FTP
);
391 sal_Bool bOK
= saSocketAddr
.setPort( IP_PORT_TELNET
);
393 CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: modify a port number setting, and check it.",
394 ( sal_True
== bOK
) &&
395 ( IP_PORT_TELNET
== saSocketAddr
.getPort( ) ) );
398 /** 0 to 1024 is known as the reserved port range (traditionally only root can assign programs to ports in
399 this range) and the ephemeral port range from 1025 to 65535.
400 As many of you programmers will know, when you specify the source port of 0 when you connect to a host,
401 the OS automatically reassigns the port number to high numbered ephemeral port. The same happens if you
402 try to bind a listening socket to port 0.
403 http://www.securiteam.com/securityreviews/5XP0Q2AAKS.html
404 another: http://www.muq.org/~cynbe/muq/mufref_564.html
408 ::osl::SocketAddr
saSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_FTP
);
409 sal_Bool bOK
= saSocketAddr
.setPort( IP_PORT_ZERO
);
411 oslSocket sHandle
= osl_createSocket( osl_Socket_FamilyInet
, osl_Socket_TypeStream
, osl_Socket_ProtocolIp
);
412 ::osl::Socket
sSocket(sHandle
);
413 sSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 );//sal_True);
414 sal_Bool bOK1
= sSocket
.bind( saSocketAddr
);
415 CPPUNIT_ASSERT_MESSAGE( "bind SocketAddr failed", bOK1
== sal_True
);
417 sal_Int32 newPort
= sSocket
.getLocalPort();
418 //t_print("#new port is %d\n", newPort );
420 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.",
421 ( 1024 <= newPort
) && ( 65535 >= newPort
) && ( bOK
== sal_True
) );
427 ::osl::SocketAddr
saSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_FTP
);
428 sal_Bool bOK
= saSocketAddr
.setPort( IP_PORT_INVAL
);
429 //on Linux, getPort return 34463
430 //t_print("#Port number is %d \n", saSocketAddr.getPort( ));
432 CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an address with invalid port. it should return error or convert it to a valid port.",
433 ( ( 1 <= saSocketAddr
.getPort( ) ) && ( 65535 >= saSocketAddr
.getPort( ) ) &&( bOK
== sal_True
) ) ||
437 /* this is not a inet-addr => can't set port */
440 ::osl::SocketAddr
saSocketAddr( rtl::OUString("123.345.67.89"), IP_PORT_FTP
);
441 sal_Bool bOK
= saSocketAddr
.setPort( IP_PORT_MYPORT
);
443 CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an invalid address with valid port. it should return error.",
447 CPPUNIT_TEST_SUITE( setPort
);
448 CPPUNIT_TEST( setPort_001
);
449 CPPUNIT_TEST( setPort_002
);
450 CPPUNIT_TEST( setPort_003
);
451 CPPUNIT_TEST( setPort_004
);
452 CPPUNIT_TEST_SUITE_END( );
458 In the following two functions, it use ::rtl::ByteSequence as an intermediate storage for address,
459 the ByteSequence object can hold sal_Int8 arrays, which is raged [-127, 127], in case of IP addr
460 that is greater than 127, say 129.158.217.202, it will stored as -127, -98, -39, -54, it is unique
461 in the range of sal_Int8, but lack of readability.
462 so may be a sal_uInt8 array is better.
465 /** testing the method:
466 inline sal_Bool SAL_CALL setAddr( const ::rtl::ByteSequence & address );
469 class setAddr
: public CppUnit::TestFixture
474 ::osl::SocketAddr
saSocketAddr( rtl::OUString("129.158.217.202"), IP_PORT_FTP
);
475 saSocketAddr
.setAddr( UStringIPToByteSequence( rtl::OUString("127.0.0.1") ) );
476 ::rtl::ByteSequence bsSocketAddr
= saSocketAddr
.getAddr( 0 );
477 sal_Bool bOK
= sal_False
;
479 // if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) && ( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
481 bOK
= ifIpv4is( bsSocketAddr
, 127, 0, 0, 1 );
483 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 ",
487 CPPUNIT_TEST_SUITE( setAddr
);
488 CPPUNIT_TEST( setAddr_001
);
489 CPPUNIT_TEST_SUITE_END( );
493 /** testing the method:
494 inline ::rtl::ByteSequence SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const;
497 class getAddr
: public CppUnit::TestFixture
502 oslSocketResult SocketResult
;
503 ::osl::SocketAddr
saSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_FTP
);
504 ::rtl::ByteSequence bsSocketAddr
= saSocketAddr
.getAddr( &SocketResult
);
506 sal_Bool bOK
= sal_False
;
508 //if ( ( osl_Socket_Ok == SocketResult ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
510 bOK
= ifIpv4is( bsSocketAddr
, 127, 0, 0, 1 );
512 CPPUNIT_ASSERT_MESSAGE( "test for getAddr() function: construct a socketaddr with IP assigned, get the address to check correctness.Caught unknown exception on (Win32)",
513 sal_True
== bOK
&& SocketResult
== osl_Socket_Ok
);
516 CPPUNIT_TEST_SUITE( getAddr
);
517 CPPUNIT_TEST( getAddr_001
);
518 CPPUNIT_TEST_SUITE_END( );
522 /** testing the methods:
523 inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr);
524 inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr);
525 inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy );
526 inline sal_Bool SAL_CALL operator== (oslSocketAddr Addr) const;
527 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; /// not implemented.
530 class operator_equal
: public CppUnit::TestFixture
533 void operator_equal_001()
535 ::osl::SocketAddr
saSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_TELNET
);
536 ::osl::SocketAddr
saSocketAddrEqual( rtl::OUString("129.158.217.202"), IP_PORT_FTP
);
538 saSocketAddrEqual
= saSocketAddr
;
539 sal_Bool bOK
= sal_False
;
540 ::rtl::ByteSequence bsSocketAddr
= saSocketAddrEqual
.getAddr( 0 );
542 // if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
543 if ( ( IP_PORT_TELNET
== saSocketAddrEqual
.getPort( ) ) && ( ifIpv4is( bsSocketAddr
, 127, 0, 0, 1 ) == sal_True
) )
546 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: use operator= to assign Ip1 to Ip2, check its modification.",
550 void operator_equal_002()
552 ::osl::SocketAddr
saSocketAddr( rtl::OUString("129.158.217.199"), IP_PORT_TELNET
);
553 ::osl::SocketAddr
saSocketAddrEqual( rtl::OUString("129.158.217.202"), IP_PORT_FTP
);
555 saSocketAddrEqual
= saSocketAddr
;
556 CPPUNIT_ASSERT_MESSAGE( "after assign, the assigned SocketAddr is not same as the original Addr",
557 IP_PORT_TELNET
== saSocketAddrEqual
.getPort( ) );
558 saSocketAddrEqual
.setPort( IP_PORT_MYPORT3
);
559 saSocketAddr
.setPort( IP_PORT_HTTP2
);
561 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.",
562 IP_PORT_MYPORT3
== saSocketAddrEqual
.getPort( ) );
565 void operator_equal_const_001()
567 const ::osl::SocketAddr
saSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_TELNET
);
568 ::osl::SocketAddr
saSocketAddrEqual( rtl::OUString("129.158.217.202"), IP_PORT_FTP
);
570 saSocketAddrEqual
= saSocketAddr
;
571 sal_Bool bOK
= sal_False
;
572 ::rtl::ByteSequence bsSocketAddr
= saSocketAddrEqual
.getAddr( 0 );
574 // if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
575 if ( ( IP_PORT_TELNET
== saSocketAddrEqual
.getPort( ) ) && ifIpv4is( bsSocketAddr
, 127, 0, 0, 1 ) == sal_True
)
578 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: use operator= const to assign Ip1 to Ip2, verify the change on the second one.",
582 void operator_equal_const_002()
584 const ::osl::SocketAddr
saSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_TELNET
);
585 ::osl::SocketAddr
saSocketAddrEqual( rtl::OUString("129.158.217.202"), IP_PORT_FTP
);
587 saSocketAddrEqual
= saSocketAddr
;
588 saSocketAddrEqual
.setPort( IP_PORT_HTTP1
);
590 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.",
591 IP_PORT_HTTP1
!= saSocketAddr
.getPort( ) );
594 void operator_equal_assign_001()
596 ::osl::SocketAddr
* pSocketAddr
= new ::osl::SocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_TELNET
);
597 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr
!= NULL
);
598 ::osl::SocketAddr
* pSocketAddrAssign
= new ::osl::SocketAddr( rtl::OUString("129.158.217.202"), IP_PORT_FTP
);
599 oslSocketAddr poslSocketAddr
= pSocketAddr
->getHandle( );
600 //if( m_handle ) osl_destroySocketAddr( m_handle ); so pSocketAddrAssign had been destroyed and then point to pSocketAddr
601 pSocketAddrAssign
->assign(poslSocketAddr
, SAL_NO_COPY
);
603 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.",
604 pSocketAddrAssign
->getPort( ) == IP_PORT_TELNET
);
606 delete pSocketAddrAssign
;
609 void operator_is_equal_001()
611 ::osl::SocketAddr
saSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_TELNET
);
612 ::osl::SocketAddr
saSocketAddrequal( rtl::OUString("127.0.0.1"), IP_PORT_TELNET
);
614 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two identical Address.",
615 sal_True
== ( saSocketAddrequal
== saSocketAddr
.getHandle( ) ) );
618 void operator_is_equal_002()
620 ::osl::SocketAddr
saSocketAddr( rtl::OUString("129.158.217.202"), IP_PORT_FTP
);
621 ::osl::SocketAddr
saSocketAddrequal( rtl::OUString("127.0.0.1"), IP_PORT_TELNET
);
623 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two different Address.",
624 sal_False
== ( saSocketAddrequal
== saSocketAddr
.getHandle( ) ) );
627 CPPUNIT_TEST_SUITE( operator_equal
);
628 CPPUNIT_TEST( operator_equal_001
);
629 CPPUNIT_TEST( operator_equal_002
);
630 CPPUNIT_TEST( operator_equal_const_001
);
631 CPPUNIT_TEST( operator_equal_const_002
);
632 CPPUNIT_TEST( operator_equal_assign_001
);
633 CPPUNIT_TEST( operator_is_equal_001
);
634 CPPUNIT_TEST( operator_is_equal_002
);
635 CPPUNIT_TEST_SUITE_END( );
637 }; // class operator_equal
639 /** testing the method:
640 inline oslSocketAddr SAL_CALL getHandle() const;
643 class getSocketAddrHandle
: public CppUnit::TestFixture
647 void getSocketAddrHandle_001()
649 ::osl::SocketAddr
* pSocketAddr
= new ::osl::SocketAddr( rtl::OUString("localhost"), IP_PORT_HTTP1
);
650 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr
!= NULL
);
651 oslSocketAddr psaOSLSocketAddr
= pSocketAddr
->getHandle( );
652 ::osl::SocketAddr
* pSocketAddrCopy
= new ::osl::SocketAddr( psaOSLSocketAddr
, SAL_NO_COPY
);
654 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.",
655 pSocketAddr
->getHandle( ) == pSocketAddrCopy
->getHandle( ) );
657 delete pSocketAddrCopy
;
660 void getSocketAddrHandle_002()
662 ::osl::SocketAddr
saSocketAddr( rtl::OUString("deuce.PRC.Sun.COM"), IP_PORT_MYPORT4
);
663 oslSocketAddr poslSocketAddr
= saSocketAddr
.getHandle( );
665 sal_Bool bOK
= ( saSocketAddr
== poslSocketAddr
);
666 //t_print("getSocketAddrHandle_002\n");
667 CPPUNIT_ASSERT_MESSAGE( "test for getHandle() function: use getHandle() function as an intermediate way to create identical address.",
671 CPPUNIT_TEST_SUITE( getSocketAddrHandle
);
672 CPPUNIT_TEST( getSocketAddrHandle_001
);
673 CPPUNIT_TEST( getSocketAddrHandle_002
);
674 CPPUNIT_TEST_SUITE_END( );
676 }; // class getSocketAddrHandle
678 /** testing the method:
679 static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0);
682 class getLocalHostname
: public CppUnit::TestFixture
685 /* the process of getLocalHostname: 1.gethostname (same as /bin/hostname) returned name A
686 2. search A in /etc/hosts, if there is an alias name is A, return the name in the same row
689 void getLocalHostname_000()
691 // _osl_getFullQualifiedDomainName( );
692 oslSocketResult aResult
= osl_Socket_Error
;
693 rtl::OUString suHostname
= osl::SocketAddr::getLocalHostname(&aResult
);
694 CPPUNIT_ASSERT_MESSAGE("getLocalHostname failed", aResult
== osl_Socket_Ok
);
697 void getLocalHostname_001()
699 oslSocketResult
*pResult
= NULL
;
700 //printSocketResult(*pResult);
701 ::rtl::OUString suResult
= ::osl::SocketAddr::getLocalHostname( pResult
);
703 // LLA: IMHO localhost, or hostname by itself should be ok.
704 rtl::OUString suThisHost
= getThisHostname( );
706 if ( suThisHost
== "localhost" )
712 if (suThisHost
.equals(suResult
))
718 ::rtl::OUString suError
;
719 suError
= outputError(suResult
, getThisHostname( ), "test for getLocalHostname() function");
721 CPPUNIT_ASSERT_MESSAGE( STD_STRING(suError
), bOk
== true );
724 CPPUNIT_TEST_SUITE( getLocalHostname
);
725 CPPUNIT_TEST( getLocalHostname_000
);
726 CPPUNIT_TEST( getLocalHostname_001
);
727 CPPUNIT_TEST_SUITE_END( );
729 }; // class getLocalHostname
731 /** testing the method:
732 static inline void SAL_CALL resolveHostname( const ::rtl::OUString & strHostName , SocketAddr & Addr );
735 class resolveHostname
: public CppUnit::TestFixture
738 void resolveHostname_001()
740 ::osl::SocketAddr saSocketAddr
;
741 ::osl::SocketAddr::resolveHostname( rtl::OUString("127.0.0.1"), saSocketAddr
);
742 ::rtl::ByteSequence bsSocketAddr
= saSocketAddr
.getAddr( 0 );
743 sal_Bool bOK
= sal_False
;
745 if ( ( bsSocketAddr
[0] == 127 ) && ( bsSocketAddr
[1] == 0 ) &&( bsSocketAddr
[2] == 0 ) && ( bsSocketAddr
[3] == 1 ) )
748 CPPUNIT_ASSERT_MESSAGE( "test for resolveHostname() function: try to resolve localhost to 127.0.0.1.",
752 CPPUNIT_TEST_SUITE( resolveHostname
);
753 CPPUNIT_TEST( resolveHostname_001
);
754 CPPUNIT_TEST_SUITE_END( );
756 }; // class resolveHostname
758 /** testing the method:
759 static inline sal_Int32 SAL_CALL getServicePort(
760 const ::rtl::OUString& strServiceName,
761 const ::rtl::OUString & strProtocolName= ::rtl::OUString("tcp") );
764 class gettheServicePort
: public CppUnit::TestFixture
767 void gettheServicePort_001()
769 rtl::OUString
suServiceFTP ("ftp");
770 rtl::OUString
suProtocolTCP ("tcp");
772 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get ftp service port on TCP protocol.",
773 IP_PORT_FTP
== ::osl::SocketAddr::getServicePort( suServiceFTP
, suProtocolTCP
) );
776 void gettheServicePort_002()
778 rtl::OUString
suServiceTELNET ("telnet");
779 rtl::OUString
suProtocolTCP ("tcp");
780 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get telnet service port on TCP protocol.",
781 IP_PORT_TELNET
== ::osl::SocketAddr::getServicePort( suServiceTELNET
, suProtocolTCP
) );
784 void gettheServicePort_003()
786 //Solaris has no service called "https", please see /etc/services
787 rtl::OUString
suServiceNETBIOS ("netbios-dgm");
788 rtl::OUString
suProtocolUDP ("udp");
789 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get netbios-ssn service port on UDP protocol.",
790 IP_PORT_NETBIOS_DGM
== ::osl::SocketAddr::getServicePort( suServiceNETBIOS
, suProtocolUDP
) );
793 void gettheServicePort_004()
795 rtl::OUString
suProtocolUDP("udp" );
796 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get a service port which is not exist.",
797 OSL_INVALID_PORT
== ::osl::SocketAddr::getServicePort( ::rtl::OUString("notexist"), suProtocolUDP
) );
800 CPPUNIT_TEST_SUITE( gettheServicePort
);
801 CPPUNIT_TEST( gettheServicePort_001
);
802 CPPUNIT_TEST( gettheServicePort_002
);
803 CPPUNIT_TEST( gettheServicePort_003
);
804 CPPUNIT_TEST( gettheServicePort_004
);
805 CPPUNIT_TEST_SUITE_END( );
807 }; // class gettheServicePort
809 /** testing the method:
813 class getFamilyOfSocketAddr
: public CppUnit::TestFixture
816 void getFamilyOfSocketAddr_001()
818 ::osl::SocketAddr
saSocketAddr( rtl::OUString("localhost"), IP_PORT_HTTP1
);
819 oslSocketAddr psaOSLSocketAddr
= saSocketAddr
.getHandle( );
820 CPPUNIT_ASSERT_EQUAL(
821 osl_Socket_FamilyInet
,
822 osl_getFamilyOfSocketAddr( psaOSLSocketAddr
) );
824 CPPUNIT_ASSERT_MESSAGE( "test for osl_getFamilyOfSocketAddr.",
825 osl_getFamilyOfSocketAddr( psaOSLSocketAddr
) == osl_Socket_FamilyInet
);
828 CPPUNIT_TEST_SUITE( getFamilyOfSocketAddr
);
829 CPPUNIT_TEST( getFamilyOfSocketAddr_001
);
830 CPPUNIT_TEST_SUITE_END( );
832 }; // class getFamilyOfSocketAddr
834 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::ctors
);
835 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::is
);
836 //TODO: enable Test with valid host names
837 //CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::getHostname);
838 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::getPort
);
839 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::setPort
);
840 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::setAddr
);
841 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::getAddr
);
842 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::operator_equal
);
843 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::getSocketAddrHandle
);
844 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::getLocalHostname
);
845 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::resolveHostname
);
846 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::gettheServicePort
);
847 CPPUNIT_TEST_SUITE_REGISTRATION(osl_SocketAddr::getFamilyOfSocketAddr
);
849 } // namespace osl_SocketAddr
851 // this macro creates an empty function, which will called by the RegisterAllFunctions()
852 // to let the user the possibility to also register some functions by hand.
853 CPPUNIT_PLUGIN_IMPLEMENT();
855 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */