Back to index

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