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