Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sal / qa / osl / pipe / osl_Pipe.cxx
blobeef77e9983808cec52b412ceb04d4304e5aebed1
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 <rtl/ustring.hxx>
29 #include <osl/test/uniquepipename.hxx>
30 #include <osl/thread.hxx>
32 #include <osl/pipe.hxx>
33 #include <osl/time.h>
35 #ifdef UNX
36 #include <unistd.h>
37 #endif
38 #include <string.h>
40 using namespace osl;
42 /** print last error of pipe system.
44 static void printPipeError( ::osl::Pipe const & aPipe )
46 oslPipeError nError = aPipe.getError( );
47 printf("#printPipeError# " );
48 switch ( nError ) {
49 case osl_Pipe_E_None:
50 printf("Success!\n" );
51 break;
52 case osl_Pipe_E_NotFound:
53 printf("The returned error is: Not found!\n" );
54 break;
55 case osl_Pipe_E_AlreadyExists:
56 printf("The returned error is: Already exist!\n" );
57 break;
58 case osl_Pipe_E_NoProtocol:
59 printf("The returned error is: No protocol!\n" );
60 break;
61 case osl_Pipe_E_NetworkReset:
62 printf("The returned error is: Network reset!\n" );
63 break;
64 case osl_Pipe_E_ConnectionAbort:
65 printf("The returned error is: Connection aborted!\n" );
66 break;
67 case osl_Pipe_E_ConnectionReset:
68 printf("The returned error is: Connection reset!\n" );
69 break;
70 case osl_Pipe_E_NoBufferSpace:
71 printf("The returned error is: No buffer space!\n" );
72 break;
73 case osl_Pipe_E_TimedOut:
74 printf("The returned error is: Timeout!\n" );
75 break;
76 case osl_Pipe_E_ConnectionRefused:
77 printf("The returned error is: Connection refused!\n" );
78 break;
79 case osl_Pipe_E_invalidError:
80 printf("The returned error is: Invalid error!\n" );
81 break;
82 default:
83 printf("The returned error is: Number %d, Unknown Error\n", nError );
84 break;
88 // pipe name and transfer contents
90 constexpr OUStringLiteral aTestPipeName(u"testpipe2");
91 constexpr OUStringLiteral aTestPipe1(u"testpipe1");
93 constexpr OStringLiteral m_pTestString1("Sun Microsystems");
94 constexpr OStringLiteral m_pTestString2("test pipe PASS/OK");
96 // test code start here
98 namespace osl_Pipe
101 // most return value -1 denote a fail of operation.
103 #define OSL_PIPE_FAIL -1
105 /** testing the methods:
106 inline Pipe();
107 inline Pipe(const OUString& strName, oslPipeOptions Options);
108 inline Pipe(const OUString& strName, oslPipeOptions Options,const Security & rSecurity);
109 inline Pipe(const Pipe& pipe);
110 inline Pipe(oslPipe pipe, __sal_NoAcquire noacquire );
111 inline Pipe(oslPipe Pipe);
113 class ctors : public CppUnit::TestFixture
115 public:
116 bool bRes, bRes1, bRes2;
118 void ctors_none( )
120 ::osl::Pipe aPipe;
121 bRes = aPipe.is( );
123 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, yet no case to test.",
124 !bRes );
127 void ctors_name_option( )
129 /// create a named pipe.
130 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
131 ::osl::Pipe aAssignPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
133 bRes = aPipe.is( ) && aAssignPipe.is( );
135 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.",
136 bRes );
139 void ctors_name_option_security( )
141 /// create a security pipe.
142 const ::osl::Security rSecurity;
143 ::osl::Pipe aSecurityPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity );
145 bRes = aSecurityPipe.is( );
147 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name, option and security, the test of security is not implemented yet.",
148 bRes );
151 void ctors_copy( )
153 /// create a pipe.
154 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
155 /// create a pipe using copy constructor.
156 ::osl::Pipe aCopyPipe( aPipe );
158 bRes = aCopyPipe.is( ) && aCopyPipe == aPipe;
160 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.",
161 bRes );
164 /* Note: DO NOT DO THIS - I have very deliberately caused send to FAIL, *on purpose* as this is the
165 only sane way to test noacquire. This is a terrible misuse of no-acquire, but in this case is
166 needed only so we can test to make sure no-acquire is working!
168 void ctors_no_acquire( )
170 /// create a pipe.
171 OUString aPipeName(test::uniquePipeName(aTestPipeName));
172 oslPipe handle(osl_createPipe(aPipeName.pData, osl_Pipe_CREATE, nullptr));
173 /// constructs a pipe reference without acquiring the handle.
174 std::unique_ptr<osl::Pipe> xNoAcquirePipe(new osl::Pipe(handle, SAL_NO_ACQUIRE));
176 StreamPipe aStreamPipe(handle);
177 xNoAcquirePipe.reset();
178 int nRet = aStreamPipe.send("a", 1);
180 if (nRet >= 0)
181 bRes = false;
182 else
183 bRes = true;
185 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no acquire of handle, deleted nonacquired pipe but could still send on original pipe!.",
186 bRes );
189 void ctors_acquire( )
191 /// create a base pipe.
192 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
193 /// constructs two pipes, the second acquires the first pipe's handle.
194 ::osl::Pipe aAcquirePipe( aPipe.getHandle( ) );
195 ::osl::Pipe aAcquirePipe1( nullptr );
197 bRes = aAcquirePipe.is();
198 bRes1 = aAcquirePipe1.is();
199 bRes2 = aPipe == aAcquirePipe;
201 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with acquire of handle, original pipe does not exist.",
202 bRes );
203 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with acquire of handle, copied pipe does not exist",
204 !bRes1 );
206 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test pipes should have same handle", bRes2);
209 CPPUNIT_TEST_SUITE( ctors );
210 CPPUNIT_TEST( ctors_none );
211 CPPUNIT_TEST( ctors_name_option );
212 CPPUNIT_TEST( ctors_name_option_security );
213 CPPUNIT_TEST( ctors_copy );
214 CPPUNIT_TEST( ctors_no_acquire );
215 CPPUNIT_TEST( ctors_acquire );
216 CPPUNIT_TEST_SUITE_END( );
219 /** testing the method:
220 inline sal_Bool SAL_CALL is() const;
222 class is : public CppUnit::TestFixture
224 public:
225 void is_001( )
227 ::osl::Pipe aPipe;
229 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), check if the pipe is a valid one.", !aPipe.is( ) );
232 void is_002( )
234 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
236 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), a normal pipe creation.", aPipe.is( ) );
239 void is_003( )
241 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
242 aPipe.clear( );
244 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid case.", !aPipe.is( ) );
247 void is_004( )
249 ::osl::Pipe aPipe( nullptr );
251 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid constructor.", !aPipe.is( ) );
254 CPPUNIT_TEST_SUITE( is );
255 CPPUNIT_TEST( is_001 );
256 CPPUNIT_TEST( is_002 );
257 CPPUNIT_TEST( is_003 );
258 CPPUNIT_TEST( is_004 );
259 CPPUNIT_TEST_SUITE_END( );
262 /** testing the methods:
263 inline sal_Bool create( const OUString & strName,
264 oslPipeOptions Options, const Security &rSec );
265 nline sal_Bool create( const OUString & strName,
266 oslPipeOptions Options = osl_Pipe_OPEN );
268 class create : public CppUnit::TestFixture
270 public:
271 bool bRes, bRes1;
273 /** tester comment:
275 security create only be tested creation, security section is
276 untested yet.
279 void create_named_security_001( )
281 const Security rSec;
282 ::osl::Pipe aPipe;
283 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec );
284 bRes1 = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec );
285 aPipe.clear( );
287 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
288 bRes);
289 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
290 !bRes1);
293 void create_named_security_002( )
295 const Security rSec;
296 ::osl::Pipe aPipe, aPipe1;
297 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec );
298 bRes1 = aPipe1.create( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN, rSec );
299 aPipe.clear( );
301 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
302 bRes);
303 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
304 bRes1);
307 void create_named_001( )
309 ::osl::Pipe aPipe;
310 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
311 bRes1 = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
312 aPipe.clear( );
314 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
315 bRes);
316 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
317 !bRes1);
320 void create_named_002( )
322 ::osl::Pipe aPipe, aPipe1;
323 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
324 bRes1 = aPipe1.create( test::uniquePipeName(aTestPipeName) );
325 aPipe.clear( );
327 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
328 bRes);
329 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
330 bRes1);
333 void create_named_003( )
335 ::osl::Pipe aPipe;
336 bRes = aPipe.create( test::uniquePipeName(aTestPipeName) );
337 aPipe.clear( );
339 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test default option is open.",
340 !bRes );
343 CPPUNIT_TEST_SUITE( create );
344 CPPUNIT_TEST( create_named_security_001 );
345 CPPUNIT_TEST( create_named_security_002 );
346 CPPUNIT_TEST( create_named_001 );
347 CPPUNIT_TEST( create_named_002 );
348 CPPUNIT_TEST( create_named_003 );
349 CPPUNIT_TEST_SUITE_END( );
352 /** testing the method:
353 inline void SAL_CALL clear();
355 class clear : public CppUnit::TestFixture
357 public:
358 bool bRes, bRes1;
360 void clear_001( )
362 ::osl::Pipe aPipe;
363 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
364 aPipe.clear( );
365 bRes = aPipe.is( );
367 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test clear.",
368 !bRes );
371 CPPUNIT_TEST_SUITE( clear );
372 CPPUNIT_TEST( clear_001 );
373 CPPUNIT_TEST_SUITE_END( );
376 /** testing the methods:
377 inline Pipe& SAL_CALL operator= (const Pipe& pipe);
378 inline Pipe& SAL_CALL operator= (const oslPipe pipe );
380 class assign : public CppUnit::TestFixture
382 public:
383 bool bRes, bRes1;
385 void assign_ref( )
387 ::osl::Pipe aPipe, aPipe1;
388 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
389 aPipe1 = aPipe;
390 bRes = aPipe1.is( );
391 bRes1 = aPipe == aPipe1;
392 aPipe.close( );
393 aPipe1.close( );
395 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.",
396 bRes );
397 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.",
398 bRes1 );
401 void assign_handle( )
403 ::osl::Pipe aPipe, aPipe1;
404 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
405 aPipe1 = aPipe.getHandle( );
406 bRes = aPipe1.is( );
407 bRes1 = aPipe == aPipe1;
408 aPipe.close( );
409 aPipe1.close( );
411 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle.",
412 bRes );
413 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle.",
414 bRes1 );
417 CPPUNIT_TEST_SUITE( assign );
418 CPPUNIT_TEST( assign_ref );
419 CPPUNIT_TEST( assign_handle );
420 CPPUNIT_TEST_SUITE_END( );
423 /** testing the method:
424 inline sal_Bool SAL_CALL isValid() const;
425 isValid( ) has not been implemented under the following platforms, please refer to osl/pipe.hxx
428 /** testing the method:
429 inline sal_Bool SAL_CALL operator==( const Pipe& rPipe ) const;
431 class isEqual : public CppUnit::TestFixture
433 public:
434 bool bRes, bRes1;
436 void isEqual_001( )
438 ::osl::Pipe aPipe;
439 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
440 bRes = aPipe == aPipe; // NOLINT(misc-redundant-expression)
441 aPipe.close( );
443 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(), compare itself.",
444 bRes );
447 void isEqual_002( )
449 ::osl::Pipe aPipe, aPipe1, aPipe2;
450 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
452 aPipe1 = aPipe;
453 aPipe2.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
455 bRes = aPipe == aPipe1;
456 bRes1 = aPipe == aPipe2;
457 aPipe.close( );
458 aPipe1.close( );
459 aPipe2.close( );
461 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(),create one copy instance, and compare.",
462 bRes );
463 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(),create one copy instance, and compare.",
464 !bRes1 );
467 CPPUNIT_TEST_SUITE( isEqual );
468 CPPUNIT_TEST( isEqual_001 );
469 CPPUNIT_TEST( isEqual_002 );
470 CPPUNIT_TEST_SUITE_END( );
473 /** testing the method:
474 inline void SAL_CALL close();
476 class close : public CppUnit::TestFixture
478 public:
479 bool bRes, bRes1;
481 void close_001( )
483 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE );
484 aPipe.close( );
485 bRes = aPipe.is( );
487 aPipe.clear( );
488 bRes1 = aPipe.is( );
490 CPPUNIT_ASSERT_MESSAGE( "#test comment#: difference between close and clear.",
491 bRes);
492 CPPUNIT_ASSERT_MESSAGE( "#test comment#: difference between close and clear.",
493 !bRes1);
496 void close_002( )
498 ::osl::StreamPipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE );
499 aPipe.close( );
500 int nRet = aPipe.send( m_pTestString1.getStr(), 3 );
502 CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: use after close.",
503 OSL_PIPE_FAIL, nRet );
506 CPPUNIT_TEST_SUITE( close );
507 CPPUNIT_TEST( close_001 );
508 CPPUNIT_TEST( close_002 );
509 CPPUNIT_TEST_SUITE_END( );
512 /** testing the method:
513 inline oslPipeError SAL_CALL accept(StreamPipe& Connection);
514 please refer to StreamPipe::recv
517 /** testing the method:
518 inline oslPipeError SAL_CALL getError() const;
520 class getError : public CppUnit::TestFixture
522 public:
523 bool bRes, bRes1;
525 void getError_001( )
527 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
528 oslPipeError nError = aPipe.getError( );
529 printPipeError( aPipe );
530 aPipe.clear( );
532 CPPUNIT_ASSERT_MESSAGE( "#test comment#: open a non-exist pipe.",
533 nError != osl_Pipe_E_None );
536 void getError_002( )
538 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
539 ::osl::Pipe aPipe1( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
540 oslPipeError nError = aPipe.getError( );
541 printPipeError( aPipe );
542 aPipe.clear( );
543 aPipe1.clear( );
545 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create an already exist pipe.",
546 nError != osl_Pipe_E_None );
549 CPPUNIT_TEST_SUITE( getError );
550 CPPUNIT_TEST( getError_001 );
551 CPPUNIT_TEST( getError_002 );
552 CPPUNIT_TEST_SUITE_END( );
555 /** testing the method:
556 inline oslPipe SAL_CALL getHandle() const;
558 class getHandle : public CppUnit::TestFixture
560 public:
561 bool bRes, bRes1;
563 void getHandle_equalityOperatorAgainstSelf( )
565 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
566 bRes = aPipe == aPipe.getHandle( );
567 aPipe.clear( );
569 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe should equal to its handle.",
570 bRes );
573 void getHandle_equalityOperatorAgainstDerivedPipe( )
575 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
576 ::osl::Pipe aPipe1( aPipe.getHandle( ) );
577 bRes = aPipe == aPipe1;
578 aPipe.clear( );
579 aPipe1.clear( );
581 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe derived from another pipe's handle.",
582 bRes );
585 CPPUNIT_TEST_SUITE( getHandle );
586 CPPUNIT_TEST( getHandle_equalityOperatorAgainstSelf );
587 CPPUNIT_TEST( getHandle_equalityOperatorAgainstDerivedPipe );
588 CPPUNIT_TEST_SUITE_END( );
591 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::ctors);
592 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::is);
593 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::create);
594 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::clear);
595 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::assign);
596 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::isEqual);
597 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::close);
598 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::getError);
599 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Pipe::getHandle);
601 } // namespace osl_Pipe
603 namespace osl_StreamPipe
606 /** testing the methods:
607 inline StreamPipe();
608 inline StreamPipe(oslPipe Pipe);
609 inline StreamPipe(const StreamPipe& Pipe);
610 inline StreamPipe(const OUString& strName, oslPipeOptions Options = osl_Pipe_OPEN);
611 inline StreamPipe(const OUString& strName, oslPipeOptions Options, const Security &rSec );
612 inline StreamPipe( oslPipe pipe, __sal_NoAcquire noacquire );
614 class ctors : public CppUnit::TestFixture
616 public:
617 bool bRes, bRes1;
619 void ctors_none( )
621 // create a pipe.
622 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
623 // create an unattached pipe.
624 ::osl::StreamPipe aStreamPipe1;
625 bRes = aStreamPipe1.is( );
627 // assign it and check.
628 aStreamPipe1 = aStreamPipe;
629 bRes1 = aStreamPipe1.is( );
630 aStreamPipe.clear( );
631 aStreamPipe1.clear( );
633 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, before and after assign.",
634 !bRes );
635 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, before and after assign.",
636 bRes1 );
639 void ctors_handle( )
641 // create a pipe.
642 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
643 // create a pipe with last handle.
644 ::osl::StreamPipe aStreamPipe1( aStreamPipe.getHandle( ) );
645 bRes = aStreamPipe1.is( ) && aStreamPipe == aStreamPipe1;
646 aStreamPipe.clear( );
647 aStreamPipe1.clear( );
649 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with other's handle.",
650 bRes );
653 void ctors_copy( )
655 // create a pipe.
656 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
657 // create an unattached pipe.
658 ::osl::StreamPipe aStreamPipe1( aStreamPipe );
659 bRes = aStreamPipe1.is( ) && aStreamPipe == aStreamPipe1;
660 aStreamPipe.clear( );
661 aStreamPipe1.clear( );
663 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.",
664 bRes );
667 void ctors_name_option( )
669 // create a pipe.
670 ::osl::StreamPipe aStreamPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
671 // create an unattached pipe.
672 ::osl::StreamPipe aStreamPipe1( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
673 bRes = aStreamPipe1.is( ) && aStreamPipe.is( );
674 aStreamPipe.clear( );
675 aStreamPipe1.clear( );
677 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.",
678 bRes );
681 void ctors_name_option_security( )
683 /// create a security pipe.
684 const ::osl::Security rSecurity;
685 ::osl::StreamPipe aSecurityPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity );
687 bRes = aSecurityPipe.is( );
688 aSecurityPipe.clear( );
690 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name, option and security, the test of security is not implemented yet.",
691 bRes );
694 /** tester comment:
696 When test the following constructor, don't know how to test the
697 acquire and no acquire action. possible plans:
698 1.release one handle and check the other( did not success since the
699 other still exist and valid. )
700 2. release one handle twice to see getLastError( )(the getLastError
701 always returns invalidError(LINUX)).
704 void ctors_no_acquire( )
706 // create a pipe.
707 ::osl::StreamPipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
708 osl_acquirePipe(aPipe.getHandle());
709 // constructs a pipe reference without acquiring the handle.
710 ::osl::StreamPipe aNoAcquirePipe( aPipe.getHandle( ), SAL_NO_ACQUIRE );
712 bRes = aNoAcquirePipe.is( );
713 aPipe.clear( );
715 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no acquire of handle, only validation test, do not know how to test no acquire.",
716 bRes );
719 CPPUNIT_TEST_SUITE( ctors );
720 CPPUNIT_TEST( ctors_none );
721 CPPUNIT_TEST( ctors_handle );
722 CPPUNIT_TEST( ctors_copy );
723 CPPUNIT_TEST( ctors_name_option );
724 CPPUNIT_TEST( ctors_name_option_security );
725 CPPUNIT_TEST( ctors_no_acquire );
726 CPPUNIT_TEST_SUITE_END( );
729 /** testing the methods:
730 inline StreamPipe & SAL_CALL operator=(oslPipe Pipe);
731 inline StreamPipe& SAL_CALL operator=(const Pipe& pipe);
732 mindy: not implemented in osl/pipe.hxx, so remove the cases
735 /** wait _nSec seconds.
737 static void thread_sleep( sal_uInt32 _nSec )
739 /// print statement in thread process must use fflush() to force display.
740 fflush(stdout);
742 osl::Thread::wait(std::chrono::seconds(_nSec));
744 // test read/write & send/recv data to pipe
746 namespace {
748 class Pipe_DataSink_Thread : public Thread
750 public:
751 char buf[256];
752 Pipe_DataSink_Thread( ) { }
754 protected:
755 void SAL_CALL run( ) override
757 printf("open pipe\n");
758 ::osl::StreamPipe aSenderPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN ); // test::uniquePipeName(aTestPipeName) is a string = "TestPipe"
759 if ( !aSenderPipe.is() )
761 printf("pipe open failed! \n");
763 else
765 printf("read\n");
766 sal_Int32 nChars = aSenderPipe.read( buf, m_pTestString1.getLength() + 1 );
767 if ( nChars < 0 )
769 printf("read failed! \n");
770 return;
772 buf[sizeof(buf)-1] = '\0';
773 printf("buffer is %s \n", buf);
774 printf("send\n");
775 nChars = aSenderPipe.send( m_pTestString2.getStr(), m_pTestString2.getLength() + 1 );
776 if ( nChars < 0 )
778 printf("client send failed! \n");
779 return;
786 class Pipe_DataSource_Thread : public Thread
788 public:
789 char buf[256];
790 ::osl::Pipe aListenPipe;
791 ::osl::StreamPipe aConnectionPipe;
792 Pipe_DataSource_Thread( )
794 printf("create pipe\n");
795 aListenPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
797 virtual ~Pipe_DataSource_Thread( ) override
799 aListenPipe.close();
801 protected:
802 void SAL_CALL run( ) override
804 //create pipe.
805 printf("listen\n");
806 if ( !aListenPipe.is() )
808 printf("pipe create failed! \n");
810 else
812 //start server and wait for connection.
813 printf("accept\n");
814 if ( aListenPipe.accept( aConnectionPipe ) != osl_Pipe_E_None )
816 printf("pipe accept failed!");
817 return;
819 printf("write\n");
820 // write to pipe
821 sal_Int32 nChars = aConnectionPipe.write( m_pTestString1.getStr(), m_pTestString1.getLength() + 1 );
822 if ( nChars < 0)
824 printf("server write failed! \n");
825 return;
827 printf("recv\n");
828 nChars = aConnectionPipe.recv( buf, 256 );
830 if ( nChars < 0)
832 printf("server receive failed! \n");
833 return;
835 buf[sizeof(buf)-1] = '\0';
836 printf("received message is: %s\n", buf );
843 /** testing the method: read/write/send/recv and Pipe::accept
845 class recv : public CppUnit::TestFixture
847 public:
848 bool bRes, bRes1;
850 void recv_001( )
852 //launch threads.
853 Pipe_DataSource_Thread myDataSourceThread;
854 Pipe_DataSink_Thread myDataSinkThread;
855 myDataSourceThread.create( );
856 thread_sleep( 1 );
857 myDataSinkThread.create( );
859 //wait until the thread terminate
860 myDataSinkThread.join( );
861 myDataSourceThread.join( );
863 int nCompare1 = strcmp( myDataSinkThread.buf, m_pTestString1.getStr() );
864 int nCompare2 = strcmp( myDataSourceThread.buf, m_pTestString2.getStr() );
865 CPPUNIT_ASSERT_EQUAL_MESSAGE( "test send/recv/write/read.", 0, nCompare1 );
866 CPPUNIT_ASSERT_EQUAL_MESSAGE( "test send/recv/write/read.", 0, nCompare2 );
868 //close pipe when accept
869 void recv_002()
871 thread_sleep( 1 );
873 Pipe_DataSource_Thread myDataSourceThread;
874 Pipe_DataSink_Thread myDataSinkThread;
875 myDataSourceThread.create( );
876 thread_sleep( 1 );
877 myDataSourceThread.aListenPipe.close();
878 myDataSourceThread.join( );
879 //no condition judgment here, if the case could finish executing within 1 or 2 seconds, it passes.
882 CPPUNIT_TEST_SUITE( recv );
883 CPPUNIT_TEST( recv_001 );
884 CPPUNIT_TEST( recv_002 );
885 CPPUNIT_TEST_SUITE_END( );
888 CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::ctors);
889 //CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::assign);
890 CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::recv);
892 } // namespace osl_StreamPipe
894 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */