os/ossrv/genericopenlibs/cstdlib/TSTLIB/TISTREAM.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description:
sl@0
    15
* Test of AF_INET stream sockets
sl@0
    16
* 
sl@0
    17
*
sl@0
    18
*/
sl@0
    19
sl@0
    20
sl@0
    21
sl@0
    22
#include <stdio.h>
sl@0
    23
#include <errno.h>
sl@0
    24
#include <stdlib.h>
sl@0
    25
#include <string.h>
sl@0
    26
#include <unistd.h>
sl@0
    27
#include <sys/types.h>
sl@0
    28
#include <sys/socket.h>
sl@0
    29
#include <sys/ioctl.h>
sl@0
    30
#include <libc/netinet/in.h>
sl@0
    31
#include <libc/arpa/inet.h>
sl@0
    32
#include <netdb.h>
sl@0
    33
sl@0
    34
#include "CTEST.H"	/* includes C interface to EPOC32 threads, and SpawnPosixServer */
sl@0
    35
sl@0
    36
test_Data;
sl@0
    37
sl@0
    38
struct sockaddr_in testaddr;
sl@0
    39
sl@0
    40
unsigned short int port = 2001;
sl@0
    41
sl@0
    42
/**
sl@0
    43
@SYMTestCaseID          SYSLIB-STDLIB-CT-1059
sl@0
    44
@SYMTestCaseDesc	    Tests for AF_INET stream sockets 
sl@0
    45
@SYMTestPriority 	    High
sl@0
    46
@SYMTestActions  	    Tests for simple operations on stream sockets,open,bind,get socket name,listen operations and test for error code
sl@0
    47
@SYMTestExpectedResults Test must not fail
sl@0
    48
@SYMREQ                 REQ0000
sl@0
    49
*/		
sl@0
    50
void testSimple()
sl@0
    51
	{
sl@0
    52
	int fd1, fd2;
sl@0
    53
sl@0
    54
	size_t addrsize;
sl@0
    55
	int err;
sl@0
    56
	struct sockaddr_in addr1, addr2;
sl@0
    57
	int optionbuf[20];
sl@0
    58
	size_t optionsize;
sl@0
    59
sl@0
    60
	test_Next("Create stream sockets");
sl@0
    61
	fd1=socket(AF_INET, SOCK_STREAM, 0);
sl@0
    62
	test_ok(fd1>=0);
sl@0
    63
sl@0
    64
	fd2=socket(AF_INET, SOCK_STREAM, 0);
sl@0
    65
	test_ok(fd2>=0);
sl@0
    66
sl@0
    67
	test_Next("Some binding tests");
sl@0
    68
sl@0
    69
	addr1.sin_family=2061;
sl@0
    70
	addr1.sin_port=htons(65530);
sl@0
    71
	addr1.sin_addr.s_addr=htonl(0x11223344);
sl@0
    72
	err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1));	/* wrong family, port out of range */
sl@0
    73
	test_errno(err<0,ENOENT);
sl@0
    74
sl@0
    75
	addr1=testaddr;
sl@0
    76
	addr1.sin_port=htons(port);
sl@0
    77
	err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1));
sl@0
    78
	test_ok(err==0);
sl@0
    79
sl@0
    80
	addr1=testaddr;
sl@0
    81
	addr1.sin_port=htons((unsigned short int)(port+1));
sl@0
    82
	err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1));	/* already bound */
sl@0
    83
	test_errno(err!=0, EEXIST);
sl@0
    84
sl@0
    85
	test_Next("Get associated addresses");
sl@0
    86
sl@0
    87
	addrsize=sizeof(addr2);
sl@0
    88
	err=getsockname(fd1,(struct sockaddr*)&addr2,&addrsize);
sl@0
    89
	test_ok(err==0);
sl@0
    90
	test(addr2.sin_family==AF_INET);
sl@0
    91
	test(addr2.sin_port==htons(port));
sl@0
    92
	test(addrsize<=sizeof(addr2));
sl@0
    93
sl@0
    94
	addrsize=sizeof(addr2);
sl@0
    95
	err=getpeername(fd1,(struct sockaddr*)&addr2,&addrsize);	/* not connected */
sl@0
    96
	test_errno(err!=0,-2);	/* OMISSION - can't report proper reason for failure */
sl@0
    97
sl@0
    98
	addrsize=sizeof(addr2);
sl@0
    99
	err=getsockname(fd2,(struct sockaddr*)&addr2,&addrsize);	/* not bound */
sl@0
   100
	test_errno(err!=0,-2);	/* OMISSION - can't report proper reason for failure */
