| 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 |
#ifdef WIN32 |
| 7 |
# include <windows.h> |
| 8 |
#endif |
| 9 |
|
| 10 |
#ifdef XP_UNIX |
| 11 |
# include <unistd.h> /* for close() */ |
| 12 |
#endif |
| 13 |
|
| 14 |
#include "prinit.h" |
| 15 |
#include "prio.h" |
| 16 |
#include "prlog.h" |
| 17 |
#include "prprf.h" |
| 18 |
#include "prnetdb.h" |
| 19 |
|
| 20 |
#include "private/pprio.h" |
| 21 |
|
| 22 |
#define CLIENT_LOOPS 5 |
| 23 |
#define BUF_SIZE 128 |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <string.h> |
| 27 |
#include <stdlib.h> |
| 28 |
|
| 29 |
#ifdef WINCE |
| 30 |
|
| 31 |
int main(int argc, char** argv) { |
| 32 |
fprintf(stderr, "Invalid/Broken Test for WinCE/WinMobile\n"); |
| 33 |
exit(1); |
| 34 |
} |
| 35 |
|
| 36 |
#else |
| 37 |
|
| 38 |
static void clientThreadFunc(void* arg) { |
| 39 |
PRUint16 port = (PRUint16)arg; |
| 40 |
PRFileDesc* sock; |
| 41 |
PRNetAddr addr; |
| 42 |
char buf[BUF_SIZE]; |
| 43 |
int i; |
| 44 |
|
| 45 |
addr.inet.family = PR_AF_INET; |
| 46 |
addr.inet.port = PR_htons(port); |
| 47 |
addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); |
| 48 |
PR_snprintf(buf, sizeof(buf), "%hu", port); |
| 49 |
|
| 50 |
for (i = 0; i < 5; i++) { |
| 51 |
sock = PR_NewTCPSocket(); |
| 52 |
PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT); |
| 53 |
|
| 54 |
PR_Write(sock, buf, sizeof(buf)); |
| 55 |
PR_Close(sock); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
int main(int argc, char** argv) { |
| 60 |
PRFileDesc *listenSock1, *listenSock2; |
| 61 |
PRFileDesc* badFD; |
| 62 |
PRUint16 listenPort1, listenPort2; |
| 63 |
PRNetAddr addr; |
| 64 |
char buf[BUF_SIZE]; |
| 65 |
PRThread* clientThread; |
| 66 |
PRPollDesc pds0[10], pds1[10], *pds, *other_pds; |
| 67 |
PRIntn npds; |
| 68 |
PRInt32 retVal; |
| 69 |
PRInt32 rv; |
| 70 |
PROsfd sd; |
| 71 |
struct sockaddr_in saddr; |
| 72 |
PRIntn saddr_len; |
| 73 |
PRUint16 listenPort3; |
| 74 |
PRFileDesc* socket_poll_fd; |
| 75 |
PRIntn i, j; |
| 76 |
|
| 77 |
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
| 78 |
PR_STDIO_INIT(); |
| 79 |
|
| 80 |
printf("This program tests PR_Poll with sockets.\n"); |
| 81 |
printf("Timeout, error reporting, and normal operation are tested.\n\n"); |
| 82 |
|
| 83 |
/* Create two listening sockets */ |
| 84 |
if ((listenSock1 = PR_NewTCPSocket()) == NULL) { |
| 85 |
fprintf(stderr, "Can't create a new TCP socket\n"); |
| 86 |
exit(1); |
| 87 |
} |
| 88 |
addr.inet.family = PR_AF_INET; |
| 89 |
addr.inet.ip = PR_htonl(PR_INADDR_ANY); |
| 90 |
addr.inet.port = PR_htons(0); |
| 91 |
if (PR_Bind(listenSock1, &addr) == PR_FAILURE) { |
| 92 |
fprintf(stderr, "Can't bind socket\n"); |
| 93 |
exit(1); |
| 94 |
} |
| 95 |
if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) { |
| 96 |
fprintf(stderr, "PR_GetSockName failed\n"); |
| 97 |
exit(1); |
| 98 |
} |
| 99 |
listenPort1 = PR_ntohs(addr.inet.port); |
| 100 |
if (PR_Listen(listenSock1, 5) == PR_FAILURE) { |
| 101 |
fprintf(stderr, "Can't listen on a socket\n"); |
| 102 |
exit(1); |
| 103 |
} |
| 104 |
|
| 105 |
if ((listenSock2 = PR_NewTCPSocket()) == NULL) { |
| 106 |
fprintf(stderr, "Can't create a new TCP socket\n"); |
| 107 |
exit(1); |
| 108 |
} |
| 109 |
addr.inet.family = PR_AF_INET; |
| 110 |
addr.inet.ip = PR_htonl(PR_INADDR_ANY); |
| 111 |
addr.inet.port = PR_htons(0); |
| 112 |
if (PR_Bind(listenSock2, &addr) == PR_FAILURE) { |
| 113 |
fprintf(stderr, "Can't bind socket\n"); |
| 114 |
exit(1); |
| 115 |
} |
| 116 |
if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) { |
| 117 |
fprintf(stderr, "PR_GetSockName failed\n"); |
| 118 |
exit(1); |
| 119 |
} |
| 120 |
listenPort2 = PR_ntohs(addr.inet.port); |
| 121 |
if (PR_Listen(listenSock2, 5) == PR_FAILURE) { |
| 122 |
fprintf(stderr, "Can't listen on a socket\n"); |
| 123 |
exit(1); |
| 124 |
} |
| 125 |
/* Set up the poll descriptor array */ |
| 126 |
pds = pds0; |
| 127 |
other_pds = pds1; |
| 128 |
memset(pds, 0, sizeof(pds)); |
| 129 |
npds = 0; |
| 130 |
pds[npds].fd = listenSock1; |
| 131 |
pds[npds].in_flags = PR_POLL_READ; |
| 132 |
npds++; |
| 133 |
pds[npds].fd = listenSock2; |
| 134 |
pds[npds].in_flags = PR_POLL_READ; |
| 135 |
npds++; |
| 136 |
|
| 137 |
sd = socket(AF_INET, SOCK_STREAM, 0); |
| 138 |
PR_ASSERT(sd >= 0); |
| 139 |
memset((char*)&saddr, 0, sizeof(saddr)); |
| 140 |
saddr.sin_family = AF_INET; |
| 141 |
saddr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 142 |
saddr.sin_port = htons(0); |
| 143 |
|
| 144 |
rv = bind(sd, (struct sockaddr*)&saddr, sizeof(saddr)); |
| 145 |
PR_ASSERT(rv == 0); |
| 146 |
saddr_len = sizeof(saddr); |
| 147 |
rv = getsockname(sd, (struct sockaddr*)&saddr, &saddr_len); |
| 148 |
PR_ASSERT(rv == 0); |
| 149 |
listenPort3 = ntohs(saddr.sin_port); |
| 150 |
|
| 151 |
rv = listen(sd, 5); |
| 152 |
PR_ASSERT(rv == 0); |
| 153 |
pds[npds].fd = socket_poll_fd = PR_CreateSocketPollFd(sd); |
| 154 |
PR_ASSERT(pds[npds].fd); |
| 155 |
pds[npds].in_flags = PR_POLL_READ; |
| 156 |
npds++; |
| 157 |
PR_snprintf(buf, sizeof(buf), |
| 158 |
"The server thread is listening on ports %hu, %hu and %hu\n\n", |
| 159 |
listenPort1, listenPort2, listenPort3); |
| 160 |
printf("%s", buf); |
| 161 |
|
| 162 |
/* Testing timeout */ |
| 163 |
printf("PR_Poll should time out in 5 seconds\n"); |
| 164 |
retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5)); |
| 165 |
if (retVal != 0) { |
| 166 |
PR_snprintf(buf, sizeof(buf), |
| 167 |
"PR_Poll should time out and return 0, but it returns %ld\n", |
| 168 |
retVal); |
| 169 |
fprintf(stderr, "%s", buf); |
| 170 |
exit(1); |
| 171 |
} |
| 172 |
printf("PR_Poll timed out. Test passed.\n\n"); |
| 173 |
|
| 174 |
/* Testing bad fd */ |
| 175 |
printf("PR_Poll should detect a bad file descriptor\n"); |
| 176 |
if ((badFD = PR_NewTCPSocket()) == NULL) { |
| 177 |
fprintf(stderr, "Can't create a TCP socket\n"); |
| 178 |
exit(1); |
| 179 |
} |
| 180 |
|
| 181 |
pds[npds].fd = badFD; |
| 182 |
pds[npds].in_flags = PR_POLL_READ; |
| 183 |
npds++; |
| 184 |
PR_Close(badFD); /* make the fd bad */ |
| 185 |
# if 0 |
| 186 |
retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT); |
| 187 |
if (retVal != 1 || (unsigned short) pds[2].out_flags != PR_POLL_NVAL) { |
| 188 |
fprintf(stderr, "Failed to detect the bad fd: " |
| 189 |
"PR_Poll returns %d, out_flags is 0x%hx\n", |
| 190 |
retVal, pds[npds - 1].out_flags); |
| 191 |
exit(1); |
| 192 |
} |
| 193 |
printf("PR_Poll detected the bad fd. Test passed.\n\n"); |
| 194 |
# endif |
| 195 |
npds--; |
| 196 |
|
| 197 |
clientThread = PR_CreateThread(PR_USER_THREAD, clientThreadFunc, |
| 198 |
(void*)listenPort1, PR_PRIORITY_NORMAL, |
| 199 |
PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); |
| 200 |
if (clientThread == NULL) { |
| 201 |
fprintf(stderr, "can't create thread\n"); |
| 202 |
exit(1); |
| 203 |
} |
| 204 |
|
| 205 |
clientThread = PR_CreateThread(PR_USER_THREAD, clientThreadFunc, |
| 206 |
(void*)listenPort2, PR_PRIORITY_NORMAL, |
| 207 |
PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0); |
| 208 |
if (clientThread == NULL) { |
| 209 |
fprintf(stderr, "can't create thread\n"); |
| 210 |
exit(1); |
| 211 |
} |
| 212 |
|
| 213 |
clientThread = PR_CreateThread( |
| 214 |
PR_USER_THREAD, clientThreadFunc, (void*)listenPort3, PR_PRIORITY_NORMAL, |
| 215 |
PR_GLOBAL_BOUND_THREAD, PR_UNJOINABLE_THREAD, 0); |
| 216 |
if (clientThread == NULL) { |
| 217 |
fprintf(stderr, "can't create thread\n"); |
| 218 |
exit(1); |
| 219 |
} |
| 220 |
|
| 221 |
printf("Three client threads are created. Each of them will\n"); |
| 222 |
printf("send data to one of the three ports the server is listening on.\n"); |
| 223 |
printf("The data they send is the port number. Each of them send\n"); |
| 224 |
printf("the data five times, so you should see ten lines below,\n"); |
| 225 |
printf("interleaved in an arbitrary order.\n"); |
| 226 |
|
| 227 |
/* 30 events total */ |
| 228 |
i = 0; |
| 229 |
while (i < 30) { |
| 230 |
PRPollDesc* tmp; |
| 231 |
int nextIndex; |
| 232 |
int nEvents = 0; |
| 233 |
|
| 234 |
retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT); |
| 235 |
PR_ASSERT(retVal != 0); /* no timeout */ |
| 236 |
if (retVal == -1) { |
| 237 |
fprintf(stderr, "PR_Poll failed\n"); |
| 238 |
exit(1); |
| 239 |
} |
| 240 |
|
| 241 |
nextIndex = 3; |
| 242 |
/* the three listening sockets */ |
| 243 |
for (j = 0; j < 3; j++) { |
| 244 |
other_pds[j] = pds[j]; |
| 245 |
PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0 && |
| 246 |
(pds[j].out_flags & PR_POLL_EXCEPT) == 0); |
| 247 |
if (pds[j].out_flags & PR_POLL_READ) { |
| 248 |
PRFileDesc* sock; |
| 249 |
|
| 250 |
nEvents++; |
| 251 |
if (j == 2) { |
| 252 |
PROsfd newsd; |
| 253 |
newsd = accept(PR_FileDesc2NativeHandle(pds[j].fd), NULL, 0); |
| 254 |
if (newsd == -1) { |
| 255 |
fprintf(stderr, "accept() failed\n"); |
| 256 |
exit(1); |
| 257 |
} |
| 258 |
other_pds[nextIndex].fd = PR_CreateSocketPollFd(newsd); |
| 259 |
PR_ASSERT(other_pds[nextIndex].fd); |
| 260 |
other_pds[nextIndex].in_flags = PR_POLL_READ; |
| 261 |
} else { |
| 262 |
sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT); |
| 263 |
if (sock == NULL) { |
| 264 |
fprintf(stderr, "PR_Accept() failed\n"); |
| 265 |
exit(1); |
| 266 |
} |
| 267 |
other_pds[nextIndex].fd = sock; |
| 268 |
other_pds[nextIndex].in_flags = PR_POLL_READ; |
| 269 |
} |
| 270 |
nextIndex++; |
| 271 |
} else if (pds[j].out_flags & PR_POLL_ERR) { |
| 272 |
fprintf(stderr, "PR_Poll() indicates that an fd has error\n"); |
| 273 |
exit(1); |
| 274 |
} else if (pds[j].out_flags & PR_POLL_NVAL) { |
| 275 |
fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n", |
| 276 |
PR_FileDesc2NativeHandle(pds[j].fd)); |
| 277 |
exit(1); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
for (j = 3; j < npds; j++) { |
| 282 |
PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0 && |
| 283 |
(pds[j].out_flags & PR_POLL_EXCEPT) == 0); |
| 284 |
if (pds[j].out_flags & PR_POLL_READ) { |
| 285 |
PRInt32 nBytes; |
| 286 |
|
| 287 |
nEvents++; |
| 288 |
/* XXX: This call is a hack and should be fixed */ |
| 289 |
if (PR_GetDescType(pds[j].fd) == (PRDescType)0) { |
| 290 |
nBytes = |
| 291 |
recv(PR_FileDesc2NativeHandle(pds[j].fd), buf, sizeof(buf), 0); |
| 292 |
if (nBytes == -1) { |
| 293 |
fprintf(stderr, "recv() failed\n"); |
| 294 |
exit(1); |
| 295 |
} |
| 296 |
printf("Server read %d bytes from native fd %d\n", nBytes, |
| 297 |
PR_FileDesc2NativeHandle(pds[j].fd)); |
| 298 |
# ifdef WIN32 |
| 299 |
closesocket((SOCKET)PR_FileDesc2NativeHandle(pds[j].fd)); |
| 300 |
# else |
| 301 |
close(PR_FileDesc2NativeHandle(pds[j].fd)); |
| 302 |
# endif |
| 303 |
PR_DestroySocketPollFd(pds[j].fd); |
| 304 |
} else { |
| 305 |
nBytes = PR_Read(pds[j].fd, buf, sizeof(buf)); |
| 306 |
if (nBytes == -1) { |
| 307 |
fprintf(stderr, "PR_Read() failed\n"); |
| 308 |
exit(1); |
| 309 |
} |
| 310 |
PR_Close(pds[j].fd); |
| 311 |
} |
| 312 |
/* Just to be safe */ |
| 313 |
buf[BUF_SIZE - 1] = '\0'; |
| 314 |
printf("The server received \"%s\" from a client\n", buf); |
| 315 |
} else if (pds[j].out_flags & PR_POLL_ERR) { |
| 316 |
fprintf(stderr, "PR_Poll() indicates that an fd has error\n"); |
| 317 |
exit(1); |
| 318 |
} else if (pds[j].out_flags & PR_POLL_NVAL) { |
| 319 |
fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n"); |
| 320 |
exit(1); |
| 321 |
} else { |
| 322 |
other_pds[nextIndex] = pds[j]; |
| 323 |
nextIndex++; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
PR_ASSERT(retVal == nEvents); |
| 328 |
/* swap */ |
| 329 |
tmp = pds; |
| 330 |
pds = other_pds; |
| 331 |
other_pds = tmp; |
| 332 |
npds = nextIndex; |
| 333 |
i += nEvents; |
| 334 |
} |
| 335 |
PR_DestroySocketPollFd(socket_poll_fd); |
| 336 |
|
| 337 |
printf("All tests finished\n"); |
| 338 |
PR_Cleanup(); |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
#endif /* ifdef WINCE */ |