1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TISTREAM.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,728 @@
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_INET 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 +#include <libc/netinet/in.h>
1.34 +#include <libc/arpa/inet.h>
1.35 +#include <netdb.h>
1.36 +
1.37 +#include "CTEST.H" /* includes C interface to EPOC32 threads, and SpawnPosixServer */
1.38 +
1.39 +test_Data;
1.40 +
1.41 +struct sockaddr_in testaddr;
1.42 +
1.43 +unsigned short int port = 2001;
1.44 +
1.45 +/**
1.46 +@SYMTestCaseID SYSLIB-STDLIB-CT-1059
1.47 +@SYMTestCaseDesc Tests for AF_INET stream sockets
1.48 +@SYMTestPriority High
1.49 +@SYMTestActions Tests for simple operations on stream sockets,open,bind,get socket name,listen operations and test for error code
1.50 +@SYMTestExpectedResults Test must not fail
1.51 +@SYMREQ REQ0000
1.52 +*/
1.53 +void testSimple()
1.54 + {
1.55 + int fd1, fd2;
1.56 +
1.57 + size_t addrsize;
1.58 + int err;
1.59 + struct sockaddr_in addr1, addr2;
1.60 + int optionbuf[20];
1.61 + size_t optionsize;
1.62 +
1.63 + test_Next("Create stream sockets");
1.64 + fd1=socket(AF_INET, SOCK_STREAM, 0);
1.65 + test_ok(fd1>=0);
1.66 +
1.67 + fd2=socket(AF_INET, SOCK_STREAM, 0);
1.68 + test_ok(fd2>=0);
1.69 +
1.70 + test_Next("Some binding tests");
1.71 +
1.72 + addr1.sin_family=2061;
1.73 + addr1.sin_port=htons(65530);
1.74 + addr1.sin_addr.s_addr=htonl(0x11223344);
1.75 + err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1)); /* wrong family, port out of range */
1.76 + test_errno(err<0,ENOENT);
1.77 +
1.78 + addr1=testaddr;
1.79 + addr1.sin_port=htons(port);
1.80 + err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1));
1.81 + test_ok(err==0);
1.82 +
1.83 + addr1=testaddr;
1.84 + addr1.sin_port=htons((unsigned short int)(port+1));
1.85 + err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1)); /* already bound */
1.86 + test_errno(err!=0, EEXIST);
1.87 +
1.88 + test_Next("Get associated addresses");
1.89 +
1.90 + addrsize=sizeof(addr2);
1.91 + err=getsockname(fd1,(struct sockaddr*)&addr2,&addrsize);
1.92 + test_ok(err==0);
1.93 + test(addr2.sin_family==AF_INET);
1.94 + test(addr2.sin_port==htons(port));
1.95 + test(addrsize<=sizeof(addr2));
1.96 +
1.97 + addrsize=sizeof(addr2);
1.98 + err=getpeername(fd1,(struct sockaddr*)&addr2,&addrsize); /* not connected */
1.99 + test_errno(err!=0,-2); /* OMISSION - can't report proper reason for failure */
1.100 +
1.101 + addrsize=sizeof(addr2);
1.102 + err=getsockname(fd2,(struct sockaddr*)&addr2,&addrsize); /* not bound */
1.103 + test_errno(err!=0,-2); /* OMISSION - can't report proper reason for failure */
1.104 +
1.105 + test_Next("More binding");
1.106 +
1.107 + addr1=testaddr;
1.108 + addr1.sin_port=htons(port);
1.109 + err=bind(fd2, (struct sockaddr*)&addr1, sizeof(addr1)); /* address in use */
1.110 + test_errno(err!=0, EACCES);
1.111 +
1.112 + addr1=testaddr;
1.113 + addr1.sin_port=0;
1.114 + err=bind(fd2, (struct sockaddr*)&addr1, sizeof(addr1)); /* unspecified port number */
1.115 + test_ok(err==0);
1.116 +
1.117 + addrsize=sizeof(addr2);
1.118 + err=getsockname(fd2,(struct sockaddr*)&addr2,&addrsize);
1.119 + test_ok(err==0);
1.120 + test(addr2.sin_family==AF_INET);
1.121 + test(addrsize<=sizeof(addr2));
1.122 + test(addr2.sin_port!=htons(port));
1.123 + test(addr2.sin_port!=0);
1.124 + {
1.125 + unsigned short port=ntohs(addr2.sin_port);
1.126 + test(port>=IPPORT_RESERVED);
1.127 + /* NB. TCPIP 030 has 9999 for IPPORT_USERRESERVED, but TCPIP 031 dropped it to 5000 */
1.128 + test(port<=IPPORT_USERRESERVED);
1.129 + printf(" allocated port %d\n",port);
1.130 + }
1.131 +
1.132 + err=listen(fd1,1);
1.133 + test_ok(err==0);
1.134 +
1.135 + test_Next("Socket options");
1.136 +
1.137 + optionbuf[0]=3500000; /* implausible size */
1.138 + optionsize=sizeof(optionbuf[0]);
1.139 + err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
1.140 + test_ok(err==0);
1.141 + test(optionbuf[0]!=3500000);
1.142 +
1.143 + optionbuf[0]=7*1024;
1.144 + optionsize=sizeof(optionbuf[0]);
1.145 + err=setsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,optionsize);
1.146 + test_ok(err==0);
1.147 +
1.148 + optionbuf[0]=3500000; /* implausible size */
1.149 + optionsize=sizeof(optionbuf[0]);
1.150 + err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
1.151 + test_ok(err==0);
1.152 + test(optionbuf[0]==7*1024);
1.153 +
1.154 + optionbuf[0]=1;
1.155 + optionsize=sizeof(optionbuf[0]);
1.156 + err=getsockopt(fd1,SOL_SOCKET,1234,optionbuf,&optionsize); /* invalid option */
1.157 + test_errno(err<0,ENOSYS);
1.158 + test(optionbuf[0]==1);
1.159 +
1.160 + optionbuf[0]=13;
1.161 + optionsize=sizeof(optionbuf[0]);
1.162 + err=setsockopt(fd1,SOL_SOCKET,1234,optionbuf,optionsize); /* invalid option */
1.163 + test_errno(err<0,ENOSYS);
1.164 + test(optionbuf[0]==13);
1.165 +
1.166 + err=close(fd1);
1.167 + test_ok(err==0);
1.168 +
1.169 + err=close(fd2);
1.170 + test_ok(err==0);
1.171 + }
1.172 +
1.173 +/* Client and server take it in turns to send, starting with the client.
1.174 + * Each matches the message they receive with the string expected
1.175 + */
1.176 +char *message_sequence[] = {
1.177 + "Hello from client",
1.178 + "Hello from server",
1.179 + "Test of send",
1.180 + "Test of recv",
1.181 + "Try sendto",
1.182 + "Try recvfrom",
1.183 + "Try sendto again",
1.184 + "Try recvfrom again",
1.185 + "Send to shutdown socket",
1.186 + "Send to closed socket",
1.187 + 0
1.188 + };
1.189 +
1.190 +/**
1.191 +@SYMTestCaseID SYSLIB-STDLIB-CT-1060
1.192 +@SYMTestCaseDesc Tests for server socket
1.193 +@SYMTestPriority High
1.194 +@SYMTestActions Create a steam socket,bind and test for a transmission process.Check for error codes
1.195 +@SYMTestExpectedResults Test must not fail
1.196 +@SYMREQ REQ0000
1.197 +*/
1.198 +void testServer()
1.199 + {
1.200 + int fd1,fd2,nbytes,i;
1.201 + size_t addrsize;
1.202 + int err;
1.203 + struct sockaddr_in addr1, addr2;
1.204 + char buf[80];
1.205 + char **mp = message_sequence;
1.206 + static int randbuf[10000];
1.207 +
1.208 + fd1=fd2=nbytes=i=0;
1.209 +
1.210 + /* Fill the buffer with a predetermined random number sequence */
1.211 + randbuf[0]=4441302;
1.212 + srand(randbuf[0]);
1.213 + for (i=1; i<10000; i++)
1.214 + randbuf[i]=rand();
1.215 +
1.216 +
1.217 + test_Next("Create server socket");
1.218 + fd1=socket(AF_INET, SOCK_STREAM, 0);
1.219 + test_ok(fd1>=0);
1.220 +
1.221 +#if 0
1.222 + /* causes ESOCK to panic the client */
1.223 + addrsize=sizeof(addr2);
1.224 + fd2=accept(fd1,&addr2,&addrsize); /* can't accept on an unbound socket */
1.225 + test_ok(fd2<0);
1.226 +#endif
1.227 +
1.228 + IN_SET_LOOPBACK_ADDR(&addr1);
1.229 + addr1.sin_port=htons(port);
1.230 + err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1));
1.231 + test_ok(err==0);
1.232 +
1.233 +#if 0
1.234 + /* causes ESOCK to panic the client */
1.235 + addrsize=sizeof(addr2);
1.236 + fd2=accept(fd1,&addr2,&addrsize); /* can't accept before listening */
1.237 + test_ok(fd2<0);
1.238 +#endif
1.239 +
1.240 + err=listen(fd1,1);
1.241 + test_ok(err==0);
1.242 +
1.243 + addrsize=sizeof(addr2);
1.244 + fd2=accept(fd1,(struct sockaddr*)&addr2,&addrsize);
1.245 + test_ok(fd2>=0);
1.246 + test(addr2.sin_family==AF_INET);
1.247 + test(addr2.sin_port!=htons(port));
1.248 + test(addr2.sin_port!=0);
1.249 + test(addrsize<=sizeof(addr2));
1.250 +
1.251 + test_Next("Server read/write");
1.252 +
1.253 + /* read */
1.254 + nbytes=strlen(*mp);
1.255 + err=read(fd2, buf, nbytes+1);
1.256 + test_ok(err==nbytes+1);
1.257 + test(strcmp(buf,*mp)==0);
1.258 +
1.259 + /* write */
1.260 + mp++;
1.261 + nbytes=strlen(*mp);
1.262 + for (i=0; i<nbytes+1; i++)
1.263 + {
1.264 + err=write(fd2,(*mp)+i,1);
1.265 + test_ok(err==1);
1.266 + }
1.267 +
1.268 + test_Next("Server send/recv");
1.269 +
1.270 + /* recv */
1.271 + mp++;
1.272 + nbytes=strlen(*mp);
1.273 + err=recv(fd2, buf, sizeof(buf) ,0);
1.274 + test_ok(err==nbytes+1);
1.275 + test(strcmp(buf,*mp)==0);
1.276 +
1.277 + /* send */
1.278 + mp++;
1.279 + nbytes=strlen(*mp);
1.280 + err=send(fd2, *mp, nbytes+1,0);
1.281 + test_ok(err==nbytes+1);
1.282 +
1.283 + /* recvfrom */
1.284 + mp++;
1.285 + nbytes=strlen(*mp);
1.286 + addrsize=sizeof(addr2);
1.287 + addr2.sin_port=0;
1.288 + err=recvfrom(fd2, buf, nbytes+1,0,(struct sockaddr*)&addr2,&addrsize);
1.289 +/*
1.290 + memset(&buf[0],0, sizeof(buf));
1.291 + err=recvfrom(fd2, buf, nbytes+1,0,0,0);
1.292 +*/
1.293 + test_ok(err==nbytes+1);
1.294 + test(strcmp(buf,*mp)==0);
1.295 + test(addr2.sin_family==AF_INET);
1.296 + test(addr2.sin_port!=htons(port));
1.297 + test(addr2.sin_port!=0);
1.298 + test(addrsize<=sizeof(addr2));
1.299 +
1.300 + /* sendto */
1.301 + mp++;
1.302 + nbytes=strlen(*mp);
1.303 + addrsize=sizeof(addr1);
1.304 + err=sendto(fd2, *mp, nbytes+1,0,(struct sockaddr*)&addr1,addrsize); /* not allowed on streams */
1.305 + test_errno(err<0,ENOSYS);
1.306 + err=send(fd2, *mp, nbytes+1,0); /* to keep synchronisation */
1.307 + test_ok(err==nbytes+1);
1.308 +
1.309 +
1.310 + test_Next("Server revcfrom with null fromaddr");
1.311 + /* recvfrom again*/
1.312 + mp++;
1.313 + nbytes=strlen(*mp);
1.314 + memset(&buf[0],0xFF, sizeof(buf));
1.315 + err=recvfrom(fd2, buf, nbytes+1,0,0,0);
1.316 + test_ok(err==nbytes+1);
1.317 + test(strcmp(buf,*mp)==0);
1.318 +
1.319 + /* sendto again*/
1.320 + mp++;
1.321 + nbytes=strlen(*mp);
1.322 + addrsize=sizeof(addr1);
1.323 + err=send(fd2, *mp, nbytes+1,0); /* to keep synchronisation */
1.324 + test_ok(err==nbytes+1);
1.325 +
1.326 +
1.327 +
1.328 + /*
1.329 + test_Next("Server shutdown transmission");
1.330 + err=shutdown(fd2,1);
1.331 + test_ok(err==0);
1.332 +
1.333 + sleep(4); // so that the client's sleep(1) finishes before we awake
1.334 +
1.335 + mp++;
1.336 + nbytes=strlen(*mp);
1.337 + addrsize=sizeof(addr1);
1.338 + err=send(fd2, *mp, nbytes+1,0);
1.339 + test_errno(err<0,EPIPE);
1.340 +
1.341 + nbytes=strlen(*mp);
1.342 + err=recv(fd2, buf, nbytes+1,0);
1.343 + test_ok(err==nbytes+1);
1.344 + test(strcmp(buf,*mp)==0);
1.345 +
1.346 + err=close(fd2);
1.347 + test_ok(err==0);
1.348 +
1.349 + // Large transmission
1.350 +*/
1.351 + test_Next("Server large transfer");
1.352 +
1.353 + addrsize=sizeof(addr2);
1.354 + fd2=accept(fd1,(struct sockaddr*)&addr2,&addrsize);
1.355 + test_ok(fd2>=0);
1.356 + test(addr2.sin_family==AF_INET);
1.357 + test(addr2.sin_port!=htons(port));
1.358 + test(addr2.sin_port!=0);
1.359 + test(addrsize<=sizeof(addr2));
1.360 +
1.361 + nbytes=sizeof(randbuf);
1.362 + err=write(fd2,(const char*)randbuf,nbytes);
1.363 + test_ok(err==nbytes);
1.364 +
1.365 + test_Next("Server close");
1.366 +
1.367 +// err=shutdown(fd2,1);
1.368 +// test_ok(err==0);
1.369 +
1.370 +// sleep(4);
1.371 +
1.372 + err=close(fd2);
1.373 + test_ok(err==0);
1.374 +
1.375 + err=close(fd1);
1.376 + test_ok(err==0);
1.377 + }
1.378 +
1.379 +/**
1.380 +@SYMTestCaseID SYSLIB-STDLIB-CT-1061
1.381 +@SYMTestCaseDesc Tests for client socket
1.382 +@SYMTestPriority High
1.383 +@SYMTestActions Create a steam socket,bind and test for a transmission process.Check for error codes
1.384 +@SYMTestExpectedResults Test must not fail
1.385 +@SYMREQ REQ0000
1.386 +*/
1.387 +void testClient()
1.388 + {
1.389 + int fd1, nbytes, nbytes2, i, status;
1.390 +
1.391 + size_t addrsize;
1.392 + int err;
1.393 + struct sockaddr_in addr1, addr2;
1.394 + char buf[80];
1.395 + char **mp = message_sequence;
1.396 + static int randbuf[10001];
1.397 +
1.398 + test_Next("Create client socket");
1.399 + fd1=socket(AF_INET, SOCK_STREAM, 0);
1.400 + test_ok(fd1>=0);
1.401 +
1.402 + sleep(5); /* give server a chance to run */
1.403 +
1.404 + addr1=testaddr;
1.405 + addr1.sin_port=htons(port);
1.406 + addrsize=sizeof(addr1);
1.407 + err=connect(fd1,(struct sockaddr*)&addr1,addrsize);
1.408 + test_ok(err==0);
1.409 +
1.410 + addrsize=sizeof(addr2);
1.411 + err=getpeername(fd1,(struct sockaddr*)&addr2,&addrsize);
1.412 + test_ok(err==0);
1.413 + test(addr2.sin_family==AF_INET);
1.414 + test(addr2.sin_port==htons(port));
1.415 + test(addrsize<=sizeof(addr2));
1.416 +
1.417 + addrsize=sizeof(addr2);
1.418 + err=getsockname(fd1,(struct sockaddr*)&addr2,&addrsize);
1.419 + test_ok(err==0);
1.420 + test(addr2.sin_family==AF_INET);
1.421 + test(addr2.sin_port!=htons(port));
1.422 + test(addr2.sin_port!=0);
1.423 + test(addrsize<=sizeof(addr2));
1.424 +
1.425 + test_Next("Client read/write");
1.426 +
1.427 + /* write */
1.428 + nbytes=strlen(*mp);
1.429 + err=write(fd1, *mp, nbytes+1);
1.430 + test_ok(err==nbytes+1);
1.431 +
1.432 + /* read */
1.433 + mp++;
1.434 + nbytes=strlen(*mp);
1.435 + err=read(fd1, buf, nbytes+1);
1.436 + test_ok(err==nbytes+1);
1.437 + test(strcmp(buf,*mp)==0);
1.438 +
1.439 + test_Next("Client send/recv");
1.440 +
1.441 + /* send */
1.442 + mp++;
1.443 + nbytes=strlen(*mp);
1.444 + err=send(fd1,*mp, nbytes+1,0);
1.445 + test_ok(err==nbytes+1);
1.446 +
1.447 + /* recv - get the first 2 bytes so that we know the buffer is full */
1.448 + mp++;
1.449 + nbytes=strlen(*mp);
1.450 + err=recv(fd1,buf,2,0);
1.451 + test_ok(err==2);
1.452 +
1.453 + /* ioctl */
1.454 +
1.455 + nbytes2=-1;
1.456 + err=ioctl(fd1,E32IONREAD,&nbytes2);
1.457 + test_ok(err==0);
1.458 + test(nbytes2==(nbytes+1-2));
1.459 +
1.460 + nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
1.461 + err=ioctl(fd1,E32IOSELECT,&nbytes2);
1.462 + test_ok(err==0);
1.463 + test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
1.464 +
1.465 + nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
1.466 + err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
1.467 + test_ok(err==0);
1.468 + err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
1.469 + test_ok(err==0);
1.470 + if (nbytes2!=(E32SELECT_READ|E32SELECT_WRITE))
1.471 + {
1.472 + nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
1.473 + err=ioctl(fd1,E32IOSELECT,&nbytes2);
1.474 + test_ok(err==0);
1.475 + test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
1.476 + }
1.477 +
1.478 + /* recv - get the rest of the data */
1.479 + for (i=2; i<nbytes+1; i++)
1.480 + {
1.481 + err=recv(fd1,buf+i,1,0);
1.482 + test_ok(err==1);
1.483 + }
1.484 + test(strcmp(buf,*mp)==0);
1.485 +
1.486 + /* ioctl again - this time there is no data pending */
1.487 + nbytes2=-1;
1.488 + err=ioctl(fd1,E32IONREAD,&nbytes2);
1.489 + test_ok(err==0);
1.490 + test(nbytes2==0);
1.491 +
1.492 + nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
1.493 + err=ioctl(fd1,E32IOSELECT,&nbytes2);
1.494 + test_ok(err==0);
1.495 + test(nbytes2==E32SELECT_WRITE);
1.496 +
1.497 + nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
1.498 + err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
1.499 + test_ok(err==0);
1.500 + err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
1.501 + test_ok(err==0);
1.502 + test(nbytes2==E32SELECT_WRITE);
1.503 +
1.504 + /* sendto */
1.505 + mp++;
1.506 + nbytes=strlen(*mp);
1.507 + addrsize=sizeof(addr1);
1.508 + err=sendto(fd1, *mp, nbytes+1,0,(struct sockaddr*)&addr1,addrsize);
1.509 + test_errno(err<0,ENOSYS);
1.510 + err=send(fd1, *mp, nbytes+1,0); /* to keep synchronisation */
1.511 + test_ok(err==nbytes+1);
1.512 +
1.513 + /* recvfrom */
1.514 + mp++;
1.515 + nbytes=strlen(*mp);
1.516 + addrsize=sizeof(addr2);
1.517 + addr2.sin_port=0;
1.518 + err=recvfrom(fd1, buf, nbytes+1,0,(struct sockaddr*)&addr2,&addrsize);
1.519 + test_ok(err==nbytes+1);
1.520 + test(strcmp(buf,*mp)==0);
1.521 + test(addr2.sin_family==AF_INET);
1.522 + test(addr2.sin_port==htons(port));
1.523 + test(addrsize<=sizeof(addr2));
1.524 +
1.525 + /* another sendto */
1.526 + test_Next("Client revcfrom with null fromaddr");
1.527 + mp++;
1.528 + nbytes=strlen(*mp);
1.529 + addrsize=sizeof(addr1);
1.530 + err=sendto(fd1, *mp, nbytes+1,0,(struct sockaddr*)&addr1,addrsize);
1.531 + test_errno(err<0,ENOSYS);
1.532 + err=send(fd1, *mp, nbytes+1,0); /* to keep synchronisation */
1.533 + test_ok(err==nbytes+1);
1.534 +
1.535 + /* recvfrom */
1.536 + mp++;
1.537 + nbytes=strlen(*mp);
1.538 + addrsize=sizeof(addr2);
1.539 + memset(buf,0xFF,sizeof(buf));
1.540 + err=recvfrom(fd1, buf, nbytes+1,0,0,0); //do a recvfrom with a null address
1.541 + test_ok(err==nbytes+1);
1.542 + test(strcmp(buf,*mp)==0);
1.543 +
1.544 +
1.545 +
1.546 + sleep(2);
1.547 +/*
1.548 + test_Next("Test half-closed connection");
1.549 +
1.550 + // read from half-closed socket
1.551 + mp++;
1.552 + nbytes=strlen(*mp);
1.553 + err=read(fd1, buf, nbytes+1);
1.554 + test_errno(err<0, EPIPE);
1.555 +
1.556 + nbytes=strlen(*mp);
1.557 + err=write(fd1,*mp,nbytes+1);
1.558 + test_ok(err==nbytes+1);
1.559 +
1.560 + sleep(2);
1.561 +
1.562 + // read from a connection closed by the other end
1.563 + mp++;
1.564 + nbytes=strlen(*mp);
1.565 + err=read(fd1,buf,nbytes+1);
1.566 + test_errno(err<0, EPIPE);
1.567 +*/
1.568 + err=close(fd1);
1.569 + test_ok(err==0);
1.570 +
1.571 + // Large transmission - and another connection to the server socket
1.572 +
1.573 + test_Next("Test large transmission");
1.574 +
1.575 + fd1=socket(AF_INET, SOCK_STREAM, 0);
1.576 + test_ok(fd1>=0);
1.577 +
1.578 + sleep(2); /* give server a chance to run */
1.579 +
1.580 + addr1=testaddr;
1.581 + addr1.sin_port=htons(port);
1.582 + addrsize=sizeof(addr1);
1.583 +
1.584 + //this connect fails. Why is this?
1.585 + err=connect(fd1,(struct sockaddr*)&addr1,addrsize);
1.586 + test_ok(err==0);
1.587 +
1.588 +
1.589 + addrsize=sizeof(addr2);
1.590 + err=getpeername(fd1,(struct sockaddr*)&addr2,&addrsize);
1.591 + test_ok(err==0);
1.592 + test(addr2.sin_family==AF_INET);
1.593 + test(addr2.sin_port==htons(port));
1.594 + test(addrsize<=sizeof(addr2));
1.595 +
1.596 + addrsize=sizeof(addr2);
1.597 + err=getsockname(fd1,(struct sockaddr*)&addr2,&addrsize);
1.598 + test_ok(err==0);
1.599 + test(addr2.sin_family==AF_INET);
1.600 + test(addr2.sin_port!=htons(port));
1.601 + test(addr2.sin_port!=0);
1.602 + test(addrsize<=sizeof(addr2));
1.603 +
1.604 + nbytes=0;
1.605 + {
1.606 + int buf;
1.607 + char * p;
1.608 +
1.609 + do
1.610 + {
1.611 + buf=sizeof(randbuf)-nbytes;
1.612 + if (buf>4095)
1.613 + buf=4095;
1.614 + p = (char*)randbuf+nbytes;
1.615 +// err=read(fd1, p, buf);
1.616 + err=recv(fd1, p, buf, 0);
1.617 + if (err>0)
1.618 + nbytes+=err;
1.619 + }
1.620 + while (nbytes<sizeof(randbuf) && err>0);
1.621 + }
1.622 +
1.623 + printf("\nnbytes=%d, err=%d, errno=%d\n", nbytes, err, errno);
1.624 +
1.625 + test_ok(err==0); /* want a graceful EOF */
1.626 + test(nbytes==sizeof(randbuf)-sizeof(int)); /* expect a smaller test data set */
1.627 + test(randbuf[0]==4441302);
1.628 + srand(randbuf[0]);
1.629 + for (i=1; i<10000; i++)
1.630 + test(randbuf[i]==rand());
1.631 +
1.632 +
1.633 +
1.634 + err=recv(fd1, (char*)randbuf, 1, 0);
1.635 + test_ok(err==0);
1.636 +
1.637 + err=close(fd1);
1.638 + test_ok(err==0);
1.639 +
1.640 + }
1.641 +
1.642 +int close_console=0;
1.643 +void testConnection()
1.644 + {
1.645 + int err;
1.646 + void* server;
1.647 + char buf[100];
1.648 +
1.649 + sprintf(buf, "TISTREAM server %d", close_console);
1.650 + server=create_thread(testServer, buf);
1.651 + test(server!=0);
1.652 + start_thread(server);
1.653 +
1.654 + sleep(2); /* give the server a chance to get started */
1.655 + testClient();
1.656 +
1.657 + err=wait_for_thread(server);
1.658 + test(err==0);
1.659 +
1.660 + if (close_console)
1.661 + {
1.662 + test_Close();
1.663 + close(0);
1.664 + close(1);
1.665 + close(2);
1.666 + }
1.667 + }
1.668 +
1.669 +int main(int argc, char *argv[])
1.670 + {
1.671 + void* client;
1.672 + int err;
1.673 +
1.674 + test_Title("AF_INET Streams");
1.675 +
1.676 + err=CommInit(0); /* ensure a workable comms environment */
1.677 + test(err==0);
1.678 +
1.679 + IN_SET_LOOPBACK_ADDR(&testaddr);
1.680 +
1.681 + if (argc==1)
1.682 + {
1.683 + // Run the test(s) without a CPosixServer first
1.684 + testSimple();
1.685 + port +=2;
1.686 + testConnection();
1.687 + port +=2;
1.688 + }
1.689 +
1.690 +
1.691 + test_Next("Test gethostname");
1.692 + {
1.693 + char * hname;
1.694 + hname = (char*)malloc(200);
1.695 + if (hname)
1.696 + {
1.697 + err = gethostname(hname, 200);
1.698 +
1.699 + if (err)
1.700 + printf("gethostname, err=%d, errno=%d\n", err, errno);
1.701 + else
1.702 + printf("host name is %s\n", hname);
1.703 +
1.704 + free(hname); }
1.705 + else
1.706 + printf("host name - malloc failure\n");
1.707 +
1.708 + }
1.709 +
1.710 + test_Next("Do it again using the CPosixServer (for them, not me)");
1.711 + close_console=1;
1.712 +
1.713 + start_posix_server(); /* calls SpawnPosixServer from C++ code */
1.714 +
1.715 +
1.716 + client=create_thread(testSimple, "TISTREAM simple");
1.717 + test(client!=0);
1.718 + start_thread(client);
1.719 + err=wait_for_thread(client);
1.720 + test(err==0);
1.721 + port +=2;
1.722 +
1.723 + client=create_thread(testConnection, "TISTREAM client");
1.724 + test(client!=0);
1.725 + start_thread(client);
1.726 + err=wait_for_thread(client);
1.727 + test(err==0);
1.728 +
1.729 + test_Close();
1.730 + return 0;
1.731 + }