tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

selct_er.c (5614B)


      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/. */
      5 
      6 /***********************************************************************
      7 **  1997 - Netscape Communications Corporation
      8 **
      9 ** Name: prselect_err.c
     10 **
     11 ** Description: tests PR_Select with sockets Error condition 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
     16 *test.
     17 **           The regress mode will be the default mode. Since the regress tool
     18 *limits
     19 **           the output to a one line status:PASS or FAIL,all of the printf
     20 *statements
     21 **           have been handled with an if (debug_mode) statement.
     22 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been
     23 *updated to
     24 **          recognize the return code from tha main program.
     25 ***********************************************************************/
     26 
     27 /***********************************************************************
     28 ** Includes
     29 ***********************************************************************/
     30 /* Used to get the command line option */
     31 #include "plgetopt.h"
     32 
     33 #include "primpl.h"
     34 #include "pprio.h"
     35 #include "prnetdb.h"
     36 
     37 #include <stdio.h>
     38 #include <string.h>
     39 #include <stdlib.h>
     40 
     41 PRIntn failed_already = 0;
     42 PRIntn debug_mode;
     43 
     44 int main(int argc, char** argv) {
     45  PRFileDesc *listenSock1, *listenSock2;
     46  PRFileDesc* badFD;
     47  PRUint16 listenPort1, listenPort2;
     48  PRNetAddr addr;
     49  PR_fd_set readFdSet;
     50  char buf[128];
     51  PRInt32 retVal;
     52 
     53  /* The command line argument: -d is used to determine if the test is being run
     54  in debug mode. The regress tool requires only one line output:PASS or FAIL.
     55  All of the printfs associated with this test has been handled with a if
     56  (debug_mode) test. Usage: test_name -d
     57  */
     58  PLOptStatus os;
     59  PLOptState* opt = PL_CreateOptState(argc, argv, "d:");
     60  while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
     61    if (PL_OPT_BAD == os) {
     62      continue;
     63    }
     64    switch (opt->option) {
     65      case 'd': /* debug mode */
     66        debug_mode = 1;
     67        break;
     68      default:
     69        break;
     70    }
     71  }
     72  PL_DestroyOptState(opt);
     73 
     74  /* main test */
     75 
     76  PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
     77 
     78  if (debug_mode) {
     79    printf("This program tests PR_Select with sockets.  Error\n");
     80    printf("reporting operations are tested.\n\n");
     81  }
     82 
     83  /* Create two listening sockets */
     84  if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
     85    fprintf(stderr, "Can't create a new TCP socket\n");
     86    failed_already = 1;
     87    goto exit_now;
     88  }
     89  addr.inet.family = AF_INET;
     90  addr.inet.ip = PR_htonl(INADDR_ANY);
     91  addr.inet.port = PR_htons(0);
     92  if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
     93    fprintf(stderr, "Can't bind socket\n");
     94    failed_already = 1;
     95    goto exit_now;
     96  }
     97  if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
     98    fprintf(stderr, "PR_GetSockName failed\n");
     99    failed_already = 1;
    100    goto exit_now;
    101  }
    102  listenPort1 = PR_ntohs(addr.inet.port);
    103  if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
    104    fprintf(stderr, "Can't listen on a socket\n");
    105    failed_already = 1;
    106    goto exit_now;
    107  }
    108 
    109  if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
    110    fprintf(stderr, "Can't create a new TCP socket\n");
    111    failed_already = 1;
    112    goto exit_now;
    113  }
    114  addr.inet.family = AF_INET;
    115  addr.inet.ip = PR_htonl(INADDR_ANY);
    116  addr.inet.port = PR_htons(0);
    117  if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
    118    fprintf(stderr, "Can't bind socket\n");
    119    failed_already = 1;
    120    goto exit_now;
    121  }
    122  if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
    123    fprintf(stderr, "PR_GetSockName failed\n");
    124    failed_already = 1;
    125    goto exit_now;
    126  }
    127  listenPort2 = PR_ntohs(addr.inet.port);
    128  if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
    129    fprintf(stderr, "Can't listen on a socket\n");
    130    failed_already = 1;
    131    goto exit_now;
    132  }
    133  PR_snprintf(buf, sizeof(buf),
    134              "The server thread is listening on ports %hu and %hu\n\n",
    135              listenPort1, listenPort2);
    136  if (debug_mode) {
    137    printf("%s", buf);
    138  }
    139 
    140  /* Set up the fd set */
    141  PR_FD_ZERO(&readFdSet);
    142  PR_FD_SET(listenSock1, &readFdSet);
    143  PR_FD_SET(listenSock2, &readFdSet);
    144 
    145  /* Testing bad fd */
    146  if (debug_mode) {
    147    printf("PR_Select should detect a bad file descriptor\n");
    148  }
    149  if ((badFD = PR_NewTCPSocket()) == NULL) {
    150    fprintf(stderr, "Can't create a TCP socket\n");
    151    failed_already = 1;
    152    goto exit_now;
    153  }
    154 
    155  PR_FD_SET(badFD, &readFdSet);
    156  /*
    157   * Make the fd invalid
    158   */
    159 #if defined(XP_UNIX)
    160  close(PR_FileDesc2NativeHandle(badFD));
    161 #elif defined(WIN32)
    162  closesocket(PR_FileDesc2NativeHandle(badFD));
    163 #else
    164 #  error "Unknown architecture"
    165 #endif
    166 
    167  retVal =
    168      PR_Select(0 /* unused */, &readFdSet, NULL, NULL, PR_INTERVAL_NO_TIMEOUT);
    169  if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) {
    170    fprintf(stderr,
    171            "Failed to detect the bad fd: "
    172            "PR_Select returns %d\n",
    173            retVal);
    174    if (retVal == -1) {
    175      fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(), PR_GetOSError());
    176      failed_already = 1;
    177    }
    178    goto exit_now;
    179  }
    180  if (debug_mode) {
    181    printf("PR_Select detected a bad fd.  Test passed.\n\n");
    182  }
    183  PR_FD_CLR(badFD, &readFdSet);
    184 
    185  PR_Cleanup();
    186  goto exit_now;
    187 exit_now:
    188  if (failed_already) {
    189    return 1;
    190  } else {
    191    return 0;
    192  }
    193 }