os/ossrv/genericopenlibs/cstdlib/TSTLIB/TLSTREAM.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 * Test of AF_LOCAL stream sockets
    16 * 
    17 *
    18 */
    19 
    20 
    21 
    22 #include <stdio.h>
    23 #include <errno.h>
    24 #include <stdlib.h>
    25 #include <string.h>
    26 #include <unistd.h>
    27 #include <sys/types.h>
    28 #include <sys/socket.h>
    29 #include <sys/ioctl.h>
    30 
    31 #include "ctest.h"	/* includes C interface to EPOC32 threads, and SpawnPosixServer */
    32 
    33 test_Data;
    34 
    35 /**
    36 @SYMTestCaseID          SYSLIB-STDLIB-CT-1115
    37 @SYMTestCaseDesc	    Tests for AF_LOCAL stream sockets
    38 @SYMTestPriority 	    High
    39 @SYMTestActions  	    Tests for create socket,binding and other simple socket operations
    40 @SYMTestExpectedResults Test must not fail
    41 @SYMREQ                 REQ0000
    42 */					
    43 void testSimple()
    44 	{
    45 	int fd1, fd2;
    46 	size_t addrsize;
    47 	int err;
    48 	struct sockaddr addr1, addr2;
    49 	int optionbuf[20];
    50 	size_t optionsize;
    51 
    52 	test_Next("Create stream sockets");
    53 	fd1=socket(AF_LOCAL, SOCK_STREAM, 0);
    54 	test_ok(fd1>=0);
    55 
    56 	fd2=socket(AF_LOCAL, SOCK_STREAM, 0);
    57 	test_ok(fd2>=0);
    58 
    59 	test_Next("Some binding tests");
    60 
    61 #if 0 /* causes socket server panic with ESOCK 058 */
    62 	addr1.sa_family=AF_UNSPEC;
    63 	addr1.sa_port=65530;
    64 	err=bind(fd1,&addr1, sizeof(addr1));	/* wrong family, port out of range */
    65 	test_ok(err!=0);
    66 #endif
    67 
    68 	addr1.sa_family=AF_LOCAL;
    69 	addr1.sa_port=1;
    70 	err=bind(fd1, &addr1, sizeof(addr1));
    71 	test_ok(err==0);
    72 
    73 	addr1.sa_family=AF_LOCAL;
    74 	addr1.sa_port=2;
    75 	err=bind(fd1, &addr1, sizeof(addr1));	/* already bound */
    76 	test_errno(err!=0, EEXIST);
    77 
    78 	test_Next("Get associated addresses");
    79 
    80 	addrsize=sizeof(addr2);
    81 	err=getsockname(fd1,&addr2,&addrsize);
    82 	test_ok(err==0);
    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));
    86 
    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 */
    90 
    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 */
    94 
    95 	test_Next("More binding");
    96 
    97 	addr1.sa_family=AF_LOCAL;
    98 	addr1.sa_port=1;
    99 	err=bind(fd2, &addr1, sizeof(addr1));	/* address in use */
   100 	test_errno(err!=0, EACCES);
   101 
   102 #if 0	/* this isn't supported by AF_LOCAL */
   103 	addr1.sa_family=AF_LOCAL;
   104 	addr1.sa_port=0;
   105 	err=bind(fd2, &addr1, sizeof(addr1));	/* unspecified port number */
   106 	test_ok(err==0);
   107 
   108 	addrsize=sizeof(addr2);
   109 	err=getsockname(fd2,&addr2,&addrsize);
   110 	test_ok(err==0);
   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));
   115 #endif
   116 
   117 	err=listen(fd1,1);
   118 	test_ok(err==0);
   119 
   120 	test_Next("Socket options");
   121 
   122 	optionbuf[0]=3500000;	/* implausible size */
   123 	optionsize=sizeof(optionbuf[0]);
   124 	err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
   125 	test_ok(err==0);
   126 	test(optionbuf[0]!=3500000);
   127 
   128 	optionbuf[0]=7*1024;
   129 	optionsize=sizeof(optionbuf[0]);
   130 	err=setsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,optionsize);
   131 	test_ok(err==0);
   132 
   133 	optionbuf[0]=3500000;	/* implausible size */
   134 	optionsize=sizeof(optionbuf[0]);
   135 	err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize);
   136 	test_ok(err==0);
   137 	test(optionbuf[0]==7*1024);
   138 
   139 	optionbuf[0]=1;
   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);
   144 
   145 	optionbuf[0]=13;
   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);
   150 
   151 	err=close(fd1);
   152 	test_ok(err==0);
   153 
   154 	err=close(fd2);
   155 	test_ok(err==0);
   156 	}
   157 
   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
   160  */
   161 char *message_sequence[] = {
   162 	"Hello from client",
   163 	"Hello from server",
   164 	"Test of send",
   165 	"Test of recv",
   166 	"Try sendto",
   167 	"Try recvfrom",
   168 	"Send to shutdown socket",
   169 	"Send to closed socket",
   170 	0
   171 	};
   172 
   173 /**
   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
   179 @SYMREQ                 REQ0000
   180 */					
   181 void testServer()
   182 	{
   183 	int fd1, fd2, nbytes, i;
   184 	size_t addrsize;
   185 	int err;
   186 	struct sockaddr addr1, addr2;
   187 	char buf[80];
   188 	char **mp = message_sequence;
   189 
   190 	test_Next("Create server socket");
   191 	fd1=socket(AF_LOCAL, SOCK_STREAM, 0);
   192 	test_ok(fd1>=0);
   193 
   194 #if 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 */
   198 	test_ok(fd2<0);
   199 #endif
   200 
   201 	addr1.sa_family=AF_LOCAL;
   202 	addr1.sa_port=1;
   203 	err=bind(fd1, &addr1, sizeof(addr1));
   204 	test_ok(err==0);
   205 
   206 #if 0
   207 	/* causes ESOCK to panic the client */
   208 	addrsize=sizeof(addr2);
   209 	fd2=accept(fd1,&addr2,&addrsize);	/* can't accept before listening */
   210 	test_ok(fd2<0);
   211 #endif
   212 
   213 	err=listen(fd1,1);
   214 	test_ok(err==0);
   215 
   216 	addrsize=sizeof(addr2);
   217 	fd2=accept(fd1,&addr2,&addrsize);
   218 	test_ok(fd2>=0);
   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));
   223 
   224 	test_Next("Server read/write");
   225 
   226 	/* read */
   227 	nbytes=strlen(*mp);
   228 	err=read(fd2, buf, nbytes+1);
   229 	test_ok(err==nbytes+1);
   230 	test(strcmp(buf,*mp)==0);
   231 
   232 	/* write */
   233 	mp++;
   234 	nbytes=strlen(*mp);
   235 	for (i=0; i<nbytes+1; i++)
   236 		{
   237 		err=write(fd2,(*mp)+i,1);
   238 		test_ok(err==1);
   239 		}
   240 
   241 	test_Next("Server send/recv");
   242 
   243 	/* recv */
   244 	mp++;
   245 	nbytes=strlen(*mp);
   246 	err=recv(fd2, buf, nbytes+1,0);
   247 	test_ok(err==nbytes+1);
   248 	test(strcmp(buf,*mp)==0);
   249 
   250 	/* send */
   251 	mp++;
   252 	nbytes=strlen(*mp);
   253 	err=send(fd2, *mp, nbytes+1,0);
   254 	test_ok(err==nbytes+1);
   255 
   256 	/* recvfrom */
   257 	mp++;
   258 	nbytes=strlen(*mp);
   259 	addrsize=sizeof(addr2);
   260 	addr2.sa_port=0;
   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));
   268 
   269 	/* sendto */
   270 	mp++;
   271 	nbytes=strlen(*mp);
   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);
   277 
   278 	test_Next("Server shutdown reception");
   279 	err=shutdown(fd2,0);
   280 	test_ok(err==0);
   281 
   282 	sleep(2);	/* so that the client's sleep(1) finishes before we awake */
   283 
   284 	err=close(fd2);
   285 	test_ok(err==0);
   286 
   287 	err=close(fd1);
   288 	test_ok(err==0);
   289 	}
   290 
   291 /**
   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
   298 @SYMREQ                 REQ0000
   299 */					
   300 void testClient()
   301 	{
   302 	int fd1, nbytes, nbytes2, i, status;
   303 	size_t addrsize;
   304 	int err;
   305 	struct sockaddr addr1, addr2;
   306 	char buf[80];
   307 	char **mp = message_sequence;
   308 
   309 	test_Next("Create client socket");
   310 	fd1=socket(AF_LOCAL, SOCK_STREAM, 0);
   311 	test_ok(fd1>=0);
   312 
   313 	addr1.sa_family=AF_LOCAL;
   314 	addr1.sa_port=1;
   315 	addrsize=sizeof(addr1);
   316 	err=connect(fd1,&addr1,addrsize);
   317 	test_ok(err==0);
   318 
   319 	addrsize=sizeof(addr2);
   320 	err=getpeername(fd1,&addr2,&addrsize);
   321 	test_ok(err==0);
   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));
   325 
   326 	addrsize=sizeof(addr2);
   327 	err=getsockname(fd1,&addr2,&addrsize);
   328 	test_ok(err==0);
   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));
   333 
   334 	test_Next("Client read/write");
   335 
   336 	/* write */
   337 	nbytes=strlen(*mp);
   338 	err=write(fd1, *mp, nbytes+1);
   339 	test_ok(err==nbytes+1);
   340 
   341 	/* read */
   342 	mp++;
   343 	nbytes=strlen(*mp);
   344 	err=read(fd1, buf, nbytes+1);
   345 	test_ok(err==nbytes+1);
   346 	test(strcmp(buf,*mp)==0);
   347 
   348 	test_Next("Client send/recv");
   349 
   350 	/* send */
   351 	mp++;
   352 	nbytes=strlen(*mp);
   353 	for (i=0; i<nbytes+1; i++)
   354 		{
   355 		err=send(fd1,(*mp)+i,1,0);
   356 		test_ok(err==1);
   357 		}
   358 
   359 	/* recv - get the first byte so that we know the buffer is full */
   360 	mp++;
   361 	nbytes=strlen(*mp);
   362 	err=recv(fd1,buf,1,0);
   363 	test_ok(err==1);
   364 
   365 	/* ioctl */
   366 	nbytes2=-1;
   367 	err=ioctl(fd1,E32IONREAD,&nbytes2);
   368 #if 0
   369 	test_ok(err==0);
   370 	test(nbytes2==nbytes);
   371 #else
   372 	test_errno(err<0,ENOSYS);	/* IPC.PRT doesn't implement KSoReadBytesPending */
   373 #endif
   374 
   375 	nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
   376 	err=ioctl(fd1,E32IOSELECT,&nbytes2);
   377 	test_ok(err==0);
   378 	test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
   379 
   380 	nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
   381 	err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
   382 	test_ok(err==0);
   383 	err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
   384 	test_ok(err==0);
   385 	if (nbytes2!=(E32SELECT_READ|E32SELECT_WRITE))
   386 		{
   387 		nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
   388 		err=ioctl(fd1,E32IOSELECT,&nbytes2);
   389 		test_ok(err==0);
   390 		test(nbytes2==(E32SELECT_READ|E32SELECT_WRITE));
   391 		}		
   392 
   393 	/* recv - get the rest of the data */
   394 	for (i=1; i<nbytes+1; i++)
   395 		{
   396 		err=recv(fd1,buf+i,1,0);
   397 		test_ok(err==1);
   398 		}
   399 	test(strcmp(buf,*mp)==0);
   400 
   401 	/* ioctl again - this time there is no data pending */
   402 	nbytes2=-1;
   403 	err=ioctl(fd1,E32IONREAD,&nbytes2);
   404 #if 0
   405 	test_ok(err==0);
   406 	test(nbytes2==0);
   407 #else
   408 	test_errno(err<0,ENOSYS);	/* IPC.PRT doesn't implement KSoReadBytesPending */
   409 #endif
   410 
   411 	nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
   412 	err=ioctl(fd1,E32IOSELECT,&nbytes2);
   413 	test_ok(err==0);
   414 	test(nbytes2==E32SELECT_WRITE);
   415 
   416 	nbytes2=E32SELECT_READ|E32SELECT_WRITE|E32SELECT_EXCEPT;
   417 	err=async_ioctl(fd1,E32IOSELECT,&nbytes2,&status);
   418 	test_ok(err==0);
   419 	err=async_ioctl_completion(fd1,E32IOSELECT,&nbytes2,&status);
   420 	test_ok(err==0);
   421 	test(nbytes2==E32SELECT_WRITE);
   422 
   423 	/* sendto */
   424 	mp++;
   425 	nbytes=strlen(*mp);
   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);
   431 
   432 	/* recvfrom */
   433 	mp++;
   434 	nbytes=strlen(*mp);
   435 	addrsize=sizeof(addr2);
   436 	addr2.sa_port=0;
   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));
   443 
   444 	sleep(1);
   445 
   446 	test_Next("Client write to closed connection");
   447 
   448 	/* write to half-closed socket */
   449 	mp++;
   450 	nbytes=strlen(*mp);
   451 	err=write(fd1, *mp, nbytes+1);
   452 #if 0
   453 	/* IPC doesn't seem to care! */
   454 	test_errno(err<0, EPIPE);
   455 #else
   456 	test_ok(err==nbytes+1);
   457 #endif
   458 
   459 	sleep(2);	
   460 	
   461 	/* write to a connection closed by the other end */
   462 	mp++;
   463 	nbytes=strlen(*mp);
   464 	err=write(fd1, *mp, nbytes+1);
   465 	test_errno(err<0, EPIPE);
   466 
   467 	err=close(fd1);
   468 	test_ok(err==0);
   469 	}
   470 
   471 int close_console=0;
   472 
   473 /**
   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
   479 @SYMREQ                 REQ0000
   480 */					
   481 void testConnection()
   482 	{
   483 	int err;
   484 	void* server;
   485 	
   486 	server=create_thread(testServer, "TLSTREAM server");
   487 	test(server!=0);
   488 	start_thread(server);
   489 
   490 	sleep(1);	/* give the server a chance to get started */
   491 	testClient();
   492 
   493 	err=wait_for_thread(server);
   494 	test(err==0);
   495 
   496 	if (close_console)
   497 		{
   498 		test_Close();
   499 		close(0);
   500 		close(1);
   501 		close(2);
   502 		}
   503 	}
   504 
   505 int main(int argc, char *argv[])
   506 	{
   507 	void* client;
   508 	int err;
   509 
   510 	test_Title("AF_LOCAL Streams");
   511 
   512 	testSimple();
   513 	testConnection();
   514 
   515 	test_Next("Do it again using the CPosixServer (for them, not me)");
   516 	close_console=1;
   517 
   518 	start_posix_server();	/* calls SpawnPosixServer from C++ code */
   519 
   520 	client=create_thread(testSimple, "TLSTREAM simple");
   521 	test(client!=0);
   522 	start_thread(client);
   523 	err=wait_for_thread(client);
   524 	test(err==0);
   525 
   526 	client=create_thread(testConnection, "TLSTREAM client");
   527 	test(client!=0);
   528 	start_thread(client);
   529 	err=wait_for_thread(client);
   530 	test(err==0);
   531 
   532 	test_Close();
   533 	return 0;
   534 	}