1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TWPIPE.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,312 @@
1.4 +/*
1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Test code for pipes, using dubious WINS extension for multiple processes...
1.19 +*
1.20 +*
1.21 +*/
1.22 +
1.23 +
1.24 +
1.25 +#include <stdlib.h>
1.26 +#include <stdio.h>
1.27 +#include <string.h>
1.28 +#include <unistd.h> /* for MAXPATHLEN */
1.29 +#include <sys/errno.h>
1.30 +#include <sys/ioctl.h>
1.31 +
1.32 +#include "CTEST.H"
1.33 +
1.34 +int fids[3];
1.35 +
1.36 +/**
1.37 +@SYMTestCaseID SYSLIB-STDLIB-CT-1092
1.38 +@SYMTestCaseDesc Tests for operations on pipes
1.39 +@SYMTestPriority High
1.40 +@SYMTestActions Tests for command line arguments,directory operations,environment variables.
1.41 +@SYMTestExpectedResults Test must not fail
1.42 +@SYMREQ REQ0000
1.43 +*/
1.44 +int do_main(int argc, wchar_t* argv[])
1.45 + {
1.46 + test_Data;
1.47 + int i;
1.48 + wchar_t* var;
1.49 + wchar_t* varname;
1.50 + wchar_t cwd[MAXPATHLEN];
1.51 + char buf[200];
1.52 + char buf1[200];
1.53 +
1.54 + test_Title("PIPE");
1.55 +
1.56 + test_Next("Command line arguments");
1.57 + test(argc>0);
1.58 + test(argv!=0);
1.59 + printf(" argc=%d\r\n", argc);
1.60 + for (i=0; i<argc; i++)
1.61 + {
1.62 + test(argv[i]!=0);
1.63 + test(-1 != wcstombs(buf, argv[i], sizeof(buf)));
1.64 + printf(" argv[%d]=\"%s\" length %d\r\n", i, buf, strlen(buf));
1.65 + }
1.66 + printf("\r\n");
1.67 +
1.68 + test_Next("Current working directory");
1.69 + var=wgetcwd(cwd,sizeof(cwd)/2);
1.70 + test(var==cwd);
1.71 + test(-1 != wcstombs(buf, cwd, sizeof(buf)));
1.72 + printf(" %s\r\n\n", buf);
1.73 +
1.74 + test_Next("Change directory");
1.75 + i=wchdir((wchar_t*)L"z:/system");
1.76 + test(i==0);
1.77 + var=wgetcwd(cwd,sizeof(cwd)/2);
1.78 + test(var==cwd);
1.79 + test(-1 != wcstombs(buf, cwd, sizeof(buf)));
1.80 + printf(" %s\r\n\n", buf);
1.81 +
1.82 + test_Next("Environment variables");
1.83 +
1.84 + varname=(wchar_t*)L"CLASSPATH";
1.85 + var=wgetenv(varname);
1.86 + test(var!=0);
1.87 + test(-1 != wcstombs(buf, var, sizeof(buf)));
1.88 + test(-1 != wcstombs(buf1, varname, sizeof(buf1)));
1.89 + printf(" %s=%s\r\n", buf1, buf);
1.90 +
1.91 + varname=(wchar_t*)"VARIABLE2";
1.92 + var=wgetenv(varname);
1.93 + if (var!=0)
1.94 + {
1.95 + test(-1 != wcstombs(buf, var, sizeof(buf)));
1.96 + test(-1 != wcstombs(buf1, varname, sizeof(buf1)));
1.97 + printf(" %s=%s\r\n", buf1, buf);
1.98 + wunsetenv((wchar_t*)"VARIABLE2");
1.99 + }
1.100 +
1.101 + varname=(wchar_t*)L"USER";
1.102 + var=wgetenv(varname);
1.103 + test(var!=0);
1.104 + test(-1 != wcstombs(buf, var, sizeof(buf)));
1.105 + test(-1 != wcstombs(buf1, varname, sizeof(buf1)));
1.106 + printf(" %s=%s\r\n", buf1, buf);
1.107 +
1.108 + sleep(5);
1.109 +
1.110 + test_Close();
1.111 + return 0;
1.112 + }
1.113 +
1.114 +/* bodge up two pretend processes */
1.115 +
1.116 +void do_mainA()
1.117 + {
1.118 + wchar_t* argv[] = { (wchar_t*)L"twpipe.exe", (wchar_t*)L"A" };
1.119 +
1.120 + wsetenv((wchar_t*)L"CLASSPATH",(wchar_t*)L".;classes;?:/classes;?:/system/java/lib/classes.zip",0);
1.121 + wsetenv((wchar_t*)L"VARIABLE2",(wchar_t*)L"value2",0);
1.122 + wsetenv((wchar_t*)L"USER",(wchar_t*)L"Tumblin' Wide Boy - Go fer yer guns!",0);
1.123 +
1.124 + do_main(2, argv);
1.125 + }
1.126 +
1.127 +/**
1.128 +@SYMTestCaseID SYSLIB-STDLIB-CT-1093
1.129 +@SYMTestCaseDesc Tests for operations on a child pipe
1.130 +@SYMTestPriority High
1.131 +@SYMTestActions Tests for reading from a pipe in the child process
1.132 +@SYMTestExpectedResults Test must not fail
1.133 +@SYMREQ REQ0000
1.134 +*/
1.135 +void do_mainB()
1.136 + {
1.137 + test_Data;
1.138 + wchar_t* argv[] = {(wchar_t*) L"twpipe.exe", (wchar_t*)L"B" };
1.139 + int nbytes=1;
1.140 + int mask, err;
1.141 + int firstread=1;
1.142 +
1.143 + /* Don't use test_* because it involves fflush and will cause deadlock */
1.144 + printf("PIPE Child");
1.145 +
1.146 + printf("Child reads from STDIN\n");
1.147 + while (nbytes>0)
1.148 + {
1.149 + char buf[5];
1.150 + printf("Child calls read of fid=0\n");
1.151 + nbytes=read(0,buf,sizeof(buf));
1.152 + printf("Child got %d bytes\n", nbytes);
1.153 + if (nbytes>0)
1.154 + {
1.155 + if (firstread)
1.156 + {
1.157 + /* We hope that there is still stuff to read */
1.158 + printf("Child calls E32IOSELECT(WRITE) on STDIN\n");
1.159 + mask=E32SELECT_WRITE;
1.160 + err=ioctl(0,E32IOSELECT,(void*)&mask);
1.161 + test(err==0);
1.162 + test(mask==0);
1.163 + printf("Child calls E32IOSELECT(READ) on STDIN\n");
1.164 + mask=E32SELECT_READ+E32SELECT_WRITE;
1.165 + err=ioctl(0,E32IOSELECT,(void*)&mask);
1.166 + test(err==0);
1.167 + test(mask==E32SELECT_READ);
1.168 + firstread=0;
1.169 + }
1.170 + printf(" %02d >%.*s<\n", nbytes, nbytes, buf);
1.171 + }
1.172 + else
1.173 + if (nbytes<0)
1.174 + printf(" %d (errno=%d)\n", nbytes, errno);
1.175 + }
1.176 +
1.177 + printf("Child calls E32IOSELECT on STDOUT");
1.178 + mask=E32SELECT_READ+E32SELECT_WRITE;
1.179 + err=ioctl(1,E32IOSELECT,(void*)&mask);
1.180 + test(err==0);
1.181 + test(mask==E32SELECT_WRITE);
1.182 +
1.183 + fflush(stdout);
1.184 +
1.185 + do_main(2, argv);
1.186 + fclose(stdout);
1.187 + }
1.188 +
1.189 +/**
1.190 +@SYMTestCaseID SYSLIB-STDLIB-CT-1094
1.191 +@SYMTestCaseDesc Tests for pipe file descriptors
1.192 +@SYMTestPriority High
1.193 +@SYMTestActions Tests for writing to the pipe file descriptors
1.194 +@SYMTestExpectedResults Test must not fail
1.195 +@SYMREQ REQ0000
1.196 +*/
1.197 +void do_piping()
1.198 + {
1.199 + test_Data;
1.200 + char* s;
1.201 + char buf[180];
1.202 +
1.203 + test_Title("PIPE");
1.204 +
1.205 + test_Next("Pipe file descriptors");
1.206 + printf(" fids= %d, %d, %d\n", fids[0], fids[1], fids[2]);
1.207 +
1.208 + if (fids[0]>=0)
1.209 + {
1.210 + int nbytes;
1.211 + int remainder;
1.212 + int mask, err;
1.213 + const char* buf="test data 18 bytes";
1.214 +
1.215 + test_Next("E32IOSELECT(READ) on Child STDIN");
1.216 + mask=E32SELECT_READ;
1.217 + err=ioctl(fids[0],E32IOSELECT,(void*)&mask);
1.218 + test(err==0);
1.219 + test(mask==0);
1.220 + test_Next("E32IOSELECT(WRITE) on Child STDIN");
1.221 + mask=E32SELECT_READ+E32SELECT_WRITE;
1.222 + err=ioctl(fids[0],E32IOSELECT,(void*)&mask);
1.223 + test(err==0);
1.224 + test(mask==E32SELECT_WRITE);
1.225 +
1.226 + test_Next("Child STDIN");
1.227 + remainder=strlen(buf);
1.228 + while (remainder>0)
1.229 + {
1.230 + int length=(remainder<7)?remainder:7;
1.231 + nbytes=write(fids[0],buf,length);
1.232 + printf(" >%.*s<",nbytes,buf);
1.233 + buf+=nbytes;
1.234 + remainder-=nbytes;
1.235 + test(nbytes>0);
1.236 + test(nbytes<=length);
1.237 + }
1.238 + printf("\n");
1.239 + close(fids[0]);
1.240 + sleep(5);
1.241 + }
1.242 + if (fids[1]>=0)
1.243 + {
1.244 + FILE* cout;
1.245 + int mask, err;
1.246 + int firstread=1;
1.247 +
1.248 + test_Next("Child STDOUT");
1.249 + cout=wfdopen(fids[1],(wchar_t*)L"r");
1.250 + test(cout!=0);
1.251 +
1.252 + do
1.253 + {
1.254 + s=fgets(buf,sizeof(buf),cout);
1.255 + if (s!=0)
1.256 + {
1.257 + printf(" >%s", s);
1.258 + if (firstread)
1.259 + {
1.260 + test_Next("\nE32IOSELECT(WRITE) on Child STDOUT");
1.261 + mask=E32SELECT_WRITE;
1.262 + err=ioctl(fids[1],E32IOSELECT,(void*)&mask);
1.263 + test(err==0);
1.264 + test(mask==0);
1.265 + test_Next("E32IOSELECT(READ) on Child STDOUT");
1.266 + mask=E32SELECT_READ+E32SELECT_WRITE;
1.267 + err=ioctl(fids[1],E32IOSELECT,(void*)&mask);
1.268 + test(err==0);
1.269 + test(mask==E32SELECT_READ);
1.270 + firstread=0;
1.271 + }
1.272 + }
1.273 + }
1.274 + while (s!=0);
1.275 + fclose(cout);
1.276 + }
1.277 +
1.278 + test_Close();
1.279 + }
1.280 +
1.281 +/* Linked with mcrt0.o, so that the exe starts the CPosixServer automatically as per the
1.282 + * plan all along.
1.283 + */
1.284 +
1.285 +int wmain(int argc, wchar_t* argv[])
1.286 + {
1.287 + void* proc2;
1.288 +
1.289 + start_redirection_server();
1.290 +
1.291 + if (argc==1)
1.292 + {
1.293 + do_mainA();
1.294 +
1.295 + proc2 = create_process(do_mainB, "B", "rw", fids);
1.296 + if (proc2)
1.297 + start_process(proc2);
1.298 + else
1.299 + perror("Failed to start processB: ");
1.300 +
1.301 + if (proc2)
1.302 + {
1.303 + int exit;
1.304 + do_piping();
1.305 + exit=wait_for_process(proc2);
1.306 + printf("wait_for_process returned %d\r\n", exit);
1.307 + }
1.308 + }
1.309 + else
1.310 + {
1.311 + do_mainB();
1.312 + }
1.313 +
1.314 + return 0;
1.315 + }