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