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