1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TPIPE.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,309 @@
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-1074
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, char* argv[])
1.45 + {
1.46 + test_Data;
1.47 + int i;
1.48 + char* var;
1.49 + char* varname;
1.50 + char cwd[MAXPATHLEN];
1.51 +
1.52 + test_Title("PIPE");
1.53 +
1.54 + test_Next("Command line arguments");
1.55 + test(argc>0);
1.56 + test(argv!=0);
1.57 + printf(" argc=%d\r\n", argc);
1.58 + for (i=0; i<argc; i++)
1.59 + {
1.60 + test(argv[i]!=0);
1.61 + printf(" argv[%d]=\"%s\" length %d\r\n", i, argv[i], strlen(argv[i]));
1.62 + }
1.63 + printf("\r\n");
1.64 +
1.65 + test_Next("Current working directory");
1.66 + var=getcwd(cwd,sizeof(cwd));
1.67 + test(var==cwd);
1.68 + printf(" %s\r\n\n", cwd);
1.69 +
1.70 + test_Next("Change directory");
1.71 + i=chdir("z:/system");
1.72 + test(i==0);
1.73 + var=getcwd(cwd,sizeof(cwd));
1.74 + test(var==cwd);
1.75 + printf(" %s\r\n\n", cwd);
1.76 +
1.77 + test_Next("Environment variables");
1.78 +
1.79 + varname="CLASSPATH";
1.80 + var=getenv(varname);
1.81 + test(var!=0);
1.82 + printf(" %s=%s\r\n", varname,var);
1.83 +
1.84 + varname="VARIABLE2";
1.85 + var=getenv(varname);
1.86 + if (var!=0)
1.87 + {
1.88 + printf(" %s=%s\r\n", varname,var);
1.89 + unsetenv("VARIABLE2");
1.90 + }
1.91 +
1.92 + varname="USER";
1.93 + var=getenv(varname);
1.94 + test(var!=0);
1.95 + printf(" %s=%s\r\n\n", varname,var);
1.96 +
1.97 + sleep(5);
1.98 +
1.99 + test_Close();
1.100 + return 0;
1.101 + }
1.102 +
1.103 +/**
1.104 +Bodge up two pretend processes
1.105 +
1.106 +@SYMTestCaseID SYSLIB-STDLIB-CT-1075
1.107 +@SYMTestCaseDesc Tests for operations on pipes
1.108 +@SYMTestPriority High
1.109 +@SYMTestActions Excecute the basic test operations
1.110 +@SYMTestExpectedResults Test must not fail
1.111 +@SYMREQ REQ0000
1.112 +*/
1.113 +void do_mainA()
1.114 + {
1.115 + char* argv[] = { "tpipe.exe", "A" };
1.116 +
1.117 + setenv("CLASSPATH",".;classes;?:/classes;?:/system/java/lib/classes.zip",0);
1.118 + setenv("VARIABLE2","value2",0);
1.119 + setenv("USER","Tumblin' Duke - Go fer yer guns!",0);
1.120 +
1.121 + do_main(2, argv);
1.122 + }
1.123 +
1.124 +/**
1.125 +@SYMTestCaseID SYSLIB-STDLIB-CT-1076
1.126 +@SYMTestCaseDesc Tests for operations on pipes
1.127 +@SYMTestPriority High
1.128 +@SYMTestActions Tests for ioctl functions
1.129 +@SYMTestExpectedResults Test must not fail
1.130 +@SYMREQ REQ0000
1.131 +*/
1.132 +void do_mainB()
1.133 + {
1.134 + test_Data;
1.135 + char* argv[] = { "tpipe.exe", "B" };
1.136 + int nbytes=1;
1.137 + int mask, err;
1.138 + int firstread=1;
1.139 +
1.140 + /* Don't use test_* because it involves fflush and will cause deadlock */
1.141 + printf("PIPE Child");
1.142 +
1.143 + printf("Child reads from STDIN\n");
1.144 + while (nbytes>0)
1.145 + {
1.146 + char buf[5];
1.147 + printf("Child calls read of fid=0\n");
1.148 + nbytes=read(0,buf,sizeof(buf));
1.149 + printf("Child got %d bytes\n", nbytes);
1.150 + if (nbytes>0)
1.151 + {
1.152 + if (firstread)
1.153 + {
1.154 + /* We hope that there is still stuff to read */
1.155 + printf("Child calls E32IOSELECT(WRITE) on STDIN\n");
1.156 + mask=E32SELECT_WRITE;
1.157 + err=ioctl(0,E32IOSELECT,(void*)&mask);
1.158 + test(err==0);
1.159 + test(mask==0);
1.160 + printf("Child calls E32IOSELECT(READ) on STDIN\n");
1.161 + mask=E32SELECT_READ+E32SELECT_WRITE;
1.162 + err=ioctl(0,E32IOSELECT,(void*)&mask);
1.163 + test(err==0);
1.164 + test(mask==E32SELECT_READ);
1.165 + firstread=0;
1.166 + }
1.167 + printf(" %02d >%.*s<\n", nbytes, nbytes, buf);
1.168 + }
1.169 + else
1.170 + if (nbytes<0)
1.171 + printf(" %d (errno=%d)\n", nbytes, errno);
1.172 + }
1.173 +
1.174 + printf("Child calls E32IOSELECT on STDOUT");
1.175 + mask=E32SELECT_READ+E32SELECT_WRITE;
1.176 + err=ioctl(1,E32IOSELECT,(void*)&mask);
1.177 + test(err==0);
1.178 + test(mask==E32SELECT_WRITE);
1.179 +
1.180 + fflush(stdout);
1.181 +
1.182 + do_main(2, argv);
1.183 + fclose(stdout);
1.184 + }
1.185 +
1.186 +/**
1.187 +@SYMTestCaseID SYSLIB-STDLIB-CT-1077
1.188 +@SYMTestCaseDesc Tests for operations on pipes
1.189 +@SYMTestPriority High
1.190 +@SYMTestActions Tests for file descriptors
1.191 +@SYMTestExpectedResults Test must not fail
1.192 +@SYMREQ REQ0000
1.193 +*/
1.194 +void do_piping()
1.195 + {
1.196 + test_Data;
1.197 + char* s;
1.198 + char buf[180];
1.199 +
1.200 + test_Title("PIPE");
1.201 +
1.202 + test_Next("Pipe file descriptors");
1.203 + printf(" fids= %d, %d, %d\n", fids[0], fids[1], fids[2]);
1.204 +
1.205 + if (fids[0]>=0)
1.206 + {
1.207 + int nbytes;
1.208 + int remainder;
1.209 + int mask, err;
1.210 + const char* buf="test data 18 bytes";
1.211 +
1.212 + test_Next("E32IOSELECT(READ) on Child STDIN");
1.213 + mask=E32SELECT_READ;
1.214 + err=ioctl(fids[0],E32IOSELECT,(void*)&mask);
1.215 + test(err==0);
1.216 + test(mask==0);
1.217 + test_Next("E32IOSELECT(WRITE) on Child STDIN");
1.218 + mask=E32SELECT_READ+E32SELECT_WRITE;
1.219 + err=ioctl(fids[0],E32IOSELECT,(void*)&mask);
1.220 + test(err==0);
1.221 + test(mask==E32SELECT_WRITE);
1.222 +
1.223 + test_Next("Child STDIN");
1.224 + remainder=strlen(buf);
1.225 + while (remainder>0)
1.226 + {
1.227 + int length=(remainder<7)?remainder:7;
1.228 + nbytes=write(fids[0],buf,length);
1.229 + printf(" >%.*s<",nbytes,buf);
1.230 + buf+=nbytes;
1.231 + remainder-=nbytes;
1.232 + test(nbytes>0);
1.233 + test(nbytes<=length);
1.234 + }
1.235 + printf("\n");
1.236 + close(fids[0]);
1.237 + sleep(5);
1.238 + }
1.239 + if (fids[1]>=0)
1.240 + {
1.241 + FILE* cout;
1.242 + int mask, err;
1.243 + int firstread=1;
1.244 +
1.245 + test_Next("Child STDOUT");
1.246 + cout=fdopen(fids[1],"r");
1.247 + test(cout!=0);
1.248 +
1.249 + do
1.250 + {
1.251 + s=fgets(buf,sizeof(buf),cout);
1.252 + if (s!=0)
1.253 + {
1.254 + printf(" >%s", s);
1.255 + if (firstread)
1.256 + {
1.257 + test_Next("\nE32IOSELECT(WRITE) on Child STDOUT");
1.258 + mask=E32SELECT_WRITE;
1.259 + err=ioctl(fids[1],E32IOSELECT,(void*)&mask);
1.260 + test(err==0);
1.261 + test(mask==0);
1.262 + test_Next("E32IOSELECT(READ) on Child STDOUT");
1.263 + mask=E32SELECT_READ+E32SELECT_WRITE;
1.264 + err=ioctl(fids[1],E32IOSELECT,(void*)&mask);
1.265 + test(err==0);
1.266 + test(mask==E32SELECT_READ);
1.267 + firstread=0;
1.268 + }
1.269 + }
1.270 + }
1.271 + while (s!=0);
1.272 + fclose(cout);
1.273 + }
1.274 +
1.275 + test_Close();
1.276 + }
1.277 +
1.278 +/* Linked with mcrt0.o, so that the exe starts the CPosixServer automatically as per the
1.279 + * plan all along.
1.280 + */
1.281 +
1.282 +int main(int argc, char* argv[])
1.283 + {
1.284 + void* proc2;
1.285 +
1.286 + start_redirection_server();
1.287 +
1.288 + if (argc==1)
1.289 + {
1.290 + do_mainA();
1.291 + sleep(5);
1.292 + proc2 = create_process(do_mainB, "B", "rw", fids);
1.293 + if (proc2)
1.294 + start_process(proc2);
1.295 + else
1.296 + perror("Failed to start processB: ");
1.297 +
1.298 + if (proc2)
1.299 + {
1.300 + int exit;
1.301 + do_piping();
1.302 + exit=wait_for_process(proc2);
1.303 + printf("wait_for_process returned %d\r\n", exit);
1.304 + }
1.305 + }
1.306 + else
1.307 + {
1.308 + do_mainB();
1.309 + }
1.310 +
1.311 + return 0;
1.312 + }