os/ossrv/genericopenlibs/cstdlib/TSTLIB/TPIPE.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description:
sl@0
    15
* Test code for pipes, using dubious WINS extension for multiple processes...
sl@0
    16
* 
sl@0
    17
*
sl@0
    18
*/
sl@0
    19
sl@0
    20
sl@0
    21
sl@0
    22
#include <stdlib.h>
sl@0
    23
#include <stdio.h>
sl@0
    24
#include <string.h>
sl@0
    25
#include <unistd.h>	/* for MAXPATHLEN */
sl@0
    26
#include <sys/errno.h>
sl@0
    27
#include <sys/ioctl.h>
sl@0
    28
sl@0
    29
#include "CTEST.H"
sl@0
    30
sl@0
    31
int fids[3];
sl@0
    32
sl@0
    33
/**
sl@0
    34
@SYMTestCaseID          SYSLIB-STDLIB-CT-1074
sl@0
    35
@SYMTestCaseDesc	    Tests for operations on pipes 
sl@0
    36
@SYMTestPriority 	    High
sl@0
    37
@SYMTestActions  	    Tests for command line arguments,directory operations,environment variables.
sl@0
    38
@SYMTestExpectedResults Test must not fail
sl@0
    39
@SYMREQ                 REQ0000
sl@0
    40
*/		
sl@0
    41
int do_main(int argc, char* argv[])
sl@0
    42
	{
sl@0
    43
	test_Data;
sl@0
    44
	int i;
sl@0
    45
	char* var;
sl@0
    46
	char* varname;
sl@0
    47
	char cwd[MAXPATHLEN];
sl@0
    48
sl@0
    49
	test_Title("PIPE");
sl@0
    50
sl@0
    51
	test_Next("Command line arguments");
sl@0
    52
	test(argc>0);
sl@0
    53
	test(argv!=0);
sl@0
    54
	printf("  argc=%d\r\n", argc);
sl@0
    55
	for (i=0; i<argc; i++)
sl@0
    56
		{
sl@0
    57
		test(argv[i]!=0);
sl@0
    58
		printf("  argv[%d]=\"%s\" length %d\r\n", i, argv[i], strlen(argv[i]));
sl@0
    59
		}
sl@0
    60
	printf("\r\n");
sl@0
    61
sl@0
    62
	test_Next("Current working directory");
sl@0
    63
	var=getcwd(cwd,sizeof(cwd));
sl@0
    64
	test(var==cwd);
sl@0
    65
	printf("  %s\r\n\n", cwd);
sl@0
    66
sl@0
    67
	test_Next("Change directory");
sl@0
    68
	i=chdir("z:/system");
sl@0
    69
	test(i==0);
sl@0
    70
	var=getcwd(cwd,sizeof(cwd));
sl@0
    71
	test(var==cwd);
sl@0
    72
	printf("  %s\r\n\n", cwd);
sl@0
    73
sl@0
    74
	test_Next("Environment variables");
sl@0
    75
sl@0
    76
	varname="CLASSPATH";
sl@0
    77
	var=getenv(varname);
sl@0
    78
	test(var!=0);
sl@0
    79
	printf("  %s=%s\r\n", varname,var);
sl@0
    80
sl@0
    81
	varname="VARIABLE2";
sl@0
    82
	var=getenv(varname);
sl@0
    83
	if (var!=0)
sl@0
    84
		{
sl@0
    85
		printf("  %s=%s\r\n", varname,var);
sl@0
    86
		unsetenv("VARIABLE2");
sl@0
    87
		}
sl@0
    88
sl@0
    89
	varname="USER";
sl@0
    90
	var=getenv(varname);
sl@0
    91
	test(var!=0);
sl@0
    92
	printf("  %s=%s\r\n\n", varname,var);
sl@0
    93
sl@0
    94
	sleep(5);
sl@0
    95
sl@0
    96
	test_Close();
sl@0
    97
	return 0;
sl@0
    98
	}
sl@0
    99
sl@0
   100
/**
sl@0
   101
Bodge up two pretend processes 
sl@0
   102
sl@0
   103
@SYMTestCaseID          SYSLIB-STDLIB-CT-1075
sl@0
   104
@SYMTestCaseDesc	    Tests for operations on pipes 
sl@0
   105
@SYMTestPriority 	    High
sl@0
   106
@SYMTestActions  	    Excecute the basic test operations
sl@0
   107
@SYMTestExpectedResults Test must not fail
sl@0
   108
@SYMREQ                 REQ0000
sl@0
   109
*/		
sl@0
   110
