Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | ** |
michael@0 | 8 | ** Name: prpoll_to.c |
michael@0 | 9 | ** |
michael@0 | 10 | ** Description: This program tests PR_Poll with sockets. |
michael@0 | 11 | ** Timeout operation is tested |
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 "private/pprio.h" |
michael@0 | 36 | |
michael@0 | 37 | #include <stdio.h> |
michael@0 | 38 | #include <string.h> |
michael@0 | 39 | #include <stdlib.h> |
michael@0 | 40 | |
michael@0 | 41 | PRIntn failed_already=0; |
michael@0 | 42 | PRIntn debug_mode; |
michael@0 | 43 | |
michael@0 | 44 | int main(int argc, char **argv) |
michael@0 | 45 | { |
michael@0 | 46 | PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL; |
michael@0 | 47 | PRUint16 listenPort1, listenPort2; |
michael@0 | 48 | PRNetAddr addr; |
michael@0 | 49 | char buf[128]; |
michael@0 | 50 | PRPollDesc pds0[10], pds1[10], *pds, *other_pds; |
michael@0 | 51 | PRIntn npds; |
michael@0 | 52 | PRInt32 retVal; |
michael@0 | 53 | |
michael@0 | 54 | /* The command line argument: -d is used to determine if the test is being run |
michael@0 | 55 | in debug mode. The regress tool requires only one line output:PASS or FAIL. |
michael@0 | 56 | All of the printfs associated with this test has been handled with a if (debug_mode) |
michael@0 | 57 | test. |
michael@0 | 58 | Usage: test_name -d |
michael@0 | 59 | */ |
michael@0 | 60 | PLOptStatus os; |
michael@0 | 61 | PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); |
michael@0 | 62 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
michael@0 | 63 | { |
michael@0 | 64 | if (PL_OPT_BAD == os) continue; |
michael@0 | 65 | switch (opt->option) |
michael@0 | 66 | { |
michael@0 | 67 | case 'd': /* debug mode */ |
michael@0 | 68 | debug_mode = 1; |
michael@0 | 69 | break; |
michael@0 | 70 | default: |
michael@0 | 71 | break; |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | PL_DestroyOptState(opt); |
michael@0 | 75 | |
michael@0 | 76 | /* main test */ |
michael@0 | 77 | |
michael@0 | 78 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
michael@0 | 79 | PR_STDIO_INIT(); |
michael@0 | 80 | |
michael@0 | 81 | if (debug_mode) { |
michael@0 | 82 | printf("This program tests PR_Poll with sockets.\n"); |
michael@0 | 83 | printf("Timeout is tested.\n\n"); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | /* Create two listening sockets */ |
michael@0 | 87 | if ((listenSock1 = PR_NewTCPSocket()) == NULL) { |
michael@0 | 88 | fprintf(stderr, "Can't create a new TCP socket\n"); |
michael@0 | 89 | if (!debug_mode) failed_already=1; |
michael@0 | 90 | goto exit_now; |
michael@0 | 91 | } |
michael@0 | 92 | memset(&addr, 0, sizeof(addr)); |
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 | if (!debug_mode) 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 | if (!debug_mode) 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 | if (!debug_mode) 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 | if (!debug_mode) 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 | if (!debug_mode) 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 | if (!debug_mode) 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 | if (!debug_mode) 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 poll descriptor array */ |
michael@0 | 143 | pds = pds0; |
michael@0 | 144 | other_pds = pds1; |
michael@0 | 145 | memset(pds, 0, sizeof(pds)); |
michael@0 | 146 | pds[0].fd = listenSock1; |
michael@0 | 147 | pds[0].in_flags = PR_POLL_READ; |
michael@0 | 148 | pds[1].fd = listenSock2; |
michael@0 | 149 | pds[1].in_flags = PR_POLL_READ; |
michael@0 | 150 | npds = 2; |
michael@0 | 151 | |
michael@0 | 152 | /* Testing timeout */ |
michael@0 | 153 | if (debug_mode) printf("PR_Poll should time out in 5 seconds\n"); |
michael@0 | 154 | retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5)); |
michael@0 | 155 | if (retVal != 0) { |
michael@0 | 156 | PR_snprintf(buf, sizeof(buf), |
michael@0 | 157 | "PR_Poll should time out and return 0, but it returns %ld\n", |
michael@0 | 158 | retVal); |
michael@0 | 159 | fprintf(stderr, "%s", buf); |
michael@0 | 160 | if (!debug_mode) failed_already=1; |
michael@0 | 161 | goto exit_now; |
michael@0 | 162 | } |
michael@0 | 163 | if (debug_mode) printf("PR_Poll timed out. Test passed.\n\n"); |
michael@0 | 164 | |
michael@0 | 165 | exit_now: |
michael@0 | 166 | |
michael@0 | 167 | if (listenSock1) { |
michael@0 | 168 | PR_Close(listenSock1); |
michael@0 | 169 | } |
michael@0 | 170 | if (listenSock2) { |
michael@0 | 171 | PR_Close(listenSock2); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | PR_Cleanup(); |
michael@0 | 175 | |
michael@0 | 176 | if(failed_already) |
michael@0 | 177 | return 1; |
michael@0 | 178 | else |
michael@0 | 179 | return 0; |
michael@0 | 180 | |
michael@0 | 181 | } |