michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Test: zerolen.c michael@0: * michael@0: * Description: a test for Bugzilla bug #17699. We perform michael@0: * the same test for PR_Writev, PR_Write, and PR_Send. In michael@0: * each test the server thread first fills up the connection michael@0: * to the client so that the next write operation will fail michael@0: * with EAGAIN. Then it calls PR_Writev, PR_Write, or PR_Send michael@0: * with a zero-length buffer. The client thread initially michael@0: * does not read so that the connection can be filled up. michael@0: * Then it empties the connection so that the server thread's michael@0: * PR_Writev, PR_Write, or PR_Send call can succeed. michael@0: * michael@0: * Bug #17699 is specific to the pthreads version on Unix, michael@0: * so on other platforms this test does nothing. michael@0: */ michael@0: michael@0: #ifndef XP_UNIX michael@0: michael@0: #include michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: printf("PASS\n"); michael@0: return 0; michael@0: } michael@0: michael@0: #else /* XP_UNIX */ michael@0: michael@0: #include "nspr.h" michael@0: #include "private/pprio.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: static void ClientThread(void *arg) michael@0: { michael@0: PRFileDesc *sock; michael@0: PRNetAddr addr; michael@0: PRUint16 port = (PRUint16) arg; michael@0: char buf[1024]; michael@0: PRInt32 nbytes; michael@0: michael@0: sock = PR_NewTCPSocket(); michael@0: if (NULL == sock) { michael@0: fprintf(stderr, "PR_NewTCPSocket failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_InitializeNetAddr(PR_IpAddrLoopback, port, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_InitializeNetAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Connect failed\n"); michael@0: exit(1); michael@0: } michael@0: /* michael@0: * Sleep 5 seconds to force the server thread to get EAGAIN. michael@0: */ michael@0: if (PR_Sleep(PR_SecondsToInterval(5)) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Sleep failed\n"); michael@0: exit(1); michael@0: } michael@0: /* michael@0: * Then start reading. michael@0: */ michael@0: while ((nbytes = PR_Read(sock, buf, sizeof(buf))) > 0) { michael@0: /* empty loop body */ michael@0: } michael@0: if (-1 == nbytes) { michael@0: fprintf(stderr, "PR_Read failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_Close(sock) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: int main() michael@0: { michael@0: PRFileDesc *listenSock; michael@0: PRFileDesc *acceptSock; michael@0: int osfd; michael@0: PRThread *clientThread; michael@0: PRNetAddr addr; michael@0: char buf[1024]; michael@0: PRInt32 nbytes; michael@0: PRIOVec iov; michael@0: #ifdef SYMBIAN michael@0: int loopcount=0; michael@0: #endif michael@0: michael@0: memset(buf, 0, sizeof(buf)); /* Initialize the buffer. */ michael@0: listenSock = PR_NewTCPSocket(); michael@0: if (NULL == listenSock) { michael@0: fprintf(stderr, "PR_NewTCPSocket failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_InitializeNetAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_Bind(listenSock, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Bind failed\n"); michael@0: exit(1); michael@0: } michael@0: /* Find out what port number we are bound to. */ michael@0: if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_GetSockName failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_Listen(listenSock, 5) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Listen failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: /* michael@0: * First test PR_Writev. michael@0: */ michael@0: clientThread = PR_CreateThread(PR_USER_THREAD, michael@0: ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), michael@0: PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: if (NULL == clientThread) { michael@0: fprintf(stderr, "PR_CreateThread failed\n"); michael@0: exit(1); michael@0: } michael@0: acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); michael@0: if (NULL == acceptSock) { michael@0: fprintf(stderr, "PR_Accept failed\n"); michael@0: exit(1); michael@0: } michael@0: osfd = PR_FileDesc2NativeHandle(acceptSock); michael@0: while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { michael@0: /* empty loop body */ michael@0: #ifdef SYMBIAN michael@0: if (loopcount++>64) break; michael@0: #endif michael@0: } michael@0: if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { michael@0: fprintf(stderr, "write failed\n"); michael@0: exit(1); michael@0: } michael@0: iov.iov_base = buf; michael@0: iov.iov_len = 0; michael@0: printf("calling PR_Writev with a zero-length buffer\n"); michael@0: fflush(stdout); michael@0: nbytes = PR_Writev(acceptSock, &iov, 1, PR_INTERVAL_NO_TIMEOUT); michael@0: if (nbytes != 0) { michael@0: fprintf(stderr, "PR_Writev should return 0 but returns %d\n", nbytes); michael@0: exit(1); michael@0: } michael@0: if (PR_Close(acceptSock) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_JoinThread(clientThread) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: /* michael@0: * Then test PR_Write. michael@0: */ michael@0: clientThread = PR_CreateThread(PR_USER_THREAD, michael@0: ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), michael@0: PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: if (NULL == clientThread) { michael@0: fprintf(stderr, "PR_CreateThread failed\n"); michael@0: exit(1); michael@0: } michael@0: #ifdef SYMBIAN michael@0: loopcount = 0; michael@0: #endif michael@0: acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); michael@0: if (NULL == acceptSock) { michael@0: fprintf(stderr, "PR_Accept failed\n"); michael@0: exit(1); michael@0: } michael@0: osfd = PR_FileDesc2NativeHandle(acceptSock); michael@0: while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { michael@0: /* empty loop body */ michael@0: #ifdef SYMBIAN michael@0: if (loopcount++>64) break; michael@0: #endif michael@0: } michael@0: if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { michael@0: fprintf(stderr, "write failed\n"); michael@0: exit(1); michael@0: } michael@0: printf("calling PR_Write with a zero-length buffer\n"); michael@0: fflush(stdout); michael@0: nbytes = PR_Write(acceptSock, buf, 0); michael@0: if (nbytes != 0) { michael@0: fprintf(stderr, "PR_Write should return 0 but returns %d\n", nbytes); michael@0: exit(1); michael@0: } michael@0: if (PR_Close(acceptSock) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_JoinThread(clientThread) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: /* michael@0: * Finally test PR_Send. michael@0: */ michael@0: clientThread = PR_CreateThread(PR_USER_THREAD, michael@0: ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), michael@0: PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: if (NULL == clientThread) { michael@0: fprintf(stderr, "PR_CreateThread failed\n"); michael@0: exit(1); michael@0: } michael@0: #ifdef SYMBIAN michael@0: loopcount = 0; michael@0: #endif michael@0: acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); michael@0: if (NULL == acceptSock) { michael@0: fprintf(stderr, "PR_Accept failed\n"); michael@0: exit(1); michael@0: } michael@0: osfd = PR_FileDesc2NativeHandle(acceptSock); michael@0: while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { michael@0: /* empty loop body */ michael@0: #ifdef SYMBIAN michael@0: if (loopcount++>64) break; michael@0: #endif michael@0: } michael@0: if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { michael@0: fprintf(stderr, "write failed\n"); michael@0: exit(1); michael@0: } michael@0: printf("calling PR_Send with a zero-length buffer\n"); michael@0: fflush(stdout); michael@0: nbytes = PR_Send(acceptSock, buf, 0, 0, PR_INTERVAL_NO_TIMEOUT); michael@0: if (nbytes != 0) { michael@0: fprintf(stderr, "PR_Send should return 0 but returns %d\n", nbytes); michael@0: exit(1); michael@0: } michael@0: if (PR_Close(acceptSock) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_JoinThread(clientThread) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: if (PR_Close(listenSock) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: printf("PASS\n"); michael@0: return 0; michael@0: } michael@0: michael@0: #endif /* XP_UNIX */