home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / poll_er.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.5 KB  |  215 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. **
  21. ** Name: prpoll_err.c
  22. **
  23. ** Description: This program tests PR_Poll with sockets.
  24. **              error reporting operation is tested
  25. **
  26. ** Modification History:
  27. ** 19-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.  
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <stdlib.h>
  47.  
  48. PRIntn failed_already=0;
  49. PRIntn debug_mode;
  50.  
  51. static void
  52. ClientThreadFunc(void *arg)
  53. {
  54.     PRFileDesc *badFD = (PRFileDesc *) arg;
  55.     /*
  56.      * Make the fd invalid
  57.      */
  58. #if defined(XP_UNIX)
  59.     close(PR_FileDesc2NativeHandle(badFD));
  60. #elif defined(XP_OS2)
  61.     soclose(PR_FileDesc2NativeHandle(badFD));
  62. #elif defined(WIN32) || defined(WIN16)
  63.     closesocket(PR_FileDesc2NativeHandle(badFD));
  64. #elif defined(XP_MAC)
  65.     _PR_MD_CLOSE_SOCKET(PR_FileDesc2NativeHandle(badFD));
  66. #else
  67. #error "Unknown architecture"
  68. #endif
  69. }
  70.  
  71. int main(int argc, char **argv)
  72. {
  73.     PRFileDesc *listenSock1, *listenSock2;
  74.     PRFileDesc *badFD;
  75.     PRUint16 listenPort1, listenPort2;
  76.     PRNetAddr addr;
  77.     char buf[128];
  78.     PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
  79.     PRIntn npds;
  80.     PRInt32 retVal;
  81.  
  82.     /* The command line argument: -d is used to determine if the test is being run
  83.     in debug mode. The regress tool requires only one line output:PASS or FAIL.
  84.     All of the printfs associated with this test has been handled with a if (debug_mode)
  85.     test.
  86.     Usage: test_name -d
  87.     */
  88.     PLOptStatus os;
  89.     PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  90.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  91.     {
  92.         if (PL_OPT_BAD == os) continue;
  93.         switch (opt->option)
  94.         {
  95.         case 'd':  /* debug mode */
  96.             debug_mode = 1;
  97.             break;
  98.          default:
  99.             break;
  100.         }
  101.     }
  102.     PL_DestroyOptState(opt);
  103.  
  104.  /* main test */
  105.     
  106.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  107.     PR_STDIO_INIT();
  108.  
  109.     if (debug_mode) {
  110.         printf("This program tests PR_Poll with sockets.\n");
  111.         printf("error reporting is  tested.\n\n");
  112.     }
  113.  
  114.     /* Create two listening sockets */
  115.     if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
  116.     fprintf(stderr, "Can't create a new TCP socket\n");
  117.     failed_already=1;
  118.     goto exit_now;
  119.     }
  120.     addr.inet.family = AF_INET;
  121.     addr.inet.ip = PR_htonl(INADDR_ANY);
  122.     addr.inet.port = PR_htons(0);
  123.     if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
  124.     fprintf(stderr, "Can't bind socket\n");
  125.     failed_already=1;
  126.     goto exit_now;
  127.     }
  128.     if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
  129.     fprintf(stderr, "PR_GetSockName failed\n");
  130.     failed_already=1;
  131.     goto exit_now;
  132.     }
  133.     listenPort1 = PR_ntohs(addr.inet.port);
  134.     if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
  135.     fprintf(stderr, "Can't listen on a socket\n");
  136.     failed_already=1;
  137.     goto exit_now;
  138.     }
  139.  
  140.     if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
  141.     fprintf(stderr, "Can't create a new TCP socket\n");
  142.     failed_already=1;    
  143.     goto exit_now;
  144.     }
  145.     addr.inet.family = AF_INET;
  146.     addr.inet.ip = PR_htonl(INADDR_ANY);
  147.     addr.inet.port = PR_htons(0);
  148.     if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
  149.     fprintf(stderr, "Can't bind socket\n");
  150.     failed_already=1;    
  151.     goto exit_now;
  152.     }
  153.     if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
  154.     fprintf(stderr, "PR_GetSockName failed\n");
  155.     failed_already=1;    
  156.     goto exit_now;
  157.     }
  158.     listenPort2 = PR_ntohs(addr.inet.port);
  159.     if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
  160.     fprintf(stderr, "Can't listen on a socket\n");
  161.     failed_already=1;    
  162.     goto exit_now;
  163.     }
  164.     PR_snprintf(buf, sizeof(buf),
  165.         "The server thread is listening on ports %hu and %hu\n\n",
  166.         listenPort1, listenPort2);
  167.     if (debug_mode) printf("%s", buf);
  168.  
  169.     /* Set up the poll descriptor array */
  170.     pds = pds0;
  171.     other_pds = pds1;
  172.     memset(pds, 0, sizeof(pds));
  173.     pds[0].fd = listenSock1;
  174.     pds[0].in_flags = PR_POLL_READ;
  175.     pds[1].fd = listenSock2;
  176.     pds[1].in_flags = PR_POLL_READ;
  177.     npds = 2;
  178.  
  179.  
  180.     /* Testing bad fd */
  181.     if (debug_mode) printf("PR_Poll should detect a bad file descriptor\n");
  182.     if ((badFD = PR_NewTCPSocket()) == NULL) {
  183.     fprintf(stderr, "Can't create a TCP socket\n");
  184.     goto exit_now;
  185.     }
  186.  
  187.     pds[2].fd = badFD;
  188.     pds[2].in_flags = PR_POLL_READ;
  189.     npds = 3;
  190.  
  191.     if (PR_CreateThread(PR_USER_THREAD, ClientThreadFunc,
  192.             badFD, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
  193.             PR_UNJOINABLE_THREAD, 0) == NULL) {
  194.         fprintf(stderr, "cannot create thread\n");
  195.         exit(1);
  196.     }
  197.  
  198.     retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
  199.     if (retVal != 1 || (unsigned short) pds[2].out_flags != PR_POLL_NVAL) {
  200.     fprintf(stderr, "Failed to detect the bad fd: "
  201.         "PR_Poll returns %d, out_flags is 0x%hx\n",
  202.         retVal, pds[2].out_flags);
  203.     failed_already=1;    
  204.     goto exit_now;
  205.     }
  206.     if (debug_mode) printf("PR_Poll detected the bad fd.  Test passed.\n\n");
  207.     PR_Cleanup();
  208.     goto exit_now;
  209. exit_now:
  210.     if(failed_already)    
  211.         return 1;
  212.     else
  213.         return 0;
  214. }
  215.