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