home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / poll_to.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.8 KB  |  189 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_to.c
  22. **
  23. ** Description: This program tests PR_Poll with sockets.
  24. **              Timeout operation is tested
  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 "prinit.h"
  43. #include "prio.h"
  44. #include "prlog.h"
  45. #include "prprf.h"
  46. #include "prnetdb.h"
  47.  
  48. #ifndef XP_MAC
  49. #include "private/pprio.h"
  50. #else
  51. #include "pprio.h"
  52. #endif
  53.  
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include <stdlib.h>
  57. PRIntn failed_already=0;
  58. PRIntn debug_mode;
  59.  
  60. int main(int argc, char **argv)
  61. {
  62.     PRFileDesc *listenSock1, *listenSock2;
  63.     PRUint16 listenPort1, listenPort2;
  64.     PRNetAddr addr;
  65.     char buf[128];
  66.     PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
  67.     PRIntn npds;
  68.     PRInt32 retVal;
  69.  
  70.     /* The command line argument: -d is used to determine if the test is being run
  71.     in debug mode. The regress tool requires only one line output:PASS or FAIL.
  72.     All of the printfs associated with this test has been handled with a if (debug_mode)
  73.     test.
  74.     Usage: test_name -d
  75.     */
  76.     PLOptStatus os;
  77.     PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  78.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  79.     {
  80.         if (PL_OPT_BAD == os) continue;
  81.         switch (opt->option)
  82.         {
  83.         case 'd':  /* debug mode */
  84.             debug_mode = 1;
  85.             break;
  86.          default:
  87.             break;
  88.         }
  89.     }
  90.     PL_DestroyOptState(opt);
  91.  
  92.  /* main test */
  93.     
  94.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  95.     PR_STDIO_INIT();
  96.  
  97.     if (debug_mode) {
  98.         printf("This program tests PR_Poll with sockets.\n");
  99.         printf("Timeout is tested.\n\n");
  100.     }
  101.  
  102.     /* Create two listening sockets */
  103.     if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
  104.     fprintf(stderr, "Can't create a new TCP socket\n");
  105.     if (!debug_mode)  failed_already=1;
  106.     goto exit_now;
  107.     }
  108.     addr.inet.family = AF_INET;
  109.     addr.inet.ip = PR_htonl(INADDR_ANY);
  110.     addr.inet.port = PR_htons(0);
  111.     if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
  112.     fprintf(stderr, "Can't bind socket\n");
  113.     if (!debug_mode)  failed_already=1;
  114.     goto exit_now;
  115.     }
  116.     if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
  117.     fprintf(stderr, "PR_GetSockName failed\n");
  118.     if (!debug_mode)  failed_already=1;
  119.     goto exit_now;
  120.     }
  121.     listenPort1 = PR_ntohs(addr.inet.port);
  122.     if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
  123.     fprintf(stderr, "Can't listen on a socket\n");
  124.     if (!debug_mode)  failed_already=1;
  125.     goto exit_now;
  126.     }
  127.  
  128.     if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
  129.     fprintf(stderr, "Can't create a new TCP socket\n");
  130.     if (!debug_mode)  failed_already=1;    
  131.     goto exit_now;
  132.     }
  133.     addr.inet.family = AF_INET;
  134.     addr.inet.ip = PR_htonl(INADDR_ANY);
  135.     addr.inet.port = PR_htons(0);
  136.     if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
  137.     fprintf(stderr, "Can't bind socket\n");
  138.     if (!debug_mode)  failed_already=1;    
  139.     goto exit_now;
  140.     }
  141.     if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
  142.     fprintf(stderr, "PR_GetSockName failed\n");
  143.     if (!debug_mode)  failed_already=1;    
  144.     goto exit_now;
  145.     }
  146.     listenPort2 = PR_ntohs(addr.inet.port);
  147.     if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
  148.     fprintf(stderr, "Can't listen on a socket\n");
  149.     if (!debug_mode)  failed_already=1;    
  150.     goto exit_now;
  151.     }
  152.     PR_snprintf(buf, sizeof(buf),
  153.         "The server thread is listening on ports %hu and %hu\n\n",
  154.         listenPort1, listenPort2);
  155.     if (debug_mode) printf("%s", buf);
  156.  
  157.     /* Set up the poll descriptor array */
  158.     pds = pds0;
  159.     other_pds = pds1;
  160.     memset(pds, 0, sizeof(pds));
  161.     pds[0].fd = listenSock1;
  162.     pds[0].in_flags = PR_POLL_READ;
  163.     pds[1].fd = listenSock2;
  164.     pds[1].in_flags = PR_POLL_READ;
  165.     npds = 2;
  166.  
  167.     /* Testing timeout */
  168.     if (debug_mode) printf("PR_Poll should time out in 5 seconds\n");
  169.     retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5));
  170.     if (retVal != 0) {
  171.     PR_snprintf(buf, sizeof(buf),
  172.         "PR_Poll should time out and return 0, but it returns %ld\n",
  173.         retVal);
  174.     fprintf(stderr, "%s", buf);
  175.     if (!debug_mode)  failed_already=1;    
  176.     goto exit_now;
  177.     }
  178.     if (debug_mode) printf("PR_Poll timed out.  Test passed.\n\n");
  179.  
  180.     PR_Cleanup();
  181.     goto exit_now;
  182. exit_now:
  183.     if(failed_already)    
  184.         return 1;
  185.     else
  186.         return 0;
  187.  
  188. }
  189.