nsprpub/pr/tests/zerolen.c

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

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

mercurial