kerneltest/e32test/pipe/t_pipe.cpp
changeset 0 a41df078684a
child 43 c1f20ce4abcf
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\pipe\t_pipe.cpp
       
    15 // Overview:
       
    16 // Test pipe mechanism
       
    17 // API Information:
       
    18 // RPipe
       
    19 // Details:
       
    20 // - Create various leagal and illegal pipe and verify that pipe
       
    21 // functionality works as stated in requirements.
       
    22 // - Create Named and UnNamed Pipes and verify.
       
    23 // - Test Pipes communication in multiprocess , multithreaded , single process
       
    24 // single threaded environment.
       
    25 // Platforms/Drives/Compatibility:
       
    26 // All.
       
    27 // Assumptions/Requirement/Pre-requisites:
       
    28 // Refer Pipes design and requirement document.
       
    29 // 1. SGL.GT0314.202 PREQ1460 Design Doc
       
    30 // 2. SGL.GT0314.203 PREQ1460 Pipe Functional Specification
       
    31 // Refer Pipe test specification document.
       
    32 // 1. SGL.GT0314.601 Pipes_Test_Specifications
       
    33 // Failures and causes:
       
    34 // Base Port information:
       
    35 // MMP File:
       
    36 // t_pipe.mmp
       
    37 // 
       
    38 //
       
    39 
       
    40 /**
       
    41  @STMTestCaseID			KBASE-T_PIPE-0217
       
    42  @SYMPREQ				PREQ1460
       
    43  @SYMREQ					REQ6141
       
    44  @SYMCR					CR0923
       
    45  @SYMTestCaseDesc		Pipe functional tests
       
    46  @SYMTestPriority		High
       
    47  @SYMTestActions			Tests the functionality of the pipe. Success and failure tests are performed.
       
    48  @SYMTestExpectedResults	Test should pass
       
    49 */
       
    50 
       
    51 #define __E32TEST_EXTENSION__
       
    52 #include <e32test.h>
       
    53 #include <e32svr.h>
       
    54 #include <e32des8.h>
       
    55 #include <e32des8_private.h>
       
    56 #include <e32cmn.h>
       
    57 #include <e32cmn_private.h>
       
    58 #include <e32math.h>
       
    59 #include <hal.h>
       
    60 
       
    61 #include "RPipe.h"
       
    62 
       
    63 LOCAL_D RTest test(_L("t_pipe"));
       
    64 
       
    65 
       
    66 //if the test is to run under the debugger, uncomment the following line
       
    67 
       
    68 
       
    69 
       
    70 const TInt KHeapSize=0x2000;
       
    71 
       
    72 
       
    73 
       
    74 // Test Data
       
    75 _LIT8(KTestData,"Pipe Data To Be Passed");
       
    76 _LIT8(KTestData1,"P");
       
    77 _LIT8(KTestData2,"Pipe Data To Be Passed");
       
    78 _LIT8(KTestData3,"ipe Data To Be Passed");
       
    79 
       
    80 
       
    81 // Test Pipe Names
       
    82 _LIT(KPipe1Name,"TestPipe1");
       
    83 _LIT(KPipe3Name,"TestPipe3");
       
    84 
       
    85 //Global semaphore name
       
    86 _LIT(KSemaphoreName,"Semaphore1");
       
    87 
       
    88 // Pipename of max pipe length 80 Charecters.
       
    89 _LIT(KMaxPipeName,"abcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijklmnopqrst");
       
    90 // PipeName of max pipe length plus one ,81 charecters
       
    91 _LIT(KMaxPipeNamePlusOne,"abcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijklmnopqrst1");
       
    92 
       
    93 // Thread Name Constants
       
    94 _LIT(KThread2Name, "thread2");
       
    95 _LIT(KThread3Name, "thread3");
       
    96 _LIT(KThread4Name, "thread4");
       
    97 _LIT(KThread5Name, "thread5");
       
    98 _LIT(KReaderThread, "ReaderThread");
       
    99 _LIT(KWriterThread, "WriterThread");
       
   100 _LIT(KThread8Name, "thread8");
       
   101 _LIT(KThread9Name, "thread9");
       
   102 _LIT(KThread11Name, "thread11");
       
   103 
       
   104 // Test Process Name Constants
       
   105 _LIT(KProcessName, "t_pipe2.exe");
       
   106 
       
   107 
       
   108 // Following class is used to pass thread handle information to different threads.
       
   109 class TData
       
   110 	{
       
   111 public:
       
   112 	TData(RPipe* aReadEnd, RPipe *aWriteEnd); 
       
   113 	TData(RPipe* aReadEnd, RPipe *aWriteEnd, const TDesC8* aPipeData, TInt aIterations=1);
       
   114 	RPipe* iReadEnd;
       
   115 	RPipe* iWriteEnd;
       
   116 	const TDesC8* iPipeData;
       
   117 	TInt iIterations;
       
   118 	};
       
   119 
       
   120 TData::TData(RPipe* aReadEnd , RPipe *aWriteEnd)
       
   121 	:iReadEnd(aReadEnd),iWriteEnd(aWriteEnd), iPipeData(NULL), iIterations(NULL)
       
   122 	{}
       
   123 
       
   124 TData::TData(RPipe* aReadEnd , RPipe *aWriteEnd, const TDesC8* aPipeData, TInt aIterations)
       
   125 	:iReadEnd(aReadEnd),iWriteEnd(aWriteEnd), iPipeData(aPipeData), iIterations(aIterations)
       
   126 	{}
       
   127 
       
   128 /**
       
   129 A utility class for running functions in other threads/processes
       
   130 */
       
   131 class TTestRemote
       
   132 	{
       
   133 public:
       
   134 	virtual TInt WaitForExitL()=0;
       
   135 	virtual ~TTestRemote()
       
   136 		{}
       
   137 
       
   138 	virtual void Rendezvous(TRequestStatus& aStatus) =0;
       
   139 
       
   140 protected:
       
   141 	TTestRemote()
       
   142 		{}
       
   143 
       
   144 	static TInt RunFunctor(TAny* aFunctor)
       
   145 		{
       
   146 		TFunctor& functor = *(TFunctor*)aFunctor;
       
   147 		functor();
       
   148 		return KErrNone;
       
   149 		}
       
   150 
       
   151 	TRequestStatus iLogonStatus;
       
   152 	static TInt iCount;
       
   153 	};
       
   154 TInt TTestRemote::iCount=0;
       
   155 
       
   156 class TTestThread : public TTestRemote
       
   157 	{
       
   158 public:
       
   159 	TTestThread(const TDesC& aName, TThreadFunction aFn, TAny* aData, TBool aAutoResume=ETrue)
       
   160 		{
       
   161 		Init(aName, aFn, aData, aAutoResume);
       
   162 		}
       
   163 
       
   164 	/**
       
   165 	Run aFunctor in another thread
       
   166 	*/
       
   167 	TTestThread(const TDesC& aName, TFunctor& aFunctor, TBool aAutoResume=ETrue)
       
   168 		{
       
   169 		Init(aName, RunFunctor, &aFunctor, aAutoResume);
       
   170 		}
       
   171 
       
   172 	~TTestThread()
       
   173 		{
       
   174 		//RTest::CloseHandleAndWaitForDestruction(iThread);
       
   175 		iThread.Close();
       
   176 		}
       
   177 
       
   178 	void Resume()
       
   179 		{
       
   180 		iThread.Resume();
       
   181 		}
       
   182 
       
   183 	/**
       
   184 	If thread exited normally, return its return code
       
   185 	Otherwise, leave with exit reason
       
   186 	*/
       
   187 	virtual TInt WaitForExitL()
       
   188 		{
       
   189 		User::WaitForRequest(iLogonStatus);
       
   190 		const TInt exitType = iThread.ExitType();
       
   191 		const TInt exitReason = iThread.ExitReason();
       
   192 
       
   193 		__ASSERT_ALWAYS(exitType != EExitPending, User::Panic(_L("TTestThread"),0));
       
   194 
       
   195 		if(exitType != EExitKill)
       
   196 			User::Leave(exitReason);
       
   197 
       
   198 		return exitReason;
       
   199 		}
       
   200 
       
   201 	virtual void Rendezvous(TRequestStatus& aStatus)
       
   202 		{
       
   203 		iThread.Rendezvous(aStatus);
       
   204 		}
       
   205 
       
   206 private:
       
   207 	void Init(const TDesC& aName, TThreadFunction aFn, TAny* aData, TBool aAutoResume)
       
   208 		{
       
   209 		TKName name(aName);
       
   210 		name.AppendFormat(_L("-%d"), iCount++);	
       
   211 		TInt r=iThread.Create(name, aFn, KDefaultStackSize, KHeapSize, KHeapSize, aData);
       
   212 		User::LeaveIfError(r);
       
   213 		
       
   214 		iThread.Logon(iLogonStatus);
       
   215 		__ASSERT_ALWAYS(iLogonStatus == KRequestPending, User::Panic(_L("TTestThread"),0));
       
   216 
       
   217 		if(aAutoResume)
       
   218 			iThread.Resume();
       
   219 		}
       
   220 
       
   221 
       
   222 
       
   223 	RThread iThread;
       
   224 	};
       
   225 
       
   226 
       
   227 /**
       
   228 Non blocking reads, verifying data as expected
       
   229 */
       
   230 TInt TestThread2(TAny* aData)
       
   231 	{
       
   232 	RTest test(_L("t_pipe_t2"));
       
   233 
       
   234 	test.Start(_L("Thread 2"));
       
   235 	test.Printf(_L("THREAD 2 called by TestMultiThreadNamedPipes And TestMultiThreadUnNamedPipes\n"));
       
   236 	
       
   237 	TBuf8<50>		cPipeReadData;
       
   238 	TInt 			ret,readsize;
       
   239 	
       
   240 
       
   241 
       
   242 	TData& data = *(TData *)aData; 					// aData will have pipe handles and size.
       
   243 
       
   244 
       
   245 	test.Next(_L("PIPE TEST:Thread 2-1 Read 1 byte of data from the pipe : Test for success\n"));
       
   246 	readsize = 1;
       
   247 		
       
   248 	ret = data.iReadEnd->Read(cPipeReadData ,readsize);
       
   249 	test_Equal(readsize, ret);
       
   250 
       
   251 
       
   252 	test.Next(_L("PIPE TEST:Thread 2-2 Validate 1 byte received is correct\n"));
       
   253 	ret = cPipeReadData.Compare(KTestData1);
       
   254 	test_KErrNone(ret);
       
   255 
       
   256 
       
   257 	test.Next(_L("PIPE TEST:Thread 2-3 Read remaining data from the pipe\n"));
       
   258 	readsize = 21;
       
   259 	ret = data.iReadEnd->Read(cPipeReadData , readsize);
       
   260 	test_Equal(readsize, ret);
       
   261 
       
   262 	test.Next(_L("PIPE TEST:Thread 2-4 Validate received data\n"));
       
   263 	ret = cPipeReadData.Compare(KTestData3);
       
   264 	test_KErrNone(ret);
       
   265 	
       
   266 	test.End();
       
   267 	test.Close();
       
   268 	return KErrNone;
       
   269 
       
   270 
       
   271 }
       
   272 /****************************************************************************
       
   273 	This function is used as thread to test Unnamed pipes.
       
   274 	TestMultiThreadUnNamedPipes() will use this function.
       
   275 	TestMultiThreadNamedPipes() will use this function.
       
   276 	@aData 			: Used to pass the pipe and handle its size information.
       
   277 
       
   278 	Return Value	: TInt
       
   279 
       
   280 ******************************************************************************/
       
   281 TInt TestThread3(TAny* aData) {
       
   282 
       
   283 	
       
   284 	TInt 				ret, aWriteSize;
       
   285 
       
   286 	TBufC8<50> 			cTestData3(KTestData2); 	// Test Data
       
   287 
       
   288 	
       
   289 	
       
   290 	TData& data = *(TData *)aData; 					// aData will have pipe handles and size.
       
   291 	RTest test(_L("t_pipe_t3"));
       
   292 
       
   293 	test.Start(_L("Thread 3"));
       
   294 	
       
   295 	test.Printf(_L(" THREAD 3 called by TestMultiThreadNamedPipes And TestMultiThreadUnNamedPipes\n"));
       
   296 		
       
   297 	test.Next(_L("PIPE TEST:Thread 3-1 Call Write blocking and write data\n"));
       
   298 	
       
   299 	// Call Writeblocking function. Write one byte of data.
       
   300 		
       
   301 	aWriteSize = cTestData3.Length();
       
   302 	ret = data.iWriteEnd->WriteBlocking(cTestData3,aWriteSize);
       
   303 	test_Equal(aWriteSize, ret);
       
   304 	
       
   305 	// Call Writeblocking function. Write aSize bye data.
       
   306 
       
   307 	// Write data so that pipe get filled.
       
   308 	test.Next(_L("PIPE TEST:Thread 3-2 Write data till pipe is filled up \n"));
       
   309 	ret = data.iWriteEnd->WriteBlocking(cTestData3,aWriteSize);
       
   310 	test_Equal(aWriteSize, ret);
       
   311 	
       
   312 	test.End();
       
   313 	test.Close();
       
   314 	return KErrNone;
       
   315 }
       
   316 /****************************************************************************
       
   317 	This function is used as thread to test Unnamed pipes.
       
   318 	TestMultiThreadUnNamedPipes() will use this function.
       
   319 
       
   320 	@aData 			: Used to pass the pipe and handle its size information.
       
   321 
       
   322 	Return Value	: TInt
       
   323 
       
   324 ******************************************************************************/
       
   325 //TRequestStatus			stat1;
       
   326 TInt TestThread4(TAny* aData) {
       
   327 
       
   328 	TData& data = *(TData *)aData; 					// aData will have pipe handles and size.
       
   329 	TRequestStatus			stat1;
       
   330 	TBuf8<150>				cPipeReadData;
       
   331 	TInt					ret;
       
   332 	RTest test(_L("t_pipe_t4"));
       
   333 	test.Start(_L("Thread 4"));
       
   334 	
       
   335 	RSemaphore				sem;					//	Handle to the global semaphore
       
   336 	ret = sem.OpenGlobal(KSemaphoreName);					// Access to the global semaphore identified by its name.
       
   337 	test(ret == KErrNone);
       
   338 
       
   339 
       
   340 	
       
   341 	test.Printf(_L("Thread 4:Created by TestNotifyMechanismPipes.\n"));
       
   342 	test.Next(_L("PIPE TEST:Thread 4-1 Register Notify Data available request.\n"));
       
   343 	data.iReadEnd->NotifyDataAvailable(stat1);
       
   344 	test_Equal(KRequestPending, stat1.Int());
       
   345 	sem.Signal(); //signal to say that we have issued notification request
       
   346 		
       
   347 	test.Next(_L("PIPE TEST:Thread 4-2 Wait till notified for data. Check for Available.\n"));
       
   348 	User::WaitForRequest(stat1);
       
   349 	test ( stat1.Int() == KErrNone);
       
   350 		
       
   351 	test.Next(_L("PIPE TEST:Thread 4-3 Read One byte of data from the pipe.\n"));
       
   352 	sem.Wait();	//wait for signal that 1 byte should be read
       
   353 	ret = data.iReadEnd->Read(cPipeReadData,1);
       
   354 	test (ret == 1);
       
   355 	
       
   356 	test.Next(_L("PIPE TEST:Thread 4-4 Verify data is correct ?.\n"));
       
   357 	test (KErrNone == cPipeReadData.Compare(KTestData1));
       
   358 
       
   359 	test.Next(_L("PIPE TEST:Thread 4-5 Read remaining data from the pipe.\n"));
       
   360 	ret = data.iReadEnd->Read(cPipeReadData,21);
       
   361 	test (ret == 21);
       
   362 	
       
   363 	
       
   364 	test.Next(_L("PIPE TEST:Thread 4-6 Verify data is correct ?.\n"));
       
   365 	test (KErrNone == cPipeReadData.Compare(KTestData3));
       
   366 	
       
   367 	sem.Signal();	//signalling to the main thread to continue its operation
       
   368 	sem.Close();	//closing the handle to the semaphore
       
   369 	test.End();
       
   370 	test.Close();
       
   371 
       
   372 	return KErrNone;
       
   373 
       
   374 }
       
   375 
       
   376 /****************************************************************************
       
   377 	This function is used as thread to test Unnamed pipes.
       
   378 	TestWaitMechanismPipes() will use this function.
       
   379 
       
   380 	@aData 			: Used to pass the pipe and handle its size information.
       
   381 
       
   382 	Return Value	: TInt
       
   383 
       
   384 ******************************************************************************/
       
   385 
       
   386 TInt TestThread5(TAny* aData) {
       
   387 
       
   388 	TData& data = *(TData *)aData; 							// aData will have pipe handles and size.
       
   389 	TRequestStatus			stat1;
       
   390 	TInt					ret;
       
   391 
       
   392 	RTest test(_L("t_pipe_t5"));
       
   393 	
       
   394 	test.Start(_L("Thread 5"));
       
   395 	test.Printf(_L("PIPE TEST:Thread 5:Created by TestWaitMechanismPipes.\n"));
       
   396 	
       
   397 	test.Next(_L("PIPE TEST:Thread 5-1:Register one more request to wait till open to read. It shall not allow\n"));
       
   398 	data.iWriteEnd->Wait(KPipe3Name, stat1);
       
   399 	test (stat1.Int() == KErrInUse);
       
   400 	data.iWriteEnd->WaitForReader(KPipe3Name, stat1);
       
   401 	test (stat1.Int() == KErrInUse);
       
   402 
       
   403 
       
   404 	test.Next(_L("PIPE TEST:Thread 5-2:Open Pipe handle to Read.\n"));
       
   405 	ret = data.iReadEnd->Open(KPipe3Name,RPipe::EOpenToRead);
       
   406 	test(ret == KErrNone);
       
   407 	
       
   408 	test.End();
       
   409 	test.Close();
       
   410 	return KErrNone;
       
   411 }
       
   412 
       
   413 /**
       
   414 The reader thread will wait till there is data in the pipe
       
   415 and then continuously read till it has read the total length
       
   416 of the pipe.
       
   417 */
       
   418 TInt ReaderThread(TAny* aData) {
       
   419 // Read data from Pipe
       
   420 	RTest test(KReaderThread);
       
   421 	test.Title();
       
   422 	test.Start(_L("Reader Thread"));
       
   423 
       
   424 	TData& data = *(TData *)aData;
       
   425 	TBuf8<10> pipeReadData;
       
   426 	
       
   427 	const TInt sizeToRead = data.iReadEnd->MaxSize(); //attempt to read everything from pipe
       
   428 	TRequestStatus status(KErrGeneral);
       
   429 	
       
   430 	//do read in loop in case thread is notified before pipe is full
       
   431 	TInt totalDataRead=0;
       
   432 	do
       
   433 		{
       
   434 		data.iReadEnd->NotifyDataAvailable(status);
       
   435 		test.Printf(_L("notify data request status is %d\n"), status.Int());
       
   436 		if(status==KRequestPending)
       
   437 			User::WaitForRequest(status);
       
   438 		test(status==KErrNone);
       
   439 		test.Printf(_L("ready to read data\n"), status.Int());
       
   440 
       
   441 		const TInt sizeRead = data.iReadEnd->Read(pipeReadData, sizeToRead);
       
   442 		test.Printf(_L("Read %d bytes from pipe\n"), sizeRead);
       
   443 		test(sizeRead>0);
       
   444 		totalDataRead+=sizeRead;
       
   445 		} 
       
   446 	while(totalDataRead<sizeToRead);
       
   447 
       
   448 	test(totalDataRead==sizeToRead);
       
   449 	test.End();
       
   450 	test.Close();
       
   451 
       
   452 	return KErrNone;
       
   453 }
       
   454 
       
   455 _LIT8(KTestDataNum1 , "12345");
       
   456 _LIT8(KTestDataNum , "1234567890");
       
   457 
       
   458 
       
   459 
       
   460 /**
       
   461 Write into pipe to completely fill it.
       
   462 */
       
   463 TInt WriterThread(TAny* aData)
       
   464 	{
       
   465 // Write data to pipe
       
   466 	RPipe* writeEnd = static_cast<TData*>(aData)->iWriteEnd;
       
   467 	RTest test(_L("WriterThread"));
       
   468 	test.Start(_L("WriterThread"));
       
   469 	writeEnd->Flush(); //make sure pipe is empty
       
   470 	const TInt sizeToWrite = writeEnd->MaxSize();
       
   471 	test.Printf(_L("Writing %d bytes in to pipe\n"), sizeToWrite);
       
   472 	TInt length=writeEnd->WriteBlocking(KTestDataNum1,sizeToWrite);
       
   473 	test(length==sizeToWrite);
       
   474 	test.End();
       
   475 	test.Close();
       
   476 	return KErrNone;
       
   477 	}
       
   478 
       
   479 /**
       
   480 The FlusherThread waits till the supplied pipe
       
   481 is full before flushing it. 
       
   482 */
       
   483 TInt FlusherThread(TAny* aData)
       
   484 	{
       
   485 	TData& data = *(TData *)aData; 							// aData will have pipe handles and size.
       
   486 	
       
   487 	//wait for pipe to fill, then flush
       
   488 	TRequestStatus status;
       
   489 	const TInt maxSize=data.iReadEnd->MaxSize();
       
   490 	do
       
   491 		{	
       
   492 		data.iReadEnd->NotifyDataAvailable(status);
       
   493 		if(status==KRequestPending)
       
   494 			User::WaitForRequest(status);
       
   495 		if(status!=KErrNone)
       
   496 			return status.Int();
       
   497 		} while(data.iReadEnd->Size()<maxSize);
       
   498 	data.iReadEnd->Flush();
       
   499 	return KErrNone;
       
   500 	}
       
   501 
       
   502 /****************************************************************************
       
   503 	This function is used as thread to test Unnamed pipes.
       
   504 	TestWaitMechanismPipes() will use this function.
       
   505 
       
   506 	@aData 			: Used to pass the pipe and handle its size information.
       
   507 
       
   508 	Return Value	: TInt
       
   509 
       
   510 ******************************************************************************/
       
   511 TInt	CloseFlag;
       
   512 
       
   513 TInt TestThread9(TAny* aData) {
       
   514 
       
   515 	TData& data = *(TData *)aData; 							// aData will have pipe handles and size.
       
   516 	
       
   517 	if (CloseFlag == 1)
       
   518 	data.iReadEnd->Close();
       
   519 	if (CloseFlag == 0)
       
   520 	data.iWriteEnd->Close();
       
   521 	
       
   522 			
       
   523 	return 0;
       
   524 	}
       
   525 
       
   526 /**
       
   527 The test will create 2 threads running this function. They will
       
   528 race to placing the blocking read request. The first
       
   529 will succeed and wait, the second will write to pipe
       
   530 */
       
   531 TInt ReadBlockThenWrite(TAny* aData) {
       
   532 
       
   533 	TData& data = *(TData *)aData; 							// aData will have pipe handles and size.
       
   534 	TBuf8<10>		cReadData;
       
   535 	RTest test(_L("Concurrent blocking read"));
       
   536 	test.Start(_L("Call blocking read on pipe\n"));
       
   537 
       
   538 	TInt ret = data.iReadEnd->ReadBlocking(cReadData,5);
       
   539 	if(ret == KErrNone)
       
   540 		{
       
   541 		test_KErrNone(cReadData.Compare(KTestDataNum1));
       
   542 		}
       
   543 
       
   544 	if(ret == KErrInUse)
       
   545 		{
       
   546 		test.Next(_L("Other thread beat us - write to pipe so it may proceed"));
       
   547 		TInt write = data.iWriteEnd->Write(KTestDataNum,5);
       
   548 		test_Equal(5, write);
       
   549 		}
       
   550 	
       
   551 	test.End();
       
   552 	test.Close();
       
   553 				
       
   554 	return ret;
       
   555 }
       
   556 
       
   557 /**
       
   558 The test will create 2 threads running this function. They will
       
   559 race to placing the blocking write request. The first
       
   560 will succeed and wait, the second will read from pipe
       
   561 */
       
   562 TInt WriteBlockThenRead(TAny* aData) {
       
   563 
       
   564 	TData& data = *(TData *)aData; 							// aData will have pipe handles and size.
       
   565 	TBuf8<10>		cReadData;
       
   566 	RTest test(_L("Concurrent blocking write"));
       
   567 	test.Start(_L("test writing blocked pipe\n"));
       
   568 
       
   569 	TInt ret = data.iWriteEnd->WriteBlocking(KTestDataNum,10);
       
   570 	if(ret == KErrInUse)
       
   571 		{
       
   572 		test.Next(_L("The other thread beat us - read from pipe so it may proceed"));
       
   573 		TInt read = data.iReadEnd->Read(cReadData,5);
       
   574 		test_Equal(5, read);
       
   575 		test_KErrNone(cReadData.Compare(KTestDataNum1));
       
   576 		}
       
   577 
       
   578 	test.End();
       
   579 	test.Close();
       
   580 				
       
   581 	return ret;
       
   582 }
       
   583 
       
   584 /****************************************************************************
       
   585 	This function is used as thread to test Unnamed pipes.
       
   586 	TestWaitMechanismPipes() will use this function.
       
   587 
       
   588 	@aData 			: Used to pass the pipe and handle its size information.
       
   589 
       
   590 	Return Value	: TInt
       
   591 
       
   592 ******************************************************************************/
       
   593 
       
   594 TInt TestThread11(TAny* aData) {
       
   595 
       
   596 	TData& data = *(TData *)aData; 							// aData will have pipe handles and size.
       
   597 	TRequestStatus			stat1;
       
   598 	TBufC<50>				cPipeName(KPipe3Name);			// Descriptor to hold data for Writing.
       
   599 	TInt					ret;
       
   600 	RTest test(_L("t_pipe_t11"));
       
   601 
       
   602 	test.Start(_L("PIPE TEST:Thread 11:Created by TestWaitMechanismPipes.\n"));
       
   603 
       
   604 	test.Next(_L("PIPE TEST:Thread 11-1:Register one more request to wait till open to read. It shall not allow\n"));
       
   605 	data.iReadEnd->WaitForWriter(cPipeName, stat1);
       
   606 	test_Equal(KErrInUse, stat1.Int());
       
   607 
       
   608 
       
   609 	test.Next(_L("PIPE TEST:Thread 11-2:Open Pipe handle to write.\n"));
       
   610 	ret = data.iWriteEnd->Open(cPipeName,RPipe::EOpenToWrite);
       
   611 	test_KErrNone(ret);
       
   612 	
       
   613 	test.End();
       
   614 	test.Close();
       
   615 	return KErrNone;
       
   616 }
       
   617 
       
   618 /****************************************************************************
       
   619 	This is a function to test Named/UnNamed pipes Performace.
       
   620 	Check the functionality of following library functions
       
   621 		-
       
   622 		-
       
   623 
       
   624 
       
   625 
       
   626 ******************************************************************************/
       
   627 /**
       
   628 	- test that WriteBlocking unblocks
       
   629 		- when data is read
       
   630 		- whed date is flushed
       
   631 	- test that notify data available request is completed as data is read
       
   632 	- test that ReadBlocking unblocks
       
   633 	- test that data available notification works
       
   634 
       
   635 */
       
   636 void TestBlockingAndNotify() {
       
   637 		
       
   638 		TRequestStatus	stat1;
       
   639 		RPipe		aReader,aWriter;
       
   640 		TInt		aSize,ret;
       
   641 		TBufC8<10>	cPipeTestDataNum(KTestDataNum);
       
   642 
       
   643 		TBuf8<10>	cReadData;
       
   644 		
       
   645 		
       
   646 		aSize = 5;
       
   647 		ret = RPipe::Create(	aSize,
       
   648 								aReader,
       
   649 								aWriter,
       
   650 								EOwnerProcess, //
       
   651 								EOwnerProcess //
       
   652 							 ); 
       
   653 		test_KErrNone(ret);
       
   654 
       
   655 		TData data(	&aReader, &aWriter);
       
   656 
       
   657 		{
       
   658 		TTestThread readerThread(KReaderThread, ReaderThread, &data);
       
   659 
       
   660 		test.Start(_L("Test that WriteBlock unblocks as data is read from pipe\n"));
       
   661 		ret = aWriter.WriteBlocking(cPipeTestDataNum,10);
       
   662 		test(ret == 10);
       
   663 		test(aWriter.Size()==5);
       
   664 		aWriter.Flush();
       
   665 		test(aWriter.Size()==0);
       
   666 
       
   667 		ret = readerThread.WaitForExitL();
       
   668 		test_KErrNone(ret);
       
   669 		}
       
   670 
       
   671 		{
       
   672 		TTestThread flusherThread(KThread8Name,	FlusherThread, &data);
       
   673 		test.Next(_L("Test that WriteBlock unblocks as data is flushed from read end of pipe\n"));
       
   674 		
       
   675 		ret = aWriter.WriteBlocking(cPipeTestDataNum,10);
       
   676 		test (ret == 10);
       
   677 		ret = flusherThread.WaitForExitL();
       
   678 		test_KErrNone(ret);
       
   679 		}
       
   680 		
       
   681 		test.Next(_L("Test that NotifyDataAvailable request is completed as data is read from pipe\n"));
       
   682 		
       
   683 		aWriter.NotifySpaceAvailable(aSize,stat1);
       
   684 		test(stat1==KRequestPending);
       
   685 		
       
   686 		{
       
   687 		TTestThread readerThread2(KReaderThread, ReaderThread, &data);
       
   688 		
       
   689 		User::WaitForRequest(stat1);
       
   690 		test(stat1==KErrNone);
       
   691 	
       
   692 		ret = readerThread2.WaitForExitL();
       
   693 		test_KErrNone(ret);
       
   694 		}
       
   695 
       
   696 		aReader.Flush();
       
   697 
       
   698 		test.Next(_L("PIPE TEST: Test that ReadBlocking unblocks\n"));
       
   699 		{
       
   700 		TTestThread writeThread(KWriterThread, WriterThread, &data);
       
   701 
       
   702 		ret = aReader.ReadBlocking(cReadData,5);		
       
   703 		test(ret == 5);
       
   704 
       
   705 		ret = writeThread.WaitForExitL();
       
   706 		test_KErrNone(ret);
       
   707 		}
       
   708 		
       
   709 		test.Next(_L("PIPE TEST: Test NotifyDataAvailable\n"));
       
   710 		aReader.Flush();
       
   711 		aReader.NotifyDataAvailable(stat1);
       
   712 		test(stat1==KRequestPending);
       
   713 
       
   714 		{	
       
   715 		TTestThread writeThread2(KWriterThread,WriterThread, &data);
       
   716 
       
   717 		User::WaitForRequest(stat1);		
       
   718 		test(stat1==KErrNone);
       
   719 
       
   720 		aReader.Flush();
       
   721 
       
   722 		ret = writeThread2.WaitForExitL();
       
   723 		test_KErrNone(ret);
       
   724 		}	
       
   725 		test.Next(_L("PIPE TEST: Test reading from pipe closed by the writer\n"));
       
   726 
       
   727 		CloseFlag = 0; // 0 Close Write Handle	
       
   728 		//CloseFlag = 1; // 1 Close Read Handle							
       
   729 		test.Next(_L("PIPE TEST: TestBlockingAndNotify 10.6\n"));
       
   730 		TTestThread closeThread(KThread9Name, TestThread9, &data);
       
   731 
       
   732 		ret = closeThread.WaitForExitL();
       
   733 		test_KErrNone(ret);
       
   734 
       
   735 		ret = aReader.ReadBlocking(cReadData,5);
       
   736 		test_Equal(KErrNotReady, ret);
       
   737 				
       
   738 		aWriter.Close();
       
   739 		aReader.Close();
       
   740 		aSize = 5;
       
   741 		ret = RPipe::Create(	aSize,
       
   742 								aReader,
       
   743 								aWriter,
       
   744 								EOwnerProcess, //
       
   745 								EOwnerProcess //
       
   746 							 ); 
       
   747 		test_KErrNone(ret);
       
   748 
       
   749 		TData data1(&aReader,&aWriter);
       
   750 				
       
   751 		
       
   752 		//CloseFlag = 0; // 0 Close Write Handle	
       
   753 		CloseFlag = 1; // 1 Close Read Handle							
       
   754 		test.Printf(_L("PIPE TEST: TestBlockingAndNotify 10.7\n"));
       
   755 		TTestThread closeThread2(KThread9Name, TestThread9, &data1);
       
   756 		ret = aWriter.WriteBlocking(cPipeTestDataNum,10);
       
   757 		test_Equal(KErrNotReady, ret);
       
   758 
       
   759 		ret = closeThread2.WaitForExitL();
       
   760 		test_KErrNone(ret);
       
   761 		
       
   762 		aWriter.Close();
       
   763 		aReader.Close();
       
   764 
       
   765 		test.End();
       
   766 }
       
   767 
       
   768 
       
   769 /****************************************************************************
       
   770 	TestPipesPermissionCheck	:
       
   771 			This function is used to test Permission and Access related
       
   772 			Errors of Pipes API.
       
   773 			APIs tested are Define , Create , Destroy , Read and Write.
       
   774 			
       
   775 
       
   776 ******************************************************************************/
       
   777 // Different pipes for different capability , VID values.
       
   778 _LIT(KPipeName2, "PipeWithNoCap");
       
   779 _LIT(KPipeName3, "PipeWithNoCapVID");
       
   780 _LIT(KPipeName4, "PipeWithRWCap");
       
   781 _LIT(KPipeName5, "PipeWithComDDCap");
       
   782 _LIT(KPipeName6, "PipeWithRWComDDCap");
       
   783 _LIT(KPipeName7, "PipeWithRWComDDCapVID");
       
   784 _LIT(KPipeName8, "PipeWithRWRUCap");
       
   785 
       
   786 // Different processes with different capability , VID  values.
       
   787 _LIT(KProcessNoCap, "t_pipe3.exe");
       
   788 _LIT(KProcessRCap, "t_pipe5.exe");
       
   789 
       
   790 // My VID and SID
       
   791 _LIT_VENDOR_ID(MyVID,0x70000001);
       
   792 
       
   793 void TestPipesPermissionCheck() {
       
   794 
       
   795 		RProcess			proc;
       
   796 		TInt 				ret;
       
   797 			
       
   798 		// Define TSecurity objects with different capabilities and VID .
       
   799 		TSecurityPolicy 	NoCapVID(MyVID,ECapability_None,ECapability_None,ECapability_None);
       
   800 		TSecurityPolicy 	RWCap(ECapabilityReadDeviceData,ECapabilityWriteDeviceData);
       
   801 		TSecurityPolicy 	ComDDCap(ECapabilityCommDD);
       
   802 		TSecurityPolicy 	RWComDDCap(ECapabilityReadDeviceData,ECapabilityCommDD,ECapabilityWriteDeviceData);
       
   803 		TSecurityPolicy 	RWComDDCapVID(MyVID,ECapabilityCommDD,ECapabilityReadDeviceData,ECapabilityWriteDeviceData);
       
   804 		TSecurityPolicy 	RWRUCap(ECapabilityReadUserData,ECapabilityReadDeviceData,ECapabilityWriteDeviceData);
       
   805 
       
   806 		// Define Named pipes with No Cap , combination of Cap and VID 
       
   807 		
       
   808 		TInt 	aSize = 10;
       
   809 		ret = RPipe::Define(KPipeName2, aSize);
       
   810 		test (ret == KErrNone);
       
   811 		ret = RPipe::Define(KPipeName3, aSize,NoCapVID);
       
   812 		test (ret == KErrNone);
       
   813 		ret = RPipe::Define(KPipeName4, aSize,RWCap);
       
   814 		test (ret == KErrNone);
       
   815 		ret = RPipe::Define(KPipeName5, aSize,ComDDCap);
       
   816 		test (ret == KErrNone);
       
   817 		ret = RPipe::Define(KPipeName6, aSize,RWComDDCap);
       
   818 		test (ret == KErrNone);
       
   819 		ret = RPipe::Define(KPipeName7, aSize,RWComDDCapVID);
       
   820 		test (ret == KErrNone);
       
   821 		ret = RPipe::Define(KPipeName8, aSize,RWRUCap);
       
   822 		test (ret == KErrNone);
       
   823 		
       
   824 		//Lets see who can use pipes. Check for Permissions and Access
       
   825 		test.Next(_L("PIPE TEST:8.1	Create Process with No Cap t_pipe3.exe\n"));
       
   826 		ret = proc.Create	(
       
   827 								KProcessNoCap,			// Launch t_pipe3.exe process
       
   828 					 			KNullDesC				// No arguments passed to t_pipe3.exe
       
   829 					 		);
       
   830 
       
   831 		if (ret != KErrNone) 
       
   832 		{
       
   833 			test.Printf(_L(" ***** t_pipe3.exe could not start ****"));
       
   834 		}
       
   835 		test_KErrNone(ret);
       
   836 		test.Printf(_L("Process Created successfully"));
       
   837 		
       
   838 		TRequestStatus procLogon;
       
   839 		proc.Logon(procLogon);
       
   840 		test(procLogon==KRequestPending);
       
   841 		proc.Resume();
       
   842 		User::WaitForRequest(procLogon);
       
   843 		proc.Close();
       
   844 		
       
   845 		// Lets see what happens with Read Capability.
       
   846 		test.Next(_L("PIPE TEST:8.2	Create Process with Read-Write-CommDD Cap t_pipe5.exe\n"));
       
   847 		ret = RPipe::Define(KPipeName2, aSize);
       
   848 		test_KErrNone(ret);
       
   849 		ret = RPipe::Define(KPipeName3, aSize,NoCapVID);
       
   850 		test_KErrNone(ret);
       
   851 	
       
   852 		ret = proc.Create	(
       
   853 								KProcessRCap,			// Launch t_pipe3.exe process
       
   854 					 			KNullDesC				// No arguments passed to t_pipe3.exe
       
   855 					 		);
       
   856 
       
   857 		if (ret != KErrNone) 
       
   858 		{
       
   859 			test.Printf(_L(" ***** t_pipe5.exe could not start ****"));
       
   860 		}
       
   861 		test_KErrNone(ret);
       
   862 		test.Printf(_L("Process Created successfully"));
       
   863 		proc.Logon(procLogon);
       
   864 		test(procLogon==KRequestPending);
       
   865 		proc.Resume();
       
   866 		User::WaitForRequest(procLogon);
       
   867 		proc.Close();
       
   868 		
       
   869 		//the t_pipe5.exe process should destroy most of these
       
   870 		//but we call destroy again to verify this
       
   871 		ret = RPipe::Destroy (KPipeName2);
       
   872 		test_Equal(KErrNotFound, ret);
       
   873 		ret = RPipe::Destroy (KPipeName3);
       
   874 		test_KErrNone(ret); //KPipeName3 is not destroyed by the other process.
       
   875 		ret = RPipe::Destroy (KPipeName4);
       
   876 		test_Equal(KErrNotFound, ret);
       
   877 		ret = RPipe::Destroy (KPipeName5);
       
   878 		test_Equal(KErrNotFound, ret);
       
   879 		ret = RPipe::Destroy (KPipeName6);
       
   880 		test_Equal(KErrNotFound, ret);
       
   881 		ret = RPipe::Destroy (KPipeName7);
       
   882 		test_KErrNone(ret); //t_pipe5.exe does not have permission to delete
       
   883 		ret = RPipe::Destroy (KPipeName8);
       
   884 		test_KErrNone(ret); //t_pipe5.exe does not have permission to delete
       
   885 				
       
   886 				
       
   887 
       
   888 		return;
       
   889 
       
   890 }
       
   891 
       
   892 /****************************************************************************
       
   893 
       
   894 	TestMiscPipes :
       
   895 	This is a function to test Named/UnNamed pipes for all Misc test cases.
       
   896 	
       
   897 
       
   898 ******************************************************************************/
       
   899 
       
   900 void TestMiscPipes() {
       
   901 
       
   902 _LIT(KInvalidPipeName,"PipeNotExist");
       
   903 		TInt			ret,aSize;
       
   904 		RPipe			aReader,aWriter;
       
   905 		TBufC<50>		cPipeName(KInvalidPipeName);			// Descriptor to hold data for Writing.
       
   906 		TBufC8<50>		cPipeTestData2(KTestData2);
       
   907 		
       
   908 		
       
   909 		// Try to create unnamed pipe with Negative size
       
   910 		test.Next(_L("PIPE TEST:9.1 Create Pipe of Negative Size.\n"));
       
   911 		aSize = -1;
       
   912 		ret = RPipe::Create(	aSize,
       
   913 								aReader,
       
   914 								aWriter,
       
   915 								EOwnerProcess, //
       
   916 								EOwnerProcess //
       
   917 							 ); 
       
   918 		test( ret == KErrArgument);
       
   919 		
       
   920 		
       
   921 		// Try to create unnamed pipe with zero size
       
   922 		test.Next(_L("PIPE TEST:9.2 Create Pipe with of Zero size.\n"));
       
   923 		aSize = 0;
       
   924 		ret = RPipe::Create(	aSize,
       
   925 								aReader,
       
   926 								aWriter,
       
   927 								EOwnerProcess, //
       
   928 								EOwnerProcess //
       
   929 							 );
       
   930 		test( ret == KErrArgument);
       
   931 		
       
   932 
       
   933 		// Try to define named pipe with Negative size
       
   934 		test.Next(_L("PIPE TEST:9.3 Define Pipe of Negative size.\n"));
       
   935 		aSize = -1;
       
   936 		ret = RPipe::Define(cPipeName, aSize); 
       
   937 		test( ret == KErrArgument);
       
   938 		
       
   939 		
       
   940 
       
   941 		// Try to define named pipe with Zero size
       
   942 		test.Next(_L("PIPE TEST:9.4 Define Pipe of Zero size.\n"));
       
   943 		aSize = 0;
       
   944 		ret = RPipe::Define(cPipeName, aSize);
       
   945 		test( ret == KErrArgument);
       
   946 		
       
   947 
       
   948 		// Try to destroy pipe which does not exists
       
   949 		test.Next(_L("PIPE TEST:9.5 Try to destroy named pipe which do not exist.\n"));
       
   950 		ret = RPipe::Destroy (cPipeName);
       
   951 		test (ret == KErrNotFound);
       
   952 
       
   953 		
       
   954 		// Try to read from pipe with invalid length data to be read
       
   955 		RPipe			aReaderUN,aWriterUN;
       
   956 		TBuf8<150>		cPipeReadData;
       
   957 		aSize = 10;
       
   958 		ret = RPipe::Create(	aSize,
       
   959 								aReaderUN,
       
   960 								aWriterUN,
       
   961 								EOwnerProcess, //
       
   962 								EOwnerProcess //
       
   963 							 );
       
   964 		test (ret == KErrNone );
       
   965 
       
   966 		
       
   967 		test.Next(_L("PIPE TEST:9.6 Try calling ReadBlocking using write handle and WriteBlocking using Read handle.\n"));
       
   968 		ret = aWriterUN.ReadBlocking(cPipeReadData, aSize);
       
   969 		test (ret == KErrAccessDenied);
       
   970 		ret = aReaderUN.WriteBlocking(cPipeTestData2,aSize);
       
   971 		test (ret == KErrAccessDenied);
       
   972 		
       
   973 		
       
   974 		
       
   975 		
       
   976 		test.Next(_L("PIPE TEST:9.7 Read negative size data from un-named pipe.\n"));
       
   977 		aSize = -1;
       
   978 		ret = aReaderUN.Read(cPipeReadData, aSize);
       
   979 		test( ret == KErrArgument);
       
   980 		ret = aWriterUN.Write(cPipeTestData2,aSize);
       
   981 		test( ret == KErrArgument);
       
   982 		
       
   983 
       
   984 		
       
   985 		test.Next(_L("PIPE TEST:9.8 Read/Write zero size data from/to un-named pipe\n"));
       
   986 		aSize = 0;
       
   987 		ret = aReaderUN.Read(cPipeReadData, aSize);
       
   988 		test( ret == KErrNone);
       
   989 
       
   990 		ret = aWriterUN.Write(cPipeTestData2,aSize);
       
   991 		test( ret == KErrNone);
       
   992 		
       
   993 
       
   994 		test.Next(_L("PIPE TEST:9.9 Call ReadBlocking and WriteBlocking to Read and Write negative size data.\n"));
       
   995 		// Try to readblocking from pipe with invalid length data to be read
       
   996 		aSize = -1;
       
   997 		ret = aReaderUN.ReadBlocking(cPipeReadData, aSize);
       
   998 		test( ret == KErrArgument);
       
   999 		ret = aWriterUN.WriteBlocking(cPipeTestData2,aSize);
       
  1000 		test( ret == KErrArgument);
       
  1001 
       
  1002 		test.Next(_L("PIPE TEST:9.10 ReadBlocking/WriteBlocking to read/write zero size data.\n"));
       
  1003 		aSize = 0;
       
  1004 		ret = aReaderUN.ReadBlocking(cPipeReadData, aSize);
       
  1005 		test( ret == KErrArgument);
       
  1006 		ret = aWriterUN.WriteBlocking(cPipeTestData2,aSize);
       
  1007 		test( ret == KErrArgument);
       
  1008 		
       
  1009 		
       
  1010 		test.Next(_L("PIPE TEST:9.11 Try calling ReadBlocking and WriteBlocking using un opened RPipe handles.\n"));		
       
  1011 		RPipe		aReaderT,aWriterT;
       
  1012 		ret = aReaderT.ReadBlocking(cPipeReadData, aSize);
       
  1013 		test (ret == KErrBadHandle);
       
  1014 		ret = aWriterT.WriteBlocking(cPipeTestData2,aSize);
       
  1015 		test (ret == KErrBadHandle);
       
  1016 		aReaderUN.Close();
       
  1017 		aWriterUN.Close();
       
  1018 		
       
  1019 
       
  1020 		return;
       
  1021 }
       
  1022 
       
  1023 /****************************************************************************
       
  1024 	This is a function to test notify mechanism of pipes.
       
  1025 	Check the functionality of following library functions
       
  1026 		- Notify...()
       
  1027 
       
  1028 
       
  1029 ******************************************************************************/
       
  1030 void TestWaitMechanismPipes() {
       
  1031 
       
  1032 		RPipe			aReader,aWriter;			// Used to pass to thread.
       
  1033 		RPipe			aWriter2;
       
  1034 		TInt			ret;
       
  1035 		TBufC<50>		cPipeName(KPipe3Name);		// Descriptor to hold data for Writing.
       
  1036 		TInt			aSize;
       
  1037 		TRequestStatus	stat1;
       
  1038 
       
  1039 		aSize = 22 * 10; // Sizeof(KTestData) * 10
       
  1040 
       
  1041 		ret = RPipe::Define( cPipeName,aSize);
       
  1042 		ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1043 		test (ret == KErrNone);
       
  1044 
       
  1045 
       
  1046 		
       
  1047 		test.Next(_L("PIPE TEST:7.1 Try calling Wait function on BadPipe name.\n"));
       
  1048 		
       
  1049 		_LIT(KBadPipeName , "***R$@#$@#%#$%#^.12-.");
       
  1050 		aWriter.Wait(KBadPipeName,stat1);
       
  1051 		test(stat1.Int() == KErrBadName);
       
  1052 		aWriter.WaitForReader(KBadPipeName,stat1);
       
  1053 		test(stat1.Int() == KErrBadName);
       
  1054 		
       
  1055 			
       
  1056 		_LIT(KBadPipeName2 , "");
       
  1057 		aWriter.Wait(KBadPipeName2,stat1);
       
  1058 		test(stat1.Int() == KErrBadName);
       
  1059 		aWriter.WaitForReader(KBadPipeName2,stat1);
       
  1060 		test(stat1.Int() == KErrBadName);
       
  1061 		
       
  1062 		
       
  1063 		
       
  1064 		test.Next(_L("PIPE TEST:7.2 Try calling Wait function on non existing Pipe\n"));
       
  1065 		_LIT(KInvalidPipe , "NotExistingPipe");
       
  1066 		aWriter.Wait(KInvalidPipe,stat1);
       
  1067 		test(stat1.Int() == KErrNotFound);
       
  1068 		aWriter.WaitForReader(KInvalidPipe,stat1);
       
  1069 		test(stat1.Int() == KErrNotFound);
       
  1070 		
       
  1071 		
       
  1072 		test.Next(_L("PIPE TEST:7.3 Try calling Wait function on Pipe name length more than maxlength.\n"));
       
  1073 		aWriter.Wait(KMaxPipeNamePlusOne,stat1);
       
  1074 		test(stat1.Int() == KErrBadName);
       
  1075 		aWriter.WaitForReader(KMaxPipeNamePlusOne,stat1);
       
  1076 		test(stat1.Int() == KErrBadName);
       
  1077 			
       
  1078 	
       
  1079 		test.Next(_L("PIPE TEST:7.4 Try calling Wait function from unopened handle.\n"));
       
  1080 		aWriter2.Wait(cPipeName, stat1);
       
  1081 		test (stat1.Int() == KErrInUse);
       
  1082 		aWriter2.WaitForReader(cPipeName, stat1);
       
  1083 		test (stat1.Int() == KErrInUse);
       
  1084 				
       
  1085 		
       
  1086 		test.Next(_L("PIPE TEST:7.5 Register a valid Wait Request .\n"));
       
  1087 		aWriter.Wait(cPipeName, stat1);
       
  1088 
       
  1089 		TData data(	&aReader, &aWriter);
       
  1090 
       
  1091 		// Create Thread 5
       
  1092 		// Pass TData object with Write and Read handle both
       
  1093 		TTestThread thread5(KThread5Name, TestThread5, &data);
       
  1094 		User::WaitForRequest(stat1);
       
  1095 
       
  1096 		ret = thread5.WaitForExitL();
       
  1097 		test_KErrNone(ret);
       
  1098 		
       
  1099 		test.Next(_L("PIPE TEST:7.6 After Wait finish check the value of status register.\n"));
       
  1100 		test_KErrNone(stat1.Int());
       
  1101 		aWriter.Close();
       
  1102 		aReader.Close();
       
  1103 		ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1104 		test_KErrNone(ret);
       
  1105 		ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1106 		test_KErrNone(ret);
       
  1107 		
       
  1108 		test.Next(_L("PIPE TEST:7.7 After Read handle is open , register request to Wait\n"));
       
  1109 		
       
  1110 		aWriter.Wait(cPipeName, stat1);
       
  1111 		test (stat1.Int() == KErrNone);
       
  1112 	
       
  1113 		test.Next(_L("PIPE TEST:7.8 Check for CancelWait.\n"));
       
  1114 		aWriter.Close();
       
  1115 		aReader.Close();
       
  1116 		ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1117 		TRequestStatus stat2;
       
  1118 		aWriter.Wait(cPipeName, stat1);
       
  1119 		aWriter.Wait(cPipeName, stat2);
       
  1120 		test(stat2.Int() == KErrInUse);
       
  1121 		aWriter.CancelWait();
       
  1122 		test(stat1.Int() == KErrCancel);
       
  1123 		test(stat2.Int() == KErrInUse);
       
  1124 		aWriter.Wait(cPipeName, stat1);
       
  1125 		ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1126 		test(stat1.Int() == KErrNone);
       
  1127 		
       
  1128 		test.Next(_L("PIPE TEST:7.9 Check Wait and CancelWait from reader end\n"));
       
  1129 		aWriter.Close();
       
  1130 		aReader.Close();
       
  1131 		ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1132 		aReader.Wait(cPipeName, stat1);
       
  1133 		test (stat1.Int() ==  KErrAccessDenied);
       
  1134 		aReader.CancelWait();
       
  1135 
       
  1136 	
       
  1137 		aWriter.Close();
       
  1138 		aReader.Close();
       
  1139 		RPipe::Destroy(cPipeName);
       
  1140 		
       
  1141 		/*****************Newly added tests for CR 1114 - WaitForReader *********/
       
  1142 		
       
  1143 		ret = RPipe::Define( cPipeName,aSize);
       
  1144 		ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1145 		test (ret == KErrNone);
       
  1146 
       
  1147 
       
  1148 		test.Next(_L("PIPE TEST:7.10 Register a valid Wait Request .\n"));
       
  1149 		aWriter.WaitForReader(cPipeName, stat1);
       
  1150 		test(stat1==KRequestPending);
       
  1151 						
       
  1152 		// Create Thread 5
       
  1153 		// Pass TData object with Write and Read handle both
       
  1154 		{
       
  1155 		TTestThread thread5_1(KThread5Name, TestThread5, &data);
       
  1156 		User::WaitForRequest(stat1);
       
  1157 
       
  1158 		ret = thread5_1.WaitForExitL();
       
  1159 		test_KErrNone(ret);
       
  1160 		}
       
  1161 		
       
  1162 		test.Next(_L("PIPE TEST:7.11 After Wait finish check the value of status register.\n"));
       
  1163 		aWriter.Close();
       
  1164 		aReader.Close();
       
  1165 		ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1166 		ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1167 		
       
  1168 		aReader.WaitForReader(cPipeName, stat1);
       
  1169 		test (stat1.Int() ==  KErrAccessDenied);
       
  1170 		
       
  1171 		test.Next(_L("PIPE TEST:7.12 After Read handle is open , register request to Wait\n"));
       
  1172 				
       
  1173 		aWriter.WaitForReader(cPipeName, stat1);
       
  1174 		test (stat1.Int() == KErrNone);
       
  1175 		
       
  1176 		test.Next(_L("PIPE TEST:7.13 Check for CancelWait.\n"));
       
  1177 		aWriter.Close();
       
  1178 		aReader.Close();
       
  1179 		ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1180 		
       
  1181 		aWriter.WaitForReader(cPipeName, stat1);
       
  1182 		aWriter.WaitForReader(cPipeName, stat2);
       
  1183 		test(stat2.Int() == KErrInUse);
       
  1184 		aWriter.CancelWait();
       
  1185 		test(stat1.Int() == KErrCancel);
       
  1186 		test(stat2.Int() == KErrInUse);
       
  1187 		aWriter.WaitForReader(cPipeName, stat1);
       
  1188 		ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1189 		test(stat1.Int() == KErrNone);
       
  1190 				
       
  1191 		// Release all the resources.
       
  1192 		aWriter.Close();
       
  1193 		aReader.Close();
       
  1194 		
       
  1195 		// Create Thread 5
       
  1196 		// Pass TData object with Write and Read handle both
       
  1197 		
       
  1198 		test.Next(_L("PIPE TEST:7.14 Register a valid Wait Request .\n"));
       
  1199 		aWriter.WaitForReader(cPipeName, stat1);
       
  1200 						
       
  1201 		{
       
  1202 		TTestThread thread5_2(KThread5Name, TestThread5, &data);
       
  1203 		User::WaitForRequest(stat1);
       
  1204 		
       
  1205 		ret = thread5_2.WaitForExitL();
       
  1206 		test_KErrNone(ret);
       
  1207 		}
       
  1208 		test.Next(_L("PIPE TEST:7.15 After Wait finish close the handles.\n"));
       
  1209 		test (stat1.Int() == KErrNone);
       
  1210 		aWriter.Close();
       
  1211 		aReader.Close();
       
  1212 		// Release all the resources.
       
  1213 		ret = RPipe::Destroy(cPipeName);
       
  1214 		test_KErrNone(ret);
       
  1215 		
       
  1216 		/*****************Newly added tests for CR 1114 - WaitForWriter *********/
       
  1217 		
       
  1218 		ret = RPipe::Define( cPipeName,aSize);
       
  1219 		test_KErrNone(ret);	
       
  1220 		ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1221 		test (ret == KErrNone);
       
  1222 
       
  1223 
       
  1224 		// Create Thread 11
       
  1225 		// Pass TData object with Write and Read handle both
       
  1226 		
       
  1227 		test.Next(_L("PIPE TEST:7.16 Register a valid Wait Request .\n"));
       
  1228 		aReader.WaitForWriter(cPipeName, stat1);
       
  1229 		test(stat1==KRequestPending);
       
  1230 			
       
  1231 		{	
       
  1232 		TTestThread thread11(KThread11Name, TestThread11, &data);
       
  1233 		User::WaitForRequest(stat1);
       
  1234 
       
  1235 		ret = thread11.WaitForExitL();
       
  1236 		test_KErrNone(ret);
       
  1237 		}
       
  1238 
       
  1239 		test.Next(_L("PIPE TEST:7.17 After Wait finish check the value of status register.\n"));
       
  1240 		aWriter.Close();
       
  1241 		aReader.Close();
       
  1242 		ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1243 		test_KErrNone(ret);	
       
  1244 		ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1245 		test_KErrNone(ret);	
       
  1246 		
       
  1247 		aWriter.WaitForWriter(cPipeName, stat1);
       
  1248 		test (stat1.Int() ==  KErrAccessDenied);
       
  1249 		
       
  1250 		test.Next(_L("PIPE TEST:7.18 After Write handle is open , register request to Wait\n"));
       
  1251 		
       
  1252 		aReader.WaitForWriter(cPipeName, stat1);
       
  1253 		test (stat1.Int() == KErrNone);
       
  1254 		
       
  1255 		test.Next(_L("PIPE TEST:7.19 Check for CancelWait.\n"));
       
  1256 		aWriter.Close();
       
  1257 		aReader.Close();
       
  1258 		ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1259 	
       
  1260 		aReader.WaitForWriter(cPipeName, stat1);
       
  1261 		aReader.WaitForWriter(cPipeName, stat2);
       
  1262 		test(stat2.Int() == KErrInUse);
       
  1263 		aReader.CancelWait();
       
  1264 		test(stat1.Int() == KErrCancel);
       
  1265 		test(stat2.Int() == KErrInUse);
       
  1266 		aReader.WaitForWriter(cPipeName, stat1);
       
  1267 		ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1268 		test(stat1.Int() == KErrNone);
       
  1269 		
       
  1270 		// Release all the resources.
       
  1271 		aWriter.Close();
       
  1272 		aReader.Close();
       
  1273 		
       
  1274 		// Create Thread 11
       
  1275 		// Pass TData object with Write and Read handle both
       
  1276 
       
  1277 		test.Next(_L("PIPE TEST:7.20 Register a valid Wait Request .\n"));
       
  1278 		aReader.WaitForWriter(cPipeName, stat1);
       
  1279 				
       
  1280 		{
       
  1281 		TTestThread thread11_2(KThread11Name, TestThread11, &data);
       
  1282 		User::WaitForRequest(stat1);
       
  1283 		
       
  1284 		test.Next(_L("PIPE TEST:7.21 After Wait finish , close the hadles.\n"));
       
  1285 		test (stat1.Int() == KErrNone);
       
  1286 		ret = thread11_2.WaitForExitL();
       
  1287 		test_KErrNone(ret);
       
  1288 
       
  1289 		// Release all the resources.
       
  1290 		aWriter.Close();
       
  1291 		aReader.Close();
       
  1292 		}
       
  1293 
       
  1294 		ret = RPipe::Destroy(cPipeName);
       
  1295 		test_KErrNone(ret);
       
  1296 		
       
  1297 		/**********************************************************/
       
  1298 		
       
  1299 		// Define the pipe.
       
  1300 		ret = RPipe::Define( cPipeName,aSize);
       
  1301 		test(ret == KErrNone);
       
  1302 		
       
  1303 		// Wait for Writer.
       
  1304 		aReader.WaitForWriter(cPipeName, stat1);
       
  1305 		// Try to open read end again. It should not open because WaitForWriter
       
  1306 		// will has already opened the Read End of the pipe.
       
  1307 		ret = aReader.Open(cPipeName ,RPipe::EOpenToRead );
       
  1308 		test(ret = KErrInUse );
       
  1309 		// Status of the wait request is pending because writer is not opened yet.
       
  1310 		test (stat1.Int() == KRequestPending);
       
  1311 		
       
  1312 		// Wait on Reader.
       
  1313 		aWriter.WaitForReader(cPipeName , stat2);
       
  1314 		// Reader was already opened so status is KErrNone.
       
  1315 		test ( stat2.Int() == KErrNone);
       
  1316 		
       
  1317 		// Try to open Write end. It should not allow because WaitForReader has 
       
  1318 		// already opened the write end of the pipe.
       
  1319 		ret = aWriter.Open(cPipeName ,RPipe::EOpenToWrite);
       
  1320 		test (ret == KErrInUse);
       
  1321 		
       
  1322 		// Check the status of the WaitForWriter  request. It should be KErrNone now.
       
  1323 		test (stat1.Int() == KErrNone);
       
  1324 		
       
  1325 		// Let's check for pipe attributes.
       
  1326 		ret = aReader.MaxSize();
       
  1327 		test (ret = aSize);
       
  1328 		ret = aWriter.MaxSize();
       
  1329 		test (ret = aSize);
       
  1330 		ret = aReader.Size();
       
  1331 		test (ret = aSize);
       
  1332 		ret = aWriter.Size();
       
  1333 		test (ret = aSize);
       
  1334 		
       
  1335 		// Close the Reade handle.
       
  1336 		aReader.Close();
       
  1337 		
       
  1338 		// Put request to wait for Reader.
       
  1339 		aWriter.WaitForReader(cPipeName , stat2);
       
  1340 		// It should be pending.
       
  1341 		test ( stat2.Int() == KRequestPending);
       
  1342 		// Open the reader end of the pipe. It should allow , KErrNone
       
  1343 		ret = aReader.Open(cPipeName ,RPipe::EOpenToRead );
       
  1344 		test ( stat2.Int() == KErrNone);
       
  1345 		test (ret == KErrNone);
       
  1346 		
       
  1347 		aReader.Close();
       
  1348 		aWriter.Close();
       
  1349 		ret=RPipe::Destroy(cPipeName);
       
  1350 		test_KErrNone(ret);
       
  1351 									
       
  1352 		return;
       
  1353 
       
  1354 
       
  1355 }
       
  1356 
       
  1357 /****************************************************************************
       
  1358 	This is a function to test notify mechanism of pipes.
       
  1359 	Check the functionality of following library functions
       
  1360 		- Notify...()
       
  1361 
       
  1362 
       
  1363 ******************************************************************************/
       
  1364 void TestNotifyMechanismPipes() {
       
  1365 		// Test NotifyDataAvailable , NotifySpaceAvailable
       
  1366 		RSemaphore 					globalSem;						// Handle to a global semaphore. Semaphore is used to maintain synchronisation between thread4 and the main thread 
       
  1367 		TInt						ret;							// Return Value variable.
       
  1368 		RPipe						aReader,aWriter;				// Used to pass to thread.
       
  1369 		RPipe						aReader2,aWriter2;				// Used to pass to thread.
       
  1370 		
       
  1371 		TInt						aSize;							// Used to pass to thread.
       
  1372 		const TBufC<50>				cPipeName(KPipe1Name);			// Descriptor to hold data for Writing.
       
  1373 
       
  1374 		TRequestStatus				stat1;
       
  1375 		TBuf8<50>					cPipeTestData1(KTestData2);
       
  1376 		
       
  1377 		const TBufC<50>             cSemaphoreName(KSemaphoreName); // Descriptor conataining the name of the global semaphore.
       
  1378 
       
  1379 		
       
  1380 		ret = globalSem.CreateGlobal(cSemaphoreName,0);		//create and open a global semaphore with initial count as 0.
       
  1381 		test (ret == KErrNone);
       
  1382 		
       
  1383 		aSize = 22; 											// Size of KTestData * 10
       
  1384 
       
  1385 		ret = RPipe::Create(	aSize,
       
  1386 								aReader,
       
  1387 								aWriter,
       
  1388 								EOwnerProcess, //
       
  1389 								EOwnerProcess //
       
  1390 							 );
       
  1391 
       
  1392 		TData data(	&aReader, &aWriter);
       
  1393 
       
  1394 		// Create Thread 4
       
  1395 		// Pass TData object with Write and Read handle both
       
  1396 		test.Next(_L("PIPE TEST:6.1 Call CancelDataAvailable/CancelSpaceAvailable using unopened RPipe handles.\n"));
       
  1397 		ret = aReader2.CancelDataAvailable();
       
  1398 		test ( ret == KErrBadHandle);
       
  1399 		ret = aWriter2.CancelSpaceAvailable();
       
  1400 		test (ret == KErrBadHandle);
       
  1401 		
       
  1402 		test.Next(_L("PIPE TEST:6.2 Call NotifySpaceAvailable with Negative size \n"));
       
  1403 		// Call Notfifyspace
       
  1404 		TInt	tempsize = -1;
       
  1405 		aWriter.NotifySpaceAvailable(tempsize, stat1);
       
  1406 		test ( stat1.Int() == KErrArgument);
       
  1407 
       
  1408 		test.Next(_L("PIPE TEST:6.2a Call NotifySpaceAvailable for space larger than pipe.\n"));
       
  1409 		aWriter.NotifySpaceAvailable(aSize+1, stat1);
       
  1410 		test_Equal( KErrArgument, stat1.Int());
       
  1411 		
       
  1412 		
       
  1413 		// Start the thread
       
  1414 		{
       
  1415 		TTestThread thread4(KThread4Name, TestThread4, &data);
       
  1416 
       
  1417 																	//	Thread 4
       
  1418 																	// 	Call Notifydata available
       
  1419 																	//	Loop for Available
       
  1420 																	//	Read the data and validate
       
  1421 
       
  1422 		test.Next(_L("PIPE TEST:6.3 Write data into the pipe , verify return value.\n"));
       
  1423 		// Write one byte data into the pipe.
       
  1424 		globalSem.Wait(); //wait before writing to ensure that other thread can register Data Available request while pipe is empty. 
       
  1425 		ret = aWriter.Write(cPipeTestData1,cPipeTestData1.Length());
       
  1426 		test (ret == cPipeTestData1.Length());
       
  1427 		
       
  1428 						
       
  1429 		test.Next(_L("PIPE TEST:6.4 Call NotifySpaceAvailable with valid parameters \n"));
       
  1430 		tempsize = 1;
       
  1431 		aWriter.NotifySpaceAvailable(tempsize, stat1);
       
  1432 		test(stat1==KRequestPending);
       
  1433 		globalSem.Signal();	//signal that thread 4 may go ahead and read 1 byte
       
  1434 		
       
  1435 		
       
  1436 		test.Next(_L("PIPE TEST:6.5 Wait till request says AVAILABLE. Available ?\n"));
       
  1437 		User::WaitForRequest(stat1);
       
  1438 		test ( stat1.Int() == KErrNone || stat1.Int() == KErrCompletion);
       
  1439 		
       
  1440 					// Thread 4
       
  1441 					// Notify data available
       
  1442 					// Loop for available
       
  1443 					// Flush the buffer
       
  1444 					// Register two request for Notifydata available
       
  1445 					// Register two request for Notifyspace available
       
  1446 					// Call Notifydata available using Read handle
       
  1447 					// Call Notifydata available using write handle
       
  1448 					// return
       
  1449 
       
  1450 
       
  1451 		// Check for Error conditions
       
  1452 
       
  1453 		// Flush buffer
       
  1454 		globalSem.Wait(); // this is waiting for a signal from thread4
       
  1455 		test.Next(_L("PIPE TEST:6.6 Register Notification for space availability. Allows ?\n"));
       
  1456 		ret = aWriter.Write(cPipeTestData1,cPipeTestData1.Length());
       
  1457 		ret = aWriter.Write(cPipeTestData1,cPipeTestData1.Length());
       
  1458 		// Register two request for Notifyspace available
       
  1459 		
       
  1460 		aWriter.NotifySpaceAvailable(tempsize, stat1);
       
  1461 		test_Equal(KRequestPending, stat1.Int() );
       
  1462 		test.Next(_L("PIPE TEST:6.7 Register One more Notification for space availability. Allows ?\n"));
       
  1463 		TRequestStatus tempstat1;
       
  1464 		aWriter.NotifySpaceAvailable(tempsize, tempstat1);
       
  1465 		test ( tempstat1.Int() == KErrInUse);
       
  1466 	
       
  1467 		test.Next(_L("PIPE TEST:6.7a Cancellation of non-outstanding requests must not disrupt oustanding requests\n"));
       
  1468 		aWriter.CancelWait();
       
  1469 		aWriter.NotifySpaceAvailable(tempsize, tempstat1);
       
  1470 		test_Equal( KErrInUse, tempstat1.Int());
       
  1471 
       
  1472 		test.Next(_L("PIPE TEST:6.8 Cancel all the pending Notifications.\n"));
       
  1473 		ret = aWriter.CancelSpaceAvailable();
       
  1474 		test ( ret == KErrNone);
       
  1475 		test_Equal(KErrCancel, stat1.Int() );
       
  1476 		
       
  1477 		test.Next(_L("PIPE TEST:6.9 Try to cancel some more notifications. It should not allow\n"));
       
  1478 		ret = aWriter.CancelSpaceAvailable();
       
  1479 		test ( ret == KErrNone);
       
  1480 		
       
  1481 		test.Next(_L("PIPE TEST:6.10 Register Notification for Data availability. Allows ?\n"));
       
  1482 		// Register two request for Notifydata available
       
  1483 		aWriter.Flush();
       
  1484 		aReader.NotifyDataAvailable(stat1);
       
  1485 			
       
  1486 		test.Next(_L("PIPE TEST:6.11 Register One More Notification for Data availability. Allows ?\n"));
       
  1487 				
       
  1488 		aReader.NotifyDataAvailable(tempstat1);
       
  1489 		test ( ( tempstat1.Int() == KErrInUse) || (tempstat1.Int() == KErrCompletion));
       
  1490 		
       
  1491 		test.Next(_L("PIPE TEST:6.12 Cancel all the pending Notifications for Data.\n"));
       
  1492 		ret = aReader.CancelDataAvailable();
       
  1493 		test ( ( ret == KErrNone) || (ret == KErrNotFound));
       
  1494 		test (stat1.Int() == KErrCancel);
       
  1495 		
       
  1496 		test.Next(_L("PIPE TEST:6.13 Try to cancel some more notifications. It should not allow\n"));
       
  1497 		ret = aReader.CancelDataAvailable();
       
  1498 		test ( ret == KErrNone);
       
  1499 
       
  1500 		test.Next(_L("PIPE TEST:6.14 Try to register Data available notification using Write handle. Should not allow.\n"));
       
  1501 		aWriter.NotifyDataAvailable(stat1);
       
  1502 		test ( stat1.Int() == KErrAccessDenied);
       
  1503 		
       
  1504 		test.Next(_L("PIPE TEST:6.15 Try to register Space available notification using Read handle. Should not allow.\n"));
       
  1505 		aReader.NotifySpaceAvailable(tempsize, stat1);
       
  1506 		test ( stat1.Int() == KErrAccessDenied);
       
  1507 		
       
  1508 		test.Next(_L("PIPE TEST:6.16 Try to Cancel Data available notification using Write handle. Should not allow.\n"));
       
  1509 		ret = aWriter.CancelDataAvailable();
       
  1510 		test ( ret == KErrAccessDenied);
       
  1511 		
       
  1512 		test.Next(_L("PIPE TEST:6.17 Try to Cancel Space available notification using Write handle. Should not allow.\n"));
       
  1513 		ret = aReader.CancelSpaceAvailable();
       
  1514 		test ( ret == KErrAccessDenied);
       
  1515 
       
  1516 		// Stop the thread and Close the Thread
       
  1517 		ret = thread4.WaitForExitL();
       
  1518 		}
       
  1519 		test_KErrNone(ret);
       
  1520 		// Close all the pipe handles.
       
  1521 		aReader.Close();
       
  1522 		aWriter.Close();
       
  1523 	
       
  1524 	
       
  1525 		
       
  1526 		test.Printf(_L(" TEST NOTIFICATION MECHNISM FOR NAMED PIPES\n"));
       
  1527 		
       
  1528 		aSize = 22; // Size of KTestData
       
  1529 		
       
  1530 		test.Next(_L("PIPE TEST:6.18 Create Named pipe and Open Read/Write Handles.\n"));
       
  1531 		ret = RPipe::Define( cPipeName,aSize);
       
  1532 		test (ret == KErrNone);
       
  1533 		ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1534 		test (ret == KErrNone);
       
  1535 		ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1536 		test (ret == KErrNone);
       
  1537 		
       
  1538 		
       
  1539 	
       
  1540 		TData data2(	&aReader, &aWriter);
       
  1541 
       
  1542 		// Create Thread 4
       
  1543 		// Pass TData object with Write and Read handle both
       
  1544 		TTestThread thread4a(KThread4Name, TestThread4, &data2);
       
  1545 
       
  1546 														//	Thread 4
       
  1547 														// 	Call Notifydata available
       
  1548 														//	Loop for Available
       
  1549 														//	Read the data and validate
       
  1550 		
       
  1551 		// Write one byte data into the pipe.
       
  1552 		test.Next(_L("PIPE TEST:6.19 Write Data and check for return value.\n"));
       
  1553 		globalSem.Wait(); //wait before writing to ensure that other thread can register Data Available request while pipe is empty. 
       
  1554 		ret = aWriter.Write(cPipeTestData1,cPipeTestData1.Length());
       
  1555 		test (ret == cPipeTestData1.Length());
       
  1556 
       
  1557 		test.Next(_L("PIPE TEST:6.20 Register Notification for Space Available.\n"));
       
  1558 		// Call Notfifyspace
       
  1559 		tempsize = 1;
       
  1560 		aWriter.NotifySpaceAvailable(tempsize, stat1);
       
  1561 		test(stat1==KRequestPending);
       
  1562 		globalSem.Signal();	//signal that thread 4 may go ahead and read byte
       
  1563 		
       
  1564 		test.Next(_L("PIPE TEST:6.21 Wait till notified for Space Availanle.\n"));
       
  1565 		User::WaitForRequest(stat1);
       
  1566 		test ( stat1.Int() == KErrNone);
       
  1567 
       
  1568 														// Thread 4
       
  1569 														// Read one byte of data
       
  1570 														// Thread 4
       
  1571 														// Notify data available
       
  1572 														// Loop for available
       
  1573 														// Flush the buffer
       
  1574 														// Register two request for Notifydata available
       
  1575 														// Register two request for Notifyspace available
       
  1576 														// Call Notifydata available using Read handle
       
  1577 														// Call Notifydata available using write handle
       
  1578 														// return
       
  1579 														
       
  1580 		globalSem.Wait();	//waiting for a signal from thread4.
       
  1581 		test.Next(_L("PIPE TEST:6.22 Notify for Space available.\n"));
       
  1582 		// Register two request for Notifydata available
       
  1583 		ret = aWriter.Write(cPipeTestData1,cPipeTestData1.Length());
       
  1584 		aWriter.NotifySpaceAvailable(tempsize, stat1);
       
  1585 		
       
  1586 		test.Next(_L("PIPE TEST:6.23 Notify one more notifier for Space available.\n"));
       
  1587 		// Register two request for Notifyspace available
       
  1588 		TRequestStatus notifyStatus;
       
  1589 		aWriter.NotifySpaceAvailable(tempsize, notifyStatus);
       
  1590 		test ( notifyStatus.Int() == KErrInUse);
       
  1591 		
       
  1592 		aWriter.Flush();
       
  1593 		aWriter.NotifySpaceAvailable(tempsize, stat1);
       
  1594 		test ( stat1.Int() == KErrNone);
       
  1595 		
       
  1596 		test.Next(_L("PIPE TEST:6.24 Cancel Notify for Space available.\n"));
       
  1597 		ret = aWriter.CancelSpaceAvailable();
       
  1598 		test (( ret == KErrNotFound) ||( ret == KErrNone));
       
  1599 		test.Next(_L("PIPE TEST:6.25 Cancel one more Notify for Space available.\n"));
       
  1600 		ret = aWriter.CancelSpaceAvailable();
       
  1601 		test ( ret == KErrNone);
       
  1602 		
       
  1603 		test.Next(_L("PIPE TEST:6.26 Register Notify for Data available.\n"));
       
  1604 		// Register two request for Notifydata available
       
  1605 		
       
  1606 		aWriter.Flush();
       
  1607 		TRequestStatus stat5,stat6;
       
  1608 		aReader.NotifyDataAvailable(stat5);
       
  1609 			
       
  1610 		test.Next(_L("PIPE TEST:6.27 Register One more Notify for Data available.\n"));
       
  1611 		// Register two request for Notifyspace available
       
  1612 		aReader.NotifyDataAvailable(notifyStatus);
       
  1613 		test ( notifyStatus.Int() == KErrInUse);
       
  1614 		test.Next(_L("PIPE TEST:6.28 Cancel Notify for Data available.\n"));
       
  1615 		ret = aReader.CancelDataAvailable();
       
  1616 		test ( ret == KErrNone);
       
  1617 		test (stat5.Int() == KErrCancel);
       
  1618 		
       
  1619 		test.Next(_L("PIPE TEST:6.29 Cancel One more Notify for Data available.\n"));
       
  1620 		ret = aReader.CancelDataAvailable();
       
  1621 		test ( ret == KErrNone);
       
  1622 
       
  1623 		test.Next(_L("PIPE TEST:6.30 Register Notify for Data available using Write handle\n"));
       
  1624 		aWriter.NotifyDataAvailable(stat6);
       
  1625 		test ( stat6.Int() == KErrAccessDenied);
       
  1626 		
       
  1627 		test.Next(_L("PIPE TEST:6.31 Register Notify for Space available using Read handle\n"));
       
  1628 		aReader.NotifySpaceAvailable(tempsize, stat6);
       
  1629 		test ( stat6.Int() == KErrAccessDenied);
       
  1630 		
       
  1631 		test.Next(_L("PIPE TEST:6.32 Cancel Notify for Data available using Write handle\n"));
       
  1632 		ret = aWriter.CancelDataAvailable();
       
  1633 		test ( ret == KErrAccessDenied);
       
  1634 		
       
  1635 		test.Next(_L("PIPE TEST:6.33 Cancel Notify for Space available using Read handle\n"));
       
  1636 		ret = aReader.CancelSpaceAvailable();
       
  1637 		test ( ret == KErrAccessDenied);
       
  1638 
       
  1639 	//close the handle to the global semaphore
       
  1640 	
       
  1641 	globalSem.Close();
       
  1642 	// Stop the thread and Close the Thread
       
  1643 		ret = thread4a.WaitForExitL();
       
  1644 		test_KErrNone(ret);
       
  1645 	aReader.Close();
       
  1646 	aWriter.Close();
       
  1647 	RPipe::Destroy(cPipeName);
       
  1648 	
       
  1649 
       
  1650 	return;
       
  1651 
       
  1652 
       
  1653 
       
  1654 } // End TestNotifyMechanismPipes()
       
  1655 
       
  1656 /****************************************************************************
       
  1657 	This is a function to test Named pipes in Mutli process environment.
       
  1658 	Check the functionality of following library functions
       
  1659 		- Define ()
       
  1660 		-
       
  1661 
       
  1662 
       
  1663 ******************************************************************************/
       
  1664 
       
  1665 
       
  1666 _LIT8(KTestDataIP, "ABCDEFGHIJ");
       
  1667 
       
  1668 void TestMultiProcessNamedPipes() {
       
  1669 
       
  1670 _LIT(KPipeName5, "InterProcessPipe1");
       
  1671 	TInt 						ret;								// Return value variable
       
  1672 	RProcess					proc;								// Process Handle
       
  1673 	RPipe 						aWriter;
       
  1674 	RPipe						aWriterUN,aReaderUN;
       
  1675 	const	TBufC<150>			cPipeName1(KPipeName5);
       
  1676 
       
  1677 	TBufC8<150>					cPipeWriteData1(KTestDataIP);
       
  1678 	TInt						aSize;
       
  1679 	TRequestStatus				stat1;
       
  1680 	TBuf8<150>					cPipeReadData1;
       
  1681 
       
  1682 	aSize = 10;
       
  1683 
       
  1684 	// Define Pipe with valid size.
       
  1685 
       
  1686 	test.Next(_L("PIPE TEST:5.1 Define Pipe and Create UnNAmed pipe and pass handle to other process.\n"));
       
  1687 	ret = RPipe::Define(cPipeName1, aSize);
       
  1688 	test_KErrNone(ret);	
       
  1689 	ret = proc.Create(
       
  1690 						KProcessName,								// Launch t_pipe2.exe process
       
  1691 					 	KNullDesC									// No arguments passed to t_pipe2.exe
       
  1692 					 );
       
  1693 
       
  1694 	if (ret != KErrNone) {
       
  1695 		// Check for process successfully launched
       
  1696 		test.Printf(_L("Error : Could not start the process t_pipe2.exe \n"));
       
  1697 	}
       
  1698 	test_KErrNone(ret);	
       
  1699 	TRequestStatus procLogon;
       
  1700 	proc.Logon(procLogon);
       
  1701 	test(procLogon==KRequestPending);
       
  1702 
       
  1703 	aSize = 512;
       
  1704 	ret = RPipe::Create(	aSize,
       
  1705 							aReaderUN,
       
  1706 							aWriterUN,
       
  1707 							EOwnerProcess,//TOwnerType aTypeW
       
  1708 							EOwnerProcess //TOwnerType aTypeW
       
  1709 						);
       
  1710 	test_KErrNone(ret);	
       
  1711 						
       
  1712 	ret = aWriterUN.Write(KTestData,22);
       
  1713 	test (ret == 22);
       
  1714 	ret = aReaderUN.Read(cPipeReadData1,10);
       
  1715 	test ( ret == 10);
       
  1716 	aWriterUN.Close();
       
  1717 	ret = aReaderUN.Read(cPipeReadData1,6);
       
  1718 	test (ret == 6);
       
  1719 	aReaderUN.NotifyDataAvailable(stat1);
       
  1720 	ret = stat1.Int();
       
  1721 	test (ret == KErrNone);
       
  1722 	ret = aReaderUN.ReadBlocking(cPipeReadData1,6);
       
  1723 	test (ret == 6);
       
  1724 	ret = aReaderUN.Read(cPipeReadData1,1);
       
  1725 	test ( ret == KErrNotReady);
       
  1726 	ret = aReaderUN.ReadBlocking(cPipeReadData1,1);
       
  1727 	test ( ret == KErrNotReady);
       
  1728 	aReaderUN.Close();
       
  1729 		
       
  1730 	ret = RPipe::Create(	aSize,
       
  1731 							aReaderUN,
       
  1732 							aWriterUN,
       
  1733 							EOwnerProcess,//TOwnerType aTypeW
       
  1734 							EOwnerProcess //TOwnerType aTypeW
       
  1735 						);
       
  1736 	test_KErrNone(ret);	
       
  1737 	proc.SetParameter(3,aReaderUN);
       
  1738 	aReaderUN.Close();
       
  1739 	ret = aWriterUN.Write(KTestData,22);
       
  1740 	test (ret == 22 );
       
  1741 	
       
  1742 	proc.Resume();
       
  1743 
       
  1744 	aSize = 10;
       
  1745 	test.Next(_L("PIPE TEST:5.2 Open Write handle to Pipe. Wait till Read handle Opened\n"));
       
  1746 	ret = aWriter.Open(cPipeName1, RPipe::EOpenToWrite);
       
  1747 	test_KErrNone(ret);	
       
  1748 	aWriter.Wait(cPipeName1,stat1);
       
  1749 	User::WaitForRequest(stat1);
       
  1750 	test (stat1.Int() == KErrNone);
       
  1751 	
       
  1752 	test.Next(_L("PIPE TEST:5.3 Write data to Pipe.\n"));
       
  1753 	ret = aWriter.Write(cPipeWriteData1,cPipeWriteData1.Length());
       
  1754 	test ( ret == cPipeWriteData1.Length());
       
  1755 
       
  1756 	test.Next(_L("PIPE TEST:5.4 Wait till Space Available in Pipe.\n"));
       
  1757 	aWriter.NotifySpaceAvailable(aSize,stat1);
       
  1758 	User::WaitForRequest(stat1);
       
  1759 	
       
  1760 	test.Next(_L("PIPE TEST:5.5 Write the data using WriteBlocking call\n"));
       
  1761 	test_Equal(0, aWriter.Size());
       
  1762 	ret = aWriter.WriteBlocking(cPipeWriteData1,cPipeWriteData1.Length());
       
  1763 	test ( ret ==cPipeWriteData1.Length() );
       
  1764 
       
  1765 	User::WaitForRequest(procLogon);
       
  1766 	
       
  1767 	aWriter.Close();
       
  1768 	ret=RPipe::Destroy(cPipeName1);
       
  1769 	test_KErrNone(ret);
       
  1770 	
       
  1771 	proc.Close();
       
  1772 	aWriterUN.Close();
       
  1773 	aReaderUN.Close();
       
  1774 	return;
       
  1775 	
       
  1776 } // End TestMultiProcessNamedPipes ()
       
  1777 /****************************************************************************
       
  1778 	This is a function to test Named pipes in Multi threaded environment.
       
  1779 	Check the functionality of following library functions
       
  1780 		- Define ()
       
  1781 		- Read()
       
  1782 		- Write()
       
  1783 		- ReadBlocking()
       
  1784 		- WriteBlocking()
       
  1785 
       
  1786 
       
  1787 ******************************************************************************/
       
  1788 //
       
  1789 // Test defining and opening both ends of pipe
       
  1790 // Attempt to readblock from pipe closed at far end (should fail)
       
  1791 // Attemp writeblock to pipe closed at far end
       
  1792 //
       
  1793 // Do some normal reading and writing between threads
       
  1794 // do some blocking read/write between threads
       
  1795 void TestMultiThreadNamedPipes() {
       
  1796 
       
  1797 	TInt					ret;							// Return Value variable.
       
  1798 	RPipe					aReader,aWriter;				// Used to pass to thread.
       
  1799 	TInt					aSize;							// Used to pass to thread.
       
  1800 	const TBufC<50>			cPipeName(KPipe1Name);			// Descriptor to hold data for Writing.
       
  1801 	TInt					aReadSize;
       
  1802 	TBuf8<150>				cPipeReadData;
       
  1803 	TBufC8<50> 				cTestData(KTestData); 			// Test Data
       
  1804 
       
  1805 
       
  1806 
       
  1807 
       
  1808 ///////////////////////////////////////////////////////////
       
  1809 //  PART : 1											///
       
  1810 //	Test Read and Write Functionality					///
       
  1811 ///////////////////////////////////////////////////////////
       
  1812 
       
  1813 	test.Next(_L("PIPE TEST:4.1 Define Pipe , Open Read and Write Handle.\n"));
       
  1814 	aSize = 22;
       
  1815 	ret = RPipe::Define( cPipeName,aSize);
       
  1816 	test (ret == KErrNone);
       
  1817 	
       
  1818 	ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1819 	test (ret == KErrNone);
       
  1820 	
       
  1821 	ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1822 	test (ret == KErrNone);
       
  1823 	
       
  1824 	aWriter.Close();
       
  1825 	
       
  1826 	test.Next(_L("PIPE TEST:4.2 ReadBlocking: Check for KErrNotReady.\n"));
       
  1827 	aReadSize = 1;
       
  1828 	ret = aReader.ReadBlocking(cPipeReadData,aReadSize);
       
  1829 	test (ret == KErrNotReady);
       
  1830 	
       
  1831 	ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1832 	test (ret == KErrNone);
       
  1833 	
       
  1834 	
       
  1835 	aReader.Close();
       
  1836 	test.Next(_L("PIPE TEST:4.3 WriteBlocking: Check for KErrNotReady.\n"));
       
  1837 	ret = aWriter.WriteBlocking(cTestData,cTestData.Length());
       
  1838 	test (ret == KErrNotReady);
       
  1839 		
       
  1840 	ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1841 	test (ret == KErrNone);
       
  1842 	
       
  1843 
       
  1844 	TData data(	&aReader, &aWriter);
       
  1845 
       
  1846 
       
  1847 
       
  1848 
       
  1849 	// Write data to Pipe using valid handle
       
  1850 	test.Next(_L("PIPE TEST:4.4 Write into the pipe and verify return value.\n"));
       
  1851 	ret = aWriter.Write(cTestData,cTestData.Length());
       
  1852 	test (ret == cTestData.Length() );
       
  1853 
       
  1854 	
       
  1855 	// Keep writing the data into pipe till it overflows
       
  1856 	
       
  1857 	test.Next(_L("PIPE TEST:4.5 Write into the pipe till it overflows.\n"));
       
  1858 	ret = aWriter.Write(cTestData,cTestData.Length());
       
  1859 	test (( ret == KErrOverflow)) ;
       
  1860 	
       
  1861 	// Start the thread.
       
  1862 	TTestThread thread2(KThread2Name, TestThread2, &data);
       
  1863 	// Read 1 byte data from pipe
       
  1864 	// Validate data
       
  1865 	// Read aByte size data
       
  1866 	// Validate data
       
  1867 	// Thread 2
       
  1868 	// Read aByte size data
       
  1869 	// User:: After (10000)
       
  1870 	// Keep reading data till zero return
       
  1871 	// return
       
  1872 
       
  1873 	// Stop the thread and Close the Thread
       
  1874 	ret = thread2.WaitForExitL();
       
  1875 	test_KErrNone(ret);
       
  1876 
       
  1877 	aReader.Close();
       
  1878 	aWriter.Close();
       
  1879 	ret = RPipe::Destroy(cPipeName);
       
  1880 	test_KErrNone(ret);
       
  1881 
       
  1882 
       
  1883 ///////////////////////////////////////////////////////////
       
  1884 //  PART : 2											///
       
  1885 //	Test ReadBlocking and WriteBlocking Functionality	///
       
  1886 ///////////////////////////////////////////////////////////
       
  1887 
       
  1888 	// Test Read and Write blocking call
       
  1889 
       
  1890 	test.Next(_L("PIPE TEST:4.6 Create UnNamed Pipe with valid size.\n"));
       
  1891 	
       
  1892 	aSize = 22;
       
  1893 	ret = RPipe::Define( cPipeName,aSize);
       
  1894 	test (ret == KErrNone);
       
  1895 	
       
  1896 	ret = aReader.Open(cPipeName,RPipe::EOpenToRead);
       
  1897 	test (ret == KErrNone);
       
  1898 	
       
  1899 	ret = aWriter.Open(cPipeName,RPipe::EOpenToWrite);
       
  1900 	test (ret == KErrNone);
       
  1901 	
       
  1902 	// Create TData object to pass Pipe handles.
       
  1903 	TData data2(	&aReader, &aWriter);
       
  1904 
       
  1905 	
       
  1906 	// Create Thread ( Note : Thread is in pending state now)
       
  1907 	// Flush the data if any in pipe to make sure its empty.
       
  1908 	aWriter.Flush();
       
  1909 	
       
  1910 	// Start the thread
       
  1911 	TTestThread thread3(KThread3Name, TestThread3, &data2);
       
  1912 											// Thread 3
       
  1913 											// Write one byte of data
       
  1914 											// Write one one byte of data using WriteBlocking call
       
  1915 											// Write aByte size data using Writblocking call
       
  1916 
       
  1917 	
       
  1918 	// Call Readblocking function.Read one byte of data.
       
  1919 	test.Next(_L("PIPE TEST:4.7 Flush the buffer and Call ReadBlocking one byte into the pipe.\n"));
       
  1920 		aReadSize = 1;
       
  1921 		ret = aReader.ReadBlocking(cPipeReadData,aReadSize);
       
  1922 		test_Equal(aReadSize, ret);
       
  1923 	
       
  1924 	test.Next(_L("PIPE TEST:4.8 Verify the data received.\n"));
       
  1925 		test ( KErrNone == cPipeReadData.Compare(KTestData1));
       
  1926 		
       
  1927 	
       
  1928 	test.Next(_L("PIPE TEST:4.9 Call ReadBlocking and read complete data from Pipe. As big as Pipe Size\n"));
       
  1929 	// Call Readblocking function.Read aSize bytes of data.
       
  1930 		aReadSize = aSize-aReadSize; //read rest of data
       
  1931 		ret = aReader.ReadBlocking(cPipeReadData,aReadSize);
       
  1932 		test_Equal(aReadSize, ret);
       
  1933 		test ( KErrNone == cPipeReadData.Compare(KTestData3));
       
  1934 		
       
  1935 											// Thread 3
       
  1936 											// Wait for some time
       
  1937 											// Call Readblocking and read 1 byte of data
       
  1938 											// Call Readblocking and Read abyte size data
       
  1939 											// Call flush buffer
       
  1940 											// return
       
  1941 	test.Next(_L("PIPE TEST:4.10 Call ReadBlocking and read complete data from Pipe. As big as Pipe Size\n"));
       
  1942 	// Call Readblocking function.Read aSize bytes of data.
       
  1943 		aReadSize = aSize;
       
  1944 		ret = aReader.ReadBlocking(cPipeReadData,aReadSize);
       
  1945 		test_Equal(aReadSize, ret);
       
  1946 		test ( KErrNone == cPipeReadData.Compare(KTestData2));
       
  1947 		
       
  1948 	// Stop the thread and Close the Thread
       
  1949 	ret = thread3.WaitForExitL();
       
  1950 	test_KErrNone(ret);
       
  1951 
       
  1952 	aReader.Close();
       
  1953 	aWriter.Close();
       
  1954 	ret=RPipe::Destroy(cPipeName);
       
  1955 	test_KErrNone(ret);
       
  1956 	return ;
       
  1957 
       
  1958 } // End TestMultiThreadNamedPipes ()
       
  1959 
       
  1960 
       
  1961 /****************************************************************************
       
  1962 	This is a function to test UnNamed pipes in Multi threaded environment.
       
  1963 	Check the functionality of following library functions
       
  1964 		- Create()
       
  1965 		- Read()
       
  1966 		- Write()
       
  1967 		- ReadBlocking()
       
  1968 		- WriteBlocking()
       
  1969 
       
  1970 
       
  1971 ******************************************************************************/
       
  1972 void TestMultiThreadUnNamedPipes() {
       
  1973 
       
  1974 	TInt					ret;							// Return Value variable.
       
  1975 	RPipe					aReader,aWriter;				// Used to pass to thread.
       
  1976 	TInt					aSize;							// Used to pass to thread.
       
  1977 	TBuf8<250>				cPipeReadData;
       
  1978 	TInt					aReadSize;
       
  1979 	TBufC8<50> 				cTestData(KTestData); 			// Test Data
       
  1980 
       
  1981 
       
  1982 ///////////////////////////////////////////////////////////
       
  1983 //  PART : 1											///
       
  1984 //	Test Read and Write Functionality					///
       
  1985 ///////////////////////////////////////////////////////////
       
  1986 	
       
  1987 	test.Next(_L("PIPE TEST: 3.1 Create Pipe : Check for no erros on Pipe Creation \n"));
       
  1988 	ret = 100;
       
  1989 	aSize = 22; 										// Size of KTestData * 10
       
  1990 
       
  1991 	ret = RPipe::Create(		aSize,
       
  1992 								aReader,
       
  1993 								aWriter,
       
  1994 								EOwnerProcess ,//TOwnerType aTypeW
       
  1995 								EOwnerProcess //TOwnerType aTypeW
       
  1996 							 );
       
  1997 
       
  1998 	test (ret == KErrNone);
       
  1999 
       
  2000 	// Create TData object to pass Pipe handles.
       
  2001 	TData data1(	&aReader, &aWriter);
       
  2002 
       
  2003 	// Create Thread ( Note : Thread is in pending state now)
       
  2004 
       
  2005 	// Create test data stream.
       
  2006 	test.Next(_L("PIPE TEST: 3.2 Write Function test : Write data into the pipe \n"));
       
  2007 	ret = aWriter.Write(cTestData,cTestData.Length()); 		// Write ""Pipe Data To Be Passed"
       
  2008 	test (ret == cTestData.Length());
       
  2009 
       
  2010 	// Keep writing the data into pipe till it overflows
       
  2011 	test.Next(_L("PIPE TEST: 3.3 Write Data till the Pipe returns KErrOverFlow\n"));
       
  2012 	ret = aWriter.Write(cTestData,cTestData.Length());
       
  2013 	test ( ret == KErrOverflow);
       
  2014 	
       
  2015 	TTestThread thread2(KThread2Name, TestThread2, &data1);
       
  2016 	// Thread2
       
  2017 	// Read 1 byte data from pipe
       
  2018 	// Validate data
       
  2019 	// Read aByte size data
       
  2020 	// Validate data
       
  2021 	// Thread 2
       
  2022 	// Read aByte size data
       
  2023 	// User:: After (10000)
       
  2024 	// Keep reading data till zero return
       
  2025 	// return
       
  2026 	
       
  2027 	// Stop the thread , Close it.
       
  2028 	
       
  2029 	ret = thread2.WaitForExitL();
       
  2030 	test_KErrNone(ret);
       
  2031 
       
  2032 	aReader.Close();
       
  2033 	aWriter.Close();
       
  2034 
       
  2035 
       
  2036 ///////////////////////////////////////////////////////////
       
  2037 //  PART : 2											///
       
  2038 //	Test ReadBlocking and WriteBlocking Functionality	///
       
  2039 ///////////////////////////////////////////////////////////
       
  2040 
       
  2041 
       
  2042 	aSize = 22; 									// Size of KTestData
       
  2043 
       
  2044 	ret = RPipe::Create(		aSize,
       
  2045 								aReader,
       
  2046 								aWriter,
       
  2047 								EOwnerProcess ,			//TOwnerType aTypeW
       
  2048 								EOwnerProcess 			//TOwnerType aTypeW
       
  2049 					   );
       
  2050 	test_KErrNone(ret);	
       
  2051 
       
  2052 	// Create TData object to pass Pipe handles.
       
  2053 
       
  2054 	TData data2(&aReader, &aWriter);
       
  2055 
       
  2056 	// Flush the data if any in pipe to make sure its empty.
       
  2057 	 aWriter.Flush();
       
  2058 	 
       
  2059 	// Start the thread
       
  2060 	TTestThread thread3(KThread3Name, TestThread3, &data2);
       
  2061 						// Thread 3
       
  2062 						// Write one byte of data
       
  2063 						// Write one one byte of data using WriteBlocking call
       
  2064 						// Write aByte size data using Writblocking call
       
  2065 
       
  2066 
       
  2067 	// Call Readblocking function.Read one byte of data.
       
  2068 	
       
  2069 	aReadSize = 1;
       
  2070 	test.Next(_L("PIPE TEST: 3.4 : Call readblocking and read one byte of data \n"));
       
  2071 	ret = aReader.ReadBlocking(cPipeReadData,aReadSize);
       
  2072 	// Call goes to Thread 3
       
  2073 	test_Equal(aReadSize, ret);
       
  2074 	
       
  2075 	test.Next(_L("PIPE TEST: 3.5 : Validate the data \n"));
       
  2076 	test_Equal(0, cPipeReadData.Compare(KTestData1));
       
  2077 	
       
  2078 
       
  2079 	// Call Readblocking function.Read aSize bytes of data.
       
  2080 	test.Next(_L("PIPE TEST: 3.6 : Read remaining data \n"));
       
  2081 	aReadSize = 21;
       
  2082 	ret = aReader.ReadBlocking(cPipeReadData,aReadSize);
       
  2083 	test_Equal(aReadSize, ret);
       
  2084 	test_Equal(0, cPipeReadData.Compare(KTestData3));
       
  2085 	
       
  2086 	
       
  2087 	test.Next(_L("PIPE TEST: 3.7 : Read complete pipe size data \n"));
       
  2088 	aReadSize = 22;
       
  2089 	ret = aReader.ReadBlocking(cPipeReadData,aReadSize);
       
  2090 	test_Equal(aReadSize, ret);
       
  2091 	test_Equal(0, cPipeReadData.Compare(KTestData2));
       
  2092 	
       
  2093 	// Wait for thread to end and Close
       
  2094 	ret = thread3.WaitForExitL();
       
  2095 	test_KErrNone(ret);
       
  2096 
       
  2097 	aReader.Close();
       
  2098 	aWriter.Close();
       
  2099 
       
  2100 	return ;
       
  2101 
       
  2102 
       
  2103 }// End TestMultiThreadUnNamedPipes()
       
  2104 
       
  2105 /****************************************************************************
       
  2106 	This is a function to test Named pipes in Single threaded environment.
       
  2107 	Check the functionality of following library functions
       
  2108 		- Define ()
       
  2109 		-
       
  2110 
       
  2111 
       
  2112 ******************************************************************************/
       
  2113 
       
  2114 void TestSingleThreadNamedPipes()
       
  2115 
       
  2116 {
       
  2117 
       
  2118 
       
  2119 	const TBufC<50> 		cPipeName(KPipe1Name); 		// Descriptor to hold data for Writing.
       
  2120 	TInt 					aSize, ret;
       
  2121 	RPipe 					testPipe1;
       
  2122 	RPipe 					testPipe2;
       
  2123 	RPipe					testPipe3;
       
  2124 	RPipe					testPipe4;
       
  2125 	RPipe					testPipe5;
       
  2126 	const TBufC<100> 		cPipeNameMax(KMaxPipeName);
       
  2127 	const TBufC<100> 		cPipeNameMaxPlusOne(KMaxPipeNamePlusOne);
       
  2128 
       
  2129 	_LIT(KBadName , "***?SFSDFWE?*_-");
       
  2130 	_LIT(KBadName2 , "");
       
  2131 
       
  2132 	test.Next(_L("PIPE TEST: 2.1 Define Function test : Check for No Error\n"));
       
  2133 		aSize = 10;
       
  2134 
       
  2135 		ret = RPipe::Define(cPipeName , aSize);
       
  2136 		test (ret == KErrNone);
       
  2137 	
       
  2138 		ret = RPipe::Destroy (cPipeName);
       
  2139 		test (ret == KErrNone);
       
  2140 
       
  2141 	
       
  2142 
       
  2143 	test.Next(_L("PIPE TEST: 2.2 Define Function test : Check for Max length \n"));
       
  2144 	
       
  2145 		aSize = 10;
       
  2146 
       
  2147 		ret = RPipe::Define(cPipeNameMax , aSize);
       
  2148 		test (ret == KErrNone);
       
  2149 	
       
  2150 		ret = RPipe::Destroy (cPipeNameMax);
       
  2151 		test (ret == KErrNone);
       
  2152 
       
  2153 	test.Next(_L("PIPE TEST: 2.3 Define Function test : Check for Max length \n"));
       
  2154 		
       
  2155 		aSize = 10;
       
  2156 		ret = RPipe::Define(cPipeNameMaxPlusOne , aSize);
       
  2157 		test (ret == KErrBadName);
       
  2158 		ret = RPipe::Destroy (cPipeNameMaxPlusOne);
       
  2159 		test (ret == KErrBadName);
       
  2160 
       
  2161 	test.Next(_L("PIPE TEST: 2.4 Open Function test : Test Open  \n"));
       
  2162 		aSize = 10;
       
  2163 		ret = RPipe::Define(cPipeName , aSize);
       
  2164 		test_KErrNone(ret);
       
  2165 
       
  2166 		ret = testPipe1.Open(KBadName,RPipe::EOpenToRead);
       
  2167 		test (ret == KErrBadName);
       
  2168 		
       
  2169 		ret = testPipe1.Open(KBadName2,RPipe::EOpenToRead);
       
  2170 		test (ret == KErrBadName);
       
  2171 		
       
  2172 		ret = testPipe1.Open(cPipeName,RPipe::EOpenToRead);
       
  2173 		test (ret == KErrNone);
       
  2174 		
       
  2175 		ret = testPipe1.Open(cPipeName,RPipe::EOpenToWrite);
       
  2176 		test (ret == KErrInUse);
       
  2177 		
       
  2178 		
       
  2179 		
       
  2180 	test.Next(_L("PIPE TEST: 2.5 Destroy Function test : Destroy opened pipe error  \n"));
       
  2181 		ret = RPipe::Destroy (cPipeName);
       
  2182 		test (ret == KErrInUse);
       
  2183 
       
  2184 	test.Next(_L("PIPE TEST: 2.6 Open Function test : Reopen pipe for reading\n"));
       
  2185 		ret = testPipe2.Open(cPipeName,RPipe::EOpenToRead);
       
  2186 		test (ret == KErrInUse);
       
  2187 
       
  2188 	test.Next(_L("PIPE TEST: 2.7 Open Function test : Bad name test  \n"));
       
  2189 		ret = testPipe2.Open(cPipeNameMaxPlusOne,RPipe::EOpenToRead);
       
  2190 		test (ret == KErrBadName);
       
  2191 			
       
  2192 
       
  2193 	test.Next(_L("PIPE TEST: 2.8 Open Function test : Write mode test\n"));
       
  2194 
       
  2195 		ret = testPipe3.Open(cPipeName,RPipe::EOpenToWrite);
       
  2196 		test (ret == KErrNone);
       
  2197 		
       
  2198 		ret = testPipe3.Open(cPipeName,RPipe::EOpenToRead);
       
  2199 		test (ret == KErrInUse);
       
  2200 
       
  2201 	test.Next(_L("PIPE TEST: 2.9 Open Function test : Bad name test  \n"));
       
  2202 	
       
  2203 		ret = testPipe4.Open(cPipeNameMaxPlusOne,RPipe::EOpenToWrite);
       
  2204 		test (ret == KErrBadName);
       
  2205 	
       
  2206 
       
  2207 	test.Next(_L("PIPE TEST: 2.10 Open Function test : Reopen for writing  \n"));
       
  2208 		ret = testPipe4.Open(cPipeName,RPipe::EOpenToWrite);
       
  2209 		test (ret == KErrInUse);
       
  2210 		
       
  2211 
       
  2212 	// Do we have pipes created ? Close and Destroy them ....!!
       
  2213 
       
  2214 	testPipe1.Close();
       
  2215 	testPipe2.Close();
       
  2216 	testPipe3.Close();
       
  2217 	testPipe4.Close();
       
  2218 	ret = RPipe::Destroy (cPipeName);
       
  2219 	test_KErrNone(ret);
       
  2220 
       
  2221 
       
  2222 
       
  2223 	test.Next(_L("PIPE TEST: 2.11a Open Function test : Write But Fail on no Readers mode before pipe defined\n"));
       
  2224 	ret = testPipe1.Open(cPipeName,RPipe::EOpenToWriteNamedPipeButFailOnNoReaders);
       
  2225 	test_Equal(KErrNotFound,ret); 
       
  2226 
       
  2227 	test.Next(_L("PIPE TEST: 2.11 Open Function test : Write But Fail on no Readers mode Error test\n"));
       
  2228 		ret = RPipe::Define(cPipeName , aSize);
       
  2229 		test (ret == KErrNone);
       
  2230 
       
  2231 		ret = testPipe1.Open(cPipeName,RPipe::EOpenToWriteNamedPipeButFailOnNoReaders);
       
  2232 		test (ret == KErrNotReady); 
       
  2233 		
       
  2234 
       
  2235 	test.Next(_L("PIPE TEST: 2.12 Open Function test : Write But Fail on no Readers mode Success test\n"));
       
  2236 		ret = testPipe1.Open(cPipeName,RPipe::EOpenToRead);
       
  2237 		test_KErrNone(ret);
       
  2238 		ret = testPipe2.Open(cPipeName,RPipe::EOpenToWriteNamedPipeButFailOnNoReaders);
       
  2239 		test ( ret == KErrNone);
       
  2240 		
       
  2241 
       
  2242 	// Do we have pipes created ? Close and Destroy them ....!!
       
  2243 
       
  2244 	testPipe1.Close();
       
  2245 	testPipe2.Close();
       
  2246 	testPipe3.Close();
       
  2247 	testPipe4.Close();
       
  2248 	ret = RPipe::Destroy (cPipeName);
       
  2249 	test_KErrNone(ret);
       
  2250 
       
  2251 
       
  2252 	
       
  2253 	test.Next(_L("	2.13 Define Function test : Check Incorrect Size\n"));
       
  2254 		aSize = -1;
       
  2255 		ret = RPipe::Define(cPipeName , aSize);
       
  2256 		test (ret == KErrArgument); 
       
  2257 	
       
  2258 	
       
  2259 	test.Next(_L("PIPE TEST: 2.14 Define Function test : Check Incorrect Size\n"));
       
  2260 		aSize = 0x1fffffff;
       
  2261 		ret = RPipe::Define(cPipeName , aSize);
       
  2262 		test (ret == KErrNoMemory); 
       
  2263 	
       
  2264 
       
  2265 	test.Next(_L("PIPE TEST: 2.15 Size Function test : Size\n"));
       
  2266 		aSize = 10;
       
  2267 		ret = RPipe::Define(cPipeName , aSize);
       
  2268 		
       
  2269 		ret = testPipe5.MaxSize();
       
  2270 		test (ret == KErrBadHandle);
       
  2271 
       
  2272 
       
  2273 	test.Next(_L("PIPE TEST: 2.16 Size Function test : Size\n"));
       
  2274 		aSize = 10;
       
  2275 		ret = RPipe::Define(cPipeName , aSize);
       
  2276 		testPipe5.Open(cPipeName, RPipe::EOpenToRead);
       
  2277 		testPipe4.Open(cPipeName, RPipe::EOpenToWrite);
       
  2278 		ret = testPipe5.MaxSize();
       
  2279 		test (ret == aSize);
       
  2280 		ret = testPipe4.MaxSize();
       
  2281 		test (ret == aSize);
       
  2282 		
       
  2283 
       
  2284 	/* Close all the pipes and Destroy*/
       
  2285 	testPipe1.Close();
       
  2286 	testPipe2.Close();
       
  2287 	testPipe3.Close();
       
  2288 	testPipe4.Close();
       
  2289 	testPipe5.Close();
       
  2290 	ret = RPipe::Destroy (cPipeName);
       
  2291 	test_KErrNone(ret);
       
  2292 
       
  2293 	_LIT(KRedefinePipe , "REDEFINEPIPE");
       
  2294 
       
  2295 	test.Next(_L("PIPE TEST: 2.17 Check for Redefining same pipe name \n"));
       
  2296 		ret = RPipe::Define(KRedefinePipe , aSize);
       
  2297 		test_KErrNone(ret);
       
  2298 		ret = RPipe::Define(KRedefinePipe , aSize);
       
  2299 		test (ret == KErrAlreadyExists);
       
  2300 	ret = RPipe::Destroy (KRedefinePipe);
       
  2301 	test_KErrNone(ret);
       
  2302 		
       
  2303 	test.Next(_L("PIPE TEST: 2.18 Open Function test : Bad Pipe name\n"));
       
  2304 		aSize = 10;
       
  2305 		RPipe testPipe6;
       
  2306 		ret = testPipe6.Open(cPipeName, RPipe::EOpenToRead);
       
  2307 		test (ret == KErrNotFound);
       
  2308 		
       
  2309 
       
  2310 	const TBufC<50> cPipeNameNull;
       
  2311 	test.Next(_L("PIPE TEST: 2.19 Define Function test : Null Pipe name and Bad Pipe name\n"));
       
  2312 		aSize = 10;
       
  2313 		ret = RPipe::Define(cPipeNameNull , aSize);
       
  2314 		test (ret == KErrBadName);
       
  2315 
       
  2316 
       
  2317 	ret = RPipe::Define(KBadName , aSize);
       
  2318 		test (ret == KErrBadName);
       
  2319 	ret = RPipe::Destroy(KBadName);
       
  2320 		test (ret == KErrBadName);
       
  2321 		
       
  2322 
       
  2323 	ret = RPipe::Define(KBadName2 , aSize);
       
  2324 		test (ret == KErrBadName);
       
  2325 	ret = RPipe::Destroy(KBadName2);
       
  2326 		test (ret == KErrBadName);
       
  2327 		
       
  2328 		
       
  2329 	test.Next(_L("PIPE TEST: 2.20 Destroy a pipe while Write end is open\n"));
       
  2330 	ret = RPipe::Define(cPipeName , aSize);
       
  2331 
       
  2332 	testPipe1.Close();
       
  2333 	ret = testPipe1.Open(cPipeName,RPipe::EOpenToRead);
       
  2334 	test (ret == KErrNone);
       
  2335 	testPipe2.Close();
       
  2336 	ret = testPipe2.Open(cPipeName,RPipe::EOpenToWrite);
       
  2337 	test (ret == KErrNone);
       
  2338 	testPipe1.Close();
       
  2339 	ret = RPipe::Destroy (cPipeName);
       
  2340 	test (ret == KErrInUse);
       
  2341 	
       
  2342 	testPipe2.Close();
       
  2343 	ret = RPipe::Destroy (cPipeName);
       
  2344 	test (ret == KErrNone);
       
  2345 	
       
  2346 	testPipe1.Close();
       
  2347 	testPipe2.Close();
       
  2348 	testPipe3.Close();
       
  2349 	testPipe4.Close();
       
  2350 	testPipe5.Close();
       
  2351 	
       
  2352 	
       
  2353 	_LIT(KPipe1Name,"TestPipeA");
       
  2354 	_LIT(KPipe2Name,"TestPipeB");
       
  2355 	_LIT(KPipe3Name,"TestPipeC");
       
  2356 	_LIT(KPipe4Name,"TestPipeD");
       
  2357 	
       
  2358 	test.Next(_L("PIPE TEST: 2.21 Define Four pipes and Destroy in different sequence ( Code Coverage test)\n"));
       
  2359 	ret = RPipe::Define(KPipe1Name , aSize);
       
  2360 	test (ret == KErrNone);
       
  2361 	ret = RPipe::Define(KPipe2Name , aSize);
       
  2362 	test (ret == KErrNone);
       
  2363 	ret = RPipe::Define(KPipe3Name , aSize);
       
  2364 	test (ret == KErrNone);
       
  2365 	ret = RPipe::Define(KPipe4Name , aSize);
       
  2366 	test (ret == KErrNone);
       
  2367 	ret = RPipe::Destroy (KPipe2Name);
       
  2368 	test (ret == KErrNone);
       
  2369 	ret = RPipe::Destroy (KPipe4Name);
       
  2370 	test (ret == KErrNone);
       
  2371 	ret = RPipe::Destroy (KPipe1Name);
       
  2372 	test (ret == KErrNone);
       
  2373 	ret = RPipe::Destroy (KPipe3Name);
       
  2374 	test (ret == KErrNone);
       
  2375 	
       
  2376 	return;
       
  2377 
       
  2378 } // End TestSingleThreadNamedPipes()
       
  2379 
       
  2380 /****************************************************************************
       
  2381 	This is a function to test UnNamed pipes in Single threaded environment.
       
  2382 	Check the functionality of following library functions
       
  2383 		- Create ()
       
  2384 		-
       
  2385 
       
  2386 
       
  2387 ******************************************************************************/
       
  2388 
       
  2389 _LIT8(KTxtDataToSend,"*Test data to send**");
       
  2390 
       
  2391 void TestSingleThreadUnNamedPipes() {
       
  2392 
       
  2393 	TInt 				ret = 1; 				// Return value test variable.
       
  2394 	TInt 				aSize;
       
  2395 	TBufC8<40>			wData(KTxtDataToSend); 	// Descriptor to hold data for Writing.
       
  2396 	RPipe				aReader,aWriter;
       
  2397 	RPipe				aReader2,aWriter2;
       
  2398 
       
  2399 
       
  2400 	// Following tests will verify all the APIs in single thread
       
  2401 	// for all the possible return values. 
       
  2402 	// This is to verify different paths of API
       
  2403 	// not for detail functional verification.
       
  2404 	// Create the Pipe and Check for No Errors when Valid parameters are passed.
       
  2405 	
       
  2406 	test.Next(_L("PIPE TEST:1.1 Create Function test : Check for No Error\n"));
       
  2407 		aSize = 10;
       
  2408 
       
  2409 		ret = RPipe::Create(	aSize,
       
  2410 								aReader,
       
  2411 								aWriter,
       
  2412 								EOwnerProcess,//TOwnerType aTypeW
       
  2413 								EOwnerProcess //TOwnerType aTypeW
       
  2414 							 );
       
  2415 
       
  2416 		test (ret == KErrNone);
       
  2417 		
       
  2418 		ret = RPipe::Create(	aSize,
       
  2419 								aReader,
       
  2420 								aWriter,
       
  2421 								EOwnerProcess,//TOwnerType aTypeW
       
  2422 								EOwnerProcess //TOwnerType aTypeW
       
  2423 							 );
       
  2424 
       
  2425 		test (ret == KErrInUse);
       
  2426 				
       
  2427 		aWriter.Close();
       
  2428 		aReader.Close();
       
  2429 	
       
  2430 
       
  2431 	// How big pipe we can create ?
       
  2432 
       
  2433 	test.Next(_L("PIPE TEST:1.2 Create Function test : Check for No Memory\n"));
       
  2434 	
       
  2435 		aSize = 0x1BCDEFFF;
       
  2436 
       
  2437 		ret = RPipe::Create(	aSize,
       
  2438 								aReader2,
       
  2439 								aWriter2,
       
  2440 								EOwnerProcess,//TOwnerType aTypeW
       
  2441 								EOwnerProcess //TOwnerType aTypeW
       
  2442 							 );
       
  2443 
       
  2444 		test (ret == KErrNoMemory);
       
  2445 
       
  2446 		aReader2.Close();
       
  2447 		aWriter2.Close();
       
  2448 
       
  2449 	
       
  2450 	test.Next(_L("PIPE TEST:1.3 Create Function test : Check for Reopening pipe\n"));
       
  2451 		aSize = 10;
       
  2452 
       
  2453 		ret = RPipe::Create(	aSize,
       
  2454 								aReader,
       
  2455 								aWriter,
       
  2456 								EOwnerProcess,//TOwnerType aTypeW
       
  2457 								EOwnerProcess//TOwnerType aTypeW
       
  2458 							 );
       
  2459 		test_KErrNone(ret);	
       
  2460 
       
  2461 		
       
  2462 		ret = RPipe::Create(	aSize,
       
  2463 								aReader,
       
  2464 								aWriter,
       
  2465 								EOwnerProcess,//TOwnerType aTypeW
       
  2466 								EOwnerProcess//TOwnerType aTypeW
       
  2467 							 );
       
  2468 
       
  2469 		test (ret == KErrInUse);
       
  2470 		
       
  2471 		aReader.Close();
       
  2472 		aWriter.Close();
       
  2473 		
       
  2474 
       
  2475 
       
  2476 	test.Next(_L("PIPE TEST:1.4 Read/Write Function test : Check for Writing to pipe\n"));
       
  2477 
       
  2478 		aSize = 100;
       
  2479 		ret = RPipe::Create(	aSize,
       
  2480 								aReader,
       
  2481 								aWriter,
       
  2482 								EOwnerProcess,//TOwnerType aTypeW
       
  2483 								EOwnerProcess//TOwnerType aTypeW
       
  2484 							 );
       
  2485 		test_KErrNone(ret);	
       
  2486 
       
  2487 		ret=aWriter.Write(wData , wData.Size());
       
  2488 		test (ret == wData.Size() ); 
       
  2489 		
       
  2490 		ret=aWriter.Write(wData , -1);
       
  2491 		test (ret == KErrArgument ); 
       
  2492 		
       
  2493 		
       
  2494 	test.Next(_L("PIPE TEST:1.5 Read/Write Function test : Check for Reading from pipe\n"));
       
  2495 
       
  2496 
       
  2497 		TBuf8<100> rData ;// Descriptor for reading data from Pipe.
       
  2498 		
       
  2499 		ret=aReader.Read(rData,wData.Size()); // Length of the data to be read from Pipe
       
  2500 		test (ret == wData.Size());// Length of the data read from the Pipe
       
  2501 
       
  2502 
       
  2503 	test.Next(_L("PIPE TEST:1.6 Read/Write Function test : Validate data received\n"));
       
  2504 
       
  2505 		test (KErrNone == rData.Compare(wData));
       
  2506 
       
  2507 
       
  2508 	test.Next(_L("PIPE TEST:1.7 Read/Write Function test : Check for Reading from pipe\n"));
       
  2509 
       
  2510 		ret=aReader.Read(rData,1);
       
  2511 		test (ret == 0);
       
  2512 		{
       
  2513 			
       
  2514 		
       
  2515 		RPipe		aReaderT, aWriterT;
       
  2516 		aSize = 20;
       
  2517 		ret = RPipe::Create(	aSize,
       
  2518 								aReaderT,
       
  2519 								aWriterT,
       
  2520 								EOwnerProcess,//TOwnerType aTypeW
       
  2521 								EOwnerProcess//TOwnerType aTypeW
       
  2522 							 );
       
  2523 		test_KErrNone(ret);	
       
  2524 							 
       
  2525 		ret=aWriterT.Write(wData ,15);
       
  2526 		test_Equal(15, ret);
       
  2527 		ret = aReaderT.Read(rData,10);
       
  2528 		test_Equal(10, ret);
       
  2529 		ret=aWriterT.Write(wData ,10);
       
  2530 		test_Equal(10, ret);
       
  2531 		ret = aReaderT.Read(rData,20);
       
  2532 		test (ret ==15);
       
  2533 		ret = aReaderT.Read(rData,5);
       
  2534 		test_Equal(0, ret);
       
  2535 		ret = aReaderT.Read(rData,25);
       
  2536 		test_Equal(0, ret);
       
  2537 
       
  2538 		aReaderT.Close();
       
  2539 		aWriterT.Close();
       
  2540 				
       
  2541 		}
       
  2542 		
       
  2543 	
       
  2544 	test.Next(_L("PIPE TEST:1.8 Read/Write Function test : Check for Wrong RPipe Handle\n"));
       
  2545 
       
  2546 
       
  2547 		ret=aWriter.Read(rData,15 );// Length of the data to be read from Pipe
       
  2548 		test (ret == KErrAccessDenied );
       
  2549 
       
  2550 	test.Next(_L("PIPE TEST:1.9 Read/Write Function test : Check for Wrong RPipe Handle\n"));
       
  2551 
       
  2552 		ret=aReader.Write(rData,rData.Size());
       
  2553 		test (ret == KErrAccessDenied );
       
  2554 		
       
  2555 
       
  2556 	test.Next(_L("PIPE TEST:1.10 Read/Write Function test : Check for write overflow\n"));
       
  2557 
       
  2558 		ret=aWriter.Write(wData,wData.Size());
       
  2559 		ret=aWriter.Write(wData,wData.Size());
       
  2560 		ret=aWriter.Write(wData,wData.Size());
       
  2561 		ret=aWriter.Write(wData,wData.Size());
       
  2562 		ret=aWriter.Write(wData,wData.Size());
       
  2563 		ret=aWriter.Write(wData,wData.Size());
       
  2564 		test (ret == KErrOverflow );
       
  2565 		
       
  2566 		
       
  2567 
       
  2568 	test.Next(_L("PIPE TEST:1.11 MaxSize Function test : MaxSize Check \n"));
       
  2569 		aSize = 10;
       
  2570 		// Just to be on safer side , close pipes if any.
       
  2571 		aReader.Close();
       
  2572 		aWriter.Close();
       
  2573 
       
  2574 		ret = RPipe::Create(	aSize,
       
  2575 								aReader,
       
  2576 								aWriter,
       
  2577 								EOwnerProcess,//TOwnerType aTypeW
       
  2578 								EOwnerProcess//TOwnerType aTypeW
       
  2579 							 );
       
  2580 		test (ret == KErrNone); // This error condition is not defined yet.
       
  2581 		ret =aReader.MaxSize();
       
  2582 		test (ret == aSize);
       
  2583 		
       
  2584 
       
  2585 	test.Next(_L("PIPE TEST:1.12 Size Function test : Size Check \n"));
       
  2586 		rData.Zero();	
       
  2587 		
       
  2588 		ret = aReader.Size();
       
  2589 		test_Equal(0, ret);
       
  2590 			
       
  2591 		_LIT8(KSizeTestData1,"123456789");
       
  2592 
       
  2593 		ret = aWriter.Write(KSizeTestData1,9);
       
  2594 		test_Equal(9, ret);
       
  2595 		ret = aReader.Size();
       
  2596 		test_Equal(9, ret);
       
  2597 
       
  2598 		ret = aReader.Read(rData,1);
       
  2599 		test_Equal(1, ret);
       
  2600 		ret = rData.Compare(_L8("1"));
       
  2601 		test_KErrNone(ret);
       
  2602 		ret = aReader.Size();
       
  2603 		test_Equal(8, ret);
       
  2604 		
       
  2605 		_LIT8(KSizeTestData2,"ab");
       
  2606 		ret = aWriter.Write(KSizeTestData2,2);
       
  2607 		test_Equal(2, ret);
       
  2608 		ret = aReader.Size();
       
  2609 		test_Equal(10, ret);
       
  2610 		
       
  2611 		ret = aWriter.Write(KSizeTestData2,1);
       
  2612 		test_Equal(KErrOverflow, ret);
       
  2613 		ret = aReader.Size();
       
  2614 		test_Equal(10, ret);
       
  2615 		
       
  2616 		ret = aReader.Read(rData,9);
       
  2617 		test_Equal(9, ret);
       
  2618 		ret = rData.Compare(_L8("23456789a"));
       
  2619 		test_KErrNone(ret);
       
  2620 		ret = aReader.Size();
       
  2621 		test_Equal(1, ret);
       
  2622 		
       
  2623 		ret = aReader.Read(rData,1);
       
  2624 		test_Equal(1, ret);
       
  2625 		ret = rData.Compare(_L8("b"));
       
  2626 		test_KErrNone(ret);
       
  2627 		ret = aReader.Size();
       
  2628 		test_Equal(0, ret);
       
  2629 		
       
  2630 		ret = aWriter.Size();
       
  2631 		test_Equal(0, ret);
       
  2632 		RPipe WrongPipeHandle;
       
  2633 		
       
  2634 		ret = WrongPipeHandle.Size();
       
  2635 		test ( ret == KErrBadHandle);
       
  2636 
       
  2637 	test.Next(_L("PIPE TEST:1.13 Size Function test : Size Function call with Wrong handle \n"));
       
  2638 
       
  2639 	
       
  2640 
       
  2641 		ret = WrongPipeHandle.MaxSize();
       
  2642 		test (ret == KErrBadHandle);
       
  2643 	
       
  2644 	aReader.Close();
       
  2645 	aWriter.Close();
       
  2646 	aReader2.Close();
       
  2647 	aWriter2.Close();
       
  2648 		
       
  2649 	test.Next(_L("PIPE TEST:1.14 Read Function : KErrNotReady \n"));
       
  2650 		aSize = 10;
       
  2651 		ret = RPipe::Create(	aSize,
       
  2652 								aReader,
       
  2653 								aWriter,
       
  2654 								EOwnerProcess,//TOwnerType aTypeW
       
  2655 								EOwnerProcess//TOwnerType aTypeW
       
  2656 							 );
       
  2657 		test (ret == KErrNone); // This error condition is not defined yet.
       
  2658 		aWriter.Close();
       
  2659 		ret = aReader.Read(rData,aSize);
       
  2660 		test (ret == KErrNotReady);
       
  2661 		ret = aReader.Read(rData,110);
       
  2662 		test (ret == KErrArgument);
       
  2663 			
       
  2664 		
       
  2665 	test.Next(_L("PIPE TEST:1.15 Check Handle Type function \n"));	
       
  2666 		aReader.Close();
       
  2667 		aWriter.Close();
       
  2668 		aSize = 10;
       
  2669 		ret = RPipe::Create(	aSize,
       
  2670 								aReader,
       
  2671 								aWriter,
       
  2672 								EOwnerProcess,//TOwnerType aTypeW
       
  2673 								EOwnerProcess//TOwnerType aTypeW
       
  2674 							 );
       
  2675 		test_KErrNone(ret);	
       
  2676 							 
       
  2677 		ret = aReader.HandleType();
       
  2678 		test (ret == RPipe::EReadChannel);
       
  2679 		
       
  2680 		ret = aWriter.HandleType();
       
  2681 		test (ret == RPipe::EWriteChannel);
       
  2682 		
       
  2683 		
       
  2684 		aReader.Close();
       
  2685 		ret = aReader.HandleType();
       
  2686 		test (ret == KErrBadHandle);
       
  2687 		
       
  2688 	test.Next(_L("PIPE TEST:1.16 Write Function : KErrNotReady \n"));	
       
  2689 		aSize = 1;
       
  2690 		ret = aWriter.Write(wData,aSize);
       
  2691 		test (ret == KErrNotReady);
       
  2692 	
       
  2693 	test.Next(_L("PIPE TEST:1.17 Write Function : Write data more than size of Descriptor \n"));			
       
  2694 		ret = aWriter.Write(wData,110);
       
  2695 		test (ret == KErrArgument);
       
  2696 		
       
  2697 		
       
  2698 	test.Next(_L("PIPE TEST:1.18 Write Function : KErrCompletion \n"));
       
  2699 		aWriter.Close();
       
  2700 		ret = RPipe::Create(	aSize,
       
  2701 								aReader,
       
  2702 								aWriter,
       
  2703 								EOwnerProcess,//TOwnerType aTypeW
       
  2704 								EOwnerProcess//TOwnerType aTypeW
       
  2705 							 );
       
  2706 		test_KErrNone(ret);	
       
  2707 		ret = aWriter.Write(wData,wData.Size());
       
  2708 		test (ret == KErrOverflow);
       
  2709 	
       
  2710 	test.Next(_L("PIPE TEST:1.19 Create Function : KErrInUse \n"));
       
  2711 		aReader.Close();
       
  2712 		ret = RPipe::Create(	aSize,
       
  2713 								aReader,
       
  2714 								aWriter,
       
  2715 								EOwnerProcess,//TOwnerType aTypeW
       
  2716 								EOwnerProcess//TOwnerType aTypeW
       
  2717 							 );
       
  2718 		test (ret == KErrInUse);
       
  2719 		
       
  2720 		aWriter.Close();
       
  2721 		ret = RPipe::Create(	aSize,
       
  2722 								aReader,
       
  2723 								aWriter,
       
  2724 								EOwnerProcess,//TOwnerType aTypeW
       
  2725 								EOwnerProcess//TOwnerType aTypeW
       
  2726 							 );
       
  2727 		test (ret == KErrNone);
       
  2728 		aWriter.Close();
       
  2729 		ret = RPipe::Create(	aSize,
       
  2730 								aReader,
       
  2731 								aWriter,
       
  2732 								EOwnerProcess,//TOwnerType aTypeW
       
  2733 								EOwnerProcess//TOwnerType aTypeW
       
  2734 							 );
       
  2735 		test (ret == KErrInUse);
       
  2736 		
       
  2737 	
       
  2738 		
       
  2739 	test.Next(_L("PIPE TEST:1.20 Read / Write using un opened handles \n"));	
       
  2740 		RPipe		aReaderT,aWriterT;
       
  2741 		
       
  2742 		ret = aWriterT.Write(wData,wData.Size());
       
  2743 		test (ret == KErrBadHandle);
       
  2744 		ret = aReaderT.Read(rData,aSize);
       
  2745 		test (ret == KErrBadHandle);
       
  2746 		
       
  2747 			
       
  2748 		
       
  2749 
       
  2750 	// Close all the pipes and return the resources.
       
  2751 		aReader.Close();
       
  2752 		aWriter.Close();
       
  2753 		aReader2.Close();
       
  2754 		aWriter2.Close();
       
  2755 	return;
       
  2756 
       
  2757 } // End TestSingleThreadUnNamedPipes()
       
  2758 
       
  2759 /****************************************************************************
       
  2760 	This is a function to test UnNamed pipes and named pipes in Single 
       
  2761 	threaded environment.
       
  2762 	This test ensures that the memory is actually being released when:
       
  2763 	RPipe::Close() is called for unnamed pipes and
       
  2764 	RPipe::Destroy() is called for named pipes.
       
  2765 ******************************************************************************/
       
  2766 void TestCreateClosePipe()
       
  2767 	{
       
  2768 	
       
  2769 	//unnamed pipes
       
  2770 	RPipe readHandle, writeHandle;
       
  2771 	const TInt K3MB = 1024*1024*3;
       
  2772 	// The loop is run ten times to ensure that if memory allocated while pipe
       
  2773 	// creation is not deallocated by close,then creation of pipe will fail with
       
  2774 	// KErrNoMemory in the sixth iteration. Default heap size is assumed.
       
  2775 	TInt i;
       
  2776 	test.Next(_L("PIPE TEST:11.1 Create Function test in a loop : Check for No Error\n"));
       
  2777 	for(i=1; i<10; i++) 
       
  2778     	{
       
  2779         TInt r = RPipe::Create(K3MB, readHandle, writeHandle,EOwnerProcess, EOwnerProcess);
       
  2780         test(KErrNone == r);
       
  2781         readHandle.Close(); 
       
  2782         writeHandle.Close();
       
  2783         }
       
  2784      
       
  2785     //named pipes
       
  2786     _LIT(KPipeName, "testPipe");
       
  2787     // The loop is run ten times to ensure that if memory allocated while pipe
       
  2788 	// creation is not deallocated by destroy,then creation of pipe will fail with
       
  2789 	// KErrNoMemory in the sixth iteration. Default heap size is assumed.
       
  2790 	test.Next(_L("PIPE TEST:11.2 Define Function test in a loop : Check for No Error\n"));
       
  2791     for(i=1; i<10; i++) 
       
  2792    	 	{
       
  2793     	TInt r = RPipe::Define(KPipeName,K3MB);
       
  2794     	test(KErrNone == r);
       
  2795     	r = RPipe::Destroy(KPipeName);
       
  2796     	test(KErrNone == r);
       
  2797     	}
       
  2798     
       
  2799     }// End TestCreateClosePipe()
       
  2800 
       
  2801 
       
  2802 struct TStressArgs
       
  2803 	{
       
  2804 	TStressArgs(TInt aIter, TInt aSize)
       
  2805 		:iIter(aIter), iSize(aSize)
       
  2806 		{}
       
  2807 
       
  2808 	const TInt iIter;
       
  2809 	const TInt iSize;
       
  2810 	};
       
  2811 
       
  2812 struct TDefDesArgs: public TStressArgs
       
  2813 	{
       
  2814 	TDefDesArgs(TInt aIter, TInt aSize, const TDesC& aName)
       
  2815 		:TStressArgs(aIter,aSize), iName(aName)
       
  2816 		{}
       
  2817 	const TDesC& iName;
       
  2818 	};
       
  2819 
       
  2820 /**
       
  2821 Repeatedly define and destroy named pipe
       
  2822 */
       
  2823 TInt DefineDestroy(TAny* aArgs)
       
  2824 	{
       
  2825 	const TDefDesArgs args = *static_cast<TDefDesArgs*>(aArgs);
       
  2826 	
       
  2827 	for(TInt i=0; i<args.iIter; i++)
       
  2828 		{
       
  2829 		TInt r = RPipe::Define(args.iName, args.iSize);
       
  2830 		if(r!=KErrNone && r!=KErrAlreadyExists)
       
  2831 			{
       
  2832 			return r;
       
  2833 			}
       
  2834 		r = RPipe::Destroy(args.iName);
       
  2835 		if(r!=KErrNone && r!=KErrInUse && r!=KErrNotFound)
       
  2836 			{
       
  2837 			return r;
       
  2838 			}
       
  2839 		}
       
  2840 	return KErrNone;
       
  2841 	}
       
  2842 
       
  2843 
       
  2844 /**
       
  2845 The parent thread will try to repeatedly open and close a named pipe
       
  2846 which is being repeatedly created and destroyed by the child thread.
       
  2847 This attempts to catch race conditions.
       
  2848 */
       
  2849 void TestRapidDefineDestroy()
       
  2850 	{
       
  2851 	const TInt iterations=1000;
       
  2852 	TDefDesArgs args(iterations, 16, KPipe1Name);
       
  2853 
       
  2854 	RPipe pipe;
       
  2855 
       
  2856 	TTestThread thread(_L("DefineDestroy"), DefineDestroy, &args);
       
  2857 	
       
  2858 	TInt r=KErrNone;
       
  2859 
       
  2860 	for(TInt i=0; i<args.iIter; i++)
       
  2861 		{
       
  2862 		r = pipe.Open(args.iName, RPipe::EOpenToWrite);
       
  2863 		if(r!=KErrNone && r !=KErrNotFound)
       
  2864 			{
       
  2865 			test_KErrNone(r);
       
  2866 			}
       
  2867 
       
  2868 		pipe.Close();
       
  2869 		}
       
  2870 	r = thread.WaitForExitL();
       
  2871 	test_KErrNone(r);
       
  2872 	}
       
  2873 
       
  2874 
       
  2875 /**
       
  2876 Write the descriptor specified in to the pipe repeating
       
  2877 as many times as specified by TData::iIterations
       
  2878 */
       
  2879 TInt WriteThread(TAny* aData)
       
  2880 	{
       
  2881 	TData& data = *static_cast<TData*>(aData);
       
  2882 	
       
  2883 	const TInt iter = data.iIterations;
       
  2884 
       
  2885 	TInt write=0;
       
  2886 	for(TInt i=0; i<iter; i++)
       
  2887 		{
       
  2888 		write = data.iWriteEnd->WriteBlocking(*data.iPipeData, data.iPipeData->Size());
       
  2889 		if(write <KErrNone)
       
  2890 			{
       
  2891 			return write;
       
  2892 			}
       
  2893 		}
       
  2894 
       
  2895 	return write*iter;
       
  2896 	}
       
  2897 
       
  2898 /**
       
  2899 Fill descriptor with random bytes
       
  2900 */
       
  2901 void FillRandom(TDes8& aDes)
       
  2902 	{
       
  2903 	aDes.Zero();
       
  2904 
       
  2905 	while(aDes.Size()+4<=aDes.MaxSize())
       
  2906 		{
       
  2907 		TUint8 rand[4];
       
  2908 		*(TUint32*)rand = Math::Random();
       
  2909 
       
  2910 		aDes.Append(rand, 4);
       
  2911 		} 
       
  2912 	}
       
  2913 
       
  2914 /**
       
  2915 @param aTotalBytes Bytes transfered in period
       
  2916 @param aTicks number of ticks elapsed in period
       
  2917 @return The rate of the transfer on kilobytes per second
       
  2918 */
       
  2919 TReal KiloBytesPerSecond(TInt aTotalBytes, TInt aTicks)
       
  2920 	{
       
  2921 	TInt period=0; //period of nanotick in microseconds
       
  2922 	TInt r = HAL::Get(HAL::ENanoTickPeriod, period);
       
  2923 	User::LeaveIfError(r);
       
  2924 
       
  2925 	//we use the definition that a kilobytes is 1000 bytes
       
  2926 	TReal result = (aTotalBytes/(aTicks*period/1000));
       
  2927 	return result;
       
  2928 	}
       
  2929 
       
  2930 
       
  2931 /**
       
  2932 Create a source data buffer of aTotal bytes and fill with random data.
       
  2933 Create a pipe and thread (WriteThread) to write the source buffer
       
  2934 into the pipe.
       
  2935 Read from the pipe into destination buffer
       
  2936 and optionally verify that buffers match
       
  2937 
       
  2938 @param aTotal Size of data buffer
       
  2939 @param aPipeData Size of pipe to create
       
  2940 @param aIter number of times to repeat transfer
       
  2941 @param aVerify Confirm that destination buffer matches source buffer
       
  2942 @param aPollRead read pipe by polling instead of using blocking read
       
  2943 @return Total number of ticks elapsed
       
  2944 */
       
  2945 TInt TestLoopBack(TInt aTotal, TInt aPipeSize, TInt aIter, TBool aVerify=ETrue, TBool aPollRead=EFalse)
       
  2946 	{
       
  2947 	const TInt bufferSize = aTotal;
       
  2948 
       
  2949 	RBuf8 sourceBuffer;
       
  2950 	sourceBuffer.CreateL(bufferSize);
       
  2951 	FillRandom(sourceBuffer);
       
  2952 	test_Equal(bufferSize,sourceBuffer.Size());
       
  2953 
       
  2954 	const TInt pipeSize=aPipeSize;
       
  2955 
       
  2956 	RPipe readEnd, writeEnd;
       
  2957 	TInt r = RPipe::Create(pipeSize, readEnd, writeEnd ,EOwnerProcess, EOwnerProcess);
       
  2958 	test_KErrNone(r);
       
  2959 
       
  2960 
       
  2961 	const TInt maxIter=aIter;
       
  2962 	TData data(NULL, &writeEnd, &sourceBuffer, maxIter);
       
  2963 
       
  2964 
       
  2965 	RBuf8 destBuffer;
       
  2966 	destBuffer.CreateL(bufferSize);
       
  2967 	
       
  2968 	RBuf8 tempBuffer;
       
  2969 	tempBuffer.CreateL(bufferSize);
       
  2970 
       
  2971 	
       
  2972 	TTestThread writer(_L("LoopBack"), WriteThread, &data);
       
  2973 	const TUint32 startTicks=User::NTickCount();
       
  2974 
       
  2975 	for(TInt iter=0; iter<maxIter; iter++)
       
  2976 		{
       
  2977 		TInt remainingData = bufferSize;
       
  2978 		do
       
  2979 			{
       
  2980 				const TInt toRead = Min(pipeSize,remainingData);
       
  2981 				if(aPollRead)
       
  2982 					{
       
  2983 					//an inefficient way to read a pipe!
       
  2984 					r = readEnd.Read(tempBuffer, toRead);
       
  2985 					test_NotNegative(r);
       
  2986 					}
       
  2987 				else
       
  2988 					{
       
  2989 					r = readEnd.ReadBlocking(tempBuffer, toRead );
       
  2990 					test_Equal(toRead, r);
       
  2991 					}
       
  2992 				destBuffer+=tempBuffer;
       
  2993 				tempBuffer.Zero();
       
  2994 				remainingData-=r;
       
  2995 			}
       
  2996 		while(remainingData);
       
  2997 
       
  2998 		if(aVerify)
       
  2999 			{
       
  3000 			r = sourceBuffer.Compare(destBuffer);
       
  3001 			test_KErrNone(r);
       
  3002 			}
       
  3003 
       
  3004 		destBuffer.Zero();
       
  3005 		}
       
  3006 	const TUint32 endTicks = User::NTickCount();
       
  3007 
       
  3008 	r = writer.WaitForExitL();
       
  3009 	test_Equal(bufferSize*maxIter, r);
       
  3010 
       
  3011 	const TUint32 ticksElapsed= endTicks - startTicks; 
       
  3012 
       
  3013 	sourceBuffer.Close();
       
  3014 	tempBuffer.Close();
       
  3015 	destBuffer.Close();
       
  3016 
       
  3017 	readEnd.Close();
       
  3018 	writeEnd.Close();
       
  3019 
       
  3020 	return ticksElapsed;
       
  3021 	}
       
  3022 
       
  3023 /**
       
  3024 Simple test to confirm that data is reproduced after being fed through a pipe
       
  3025 */
       
  3026 void TestTransferIntegrity()
       
  3027 	{
       
  3028 
       
  3029 	TestLoopBack(128*1024, 128, 1, ETrue);
       
  3030 	TestLoopBack(1024, 1, 1, ETrue);
       
  3031 
       
  3032 	//read by constantly polling
       
  3033 	TestLoopBack(128*1024, 1024, 1, ETrue, ETrue);
       
  3034 	}
       
  3035 
       
  3036 
       
  3037 /**
       
  3038 Enable Writeblocking and Readblocking notifications
       
  3039 without actual reads and writes
       
  3040 */
       
  3041 class RTestPipe: public RPipe
       
  3042 	{
       
  3043 public:
       
  3044 	void RequestWriteBlocking(TInt aNumByte, TRequestStatus& aStatus)
       
  3045 		{
       
  3046  		DoRequest(EWriteBlocking, aStatus, &aNumByte);
       
  3047 		}
       
  3048 		
       
  3049 	void RequestReadBlocking(TRequestStatus& aStatus)
       
  3050 		{
       
  3051 		DoRequest(EReadBlocking, aStatus);
       
  3052 		}
       
  3053 	};
       
  3054 
       
  3055 
       
  3056 /**
       
  3057 A test which will request some type of notification
       
  3058 */
       
  3059 struct CNotificationTest : public TFunctor
       
  3060 	{
       
  3061 	CNotificationTest(RTestPipe& aPipe)
       
  3062 		:iPipe(aPipe)
       
  3063 		{
       
  3064 		TInt r = iParent.Open(iParent.Id());
       
  3065 		test_KErrNone(r);
       
  3066 		}
       
  3067 
       
  3068 	virtual ~CNotificationTest()
       
  3069 		{
       
  3070 		}
       
  3071 
       
  3072 	void operator()()
       
  3073 		{
       
  3074 		RunTest();
       
  3075 
       
  3076 		//set up rendezvous with parent
       
  3077 		iParent.Rendezvous(iRendezvousStatus);
       
  3078 
       
  3079 		//announce we have run test
       
  3080 		RThread::Rendezvous(KErrNone);
       
  3081 		
       
  3082 		//wait untill parent has reached rendezvous
       
  3083 		User::WaitForRequest(iRendezvousStatus);
       
  3084 		}
       
  3085 
       
  3086 	virtual CNotificationTest* Clone()=0;
       
  3087 
       
  3088 	/**
       
  3089 	If necessary, gets pipe into correct state for the start of test
       
  3090 	*/
       
  3091 	virtual void PreparePipe() =0;
       
  3092 	virtual void RunTest() =0;
       
  3093 
       
  3094 	/**
       
  3095 	Cancel the notification
       
  3096 	*/
       
  3097 	virtual void Cancel() =0;
       
  3098 
       
  3099 	virtual TInt GetReturn()
       
  3100 		{
       
  3101 		return iStatus.Int();
       
  3102 		}
       
  3103 
       
  3104 	RTestPipe& iPipe;
       
  3105 	TRequestStatus iStatus;
       
  3106 
       
  3107 	TRequestStatus iRendezvousStatus;
       
  3108 	RThread iParent;
       
  3109 	};
       
  3110 
       
  3111 
       
  3112 
       
  3113 /**
       
  3114 Will request free space notification
       
  3115 */
       
  3116 struct CSpaceNotificationTest : public CNotificationTest
       
  3117 	{
       
  3118 	typedef void (RTestPipe::*TSpaceNotification) (TInt, TRequestStatus&);
       
  3119 
       
  3120 	/**
       
  3121 	@param aPipe Pipe handle to use
       
  3122 	@param TSpaceNotification A pointer for the method to test
       
  3123 	@param aNumBytes Amount of space to request
       
  3124 	*/
       
  3125 	CSpaceNotificationTest(RTestPipe& aPipe, TSpaceNotification aFunc, TInt aNumBytes)
       
  3126 		:CNotificationTest(aPipe), iFn(aFunc), iNumBytes(aNumBytes)
       
  3127 		{}
       
  3128 	
       
  3129 	CNotificationTest* Clone()
       
  3130 		{
       
  3131 		return new CSpaceNotificationTest(*this);
       
  3132 		}
       
  3133 
       
  3134 	void RunTest()
       
  3135 		{
       
  3136 		(iPipe.*iFn)(iNumBytes, iStatus);
       
  3137 		}
       
  3138 
       
  3139 	//Make sure space notification won't complete immediately
       
  3140 	void PreparePipe()
       
  3141 		{
       
  3142 		TInt freeSpace = iPipe.MaxSize() - iPipe.Size();
       
  3143 		if(freeSpace >= iNumBytes)
       
  3144 			{
       
  3145 			TInt r = iPipe.Write(KTestData, freeSpace);
       
  3146 			test_Equal(freeSpace, r);
       
  3147 			}
       
  3148 		}
       
  3149 
       
  3150 	void Cancel()
       
  3151 		{
       
  3152 		iPipe.CancelSpaceAvailable();
       
  3153 		};
       
  3154 
       
  3155 	TSpaceNotification iFn; 
       
  3156 	TInt iNumBytes;
       
  3157 	};
       
  3158 
       
  3159 struct CDataNotificationTest : public CNotificationTest
       
  3160 	{
       
  3161 	typedef void (RTestPipe::*TDataNotification) (TRequestStatus&);
       
  3162 
       
  3163 	CDataNotificationTest(RTestPipe& aPipe, TDataNotification aFunc)
       
  3164 		:CNotificationTest(aPipe), iFn(aFunc)
       
  3165 		{}
       
  3166 	
       
  3167 	CNotificationTest* Clone()
       
  3168 		{
       
  3169 		return new CDataNotificationTest(*this);
       
  3170 		}
       
  3171 
       
  3172 	void RunTest()
       
  3173 		{
       
  3174 		(iPipe.*iFn)(iStatus);
       
  3175 		}
       
  3176 
       
  3177 	//make sure we start with an empty pipe
       
  3178 	void PreparePipe()
       
  3179 		{
       
  3180 		iPipe.Flush();
       
  3181 		}
       
  3182 
       
  3183 	void Cancel()
       
  3184 		{
       
  3185 		iPipe.CancelDataAvailable();
       
  3186 		};
       
  3187 
       
  3188 	TDataNotification iFn; 
       
  3189 	};
       
  3190 
       
  3191 
       
  3192 void ProcessNotificationTests(RPointerArray<CNotificationTest>& aArray)
       
  3193 	{
       
  3194 	const TInt count = aArray.Count();
       
  3195 	for(TInt i=0; i < count; i++)
       
  3196 		{
       
  3197 		for(TInt j=0; j < count; j++)
       
  3198 			{
       
  3199 			//need copies as objects contain request states
       
  3200 			CNotificationTest* testA = aArray[i]->Clone();
       
  3201 			test_NotNull(testA);
       
  3202 			CNotificationTest* testB = aArray[j]->Clone();
       
  3203 			test_NotNull(testB);
       
  3204 
       
  3205 			testA->PreparePipe(); testB->PreparePipe();
       
  3206 
       
  3207 			TTestThread a(_L("CNotificationTestA"), *testA, EFalse);
       
  3208 			TTestThread b(_L("CNotificationTestB"), *testB, EFalse);
       
  3209 
       
  3210 			TRequestStatus rendezvousA, rendezvousB;
       
  3211 			a.Rendezvous(rendezvousA);
       
  3212 			b.Rendezvous(rendezvousB);
       
  3213 
       
  3214 			a.Resume();	b.Resume();
       
  3215 
       
  3216 			//wait till after threads have made notification request.
       
  3217 			User::WaitForRequest(rendezvousA);
       
  3218 			User::WaitForRequest(rendezvousB);
       
  3219 
       
  3220 			TInt retA = testA->GetReturn(); TInt retB = testB->GetReturn();
       
  3221 
       
  3222 			//announce that we have read status requests, allowing
       
  3223 			//child threads to terminate
       
  3224 			RThread::Rendezvous(KErrNone);
       
  3225 
       
  3226 			a.WaitForExitL();
       
  3227 			b.WaitForExitL();
       
  3228 
       
  3229 			TBool oneRequestSupported = ((retA == KRequestPending) && (retB == KErrInUse))
       
  3230 										|| ((retB == KRequestPending) && (retA == KErrInUse));
       
  3231 			TBool bothSupported = (retA == KRequestPending) && (retB == KRequestPending);
       
  3232 
       
  3233 			if(!(oneRequestSupported || bothSupported))
       
  3234 				{
       
  3235 				test.Printf(_L("Failure: i=%d, j=%d"), i, j);
       
  3236 				test(EFalse);
       
  3237 				}
       
  3238 
       
  3239 			testA->Cancel(); testB->Cancel();
       
  3240 			delete testA;
       
  3241 			delete testB;
       
  3242 			}
       
  3243 		}
       
  3244 	}
       
  3245 
       
  3246 /**
       
  3247 Test abillity of pipe channels to handle multiple notification requests
       
  3248 simultaneously.
       
  3249 */
       
  3250 void TestNotifications()
       
  3251 	{
       
  3252 	RTestPipe readEnd, writeEnd;
       
  3253 
       
  3254 	TInt pipeSize = 5;
       
  3255 	TInt r = RPipe::Create(pipeSize, readEnd, writeEnd, EOwnerProcess, EOwnerProcess);
       
  3256 	test_KErrNone(r);
       
  3257 
       
  3258 	test.Next(_L("Test write end requests"));
       
  3259 	CSpaceNotificationTest writeBlocking(writeEnd, &RTestPipe::RequestWriteBlocking, pipeSize);
       
  3260 	CSpaceNotificationTest spaceAvailable(writeEnd, &RTestPipe::NotifySpaceAvailable, pipeSize);
       
  3261 	RPointerArray<CNotificationTest> writeEndTests;
       
  3262 	
       
  3263 	writeEndTests.AppendL(&writeBlocking);
       
  3264 	writeEndTests.AppendL(&spaceAvailable);
       
  3265 
       
  3266 	for(TInt i=0; i<10; i++)
       
  3267 		{
       
  3268 		ProcessNotificationTests(writeEndTests);
       
  3269 		}
       
  3270 	writeEndTests.Close();
       
  3271 
       
  3272 	test.Next(_L("Test read end requests"));
       
  3273 	CDataNotificationTest readBlocking(readEnd, &RTestPipe::RequestReadBlocking);
       
  3274 	CDataNotificationTest dataAvailable(readEnd, &RTestPipe::NotifyDataAvailable);
       
  3275 	RPointerArray<CNotificationTest> readEndTests;
       
  3276 
       
  3277 	readEndTests.AppendL(&readBlocking);
       
  3278 	readEndTests.AppendL(&dataAvailable);
       
  3279 
       
  3280 	for(TInt j=0; j<10; j++)
       
  3281 		{
       
  3282 		ProcessNotificationTests(readEndTests);
       
  3283 		}
       
  3284 
       
  3285 	readEndTests.Close();
       
  3286 
       
  3287 	readEnd.Close();
       
  3288 	writeEnd.Close();
       
  3289 	}
       
  3290 
       
  3291 LOCAL_C void RunTests(void)
       
  3292 	{
       
  3293 	//  Test UnNamed Pipes in Single Thread
       
  3294 	test.Next(_L("PIPE TEST: 1.Un Named pipes in Single Thread\n"));
       
  3295 	TestSingleThreadUnNamedPipes();
       
  3296 
       
  3297 	//	Test Named Pipes in Single Thread
       
  3298 	test.Next(_L("PIPE TEST: 2.Named pipes in Single Thread\n"));
       
  3299 	TestSingleThreadNamedPipes();
       
  3300 
       
  3301 	//	Test UnNamed Pipes in MultiThread
       
  3302 	test.Next(_L("PIPE TEST: 3.Un Named pipes in Multi Thread\n"));
       
  3303 	TestMultiThreadUnNamedPipes();
       
  3304 
       
  3305 	//	Test Named Pipes in MultiThread
       
  3306 	test.Next(_L("PIPE TEST: 4.Named pipes in Multi Thread\n"));
       
  3307 	TestMultiThreadNamedPipes();
       
  3308 
       
  3309 	//	Test Named Pipes in Multi Process
       
  3310 	test.Next(_L("PIPE TEST: 5.Named pipes in Multi Process\n"));
       
  3311 	TestMultiProcessNamedPipes();
       
  3312 
       
  3313 	//  Test Notify mechanism
       
  3314 	test.Next(_L("PIPE TEST: 6.Pipes Notify mechanism test\n"));
       
  3315 	TestNotifyMechanismPipes();
       
  3316 
       
  3317 	//	Test Wait Mechanism
       
  3318 	test.Next(_L("PIPE TEST: 7.Pipes Wait mechanism test\n"));
       
  3319 	TestWaitMechanismPipes();
       
  3320 
       
  3321 
       
  3322 	//  Test Pipes performance
       
  3323 	test.Next(_L("PIPE TEST: 8.Pipes Permission test\n"));
       
  3324 	TestPipesPermissionCheck ();
       
  3325 
       
  3326 	//	Misc Pipe tests
       
  3327 	test.Next(_L("PIPE TEST: 9.Misc Pipe tests\n"));
       
  3328 	TestMiscPipes();
       
  3329 	
       
  3330 	// Blocking and Notify method tests.
       
  3331 	test.Next(_L("PIPE TEST: 10.Blocking and Notification calls test\n"));
       
  3332 	TestBlockingAndNotify();
       
  3333 	
       
  3334 	//Creation and closure of a large number of Pipes
       
  3335 	test.Next(_L("PIPE TEST: 11. Creation and subsequent closure of a large number of pipes\n"));
       
  3336 	TestCreateClosePipe();
       
  3337 
       
  3338 	test.Next(_L("Test concurrent notification requests"));
       
  3339 	TestNotifications();
       
  3340 
       
  3341 	test.Next(_L("Repeatedly open a named pipe whilst it's being created and destroyed"));
       
  3342 	TestRapidDefineDestroy();
       
  3343 
       
  3344 	test.Next(_L("Check integrity of data after transfer\n"));
       
  3345 	TestTransferIntegrity();
       
  3346 
       
  3347 	test.Next(_L("PIPE TEST: Ending test.\n"));
       
  3348 	return;
       
  3349 	}
       
  3350 
       
  3351 TInt ParseCommandLine()
       
  3352 	{
       
  3353 	TBuf<20> cmdLine;
       
  3354 	//TInt r= cmdLine.Create(User::CommandLineLength());
       
  3355 	//test_KErrNone(r);
       
  3356 	User::CommandLine(cmdLine);
       
  3357 	TLex lex(cmdLine);
       
  3358 
       
  3359 	TPtrC token=lex.NextToken();
       
  3360 	if(token.Length()>0)
       
  3361 		{
       
  3362 		TInt os=token.Match(_L("-n*"));
       
  3363 		if(os==0)
       
  3364 			{
       
  3365 			if(token.Length()>2)
       
  3366 				lex.SkipAndMark(2-lex.TokenLength()); //set mark backwards to after the "-n"
       
  3367 			token.Set(lex.NextToken());
       
  3368 			if(token.Length()==0)
       
  3369 				return KErrArgument;
       
  3370 
       
  3371 			TLex valLex(token);
       
  3372 			TInt value;
       
  3373 			TInt r=valLex.Val(value);
       
  3374 			if(r<0)
       
  3375 				return r;
       
  3376 			else
       
  3377 				return value;
       
  3378 			}
       
  3379 		else
       
  3380 			{
       
  3381 			return KErrArgument;
       
  3382 			}
       
  3383 		}
       
  3384 	else
       
  3385 		{	
       
  3386 		const TInt KDefaultRuns=1;
       
  3387 		return KDefaultRuns;
       
  3388 		}
       
  3389 	}
       
  3390 
       
  3391 GLDEF_C TInt E32Main()
       
  3392 // Main entry point for the test.
       
  3393     {
       
  3394 	__UHEAP_MARK;
       
  3395 	TInt		ret = 0;
       
  3396 
       
  3397 	test.Start(_L("PIPE TEST: Testing"));
       
  3398 	ret = RPipe::Init();
       
  3399 	if ( ret != KErrNone && ret != KErrAlreadyExists) 
       
  3400 		{
       
  3401 		test.Printf(_L("Fail to load RPIPE driver %d\n"),ret);
       
  3402 		return KErrNone;
       
  3403 		}
       
  3404 	TName pddName(RPipe::Name());
       
  3405 	ret = RPipe::Define (KPipe1Name,10);
       
  3406 	test_KErrNone(ret);
       
  3407 	
       
  3408 	User::FreeLogicalDevice(pddName);
       
  3409 	ret = RPipe::Define (KPipe1Name,10);
       
  3410 	test_Equal(KErrNotFound, ret);
       
  3411 		
       
  3412 	ret = RPipe::Init();
       
  3413 	if ( ret != KErrNone && ret != KErrAlreadyExists) 
       
  3414 		{
       
  3415 		test.Printf(_L("Fail to load RPIPE driver %d\n"),ret);
       
  3416 		return KErrNone;
       
  3417 		}
       
  3418 		
       
  3419 
       
  3420 	TInt runs=ParseCommandLine();
       
  3421 	if(runs>=0)
       
  3422 		{
       
  3423 		TBool forever=(runs==0);
       
  3424 		for(TInt i=0; forever||(i<runs); ++i)
       
  3425 			{
       
  3426 			if(i!=0)
       
  3427 				test.Next(_L("Next iteration"));
       
  3428 
       
  3429 			test.Start(_L("Starting Run")); //sub nest each test iteration
       
  3430 			__UHEAP_MARK;
       
  3431 			TRAP(ret, RunTests());
       
  3432 			test_KErrNone(ret);
       
  3433 			__UHEAP_MARKEND;
       
  3434 			test.End();
       
  3435 			}
       
  3436 		}
       
  3437 	else
       
  3438 		{
       
  3439 		test.Printf(_L("Usage: t_pipe -n N\n N is number of runs to do. If N=0 run forever"));	
       
  3440 		}
       
  3441 	
       
  3442 	TName pddName2(RPipe::Name());
       
  3443 	ret= User::FreeLogicalDevice(pddName2);
       
  3444 	test_KErrNone(ret);
       
  3445 	test.End();
       
  3446 	test.Close();
       
  3447 	__UHEAP_MARKEND;
       
  3448 	return KErrNone;
       
  3449  }
       
  3450  
       
  3451