os/ossrv/genericopenlibs/cstdlib/TSTLIB/TWPIPE.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-1092
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, wchar_t* argv[])
sl@0
    42
	{
sl@0
    43
	test_Data;
sl@0
    44
	int i;
sl@0
    45
	wchar_t* var;
sl@0
    46
	wchar_t* varname;
sl@0
    47
	wchar_t cwd[MAXPATHLEN];
sl@0
    48
	char buf[200];
sl@0
    49
	char buf1[200];
sl@0
    50
sl@0
    51
	test_Title("PIPE");
sl@0
    52
sl@0
    53
	test_Next("Command line arguments");
sl@0
    54
	test(argc>0);
sl@0
    55
	test(argv!=0);
sl@0
    56
	printf("  argc=%d\r\n", argc);
sl@0
    57
	for (i=0; i<argc; i++)
sl@0
    58
		{
sl@0
    59
		test(argv[i]!=0);
sl@0
    60
		test(-1 != wcstombs(buf, argv[i], sizeof(buf)));
sl@0
    61
		printf("  argv[%d]=\"%s\" length %d\r\n", i, buf, strlen(buf));
sl@0
    62
		}
sl@0
    63
	printf("\r\n");
sl@0
    64
sl@0
    65
	test_Next("Current working directory");
sl@0
    66
	var=wgetcwd(cwd,sizeof(cwd)/2);
sl@0
    67
	test(var==cwd);
sl@0
    68
	test(-1 != wcstombs(buf, cwd, sizeof(buf)));
sl@0
    69
	printf("  %s\r\n\n", buf);
sl@0
    70
sl@0
    71
	test_Next("Change directory");
sl@0
    72
	i=wchdir((wchar_t*)L"z:/system");
sl@0
    73
	test(i==0);
sl@0
    74
	var=wgetcwd(cwd,sizeof(cwd)/2);
sl@0
    75
	test(var==cwd);
sl@0
    76
	test(-1 != wcstombs(buf, cwd, sizeof(buf)));
sl@0
    77
	printf("  %s\r\n\n", buf);
sl@0
    78
sl@0
    79
	test_Next("Environment variables");
sl@0
    80
sl@0
    81
	varname=(wchar_t*)L"CLASSPATH";
sl@0
    82
	var=wgetenv(varname);
sl@0
    83
	test(var!=0);
sl@0
    84
	test(-1 != wcstombs(buf, var, sizeof(buf)));
sl@0
    85
	test(-1 != wcstombs(buf1, varname, sizeof(buf1)));
sl@0
    86
	printf("  %s=%s\r\n", buf1, buf);
sl@0
    87
sl@0
    88
	varname=(wchar_t*)"VARIABLE2";
sl@0
    89
	var=wgetenv(varname);
sl@0
    90
	if (var!=0)
sl@0
    91
		{
sl@0
    92
		test(-1 != wcstombs(buf, var, sizeof(buf)));
sl@0
    93
		test(-1 != wcstombs(buf1, varname, sizeof(buf1)));
sl@0
    94
		printf("  %s=%s\r\n", buf1, buf);
sl@0
    95
		wunsetenv((wchar_t*)"VARIABLE2");
sl@0
    96
		}
sl@0
    97
sl@0
    98
	varname=(wchar_t*)L"USER";
sl@0
    99
	var=wgetenv(varname);
sl@0
   100
	test(var!=0);
sl@0
   101
	test(-1 != wcstombs(buf, var, sizeof(buf)));
sl@0
   102
	test(-1 != wcstombs(buf1, varname, sizeof(buf1)));
sl@0
   103
	printf("  %s=%s\r\n", buf1, buf);
sl@0
   104
sl@0
   105
	sleep(5);
sl@0
   106
sl@0
   107
	test_Close();
sl@0
   108
	return 0;
sl@0
   109
	}
sl@0
   110
sl@0
   111
/* bodge up two pretend processes */
sl@0
   112
sl@0
   113
void do_mainA()
sl@0
   114
	{
sl@0
   115
	wchar_t* argv[] = { (wchar_t*)L"twpipe.exe", (wchar_t*)L"A" };
sl@0
   116
sl@0
   117
	wsetenv((wchar_t*)L"CLASSPATH",(wchar_t*)L".;classes;?:/classes;?:/system/java/lib/classes.zip",0);
sl@0
   118
	wsetenv((wchar_t*)L"VARIABLE2",(wchar_t*)L"value2",0);
sl@0
   119
	wsetenv((wchar_t*)L"USER",(wchar_t*)L"Tumblin' Wide Boy - Go fer yer guns!",0);
sl@0
   120
sl@0
   121
	do_main(2, argv);
sl@0
   122
	}
