1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_sal.hxx" 30 31 /** test coder preface: 32 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform 33 if you are not including ws2_32.lib in makefile.mk, the including format will be like this: 34 35 .IF "$(GUI)" == "WNT" 36 SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib 37 SHL1STDLIBS += ws2_32.lib 38 .ENDIF 39 40 likewise on Solaris platform. 41 .IF "$(GUI)" == "UNX" 42 SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a 43 SHL1STDLIBS += -lsocket -ldl -lnsl 44 .ENDIF 45 46 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4 47 category. 48 49 3. some fragment of Socket source implementation are lack of comment so it is hard for testers 50 guess what the exact functionality or usage of a member. Hope the Socket section's comment 51 will be added. 52 53 4. following functions are declared but not implemented: 54 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; 55 */ 56 57 //------------------------------------------------------------------------ 58 // include files 59 //------------------------------------------------------------------------ 60 61 #include <testshl/simpleheader.hxx> 62 63 //#include "osl_Socket_Const.h" 64 #include "sockethelper.hxx" 65 66 using namespace osl; 67 using namespace rtl; 68 69 #define IP_PORT_ZERO 0 70 #define IP_PORT_FTP 21 71 #define IP_PORT_TELNET 23 72 #define IP_PORT_HTTP1 80 73 #define IP_PORT_HTTP2 8080 74 75 #define IP_PORT_MYPORT 8881 //8888 76 #define IP_PORT_MYPORT2 8883 //8890 77 #define IP_PORT_MYPORT3 8884 //8891 78 #define IP_PORT_INVAL 99999 79 #define IP_PORT_MYPORT4 8885 //8892 80 #define IP_PORT_NETBIOS_DGM 138 81 82 83 namespace osl_SocketAddr 84 { 85 86 /** testing the methods: 87 inline SocketAddr(); 88 inline SocketAddr(const SocketAddr& Addr); 89 inline SocketAddr(const oslSocketAddr , __osl_socket_NoCopy nocopy ); 90 inline SocketAddr(oslSocketAddr Addr); 91 inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort ); 92 */ 93 94 class ctors : public CppUnit::TestFixture 95 { 96 public: 97 98 void ctors_none() 99 { 100 /// SocketAddr constructor. 101 ::osl::SocketAddr saSocketAddr; 102 103 // oslSocketResult aResult; 104 // rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult); 105 106 // rtl::OUString suHost2 = getThisHostname(); 107 108 CPPUNIT_ASSERT_MESSAGE("test for none parameter constructor function: check if the socket address was created successfully", 109 sal_True == saSocketAddr.is( ) ); 110 } 111 112 void ctors_none_000() 113 { 114 /// SocketAddr constructor. 115 ::osl::SocketAddr saSocketAddr; 116 117 oslSocketResult aResult; 118 rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult); 119 rtl::OUString suHost2 = getThisHostname(); 120 121 sal_Bool bOk = compareUString(suHost, suHost2); 122 123 rtl::OUString suError = rtl::OUString::createFromAscii("Host names should be the same. From SocketAddr.getLocalHostname() it is'"); 124 suError += suHost; 125 suError += rtl::OUString::createFromAscii("', from getThisHostname() it is '"); 126 suError += suHost2; 127 suError += rtl::OUString::createFromAscii("'."); 128 129 CPPUNIT_ASSERT_MESSAGE(suError, sal_True == bOk); 130 } 131 132 void ctors_copy() 133 { 134 /// SocketAddr copy constructor. 135 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 136 ::osl::SocketAddr saCopySocketAddr( saSocketAddr ); 137 138 sal_Int32 nPort = saCopySocketAddr.getPort( ); 139 140 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy constructor function: copy constructor, do an action of copy construction then check the port with original set.", 141 ( sal_True == saCopySocketAddr.is( ) ) && ( nPort == IP_PORT_HTTP1 ) ); 142 } 143 144 void ctors_copy_no_001() 145 { 146 #if 0 147 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 148 oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( ); 149 150 ::osl::SocketAddr saSocketAddrCopy( psaOSLSocketAddr, SAL_NO_COPY ); 151 saSocketAddrCopy.setPort( IP_PORT_HTTP2 ); 152 153 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.", 154 saSocketAddr.getPort( ) == IP_PORT_HTTP2 ); 155 #endif 156 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 157 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 158 159 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); 160 161 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); 162 163 pSocketAddrCopy->setPort( IP_PORT_HTTP2 ); 164 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.", 165 pSocketAddr->getPort( ) == IP_PORT_HTTP2 ); 166 167 delete pSocketAddrCopy; 168 // LLA: don't do this also: delete pSocketAddr; 169 } 170 171 void ctors_copy_no_002() 172 { 173 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 174 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 175 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); 176 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); 177 178 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.", 179 pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) ); 180 181 delete pSocketAddrCopy; 182 } 183 184 void ctors_copy_handle_001() 185 { 186 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 187 ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) ); 188 189 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, get its port to check copy effect.", 190 saSocketAddrCopy.getPort( ) == IP_PORT_HTTP1 ); 191 } 192 193 void ctors_copy_handle_002() 194 { 195 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 196 ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) ); 197 saSocketAddrCopy.setPort( IP_PORT_HTTP2 ); 198 199 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, the original one should not be changed.", 200 saSocketAddr.getPort( ) != IP_PORT_HTTP2 ); 201 } 202 203 void ctors_hostname_port_001() 204 { 205 /// tcpip-specif constructor. 206 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 207 printUString( saSocketAddr.getHostname( ), "ctors_hostname_port_001:getHostname"); 208 209 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: do a constructor using tcpip spec, check the result.", 210 saSocketAddr.is( ) == sal_True && 211 ( saSocketAddr.getPort( ) == IP_PORT_FTP )/*&& 212 ( sal_True == compareUString( saSocketAddr.getHostname( ), aHostName1 ) ) */); 213 } 214 215 //same as is_002 216 void ctors_hostname_port_002() 217 { 218 /// tcpip-specif constructor. 219 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT2 ); 220 221 CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: using an invalid IP address, the socketaddr ctors should fail", sal_False == saSocketAddr.is( )); 222 } 223 CPPUNIT_TEST_SUITE( ctors ); 224 CPPUNIT_TEST( ctors_none ); 225 CPPUNIT_TEST( ctors_none_000 ); 226 CPPUNIT_TEST( ctors_copy ); 227 CPPUNIT_TEST( ctors_copy_no_001 ); 228 CPPUNIT_TEST( ctors_copy_no_002 ); 229 CPPUNIT_TEST( ctors_copy_handle_001 ); 230 CPPUNIT_TEST( ctors_copy_handle_002 ); 231 CPPUNIT_TEST( ctors_hostname_port_001 ); 232 CPPUNIT_TEST( ctors_hostname_port_002 ); 233 CPPUNIT_TEST_SUITE_END(); 234 235 }; // class ctors 236 237 238 /** testing the method: 239 inline sal_Bool is() const; 240 */ 241 242 class is : public CppUnit::TestFixture 243 { 244 public: 245 void is_001() 246 { 247 ::osl::SocketAddr saSocketAddr; 248 249 CPPUNIT_ASSERT_MESSAGE("test for is() function: create an unknown type socket, it should be True when call is.", 250 sal_True == saSocketAddr.is( ) ); 251 } 252 // refer to setPort_003() 253 void is_002() 254 { 255 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_INVAL ); 256 257 CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid port number", 258 sal_True == saSocketAddr.is( ) ); 259 } 260 261 void is_003() 262 { 263 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT ); 264 265 CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid Ip number", 266 sal_True != saSocketAddr.is( ) ); 267 } 268 269 CPPUNIT_TEST_SUITE( is ); 270 CPPUNIT_TEST( is_001 ); 271 CPPUNIT_TEST( is_002 ); 272 CPPUNIT_TEST( is_003 ); 273 CPPUNIT_TEST_SUITE_END(); 274 275 }; // class is 276 277 278 /** testing the method: 279 inline ::rtl::OUString SAL_CALL getHostname( oslSocketResult *pResult = 0 ) const; 280 */ 281 282 class getHostname : public CppUnit::TestFixture 283 { 284 public: 285 void setUp() 286 { 287 } 288 289 void tearDown() 290 { 291 } 292 293 void getHostname_000() 294 { 295 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.107"), IP_PORT_FTP ); 296 rtl::OUString suResult = saSocketAddr.getHostname( 0 ); 297 298 } 299 300 /** it will search the Ip in current machine's /etc/hosts at first, if find, then return the 301 mapped hostname, otherwise, it will search via DNS server, and often return hostname+ Domain name 302 like "sceri.PRC.Sun.COM" 303 The process is same as Socket::getLocalHost(), but getLocalHost can only return hostname of the current machine. 304 */ 305 void getHostname_001() 306 { 307 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.107"), IP_PORT_FTP ); 308 rtl::OUString suResult = saSocketAddr.getHostname( 0 ); 309 rtl::OUString suError = outputError(suResult, rtl::OUString::createFromAscii("sceri.PRC.Sun.COM"), "test for getHostname(0)"); 310 sal_Bool bOK = compareUString( suResult, rtl::OUString::createFromAscii("sceri.PRC.Sun.COM") ); 311 // search the returned hostname in /etc/hosts, if find, and the IP in the row is same as IP 312 // in the Addr, it's right also. 313 if ( bOK == sal_False) 314 { 315 rtl::OString aString = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US ); 316 if ( compareUString( getIPbyName( aString ), rtl::OUString::createFromAscii("129.158.217.107") ) == sal_True ) 317 bOK = sal_True; 318 } 319 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK); 320 } 321 322 // LLA: now we have to control, if this behaviour is right. 323 // LLA: this function does not work in company (Linux, Windows) but at home 324 void getHostname_002() 325 { 326 rtl::OUString suHostname = rtl::OUString::createFromAscii("cn-1.germany.sun.com"); 327 rtl::OString aString = ::rtl::OUStringToOString( suHostname, RTL_TEXTENCODING_ASCII_US ); 328 rtl::OUString aHostIP = getIPbyName( aString ); 329 330 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_FTP ); 331 sal_Bool bOK = saSocketAddr.setHostname( suHostname ); 332 CPPUNIT_ASSERT_MESSAGE("#SocketAddr.setHostname failed", sal_True == bOK ); 333 oslSocketResult aResult; 334 rtl::OUString suResult = saSocketAddr.getHostname( &aResult ); 335 CPPUNIT_ASSERT_MESSAGE("SocketAddr.getHostname failed.", aResult == osl_Socket_Ok); 336 337 rtl::OUString suError = outputError(suResult, suHostname, "test for getHostname(0)"); 338 bOK = compareUString( suResult, suHostname ); 339 if ( bOK == sal_False) 340 { 341 rtl::OString aStringResult = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US ); 342 rtl::OString aStringHostname = ::rtl::OUStringToOString( suHostname, RTL_TEXTENCODING_ASCII_US ); 343 if ( compareUString( getIPbyName( aStringResult ) , getIPbyName( aStringHostname ) ) == sal_True ) 344 { 345 bOK = sal_True; 346 } 347 } 348 349 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 350 } 351 352 353 CPPUNIT_TEST_SUITE( getHostname ); 354 CPPUNIT_TEST( getHostname_001 ); 355 CPPUNIT_TEST( getHostname_002 ); 356 CPPUNIT_TEST_SUITE_END(); 357 358 }; // class getHostname 359 360 361 /** testing the method: 362 inline sal_Int32 SAL_CALL getPort() const; 363 */ 364 365 class getPort : public CppUnit::TestFixture 366 { 367 public: 368 void getPort_001() 369 { 370 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 371 372 CPPUNIT_ASSERT_MESSAGE( "test for getPort() function: get a normal port number.", 373 IP_PORT_FTP == saSocketAddr.getPort( ) ); 374 } 375 376 void getPort_002() 377 { 378 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_INVAL ); 379 380 //t_print("#getPort_002: Port number is %d \n", saSocketAddr.getPort( )); 381 382 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).", 383 saSocketAddr.getPort( )>=1 && saSocketAddr.getPort( ) <= 65535 ); 384 } 385 //two cases will return OSL_INVALID_PORT: 1. not valid SocketAddr 386 //2. SocketAddr family is not osl_Socket_FamilyInet, but case 2 could not be constructed 387 void getPort_003() 388 { 389 rtl::OUString suInvalidIP = rtl::OUString::createFromAscii("123.345.67.89"); 390 ::osl::SocketAddr saSocketAddr( suInvalidIP, IP_PORT_MYPORT ); 391 392 CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid IP to a SocketAddr, get the port to see returned value. ", 393 saSocketAddr.getPort( ) == OSL_INVALID_PORT ); 394 } 395 396 CPPUNIT_TEST_SUITE( getPort ); 397 CPPUNIT_TEST( getPort_001 ); 398 CPPUNIT_TEST( getPort_002 ); 399 CPPUNIT_TEST( getPort_003 ); 400 CPPUNIT_TEST_SUITE_END( ); 401 402 }; // class getPort 403 404 405 /** testing the method: 406 inline sal_Bool SAL_CALL setPort( sal_Int32 nPort ); 407 rfc1413.txt: TCP port numbers are from 1-65535 408 rfc1700.txt: 0/tcp Reserved ; 0/udp Reserved 409 */ 410 411 class setPort : public CppUnit::TestFixture 412 { 413 public: 414 void setPort_001() 415 { 416 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 417 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_TELNET ); 418 419 CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: modify a port number setting, and check it.", 420 ( sal_True == bOK ) && 421 ( IP_PORT_TELNET == saSocketAddr.getPort( ) ) ); 422 } 423 424 /** 0 to 1024 is known as the reserved port range (traditionally only root can assign programs to ports in 425 this range) and the ephemeral port range from 1025 to 65535. 426 As many of you programmers will know, when you specify the source port of 0 when you connect to a host, 427 the OS automatically reassigns the port number to high numbered ephemeral port. The same happens if you 428 try to bind a listening socket to port 0. 429 http://www.securiteam.com/securityreviews/5XP0Q2AAKS.html 430 another: http://www.muq.org/~cynbe/muq/mufref_564.html 431 */ 432 void setPort_002() 433 { 434 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 435 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_ZERO ); 436 437 oslSocket sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 438 ::osl::Socket sSocket(sHandle); 439 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 );//sal_True); 440 sal_Bool bOK1 = sSocket.bind( saSocketAddr ); 441 CPPUNIT_ASSERT_MESSAGE( "bind SocketAddr failed", bOK1 == sal_True ); 442 443 sal_Int32 newPort = sSocket.getLocalPort(); 444 //t_print("#new port is %d\n", newPort ); 445 446 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.", 447 ( 1024 <= newPort ) && ( 65535 >= newPort ) && ( bOK == sal_True ) ); 448 449 } 450 451 void setPort_003() 452 { 453 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP); 454 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_INVAL ); 455 //on Linux, getPort return 34463 456 //t_print("#Port number is %d \n", saSocketAddr.getPort( )); 457 458 CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an address with invalid port. it should return error or convert it to a valid port.", 459 ( ( 1 <= saSocketAddr.getPort( ) ) && ( 65535 >= saSocketAddr.getPort( ) ) &&( bOK == sal_True ) ) || 460 bOK == sal_False); 461 } 462 463 /* this is not a inet-addr => can't set port */ 464 void setPort_004() 465 { 466 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_FTP); 467 sal_Bool bOK = saSocketAddr.setPort( IP_PORT_MYPORT ); 468 469 CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an invalid address with valid port. it should return error.", 470 bOK == sal_False); 471 } 472 473 474 CPPUNIT_TEST_SUITE( setPort ); 475 CPPUNIT_TEST( setPort_001 ); 476 CPPUNIT_TEST( setPort_002 ); 477 CPPUNIT_TEST( setPort_003 ); 478 CPPUNIT_TEST( setPort_004 ); 479 CPPUNIT_TEST_SUITE_END( ); 480 481 }; // class setPort 482 483 484 /** tester comment: 485 486 In the following two functions, it use ::rtl::ByteSequence as an intermediate storage for address, 487 the ByteSequence object can hold sal_Int8 arrays, which is raged [-127, 127], in case of IP addr 488 that is greater than 127, say 129.158.217.202, it will stored as -127, -98, -39, -54, it is unique 489 in the range of sal_Int8, but lack of readability. 490 so may be a sal_uInt8 array is better. 491 */ 492 493 494 /** testing the method: 495 inline sal_Bool SAL_CALL setAddr( const ::rtl::ByteSequence & address ); 496 */ 497 498 class setAddr : public CppUnit::TestFixture 499 { 500 public: 501 void setAddr_001() 502 { 503 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 504 saSocketAddr.setAddr( UStringIPToByteSequence( rtl::OUString::createFromAscii("127.0.0.1") ) ); 505 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 ); 506 sal_Bool bOK = sal_False; 507 508 // if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) && ( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 509 // bOK = sal_True; 510 bOK = ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ); 511 512 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 ", 513 sal_True == bOK ); 514 } 515 516 517 CPPUNIT_TEST_SUITE( setAddr ); 518 CPPUNIT_TEST( setAddr_001 ); 519 CPPUNIT_TEST_SUITE_END( ); 520 521 }; // class setAddr 522 523 524 /** testing the method: 525 inline ::rtl::ByteSequence SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const; 526 */ 527 528 class getAddr : public CppUnit::TestFixture 529 { 530 public: 531 void getAddr_001() 532 { 533 oslSocketResult SocketResult; 534 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 535 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( &SocketResult ); 536 537 sal_Bool bOK = sal_False; 538 539 //if ( ( osl_Socket_Ok == SocketResult ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 540 // bOK = sal_True; 541 bOK = ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ); 542 543 CPPUNIT_ASSERT_MESSAGE( "test for getAddr() function: construct a socketaddr with IP assigned, get the address to check correctness.Caught unknown exception on (Win32)", 544 sal_True == bOK && SocketResult == osl_Socket_Ok); 545 } 546 547 CPPUNIT_TEST_SUITE( getAddr ); 548 CPPUNIT_TEST( getAddr_001 ); 549 CPPUNIT_TEST_SUITE_END( ); 550 551 }; // class getAddr 552 553 554 /** testing the methods: 555 inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr); 556 inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr); 557 inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy ); 558 inline sal_Bool SAL_CALL operator== (oslSocketAddr Addr) const; 559 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; /// not implemented. 560 */ 561 562 class operator_equal : public CppUnit::TestFixture 563 { 564 public: 565 void operator_equal_001() 566 { 567 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); 568 ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 569 570 saSocketAddrEqual = saSocketAddr; 571 sal_Bool bOK = sal_False; 572 ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 ); 573 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 ) ) 576 bOK = sal_True; 577 578 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: use operator= to assign Ip1 to Ip2, check its modification.", 579 sal_True == bOK ); 580 } 581 582 583 void operator_equal_002() 584 { 585 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.199"), IP_PORT_TELNET); 586 ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 587 588 saSocketAddrEqual = saSocketAddr; 589 CPPUNIT_ASSERT_MESSAGE( "after assign, the assigned SocketAddr is not same as the original Addr", 590 IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ); 591 saSocketAddrEqual.setPort( IP_PORT_MYPORT3 ); 592 saSocketAddr.setPort( IP_PORT_HTTP2 ); 593 594 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.", 595 IP_PORT_MYPORT3 == saSocketAddrEqual.getPort( ) ); 596 } 597 598 void operator_equal_const_001() 599 { 600 const ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); 601 ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 602 603 saSocketAddrEqual = saSocketAddr; 604 sal_Bool bOK = sal_False; 605 ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 ); 606 607 // if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 608 if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) && ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ) == sal_True ) 609 bOK = sal_True; 610 611 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: use operator= const to assign Ip1 to Ip2, verify the change on the second one.", 612 sal_True == bOK ); 613 } 614 615 void operator_equal_const_002() 616 { 617 const ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); 618 ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 619 620 saSocketAddrEqual = saSocketAddr; 621 saSocketAddrEqual.setPort( IP_PORT_HTTP1 ); 622 623 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.", 624 IP_PORT_HTTP1 != saSocketAddr.getPort( ) ); 625 } 626 627 void operator_equal_assign_001() 628 { 629 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET ); 630 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 631 ::osl::SocketAddr* pSocketAddrAssign = new ::osl::SocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 632 oslSocketAddr poslSocketAddr = pSocketAddr->getHandle( ); 633 //if( m_handle ) osl_destroySocketAddr( m_handle ); so pSocketAddrAssign had been destroyed and then point to pSocketAddr 634 pSocketAddrAssign->assign(poslSocketAddr, SAL_NO_COPY); 635 636 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.", 637 pSocketAddrAssign->getPort( ) == IP_PORT_TELNET ); 638 639 delete pSocketAddrAssign; 640 } 641 642 void operator_is_equal_001() 643 { 644 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); 645 ::osl::SocketAddr saSocketAddrequal( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET ); 646 647 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two identical Address.", 648 sal_True == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) ); 649 } 650 651 void operator_is_equal_002() 652 { 653 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP); 654 ::osl::SocketAddr saSocketAddrequal( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET ); 655 656 CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two different Address.", 657 sal_False == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) ); 658 } 659 660 CPPUNIT_TEST_SUITE( operator_equal ); 661 CPPUNIT_TEST( operator_equal_001 ); 662 CPPUNIT_TEST( operator_equal_002 ); 663 CPPUNIT_TEST( operator_equal_const_001 ); 664 CPPUNIT_TEST( operator_equal_const_002 ); 665 CPPUNIT_TEST( operator_equal_assign_001 ); 666 CPPUNIT_TEST( operator_is_equal_001 ); 667 CPPUNIT_TEST( operator_is_equal_002 ); 668 CPPUNIT_TEST_SUITE_END( ); 669 670 }; // class operator_equal 671 672 673 674 /** testing the method: 675 inline oslSocketAddr SAL_CALL getHandle() const; 676 */ 677 678 class getSocketAddrHandle : public CppUnit::TestFixture 679 { 680 public: 681 682 void getSocketAddrHandle_001() 683 { 684 ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 685 CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 686 oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); 687 ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); 688 689 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.", 690 pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) ); 691 692 delete pSocketAddrCopy; 693 } 694 695 void getSocketAddrHandle_002() 696 { 697 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("deuce.PRC.Sun.COM"), IP_PORT_MYPORT4 ); 698 oslSocketAddr poslSocketAddr = saSocketAddr.getHandle( ); 699 700 sal_Bool bOK = ( saSocketAddr == poslSocketAddr ); 701 //t_print("getSocketAddrHandle_002\n"); 702 CPPUNIT_ASSERT_MESSAGE( "test for getHandle() function: use getHandle() function as an intermediate way to create identical address.", 703 sal_True == bOK ); 704 } 705 706 CPPUNIT_TEST_SUITE( getSocketAddrHandle ); 707 CPPUNIT_TEST( getSocketAddrHandle_001 ); 708 CPPUNIT_TEST( getSocketAddrHandle_002 ); 709 CPPUNIT_TEST_SUITE_END( ); 710 711 }; // class getSocketAddrHandle 712 713 714 /** testing the method: 715 static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0); 716 */ 717 718 class getLocalHostname : public CppUnit::TestFixture 719 { 720 public: 721 /* the process of getLocalHostname: 1.gethostname (same as /bin/hostname) returned name A 722 2. search A in /etc/hosts, if there is an alias name is A, return the name in the same row 723 */ 724 725 void getLocalHostname_000() 726 { 727 // _osl_getFullQualifiedDomainName( ); 728 oslSocketResult aResult = osl_Socket_Error; 729 rtl::OUString suHostname = osl::SocketAddr::getLocalHostname(&aResult); 730 CPPUNIT_ASSERT_MESSAGE("getLocalHostname failed", aResult == osl_Socket_Ok); 731 } 732 733 void getLocalHostname_001() 734 { 735 oslSocketResult *pResult = NULL; 736 //printSocketResult(*pResult); 737 ::rtl::OUString suResult = ::osl::SocketAddr::getLocalHostname( pResult ); 738 739 // LLA: IMHO localhost, or hostname by itself should be ok. 740 rtl::OUString suThisHost = getThisHostname( ); 741 bool bOk = false; 742 if (suThisHost.equals(rtl::OUString::createFromAscii("localhost"))) 743 { 744 bOk = true; 745 } 746 else 747 { 748 if (suThisHost.equals(suResult)) 749 { 750 bOk = true; 751 } 752 } 753 754 ::rtl::OUString suError; 755 suError = outputError(suResult, getThisHostname( ), "test for getLocalHostname() function"); 756 757 CPPUNIT_ASSERT_MESSAGE( suError, bOk == true ); 758 } 759 760 CPPUNIT_TEST_SUITE( getLocalHostname ); 761 CPPUNIT_TEST( getLocalHostname_000 ); 762 CPPUNIT_TEST( getLocalHostname_001 ); 763 CPPUNIT_TEST_SUITE_END( ); 764 765 }; // class getLocalHostname 766 767 768 /** testing the method: 769 static inline void SAL_CALL resolveHostname( const ::rtl::OUString & strHostName , SocketAddr & Addr ); 770 */ 771 772 class resolveHostname : public CppUnit::TestFixture 773 { 774 public: 775 void resolveHostname_001() 776 { 777 ::osl::SocketAddr saSocketAddr; 778 ::osl::SocketAddr::resolveHostname( rtl::OUString::createFromAscii("127.0.0.1"), saSocketAddr ); 779 ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 ); 780 sal_Bool bOK = sal_False; 781 782 if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 783 bOK = sal_True; 784 785 CPPUNIT_ASSERT_MESSAGE( "test for resolveHostname() function: try to resolve localhost to 127.0.0.1.", 786 sal_True == bOK ); 787 } 788 789 CPPUNIT_TEST_SUITE( resolveHostname ); 790 CPPUNIT_TEST( resolveHostname_001 ); 791 CPPUNIT_TEST_SUITE_END( ); 792 793 }; // class resolveHostname 794 795 796 /** testing the method: 797 static inline sal_Int32 SAL_CALL getServicePort( 798 const ::rtl::OUString& strServiceName, 799 const ::rtl::OUString & strProtocolName= ::rtl::OUString::createFromAscii( "tcp" ) ); 800 */ 801 802 class gettheServicePort : public CppUnit::TestFixture 803 { 804 public: 805 void gettheServicePort_001() 806 { 807 rtl::OUString suServiceFTP = rtl::OUString::createFromAscii( "ftp" ); 808 rtl::OUString suProtocolTCP = rtl::OUString::createFromAscii( "tcp" ); 809 810 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get ftp service port on TCP protocol.", 811 IP_PORT_FTP== ::osl::SocketAddr::getServicePort( suServiceFTP, suProtocolTCP ) ); 812 } 813 814 void gettheServicePort_002() 815 { 816 rtl::OUString suServiceTELNET = rtl::OUString::createFromAscii( "telnet" ); 817 rtl::OUString suProtocolTCP = rtl::OUString::createFromAscii( "tcp" ); 818 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get telnet service port on TCP protocol.", 819 IP_PORT_TELNET== ::osl::SocketAddr::getServicePort( suServiceTELNET, suProtocolTCP ) ); 820 } 821 822 void gettheServicePort_003() 823 { 824 //Solaris has no service called "https", please see /etc/services 825 rtl::OUString suServiceNETBIOS = rtl::OUString::createFromAscii( "netbios-dgm" ); 826 rtl::OUString suProtocolUDP = rtl::OUString::createFromAscii( "udp" ); 827 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get netbios-ssn service port on UDP protocol.", 828 IP_PORT_NETBIOS_DGM == ::osl::SocketAddr::getServicePort( suServiceNETBIOS, suProtocolUDP ) ); 829 } 830 831 void gettheServicePort_004() 832 { 833 rtl::OUString suProtocolUDP = rtl::OUString::createFromAscii( "udp" ); 834 CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get a service port which is not exist.", 835 OSL_INVALID_PORT == ::osl::SocketAddr::getServicePort( ::rtl::OUString::createFromAscii( "notexist" ), suProtocolUDP ) ); 836 } 837 838 CPPUNIT_TEST_SUITE( gettheServicePort ); 839 CPPUNIT_TEST( gettheServicePort_001 ); 840 CPPUNIT_TEST( gettheServicePort_002 ); 841 CPPUNIT_TEST( gettheServicePort_003 ); 842 CPPUNIT_TEST( gettheServicePort_004 ); 843 CPPUNIT_TEST_SUITE_END( ); 844 845 }; // class gettheServicePort 846 847 /** testing the method: 848 849 */ 850 851 class getFamilyOfSocketAddr : public CppUnit::TestFixture 852 { 853 public: 854 void getFamilyOfSocketAddr_001() 855 { 856 ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 857 oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( ); 858 CPPUNIT_ASSERT_EQUAL( 859 osl_Socket_FamilyInet, 860 osl_getFamilyOfSocketAddr( psaOSLSocketAddr ) ); 861 862 CPPUNIT_ASSERT_MESSAGE( "test for osl_getFamilyOfSocketAddr.", 863 osl_getFamilyOfSocketAddr( psaOSLSocketAddr ) == osl_Socket_FamilyInet ); 864 } 865 866 CPPUNIT_TEST_SUITE( getFamilyOfSocketAddr ); 867 CPPUNIT_TEST( getFamilyOfSocketAddr_001 ); 868 CPPUNIT_TEST_SUITE_END( ); 869 870 }; // class getFamilyOfSocketAddr 871 872 // ----------------------------------------------------------------------------- 873 874 875 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::ctors, "osl_SocketAddr"); 876 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::is, "osl_SocketAddr"); 877 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getHostname, "osl_SocketAddr"); 878 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getPort, "osl_SocketAddr"); 879 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setPort, "osl_SocketAddr"); 880 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setAddr, "osl_SocketAddr"); 881 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getAddr, "osl_SocketAddr"); 882 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::operator_equal, "osl_SocketAddr"); 883 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getSocketAddrHandle, "osl_SocketAddr"); 884 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getLocalHostname, "osl_SocketAddr"); 885 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::resolveHostname, "osl_SocketAddr"); 886 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::gettheServicePort, "osl_SocketAddr"); 887 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getFamilyOfSocketAddr, "osl_SocketAddr"); 888 889 } // namespace osl_SocketAddr 890 891 // ----------------------------------------------------------------------------- 892 893 // this macro creates an empty function, which will called by the RegisterAllFunctions() 894 // to let the user the possibility to also register some functions by hand. 895 NOADDITIONAL; 896