Back to index

lightning-sunbird  0.9+nobinonly
selct_er.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_err.c
00042 **
00043 ** Description: tests PR_Select with sockets Error condition 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 #ifdef XP_BEOS
00056 #include <stdio.h>
00057 int main()
00058 {
00059     printf( "This test is not ported to the BeOS\n" );
00060     return 0;
00061 }
00062 #else
00063 
00064 /***********************************************************************
00065 ** Includes
00066 ***********************************************************************/
00067 /* Used to get the command line option */
00068 #include "plgetopt.h"
00069 
00070 #include "primpl.h"
00071 #include "pprio.h"
00072 #include "prnetdb.h"
00073 
00074 #include <stdio.h>
00075 #include <string.h>
00076 #include <stdlib.h>
00077 
00078 
00079 PRIntn failed_already=0;
00080 PRIntn debug_mode;
00081 
00082 int main(int argc, char **argv)
00083 {
00084     PRFileDesc *listenSock1, *listenSock2;
00085     PRFileDesc *badFD;
00086     PRUint16 listenPort1, listenPort2;
00087     PRNetAddr addr;
00088     PR_fd_set readFdSet;
00089     char buf[128];
00090     PRInt32 retVal;
00091 
00092        /* The command line argument: -d is used to determine if the test is being run
00093        in debug mode. The regress tool requires only one line output:PASS or FAIL.
00094        All of the printfs associated with this test has been handled with a if (debug_mode)
00095        test.
00096        Usage: test_name -d
00097        */
00098        PLOptStatus os;
00099        PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
00100        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00101     {
00102               if (PL_OPT_BAD == os) continue;
00103         switch (opt->option)
00104         {
00105         case 'd':  /* debug mode */
00106                      debug_mode = 1;
00107             break;
00108          default:
00109             break;
00110         }
00111     }
00112        PL_DestroyOptState(opt);
00113 
00114  /* main test */
00115        
00116     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00117     PR_STDIO_INIT();
00118 
00119     if (debug_mode) {
00120               printf("This program tests PR_Select with sockets.  Error\n");
00121               printf("reporting operations are tested.\n\n");
00122        }
00123 
00124     /* Create two listening sockets */
00125     if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
00126        fprintf(stderr, "Can't create a new TCP socket\n");
00127        failed_already=1;
00128        goto exit_now;
00129     }
00130     addr.inet.family = AF_INET;
00131     addr.inet.ip = PR_htonl(INADDR_ANY);
00132     addr.inet.port = PR_htons(0);
00133     if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
00134        fprintf(stderr, "Can't bind socket\n");
00135        failed_already=1;
00136        goto exit_now;
00137     }
00138     if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
00139        fprintf(stderr, "PR_GetSockName failed\n");
00140        failed_already=1;
00141        goto exit_now;
00142     }
00143     listenPort1 = PR_ntohs(addr.inet.port);
00144     if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
00145        fprintf(stderr, "Can't listen on a socket\n");
00146        failed_already=1;
00147        goto exit_now;
00148     }
00149 
00150     if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
00151        fprintf(stderr, "Can't create a new TCP socket\n");
00152        failed_already=1;
00153        goto exit_now;
00154     }
00155     addr.inet.family = AF_INET;
00156     addr.inet.ip = PR_htonl(INADDR_ANY);
00157     addr.inet.port = PR_htons(0);
00158     if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
00159        fprintf(stderr, "Can't bind socket\n");
00160        failed_already=1;
00161        goto exit_now;
00162     }
00163     if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
00164        fprintf(stderr, "PR_GetSockName failed\n");
00165        failed_already=1;
00166        goto exit_now;
00167     }
00168     listenPort2 = PR_ntohs(addr.inet.port);
00169     if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
00170        fprintf(stderr, "Can't listen on a socket\n");
00171        failed_already=1;
00172        goto exit_now;
00173     }
00174     PR_snprintf(buf, sizeof(buf),
00175            "The server thread is listening on ports %hu and %hu\n\n",
00176            listenPort1, listenPort2);
00177     if (debug_mode) printf("%s", buf);
00178 
00179     /* Set up the fd set */
00180     PR_FD_ZERO(&readFdSet);
00181     PR_FD_SET(listenSock1, &readFdSet);
00182     PR_FD_SET(listenSock2, &readFdSet);
00183 
00184 
00185     /* Testing bad fd */
00186     if (debug_mode) printf("PR_Select should detect a bad file descriptor\n");
00187     if ((badFD = PR_NewTCPSocket()) == NULL) {
00188        fprintf(stderr, "Can't create a TCP socket\n");
00189        failed_already=1;
00190        goto exit_now;
00191     }
00192 
00193     PR_FD_SET(badFD, &readFdSet);
00194     /*
00195      * Make the fd invalid
00196      */
00197 #if defined(XP_UNIX)
00198     close(PR_FileDesc2NativeHandle(badFD));
00199 #elif defined(XP_OS2)
00200     soclose(PR_FileDesc2NativeHandle(badFD));
00201 #elif defined(WIN32) || defined(WIN16)
00202     closesocket(PR_FileDesc2NativeHandle(badFD));
00203 #elif defined(XP_MAC)
00204     _PR_MD_CLOSE_SOCKET(PR_FileDesc2NativeHandle(badFD));
00205 #else
00206 #error "Unknown architecture"
00207 #endif
00208 
00209     retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
00210            PR_INTERVAL_NO_TIMEOUT);
00211     if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) {
00212        fprintf(stderr, "Failed to detect the bad fd: "
00213               "PR_Select returns %d\n", retVal);
00214        if (retVal == -1) {
00215            fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(),
00216                   PR_GetOSError());
00217               failed_already=1;
00218        }
00219        goto exit_now;
00220     }
00221     if (debug_mode) printf("PR_Select detected a bad fd.  Test passed.\n\n");
00222        PR_FD_CLR(badFD, &readFdSet);
00223 
00224        PR_Cleanup();
00225        goto exit_now;
00226 exit_now:
00227        if(failed_already)   
00228               return 1;
00229        else
00230               return 0;
00231 
00232 }
00233 
00234 #endif /* XP_BEOS */