tor-browser

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

selct_to.c (5247B)


      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_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
     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 "prinit.h"
     34 #include "prio.h"
     35 #include "prlog.h"
     36 #include "prprf.h"
     37 #include "prnetdb.h"
     38 
     39 #include "obsolete/probslet.h"
     40 
     41 #include "prerror.h"
     42 
     43 #include <stdio.h>
     44 #include <string.h>
     45 #include <stdlib.h>
     46 
     47 PRIntn failed_already = 0;
     48 PRIntn debug_mode;
     49 
     50 int main(int argc, char** argv) {
     51  PRFileDesc *listenSock1, *listenSock2;
     52  PRUint16 listenPort1, listenPort2;
     53  PRNetAddr addr;
     54  PR_fd_set readFdSet;
     55  char buf[128];
     56  PRInt32 retVal;
     57 
     58  /* The command line argument: -d is used to determine if the test is being run
     59  in debug mode. The regress tool requires only one line output:PASS or FAIL.
     60  All of the printfs associated with this test has been handled with a if
     61  (debug_mode) test. Usage: test_name -d
     62  */
     63  PLOptStatus os;
     64  PLOptState* opt = PL_CreateOptState(argc, argv, "d:");
     65  while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
     66    if (PL_OPT_BAD == os) {
     67      continue;
     68    }
     69    switch (opt->option) {
     70      case 'd': /* debug mode */
     71        debug_mode = 1;
     72        break;
     73      default:
     74        break;
     75    }
     76  }
     77  PL_DestroyOptState(opt);
     78 
     79  /* main test */
     80 
     81  PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
     82 
     83  if (debug_mode) {
     84    printf("This program tests PR_Select with sockets.  Timeout \n");
     85    printf("operations are tested.\n\n");
     86  }
     87 
     88  /* Create two listening sockets */
     89  if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
     90    fprintf(stderr, "Can't create a new TCP socket\n");
     91    failed_already = 1;
     92    goto exit_now;
     93  }
     94  addr.inet.family = PR_AF_INET;
     95  addr.inet.ip = PR_htonl(PR_INADDR_ANY);
     96  addr.inet.port = PR_htons(0);
     97  if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
     98    fprintf(stderr, "Can't bind socket\n");
     99    failed_already = 1;
    100    goto exit_now;
    101  }
    102  if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
    103    fprintf(stderr, "PR_GetSockName failed\n");
    104    failed_already = 1;
    105    goto exit_now;
    106  }
    107  listenPort1 = PR_ntohs(addr.inet.port);
    108  if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
    109    fprintf(stderr, "Can't listen on a socket\n");
    110    failed_already = 1;
    111    goto exit_now;
    112  }
    113 
    114  if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
    115    fprintf(stderr, "Can't create a new TCP socket\n");
    116    failed_already = 1;
    117    goto exit_now;
    118  }
    119  addr.inet.family = PR_AF_INET;
    120  addr.inet.ip = PR_htonl(PR_INADDR_ANY);
    121  addr.inet.port = PR_htons(0);
    122  if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
    123    fprintf(stderr, "Can't bind socket\n");
    124    failed_already = 1;
    125    goto exit_now;
    126  }
    127  if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
    128    fprintf(stderr, "PR_GetSockName failed\n");
    129    failed_already = 1;
    130    goto exit_now;
    131  }
    132  listenPort2 = PR_ntohs(addr.inet.port);
    133  if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
    134    fprintf(stderr, "Can't listen on a socket\n");
    135    failed_already = 1;
    136    goto exit_now;
    137  }
    138  PR_snprintf(buf, sizeof(buf),
    139              "The server thread is listening on ports %hu and %hu\n\n",
    140              listenPort1, listenPort2);
    141  if (debug_mode) {
    142    printf("%s", buf);
    143  }
    144 
    145  /* Set up the fd set */
    146  PR_FD_ZERO(&readFdSet);
    147  PR_FD_SET(listenSock1, &readFdSet);
    148  PR_FD_SET(listenSock2, &readFdSet);
    149 
    150  /* Testing timeout */
    151  if (debug_mode) {
    152    printf("PR_Select should time out in 5 seconds\n");
    153  }
    154  retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
    155                     PR_SecondsToInterval(5));
    156  if (retVal != 0) {
    157    PR_snprintf(buf, sizeof(buf),
    158                "PR_Select should time out and return 0, but it returns %ld\n",
    159                retVal);
    160    fprintf(stderr, "%s", buf);
    161    if (retVal == -1) {
    162      fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(), PR_GetOSError());
    163      failed_already = 1;
    164    }
    165    goto exit_now;
    166  }
    167  if (debug_mode) {
    168    printf("PR_Select timed out.  Test passed.\n\n");
    169  }
    170 
    171  PR_Cleanup();
    172 
    173 exit_now:
    174  if (failed_already) {
    175    return 1;
    176  } else {
    177    return 0;
    178  }
    179 }