Back to index

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