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