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 //------------------------------------------------------------------------ 28 // include files 29 //------------------------------------------------------------------------ 30 31 #include <sal/types.h> 32 #include <rtl/ustring.hxx> 33 34 #ifndef _OSL_THREAD_HXX 35 #include <osl/thread.hxx> 36 #endif 37 38 #ifndef _OSL_MUTEX_HXX 39 #include <osl/mutex.hxx> 40 #endif 41 42 #ifndef _OSL_MUTEX_HXX 43 #include <osl/pipe.hxx> 44 #endif 45 #include <osl/time.h> 46 #include <osl/process.h> 47 48 #ifdef UNX 49 #include <unistd.h> 50 #endif 51 #include <string.h> 52 53 #include "gtest/gtest.h" 54 55 using namespace osl; 56 using namespace rtl; 57 58 //------------------------------------------------------------------------ 59 // helper functions 60 //------------------------------------------------------------------------ 61 62 63 /** helper function for unique pipe names 64 */ 65 OUString uniquePipeName(OUString const & name) { 66 oslProcessInfo info; 67 info.Size = sizeof info; 68 EXPECT_TRUE( osl_Process_E_None == osl_getProcessInfo(0, osl_Process_IDENTIFIER, &info) ); 69 return name + OUString::valueOf((sal_Int32)info.Ident); 70 } 71 72 /** print Boolean value. 73 */ 74 inline void printBool( sal_Bool bOk ) 75 { 76 printf("#printBool# " ); 77 ( sal_True == bOk ) ? printf("YES!\n" ): printf("NO!\n" ); 78 } 79 80 /** print a UNI_CODE String. 81 */ 82 inline void printUString( const ::rtl::OUString & str ) 83 { 84 rtl::OString aString; 85 86 printf("#printUString_u# " ); 87 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ); 88 printf("%s\n", aString.getStr( ) ); 89 } 90 91 /** print last error of pipe system. 92 */ 93 inline void printPipeError( ::osl::Pipe aPipe ) 94 { 95 oslPipeError nError = aPipe.getError( ); 96 printf("#printPipeError# " ); 97 switch ( nError ) { 98 case osl_Pipe_E_None: 99 printf("Success!\n" ); 100 break; 101 case osl_Pipe_E_NotFound: 102 printf("The returned error is: Not found!\n" ); 103 break; 104 case osl_Pipe_E_AlreadyExists: 105 printf("The returned error is: Already exist!\n" ); 106 break; 107 case osl_Pipe_E_NoProtocol: 108 printf("The returned error is: No protocol!\n" ); 109 break; 110 case osl_Pipe_E_NetworkReset: 111 printf("The returned error is: Network reset!\n" ); 112 break; 113 case osl_Pipe_E_ConnectionAbort: 114 printf("The returned error is: Connection aborted!\n" ); 115 break; 116 case osl_Pipe_E_ConnectionReset: 117 printf("The returned error is: Connection reset!\n" ); 118 break; 119 case osl_Pipe_E_NoBufferSpace: 120 printf("The returned error is: No buffer space!\n" ); 121 break; 122 case osl_Pipe_E_TimedOut: 123 printf("The returned error is: Timeout!\n" ); 124 break; 125 case osl_Pipe_E_ConnectionRefused: 126 printf("The returned error is: Connection refused!\n" ); 127 break; 128 case osl_Pipe_E_invalidError: 129 printf("The returned error is: Invalid error!\n" ); 130 break; 131 default: 132 printf("The returned error is: Number %d, Unknown Error\n", nError ); 133 break; 134 } 135 } 136 137 138 139 //------------------------------------------------------------------------ 140 // pipe name and transfer contents 141 //------------------------------------------------------------------------ 142 const rtl::OUString aTestPipeName = rtl::OUString::createFromAscii( "testpipe2" ); 143 const rtl::OUString aTestPipe1 = rtl::OUString::createFromAscii( "testpipe1" ); 144 const rtl::OUString aTestString = rtl::OUString::createFromAscii( "Apache Software Foundation" ); 145 146 const OString m_pTestString1("Apache Software Foundation"); 147 const OString m_pTestString2("test pipe PASS/OK"); 148 149 //------------------------------------------------------------------------ 150 // test code start here 151 //------------------------------------------------------------------------ 152 153 namespace osl_Pipe 154 { 155 156 //------------------------------------------------------------------------ 157 // most return value -1 denote a fail of operation. 158 //------------------------------------------------------------------------ 159 #define OSL_PIPE_FAIL -1 160 161 /** testing the methods: 162 inline Pipe(); 163 inline Pipe(const ::rtl::OUString& strName, oslPipeOptions Options); 164 inline Pipe(const ::rtl::OUString& strName, oslPipeOptions Options,const Security & rSecurity); 165 inline Pipe(const Pipe& pipe); 166 inline Pipe(oslPipe pipe, __sal_NoAcquire noacquire ); 167 inline Pipe(oslPipe Pipe); 168 */ 169 TEST(Sal_Test_Pipe, ctors_none) { 170 ::osl::Pipe aPipe; 171 sal_Bool bRes = aPipe.is( ); 172 173 // #test comment#: test constructor with no parameter, yet no case to test. 174 ASSERT_TRUE( !bRes ); 175 } 176 177 TEST(Sal_Test_Pipe, ctors_name_option) { 178 /// create a named pipe. 179 ::osl::Pipe aPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 180 ::osl::Pipe aAssignPipe( uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); 181 182 sal_Bool bRes = aPipe.is( ) && aAssignPipe.is( ); 183 184 // #test comment#: test constructor with name and option. 185 ASSERT_TRUE( bRes ); 186 } 187 188 TEST(Sal_Test_Pipe, ctors_name_option_security) { 189 /// create a security pipe. 190 const ::osl::Security rSecurity; 191 ::osl::Pipe aSecurityPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity ); 192 193 sal_Bool bRes = aSecurityPipe.is( ); 194 195 // #test comment#: test constructor with name, option and security, the test of security is not implemented yet. 196 ASSERT_TRUE( bRes ); 197 } 198 199 TEST(Sal_Test_Pipe, ctors_copy) { 200 /// create a pipe. 201 ::osl::Pipe aPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 202 /// create a pipe using copy constructor. 203 ::osl::Pipe aCopyPipe( aPipe ); 204 205 sal_Bool bRes = aCopyPipe.is( ) && aCopyPipe == aPipe; 206 207 // #test comment#: test copy constructor. 208 ASSERT_TRUE( bRes ); 209 } 210 211 212 /** tester comment: 213 214 When test the following two constructors, don't know how to test the 215 acquire and no acquire action. possible plans: 216 1.release one handle and check the other( did not success since the 217 other still exist and valid. ) 218 2. release one handle twice to see getLastError( )(the getLastError 219 always returns invalidError(LINUX)). 220 */ 221 TEST(Sal_Test_Pipe, ctors_no_acquire) { 222 /// create a pipe. 223 ::osl::Pipe aPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 224 /// constructs a pipe reference without acquiring the handle. 225 ::osl::Pipe aNoAcquirePipe( aPipe.getHandle( ), SAL_NO_ACQUIRE ); 226 227 sal_Bool bRes = aNoAcquirePipe.is( ); 228 ///aPipe.clear( ); 229 ///bRes1 = aNoAcquirePipe.is( ); 230 231 232 // #test comment#: test constructor with no acquire of handle, only validation test, do not know how to test no acquire. 233 ASSERT_TRUE( bRes ); 234 } 235 236 TEST(Sal_Test_Pipe, ctors_acquire) { 237 /// create a base pipe. 238 ::osl::Pipe aPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 239 /// constructs two pipes without acquiring the handle on the base pipe. 240 ::osl::Pipe aAcquirePipe( aPipe.getHandle( ) ); 241 ::osl::Pipe aAcquirePipe1( NULL ); 242 243 sal_Bool bRes = aAcquirePipe.is( ); 244 sal_Bool bRes1 = aAcquirePipe1.is( ); 245 246 // #test comment#: test constructor with no acquire of handle.only validation test, do not know how to test no acquire. 247 ASSERT_TRUE( bRes && !bRes1 ); 248 } 249 250 251 /** testing the method: 252 inline sal_Bool SAL_CALL is() const; 253 */ 254 TEST(Sal_Test_Pipe, is_001) { 255 ::osl::Pipe aPipe; 256 257 // #test comment#: test is(), check if the pipe is a valid one. 258 ASSERT_TRUE( !aPipe.is( ) ); 259 } 260 261 TEST(Sal_Test_Pipe, is_002) { 262 ::osl::Pipe aPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 263 264 // #test comment#: test is(), a normal pipe creation. 265 ASSERT_TRUE( aPipe.is( ) ); 266 } 267 268 TEST(Sal_Test_Pipe, is_003) { 269 ::osl::Pipe aPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 270 aPipe.clear( ); 271 272 // #test comment#: test is(), an invalid case 273 ASSERT_TRUE( !aPipe.is( ) ); 274 } 275 276 TEST(Sal_Test_Pipe, is_004) { 277 ::osl::Pipe aPipe( NULL ); 278 279 // #test comment#: test is(), an invalid constructor. 280 ASSERT_TRUE( !aPipe.is( ) ); 281 } 282 283 284 /** testing the methods: 285 inline sal_Bool create( const ::rtl::OUString & strName, 286 oslPipeOptions Options, const Security &rSec ); 287 nline sal_Bool create( const ::rtl::OUString & strName, 288 oslPipeOptions Options = osl_Pipe_OPEN ); 289 */ 290 TEST(Sal_Test_Pipe, create_named_security_001) { 291 const Security rSec; 292 ::osl::Pipe aPipe; 293 sal_Bool bRes = aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec ); 294 sal_Bool bRes1 = aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec ); 295 aPipe.clear( ); 296 297 // #test comment#: test creation. 298 ASSERT_TRUE( bRes && !bRes1 ); 299 } 300 301 TEST(Sal_Test_Pipe, create_named_security_002) { 302 const Security rSec; 303 ::osl::Pipe aPipe, aPipe1; 304 sal_Bool bRes = aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec ); 305 sal_Bool bRes1 = aPipe1.create( uniquePipeName(aTestPipeName), osl_Pipe_OPEN, rSec ); 306 aPipe.clear( ); 307 308 // #test comment#: test creation and open. 309 ASSERT_TRUE( bRes && bRes1 ); 310 } 311 312 TEST(Sal_Test_Pipe, create_named_001) { 313 ::osl::Pipe aPipe; 314 sal_Bool bRes = aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 315 sal_Bool bRes1 = aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 316 aPipe.clear( ); 317 318 // #test comment#: test creation. 319 ASSERT_TRUE( bRes && !bRes1); 320 } 321 322 TEST(Sal_Test_Pipe, create_named_002) { 323 ::osl::Pipe aPipe, aPipe1; 324 sal_Bool bRes = aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 325 sal_Bool bRes1 = aPipe1.create( uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); 326 aPipe.clear( ); 327 328 // #test comment#: test creation and open. 329 ASSERT_TRUE( bRes && bRes1); 330 } 331 332 TEST(Sal_Test_Pipe, create_named_003) { 333 ::osl::Pipe aPipe; 334 sal_Bool bRes = aPipe.create( uniquePipeName(aTestPipeName) ); 335 aPipe.clear( ); 336 337 // #test comment#: test default option is open. 338 ASSERT_TRUE( !bRes ); 339 } 340 341 342 /** testing the method: 343 inline void SAL_CALL clear(); 344 */ 345 TEST(Sal_Test_Pipe, clear_001) { 346 ::osl::Pipe aPipe; 347 aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 348 aPipe.clear( ); 349 sal_Bool bRes = aPipe.is( ); 350 351 // #test comment#: test clear. 352 ASSERT_TRUE( !bRes ); 353 } 354 355 356 /** testing the methods: 357 inline Pipe& SAL_CALL operator= (const Pipe& pipe); 358 inline Pipe& SAL_CALL operator= (const oslPipe pipe ); 359 */ 360 TEST(Sal_Test_Pipe, assign_ref) { 361 ::osl::Pipe aPipe, aPipe1; 362 aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 363 aPipe1 = aPipe; 364 sal_Bool bRes = aPipe1.is( ); 365 sal_Bool bRes1 = aPipe == aPipe1; 366 aPipe.close( ); 367 aPipe1.close( ); 368 369 // #test comment#: test assign with reference. 370 ASSERT_TRUE( bRes && bRes1 ); 371 } 372 373 TEST(Sal_Test_Pipe, assign_handle) { 374 ::osl::Pipe aPipe, aPipe1; 375 aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 376 aPipe1 = aPipe.getHandle( ); 377 sal_Bool bRes = aPipe1.is( ); 378 sal_Bool bRes1 = aPipe == aPipe1; 379 aPipe.close( ); 380 aPipe1.close( ); 381 382 // #test comment#: test assign with handle. 383 ASSERT_TRUE( bRes && bRes1 ); 384 } 385 386 387 /** testing the method: 388 inline sal_Bool SAL_CALL isValid() const; 389 isValid( ) has not been implemented under the following platforms, please refer to osl/pipe.hxx 390 */ 391 // TEST(Sal_Test_Pipe, isValid_001) { 392 // ... 393 // // #test comment#: isValid() has not been implemented on all platforms. 394 // ASSERT_TRUE( ... ); 395 // } 396 397 398 /** testing the method: 399 inline sal_Bool SAL_CALL operator==( const Pipe& rPipe ) const; 400 */ 401 TEST(Sal_Test_Pipe, isEqual_001) { 402 ::osl::Pipe aPipe; 403 aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 404 sal_Bool bRes = aPipe == aPipe; 405 aPipe.close( ); 406 407 // #test comment#: test isEqual(), compare its self. 408 ASSERT_TRUE( bRes ); 409 } 410 411 TEST(Sal_Test_Pipe, isEqual_002) { 412 ::osl::Pipe aPipe, aPipe1, aPipe2; 413 aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 414 aPipe1 = aPipe; 415 aPipe2.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 416 417 sal_Bool bRes = aPipe == aPipe1; 418 sal_Bool bRes1 = aPipe == aPipe2; 419 aPipe.close( ); 420 aPipe1.close( ); 421 aPipe2.close( ); 422 423 // #test comment#: test isEqual(),create one copy instance, and compare. 424 ASSERT_TRUE( bRes && !bRes1 ); 425 } 426 427 428 /** testing the method: 429 inline void SAL_CALL close(); 430 */ 431 TEST(Sal_Test_Pipe, close_001) { 432 ::osl::Pipe aPipe( uniquePipeName(aTestPipe1), osl_Pipe_CREATE ); 433 aPipe.close( ); 434 sal_Bool bRes = aPipe.is( ); 435 436 aPipe.clear( ); 437 sal_Bool bRes1 = aPipe.is( ); 438 439 // #test comment#: difference between close and clear. 440 ASSERT_TRUE( bRes && !bRes1); 441 } 442 443 TEST(Sal_Test_Pipe, close_002) { 444 ::osl::StreamPipe aPipe( uniquePipeName(aTestPipe1), osl_Pipe_CREATE ); 445 aPipe.close( ); 446 int nRet = aPipe.send( m_pTestString1.getStr(), 3 ); 447 448 // #test comment#: use after close. 449 ASSERT_EQ( nRet, OSL_PIPE_FAIL ); 450 } 451 452 453 /** testing the method: 454 inline oslPipeError SAL_CALL accept(StreamPipe& Connection); 455 please refer to StreamPipe::recv 456 */ 457 458 459 /** testing the method: 460 inline oslPipeError SAL_CALL getError() const; 461 */ 462 /* 463 PipeError[]= { 464 { 0, osl_Pipe_E_None }, // no error 465 { EPROTOTYPE, osl_Pipe_E_NoProtocol }, // Protocol wrong type for socket 466 { ENOPROTOOPT, osl_Pipe_E_NoProtocol }, // Protocol not available 467 { EPROTONOSUPPORT, osl_Pipe_E_NoProtocol }, // Protocol not supported 468 { ESOCKTNOSUPPORT, osl_Pipe_E_NoProtocol }, // Socket type not supported 469 { EPFNOSUPPORT, osl_Pipe_E_NoProtocol }, // Protocol family not supported 470 { EAFNOSUPPORT, osl_Pipe_E_NoProtocol }, // Address family not supported by 471 // protocol family 472 { ENETRESET, osl_Pipe_E_NetworkReset }, // Network dropped connection because 473 // of reset 474 { ECONNABORTED, osl_Pipe_E_ConnectionAbort }, // Software caused connection abort 475 { ECONNRESET, osl_Pipe_E_ConnectionReset }, // Connection reset by peer 476 { ENOBUFS, osl_Pipe_E_NoBufferSpace }, // No buffer space available 477 { ETIMEDOUT, osl_Pipe_E_TimedOut }, // Connection timed out 478 { ECONNREFUSED, osl_Pipe_E_ConnectionRefused }, // Connection refused 479 { -1, osl_Pipe_E_invalidError } 480 }; 481 did not define osl_Pipe_E_NotFound, osl_Pipe_E_AlreadyExists 482 */ 483 TEST(Sal_Test_Pipe, getError_001) { 484 ::osl::Pipe aPipe( uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); 485 oslPipeError nError = aPipe.getError( ); 486 printPipeError( aPipe ); 487 aPipe.clear( ); 488 489 // #test comment#: open a non-exist pipe. 490 ASSERT_NE( nError, osl_Pipe_E_None ); 491 } 492 493 TEST(Sal_Test_Pipe, getError_002) { 494 ::osl::Pipe aPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 495 ::osl::Pipe aPipe1( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 496 oslPipeError nError = aPipe.getError( ); 497 printPipeError( aPipe ); 498 aPipe.clear( ); 499 aPipe1.clear( ); 500 501 // #test comment#: create an already exist pipe. 502 ASSERT_NE( nError, osl_Pipe_E_None ); 503 } 504 505 506 /** testing the method: 507 inline oslPipe SAL_CALL getHandle() const; 508 */ 509 TEST(Sal_Test_Pipe, getHandle_001) { 510 ::osl::Pipe aPipe( uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); 511 sal_Bool bRes = aPipe == aPipe.getHandle( ); 512 aPipe.clear( ); 513 514 // #test comment#: one pipe should equal to its handle. 515 ASSERT_TRUE( bRes ); 516 } 517 518 TEST(Sal_Test_Pipe, getHandle_002) { 519 ::osl::Pipe aPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 520 ::osl::Pipe aPipe1( aPipe.getHandle( ) ); 521 sal_Bool bRes = aPipe == aPipe1; 522 aPipe.clear( ); 523 aPipe1.clear( ); 524 525 // #test comment#: one pipe derived from another pipe's handle. 526 ASSERT_TRUE( bRes ); 527 } 528 529 } // namespace osl_Pipe 530 531 532 namespace osl_StreamPipe 533 { 534 535 /** testing the methods: 536 inline StreamPipe(); 537 inline StreamPipe(oslPipe Pipe);; 538 inline StreamPipe(const StreamPipe& Pipe); 539 inline StreamPipe(const ::rtl::OUString& strName, oslPipeOptions Options = osl_Pipe_OPEN); 540 inline StreamPipe(const ::rtl::OUString& strName, oslPipeOptions Options, const Security &rSec ); 541 inline StreamPipe( oslPipe pipe, __sal_NoAcquire noacquire ); 542 */ 543 544 TEST(Sal_Test_StreamPipe, ctors_none) { 545 // create a pipe. 546 ::osl::StreamPipe aStreamPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 547 // create an unattached pipe. 548 ::osl::StreamPipe aStreamPipe1; 549 sal_Bool bRes = aStreamPipe1.is( ); 550 551 // assign it and check. 552 aStreamPipe1 = aStreamPipe; 553 sal_Bool bRes1 = aStreamPipe1.is( ); 554 aStreamPipe.clear( ); 555 aStreamPipe1.clear( ); 556 557 // #test comment#: test constructor with no parameter, before and after assign. 558 ASSERT_TRUE( !bRes && bRes1 ); 559 } 560 561 TEST(Sal_Test_StreamPipe, ctors_handle) { 562 // create a pipe. 563 ::osl::StreamPipe aStreamPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 564 // create a pipe with last handle. 565 ::osl::StreamPipe aStreamPipe1( aStreamPipe.getHandle( ) ); 566 sal_Bool bRes = aStreamPipe1.is( ) && aStreamPipe == aStreamPipe1; 567 aStreamPipe.clear( ); 568 aStreamPipe1.clear( ); 569 570 // #test comment#: test constructor with other's handle. 571 ASSERT_TRUE( bRes ); 572 } 573 574 TEST(Sal_Test_StreamPipe, ctors_copy) { 575 // create a pipe. 576 ::osl::StreamPipe aStreamPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 577 // create an unattached pipe. 578 ::osl::StreamPipe aStreamPipe1( aStreamPipe ); 579 sal_Bool bRes = aStreamPipe1.is( ) && aStreamPipe == aStreamPipe1; 580 aStreamPipe.clear( ); 581 aStreamPipe1.clear( ); 582 583 // #test comment#: test copy constructor. 584 ASSERT_TRUE( bRes ); 585 } 586 587 TEST(Sal_Test_StreamPipe, ctors_name_option) { 588 // create a pipe. 589 ::osl::StreamPipe aStreamPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 590 // create an unattached pipe. 591 ::osl::StreamPipe aStreamPipe1( uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); 592 sal_Bool bRes = aStreamPipe1.is( ) && aStreamPipe.is( ); 593 aStreamPipe.clear( ); 594 aStreamPipe1.clear( ); 595 596 // #test comment#: test constructor with name and option. 597 ASSERT_TRUE( bRes ); 598 } 599 600 TEST(Sal_Test_StreamPipe, ctors_name_option_security) { 601 // create a security pipe. 602 const ::osl::Security rSecurity; 603 ::osl::StreamPipe aSecurityPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity ); 604 605 sal_Bool bRes = aSecurityPipe.is( ); 606 aSecurityPipe.clear( ); 607 608 // #test comment#: test constructor with name, option and security, the test of security is not implemented yet. 609 ASSERT_TRUE( bRes ); 610 } 611 612 /** tester comment: 613 614 When test the following constructor, don't know how to test the 615 acquire and no acquire action. possible plans: 616 1.release one handle and check the other( did not success since the 617 other still exist and valid. ) 618 2. release one handle twice to see getLastError( )(the getLastError 619 always returns invalidError(LINUX)). 620 */ 621 TEST(Sal_Test_StreamPipe, ctors_no_acquire) { 622 // create a pipe. 623 ::osl::StreamPipe aPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 624 // constructs a pipe reference without acquiring the handle. 625 ::osl::StreamPipe aNoAcquirePipe( aPipe.getHandle( ), SAL_NO_ACQUIRE ); 626 627 sal_Bool bRes = aNoAcquirePipe.is( ); 628 aPipe.clear( ); 629 // bRes1 = aNoAcquirePipe.is( ); 630 631 // #test comment#: test constructor with no acquire of handle, only validation test, do not know how to test no acquire. 632 ASSERT_TRUE( bRes ); 633 } 634 635 /** testing the methods: 636 inline StreamPipe & SAL_CALL operator=(oslPipe Pipe); 637 inline StreamPipe& SAL_CALL operator=(const Pipe& pipe); 638 mindy: not implementated in osl/pipe.hxx, so remove the cases 639 */ 640 // TEST(Sal_Test_StreamPipe, assign_ref) { 641 // ::osl::StreamPipe aPipe, aPipe1; 642 // aPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 643 // aPipe1 = aPipe; 644 // sal_Bool bRes = aPipe1.is( ); 645 // sal_Bool bRes1 = aPipe == aPipe1; 646 // aPipe.close( ); 647 // aPipe1.close( ); 648 649 // // #test comment#: test assign with reference. 650 // ASSERT_TRUE( bRes && bRes1 ); 651 // } 652 653 // TEST(Sal_Test_StreamPipe, assign_ref) { 654 // ::osl::StreamPipe * pPipe = new ::osl::StreamPipe( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 655 // ::osl::StreamPipe * pAssignPipe = new ::osl::StreamPipe; 656 // *pAssignPipe = pPipe->getHandle( ); 657 658 // bRes = pAssignPipe->is( ); 659 // bRes1 = ( *pPipe == *pAssignPipe ); 660 // pPipe->close( ); 661 662 // delete pAssignPipe; 663 664 // // #test comment#: test assign with handle., seems not implemented under (LINUX)(W32) 665 // ASSERT_TRUE( bRes && bRes1 ); 666 // } 667 668 669 /** wait _nSec seconds. 670 */ 671 void thread_sleep( sal_Int32 _nSec ) 672 { 673 /// print statement in thread process must use fflush() to force display. 674 // printf("wait %d seconds. ", _nSec ); 675 fflush(stdout); 676 677 #ifdef WNT //Windows 678 Sleep( _nSec * 1000 ); 679 #endif 680 #if ( defined UNX ) || ( defined OS2 ) //Unix 681 sleep( _nSec ); 682 #endif 683 // printf("done\n" ); 684 } 685 686 // test read/write & send/recv data to pipe 687 // ----------------------------------------------------------------------------- 688 689 class Pipe_DataSink_Thread : public Thread 690 { 691 public: 692 sal_Char buf[256]; 693 Pipe_DataSink_Thread( ) { } 694 695 ~Pipe_DataSink_Thread( ) 696 { 697 } 698 protected: 699 void SAL_CALL run( ) 700 { 701 sal_Int32 nChars = 0; 702 703 printf("open pipe\n"); 704 // uniquePipeName(aTestPipeName) is a string = "TestPipe" 705 ::osl::StreamPipe aSenderPipe( uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); 706 if ( aSenderPipe.is() == sal_False ) 707 { 708 printf("pipe open failed! \n"); 709 } 710 else 711 { 712 printf("read\n"); 713 nChars = aSenderPipe.read( buf, m_pTestString1.getLength() + 1 ); 714 if ( nChars < 0 ) 715 { 716 printf("read failed! \n"); 717 return; 718 } 719 printf("buffer is %s \n", buf); 720 printf("send\n"); 721 nChars = aSenderPipe.send( m_pTestString2.getStr(), m_pTestString2.getLength() + 1 ); 722 if ( nChars < 0 ) 723 { 724 printf("client send failed! \n"); 725 return; 726 } 727 } 728 } 729 }; 730 731 // ----------------------------------------------------------------------------- 732 733 class Pipe_DataSource_Thread : public Thread 734 { 735 public: 736 sal_Char buf[256]; 737 //::osl::StreamPipe aListenPipe; //( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 738 ::osl::Pipe aListenPipe; 739 ::osl::StreamPipe aConnectionPipe; 740 Pipe_DataSource_Thread( ) 741 { 742 printf("create pipe\n"); 743 aListenPipe.create( uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 744 } 745 ~Pipe_DataSource_Thread( ) 746 { 747 aListenPipe.close(); 748 } 749 protected: 750 void SAL_CALL run( ) 751 { 752 //create pipe. 753 sal_Int32 nChars; 754 //::osl::StreamPipe aListenPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE ); 755 printf("listen\n"); 756 if ( aListenPipe.is() == sal_False ) 757 { 758 printf("pipe create failed! \n"); 759 } 760 else 761 { 762 //::osl::StreamPipe aConnectionPipe; 763 764 //start server and wait for connection. 765 printf("accept\n"); 766 if ( osl_Pipe_E_None != aListenPipe.accept( aConnectionPipe ) ) 767 { 768 printf("pipe accept failed!"); 769 return; 770 } 771 printf("write\n"); 772 // write to pipe 773 nChars = aConnectionPipe.write( m_pTestString1.getStr(), m_pTestString1.getLength() + 1 ); 774 if ( nChars < 0) 775 { 776 printf("server write failed! \n"); 777 return; 778 } 779 printf("recv\n"); 780 nChars = aConnectionPipe.recv( buf, 256 ); 781 782 if ( nChars < 0) 783 { 784 printf("server receive failed! \n"); 785 return; 786 } 787 //thread_sleep( 2 ); 788 printf("received message is: %s\n", buf ); 789 //aConnectionPipe.close(); 790 } 791 } 792 }; 793 794 795 /** testing the method: read/write/send/recv and Pipe::accept 796 */ 797 TEST(Sal_Test_StreamPipe, recv_001) { 798 //launch threads. 799 Pipe_DataSource_Thread myDataSourceThread; 800 Pipe_DataSink_Thread myDataSinkThread; 801 myDataSourceThread.create( ); 802 thread_sleep( 1 ); 803 myDataSinkThread.create( ); 804 805 //wait until the thread terminate 806 myDataSinkThread.join( ); 807 myDataSourceThread.join( ); 808 809 int nCompare1 = strcmp( myDataSinkThread.buf, m_pTestString1.getStr() ); 810 int nCompare2 = strcmp( myDataSourceThread.buf, m_pTestString2.getStr() ); 811 812 // test send/recv/write/read. 813 ASSERT_TRUE( nCompare1 == 0 && nCompare2 == 0 ); 814 } 815 816 } // namespace osl_StreamPipe 817 818 int main(int argc, char **argv) 819 { 820 ::testing::InitGoogleTest(&argc, argv); 821 return RUN_ALL_TESTS(); 822 } 823