Debian Codegraph @

sal/qa/osl/pipe/osl_Pipe.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 // include files
21
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>
27
28 #include <osl/test/uniquepipename.hxx>
29 #include <osl/thread.hxx>
30
31 #include <osl/pipe.hxx>
32
33 #include <string.h>
34
35 using namespace osl;
36
37 /** print last error of pipe system.
38 */
39 static void printPipeError( ::osl::Pipe const & aPipe )
40 {
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;
80 }
81 }
82
83 // pipe name and transfer contents
84
85 constexpr OUString aTestPipeName(u"testpipe2"_ustr);
86 constexpr OUString aTestPipe1(u"testpipe1"_ustr);
87
88 constexpr OString m_pTestString1("Sun Microsystems"_ostr);
89 constexpr OString m_pTestString2("test pipe PASS/OK"_ostr);
90
91 // test code start here
92
93 namespace osl_Pipe
94 {
95
96 // most return value -1 denote a fail of operation.
97
98 #define OSL_PIPE_FAIL -1
99
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);
107 */
108 class ctors : public CppUnit::TestFixture
109 {
110 public:
111 bool bRes, bRes1, bRes2;
112
113 void ctors_none( )
114 {
115 ::osl::Pipe aPipe;
116 bRes = aPipe.is( );
117
118 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with no parameter, yet no case to test.",
119 !bRes );
120 }
121
122 void ctors_name_option( )
123 {
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 );
127
128 bRes = aPipe.is( ) && aAssignPipe.is( );
129
130 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.",
131 bRes );
132 }
133
134 void ctors_name_option_security( )
135 {
136 /// create a security pipe.
137 const ::osl::Security rSecurity;
138 ::osl::Pipe aSecurityPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity );
139
140 bRes = aSecurityPipe.is( );
141
142 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name, option and security, the test of security is not implemented yet.",
143 bRes );
144 }
145
146 void ctors_copy( )
147 {
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 );
152
153 bRes = aCopyPipe.is( ) && aCopyPipe == aPipe;
154
155 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.",
156 bRes );
157 }
158
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!
162 */
163 void ctors_no_acquire( )
164 {
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));
170
171 StreamPipe aStreamPipe(handle);
172 xNoAcquirePipe.reset();
173 int nRet = aStreamPipe.send("a", 1);
174
175 if (nRet >= 0)
176 bRes = false;
177 else
178 bRes = true;
179
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 );
182 }
183
184 void ctors_acquire( )
185 {
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 );
191
192 bRes = aAcquirePipe.is();
193 bRes1 = aAcquirePipe1.is();
194 bRes2 = aPipe == aAcquirePipe;
195
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 );
200
201 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test pipes should have same handle", bRes2);
202 }
203
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( );
212 };
213
214 /** testing the method:
215 inline sal_Bool SAL_CALL is() const;
216 */
217 class is : public CppUnit::TestFixture
218 {
219 public:
220 void is_001( )
221 {
222 ::osl::Pipe aPipe;
223
224 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), check if the pipe is a valid one.", !aPipe.is( ) );
225 }
226
227 void is_002( )
228 {
229 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
230
231 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), a normal pipe creation.", aPipe.is( ) );
232 }
233
234 void is_003( )
235 {
236 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
237 aPipe.clear( );
238
239 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid case.", !aPipe.is( ) );
240 }
241
242 void is_004( )
243 {
244 ::osl::Pipe aPipe( nullptr );
245
246 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test is(), an invalid constructor.", !aPipe.is( ) );
247 }
248
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( );
255 };
256
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 );
262 */
263 class create : public CppUnit::TestFixture
264 {
265 public:
266 bool bRes, bRes1;
267
268 /** tester comment:
269
270 security create only be tested creation, security section is
271 untested yet.
272 */
273
274 void create_named_security_001( )
275 {
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( );
281
282 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
283 bRes);
284 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
285 !bRes1);
286 }
287
288 void create_named_security_002( )
289 {
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( );
295
296 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
297 bRes);
298 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
299 bRes1);
300 }
301
302 void create_named_001( )
303 {
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( );
308
309 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
310 bRes);
311 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation.",
312 !bRes1);
313 }
314
315 void create_named_002( )
316 {
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( );
321
322 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
323 bRes);
324 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test creation and open.",
325 bRes1);
326 }
327
328 void create_named_003( )
329 {
330 ::osl::Pipe aPipe;
331 bRes = aPipe.create( test::uniquePipeName(aTestPipeName) );
332 aPipe.clear( );
333
334 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test default option is open.",
335 !bRes );
336 }
337
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( );
345 };
346
347 /** testing the method:
348 inline void SAL_CALL clear();
349 */
350 class clear : public CppUnit::TestFixture
351 {
352 public:
353 bool bRes, bRes1;
354
355 void clear_001( )
356 {
357 ::osl::Pipe aPipe;
358 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
359 aPipe.clear( );
360 bRes = aPipe.is( );
361
362 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test clear.",
363 !bRes );
364 }
365
366 CPPUNIT_TEST_SUITE( clear );
367 CPPUNIT_TEST( clear_001 );
368 CPPUNIT_TEST_SUITE_END( );
369 };
370
371 /** testing the methods:
372 inline Pipe& SAL_CALL operator= (const Pipe& pipe);
373 inline Pipe& SAL_CALL operator= (const oslPipe pipe );
374 */
375 class assign : public CppUnit::TestFixture
376 {
377 public:
378 bool bRes, bRes1;
379
380 void assign_ref( )
381 {
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( );
389
390 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.",
391 bRes );
392 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with reference.",
393 bRes1 );
394 }
395
396 void assign_handle( )
397 {
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( );
405
406 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle.",
407 bRes );
408 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test assign with handle.",
409 bRes1 );
410 }
411
412 CPPUNIT_TEST_SUITE( assign );
413 CPPUNIT_TEST( assign_ref );
414 CPPUNIT_TEST( assign_handle );
415 CPPUNIT_TEST_SUITE_END( );
416 };
417
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
421 */
422
423 /** testing the method:
424 inline sal_Bool SAL_CALL operator==( const Pipe& rPipe ) const;
425 */
426 class isEqual : public CppUnit::TestFixture
427 {
428 public:
429 bool bRes, bRes1;
430
431 void isEqual_001( )
432 {
433 ::osl::Pipe aPipe;
434 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
435 bRes = aPipe == aPipe; // NOLINT(misc-redundant-expression)
436 aPipe.close( );
437
438 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test isEqual(), compare itself.",
439 bRes );
440 }
441
442 void isEqual_002( )
443 {
444 ::osl::Pipe aPipe, aPipe1, aPipe2;
445 aPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
446
447 aPipe1 = aPipe;
448 aPipe2.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
449
450 bRes = aPipe == aPipe1;
451 bRes1 = aPipe == aPipe2;
452 aPipe.close( );
453 aPipe1.close( );
454 aPipe2.close( );
455
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 );
460 }
461
462 CPPUNIT_TEST_SUITE( isEqual );
463 CPPUNIT_TEST( isEqual_001 );
464 CPPUNIT_TEST( isEqual_002 );
465 CPPUNIT_TEST_SUITE_END( );
466 };
467
468 /** testing the method:
469 inline void SAL_CALL close();
470 */
471 class close : public CppUnit::TestFixture
472 {
473 public:
474 bool bRes, bRes1;
475
476 void close_001( )
477 {
478 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE );
479 aPipe.close( );
480 bRes = aPipe.is( );
481
482 aPipe.clear( );
483 bRes1 = aPipe.is( );
484
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);
489 }
490
491 void close_002( )
492 {
493 ::osl::StreamPipe aPipe( test::uniquePipeName(aTestPipe1), osl_Pipe_CREATE );
494 aPipe.close( );
495 int nRet = aPipe.send( m_pTestString1.getStr(), 3 );
496
497 CPPUNIT_ASSERT_EQUAL_MESSAGE( "#test comment#: use after close.",
498 OSL_PIPE_FAIL, nRet );
499 }
500
501 CPPUNIT_TEST_SUITE( close );
502 CPPUNIT_TEST( close_001 );
503 CPPUNIT_TEST( close_002 );
504 CPPUNIT_TEST_SUITE_END( );
505 };
506
507 /** testing the method:
508 inline oslPipeError SAL_CALL accept(StreamPipe& Connection);
509 please refer to StreamPipe::recv
510 */
511
512 /** testing the method:
513 inline oslPipeError SAL_CALL getError() const;
514 */
515 class getError : public CppUnit::TestFixture
516 {
517 public:
518 bool bRes, bRes1;
519
520 void getError_001( )
521 {
522 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
523 oslPipeError nError = aPipe.getError( );
524 printPipeError( aPipe );
525 aPipe.clear( );
526
527 CPPUNIT_ASSERT_MESSAGE( "#test comment#: open a non-exist pipe.",
528 nError != osl_Pipe_E_None );
529 }
530
531 void getError_002( )
532 {
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( );
539
540 CPPUNIT_ASSERT_MESSAGE( "#test comment#: create an already exist pipe.",
541 nError != osl_Pipe_E_None );
542 }
543
544 CPPUNIT_TEST_SUITE( getError );
545 CPPUNIT_TEST( getError_001 );
546 CPPUNIT_TEST( getError_002 );
547 CPPUNIT_TEST_SUITE_END( );
548 };
549
550 /** testing the method:
551 inline oslPipe SAL_CALL getHandle() const;
552 */
553 class getHandle : public CppUnit::TestFixture
554 {
555 public:
556 bool bRes, bRes1;
557
558 void getHandle_equalityOperatorAgainstSelf( )
559 {
560 ::osl::Pipe aPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_OPEN );
561 bRes = aPipe == aPipe.getHandle( );
562 aPipe.clear( );
563
564 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe should equal to its handle.",
565 bRes );
566 }
567
568 void getHandle_equalityOperatorAgainstDerivedPipe( )
569 {
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( );
575
576 CPPUNIT_ASSERT_MESSAGE( "#test comment#: one pipe derived from another pipe's handle.",
577 bRes );
578 }
579
580 CPPUNIT_TEST_SUITE( getHandle );
581 CPPUNIT_TEST( getHandle_equalityOperatorAgainstSelf );
582 CPPUNIT_TEST( getHandle_equalityOperatorAgainstDerivedPipe );
583 CPPUNIT_TEST_SUITE_END( );
584 };
585
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);
595
596 } // namespace osl_Pipe
597
598 namespace osl_StreamPipe
599 {
600
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 );
608 */
609 class ctors : public CppUnit::TestFixture
610 {
611 public:
612 bool bRes, bRes1;
613
614 void ctors_none( )
615 {
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( );
621
622 // assign it and check.
623 aStreamPipe1 = aStreamPipe;
624 bRes1 = aStreamPipe1.is( );
625 aStreamPipe.clear( );
626 aStreamPipe1.clear( );
627
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 );
632 }
633
634 void ctors_handle( )
635 {
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( );
643
644 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with other's handle.",
645 bRes );
646 }
647
648 void ctors_copy( )
649 {
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( );
657
658 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test copy constructor.",
659 bRes );
660 }
661
662 void ctors_name_option( )
663 {
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( );
671
672 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name and option.",
673 bRes );
674 }
675
676 void ctors_name_option_security( )
677 {
678 /// create a security pipe.
679 const ::osl::Security rSecurity;
680 ::osl::StreamPipe aSecurityPipe( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE, rSecurity );
681
682 bRes = aSecurityPipe.is( );
683 aSecurityPipe.clear( );
684
685 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with name, option and security, the test of security is not implemented yet.",
686 bRes );
687 }
688
689 /** tester comment:
690
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)).
697 */
698
699 void ctors_no_acquire( )
700 {
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 );
706
707 bRes = aNoAcquirePipe.is( );
708 aPipe.clear( );
709
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 );
712 }
713
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( );
722 };
723
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
728 */
729
730 /** wait _nSec seconds.
731 */
732 static void thread_sleep( sal_uInt32 _nSec )
733 {
734 /// print statement in thread process must use fflush() to force display.
735 fflush(stdout);
736
737 osl::Thread::wait(std::chrono::seconds(_nSec));
738 }
739 // test read/write & send/recv data to pipe
740
741 namespace {
742
743 class Pipe_DataSink_Thread : public Thread
744 {
745 public:
746 char buf[256];
747 Pipe_DataSink_Thread( ) { }
748
749 protected:
750 void SAL_CALL run( ) override
751 {
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() )
755 {
756 printf("pipe open failed! \n");
757 }
758 else
759 {
760 printf("read\n");
761 sal_Int32 nChars = aSenderPipe.read( buf, m_pTestString1.getLength() + 1 );
762 if ( nChars < 0 )
763 {
764 printf("read failed! \n");
765 return;
766 }
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 )
772 {
773 printf("client send failed! \n");
774 return;
775 }
776 }
777 }
778
779 };
780
781 class Pipe_DataSource_Thread : public Thread
782 {
783 public:
784 char buf[256];
785 ::osl::Pipe aListenPipe;
786 ::osl::StreamPipe aConnectionPipe;
787 Pipe_DataSource_Thread( )
788 {
789 printf("create pipe\n");
790 aListenPipe.create( test::uniquePipeName(aTestPipeName), osl_Pipe_CREATE );
791 }
792 virtual ~Pipe_DataSource_Thread( ) override
793 {
794 aListenPipe.close();
795 }
796 protected:
797 void SAL_CALL run( ) override
798 {
799 //create pipe.
800 printf("listen\n");
801 if ( !aListenPipe.is() )
802 {
803 printf("pipe create failed! \n");
804 }
805 else
806 {
807 //start server and wait for connection.
808 printf("accept\n");
809 if ( aListenPipe.accept( aConnectionPipe ) != osl_Pipe_E_None )
810 {
811 printf("pipe accept failed!");
812 return;
813 }
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)
818 {
819 printf("server write failed! \n");
820 return;
821 }
822 printf("recv\n");
823 nChars = aConnectionPipe.recv( buf, 256 );
824
825 if ( nChars < 0)
826 {
827 printf("server receive failed! \n");
828 return;
829 }
830 buf[sizeof(buf)-1] = '\0';
831 printf("received message is: %s\n", buf );
832 }
833 }
834 };
835
836 }
837
838 /** testing the method: read/write/send/recv and Pipe::accept
839 */
840 class recv : public CppUnit::TestFixture
841 {
842 public:
843 bool bRes, bRes1;
844
845 void recv_001( )
846 {
847 //launch threads.
848 Pipe_DataSource_Thread myDataSourceThread;
849 Pipe_DataSink_Thread myDataSinkThread;
850 myDataSourceThread.create( );
851 thread_sleep( 1 );
852 myDataSinkThread.create( );
853
854 //wait until the thread terminate
855 myDataSinkThread.join( );
856 myDataSourceThread.join( );
857
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 );
862 }
863 //close pipe when accept
864 void recv_002()
865 {
866 thread_sleep( 1 );
867
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.
875 }
876
877 CPPUNIT_TEST_SUITE( recv );
878 CPPUNIT_TEST( recv_001 );
879 CPPUNIT_TEST( recv_002 );
880 CPPUNIT_TEST_SUITE_END( );
881 };
882
883 CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::ctors);
884 //CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::assign);
885 CPPUNIT_TEST_SUITE_REGISTRATION(osl_StreamPipe::recv);
886
887 } // namespace osl_StreamPipe
888
889 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */