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 //------------------------------------------------------------------------ 32 // include files 33 //------------------------------------------------------------------------ 34 35 #include "cppunit/TestAssert.h" 36 #include "cppunit/TestFixture.h" 37 #include "cppunit/extensions/HelperMacros.h" 38 #include "cppunit/plugin/TestPlugIn.h" 39 #include "test/uniquepipename.hxx" 40 #include <sal/types.h> 41 #include <rtl/ustring.hxx> 42 43 #ifndef _OSL_THREAD_HXX 44 #include <osl/thread.hxx> 45 #endif 46 47 #ifndef _OSL_MUTEX_HXX 48 #include <osl/mutex.hxx> 49 #endif 50 51 #ifndef _OSL_MUTEX_HXX 52 #include <osl/pipe.hxx> 53 #endif 54 #include <osl/time.h> 55 56 #ifdef UNX 57 #include <unistd.h> 58 #endif 59 #include <string.h> 60 61 using namespace osl; 62 using namespace rtl; 63 64 //------------------------------------------------------------------------ 65 // helper functions 66 //------------------------------------------------------------------------ 67 68 /** print Boolean value. 69 */ 70 inline void printBool( sal_Bool bOk ) 71 { 72 printf("#printBool# " ); 73 ( sal_True == bOk ) ? printf("YES!\n" ): printf("NO!\n" ); 74 } 75 76 /** print a UNI_CODE String. 77 */ 78 inline void printUString( const ::rtl::OUString & str ) 79 { 80 rtl::OString aString; 81 82 printf("#printUString_u# " ); 83 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ); 84 printf("%s\n", aString.getStr( ) ); 85 } 86 87 /** print last error of pipe system. 88 */ 89 inline void printPipeError( ::osl::Pipe aPipe ) 90 { 91 oslPipeError nError = aPipe.getError( ); 92 printf("#printPipeError# " ); 93 switch ( nError ) { 94 case osl_Pipe_E_None: 95 printf("Success!\n" ); 96 break; 97 case osl_Pipe_E_NotFound: 98 printf("The returned error is: Not found!\n" ); 99 break; 100 case osl_Pipe_E_AlreadyExists: 101 printf("The returned error is: Already exist!\n" ); 102 break; 103 case osl_Pipe_E_NoProtocol: 104 printf("The returned error is: No protocol!\n" ); 105 break; 106 case osl_Pipe_E_NetworkReset: 107 printf("The returned error is: Network reset!\n" ); 108 break; 109 case osl_Pipe_E_ConnectionAbort: 110 printf("The returned error is: Connection aborted!\n" ); 111 break; 112 case osl_Pipe_E_ConnectionReset: 113 printf("The returned error is: Connection reset!\n" ); 114 break; 115 case osl_Pipe_E_NoBufferSpace: 116 printf("The returned error is: No buffer space!\n" ); 117 break; 118 case osl_Pipe_E_TimedOut: 119 printf("The returned error is: Timeout!\n" ); 120 break; 121 case osl_Pipe_E_ConnectionRefused: 122 printf("The returned error is: Connection refused!\n" ); 123 break; 124 case osl_Pipe_E_invalidError: 125 printf("The returned error is: Invalid error!\n" ); 126 break; 127 default: 128 printf("The returned error is: Number %d, Unknown Error\n", nError ); 129 break; 130 } 131 } 132 133 134 135 //------------------------------------------------------------------------ 136 // pipe name and transfer contents 137 //------------------------------------------------------------------------ 138 const rtl::OUString aTestPipeName = rtl::OUString::createFromAscii( "testpipe2" ); 139 const rtl::OUString aTestPipe1 = rtl::OUString::createFromAscii( "testpipe1" ); 140 const rtl::OUString aTestString = rtl::OUString::createFromAscii( "Sun Microsystems" ); 141 142 const OString m_pTestString1("Sun Microsystems"); 143 const OString m_pTestString2("test pipe PASS/OK"); 144 145 //------------------------------------------------------------------------ 146 // test code start here 147 //------------------------------------------------------------------------ 148 149 namespace osl_Pipe 150 { 151 152 //------------------------------------------------------------------------ 153 // most return value -1 denote a fail of operation. 154 //------------------------------------------------------------------------ 155 #define OSL_PIPE_FAIL -1 156 157 /** testing the methods: 158 inline Pipe(); 159 inline Pipe(const ::rtl::OUString& strName, oslPipeOptions Options); 160 inline Pipe(const ::rtl::OUString& strName, oslPipeOptions Options,const Security & rSecurity); 161 inline Pipe(const Pipe& pipe); 162 inline Pipe(oslPipe pipe, __sal_NoAcquire noacquire ); 163 inline Pipe(oslPipe Pipe); 164 */ 165 class ctors : public CppUnit::TestFixture 166 { 167 public: 168 sal_Bool bRes, bRes1; 169 170 void setUp( ) 171 { 172 } 173 174 void tearDown( ) 175 { 176 } 177 178 void ctors_none( ) 179 { 180 ::osl::Pipe aPipe; 181 bRes = aPipe.is( ); 182 183 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, yet no case to test.", 184 sal_False == bRes ); 185 } 186 187 void ctors_name_option( ) 188 { 189 /// create a named pipe. 190 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 191 ::osl::Pipe aAssignPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); 192 193 bRes = aPipe.is( ) && aAssignPipe.is( ); 194 195 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.", 196 sal_True == bRes ); 197 } 198 199 void ctors_name_option_security( ) 200 { 201 /// create a security pipe. 202 const ::osl::Security rSecurity; 203 ::osl::Pipe aSecurityPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity ); 204 205 bRes = aSecurityPipe.is( ); 206 207 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name, option and security, the test of security is not implemented yet.", 208 sal_True == bRes ); 209 } 210 211 void ctors_copy( ) 212 { 213 /// create a pipe. 214 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 215 /// create a pipe using copy constructor. 216 ::osl::Pipe aCopyPipe( aPipe ); 217 218 bRes = aCopyPipe.is( ) && aCopyPipe == aPipe; 219 220 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.", 221 sal_True == bRes ); 222 } 223 224 /** tester comment: 225 226 When test the following two constructors, don't know how to test the 227 acquire and no acquire action. possible plans: 228 1.release one handle and check the other( did not success since the 229 other still exist and valid. ) 230 2. release one handle twice to see getLastError( )(the getLastError 231 always returns invalidError(LINUX)). 232 */ 233 234 void ctors_no_acquire( ) 235 { 236 /// create a pipe. 237 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 238 /// constructs a pipe reference without acquiring the handle. 239 ::osl::Pipe aNoAcquirePipe( aPipe.getHandle( ), SAL_NO_ACQUIRE ); 240 241 bRes = aNoAcquirePipe.is( ); 242 ///aPipe.clear( ); 243 ///bRes1 = aNoAcquirePipe.is( ); 244 245 246 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no aquire of handle, only validation test, do not know how to test no acquire.", 247 sal_True == bRes ); 248 } 249 250 void ctors_acquire( ) 251 { 252 /// create a base pipe. 253 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 254 /// constructs two pipes without acquiring the handle on the base pipe. 255 ::osl::Pipe aAcquirePipe( aPipe.getHandle( ) ); 256 ::osl::Pipe aAcquirePipe1( NULL ); 257 258 bRes = aAcquirePipe.is( ); 259 bRes1 = aAcquirePipe1.is( ); 260 261 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no aquire of handle.only validation test, do not know how to test no acquire.", 262 sal_True == bRes && sal_False == bRes1 ); 263 } 264 265 CPPUNIT_TEST_SUITE( ctors ); 266 CPPUNIT_TEST( ctors_none ); 267 CPPUNIT_TEST( ctors_name_option ); 268 CPPUNIT_TEST( ctors_name_option_security ); 269 CPPUNIT_TEST( ctors_copy ); 270 CPPUNIT_TEST( ctors_no_acquire ); 271 CPPUNIT_TEST( ctors_acquire ); 272 CPPUNIT_TEST_SUITE_END( ); 273 }; // class ctors 274 275 276 /** testing the method: 277 inline sal_Bool SAL_CALL is() const; 278 */ 279 class is : public CppUnit::TestFixture 280 { 281 public: 282 void is_001( ) 283 { 284 ::osl::Pipe aPipe; 285 286 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), check if the pipe is a valid one.", sal_False == aPipe.is( ) ); 287 } 288 289 void is_002( ) 290 { 291 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 292 293 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), a normal pipe creation.", sal_True == aPipe.is( ) ); 294 } 295 296 void is_003( ) 297 { 298 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 299 aPipe.clear( ); 300 301 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid case.", sal_False == aPipe.is( ) ); 302 } 303 304 void is_004( ) 305 { 306 ::osl::Pipe aPipe( NULL ); 307 308 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid constructor.", sal_False == aPipe.is( ) ); 309 } 310 311 CPPUNIT_TEST_SUITE( is ); 312 CPPUNIT_TEST( is_001 ); 313 CPPUNIT_TEST( is_002 ); 314 CPPUNIT_TEST( is_003 ); 315 CPPUNIT_TEST( is_004 ); 316 CPPUNIT_TEST_SUITE_END( ); 317 }; // class is 318 319 320 /** testing the methods: 321 inline sal_Bool create( const ::rtl::OUString & strName, 322 oslPipeOptions Options, const Security &rSec ); 323 nline sal_Bool create( const ::rtl::OUString & strName, 324 oslPipeOptions Options = osl_Pipe_OPEN ); 325 */ 326 class create : public CppUnit::TestFixture 327 { 328 public: 329 sal_Bool bRes, bRes1; 330 331 /** tester comment: 332 333 security create only be tested creation, security section is 334 untested yet. 335 */ 336 337 void create_named_security_001( ) 338 { 339 const Security rSec; 340 ::osl::Pipe aPipe; 341 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec ); 342 bRes1 = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec ); 343 aPipe.clear( ); 344 345 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.", 346 sal_True == bRes && sal_False == bRes1); 347 } 348 349 void create_named_security_002( ) 350 { 351 const Security rSec; 352 ::osl::Pipe aPipe, aPipe1; 353 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec ); 354 bRes1 = aPipe1.create( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN, rSec ); 355 aPipe.clear( ); 356 357 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.", 358 sal_True == bRes && sal_True == bRes1); 359 } 360 361 void create_named_001( ) 362 { 363 ::osl::Pipe aPipe; 364 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 365 bRes1 = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 366 aPipe.clear( ); 367 368 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.", 369 sal_True == bRes && sal_False == bRes1); 370 } 371 372 void create_named_002( ) 373 { 374 ::osl::Pipe aPipe, aPipe1; 375 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 376 bRes1 = aPipe1.create( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); 377 aPipe.clear( ); 378 379 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.", 380 sal_True == bRes && sal_True == bRes1); 381 } 382 383 void create_named_003( ) 384 { 385 ::osl::Pipe aPipe; 386 bRes = aPipe.create( test::uniquePipeName(aTestPipeName) ); 387 aPipe.clear( ); 388 389 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test default option is open.", 390 sal_False == bRes ); 391 } 392 393 CPPUNIT_TEST_SUITE( create ); 394 CPPUNIT_TEST( create_named_security_001 ); 395 CPPUNIT_TEST( create_named_security_002 ); 396 CPPUNIT_TEST( create_named_001 ); 397 CPPUNIT_TEST( create_named_002 ); 398 CPPUNIT_TEST( create_named_003 ); 399 CPPUNIT_TEST_SUITE_END( ); 400 }; // class create 401 402 403 /** testing the method: 404 inline void SAL_CALL clear(); 405 */ 406 class clear : public CppUnit::TestFixture 407 { 408 public: 409 sal_Bool bRes, bRes1; 410 411 void clear_001( ) 412 { 413 ::osl::Pipe aPipe; 414 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 415 aPipe.clear( ); 416 bRes = aPipe.is( ); 417 418 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test clear.", 419 sal_False == bRes ); 420 } 421 422 CPPUNIT_TEST_SUITE( clear ); 423 CPPUNIT_TEST( clear_001 ); 424 CPPUNIT_TEST_SUITE_END( ); 425 }; // class clear 426 427 428 /** testing the methods: 429 inline Pipe& SAL_CALL operator= (const Pipe& pipe); 430 inline Pipe& SAL_CALL operator= (const oslPipe pipe ); 431 */ 432 class assign : public CppUnit::TestFixture 433 { 434 public: 435 sal_Bool bRes, bRes1; 436 437 void assign_ref( ) 438 { 439 ::osl::Pipe aPipe, aPipe1; 440 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 441 aPipe1 = aPipe; 442 bRes = aPipe1.is( ); 443 bRes1 = aPipe == aPipe1; 444 aPipe.close( ); 445 aPipe1.close( ); 446 447 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.", 448 sal_True == bRes && sal_True == bRes1 ); 449 } 450 451 void assign_handle( ) 452 { 453 ::osl::Pipe aPipe, aPipe1; 454 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 455 aPipe1 = aPipe.getHandle( ); 456 bRes = aPipe1.is( ); 457 bRes1 = aPipe == aPipe1; 458 aPipe.close( ); 459 aPipe1.close( ); 460 461 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle.", 462 sal_True == bRes && sal_True == bRes1 ); 463 } 464 465 CPPUNIT_TEST_SUITE( assign ); 466 CPPUNIT_TEST( assign_ref ); 467 CPPUNIT_TEST( assign_handle ); 468 CPPUNIT_TEST_SUITE_END( ); 469 }; // class assign 470 471 472 /** testing the method: 473 inline sal_Bool SAL_CALL isValid() const; 474 isValid( ) has not been implemented under the following platforms, please refer to osl/pipe.hxx 475 */ 476 /*class isValid : public CppUnit::TestFixture 477 { 478 public: 479 sal_Bool bRes, bRes1; 480 481 void isValid_001( ) 482 { 483 CPPUNIT_ASSERT_MESSAGE( "#test comment#: isValid() has not been implemented on all platforms.", 484 sal_False ); 485 } 486 487 CPPUNIT_TEST_SUITE( isValid ); 488 CPPUNIT_TEST( isValid_001 ); 489 CPPUNIT_TEST_SUITE_END( ); 490 };*/ // class isValid 491 492 493 /** testing the method: 494 inline sal_Bool SAL_CALL operator==( const Pipe& rPipe ) const; 495 */ 496 class isEqual : public CppUnit::TestFixture 497 { 498 public: 499 sal_Bool bRes, bRes1; 500 501 void isEqual_001( ) 502 { 503 ::osl::Pipe aPipe; 504 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 505 bRes = aPipe == aPipe; 506 aPipe.close( ); 507 508 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(), compare its self.", 509 sal_True == bRes ); 510 } 511 512 void isEqual_002( ) 513 { 514 ::osl::Pipe aPipe, aPipe1, aPipe2; 515 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 516 517 aPipe1 = aPipe; 518 aPipe2.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 519 520 bRes = aPipe == aPipe1; 521 bRes1 = aPipe == aPipe2; 522 aPipe.close( ); 523 aPipe1.close( ); 524 aPipe2.close( ); 525 526 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(),create one copy instance, and compare.", 527 sal_True == bRes && sal_False == bRes1 ); 528 } 529 530 CPPUNIT_TEST_SUITE( isEqual ); 531 CPPUNIT_TEST( isEqual_001 ); 532 CPPUNIT_TEST( isEqual_002 ); 533 CPPUNIT_TEST_SUITE_END( ); 534 }; // class isEqual 535 536 537 /** testing the method: 538 inline void SAL_CALL close(); 539 */ 540 class close : public CppUnit::TestFixture 541 { 542 public: 543 sal_Bool bRes, bRes1; 544 545 void close_001( ) 546 { 547 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE ); 548 aPipe.close( ); 549 bRes = aPipe.is( ); 550 551 aPipe.clear( ); 552 bRes1 = aPipe.is( ); 553 554 CPPUNIT_ASSERT_MESSAGE( "#test comment#: difference between close and clear.", 555 sal_True == bRes && sal_False == bRes1); 556 } 557 558 void close_002( ) 559 { 560 ::osl::StreamPipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE ); 561 aPipe.close( ); 562 int nRet = aPipe.send( m_pTestString1.getStr(), 3 ); 563 564 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use after close.", 565 OSL_PIPE_FAIL == nRet ); 566 } 567 568 CPPUNIT_TEST_SUITE( close ); 569 CPPUNIT_TEST( close_001 ); 570 CPPUNIT_TEST( close_002 ); 571 CPPUNIT_TEST_SUITE_END( ); 572 }; // class close 573 574 575 /** testing the method: 576 inline oslPipeError SAL_CALL accept(StreamPipe& Connection); 577 please refer to StreamPipe::recv 578 */ 579 /* class accept : public CppUnit::TestFixture 580 { 581 public: 582 sal_Bool bRes, bRes1; 583 584 void accept_001( ) 585 { 586 587 // CPPUNIT_ASSERT_MESSAGE( "#test comment#: accept, untested.", 1 == 1 ); 588 //CPPUNIT_ASSERT_STUB(); 589 } 590 591 CPPUNIT_TEST_SUITE( accept ); 592 CPPUNIT_TEST( accept_001 ); 593 CPPUNIT_TEST_SUITE_END( ); 594 };*/ // class accept 595 596 597 /** testing the method: 598 inline oslPipeError SAL_CALL getError() const; 599 */ 600 class getError : public CppUnit::TestFixture 601 { 602 public: 603 sal_Bool bRes, bRes1; 604 /* 605 PipeError[]= { 606 { 0, osl_Pipe_E_None }, // no error 607 { EPROTOTYPE, osl_Pipe_E_NoProtocol }, // Protocol wrong type for socket 608 { ENOPROTOOPT, osl_Pipe_E_NoProtocol }, // Protocol not available 609 { EPROTONOSUPPORT, osl_Pipe_E_NoProtocol }, // Protocol not supported 610 { ESOCKTNOSUPPORT, osl_Pipe_E_NoProtocol }, // Socket type not supported 611 { EPFNOSUPPORT, osl_Pipe_E_NoProtocol }, // Protocol family not supported 612 { EAFNOSUPPORT, osl_Pipe_E_NoProtocol }, // Address family not supported by 613 // protocol family 614 { ENETRESET, osl_Pipe_E_NetworkReset }, // Network dropped connection because 615 // of reset 616 { ECONNABORTED, osl_Pipe_E_ConnectionAbort }, // Software caused connection abort 617 { ECONNRESET, osl_Pipe_E_ConnectionReset }, // Connection reset by peer 618 { ENOBUFS, osl_Pipe_E_NoBufferSpace }, // No buffer space available 619 { ETIMEDOUT, osl_Pipe_E_TimedOut }, // Connection timed out 620 { ECONNREFUSED, osl_Pipe_E_ConnectionRefused }, // Connection refused 621 { -1, osl_Pipe_E_invalidError } 622 }; 623 did not define osl_Pipe_E_NotFound, osl_Pipe_E_AlreadyExists 624 */ 625 626 void getError_001( ) 627 { 628 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); 629 oslPipeError nError = aPipe.getError( ); 630 printPipeError( aPipe ); 631 aPipe.clear( ); 632 633 CPPUNIT_ASSERT_MESSAGE( "#test comment#: open a non-exist pipe.", 634 nError != osl_Pipe_E_None ); 635 } 636 637 void getError_002( ) 638 { 639 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 640 ::osl::Pipe aPipe1( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 641 oslPipeError nError = aPipe.getError( ); 642 printPipeError( aPipe ); 643 aPipe.clear( ); 644 aPipe1.clear( ); 645 646 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create an already exist pipe.", 647 nError != osl_Pipe_E_None ); 648 } 649 650 CPPUNIT_TEST_SUITE( getError ); 651 CPPUNIT_TEST( getError_001 ); 652 CPPUNIT_TEST( getError_002 ); 653 CPPUNIT_TEST_SUITE_END( ); 654 }; // class getError 655 656 657 /** testing the method: 658 inline oslPipe SAL_CALL getHandle() const; 659 */ 660 class getHandle : public CppUnit::TestFixture 661 { 662 public: 663 sal_Bool bRes, bRes1; 664 665 void getHandle_001( ) 666 { 667 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); 668 bRes = aPipe == aPipe.getHandle( ); 669 aPipe.clear( ); 670 671 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe should equal to its handle.", 672 sal_True == bRes ); 673 } 674 675 void getHandle_002( ) 676 { 677 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 678 ::osl::Pipe aPipe1( aPipe.getHandle( ) ); 679 bRes = aPipe == aPipe1; 680 aPipe.clear( ); 681 aPipe1.clear( ); 682 683 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe derived from another pipe's handle.", 684 sal_True == bRes ); 685 } 686 687 CPPUNIT_TEST_SUITE( getHandle ); 688 CPPUNIT_TEST( getHandle_001 ); 689 CPPUNIT_TEST( getHandle_002 ); 690 CPPUNIT_TEST_SUITE_END( ); 691 }; // class getHandle 692 693 694 // ----------------------------------------------------------------------------- 695 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::ctors); 696 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::is); 697 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::create); 698 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::clear); 699 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::assign); 700 //CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::isValid); 701 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::isEqual); 702 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::close); 703 //CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::accept); 704 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::getError); 705 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::getHandle); 706 // ----------------------------------------------------------------------------- 707 708 } // namespace osl_Pipe 709 710 711 namespace osl_StreamPipe 712 { 713 714 /** testing the methods: 715 inline StreamPipe(); 716 inline StreamPipe(oslPipe Pipe);; 717 inline StreamPipe(const StreamPipe& Pipe); 718 inline StreamPipe(const ::rtl::OUString& strName, oslPipeOptions Options = osl_Pipe_OPEN); 719 inline StreamPipe(const ::rtl::OUString& strName, oslPipeOptions Options, const Security &rSec ); 720 inline StreamPipe( oslPipe pipe, __sal_NoAcquire noacquire ); 721 */ 722 class ctors : public CppUnit::TestFixture 723 { 724 public: 725 sal_Bool bRes, bRes1; 726 727 void ctors_none( ) 728 { 729 // create a pipe. 730 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 731 // create an unattached pipe. 732 ::osl::StreamPipe aStreamPipe1; 733 bRes = aStreamPipe1.is( ); 734 735 // assign it and check. 736 aStreamPipe1 = aStreamPipe; 737 bRes1 = aStreamPipe1.is( ); 738 aStreamPipe.clear( ); 739 aStreamPipe1.clear( ); 740 741 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, before and after assign.", 742 sal_False == bRes && sal_True == bRes1 ); 743 } 744 745 void ctors_handle( ) 746 { 747 // create a pipe. 748 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 749 // create a pipe with last handle. 750 ::osl::StreamPipe aStreamPipe1( aStreamPipe.getHandle( ) ); 751 bRes = aStreamPipe1.is( ) && aStreamPipe == aStreamPipe1; 752 aStreamPipe.clear( ); 753 aStreamPipe1.clear( ); 754 755 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with other's handle.", 756 sal_True == bRes ); 757 } 758 759 void ctors_copy( ) 760 { 761 // create a pipe. 762 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 763 // create an unattached pipe. 764 ::osl::StreamPipe aStreamPipe1( aStreamPipe ); 765 bRes = aStreamPipe1.is( ) && aStreamPipe == aStreamPipe1; 766 aStreamPipe.clear( ); 767 aStreamPipe1.clear( ); 768 769 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.", 770 sal_True == bRes ); 771 } 772 773 void ctors_name_option( ) 774 { 775 // create a pipe. 776 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 777 // create an unattached pipe. 778 ::osl::StreamPipe aStreamPipe1( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); 779 bRes = aStreamPipe1.is( ) && aStreamPipe.is( ); 780 aStreamPipe.clear( ); 781 aStreamPipe1.clear( ); 782 783 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.", 784 sal_True == bRes ); 785 } 786 787 void ctors_name_option_security( ) 788 { 789 /// create a security pipe. 790 const ::osl::Security rSecurity; 791 ::osl::StreamPipe aSecurityPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity ); 792 793 bRes = aSecurityPipe.is( ); 794 aSecurityPipe.clear( ); 795 796 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name, option and security, the test of security is not implemented yet.", 797 sal_True == bRes ); 798 } 799 800 /** tester comment: 801 802 When test the following constructor, don't know how to test the 803 acquire and no acquire action. possible plans: 804 1.release one handle and check the other( did not success since the 805 other still exist and valid. ) 806 2. release one handle twice to see getLastError( )(the getLastError 807 always returns invalidError(LINUX)). 808 */ 809 810 void ctors_no_acquire( ) 811 { 812 // create a pipe. 813 ::osl::StreamPipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 814 // constructs a pipe reference without acquiring the handle. 815 ::osl::StreamPipe aNoAcquirePipe( aPipe.getHandle( ), SAL_NO_ACQUIRE ); 816 817 bRes = aNoAcquirePipe.is( ); 818 aPipe.clear( ); 819 // bRes1 = aNoAcquirePipe.is( ); 820 821 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no aquire of handle, only validation test, do not know how to test no acquire.", 822 sal_True == bRes ); 823 } 824 825 CPPUNIT_TEST_SUITE( ctors ); 826 CPPUNIT_TEST( ctors_none ); 827 CPPUNIT_TEST( ctors_handle ); 828 CPPUNIT_TEST( ctors_copy ); 829 CPPUNIT_TEST( ctors_name_option ); 830 CPPUNIT_TEST( ctors_name_option_security ); 831 CPPUNIT_TEST( ctors_no_acquire ); 832 CPPUNIT_TEST_SUITE_END( ); 833 }; // class ctors 834 835 836 /** testing the methods: 837 inline StreamPipe & SAL_CALL operator=(oslPipe Pipe); 838 inline StreamPipe& SAL_CALL operator=(const Pipe& pipe); 839 mindy: not implementated in osl/pipe.hxx, so remove the cases 840 */ 841 /* 842 class assign : public CppUnit::TestFixture 843 { 844 public: 845 sal_Bool bRes, bRes1; 846 847 void assign_ref( ) 848 { 849 ::osl::StreamPipe aPipe, aPipe1; 850 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 851 aPipe1 = aPipe; 852 bRes = aPipe1.is( ); 853 bRes1 = aPipe == aPipe1; 854 aPipe.close( ); 855 aPipe1.close( ); 856 857 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.", 858 sal_True == bRes && sal_True == bRes1 ); 859 } 860 861 void assign_handle( ) 862 { 863 ::osl::StreamPipe * pPipe = new ::osl::StreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 864 ::osl::StreamPipe * pAssignPipe = new ::osl::StreamPipe; 865 *pAssignPipe = pPipe->getHandle( ); 866 867 bRes = pAssignPipe->is( ); 868 bRes1 = ( *pPipe == *pAssignPipe ); 869 pPipe->close( ); 870 871 delete pAssignPipe; 872 873 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle., seems not implemented under (LINUX)(W32)", 874 sal_True == bRes && sal_True == bRes1 ); 875 } 876 877 CPPUNIT_TEST_SUITE( assign ); 878 CPPUNIT_TEST( assign_ref ); 879 CPPUNIT_TEST( assign_handle ); 880 CPPUNIT_TEST_SUITE_END( ); 881 };*/ // class assign 882 883 884 /** wait _nSec seconds. 885 */ 886 void thread_sleep( sal_Int32 _nSec ) 887 { 888 /// print statement in thread process must use fflush() to force display. 889 // printf("wait %d seconds. ", _nSec ); 890 fflush(stdout); 891 892 #ifdef WNT //Windows 893 Sleep( _nSec * 1000 ); 894 #endif 895 #if ( defined UNX ) || ( defined OS2 ) //Unix 896 sleep( _nSec ); 897 #endif 898 // printf("done\n" ); 899 } 900 // test read/write & send/recv data to pipe 901 // ----------------------------------------------------------------------------- 902 903 class Pipe_DataSink_Thread : public Thread 904 { 905 public: 906 sal_Char buf[256]; 907 Pipe_DataSink_Thread( ) { } 908 909 ~Pipe_DataSink_Thread( ) 910 { 911 } 912 protected: 913 void SAL_CALL run( ) 914 { 915 sal_Int32 nChars = 0; 916 917 printf("open pipe\n"); 918 ::osl::StreamPipe aSenderPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); // test::uniquePipeName(aTestPipeName) is a string = "TestPipe" 919 if ( aSenderPipe.is() == sal_False ) 920 { 921 printf("pipe open failed! \n"); 922 } 923 else 924 { 925 printf("read\n"); 926 nChars = aSenderPipe.read( buf, m_pTestString1.getLength() + 1 ); 927 if ( nChars < 0 ) 928 { 929 printf("read failed! \n"); 930 return; 931 } 932 printf("buffer is %s \n", buf); 933 printf("send\n"); 934 nChars = aSenderPipe.send( m_pTestString2.getStr(), m_pTestString2.getLength() + 1 ); 935 if ( nChars < 0 ) 936 { 937 printf("client send failed! \n"); 938 return; 939 } 940 } 941 } 942 943 }; 944 945 // ----------------------------------------------------------------------------- 946 947 class Pipe_DataSource_Thread : public Thread 948 { 949 public: 950 sal_Char buf[256]; 951 //::osl::StreamPipe aListenPipe; //( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 952 ::osl::Pipe aListenPipe; 953 ::osl::StreamPipe aConnectionPipe; 954 Pipe_DataSource_Thread( ) 955 { 956 printf("create pipe\n"); 957 aListenPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 958 } 959 ~Pipe_DataSource_Thread( ) 960 { 961 aListenPipe.close(); 962 } 963 protected: 964 void SAL_CALL run( ) 965 { 966 //create pipe. 967 sal_Int32 nChars; 968 //::osl::StreamPipe aListenPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 969 printf("listen\n"); 970 if ( aListenPipe.is() == sal_False ) 971 { 972 printf("pipe create failed! \n"); 973 } 974 else 975 { 976 //::osl::StreamPipe aConnectionPipe; 977 978 //start server and wait for connection. 979 printf("accept\n"); 980 if ( osl_Pipe_E_None != aListenPipe.accept( aConnectionPipe ) ) 981 { 982 printf("pipe accept failed!"); 983 return; 984 } 985 printf("write\n"); 986 // write to pipe 987 nChars = aConnectionPipe.write( m_pTestString1.getStr(), m_pTestString1.getLength() + 1 ); 988 if ( nChars < 0) 989 { 990 printf("server write failed! \n"); 991 return; 992 } 993 printf("recv\n"); 994 nChars = aConnectionPipe.recv( buf, 256 ); 995 996 if ( nChars < 0) 997 { 998 printf("server receive failed! \n"); 999 return; 1000 } 1001 //thread_sleep( 2 ); 1002 printf("received message is: %s\n", buf ); 1003 //aConnectionPipe.close(); 1004 } 1005 } 1006 }; 1007 1008 /** testing the method: read/write/send/recv and Pipe::accept 1009 */ 1010 class recv : public CppUnit::TestFixture 1011 { 1012 public: 1013 sal_Bool bRes, bRes1; 1014 1015 void recv_001( ) 1016 { 1017 //launch threads. 1018 Pipe_DataSource_Thread myDataSourceThread; 1019 Pipe_DataSink_Thread myDataSinkThread; 1020 myDataSourceThread.create( ); 1021 thread_sleep( 1 ); 1022 myDataSinkThread.create( ); 1023 1024 //wait until the thread terminate 1025 myDataSinkThread.join( ); 1026 myDataSourceThread.join( ); 1027 1028 int nCompare1 = strcmp( myDataSinkThread.buf, m_pTestString1.getStr() ); 1029 int nCompare2 = strcmp( myDataSourceThread.buf, m_pTestString2.getStr() ); 1030 CPPUNIT_ASSERT_MESSAGE( "test send/recv/write/read.", nCompare1 == 0 && nCompare2 == 0 ); 1031 } 1032 //close pipe when accept 1033 void recv_002() 1034 { 1035 thread_sleep( 1 ); 1036 1037 Pipe_DataSource_Thread myDataSourceThread; 1038 Pipe_DataSink_Thread myDataSinkThread; 1039 myDataSourceThread.create( ); 1040 thread_sleep( 1 ); 1041 myDataSourceThread.aListenPipe.close(); 1042 myDataSourceThread.join( ); 1043 //no condition judgement here, if the case could finish excuting within 1 or 2 seconds, it passes. 1044 } 1045 1046 CPPUNIT_TEST_SUITE( recv ); 1047 CPPUNIT_TEST( recv_001 ); 1048 CPPUNIT_TEST( recv_002 ); 1049 CPPUNIT_TEST_SUITE_END( ); 1050 }; // class recv 1051 1052 // ----------------------------------------------------------------------------- 1053 CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::ctors); 1054 //CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::assign); 1055 CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::recv); 1056 // ----------------------------------------------------------------------------- 1057 1058 } // namespace osl_StreamPipe 1059 1060 CPPUNIT_PLUGIN_IMPLEMENT(); 1061