sl@0
   101
sl@0
   102
	test_Next("More binding");
sl@0
   103
sl@0
   104
	addr1=testaddr;
sl@0
   105
	addr1.sin_port=htons(port);
sl@0
   106
	err=bind(fd2, (struct sockaddr*)&addr1, sizeof(addr1));	/* address in use */
sl@0
   107
	test_errno(err!=0, EACCES);
sl@0
   108
sl@0
   109
	addr1=testaddr;
sl@0
   110
	addr1.sin_port=0;
sl@0
   111
	err=bind(fd2, (struct sockaddr*)&addr1, sizeof(addr1));	/* unspecified port number */
sl@0
   112
	test_ok(err==0);
sl@0
   113
sl@0
   114
	addrsize=sizeof(addr2);
sl@0
   115
	err=getsockname(fd2,(struct sockaddr*)&addr2,&addrsize);
sl@0
   116
	test_ok(err==0);
sl@0
   117
	test(addr2.sin_family==AF_INET);
sl@0
   118
	test(addrsize<=sizeof(addr2));
sl@0
   119
	test(addr2.sin_port!=htons(port));
sl@0
   120
	test(addr2.sin_port!=0);
sl@0
   121
	{
sl@0
   122
		unsigned short port=ntohs(addr2.sin_port);
sl@0
   123
		test(port>=IPPORT_RESERVED);
sl@0
   124
		/* NB. TCPIP 030 has 9999 for IPPORT_USERRESERVED, but TCPIP 031 dropped it to 5000 */
sl@0
   125
		test(port<=IPPORT_USERRESERVED);
sl@0
   126
		printf("  allocated port %d\n",port);
sl@0
   127
	}
sl@0
   128
sl@0
   129
	err=listen(fd1,1);
sl@0
   130
	test_ok(err==0);
sl@0
   131
sl@0
   132
	test_Next("Socket options");
sl@0
   133
sl@0
   134
	optionbuf[0]=3500000;	/* implausible size */
sl@0
   135
	optionsize=sizeof(optionbuf[0]);
sl@0
   136
	err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
sl@0
   137
	test_ok(err==0);
sl@0
   138
	test(optionbuf[0]!=3500000);
sl@0
   139
sl@0
   140
	optionbuf[0]=7*1024;
sl@0
   141
	optionsize=sizeof(optionbuf[0]);
sl@0
   142
	err=setsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,optionsize);
sl@0
   143
	test_ok(err==0);
sl@0
   144
sl@0
   145
	optionbuf[0]=3500000;	/* implausible size */
sl@0
   146
	optionsize=sizeof(optionbuf[0]);
sl@0
   147
	err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
sl@0
   148
	test_ok(err==0);
sl@0
   149
	test(optionbuf[0]==7*1024);
sl@0
   150
sl@0
   151
	optionbuf[0]=1;
sl@0
   152
	optionsize=sizeof(optionbuf[0]);
sl@0
   153
	err=getsockopt(fd1,SOL_SOCKET,1234,optionbuf,&optionsize);	/* invalid option */
sl@0
   154
	test_errno(err<0,ENOSYS);
sl@0
   155
	test(optionbuf[0]==1);
sl@0
   156
sl@0
   157
	optionbuf[0]=13;
sl@0
   158
	optionsize=sizeof(optionbuf[0]);
sl@0
   159
	err=setsockopt(fd1,SOL_SOCKET,1234,optionbuf,optionsize);	/* invalid option */
sl@0
   160
	test_errno(err<0,ENOSYS);
sl@0
   161
	test(optionbuf[0]==13);
sl@0
   162
sl@0
   163
	err=close(fd1);
sl@0
   164
	test_ok(err==0);
sl@0
   165
sl@0
   166
	err=close(fd2);
sl@0
   167
	test_ok(err==0);
sl@0
   168
	}
sl@0
   169
sl@0
   170
/* Client and server take it in turns to send, starting with the client.
sl@0
   171
 * Each matches the message they receive with the string expected
sl@0
   172
 */
sl@0
   173
char *message_sequence[] = {
sl@0
   174
	"Hello from client",
sl@0
   175
	"Hello from server",
sl@0
   176
	"Test of send",
sl@0
   177
	"Test of recv",
sl@0
   178
	"Try sendto",
sl@0
   179
	"Try recvfrom",
sl@0
   180
	"Try sendto again",
sl@0
   181
	"Try recvfrom again",
sl@0
   182
	"Send to shutdown socket",
sl@0
   183
	"Send to closed socket",
sl@0
   184
	0
sl@0
   185
	};
