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