1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <sal/types.h>
23 #include <cppunit/TestAssert.h>
24 #include <cppunit/TestFixture.h>
25 #include <cppunit/extensions/HelperMacros.h>
26 #include <rtl/ustring.hxx>
28 #include <osl/test/uniquepipename.hxx>
29 #include <osl/thread.hxx>
31 #include <osl/pipe.hxx>
37 /** print last error of pipe system.
39 static void printPipeError( ::osl::Pipe
const & aPipe
)
41 oslPipeError nError
= aPipe
.getError( );
42 printf("#printPipeError# " );
45 printf("Success!\n" );
47 case osl_Pipe_E_NotFound
:
48 printf("The returned error is: Not found!\n" );
50 case osl_Pipe_E_AlreadyExists
:
51 printf("The returned error is: Already exist!\n" );
53 case osl_Pipe_E_NoProtocol
:
54 printf("The returned error is: No protocol!\n" );
56 case osl_Pipe_E_NetworkReset
:
57 printf("The returned error is: Network reset!\n" );
59 case osl_Pipe_E_ConnectionAbort
:
60 printf("The returned error is: Connection aborted!\n" );
62 case osl_Pipe_E_ConnectionReset
:
63 printf("The returned error is: Connection reset!\n" );
65 case osl_Pipe_E_NoBufferSpace
:
66 printf("The returned error is: No buffer space!\n" );
68 case osl_Pipe_E_TimedOut
:
69 printf("The returned error is: Timeout!\n" );
71 case osl_Pipe_E_ConnectionRefused
:
72 printf("The returned error is: Connection refused!\n" );
74 case osl_Pipe_E_invalidError
:
75 printf("The returned error is: Invalid error!\n" );
78 printf("The returned error is: Number %d, Unknown Error\n", nError
);
83 // pipe name and transfer contents
85 constexpr OUString
aTestPipeName(u
"testpipe2"_ustr
);
86 constexpr OUString
aTestPipe1(u
"testpipe1"_ustr
);
88 constexpr OString
m_pTestString1("Sun Microsystems"_ostr
);
89 constexpr OString
m_pTestString2("test pipe PASS/OK"_ostr
);
91 // test code start here
96 // most return value -1 denote a fail of operation.
98 #define OSL_PIPE_FAIL -1
100 /** testing the methods:
102 inline Pipe(const OUString& strName, oslPipeOptions Options);
103 inline Pipe(const OUString& strName, oslPipeOptions Options,const Security & rSecurity);
104 inline Pipe(const Pipe& pipe);
105 inline Pipe(oslPipe pipe, __sal_NoAcquire noacquire );
106 inline Pipe(oslPipe Pipe);
108 class ctors
: public CppUnit::TestFixture
111 bool bRes
, bRes1
, bRes2
;
118 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, yet no case to test.",
122 void ctors_name_option( )
124 /// create a named pipe.
125 ::osl::Pipe
aPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
126 ::osl::Pipe
aAssignPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_OPEN
);
128 bRes
= aPipe
.is( ) && aAssignPipe
.is( );
130 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.",
134 void ctors_name_option_security( )
136 /// create a security pipe.
137 const ::osl::Security rSecurity
;
138 ::osl::Pipe
aSecurityPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
, rSecurity
);
140 bRes
= aSecurityPipe
.is( );
142 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name, option and security, the test of security is not implemented yet.",
149 ::osl::Pipe
aPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
150 /// create a pipe using copy constructor.
151 ::osl::Pipe
aCopyPipe( aPipe
);
153 bRes
= aCopyPipe
.is( ) && aCopyPipe
== aPipe
;
155 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.",
159 /* Note: DO NOT DO THIS - I have very deliberately caused send to FAIL, *on purpose* as this is the
160 only sane way to test noacquire. This is a terrible misuse of no-acquire, but in this case is
161 needed only so we can test to make sure no-acquire is working!
163 void ctors_no_acquire( )
166 OUString
aPipeName(test::uniquePipeName(aTestPipeName
));
167 oslPipe
handle(osl_createPipe(aPipeName
.pData
, osl_Pipe_CREATE
, nullptr));
168 /// constructs a pipe reference without acquiring the handle.
169 std::unique_ptr
<osl::Pipe
> xNoAcquirePipe(new osl::Pipe(handle
, SAL_NO_ACQUIRE
));
171 StreamPipe
aStreamPipe(handle
);
172 xNoAcquirePipe
.reset();
173 int nRet
= aStreamPipe
.send("a", 1);
180 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no acquire of handle, deleted nonacquired pipe but could still send on original pipe!.",
184 void ctors_acquire( )
186 /// create a base pipe.
187 ::osl::Pipe
aPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
188 /// constructs two pipes, the second acquires the first pipe's handle.
189 ::osl::Pipe
aAcquirePipe( aPipe
.getHandle( ) );
190 ::osl::Pipe
aAcquirePipe1( nullptr );
192 bRes
= aAcquirePipe
.is();
193 bRes1
= aAcquirePipe1
.is();
194 bRes2
= aPipe
== aAcquirePipe
;
196 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with acquire of handle, original pipe does not exist.",
198 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with acquire of handle, copied pipe does not exist",
201 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test pipes should have same handle", bRes2
);
204 CPPUNIT_TEST_SUITE( ctors
);
205 CPPUNIT_TEST( ctors_none
);
206 CPPUNIT_TEST( ctors_name_option
);
207 CPPUNIT_TEST( ctors_name_option_security
);
208 CPPUNIT_TEST( ctors_copy
);
209 CPPUNIT_TEST( ctors_no_acquire
);
210 CPPUNIT_TEST( ctors_acquire
);
211 CPPUNIT_TEST_SUITE_END( );
214 /** testing the method:
215 inline sal_Bool SAL_CALL is() const;
217 class is
: public CppUnit::TestFixture
224 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), check if the pipe is a valid one.", !aPipe
.is( ) );
229 ::osl::Pipe
aPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
231 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), a normal pipe creation.", aPipe
.is( ) );
236 ::osl::Pipe
aPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
239 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid case.", !aPipe
.is( ) );
244 ::osl::Pipe
aPipe( nullptr );
246 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid constructor.", !aPipe
.is( ) );
249 CPPUNIT_TEST_SUITE( is
);
250 CPPUNIT_TEST( is_001
);
251 CPPUNIT_TEST( is_002
);
252 CPPUNIT_TEST( is_003
);
253 CPPUNIT_TEST( is_004
);
254 CPPUNIT_TEST_SUITE_END( );
257 /** testing the methods:
258 inline sal_Bool create( const OUString & strName,
259 oslPipeOptions Options, const Security &rSec );
260 nline sal_Bool create( const OUString & strName,
261 oslPipeOptions Options = osl_Pipe_OPEN );
263 class create
: public CppUnit::TestFixture
270 security create only be tested creation, security section is
274 void create_named_security_001( )
278 bRes
= aPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
, rSec
);
279 bRes1
= aPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
, rSec
);
282 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
284 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
288 void create_named_security_002( )
291 ::osl::Pipe aPipe
, aPipe1
;
292 bRes
= aPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
, rSec
);
293 bRes1
= aPipe1
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_OPEN
, rSec
);
296 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
298 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
302 void create_named_001( )
305 bRes
= aPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
306 bRes1
= aPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
309 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
311 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
315 void create_named_002( )
317 ::osl::Pipe aPipe
, aPipe1
;
318 bRes
= aPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
319 bRes1
= aPipe1
.create( test::uniquePipeName(aTestPipeName
) );
322 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
324 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
328 void create_named_003( )
331 bRes
= aPipe
.create( test::uniquePipeName(aTestPipeName
) );
334 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test default option is open.",
338 CPPUNIT_TEST_SUITE( create
);
339 CPPUNIT_TEST( create_named_security_001
);
340 CPPUNIT_TEST( create_named_security_002
);
341 CPPUNIT_TEST( create_named_001
);
342 CPPUNIT_TEST( create_named_002
);
343 CPPUNIT_TEST( create_named_003
);
344 CPPUNIT_TEST_SUITE_END( );
347 /** testing the method:
348 inline void SAL_CALL clear();
350 class clear
: public CppUnit::TestFixture
358 aPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
362 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test clear.",
366 CPPUNIT_TEST_SUITE( clear
);
367 CPPUNIT_TEST( clear_001
);
368 CPPUNIT_TEST_SUITE_END( );
371 /** testing the methods:
372 inline Pipe& SAL_CALL operator= (const Pipe& pipe);
373 inline Pipe& SAL_CALL operator= (const oslPipe pipe );
375 class assign
: public CppUnit::TestFixture
382 ::osl::Pipe aPipe
, aPipe1
;
383 aPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
386 bRes1
= aPipe
== aPipe1
;
390 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.",
392 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.",
396 void assign_handle( )
398 ::osl::Pipe aPipe
, aPipe1
;
399 aPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
400 aPipe1
= aPipe
.getHandle( );
402 bRes1
= aPipe
== aPipe1
;
406 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle.",
408 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle.",
412 CPPUNIT_TEST_SUITE( assign
);
413 CPPUNIT_TEST( assign_ref
);
414 CPPUNIT_TEST( assign_handle
);
415 CPPUNIT_TEST_SUITE_END( );
418 /** testing the method:
419 inline sal_Bool SAL_CALL isValid() const;
420 isValid( ) has not been implemented under the following platforms, please refer to osl/pipe.hxx
423 /** testing the method:
424 inline sal_Bool SAL_CALL operator==( const Pipe& rPipe ) const;
426 class isEqual
: public CppUnit::TestFixture
434 aPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
435 bRes
= aPipe
== aPipe
; // NOLINT(misc-redundant-expression)
438 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(), compare itself.",
444 ::osl::Pipe aPipe
, aPipe1
, aPipe2
;
445 aPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
448 aPipe2
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
450 bRes
= aPipe
== aPipe1
;
451 bRes1
= aPipe
== aPipe2
;
456 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(),create one copy instance, and compare.",
458 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(),create one copy instance, and compare.",
462 CPPUNIT_TEST_SUITE( isEqual
);
463 CPPUNIT_TEST( isEqual_001
);
464 CPPUNIT_TEST( isEqual_002
);
465 CPPUNIT_TEST_SUITE_END( );
468 /** testing the method:
469 inline void SAL_CALL close();
471 class close
: public CppUnit::TestFixture
478 ::osl::Pipe
aPipe( test::uniquePipeName(aTestPipe1
), osl_Pipe_CREATE
);
485 CPPUNIT_ASSERT_MESSAGE( "#test comment#: difference between close and clear.",
487 CPPUNIT_ASSERT_MESSAGE( "#test comment#: difference between close and clear.",
493 ::osl::StreamPipe
aPipe( test::uniquePipeName(aTestPipe1
), osl_Pipe_CREATE
);
495 int nRet
= aPipe
.send( m_pTestString1
.getStr(), 3 );
497 CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: use after close.",
498 OSL_PIPE_FAIL
, nRet
);
501 CPPUNIT_TEST_SUITE( close
);
502 CPPUNIT_TEST( close_001
);
503 CPPUNIT_TEST( close_002
);
504 CPPUNIT_TEST_SUITE_END( );
507 /** testing the method:
508 inline oslPipeError SAL_CALL accept(StreamPipe& Connection);
509 please refer to StreamPipe::recv
512 /** testing the method:
513 inline oslPipeError SAL_CALL getError() const;
515 class getError
: public CppUnit::TestFixture
522 ::osl::Pipe
aPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_OPEN
);
523 oslPipeError nError
= aPipe
.getError( );
524 printPipeError( aPipe
);
527 CPPUNIT_ASSERT_MESSAGE( "#test comment#: open a non-exist pipe.",
528 nError
!= osl_Pipe_E_None
);
533 ::osl::Pipe
aPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
534 ::osl::Pipe
aPipe1( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
535 oslPipeError nError
= aPipe
.getError( );
536 printPipeError( aPipe
);
540 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create an already exist pipe.",
541 nError
!= osl_Pipe_E_None
);
544 CPPUNIT_TEST_SUITE( getError
);
545 CPPUNIT_TEST( getError_001
);
546 CPPUNIT_TEST( getError_002
);
547 CPPUNIT_TEST_SUITE_END( );
550 /** testing the method:
551 inline oslPipe SAL_CALL getHandle() const;
553 class getHandle
: public CppUnit::TestFixture
558 void getHandle_equalityOperatorAgainstSelf( )
560 ::osl::Pipe
aPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_OPEN
);
561 bRes
= aPipe
== aPipe
.getHandle( );
564 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe should equal to its handle.",
568 void getHandle_equalityOperatorAgainstDerivedPipe( )
570 ::osl::Pipe
aPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
571 ::osl::Pipe
aPipe1( aPipe
.getHandle( ) );
572 bRes
= aPipe
== aPipe1
;
576 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe derived from another pipe's handle.",
580 CPPUNIT_TEST_SUITE( getHandle
);
581 CPPUNIT_TEST( getHandle_equalityOperatorAgainstSelf
);
582 CPPUNIT_TEST( getHandle_equalityOperatorAgainstDerivedPipe
);
583 CPPUNIT_TEST_SUITE_END( );
586 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::ctors
);
587 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::is
);
588 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::create
);
589 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::clear
);
590 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::assign
);
591 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::isEqual
);
592 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::close
);
593 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::getError
);
594 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::getHandle
);
596 } // namespace osl_Pipe
598 namespace osl_StreamPipe
601 /** testing the methods:
603 inline StreamPipe(oslPipe Pipe);
604 inline StreamPipe(const StreamPipe& Pipe);
605 inline StreamPipe(const OUString& strName, oslPipeOptions Options = osl_Pipe_OPEN);
606 inline StreamPipe(const OUString& strName, oslPipeOptions Options, const Security &rSec );
607 inline StreamPipe( oslPipe pipe, __sal_NoAcquire noacquire );
609 class ctors
: public CppUnit::TestFixture
617 ::osl::StreamPipe
aStreamPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
618 // create an unattached pipe.
619 ::osl::StreamPipe aStreamPipe1
;
620 bRes
= aStreamPipe1
.is( );
622 // assign it and check.
623 aStreamPipe1
= aStreamPipe
;
624 bRes1
= aStreamPipe1
.is( );
625 aStreamPipe
.clear( );
626 aStreamPipe1
.clear( );
628 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, before and after assign.",
630 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, before and after assign.",
637 ::osl::StreamPipe
aStreamPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
638 // create a pipe with last handle.
639 ::osl::StreamPipe
aStreamPipe1( aStreamPipe
.getHandle( ) );
640 bRes
= aStreamPipe1
.is( ) && aStreamPipe
== aStreamPipe1
;
641 aStreamPipe
.clear( );
642 aStreamPipe1
.clear( );
644 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with other's handle.",
651 ::osl::StreamPipe
aStreamPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
652 // create an unattached pipe.
653 ::osl::StreamPipe
aStreamPipe1( aStreamPipe
);
654 bRes
= aStreamPipe1
.is( ) && aStreamPipe
== aStreamPipe1
;
655 aStreamPipe
.clear( );
656 aStreamPipe1
.clear( );
658 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.",
662 void ctors_name_option( )
665 ::osl::StreamPipe
aStreamPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
666 // create an unattached pipe.
667 ::osl::StreamPipe
aStreamPipe1( test::uniquePipeName(aTestPipeName
), osl_Pipe_OPEN
);
668 bRes
= aStreamPipe1
.is( ) && aStreamPipe
.is( );
669 aStreamPipe
.clear( );
670 aStreamPipe1
.clear( );
672 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.",
676 void ctors_name_option_security( )
678 /// create a security pipe.
679 const ::osl::Security rSecurity
;
680 ::osl::StreamPipe
aSecurityPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
, rSecurity
);
682 bRes
= aSecurityPipe
.is( );
683 aSecurityPipe
.clear( );
685 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name, option and security, the test of security is not implemented yet.",
691 When test the following constructor, don't know how to test the
692 acquire and no acquire action. possible plans:
693 1.release one handle and check the other( did not success since the
694 other still exist and valid. )
695 2. release one handle twice to see getLastError( )(the getLastError
696 always returns invalidError(LINUX)).
699 void ctors_no_acquire( )
702 ::osl::StreamPipe
aPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
703 osl_acquirePipe(aPipe
.getHandle());
704 // constructs a pipe reference without acquiring the handle.
705 ::osl::StreamPipe
aNoAcquirePipe( aPipe
.getHandle( ), SAL_NO_ACQUIRE
);
707 bRes
= aNoAcquirePipe
.is( );
710 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no acquire of handle, only validation test, do not know how to test no acquire.",
714 CPPUNIT_TEST_SUITE( ctors
);
715 CPPUNIT_TEST( ctors_none
);
716 CPPUNIT_TEST( ctors_handle
);
717 CPPUNIT_TEST( ctors_copy
);
718 CPPUNIT_TEST( ctors_name_option
);
719 CPPUNIT_TEST( ctors_name_option_security
);
720 CPPUNIT_TEST( ctors_no_acquire
);
721 CPPUNIT_TEST_SUITE_END( );
724 /** testing the methods:
725 inline StreamPipe & SAL_CALL operator=(oslPipe Pipe);
726 inline StreamPipe& SAL_CALL operator=(const Pipe& pipe);
727 mindy: not implemented in osl/pipe.hxx, so remove the cases
730 /** wait _nSec seconds.
732 static void thread_sleep( sal_uInt32 _nSec
)
734 /// print statement in thread process must use fflush() to force display.
737 osl::Thread::wait(std::chrono::seconds(_nSec
));
739 // test read/write & send/recv data to pipe
743 class Pipe_DataSink_Thread
: public Thread
747 Pipe_DataSink_Thread( ) { }
750 void SAL_CALL
run( ) override
752 printf("open pipe\n");
753 ::osl::StreamPipe
aSenderPipe( test::uniquePipeName(aTestPipeName
), osl_Pipe_OPEN
); // test::uniquePipeName(aTestPipeName) is a string = "TestPipe"
754 if ( !aSenderPipe
.is() )
756 printf("pipe open failed! \n");
761 sal_Int32 nChars
= aSenderPipe
.read( buf
, m_pTestString1
.getLength() + 1 );
764 printf("read failed! \n");
767 buf
[sizeof(buf
)-1] = '\0';
768 printf("buffer is %s \n", buf
);
770 nChars
= aSenderPipe
.send( m_pTestString2
.getStr(), m_pTestString2
.getLength() + 1 );
773 printf("client send failed! \n");
781 class Pipe_DataSource_Thread
: public Thread
785 ::osl::Pipe aListenPipe
;
786 ::osl::StreamPipe aConnectionPipe
;
787 Pipe_DataSource_Thread( )
789 printf("create pipe\n");
790 aListenPipe
.create( test::uniquePipeName(aTestPipeName
), osl_Pipe_CREATE
);
792 virtual ~Pipe_DataSource_Thread( ) override
797 void SAL_CALL
run( ) override
801 if ( !aListenPipe
.is() )
803 printf("pipe create failed! \n");
807 //start server and wait for connection.
809 if ( aListenPipe
.accept( aConnectionPipe
) != osl_Pipe_E_None
)
811 printf("pipe accept failed!");
816 sal_Int32 nChars
= aConnectionPipe
.write( m_pTestString1
.getStr(), m_pTestString1
.getLength() + 1 );
819 printf("server write failed! \n");
823 nChars
= aConnectionPipe
.recv( buf
, 256 );
827 printf("server receive failed! \n");
830 buf
[sizeof(buf
)-1] = '\0';
831 printf("received message is: %s\n", buf
);
838 /** testing the method: read/write/send/recv and Pipe::accept
840 class recv
: public CppUnit::TestFixture
848 Pipe_DataSource_Thread myDataSourceThread
;
849 Pipe_DataSink_Thread myDataSinkThread
;
850 myDataSourceThread
.create( );
852 myDataSinkThread
.create( );
854 //wait until the thread terminate
855 myDataSinkThread
.join( );
856 myDataSourceThread
.join( );
858 int nCompare1
= strcmp( myDataSinkThread
.buf
, m_pTestString1
.getStr() );
859 int nCompare2
= strcmp( myDataSourceThread
.buf
, m_pTestString2
.getStr() );
860 CPPUNIT_ASSERT_EQUAL_MESSAGE( "test send/recv/write/read.", 0, nCompare1
);
861 CPPUNIT_ASSERT_EQUAL_MESSAGE( "test send/recv/write/read.", 0, nCompare2
);
863 //close pipe when accept
868 Pipe_DataSource_Thread myDataSourceThread
;
869 Pipe_DataSink_Thread myDataSinkThread
;
870 myDataSourceThread
.create( );
872 myDataSourceThread
.aListenPipe
.close();
873 myDataSourceThread
.join( );
874 //no condition judgment here, if the case could finish executing within 1 or 2 seconds, it passes.
877 CPPUNIT_TEST_SUITE( recv
);
878 CPPUNIT_TEST( recv_001
);
879 CPPUNIT_TEST( recv_002
);
880 CPPUNIT_TEST_SUITE_END( );
883 CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::ctors
);
884 //CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::assign);
885 CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::recv
);
887 } // namespace osl_StreamPipe
889 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */