Update contrib.
2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Test of AF_INET stream sockets
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/ioctl.h>
30 #include <libc/netinet/in.h>
31 #include <libc/arpa/inet.h>
34 #include "CTEST.H" /* includes C interface to EPOC32 threads, and SpawnPosixServer */
38 struct sockaddr_in testaddr;
40 unsigned short int port = 2001;
43 @SYMTestCaseID SYSLIB-STDLIB-CT-1059
44 @SYMTestCaseDesc Tests for AF_INET stream sockets
46 @SYMTestActions Tests for simple operations on stream sockets,open,bind,get socket name,listen operations and test for error code
47 @SYMTestExpectedResults Test must not fail
56 struct sockaddr_in addr1, addr2;
60 test_Next("Create stream sockets");
61 fd1=socket(AF_INET, SOCK_STREAM, 0);
64 fd2=socket(AF_INET, SOCK_STREAM, 0);
67 test_Next("Some binding tests");
69 addr1.sin_family=2061;
70 addr1.sin_port=htons(65530);
71 addr1.sin_addr.s_addr=htonl(0x11223344);
72 err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1)); /* wrong family, port out of range */
73 test_errno(err<0,ENOENT);
76 addr1.sin_port=htons(port);
77 err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1));
81 addr1.sin_port=htons((unsigned short int)(port+1));
82 err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1)); /* already bound */
83 test_errno(err!=0, EEXIST);
85 test_Next("Get associated addresses");
87 addrsize=sizeof(addr2);
88 err=getsockname(fd1,(struct sockaddr*)&addr2,&addrsize);
90 test(addr2.sin_family==AF_INET);
91 test(addr2.sin_port==htons(port));
92 test(addrsize<=sizeof(addr2));
94 addrsize=sizeof(addr2);
95 err=getpeername(fd1,(struct sockaddr*)&addr2,&addrsize); /* not connected */
96 test_errno(err!=0,-2); /* OMISSION - can't report proper reason for failure */
98 addrsize=sizeof(addr2);
99 err=getsockname(fd2,(struct sockaddr*)&addr2,&addrsize); /* not bound */
100 test_errno(err!=0,-2); /* OMISSION - can't report proper reason for failure */
102 test_Next("More binding");
105 addr1.sin_port=htons(port);
106 err=bind(fd2, (struct sockaddr*)&addr1, sizeof(addr1)); /* address in use */
107 test_errno(err!=0, EACCES);
111 err=bind(fd2, (struct sockaddr*)&addr1, sizeof(addr1)); /* unspecified port number */
114 addrsize=sizeof(addr2);
115 err=getsockname(fd2,(struct sockaddr*)&addr2,&addrsize);
117 test(addr2.sin_family==AF_INET);
118 test(addrsize<=sizeof(addr2));
119 test(addr2.sin_port!=htons(port));
120 test(addr2.sin_port!=0);
122 unsigned short port=ntohs(addr2.sin_port);
123 test(port>=IPPORT_RESERVED);
124 /* NB. TCPIP 030 has 9999 for IPPORT_USERRESERVED, but TCPIP 031 dropped it to 5000 */
125 test(port<=IPPORT_USERRESERVED);
126 printf(" allocated port %d\n",port);
132 test_Next("Socket options");
134 optionbuf[0]=3500000; /* implausible size */
135 optionsize=sizeof(optionbuf[0]);
136 err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
138 test(optionbuf[0]!=3500000);
141 optionsize=sizeof(optionbuf[0]);
142 err=setsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,optionsize);
145 optionbuf[0]=3500000; /* implausible size */
146 optionsize=sizeof(optionbuf[0]);
147 err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
149 test(optionbuf[0]==7*1024);
152 optionsize=sizeof(optionbuf[0]);
153 err=getsockopt(fd1,SOL_SOCKET,1234,optionbuf,&optionsize); /* invalid option */
154 test_errno(err<0,ENOSYS);
155 test(optionbuf[0]==1);
158 optionsize=sizeof(optionbuf[0]);
159 err=setsockopt(fd1,SOL_SOCKET,1234,optionbuf,optionsize); /* invalid option */
160 test_errno(err<0,ENOSYS);
161 test(optionbuf[0]==13);
170 /* Client and server take it in turns to send, starting with the client.
171 * Each matches the message they receive with the string expected
173 char *message_sequence[] = {
181 "Try recvfrom again",
182 "Send to shutdown socket",
183 "Send to closed socket",
188 @SYMTestCaseID SYSLIB-STDLIB-CT-1060
189 @SYMTestCaseDesc Tests for server socket
190 @SYMTestPriority High
191 @SYMTestActions Create a steam socket,bind and test for a transmission process.Check for error codes
192 @SYMTestExpectedResults Test must not fail
197 int fd1,fd2,nbytes,i;
200 struct sockaddr_in addr1, addr2;
202 char **mp = message_sequence;
203 static int randbuf[10000];
207 /* Fill the buffer with a predetermined random number sequence */
210 for (i=1; i<10000; i++)
214 test_Next("Create server socket");
215 fd1=socket(AF_INET, SOCK_STREAM, 0);
219 /* causes ESOCK to panic the client */
220 addrsize=sizeof(addr2);
221 fd2=accept(fd1,&addr2,&addrsize); /* can't accept on an unbound socket */
225 IN_SET_LOOPBACK_ADDR(&addr1);
226 addr1.sin_port=htons(port);
227 err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1));
231 /* causes ESOCK to panic the client */
232 addrsize=sizeof(addr2);
233 fd2=accept(fd1,&addr2,&addrsize); /* can't accept before listening */
240 addrsize=sizeof(addr2);
241 fd2=accept(fd1,(struct sockaddr*)&addr2,&addrsize);
243 test(addr2.sin_family==AF_INET);
244 test(addr2.sin_port!=htons(port));
245 test(addr2.sin_port!=0);
246 test(addrsize<=sizeof(addr2));
248 test_Next("Server read/write");
252 err=read(fd2, buf, nbytes+1);
253 test_ok(err==nbytes+1);
254 test(strcmp(buf,*mp)==0);
259 for (i=0; i<nbytes+1; i++)
261 err=write(fd2,(*mp)+i,1);
265 test_Next("Server send/recv");
270 err=recv(fd2, buf, sizeof(buf) ,0);
271 test_ok(err==nbytes+1);
272 test(strcmp(buf,*mp)==0);
277 err=send(fd2, *mp, nbytes+1,0);
278 test_ok(err==nbytes+1);
283 addrsize=sizeof(addr2);
285 err=recvfrom(fd2, buf, nbytes+1,0,(struct sockaddr*)&addr2,&addrsize);
287 memset(&buf[0],0, sizeof(buf));
288 err=recvfrom(fd2, buf, nbytes+1,0,0,0);
290 test_ok(err==nbytes+1);
291 test(strcmp(buf,*mp)==0);
292 test(addr2.sin_family==AF_INET);
293 test(addr2.sin_port!=htons(port));
294 test(addr2.sin_port!=0);
295 test(addrsize<=sizeof(addr2));
300 addrsize=sizeof(addr1);
301 err=sendto(fd2, *mp, nbytes+1,0,(struct sockaddr*)&addr1,addrsize); /* not allowed on streams */
302 test_errno(err<0,ENOSYS);
303 err=send(fd2, *mp, nbytes+1,0); /* to keep synchronisation */
304 test_ok(err==nbytes+1);
307 test_Next("Server revcfrom with null fromaddr");
311 memset(&buf[0],0xFF, sizeof(buf));
312 err=recvfrom(fd2, buf, nbytes+1,0,0,0);
313 test_ok(err==nbytes+1);
314 test(strcmp(buf,*mp)==0);
319 addrsize=sizeof(addr1);
320 err=send(fd2, *mp, nbytes+1,0); /* to keep synchronisation */
321 test_ok(err==nbytes+1);
326 test_Next("Server shutdown transmission");
330 sleep(4); // so that the client's sleep(1) finishes before we awake
334 addrsize=sizeof(addr1);
335 err=send(fd2, *mp, nbytes+1,0);
336 test_errno(err<0,EPIPE);
339 err=recv(fd2, buf, nbytes+1,0);
340 test_ok(err==nbytes+1);
341 test(strcmp(buf,*mp)==0);
346 // Large transmission
348 test_Next("Server large transfer");
350 addrsize=sizeof(addr2);
351 fd2=accept(fd1,(struct sockaddr*)&addr2,&addrsize);
353 test(addr2.sin_family==AF_INET);
354 test(addr2.sin_port!=htons(port));
355 test(addr2.sin_port!=0);
356 test(addrsize<=sizeof(addr2));
358 nbytes=sizeof(randbuf);
359 err=write(fd2,(const char*)randbuf,nbytes);
360 test_ok(err==nbytes);
362 test_Next("Server close");
364 // err=shutdown(fd2,1);
377 @SYMTestCaseID SYSLIB-STDLIB-CT-1061
378 @SYMTestCaseDesc Tests for client socket
379 @SYMTestPriority High
380 @SYMTestActions Create a steam socket,bind and test for a transmission process.Check for error codes
381 @SYMTestExpectedResults Test must not fail
386 int fd1, nbytes, nbytes2, i, status;
390 struct sockaddr_in addr1, addr2;
392 char **mp = message_sequence;
393 static int randbuf[10001];
395 test_Next("Create client socket");
396 fd1=socket(AF_INET, SOCK_STREAM, 0);
399 sleep(5); /* give server a chance to run */
402 addr1.sin_port=htons(port);
403 addrsize=sizeof(addr1);
404 err=connect(fd1,(struct sockaddr*)&addr1,addrsize);
407 addrsize=sizeof(addr2);
408 err=getpeername(fd1,(struct sockaddr*)&addr2,&addrsize);
410 test(addr2.sin_family==AF_INET);
411 test(addr2.sin_port==htons(port));
412 test(addrsize<=sizeof(addr2));
414 addrsize=sizeof(addr2);
415 err=getsockname(fd1,(struct sockaddr*)&addr2,&addrsize);
417 test(addr2.sin_family==AF_INET);
418 test(addr2.sin_port!=htons(port));
419 test(addr2.sin_port!=0);
420 test(addrsize<=sizeof(addr2));
422 test_Next("Client read/write");
426 err=write(fd1, *mp, nbytes+1);
427 test_ok(err==nbytes+1);
432 err=read(fd1, buf, nbytes+1);
433 test_ok(err==nbytes+1);
434 test(strcmp(buf,*mp)==0);
436 test_Next("Client send/recv");
441 err=send(fd1,*mp, nbytes+1,0);
442 test_ok(err==nbytes+1);
444 /* recv - get the first 2 bytes so that we know the buffer is full */
447 err=recv(fd1,buf,2,0);
453 err=ioctl(fd1,E32IONREAD,&nbytes2);
455 test(nbytes2==(nbytes+1-2));
457 nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
458 err=ioctl(fd1,E32IOSELECT,&nbytes2);
460 test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
462 nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
463 err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
465 err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
467 if (nbytes2!=(E32SELECT_READ|E32SELECT_WRITE))
469 nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
470 err=ioctl(fd1,E32IOSELECT,&nbytes2);
472 test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
475 /* recv - get the rest of the data */
476 for (i=2; i<nbytes+1; i++)
478 err=recv(fd1,buf+i,1,0);
481 test(strcmp(buf,*mp)==0);
483 /* ioctl again - this time there is no data pending */
485 err=ioctl(fd1,E32IONREAD,&nbytes2);
489 nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
490 err=ioctl(fd1,E32IOSELECT,&nbytes2);
492 test(nbytes2==E32SELECT_WRITE);
494 nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
495 err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
497 err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
499 test(nbytes2==E32SELECT_WRITE);
504 addrsize=sizeof(addr1);
505 err=sendto(fd1, *mp, nbytes+1,0,(struct sockaddr*)&addr1,addrsize);
506 test_errno(err<0,ENOSYS);
507 err=send(fd1, *mp, nbytes+1,0); /* to keep synchronisation */
508 test_ok(err==nbytes+1);
513 addrsize=sizeof(addr2);
515 err=recvfrom(fd1, buf, nbytes+1,0,(struct sockaddr*)&addr2,&addrsize);
516 test_ok(err==nbytes+1);
517 test(strcmp(buf,*mp)==0);
518 test(addr2.sin_family==AF_INET);
519 test(addr2.sin_port==htons(port));
520 test(addrsize<=sizeof(addr2));
523 test_Next("Client revcfrom with null fromaddr");
526 addrsize=sizeof(addr1);
527 err=sendto(fd1, *mp, nbytes+1,0,(struct sockaddr*)&addr1,addrsize);
528 test_errno(err<0,ENOSYS);
529 err=send(fd1, *mp, nbytes+1,0); /* to keep synchronisation */
530 test_ok(err==nbytes+1);
535 addrsize=sizeof(addr2);
536 memset(buf,0xFF,sizeof(buf));
537 err=recvfrom(fd1, buf, nbytes+1,0,0,0); //do a recvfrom with a null address
538 test_ok(err==nbytes+1);
539 test(strcmp(buf,*mp)==0);
545 test_Next("Test half-closed connection");
547 // read from half-closed socket
550 err=read(fd1, buf, nbytes+1);
551 test_errno(err<0, EPIPE);
554 err=write(fd1,*mp,nbytes+1);
555 test_ok(err==nbytes+1);
559 // read from a connection closed by the other end
562 err=read(fd1,buf,nbytes+1);
563 test_errno(err<0, EPIPE);
568 // Large transmission - and another connection to the server socket
570 test_Next("Test large transmission");
572 fd1=socket(AF_INET, SOCK_STREAM, 0);
575 sleep(2); /* give server a chance to run */
578 addr1.sin_port=htons(port);
579 addrsize=sizeof(addr1);
581 //this connect fails. Why is this?
582 err=connect(fd1,(struct sockaddr*)&addr1,addrsize);
586 addrsize=sizeof(addr2);
587 err=getpeername(fd1,(struct sockaddr*)&addr2,&addrsize);
589 test(addr2.sin_family==AF_INET);
590 test(addr2.sin_port==htons(port));
591 test(addrsize<=sizeof(addr2));
593 addrsize=sizeof(addr2);
594 err=getsockname(fd1,(struct sockaddr*)&addr2,&addrsize);
596 test(addr2.sin_family==AF_INET);
597 test(addr2.sin_port!=htons(port));
598 test(addr2.sin_port!=0);
599 test(addrsize<=sizeof(addr2));
608 buf=sizeof(randbuf)-nbytes;
611 p = (char*)randbuf+nbytes;
612 // err=read(fd1, p, buf);
613 err=recv(fd1, p, buf, 0);
617 while (nbytes<sizeof(randbuf) && err>0);
620 printf("\nnbytes=%d, err=%d, errno=%d\n", nbytes, err, errno);
622 test_ok(err==0); /* want a graceful EOF */
623 test(nbytes==sizeof(randbuf)-sizeof(int)); /* expect a smaller test data set */
624 test(randbuf[0]==4441302);
626 for (i=1; i<10000; i++)
627 test(randbuf[i]==rand());
631 err=recv(fd1, (char*)randbuf, 1, 0);
640 void testConnection()
646 sprintf(buf, "TISTREAM server %d", close_console);
647 server=create_thread(testServer, buf);
649 start_thread(server);
651 sleep(2); /* give the server a chance to get started */
654 err=wait_for_thread(server);
666 int main(int argc, char *argv[])
671 test_Title("AF_INET Streams");
673 err=CommInit(0); /* ensure a workable comms environment */
676 IN_SET_LOOPBACK_ADDR(&testaddr);
680 // Run the test(s) without a CPosixServer first
688 test_Next("Test gethostname");
691 hname = (char*)malloc(200);
694 err = gethostname(hname, 200);
697 printf("gethostname, err=%d, errno=%d\n", err, errno);
699 printf("host name is %s\n", hname);
703 printf("host name - malloc failure\n");
707 test_Next("Do it again using the CPosixServer (for them, not me)");
710 start_posix_server(); /* calls SpawnPosixServer from C++ code */
713 client=create_thread(testSimple, "TISTREAM simple");
715 start_thread(client);
716 err=wait_for_thread(client);
720 client=create_thread(testConnection, "TISTREAM client");
722 start_thread(client);
723 err=wait_for_thread(client);