sl@0
   186
sl@0
   187
/**
sl@0
   188
@SYMTestCaseID          SYSLIB-STDLIB-CT-1060
sl@0
   189
@SYMTestCaseDesc	    Tests for server socket
sl@0
   190
@SYMTestPriority 	    High
sl@0
   191
@SYMTestActions  	    Create a steam socket,bind and test for a transmission process.Check for error codes 
sl@0
   192
@SYMTestExpectedResults Test must not fail
sl@0
   193
@SYMREQ                 REQ0000
sl@0
   194
*/		
sl@0
   195
void testServer()
sl@0
   196
	{
sl@0
   197
	int fd1,fd2,nbytes,i;
sl@0
   198
	size_t addrsize;
sl@0
   199
	int err;
sl@0
   200
	struct sockaddr_in addr1, addr2;
sl@0
   201
	char buf[80];
sl@0
   202
	char **mp = message_sequence;
sl@0
   203
	static int randbuf[10000];
sl@0
   204
sl@0
   205
	 fd1=fd2=nbytes=i=0;
sl@0
   206
sl@0
   207
	 /* Fill the buffer with a predetermined random number sequence */
sl@0
   208
	randbuf[0]=4441302;
sl@0
   209
	srand(randbuf[0]);
sl@0
   210
	for (i=1; i<10000; i++)
sl@0
   211
		randbuf[i]=rand();
sl@0
   212
sl@0
   213
sl@0
   214
	test_Next("Create server socket");
sl@0
   215
	fd1=socket(AF_INET, SOCK_STREAM, 0);
sl@0
   216
	test_ok(fd1>=0);
sl@0
   217
sl@0
   218
#if 0
sl@0
   219
	/* causes ESOCK to panic the client */
sl@0
   220
	addrsize=sizeof(addr2);
sl@0
   221
	fd2=accept(fd1,&addr2,&addrsize);	/* can't accept on an unbound socket */
sl@0
   222
	test_ok(fd2<0);
sl@0
   223
#endif
sl@0
   224
sl@0
   225
	IN_SET_LOOPBACK_ADDR(&addr1);
sl@0
   226
	addr1.sin_port=htons(port);
sl@0
   227
	err=bind(fd1, (struct sockaddr*)&addr1, sizeof(addr1));
sl@0
   228
	test_ok(err==0);
sl@0
   229
sl@0
   230
#if 0
sl@0
   231
	/* causes ESOCK to panic the client */
sl@0
   232
	addrsize=sizeof(addr2);
sl@0
   233
	fd2=accept(fd1,&addr2,&addrsize);	/* can't accept before listening */
sl@0
   234
	test_ok(fd2<0);
sl@0
   235
#endif
sl@0
   236
sl@0
   237
	err=listen(fd1,1);
sl@0
   238
	test_ok(err==0);
sl@0
   239
sl@0
   240
	addrsize=sizeof(addr2);
sl@0
   241
	fd2=accept(fd1,(struct sockaddr*)&addr2,&addrsize);
sl@0
   242
	test_ok(fd2>=0);
sl@0
   243
	test(addr2.sin_family==AF_INET);
sl@0
   244
	test(addr2.sin_port!=htons(port));
sl@0
   245
	test(addr2.sin_port!=0);
sl@0
   246
	test(addrsize<=sizeof(addr2));
sl@0
   247
sl@0
   248
	test_Next("Server read/write");
sl@0
   249
sl@0
   250
	/* read */
sl@0
   251
	nbytes=strlen(*mp);
sl@0
   252
	err=read(fd2, buf, nbytes+1);
sl@0
   253
	test_ok(err==nbytes+1);
sl@0
   254
	test(strcmp(buf,*mp)==0);
sl@0
   255
sl@0
   256
	/* write */
sl@0
   257
	mp++;
sl@0
   258
	nbytes=strlen(*mp);
sl@0
   259
	for (i=0; i<nbytes+1; i++)
sl@0
   260
		{
sl@0
   261
		err=write(fd2,(*mp)+i,1);
sl@0
   262
		test_ok(err==1);
sl@0
   263
		}
sl@0
   264
sl@0
   265
	test_Next("Server send/recv");
sl@0
   266
sl@0
   267
	/* recv */
sl@0
   268
	mp++;
sl@0
   269
	nbytes=strlen(*mp);
sl@0
   270
	err=recv(fd2, buf, sizeof(buf) ,0);
sl@0
   271
	test_ok(err==nbytes+1);
sl@0
   272
	test(strcmp(buf,*mp)==0);
sl@0
   273
sl@0
   274
	/* send */
sl@0
   275
	mp++;
sl@0
   276
	nbytes=strlen(*mp);
sl@0
   277
	err=send(fd2, *mp, nbytes+1,0);
sl@0
   278
	test_ok(err==nbytes+1);
sl@0
   279
sl@0
   280
	/* recvfrom */
sl@0
   281
	mp++;
sl@0
   282
	nbytes=strlen(*mp);
sl@0
   283
	addrsize=sizeof(addr2);
sl@0
   284
	addr2.sin_port=0;
sl@0
   285
	err=recvfrom(fd2, buf, nbytes+1,0,(struct sockaddr*)&addr2,&addrsize);
sl@0
   286
/*
sl@0
   287
	memset(&buf[0],0, sizeof(buf));
sl@0
   288
	err=recvfrom(fd2, buf, nbytes+1,0,0,0);
sl@0
   289
*/
sl@0
   290
	test_ok(err==nbytes+1);
sl@0
   291
	test(strcmp(buf,*mp)==0);
sl@0
   292
	test(addr2.sin_family==AF_INET);
sl@0
   293
	test(addr2.sin_port!=htons(port));
sl@0
   294
	test(addr2.sin_port!=0);
sl@0
   295
	test(addrsize<=sizeof(addr2));
sl@0
   296
sl@0
   297
	/* sendto */
sl@0
   298
	mp++;
sl@0
   299
	nbytes=strlen(*mp);
sl@0
   300
	addrsize=sizeof(addr1);
sl@0
   301
	err=sendto(fd2, *mp, nbytes+1,0,(struct sockaddr*)&addr1,addrsize);	/* not allowed on streams */
sl@0
   302
	test_errno(err<0,ENOSYS);
sl@0
   303
	err=send(fd2, *mp, nbytes+1,0);		/* to keep synchronisation */
sl@0
   304
	test_ok(err==nbytes+1);
sl@0
   305
sl@0
   306
	
sl@0
   307
	test_Next("Server revcfrom with null fromaddr");
sl@0
   308
		/* recvfrom again*/
sl@0
   309
	mp++;
sl@0
   310
	nbytes=strlen(*mp);
sl@0
   311
	memset(&buf[0],0xFF, sizeof(buf));
sl@0
   312
	err=recvfrom(fd2, buf, nbytes+1,0,0,0);
sl@0
   313
	test_ok(err==nbytes+1);
sl@0
   314
	test(strcmp(buf,*mp)==0);
sl@0
   315
sl@0
   316
	/* sendto again*/
sl@0
   317
	mp++;
sl@0
   318
	nbytes=strlen(*mp);
sl@0
   319
	addrsize=sizeof(addr1);
sl@0
   320
	err=send(fd2, *mp, nbytes+1,0);		/* to keep synchronisation */
sl@0
   321
	test_ok(err==nbytes+1);
sl@0
   322
sl@0
   323
	
sl@0
   324
	
sl@0
   325
	/*
sl@0
   326
	test_Next("Server shutdown transmission");
sl@0
   327
	err=shutdown(fd2,1);
sl@0
   328
	test_ok(err==0);
sl@0
   329
sl@0
   330
	sleep(4);	// so that the client's sleep(1) finishes before we awake 
sl@0
   331
sl@0
   332
	mp++;
sl@0
   333
	nbytes=strlen(*mp);
sl@0
   334
	addrsize=sizeof(addr1);
sl@0
   335
	err=send(fd2, *mp, nbytes+1,0);
sl@0
   336
	test_errno(err<0,EPIPE);
sl@0
   337
sl@0
   338
	nbytes=strlen(*mp);
sl@0
   339
	err=recv(fd2, buf, nbytes+1,0);
sl@0
   340
	test_ok(err==nbytes+1);
sl@0
   341
	test(strcmp(buf,*mp)==0);
sl@0
   342
sl@0
   343
	err=close(fd2);
sl@0
   344
	test_ok(err==0);
sl@0
   345
sl@0
   346
	// Large transmission 
sl@0
   347
*/
sl@0
   348
	test_Next("Server large transfer"); 
sl@0
   349
sl@0
   350
	addrsize=sizeof(addr2);
sl@0
   351
	fd2=accept(fd1,(struct sockaddr*)&addr2,&addrsize);
sl@0
   352
	test_ok(fd2>=0);
sl@0
   353
	test(addr2.sin_family==AF_INET);
sl@0
   354
	test(addr2.sin_port!=htons(port));
sl@0
   355
	test(addr2.sin_port!=0);
sl@0
   356
	test(addrsize<=sizeof(addr2));
sl@0
   357
sl@0
   358
	nbytes=sizeof(randbuf);
sl@0
   359
	err=write(fd2,(const char*)randbuf,nbytes);
sl@0
   360
	test_ok(err==nbytes);
sl@0
   361
sl@0
   362
	test_Next("Server close");
sl@0
   363
sl@0
   364
//	err=shutdown(fd2,1);	
sl@0
   365
//	test_ok(err==0);
sl@0
   366
sl@0
   367
//	sleep(4);
sl@0
   368
	
sl@0
   369
	err=close(fd2);
sl@0
   370
	test_ok(err==0);
sl@0
   371
sl@0
   372
	err=close(fd1);
sl@0
   373
	test_ok(err==0);
sl@0
   374
	}
