nsprpub/pr/tests/prpoll.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial