sw a11y: clang-format SidebarWinAccessible code
[LibreOffice.git] / sal / qa / osl / pipe / osl_Pipe.cxx
blob62463f8868dc1f7622046c62274ec4b7f3a4cc84
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 <rtl/ustring.hxx>
28 #include <osl/test/uniquepipename.hxx>
29 #include <osl/thread.hxx>
31 #include <osl/pipe.hxx>
33 #include <string.h>
35 using namespace osl;
37 /** print last error of pipe system.
39 static void printPipeError( ::osl::Pipe const & aPipe )
41 oslPipeError nError = aPipe.getError( );
42 printf("#printPipeError# " );
43 switch ( nError ) {
44 case osl_Pipe_E_None:
45 printf("Success!\n" );
46 break;
47 case osl_Pipe_E_NotFound:
48 printf("The returned error is: Not found!\n" );
49 break;
50 case osl_Pipe_E_AlreadyExists:
51 printf("The returned error is: Already exist!\n" );
52 break;
53 case osl_Pipe_E_NoProtocol:
54 printf("The returned error is: No protocol!\n" );
55 break;
56 case osl_Pipe_E_NetworkReset:
57 printf("The returned error is: Network reset!\n" );
58 break;
59 case osl_Pipe_E_ConnectionAbort:
60 printf("The returned error is: Connection aborted!\n" );
61 break;
62 case osl_Pipe_E_ConnectionReset:
63 printf("The returned error is: Connection reset!\n" );
64 break;
65 case osl_Pipe_E_NoBufferSpace:
66 printf("The returned error is: No buffer space!\n" );
67 break;
68 case osl_Pipe_E_TimedOut:
69 printf("The returned error is: Timeout!\n" );
70 break;
71 case osl_Pipe_E_ConnectionRefused:
72 printf("The returned error is: Connection refused!\n" );
73 break;
74 case osl_Pipe_E_invalidError:
75 printf("The returned error is: Invalid error!\n" );
76 break;
77 default:
78 printf("The returned error is: Number %d, Unknown Error\n", nError );
79 break;
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
93 namespace osl_Pipe
96 // most return value -1 denote a fail of operation.
98 #define OSL_PIPE_FAIL -1
100 /** testing the methods:
101 inline Pipe();
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
110 public:
111 bool bRes, bRes1, bRes2;
113 void ctors_none( )
115 ::osl::Pipe aPipe;
116 bRes = aPipe.is( );
118 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, yet no case to test.",
119 !bRes );
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.",
131 bRes );
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.",
143 bRes );
146 void ctors_copy( )
148 /// create a pipe.
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.",
156 bRes );
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( )
165 /// create a pipe.
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);
175 if (nRet >= 0)
176 bRes = false;
177 else
178 bRes = true;
180 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no acquire of handle, deleted nonacquired pipe but could still send on original pipe!.",
181 bRes );
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.",
197 bRes );
198 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with acquire of handle, copied pipe does not exist",
199 !bRes1 );
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
219 public:
220 void is_001( )
222 ::osl::Pipe aPipe;
224 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), check if the pipe is a valid one.", !aPipe.is( ) );
227 void is_002( )
229 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
231 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), a normal pipe creation.", aPipe.is( ) );
234 void is_003( )
236 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
237 aPipe.clear( );
239 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid case.", !aPipe.is( ) );
242 void is_004( )
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
265 public:
266 bool bRes, bRes1;
268 /** tester comment:
270 security create only be tested creation, security section is
271 untested yet.
274 void create_named_security_001( )
276 const Security rSec;
277 ::osl::Pipe aPipe;
278 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec );
279 bRes1 = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSec );
280 aPipe.clear( );
282 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
283 bRes);
284 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
285 !bRes1);
288 void create_named_security_002( )
290 const Security rSec;
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 );
294 aPipe.clear( );
296 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
297 bRes);
298 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
299 bRes1);
302 void create_named_001( )
304 ::osl::Pipe aPipe;
305 bRes = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
306 bRes1 = aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
307 aPipe.clear( );
309 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
310 bRes);
311 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
312 !bRes1);
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) );
320 aPipe.clear( );
322 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
323 bRes);
324 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
325 bRes1);
328 void create_named_003( )
330 ::osl::Pipe aPipe;
331 bRes = aPipe.create( test::uniquePipeName(aTestPipeName) );
332 aPipe.clear( );
334 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test default option is open.",
335 !bRes );
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
352 public:
353 bool bRes, bRes1;
355 void clear_001( )
357 ::osl::Pipe aPipe;
358 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
359 aPipe.clear( );
360 bRes = aPipe.is( );
362 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test clear.",
363 !bRes );
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
377 public:
378 bool bRes, bRes1;
380 void assign_ref( )
382 ::osl::Pipe aPipe, aPipe1;
383 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
384 aPipe1 = aPipe;
385 bRes = aPipe1.is( );
386 bRes1 = aPipe == aPipe1;
387 aPipe.close( );
388 aPipe1.close( );
390 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.",
391 bRes );
392 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.",
393 bRes1 );
396 void assign_handle( )
398 ::osl::Pipe aPipe, aPipe1;
399 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
400 aPipe1 = aPipe.getHandle( );
401 bRes = aPipe1.is( );
402 bRes1 = aPipe == aPipe1;
403 aPipe.close( );
404 aPipe1.close( );
406 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle.",
407 bRes );
408 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle.",
409 bRes1 );
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
428 public:
429 bool bRes, bRes1;
431 void isEqual_001( )
433 ::osl::Pipe aPipe;
434 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
435 bRes = aPipe == aPipe; // NOLINT(misc-redundant-expression)
436 aPipe.close( );
438 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(), compare itself.",
439 bRes );
442 void isEqual_002( )
444 ::osl::Pipe aPipe, aPipe1, aPipe2;
445 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
447 aPipe1 = aPipe;
448 aPipe2.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
450 bRes = aPipe == aPipe1;
451 bRes1 = aPipe == aPipe2;
452 aPipe.close( );
453 aPipe1.close( );
454 aPipe2.close( );
456 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(),create one copy instance, and compare.",
457 bRes );
458 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(),create one copy instance, and compare.",
459 !bRes1 );
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
473 public:
474 bool bRes, bRes1;
476 void close_001( )
478 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE );
479 aPipe.close( );
480 bRes = aPipe.is( );
482 aPipe.clear( );
483 bRes1 = aPipe.is( );
485 CPPUNIT_ASSERT_MESSAGE( "#test comment#: difference between close and clear.",
486 bRes);
487 CPPUNIT_ASSERT_MESSAGE( "#test comment#: difference between close and clear.",
488 !bRes1);
491 void close_002( )
493 ::osl::StreamPipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE );
494 aPipe.close( );
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
517 public:
518 bool bRes, bRes1;
520 void getError_001( )
522 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
523 oslPipeError nError = aPipe.getError( );
524 printPipeError( aPipe );
525 aPipe.clear( );
527 CPPUNIT_ASSERT_MESSAGE( "#test comment#: open a non-exist pipe.",
528 nError != osl_Pipe_E_None );
531 void getError_002( )
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 );
537 aPipe.clear( );
538 aPipe1.clear( );
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
555 public:
556 bool bRes, bRes1;
558 void getHandle_equalityOperatorAgainstSelf( )
560 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
561 bRes = aPipe == aPipe.getHandle( );
562 aPipe.clear( );
564 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe should equal to its handle.",
565 bRes );
568 void getHandle_equalityOperatorAgainstDerivedPipe( )
570 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
571 ::osl::Pipe aPipe1( aPipe.getHandle( ) );
572 bRes = aPipe == aPipe1;
573 aPipe.clear( );
574 aPipe1.clear( );
576 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe derived from another pipe's handle.",
577 bRes );
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:
602 inline StreamPipe();
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
611 public:
612 bool bRes, bRes1;
614 void ctors_none( )
616 // create a pipe.
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.",
629 !bRes );
630 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, before and after assign.",
631 bRes1 );
634 void ctors_handle( )
636 // create a pipe.
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.",
645 bRes );
648 void ctors_copy( )
650 // create a pipe.
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.",
659 bRes );
662 void ctors_name_option( )
664 // create a pipe.
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.",
673 bRes );
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.",
686 bRes );
689 /** tester comment:
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( )
701 // create a pipe.
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( );
708 aPipe.clear( );
710 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no acquire of handle, only validation test, do not know how to test no acquire.",
711 bRes );
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.
735 fflush(stdout);
737 osl::Thread::wait(std::chrono::seconds(_nSec));
739 // test read/write & send/recv data to pipe
741 namespace {
743 class Pipe_DataSink_Thread : public Thread
745 public:
746 char buf[256];
747 Pipe_DataSink_Thread( ) { }
749 protected:
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");
758 else
760 printf("read\n");
761 sal_Int32 nChars = aSenderPipe.read( buf, m_pTestString1.getLength() + 1 );
762 if ( nChars < 0 )
764 printf("read failed! \n");
765 return;
767 buf[sizeof(buf)-1] = '\0';
768 printf("buffer is %s \n", buf);
769 printf("send\n");
770 nChars = aSenderPipe.send( m_pTestString2.getStr(), m_pTestString2.getLength() + 1 );
771 if ( nChars < 0 )
773 printf("client send failed! \n");
774 return;
781 class Pipe_DataSource_Thread : public Thread
783 public:
784 char buf[256];
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
794 aListenPipe.close();
796 protected:
797 void SAL_CALL run( ) override
799 //create pipe.
800 printf("listen\n");
801 if ( !aListenPipe.is() )
803 printf("pipe create failed! \n");
805 else
807 //start server and wait for connection.
808 printf("accept\n");
809 if ( aListenPipe.accept( aConnectionPipe ) != osl_Pipe_E_None )
811 printf("pipe accept failed!");
812 return;
814 printf("write\n");
815 // write to pipe
816 sal_Int32 nChars = aConnectionPipe.write( m_pTestString1.getStr(), m_pTestString1.getLength() + 1 );
817 if ( nChars < 0)
819 printf("server write failed! \n");
820 return;
822 printf("recv\n");
823 nChars = aConnectionPipe.recv( buf, 256 );
825 if ( nChars < 0)
827 printf("server receive failed! \n");
828 return;
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
842 public:
843 bool bRes, bRes1;
845 void recv_001( )
847 //launch threads.
848 Pipe_DataSource_Thread myDataSourceThread;
849 Pipe_DataSink_Thread myDataSinkThread;
850 myDataSourceThread.create( );
851 thread_sleep( 1 );
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
864 void recv_002()
866 thread_sleep( 1 );
868 Pipe_DataSource_Thread myDataSourceThread;
869 Pipe_DataSink_Thread myDataSinkThread;
870 myDataSourceThread.create( );
871 thread_sleep( 1 );
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: */