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