nsprpub/pr/tests/selct_nm.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/selct_nm.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,284 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/***********************************************************************
    1.10 +**  1997 - Netscape Communications Corporation
    1.11 +**
    1.12 +** Name: prselect_norm.c
    1.13 +**
    1.14 +** Description: tests PR_Select with sockets - Normal operations.
    1.15 +**
    1.16 +** Modification History:
    1.17 +** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
    1.18 +**	         The debug mode will print all of the printfs associated with this test.
    1.19 +**			 The regress mode will be the default mode. Since the regress tool limits
    1.20 +**           the output to a one line status:PASS or FAIL,all of the printf statements
    1.21 +**			 have been handled with an if (debug_mode) statement.
    1.22 +** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
    1.23 +**			recognize the return code from tha main program.
    1.24 +***********************************************************************/
    1.25 +
    1.26 +/***********************************************************************
    1.27 +** Includes
    1.28 +***********************************************************************/
    1.29 +/* Used to get the command line option */
    1.30 +#include "plgetopt.h"
    1.31 +
    1.32 +#include "prinit.h"
    1.33 +#include "prio.h"
    1.34 +#include "prlog.h"
    1.35 +#include "prprf.h"
    1.36 +#include "prerror.h"
    1.37 +#include "prnetdb.h"
    1.38 +
    1.39 +#include "obsolete/probslet.h"
    1.40 +
    1.41 +#include <stdio.h>
    1.42 +#include <string.h>
    1.43 +#include <stdlib.h>
    1.44 +
    1.45 +PRIntn failed_already=0;
    1.46 +PRIntn debug_mode;
    1.47 +
    1.48 +static void
    1.49 +clientThreadFunc(void *arg)
    1.50 +{
    1.51 +    PRUintn port = (PRUintn) arg;
    1.52 +    PRFileDesc *sock;
    1.53 +    PRNetAddr addr;
    1.54 +    char buf[128];
    1.55 +    int i;
    1.56 +
    1.57 +    addr.inet.family = PR_AF_INET;
    1.58 +    addr.inet.port = PR_htons((PRUint16)port);
    1.59 +    addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
    1.60 +    PR_snprintf(buf, sizeof(buf), "%hu", addr.inet.port);
    1.61 +
    1.62 +    for (i = 0; i < 5; i++) {
    1.63 +	sock = PR_NewTCPSocket();
    1.64 +        PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
    1.65 +	PR_Write(sock, buf, sizeof(buf));
    1.66 +	PR_Close(sock);
    1.67 +    }
    1.68 +}
    1.69 +
    1.70 +int main(int argc, char **argv)
    1.71 +{
    1.72 +    PRFileDesc *listenSock1, *listenSock2;
    1.73 +    PRFileDesc *fds0[10], *fds1[10], **fds, **other_fds;
    1.74 +    PRIntn nfds;
    1.75 +    PRUint16 listenPort1, listenPort2;
    1.76 +    PRNetAddr addr;
    1.77 +    PR_fd_set readFdSet;
    1.78 +    char buf[128];
    1.79 +    PRThread *clientThread;
    1.80 +    PRInt32 retVal;
    1.81 +    PRIntn i, j;
    1.82 +
    1.83 +	/* The command line argument: -d is used to determine if the test is being run
    1.84 +	in debug mode. The regress tool requires only one line output:PASS or FAIL.
    1.85 +	All of the printfs associated with this test has been handled with a if (debug_mode)
    1.86 +	test.
    1.87 +	Usage: test_name -d
    1.88 +	*/
    1.89 +	PLOptStatus os;
    1.90 +	PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
    1.91 +	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    1.92 +    {
    1.93 +		if (PL_OPT_BAD == os) continue;
    1.94 +        switch (opt->option)
    1.95 +        {
    1.96 +        case 'd':  /* debug mode */
    1.97 +			debug_mode = 1;
    1.98 +            break;
    1.99 +         default:
   1.100 +            break;
   1.101 +        }
   1.102 +    }
   1.103 +	PL_DestroyOptState(opt);
   1.104 +
   1.105 + /* main test */
   1.106 +	
   1.107 +    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   1.108 +    PR_STDIO_INIT();
   1.109 +
   1.110 +    if (debug_mode) {
   1.111 +		printf("This program tests PR_Select with sockets.  \n");
   1.112 +		printf(" Normal operation are tested.\n\n");
   1.113 +	}
   1.114 +
   1.115 +    /* Create two listening sockets */
   1.116 +    if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
   1.117 +	fprintf(stderr, "Can't create a new TCP socket\n");
   1.118 +	failed_already=1;
   1.119 +	goto exit_now;
   1.120 +    }
   1.121 +    addr.inet.family = PR_AF_INET;
   1.122 +    addr.inet.ip = PR_htonl(PR_INADDR_ANY);
   1.123 +    addr.inet.port = PR_htons(0);
   1.124 +    if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
   1.125 +	fprintf(stderr, "Can't bind socket\n");
   1.126 +	failed_already=1;
   1.127 +	goto exit_now;
   1.128 +    }
   1.129 +    if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
   1.130 +	fprintf(stderr, "PR_GetSockName failed\n");
   1.131 +	failed_already=1;
   1.132 +	goto exit_now;
   1.133 +    }
   1.134 +    listenPort1 = PR_ntohs(addr.inet.port);
   1.135 +    if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
   1.136 +	fprintf(stderr, "Can't listen on a socket\n");
   1.137 +	failed_already=1;
   1.138 +	goto exit_now;
   1.139 +    }
   1.140 +
   1.141 +    if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
   1.142 +	fprintf(stderr, "Can't create a new TCP socket\n");
   1.143 +	failed_already=1;
   1.144 +	goto exit_now;
   1.145 +    }
   1.146 +    addr.inet.family = PR_AF_INET;
   1.147 +    addr.inet.ip = PR_htonl(PR_INADDR_ANY);
   1.148 +    addr.inet.port = PR_htons(0);
   1.149 +    if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
   1.150 +	fprintf(stderr, "Can't bind socket\n");
   1.151 +	failed_already=1;
   1.152 +	goto exit_now;
   1.153 +    }
   1.154 +    if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
   1.155 +	fprintf(stderr, "PR_GetSockName failed\n");
   1.156 +	failed_already=1;
   1.157 +	goto exit_now;
   1.158 +    }
   1.159 +    listenPort2 = PR_ntohs(addr.inet.port);
   1.160 +    if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
   1.161 +	fprintf(stderr, "Can't listen on a socket\n");
   1.162 +failed_already=1;
   1.163 +	goto exit_now;
   1.164 +    }
   1.165 +    PR_snprintf(buf, sizeof(buf),
   1.166 +	    "The server thread is listening on ports %hu and %hu\n\n",
   1.167 +	    listenPort1, listenPort2);
   1.168 +    if (debug_mode) printf("%s", buf);
   1.169 +
   1.170 +    clientThread = PR_CreateThread(PR_USER_THREAD,
   1.171 +	    clientThreadFunc, (void *) listenPort1,
   1.172 +	    PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
   1.173 +	    PR_UNJOINABLE_THREAD, 0);
   1.174 +    if (clientThread == NULL) {
   1.175 +	fprintf(stderr, "can't create thread\n");
   1.176 +	failed_already=1;
   1.177 +	goto exit_now;
   1.178 +    }
   1.179 +
   1.180 +    clientThread = PR_CreateThread(PR_USER_THREAD,
   1.181 +	    clientThreadFunc, (void *) listenPort2,
   1.182 +	    PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
   1.183 +	    PR_UNJOINABLE_THREAD, 0);
   1.184 +    if (clientThread == NULL) {
   1.185 +	fprintf(stderr, "can't create thread\n");
   1.186 +	failed_already=1;
   1.187 +	goto exit_now;
   1.188 +    }
   1.189 +
   1.190 +    if (debug_mode) {
   1.191 +		printf("Two client threads are created.  Each of them will\n");
   1.192 +		printf("send data to one of the two ports the server is listening on.\n");
   1.193 +		printf("The data they send is the port number.  Each of them send\n");
   1.194 +		printf("the data five times, so you should see ten lines below,\n");
   1.195 +		printf("interleaved in an arbitrary order.\n");
   1.196 +	}
   1.197 +    /* set up the fd array */
   1.198 +    fds = fds0;
   1.199 +    other_fds = fds1;
   1.200 +    fds[0] = listenSock1;
   1.201 +    fds[1] = listenSock2;
   1.202 +    nfds = 2;
   1.203 +    /* Set up the fd set */
   1.204 +    PR_FD_ZERO(&readFdSet);
   1.205 +    PR_FD_SET(listenSock1, &readFdSet);
   1.206 +    PR_FD_SET(listenSock2, &readFdSet);
   1.207 +
   1.208 +    /* 20 events total */
   1.209 +    i = 0;
   1.210 +    while (i < 20) {
   1.211 +	PRFileDesc **tmp;
   1.212 +	int nextIndex;
   1.213 +	int nEvents = 0;
   1.214 +
   1.215 +	retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
   1.216 +		PR_INTERVAL_NO_TIMEOUT);
   1.217 +	PR_ASSERT(retVal != 0);  /* no timeout */
   1.218 +	if (retVal == -1) {
   1.219 +	    fprintf(stderr, "PR_Select failed (%d, %d)\n", PR_GetError(),
   1.220 +		    PR_GetOSError());
   1.221 +	failed_already=1;
   1.222 +	    goto exit_now;
   1.223 +	}
   1.224 +
   1.225 +	nextIndex = 2;
   1.226 +	/* the two listening sockets */
   1.227 +	for (j = 0; j < 2; j++) {
   1.228 +	    other_fds[j] = fds[j];
   1.229 +	    if (PR_FD_ISSET(fds[j], &readFdSet)) {
   1.230 +		PRFileDesc *sock;
   1.231 +
   1.232 +		nEvents++;
   1.233 +		sock = PR_Accept(fds[j], NULL, PR_INTERVAL_NO_TIMEOUT);
   1.234 +		if (sock == NULL) {
   1.235 +		    fprintf(stderr, "PR_Accept() failed\n");
   1.236 +		failed_already=1;
   1.237 +		    goto exit_now;
   1.238 +		}
   1.239 +		other_fds[nextIndex] = sock;
   1.240 +		PR_FD_SET(sock, &readFdSet);
   1.241 +		nextIndex++;
   1.242 +	    }
   1.243 +	    PR_FD_SET(fds[j], &readFdSet);
   1.244 +	}
   1.245 +
   1.246 +	for (j = 2; j < nfds; j++) {
   1.247 +	    if (PR_FD_ISSET(fds[j], &readFdSet)) {
   1.248 +		PRInt32 nBytes;
   1.249 +
   1.250 +		PR_FD_CLR(fds[j], &readFdSet);
   1.251 +		nEvents++;
   1.252 +		nBytes = PR_Read(fds[j], buf, sizeof(buf));
   1.253 +		if (nBytes == -1) {
   1.254 +		    fprintf(stderr, "PR_Read() failed\n");
   1.255 +		failed_already=1;
   1.256 +		    goto exit_now;
   1.257 +		}
   1.258 +		/* Just to be safe */
   1.259 +		buf[127] = '\0';
   1.260 +		PR_Close(fds[j]);
   1.261 +		if (debug_mode) printf("The server received \"%s\" from a client\n", buf);
   1.262 +	    } else {
   1.263 +		PR_FD_SET(fds[j], &readFdSet);
   1.264 +		other_fds[nextIndex] = fds[j];
   1.265 +		nextIndex++;
   1.266 +	    }
   1.267 +	}
   1.268 +
   1.269 +	PR_ASSERT(retVal == nEvents);
   1.270 +	/* swap */
   1.271 +	tmp = fds;
   1.272 +	fds = other_fds;
   1.273 +	other_fds = tmp;
   1.274 +	nfds = nextIndex;
   1.275 +	i += nEvents;
   1.276 +    }
   1.277 +
   1.278 +    if (debug_mode) printf("Test passed\n");
   1.279 +
   1.280 +    PR_Cleanup();
   1.281 +	goto exit_now;
   1.282 +exit_now:
   1.283 +	if(failed_already)	
   1.284 +		return 1;
   1.285 +	else
   1.286 +		return 0;
   1.287 +}

mercurial