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 | /* |
michael@0 | 7 | * Test: zerolen.c |
michael@0 | 8 | * |
michael@0 | 9 | * Description: a test for Bugzilla bug #17699. We perform |
michael@0 | 10 | * the same test for PR_Writev, PR_Write, and PR_Send. In |
michael@0 | 11 | * each test the server thread first fills up the connection |
michael@0 | 12 | * to the client so that the next write operation will fail |
michael@0 | 13 | * with EAGAIN. Then it calls PR_Writev, PR_Write, or PR_Send |
michael@0 | 14 | * with a zero-length buffer. The client thread initially |
michael@0 | 15 | * does not read so that the connection can be filled up. |
michael@0 | 16 | * Then it empties the connection so that the server thread's |
michael@0 | 17 | * PR_Writev, PR_Write, or PR_Send call can succeed. |
michael@0 | 18 | * |
michael@0 | 19 | * Bug #17699 is specific to the pthreads version on Unix, |
michael@0 | 20 | * so on other platforms this test does nothing. |
michael@0 | 21 | */ |
michael@0 | 22 | |
michael@0 | 23 | #ifndef XP_UNIX |
michael@0 | 24 | |
michael@0 | 25 | #include <stdio.h> |
michael@0 | 26 | |
michael@0 | 27 | int main(int argc, char **argv) |
michael@0 | 28 | { |
michael@0 | 29 | printf("PASS\n"); |
michael@0 | 30 | return 0; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | #else /* XP_UNIX */ |
michael@0 | 34 | |
michael@0 | 35 | #include "nspr.h" |
michael@0 | 36 | #include "private/pprio.h" |
michael@0 | 37 | |
michael@0 | 38 | #include <stdio.h> |
michael@0 | 39 | #include <stdlib.h> |
michael@0 | 40 | #include <string.h> |
michael@0 | 41 | #include <errno.h> |
michael@0 | 42 | #include <unistd.h> |
michael@0 | 43 | |
michael@0 | 44 | static void ClientThread(void *arg) |
michael@0 | 45 | { |
michael@0 | 46 | PRFileDesc *sock; |
michael@0 | 47 | PRNetAddr addr; |
michael@0 | 48 | PRUint16 port = (PRUint16) arg; |
michael@0 | 49 | char buf[1024]; |
michael@0 | 50 | PRInt32 nbytes; |
michael@0 | 51 | |
michael@0 | 52 | sock = PR_NewTCPSocket(); |
michael@0 | 53 | if (NULL == sock) { |
michael@0 | 54 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 55 | exit(1); |
michael@0 | 56 | } |
michael@0 | 57 | if (PR_InitializeNetAddr(PR_IpAddrLoopback, port, &addr) == PR_FAILURE) { |
michael@0 | 58 | fprintf(stderr, "PR_InitializeNetAddr failed\n"); |
michael@0 | 59 | exit(1); |
michael@0 | 60 | } |
michael@0 | 61 | if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) { |
michael@0 | 62 | fprintf(stderr, "PR_Connect failed\n"); |
michael@0 | 63 | exit(1); |
michael@0 | 64 | } |
michael@0 | 65 | /* |
michael@0 | 66 | * Sleep 5 seconds to force the server thread to get EAGAIN. |
michael@0 | 67 | */ |
michael@0 | 68 | if (PR_Sleep(PR_SecondsToInterval(5)) == PR_FAILURE) { |
michael@0 | 69 | fprintf(stderr, "PR_Sleep failed\n"); |
michael@0 | 70 | exit(1); |
michael@0 | 71 | } |
michael@0 | 72 | /* |
michael@0 | 73 | * Then start reading. |
michael@0 | 74 | */ |
michael@0 | 75 | while ((nbytes = PR_Read(sock, buf, sizeof(buf))) > 0) { |
michael@0 | 76 | /* empty loop body */ |
michael@0 | 77 | } |
michael@0 | 78 | if (-1 == nbytes) { |
michael@0 | 79 | fprintf(stderr, "PR_Read failed\n"); |
michael@0 | 80 | exit(1); |
michael@0 | 81 | } |
michael@0 | 82 | if (PR_Close(sock) == PR_FAILURE) { |
michael@0 | 83 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 84 | exit(1); |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | int main() |
michael@0 | 89 | { |
michael@0 | 90 | PRFileDesc *listenSock; |
michael@0 | 91 | PRFileDesc *acceptSock; |
michael@0 | 92 | int osfd; |
michael@0 | 93 | PRThread *clientThread; |
michael@0 | 94 | PRNetAddr addr; |
michael@0 | 95 | char buf[1024]; |
michael@0 | 96 | PRInt32 nbytes; |
michael@0 | 97 | PRIOVec iov; |
michael@0 | 98 | #ifdef SYMBIAN |
michael@0 | 99 | int loopcount=0; |
michael@0 | 100 | #endif |
michael@0 | 101 | |
michael@0 | 102 | memset(buf, 0, sizeof(buf)); /* Initialize the buffer. */ |
michael@0 | 103 | listenSock = PR_NewTCPSocket(); |
michael@0 | 104 | if (NULL == listenSock) { |
michael@0 | 105 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 106 | exit(1); |
michael@0 | 107 | } |
michael@0 | 108 | if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) { |
michael@0 | 109 | fprintf(stderr, "PR_InitializeNetAddr failed\n"); |
michael@0 | 110 | exit(1); |
michael@0 | 111 | } |
michael@0 | 112 | if (PR_Bind(listenSock, &addr) == PR_FAILURE) { |
michael@0 | 113 | fprintf(stderr, "PR_Bind failed\n"); |
michael@0 | 114 | exit(1); |
michael@0 | 115 | } |
michael@0 | 116 | /* Find out what port number we are bound to. */ |
michael@0 | 117 | if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) { |
michael@0 | 118 | fprintf(stderr, "PR_GetSockName failed\n"); |
michael@0 | 119 | exit(1); |
michael@0 | 120 | } |
michael@0 | 121 | if (PR_Listen(listenSock, 5) == PR_FAILURE) { |
michael@0 | 122 | fprintf(stderr, "PR_Listen failed\n"); |
michael@0 | 123 | exit(1); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | /* |
michael@0 | 127 | * First test PR_Writev. |
michael@0 | 128 | */ |
michael@0 | 129 | clientThread = PR_CreateThread(PR_USER_THREAD, |
michael@0 | 130 | ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), |
michael@0 | 131 | PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); |
michael@0 | 132 | if (NULL == clientThread) { |
michael@0 | 133 | fprintf(stderr, "PR_CreateThread failed\n"); |
michael@0 | 134 | exit(1); |
michael@0 | 135 | } |
michael@0 | 136 | acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 137 | if (NULL == acceptSock) { |
michael@0 | 138 | fprintf(stderr, "PR_Accept failed\n"); |
michael@0 | 139 | exit(1); |
michael@0 | 140 | } |
michael@0 | 141 | osfd = PR_FileDesc2NativeHandle(acceptSock); |
michael@0 | 142 | while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { |
michael@0 | 143 | /* empty loop body */ |
michael@0 | 144 | #ifdef SYMBIAN |
michael@0 | 145 | if (loopcount++>64) break; |
michael@0 | 146 | #endif |
michael@0 | 147 | } |
michael@0 | 148 | if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { |
michael@0 | 149 | fprintf(stderr, "write failed\n"); |
michael@0 | 150 | exit(1); |
michael@0 | 151 | } |
michael@0 | 152 | iov.iov_base = buf; |
michael@0 | 153 | iov.iov_len = 0; |
michael@0 | 154 | printf("calling PR_Writev with a zero-length buffer\n"); |
michael@0 | 155 | fflush(stdout); |
michael@0 | 156 | nbytes = PR_Writev(acceptSock, &iov, 1, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 157 | if (nbytes != 0) { |
michael@0 | 158 | fprintf(stderr, "PR_Writev should return 0 but returns %d\n", nbytes); |
michael@0 | 159 | exit(1); |
michael@0 | 160 | } |
michael@0 | 161 | if (PR_Close(acceptSock) == PR_FAILURE) { |
michael@0 | 162 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 163 | exit(1); |
michael@0 | 164 | } |
michael@0 | 165 | if (PR_JoinThread(clientThread) == PR_FAILURE) { |
michael@0 | 166 | fprintf(stderr, "PR_JoinThread failed\n"); |
michael@0 | 167 | exit(1); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | /* |
michael@0 | 171 | * Then test PR_Write. |
michael@0 | 172 | */ |
michael@0 | 173 | clientThread = PR_CreateThread(PR_USER_THREAD, |
michael@0 | 174 | ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), |
michael@0 | 175 | PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); |
michael@0 | 176 | if (NULL == clientThread) { |
michael@0 | 177 | fprintf(stderr, "PR_CreateThread failed\n"); |
michael@0 | 178 | exit(1); |
michael@0 | 179 | } |
michael@0 | 180 | #ifdef SYMBIAN |
michael@0 | 181 | loopcount = 0; |
michael@0 | 182 | #endif |
michael@0 | 183 | acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 184 | if (NULL == acceptSock) { |
michael@0 | 185 | fprintf(stderr, "PR_Accept failed\n"); |
michael@0 | 186 | exit(1); |
michael@0 | 187 | } |
michael@0 | 188 | osfd = PR_FileDesc2NativeHandle(acceptSock); |
michael@0 | 189 | while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { |
michael@0 | 190 | /* empty loop body */ |
michael@0 | 191 | #ifdef SYMBIAN |
michael@0 | 192 | if (loopcount++>64) break; |
michael@0 | 193 | #endif |
michael@0 | 194 | } |
michael@0 | 195 | if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { |
michael@0 | 196 | fprintf(stderr, "write failed\n"); |
michael@0 | 197 | exit(1); |
michael@0 | 198 | } |
michael@0 | 199 | printf("calling PR_Write with a zero-length buffer\n"); |
michael@0 | 200 | fflush(stdout); |
michael@0 | 201 | nbytes = PR_Write(acceptSock, buf, 0); |
michael@0 | 202 | if (nbytes != 0) { |
michael@0 | 203 | fprintf(stderr, "PR_Write should return 0 but returns %d\n", nbytes); |
michael@0 | 204 | exit(1); |
michael@0 | 205 | } |
michael@0 | 206 | if (PR_Close(acceptSock) == PR_FAILURE) { |
michael@0 | 207 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 208 | exit(1); |
michael@0 | 209 | } |
michael@0 | 210 | if (PR_JoinThread(clientThread) == PR_FAILURE) { |
michael@0 | 211 | fprintf(stderr, "PR_JoinThread failed\n"); |
michael@0 | 212 | exit(1); |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | /* |
michael@0 | 216 | * Finally test PR_Send. |
michael@0 | 217 | */ |
michael@0 | 218 | clientThread = PR_CreateThread(PR_USER_THREAD, |
michael@0 | 219 | ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), |
michael@0 | 220 | PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); |
michael@0 | 221 | if (NULL == clientThread) { |
michael@0 | 222 | fprintf(stderr, "PR_CreateThread failed\n"); |
michael@0 | 223 | exit(1); |
michael@0 | 224 | } |
michael@0 | 225 | #ifdef SYMBIAN |
michael@0 | 226 | loopcount = 0; |
michael@0 | 227 | #endif |
michael@0 | 228 | acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 229 | if (NULL == acceptSock) { |
michael@0 | 230 | fprintf(stderr, "PR_Accept failed\n"); |
michael@0 | 231 | exit(1); |
michael@0 | 232 | } |
michael@0 | 233 | osfd = PR_FileDesc2NativeHandle(acceptSock); |
michael@0 | 234 | while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { |
michael@0 | 235 | /* empty loop body */ |
michael@0 | 236 | #ifdef SYMBIAN |
michael@0 | 237 | if (loopcount++>64) break; |
michael@0 | 238 | #endif |
michael@0 | 239 | } |
michael@0 | 240 | if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { |
michael@0 | 241 | fprintf(stderr, "write failed\n"); |
michael@0 | 242 | exit(1); |
michael@0 | 243 | } |
michael@0 | 244 | printf("calling PR_Send with a zero-length buffer\n"); |
michael@0 | 245 | fflush(stdout); |
michael@0 | 246 | nbytes = PR_Send(acceptSock, buf, 0, 0, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 247 | if (nbytes != 0) { |
michael@0 | 248 | fprintf(stderr, "PR_Send should return 0 but returns %d\n", nbytes); |
michael@0 | 249 | exit(1); |
michael@0 | 250 | } |
michael@0 | 251 | if (PR_Close(acceptSock) == PR_FAILURE) { |
michael@0 | 252 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 253 | exit(1); |
michael@0 | 254 | } |
michael@0 | 255 | if (PR_JoinThread(clientThread) == PR_FAILURE) { |
michael@0 | 256 | fprintf(stderr, "PR_JoinThread failed\n"); |
michael@0 | 257 | exit(1); |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | if (PR_Close(listenSock) == PR_FAILURE) { |
michael@0 | 261 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 262 | exit(1); |
michael@0 | 263 | } |
michael@0 | 264 | printf("PASS\n"); |
michael@0 | 265 | return 0; |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | #endif /* XP_UNIX */ |