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_FTP 21 66 #define IP_PORT_TELNET 23 67 #define IP_PORT_HTTP2 8080 68 #define IP_PORT_INVAL 99999 69 #define IP_PORT_POP3 110 70 #define IP_PORT_NETBIOS 139 71 #define IP_PORT_MYPORT 8881 72 #define IP_PORT_MYPORT1 8882 73 #define IP_PORT_MYPORT5 8886 74 #define IP_PORT_MYPORT6 8887 75 #define IP_PORT_MYPORT7 8895 76 #define IP_PORT_MYPORT8 8896 77 #define IP_PORT_MYPORT9 8897 78 79 //------------------------------------------------------------------------ 80 // helper functions 81 //------------------------------------------------------------------------ 82 83 // just used to test socket::close() when accepting 84 class AcceptorThread : public Thread 85 { 86 ::osl::AcceptorSocket asAcceptorSocket; 87 ::rtl::OUString aHostIP; 88 sal_Bool bOK; 89 protected: run()90 void SAL_CALL run( ) 91 { 92 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 ); 93 ::osl::StreamSocket ssStreamConnection; 94 95 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True); 96 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 97 if ( sal_True != bOK1 ) 98 { 99 t_print("# AcceptorSocket bind address failed.\n" ) ; 100 return; 101 } 102 sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 103 if ( sal_True != bOK2 ) 104 { 105 t_print("# AcceptorSocket listen address failed.\n" ) ; 106 return; 107 } 108 109 asAcceptorSocket.enableNonBlockingMode( sal_False ); 110 111 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection ); 112 if (eResult != osl_Socket_Ok ) 113 { 114 bOK = sal_True; 115 t_print("AcceptorThread: acceptConnection failed! \n"); 116 } 117 } 118 public: AcceptorThread(::osl::AcceptorSocket & asSocket,::rtl::OUString const & aBindIP)119 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP ) 120 : asAcceptorSocket( asSocket ), aHostIP( aBindIP ) 121 { 122 bOK = sal_False; 123 } 124 isOK()125 sal_Bool isOK() { return bOK; } 126 ~AcceptorThread()127 ~AcceptorThread( ) 128 { 129 if ( isRunning( ) ) 130 { 131 asAcceptorSocket.shutdown(); 132 t_print("# error: Acceptor thread not terminated.\n" ); 133 } 134 } 135 }; 136 137 namespace osl_Socket 138 { 139 140 /** testing the methods: 141 inline Socket( ); 142 inline Socket( const Socket & socket ); 143 inline Socket( oslSocket socketHandle ); 144 inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire ); 145 */ 146 147 /** test writer's comment: 148 149 class Socket can not be initialized by its protected constructor, though the protected 150 constructor is the most convenient way to create a new socket. 151 it only allow the method of C function osl_createSocket like: 152 ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, 153 osl_Socket_ProtocolIp ) ); 154 the use of C method lost some of the transparent of tester using C++ wrapper. 155 */ 156 157 158 class ctors : public CppUnit::TestFixture 159 { 160 public: 161 oslSocket sHandle; 162 // initialization setUp()163 void setUp( ) 164 { 165 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 166 } 167 tearDown()168 void tearDown( ) 169 { 170 sHandle = NULL; 171 } 172 173 ctors_none()174 void ctors_none() 175 { 176 /// Socket constructor. 177 // ::osl::Socket sSocket(); 178 179 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the socket was created successfully, if no exception occured", 180 1 == 1 ); 181 } 182 ctors_acquire()183 void ctors_acquire() 184 { 185 /// Socket constructor. 186 ::osl::Socket sSocket( sHandle ); 187 188 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully", 189 osl_Socket_TypeStream == sSocket.getType( ) ); 190 } 191 ctors_no_acquire()192 void ctors_no_acquire() 193 { 194 /// Socket constructor. 195 ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE ); 196 197 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully", 198 osl_Socket_TypeStream == sSocket.getType( ) ); 199 } 200 ctors_copy_ctor()201 void ctors_copy_ctor() 202 { 203 ::osl::Socket sSocket( sHandle ); 204 /// Socket copy constructor. 205 ::osl::Socket copySocket( sSocket ); 206 207 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor", 208 osl_Socket_TypeStream == copySocket.getType( ) ); 209 } 210 ctors_TypeRaw()211 void ctors_TypeRaw() 212 { 213 #ifdef WNT 214 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 215 // LLA: ? ::osl::Socket sSocket( sHandleRaw ); 216 CPPUNIT_ASSERT_MESSAGE( " type osl_Socket_TypeRaw socket create failed on UNX ", sHandleRaw != NULL); 217 #else 218 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 219 CPPUNIT_ASSERT_MESSAGE( " can't create socket with type osl_Socket_TypeRaw within UNX is ok.", sHandleRaw == NULL); 220 #endif 221 } 222 ctors_family_Ipx()223 void ctors_family_Ipx() 224 { 225 oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 226 CPPUNIT_ASSERT_MESSAGE( " family osl_Socket_FamilyIpx socket create failed! ", sHandleIpx != NULL); 227 ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE ); 228 t_print("#Type is %d \n", sSocket.getType( ) ); 229 230 CPPUNIT_ASSERT_MESSAGE(" test for create new Socket instance that family is osl_Socket_FamilyIpx", 231 osl_Socket_TypeStream == sSocket.getType( ) ); 232 } 233 234 235 236 CPPUNIT_TEST_SUITE( ctors ); 237 CPPUNIT_TEST( ctors_none ); 238 CPPUNIT_TEST( ctors_acquire ); 239 CPPUNIT_TEST( ctors_no_acquire ); 240 CPPUNIT_TEST( ctors_copy_ctor ); 241 CPPUNIT_TEST( ctors_TypeRaw ); 242 CPPUNIT_TEST( ctors_family_Ipx ); 243 CPPUNIT_TEST_SUITE_END(); 244 245 }; // class ctors 246 247 248 /** testing the methods: 249 inline Socket& SAL_CALL operator= ( oslSocket socketHandle); 250 inline Socket& SAL_CALL operator= (const Socket& sock); 251 inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ; 252 inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const; 253 */ 254 255 class operators : public CppUnit::TestFixture 256 { 257 public: 258 oslSocket sHandle; 259 // initialization setUp()260 void setUp( ) 261 { 262 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 263 } 264 tearDown()265 void tearDown( ) 266 { 267 sHandle = NULL; 268 } 269 270 271 /** test writer's comment: 272 273 the assignment operator does not support direct assinment like: 274 ::osl::Socket sSocket = sHandle. 275 */ operators_assignment_handle()276 void operators_assignment_handle() 277 { 278 ::osl::Socket sSocket(sHandle); 279 ::osl::Socket assignSocket = sSocket.getHandle(); 280 281 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.", 282 osl_Socket_TypeStream == assignSocket.getType( ) ); 283 } 284 operators_assignment()285 void operators_assignment() 286 { 287 ::osl::Socket sSocket( sHandle ); 288 ::osl::Socket assignSocket = sSocket; 289 290 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator", 291 osl_Socket_TypeStream == assignSocket.getType( ) ); 292 } 293 operators_equal_handle_001()294 void operators_equal_handle_001() 295 { 296 /// Socket constructor. 297 ::osl::Socket sSocket( sHandle ); 298 ::osl::Socket equalSocket = sSocket; 299 300 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check equal.", 301 equalSocket == sHandle ); 302 } 303 operators_equal_handle_002()304 void operators_equal_handle_002() 305 { 306 /// Socket constructor. 307 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); 308 309 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check unequal.", 310 !( equalSocket == sHandle ) ); 311 } 312 operators_equal_001()313 void operators_equal_001() 314 { 315 ::osl::Socket sSocket( sHandle ); 316 /// Socket copy constructor. 317 ::osl::Socket equalSocket( sSocket ); 318 319 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal function: check equal.", 320 equalSocket == sSocket ); 321 } 322 operators_equal_002()323 void operators_equal_002() 324 { 325 ::osl::Socket sSocket( sHandle ); 326 /// Socket copy constructor. 327 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) ); 328 329 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_002 function: check unequal.", 330 !( equalSocket == sSocket ) ); 331 } 332 333 CPPUNIT_TEST_SUITE( operators ); 334 CPPUNIT_TEST( operators_assignment_handle ); 335 CPPUNIT_TEST( operators_assignment ); 336 CPPUNIT_TEST( operators_equal_handle_001 ); 337 CPPUNIT_TEST( operators_equal_handle_002 ); 338 CPPUNIT_TEST( operators_equal_001 ); 339 CPPUNIT_TEST( operators_equal_002 ); 340 CPPUNIT_TEST_SUITE_END(); 341 342 }; // class operators 343 344 345 /** testing the methods: 346 inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite ); 347 inline void SAL_CALL close(); 348 */ 349 350 class close : public CppUnit::TestFixture 351 { 352 public: 353 oslSocket sHandle; 354 // initialization setUp()355 void setUp( ) 356 { 357 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 358 } 359 tearDown()360 void tearDown( ) 361 { 362 sHandle = NULL; 363 } 364 365 close_001()366 void close_001() 367 { 368 ::osl::Socket sSocket(sHandle); 369 sSocket.close(); 370 371 CPPUNIT_ASSERT_MESSAGE( "test for close_001 function: this function is reserved for test.", 372 sSocket.getHandle() == sHandle ); 373 } 374 close_002()375 void close_002() 376 { 377 //#if defined(LINUX) 378 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 379 AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") ); 380 myAcceptorThread.create(); 381 382 thread_sleep( 1 ); 383 //when accepting, close the socket, the thread will not block for accepting 384 //man close:Any locks held on the file it was associated with, and owned by the process, are removed 385 asSocket.close(); 386 //thread_sleep( 2 ); 387 myAcceptorThread.join(); 388 389 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", 390 myAcceptorThread.isOK() == sal_True ); 391 //#endif 392 } 393 394 // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( ) close_003()395 void close_003() 396 { 397 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 398 AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("0.0.0.0") ); 399 myAcceptorThread.create(); 400 401 thread_sleep( 1 ); 402 asSocket.close(); 403 myAcceptorThread.join(); 404 405 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.", 406 myAcceptorThread.isOK() == sal_True ); 407 } 408 409 CPPUNIT_TEST_SUITE( close ); 410 CPPUNIT_TEST( close_001 ); 411 CPPUNIT_TEST( close_002 ); 412 CPPUNIT_TEST( close_003 ); 413 CPPUNIT_TEST_SUITE_END(); 414 415 }; // class close 416 417 /** testing the method: 418 inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const; 419 */ 420 421 class getLocalAddr : public CppUnit::TestFixture 422 { 423 public: 424 oslSocket sHandle; 425 // initialization setUp()426 void setUp( ) 427 { 428 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 429 } 430 tearDown()431 void tearDown( ) 432 { 433 sHandle = NULL; 434 } 435 436 // get the Address of the local end of the socket getLocalAddr_001()437 void getLocalAddr_001() 438 { 439 ::osl::Socket sSocket(sHandle); 440 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT8 ); 441 ::osl::SocketAddr saLocalSocketAddr; 442 443 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 444 445 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 446 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 447 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 448 449 sSocket.getLocalAddr( saLocalSocketAddr ); 450 451 sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ; 452 453 CPPUNIT_ASSERT_MESSAGE( "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.", 454 sal_True == bOK ); 455 } 456 457 458 CPPUNIT_TEST_SUITE( getLocalAddr ); 459 CPPUNIT_TEST( getLocalAddr_001 ); 460 CPPUNIT_TEST_SUITE_END(); 461 462 }; // class getLocalAddr 463 464 465 /** testing the method: 466 inline sal_Int32 SAL_CALL getLocalPort() const; 467 */ 468 469 class getLocalPort : public CppUnit::TestFixture 470 { 471 public: 472 oslSocket sHandle; 473 // initialization setUp()474 void setUp( ) 475 { 476 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 477 } 478 tearDown()479 void tearDown( ) 480 { 481 sHandle = NULL; 482 } 483 484 getLocalPort_001()485 void getLocalPort_001() 486 { 487 ::osl::Socket sSocket(sHandle); 488 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT7 ); // aHostIp1 localhost 489 ::osl::SocketAddr saLocalSocketAddr; 490 491 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 492 493 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 494 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 495 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 496 sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) ); 497 498 CPPUNIT_ASSERT_MESSAGE( "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.", 499 sal_True == bOK ); 500 } 501 502 /** test writer's comment: 503 504 the invalid port number can not be set by giving invalid port number 505 such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be 506 valid, the only instance that the getLocalPort returns OSL_INVALID_PORT 507 is when saSocketAddr itself is an invalid one, that is , the IP or host name 508 can not be found, then the created socket address is not valid. 509 */ getLocalPort_002()510 void getLocalPort_002() 511 { 512 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_TELNET); 513 #ifdef WNT 514 ::osl::Socket sSocket(sHandle); 515 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 516 sSocket.bind( saBindSocketAddr ); 517 //Invalid IP, so bind should fail 518 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), 519 ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT), 520 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned."); 521 sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) ); 522 (void)bOK; 523 #else 524 //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT 525 ::rtl::OUString suError = ::rtl::OUString::createFromAscii( "on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT, but can not create Addr of that case"); 526 #endif 527 CPPUNIT_ASSERT_MESSAGE( suError, sal_False ); 528 529 } 530 getLocalPort_003()531 void getLocalPort_003() 532 { 533 ::osl::Socket sSocket(sHandle); 534 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL); 535 536 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 537 538 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 539 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 540 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 541 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )), 542 ::rtl::OUString::createFromAscii("34463"), 543 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned"); 544 sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535); 545 546 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 547 } 548 549 CPPUNIT_TEST_SUITE( getLocalPort ); 550 CPPUNIT_TEST( getLocalPort_001 ); 551 // LLA: CPPUNIT_TEST( getLocalPort_002 ); 552 CPPUNIT_TEST( getLocalPort_003 ); 553 CPPUNIT_TEST_SUITE_END(); 554 555 }; // class getLocalPort 556 557 558 /** testing the method: 559 inline ::rtl::OUString SAL_CALL getLocalHost() const; 560 561 Mindyliu: on Linux, at first it will check the binded in /etc/hosts, if it has the binded IP, it will return the hostname in it; 562 else if the binded IP is "127.0.0.1", it will return "localhost", if it's the machine's ethernet ip such as "129.158.217.90", it 563 will return hostname of current processor such as "aegean.PRC.Sun.COM" 564 */ 565 566 class getLocalHost : public CppUnit::TestFixture 567 { 568 public: 569 oslSocket sHandle; 570 // initialization setUp()571 void setUp( ) 572 { 573 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 574 } 575 tearDown()576 void tearDown( ) 577 { 578 sHandle = NULL; 579 } 580 581 getLocalHost_001()582 void getLocalHost_001() 583 { 584 ::osl::Socket sSocket(sHandle); 585 //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu 586 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT6 ); 587 588 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 589 590 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 591 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString(); 592 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 ); 593 sal_Bool bOK; 594 ::rtl::OUString suError; 595 #ifdef WNT 596 bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; 597 suError = outputError(sSocket.getLocalHost( ), getThisHostname( ), 598 "test for getLocalHost function: create localhost socket and check name"); 599 #else 600 ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" ); 601 sal_Bool bRes1, bRes2; 602 bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ; 603 bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ; 604 bOK = bRes1 || bRes2; 605 suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name"); 606 #endif 607 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 608 } 609 getLocalHost_002()610 void getLocalHost_002() 611 { 612 ::osl::Socket sSocket(sHandle); 613 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_POP3); 614 ::osl::SocketAddr saLocalSocketAddr; 615 616 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 617 sSocket.bind( saBindSocketAddr ); 618 //Invalid IP, so bind should fail 619 sal_Bool bOK = compareUString( sSocket.getLocalHost( ), rtl::OUString::createFromAscii("") ) ; 620 ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), rtl::OUString::createFromAscii(""), "test for getLocalHost function: getLocalHost with invalid SocketAddr"); 621 622 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 623 } 624 625 CPPUNIT_TEST_SUITE( getLocalHost ); 626 CPPUNIT_TEST( getLocalHost_001 ); 627 CPPUNIT_TEST( getLocalHost_002 ); 628 CPPUNIT_TEST_SUITE_END(); 629 630 }; // class getLocalHost 631 632 633 /** testing the methods: 634 inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const; 635 inline sal_Int32 SAL_CALL getPeerPort() const; 636 inline ::rtl::OUString SAL_CALL getPeerHost() const; 637 */ 638 class getPeer : public CppUnit::TestFixture 639 { 640 public: 641 oslSocket sHandle; 642 TimeValue *pTimeout; 643 ::osl::AcceptorSocket asAcceptorSocket; 644 ::osl::ConnectorSocket csConnectorSocket; 645 646 647 // initialization setUp()648 void setUp( ) 649 { 650 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 651 pTimeout->Seconds = 3; 652 pTimeout->Nanosec = 0; 653 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 654 } 655 tearDown()656 void tearDown( ) 657 { 658 free( pTimeout ); 659 sHandle = NULL; 660 asAcceptorSocket.close( ); 661 csConnectorSocket.close( ); 662 } 663 664 getPeer_001()665 void getPeer_001() 666 { 667 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 668 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 669 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 670 ::osl::StreamSocket ssConnection; 671 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 672 /// launch server socket 673 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 674 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind '127.0.0.1' address failed.", sal_True == bOK1 ); 675 sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 676 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 677 678 asAcceptorSocket.enableNonBlockingMode( sal_True ); 679 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 680 681 /// launch client socket 682 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 683 684 /// get peer information 685 csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected. 686 sal_Int32 peerPort = csConnectorSocket.getPeerPort( ); 687 ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( ); 688 689 CPPUNIT_ASSERT_MESSAGE( "test for getPeer function: setup a connection and then get the peer address, port and host from client side.", 690 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&& 691 ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) && 692 ( peerPort == saLocalSocketAddr.getPort( ) )); 693 } 694 695 696 CPPUNIT_TEST_SUITE( getPeer ); 697 CPPUNIT_TEST( getPeer_001 ); 698 CPPUNIT_TEST_SUITE_END(); 699 700 }; // class getPeer 701 702 703 /** testing the methods: 704 inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface); 705 */ 706 707 708 class bind : public CppUnit::TestFixture 709 { 710 public: 711 oslSocket sHandle; 712 // initialization setUp()713 void setUp( ) 714 { 715 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 716 } 717 tearDown()718 void tearDown( ) 719 { 720 sHandle = NULL; 721 } 722 723 bind_001()724 void bind_001() 725 { 726 ::osl::Socket sSocket(sHandle); 727 //bind must use local IP address ---mindyliu 728 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 ); 729 730 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 731 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 732 CPPUNIT_ASSERT_MESSAGE( "Socket bind fail.", sal_True == bOK1 ); 733 734 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ; 735 736 sSocket.close(); 737 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", sal_True == bOK2 ); 738 } 739 bind_002()740 void bind_002() 741 { 742 ::osl::Socket sSocket(sHandle); 743 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_NETBIOS ); 744 ::osl::SocketAddr saLocalSocketAddr; 745 746 sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True); 747 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr ); 748 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ; 749 750 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", 751 ( sal_False == bOK1 ) && ( sal_False == bOK2 ) ); 752 } 753 754 CPPUNIT_TEST_SUITE( bind ); 755 CPPUNIT_TEST( bind_001 ); 756 CPPUNIT_TEST( bind_002 ); 757 CPPUNIT_TEST_SUITE_END(); 758 759 }; // class bind 760 761 762 /** testing the methods: 763 inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const; 764 765 */ 766 class isRecvReady : public CppUnit::TestFixture 767 { 768 public: 769 oslSocket sHandle; 770 TimeValue *pTimeout; 771 ::osl::AcceptorSocket asAcceptorSocket; 772 ::osl::ConnectorSocket csConnectorSocket; 773 774 775 // initialization setUp()776 void setUp( ) 777 { 778 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 779 pTimeout->Seconds = 3; 780 pTimeout->Nanosec = 0; 781 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 782 } 783 tearDown()784 void tearDown( ) 785 { 786 free( pTimeout ); 787 sHandle = NULL; 788 asAcceptorSocket.close( ); 789 csConnectorSocket.close( ); 790 } 791 792 isRecvReady_001()793 void isRecvReady_001() 794 { 795 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 ); 796 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 ); 797 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 798 ::osl::StreamSocket ssConnection; 799 /// launch server socket 800 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 801 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 802 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 803 sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 804 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 805 asAcceptorSocket.enableNonBlockingMode( sal_True ); 806 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 807 808 /// launch client socket 809 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 810 811 /// is receive ready? 812 sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout ); 813 814 CPPUNIT_ASSERT_MESSAGE( "test for isRecvReady function: setup a connection and then check if it can transmit data.", 815 ( sal_True == bOK3 ) ); 816 } 817 818 819 CPPUNIT_TEST_SUITE( isRecvReady ); 820 CPPUNIT_TEST( isRecvReady_001 ); 821 CPPUNIT_TEST_SUITE_END(); 822 823 }; // class isRecvReady 824 825 826 /** testing the methods: 827 inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const; 828 */ 829 class isSendReady : public CppUnit::TestFixture 830 { 831 public: 832 oslSocket sHandle; 833 TimeValue *pTimeout; 834 ::osl::AcceptorSocket asAcceptorSocket; 835 ::osl::ConnectorSocket csConnectorSocket; 836 837 838 // initialization setUp()839 void setUp( ) 840 { 841 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 842 pTimeout->Seconds = 3; 843 pTimeout->Nanosec = 0; 844 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 845 } 846 tearDown()847 void tearDown( ) 848 { 849 free( pTimeout ); 850 sHandle = NULL; 851 asAcceptorSocket.close( ); 852 csConnectorSocket.close( ); 853 } 854 855 isSendReady_001()856 void isSendReady_001() 857 { 858 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 859 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 860 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 861 ::osl::StreamSocket ssConnection; 862 863 /// launch server socket 864 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 865 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 866 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 867 sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 868 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 869 asAcceptorSocket.enableNonBlockingMode( sal_True ); 870 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 871 872 /// launch client socket 873 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server... 874 875 /// is send ready? 876 sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout ); 877 878 CPPUNIT_ASSERT_MESSAGE( "test for isSendReady function: setup a connection and then check if it can transmit data.", 879 ( sal_True == bOK3 ) ); 880 } 881 882 883 CPPUNIT_TEST_SUITE( isSendReady ); 884 CPPUNIT_TEST( isSendReady_001 ); 885 CPPUNIT_TEST_SUITE_END(); 886 887 }; // class isSendReady 888 889 890 /** testing the methods: 891 inline oslSocketType SAL_CALL getType() const; 892 893 */ 894 895 class getType : public CppUnit::TestFixture 896 { 897 public: 898 oslSocket sHandle; 899 // initialization setUp()900 void setUp( ) 901 { 902 903 } 904 tearDown()905 void tearDown( ) 906 { 907 sHandle = NULL; 908 } 909 910 getType_001()911 void getType_001() 912 { 913 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 914 ::osl::Socket sSocket(sHandle); 915 916 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", 917 osl_Socket_TypeStream == sSocket.getType( ) ); 918 } 919 getType_002()920 void getType_002() 921 { 922 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 923 ::osl::Socket sSocket(sHandle); 924 925 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", 926 osl_Socket_TypeDgram == sSocket.getType( ) ); 927 } 928 929 #ifdef UNX 930 // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here 931 // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw() getType_003()932 void getType_003() 933 { 934 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.this is not passed in (LINUX, SOLARIS), the osl_Socket_TypeRaw, type socket can not be created.", 935 sal_True); 936 } 937 #else getType_003()938 void getType_003() 939 { 940 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp ); 941 ::osl::Socket sSocket(sHandle); 942 943 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.", 944 osl_Socket_TypeRaw == sSocket.getType( ) ); 945 } 946 #endif 947 948 CPPUNIT_TEST_SUITE( getType ); 949 CPPUNIT_TEST( getType_001 ); 950 CPPUNIT_TEST( getType_002 ); 951 CPPUNIT_TEST( getType_003 ); 952 CPPUNIT_TEST_SUITE_END(); 953 954 }; // class getType 955 956 957 958 /** testing the methods: 959 inline sal_Int32 SAL_CALL getOption( 960 oslSocketOption Option, 961 void* pBuffer, 962 sal_uInt32 BufferLen, 963 oslSocketOptionLevel Level= osl_Socket_LevelSocket) const; 964 965 inline sal_Int32 getOption( oslSocketOption option ) const; 966 967 */ 968 969 class getOption : public CppUnit::TestFixture 970 { 971 public: 972 oslSocket sHandle; 973 // initialization setUp()974 void setUp( ) 975 { 976 977 } 978 tearDown()979 void tearDown( ) 980 { 981 sHandle = NULL; 982 } 983 984 /** test writer's comment: 985 986 in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream. 987 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ 988 in 1. 989 */ 990 getOption_001()991 void getOption_001() 992 { 993 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 994 ::osl::Socket sSocket(sHandle); 995 sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 996 *pType = 0; 997 sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) ); 998 sal_Bool bOK = ( SOCK_STREAM == *pType ); 999 // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream 1000 //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType ); 1001 free( pType ); 1002 1003 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get type option of socket.", 1004 sal_True == bOK ); 1005 } 1006 1007 // getsockopt error getOption_004()1008 void getOption_004() 1009 { 1010 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 1011 ::osl::Socket sSocket(sHandle); 1012 1013 sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 1014 sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) ); 1015 free( pbDontRoute ); 1016 1017 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get invalid option of socket, should return -1.", 1018 nRes == -1 ); 1019 } 1020 getOption_simple_001()1021 void getOption_simple_001() 1022 { 1023 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 1024 ::osl::Socket sSocket(sHandle); 1025 1026 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) ); 1027 1028 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.", 1029 sal_True == bOK ); 1030 } 1031 getOption_simple_002()1032 void getOption_simple_002() 1033 { 1034 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ); 1035 ::osl::Socket sSocket(sHandle); 1036 1037 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) ); 1038 1039 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.", 1040 sal_True == bOK ); 1041 } 1042 1043 CPPUNIT_TEST_SUITE( getOption ); 1044 CPPUNIT_TEST( getOption_001 ); 1045 CPPUNIT_TEST( getOption_004 ); 1046 CPPUNIT_TEST( getOption_simple_001 ); 1047 CPPUNIT_TEST( getOption_simple_002 ); 1048 CPPUNIT_TEST_SUITE_END(); 1049 1050 }; // class getOption 1051 1052 1053 /** testing the methods: 1054 inline sal_Bool SAL_CALL setOption( oslSocketOption Option, 1055 void* pBuffer, 1056 sal_uInt32 BufferLen, 1057 oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const; 1058 */ 1059 1060 class setOption : public CppUnit::TestFixture 1061 { 1062 public: 1063 TimeValue *pTimeout; 1064 // LLA: maybe there is an error in the source, 1065 // as long as I remember, if a derived class do not overload all ctors there is a problem. 1066 1067 ::osl::AcceptorSocket asAcceptorSocket; 1068 setUp()1069 void setUp( ) 1070 { 1071 1072 } 1073 tearDown()1074 void tearDown( ) 1075 { 1076 asAcceptorSocket.close( ); 1077 } 1078 1079 1080 // LLA: 1081 // getSocketOption returns BufferLen, or -1 if something failed 1082 1083 // setSocketOption returns sal_True, if option could stored 1084 // else sal_False 1085 setOption_001()1086 void setOption_001() 1087 { 1088 /// set and get option. 1089 int nBufferLen = sizeof ( sal_Int32); 1090 // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool! 1091 1092 sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 1093 *pbDontRouteSet = 1; // sal_True; 1094 1095 sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) ); 1096 *pGetBuffer = 0; 1097 1098 // maybe asAcceptorSocket is not right initialized 1099 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen ); 1100 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b1 ) ); 1101 sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); 1102 CPPUNIT_ASSERT_MESSAGE( "getOption function failed.", ( n2 == nBufferLen ) ); 1103 1104 // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value, 1105 // just judge it is zero or not! 1106 sal_Bool bOK = ( 0 != *pGetBuffer ); 1107 t_print("#setOption_001: getOption is %d \n", *pGetBuffer); 1108 1109 // toggle check, set to 0 1110 *pbDontRouteSet = 0; 1111 1112 sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) ); 1113 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b3 ) ); 1114 sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen ); 1115 CPPUNIT_ASSERT_MESSAGE( "getOption (DONTROUTE) function failed.", ( n4 == nBufferLen ) ); 1116 1117 sal_Bool bOK2 = ( 0 == *pGetBuffer ); 1118 1119 t_print("#setOption_001: getOption is %d \n", *pGetBuffer); 1120 1121 // LLA: sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 1122 // LLA: *pbDontTouteSet = sal_True; 1123 // LLA: sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) ); 1124 // LLA: *pbDontTouteGet = sal_False; 1125 // LLA: asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontTouteSet, sizeof ( sal_Bool ) ); 1126 // LLA: asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pbDontTouteGet, sizeof ( sal_Bool ) ); 1127 // LLA: ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet), 1128 // LLA: ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet), 1129 // LLA: "test for setOption function: set osl_Socket_OptionDontRoute and then check"); 1130 // LLA: 1131 // LLA: sal_Bool bOK = ( sal_True == *pbDontTouteGet ); 1132 // LLA: free( pbDontTouteSet ); 1133 // LLA: free( pbDontTouteGet ); 1134 1135 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", 1136 ( sal_True == bOK ) && (sal_True == bOK2) ); 1137 1138 free( pbDontRouteSet ); 1139 free( pGetBuffer ); 1140 // LLA: CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 1141 } 1142 setOption_002()1143 void setOption_002() 1144 { 1145 /// set and get option. 1146 1147 // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen ); 1148 // *pbLingerSet = 7; 1149 // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen ); 1150 /* struct */linger aLingerSet; 1151 sal_Int32 nBufferLen = sizeof( struct linger ); 1152 aLingerSet.l_onoff = 1; 1153 aLingerSet.l_linger = 7; 1154 1155 linger aLingerGet; 1156 1157 asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen ); 1158 1159 sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen ); 1160 CPPUNIT_ASSERT_MESSAGE( "getOption (SO_LINGER) function failed.", ( n1 == nBufferLen ) ); 1161 1162 //t_print("#setOption_002: getOption is %d \n", aLingerGet.l_linger); 1163 sal_Bool bOK = ( 7 == aLingerGet.l_linger ); 1164 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check. ", 1165 sal_True == bOK ); 1166 1167 } 1168 setOption_003()1169 void setOption_003() 1170 { 1171 linger aLingerSet; 1172 aLingerSet.l_onoff = 1; 1173 aLingerSet.l_linger = 7; 1174 1175 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 ); 1176 printUString( asAcceptorSocket.getErrorAsString( ) ); 1177 CPPUNIT_ASSERT_MESSAGE( "setOption (SO_LINGER) function failed for optlen is 0.", 1178 ( b1 == sal_False ) ); 1179 } 1180 setOption_simple_001()1181 void setOption_simple_001() 1182 { 1183 /// set and get option. 1184 asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True ); 1185 sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); 1186 1187 t_print("setOption_simple_001(): getoption is %d \n", asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) ); 1188 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", 1189 ( sal_True == bOK ) ); 1190 } 1191 setOption_simple_002()1192 void setOption_simple_002() 1193 { 1194 /// set and get option. 1195 // LLA: this does not work, due to the fact that SO_LINGER is a structure 1196 // LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 ); 1197 // LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) ); 1198 1199 // LLA: CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.", 1200 // LLA: ( sal_True == bOK ) ); 1201 } 1202 1203 CPPUNIT_TEST_SUITE( setOption ); 1204 CPPUNIT_TEST( setOption_001 ); 1205 CPPUNIT_TEST( setOption_002 ); 1206 CPPUNIT_TEST( setOption_003 ); 1207 CPPUNIT_TEST( setOption_simple_001 ); 1208 // LLA: CPPUNIT_TEST( setOption_simple_002 ); 1209 CPPUNIT_TEST_SUITE_END(); 1210 1211 }; // class setOption 1212 1213 1214 1215 /** testing the method: 1216 inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode); 1217 */ 1218 class enableNonBlockingMode : public CppUnit::TestFixture 1219 { 1220 public: 1221 ::osl::AcceptorSocket asAcceptorSocket; 1222 enableNonBlockingMode_001()1223 void enableNonBlockingMode_001() 1224 { 1225 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 1226 ::osl::StreamSocket ssConnection; 1227 1228 /// launch server socket 1229 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1230 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 1231 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 1232 sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 1233 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 1234 asAcceptorSocket.enableNonBlockingMode( sal_True ); 1235 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 1236 1237 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. 1238 sal_Bool bOK = sal_True; 1239 asAcceptorSocket.close( ); 1240 1241 CPPUNIT_ASSERT_MESSAGE( "test for enableNonBlockingMode function: launch a server socket and make it non blocking. if it can pass the acceptConnection statement, it is non-blocking", 1242 ( sal_True == bOK ) ); 1243 } 1244 1245 1246 CPPUNIT_TEST_SUITE( enableNonBlockingMode ); 1247 CPPUNIT_TEST( enableNonBlockingMode_001 ); 1248 CPPUNIT_TEST_SUITE_END(); 1249 1250 }; // class enableNonBlockingMode 1251 1252 1253 /** testing the method: 1254 inline sal_Bool SAL_CALL isNonBlockingMode() const; 1255 */ 1256 class isNonBlockingMode : public CppUnit::TestFixture 1257 { 1258 public: 1259 ::osl::AcceptorSocket asAcceptorSocket; 1260 isNonBlockingMode_001()1261 void isNonBlockingMode_001() 1262 { 1263 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT ); 1264 ::osl::StreamSocket ssConnection; 1265 1266 /// launch server socket 1267 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True); 1268 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr ); 1269 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 ); 1270 sal_Bool bOK2 = asAcceptorSocket.listen( 1 ); 1271 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 ); 1272 1273 sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( ); 1274 asAcceptorSocket.enableNonBlockingMode( sal_True ); 1275 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection... 1276 1277 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default. 1278 sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( ); 1279 asAcceptorSocket.close( ); 1280 1281 CPPUNIT_ASSERT_MESSAGE( "test for isNonBlockingMode function: launch a server socket and make it non blocking. it is expected to change from blocking mode to non-blocking mode.", 1282 ( sal_False == bOK3 ) && ( sal_True == bOK4 ) ); 1283 } 1284 1285 1286 CPPUNIT_TEST_SUITE( isNonBlockingMode ); 1287 CPPUNIT_TEST( isNonBlockingMode_001 ); 1288 CPPUNIT_TEST_SUITE_END(); 1289 1290 }; // class isNonBlockingMode 1291 1292 /** testing the method: 1293 inline void SAL_CALL clearError() const; 1294 */ 1295 class clearError : public CppUnit::TestFixture 1296 { 1297 public: 1298 oslSocket sHandle; 1299 // initialization setUp()1300 void setUp( ) 1301 { 1302 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1303 } 1304 tearDown()1305 void tearDown( ) 1306 { 1307 sHandle = NULL; 1308 } 1309 1310 clearError_001()1311 void clearError_001() 1312 { 1313 ::osl::Socket sSocket(sHandle); 1314 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_HTTP2 ); 1315 ::osl::SocketAddr saLocalSocketAddr; 1316 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1317 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" 1318 oslSocketError seBind = sSocket.getError( ); 1319 sSocket.clearError( ); 1320 1321 CPPUNIT_ASSERT_MESSAGE( "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.", 1322 osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None ); 1323 } 1324 1325 1326 CPPUNIT_TEST_SUITE( clearError ); 1327 CPPUNIT_TEST( clearError_001 ); 1328 CPPUNIT_TEST_SUITE_END(); 1329 1330 }; // class clearError 1331 1332 1333 /** testing the methods: 1334 inline oslSocketError getError() const; 1335 inline ::rtl::OUString getErrorAsString( ) const; 1336 */ 1337 class getError : public CppUnit::TestFixture 1338 { 1339 public: 1340 oslSocket sHandle; 1341 // initialization setUp()1342 void setUp( ) 1343 { 1344 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1345 } 1346 tearDown()1347 void tearDown( ) 1348 { 1349 sHandle = NULL; 1350 } 1351 1352 getError_001()1353 void getError_001() 1354 { 1355 ::osl::Socket sSocket(sHandle); 1356 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 1357 ::osl::SocketAddr saLocalSocketAddr; 1358 1359 CPPUNIT_ASSERT_MESSAGE( "test for getError function: should get no error.", 1360 osl_Socket_E_None == sSocket.getError( ) ); 1361 } 1362 getError_002()1363 void getError_002() 1364 { 1365 ::osl::Socket sSocket(sHandle); 1366 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_FTP ); 1367 ::osl::SocketAddr saLocalSocketAddr; 1368 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True); 1369 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail" 1370 //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError. 1371 #if defined(SOLARIS) 1372 CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. ", 1373 osl_Socket_E_InvalidError == sSocket.getError( ) ); 1374 #else 1375 //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail. 1376 1377 CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. Passed on Linux & Win32", 1378 osl_Socket_E_AddrNotAvail == sSocket.getError( ) ); 1379 #endif 1380 } 1381 1382 CPPUNIT_TEST_SUITE( getError ); 1383 CPPUNIT_TEST( getError_001 ); 1384 CPPUNIT_TEST( getError_002 ); 1385 CPPUNIT_TEST_SUITE_END(); 1386 1387 }; // class getError 1388 1389 1390 1391 /** testing the methods: 1392 inline oslSocket getHandle() const; 1393 */ 1394 1395 class getHandle : public CppUnit::TestFixture 1396 { 1397 public: 1398 oslSocket sHandle; 1399 // initialization setUp()1400 void setUp( ) 1401 { 1402 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 1403 } 1404 tearDown()1405 void tearDown( ) 1406 { 1407 sHandle = NULL; 1408 } 1409 getHandle_001()1410 void getHandle_001() 1411 { 1412 ::osl::Socket sSocket(sHandle); 1413 ::osl::Socket assignSocket = sSocket.getHandle(); 1414 1415 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.", 1416 osl_Socket_TypeStream == assignSocket.getType( ) ); 1417 } 1418 getHandle_002()1419 void getHandle_002() 1420 { 1421 ::osl::Socket sSocket( sHandle ); 1422 ::osl::Socket assignSocket ( sSocket.getHandle( ) ); 1423 1424 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator", 1425 osl_Socket_TypeStream == assignSocket.getType( ) ); 1426 } 1427 1428 CPPUNIT_TEST_SUITE( getHandle ); 1429 CPPUNIT_TEST( getHandle_001 ); 1430 CPPUNIT_TEST( getHandle_002 ); 1431 CPPUNIT_TEST_SUITE_END(); 1432 1433 }; // class getHandle 1434 1435 1436 // ----------------------------------------------------------------------------- 1437 1438 1439 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::ctors, "osl_Socket"); 1440 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::operators, "osl_Socket"); 1441 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::close, "osl_Socket"); 1442 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalAddr, "osl_Socket"); 1443 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalPort, "osl_Socket"); 1444 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalHost, "osl_Socket"); 1445 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getPeer, "osl_Socket"); 1446 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::bind, "osl_Socket"); 1447 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isRecvReady, "osl_Socket"); 1448 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isSendReady, "osl_Socket"); 1449 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getType, "osl_Socket"); 1450 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getOption, "osl_Socket"); 1451 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::setOption, "osl_Socket"); 1452 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::enableNonBlockingMode, "osl_Socket"); 1453 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isNonBlockingMode, "osl_Socket"); 1454 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::clearError, "osl_Socket"); 1455 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getError, "osl_Socket"); 1456 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getHandle, "osl_Socket"); 1457 1458 } // namespace osl_Socket 1459 1460 // ----------------------------------------------------------------------------- 1461 1462 // this macro creates an empty function, which will called by the RegisterAllFunctions() 1463 // to let the user the possibility to also register some functions by hand. 1464 NOADDITIONAL; 1465