void do_mainA()
sl@0
   111
	{
sl@0
   112
	char* argv[] = { "tpipe.exe", "A" };
sl@0
   113
sl@0
   114
	setenv("CLASSPATH",".;classes;?:/classes;?:/system/java/lib/classes.zip",0);
sl@0
   115
	setenv("VARIABLE2","value2",0);
sl@0
   116
	setenv("USER","Tumblin' Duke - Go fer yer guns!",0);
sl@0
   117
sl@0
   118
	do_main(2, argv);
sl@0
   119
	}
sl@0
   120
sl@0
   121
/**
sl@0
   122
@SYMTestCaseID          SYSLIB-STDLIB-CT-1076
sl@0
   123
@SYMTestCaseDesc	    Tests for operations on pipes 
sl@0
   124
@SYMTestPriority 	    High
sl@0
   125
@SYMTestActions  	    Tests for ioctl functions
sl@0
   126
@SYMTestExpectedResults Test must not fail
sl@0
   127
@SYMREQ                 REQ0000
sl@0
   128
*/		
sl@0
   129
void do_mainB()
sl@0
   130
	{
sl@0
   131
	test_Data;
sl@0
   132
	char* argv[] = { "tpipe.exe", "B" };
sl@0
   133
	int nbytes=1;
sl@0
   134
	int mask, err;
sl@0
   135
	int firstread=1;
sl@0
   136
sl@0
   137
	/* Don't use test_* because it involves fflush and will cause deadlock */
sl@0
   138
	printf("PIPE Child");
sl@0
   139
sl@0
   140
	printf("Child reads from STDIN\n");
sl@0
   141
	while (nbytes>0)
sl@0
   142
		{
sl@0
   143
		char buf[5];
sl@0
   144
		printf("Child calls read of fid=0\n");
sl@0
   145
		nbytes=read(0,buf,sizeof(buf));
sl@0
   146
		printf("Child got %d bytes\n", nbytes);
sl@0
   147
		if (nbytes>0)
sl@0
   148
			{
sl@0
   149
			if (firstread)
sl@0
   150
				{
sl@0
   151
				/* We hope that there is still stuff to read */
sl@0
   152
				printf("Child calls E32IOSELECT(WRITE) on STDIN\n");
sl@0
   153
				mask=E32SELECT_WRITE;
sl@0
   154
				err=ioctl(0,E32IOSELECT,(void*)&mask);
sl@0
   155
				test(err==0);
sl@0
   156
				test(mask==0);
sl@0
   157
				printf("Child calls E32IOSELECT(READ) on STDIN\n");
sl@0
   158
				mask=E32SELECT_READ+E32SELECT_WRITE;
sl@0
   159
				err=ioctl(0,E32IOSELECT,(void*)&mask);
sl@0
   160
				test(err==0);
sl@0
   161
				test(mask==E32SELECT_READ);
sl@0
   162
				firstread=0;
sl@0
   163
				}
sl@0
   164
			printf("  %02d >%.*s<\n", nbytes, nbytes, buf);
sl@0
   165
			}
sl@0
   166
		else
sl@0
   167
		if (nbytes<0)
sl@0
   168
			printf("  %d (errno=%d)\n", nbytes, errno);
sl@0
   169
		}
sl@0
   170
sl@0
   171
	printf("Child calls E32IOSELECT on STDOUT");
sl@0
   172
	mask=E32SELECT_READ+E32SELECT_WRITE;
sl@0
   173
	err=ioctl(1,E32IOSELECT,(void*)&mask);
sl@0
   174
	test(err==0);
sl@0
   175
	test(mask==E32SELECT_WRITE);
sl@0
   176
sl@0
   177
	fflush(stdout);
sl@0
   178
sl@0
   179
	do_main(2, argv);
sl@0
   180
	fclose(stdout);
sl@0
   181
	}
sl@0
   182
sl@0
   183
/**
sl@0
   184
@SYMTestCaseID          SYSLIB-STDLIB-CT-1077
sl@0
   185
@SYMTestCaseDesc	    Tests for operations on pipes 
sl@0
   186
@SYMTestPriority 	    High
sl@0
   187
@SYMTestActions  	    Tests for file descriptors
sl@0
   188
@SYMTestExpectedResults Test must not fail
sl@0
   189
@SYMREQ                 REQ0000
sl@0
   190
*/		
sl@0
   191
