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_LOCAL stream sockets
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/ioctl.h>
31 #include "ctest.h" /* includes C interface to EPOC32 threads, and SpawnPosixServer */
36 @SYMTestCaseID SYSLIB-STDLIB-CT-1115
37 @SYMTestCaseDesc Tests for AF_LOCAL stream sockets
39 @SYMTestActions Tests for create socket,binding and other simple socket operations
40 @SYMTestExpectedResults Test must not fail
48 struct sockaddr addr1, addr2;
52 test_Next("Create stream sockets");
53 fd1=socket(AF_LOCAL, SOCK_STREAM, 0);
56 fd2=socket(AF_LOCAL, SOCK_STREAM, 0);
59 test_Next("Some binding tests");
61 #if 0 /* causes socket server panic with ESOCK 058 */
62 addr1.sa_family=AF_UNSPEC;
64 err=bind(fd1,&addr1, sizeof(addr1)); /* wrong family, port out of range */
68 addr1.sa_family=AF_LOCAL;
70 err=bind(fd1, &addr1, sizeof(addr1));
73 addr1.sa_family=AF_LOCAL;
75 err=bind(fd1, &addr1, sizeof(addr1)); /* already bound */
76 test_errno(err!=0, EEXIST);
78 test_Next("Get associated addresses");
80 addrsize=sizeof(addr2);
81 err=getsockname(fd1,&addr2,&addrsize);
83 /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
84 test(addr2.sa_port==1);
85 test(addrsize<=sizeof(addr2));
87 addrsize=sizeof(addr2);
88 err=getpeername(fd1,&addr2,&addrsize); /* not connected */
89 test_errno(err!=0,-2); /* OMISSION - can't report proper reason for failure */
91 addrsize=sizeof(addr2);
92 err=getsockname(fd2,&addr2,&addrsize); /* not bound */
93 test_errno(err!=0,-2); /* OMISSION - can't report proper reason for failure */
95 test_Next("More binding");
97 addr1.sa_family=AF_LOCAL;
99 err=bind(fd2, &addr1, sizeof(addr1)); /* address in use */
100 test_errno(err!=0, EACCES);
102 #if 0 /* this isn't supported by AF_LOCAL */
103 addr1.sa_family=AF_LOCAL;
105 err=bind(fd2, &addr1, sizeof(addr1)); /* unspecified port number */
108 addrsize=sizeof(addr2);
109 err=getsockname(fd2,&addr2,&addrsize);
111 /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
112 test(addr2.sa_port!=1);
113 test(addr2.sa_port!=0);
114 test(addrsize<=sizeof(addr2));
120 test_Next("Socket options");
122 optionbuf[0]=3500000; /* implausible size */
123 optionsize=sizeof(optionbuf[0]);
124 err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
126 test(optionbuf[0]!=3500000);
129 optionsize=sizeof(optionbuf[0]);
130 err=setsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,optionsize);
133 optionbuf[0]=3500000; /* implausible size */
134 optionsize=sizeof(optionbuf[0]);
135 err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
137 test(optionbuf[0]==7*1024);
140 optionsize=sizeof(optionbuf[0]);
141 err=getsockopt(fd1,SOL_SOCKET,1234,optionbuf,&optionsize); /* invalid option */
142 test_errno(err<0,ENOSYS);
143 test(optionbuf[0]==1);
146 optionsize=sizeof(optionbuf[0]);
147 err=setsockopt(fd1,SOL_SOCKET,1234,optionbuf,optionsize); /* invalid option */
148 test_errno(err<0,ENOSYS);
149 test(optionbuf[0]==13);
158 /* Client and server take it in turns to send, starting with the client.
159 * Each matches the message they receive with the string expected
161 char *message_sequence[] = {
168 "Send to shutdown socket",
169 "Send to closed socket",
174 @SYMTestCaseID SYSLIB-STDLIB-CT-1116
175 @SYMTestCaseDesc Tests for server socket
176 @SYMTestPriority High
177 @SYMTestActions Tests for server socket,create,accept,send and receive functions
178 @SYMTestExpectedResults Test must not fail
183 int fd1, fd2, nbytes, i;
186 struct sockaddr addr1, addr2;
188 char **mp = message_sequence;
190 test_Next("Create server socket");
191 fd1=socket(AF_LOCAL, SOCK_STREAM, 0);
195 /* causes ESOCK to panic the client */
196 addrsize=sizeof(addr2);
197 fd2=accept(fd1,&addr2,&addrsize); /* can't accept on an unbound socket */
201 addr1.sa_family=AF_LOCAL;
203 err=bind(fd1, &addr1, sizeof(addr1));
207 /* causes ESOCK to panic the client */
208 addrsize=sizeof(addr2);
209 fd2=accept(fd1,&addr2,&addrsize); /* can't accept before listening */
216 addrsize=sizeof(addr2);
217 fd2=accept(fd1,&addr2,&addrsize);
219 /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
220 test(addr2.sa_port!=1);
221 test(addr2.sa_port!=0);
222 test(addrsize<=sizeof(addr2));
224 test_Next("Server read/write");
228 err=read(fd2, buf, nbytes+1);
229 test_ok(err==nbytes+1);
230 test(strcmp(buf,*mp)==0);
235 for (i=0; i<nbytes+1; i++)
237 err=write(fd2,(*mp)+i,1);
241 test_Next("Server send/recv");
246 err=recv(fd2, buf, nbytes+1,0);
247 test_ok(err==nbytes+1);
248 test(strcmp(buf,*mp)==0);
253 err=send(fd2, *mp, nbytes+1,0);
254 test_ok(err==nbytes+1);
259 addrsize=sizeof(addr2);
261 err=recvfrom(fd2, buf, nbytes+1,0,&addr2,&addrsize);
262 test_ok(err==nbytes+1);
263 test(strcmp(buf,*mp)==0);
264 /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
265 test(addr2.sa_port!=1);
266 test(addr2.sa_port!=0);
267 test(addrsize<=sizeof(addr2));
272 addrsize=sizeof(addr1);
273 err=sendto(fd2, *mp, nbytes+1,0,&addr1,addrsize); /* not allowed on streams */
274 test_errno(err<0,ENOSYS);
275 err=send(fd2, *mp, nbytes+1,0); /* to keep synchronisation */
276 test_ok(err==nbytes+1);
278 test_Next("Server shutdown reception");
282 sleep(2); /* so that the client's sleep(1) finishes before we awake */
292 @SYMTestCaseID SYSLIB-STDLIB-CT-1117
293 @SYMTestCaseDesc Tests for client socket
294 @SYMTestPriority High
295 @SYMTestActions Tests for client socket,create,accept,send and receive functions
296 Write to a connection closed socket and test for error
297 @SYMTestExpectedResults Test must not fail
302 int fd1, nbytes, nbytes2, i, status;
305 struct sockaddr addr1, addr2;
307 char **mp = message_sequence;
309 test_Next("Create client socket");
310 fd1=socket(AF_LOCAL, SOCK_STREAM, 0);
313 addr1.sa_family=AF_LOCAL;
315 addrsize=sizeof(addr1);
316 err=connect(fd1,&addr1,addrsize);
319 addrsize=sizeof(addr2);
320 err=getpeername(fd1,&addr2,&addrsize);
322 /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
323 test(addr2.sa_port==1);
324 test(addrsize<=sizeof(addr2));
326 addrsize=sizeof(addr2);
327 err=getsockname(fd1,&addr2,&addrsize);
329 /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
330 test(addr2.sa_port!=1);
331 test(addr2.sa_port!=0);
332 test(addrsize<=sizeof(addr2));
334 test_Next("Client read/write");
338 err=write(fd1, *mp, nbytes+1);
339 test_ok(err==nbytes+1);
344 err=read(fd1, buf, nbytes+1);
345 test_ok(err==nbytes+1);
346 test(strcmp(buf,*mp)==0);
348 test_Next("Client send/recv");
353 for (i=0; i<nbytes+1; i++)
355 err=send(fd1,(*mp)+i,1,0);
359 /* recv - get the first byte so that we know the buffer is full */
362 err=recv(fd1,buf,1,0);
367 err=ioctl(fd1,E32IONREAD,&nbytes2);
370 test(nbytes2==nbytes);
372 test_errno(err<0,ENOSYS); /* IPC.PRT doesn't implement KSoReadBytesPending */
375 nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
376 err=ioctl(fd1,E32IOSELECT,&nbytes2);
378 test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
380 nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
381 err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
383 err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
385 if (nbytes2!=(E32SELECT_READ|E32SELECT_WRITE))
387 nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
388 err=ioctl(fd1,E32IOSELECT,&nbytes2);
390 test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
393 /* recv - get the rest of the data */
394 for (i=1; i<nbytes+1; i++)
396 err=recv(fd1,buf+i,1,0);
399 test(strcmp(buf,*mp)==0);
401 /* ioctl again - this time there is no data pending */
403 err=ioctl(fd1,E32IONREAD,&nbytes2);
408 test_errno(err<0,ENOSYS); /* IPC.PRT doesn't implement KSoReadBytesPending */
411 nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
412 err=ioctl(fd1,E32IOSELECT,&nbytes2);
414 test(nbytes2==E32SELECT_WRITE);
416 nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
417 err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
419 err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
421 test(nbytes2==E32SELECT_WRITE);
426 addrsize=sizeof(addr1);
427 err=sendto(fd1, *mp, nbytes+1,0,&addr1,addrsize);
428 test_errno(err<0,ENOSYS);
429 err=send(fd1, *mp, nbytes+1,0); /* to keep synchronisation */
430 test_ok(err==nbytes+1);
435 addrsize=sizeof(addr2);
437 err=recvfrom(fd1, buf, nbytes+1,0,&addr2,&addrsize);
438 test_ok(err==nbytes+1);
439 test(strcmp(buf,*mp)==0);
440 /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
441 test(addr2.sa_port==1);
442 test(addrsize<=sizeof(addr2));
446 test_Next("Client write to closed connection");
448 /* write to half-closed socket */
451 err=write(fd1, *mp, nbytes+1);
453 /* IPC doesn't seem to care! */
454 test_errno(err<0, EPIPE);
456 test_ok(err==nbytes+1);
461 /* write to a connection closed by the other end */
464 err=write(fd1, *mp, nbytes+1);
465 test_errno(err<0, EPIPE);
474 @SYMTestCaseID SYSLIB-STDLIB-CT-1118
475 @SYMTestCaseDesc Tests for STREAM server
476 @SYMTestPriority High
477 @SYMTestActions Tests for server entity
478 @SYMTestExpectedResults Test must not fail
481 void testConnection()
486 server=create_thread(testServer, "TLSTREAM server");
488 start_thread(server);
490 sleep(1); /* give the server a chance to get started */
493 err=wait_for_thread(server);
505 int main(int argc, char *argv[])
510 test_Title("AF_LOCAL Streams");
515 test_Next("Do it again using the CPosixServer (for them, not me)");
518 start_posix_server(); /* calls SpawnPosixServer from C++ code */
520 client=create_thread(testSimple, "TLSTREAM simple");
522 start_thread(client);
523 err=wait_for_thread(client);
526 client=create_thread(testConnection, "TLSTREAM client");
528 start_thread(client);
529 err=wait_for_thread(client);