1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/zerolen.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,268 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * Test: zerolen.c 1.11 + * 1.12 + * Description: a test for Bugzilla bug #17699. We perform 1.13 + * the same test for PR_Writev, PR_Write, and PR_Send. In 1.14 + * each test the server thread first fills up the connection 1.15 + * to the client so that the next write operation will fail 1.16 + * with EAGAIN. Then it calls PR_Writev, PR_Write, or PR_Send 1.17 + * with a zero-length buffer. The client thread initially 1.18 + * does not read so that the connection can be filled up. 1.19 + * Then it empties the connection so that the server thread's 1.20 + * PR_Writev, PR_Write, or PR_Send call can succeed. 1.21 + * 1.22 + * Bug #17699 is specific to the pthreads version on Unix, 1.23 + * so on other platforms this test does nothing. 1.24 + */ 1.25 + 1.26 +#ifndef XP_UNIX 1.27 + 1.28 +#include <stdio.h> 1.29 + 1.30 +int main(int argc, char **argv) 1.31 +{ 1.32 + printf("PASS\n"); 1.33 + return 0; 1.34 +} 1.35 + 1.36 +#else /* XP_UNIX */ 1.37 + 1.38 +#include "nspr.h" 1.39 +#include "private/pprio.h" 1.40 + 1.41 +#include <stdio.h> 1.42 +#include <stdlib.h> 1.43 +#include <string.h> 1.44 +#include <errno.h> 1.45 +#include <unistd.h> 1.46 + 1.47 +static void ClientThread(void *arg) 1.48 +{ 1.49 + PRFileDesc *sock; 1.50 + PRNetAddr addr; 1.51 + PRUint16 port = (PRUint16) arg; 1.52 + char buf[1024]; 1.53 + PRInt32 nbytes; 1.54 + 1.55 + sock = PR_NewTCPSocket(); 1.56 + if (NULL == sock) { 1.57 + fprintf(stderr, "PR_NewTCPSocket failed\n"); 1.58 + exit(1); 1.59 + } 1.60 + if (PR_InitializeNetAddr(PR_IpAddrLoopback, port, &addr) == PR_FAILURE) { 1.61 + fprintf(stderr, "PR_InitializeNetAddr failed\n"); 1.62 + exit(1); 1.63 + } 1.64 + if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) { 1.65 + fprintf(stderr, "PR_Connect failed\n"); 1.66 + exit(1); 1.67 + } 1.68 + /* 1.69 + * Sleep 5 seconds to force the server thread to get EAGAIN. 1.70 + */ 1.71 + if (PR_Sleep(PR_SecondsToInterval(5)) == PR_FAILURE) { 1.72 + fprintf(stderr, "PR_Sleep failed\n"); 1.73 + exit(1); 1.74 + } 1.75 + /* 1.76 + * Then start reading. 1.77 + */ 1.78 + while ((nbytes = PR_Read(sock, buf, sizeof(buf))) > 0) { 1.79 + /* empty loop body */ 1.80 + } 1.81 + if (-1 == nbytes) { 1.82 + fprintf(stderr, "PR_Read failed\n"); 1.83 + exit(1); 1.84 + } 1.85 + if (PR_Close(sock) == PR_FAILURE) { 1.86 + fprintf(stderr, "PR_Close failed\n"); 1.87 + exit(1); 1.88 + } 1.89 +} 1.90 + 1.91 +int main() 1.92 +{ 1.93 + PRFileDesc *listenSock; 1.94 + PRFileDesc *acceptSock; 1.95 + int osfd; 1.96 + PRThread *clientThread; 1.97 + PRNetAddr addr; 1.98 + char buf[1024]; 1.99 + PRInt32 nbytes; 1.100 + PRIOVec iov; 1.101 +#ifdef SYMBIAN 1.102 + int loopcount=0; 1.103 +#endif 1.104 + 1.105 + memset(buf, 0, sizeof(buf)); /* Initialize the buffer. */ 1.106 + listenSock = PR_NewTCPSocket(); 1.107 + if (NULL == listenSock) { 1.108 + fprintf(stderr, "PR_NewTCPSocket failed\n"); 1.109 + exit(1); 1.110 + } 1.111 + if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) { 1.112 + fprintf(stderr, "PR_InitializeNetAddr failed\n"); 1.113 + exit(1); 1.114 + } 1.115 + if (PR_Bind(listenSock, &addr) == PR_FAILURE) { 1.116 + fprintf(stderr, "PR_Bind failed\n"); 1.117 + exit(1); 1.118 + } 1.119 + /* Find out what port number we are bound to. */ 1.120 + if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) { 1.121 + fprintf(stderr, "PR_GetSockName failed\n"); 1.122 + exit(1); 1.123 + } 1.124 + if (PR_Listen(listenSock, 5) == PR_FAILURE) { 1.125 + fprintf(stderr, "PR_Listen failed\n"); 1.126 + exit(1); 1.127 + } 1.128 + 1.129 + /* 1.130 + * First test PR_Writev. 1.131 + */ 1.132 + clientThread = PR_CreateThread(PR_USER_THREAD, 1.133 + ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), 1.134 + PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); 1.135 + if (NULL == clientThread) { 1.136 + fprintf(stderr, "PR_CreateThread failed\n"); 1.137 + exit(1); 1.138 + } 1.139 + acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); 1.140 + if (NULL == acceptSock) { 1.141 + fprintf(stderr, "PR_Accept failed\n"); 1.142 + exit(1); 1.143 + } 1.144 + osfd = PR_FileDesc2NativeHandle(acceptSock); 1.145 + while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { 1.146 + /* empty loop body */ 1.147 +#ifdef SYMBIAN 1.148 + if (loopcount++>64) break; 1.149 +#endif 1.150 + } 1.151 + if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { 1.152 + fprintf(stderr, "write failed\n"); 1.153 + exit(1); 1.154 + } 1.155 + iov.iov_base = buf; 1.156 + iov.iov_len = 0; 1.157 + printf("calling PR_Writev with a zero-length buffer\n"); 1.158 + fflush(stdout); 1.159 + nbytes = PR_Writev(acceptSock, &iov, 1, PR_INTERVAL_NO_TIMEOUT); 1.160 + if (nbytes != 0) { 1.161 + fprintf(stderr, "PR_Writev should return 0 but returns %d\n", nbytes); 1.162 + exit(1); 1.163 + } 1.164 + if (PR_Close(acceptSock) == PR_FAILURE) { 1.165 + fprintf(stderr, "PR_Close failed\n"); 1.166 + exit(1); 1.167 + } 1.168 + if (PR_JoinThread(clientThread) == PR_FAILURE) { 1.169 + fprintf(stderr, "PR_JoinThread failed\n"); 1.170 + exit(1); 1.171 + } 1.172 + 1.173 + /* 1.174 + * Then test PR_Write. 1.175 + */ 1.176 + clientThread = PR_CreateThread(PR_USER_THREAD, 1.177 + ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), 1.178 + PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); 1.179 + if (NULL == clientThread) { 1.180 + fprintf(stderr, "PR_CreateThread failed\n"); 1.181 + exit(1); 1.182 + } 1.183 +#ifdef SYMBIAN 1.184 + loopcount = 0; 1.185 +#endif 1.186 + acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); 1.187 + if (NULL == acceptSock) { 1.188 + fprintf(stderr, "PR_Accept failed\n"); 1.189 + exit(1); 1.190 + } 1.191 + osfd = PR_FileDesc2NativeHandle(acceptSock); 1.192 + while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { 1.193 + /* empty loop body */ 1.194 +#ifdef SYMBIAN 1.195 + if (loopcount++>64) break; 1.196 +#endif 1.197 + } 1.198 + if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { 1.199 + fprintf(stderr, "write failed\n"); 1.200 + exit(1); 1.201 + } 1.202 + printf("calling PR_Write with a zero-length buffer\n"); 1.203 + fflush(stdout); 1.204 + nbytes = PR_Write(acceptSock, buf, 0); 1.205 + if (nbytes != 0) { 1.206 + fprintf(stderr, "PR_Write should return 0 but returns %d\n", nbytes); 1.207 + exit(1); 1.208 + } 1.209 + if (PR_Close(acceptSock) == PR_FAILURE) { 1.210 + fprintf(stderr, "PR_Close failed\n"); 1.211 + exit(1); 1.212 + } 1.213 + if (PR_JoinThread(clientThread) == PR_FAILURE) { 1.214 + fprintf(stderr, "PR_JoinThread failed\n"); 1.215 + exit(1); 1.216 + } 1.217 + 1.218 + /* 1.219 + * Finally test PR_Send. 1.220 + */ 1.221 + clientThread = PR_CreateThread(PR_USER_THREAD, 1.222 + ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), 1.223 + PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); 1.224 + if (NULL == clientThread) { 1.225 + fprintf(stderr, "PR_CreateThread failed\n"); 1.226 + exit(1); 1.227 + } 1.228 +#ifdef SYMBIAN 1.229 + loopcount = 0; 1.230 +#endif 1.231 + acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); 1.232 + if (NULL == acceptSock) { 1.233 + fprintf(stderr, "PR_Accept failed\n"); 1.234 + exit(1); 1.235 + } 1.236 + osfd = PR_FileDesc2NativeHandle(acceptSock); 1.237 + while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { 1.238 + /* empty loop body */ 1.239 +#ifdef SYMBIAN 1.240 + if (loopcount++>64) break; 1.241 +#endif 1.242 + } 1.243 + if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { 1.244 + fprintf(stderr, "write failed\n"); 1.245 + exit(1); 1.246 + } 1.247 + printf("calling PR_Send with a zero-length buffer\n"); 1.248 + fflush(stdout); 1.249 + nbytes = PR_Send(acceptSock, buf, 0, 0, PR_INTERVAL_NO_TIMEOUT); 1.250 + if (nbytes != 0) { 1.251 + fprintf(stderr, "PR_Send should return 0 but returns %d\n", nbytes); 1.252 + exit(1); 1.253 + } 1.254 + if (PR_Close(acceptSock) == PR_FAILURE) { 1.255 + fprintf(stderr, "PR_Close failed\n"); 1.256 + exit(1); 1.257 + } 1.258 + if (PR_JoinThread(clientThread) == PR_FAILURE) { 1.259 + fprintf(stderr, "PR_JoinThread failed\n"); 1.260 + exit(1); 1.261 + } 1.262 + 1.263 + if (PR_Close(listenSock) == PR_FAILURE) { 1.264 + fprintf(stderr, "PR_Close failed\n"); 1.265 + exit(1); 1.266 + } 1.267 + printf("PASS\n"); 1.268 + return 0; 1.269 +} 1.270 + 1.271 +#endif /* XP_UNIX */