home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / selct_er.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.2 KB  |  205 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /***********************************************************************
  20. **  1997 - Netscape Communications Corporation
  21. **
  22. ** Name: prselect_err.c
  23. **
  24. ** Description: tests PR_Select with sockets Error condition functions.
  25. **
  26. ** Modification History:
  27. ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  28. **             The debug mode will print all of the printfs associated with this test.
  29. **             The regress mode will be the default mode. Since the regress tool limits
  30. **           the output to a one line status:PASS or FAIL,all of the printf statements
  31. **             have been handled with an if (debug_mode) statement.
  32. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  33. **            recognize the return code from tha main program.
  34. ***********************************************************************/
  35.  
  36. /***********************************************************************
  37. ** Includes
  38. ***********************************************************************/
  39. /* Used to get the command line option */
  40. #include "plgetopt.h"
  41.  
  42. #include "primpl.h"
  43. #include "pprio.h"
  44. #include "prnetdb.h"
  45.  
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <stdlib.h>
  49.  
  50.  
  51. PRIntn failed_already=0;
  52. PRIntn debug_mode;
  53.  
  54. int main(int argc, char **argv)
  55. {
  56.     PRFileDesc *listenSock1, *listenSock2;
  57.     PRFileDesc *badFD;
  58.     PRUint16 listenPort1, listenPort2;
  59.     PRNetAddr addr;
  60.     PR_fd_set readFdSet;
  61.     char buf[128];
  62.     PRInt32 retVal;
  63.  
  64.     /* The command line argument: -d is used to determine if the test is being run
  65.     in debug mode. The regress tool requires only one line output:PASS or FAIL.
  66.     All of the printfs associated with this test has been handled with a if (debug_mode)
  67.     test.
  68.     Usage: test_name -d
  69.     */
  70.     PLOptStatus os;
  71.     PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  72.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  73.     {
  74.         if (PL_OPT_BAD == os) continue;
  75.         switch (opt->option)
  76.         {
  77.         case 'd':  /* debug mode */
  78.             debug_mode = 1;
  79.             break;
  80.          default:
  81.             break;
  82.         }
  83.     }
  84.     PL_DestroyOptState(opt);
  85.  
  86.  /* main test */
  87.     
  88.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  89.     PR_STDIO_INIT();
  90.  
  91.     if (debug_mode) {
  92.         printf("This program tests PR_Select with sockets.  Error\n");
  93.         printf("reporting operations are tested.\n\n");
  94.     }
  95.  
  96.     /* Create two listening sockets */
  97.     if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
  98.     fprintf(stderr, "Can't create a new TCP socket\n");
  99.     failed_already=1;
  100.     goto exit_now;
  101.     }
  102.     addr.inet.family = AF_INET;
  103.     addr.inet.ip = PR_htonl(INADDR_ANY);
  104.     addr.inet.port = PR_htons(0);
  105.     if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
  106.     fprintf(stderr, "Can't bind socket\n");
  107.     failed_already=1;
  108.     goto exit_now;
  109.     }
  110.     if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
  111.     fprintf(stderr, "PR_GetSockName failed\n");
  112.     failed_already=1;
  113.     goto exit_now;
  114.     }
  115.     listenPort1 = PR_ntohs(addr.inet.port);
  116.     if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
  117.     fprintf(stderr, "Can't listen on a socket\n");
  118.     failed_already=1;
  119.     goto exit_now;
  120.     }
  121.  
  122.     if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
  123.     fprintf(stderr, "Can't create a new TCP socket\n");
  124.     failed_already=1;
  125.     goto exit_now;
  126.     }
  127.     addr.inet.family = AF_INET;
  128.     addr.inet.ip = PR_htonl(INADDR_ANY);
  129.     addr.inet.port = PR_htons(0);
  130.     if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
  131.     fprintf(stderr, "Can't bind socket\n");
  132.     failed_already=1;
  133.     goto exit_now;
  134.     }
  135.     if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
  136.     fprintf(stderr, "PR_GetSockName failed\n");
  137.     failed_already=1;
  138.     goto exit_now;
  139.     }
  140.     listenPort2 = PR_ntohs(addr.inet.port);
  141.     if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
  142.     fprintf(stderr, "Can't listen on a socket\n");
  143.     failed_already=1;
  144.     goto exit_now;
  145.     }
  146.     PR_snprintf(buf, sizeof(buf),
  147.         "The server thread is listening on ports %hu and %hu\n\n",
  148.         listenPort1, listenPort2);
  149.     if (debug_mode) printf("%s", buf);
  150.  
  151.     /* Set up the fd set */
  152.     PR_FD_ZERO(&readFdSet);
  153.     PR_FD_SET(listenSock1, &readFdSet);
  154.     PR_FD_SET(listenSock2, &readFdSet);
  155.  
  156.  
  157.     /* Testing bad fd */
  158.     if (debug_mode) printf("PR_Select should detect a bad file descriptor\n");
  159.     if ((badFD = PR_NewTCPSocket()) == NULL) {
  160.     fprintf(stderr, "Can't create a TCP socket\n");
  161.     failed_already=1;
  162.     goto exit_now;
  163.     }
  164.  
  165.     PR_FD_SET(badFD, &readFdSet);
  166.     /*
  167.      * Make the fd invalid
  168.      */
  169. #if defined(XP_UNIX)
  170.     close(PR_FileDesc2NativeHandle(badFD));
  171. #elif defined(XP_OS2)
  172.     soclose(PR_FileDesc2NativeHandle(badFD));
  173. #elif defined(WIN32) || defined(WIN16)
  174.     closesocket(PR_FileDesc2NativeHandle(badFD));
  175. #elif defined(XP_MAC)
  176.     _PR_MD_CLOSE_SOCKET(PR_FileDesc2NativeHandle(badFD));
  177. #else
  178. #error "Unknown architecture"
  179. #endif
  180.  
  181.     retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
  182.         PR_INTERVAL_NO_TIMEOUT);
  183.     if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) {
  184.     fprintf(stderr, "Failed to detect the bad fd: "
  185.         "PR_Select returns %d\n", retVal);
  186.     if (retVal == -1) {
  187.         fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(),
  188.             PR_GetOSError());
  189.         failed_already=1;
  190.     }
  191.     goto exit_now;
  192.     }
  193.     if (debug_mode) printf("PR_Select detected a bad fd.  Test passed.\n\n");
  194.     PR_FD_CLR(badFD, &readFdSet);
  195.  
  196.     PR_Cleanup();
  197.     goto exit_now;
  198. exit_now:
  199.     if(failed_already)    
  200.         return 1;
  201.     else
  202.         return 0;
  203.  
  204. }
  205.