sl@0
   375
sl@0
   376
/**
sl@0
   377
@SYMTestCaseID          SYSLIB-STDLIB-CT-1061
sl@0
   378
@SYMTestCaseDesc	    Tests for client socket
sl@0
   379
@SYMTestPriority 	    High
sl@0
   380
@SYMTestActions  	    Create a steam socket,bind and test for a transmission process.Check for error codes 
sl@0
   381
@SYMTestExpectedResults Test must not fail
sl@0
   382
@SYMREQ                 REQ0000
sl@0
   383
*/		
sl@0
   384
void testClient()
sl@0
   385
	{
sl@0
   386
	int fd1, nbytes, nbytes2, i, status;
sl@0
   387
sl@0
   388
	size_t addrsize;
sl@0
   389
	int err;
sl@0
   390
	struct sockaddr_in addr1, addr2;
sl@0
   391
	char buf[80];
sl@0
   392
	char **mp = message_sequence;
sl@0
   393
	static int randbuf[10001];
sl@0
   394
sl@0
   395
	test_Next("Create client socket");
sl@0
   396
	fd1=socket(AF_INET, SOCK_STREAM, 0);
sl@0
   397
	test_ok(fd1>=0);
sl@0
   398
sl@0
   399
	sleep(5);	/* give server a chance to run */
sl@0
   400
sl@0
   401
	addr1=testaddr;
sl@0
   402
	addr1.sin_port=htons(port);
sl@0
   403
	addrsize=sizeof(addr1);
sl@0
   404
	err=connect(fd1,(struct sockaddr*)&addr1,addrsize);
sl@0
   405
	test_ok(err==0);
sl@0
   406
sl@0
   407
	addrsize=sizeof(addr2);
sl@0
   408
	err=getpeername(fd1,(struct sockaddr*)&addr2,&addrsize);
sl@0
   409
	test_ok(err==0);
sl@0
   410
	test(addr2.sin_family==AF_INET);
sl@0
   411
	test(addr2.sin_port==htons(port));
sl@0
   412
	test(addrsize<=sizeof(addr2));
sl@0
   413
sl@0
   414
	addrsize=sizeof(addr2);
sl@0
   415
	err=getsockname(fd1,(struct sockaddr*)&addr2,&addrsize);
sl@0
   416
	test_ok(err==0);
sl@0
   417
	test(addr2.sin_family==AF_INET);
sl@0
   418
	test(addr2.sin_port!=htons(port));
sl@0
   419
	test(addr2.sin_port!=0);
sl@0
   420
	test(addrsize<=sizeof(addr2));
sl@0
   421
sl@0
   422
	test_Next("Client read/write");
sl@0
   423
sl@0
   424
	/* write */
sl@0
   425
	nbytes=strlen(*mp);
sl@0
   426
	err=write(fd1, *mp, nbytes+1);
sl@0
   427
	test_ok(err==nbytes+1);
sl@0
   428
sl@0
   429
	/* read */
sl@0
   430
	mp++;
sl@0
   431
	nbytes=strlen(*mp);
sl@0
   432
	err=read(fd1, buf, nbytes+1);
sl@0
   433
	test_ok(err==nbytes+1);
sl@0
   434
	test(strcmp(buf,*mp)==0);
sl@0
   435
sl@0
   436
	test_Next("Client send/recv");
sl@0
   437
sl@0
   438
	/* send */
sl@0
   439
	mp++;
sl@0
   440
	nbytes=strlen(*mp);
sl@0
   441
	err=send(fd1,*mp, nbytes+1,0);
sl@0
   442
	test_ok(err==nbytes+1);
sl@0
   443
sl@0
   444
	/* recv - get the first 2 bytes so that we know the buffer is full */
sl@0
   445
	mp++;
sl@0
   446
	nbytes=strlen(*mp);
sl@0
   447
	err=recv(fd1,buf,2,0);
sl@0
   448
	test_ok(err==2);
sl@0
   449
sl@0
   450
	/* ioctl */
sl@0
   451
sl@0
   452
	nbytes2=-1;
sl@0
   453
	err=ioctl(fd1,E32IONREAD,&nbytes2);
sl@0
   454
	test_ok(err==0);
sl@0
   455
	test(nbytes2==(nbytes+1-2));
sl@0
   456
sl@0
   457
	nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
sl@0
   458
	err=ioctl(fd1,E32IOSELECT,&nbytes2);
sl@0
   459
	test_ok(err==0);
sl@0
   460
	test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
sl@0
   461
sl@0
   462
	nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
sl@0
   463
	err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
sl@0
   464
	test_ok(err==0);
sl@0
   465
	err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
sl@0
   466
	test_ok(err==0);
sl@0
   467
	if (nbytes2!=(E32SELECT_READ|E32SELECT_WRITE))
sl@0
   468
		{
sl@0
   469
		nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
sl@0
   470
		err=ioctl(fd1,E32IOSELECT,&nbytes2);
sl@0
   471
		test_ok(err==0);
sl@0
   472
		test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
sl@0
   473
		}		
sl@0
   474
sl@0
   475
	/* recv - get the rest of the data */
sl@0
   476
	for (i=2; i<nbytes+1; i++)
sl@0
   477
		{
sl@0
   478
		err=recv(fd1,buf+i,1,0);
sl@0
   479
		test_ok(err==1);
sl@0
   480
		}
sl@0
   481
	test(strcmp(buf,*mp)==0);
sl@0
   482
sl@0
   483
	/* ioctl again - this time there is no data pending */
sl@0
   484
	nbytes2=-1;
sl@0
   485
	err=ioctl(fd1,E32IONREAD,&nbytes2);
sl@0
   486
	test_ok(err==0);
sl@0
   487
	test(nbytes2==0);
sl@0
   488
sl@0
   489
	nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
sl@0
   490
	err=ioctl(fd1,E32IOSELECT,&nbytes2);
sl@0
   491
	test_ok(err==0);
sl@0
   492
	test(nbytes2==E32SELECT_WRITE);
sl@0
   493
sl@0
   494
	nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
sl@0
   495
	err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
sl@0
   496
	test_ok(err==0);
sl@0
   497
	err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
sl@0
   498
	test_ok(err==0);
sl@0
   499
	test(nbytes2==E32SELECT_WRITE);
sl@0
   500
sl@0
   501
	/* sendto */
sl@0
   502
	mp++;
sl@0
   503
	nbytes=strlen(*mp);
sl@0
   504
	addrsize=sizeof(addr1);
sl@0
   505
	err=sendto(fd1, *mp, nbytes+1,0,(struct sockaddr*)&addr1,addrsize);
sl@0
   506
	test_errno(err<0,ENOSYS);
sl@0
   507
	err=send(fd1, *mp, nbytes+1,0);		/* to keep synchronisation */
sl@0
   508
	test_ok(err==nbytes+1);
sl@0
   509
sl@0
   510
	/* recvfrom */
sl@0
   511
	mp++;
sl@0
   512
	nbytes=strlen(*mp);
sl@0
   513
	addrsize=sizeof(addr2);
sl@0
   514
	addr2.sin_port=0;
sl@0
   515
	err=recvfrom(fd1, buf, nbytes+1,0,(struct sockaddr*)&addr2,&addrsize);
sl@0
   516
	test_ok(err==nbytes+1);
sl@0
   517
	test(strcmp(buf,*mp)==0);
sl@0
   518
	test(addr2.sin_family==AF_INET);
sl@0
   519
	test(addr2.sin_port==htons(port));
sl@0
   520
	test(addrsize<=sizeof(addr2));
sl@0
   521
sl@0
   522
	/* another sendto */
sl@0
   523
	test_Next("Client revcfrom with null fromaddr");
sl@0
   524
	mp++;
sl@0
   525
	nbytes=strlen(*mp);
sl@0
   526
	addrsize=sizeof(addr1);
sl@0
   527
	err=sendto(fd1, *mp, nbytes+1,0,(struct sockaddr*)&addr1,addrsize);
sl@0
   528
	test_errno(err<0,ENOSYS);
sl@0
   529
	err=send(fd1, *mp, nbytes+1,0);		/* to keep synchronisation */
sl@0
   530
	test_ok(err==nbytes+1);
sl@0
   531
sl@0
   532
	/* recvfrom */
sl@0
   533
	mp++;
sl@0
   534
	nbytes=strlen(*mp);
sl@0
   535
	addrsize=sizeof(addr2);
sl@0
   536
	memset(buf,0xFF,sizeof(buf));
sl@0
   537
	err=recvfrom(fd1, buf, nbytes+1,0,0,0);	//do a recvfrom with a null address
sl@0
   538
	test_ok(err==nbytes+1);
sl@0
   539
	test(strcmp(buf,*mp)==0);
sl@0
   540
sl@0
   541
sl@0
   542
sl@0
   543
	sleep(2);
sl@0
   544
/*
sl@0
   545
	test_Next("Test half-closed connection");
sl@0
   546
sl@0
   547
	// read from half-closed socket 
sl@0
   548
	mp++;
sl@0
   549
	nbytes=strlen(*mp);
sl@0
   550
	err=read(fd1, buf, nbytes+1);
sl@0
   551
	test_errno(err<0, EPIPE);
sl@0
   552
sl@0
   553
	nbytes=strlen(*mp);
sl@0
   554
	err=write(fd1,*mp,nbytes+1);
sl@0
   555
	test_ok(err==nbytes+1);
sl@0
   556
sl@0
   557
	sleep(2);	
sl@0
   558
	
sl@0
   559
	// read from a connection closed by the other end 
sl@0
   560
	mp++;
sl@0
   561
	nbytes=strlen(*mp);
sl@0
   562
	err=read(fd1,buf,nbytes+1);
sl@0
   563
	test_errno(err<0, EPIPE);
sl@0
   564
*/
sl@0
   565
	err=close(fd1);
sl@0
   566
	test_ok(err==0);
sl@0
   567
sl@0
   568
	// Large transmission - and another connection to the server socket 
sl@0
   569
sl@0
   570
	test_Next("Test large transmission");
sl@0
   571
sl@0
   572
	fd1=socket(AF_INET, SOCK_STREAM, 0);
sl@0
   573
	test_ok(fd1>=0);
sl@0
   574
sl@0
   575
	sleep(2);	/* give server a chance to run */
sl@0
   576
sl@0
   577
	addr1=testaddr;
sl@0
   578
	addr1.sin_port=htons(port);
sl@0
   579
	addrsize=sizeof(addr1);
sl@0
   580
sl@0
   581
	//this connect fails.  Why is this?
sl@0
   582
	err=connect(fd1,(struct sockaddr*)&addr1,addrsize);
sl@0
   583
	test_ok(err==0);
sl@0
   584
sl@0
   585
sl@0
   586
	addrsize=sizeof(addr2);
sl@0
   587
	err=getpeername(fd1,(struct sockaddr*)&addr2,&addrsize);
sl@0
   588
	test_ok(err==0);
sl@0
   589
	test(addr2.sin_family==AF_INET);
sl@0
   590
	test(addr2.sin_port==htons(port));
sl@0
   591
	test(addrsize<=sizeof(addr2));
sl@0
   592
sl@0
   593
	addrsize=sizeof(addr2);
sl@0
   594
	err=getsockname(fd1,(struct sockaddr*)&addr2,&addrsize);
sl@0
   595
	test_ok(err==0);
sl@0
   596
	test(addr2.sin_family==AF_INET);
sl@0
   597
	test(addr2.sin_port!=htons(port));
sl@0
   598
	test(addr2.sin_port!=0);
sl@0
   599
	test(addrsize<=sizeof(addr2));
sl@0
   600
sl@0
   601
	nbytes=0;
sl@0
   602
	{
sl@0
   603
		int buf;
sl@0
   604
		char * p;
sl@0
   605
sl@0
   606
		do
sl@0
   607
		{
sl@0
   608
		buf=sizeof(randbuf)-nbytes;
sl@0
   609
		if (buf>4095)
sl@0
   610
				buf=4095;
sl@0
   611
		p = (char*)randbuf+nbytes;
sl@0
   612
//		err=read(fd1, p, buf); 
sl@0
   613
		err=recv(fd1, p, buf, 0); 
sl@0
   614
		if (err>0)
sl@0
   615
			nbytes+=err;
sl@0
   616
		}
sl@0
   617
	while (nbytes<sizeof(randbuf) && err>0);
sl@0
   618
	}
sl@0
   619
sl@0
   620
	printf("\nnbytes=%d, err=%d, errno=%d\n", nbytes, err, errno);
sl@0
   621
sl@0
   622
	test_ok(err==0);	/* want a graceful EOF */
sl@0
   623
	test(nbytes==sizeof(randbuf)-sizeof(int));	/* expect a smaller test data set */
sl@0
   624
	test(randbuf[0]==4441302);
sl@0
   625
	srand(randbuf[0]);
sl@0
   626
	for (i=1; i<10000; i++)
sl@0
   627
		test(randbuf[i]==rand());
sl@0
   628
sl@0
   629
sl@0
   630
sl@0
   631
	err=recv(fd1, (char*)randbuf, 1, 0); 
sl@0
   632
	test_ok(err==0);
sl@0
   633
sl@0
   634
	err=close(fd1);
sl@0
   635
	test_ok(err==0);
sl@0
   636
sl@0
   637
	}
