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