31
|
1 |
/*
|
|
2 |
* Copyright (c) 2002-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 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
#include "tselect.h"
|
|
21 |
|
|
22 |
// CONSTANTS
|
|
23 |
const TUint KTestPort = 2080;
|
|
24 |
|
|
25 |
TInt CTestSelect::TestTimeout()
|
|
26 |
{
|
|
27 |
|
|
28 |
// Create temporary variables in stack
|
|
29 |
TInt timesecs;
|
|
30 |
TInt timemicrosecs;
|
|
31 |
|
|
32 |
timemicrosecs = 0;
|
|
33 |
timesecs = 10;
|
|
34 |
|
|
35 |
struct timeval tv;
|
|
36 |
tv.tv_sec = timesecs;
|
|
37 |
tv.tv_usec = timemicrosecs;
|
|
38 |
|
|
39 |
time_t time1,time2;
|
|
40 |
int err = time(&time1);
|
|
41 |
|
|
42 |
int fd = select(1, NULL, NULL,NULL, &tv);
|
|
43 |
err = time(&time2);
|
|
44 |
if ( ((time2 - time1) >= timesecs) && fd != -1 )
|
|
45 |
{
|
|
46 |
return KErrNone;
|
|
47 |
}
|
|
48 |
else
|
|
49 |
{
|
|
50 |
return KErrGeneral;
|
|
51 |
}
|
|
52 |
}
|
|
53 |
|
|
54 |
|
|
55 |
TInt CTestSelect::TestReadFileDesc()
|
|
56 |
{
|
|
57 |
|
|
58 |
TInt timesecs = 5;
|
|
59 |
TInt timemicrosecs = 0;
|
|
60 |
|
|
61 |
struct timeval tv;
|
|
62 |
tv.tv_sec = timesecs;
|
|
63 |
tv.tv_usec = timemicrosecs;
|
|
64 |
|
|
65 |
// read only
|
|
66 |
int fd = open("c:\\tselect.cfg", O_RDONLY|O_CREAT);
|
|
67 |
fd_set readfds;
|
|
68 |
FD_ZERO(&readfds);
|
|
69 |
FD_SET(fd, &readfds);
|
|
70 |
int maxfd = fd + 1;
|
|
71 |
int ret = select(maxfd, &readfds, NULL, NULL,&tv);
|
|
72 |
if ( ret != -1 && FD_ISSET(fd, &readfds))
|
|
73 |
{
|
|
74 |
_LIT(KMsg1, "Read on file fd Test Passed");
|
|
75 |
INFO_PRINTF1(KMsg1);
|
|
76 |
return KErrNone;
|
|
77 |
}
|
|
78 |
else
|
|
79 |
{
|
|
80 |
_LIT(KMsg1, "Read on file fd Test Failed");
|
|
81 |
INFO_PRINTF1(KMsg1);
|
|
82 |
return KErrGeneral;
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
TInt CTestSelect::TestWriteFileDesc()
|
|
87 |
{
|
|
88 |
|
|
89 |
TInt timesecs = 2;
|
|
90 |
TInt timemicrosecs = 0;
|
|
91 |
|
|
92 |
struct timeval tv;
|
|
93 |
tv.tv_sec = timesecs;
|
|
94 |
tv.tv_usec = timemicrosecs;
|
|
95 |
|
|
96 |
// write check
|
|
97 |
int fd = open("c:\\tselect.cfg", O_RDONLY);
|
|
98 |
fd_set writefds;
|
|
99 |
FD_ZERO(&writefds);
|
|
100 |
FD_SET(fd, &writefds);
|
|
101 |
int maxfd = fd + 1;
|
|
102 |
int ret = select(maxfd, NULL,&writefds, NULL,&tv);
|
|
103 |
if ( ret != -1 && FD_ISSET(fd, &writefds))
|
|
104 |
{
|
|
105 |
_LIT(KMsg1, "Write on file fd Test Passed");
|
|
106 |
INFO_PRINTF1(KMsg1);
|
|
107 |
return KErrNone;
|
|
108 |
}
|
|
109 |
else
|
|
110 |
{
|
|
111 |
_LIT(KMsg1, "Write on file fd Test failed");
|
|
112 |
INFO_PRINTF1(KMsg1);
|
|
113 |
return KErrGeneral;
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
TInt CTestSelect::TestReadWriteFileDesc()
|
|
118 |
{
|
|
119 |
// both read and write
|
|
120 |
struct timeval tv;
|
|
121 |
tv.tv_sec = 0;
|
|
122 |
tv.tv_usec = 0;
|
|
123 |
|
|
124 |
int fd = open("c:\\tselect.cfg", O_RDONLY);
|
|
125 |
fd_set readfds;
|
|
126 |
FD_ZERO(&readfds);
|
|
127 |
FD_SET(fd, &readfds);
|
|
128 |
fd_set writefds;
|
|
129 |
FD_ZERO(&writefds);
|
|
130 |
FD_SET(fd, &writefds);
|
|
131 |
int maxfd = fd + 1;
|
|
132 |
int ret = select(maxfd, &readfds, &writefds, NULL,&tv);
|
|
133 |
if ( ret != -1 && FD_ISSET(fd, &readfds) && FD_ISSET(fd, &writefds) )
|
|
134 |
{
|
|
135 |
_LIT(KMsg1, "R/W on file fd Test passed");
|
|
136 |
INFO_PRINTF1(KMsg1);
|
|
137 |
return KErrNone;
|
|
138 |
}
|
|
139 |
else
|
|
140 |
{
|
|
141 |
_LIT(KMsg1, "R/W on file fd Test failed");
|
|
142 |
INFO_PRINTF1(KMsg1);
|
|
143 |
return KErrGeneral;
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
TInt CTestSelect::TestPipeReadDesc()
|
|
150 |
{
|
|
151 |
|
|
152 |
int fd[2];
|
|
153 |
INFO_PRINTF1(_L("Before Pipe "));
|
|
154 |
if ( pipe(fd) < 0 )
|
|
155 |
{
|
|
156 |
return -1;
|
|
157 |
}
|
|
158 |
|
|
159 |
char a = 's';
|
|
160 |
write(fd[1],&a,1);
|
|
161 |
fd_set readfds;
|
|
162 |
FD_ZERO(&readfds);
|
|
163 |
FD_SET(fd[0], &readfds);
|
|
164 |
int ret = select(fd[0] +1, &readfds, NULL, NULL, NULL );
|
|
165 |
if ( ret != -1 && FD_ISSET(fd[0], &readfds))
|
|
166 |
{
|
|
167 |
_LIT(KMsg1, "Read on pipe fd Test passed");
|
|
168 |
INFO_PRINTF1(KMsg1);
|
|
169 |
ret = KErrNone;
|
|
170 |
}
|
|
171 |
else
|
|
172 |
{
|
|
173 |
_LIT(KMsg1, "Read on pipe fd Test failed");
|
|
174 |
INFO_PRINTF1(KMsg1);
|
|
175 |
ret = KErrGeneral;
|
|
176 |
}
|
|
177 |
close(fd[0]);
|
|
178 |
close(fd[1]);
|
|
179 |
return ret;
|
|
180 |
|
|
181 |
}
|
|
182 |
|
|
183 |
//Test case added to check when write end of a pipe is closed
|
|
184 |
TInt CTestSelect::TestPipeReadDesc1()
|
|
185 |
{
|
|
186 |
int fd[2];
|
|
187 |
INFO_PRINTF1(_L("Before Pipe "));
|
|
188 |
if ( pipe(fd) < 0 )
|
|
189 |
{
|
|
190 |
return -1;
|
|
191 |
}
|
|
192 |
|
|
193 |
char a = 's';
|
|
194 |
write(fd[1],&a,0);
|
|
195 |
fd_set readfds,exceptfds;
|
|
196 |
FD_ZERO(&readfds);
|
|
197 |
FD_ZERO(&exceptfds);
|
|
198 |
FD_SET(fd[0], &readfds);
|
|
199 |
FD_SET(fd[0], &exceptfds);
|
|
200 |
FD_SET(fd[1], &exceptfds);
|
|
201 |
close(fd[1]);
|
|
202 |
int ret = select(fd[0] +1, &readfds, NULL, &exceptfds, NULL );
|
|
203 |
if ( ret != -1 && FD_ISSET(fd[0], &exceptfds))
|
|
204 |
{
|
|
205 |
_LIT(KMsg1, "Read on pipe fd Test passed and passed to set EAnyException on write end of a pipe being closed\n");
|
|
206 |
INFO_PRINTF1(KMsg1);
|
|
207 |
ret = KErrNone;
|
|
208 |
}
|
|
209 |
else
|
|
210 |
{
|
|
211 |
_LIT(KMsg1, "Failed to set EAnyException on write end being closed\n");
|
|
212 |
INFO_PRINTF1(KMsg1);
|
|
213 |
ret = KErrGeneral;
|
|
214 |
}
|
|
215 |
close(fd[0]);
|
|
216 |
return ret;
|
|
217 |
}
|
|
218 |
|
|
219 |
|
|
220 |
TInt CTestSelect::TestPipeWriteDesc()
|
|
221 |
{
|
|
222 |
int fd[2];
|
|
223 |
INFO_PRINTF1(_L("Before Pipe "));
|
|
224 |
if ( pipe(fd) < 0 )
|
|
225 |
{
|
|
226 |
return -1;
|
|
227 |
}
|
|
228 |
|
|
229 |
INFO_PRINTF1(_L("Pipe Created"));
|
|
230 |
fd_set writefds;
|
|
231 |
FD_ZERO(&writefds);
|
|
232 |
FD_SET(fd[1], &writefds);
|
|
233 |
int ret = select(fd[1] +1, NULL, &writefds, NULL, NULL );
|
|
234 |
iSelectflag = 1;
|
|
235 |
|
|
236 |
if ( ret != -1 && FD_ISSET(fd[1], &writefds))
|
|
237 |
{
|
|
238 |
_LIT(KMsg1, "Write on pipe fd Test passed");
|
|
239 |
INFO_PRINTF1(KMsg1);
|
|
240 |
ret = KErrNone;
|
|
241 |
}
|
|
242 |
else
|
|
243 |
{
|
|
244 |
_LIT(KMsg1, "Write on pipe fd Test failed");
|
|
245 |
INFO_PRINTF1(KMsg1);
|
|
246 |
ret = KErrGeneral;
|
|
247 |
}
|
|
248 |
|
|
249 |
close(fd[0]);
|
|
250 |
close(fd[1]);
|
|
251 |
|
|
252 |
return ret;
|
|
253 |
}
|
|
254 |
|
|
255 |
void* CTestSelect::ServerThread(TAny* ptr)
|
|
256 |
{
|
|
257 |
CTestSelect* thisptr = (CTestSelect*)ptr;
|
|
258 |
int sock_fd,newsock_fd;
|
|
259 |
unsigned int addr_len;
|
|
260 |
sockaddr_in serv_addr,new_socket;
|
|
261 |
|
|
262 |
sock_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
|
|
263 |
if (sock_fd < 0)
|
|
264 |
{
|
|
265 |
return NULL;//KErrGeneral;
|
|
266 |
}
|
|
267 |
|
|
268 |
serv_addr.sin_family = AF_INET;
|
|
269 |
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
270 |
serv_addr.sin_port = htons(KTestPort);
|
|
271 |
|
|
272 |
if (bind(sock_fd,(sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
|
|
273 |
{
|
|
274 |
shutdown(sock_fd,SHUT_RDWR); // 2 -> SHUT_RDWT
|
|
275 |
close(sock_fd);
|
|
276 |
return NULL;//KErrBind;
|
|
277 |
}
|
|
278 |
|
|
279 |
if (listen(sock_fd,1) < 0)
|
|
280 |
{
|
|
281 |
shutdown(sock_fd,SHUT_RDWR); // 2 -> SHUT_RDWT
|
|
282 |
close(sock_fd);
|
|
283 |
return NULL;//KErrListen;
|
|
284 |
}
|
|
285 |
|
|
286 |
addr_len = sizeof(new_socket);
|
|
287 |
thisptr->iAcceptflag = 1;
|
|
288 |
|
|
289 |
newsock_fd = accept(sock_fd,(sockaddr*)&new_socket,&addr_len); // Code blocks here
|
|
290 |
char a = 'S';
|
|
291 |
write(newsock_fd, &a, 1);
|
|
292 |
thisptr->iWriteflag = 1;
|
|
293 |
|
|
294 |
while(!thisptr->iSelectflag)
|
|
295 |
{
|
|
296 |
usleep(10000);
|
|
297 |
}
|
|
298 |
close(sock_fd);
|
|
299 |
close(newsock_fd);
|
|
300 |
return NULL;//KErrNone;
|
|
301 |
}
|
|
302 |
|
|
303 |
TInt CTestSelect::TestSocketDesc()
|
|
304 |
{
|
|
305 |
struct timeval tv;
|
|
306 |
struct sockaddr_in testaddr;
|
|
307 |
tv.tv_sec = 1;
|
|
308 |
tv.tv_usec = 0;
|
|
309 |
memset( &testaddr,0, sizeof( testaddr));
|
|
310 |
// now connect to the server
|
|
311 |
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
312 |
INFO_PRINTF1(_L("socket in client called"));
|
|
313 |
|
|
314 |
pthread_t testThread;
|
|
315 |
pthread_create(&testThread, NULL, &ServerThread, (void*)this);
|
|
316 |
|
|
317 |
testaddr.sin_family = AF_INET;
|
|
318 |
testaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
319 |
testaddr.sin_port = htons(KTestPort);
|
|
320 |
while ( !iAcceptflag )
|
|
321 |
{
|
|
322 |
usleep(10000);
|
|
323 |
}
|
|
324 |
|
|
325 |
INFO_PRINTF1(_L("connect called"));
|
|
326 |
|
|
327 |
TInt err=connect(fd,(struct sockaddr*)&testaddr, sizeof(testaddr));
|
|
328 |
|
|
329 |
fd_set readfds,writefds;
|
|
330 |
FD_ZERO(&readfds);
|
|
331 |
FD_ZERO(&writefds);
|
|
332 |
FD_SET(fd, &readfds);
|
|
333 |
FD_SET(fd, &writefds);
|
|
334 |
int maxfd = fd + 1;
|
|
335 |
while ( !iWriteflag )
|
|
336 |
{
|
|
337 |
usleep(10000);
|
|
338 |
}
|
|
339 |
|
|
340 |
INFO_PRINTF1(_L("before select "));
|
|
341 |
|
|
342 |
int ret = select(maxfd, &readfds, NULL, NULL,&tv);
|
|
343 |
|
|
344 |
INFO_PRINTF1(_L("after select"));
|
|
345 |
iSelectflag = 1;
|
|
346 |
if ( ret != -1 && FD_ISSET(fd, &readfds))
|
|
347 |
{
|
|
348 |
ret = KErrNone;
|
|
349 |
}
|
|
350 |
else
|
|
351 |
{
|
|
352 |
ret = KErrGeneral;
|
|
353 |
}
|
|
354 |
|
|
355 |
close(fd);
|
|
356 |
int *p;
|
|
357 |
pthread_join(testThread,(void**)&p);
|
|
358 |
return ret;
|
|
359 |
}
|
|
360 |
|
|
361 |
//Test case added for select not hanging for zero timeout- DEF122924
|
|
362 |
TInt CTestSelect::TestSocketDesc2()
|
|
363 |
{
|
|
364 |
struct timeval tv;
|
|
365 |
tv.tv_sec = 0;
|
|
366 |
tv.tv_usec = 0;
|
|
367 |
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
368 |
INFO_PRINTF1(_L("socket in client called"));
|
|
369 |
|
|
370 |
fd_set readfds,writefds,errfds;
|
|
371 |
FD_ZERO(&readfds);
|
|
372 |
FD_ZERO(&writefds);
|
|
373 |
FD_ZERO(&errfds);
|
|
374 |
FD_SET(fd, &readfds);
|
|
375 |
int maxfd = fd + 1;
|
|
376 |
INFO_PRINTF1(_L("before select "));
|
|
377 |
int ret = select(maxfd, &readfds, &writefds, &errfds,&tv);
|
|
378 |
INFO_PRINTF1(_L("select passes, does not hang."));
|
|
379 |
close(fd);
|
|
380 |
return ret;
|
|
381 |
}
|
|
382 |
|
|
383 |
int FlgReadWrite = 0;
|
|
384 |
int FlgRead = 0;
|
|
385 |
#define ReadWriteErr2 7
|
|
386 |
#define ReadWriteErr1 6
|
|
387 |
#define Write2Err 5
|
|
388 |
#define Write1Err 4
|
|
389 |
#define Read1Err 3
|
|
390 |
#define PipeReadDesc 8
|
|
391 |
#define PipeWriteDesc 9
|
|
392 |
|
|
393 |
|
|
394 |
void* CreateConnection(void* /*aParam*/)
|
|
395 |
{
|
|
396 |
// Only make a write on the sock_fd
|
|
397 |
int ret;
|
|
398 |
struct sockaddr_in host;
|
|
399 |
int dest_fd;
|
|
400 |
char buf[50];
|
|
401 |
host.sin_port = htons(5002);
|
|
402 |
host.sin_family = AF_INET;
|
|
403 |
host.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
404 |
dest_fd = socket(AF_INET,SOCK_STREAM,0);
|
|
405 |
ret = connect(dest_fd,(struct sockaddr*)&host,sizeof(host));
|
|
406 |
ret = read(dest_fd,buf,sizeof(buf));
|
|
407 |
ret = write(dest_fd,buf,ret);
|
|
408 |
close(dest_fd);
|
|
409 |
return NULL;
|
|
410 |
}
|
|
411 |
|
|
412 |
void* SocketReadWrite(void* aSock)
|
|
413 |
{
|
|
414 |
// Note, here "aSock" is being treated as an array of FDs.
|
|
415 |
// The FD at the 0th index is the write-fd, and the FD at
|
|
416 |
// the 1st index is the read-fd.
|
|
417 |
int sock_fd = *((int*)aSock);
|
|
418 |
fd_set fds;
|
|
419 |
int ret;
|
|
420 |
char buf[] = "Write from third thread";
|
|
421 |
char buf2[50];
|
|
422 |
|
|
423 |
struct timeval timeout;
|
|
424 |
timeout.tv_sec = 10;
|
|
425 |
timeout.tv_usec = 0;
|
|
426 |
|
|
427 |
// Setting the write-fd.
|
|
428 |
FD_ZERO(&fds);
|
|
429 |
FD_SET(sock_fd,&fds);
|
|
430 |
|
|
431 |
fd_set readfds;
|
|
432 |
FD_ZERO(&readfds);
|
|
433 |
//FD_SET(((int*)aSock)[0],&readfds);
|
|
434 |
FD_SET(((int*)aSock)[1],&readfds);
|
|
435 |
FD_SET(((int*)aSock)[PipeReadDesc],&readfds);
|
|
436 |
|
|
437 |
((int*)aSock)[ReadWriteErr1] = 0;
|
|
438 |
((int*)aSock)[ReadWriteErr2] = 0;
|
|
439 |
|
|
440 |
//while(FlgReadWrite == 0)
|
|
441 |
//while(FlgRead == 0)
|
|
442 |
{
|
|
443 |
}
|
|
444 |
|
|
445 |
ret = select(((int*)aSock)[PipeReadDesc] + 1,&readfds,NULL,NULL,&timeout);
|
|
446 |
//ret = select(((int*)aSock)[PipeReadDesc] + 1,&readfds,&fds,NULL,&timeout);
|
|
447 |
//ret = select(((int*)aSock)[1] + 1,&readfds,NULL,NULL,&timeout);
|
|
448 |
#if 0
|
|
449 |
if (ret && FD_ISSET(sock_fd,&fds))
|
|
450 |
{
|
|
451 |
ret = write(sock_fd,buf,sizeof(buf));
|
|
452 |
fprintf( (FILE*)(((int*)aSock)[2]), "WRITING SocketReadWrite END - have written = %d and %s\n",ret,buf);
|
|
453 |
if (ret < 0)
|
|
454 |
((int*)aSock)[ReadWriteErr1] = ret;
|
|
455 |
}
|
|
456 |
#endif
|
|
457 |
if(ret && FD_ISSET(((int*)aSock)[1],&readfds))
|
|
458 |
{
|
|
459 |
ret = read(((int*)aSock)[1],buf2,sizeof(buf2));
|
|
460 |
fprintf( (FILE*)(((int*)aSock)[2]), "READING SocketReadWrite END - have read = %d and %s\n",ret,buf2);
|
|
461 |
if(ret < 0)
|
|
462 |
((int*)aSock)[ReadWriteErr2] = ret;
|
|
463 |
}
|
|
464 |
|
|
465 |
if(ret && FD_ISSET(((int*)aSock)[PipeReadDesc],&readfds))
|
|
466 |
{
|
|
467 |
ret = read(((int*)aSock)[PipeReadDesc],buf,sizeof(buf));
|
|
468 |
fprintf( (FILE*)(((int*)aSock)[2]), "READING SocketReadWrite END - have read = %d and %s\n",ret,buf);
|
|
469 |
if(ret < 0)
|
|
470 |
((int*)aSock)[ReadWriteErr2] = ret;
|
|
471 |
}
|
|
472 |
|
|
473 |
if(ret == -1)
|
|
474 |
{
|
|
475 |
fprintf( (FILE*)(((int*)aSock)[2]), "WRITING SocketReadWrite ERROR END - have written = %d \n",ret);
|
|
476 |
((int*)aSock)[ReadWriteErr1] = ret;
|
|
477 |
}
|
|
478 |
return aSock;
|
|
479 |
}
|
|
480 |
|
|
481 |
void* SocketWrite1(void* aSock)
|
|
482 |
{
|
|
483 |
int *sock_fd = (int*)aSock;
|
|
484 |
((int*)aSock)[Write1Err] = KErrNone;
|
|
485 |
|
|
486 |
char buf[] = "Write from first thread";
|
|
487 |
int ret = 0;
|
|
488 |
// sleep(3);
|
|
489 |
ret = write(*sock_fd,buf,sizeof(buf));
|
|
490 |
FlgRead = 1;
|
|
491 |
if (ret < 0)
|
|
492 |
((int*)aSock)[Write1Err] = ret;
|
|
493 |
else
|
|
494 |
((int*)aSock)[Write1Err] = KErrNone;
|
|
495 |
|
|
496 |
return aSock;
|
|
497 |
}
|
|
498 |
|
|
499 |
void* SocketWrite2(void* aSock)
|
|
500 |
{
|
|
501 |
// int *sock_fd = (int*)aSock;
|
|
502 |
((int*)aSock)[Write2Err] = KErrNone;
|
|
503 |
|
|
504 |
char buf[] = "Write from second thread";
|
|
505 |
// sleep(2);
|
|
506 |
int ret = write(((int*)aSock)[1],buf,sizeof(buf));
|
|
507 |
FlgReadWrite = 1;
|
|
508 |
if (ret < 0)
|
|
509 |
((int*)aSock)[Write2Err] = ret;
|
|
510 |
else
|
|
511 |
((int*)aSock)[Write2Err] = KErrNone;
|
|
512 |
|
|
513 |
return aSock;
|
|
514 |
}
|
|
515 |
|
|
516 |
void* PipeWrite(void* aPipe)
|
|
517 |
{
|
|
518 |
//int *pipe_fd = (int*)aPipe;
|
|
519 |
char buf[] = "Pipe Write";
|
|
520 |
sleep(1);
|
|
521 |
int ret = write(((int*)aPipe)[PipeWriteDesc],buf,sizeof(buf));
|
|
522 |
FlgReadWrite = 1;
|
|
523 |
if (ret < 0)
|
|
524 |
((int*)aPipe)[Write2Err] = ret;
|
|
525 |
else
|
|
526 |
((int*)aPipe)[Write2Err] = KErrNone;
|
|
527 |
return aPipe;
|
|
528 |
}
|
|
529 |
|
|
530 |
void* SocketRead1(void* aSock)
|
|
531 |
{
|
|
532 |
int sock_fd = *((int*)aSock);
|
|
533 |
fd_set fds;
|
|
534 |
char buf[50];
|
|
535 |
int ret;
|
|
536 |
|
|
537 |
struct timeval timeout;
|
|
538 |
timeout.tv_sec = 10;
|
|
539 |
timeout.tv_usec = 0;
|
|
540 |
|
|
541 |
((int*)aSock)[Read1Err] = KErrNone;
|
|
542 |
FD_ZERO(&fds);
|
|
543 |
FD_SET(sock_fd,&fds);
|
|
544 |
|
|
545 |
//while(FlgRead == 0)
|
|
546 |
{
|
|
547 |
}
|
|
548 |
|
|
549 |
ret = select(sock_fd+1,&fds,NULL,NULL,&timeout);
|
|
550 |
|
|
551 |
if (ret && FD_ISSET(sock_fd,&fds))
|
|
552 |
{
|
|
553 |
ret = read(sock_fd,buf,sizeof(buf));
|
|
554 |
if(ret <= 0)
|
|
555 |
{
|
|
556 |
((int*)aSock)[Read1Err] = ret;
|
|
557 |
fprintf( (FILE*)(((int*)aSock)[2]), "READING SocketRead END - have read = %d \n",ret);
|
|
558 |
}
|
|
559 |
else
|
|
560 |
{
|
|
561 |
fprintf( (FILE*)(((int*)aSock)[2]), "READING SocketRead END - have read = %d and %s\n",ret,buf);
|
|
562 |
}
|
|
563 |
}
|
|
564 |
else if(ret == -1)
|
|
565 |
{
|
|
566 |
fprintf( (FILE*)(((int*)aSock)[2]), "READING SocketRead ERROR END - have read = %d \n",ret);
|
|
567 |
((int*)aSock)[Read1Err] = ret;
|
|
568 |
}
|
|
569 |
return aSock;
|
|
570 |
}
|
|
571 |
|
|
572 |
TInt CTestSelect::TestSocketDesc3()
|
|
573 |
{
|
|
574 |
// __UHEAP_MARK;
|
|
575 |
int ret;
|
|
576 |
char buf[50];
|
|
577 |
fd_set fds;
|
|
578 |
FD_ZERO(&fds);
|
|
579 |
int sock_fds[10];
|
|
580 |
int listen_sock = socket(AF_INET,SOCK_STREAM,0);
|
|
581 |
struct sockaddr_in host,dest;
|
|
582 |
pthread_t client1, client2, sockwrite1, sockwrite2, sockread1, pipewrite, sockreadwrite;
|
|
583 |
int err = KErrNone;
|
|
584 |
|
|
585 |
struct timeval timeout;
|
|
586 |
timeout.tv_sec = 10;
|
|
587 |
timeout.tv_usec = 0;
|
|
588 |
|
|
589 |
FILE* outFile = fopen("c:\\logs\\TestSocketDesc3.txt", "w+");
|
|
590 |
sock_fds[2] = (int)outFile;
|
|
591 |
|
|
592 |
host.sin_port = htons(5002);
|
|
593 |
host.sin_family = AF_INET;
|
|
594 |
host.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
595 |
ret = bind(listen_sock,(struct sockaddr*)&host,sizeof(host));
|
|
596 |
if(ret<0)
|
|
597 |
{
|
|
598 |
INFO_PRINTF1(_L("Error in bind\n"));
|
|
599 |
return KErrGeneral ;
|
|
600 |
}
|
|
601 |
ret = listen(listen_sock,1);
|
|
602 |
if(ret<0)
|
|
603 |
{
|
|
604 |
INFO_PRINTF1(_L("Error in listen\n"));
|
|
605 |
return KErrGeneral ;
|
|
606 |
}
|
|
607 |
unsigned size = sizeof(host);
|
|
608 |
|
|
609 |
/*************************** First Client Connection ***************************/
|
|
610 |
ret = pthread_create(&client1,(pthread_attr_t*)NULL,&CreateConnection,NULL);
|
|
611 |
if(ret<0)
|
|
612 |
{
|
|
613 |
INFO_PRINTF2(_L("Error creating thread for client1:%d\n"),errno);
|
|
614 |
return KErrGeneral ;
|
|
615 |
}
|
|
616 |
// This socket monitored by 4 threads - two for read, and two for write.
|
|
617 |
sock_fds[0] = accept(listen_sock,(struct sockaddr*)&dest,&size);
|
|
618 |
/********************************************************************************/
|
|
619 |
|
|
620 |
/*************************** Second Client Connection ***************************/
|
|
621 |
ret = pthread_create(&client2,(pthread_attr_t*)NULL,&CreateConnection,NULL);
|
|
622 |
if(ret<0)
|
|
623 |
{
|
|
624 |
INFO_PRINTF2(_L("Error creating thread for client2:%d\n"),errno);
|
|
625 |
return KErrGeneral ;
|
|
626 |
}
|
|
627 |
// This socket monitored by 2 threads - one for read, and one for write.
|
|
628 |
sock_fds[1] = accept(listen_sock,(struct sockaddr*)&dest,&size);
|
|
629 |
/********************************************************************************/
|
|
630 |
|
|
631 |
/*************************** Pipe Creation and Writing **************************/
|
|
632 |
int fd[2];
|
|
633 |
//char buf1[50]= "Write from main thread";
|
|
634 |
INFO_PRINTF1(_L("Before Pipe "));
|
|
635 |
if ( pipe(fd) < 0 )
|
|
636 |
{
|
|
637 |
return KErrGeneral;
|
|
638 |
}
|
|
639 |
// write(fd[1],buf1,sizeof(buf1));
|
|
640 |
sock_fds[PipeReadDesc] = fd[0];
|
|
641 |
sock_fds[PipeWriteDesc]= fd[1];
|
|
642 |
/********************************************************************************/
|
|
643 |
|
|
644 |
// Creating the 1st Read thread.
|
|
645 |
ret = pthread_create(&sockread1,(pthread_attr_t*)NULL,&SocketRead1,sock_fds);
|
|
646 |
|
|
647 |
// sleep(3);
|
|
648 |
// Creating the Read/Write thread.
|
|
649 |
ret = pthread_create(&sockreadwrite,(pthread_attr_t*)NULL,&SocketReadWrite,sock_fds);
|
|
650 |
|
|
651 |
// Creating the 1st Write thread.
|
|
652 |
ret = pthread_create(&sockwrite1,(pthread_attr_t*)NULL,&SocketWrite1,sock_fds);
|
|
653 |
if(ret<0)
|
|
654 |
{
|
|
655 |
INFO_PRINTF2(_L("Error creating thread for Write1:%d\n"),errno);
|
|
656 |
return KErrGeneral ;
|
|
657 |
}
|
|
658 |
|
|
659 |
// Creating the 2nd Write thread.
|
|
660 |
ret = pthread_create(&sockwrite2,(pthread_attr_t*)NULL,&SocketWrite2,sock_fds);
|
|
661 |
|
|
662 |
// Creating the 3rd Write thread.
|
|
663 |
ret = pthread_create(&pipewrite,(pthread_attr_t*)NULL,&PipeWrite,sock_fds);
|
|
664 |
|
|
665 |
// The 2nd Read thread.
|
|
666 |
FD_SET(sock_fds[0],&fds);
|
|
667 |
|
|
668 |
ret = select(sock_fds[0]+1,&fds,NULL,NULL,&timeout);
|
|
669 |
if (ret && FD_ISSET(sock_fds[0],&fds))
|
|
670 |
{
|
|
671 |
ret = read(sock_fds[0],buf,sizeof(buf));
|
|
672 |
if(ret > 0)
|
|
673 |
{
|
|
674 |
fprintf(outFile,"READING socket main END - have read = %d and %s\n",ret,buf);
|
|
675 |
//INFO_PRINTF3(_L("READING socket main END - have read = %d and %s\n"),ret,buf);
|
|
676 |
}
|
|
677 |
else
|
|
678 |
{
|
|
679 |
fprintf(outFile,"READING socket main END - have read = %d \n",ret);
|
|
680 |
//INFO_PRINTF2(_L("READING socket main END - have read = %d \n"),ret);
|
|
681 |
}
|
|
682 |
}
|
|
683 |
else
|
|
684 |
{
|
|
685 |
fprintf(outFile,"READING socket main ERROR END - have read = %d \n",ret);
|
|
686 |
//INFO_PRINTF2(_L("READING socket main ERROR END - have read = %d \n"),ret);
|
|
687 |
}
|
|
688 |
|
|
689 |
pthread_join(sockwrite1,(void**)&sock_fds);
|
|
690 |
if(KErrNone != sock_fds[Write1Err])
|
|
691 |
{
|
|
692 |
err = KErrGeneral ;
|
|
693 |
}
|
|
694 |
pthread_join(sockread1,(void**)&sock_fds);
|
|
695 |
if(KErrNone != sock_fds[Read1Err] && KErrNone == err)
|
|
696 |
{
|
|
697 |
err = KErrGeneral ;
|
|
698 |
}
|
|
699 |
pthread_join(client1,(void**)NULL);
|
|
700 |
pthread_join(sockreadwrite,(void**)&sock_fds);
|
|
701 |
if((KErrNone != sock_fds[ReadWriteErr1] || KErrNone != sock_fds[ReadWriteErr2]) && KErrNone == err)
|
|
702 |
{
|
|
703 |
err = KErrGeneral ;
|
|
704 |
}
|
|
705 |
pthread_join(sockwrite2,(void**)&sock_fds);
|
|
706 |
if(KErrNone != sock_fds[Write2Err] && KErrNone == err)
|
|
707 |
{
|
|
708 |
err = KErrGeneral ;
|
|
709 |
}
|
|
710 |
pthread_join(client2,(void**)NULL);
|
|
711 |
pthread_join(pipewrite,(void**)&sock_fds);
|
|
712 |
if(KErrNone != sock_fds[Write2Err] && KErrNone == err)
|
|
713 |
{
|
|
714 |
err = KErrGeneral ;
|
|
715 |
}
|
|
716 |
|
|
717 |
close(sock_fds[0]);
|
|
718 |
close(sock_fds[1]);
|
|
719 |
fclose(outFile);
|
|
720 |
close(fd[0]);
|
|
721 |
close(fd[1]);
|
|
722 |
close(listen_sock);
|
|
723 |
// __UHEAP_MARKEND;
|
|
724 |
return err;
|
|
725 |
}
|
|
726 |
|
|
727 |
TInt CTestSelect::TestMultipleDesc()
|
|
728 |
{
|
|
729 |
|
|
730 |
return KErrNone;
|
|
731 |
|
|
732 |
}
|
|
733 |
|
|
734 |
TInt CTestSelect::TestConnectErrorDesc()
|
|
735 |
{
|
|
736 |
TInt ret = KErrGeneral;
|
|
737 |
struct sockaddr_in host;
|
|
738 |
int dest_fd;
|
|
739 |
host.sin_port = htons(122);
|
|
740 |
host.sin_family = AF_INET;
|
|
741 |
host.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
742 |
dest_fd = socket(AF_INET,SOCK_STREAM,0);
|
|
743 |
|
|
744 |
TInt blocking;
|
|
745 |
|
|
746 |
if(!GetIntFromConfig(ConfigSection(), _L("blocking"),blocking ))
|
|
747 |
{
|
|
748 |
ERR_PRINTF1(_L("Reading case from .ini fails"));
|
|
749 |
return ret;
|
|
750 |
}
|
|
751 |
|
|
752 |
if(blocking == 0)
|
|
753 |
{
|
|
754 |
fcntl( dest_fd, F_SETFL, O_NONBLOCK );
|
|
755 |
}
|
|
756 |
|
|
757 |
ret = connect(dest_fd,(struct sockaddr*)&host,sizeof(host));
|
|
758 |
if(ret == -1)
|
|
759 |
{
|
|
760 |
INFO_PRINTF2(_L("Connect errno value is %d\n"), errno);
|
|
761 |
if((blocking == 0) && (errno != EINPROGRESS))
|
|
762 |
return KErrGeneral;
|
|
763 |
}
|
|
764 |
|
|
765 |
fd_set readfds;
|
|
766 |
fd_set writefds;
|
|
767 |
fd_set exceptfds;
|
|
768 |
|
|
769 |
FD_ZERO(&writefds);
|
|
770 |
FD_SET(dest_fd, &writefds);
|
|
771 |
FD_ZERO(&readfds);
|
|
772 |
FD_SET(dest_fd, &readfds);
|
|
773 |
FD_ZERO(&exceptfds);
|
|
774 |
FD_SET(dest_fd, &exceptfds);
|
|
775 |
|
|
776 |
int maxfd = dest_fd + 1;
|
|
777 |
ret = select(maxfd, &readfds, &writefds, NULL,NULL);
|
|
778 |
if(ret == -1)
|
|
779 |
{
|
|
780 |
INFO_PRINTF2(_L("select errno value is %d\n"), errno);
|
|
781 |
return KErrGeneral;
|
|
782 |
}
|
|
783 |
else
|
|
784 |
{
|
|
785 |
INFO_PRINTF2(_L("select ret value is %d\n"), ret);
|
|
786 |
}
|
|
787 |
|
|
788 |
FD_ZERO(&writefds);
|
|
789 |
FD_SET(dest_fd, &writefds);
|
|
790 |
FD_ZERO(&readfds);
|
|
791 |
FD_SET(dest_fd, &readfds);
|
|
792 |
FD_ZERO(&exceptfds);
|
|
793 |
FD_SET(dest_fd, &exceptfds);
|
|
794 |
|
|
795 |
ret = select(maxfd, &readfds, NULL, &exceptfds, NULL);
|
|
796 |
if(ret == -1)
|
|
797 |
{
|
|
798 |
INFO_PRINTF2(_L("select errno value is %d\n"), errno);
|
|
799 |
return KErrGeneral;
|
|
800 |
}
|
|
801 |
else
|
|
802 |
{
|
|
803 |
INFO_PRINTF2(_L("select ret value is %d\n"), ret);
|
|
804 |
}
|
|
805 |
|
|
806 |
FD_ZERO(&writefds);
|
|
807 |
FD_SET(dest_fd, &writefds);
|
|
808 |
FD_ZERO(&readfds);
|
|
809 |
FD_SET(dest_fd, &readfds);
|
|
810 |
FD_ZERO(&exceptfds);
|
|
811 |
FD_SET(dest_fd, &exceptfds);
|
|
812 |
|
|
813 |
ret = select(maxfd, NULL, &writefds, &exceptfds, NULL);
|
|
814 |
if(ret == -1)
|
|
815 |
{
|
|
816 |
INFO_PRINTF2(_L("select errno value is %d\n"), errno);
|
|
817 |
return KErrGeneral;
|
|
818 |
}
|
|
819 |
else
|
|
820 |
{
|
|
821 |
INFO_PRINTF2(_L("select ret value is %d\n"), ret);
|
|
822 |
}
|
|
823 |
|
|
824 |
FD_ZERO(&writefds);
|
|
825 |
FD_SET(dest_fd, &writefds);
|
|
826 |
FD_ZERO(&readfds);
|
|
827 |
FD_SET(dest_fd, &readfds);
|
|
828 |
FD_ZERO(&exceptfds);
|
|
829 |
FD_SET(dest_fd, &exceptfds);
|
|
830 |
|
|
831 |
ret = select(maxfd, NULL, NULL, &exceptfds, NULL);
|
|
832 |
if(ret == -1)
|
|
833 |
{
|
|
834 |
INFO_PRINTF2(_L("select errno value is %d\n"), errno);
|
|
835 |
return KErrGeneral;
|
|
836 |
}
|
|
837 |
else
|
|
838 |
{
|
|
839 |
INFO_PRINTF2(_L("select ret value is %d\n"), ret);
|
|
840 |
}
|
|
841 |
|
|
842 |
ret = connect(dest_fd,(struct sockaddr*)&host,sizeof(host));
|
|
843 |
if(ret == -1)
|
|
844 |
{
|
|
845 |
INFO_PRINTF2(_L("Connect errno value is %d\n"), errno);
|
|
846 |
}
|
|
847 |
|
|
848 |
close(dest_fd);
|
|
849 |
return 0;
|
|
850 |
}
|
|
851 |
|
|
852 |
TInt CTestSelect::TestSelectOnBadPipeEnd1()
|
|
853 |
{
|
|
854 |
int fds[2],rc,max;
|
|
855 |
fd_set readfds,writefds;
|
|
856 |
|
|
857 |
if (pipe(fds) == -1)
|
|
858 |
{
|
|
859 |
INFO_PRINTF1(_L("Pipe creation failed!\n")) ;
|
|
860 |
}
|
|
861 |
/* fds[0] - opened for read */
|
|
862 |
/* fds[1] - opened for write */
|
|
863 |
write(fds[1],"hello",5); // Writing data onto the pipe, thus setting the read event bit
|
|
864 |
if(fds[0] > fds[1])
|
|
865 |
max = fds[0] + 1;
|
|
866 |
else
|
|
867 |
max = fds[1] + 1;
|
|
868 |
FD_ZERO(&readfds);
|
|
869 |
FD_ZERO(&writefds);
|
|
870 |
FD_SET(fds[0],&readfds);
|
|
871 |
FD_SET(fds[0],&writefds); // issue here is:-- trying to set the write operation bit on the fixed read end of the pipe
|
|
872 |
// OR.. FD_SET(fds[1],&writefds); FD_SET(fds[1],&readfds); //similar issue here is:-- trying to set the read operation bit on the fixed write end of the pipe
|
|
873 |
rc = select(max,&readfds,&writefds, NULL, NULL);
|
|
874 |
if (rc == -1)
|
|
875 |
{
|
|
876 |
INFO_PRINTF1(_L("select failed\n")) ;
|
|
877 |
return KErrGeneral ;
|
|
878 |
}
|
|
879 |
else
|
|
880 |
{
|
|
881 |
if(FD_ISSET(fds[0],&readfds))
|
|
882 |
INFO_PRINTF1(_L("ready for reading\n\n")) ;
|
|
883 |
}
|
|
884 |
INFO_PRINTF2(_L("Return value from select is %d\n"), rc);
|
|
885 |
close(fds[0]);
|
|
886 |
close(fds[1]);
|
|
887 |
return 0;
|
|
888 |
}
|
|
889 |
|
|
890 |
////One more Test case added to check for writability at the read end of the pipe or readability for the write end of the pipe while notifying the event
|
|
891 |
|
|
892 |
void *WritingToPipe(void * p)
|
|
893 |
{
|
|
894 |
int *fd;
|
|
895 |
fd = (int *)p;
|
|
896 |
sleep(3);
|
|
897 |
write( *(fd + 1),"hello",5);
|
|
898 |
return 0;
|
|
899 |
}
|
|
900 |
|
|
901 |
TInt CTestSelect::TestSelectOnBadPipeEnd2()
|
|
902 |
{
|
|
903 |
int fds[2],rc, max, *p,t;
|
|
904 |
fd_set readfds,writefds;
|
|
905 |
pthread_t threads1;
|
|
906 |
int threadRetVal = 0;
|
|
907 |
|
|
908 |
if (pipe(fds) == -1)
|
|
909 |
{
|
|
910 |
INFO_PRINTF1(_L("Pipe creation failed!\n")) ;
|
|
911 |
}
|
|
912 |
p = &fds[0];
|
|
913 |
/* fds[0] - opened for read */
|
|
914 |
/* fds[1] - opened for write */
|
|
915 |
// creating the new thread for writing data onto the pipe when select is actually waiting for someone to write into the pipe
|
|
916 |
t = pthread_create(&threads1, NULL, WritingToPipe, (void *)p);
|
|
917 |
if (t)
|
|
918 |
{
|
|
919 |
INFO_PRINTF2(_L("ERROR; return code from pthread_create() is %d\n"), t);
|
|
920 |
}
|
|
921 |
if(fds[0] > fds[1])
|
|
922 |
max = fds[0] + 1;
|
|
923 |
else
|
|
924 |
max = fds[1] + 1;
|
|
925 |
FD_ZERO(&readfds);
|
|
926 |
FD_ZERO(&writefds);
|
|
927 |
FD_SET(fds[0],&readfds);
|
|
928 |
FD_SET(fds[0],&writefds); // issue here is:-- trying to set the write operation bit on the fixed read end of the pipe
|
|
929 |
// OR.. FD_SET(fds[1],&writefds); FD_SET(fds[1],&readfds); // similar issue here is:-- trying to set the read operation bit on the fixed write end of the pipe
|
|
930 |
const int timeout = 5000; // 5 seconds
|
|
931 |
struct timeval tv;
|
|
932 |
tv.tv_sec = timeout / 1000;
|
|
933 |
tv.tv_usec = (timeout % 1000) * 1000;
|
|
934 |
rc = select(max,&readfds,&writefds, NULL, &tv);
|
|
935 |
if (rc == -1)
|
|
936 |
{
|
|
937 |
INFO_PRINTF1(_L("select failed\n")) ;
|
|
938 |
return KErrGeneral ;
|
|
939 |
}
|
|
940 |
else
|
|
941 |
{
|
|
942 |
if(FD_ISSET(fds[0],&readfds))
|
|
943 |
INFO_PRINTF1(_L("ready for reading\n\n")) ;
|
|
944 |
}
|
|
945 |
INFO_PRINTF2(_L("Return value from select is %d\n"), rc);
|
|
946 |
pthread_join(threads1, (void**)threadRetVal);
|
|
947 |
close(fds[0]);
|
|
948 |
close(fds[1]);
|
|
949 |
return 0;
|
|
950 |
}
|
|
951 |
|
|
952 |
/* -----------------------------------------------------------------------------
|
|
953 |
*Function Name :TestSelectLargeFd
|
|
954 |
*API Tested :select()
|
|
955 |
*TestCase Description:Testing the behaviour of select with nfds set to FD_SETSIZE.
|
|
956 |
*(FD_SETSIZE is macro whose value is the maximum number of file descriptors that a fd_set object can hold information about.)
|
|
957 |
* -----------------------------------------------------------------------------
|
|
958 |
*/
|
|
959 |
TInt CTestSelect::TestSelectLargeFd()
|
|
960 |
{
|
|
961 |
struct timeval to;
|
|
962 |
to.tv_sec = 0;
|
|
963 |
to.tv_usec = 100;
|
|
964 |
fd_set readfds;
|
|
965 |
FD_ZERO(&readfds);
|
|
966 |
TInt ret = KErrGeneral;
|
|
967 |
TInt fd;
|
|
968 |
if(!GetIntFromConfig(ConfigSection(), _L("fd"),fd ))
|
|
969 |
{
|
|
970 |
ERR_PRINTF1(_L("Reading case from .ini fails"));
|
|
971 |
return ret;
|
|
972 |
}
|
|
973 |
TInt retVal = select(fd, &readfds, NULL, NULL, &to);
|
|
974 |
TInt expVal;
|
|
975 |
if(!GetIntFromConfig(ConfigSection(), _L("expVal"),expVal ))
|
|
976 |
{
|
|
977 |
ERR_PRINTF1(_L("Reading case from .ini fails"));
|
|
978 |
return ret;
|
|
979 |
}
|
|
980 |
TInt err;
|
|
981 |
if(!GetIntFromConfig(ConfigSection(), _L("err"),err ))
|
|
982 |
{
|
|
983 |
ERR_PRINTF1(_L("Reading case from .ini fails"));
|
|
984 |
return ret;
|
|
985 |
}
|
|
986 |
if(retVal != expVal && errno != err)
|
|
987 |
{
|
|
988 |
ERR_PRINTF4(_L("Expected: %d and Result: %d ,errno: %d"),expVal, retVal,errno);
|
|
989 |
return ret;
|
|
990 |
}
|
|
991 |
ret = KErrNone ;
|
|
992 |
return ret ;
|
|
993 |
}
|
|
994 |
/* -----------------------------------------------------------------------------
|
|
995 |
*Function Name :TestSelectRedirDescWrite
|
|
996 |
*API Tested :select()
|
|
997 |
*TestCase Description:Testing the behaviour of select with redir desc as the fd
|
|
998 |
* -----------------------------------------------------------------------------
|
|
999 |
*/
|
|
1000 |
TInt CTestSelect::TestSelectRedirDescWrite()
|
|
1001 |
{
|
|
1002 |
TBool stdinpass = EFalse;
|
|
1003 |
fd_set readfds;
|
|
1004 |
fd_set writefds;
|
|
1005 |
fd_set exceptfds;
|
|
1006 |
int maxfd = STDIN_FILENO;
|
|
1007 |
// int select_ret = 0;
|
|
1008 |
maxfd = STDIN_FILENO;
|
|
1009 |
FD_ZERO(&writefds);
|
|
1010 |
FD_SET(STDIN_FILENO,&writefds);
|
|
1011 |
/*select_ret =*/ select(maxfd+1,&readfds,&writefds,&exceptfds,NULL);
|
|
1012 |
if( FD_ISSET(STDIN_FILENO,&writefds) )
|
|
1013 |
{
|
|
1014 |
stdinpass = ETrue;
|
|
1015 |
INFO_PRINTF1(_L("select on writefds of STDIN passed\n")) ;
|
|
1016 |
}
|
|
1017 |
else
|
|
1018 |
{
|
|
1019 |
INFO_PRINTF1(_L("select on writefds of STDIN failed\n")) ;
|
|
1020 |
return KErrGeneral;
|
|
1021 |
}
|
|
1022 |
maxfd = STDOUT_FILENO;
|
|
1023 |
FD_ZERO(&writefds);
|
|
1024 |
FD_SET(STDOUT_FILENO,&writefds);
|
|
1025 |
/*select_ret = */ select(maxfd+1,NULL,&writefds,NULL,NULL);
|
|
1026 |
if( FD_ISSET(STDOUT_FILENO,&writefds) )
|
|
1027 |
{
|
|
1028 |
if( stdinpass )
|
|
1029 |
{
|
|
1030 |
INFO_PRINTF1(_L("select on writefds of STDOUT passed\n")) ;
|
|
1031 |
return KErrNone;
|
|
1032 |
}
|
|
1033 |
else
|
|
1034 |
{
|
|
1035 |
INFO_PRINTF1(_L("select on writefds of STDOUT failed\n")) ;
|
|
1036 |
}
|
|
1037 |
}
|
|
1038 |
return KErrGeneral;
|
|
1039 |
}
|
|
1040 |
|
|
1041 |
/* -----------------------------------------------------------------------------
|
|
1042 |
*Function Name :TestSelectConnectFailure
|
|
1043 |
*API Tested :select()
|
|
1044 |
*TestCase Description:Testing the behaviour of select on non blocking socket
|
|
1045 |
*when connect fails
|
|
1046 |
* -----------------------------------------------------------------------------
|
|
1047 |
*/
|
|
1048 |
TInt CTestSelect::TestSelectConnectFailure()
|
|
1049 |
{
|
|
1050 |
// Loop back
|
|
1051 |
char *addr="127.0.0.1";
|
|
1052 |
int sock_fd;
|
|
1053 |
int check = 0;
|
|
1054 |
int flags;
|
|
1055 |
fd_set writefds;
|
|
1056 |
fd_set readfds;
|
|
1057 |
fd_set exceptfds;
|
|
1058 |
int max;
|
|
1059 |
int select_ret = 0;
|
|
1060 |
// int ret = KErrGeneral;
|
|
1061 |
struct sockaddr_in serv_addr;
|
|
1062 |
serv_addr.sin_family = AF_INET;
|
|
1063 |
serv_addr.sin_addr.s_addr = inet_addr(addr);
|
|
1064 |
serv_addr.sin_port = htons(80);
|
|
1065 |
sock_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
|
|
1066 |
if (-1 == (flags = fcntl(sock_fd, F_GETFL, 0)))
|
|
1067 |
{
|
|
1068 |
flags = 0;
|
|
1069 |
}
|
|
1070 |
check = fcntl(sock_fd, F_SETFL, flags | O_NONBLOCK);
|
|
1071 |
if ( check != -1 )
|
|
1072 |
{
|
|
1073 |
connect(sock_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
|
|
1074 |
}
|
|
1075 |
max = sock_fd + 1;
|
|
1076 |
// readfds,writefds and exceptfds are requested
|
|
1077 |
FD_ZERO(&readfds);
|
|
1078 |
FD_ZERO(&writefds);
|
|
1079 |
FD_ZERO(&exceptfds);
|
|
1080 |
FD_SET(sock_fd, &readfds);
|
|
1081 |
FD_SET(sock_fd, &writefds);
|
|
1082 |
FD_SET(sock_fd, &exceptfds);
|
|
1083 |
select_ret = select(max,&readfds,&writefds,&exceptfds,NULL);
|
|
1084 |
if( !(select_ret == 3 && FD_ISSET(sock_fd,&readfds)
|
|
1085 |
&& FD_ISSET(sock_fd,&writefds) && FD_ISSET(sock_fd,&exceptfds)) )
|
|
1086 |
{
|
|
1087 |
return KErrGeneral;
|
|
1088 |
}
|
|
1089 |
// readfds and writefds are requested
|
|
1090 |
FD_ZERO(&readfds);
|
|
1091 |
FD_ZERO(&writefds);
|
|
1092 |
FD_SET(sock_fd, &readfds);
|
|
1093 |
FD_SET(sock_fd, &writefds);
|
|
1094 |
select_ret = select(max,&readfds,&writefds,NULL,NULL);
|
|
1095 |
if(!(select_ret == 2 && FD_ISSET(sock_fd,&readfds)
|
|
1096 |
&& FD_ISSET(sock_fd,&writefds)))
|
|
1097 |
{
|
|
1098 |
return KErrGeneral;
|
|
1099 |
}
|
|
1100 |
// Only readfds is requested
|
|
1101 |
FD_ZERO(&readfds);
|
|
1102 |
FD_SET(sock_fd, &readfds);
|
|
1103 |
select_ret = select(max,&readfds,NULL,NULL,NULL);
|
|
1104 |
if(!(select_ret == 1 && FD_ISSET(sock_fd,&readfds)))
|
|
1105 |
{
|
|
1106 |
return KErrGeneral;
|
|
1107 |
}
|
|
1108 |
// Only writefds is requested
|
|
1109 |
FD_ZERO(&writefds);
|
|
1110 |
FD_SET(sock_fd, &writefds);
|
|
1111 |
select_ret = select(max,NULL,&writefds,NULL,NULL);
|
|
1112 |
if(!(select_ret == 1 && FD_ISSET(sock_fd,&writefds)))
|
|
1113 |
{
|
|
1114 |
return KErrGeneral;
|
|
1115 |
}
|
|
1116 |
return KErrNone;
|
|
1117 |
}
|
|
1118 |
|