sl@0
   123
sl@0
   124
/**
sl@0
   125
@SYMTestCaseID          SYSLIB-STDLIB-CT-1093
sl@0
   126
@SYMTestCaseDesc	    Tests for operations on a child pipe
sl@0
   127
@SYMTestPriority 	    High
sl@0
   128
@SYMTestActions  	    Tests for reading from a pipe in the child process
sl@0
   129
@SYMTestExpectedResults Test must not fail
sl@0
   130
@SYMREQ                 REQ0000
sl@0
   131
*/		
sl@0
   132
void do_mainB()
sl@0
   133
	{
sl@0
   134
	test_Data;
sl@0
   135
	wchar_t* argv[] = {(wchar_t*) L"twpipe.exe", (wchar_t*)L"B" };
sl@0
   136
	int nbytes=1;
sl@0
   137
	int mask, err;
sl@0
   138
	int firstread=1;
sl@0
   139
sl@0
   140
	/* Don't use test_* because it involves fflush and will cause deadlock */
sl@0
   141
	printf("PIPE Child");
sl@0
   142
sl@0
   143
	printf("Child reads from STDIN\n");
sl@0
   144
	while (nbytes>0)
sl@0
   145
		{
sl@0
   146
		char buf[5];
sl@0
   147
		printf("Child calls read of fid=0\n");
sl@0
   148
		nbytes=read(0,buf,sizeof(buf));
sl@0
   149
		printf("Child got %d bytes\n", nbytes);
sl@0
   150
		if (nbytes>0)
sl@0
   151
			{
sl@0
   152
			if (firstread)
sl@0
   153
				{
sl@0
   154
				/* We hope that there is still stuff to read */
sl@0
   155
				printf("Child calls E32IOSELECT(WRITE) on STDIN\n");
sl@0
   156
				mask=E32SELECT_WRITE;
sl@0
   157
				err=ioctl(0,E32IOSELECT,(void*)&mask);
sl@0
   158
				test(err==0);
sl@0
   159
				test(mask==0);
sl@0
   160
				printf("Child calls E32IOSELECT(READ) on STDIN\n");
sl@0
   161
				mask=E32SELECT_READ+E32SELECT_WRITE;
sl@0
   162
				err=ioctl(0,E32IOSELECT,(void*)&mask);
sl@0
   163
				test(err==0);
sl@0
   164
				test(mask==E32SELECT_READ);
sl@0
   165
				firstread=0;
sl@0
   166
				}
sl@0
   167
			printf("  %02d >%.*s<\n", nbytes, nbytes, buf);
sl@0
   168
			}
sl@0
   169
		else
sl@0
   170
		if (nbytes<0)
sl@0
   171
			printf("  %d (errno=%d)\n", nbytes, errno);
sl@0
   172
		}
sl@0
   173
sl@0
   174
	printf("Child calls E32IOSELECT on STDOUT");
sl@0
   175
	mask=E32SELECT_READ+E32SELECT_WRITE;
sl@0
   176
	err=ioctl(1,E32IOSELECT,(void*)&mask);
sl@0
   177
	test(err==0);
sl@0
   178
	test(mask==E32SELECT_WRITE);
sl@0
   179
sl@0
   180
	fflush(stdout);
sl@0
   181
sl@0
   182
	do_main(2, argv);
sl@0
   183
	fclose(stdout);
sl@0
   184
	}
sl@0
   185
sl@0
   186
/**
sl@0
   187
@SYMTestCaseID          SYSLIB-STDLIB-CT-1094
sl@0
   188
@SYMTestCaseDesc	    Tests for pipe file descriptors
sl@0
   189
@SYMTestPriority 	    High
sl@0
   190
@SYMTestActions  	    Tests for writing to the pipe file descriptors
sl@0
   191
@SYMTestExpectedResults Test must not fail
sl@0
   192
@SYMREQ                 REQ0000
sl@0
   193
*/		
sl@0
   194
