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