void do_piping()
sl@0
   192
	{
sl@0
   193
	test_Data;
sl@0
   194
	char* s;
sl@0
   195
	char buf[180];
sl@0
   196
sl@0
   197
	test_Title("PIPE");
sl@0
   198
sl@0
   199
	test_Next("Pipe file descriptors");
sl@0
   200
	printf("  fids= %d, %d, %d\n", fids[0], fids[1], fids[2]);
sl@0
   201
sl@0
   202
	if (fids[0]>=0)
sl@0
   203
		{
sl@0
   204
		int nbytes;
sl@0
   205
		int remainder;
sl@0
   206
		int mask, err;
sl@0
   207
		const char* buf="test data 18 bytes";
sl@0
   208
sl@0
   209
		test_Next("E32IOSELECT(READ) on Child STDIN");
sl@0
   210
		mask=E32SELECT_READ;
sl@0
   211
		err=ioctl(fids[0],E32IOSELECT,(void*)&mask);
sl@0
   212
		test(err==0);
sl@0
   213
		test(mask==0);
sl@0
   214
		test_Next("E32IOSELECT(WRITE) on Child STDIN");
sl@0
   215
		mask=E32SELECT_READ+E32SELECT_WRITE;
sl@0
   216
		err=ioctl(fids[0],E32IOSELECT,(void*)&mask);
sl@0
   217
		test(err==0);
sl@0
   218
		test(mask==E32SELECT_WRITE);
sl@0
   219
sl@0
   220
		test_Next("Child STDIN");
sl@0
   221
		remainder=strlen(buf);
sl@0
   222
		while (remainder>0)
sl@0
   223
			{
sl@0
   224
			int length=(remainder<7)?remainder:7;
sl@0
   225
			nbytes=write(fids[0],buf,length);
sl@0
   226
			printf("  >%.*s<",nbytes,buf);
sl@0
   227
			buf+=nbytes;
sl@0
   228
			remainder-=nbytes;
sl@0
   229
			test(nbytes>0);
sl@0
   230
			test(nbytes<=length);
sl@0
   231
			}
sl@0
   232
		printf("\n");
sl@0
   233
		close(fids[0]);
sl@0
   234
		sleep(5);
sl@0
   235
		}
sl@0
   236
	if (fids[1]>=0)
sl@0
   237
		{
sl@0
   238
		FILE* cout;
sl@0
   239
		int mask, err;
sl@0
   240
		int firstread=1;
sl@0
   241
sl@0
   242
		test_Next("Child STDOUT");
sl@0
   243
		cout=fdopen(fids[1],"r");
sl@0
   244
		test(cout!=0);
sl@0
   245
sl@0
   246
		do
sl@0
   247
			{
sl@0
   248
			s=fgets(buf,sizeof(buf),cout);
sl@0
   249
			if (s!=0)
sl@0
   250
				{
sl@0
   251
				printf("  >%s", s);
sl@0
   252
				if (firstread)
sl@0
   253
					{
sl@0
   254
					test_Next("\nE32IOSELECT(WRITE) on Child STDOUT");
sl@0
   255
					mask=E32SELECT_WRITE;
sl@0
   256
					err=ioctl(fids[1],E32IOSELECT,(void*)&mask);
sl@0
   257
					test(err==0);
sl@0
   258
					test(mask==0);
sl@0
   259
					test_Next("E32IOSELECT(READ) on Child STDOUT");
sl@0
   260
					mask=E32SELECT_READ+E32SELECT_WRITE;
sl@0
   261
					err=ioctl(fids[1],E32IOSELECT,(void*)&mask);
sl@0
   262
					test(err==0);
sl@0
   263
					test(mask==E32SELECT_READ);
sl@0
   264
					firstread=0;
sl@0
   265
					}
sl@0
   266
				}
sl@0
   267
			}
sl@0
   268
		while (s!=0);
sl@0
   269
		fclose(cout);
sl@0
   270
		}
sl@0
   271
sl@0
   272
	test_Close();
sl@0
   273
	}
sl@0
   274
sl@0
   275
/* Linked with mcrt0.o, so that the exe starts the CPosixServer automatically as per the
sl@0
   276
 * plan all along.
sl@0
   277
 */
sl@0
   278
sl@0
   279
int main(int argc, char* argv[])
sl@0
   280
	{
sl@0
   281
	void* proc2;
sl@0
   282
sl@0
   283
	start_redirection_server();
sl@0
   284
sl@0
   285
	if (argc==1)
sl@0
   286
		{
sl@0
   287
		do_mainA();
sl@0
   288
		sleep(5);
sl@0
   289
		proc2 = create_process(do_mainB, "B", "rw", fids);
sl@0
   290
		if (proc2)
sl@0
   291
			start_process(proc2);
sl@0
   292
		else
sl@0
   293
			perror("Failed to start processB: ");
sl@0
   294
sl@0
   295
		if (proc2)
sl@0
   296
			{
sl@0
   297
			int exit;
sl@0
   298
			do_piping();
sl@0
   299
			exit=wait_for_process(proc2);
sl@0
   300
			printf("wait_for_process returned %d\r\n", exit);
sl@0
   301
			}
sl@0
   302
		}
sl@0
   303
	else
sl@0
   304
		{
sl@0
   305
		do_mainB();
sl@0
   306
		}
sl@0
   307
sl@0
   308
	return 0;
sl@0
   309
	}