Back to index

lightning-sunbird  0.9+nobinonly
poll_nm.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /***********************************************************************
00039 **
00040 ** Name: prpoll_norm.c
00041 **
00042 ** Description: This program tests PR_Poll with sockets.
00043 **              Normal operation are tested
00044 **
00045 ** Modification History:
00046 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
00047 **              The debug mode will print all of the printfs associated with this test.
00048 **                    The regress mode will be the default mode. Since the regress tool limits
00049 **           the output to a one line status:PASS or FAIL,all of the printf statements
00050 **                    have been handled with an if (debug_mode) statement.
00051 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
00052 **                   recognize the return code from tha main program.
00053 ***********************************************************************/
00054 
00055 /***********************************************************************
00056 ** Includes
00057 ***********************************************************************/
00058 /* Used to get the command line option */
00059 #include "plgetopt.h"
00060 
00061 #include "prinit.h"
00062 #include "prio.h"
00063 #include "prlog.h"
00064 #include "prprf.h"
00065 #include "prnetdb.h"
00066 #ifndef XP_MAC
00067 #include "obsolete/probslet.h"
00068 #else
00069 #include "probslet.h"
00070 #endif
00071 
00072 #ifndef XP_MAC
00073 #include "private/pprio.h"
00074 #else
00075 #include "pprio.h"
00076 #endif
00077 
00078 #include <stdio.h>
00079 #include <string.h>
00080 #include <stdlib.h>
00081 PRIntn failed_already=0;
00082 PRIntn debug_mode;
00083 
00084 #define NUM_ITERATIONS 5
00085 
00086 #ifdef XP_MAC
00087 int fprintf(FILE *stream, const char *fmt, ...)
00088 {
00089 PR_LogPrint(fmt);
00090 return 0;
00091 }
00092 #define printf PR_LogPrint
00093 extern void SetupMacPrintfLog(char *logFile);
00094 #endif
00095 
00096 static void PR_CALLBACK
00097 clientThreadFunc(void *arg)
00098 {
00099     PRUintn port = (PRUintn) arg;
00100     PRFileDesc *sock;
00101     PRNetAddr addr;
00102     char buf[128];
00103     int i;
00104     PRStatus sts;
00105     PRInt32 n;
00106 
00107     addr.inet.family = PR_AF_INET;
00108     addr.inet.port = PR_htons((PRUint16)port);
00109     addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
00110     memset(buf, 0, sizeof(buf));
00111     PR_snprintf(buf, sizeof(buf), "%hu", port);
00112 
00113     for (i = 0; i < NUM_ITERATIONS; i++) {
00114        sock = PR_NewTCPSocket();
00115        PR_ASSERT(sock != NULL);
00116        
00117     sts = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
00118        PR_ASSERT(sts == PR_SUCCESS);
00119 
00120        n = PR_Write(sock, buf, sizeof(buf));
00121        PR_ASSERT(n >= 0);
00122 
00123        sts = PR_Close(sock);
00124        PR_ASSERT(sts == PR_SUCCESS);
00125     }
00126 }
00127 
00128 int main(int argc, char **argv)
00129 {
00130     PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
00131     PRUint16 listenPort1, listenPort2;
00132     PRNetAddr addr;
00133     char buf[128];
00134     PRThread *clientThread;
00135     PRPollDesc pds0[20], pds1[20], *pds, *other_pds;
00136     PRIntn npds;
00137     PRInt32 retVal;
00138     PRIntn i, j;
00139     PRSocketOptionData optval;
00140 
00141        /* The command line argument: -d is used to determine if the test is being run
00142        in debug mode. The regress tool requires only one line output:PASS or FAIL.
00143        All of the printfs associated with this test has been handled with a if (debug_mode)
00144        test.
00145        Usage: test_name -d
00146        */
00147        PLOptStatus os;
00148        PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
00149        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00150     {
00151               if (PL_OPT_BAD == os) continue;
00152         switch (opt->option)
00153         {
00154         case 'd':  /* debug mode */
00155                      debug_mode = 1;
00156             break;
00157          default:
00158             break;
00159         }
00160     }
00161        PL_DestroyOptState(opt);
00162 
00163  /* main test */
00164        
00165     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00166     PR_STDIO_INIT();
00167 
00168 #ifdef XP_MAC
00169        debug_mode = 1;
00170        SetupMacPrintfLog("poll_nm.log");
00171 #endif
00172 
00173     if (debug_mode) {
00174               printf("This program tests PR_Poll with sockets.\n");
00175               printf("Normal operation are tested.\n\n");
00176        }
00177 
00178     /* Create two listening sockets */
00179     if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
00180        fprintf(stderr, "Can't create a new TCP socket\n");
00181        failed_already=1;
00182        goto exit_now;
00183     }
00184     memset(&addr, 0, sizeof(addr));
00185     addr.inet.family = PR_AF_INET;
00186     addr.inet.ip = PR_htonl(PR_INADDR_ANY);
00187     addr.inet.port = PR_htons(0);
00188     if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
00189        fprintf(stderr, "Can't bind socket\n");
00190        failed_already=1;
00191        goto exit_now;
00192     }
00193     if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
00194        fprintf(stderr, "PR_GetSockName failed\n");
00195        failed_already=1;
00196        goto exit_now;
00197     }
00198     listenPort1 = PR_ntohs(addr.inet.port);
00199     optval.option = PR_SockOpt_Nonblocking;
00200     optval.value.non_blocking = PR_TRUE;
00201     PR_SetSocketOption(listenSock1, &optval);
00202     if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
00203        fprintf(stderr, "Can't listen on a socket\n");
00204        failed_already=1;
00205        goto exit_now;
00206     }
00207 
00208     if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
00209        fprintf(stderr, "Can't create a new TCP socket\n");
00210        failed_already=1;    
00211        goto exit_now;
00212     }
00213     addr.inet.family = PR_AF_INET;
00214     addr.inet.ip = PR_htonl(PR_INADDR_ANY);
00215     addr.inet.port = PR_htons(0);
00216     if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
00217        fprintf(stderr, "Can't bind socket\n");
00218        failed_already=1;    
00219        goto exit_now;
00220     }
00221     if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
00222        fprintf(stderr, "PR_GetSockName failed\n");
00223        failed_already=1;    
00224        goto exit_now;
00225     }
00226     listenPort2 = PR_ntohs(addr.inet.port);
00227     PR_SetSocketOption(listenSock2, &optval);
00228     if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
00229        fprintf(stderr, "Can't listen on a socket\n");
00230        failed_already=1;    
00231        goto exit_now;
00232     }
00233     PR_snprintf(buf, sizeof(buf),
00234            "The server thread is listening on ports %hu and %hu\n\n",
00235            listenPort1, listenPort2);
00236     if (debug_mode) printf("%s", buf);
00237 
00238     /* Set up the poll descriptor array */
00239     pds = pds0;
00240     other_pds = pds1;
00241     memset(pds, 0, sizeof(pds));
00242     pds[0].fd = listenSock1;
00243     pds[0].in_flags = PR_POLL_READ;
00244     pds[1].fd = listenSock2;
00245     pds[1].in_flags = PR_POLL_READ;
00246     /* Add some unused entries to test if they are ignored by PR_Poll() */
00247     memset(&pds[2], 0, sizeof(pds[2]));
00248     memset(&pds[3], 0, sizeof(pds[3]));
00249     memset(&pds[4], 0, sizeof(pds[4]));
00250     npds = 5;
00251 
00252     clientThread = PR_CreateThread(PR_USER_THREAD,
00253            clientThreadFunc, (void *) listenPort1,
00254            PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
00255            PR_UNJOINABLE_THREAD, 0);
00256     if (clientThread == NULL) {
00257        fprintf(stderr, "can't create thread\n");
00258        failed_already=1;    
00259        goto exit_now;
00260     }
00261 
00262     clientThread = PR_CreateThread(PR_USER_THREAD,
00263            clientThreadFunc, (void *) listenPort2,
00264            PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
00265            PR_UNJOINABLE_THREAD, 0);
00266     if (clientThread == NULL) {
00267        fprintf(stderr, "can't create thread\n");
00268        failed_already=1;           
00269        goto exit_now;
00270     }
00271 
00272     if (debug_mode) {
00273               printf("Two client threads are created.  Each of them will\n");
00274               printf("send data to one of the two ports the server is listening on.\n");
00275               printf("The data they send is the port number.  Each of them send\n");
00276               printf("the data five times, so you should see ten lines below,\n");
00277               printf("interleaved in an arbitrary order.\n");
00278        }
00279 
00280     /* two clients, three events per iteration: accept, read, close */
00281     i = 0;
00282     while (i < 2 * 3 * NUM_ITERATIONS) {
00283        PRPollDesc *tmp;
00284        int nextIndex;
00285        int nEvents = 0;
00286 
00287        retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
00288        PR_ASSERT(retVal != 0);  /* no timeout */
00289        if (retVal == -1) {
00290            fprintf(stderr, "PR_Poll failed\n");
00291               failed_already=1;                  
00292            goto exit_now;
00293        }
00294 
00295        nextIndex = 2;
00296        /* the two listening sockets */
00297        for (j = 0; j < 2; j++) {
00298            other_pds[j] = pds[j];
00299            PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
00300                   && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
00301            if (pds[j].out_flags & PR_POLL_READ) {
00302               PRFileDesc *sock;
00303 
00304               nEvents++;
00305               sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT);
00306               if (sock == NULL) {
00307                   fprintf(stderr, "PR_Accept() failed\n");
00308                      failed_already=1;    
00309                   goto exit_now;
00310               }
00311               other_pds[nextIndex].fd = sock;
00312               other_pds[nextIndex].in_flags = PR_POLL_READ;
00313               nextIndex++;
00314            } else if (pds[j].out_flags & PR_POLL_ERR) {
00315               fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
00316               failed_already=1;    
00317               goto exit_now;
00318            } else if (pds[j].out_flags & PR_POLL_NVAL) {
00319               fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n",
00320                      PR_FileDesc2NativeHandle(pds[j].fd));
00321               failed_already=1;    
00322               goto exit_now;
00323            }
00324        }
00325 
00326        for (j = 2; j < npds; j++) {
00327             if (NULL == pds[j].fd) {
00328                 /*
00329                  * Keep the unused entries in the poll descriptor array
00330                  * for testing purposes.
00331                  */
00332                 other_pds[nextIndex] = pds[j];
00333                 nextIndex++;
00334                 continue;
00335             }
00336 
00337            PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
00338                   && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
00339            if (pds[j].out_flags & PR_POLL_READ) {
00340                 PRInt32 nAvail;
00341               PRInt32 nRead;
00342 
00343               nEvents++;
00344                 nAvail = PR_Available(pds[j].fd);
00345               nRead = PR_Read(pds[j].fd, buf, sizeof(buf));
00346                 PR_ASSERT(nAvail == nRead);
00347               if (nRead == -1) {
00348                   fprintf(stderr, "PR_Read() failed\n");
00349                      failed_already=1;    
00350                   goto exit_now;
00351                 } else if (nRead == 0) {
00352                     PR_Close(pds[j].fd);
00353                     continue;
00354                 } else {
00355                     /* Just to be safe */
00356                     buf[127] = '\0';
00357                     if (debug_mode) printf("The server received \"%s\" from a client\n", buf);
00358                 }
00359            } else if (pds[j].out_flags & PR_POLL_ERR) {
00360               fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
00361               failed_already=1;                  
00362               goto exit_now;
00363            } else if (pds[j].out_flags & PR_POLL_NVAL) {
00364               fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n");
00365               failed_already=1;                  
00366               goto exit_now;
00367            }
00368             other_pds[nextIndex] = pds[j];
00369             nextIndex++;
00370        }
00371 
00372        PR_ASSERT(retVal == nEvents);
00373        /* swap */
00374        tmp = pds;
00375        pds = other_pds;
00376        other_pds = tmp;
00377        npds = nextIndex;
00378        i += nEvents;
00379     }
00380 
00381     if (debug_mode) printf("Tests passed\n");
00382 
00383 exit_now:
00384 
00385     if (listenSock1) {
00386         PR_Close(listenSock1);
00387     }
00388     if (listenSock2) {
00389         PR_Close(listenSock2);
00390     }
00391 
00392     PR_Cleanup();
00393        
00394        if(failed_already)   
00395               return 1;
00396        else
00397               return 0;
00398 
00399 }