tor-browser

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

poll_to.c (5609B)


      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 **
      8 ** Name: prpoll_to.c
      9 **
     10 ** Description: This program tests PR_Poll with sockets.
     11 **              Timeout operation is tested
     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 "private/pprio.h"
     40 
     41 #include <stdio.h>
     42 #include <string.h>
     43 #include <stdlib.h>
     44 
     45 PRIntn failed_already = 0;
     46 PRIntn debug_mode;
     47 
     48 int main(int argc, char** argv) {
     49  PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
     50  PRUint16 listenPort1, listenPort2;
     51  PRNetAddr addr;
     52  char buf[128];
     53  PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
     54  PRIntn npds;
     55  PRInt32 retVal;
     56 
     57  /* The command line argument: -d is used to determine if the test is being run
     58  in debug mode. The regress tool requires only one line output:PASS or FAIL.
     59  All of the printfs associated with this test has been handled with a if
     60  (debug_mode) test. Usage: test_name -d
     61  */
     62  PLOptStatus os;
     63  PLOptState* opt = PL_CreateOptState(argc, argv, "d:");
     64  while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
     65    if (PL_OPT_BAD == os) {
     66      continue;
     67    }
     68    switch (opt->option) {
     69      case 'd': /* debug mode */
     70        debug_mode = 1;
     71        break;
     72      default:
     73        break;
     74    }
     75  }
     76  PL_DestroyOptState(opt);
     77 
     78  /* main test */
     79 
     80  PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
     81 
     82  if (debug_mode) {
     83    printf("This program tests PR_Poll with sockets.\n");
     84    printf("Timeout is tested.\n\n");
     85  }
     86 
     87  /* Create two listening sockets */
     88  if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
     89    fprintf(stderr, "Can't create a new TCP socket\n");
     90    if (!debug_mode) {
     91      failed_already = 1;
     92    }
     93    goto exit_now;
     94  }
     95  memset(&addr, 0, sizeof(addr));
     96  addr.inet.family = PR_AF_INET;
     97  addr.inet.ip = PR_htonl(PR_INADDR_ANY);
     98  addr.inet.port = PR_htons(0);
     99  if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
    100    fprintf(stderr, "Can't bind socket\n");
    101    if (!debug_mode) {
    102      failed_already = 1;
    103    }
    104    goto exit_now;
    105  }
    106  if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
    107    fprintf(stderr, "PR_GetSockName failed\n");
    108    if (!debug_mode) {
    109      failed_already = 1;
    110    }
    111    goto exit_now;
    112  }
    113  listenPort1 = PR_ntohs(addr.inet.port);
    114  if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
    115    fprintf(stderr, "Can't listen on a socket\n");
    116    if (!debug_mode) {
    117      failed_already = 1;
    118    }
    119    goto exit_now;
    120  }
    121 
    122  if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
    123    fprintf(stderr, "Can't create a new TCP socket\n");
    124    if (!debug_mode) {
    125      failed_already = 1;
    126    }
    127    goto exit_now;
    128  }
    129  addr.inet.family = PR_AF_INET;
    130  addr.inet.ip = PR_htonl(PR_INADDR_ANY);
    131  addr.inet.port = PR_htons(0);
    132  if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
    133    fprintf(stderr, "Can't bind socket\n");
    134    if (!debug_mode) {
    135      failed_already = 1;
    136    }
    137    goto exit_now;
    138  }
    139  if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
    140    fprintf(stderr, "PR_GetSockName failed\n");
    141    if (!debug_mode) {
    142      failed_already = 1;
    143    }
    144    goto exit_now;
    145  }
    146  listenPort2 = PR_ntohs(addr.inet.port);
    147  if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
    148    fprintf(stderr, "Can't listen on a socket\n");
    149    if (!debug_mode) {
    150      failed_already = 1;
    151    }
    152    goto exit_now;
    153  }
    154  PR_snprintf(buf, sizeof(buf),
    155              "The server thread is listening on ports %hu and %hu\n\n",
    156              listenPort1, listenPort2);
    157  if (debug_mode) {
    158    printf("%s", buf);
    159  }
    160 
    161  /* Set up the poll descriptor array */
    162  pds = pds0;
    163  other_pds = pds1;
    164  memset(pds, 0, sizeof(pds));
    165  pds[0].fd = listenSock1;
    166  pds[0].in_flags = PR_POLL_READ;
    167  pds[1].fd = listenSock2;
    168  pds[1].in_flags = PR_POLL_READ;
    169  npds = 2;
    170 
    171  /* Testing timeout */
    172  if (debug_mode) {
    173    printf("PR_Poll should time out in 5 seconds\n");
    174  }
    175  retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5));
    176  if (retVal != 0) {
    177    PR_snprintf(buf, sizeof(buf),
    178                "PR_Poll should time out and return 0, but it returns %ld\n",
    179                retVal);
    180    fprintf(stderr, "%s", buf);
    181    if (!debug_mode) {
    182      failed_already = 1;
    183    }
    184    goto exit_now;
    185  }
    186  if (debug_mode) {
    187    printf("PR_Poll timed out.  Test passed.\n\n");
    188  }
    189 
    190 exit_now:
    191 
    192  if (listenSock1) {
    193    PR_Close(listenSock1);
    194  }
    195  if (listenSock2) {
    196    PR_Close(listenSock2);
    197  }
    198 
    199  PR_Cleanup();
    200 
    201  if (failed_already) {
    202    return 1;
    203  } else {
    204    return 0;
    205  }
    206 }