home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / poll_nm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.9 KB  |  378 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_norm.c
  22. **
  23. ** Description: This program tests PR_Poll with sockets.
  24. **              Normal operation are 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 "prinit.h"
  43. #include "prio.h"
  44. #include "prlog.h"
  45. #include "prprf.h"
  46. #include "prnetdb.h"
  47. #ifndef XP_MAC
  48. #include "obsolete/probslet.h"
  49. #else
  50. #include "probslet.h"
  51. #endif
  52.  
  53. #ifndef XP_MAC
  54. #include "private/pprio.h"
  55. #else
  56. #include "pprio.h"
  57. #endif
  58.  
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <stdlib.h>
  62. PRIntn failed_already=0;
  63. PRIntn debug_mode;
  64.  
  65. #define NUM_ITERATIONS 5
  66.  
  67. #ifdef XP_MAC
  68. int fprintf(FILE *stream, const char *fmt, ...)
  69. {
  70. PR_LogPrint(fmt);
  71. return 0;
  72. }
  73. #define printf PR_LogPrint
  74. extern void SetupMacPrintfLog(char *logFile);
  75. #endif
  76.  
  77. static void PR_CALLBACK
  78. clientThreadFunc(void *arg)
  79. {
  80.     PRUintn port = (PRUintn) arg;
  81.     PRFileDesc *sock;
  82.     PRNetAddr addr;
  83.     char buf[128];
  84.     int i;
  85.     PRStatus sts;
  86.     PRInt32 n;
  87.  
  88.     addr.inet.family = AF_INET;
  89.     addr.inet.port = PR_htons((PRUint16)port);
  90.     addr.inet.ip = PR_htonl(INADDR_LOOPBACK);
  91.     memset(buf, 0, sizeof(buf));
  92.     PR_snprintf(buf, sizeof(buf), "%hu", addr.inet.ip);
  93.  
  94.     for (i = 0; i < NUM_ITERATIONS; i++) {
  95.     sock = PR_NewTCPSocket();
  96.     PR_ASSERT(sock != NULL);
  97.     
  98.     sts = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
  99.     PR_ASSERT(sts == PR_SUCCESS);
  100.  
  101.     n = PR_Write(sock, buf, sizeof(buf));
  102.     PR_ASSERT(n >= 0);
  103.  
  104.     sts = PR_Close(sock);
  105.     PR_ASSERT(sts == PR_SUCCESS);
  106.     }
  107. }
  108.  
  109. int main(int argc, char **argv)
  110. {
  111.     PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
  112.     PRUint16 listenPort1, listenPort2;
  113.     PRNetAddr addr;
  114.     char buf[128];
  115.     PRThread *clientThread;
  116.     PRPollDesc pds0[20], pds1[20], *pds, *other_pds;
  117.     PRIntn npds;
  118.     PRInt32 retVal;
  119.     PRIntn i, j;
  120.     PRIntn optval = 1;
  121.  
  122.     /* The command line argument: -d is used to determine if the test is being run
  123.     in debug mode. The regress tool requires only one line output:PASS or FAIL.
  124.     All of the printfs associated with this test has been handled with a if (debug_mode)
  125.     test.
  126.     Usage: test_name -d
  127.     */
  128.     PLOptStatus os;
  129.     PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  130.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  131.     {
  132.         if (PL_OPT_BAD == os) continue;
  133.         switch (opt->option)
  134.         {
  135.         case 'd':  /* debug mode */
  136.             debug_mode = 1;
  137.             break;
  138.          default:
  139.             break;
  140.         }
  141.     }
  142.     PL_DestroyOptState(opt);
  143.  
  144.  /* main test */
  145.     
  146.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  147.     PR_STDIO_INIT();
  148.  
  149. #ifdef XP_MAC
  150.     debug_mode = 1;
  151.     SetupMacPrintfLog("poll_nm.log");
  152. #endif
  153.  
  154.     if (debug_mode) {
  155.         printf("This program tests PR_Poll with sockets.\n");
  156.         printf("Normal operation are tested.\n\n");
  157.     }
  158.  
  159.     /* Create two listening sockets */
  160.     if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
  161.     fprintf(stderr, "Can't create a new TCP socket\n");
  162.     failed_already=1;
  163.     goto exit_now;
  164.     }
  165.     addr.inet.family = AF_INET;
  166.     addr.inet.ip = PR_htonl(INADDR_ANY);
  167.     addr.inet.port = PR_htons(0);
  168.     if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
  169.     fprintf(stderr, "Can't bind socket\n");
  170.     failed_already=1;
  171.     goto exit_now;
  172.     }
  173.     if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
  174.     fprintf(stderr, "PR_GetSockName failed\n");
  175.     failed_already=1;
  176.     goto exit_now;
  177.     }
  178.     listenPort1 = PR_ntohs(addr.inet.port);
  179.     PR_SetSockOpt(listenSock1, PR_SockOpt_Nonblocking, &optval, sizeof(optval));
  180.     if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
  181.     fprintf(stderr, "Can't listen on a socket\n");
  182.     failed_already=1;
  183.     goto exit_now;
  184.     }
  185.  
  186.     if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
  187.     fprintf(stderr, "Can't create a new TCP socket\n");
  188.     failed_already=1;    
  189.     goto exit_now;
  190.     }
  191.     addr.inet.family = AF_INET;
  192.     addr.inet.ip = PR_htonl(INADDR_ANY);
  193.     addr.inet.port = PR_htons(0);
  194.     if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
  195.     fprintf(stderr, "Can't bind socket\n");
  196.     failed_already=1;    
  197.     goto exit_now;
  198.     }
  199.     if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
  200.     fprintf(stderr, "PR_GetSockName failed\n");
  201.     failed_already=1;    
  202.     goto exit_now;
  203.     }
  204.     listenPort2 = PR_ntohs(addr.inet.port);
  205.     PR_SetSockOpt(listenSock2, PR_SockOpt_Nonblocking, &optval, sizeof(optval));
  206.     if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
  207.     fprintf(stderr, "Can't listen on a socket\n");
  208.     failed_already=1;    
  209.     goto exit_now;
  210.     }
  211.     PR_snprintf(buf, sizeof(buf),
  212.         "The server thread is listening on ports %hu and %hu\n\n",
  213.         listenPort1, listenPort2);
  214.     if (debug_mode) printf("%s", buf);
  215.  
  216.     /* Set up the poll descriptor array */
  217.     pds = pds0;
  218.     other_pds = pds1;
  219.     memset(pds, 0, sizeof(pds));
  220.     pds[0].fd = listenSock1;
  221.     pds[0].in_flags = PR_POLL_READ;
  222.     pds[1].fd = listenSock2;
  223.     pds[1].in_flags = PR_POLL_READ;
  224.     /* Add some unused entries to test if they are ignored by PR_Poll() */
  225.     memset(&pds[2], 0, sizeof(pds[2]));
  226.     memset(&pds[3], 0, sizeof(pds[3]));
  227.     memset(&pds[4], 0, sizeof(pds[4]));
  228.     npds = 5;
  229.  
  230.     clientThread = PR_CreateThread(PR_USER_THREAD,
  231.         clientThreadFunc, (void *) listenPort1,
  232.         PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
  233.         PR_UNJOINABLE_THREAD, 0);
  234.     if (clientThread == NULL) {
  235.     fprintf(stderr, "can't create thread\n");
  236.     failed_already=1;    
  237.     goto exit_now;
  238.     }
  239.  
  240.     clientThread = PR_CreateThread(PR_USER_THREAD,
  241.         clientThreadFunc, (void *) listenPort2,
  242.         PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
  243.         PR_UNJOINABLE_THREAD, 0);
  244.     if (clientThread == NULL) {
  245.     fprintf(stderr, "can't create thread\n");
  246.     failed_already=1;        
  247.     goto exit_now;
  248.     }
  249.  
  250.     if (debug_mode) {
  251.         printf("Two client threads are created.  Each of them will\n");
  252.         printf("send data to one of the two ports the server is listening on.\n");
  253.         printf("The data they send is the port number.  Each of them send\n");
  254.         printf("the data five times, so you should see ten lines below,\n");
  255.         printf("interleaved in an arbitrary order.\n");
  256.     }
  257.  
  258.     /* two clients, three events per iteration: accept, read, close */
  259.     i = 0;
  260.     while (i < 2 * 3 * NUM_ITERATIONS) {
  261.     PRPollDesc *tmp;
  262.     int nextIndex;
  263.     int nEvents = 0;
  264.  
  265.     retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
  266.     PR_ASSERT(retVal != 0);  /* no timeout */
  267.     if (retVal == -1) {
  268.         fprintf(stderr, "PR_Poll failed\n");
  269.         failed_already=1;            
  270.         goto exit_now;
  271.     }
  272.  
  273.     nextIndex = 2;
  274.     /* the two listening sockets */
  275.     for (j = 0; j < 2; j++) {
  276.         other_pds[j] = pds[j];
  277.         PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
  278.             && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
  279.         if (pds[j].out_flags & PR_POLL_READ) {
  280.         PRFileDesc *sock;
  281.  
  282.         nEvents++;
  283.         sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT);
  284.         if (sock == NULL) {
  285.             fprintf(stderr, "PR_Accept() failed\n");
  286.             failed_already=1;    
  287.             goto exit_now;
  288.         }
  289.         other_pds[nextIndex].fd = sock;
  290.         other_pds[nextIndex].in_flags = PR_POLL_READ;
  291.         nextIndex++;
  292.         } else if (pds[j].out_flags & PR_POLL_ERR) {
  293.         fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
  294.         failed_already=1;    
  295.         goto exit_now;
  296.         } else if (pds[j].out_flags & PR_POLL_NVAL) {
  297.         fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n",
  298.             PR_FileDesc2NativeHandle(pds[j].fd));
  299.         failed_already=1;    
  300.         goto exit_now;
  301.         }
  302.     }
  303.  
  304.     for (j = 2; j < npds; j++) {
  305.             if (NULL == pds[j].fd) {
  306.                 /*
  307.                  * Keep the unused entries in the poll descriptor array
  308.                  * for testing purposes.
  309.                  */
  310.                 other_pds[nextIndex] = pds[j];
  311.                 nextIndex++;
  312.                 continue;
  313.             }
  314.  
  315.         PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
  316.             && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
  317.         if (pds[j].out_flags & PR_POLL_READ) {
  318.                 PRInt32 nAvail;
  319.         PRInt32 nRead;
  320.  
  321.         nEvents++;
  322.                 nAvail = PR_Available(pds[j].fd);
  323.         nRead = PR_Read(pds[j].fd, buf, sizeof(buf));
  324.                 PR_ASSERT(nAvail == nRead);
  325.         if (nRead == -1) {
  326.             fprintf(stderr, "PR_Read() failed\n");
  327.             failed_already=1;    
  328.             goto exit_now;
  329.                 } else if (nRead == 0) {
  330.                     PR_Close(pds[j].fd);
  331.                     continue;
  332.                 } else {
  333.                     /* Just to be safe */
  334.                     buf[127] = '\0';
  335.                     if (debug_mode) printf("The server received \"%s\" from a client\n", buf);
  336.                 }
  337.         } else if (pds[j].out_flags & PR_POLL_ERR) {
  338.         fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
  339.         failed_already=1;            
  340.         goto exit_now;
  341.         } else if (pds[j].out_flags & PR_POLL_NVAL) {
  342.         fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n");
  343.         failed_already=1;            
  344.         goto exit_now;
  345.         }
  346.             other_pds[nextIndex] = pds[j];
  347.             nextIndex++;
  348.     }
  349.  
  350.     PR_ASSERT(retVal == nEvents);
  351.     /* swap */
  352.     tmp = pds;
  353.     pds = other_pds;
  354.     other_pds = tmp;
  355.     npds = nextIndex;
  356.     i += nEvents;
  357.     }
  358.  
  359.     if (debug_mode) printf("Tests passed\n");
  360.  
  361. exit_now:
  362.  
  363.     if (listenSock1) {
  364.         PR_Close(listenSock1);
  365.     }
  366.     if (listenSock2) {
  367.         PR_Close(listenSock2);
  368.     }
  369.  
  370.     PR_Cleanup();
  371.     
  372.     if(failed_already)    
  373.         return 1;
  374.     else
  375.         return 0;
  376.  
  377. }
  378.