Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
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_err.c |
michael@0 | 9 | ** |
michael@0 | 10 | ** Description: This program tests PR_Poll with sockets. |
michael@0 | 11 | ** error reporting operation is tested |
michael@0 | 12 | ** |
michael@0 | 13 | ** Modification History: |
michael@0 | 14 | ** 19-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 | |
michael@0 | 40 | #include <stdio.h> |
michael@0 | 41 | #include <string.h> |
michael@0 | 42 | #include <stdlib.h> |
michael@0 | 43 | |
michael@0 | 44 | PRIntn failed_already=0; |
michael@0 | 45 | PRIntn debug_mode; |
michael@0 | 46 | |
michael@0 | 47 | static void |
michael@0 | 48 | ClientThreadFunc(void *arg) |
michael@0 | 49 | { |
michael@0 | 50 | PRFileDesc *badFD = (PRFileDesc *) arg; |
michael@0 | 51 | /* |
michael@0 | 52 | * Make the fd invalid |
michael@0 | 53 | */ |
michael@0 | 54 | #if defined(XP_UNIX) |
michael@0 | 55 | close(PR_FileDesc2NativeHandle(badFD)); |
michael@0 | 56 | #elif defined(XP_OS2) |
michael@0 | 57 | soclose(PR_FileDesc2NativeHandle(badFD)); |
michael@0 | 58 | #elif defined(WIN32) || defined(WIN16) |
michael@0 | 59 | closesocket(PR_FileDesc2NativeHandle(badFD)); |
michael@0 | 60 | #else |
michael@0 | 61 | #error "Unknown architecture" |
michael@0 | 62 | #endif |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | int main(int argc, char **argv) |
michael@0 | 66 | { |
michael@0 | 67 | PRFileDesc *listenSock1, *listenSock2; |
michael@0 | 68 | PRFileDesc *badFD; |
michael@0 | 69 | PRUint16 listenPort1, listenPort2; |
michael@0 | 70 | PRNetAddr addr; |
michael@0 | 71 | char buf[128]; |
michael@0 | 72 | PRPollDesc pds0[10], pds1[10], *pds, *other_pds; |
michael@0 | 73 | PRIntn npds; |
michael@0 | 74 | PRInt32 retVal; |
michael@0 | 75 | |
michael@0 | 76 | /* The command line argument: -d is used to determine if the test is being run |
michael@0 | 77 | in debug mode. The regress tool requires only one line output:PASS or FAIL. |
michael@0 | 78 | All of the printfs associated with this test has been handled with a if (debug_mode) |
michael@0 | 79 | test. |
michael@0 | 80 | Usage: test_name -d |
michael@0 | 81 | */ |
michael@0 | 82 | PLOptStatus os; |
michael@0 | 83 | PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); |
michael@0 | 84 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
michael@0 | 85 | { |
michael@0 | 86 | if (PL_OPT_BAD == os) continue; |
michael@0 | 87 | switch (opt->option) |
michael@0 | 88 | { |
michael@0 | 89 | case 'd': /* debug mode */ |
michael@0 | 90 | debug_mode = 1; |
michael@0 | 91 | break; |
michael@0 | 92 | default: |
michael@0 | 93 | break; |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | PL_DestroyOptState(opt); |
michael@0 | 97 | |
michael@0 | 98 | /* main test */ |
michael@0 | 99 | |
michael@0 | 100 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
michael@0 | 101 | PR_STDIO_INIT(); |
michael@0 | 102 | |
michael@0 | 103 | if (debug_mode) { |
michael@0 | 104 | printf("This program tests PR_Poll with sockets.\n"); |
michael@0 | 105 | printf("error reporting is tested.\n\n"); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | /* Create two listening sockets */ |
michael@0 | 109 | if ((listenSock1 = PR_NewTCPSocket()) == NULL) { |
michael@0 | 110 | fprintf(stderr, "Can't create a new TCP socket\n"); |
michael@0 | 111 | failed_already=1; |
michael@0 | 112 | goto exit_now; |
michael@0 | 113 | } |
michael@0 | 114 | addr.inet.family = AF_INET; |
michael@0 | 115 | addr.inet.ip = PR_htonl(INADDR_ANY); |
michael@0 | 116 | addr.inet.port = PR_htons(0); |
michael@0 | 117 | if (PR_Bind(listenSock1, &addr) == PR_FAILURE) { |
michael@0 | 118 | fprintf(stderr, "Can't bind socket\n"); |
michael@0 | 119 | failed_already=1; |
michael@0 | 120 | goto exit_now; |
michael@0 | 121 | } |
michael@0 | 122 | if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) { |
michael@0 | 123 | fprintf(stderr, "PR_GetSockName failed\n"); |
michael@0 | 124 | failed_already=1; |
michael@0 | 125 | goto exit_now; |
michael@0 | 126 | } |
michael@0 | 127 | listenPort1 = PR_ntohs(addr.inet.port); |
michael@0 | 128 | if (PR_Listen(listenSock1, 5) == PR_FAILURE) { |
michael@0 | 129 | fprintf(stderr, "Can't listen on a socket\n"); |
michael@0 | 130 | failed_already=1; |
michael@0 | 131 | goto exit_now; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | if ((listenSock2 = PR_NewTCPSocket()) == NULL) { |
michael@0 | 135 | fprintf(stderr, "Can't create a new TCP socket\n"); |
michael@0 | 136 | failed_already=1; |
michael@0 | 137 | goto exit_now; |
michael@0 | 138 | } |
michael@0 | 139 | addr.inet.family = AF_INET; |
michael@0 | 140 | addr.inet.ip = PR_htonl(INADDR_ANY); |
michael@0 | 141 | addr.inet.port = PR_htons(0); |
michael@0 | 142 | if (PR_Bind(listenSock2, &addr) == PR_FAILURE) { |
michael@0 | 143 | fprintf(stderr, "Can't bind socket\n"); |
michael@0 | 144 | failed_already=1; |
michael@0 | 145 | goto exit_now; |
michael@0 | 146 | } |
michael@0 | 147 | if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) { |
michael@0 | 148 | fprintf(stderr, "PR_GetSockName failed\n"); |
michael@0 | 149 | failed_already=1; |
michael@0 | 150 | goto exit_now; |
michael@0 | 151 | } |
michael@0 | 152 | listenPort2 = PR_ntohs(addr.inet.port); |
michael@0 | 153 | if (PR_Listen(listenSock2, 5) == PR_FAILURE) { |
michael@0 | 154 | fprintf(stderr, "Can't listen on a socket\n"); |
michael@0 | 155 | failed_already=1; |
michael@0 | 156 | goto exit_now; |
michael@0 | 157 | } |
michael@0 | 158 | PR_snprintf(buf, sizeof(buf), |
michael@0 | 159 | "The server thread is listening on ports %hu and %hu\n\n", |
michael@0 | 160 | listenPort1, listenPort2); |
michael@0 | 161 | if (debug_mode) printf("%s", buf); |
michael@0 | 162 | |
michael@0 | 163 | /* Set up the poll descriptor array */ |
michael@0 | 164 | pds = pds0; |
michael@0 | 165 | other_pds = pds1; |
michael@0 | 166 | memset(pds, 0, sizeof(pds)); |
michael@0 | 167 | pds[0].fd = listenSock1; |
michael@0 | 168 | pds[0].in_flags = PR_POLL_READ; |
michael@0 | 169 | pds[1].fd = listenSock2; |
michael@0 | 170 | pds[1].in_flags = PR_POLL_READ; |
michael@0 | 171 | npds = 2; |
michael@0 | 172 | |
michael@0 | 173 | |
michael@0 | 174 | /* Testing bad fd */ |
michael@0 | 175 | if (debug_mode) printf("PR_Poll should detect a bad file descriptor\n"); |
michael@0 | 176 | if ((badFD = PR_NewTCPSocket()) == NULL) { |
michael@0 | 177 | fprintf(stderr, "Can't create a TCP socket\n"); |
michael@0 | 178 | goto exit_now; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | pds[2].fd = badFD; |
michael@0 | 182 | pds[2].in_flags = PR_POLL_READ; |
michael@0 | 183 | npds = 3; |
michael@0 | 184 | |
michael@0 | 185 | if (PR_CreateThread(PR_USER_THREAD, ClientThreadFunc, |
michael@0 | 186 | badFD, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, |
michael@0 | 187 | PR_UNJOINABLE_THREAD, 0) == NULL) { |
michael@0 | 188 | fprintf(stderr, "cannot create thread\n"); |
michael@0 | 189 | exit(1); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 193 | if (retVal != 1 || (unsigned short) pds[2].out_flags != PR_POLL_NVAL) { |
michael@0 | 194 | fprintf(stderr, "Failed to detect the bad fd: " |
michael@0 | 195 | "PR_Poll returns %d, out_flags is 0x%hx\n", |
michael@0 | 196 | retVal, pds[2].out_flags); |
michael@0 | 197 | failed_already=1; |
michael@0 | 198 | goto exit_now; |
michael@0 | 199 | } |
michael@0 | 200 | if (debug_mode) printf("PR_Poll detected the bad fd. Test passed.\n\n"); |
michael@0 | 201 | PR_Cleanup(); |
michael@0 | 202 | goto exit_now; |
michael@0 | 203 | exit_now: |
michael@0 | 204 | if(failed_already) |
michael@0 | 205 | return 1; |
michael@0 | 206 | else |
michael@0 | 207 | return 0; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | #endif /* XP_BEOS */ |