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