os/ossrv/genericopenlibs/cstdlib/TSTLIB/TPIPE3.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2005-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 "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 // Test code for pipes, using dubious WINS extension for multiple processes...
    15 // 
    16 //
    17 
    18 #include <stdlib.h>
    19 #include <stdio.h>
    20 #include <string.h>
    21 #include <unistd.h>	// for MAXPATHLEN 
    22 #include <sys/errno.h>
    23 #include <sys/ioctl.h>
    24 #include <e32debug.h> // for RDebug::Print
    25 
    26 #include <e32std.h>
    27 #include <estlib.h>	// for multi-threading control
    28 
    29 extern "C" {
    30 #include "CTEST.H"
    31 }
    32 
    33 #ifdef _DEBUG
    34 #define DebugPrint	RDebug::Print
    35 #else
    36 inline void DebugPrint(const TDesC&, ...) {}
    37 #endif
    38 
    39 // prepare buf for pipe write/read opereations
    40 void fillbuf(int seed, char* buf, int buflen)
    41 	{
    42 	int j;
    43 	sleep(seed/3);
    44 	seed += 'A';
    45 	for (j=0; j<buflen; j++)
    46 		{
    47 		buf[j]=(char)seed;
    48 		seed+=13;
    49 		if (seed > 127)
    50 			seed = seed - 127 + 'A';
    51 		}
    52 	}
    53 
    54 int fids[3];
    55 
    56 // Producer, writing buf to pipe(fid)
    57 void producer(int fid)
    58 	{
    59 
    60 	test_Data;
    61 	char buf[128];
    62 	int nbytes;
    63 
    64 	test_Title("Producer");
    65 	
    66 	fillbuf(1,buf,sizeof(buf));
    67 	fflush(stdout);
    68 	
    69 	// Pipe Write test
    70 	nbytes=write(fid, buf, sizeof(buf));
    71 	
    72 	fflush(stdout);
    73 	test(nbytes==sizeof(buf));
    74 	
    75 	TProcessId id=RProcess().Id();
    76 	TUint pid=*REINTERPRET_CAST(TUint*,&id);
    77 	DebugPrint(_L("Process %d: Pipe Write success"), pid);
    78 	
    79 	printf("1.\n\n");	
    80 	return;
    81 	}
    82 	
    83 #define select_test(fid)	\
    84 		mask=E32SELECT_READ+E32SELECT_WRITE; \
    85 		err=ioctl(fid,E32IOSELECT,(void*)&mask);
    86 
    87 // consumer, doing ioctl test and then read from pipe(fid)					
    88 void consumer(int fid)
    89 	{
    90 
    91 	test_Data;
    92 	char buf[256];
    93 	char checkbuf[256];
    94 	int nbytes;
    95 	int mask=E32SELECT_READ;
    96 	int err=0;
    97 
    98 	test_Title("Consumer");
    99 	fillbuf(1,checkbuf,128);
   100 	
   101 	test_Next("Simple read, exactly matching write\n");
   102 	
   103 	// Pipe Ioctl test
   104 	select_test(fid);
   105 
   106 	test(err==0);
   107 	test(mask==E32SELECT_READ);
   108 	
   109 	// Pipe Read test
   110 	nbytes=read(fid,buf,128);
   111 	
   112 	test(nbytes==128);
   113 	test(memcmp(buf,checkbuf,128)==0);
   114 
   115 	TProcessId id=RProcess().Id();
   116 	TUint pid=*REINTERPRET_CAST(TUint*,&id);
   117 	DebugPrint(_L("Process %d: Pipe Read success"), pid);
   118 	}
   119 
   120 /**
   121 @SYMTestCaseID          SYSLIB-STDLIB-UT-1572
   122 @SYMTestCaseDesc	    Tests for cancellation on pipe operations
   123 @SYMTestPriority 	    High
   124 @SYMTestActions  	    Cancel an outstanding pipe operation request 
   125 						and check if CPosixIPCSession::PipeCancel() handling correct.
   126 @SYMTestExpectedResults Test must not fail
   127 @SYMREQ                 REQ0000
   128 */
   129 void pipeCancel(int fid)
   130 	{
   131 	test_Data;
   132 	int err=0;
   133 	int mask=E32SELECT_READ+E32SELECT_WRITE; 
   134 	
   135 	test_Title("Pipe cancellation");
   136 	DebugPrint(_L("Pipe cancellation test"));
   137 	TRequestStatus aStatus;
   138 
   139 	// Issue an ansynchronous pipe operation request
   140 	err=ioctl(fid,E32IOSELECT,(void*)&mask, aStatus);
   141 	
   142 	// Cancel pipe ioctl
   143 	err=ioctl_cancel(fid);
   144 	test(err==0);
   145 	
   146 	TProcessId id=RProcess().Id();
   147 	TUint pid=*REINTERPRET_CAST(TUint*,&id);
   148 	DebugPrint(_L("Process %d: Pipe Cancellation SUCCESS"), pid);
   149 	
   150 	test_Close();
   151 	}
   152 
   153 void do_parent()
   154 	{
   155 	// testing pipe with writing to child stdin
   156 	producer(fids[0]);
   157 	}
   158 	
   159 void do_child()
   160 	{
   161 	// testing pipe with reading from stdin
   162 	consumer(0);
   163 
   164 	// pipe cancellation test
   165 	pipeCancel(0);
   166 	
   167 	// close pipe
   168 	close(0);	
   169 	}
   170 	
   171 
   172 // Linked with mcrt0.o, so that the exe starts the CPosixServer automatically as per the
   173 // plan all along.
   174 
   175 int main(int argc, char* argv[])
   176 	{
   177 	void* proc2;
   178 
   179 	start_redirection_server();
   180 
   181 	if (argc==1)
   182 		{
   183 		// create Child process with read/err pipes	
   184 		proc2 = create_process(do_child, "CHILD", "r", fids);
   185 		if (proc2)
   186 			start_process(proc2);
   187 		else
   188 			perror("Failed to start process CHILD: ");
   189 
   190 		if (proc2)
   191 			{
   192 			int exit;
   193 			
   194 			// parent process
   195 			do_parent();
   196 			exit=wait_for_process(proc2);
   197 			printf("wait_for_process() returned %d\r\n", exit);
   198 			}
   199 		}
   200 	else
   201 		{
   202 		// child process
   203 		do_child();
   204 		}
   205 
   206 	// exit here, for the moment crt0 libraries panic
   207 	exit(0);
   208 	
   209 	return KErrNone;
   210 	}
   211