sl@0
   638
sl@0
   639
int close_console=0;
sl@0
   640
void testConnection()
sl@0
   641
	{
sl@0
   642
	int err;
sl@0
   643
	void* server;
sl@0
   644
	char buf[100];
sl@0
   645
sl@0
   646
	sprintf(buf, "TISTREAM server %d", close_console);
sl@0
   647
	server=create_thread(testServer, buf);
sl@0
   648
	test(server!=0);
sl@0
   649
	start_thread(server);
sl@0
   650
sl@0
   651
	sleep(2);	/* give the server a chance to get started */
sl@0
   652
	testClient();
sl@0
   653
sl@0
   654
	err=wait_for_thread(server);
sl@0
   655
	test(err==0);
sl@0
   656
sl@0
   657
	if (close_console)
sl@0
   658
		{
sl@0
   659
		test_Close();
sl@0
   660
		close(0);
sl@0
   661
		close(1);
sl@0
   662
		close(2);
sl@0
   663
		}
sl@0
   664
	}
sl@0
   665
sl@0
   666
int main(int argc, char *argv[])
sl@0
   667
	{
sl@0
   668
	void* client;
sl@0
   669
	int err;
sl@0
   670
sl@0
   671
	test_Title("AF_INET Streams");
sl@0
   672
sl@0
   673
	err=CommInit(0);	/* ensure a workable comms environment */
sl@0
   674
	test(err==0);
sl@0
   675
sl@0
   676
	IN_SET_LOOPBACK_ADDR(&testaddr);
sl@0
   677
sl@0
   678
	if (argc==1)
sl@0
   679
		{
sl@0
   680
		// Run the test(s) without a CPosixServer first 
sl@0
   681
		testSimple();
sl@0
   682
		port +=2;
sl@0
   683
		testConnection();
sl@0
   684
		port +=2;
sl@0
   685
		}
sl@0
   686
sl@0
   687
sl@0
   688
	test_Next("Test gethostname");
sl@0
   689
	{
sl@0
   690
		char * hname;
sl@0
   691
		hname = (char*)malloc(200);
sl@0
   692
		if (hname)
sl@0
   693
			{
sl@0
   694
			err = gethostname(hname, 200);
sl@0
   695
sl@0
   696
			if (err)
sl@0
   697
				printf("gethostname, err=%d, errno=%d\n", err, errno);
sl@0
   698
			else
sl@0
   699
				printf("host name is %s\n", hname);
sl@0
   700
			
sl@0
   701
			free(hname);			}
sl@0
   702
		else
sl@0
   703
			printf("host name - malloc failure\n");
sl@0
   704
sl@0
   705
	}
sl@0
   706
sl@0
   707
	test_Next("Do it again using the CPosixServer (for them, not me)");
sl@0
   708
	close_console=1;
sl@0
   709
sl@0
   710
	start_posix_server();	/* calls SpawnPosixServer from C++ code */
sl@0
   711
sl@0
   712
	
sl@0
   713
	client=create_thread(testSimple, "TISTREAM simple");
sl@0
   714
	test(client!=0);
sl@0
   715
	start_thread(client);
sl@0
   716
	err=wait_for_thread(client);
sl@0
   717
	test(err==0);
sl@0
   718
	port +=2;
sl@0
   719
sl@0
   720
	client=create_thread(testConnection, "TISTREAM client");
sl@0
   721
	test(client!=0);
sl@0
   722
	start_thread(client);
sl@0
   723
	err=wait_for_thread(client);
sl@0
   724
	test(err==0);
sl@0
   725
sl@0
   726
	test_Close();
sl@0
   727
	return 0;
sl@0
   728
	}