merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / qa / osl / pipe / osl_Pipe.cxx
blobffb9f314a5bf7235336411d16d401e536802ef08
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
31 //------------------------------------------------------------------------
32 // include files
33 //------------------------------------------------------------------------
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>
43 #ifndef _OSL_THREAD_HXX
44 #include <osl/thread.hxx>
45 #endif
47 #ifndef _OSL_MUTEX_HXX
48 #include <osl/mutex.hxx>
49 #endif
51 #ifndef _OSL_MUTEX_HXX
52 #include <osl/pipe.hxx>
53 #endif
54 #include <osl/time.h>
56 #ifdef UNX
57 #include <unistd.h>
58 #endif
59 #include <string.h>
61 using namespace osl;
62 using namespace rtl;
64 //------------------------------------------------------------------------
65 // helper functions
66 //------------------------------------------------------------------------
68 /** print Boolean value.
70 inline void printBool( sal_Bool bOk )
72 printf("#printBool# " );
73 ( sal_True == bOk ) ? printf("YES!\n" ): printf("NO!\n" );
76 /** print a UNI_CODE String.
78 inline void printUString( const ::rtl::OUString & str )
80 rtl::OString aString;
82 printf("#printUString_u# " );
83 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
84 printf("%s\n", aString.getStr( ) );
87 /** print last error of pipe system.
89 inline void printPipeError( ::osl::Pipe aPipe )
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;
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" );
142 const OString m_pTestString1("Sun Microsystems");
143 const OString m_pTestString2("test pipe PASS/OK");
145 //------------------------------------------------------------------------
146 // test code start here
147 //------------------------------------------------------------------------
149 namespace osl_Pipe
152 //------------------------------------------------------------------------
153 // most return value -1 denote a fail of operation.
154 //------------------------------------------------------------------------
155 #define OSL_PIPE_FAIL -1
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);
165 class ctors : public CppUnit::TestFixture
167 public:
168 sal_Bool bRes, bRes1;
170 void setUp( )
174 void tearDown( )
178 void ctors_none( )
180 ::osl::Pipe aPipe;
181 bRes = aPipe.is( );
183 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, yet no case to test.",
184 sal_False == bRes );
187 void ctors_name_option( )
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 );
193 bRes = aPipe.is( ) && aAssignPipe.is( );
195 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.",
196 sal_True == bRes );
199 void ctors_name_option_security( )
201 /// create a security pipe.
202 const ::osl::Security rSecurity;
203 ::osl::Pipe aSecurityPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity );
205 bRes = aSecurityPipe.is( );
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 );
211 void ctors_copy( )
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 );
218 bRes = aCopyPipe.is( ) && aCopyPipe == aPipe;
220 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.",
221 sal_True == bRes );
224 /** tester comment:
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)).
234 void ctors_no_acquire( )
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 );
241 bRes = aNoAcquirePipe.is( );
242 ///aPipe.clear( );
243 ///bRes1 = aNoAcquirePipe.is( );
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 );
250 void ctors_acquire( )
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 );
258 bRes = aAcquirePipe.is( );
259 bRes1 = aAcquirePipe1.is( );
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 );
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
276 /** testing the method:
277 inline sal_Bool SAL_CALL is() const;
279 class is : public CppUnit::TestFixture
281 public:
282 void is_001( )
284 ::osl::Pipe aPipe;
286 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), check if the pipe is a valid one.", sal_False == aPipe.is( ) );
289 void is_002( )
291 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
293 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), a normal pipe creation.", sal_True == aPipe.is( ) );
296 void is_003( )
298 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
299 aPipe.clear( );
301 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid case.", sal_False == aPipe.is( ) );
304 void is_004( )
306 ::osl::Pipe aPipe( NULL );
308 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid constructor.", sal_False == aPipe.is( ) );
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
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 );
326 class create : public CppUnit::TestFixture
328 public:
329 sal_Bool bRes, bRes1;
331 /** tester comment:
333 security create only be tested creation, security section is
334 untested yet.
337 void create_named_security_001( )
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( );
345 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
346 sal_True == bRes && sal_False == bRes1);
349 void create_named_security_002( )
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( );
357 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
358 sal_True == bRes && sal_True == bRes1);
361 void create_named_001( )
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( );
368 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
369 sal_True == bRes && sal_False == bRes1);
372 void create_named_002( )
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( );
379 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
380 sal_True == bRes && sal_True == bRes1);
383 void create_named_003( )
385 ::osl::Pipe aPipe;
386 bRes = aPipe.create( test::uniquePipeName(aTestPipeName) );
387 aPipe.clear( );
389 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test default option is open.",
390 sal_False == bRes );
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
403 /** testing the method:
404 inline void SAL_CALL clear();
406 class clear : public CppUnit::TestFixture
408 public:
409 sal_Bool bRes, bRes1;
411 void clear_001( )
413 ::osl::Pipe aPipe;
414 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
415 aPipe.clear( );
416 bRes = aPipe.is( );
418 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test clear.",
419 sal_False == bRes );
422 CPPUNIT_TEST_SUITE( clear );
423 CPPUNIT_TEST( clear_001 );
424 CPPUNIT_TEST_SUITE_END( );
425 }; // class clear
428 /** testing the methods:
429 inline Pipe& SAL_CALL operator= (const Pipe& pipe);
430 inline Pipe& SAL_CALL operator= (const oslPipe pipe );
432 class assign : public CppUnit::TestFixture
434 public:
435 sal_Bool bRes, bRes1;
437 void assign_ref( )
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( );
447 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.",
448 sal_True == bRes && sal_True == bRes1 );
451 void assign_handle( )
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( );
461 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle.",
462 sal_True == bRes && sal_True == bRes1 );
465 CPPUNIT_TEST_SUITE( assign );
466 CPPUNIT_TEST( assign_ref );
467 CPPUNIT_TEST( assign_handle );
468 CPPUNIT_TEST_SUITE_END( );
469 }; // class assign
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
476 /*class isValid : public CppUnit::TestFixture
478 public:
479 sal_Bool bRes, bRes1;
481 void isValid_001( )
483 CPPUNIT_ASSERT_MESSAGE( "#test comment#: isValid() has not been implemented on all platforms.",
484 sal_False );
487 CPPUNIT_TEST_SUITE( isValid );
488 CPPUNIT_TEST( isValid_001 );
489 CPPUNIT_TEST_SUITE_END( );
490 };*/ // class isValid
493 /** testing the method:
494 inline sal_Bool SAL_CALL operator==( const Pipe& rPipe ) const;
496 class isEqual : public CppUnit::TestFixture
498 public:
499 sal_Bool bRes, bRes1;
501 void isEqual_001( )
503 ::osl::Pipe aPipe;
504 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
505 bRes = aPipe == aPipe;
506 aPipe.close( );
508 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(), compare its self.",
509 sal_True == bRes );
512 void isEqual_002( )
514 ::osl::Pipe aPipe, aPipe1, aPipe2;
515 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
517 aPipe1 = aPipe;
518 aPipe2.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
520 bRes = aPipe == aPipe1;
521 bRes1 = aPipe == aPipe2;
522 aPipe.close( );
523 aPipe1.close( );
524 aPipe2.close( );
526 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(),create one copy instance, and compare.",
527 sal_True == bRes && sal_False == bRes1 );
530 CPPUNIT_TEST_SUITE( isEqual );
531 CPPUNIT_TEST( isEqual_001 );
532 CPPUNIT_TEST( isEqual_002 );
533 CPPUNIT_TEST_SUITE_END( );
534 }; // class isEqual
537 /** testing the method:
538 inline void SAL_CALL close();
540 class close : public CppUnit::TestFixture
542 public:
543 sal_Bool bRes, bRes1;
545 void close_001( )
547 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE );
548 aPipe.close( );
549 bRes = aPipe.is( );
551 aPipe.clear( );
552 bRes1 = aPipe.is( );
554 CPPUNIT_ASSERT_MESSAGE( "#test comment#: difference between close and clear.",
555 sal_True == bRes && sal_False == bRes1);
558 void close_002( )
560 ::osl::StreamPipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE );
561 aPipe.close( );
562 int nRet = aPipe.send( m_pTestString1.getStr(), 3 );
564 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use after close.",
565 OSL_PIPE_FAIL == nRet );
568 CPPUNIT_TEST_SUITE( close );
569 CPPUNIT_TEST( close_001 );
570 CPPUNIT_TEST( close_002 );
571 CPPUNIT_TEST_SUITE_END( );
572 }; // class close
575 /** testing the method:
576 inline oslPipeError SAL_CALL accept(StreamPipe& Connection);
577 please refer to StreamPipe::recv
579 /* class accept : public CppUnit::TestFixture
581 public:
582 sal_Bool bRes, bRes1;
584 void accept_001( )
587 // CPPUNIT_ASSERT_MESSAGE( "#test comment#: accept, untested.", 1 == 1 );
588 //CPPUNIT_ASSERT_STUB();
591 CPPUNIT_TEST_SUITE( accept );
592 CPPUNIT_TEST( accept_001 );
593 CPPUNIT_TEST_SUITE_END( );
594 };*/ // class accept
597 /** testing the method:
598 inline oslPipeError SAL_CALL getError() const;
600 class getError : public CppUnit::TestFixture
602 public:
603 sal_Bool bRes, bRes1;
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 }
623 did not define osl_Pipe_E_NotFound, osl_Pipe_E_AlreadyExists
626 void getError_001( )
628 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
629 oslPipeError nError = aPipe.getError( );
630 printPipeError( aPipe );
631 aPipe.clear( );
633 CPPUNIT_ASSERT_MESSAGE( "#test comment#: open a non-exist pipe.",
634 nError != osl_Pipe_E_None );
637 void getError_002( )
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( );
646 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create an already exist pipe.",
647 nError != osl_Pipe_E_None );
650 CPPUNIT_TEST_SUITE( getError );
651 CPPUNIT_TEST( getError_001 );
652 CPPUNIT_TEST( getError_002 );
653 CPPUNIT_TEST_SUITE_END( );
654 }; // class getError
657 /** testing the method:
658 inline oslPipe SAL_CALL getHandle() const;
660 class getHandle : public CppUnit::TestFixture
662 public:
663 sal_Bool bRes, bRes1;
665 void getHandle_001( )
667 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
668 bRes = aPipe == aPipe.getHandle( );
669 aPipe.clear( );
671 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe should equal to its handle.",
672 sal_True == bRes );
675 void getHandle_002( )
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( );
683 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe derived from another pipe's handle.",
684 sal_True == bRes );
687 CPPUNIT_TEST_SUITE( getHandle );
688 CPPUNIT_TEST( getHandle_001 );
689 CPPUNIT_TEST( getHandle_002 );
690 CPPUNIT_TEST_SUITE_END( );
691 }; // class getHandle
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 // -----------------------------------------------------------------------------
708 } // namespace osl_Pipe
711 namespace osl_StreamPipe
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 );
722 class ctors : public CppUnit::TestFixture
724 public:
725 sal_Bool bRes, bRes1;
727 void ctors_none( )
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( );
735 // assign it and check.
736 aStreamPipe1 = aStreamPipe;
737 bRes1 = aStreamPipe1.is( );
738 aStreamPipe.clear( );
739 aStreamPipe1.clear( );
741 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, before and after assign.",
742 sal_False == bRes && sal_True == bRes1 );
745 void ctors_handle( )
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( );
755 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with other's handle.",
756 sal_True == bRes );
759 void ctors_copy( )
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( );
769 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.",
770 sal_True == bRes );
773 void ctors_name_option( )
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( );
783 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.",
784 sal_True == bRes );
787 void ctors_name_option_security( )
789 /// create a security pipe.
790 const ::osl::Security rSecurity;
791 ::osl::StreamPipe aSecurityPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity );
793 bRes = aSecurityPipe.is( );
794 aSecurityPipe.clear( );
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 );
800 /** tester comment:
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)).
810 void ctors_no_acquire( )
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 );
817 bRes = aNoAcquirePipe.is( );
818 aPipe.clear( );
819 // bRes1 = aNoAcquirePipe.is( );
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 );
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
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
842 class assign : public CppUnit::TestFixture
844 public:
845 sal_Bool bRes, bRes1;
847 void assign_ref( )
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( );
857 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.",
858 sal_True == bRes && sal_True == bRes1 );
861 void assign_handle( )
863 ::osl::StreamPipe * pPipe = new ::osl::StreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
864 ::osl::StreamPipe * pAssignPipe = new ::osl::StreamPipe;
865 *pAssignPipe = pPipe->getHandle( );
867 bRes = pAssignPipe->is( );
868 bRes1 = ( *pPipe == *pAssignPipe );
869 pPipe->close( );
871 delete pAssignPipe;
873 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle., seems not implemented under (LINUX)(W32)",
874 sal_True == bRes && sal_True == bRes1 );
877 CPPUNIT_TEST_SUITE( assign );
878 CPPUNIT_TEST( assign_ref );
879 CPPUNIT_TEST( assign_handle );
880 CPPUNIT_TEST_SUITE_END( );
881 };*/ // class assign
884 /** wait _nSec seconds.
886 void thread_sleep( sal_Int32 _nSec )
888 /// print statement in thread process must use fflush() to force display.
889 // printf("wait %d seconds. ", _nSec );
890 fflush(stdout);
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" );
900 // test read/write & send/recv data to pipe
901 // -----------------------------------------------------------------------------
903 class Pipe_DataSink_Thread : public Thread
905 public:
906 sal_Char buf[256];
907 Pipe_DataSink_Thread( ) { }
909 ~Pipe_DataSink_Thread( )
912 protected:
913 void SAL_CALL run( )
915 sal_Int32 nChars = 0;
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 )
921 printf("pipe open failed! \n");
923 else
925 printf("read\n");
926 nChars = aSenderPipe.read( buf, m_pTestString1.getLength() + 1 );
927 if ( nChars < 0 )
929 printf("read failed! \n");
930 return;
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 )
937 printf("client send failed! \n");
938 return;
945 // -----------------------------------------------------------------------------
947 class Pipe_DataSource_Thread : public Thread
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( )
956 printf("create pipe\n");
957 aListenPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
959 ~Pipe_DataSource_Thread( )
961 aListenPipe.close();
963 protected:
964 void SAL_CALL run( )
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 )
972 printf("pipe create failed! \n");
974 else
976 //::osl::StreamPipe aConnectionPipe;
978 //start server and wait for connection.
979 printf("accept\n");
980 if ( osl_Pipe_E_None != aListenPipe.accept( aConnectionPipe ) )
982 printf("pipe accept failed!");
983 return;
985 printf("write\n");
986 // write to pipe
987 nChars = aConnectionPipe.write( m_pTestString1.getStr(), m_pTestString1.getLength() + 1 );
988 if ( nChars < 0)
990 printf("server write failed! \n");
991 return;
993 printf("recv\n");
994 nChars = aConnectionPipe.recv( buf, 256 );
996 if ( nChars < 0)
998 printf("server receive failed! \n");
999 return;
1001 //thread_sleep( 2 );
1002 printf("received message is: %s\n", buf );
1003 //aConnectionPipe.close();
1008 /** testing the method: read/write/send/recv and Pipe::accept
1010 class recv : public CppUnit::TestFixture
1012 public:
1013 sal_Bool bRes, bRes1;
1015 void recv_001( )
1017 //launch threads.
1018 Pipe_DataSource_Thread myDataSourceThread;
1019 Pipe_DataSink_Thread myDataSinkThread;
1020 myDataSourceThread.create( );
1021 thread_sleep( 1 );
1022 myDataSinkThread.create( );
1024 //wait until the thread terminate
1025 myDataSinkThread.join( );
1026 myDataSourceThread.join( );
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 );
1032 //close pipe when accept
1033 void recv_002()
1035 thread_sleep( 1 );
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.
1046 CPPUNIT_TEST_SUITE( recv );
1047 CPPUNIT_TEST( recv_001 );
1048 CPPUNIT_TEST( recv_002 );
1049 CPPUNIT_TEST_SUITE_END( );
1050 }; // class recv
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 // -----------------------------------------------------------------------------
1058 } // namespace osl_StreamPipe
1060 CPPUNIT_PLUGIN_IMPLEMENT();