Wed, 31 Dec 2014 06:09:35 +0100
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 | #include "nspr.h" |
michael@0 | 7 | #include "prio.h" |
michael@0 | 8 | #include "prerror.h" |
michael@0 | 9 | #include "prlog.h" |
michael@0 | 10 | #include "prprf.h" |
michael@0 | 11 | #include "prnetdb.h" |
michael@0 | 12 | #include "plerror.h" |
michael@0 | 13 | #include "obsolete/probslet.h" |
michael@0 | 14 | |
michael@0 | 15 | #include <stdio.h> |
michael@0 | 16 | #include <string.h> |
michael@0 | 17 | #include <stdlib.h> |
michael@0 | 18 | |
michael@0 | 19 | #define NUMBER_ROUNDS 5 |
michael@0 | 20 | |
michael@0 | 21 | #if defined(WIN16) |
michael@0 | 22 | /* |
michael@0 | 23 | ** Make win16 unit_time interval 300 milliseconds, others get 100 |
michael@0 | 24 | */ |
michael@0 | 25 | #define UNIT_TIME 200 /* unit time in milliseconds */ |
michael@0 | 26 | #elif defined(SYMBIAN) |
michael@0 | 27 | #define UNIT_TIME 5000 /* unit time in milliseconds */ |
michael@0 | 28 | #else |
michael@0 | 29 | #define UNIT_TIME 100 /* unit time in milliseconds */ |
michael@0 | 30 | #endif |
michael@0 | 31 | #define CHUNK_SIZE 10 |
michael@0 | 32 | #undef USE_PR_SELECT /* If defined, we use PR_Select. |
michael@0 | 33 | * If not defined, use PR_Poll instead. */ |
michael@0 | 34 | |
michael@0 | 35 | #if defined(USE_PR_SELECT) |
michael@0 | 36 | #include "pprio.h" |
michael@0 | 37 | #endif |
michael@0 | 38 | |
michael@0 | 39 | static void PR_CALLBACK |
michael@0 | 40 | clientThreadFunc(void *arg) |
michael@0 | 41 | { |
michael@0 | 42 | PRUintn port = (PRUintn)arg; |
michael@0 | 43 | PRFileDesc *sock; |
michael@0 | 44 | PRNetAddr addr; |
michael@0 | 45 | char buf[CHUNK_SIZE]; |
michael@0 | 46 | int i; |
michael@0 | 47 | PRIntervalTime unitTime = PR_MillisecondsToInterval(UNIT_TIME); |
michael@0 | 48 | PRSocketOptionData optval; |
michael@0 | 49 | PRStatus retVal; |
michael@0 | 50 | PRInt32 nBytes; |
michael@0 | 51 | |
michael@0 | 52 | /* Initialize the buffer so that Purify won't complain */ |
michael@0 | 53 | memset(buf, 0, sizeof(buf)); |
michael@0 | 54 | |
michael@0 | 55 | addr.inet.family = PR_AF_INET; |
michael@0 | 56 | addr.inet.port = PR_htons((PRUint16)port); |
michael@0 | 57 | addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); |
michael@0 | 58 | PR_snprintf(buf, sizeof(buf), "%hu", addr.inet.ip); |
michael@0 | 59 | |
michael@0 | 60 | /* time 1 */ |
michael@0 | 61 | PR_Sleep(unitTime); |
michael@0 | 62 | sock = PR_NewTCPSocket(); |
michael@0 | 63 | optval.option = PR_SockOpt_Nonblocking; |
michael@0 | 64 | optval.value.non_blocking = PR_TRUE; |
michael@0 | 65 | PR_SetSocketOption(sock, &optval); |
michael@0 | 66 | retVal = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 67 | if (retVal == PR_FAILURE && PR_GetError() == PR_IN_PROGRESS_ERROR) { |
michael@0 | 68 | #if !defined(USE_PR_SELECT) |
michael@0 | 69 | PRPollDesc pd; |
michael@0 | 70 | PRInt32 n; |
michael@0 | 71 | fprintf(stderr, "connect: EWOULDBLOCK, good\n"); |
michael@0 | 72 | pd.fd = sock; |
michael@0 | 73 | pd.in_flags = PR_POLL_WRITE; |
michael@0 | 74 | n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 75 | PR_ASSERT(n == 1); |
michael@0 | 76 | PR_ASSERT(pd.out_flags == PR_POLL_WRITE); |
michael@0 | 77 | #else |
michael@0 | 78 | PR_fd_set writeSet; |
michael@0 | 79 | PRInt32 n; |
michael@0 | 80 | fprintf(stderr, "connect: EWOULDBLOCK, good\n"); |
michael@0 | 81 | PR_FD_ZERO(&writeSet); |
michael@0 | 82 | PR_FD_SET(sock, &writeSet); |
michael@0 | 83 | n = PR_Select(0, NULL, &writeSet, NULL, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 84 | PR_ASSERT(n == 1); |
michael@0 | 85 | PR_ASSERT(PR_FD_ISSET(sock, &writeSet)); |
michael@0 | 86 | #endif |
michael@0 | 87 | } |
michael@0 | 88 | printf("client connected\n"); |
michael@0 | 89 | fflush(stdout); |
michael@0 | 90 | |
michael@0 | 91 | /* time 4, 7, 11, etc. */ |
michael@0 | 92 | for (i = 0; i < NUMBER_ROUNDS; i++) { |
michael@0 | 93 | PR_Sleep(3 * unitTime); |
michael@0 | 94 | nBytes = PR_Write(sock, buf, sizeof(buf)); |
michael@0 | 95 | if (nBytes == -1) { |
michael@0 | 96 | if (PR_GetError() == PR_WOULD_BLOCK_ERROR) { |
michael@0 | 97 | fprintf(stderr, "write: EWOULDBLOCK\n"); |
michael@0 | 98 | exit(1); |
michael@0 | 99 | } else { |
michael@0 | 100 | fprintf(stderr, "write: failed\n"); |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | printf("client sent %d bytes\n", nBytes); |
michael@0 | 104 | fflush(stdout); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | PR_Close(sock); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv ) |
michael@0 | 111 | { |
michael@0 | 112 | PRFileDesc *listenSock, *sock; |
michael@0 | 113 | PRUint16 listenPort; |
michael@0 | 114 | PRNetAddr addr; |
michael@0 | 115 | char buf[CHUNK_SIZE]; |
michael@0 | 116 | PRThread *clientThread; |
michael@0 | 117 | PRInt32 retVal; |
michael@0 | 118 | PRSocketOptionData optval; |
michael@0 | 119 | PRIntn i; |
michael@0 | 120 | PRIntervalTime unitTime = PR_MillisecondsToInterval(UNIT_TIME); |
michael@0 | 121 | |
michael@0 | 122 | /* Create a listening socket */ |
michael@0 | 123 | if ((listenSock = PR_NewTCPSocket()) == NULL) { |
michael@0 | 124 | fprintf(stderr, "Can't create a new TCP socket\n"); |
michael@0 | 125 | exit(1); |
michael@0 | 126 | } |
michael@0 | 127 | addr.inet.family = PR_AF_INET; |
michael@0 | 128 | addr.inet.ip = PR_htonl(PR_INADDR_ANY); |
michael@0 | 129 | addr.inet.port = PR_htons(0); |
michael@0 | 130 | if (PR_Bind(listenSock, &addr) == PR_FAILURE) { |
michael@0 | 131 | fprintf(stderr, "Can't bind socket\n"); |
michael@0 | 132 | exit(1); |
michael@0 | 133 | } |
michael@0 | 134 | if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) { |
michael@0 | 135 | fprintf(stderr, "PR_GetSockName failed\n"); |
michael@0 | 136 | exit(1); |
michael@0 | 137 | } |
michael@0 | 138 | listenPort = PR_ntohs(addr.inet.port); |
michael@0 | 139 | if (PR_Listen(listenSock, 5) == PR_FAILURE) { |
michael@0 | 140 | fprintf(stderr, "Can't listen on a socket\n"); |
michael@0 | 141 | exit(1); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | PR_snprintf(buf, sizeof(buf), |
michael@0 | 145 | "The server thread is listening on port %hu\n\n", |
michael@0 | 146 | listenPort); |
michael@0 | 147 | printf("%s", buf); |
michael@0 | 148 | |
michael@0 | 149 | clientThread = PR_CreateThread(PR_USER_THREAD, |
michael@0 | 150 | clientThreadFunc, (void *) listenPort, |
michael@0 | 151 | PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, |
michael@0 | 152 | PR_UNJOINABLE_THREAD, 0); |
michael@0 | 153 | if (clientThread == NULL) { |
michael@0 | 154 | fprintf(stderr, "can't create thread\n"); |
michael@0 | 155 | exit(1); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | printf("client thread created.\n"); |
michael@0 | 159 | |
michael@0 | 160 | optval.option = PR_SockOpt_Nonblocking; |
michael@0 | 161 | optval.value.non_blocking = PR_TRUE; |
michael@0 | 162 | PR_SetSocketOption(listenSock, &optval); |
michael@0 | 163 | /* time 0 */ |
michael@0 | 164 | sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 165 | if (sock != NULL || PR_GetError() != PR_WOULD_BLOCK_ERROR) { |
michael@0 | 166 | PL_PrintError("First Accept\n"); |
michael@0 | 167 | fprintf(stderr, "First PR_Accept() xxx\n" ); |
michael@0 | 168 | exit(1); |
michael@0 | 169 | } |
michael@0 | 170 | printf("accept: EWOULDBLOCK, good\n"); |
michael@0 | 171 | fflush(stdout); |
michael@0 | 172 | /* time 2 */ |
michael@0 | 173 | PR_Sleep(2 * unitTime); |
michael@0 | 174 | sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 175 | if (sock == NULL) { |
michael@0 | 176 | PL_PrintError("Second Accept\n"); |
michael@0 | 177 | fprintf(stderr, "Second PR_Accept() failed: (%d, %d)\n", |
michael@0 | 178 | PR_GetError(), PR_GetOSError()); |
michael@0 | 179 | exit(1); |
michael@0 | 180 | } |
michael@0 | 181 | printf("accept: succeeded, good\n"); |
michael@0 | 182 | fflush(stdout); |
michael@0 | 183 | PR_Close(listenSock); |
michael@0 | 184 | |
michael@0 | 185 | PR_SetSocketOption(sock, &optval); |
michael@0 | 186 | |
michael@0 | 187 | /* time 3, 5, 6, 8, etc. */ |
michael@0 | 188 | for (i = 0; i < NUMBER_ROUNDS; i++) { |
michael@0 | 189 | PR_Sleep(unitTime); |
michael@0 | 190 | retVal = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 191 | if (retVal != -1 || PR_GetError() != PR_WOULD_BLOCK_ERROR) { |
michael@0 | 192 | PL_PrintError("First Receive:\n"); |
michael@0 | 193 | fprintf(stderr, "First PR_Recv: retVal: %ld, Error: %ld\n", |
michael@0 | 194 | retVal, PR_GetError()); |
michael@0 | 195 | exit(1); |
michael@0 | 196 | } |
michael@0 | 197 | printf("read: EWOULDBLOCK, good\n"); |
michael@0 | 198 | fflush(stdout); |
michael@0 | 199 | PR_Sleep(2 * unitTime); |
michael@0 | 200 | retVal = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 201 | if (retVal != CHUNK_SIZE) { |
michael@0 | 202 | PL_PrintError("Second Receive:\n"); |
michael@0 | 203 | fprintf(stderr, "Second PR_Recv: retVal: %ld, Error: %ld\n", |
michael@0 | 204 | retVal, PR_GetError()); |
michael@0 | 205 | exit(1); |
michael@0 | 206 | } |
michael@0 | 207 | printf("read: %d bytes, good\n", retVal); |
michael@0 | 208 | fflush(stdout); |
michael@0 | 209 | } |
michael@0 | 210 | PR_Close(sock); |
michael@0 | 211 | |
michael@0 | 212 | printf("All tests finished\n"); |
michael@0 | 213 | printf("PASS\n"); |
michael@0 | 214 | return 0; |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | int main(int argc, char **argv) |
michael@0 | 218 | { |
michael@0 | 219 | PRIntn rv; |
michael@0 | 220 | |
michael@0 | 221 | PR_STDIO_INIT(); |
michael@0 | 222 | rv = PR_Initialize(RealMain, argc, argv, 0); |
michael@0 | 223 | return rv; |
michael@0 | 224 | } /* main */ |
michael@0 | 225 |