bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / osl / pipe / osl_Pipe.cxx
bloba91ea189fc9f38726c42ee4c5d9ad438184378af
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 // include files
22 #include <sal/types.h>
23 #include "cppunit/TestAssert.h"
24 #include "cppunit/TestFixture.h"
25 #include "cppunit/extensions/HelperMacros.h"
26 #include "cppunit/plugin/TestPlugIn.h"
27 #include "unotest/uniquepipename.hxx"
28 #include <rtl/ustring.hxx>
30 #include <osl/thread.hxx>
32 #include <osl/mutex.hxx>
34 #include <osl/pipe.hxx>
35 #include <osl/time.h>
37 #ifdef UNX
38 #include <unistd.h>
39 #endif
40 #include <string.h>
42 using namespace osl;
44 using ::rtl::OUString;
45 using ::rtl::OUStringToOString;
46 using ::rtl::OString;
48 // helper functions
50 /** print Boolean value.
52 inline void printBool( sal_Bool bOk )
54 printf("#printBool# " );
55 ( sal_True == bOk ) ? printf("YES!\n" ): printf("NO!\n" );
58 /** print a UNI_CODE String.
60 inline void printUString( const ::rtl::OUString & str )
62 rtl::OString aString;
64 printf("#printUString_u# " );
65 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
66 printf("%s\n", aString.getStr( ) );
69 /** print last error of pipe system.
71 inline void printPipeError( ::osl::Pipe aPipe )
73 oslPipeError nError = aPipe.getError( );
74 printf("#printPipeError# " );
75 switch ( nError ) {
76 case osl_Pipe_E_None:
77 printf("Success!\n" );
78 break;
79 case osl_Pipe_E_NotFound:
80 printf("The returned error is: Not found!\n" );
81 break;
82 case osl_Pipe_E_AlreadyExists:
83 printf("The returned error is: Already exist!\n" );
84 break;
85 case osl_Pipe_E_NoProtocol:
86 printf("The returned error is: No protocol!\n" );
87 break;
88 case osl_Pipe_E_NetworkReset:
89 printf("The returned error is: Network reset!\n" );
90 break;
91 case osl_Pipe_E_ConnectionAbort:
92 printf("The returned error is: Connection aborted!\n" );
93 break;
94 case osl_Pipe_E_ConnectionReset:
95 printf("The returned error is: Connection reset!\n" );
96 break;
97 case osl_Pipe_E_NoBufferSpace:
98 printf("The returned error is: No buffer space!\n" );
99 break;
100 case osl_Pipe_E_TimedOut:
101 printf("The returned error is: Timeout!\n" );
102 break;
103 case osl_Pipe_E_ConnectionRefused:
104 printf("The returned error is: Connection refused!\n" );
105 break;
106 case osl_Pipe_E_invalidError:
107 printf("The returned error is: Invalid error!\n" );
108 break;
109 default:
110 printf("The returned error is: Number %d, Unknown Error\n", nError );
111 break;
115 // pipe name and transfer contents
117 const rtl::OUString aTestPipeName("testpipe2");
118 const rtl::OUString aTestPipe1("testpipe1");
119 const rtl::OUString aTestString("Sun Microsystems");
121 const OString m_pTestString1("Sun Microsystems");
122 const OString m_pTestString2("test pipe PASS/OK");
124 // test code start here
126 namespace osl_Pipe
129 // most return value -1 denote a fail of operation.
131 #define OSL_PIPE_FAIL -1
133 /** testing the methods:
134 inline Pipe();
135 inline Pipe(const ::rtl::OUString& strName, oslPipeOptions Options);
136 inline Pipe(const ::rtl::OUString& strName, oslPipeOptions Options,const Security & rSecurity);
137 inline Pipe(const Pipe& pipe);
138 inline Pipe(oslPipe pipe, __sal_NoAcquire noacquire );
139 inline Pipe(oslPipe Pipe);
141 class ctors : public CppUnit::TestFixture
143 public:
144 sal_Bool bRes, bRes1;
146 void setUp( )
150 void tearDown( )
154 void ctors_none( )
156 ::osl::Pipe aPipe;
157 bRes = aPipe.is( );
159 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, yet no case to test.",
160 sal_False == bRes );
163 void ctors_name_option( )
165 /// create a named pipe.
166 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
167 ::osl::Pipe aAssignPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
169 bRes = aPipe.is( ) && aAssignPipe.is( );
171 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.",
172 sal_True == bRes );
175 void ctors_name_option_security( )
177 /// create a security pipe.
178 const ::osl::Security rSecurity;
179 ::osl::Pipe aSecurityPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity );
181 bRes = aSecurityPipe.is( );
183 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name, option and security, the test of security is not implemented yet.",
184 sal_True == bRes );
187 void ctors_copy( )
189 /// create a pipe.
190 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
191 /// create a pipe using copy constructor.
192 ::osl::Pipe aCopyPipe( aPipe );
194 bRes = aCopyPipe.is( ) && aCopyPipe == aPipe;
196 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.",
197 sal_True == bRes );
200 /** tester comment:
202 When test the following two constructors, don't know how to test the
203 acquire and no acquire action. possible plans:
204 1.release one handle and check the other( did not success since the
205 other still exist and valid. )
206 2. release one handle twice to see getLastError( )(the getLastError
207 always returns invalidError(LINUX)).
210 void ctors_no_acquire( )
212 /// create a pipe.
213 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
214 /// constructs a pipe reference without acquiring the handle.
215 ::osl::Pipe aNoAcquirePipe( aPipe.getHandle( ), SAL_NO_ACQUIRE );
217 bRes = aNoAcquirePipe.is( );
218 ///aPipe.clear( );
219 ///bRes1 = aNoAcquirePipe.is( );
221 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no acquire of handle, only validation test, do not know how to test no acquire.",
222 sal_True == bRes );
225 void ctors_acquire( )
227 /// create a base pipe.
228 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
229 /// constructs two pipes without acquiring the handle on the base pipe.
230 ::osl::Pipe aAcquirePipe( aPipe.getHandle( ) );
231 ::osl::Pipe aAcquirePipe1( NULL );
233 bRes = aAcquirePipe.is( );
234 bRes1 = aAcquirePipe1.is( );
236 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no acquire of handle.only validation test, do not know how to test no acquire.",
237 sal_True == bRes && sal_False == bRes1 );
240 CPPUNIT_TEST_SUITE( ctors );
241 CPPUNIT_TEST( ctors_none );
242 CPPUNIT_TEST( ctors_name_option );
243 CPPUNIT_TEST( ctors_name_option_security );
244 CPPUNIT_TEST( ctors_copy );
245 CPPUNIT_TEST( ctors_no_acquire );
246 CPPUNIT_TEST( ctors_acquire );
247 CPPUNIT_TEST_SUITE_END( );
248 }; // class ctors
250 /** testing the method:
251 inline sal_Bool SAL_CALL is() const;
253 class is : public CppUnit::TestFixture
255 public:
256 void is_001( )
258 ::osl::Pipe aPipe;
260 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), check if the pipe is a valid one.", sal_False == aPipe.is( ) );
263 void is_002( )
265 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
267 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), a normal pipe creation.", sal_True == aPipe.is( ) );
270 void is_003( )
272 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
273 aPipe.clear( );
275 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid case.", sal_False == aPipe.is( ) );
278 void is_004( )
280 ::osl::Pipe aPipe( NULL );
282 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid constructor.", sal_False == aPipe.is( ) );
285 CPPUNIT_TEST_SUITE( is );
286 CPPUNIT_TEST( is_001 );
287 CPPUNIT_TEST( is_002 );
288 CPPUNIT_TEST( is_003 );
289 CPPUNIT_TEST( is_004 );
290 CPPUNIT_TEST_SUITE_END( );
291 }; // class is
293 /** testing the methods:
294 inline sal_Bool create( const ::rtl::OUString & strName,
295 oslPipeOptions Options, const Security &rSec );
296 nline sal_Bool create( const ::rtl::OUString & strName,
297 oslPipeOptions Options = osl_Pipe_OPEN );
299 class create : public CppUnit::TestFixture
301 public:
302 sal_Bool bRes, bRes1;
304 /** tester comment:
306 security create only be tested creation, security section is
307 untested yet.
310 void create_named_security_001( )
312 const Security rSec;
313 ::osl::Pipe aPipe;
314 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec );
315 bRes1 = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec );
316 aPipe.clear( );
318 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
319 sal_True == bRes && sal_False == bRes1);
322 void create_named_security_002( )
324 const Security rSec;
325 ::osl::Pipe aPipe, aPipe1;
326 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec );
327 bRes1 = aPipe1.create( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN, rSec );
328 aPipe.clear( );
330 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
331 sal_True == bRes && sal_True == bRes1);
334 void create_named_001( )
336 ::osl::Pipe aPipe;
337 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
338 bRes1 = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
339 aPipe.clear( );
341 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
342 sal_True == bRes && sal_False == bRes1);
345 void create_named_002( )
347 ::osl::Pipe aPipe, aPipe1;
348 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
349 bRes1 = aPipe1.create( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
350 aPipe.clear( );
352 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
353 sal_True == bRes && sal_True == bRes1);
356 void create_named_003( )
358 ::osl::Pipe aPipe;
359 bRes = aPipe.create( test::uniquePipeName(aTestPipeName) );
360 aPipe.clear( );
362 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test default option is open.",
363 sal_False == bRes );
366 CPPUNIT_TEST_SUITE( create );
367 CPPUNIT_TEST( create_named_security_001 );
368 CPPUNIT_TEST( create_named_security_002 );
369 CPPUNIT_TEST( create_named_001 );
370 CPPUNIT_TEST( create_named_002 );
371 CPPUNIT_TEST( create_named_003 );
372 CPPUNIT_TEST_SUITE_END( );
373 }; // class create
375 /** testing the method:
376 inline void SAL_CALL clear();
378 class clear : public CppUnit::TestFixture
380 public:
381 sal_Bool bRes, bRes1;
383 void clear_001( )
385 ::osl::Pipe aPipe;
386 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
387 aPipe.clear( );
388 bRes = aPipe.is( );
390 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test clear.",
391 sal_False == bRes );
394 CPPUNIT_TEST_SUITE( clear );
395 CPPUNIT_TEST( clear_001 );
396 CPPUNIT_TEST_SUITE_END( );
397 }; // class clear
399 /** testing the methods:
400 inline Pipe& SAL_CALL operator= (const Pipe& pipe);
401 inline Pipe& SAL_CALL operator= (const oslPipe pipe );
403 class assign : public CppUnit::TestFixture
405 public:
406 sal_Bool bRes, bRes1;
408 void assign_ref( )
410 ::osl::Pipe aPipe, aPipe1;
411 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
412 aPipe1 = aPipe;
413 bRes = aPipe1.is( );
414 bRes1 = aPipe == aPipe1;
415 aPipe.close( );
416 aPipe1.close( );
418 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.",
419 sal_True == bRes && sal_True == bRes1 );
422 void assign_handle( )
424 ::osl::Pipe aPipe, aPipe1;
425 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
426 aPipe1 = aPipe.getHandle( );
427 bRes = aPipe1.is( );
428 bRes1 = aPipe == aPipe1;
429 aPipe.close( );
430 aPipe1.close( );
432 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle.",
433 sal_True == bRes && sal_True == bRes1 );
436 CPPUNIT_TEST_SUITE( assign );
437 CPPUNIT_TEST( assign_ref );
438 CPPUNIT_TEST( assign_handle );
439 CPPUNIT_TEST_SUITE_END( );
440 }; // class assign
442 /** testing the method:
443 inline sal_Bool SAL_CALL isValid() const;
444 isValid( ) has not been implemented under the following platforms, please refer to osl/pipe.hxx
447 /** testing the method:
448 inline sal_Bool SAL_CALL operator==( const Pipe& rPipe ) const;
450 class isEqual : public CppUnit::TestFixture
452 public:
453 sal_Bool bRes, bRes1;
455 void isEqual_001( )
457 ::osl::Pipe aPipe;
458 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
459 bRes = aPipe == aPipe;
460 aPipe.close( );
462 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(), compare its self.",
463 sal_True == bRes );
466 void isEqual_002( )
468 ::osl::Pipe aPipe, aPipe1, aPipe2;
469 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
471 aPipe1 = aPipe;
472 aPipe2.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
474 bRes = aPipe == aPipe1;
475 bRes1 = aPipe == aPipe2;
476 aPipe.close( );
477 aPipe1.close( );
478 aPipe2.close( );
480 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(),create one copy instance, and compare.",
481 sal_True == bRes && sal_False == bRes1 );
484 CPPUNIT_TEST_SUITE( isEqual );
485 CPPUNIT_TEST( isEqual_001 );
486 CPPUNIT_TEST( isEqual_002 );
487 CPPUNIT_TEST_SUITE_END( );
488 }; // class isEqual
490 /** testing the method:
491 inline void SAL_CALL close();
493 class close : public CppUnit::TestFixture
495 public:
496 sal_Bool bRes, bRes1;
498 void close_001( )
500 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE );
501 aPipe.close( );
502 bRes = aPipe.is( );
504 aPipe.clear( );
505 bRes1 = aPipe.is( );
507 CPPUNIT_ASSERT_MESSAGE( "#test comment#: difference between close and clear.",
508 sal_True == bRes && sal_False == bRes1);
511 void close_002( )
513 ::osl::StreamPipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE );
514 aPipe.close( );
515 int nRet = aPipe.send( m_pTestString1.getStr(), 3 );
517 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use after close.",
518 OSL_PIPE_FAIL == nRet );
521 CPPUNIT_TEST_SUITE( close );
522 CPPUNIT_TEST( close_001 );
523 CPPUNIT_TEST( close_002 );
524 CPPUNIT_TEST_SUITE_END( );
525 }; // class close
527 /** testing the method:
528 inline oslPipeError SAL_CALL accept(StreamPipe& Connection);
529 please refer to StreamPipe::recv
532 /** testing the method:
533 inline oslPipeError SAL_CALL getError() const;
535 class getError : public CppUnit::TestFixture
537 public:
538 sal_Bool bRes, bRes1;
540 void getError_001( )
542 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
543 oslPipeError nError = aPipe.getError( );
544 printPipeError( aPipe );
545 aPipe.clear( );
547 CPPUNIT_ASSERT_MESSAGE( "#test comment#: open a non-exist pipe.",
548 nError != osl_Pipe_E_None );
551 void getError_002( )
553 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
554 ::osl::Pipe aPipe1( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
555 oslPipeError nError = aPipe.getError( );
556 printPipeError( aPipe );
557 aPipe.clear( );
558 aPipe1.clear( );
560 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create an already exist pipe.",
561 nError != osl_Pipe_E_None );
564 CPPUNIT_TEST_SUITE( getError );
565 CPPUNIT_TEST( getError_001 );
566 CPPUNIT_TEST( getError_002 );
567 CPPUNIT_TEST_SUITE_END( );
568 }; // class getError
570 /** testing the method:
571 inline oslPipe SAL_CALL getHandle() const;
573 class getHandle : public CppUnit::TestFixture
575 public:
576 sal_Bool bRes, bRes1;
578 void getHandle_001( )
580 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
581 bRes = aPipe == aPipe.getHandle( );
582 aPipe.clear( );
584 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe should equal to its handle.",
585 sal_True == bRes );
588 void getHandle_002( )
590 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
591 ::osl::Pipe aPipe1( aPipe.getHandle( ) );
592 bRes = aPipe == aPipe1;
593 aPipe.clear( );
594 aPipe1.clear( );
596 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe derived from another pipe's handle.",
597 sal_True == bRes );
600 CPPUNIT_TEST_SUITE( getHandle );
601 CPPUNIT_TEST( getHandle_001 );
602 CPPUNIT_TEST( getHandle_002 );
603 CPPUNIT_TEST_SUITE_END( );
604 }; // class getHandle
606 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::ctors);
607 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::is);
608 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::create);
609 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::clear);
610 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::assign);
611 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::isEqual);
612 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::close);
613 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::getError);
614 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::getHandle);
616 } // namespace osl_Pipe
618 namespace osl_StreamPipe
621 /** testing the methods:
622 inline StreamPipe();
623 inline StreamPipe(oslPipe Pipe);;
624 inline StreamPipe(const StreamPipe& Pipe);
625 inline StreamPipe(const ::rtl::OUString& strName, oslPipeOptions Options = osl_Pipe_OPEN);
626 inline StreamPipe(const ::rtl::OUString& strName, oslPipeOptions Options, const Security &rSec );
627 inline StreamPipe( oslPipe pipe, __sal_NoAcquire noacquire );
629 class ctors : public CppUnit::TestFixture
631 public:
632 sal_Bool bRes, bRes1;
634 void ctors_none( )
636 // create a pipe.
637 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
638 // create an unattached pipe.
639 ::osl::StreamPipe aStreamPipe1;
640 bRes = aStreamPipe1.is( );
642 // assign it and check.
643 aStreamPipe1 = aStreamPipe;
644 bRes1 = aStreamPipe1.is( );
645 aStreamPipe.clear( );
646 aStreamPipe1.clear( );
648 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, before and after assign.",
649 sal_False == bRes && sal_True == bRes1 );
652 void ctors_handle( )
654 // create a pipe.
655 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
656 // create a pipe with last handle.
657 ::osl::StreamPipe aStreamPipe1( aStreamPipe.getHandle( ) );
658 bRes = aStreamPipe1.is( ) && aStreamPipe == aStreamPipe1;
659 aStreamPipe.clear( );
660 aStreamPipe1.clear( );
662 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with other's handle.",
663 sal_True == bRes );
666 void ctors_copy( )
668 // create a pipe.
669 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
670 // create an unattached pipe.
671 ::osl::StreamPipe aStreamPipe1( aStreamPipe );
672 bRes = aStreamPipe1.is( ) && aStreamPipe == aStreamPipe1;
673 aStreamPipe.clear( );
674 aStreamPipe1.clear( );
676 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.",
677 sal_True == bRes );
680 void ctors_name_option( )
682 // create a pipe.
683 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
684 // create an unattached pipe.
685 ::osl::StreamPipe aStreamPipe1( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
686 bRes = aStreamPipe1.is( ) && aStreamPipe.is( );
687 aStreamPipe.clear( );
688 aStreamPipe1.clear( );
690 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.",
691 sal_True == bRes );
694 void ctors_name_option_security( )
696 /// create a security pipe.
697 const ::osl::Security rSecurity;
698 ::osl::StreamPipe aSecurityPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity );
700 bRes = aSecurityPipe.is( );
701 aSecurityPipe.clear( );
703 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name, option and security, the test of security is not implemented yet.",
704 sal_True == bRes );
707 /** tester comment:
709 When test the following constructor, don't know how to test the
710 acquire and no acquire action. possible plans:
711 1.release one handle and check the other( did not success since the
712 other still exist and valid. )
713 2. release one handle twice to see getLastError( )(the getLastError
714 always returns invalidError(LINUX)).
717 void ctors_no_acquire( )
719 // create a pipe.
720 ::osl::StreamPipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
721 // constructs a pipe reference without acquiring the handle.
722 ::osl::StreamPipe aNoAcquirePipe( aPipe.getHandle( ), SAL_NO_ACQUIRE );
724 bRes = aNoAcquirePipe.is( );
725 aPipe.clear( );
727 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no acquire of handle, only validation test, do not know how to test no acquire.",
728 sal_True == bRes );
731 CPPUNIT_TEST_SUITE( ctors );
732 CPPUNIT_TEST( ctors_none );
733 CPPUNIT_TEST( ctors_handle );
734 CPPUNIT_TEST( ctors_copy );
735 CPPUNIT_TEST( ctors_name_option );
736 CPPUNIT_TEST( ctors_name_option_security );
737 CPPUNIT_TEST( ctors_no_acquire );
738 CPPUNIT_TEST_SUITE_END( );
739 }; // class ctors
741 /** testing the methods:
742 inline StreamPipe & SAL_CALL operator=(oslPipe Pipe);
743 inline StreamPipe& SAL_CALL operator=(const Pipe& pipe);
744 mindy: not implementated in osl/pipe.hxx, so remove the cases
747 /** wait _nSec seconds.
749 void thread_sleep( sal_uInt32 _nSec )
751 /// print statement in thread process must use fflush() to force display.
752 fflush(stdout);
754 TimeValue nTV;
755 nTV.Seconds = _nSec;
756 nTV.Nanosec = 0;
757 osl_waitThread(&nTV);
759 // test read/write & send/recv data to pipe
761 class Pipe_DataSink_Thread : public Thread
763 public:
764 sal_Char buf[256];
765 Pipe_DataSink_Thread( ) { }
767 ~Pipe_DataSink_Thread( )
770 protected:
771 void SAL_CALL run( )
773 printf("open pipe\n");
774 ::osl::StreamPipe aSenderPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); // test::uniquePipeName(aTestPipeName) is a string = "TestPipe"
775 if ( aSenderPipe.is() == sal_False )
777 printf("pipe open failed! \n");
779 else
781 printf("read\n");
782 sal_Int32 nChars = aSenderPipe.read( buf, m_pTestString1.getLength() + 1 );
783 if ( nChars < 0 )
785 printf("read failed! \n");
786 return;
788 printf("buffer is %s \n", buf);
789 printf("send\n");
790 nChars = aSenderPipe.send( m_pTestString2.getStr(), m_pTestString2.getLength() + 1 );
791 if ( nChars < 0 )
793 printf("client send failed! \n");
794 return;
801 class Pipe_DataSource_Thread : public Thread
803 public:
804 sal_Char buf[256];
805 ::osl::Pipe aListenPipe;
806 ::osl::StreamPipe aConnectionPipe;
807 Pipe_DataSource_Thread( )
809 printf("create pipe\n");
810 aListenPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
812 ~Pipe_DataSource_Thread( )
814 aListenPipe.close();
816 protected:
817 void SAL_CALL run( )
819 //create pipe.
820 sal_Int32 nChars;
821 printf("listen\n");
822 if ( aListenPipe.is() == sal_False )
824 printf("pipe create failed! \n");
826 else
828 //start server and wait for connection.
829 printf("accept\n");
830 if ( osl_Pipe_E_None != aListenPipe.accept( aConnectionPipe ) )
832 printf("pipe accept failed!");
833 return;
835 printf("write\n");
836 // write to pipe
837 nChars = aConnectionPipe.write( m_pTestString1.getStr(), m_pTestString1.getLength() + 1 );
838 if ( nChars < 0)
840 printf("server write failed! \n");
841 return;
843 printf("recv\n");
844 nChars = aConnectionPipe.recv( buf, 256 );
846 if ( nChars < 0)
848 printf("server receive failed! \n");
849 return;
851 printf("received message is: %s\n", buf );
856 /** testing the method: read/write/send/recv and Pipe::accept
858 class recv : public CppUnit::TestFixture
860 public:
861 sal_Bool bRes, bRes1;
863 void recv_001( )
865 //launch threads.
866 Pipe_DataSource_Thread myDataSourceThread;
867 Pipe_DataSink_Thread myDataSinkThread;
868 myDataSourceThread.create( );
869 thread_sleep( 1 );
870 myDataSinkThread.create( );
872 //wait until the thread terminate
873 myDataSinkThread.join( );
874 myDataSourceThread.join( );
876 int nCompare1 = strcmp( myDataSinkThread.buf, m_pTestString1.getStr() );
877 int nCompare2 = strcmp( myDataSourceThread.buf, m_pTestString2.getStr() );
878 CPPUNIT_ASSERT_MESSAGE( "test send/recv/write/read.", nCompare1 == 0 && nCompare2 == 0 );
880 //close pipe when accept
881 void recv_002()
883 thread_sleep( 1 );
885 Pipe_DataSource_Thread myDataSourceThread;
886 Pipe_DataSink_Thread myDataSinkThread;
887 myDataSourceThread.create( );
888 thread_sleep( 1 );
889 myDataSourceThread.aListenPipe.close();
890 myDataSourceThread.join( );
891 //no condition judgement here, if the case could finish excuting within 1 or 2 seconds, it passes.
894 CPPUNIT_TEST_SUITE( recv );
895 CPPUNIT_TEST( recv_001 );
896 CPPUNIT_TEST( recv_002 );
897 CPPUNIT_TEST_SUITE_END( );
898 }; // class recv
900 CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::ctors);
901 //CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::assign);
902 CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::recv);
904 } // namespace osl_StreamPipe
906 CPPUNIT_PLUGIN_IMPLEMENT();
908 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */