1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TLSTREAM.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,534 @@
1.4 +/*
1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Test of AF_LOCAL stream sockets
1.19 +*
1.20 +*
1.21 +*/
1.22 +
1.23 +
1.24 +
1.25 +#include <stdio.h>
1.26 +#include <errno.h>
1.27 +#include <stdlib.h>
1.28 +#include <string.h>
1.29 +#include <unistd.h>
1.30 +#include <sys/types.h>
1.31 +#include <sys/socket.h>
1.32 +#include <sys/ioctl.h>
1.33 +
1.34 +#include "ctest.h" /* includes C interface to EPOC32 threads, and SpawnPosixServer */
1.35 +
1.36 +test_Data;
1.37 +
1.38 +/**
1.39 +@SYMTestCaseID SYSLIB-STDLIB-CT-1115
1.40 +@SYMTestCaseDesc Tests for AF_LOCAL stream sockets
1.41 +@SYMTestPriority High
1.42 +@SYMTestActions Tests for create socket,binding and other simple socket operations
1.43 +@SYMTestExpectedResults Test must not fail
1.44 +@SYMREQ REQ0000
1.45 +*/
1.46 +void testSimple()
1.47 + {
1.48 + int fd1, fd2;
1.49 + size_t addrsize;
1.50 + int err;
1.51 + struct sockaddr addr1, addr2;
1.52 + int optionbuf[20];
1.53 + size_t optionsize;
1.54 +
1.55 + test_Next("Create stream sockets");
1.56 + fd1=socket(AF_LOCAL, SOCK_STREAM, 0);
1.57 + test_ok(fd1>=0);
1.58 +
1.59 + fd2=socket(AF_LOCAL, SOCK_STREAM, 0);
1.60 + test_ok(fd2>=0);
1.61 +
1.62 + test_Next("Some binding tests");
1.63 +
1.64 +#if 0 /* causes socket server panic with ESOCK 058 */
1.65 + addr1.sa_family=AF_UNSPEC;
1.66 + addr1.sa_port=65530;
1.67 + err=bind(fd1,&addr1, sizeof(addr1)); /* wrong family, port out of range */
1.68 + test_ok(err!=0);
1.69 +#endif
1.70 +
1.71 + addr1.sa_family=AF_LOCAL;
1.72 + addr1.sa_port=1;
1.73 + err=bind(fd1, &addr1, sizeof(addr1));
1.74 + test_ok(err==0);
1.75 +
1.76 + addr1.sa_family=AF_LOCAL;
1.77 + addr1.sa_port=2;
1.78 + err=bind(fd1, &addr1, sizeof(addr1)); /* already bound */
1.79 + test_errno(err!=0, EEXIST);
1.80 +
1.81 + test_Next("Get associated addresses");
1.82 +
1.83 + addrsize=sizeof(addr2);
1.84 + err=getsockname(fd1,&addr2,&addrsize);
1.85 + test_ok(err==0);
1.86 + /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
1.87 + test(addr2.sa_port==1);
1.88 + test(addrsize<=sizeof(addr2));
1.89 +
1.90 + addrsize=sizeof(addr2);
1.91 + err=getpeername(fd1,&addr2,&addrsize); /* not connected */
1.92 + test_errno(err!=0,-2); /* OMISSION - can't report proper reason for failure */
1.93 +
1.94 + addrsize=sizeof(addr2);
1.95 + err=getsockname(fd2,&addr2,&addrsize); /* not bound */
1.96 + test_errno(err!=0,-2); /* OMISSION - can't report proper reason for failure */
1.97 +
1.98 + test_Next("More binding");
1.99 +
1.100 + addr1.sa_family=AF_LOCAL;
1.101 + addr1.sa_port=1;
1.102 + err=bind(fd2, &addr1, sizeof(addr1)); /* address in use */
1.103 + test_errno(err!=0, EACCES);
1.104 +
1.105 +#if 0 /* this isn't supported by AF_LOCAL */
1.106 + addr1.sa_family=AF_LOCAL;
1.107 + addr1.sa_port=0;
1.108 + err=bind(fd2, &addr1, sizeof(addr1)); /* unspecified port number */
1.109 + test_ok(err==0);
1.110 +
1.111 + addrsize=sizeof(addr2);
1.112 + err=getsockname(fd2,&addr2,&addrsize);
1.113 + test_ok(err==0);
1.114 + /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
1.115 + test(addr2.sa_port!=1);
1.116 + test(addr2.sa_port!=0);
1.117 + test(addrsize<=sizeof(addr2));
1.118 +#endif
1.119 +
1.120 + err=listen(fd1,1);
1.121 + test_ok(err==0);
1.122 +
1.123 + test_Next("Socket options");
1.124 +
1.125 + optionbuf[0]=3500000; /* implausible size */
1.126 + optionsize=sizeof(optionbuf[0]);
1.127 + err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
1.128 + test_ok(err==0);
1.129 + test(optionbuf[0]!=3500000);
1.130 +
1.131 + optionbuf[0]=7*1024;
1.132 + optionsize=sizeof(optionbuf[0]);
1.133 + err=setsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,optionsize);
1.134 + test_ok(err==0);
1.135 +
1.136 + optionbuf[0]=3500000; /* implausible size */
1.137 + optionsize=sizeof(optionbuf[0]);
1.138 + err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
1.139 + test_ok(err==0);
1.140 + test(optionbuf[0]==7*1024);
1.141 +
1.142 + optionbuf[0]=1;
1.143 + optionsize=sizeof(optionbuf[0]);
1.144 + err=getsockopt(fd1,SOL_SOCKET,1234,optionbuf,&optionsize); /* invalid option */
1.145 + test_errno(err<0,ENOSYS);
1.146 + test(optionbuf[0]==1);
1.147 +
1.148 + optionbuf[0]=13;
1.149 + optionsize=sizeof(optionbuf[0]);
1.150 + err=setsockopt(fd1,SOL_SOCKET,1234,optionbuf,optionsize); /* invalid option */
1.151 + test_errno(err<0,ENOSYS);
1.152 + test(optionbuf[0]==13);
1.153 +
1.154 + err=close(fd1);
1.155 + test_ok(err==0);
1.156 +
1.157 + err=close(fd2);
1.158 + test_ok(err==0);
1.159 + }
1.160 +
1.161 +/* Client and server take it in turns to send, starting with the client.
1.162 + * Each matches the message they receive with the string expected
1.163 + */
1.164 +char *message_sequence[] = {
1.165 + "Hello from client",
1.166 + "Hello from server",
1.167 + "Test of send",
1.168 + "Test of recv",
1.169 + "Try sendto",
1.170 + "Try recvfrom",
1.171 + "Send to shutdown socket",
1.172 + "Send to closed socket",
1.173 + 0
1.174 + };
1.175 +
1.176 +/**
1.177 +@SYMTestCaseID SYSLIB-STDLIB-CT-1116
1.178 +@SYMTestCaseDesc Tests for server socket
1.179 +@SYMTestPriority High
1.180 +@SYMTestActions Tests for server socket,create,accept,send and receive functions
1.181 +@SYMTestExpectedResults Test must not fail
1.182 +@SYMREQ REQ0000
1.183 +*/
1.184 +void testServer()
1.185 + {
1.186 + int fd1, fd2, nbytes, i;
1.187 + size_t addrsize;
1.188 + int err;
1.189 + struct sockaddr addr1, addr2;
1.190 + char buf[80];
1.191 + char **mp = message_sequence;
1.192 +
1.193 + test_Next("Create server socket");
1.194 + fd1=socket(AF_LOCAL, SOCK_STREAM, 0);
1.195 + test_ok(fd1>=0);
1.196 +
1.197 +#if 0
1.198 + /* causes ESOCK to panic the client */
1.199 + addrsize=sizeof(addr2);
1.200 + fd2=accept(fd1,&addr2,&addrsize); /* can't accept on an unbound socket */
1.201 + test_ok(fd2<0);
1.202 +#endif
1.203 +
1.204 + addr1.sa_family=AF_LOCAL;
1.205 + addr1.sa_port=1;
1.206 + err=bind(fd1, &addr1, sizeof(addr1));
1.207 + test_ok(err==0);
1.208 +
1.209 +#if 0
1.210 + /* causes ESOCK to panic the client */
1.211 + addrsize=sizeof(addr2);
1.212 + fd2=accept(fd1,&addr2,&addrsize); /* can't accept before listening */
1.213 + test_ok(fd2<0);
1.214 +#endif
1.215 +
1.216 + err=listen(fd1,1);
1.217 + test_ok(err==0);
1.218 +
1.219 + addrsize=sizeof(addr2);
1.220 + fd2=accept(fd1,&addr2,&addrsize);
1.221 + test_ok(fd2>=0);
1.222 + /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
1.223 + test(addr2.sa_port!=1);
1.224 + test(addr2.sa_port!=0);
1.225 + test(addrsize<=sizeof(addr2));
1.226 +
1.227 + test_Next("Server read/write");
1.228 +
1.229 + /* read */
1.230 + nbytes=strlen(*mp);
1.231 + err=read(fd2, buf, nbytes+1);
1.232 + test_ok(err==nbytes+1);
1.233 + test(strcmp(buf,*mp)==0);
1.234 +
1.235 + /* write */
1.236 + mp++;
1.237 + nbytes=strlen(*mp);
1.238 + for (i=0; i<nbytes+1; i++)
1.239 + {
1.240 + err=write(fd2,(*mp)+i,1);
1.241 + test_ok(err==1);
1.242 + }
1.243 +
1.244 + test_Next("Server send/recv");
1.245 +
1.246 + /* recv */
1.247 + mp++;
1.248 + nbytes=strlen(*mp);
1.249 + err=recv(fd2, buf, nbytes+1,0);
1.250 + test_ok(err==nbytes+1);
1.251 + test(strcmp(buf,*mp)==0);
1.252 +
1.253 + /* send */
1.254 + mp++;
1.255 + nbytes=strlen(*mp);
1.256 + err=send(fd2, *mp, nbytes+1,0);
1.257 + test_ok(err==nbytes+1);
1.258 +
1.259 + /* recvfrom */
1.260 + mp++;
1.261 + nbytes=strlen(*mp);
1.262 + addrsize=sizeof(addr2);
1.263 + addr2.sa_port=0;
1.264 + err=recvfrom(fd2, buf, nbytes+1,0,&addr2,&addrsize);
1.265 + test_ok(err==nbytes+1);
1.266 + test(strcmp(buf,*mp)==0);
1.267 + /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
1.268 + test(addr2.sa_port!=1);
1.269 + test(addr2.sa_port!=0);
1.270 + test(addrsize<=sizeof(addr2));
1.271 +
1.272 + /* sendto */
1.273 + mp++;
1.274 + nbytes=strlen(*mp);
1.275 + addrsize=sizeof(addr1);
1.276 + err=sendto(fd2, *mp, nbytes+1,0,&addr1,addrsize); /* not allowed on streams */
1.277 + test_errno(err<0,ENOSYS);
1.278 + err=send(fd2, *mp, nbytes+1,0); /* to keep synchronisation */
1.279 + test_ok(err==nbytes+1);
1.280 +
1.281 + test_Next("Server shutdown reception");
1.282 + err=shutdown(fd2,0);
1.283 + test_ok(err==0);
1.284 +
1.285 + sleep(2); /* so that the client's sleep(1) finishes before we awake */
1.286 +
1.287 + err=close(fd2);
1.288 + test_ok(err==0);
1.289 +
1.290 + err=close(fd1);
1.291 + test_ok(err==0);
1.292 + }
1.293 +
1.294 +/**
1.295 +@SYMTestCaseID SYSLIB-STDLIB-CT-1117
1.296 +@SYMTestCaseDesc Tests for client socket
1.297 +@SYMTestPriority High
1.298 +@SYMTestActions Tests for client socket,create,accept,send and receive functions
1.299 + Write to a connection closed socket and test for error
1.300 +@SYMTestExpectedResults Test must not fail
1.301 +@SYMREQ REQ0000
1.302 +*/
1.303 +void testClient()
1.304 + {
1.305 + int fd1, nbytes, nbytes2, i, status;
1.306 + size_t addrsize;
1.307 + int err;
1.308 + struct sockaddr addr1, addr2;
1.309 + char buf[80];
1.310 + char **mp = message_sequence;
1.311 +
1.312 + test_Next("Create client socket");
1.313 + fd1=socket(AF_LOCAL, SOCK_STREAM, 0);
1.314 + test_ok(fd1>=0);
1.315 +
1.316 + addr1.sa_family=AF_LOCAL;
1.317 + addr1.sa_port=1;
1.318 + addrsize=sizeof(addr1);
1.319 + err=connect(fd1,&addr1,addrsize);
1.320 + test_ok(err==0);
1.321 +
1.322 + addrsize=sizeof(addr2);
1.323 + err=getpeername(fd1,&addr2,&addrsize);
1.324 + test_ok(err==0);
1.325 + /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
1.326 + test(addr2.sa_port==1);
1.327 + test(addrsize<=sizeof(addr2));
1.328 +
1.329 + addrsize=sizeof(addr2);
1.330 + err=getsockname(fd1,&addr2,&addrsize);
1.331 + test_ok(err==0);
1.332 + /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
1.333 + test(addr2.sa_port!=1);
1.334 + test(addr2.sa_port!=0);
1.335 + test(addrsize<=sizeof(addr2));
1.336 +
1.337 + test_Next("Client read/write");
1.338 +
1.339 + /* write */
1.340 + nbytes=strlen(*mp);
1.341 + err=write(fd1, *mp, nbytes+1);
1.342 + test_ok(err==nbytes+1);
1.343 +
1.344 + /* read */
1.345 + mp++;
1.346 + nbytes=strlen(*mp);
1.347 + err=read(fd1, buf, nbytes+1);
1.348 + test_ok(err==nbytes+1);
1.349 + test(strcmp(buf,*mp)==0);
1.350 +
1.351 + test_Next("Client send/recv");
1.352 +
1.353 + /* send */
1.354 + mp++;
1.355 + nbytes=strlen(*mp);
1.356 + for (i=0; i<nbytes+1; i++)
1.357 + {
1.358 + err=send(fd1,(*mp)+i,1,0);
1.359 + test_ok(err==1);
1.360 + }
1.361 +
1.362 + /* recv - get the first byte so that we know the buffer is full */
1.363 + mp++;
1.364 + nbytes=strlen(*mp);
1.365 + err=recv(fd1,buf,1,0);
1.366 + test_ok(err==1);
1.367 +
1.368 + /* ioctl */
1.369 + nbytes2=-1;
1.370 + err=ioctl(fd1,E32IONREAD,&nbytes2);
1.371 +#if 0
1.372 + test_ok(err==0);
1.373 + test(nbytes2==nbytes);
1.374 +#else
1.375 + test_errno(err<0,ENOSYS); /* IPC.PRT doesn't implement KSoReadBytesPending */
1.376 +#endif
1.377 +
1.378 + nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
1.379 + err=ioctl(fd1,E32IOSELECT,&nbytes2);
1.380 + test_ok(err==0);
1.381 + test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
1.382 +
1.383 + nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
1.384 + err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
1.385 + test_ok(err==0);
1.386 + err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
1.387 + test_ok(err==0);
1.388 + if (nbytes2!=(E32SELECT_READ|E32SELECT_WRITE))
1.389 + {
1.390 + nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
1.391 + err=ioctl(fd1,E32IOSELECT,&nbytes2);
1.392 + test_ok(err==0);
1.393 + test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
1.394 + }
1.395 +
1.396 + /* recv - get the rest of the data */
1.397 + for (i=1; i<nbytes+1; i++)
1.398 + {
1.399 + err=recv(fd1,buf+i,1,0);
1.400 + test_ok(err==1);
1.401 + }
1.402 + test(strcmp(buf,*mp)==0);
1.403 +
1.404 + /* ioctl again - this time there is no data pending */
1.405 + nbytes2=-1;
1.406 + err=ioctl(fd1,E32IONREAD,&nbytes2);
1.407 +#if 0
1.408 + test_ok(err==0);
1.409 + test(nbytes2==0);
1.410 +#else
1.411 + test_errno(err<0,ENOSYS); /* IPC.PRT doesn't implement KSoReadBytesPending */
1.412 +#endif
1.413 +
1.414 + nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
1.415 + err=ioctl(fd1,E32IOSELECT,&nbytes2);
1.416 + test_ok(err==0);
1.417 + test(nbytes2==E32SELECT_WRITE);
1.418 +
1.419 + nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
1.420 + err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
1.421 + test_ok(err==0);
1.422 + err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
1.423 + test_ok(err==0);
1.424 + test(nbytes2==E32SELECT_WRITE);
1.425 +
1.426 + /* sendto */
1.427 + mp++;
1.428 + nbytes=strlen(*mp);
1.429 + addrsize=sizeof(addr1);
1.430 + err=sendto(fd1, *mp, nbytes+1,0,&addr1,addrsize);
1.431 + test_errno(err<0,ENOSYS);
1.432 + err=send(fd1, *mp, nbytes+1,0); /* to keep synchronisation */
1.433 + test_ok(err==nbytes+1);
1.434 +
1.435 + /* recvfrom */
1.436 + mp++;
1.437 + nbytes=strlen(*mp);
1.438 + addrsize=sizeof(addr2);
1.439 + addr2.sa_port=0;
1.440 + err=recvfrom(fd1, buf, nbytes+1,0,&addr2,&addrsize);
1.441 + test_ok(err==nbytes+1);
1.442 + test(strcmp(buf,*mp)==0);
1.443 + /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */
1.444 + test(addr2.sa_port==1);
1.445 + test(addrsize<=sizeof(addr2));
1.446 +
1.447 + sleep(1);
1.448 +
1.449 + test_Next("Client write to closed connection");
1.450 +
1.451 + /* write to half-closed socket */
1.452 + mp++;
1.453 + nbytes=strlen(*mp);
1.454 + err=write(fd1, *mp, nbytes+1);
1.455 +#if 0
1.456 + /* IPC doesn't seem to care! */
1.457 + test_errno(err<0, EPIPE);
1.458 +#else
1.459 + test_ok(err==nbytes+1);
1.460 +#endif
1.461 +
1.462 + sleep(2);
1.463 +
1.464 + /* write to a connection closed by the other end */
1.465 + mp++;
1.466 + nbytes=strlen(*mp);
1.467 + err=write(fd1, *mp, nbytes+1);
1.468 + test_errno(err<0, EPIPE);
1.469 +
1.470 + err=close(fd1);
1.471 + test_ok(err==0);
1.472 + }
1.473 +
1.474 +int close_console=0;
1.475 +
1.476 +/**
1.477 +@SYMTestCaseID SYSLIB-STDLIB-CT-1118
1.478 +@SYMTestCaseDesc Tests for STREAM server
1.479 +@SYMTestPriority High
1.480 +@SYMTestActions Tests for server entity
1.481 +@SYMTestExpectedResults Test must not fail
1.482 +@SYMREQ REQ0000
1.483 +*/
1.484 +void testConnection()
1.485 + {
1.486 + int err;
1.487 + void* server;
1.488 +
1.489 + server=create_thread(testServer, "TLSTREAM server");
1.490 + test(server!=0);
1.491 + start_thread(server);
1.492 +
1.493 + sleep(1); /* give the server a chance to get started */
1.494 + testClient();
1.495 +
1.496 + err=wait_for_thread(server);
1.497 + test(err==0);
1.498 +
1.499 + if (close_console)
1.500 + {
1.501 + test_Close();
1.502 + close(0);
1.503 + close(1);
1.504 + close(2);
1.505 + }
1.506 + }
1.507 +
1.508 +int main(int argc, char *argv[])
1.509 + {
1.510 + void* client;
1.511 + int err;
1.512 +
1.513 + test_Title("AF_LOCAL Streams");
1.514 +
1.515 + testSimple();
1.516 + testConnection();
1.517 +
1.518 + test_Next("Do it again using the CPosixServer (for them, not me)");
1.519 + close_console=1;
1.520 +
1.521 + start_posix_server(); /* calls SpawnPosixServer from C++ code */
1.522 +
1.523 + client=create_thread(testSimple, "TLSTREAM simple");
1.524 + test(client!=0);
1.525 + start_thread(client);
1.526 + err=wait_for_thread(client);
1.527 + test(err==0);
1.528 +
1.529 + client=create_thread(testConnection, "TLSTREAM client");
1.530 + test(client!=0);
1.531 + start_thread(client);
1.532 + err=wait_for_thread(client);
1.533 + test(err==0);
1.534 +
1.535 + test_Close();
1.536 + return 0;
1.537 + }
1.538 \ No newline at end of file