os/ossrv/genericopenlibs/cstdlib/TSTLIB/TPIPE2.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TPIPE2.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,336 @@
     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 +void fillbuf(int seed, char* buf, int buflen)
    1.35 +	{
    1.36 +	int j;
    1.37 +	sleep(seed/3);
    1.38 +	seed += 'A';
    1.39 +	for (j=0; j<buflen; j++)
    1.40 +		{
    1.41 +		buf[j]=(char)seed;
    1.42 +		seed+=13;
    1.43 +		if (seed > 127)
    1.44 +			seed = seed - 127 + 'A';
    1.45 +		}
    1.46 +	}
    1.47 +
    1.48 +/**
    1.49 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1078
    1.50 +@SYMTestCaseDesc	    Tests for operations on pipes 
    1.51 +@SYMTestPriority 	    High
    1.52 +@SYMTestActions  	    Tests for writing to an opened pipe and a closed pipe
    1.53 +@SYMTestExpectedResults Test must not fail
    1.54 +@SYMREQ                 REQ0000
    1.55 +*/		
    1.56 +void producer(int fid, int final)
    1.57 +	{
    1.58 +	test_Data;
    1.59 +	char buf[128];
    1.60 +	int nbytes;
    1.61 +	int i;
    1.62 +	int t_errno;
    1.63 +
    1.64 +	test_Title("Producer");
    1.65 +
    1.66 +	for (i=1; i<=8; i++)
    1.67 +		{
    1.68 +		fillbuf(i,buf,sizeof(buf));
    1.69 +		printf("%d",i);
    1.70 +		fflush(stdout);
    1.71 +		nbytes=write(fid, buf, sizeof(buf));
    1.72 +		printf(".");
    1.73 +		fflush(stdout);
    1.74 +		test(nbytes==sizeof(buf));
    1.75 +		}
    1.76 +	printf("\n");
    1.77 +
    1.78 +	if (!final)
    1.79 +		return;
    1.80 +
    1.81 +	test_Next("Writing to closed pipe");
    1.82 +
    1.83 +	fillbuf(0,buf,sizeof(buf));
    1.84 +	close(fid);
    1.85 +	nbytes=write(fid, buf, sizeof(buf));
    1.86 +	test(nbytes<0);
    1.87 +	t_errno=errno;
    1.88 +	test(t_errno==ENOENT);
    1.89 +	}
    1.90 +
    1.91 +#define select_test(fid)	\
    1.92 +	if (async) { \
    1.93 +		mask=E32SELECT_READ+E32SELECT_WRITE; \
    1.94 +		err=ioctl(fid,E32IOSELECT,(void*)&mask); \
    1.95 +		}
    1.96 +
    1.97 +/**
    1.98 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1079
    1.99 +@SYMTestCaseDesc	    Tests for operations on pipes 
   1.100 +@SYMTestPriority 	    High
   1.101 +@SYMTestActions  	    Tests for reading from the pipe,
   1.102 +@SYMTestExpectedResults Test must not fail
   1.103 +@SYMREQ                 REQ0000
   1.104 +*/		
   1.105 +void consumer(int fid, int async)
   1.106 +	{
   1.107 +	test_Data;
   1.108 +	char buf[256];
   1.109 +	char checkbuf[256];
   1.110 +	int nbytes;
   1.111 +	int mask=E32SELECT_READ;
   1.112 +	int err=0;
   1.113 +
   1.114 +	if (async)
   1.115 +		test_Title("Asynchronous consumer");
   1.116 +	else
   1.117 +		test_Title("Consumer");
   1.118 +
   1.119 +		fillbuf(1,checkbuf,128);
   1.120 +
   1.121 +	/* Simple read, exactly matching single write */
   1.122 +
   1.123 +	test_Next("Simple read, exactly matching write");
   1.124 +
   1.125 +	select_test(fid);
   1.126 +	test(err==0);
   1.127 +	test(mask==E32SELECT_READ);
   1.128 +	nbytes=read(fid,buf,128);
   1.129 +	test(nbytes==128);
   1.130 +	test(memcmp(buf,checkbuf,128)==0);
   1.131 +
   1.132 +		fillbuf(2,checkbuf,128);
   1.133 +		fillbuf(3,checkbuf+128,128);
   1.134 +
   1.135 +	/* Simple read, exactly matching 2 writes */
   1.136 +
   1.137 +	test_Next("Simple read, exactly matching 2 writes");
   1.138 +
   1.139 +	select_test(fid);
   1.140 +	test(err==0);
   1.141 +	test(mask==E32SELECT_READ);
   1.142 +	nbytes=read(fid,buf,256);
   1.143 +
   1.144 +#ifdef PIPES_SUPPORT_BUFFERING
   1.145 +	test(nbytes==256);
   1.146 +#else
   1.147 +	test(nbytes==128);				/* truncated at first write */
   1.148 +
   1.149 +	select_test(fid);
   1.150 +	test(err==0);
   1.151 +	test(mask==E32SELECT_READ);
   1.152 +	nbytes=read(fid,buf+128,128);	/* manually continue the read */
   1.153 +	test(nbytes==128);
   1.154 +#endif
   1.155 +	test(memcmp(buf,checkbuf,256)==0);
   1.156 +
   1.157 +		fillbuf(4,checkbuf,128);
   1.158 +
   1.159 +	/* Partial read */
   1.160 +
   1.161 +	test_Next("Partial read");
   1.162 +
   1.163 +	select_test(fid);
   1.164 +	test(err==0);
   1.165 +	test(mask==E32SELECT_READ);
   1.166 +	nbytes=read(fid,buf,100);
   1.167 +	test(nbytes==100);
   1.168 +	test(memcmp(buf,checkbuf,100)==0);
   1.169 +
   1.170 +	/* Partial read, completing the write exactly */
   1.171 +
   1.172 +	test_Next("Partial read, completes matching write");
   1.173 +
   1.174 +	select_test(fid);
   1.175 +	test(err==0);
   1.176 +	test(mask==E32SELECT_READ);
   1.177 +	nbytes=read(fid,buf,28);
   1.178 +	test(nbytes==28);
   1.179 +	test(memcmp(buf,checkbuf+100,28)==0);
   1.180 +
   1.181 +		fillbuf(5,checkbuf,128);
   1.182 +		fillbuf(6,checkbuf+128,128);
   1.183 +
   1.184 +	/* Partial read */
   1.185 +
   1.186 +	test_Next("Partial read");
   1.187 +
   1.188 +	select_test(fid);
   1.189 +	test(err==0);
   1.190 +	test(mask==E32SELECT_READ);
   1.191 +	nbytes=read(fid,buf,100);
   1.192 +	test(nbytes==100);
   1.193 +	test(memcmp(buf,checkbuf,100)==0);
   1.194 +
   1.195 +	/* Partial read, completing the write and the following write exactly */
   1.196 +
   1.197 +	test_Next("Partial read across write boundary, completes next write");
   1.198 +
   1.199 +	select_test(fid);
   1.200 +	test(err==0);
   1.201 +	test(mask==E32SELECT_READ);
   1.202 +	nbytes=read(fid,buf,156);
   1.203 +#ifdef PIPES_SUPPORT_BUFFERING
   1.204 +	test(nbytes==156);
   1.205 +#else
   1.206 +	test(nbytes==28);				/* truncated at first write */
   1.207 +
   1.208 +	select_test(fid);
   1.209 +	test(err==0);
   1.210 +	test(mask==E32SELECT_READ);
   1.211 +	nbytes=read(fid,buf+28,128);	/* manually continue the read */
   1.212 +	test(nbytes==128);
   1.213 +#endif
   1.214 +	test(memcmp(buf,checkbuf+100,156)==0);
   1.215 +
   1.216 +		fillbuf(7,checkbuf,128);
   1.217 +		fillbuf(8,checkbuf+128,128);
   1.218 +
   1.219 +	/* Partial read */
   1.220 +
   1.221 +	test_Next("Partial read");
   1.222 +
   1.223 +	select_test(fid);
   1.224 +	test(err==0);
   1.225 +	test(mask==E32SELECT_READ);
   1.226 +	nbytes=read(fid,buf,50);
   1.227 +	test(nbytes==50);
   1.228 +	test(memcmp(buf,checkbuf,50)==0);
   1.229 +
   1.230 +	/* Partial read, starting part way through the write and still not completing it */
   1.231 +
   1.232 +	test_Next("Partial read, starting part way through write");
   1.233 +
   1.234 +	select_test(fid);
   1.235 +	test(err==0);
   1.236 +	test(mask==E32SELECT_READ);
   1.237 +	nbytes=read(fid,buf,50);
   1.238 +	test(nbytes==50);
   1.239 +	test(memcmp(buf,checkbuf+50,50)==0);
   1.240 +
   1.241 +	/* Partial read, completing the 1st write and a partial read on the 2nd write */
   1.242 +
   1.243 +	test_Next("Partial read across write boundary");
   1.244 +
   1.245 +	nbytes=read(fid,buf,50);
   1.246 +#ifdef PIPES_SUPPORT_BUFFERING
   1.247 +	test(nbytes==50);
   1.248 +#else
   1.249 +	test(nbytes==28);				/* truncated at first write */
   1.250 +
   1.251 +	select_test(fid);
   1.252 +	test(err==0);
   1.253 +	test(mask==E32SELECT_READ);
   1.254 +	nbytes=read(fid,buf+28,22);		/* manually continue the read */
   1.255 +	test(nbytes==22);
   1.256 +#endif
   1.257 +	test(memcmp(buf,checkbuf+100,50)==0);
   1.258 +
   1.259 +	/* Partial read, again in the 2nd write */
   1.260 +
   1.261 +	test_Next("Partial read, starting part way through write");
   1.262 +
   1.263 +	select_test(fid);
   1.264 +	test(err==0);
   1.265 +	test(mask==E32SELECT_READ);
   1.266 +	nbytes=read(fid,buf,100);
   1.267 +	test(nbytes==100);
   1.268 +	test(memcmp(buf,checkbuf+150,100)==0);
   1.269 +
   1.270 +	/* Partial read, completing the 2nd write exactly */
   1.271 +
   1.272 +	test_Next("Partial read completing write");
   1.273 +
   1.274 +	select_test(fid);
   1.275 +	test(err==0);
   1.276 +	test(mask==E32SELECT_READ);
   1.277 +	nbytes=read(fid,buf,6);
   1.278 +	test(nbytes==6);
   1.279 +	test(memcmp(buf,checkbuf+250,6)==0);
   1.280 +
   1.281 +	}
   1.282 +
   1.283 +
   1.284 +void do_child()
   1.285 +	{
   1.286 +	producer(2,0);		/* produce on stderr */
   1.287 +	producer(2,1);		/* produce on stderr */
   1.288 +
   1.289 +	consumer(0,0);		/* consume on stdin */
   1.290 +	consumer(0,1);		/* consume on stdin */
   1.291 +
   1.292 +	close(0);
   1.293 +	close(1);
   1.294 +	}
   1.295 +
   1.296 +int fids[3];
   1.297 +
   1.298 +void do_parent()
   1.299 +	{
   1.300 +	consumer(fids[2],0);	/* consume on child stderr */
   1.301 +	consumer(fids[2],1);	/* consume on child stderr */
   1.302 +
   1.303 +	producer(fids[0],0);	/* produce on child stdin */
   1.304 +	producer(fids[0],1);	/* produce on child stdin */
   1.305 +	}
   1.306 +
   1.307 +/* Linked with mcrt0.o, so that the exe starts the CPosixServer automatically as per the
   1.308 + * plan all along.
   1.309 + */
   1.310 +
   1.311 +int main(int argc, char* argv[])
   1.312 +	{
   1.313 +	void* proc2;
   1.314 +
   1.315 +	start_redirection_server();
   1.316 +
   1.317 +	if (argc==1)
   1.318 +		{
   1.319 +		proc2 = create_process(do_child, "CHILD", "re", fids);
   1.320 +		if (proc2)
   1.321 +			start_process(proc2);
   1.322 +		else
   1.323 +			perror("Failed to start processB: ");
   1.324 +
   1.325 +		if (proc2)
   1.326 +			{
   1.327 +			int exit;
   1.328 +			do_parent();
   1.329 +			exit=wait_for_process(proc2);
   1.330 +			printf("wait_for_process returned %d\r\n", exit);
   1.331 +			}
   1.332 +		}
   1.333 +	else
   1.334 +		{
   1.335 +		do_child();
   1.336 +		}
   1.337 +
   1.338 +	return 0;
   1.339 +	}