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