genericopenlibs/cstdlib/TSTLIB/TPIPE2.C
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 void fillbuf(int seed, char* buf, int buflen)
       
    32 	{
       
    33 	int j;
       
    34 	sleep(seed/3);
       
    35 	seed += 'A';
       
    36 	for (j=0; j<buflen; j++)
       
    37 		{
       
    38 		buf[j]=(char)seed;
       
    39 		seed+=13;
       
    40 		if (seed > 127)
       
    41 			seed = seed - 127 + 'A';
       
    42 		}
       
    43 	}
       
    44 
       
    45 /**
       
    46 @SYMTestCaseID          SYSLIB-STDLIB-CT-1078
       
    47 @SYMTestCaseDesc	    Tests for operations on pipes 
       
    48 @SYMTestPriority 	    High
       
    49 @SYMTestActions  	    Tests for writing to an opened pipe and a closed pipe
       
    50 @SYMTestExpectedResults Test must not fail
       
    51 @SYMREQ                 REQ0000
       
    52 */		
       
    53 void producer(int fid, int final)
       
    54 	{
       
    55 	test_Data;
       
    56 	char buf[128];
       
    57 	int nbytes;
       
    58 	int i;
       
    59 	int t_errno;
       
    60 
       
    61 	test_Title("Producer");
       
    62 
       
    63 	for (i=1; i<=8; i++)
       
    64 		{
       
    65 		fillbuf(i,buf,sizeof(buf));
       
    66 		printf("%d",i);
       
    67 		fflush(stdout);
       
    68 		nbytes=write(fid, buf, sizeof(buf));
       
    69 		printf(".");
       
    70 		fflush(stdout);
       
    71 		test(nbytes==sizeof(buf));
       
    72 		}
       
    73 	printf("\n");
       
    74 
       
    75 	if (!final)
       
    76 		return;
       
    77 
       
    78 	test_Next("Writing to closed pipe");
       
    79 
       
    80 	fillbuf(0,buf,sizeof(buf));
       
    81 	close(fid);
       
    82 	nbytes=write(fid, buf, sizeof(buf));
       
    83 	test(nbytes<0);
       
    84 	t_errno=errno;
       
    85 	test(t_errno==ENOENT);
       
    86 	}
       
    87 
       
    88 #define select_test(fid)	\
       
    89 	if (async) { \
       
    90 		mask=E32SELECT_READ+E32SELECT_WRITE; \
       
    91 		err=ioctl(fid,E32IOSELECT,(void*)&mask); \
       
    92 		}
       
    93 
       
    94 /**
       
    95 @SYMTestCaseID          SYSLIB-STDLIB-CT-1079
       
    96 @SYMTestCaseDesc	    Tests for operations on pipes 
       
    97 @SYMTestPriority 	    High
       
    98 @SYMTestActions  	    Tests for reading from the pipe,
       
    99 @SYMTestExpectedResults Test must not fail
       
   100 @SYMREQ                 REQ0000
       
   101 */		
       
   102 void consumer(int fid, int async)
       
   103 	{
       
   104 	test_Data;
       
   105 	char buf[256];
       
   106 	char checkbuf[256];
       
   107 	int nbytes;
       
   108 	int mask=E32SELECT_READ;
       
   109 	int err=0;
       
   110 
       
   111 	if (async)
       
   112 		test_Title("Asynchronous consumer");
       
   113 	else
       
   114 		test_Title("Consumer");
       
   115 
       
   116 		fillbuf(1,checkbuf,128);
       
   117 
       
   118 	/* Simple read, exactly matching single write */
       
   119 
       
   120 	test_Next("Simple read, exactly matching write");
       
   121 
       
   122 	select_test(fid);
       
   123 	test(err==0);
       
   124 	test(mask==E32SELECT_READ);
       
   125 	nbytes=read(fid,buf,128);
       
   126 	test(nbytes==128);
       
   127 	test(memcmp(buf,checkbuf,128)==0);
       
   128 
       
   129 		fillbuf(2,checkbuf,128);
       
   130 		fillbuf(3,checkbuf+128,128);
       
   131 
       
   132 	/* Simple read, exactly matching 2 writes */
       
   133 
       
   134 	test_Next("Simple read, exactly matching 2 writes");
       
   135 
       
   136 	select_test(fid);
       
   137 	test(err==0);
       
   138 	test(mask==E32SELECT_READ);
       
   139 	nbytes=read(fid,buf,256);
       
   140 
       
   141 #ifdef PIPES_SUPPORT_BUFFERING
       
   142 	test(nbytes==256);
       
   143 #else
       
   144 	test(nbytes==128);				/* truncated at first write */
       
   145 
       
   146 	select_test(fid);
       
   147 	test(err==0);
       
   148 	test(mask==E32SELECT_READ);
       
   149 	nbytes=read(fid,buf+128,128);	/* manually continue the read */
       
   150 	test(nbytes==128);
       
   151 #endif
       
   152 	test(memcmp(buf,checkbuf,256)==0);
       
   153 
       
   154 		fillbuf(4,checkbuf,128);
       
   155 
       
   156 	/* Partial read */
       
   157 
       
   158 	test_Next("Partial read");
       
   159 
       
   160 	select_test(fid);
       
   161 	test(err==0);
       
   162 	test(mask==E32SELECT_READ);
       
   163 	nbytes=read(fid,buf,100);
       
   164 	test(nbytes==100);
       
   165 	test(memcmp(buf,checkbuf,100)==0);
       
   166 
       
   167 	/* Partial read, completing the write exactly */
       
   168 
       
   169 	test_Next("Partial read, completes matching write");
       
   170 
       
   171 	select_test(fid);
       
   172 	test(err==0);
       
   173 	test(mask==E32SELECT_READ);
       
   174 	nbytes=read(fid,buf,28);
       
   175 	test(nbytes==28);
       
   176 	test(memcmp(buf,checkbuf+100,28)==0);
       
   177 
       
   178 		fillbuf(5,checkbuf,128);
       
   179 		fillbuf(6,checkbuf+128,128);
       
   180 
       
   181 	/* Partial read */
       
   182 
       
   183 	test_Next("Partial read");
       
   184 
       
   185 	select_test(fid);
       
   186 	test(err==0);
       
   187 	test(mask==E32SELECT_READ);
       
   188 	nbytes=read(fid,buf,100);
       
   189 	test(nbytes==100);
       
   190 	test(memcmp(buf,checkbuf,100)==0);
       
   191 
       
   192 	/* Partial read, completing the write and the following write exactly */
       
   193 
       
   194 	test_Next("Partial read across write boundary, completes next write");
       
   195 
       
   196 	select_test(fid);
       
   197 	test(err==0);
       
   198 	test(mask==E32SELECT_READ);
       
   199 	nbytes=read(fid,buf,156);
       
   200 #ifdef PIPES_SUPPORT_BUFFERING
       
   201 	test(nbytes==156);
       
   202 #else
       
   203 	test(nbytes==28);				/* truncated at first write */
       
   204 
       
   205 	select_test(fid);
       
   206 	test(err==0);
       
   207 	test(mask==E32SELECT_READ);
       
   208 	nbytes=read(fid,buf+28,128);	/* manually continue the read */
       
   209 	test(nbytes==128);
       
   210 #endif
       
   211 	test(memcmp(buf,checkbuf+100,156)==0);
       
   212 
       
   213 		fillbuf(7,checkbuf,128);
       
   214 		fillbuf(8,checkbuf+128,128);
       
   215 
       
   216 	/* Partial read */
       
   217 
       
   218 	test_Next("Partial read");
       
   219 
       
   220 	select_test(fid);
       
   221 	test(err==0);
       
   222 	test(mask==E32SELECT_READ);
       
   223 	nbytes=read(fid,buf,50);
       
   224 	test(nbytes==50);
       
   225 	test(memcmp(buf,checkbuf,50)==0);
       
   226 
       
   227 	/* Partial read, starting part way through the write and still not completing it */
       
   228 
       
   229 	test_Next("Partial read, starting part way through write");
       
   230 
       
   231 	select_test(fid);
       
   232 	test(err==0);
       
   233 	test(mask==E32SELECT_READ);
       
   234 	nbytes=read(fid,buf,50);
       
   235 	test(nbytes==50);
       
   236 	test(memcmp(buf,checkbuf+50,50)==0);
       
   237 
       
   238 	/* Partial read, completing the 1st write and a partial read on the 2nd write */
       
   239 
       
   240 	test_Next("Partial read across write boundary");
       
   241 
       
   242 	nbytes=read(fid,buf,50);
       
   243 #ifdef PIPES_SUPPORT_BUFFERING
       
   244 	test(nbytes==50);
       
   245 #else
       
   246 	test(nbytes==28);				/* truncated at first write */
       
   247 
       
   248 	select_test(fid);
       
   249 	test(err==0);
       
   250 	test(mask==E32SELECT_READ);
       
   251 	nbytes=read(fid,buf+28,22);		/* manually continue the read */
       
   252 	test(nbytes==22);
       
   253 #endif
       
   254 	test(memcmp(buf,checkbuf+100,50)==0);
       
   255 
       
   256 	/* Partial read, again in the 2nd write */
       
   257 
       
   258 	test_Next("Partial read, starting part way through write");
       
   259 
       
   260 	select_test(fid);
       
   261 	test(err==0);
       
   262 	test(mask==E32SELECT_READ);
       
   263 	nbytes=read(fid,buf,100);
       
   264 	test(nbytes==100);
       
   265 	test(memcmp(buf,checkbuf+150,100)==0);
       
   266 
       
   267 	/* Partial read, completing the 2nd write exactly */
       
   268 
       
   269 	test_Next("Partial read completing write");
       
   270 
       
   271 	select_test(fid);
       
   272 	test(err==0);
       
   273 	test(mask==E32SELECT_READ);
       
   274 	nbytes=read(fid,buf,6);
       
   275 	test(nbytes==6);
       
   276 	test(memcmp(buf,checkbuf+250,6)==0);
       
   277 
       
   278 	}
       
   279 
       
   280 
       
   281 void do_child()
       
   282 	{
       
   283 	producer(2,0);		/* produce on stderr */
       
   284 	producer(2,1);		/* produce on stderr */
       
   285 
       
   286 	consumer(0,0);		/* consume on stdin */
       
   287 	consumer(0,1);		/* consume on stdin */
       
   288 
       
   289 	close(0);
       
   290 	close(1);
       
   291 	}
       
   292 
       
   293 int fids[3];
       
   294 
       
   295 void do_parent()
       
   296 	{
       
   297 	consumer(fids[2],0);	/* consume on child stderr */
       
   298 	consumer(fids[2],1);	/* consume on child stderr */
       
   299 
       
   300 	producer(fids[0],0);	/* produce on child stdin */
       
   301 	producer(fids[0],1);	/* produce on child stdin */
       
   302 	}
       
   303 
       
   304 /* Linked with mcrt0.o, so that the exe starts the CPosixServer automatically as per the
       
   305  * plan all along.
       
   306  */
       
   307 
       
   308 int main(int argc, char* argv[])
       
   309 	{
       
   310 	void* proc2;
       
   311 
       
   312 	start_redirection_server();
       
   313 
       
   314 	if (argc==1)
       
   315 		{
       
   316 		proc2 = create_process(do_child, "CHILD", "re", fids);
       
   317 		if (proc2)
       
   318 			start_process(proc2);
       
   319 		else
       
   320 			perror("Failed to start processB: ");
       
   321 
       
   322 		if (proc2)
       
   323 			{
       
   324 			int exit;
       
   325 			do_parent();
       
   326 			exit=wait_for_process(proc2);
       
   327 			printf("wait_for_process returned %d\r\n", exit);
       
   328 			}
       
   329 		}
       
   330 	else
       
   331 		{
       
   332 		do_child();
       
   333 		}
       
   334 
       
   335 	return 0;
       
   336 	}