nsprpub/pr/tests/poll_nm.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/poll_nm.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,345 @@
     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 +**
    1.11 +** Name: prpoll_norm.c
    1.12 +**
    1.13 +** Description: This program tests PR_Poll with sockets.
    1.14 +**              Normal operation are tested
    1.15 +**
    1.16 +** Modification History:
    1.17 +** 19-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 "prnetdb.h"
    1.37 +#include "obsolete/probslet.h"
    1.38 +
    1.39 +#include "private/pprio.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 +#define NUM_ITERATIONS 5
    1.49 +
    1.50 +static void PR_CALLBACK
    1.51 +clientThreadFunc(void *arg)
    1.52 +{
    1.53 +    PRUintn port = (PRUintn) arg;
    1.54 +    PRFileDesc *sock;
    1.55 +    PRNetAddr addr;
    1.56 +    char buf[128];
    1.57 +    int i;
    1.58 +    PRStatus sts;
    1.59 +    PRInt32 n;
    1.60 +
    1.61 +    addr.inet.family = PR_AF_INET;
    1.62 +    addr.inet.port = PR_htons((PRUint16)port);
    1.63 +    addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
    1.64 +    memset(buf, 0, sizeof(buf));
    1.65 +    PR_snprintf(buf, sizeof(buf), "%hu", port);
    1.66 +
    1.67 +    for (i = 0; i < NUM_ITERATIONS; i++) {
    1.68 +	sock = PR_NewTCPSocket();
    1.69 +	PR_ASSERT(sock != NULL);
    1.70 +	
    1.71 +    sts = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
    1.72 +	PR_ASSERT(sts == PR_SUCCESS);
    1.73 +
    1.74 +	n = PR_Write(sock, buf, sizeof(buf));
    1.75 +	PR_ASSERT(n >= 0);
    1.76 +
    1.77 +	sts = PR_Close(sock);
    1.78 +	PR_ASSERT(sts == PR_SUCCESS);
    1.79 +    }
    1.80 +}
    1.81 +
    1.82 +int main(int argc, char **argv)
    1.83 +{
    1.84 +    PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
    1.85 +    PRUint16 listenPort1, listenPort2;
    1.86 +    PRNetAddr addr;
    1.87 +    char buf[128];
    1.88 +    PRThread *clientThread;
    1.89 +    PRPollDesc pds0[20], pds1[20], *pds, *other_pds;
    1.90 +    PRIntn npds;
    1.91 +    PRInt32 retVal;
    1.92 +    PRIntn i, j;
    1.93 +    PRSocketOptionData optval;
    1.94 +
    1.95 +	/* The command line argument: -d is used to determine if the test is being run
    1.96 +	in debug mode. The regress tool requires only one line output:PASS or FAIL.
    1.97 +	All of the printfs associated with this test has been handled with a if (debug_mode)
    1.98 +	test.
    1.99 +	Usage: test_name -d
   1.100 +	*/
   1.101 +	PLOptStatus os;
   1.102 +	PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
   1.103 +	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
   1.104 +    {
   1.105 +		if (PL_OPT_BAD == os) continue;
   1.106 +        switch (opt->option)
   1.107 +        {
   1.108 +        case 'd':  /* debug mode */
   1.109 +			debug_mode = 1;
   1.110 +            break;
   1.111 +         default:
   1.112 +            break;
   1.113 +        }
   1.114 +    }
   1.115 +	PL_DestroyOptState(opt);
   1.116 +
   1.117 + /* main test */
   1.118 +	
   1.119 +    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   1.120 +    PR_STDIO_INIT();
   1.121 +
   1.122 +    if (debug_mode) {
   1.123 +		printf("This program tests PR_Poll with sockets.\n");
   1.124 +		printf("Normal operation are tested.\n\n");
   1.125 +	}
   1.126 +
   1.127 +    /* Create two listening sockets */
   1.128 +    if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
   1.129 +	fprintf(stderr, "Can't create a new TCP socket\n");
   1.130 +	failed_already=1;
   1.131 +	goto exit_now;
   1.132 +    }
   1.133 +    memset(&addr, 0, sizeof(addr));
   1.134 +    addr.inet.family = PR_AF_INET;
   1.135 +    addr.inet.ip = PR_htonl(PR_INADDR_ANY);
   1.136 +    addr.inet.port = PR_htons(0);
   1.137 +    if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
   1.138 +	fprintf(stderr, "Can't bind socket\n");
   1.139 +	failed_already=1;
   1.140 +	goto exit_now;
   1.141 +    }
   1.142 +    if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
   1.143 +	fprintf(stderr, "PR_GetSockName failed\n");
   1.144 +	failed_already=1;
   1.145 +	goto exit_now;
   1.146 +    }
   1.147 +    listenPort1 = PR_ntohs(addr.inet.port);
   1.148 +    optval.option = PR_SockOpt_Nonblocking;
   1.149 +    optval.value.non_blocking = PR_TRUE;
   1.150 +    PR_SetSocketOption(listenSock1, &optval);
   1.151 +    if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
   1.152 +	fprintf(stderr, "Can't listen on a socket\n");
   1.153 +	failed_already=1;
   1.154 +	goto exit_now;
   1.155 +    }
   1.156 +
   1.157 +    if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
   1.158 +	fprintf(stderr, "Can't create a new TCP socket\n");
   1.159 +	failed_already=1;	
   1.160 +	goto exit_now;
   1.161 +    }
   1.162 +    addr.inet.family = PR_AF_INET;
   1.163 +    addr.inet.ip = PR_htonl(PR_INADDR_ANY);
   1.164 +    addr.inet.port = PR_htons(0);
   1.165 +    if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
   1.166 +	fprintf(stderr, "Can't bind socket\n");
   1.167 +	failed_already=1;	
   1.168 +	goto exit_now;
   1.169 +    }
   1.170 +    if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
   1.171 +	fprintf(stderr, "PR_GetSockName failed\n");
   1.172 +	failed_already=1;	
   1.173 +	goto exit_now;
   1.174 +    }
   1.175 +    listenPort2 = PR_ntohs(addr.inet.port);
   1.176 +    PR_SetSocketOption(listenSock2, &optval);
   1.177 +    if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
   1.178 +	fprintf(stderr, "Can't listen on a socket\n");
   1.179 +	failed_already=1;	
   1.180 +	goto exit_now;
   1.181 +    }
   1.182 +    PR_snprintf(buf, sizeof(buf),
   1.183 +	    "The server thread is listening on ports %hu and %hu\n\n",
   1.184 +	    listenPort1, listenPort2);
   1.185 +    if (debug_mode) printf("%s", buf);
   1.186 +
   1.187 +    /* Set up the poll descriptor array */
   1.188 +    pds = pds0;
   1.189 +    other_pds = pds1;
   1.190 +    memset(pds, 0, sizeof(pds));
   1.191 +    pds[0].fd = listenSock1;
   1.192 +    pds[0].in_flags = PR_POLL_READ;
   1.193 +    pds[1].fd = listenSock2;
   1.194 +    pds[1].in_flags = PR_POLL_READ;
   1.195 +    /* Add some unused entries to test if they are ignored by PR_Poll() */
   1.196 +    memset(&pds[2], 0, sizeof(pds[2]));
   1.197 +    memset(&pds[3], 0, sizeof(pds[3]));
   1.198 +    memset(&pds[4], 0, sizeof(pds[4]));
   1.199 +    npds = 5;
   1.200 +
   1.201 +    clientThread = PR_CreateThread(PR_USER_THREAD,
   1.202 +	    clientThreadFunc, (void *) listenPort1,
   1.203 +	    PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
   1.204 +	    PR_UNJOINABLE_THREAD, 0);
   1.205 +    if (clientThread == NULL) {
   1.206 +	fprintf(stderr, "can't create thread\n");
   1.207 +	failed_already=1;	
   1.208 +	goto exit_now;
   1.209 +    }
   1.210 +
   1.211 +    clientThread = PR_CreateThread(PR_USER_THREAD,
   1.212 +	    clientThreadFunc, (void *) listenPort2,
   1.213 +	    PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
   1.214 +	    PR_UNJOINABLE_THREAD, 0);
   1.215 +    if (clientThread == NULL) {
   1.216 +	fprintf(stderr, "can't create thread\n");
   1.217 +	failed_already=1;		
   1.218 +	goto exit_now;
   1.219 +    }
   1.220 +
   1.221 +    if (debug_mode) {
   1.222 +		printf("Two client threads are created.  Each of them will\n");
   1.223 +		printf("send data to one of the two ports the server is listening on.\n");
   1.224 +		printf("The data they send is the port number.  Each of them send\n");
   1.225 +		printf("the data five times, so you should see ten lines below,\n");
   1.226 +		printf("interleaved in an arbitrary order.\n");
   1.227 +	}
   1.228 +
   1.229 +    /* two clients, three events per iteration: accept, read, close */
   1.230 +    i = 0;
   1.231 +    while (i < 2 * 3 * NUM_ITERATIONS) {
   1.232 +	PRPollDesc *tmp;
   1.233 +	int nextIndex;
   1.234 +	int nEvents = 0;
   1.235 +
   1.236 +	retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
   1.237 +	PR_ASSERT(retVal != 0);  /* no timeout */
   1.238 +	if (retVal == -1) {
   1.239 +	    fprintf(stderr, "PR_Poll failed\n");
   1.240 +		failed_already=1;			
   1.241 +	    goto exit_now;
   1.242 +	}
   1.243 +
   1.244 +	nextIndex = 2;
   1.245 +	/* the two listening sockets */
   1.246 +	for (j = 0; j < 2; j++) {
   1.247 +	    other_pds[j] = pds[j];
   1.248 +	    PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
   1.249 +		    && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
   1.250 +	    if (pds[j].out_flags & PR_POLL_READ) {
   1.251 +		PRFileDesc *sock;
   1.252 +
   1.253 +		nEvents++;
   1.254 +		sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT);
   1.255 +		if (sock == NULL) {
   1.256 +		    fprintf(stderr, "PR_Accept() failed\n");
   1.257 +			failed_already=1;	
   1.258 +		    goto exit_now;
   1.259 +		}
   1.260 +		other_pds[nextIndex].fd = sock;
   1.261 +		other_pds[nextIndex].in_flags = PR_POLL_READ;
   1.262 +		nextIndex++;
   1.263 +	    } else if (pds[j].out_flags & PR_POLL_ERR) {
   1.264 +		fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
   1.265 +		failed_already=1;	
   1.266 +		goto exit_now;
   1.267 +	    } else if (pds[j].out_flags & PR_POLL_NVAL) {
   1.268 +		fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n",
   1.269 +			PR_FileDesc2NativeHandle(pds[j].fd));
   1.270 +		failed_already=1;	
   1.271 +		goto exit_now;
   1.272 +	    }
   1.273 +	}
   1.274 +
   1.275 +	for (j = 2; j < npds; j++) {
   1.276 +            if (NULL == pds[j].fd) {
   1.277 +                /*
   1.278 +                 * Keep the unused entries in the poll descriptor array
   1.279 +                 * for testing purposes.
   1.280 +                 */
   1.281 +                other_pds[nextIndex] = pds[j];
   1.282 +                nextIndex++;
   1.283 +                continue;
   1.284 +            }
   1.285 +
   1.286 +	    PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
   1.287 +		    && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
   1.288 +	    if (pds[j].out_flags & PR_POLL_READ) {
   1.289 +                PRInt32 nAvail;
   1.290 +		PRInt32 nRead;
   1.291 +
   1.292 +		nEvents++;
   1.293 +                nAvail = PR_Available(pds[j].fd);
   1.294 +		nRead = PR_Read(pds[j].fd, buf, sizeof(buf));
   1.295 +                PR_ASSERT(nAvail == nRead);
   1.296 +		if (nRead == -1) {
   1.297 +		    fprintf(stderr, "PR_Read() failed\n");
   1.298 +			failed_already=1;	
   1.299 +		    goto exit_now;
   1.300 +                } else if (nRead == 0) {
   1.301 +                    PR_Close(pds[j].fd);
   1.302 +                    continue;
   1.303 +                } else {
   1.304 +                    /* Just to be safe */
   1.305 +                    buf[127] = '\0';
   1.306 +                    if (debug_mode) printf("The server received \"%s\" from a client\n", buf);
   1.307 +                }
   1.308 +	    } else if (pds[j].out_flags & PR_POLL_ERR) {
   1.309 +		fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
   1.310 +		failed_already=1;			
   1.311 +		goto exit_now;
   1.312 +	    } else if (pds[j].out_flags & PR_POLL_NVAL) {
   1.313 +		fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n");
   1.314 +		failed_already=1;			
   1.315 +		goto exit_now;
   1.316 +	    }
   1.317 +            other_pds[nextIndex] = pds[j];
   1.318 +            nextIndex++;
   1.319 +	}
   1.320 +
   1.321 +	PR_ASSERT(retVal == nEvents);
   1.322 +	/* swap */
   1.323 +	tmp = pds;
   1.324 +	pds = other_pds;
   1.325 +	other_pds = tmp;
   1.326 +	npds = nextIndex;
   1.327 +	i += nEvents;
   1.328 +    }
   1.329 +
   1.330 +    if (debug_mode) printf("Tests passed\n");
   1.331 +
   1.332 +exit_now:
   1.333 +
   1.334 +    if (listenSock1) {
   1.335 +        PR_Close(listenSock1);
   1.336 +    }
   1.337 +    if (listenSock2) {
   1.338 +        PR_Close(listenSock2);
   1.339 +    }
   1.340 +
   1.341 +    PR_Cleanup();
   1.342 +	
   1.343 +	if(failed_already)	
   1.344 +		return 1;
   1.345 +	else
   1.346 +		return 0;
   1.347 +
   1.348 +}

mercurial