nsprpub/pr/tests/selct_to.c

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 /***********************************************************************
     7 **  1997 - Netscape Communications Corporation
     8 **
     9 ** Name: prselect_to.c
    10 **
    11 ** Description: tests PR_Select with sockets. Time out functions
    12 **
    13 ** Modification History:
    14 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
    15 **	         The debug mode will print all of the printfs associated with this test.
    16 **			 The regress mode will be the default mode. Since the regress tool limits
    17 **           the output to a one line status:PASS or FAIL,all of the printf statements
    18 **			 have been handled with an if (debug_mode) statement.
    19 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
    20 **			recognize the return code from tha main program.
    21 ***********************************************************************/
    23 /***********************************************************************
    24 ** Includes
    25 ***********************************************************************/
    26 /* Used to get the command line option */
    27 #include "plgetopt.h"
    29 #include "prinit.h"
    30 #include "prio.h"
    31 #include "prlog.h"
    32 #include "prprf.h"
    33 #include "prnetdb.h"
    35 #include "obsolete/probslet.h"
    37 #include "prerror.h"
    39 #include <stdio.h>
    40 #include <string.h>
    41 #include <stdlib.h>
    43 PRIntn failed_already=0;
    44 PRIntn debug_mode;
    46 int main(int argc, char **argv)
    47 {
    48     PRFileDesc *listenSock1, *listenSock2;
    49     PRUint16 listenPort1, listenPort2;
    50     PRNetAddr addr;
    51     PR_fd_set readFdSet;
    52     char buf[128];
    53     PRInt32 retVal;
    55 	/* The command line argument: -d is used to determine if the test is being run
    56 	in debug mode. The regress tool requires only one line output:PASS or FAIL.
    57 	All of the printfs associated with this test has been handled with a if (debug_mode)
    58 	test.
    59 	Usage: test_name -d
    60 	*/
    61 	PLOptStatus os;
    62 	PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
    63 	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    64     {
    65 		if (PL_OPT_BAD == os) continue;
    66         switch (opt->option)
    67         {
    68         case 'd':  /* debug mode */
    69 			debug_mode = 1;
    70             break;
    71          default:
    72             break;
    73         }
    74     }
    75 	PL_DestroyOptState(opt);
    77  /* main test */
    79     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
    80     PR_STDIO_INIT();
    82     if (debug_mode) {
    83 		printf("This program tests PR_Select with sockets.  Timeout \n");
    84 		printf("operations are tested.\n\n");
    85 	}
    87     /* Create two listening sockets */
    88     if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
    89 	fprintf(stderr, "Can't create a new TCP socket\n");
    90 	failed_already=1;
    91 	goto exit_now;
    92     }
    93     addr.inet.family = PR_AF_INET;
    94     addr.inet.ip = PR_htonl(PR_INADDR_ANY);
    95     addr.inet.port = PR_htons(0);
    96     if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
    97 	fprintf(stderr, "Can't bind socket\n");
    98 	failed_already=1;
    99 	goto exit_now;
   100     }
   101     if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
   102 	fprintf(stderr, "PR_GetSockName failed\n");
   103 	failed_already=1;
   104 	goto exit_now;
   105     }
   106     listenPort1 = PR_ntohs(addr.inet.port);
   107     if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
   108 	fprintf(stderr, "Can't listen on a socket\n");
   109 	failed_already=1;
   110 	goto exit_now;
   111     }
   113     if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
   114 	fprintf(stderr, "Can't create a new TCP socket\n");
   115 	failed_already=1;
   116 	goto exit_now;
   117     }
   118     addr.inet.family = PR_AF_INET;
   119     addr.inet.ip = PR_htonl(PR_INADDR_ANY);
   120     addr.inet.port = PR_htons(0);
   121     if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
   122 	fprintf(stderr, "Can't bind socket\n");
   123 	failed_already=1;
   124 	goto exit_now;
   125     }
   126     if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
   127 	fprintf(stderr, "PR_GetSockName failed\n");
   128 	failed_already=1;
   129 	goto exit_now;
   130     }
   131     listenPort2 = PR_ntohs(addr.inet.port);
   132     if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
   133 	fprintf(stderr, "Can't listen on a socket\n");
   134 	failed_already=1;
   135 	goto exit_now;
   136     }
   137     PR_snprintf(buf, sizeof(buf),
   138 	    "The server thread is listening on ports %hu and %hu\n\n",
   139 	    listenPort1, listenPort2);
   140     if (debug_mode) printf("%s", buf);
   142     /* Set up the fd set */
   143     PR_FD_ZERO(&readFdSet);
   144     PR_FD_SET(listenSock1, &readFdSet);
   145     PR_FD_SET(listenSock2, &readFdSet);
   147     /* Testing timeout */
   148     if (debug_mode) printf("PR_Select should time out in 5 seconds\n");
   149     retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
   150 	    PR_SecondsToInterval(5));
   151     if (retVal != 0) {
   152 	PR_snprintf(buf, sizeof(buf),
   153 		"PR_Select should time out and return 0, but it returns %ld\n",
   154 		retVal);
   155 	fprintf(stderr, "%s", buf);
   156 	if (retVal == -1) {
   157 	    fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(),
   158 		    PR_GetOSError());
   159 			failed_already=1;
   160 	}
   161 	goto exit_now;
   162     }
   163     if (debug_mode) printf("PR_Select timed out.  Test passed.\n\n");
   165     PR_Cleanup();
   167 exit_now:
   168 	if(failed_already)	
   169 		return 1;
   170 	else
   171 		return 0;
   172 }

mercurial