nsprpub/pr/tests/poll_er.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/poll_er.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,210 @@
     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_err.c
    1.12 +**
    1.13 +** Description: This program tests PR_Poll with sockets.
    1.14 +**              error reporting operation is 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 +#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 +
    1.43 +#include <stdio.h>
    1.44 +#include <string.h>
    1.45 +#include <stdlib.h>
    1.46 +
    1.47 +PRIntn failed_already=0;
    1.48 +PRIntn debug_mode;
    1.49 +
    1.50 +static void
    1.51 +ClientThreadFunc(void *arg)
    1.52 +{
    1.53 +    PRFileDesc *badFD = (PRFileDesc *) arg;
    1.54 +    /*
    1.55 +     * Make the fd invalid
    1.56 +     */
    1.57 +#if defined(XP_UNIX)
    1.58 +    close(PR_FileDesc2NativeHandle(badFD));
    1.59 +#elif defined(XP_OS2)
    1.60 +    soclose(PR_FileDesc2NativeHandle(badFD));
    1.61 +#elif defined(WIN32) || defined(WIN16)
    1.62 +    closesocket(PR_FileDesc2NativeHandle(badFD));
    1.63 +#else
    1.64 +#error "Unknown architecture"
    1.65 +#endif
    1.66 +}
    1.67 +
    1.68 +int main(int argc, char **argv)
    1.69 +{
    1.70 +    PRFileDesc *listenSock1, *listenSock2;
    1.71 +    PRFileDesc *badFD;
    1.72 +    PRUint16 listenPort1, listenPort2;
    1.73 +    PRNetAddr addr;
    1.74 +    char buf[128];
    1.75 +    PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
    1.76 +    PRIntn npds;
    1.77 +    PRInt32 retVal;
    1.78 +
    1.79 +	/* The command line argument: -d is used to determine if the test is being run
    1.80 +	in debug mode. The regress tool requires only one line output:PASS or FAIL.
    1.81 +	All of the printfs associated with this test has been handled with a if (debug_mode)
    1.82 +	test.
    1.83 +	Usage: test_name -d
    1.84 +	*/
    1.85 +	PLOptStatus os;
    1.86 +	PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
    1.87 +	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    1.88 +    {
    1.89 +		if (PL_OPT_BAD == os) continue;
    1.90 +        switch (opt->option)
    1.91 +        {
    1.92 +        case 'd':  /* debug mode */
    1.93 +			debug_mode = 1;
    1.94 +            break;
    1.95 +         default:
    1.96 +            break;
    1.97 +        }
    1.98 +    }
    1.99 +	PL_DestroyOptState(opt);
   1.100 +
   1.101 + /* main test */
   1.102 +	
   1.103 +    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   1.104 +    PR_STDIO_INIT();
   1.105 +
   1.106 +    if (debug_mode) {
   1.107 +		printf("This program tests PR_Poll with sockets.\n");
   1.108 +		printf("error reporting is  tested.\n\n");
   1.109 +	}
   1.110 +
   1.111 +    /* Create two listening sockets */
   1.112 +    if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
   1.113 +	fprintf(stderr, "Can't create a new TCP socket\n");
   1.114 +	failed_already=1;
   1.115 +	goto exit_now;
   1.116 +    }
   1.117 +    addr.inet.family = AF_INET;
   1.118 +    addr.inet.ip = PR_htonl(INADDR_ANY);
   1.119 +    addr.inet.port = PR_htons(0);
   1.120 +    if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
   1.121 +	fprintf(stderr, "Can't bind socket\n");
   1.122 +	failed_already=1;
   1.123 +	goto exit_now;
   1.124 +    }
   1.125 +    if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
   1.126 +	fprintf(stderr, "PR_GetSockName failed\n");
   1.127 +	failed_already=1;
   1.128 +	goto exit_now;
   1.129 +    }
   1.130 +    listenPort1 = PR_ntohs(addr.inet.port);
   1.131 +    if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
   1.132 +	fprintf(stderr, "Can't listen on a socket\n");
   1.133 +	failed_already=1;
   1.134 +	goto exit_now;
   1.135 +    }
   1.136 +
   1.137 +    if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
   1.138 +	fprintf(stderr, "Can't create a new TCP socket\n");
   1.139 +	failed_already=1;	
   1.140 +	goto exit_now;
   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(listenSock2, &addr) == PR_FAILURE) {
   1.146 +	fprintf(stderr, "Can't bind socket\n");
   1.147 +	failed_already=1;	
   1.148 +	goto exit_now;
   1.149 +    }
   1.150 +    if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
   1.151 +	fprintf(stderr, "PR_GetSockName failed\n");
   1.152 +	failed_already=1;	
   1.153 +	goto exit_now;
   1.154 +    }
   1.155 +    listenPort2 = PR_ntohs(addr.inet.port);
   1.156 +    if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
   1.157 +	fprintf(stderr, "Can't listen on a socket\n");
   1.158 +	failed_already=1;	
   1.159 +	goto exit_now;
   1.160 +    }
   1.161 +    PR_snprintf(buf, sizeof(buf),
   1.162 +	    "The server thread is listening on ports %hu and %hu\n\n",
   1.163 +	    listenPort1, listenPort2);
   1.164 +    if (debug_mode) printf("%s", buf);
   1.165 +
   1.166 +    /* Set up the poll descriptor array */
   1.167 +    pds = pds0;
   1.168 +    other_pds = pds1;
   1.169 +    memset(pds, 0, sizeof(pds));
   1.170 +    pds[0].fd = listenSock1;
   1.171 +    pds[0].in_flags = PR_POLL_READ;
   1.172 +    pds[1].fd = listenSock2;
   1.173 +    pds[1].in_flags = PR_POLL_READ;
   1.174 +    npds = 2;
   1.175 +
   1.176 +
   1.177 +    /* Testing bad fd */
   1.178 +    if (debug_mode) printf("PR_Poll should detect a bad file descriptor\n");
   1.179 +    if ((badFD = PR_NewTCPSocket()) == NULL) {
   1.180 +	fprintf(stderr, "Can't create a TCP socket\n");
   1.181 +	goto exit_now;
   1.182 +    }
   1.183 +
   1.184 +    pds[2].fd = badFD;
   1.185 +    pds[2].in_flags = PR_POLL_READ;
   1.186 +    npds = 3;
   1.187 +
   1.188 +    if (PR_CreateThread(PR_USER_THREAD, ClientThreadFunc,
   1.189 +            badFD, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
   1.190 +            PR_UNJOINABLE_THREAD, 0) == NULL) {
   1.191 +        fprintf(stderr, "cannot create thread\n");
   1.192 +        exit(1);
   1.193 +    }
   1.194 +
   1.195 +    retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
   1.196 +    if (retVal != 1 || (unsigned short) pds[2].out_flags != PR_POLL_NVAL) {
   1.197 +	fprintf(stderr, "Failed to detect the bad fd: "
   1.198 +		"PR_Poll returns %d, out_flags is 0x%hx\n",
   1.199 +		retVal, pds[2].out_flags);
   1.200 +	failed_already=1;	
   1.201 +	goto exit_now;
   1.202 +    }
   1.203 +    if (debug_mode) printf("PR_Poll detected the bad fd.  Test passed.\n\n");
   1.204 +    PR_Cleanup();
   1.205 +	goto exit_now;
   1.206 +exit_now:
   1.207 +	if(failed_already)	
   1.208 +		return 1;
   1.209 +	else
   1.210 +		return 0;
   1.211 +}
   1.212 +
   1.213 +#endif /* XP_BEOS */

mercurial