nsprpub/pr/tests/prselect.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/prselect.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,340 @@
     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_err.c
    1.13 +**
    1.14 +** Description: tests PR_Select with sockets Error condition functions.
    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 +***********************************************************************/
    1.23 +
    1.24 +/***********************************************************************
    1.25 +** Includes
    1.26 +***********************************************************************/
    1.27 +/* Used to get the command line option */
    1.28 +#include "plgetopt.h"
    1.29 +#include "prttools.h"
    1.30 +
    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 <stdio.h>
    1.40 +#include <string.h>
    1.41 +#include <stdlib.h>
    1.42 +
    1.43 +/***********************************************************************
    1.44 +** PRIVATE FUNCTION:    Test_Result
    1.45 +** DESCRIPTION: Used in conjunction with the regress tool, prints out the
    1.46 +**				status of the test case.
    1.47 +** INPUTS:      PASS/FAIL
    1.48 +** OUTPUTS:     None
    1.49 +** RETURN:      None
    1.50 +** SIDE EFFECTS:
    1.51 +**      
    1.52 +** RESTRICTIONS:
    1.53 +**      None
    1.54 +** MEMORY:      NA
    1.55 +** ALGORITHM:   Determine what the status is and print accordingly.
    1.56 +**      
    1.57 +***********************************************************************/
    1.58 +
    1.59 +
    1.60 +static Test_Result (int result)
    1.61 +{
    1.62 +	if (result == PASS)
    1.63 +		printf ("PASS\n");
    1.64 +	else
    1.65 +		printf ("FAIL\n");
    1.66 +}
    1.67 +
    1.68 +static void
    1.69 +clientThreadFunc(void *arg)
    1.70 +{
    1.71 +    PRUint16 port = (PRUint16) arg;
    1.72 +    PRFileDesc *sock;
    1.73 +    PRNetAddr addr;
    1.74 +    char buf[128];
    1.75 +    int i;
    1.76 +
    1.77 +    addr.inet.family = AF_INET;
    1.78 +    addr.inet.port = PR_htons(port);
    1.79 +    addr.inet.ip = PR_htonl(INADDR_LOOPBACK);
    1.80 +    PR_snprintf(buf, sizeof(buf), "%hu", port);
    1.81 +
    1.82 +    for (i = 0; i < 5; i++) {
    1.83 +	sock = PR_NewTCPSocket();
    1.84 +        PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
    1.85 +	PR_Write(sock, buf, sizeof(buf));
    1.86 +	PR_Close(sock);
    1.87 +    }
    1.88 +}
    1.89 +
    1.90 +int main(int argc, char **argv)
    1.91 +{
    1.92 +    PRFileDesc *listenSock1, *listenSock2;
    1.93 +    PRFileDesc *badFD;
    1.94 +    PRFileDesc *fds0[10], *fds1[10], **fds, **other_fds;
    1.95 +    PRIntn nfds;
    1.96 +    PRUint16 listenPort1, listenPort2;
    1.97 +    PRNetAddr addr;
    1.98 +    PR_fd_set readFdSet;
    1.99 +    char buf[128];
   1.100 +    PRThread *clientThread;
   1.101 +    PRInt32 retVal;
   1.102 +    PRIntn i, j;
   1.103 +
   1.104 +	/* The command line argument: -d is used to determine if the test is being run
   1.105 +	in debug mode. The regress tool requires only one line output:PASS or FAIL.
   1.106 +	All of the printfs associated with this test has been handled with a if (debug_mode)
   1.107 +	test.
   1.108 +	Usage: test_name -d
   1.109 +	*/
   1.110 +	PLOptStatus os;
   1.111 +	PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
   1.112 +	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
   1.113 +    {
   1.114 +		if (PL_OPT_BAD == os) continue;
   1.115 +        switch (opt->option)
   1.116 +        {
   1.117 +        case 'd':  /* debug mode */
   1.118 +			debug_mode = 1;
   1.119 +            break;
   1.120 +         default:
   1.121 +            break;
   1.122 +        }
   1.123 +    }
   1.124 +	PL_DestroyOptState(opt);
   1.125 +
   1.126 + /* main test */
   1.127 +	
   1.128 +    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   1.129 +    PR_STDIO_INIT();
   1.130 +
   1.131 +    if (debug_mode) {
   1.132 +		printf("This program tests PR_Select with sockets.  Timeout, error\n");
   1.133 +		printf("reporting, and normal operation are tested.\n\n");
   1.134 +	}
   1.135 +
   1.136 +    /* Create two listening sockets */
   1.137 +    if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
   1.138 +	fprintf(stderr, "Can't create a new TCP socket\n");
   1.139 +	if (!debug_mode) Test_Result(FAIL);
   1.140 +	exit(1);
   1.141 +    }
   1.142 +    addr.inet.family = AF_INET;
   1.143 +    addr.inet.ip = PR_htonl(INADDR_ANY);
   1.144 +    addr.inet.port = PR_htons(0);
   1.145 +    if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
   1.146 +	fprintf(stderr, "Can't bind socket\n");
   1.147 +	if (!debug_mode) Test_Result(FAIL);
   1.148 +	exit(1);
   1.149 +    }
   1.150 +    if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
   1.151 +	fprintf(stderr, "PR_GetSockName failed\n");
   1.152 +	if (!debug_mode) Test_Result(FAIL);
   1.153 +	exit(1);
   1.154 +    }
   1.155 +    listenPort1 = PR_ntohs(addr.inet.port);
   1.156 +    if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
   1.157 +	fprintf(stderr, "Can't listen on a socket\n");
   1.158 +	if (!debug_mode) Test_Result(FAIL);
   1.159 +	exit(1);
   1.160 +    }
   1.161 +
   1.162 +    if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
   1.163 +	fprintf(stderr, "Can't create a new TCP socket\n");
   1.164 +	if (!debug_mode) Test_Result(FAIL);
   1.165 +	exit(1);
   1.166 +    }
   1.167 +    addr.inet.family = AF_INET;
   1.168 +    addr.inet.ip = PR_htonl(INADDR_ANY);
   1.169 +    addr.inet.port = PR_htons(0);
   1.170 +    if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
   1.171 +	fprintf(stderr, "Can't bind socket\n");
   1.172 +	if (!debug_mode) Test_Result(FAIL);
   1.173 +	exit(1);
   1.174 +    }
   1.175 +    if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
   1.176 +	fprintf(stderr, "PR_GetSockName failed\n");
   1.177 +	if (!debug_mode) Test_Result(FAIL);
   1.178 +	exit(1);
   1.179 +    }
   1.180 +    listenPort2 = PR_ntohs(addr.inet.port);
   1.181 +    if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
   1.182 +	fprintf(stderr, "Can't listen on a socket\n");
   1.183 +	if (!debug_mode) Test_Result(FAIL);
   1.184 +	exit(1);
   1.185 +    }
   1.186 +    PR_snprintf(buf, sizeof(buf),
   1.187 +	    "The server thread is listening on ports %hu and %hu\n\n",
   1.188 +	    listenPort1, listenPort2);
   1.189 +    printf("%s", buf);
   1.190 +
   1.191 +    /* Set up the fd set */
   1.192 +    PR_FD_ZERO(&readFdSet);
   1.193 +    PR_FD_SET(listenSock1, &readFdSet);
   1.194 +    PR_FD_SET(listenSock2, &readFdSet);
   1.195 +
   1.196 +    /* Testing timeout */
   1.197 +    if (debug_mode) printf("PR_Select should time out in 5 seconds\n");
   1.198 +    retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
   1.199 +	    PR_SecondsToInterval(5));
   1.200 +    if (retVal != 0) {
   1.201 +	PR_snprintf(buf, sizeof(buf),
   1.202 +		"PR_Select should time out and return 0, but it returns %ld\n",
   1.203 +		retVal);
   1.204 +	fprintf(stderr, "%s", buf);
   1.205 +	if (retVal == -1) {
   1.206 +	    fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(),
   1.207 +		    PR_GetOSError());
   1.208 +			if (!debug_mode) Test_Result(FAIL);
   1.209 +	}
   1.210 +	exit(1);
   1.211 +    }
   1.212 +    if (debug_mode) printf("PR_Select timed out.  Test passed.\n\n");
   1.213 +	else Test_Result(PASS);
   1.214 +
   1.215 +    /* Testing bad fd */
   1.216 +    printf("PR_Select should detect a bad file descriptor\n");
   1.217 +    if ((badFD = PR_NewTCPSocket()) == NULL) {
   1.218 +	fprintf(stderr, "Can't create a TCP socket\n");
   1.219 +	exit(1);
   1.220 +    }
   1.221 +
   1.222 +    PR_FD_SET(listenSock1, &readFdSet);
   1.223 +    PR_FD_SET(listenSock2, &readFdSet);
   1.224 +    PR_FD_SET(badFD, &readFdSet);
   1.225 +    PR_Close(badFD);  /* make the fd bad */
   1.226 +    retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
   1.227 +	    PR_INTERVAL_NO_TIMEOUT);
   1.228 +    if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) {
   1.229 +	fprintf(stderr, "Failed to detect the bad fd: "
   1.230 +		"PR_Select returns %d\n", retVal);
   1.231 +	if (retVal == -1) {
   1.232 +	    fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(),
   1.233 +		    PR_GetOSError());
   1.234 +	}
   1.235 +	exit(1);
   1.236 +    }
   1.237 +    printf("PR_Select detected a bad fd.  Test passed.\n\n");
   1.238 +    PR_FD_CLR(badFD, &readFdSet);
   1.239 +
   1.240 +    clientThread = PR_CreateThread(PR_USER_THREAD,
   1.241 +	    clientThreadFunc, (void *) listenPort1,
   1.242 +	    PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
   1.243 +	    PR_UNJOINABLE_THREAD, 0);
   1.244 +    if (clientThread == NULL) {
   1.245 +	fprintf(stderr, "can't create thread\n");
   1.246 +	exit(1);
   1.247 +    }
   1.248 +
   1.249 +    clientThread = PR_CreateThread(PR_USER_THREAD,
   1.250 +	    clientThreadFunc, (void *) listenPort2,
   1.251 +	    PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
   1.252 +	    PR_UNJOINABLE_THREAD, 0);
   1.253 +    if (clientThread == NULL) {
   1.254 +	fprintf(stderr, "can't create thread\n");
   1.255 +	exit(1);
   1.256 +    }
   1.257 +
   1.258 +    printf("Two client threads are created.  Each of them will\n");
   1.259 +    printf("send data to one of the two ports the server is listening on.\n");
   1.260 +    printf("The data they send is the port number.  Each of them send\n");
   1.261 +    printf("the data five times, so you should see ten lines below,\n");
   1.262 +    printf("interleaved in an arbitrary order.\n");
   1.263 +
   1.264 +    /* set up the fd array */
   1.265 +    fds = fds0;
   1.266 +    other_fds = fds1;
   1.267 +    fds[0] = listenSock1;
   1.268 +    fds[1] = listenSock2;
   1.269 +    nfds = 2;
   1.270 +    PR_FD_SET(listenSock1, &readFdSet);
   1.271 +    PR_FD_SET(listenSock2, &readFdSet);
   1.272 +
   1.273 +    /* 20 events total */
   1.274 +    i = 0;
   1.275 +    while (i < 20) {
   1.276 +	PRFileDesc **tmp;
   1.277 +	int nextIndex;
   1.278 +	int nEvents = 0;
   1.279 +
   1.280 +	retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
   1.281 +		PR_INTERVAL_NO_TIMEOUT);
   1.282 +	PR_ASSERT(retVal != 0);  /* no timeout */
   1.283 +	if (retVal == -1) {
   1.284 +	    fprintf(stderr, "PR_Select failed (%d, %d)\n", PR_GetError(),
   1.285 +		    PR_GetOSError());
   1.286 +	    exit(1);
   1.287 +	}
   1.288 +
   1.289 +	nextIndex = 2;
   1.290 +	/* the two listening sockets */
   1.291 +	for (j = 0; j < 2; j++) {
   1.292 +	    other_fds[j] = fds[j];
   1.293 +	    if (PR_FD_ISSET(fds[j], &readFdSet)) {
   1.294 +		PRFileDesc *sock;
   1.295 +
   1.296 +		nEvents++;
   1.297 +		sock = PR_Accept(fds[j], NULL, PR_INTERVAL_NO_TIMEOUT);
   1.298 +		if (sock == NULL) {
   1.299 +		    fprintf(stderr, "PR_Accept() failed\n");
   1.300 +		    exit(1);
   1.301 +		}
   1.302 +		other_fds[nextIndex] = sock;
   1.303 +		PR_FD_SET(sock, &readFdSet);
   1.304 +		nextIndex++;
   1.305 +	    }
   1.306 +	    PR_FD_SET(fds[j], &readFdSet);
   1.307 +	}
   1.308 +
   1.309 +	for (j = 2; j < nfds; j++) {
   1.310 +	    if (PR_FD_ISSET(fds[j], &readFdSet)) {
   1.311 +		PRInt32 nBytes;
   1.312 +
   1.313 +		PR_FD_CLR(fds[j], &readFdSet);
   1.314 +		nEvents++;
   1.315 +		nBytes = PR_Read(fds[j], buf, sizeof(buf));
   1.316 +		if (nBytes == -1) {
   1.317 +		    fprintf(stderr, "PR_Read() failed\n");
   1.318 +		    exit(1);
   1.319 +		}
   1.320 +		/* Just to be safe */
   1.321 +		buf[127] = '\0';
   1.322 +		PR_Close(fds[j]);
   1.323 +		printf("The server received \"%s\" from a client\n", buf);
   1.324 +	    } else {
   1.325 +		PR_FD_SET(fds[j], &readFdSet);
   1.326 +		other_fds[nextIndex] = fds[j];
   1.327 +		nextIndex++;
   1.328 +	    }
   1.329 +	}
   1.330 +
   1.331 +	PR_ASSERT(retVal == nEvents);
   1.332 +	/* swap */
   1.333 +	tmp = fds;
   1.334 +	fds = other_fds;
   1.335 +	other_fds = tmp;
   1.336 +	nfds = nextIndex;
   1.337 +	i += nEvents;
   1.338 +    }
   1.339 +
   1.340 +    printf("All tests finished\n");
   1.341 +    PR_Cleanup();
   1.342 +    return 0;
   1.343 +}

mercurial