void do_piping()
sl@0
   195
	{
sl@0
   196
	test_Data;
sl@0
   197
	char* s;
sl@0
   198
	char buf[180];
sl@0
   199
sl@0
   200
	test_Title("PIPE");
sl@0
   201
sl@0
   202
	test_Next("Pipe file descriptors");
sl@0
   203
	printf("  fids= %d, %d, %d\n", fids[0], fids[1], fids[2]);
sl@0
   204
sl@0
   205
	if (fids[0]>=0)
sl@0
   206
		{
sl@0
   207
		int nbytes;
sl@0
   208
		int remainder;
sl@0
   209
		int mask, err;
sl@0
   210
		const char* buf="test data 18 bytes";
sl@0
   211
sl@0
   212
		test_Next("E32IOSELECT(READ) on Child STDIN");
sl@0
   213
		mask=E32SELECT_READ;
sl@0
   214
		err=ioctl(fids[0],E32IOSELECT,(void*)&mask);
sl@0
   215
		test(err==0);
sl@0
   216
		test(mask==0);
sl@0
   217
		test_Next("E32IOSELECT(WRITE) on Child STDIN");
sl@0
   218
		mask=E32SELECT_READ+E32SELECT_WRITE;
sl@0
   219
		err=ioctl(fids[0],E32IOSELECT,(void*)&mask);
sl@0
   220
		test(err==0);
sl@0
   221
		test(mask==E32SELECT_WRITE);
sl@0
   222
sl@0
   223
		test_Next("Child STDIN");
sl@0
   224
		remainder=strlen(buf);
sl@0
   225
		while (remainder>0)
sl@0
   226
			{
sl@0
   227
			int length=(remainder<7)?remainder:7;
sl@0
   228
			nbytes=write(fids[0],buf,length);
sl@0
   229
			printf("  >%.*s<",nbytes,buf);
sl@0
   230
			buf+=nbytes;
sl@0
   231
			remainder-=nbytes;
sl@0
   232
			test(nbytes>0);
sl@0
   233
			test(nbytes<=length);
sl@0
   234
			}
sl@0
   235
		printf("\n");
sl@0
   236
		close(fids[0]);
sl@0
   237
		sleep(5);
sl@0
   238
		}
sl@0
   239
	if (fids[1]>=0)
sl@0
   240
		{
sl@0
   241
		FILE* cout;
sl@0
   242
		int mask, err;
sl@0
   243
		int firstread=1;
sl@0
   244
sl@0
   245
		test_Next("Child STDOUT");
sl@0
   246
		cout=wfdopen(fids[1],(wchar_t*)L"r");
sl@0
   247
		test(cout!=0);
sl@0
   248
sl@0
   249
		do
sl@0
   250
			{
sl@0
   251
			s=fgets(buf,sizeof(buf),cout);
sl@0
   252
			if (s!=0)
sl@0
   253
				{
sl@0
   254
				printf("  >%s", s);
sl@0
   255
				if (firstread)
sl@0
   256
					{
sl@0
   257
					test_Next("\nE32IOSELECT(WRITE) on Child STDOUT");
sl@0
   258
					mask=E32SELECT_WRITE;
sl@0
   259
					err=ioctl(fids[1],E32IOSELECT,(void*)&mask);
sl@0
   260
					test(err==0);
sl@0
   261
					test(mask==0);
sl@0
   262
					test_Next("E32IOSELECT(READ) on Child STDOUT");
sl@0
   263
					mask=E32SELECT_READ+E32SELECT_WRITE;
sl@0
   264
					err=ioctl(fids[1],E32IOSELECT,(void*)&mask);
sl@0
   265
					test(err==0);
sl@0
   266
					test(mask==E32SELECT_READ);
sl@0
   267
					firstread=0;
sl@0
   268
					}
sl@0
   269
				}
sl@0
   270
			}
sl@0
   271
		while (s!=0);
sl@0
   272
		fclose(cout);
sl@0
   273
		}
sl@0
   274
sl@0
   275
	test_Close();
sl@0
   276
	}
sl@0
   277
sl@0
   278
/* Linked with mcrt0.o, so that the exe starts the CPosixServer automatically as per the
sl@0
   279
 * plan all along.
sl@0
   280
 */
sl@0
   281
sl@0
   282
int wmain(int argc, wchar_t* argv[])
sl@0
   283
	{
sl@0
   284
	void* proc2;
sl@0
   285
sl@0
   286
	start_redirection_server();
sl@0
   287
sl@0
   288
	if (argc==1)
sl@0
   289
		{
sl@0
   290
		do_mainA();
sl@0
   291
sl@0
   292
		proc2 = create_process(do_mainB, "B", "rw", fids);
sl@0
   293
		if (proc2)
sl@0
   294
			start_process(proc2);
sl@0
   295
		else
sl@0
   296
			perror("Failed to start processB: ");
sl@0
   297
sl@0
   298
		if (proc2)
sl@0
   299
			{
sl@0
   300
			int exit;
sl@0
   301
			do_piping();
sl@0
   302
			exit=wait_for_process(proc2);
sl@0
   303
			printf("wait_for_process returned %d\r\n", exit);
sl@0
   304
			}
sl@0
   305
		}
sl@0
   306
	else
sl@0
   307
		{
sl@0
   308
		do_mainB();
sl@0
   309
		}
sl@0
   310
sl@0
   311
	return 0;
sl@0
   312
	}