nsprpub/pr/tests/selct_er.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/selct_er.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,200 @@
     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 +** 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 +#ifdef XP_BEOS
    1.27 +#include <stdio.h>
    1.28 +int main()
    1.29 +{
    1.30 +    printf( "This test is not ported to the BeOS\n" );
    1.31 +    return 0;
    1.32 +}
    1.33 +#else
    1.34 +
    1.35 +/***********************************************************************
    1.36 +** Includes
    1.37 +***********************************************************************/
    1.38 +/* Used to get the command line option */
    1.39 +#include "plgetopt.h"
    1.40 +
    1.41 +#include "primpl.h"
    1.42 +#include "pprio.h"
    1.43 +#include "prnetdb.h"
    1.44 +
    1.45 +#include <stdio.h>
    1.46 +#include <string.h>
    1.47 +#include <stdlib.h>
    1.48 +
    1.49 +
    1.50 +PRIntn failed_already=0;
    1.51 +PRIntn debug_mode;
    1.52 +
    1.53 +int main(int argc, char **argv)
    1.54 +{
    1.55 +    PRFileDesc *listenSock1, *listenSock2;
    1.56 +    PRFileDesc *badFD;
    1.57 +    PRUint16 listenPort1, listenPort2;
    1.58 +    PRNetAddr addr;
    1.59 +    PR_fd_set readFdSet;
    1.60 +    char buf[128];
    1.61 +    PRInt32 retVal;
    1.62 +
    1.63 +	/* The command line argument: -d is used to determine if the test is being run
    1.64 +	in debug mode. The regress tool requires only one line output:PASS or FAIL.
    1.65 +	All of the printfs associated with this test has been handled with a if (debug_mode)
    1.66 +	test.
    1.67 +	Usage: test_name -d
    1.68 +	*/
    1.69 +	PLOptStatus os;
    1.70 +	PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
    1.71 +	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    1.72 +    {
    1.73 +		if (PL_OPT_BAD == os) continue;
    1.74 +        switch (opt->option)
    1.75 +        {
    1.76 +        case 'd':  /* debug mode */
    1.77 +			debug_mode = 1;
    1.78 +            break;
    1.79 +         default:
    1.80 +            break;
    1.81 +        }
    1.82 +    }
    1.83 +	PL_DestroyOptState(opt);
    1.84 +
    1.85 + /* main test */
    1.86 +	
    1.87 +    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
    1.88 +    PR_STDIO_INIT();
    1.89 +
    1.90 +    if (debug_mode) {
    1.91 +		printf("This program tests PR_Select with sockets.  Error\n");
    1.92 +		printf("reporting operations are tested.\n\n");
    1.93 +	}
    1.94 +
    1.95 +    /* Create two listening sockets */
    1.96 +    if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
    1.97 +	fprintf(stderr, "Can't create a new TCP socket\n");
    1.98 +	failed_already=1;
    1.99 +	goto exit_now;
   1.100 +    }
   1.101 +    addr.inet.family = AF_INET;
   1.102 +    addr.inet.ip = PR_htonl(INADDR_ANY);
   1.103 +    addr.inet.port = PR_htons(0);
   1.104 +    if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
   1.105 +	fprintf(stderr, "Can't bind socket\n");
   1.106 +	failed_already=1;
   1.107 +	goto exit_now;
   1.108 +    }
   1.109 +    if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
   1.110 +	fprintf(stderr, "PR_GetSockName failed\n");
   1.111 +	failed_already=1;
   1.112 +	goto exit_now;
   1.113 +    }
   1.114 +    listenPort1 = PR_ntohs(addr.inet.port);
   1.115 +    if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
   1.116 +	fprintf(stderr, "Can't listen on a socket\n");
   1.117 +	failed_already=1;
   1.118 +	goto exit_now;
   1.119 +    }
   1.120 +
   1.121 +    if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
   1.122 +	fprintf(stderr, "Can't create a new TCP socket\n");
   1.123 +	failed_already=1;
   1.124 +	goto exit_now;
   1.125 +    }
   1.126 +    addr.inet.family = AF_INET;
   1.127 +    addr.inet.ip = PR_htonl(INADDR_ANY);
   1.128 +    addr.inet.port = PR_htons(0);
   1.129 +    if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
   1.130 +	fprintf(stderr, "Can't bind socket\n");
   1.131 +	failed_already=1;
   1.132 +	goto exit_now;
   1.133 +    }
   1.134 +    if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
   1.135 +	fprintf(stderr, "PR_GetSockName failed\n");
   1.136 +	failed_already=1;
   1.137 +	goto exit_now;
   1.138 +    }
   1.139 +    listenPort2 = PR_ntohs(addr.inet.port);
   1.140 +    if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
   1.141 +	fprintf(stderr, "Can't listen on a socket\n");
   1.142 +	failed_already=1;
   1.143 +	goto exit_now;
   1.144 +    }
   1.145 +    PR_snprintf(buf, sizeof(buf),
   1.146 +	    "The server thread is listening on ports %hu and %hu\n\n",
   1.147 +	    listenPort1, listenPort2);
   1.148 +    if (debug_mode) printf("%s", buf);
   1.149 +
   1.150 +    /* Set up the fd set */
   1.151 +    PR_FD_ZERO(&readFdSet);
   1.152 +    PR_FD_SET(listenSock1, &readFdSet);
   1.153 +    PR_FD_SET(listenSock2, &readFdSet);
   1.154 +
   1.155 +
   1.156 +    /* Testing bad fd */
   1.157 +    if (debug_mode) printf("PR_Select should detect a bad file descriptor\n");
   1.158 +    if ((badFD = PR_NewTCPSocket()) == NULL) {
   1.159 +	fprintf(stderr, "Can't create a TCP socket\n");
   1.160 +	failed_already=1;
   1.161 +	goto exit_now;
   1.162 +    }
   1.163 +
   1.164 +    PR_FD_SET(badFD, &readFdSet);
   1.165 +    /*
   1.166 +     * Make the fd invalid
   1.167 +     */
   1.168 +#if defined(XP_UNIX)
   1.169 +    close(PR_FileDesc2NativeHandle(badFD));
   1.170 +#elif defined(XP_OS2)
   1.171 +    soclose(PR_FileDesc2NativeHandle(badFD));
   1.172 +#elif defined(WIN32) || defined(WIN16)
   1.173 +    closesocket(PR_FileDesc2NativeHandle(badFD));
   1.174 +#else
   1.175 +#error "Unknown architecture"
   1.176 +#endif
   1.177 +
   1.178 +    retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
   1.179 +	    PR_INTERVAL_NO_TIMEOUT);
   1.180 +    if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) {
   1.181 +	fprintf(stderr, "Failed to detect the bad fd: "
   1.182 +		"PR_Select returns %d\n", retVal);
   1.183 +	if (retVal == -1) {
   1.184 +	    fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(),
   1.185 +		    PR_GetOSError());
   1.186 +		failed_already=1;
   1.187 +	}
   1.188 +	goto exit_now;
   1.189 +    }
   1.190 +    if (debug_mode) printf("PR_Select detected a bad fd.  Test passed.\n\n");
   1.191 +	PR_FD_CLR(badFD, &readFdSet);
   1.192 +
   1.193 +	PR_Cleanup();
   1.194 +	goto exit_now;
   1.195 +exit_now:
   1.196 +	if(failed_already)	
   1.197 +		return 1;
   1.198 +	else
   1.199 +		return 0;
   1.200 +
   1.201 +}
   1.202 +
   1.203 +#endif /* XP_BEOS */

mercurial