Back to index

lightning-sunbird  0.9+nobinonly
poll_to.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_to.c
00041 **
00042 ** Description: This program tests PR_Poll with sockets.
00043 **              Timeout operation is tested
00044 **
00045 ** Modification History:
00046 ** 14-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 
00067 #ifndef XP_MAC
00068 #include "private/pprio.h"
00069 #else
00070 #include "pprio.h"
00071 #endif
00072 
00073 #include <stdio.h>
00074 #include <string.h>
00075 #include <stdlib.h>
00076 PRIntn failed_already=0;
00077 PRIntn debug_mode;
00078 
00079 int main(int argc, char **argv)
00080 {
00081     PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
00082     PRUint16 listenPort1, listenPort2;
00083     PRNetAddr addr;
00084     char buf[128];
00085     PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
00086     PRIntn npds;
00087     PRInt32 retVal;
00088 
00089        /* The command line argument: -d is used to determine if the test is being run
00090        in debug mode. The regress tool requires only one line output:PASS or FAIL.
00091        All of the printfs associated with this test has been handled with a if (debug_mode)
00092        test.
00093        Usage: test_name -d
00094        */
00095        PLOptStatus os;
00096        PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
00097        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00098     {
00099               if (PL_OPT_BAD == os) continue;
00100         switch (opt->option)
00101         {
00102         case 'd':  /* debug mode */
00103                      debug_mode = 1;
00104             break;
00105          default:
00106             break;
00107         }
00108     }
00109        PL_DestroyOptState(opt);
00110 
00111  /* main test */
00112        
00113     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00114     PR_STDIO_INIT();
00115 
00116     if (debug_mode) {
00117               printf("This program tests PR_Poll with sockets.\n");
00118               printf("Timeout is tested.\n\n");
00119        }
00120 
00121     /* Create two listening sockets */
00122     if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
00123        fprintf(stderr, "Can't create a new TCP socket\n");
00124        if (!debug_mode)  failed_already=1;
00125        goto exit_now;
00126     }
00127     memset(&addr, 0, sizeof(addr));
00128     addr.inet.family = PR_AF_INET;
00129     addr.inet.ip = PR_htonl(PR_INADDR_ANY);
00130     addr.inet.port = PR_htons(0);
00131     if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
00132        fprintf(stderr, "Can't bind socket\n");
00133        if (!debug_mode)  failed_already=1;
00134        goto exit_now;
00135     }
00136     if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
00137        fprintf(stderr, "PR_GetSockName failed\n");
00138        if (!debug_mode)  failed_already=1;
00139        goto exit_now;
00140     }
00141     listenPort1 = PR_ntohs(addr.inet.port);
00142     if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
00143        fprintf(stderr, "Can't listen on a socket\n");
00144        if (!debug_mode)  failed_already=1;
00145        goto exit_now;
00146     }
00147 
00148     if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
00149        fprintf(stderr, "Can't create a new TCP socket\n");
00150        if (!debug_mode)  failed_already=1;       
00151        goto exit_now;
00152     }
00153     addr.inet.family = PR_AF_INET;
00154     addr.inet.ip = PR_htonl(PR_INADDR_ANY);
00155     addr.inet.port = PR_htons(0);
00156     if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
00157        fprintf(stderr, "Can't bind socket\n");
00158        if (!debug_mode)  failed_already=1;       
00159        goto exit_now;
00160     }
00161     if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
00162        fprintf(stderr, "PR_GetSockName failed\n");
00163        if (!debug_mode)  failed_already=1;       
00164        goto exit_now;
00165     }
00166     listenPort2 = PR_ntohs(addr.inet.port);
00167     if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
00168        fprintf(stderr, "Can't listen on a socket\n");
00169        if (!debug_mode)  failed_already=1;       
00170        goto exit_now;
00171     }
00172     PR_snprintf(buf, sizeof(buf),
00173            "The server thread is listening on ports %hu and %hu\n\n",
00174            listenPort1, listenPort2);
00175     if (debug_mode) printf("%s", buf);
00176 
00177     /* Set up the poll descriptor array */
00178     pds = pds0;
00179     other_pds = pds1;
00180     memset(pds, 0, sizeof(pds));
00181     pds[0].fd = listenSock1;
00182     pds[0].in_flags = PR_POLL_READ;
00183     pds[1].fd = listenSock2;
00184     pds[1].in_flags = PR_POLL_READ;
00185     npds = 2;
00186 
00187     /* Testing timeout */
00188     if (debug_mode) printf("PR_Poll should time out in 5 seconds\n");
00189     retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5));
00190     if (retVal != 0) {
00191        PR_snprintf(buf, sizeof(buf),
00192               "PR_Poll should time out and return 0, but it returns %ld\n",
00193               retVal);
00194        fprintf(stderr, "%s", buf);
00195        if (!debug_mode)  failed_already=1;       
00196        goto exit_now;
00197     }
00198     if (debug_mode) printf("PR_Poll timed out.  Test passed.\n\n");
00199 
00200 exit_now:
00201 
00202     if (listenSock1) {
00203         PR_Close(listenSock1);
00204     }
00205     if (listenSock2) {
00206         PR_Close(listenSock2);
00207     }
00208 
00209     PR_Cleanup();
00210 
00211        if(failed_already)   
00212               return 1;
00213        else
00214               return 0;
00215 
00216 }