| 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 |
** 1997 - Netscape Communications Corporation |
| 8 |
** |
| 9 |
** Name: prselect_err.c |
| 10 |
** |
| 11 |
** Description: tests PR_Select with sockets Error condition functions. |
| 12 |
** |
| 13 |
** Modification History: |
| 14 |
** 14-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 |
***********************************************************************/ |
| 23 |
|
| 24 |
/*********************************************************************** |
| 25 |
** Includes |
| 26 |
***********************************************************************/ |
| 27 |
/* Used to get the command line option */ |
| 28 |
#include "plgetopt.h" |
| 29 |
#include "prttools.h" |
| 30 |
|
| 31 |
#include "prinit.h" |
| 32 |
#include "prio.h" |
| 33 |
#include "prlog.h" |
| 34 |
#include "prprf.h" |
| 35 |
#include "prerror.h" |
| 36 |
#include "prnetdb.h" |
| 37 |
|
| 38 |
#include <stdio.h> |
| 39 |
#include <string.h> |
| 40 |
#include <stdlib.h> |
| 41 |
|
| 42 |
/*********************************************************************** |
| 43 |
** PRIVATE FUNCTION: Test_Result |
| 44 |
** DESCRIPTION: Used in conjunction with the regress tool, prints out the |
| 45 |
** status of the test case. |
| 46 |
** INPUTS: PASS/FAIL |
| 47 |
** OUTPUTS: None |
| 48 |
** RETURN: None |
| 49 |
** SIDE EFFECTS: |
| 50 |
** |
| 51 |
** RESTRICTIONS: |
| 52 |
** None |
| 53 |
** MEMORY: NA |
| 54 |
** ALGORITHM: Determine what the status is and print accordingly. |
| 55 |
** |
| 56 |
***********************************************************************/ |
| 57 |
|
| 58 |
static Test_Result(int result) { |
| 59 |
if (result == PASS) { |
| 60 |
printf("PASS\n"); |
| 61 |
} else { |
| 62 |
printf("FAIL\n"); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static void clientThreadFunc(void* arg) { |
| 67 |
PRUint16 port = (PRUint16)arg; |
| 68 |
PRFileDesc* sock; |
| 69 |
PRNetAddr addr; |
| 70 |
char buf[128]; |
| 71 |
int i; |
| 72 |
|
| 73 |
addr.inet.family = AF_INET; |
| 74 |
addr.inet.port = PR_htons(port); |
| 75 |
addr.inet.ip = PR_htonl(INADDR_LOOPBACK); |
| 76 |
PR_snprintf(buf, sizeof(buf), "%hu", port); |
| 77 |
|
| 78 |
for (i = 0; i < 5; i++) { |
| 79 |
sock = PR_NewTCPSocket(); |
| 80 |
PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT); |
| 81 |
PR_Write(sock, buf, sizeof(buf)); |
| 82 |
PR_Close(sock); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
int main(int argc, char** argv) { |
| 87 |
PRFileDesc *listenSock1, *listenSock2; |
| 88 |
PRFileDesc* badFD; |
| 89 |
PRFileDesc *fds0[10], *fds1[10], **fds, **other_fds; |
| 90 |
PRIntn nfds; |
| 91 |
PRUint16 listenPort1, listenPort2; |
| 92 |
PRNetAddr addr; |
| 93 |
PR_fd_set readFdSet; |
| 94 |
char buf[128]; |
| 95 |
PRThread* clientThread; |
| 96 |
PRInt32 retVal; |
| 97 |
PRIntn i, j; |
| 98 |
|
| 99 |
/* The command line argument: -d is used to determine if the test is being run |
| 100 |
in debug mode. The regress tool requires only one line output:PASS or FAIL. |
| 101 |
All of the printfs associated with this test has been handled with a if |
| 102 |
(debug_mode) test. Usage: test_name -d |
| 103 |
*/ |
| 104 |
PLOptStatus os; |
| 105 |
PLOptState* opt = PL_CreateOptState(argc, argv, "d:"); |
| 106 |
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
| 107 |
if (PL_OPT_BAD == os) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
switch (opt->option) { |
| 111 |
case 'd': /* debug mode */ |
| 112 |
debug_mode = 1; |
| 113 |
break; |
| 114 |
default: |
| 115 |
break; |
| 116 |
} |
| 117 |
} |
| 118 |
PL_DestroyOptState(opt); |
| 119 |
|
| 120 |
/* main test */ |
| 121 |
|
| 122 |
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
| 123 |
PR_STDIO_INIT(); |
| 124 |
|
| 125 |
if (debug_mode) { |
| 126 |
printf("This program tests PR_Select with sockets. Timeout, error\n"); |
| 127 |
printf("reporting, and normal operation are tested.\n\n"); |
| 128 |
} |
| 129 |
|
| 130 |
/* Create two listening sockets */ |
| 131 |
if ((listenSock1 = PR_NewTCPSocket()) == NULL) { |
| 132 |
fprintf(stderr, "Can't create a new TCP socket\n"); |
| 133 |
if (!debug_mode) { |
| 134 |
Test_Result(FAIL); |
| 135 |
} |
| 136 |
exit(1); |
| 137 |
} |
| 138 |
addr.inet.family = AF_INET; |
| 139 |
addr.inet.ip = PR_htonl(INADDR_ANY); |
| 140 |
addr.inet.port = PR_htons(0); |
| 141 |
if (PR_Bind(listenSock1, &addr) == PR_FAILURE) { |
| 142 |
fprintf(stderr, "Can't bind socket\n"); |
| 143 |
if (!debug_mode) { |
| 144 |
Test_Result(FAIL); |
| 145 |
} |
| 146 |
exit(1); |
| 147 |
} |
| 148 |
if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) { |
| 149 |
fprintf(stderr, "PR_GetSockName failed\n"); |
| 150 |
if (!debug_mode) { |
| 151 |
Test_Result(FAIL); |
| 152 |
} |
| 153 |
exit(1); |
| 154 |
} |
| 155 |
listenPort1 = PR_ntohs(addr.inet.port); |
| 156 |
if (PR_Listen(listenSock1, 5) == PR_FAILURE) { |
| 157 |
fprintf(stderr, "Can't listen on a socket\n"); |
| 158 |
if (!debug_mode) { |
| 159 |
Test_Result(FAIL); |
| 160 |
} |
| 161 |
exit(1); |
| 162 |
} |
| 163 |
|
| 164 |
if ((listenSock2 = PR_NewTCPSocket()) == NULL) { |
| 165 |
fprintf(stderr, "Can't create a new TCP socket\n"); |
| 166 |
if (!debug_mode) { |
| 167 |
Test_Result(FAIL); |
| 168 |
} |
| 169 |
exit(1); |
| 170 |
} |
| 171 |
addr.inet.family = AF_INET; |
| 172 |
addr.inet.ip = PR_htonl(INADDR_ANY); |
| 173 |
addr.inet.port = PR_htons(0); |
| 174 |
if (PR_Bind(listenSock2, &addr) == PR_FAILURE) { |
| 175 |
fprintf(stderr, "Can't bind socket\n"); |
| 176 |
if (!debug_mode) { |
| 177 |
Test_Result(FAIL); |
| 178 |
} |
| 179 |
exit(1); |
| 180 |
} |
| 181 |
if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) { |
| 182 |
fprintf(stderr, "PR_GetSockName failed\n"); |
| 183 |
if (!debug_mode) { |
| 184 |
Test_Result(FAIL); |
| 185 |
} |
| 186 |
exit(1); |
| 187 |
} |
| 188 |
listenPort2 = PR_ntohs(addr.inet.port); |
| 189 |
if (PR_Listen(listenSock2, 5) == PR_FAILURE) { |
| 190 |
fprintf(stderr, "Can't listen on a socket\n"); |
| 191 |
if (!debug_mode) { |
| 192 |
Test_Result(FAIL); |
| 193 |
} |
| 194 |
exit(1); |
| 195 |
} |
| 196 |
PR_snprintf(buf, sizeof(buf), |
| 197 |
"The server thread is listening on ports %hu and %hu\n\n", |
| 198 |
listenPort1, listenPort2); |
| 199 |
printf("%s", buf); |
| 200 |
|
| 201 |
/* Set up the fd set */ |
| 202 |
PR_FD_ZERO(&readFdSet); |
| 203 |
PR_FD_SET(listenSock1, &readFdSet); |
| 204 |
PR_FD_SET(listenSock2, &readFdSet); |
| 205 |
|
| 206 |
/* Testing timeout */ |
| 207 |
if (debug_mode) { |
| 208 |
printf("PR_Select should time out in 5 seconds\n"); |
| 209 |
} |
| 210 |
retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL, |
| 211 |
PR_SecondsToInterval(5)); |
| 212 |
if (retVal != 0) { |
| 213 |
PR_snprintf(buf, sizeof(buf), |
| 214 |
"PR_Select should time out and return 0, but it returns %ld\n", |
| 215 |
retVal); |
| 216 |
fprintf(stderr, "%s", buf); |
| 217 |
if (retVal == -1) { |
| 218 |
fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(), PR_GetOSError()); |
| 219 |
if (!debug_mode) { |
| 220 |
Test_Result(FAIL); |
| 221 |
} |
| 222 |
} |
| 223 |
exit(1); |
| 224 |
} |
| 225 |
if (debug_mode) { |
| 226 |
printf("PR_Select timed out. Test passed.\n\n"); |
| 227 |
} else { |
| 228 |
Test_Result(PASS); |
| 229 |
} |
| 230 |
|
| 231 |
/* Testing bad fd */ |
| 232 |
printf("PR_Select should detect a bad file descriptor\n"); |
| 233 |
if ((badFD = PR_NewTCPSocket()) == NULL) { |
| 234 |
fprintf(stderr, "Can't create a TCP socket\n"); |
| 235 |
exit(1); |
| 236 |
} |
| 237 |
|
| 238 |
PR_FD_SET(listenSock1, &readFdSet); |
| 239 |
PR_FD_SET(listenSock2, &readFdSet); |
| 240 |
PR_FD_SET(badFD, &readFdSet); |
| 241 |
PR_Close(badFD); /* make the fd bad */ |
| 242 |
retVal = |
| 243 |
PR_Select(0 /* unused */, &readFdSet, NULL, NULL, PR_INTERVAL_NO_TIMEOUT); |
| 244 |
if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) { |
| 245 |
fprintf(stderr, |
| 246 |
"Failed to detect the bad fd: " |
| 247 |
"PR_Select returns %d\n", |
| 248 |
retVal); |
| 249 |
if (retVal == -1) { |
| 250 |
fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(), PR_GetOSError()); |
| 251 |
} |
| 252 |
exit(1); |
| 253 |
} |
| 254 |
printf("PR_Select detected a bad fd. Test passed.\n\n"); |
| 255 |
PR_FD_CLR(badFD, &readFdSet); |
| 256 |
|
| 257 |
clientThread = PR_CreateThread(PR_USER_THREAD, clientThreadFunc, |
| 258 |
(void*)listenPort1, PR_PRIORITY_NORMAL, |
| 259 |
PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); |
| 260 |
if (clientThread == NULL) { |
| 261 |
fprintf(stderr, "can't create thread\n"); |
| 262 |
exit(1); |
| 263 |
} |
| 264 |
|
| 265 |
clientThread = PR_CreateThread(PR_USER_THREAD, clientThreadFunc, |
| 266 |
(void*)listenPort2, PR_PRIORITY_NORMAL, |
| 267 |
PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); |
| 268 |
if (clientThread == NULL) { |
| 269 |
fprintf(stderr, "can't create thread\n"); |
| 270 |
exit(1); |
| 271 |
} |
| 272 |
|
| 273 |
printf("Two client threads are created. Each of them will\n"); |
| 274 |
printf("send data to one of the two ports the server is listening on.\n"); |
| 275 |
printf("The data they send is the port number. Each of them send\n"); |
| 276 |
printf("the data five times, so you should see ten lines below,\n"); |
| 277 |
printf("interleaved in an arbitrary order.\n"); |
| 278 |
|
| 279 |
/* set up the fd array */ |
| 280 |
fds = fds0; |
| 281 |
other_fds = fds1; |
| 282 |
fds[0] = listenSock1; |
| 283 |
fds[1] = listenSock2; |
| 284 |
nfds = 2; |
| 285 |
PR_FD_SET(listenSock1, &readFdSet); |
| 286 |
PR_FD_SET(listenSock2, &readFdSet); |
| 287 |
|
| 288 |
/* 20 events total */ |
| 289 |
i = 0; |
| 290 |
while (i < 20) { |
| 291 |
PRFileDesc** tmp; |
| 292 |
int nextIndex; |
| 293 |
int nEvents = 0; |
| 294 |
|
| 295 |
retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL, |
| 296 |
PR_INTERVAL_NO_TIMEOUT); |
| 297 |
PR_ASSERT(retVal != 0); /* no timeout */ |
| 298 |
if (retVal == -1) { |
| 299 |
fprintf(stderr, "PR_Select failed (%d, %d)\n", PR_GetError(), |
| 300 |
PR_GetOSError()); |
| 301 |
exit(1); |
| 302 |
} |
| 303 |
|
| 304 |
nextIndex = 2; |
| 305 |
/* the two listening sockets */ |
| 306 |
for (j = 0; j < 2; j++) { |
| 307 |
other_fds[j] = fds[j]; |
| 308 |
if (PR_FD_ISSET(fds[j], &readFdSet)) { |
| 309 |
PRFileDesc* sock; |
| 310 |
|
| 311 |
nEvents++; |
| 312 |
sock = PR_Accept(fds[j], NULL, PR_INTERVAL_NO_TIMEOUT); |
| 313 |
if (sock == NULL) { |
| 314 |
fprintf(stderr, "PR_Accept() failed\n"); |
| 315 |
exit(1); |
| 316 |
} |
| 317 |
other_fds[nextIndex] = sock; |
| 318 |
PR_FD_SET(sock, &readFdSet); |
| 319 |
nextIndex++; |
| 320 |
} |
| 321 |
PR_FD_SET(fds[j], &readFdSet); |
| 322 |
} |
| 323 |
|
| 324 |
for (j = 2; j < nfds; j++) { |
| 325 |
if (PR_FD_ISSET(fds[j], &readFdSet)) { |
| 326 |
PRInt32 nBytes; |
| 327 |
|
| 328 |
PR_FD_CLR(fds[j], &readFdSet); |
| 329 |
nEvents++; |
| 330 |
nBytes = PR_Read(fds[j], buf, sizeof(buf)); |
| 331 |
if (nBytes == -1) { |
| 332 |
fprintf(stderr, "PR_Read() failed\n"); |
| 333 |
exit(1); |
| 334 |
} |
| 335 |
/* Just to be safe */ |
| 336 |
buf[127] = '\0'; |
| 337 |
PR_Close(fds[j]); |
| 338 |
printf("The server received \"%s\" from a client\n", buf); |
| 339 |
} else { |
| 340 |
PR_FD_SET(fds[j], &readFdSet); |
| 341 |
other_fds[nextIndex] = fds[j]; |
| 342 |
nextIndex++; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
PR_ASSERT(retVal == nEvents); |
| 347 |
/* swap */ |
| 348 |
tmp = fds; |
| 349 |
fds = other_fds; |
| 350 |
other_fds = tmp; |
| 351 |
nfds = nextIndex; |
| 352 |
i += nEvents; |
| 353 |
} |
| 354 |
|
| 355 |
printf("All tests finished\n"); |
| 356 |
PR_Cleanup(); |
| 357 |
return 0; |
| 358 |
} |