poll_to.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /***********************************************************************
  6. **
  7. ** Name: prpoll_to.c
  8. **
  9. ** Description: This program tests PR_Poll with sockets.
  10. ** Timeout operation is tested
  11. **
  12. ** Modification History:
  13. ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  14. ** The debug mode will print all of the printfs associated with this test.
  15. ** The regress mode will be the default mode. Since the regress tool limits
  16. ** the output to a one line status:PASS or FAIL,all of the printf statements
  17. ** have been handled with an if (debug_mode) statement.
  18. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  19. ** recognize the return code from tha main program.
  20. ***********************************************************************/
  21. /***********************************************************************
  22. ** Includes
  23. ***********************************************************************/
  24. /* Used to get the command line option */
  25. #include "plgetopt.h"
  26. #include "prinit.h"
  27. #include "prio.h"
  28. #include "prlog.h"
  29. #include "prprf.h"
  30. #include "prnetdb.h"
  31. #include "private/pprio.h"
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. PRIntn failed_already=0;
  36. PRIntn debug_mode;
  37. int main(int argc, char **argv)
  38. {
  39. PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
  40. PRUint16 listenPort1, listenPort2;
  41. PRNetAddr addr;
  42. char buf[128];
  43. PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
  44. PRIntn npds;
  45. PRInt32 retVal;
  46. /* The command line argument: -d is used to determine if the test is being run
  47. in debug mode. The regress tool requires only one line output:PASS or FAIL.
  48. All of the printfs associated with this test has been handled with a if (debug_mode)
  49. test.
  50. Usage: test_name -d
  51. */
  52. PLOptStatus os;
  53. PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  54. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  55. {
  56. if (PL_OPT_BAD == os) {
  57. continue;
  58. }
  59. switch (opt->option)
  60. {
  61. case 'd': /* debug mode */
  62. debug_mode = 1;
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. PL_DestroyOptState(opt);
  69. /* main test */
  70. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  71. PR_STDIO_INIT();
  72. if (debug_mode) {
  73. printf("This program tests PR_Poll with sockets.\n");
  74. printf("Timeout is tested.\n\n");
  75. }
  76. /* Create two listening sockets */
  77. if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
  78. fprintf(stderr, "Can't create a new TCP socket\n");
  79. if (!debug_mode) {
  80. failed_already=1;
  81. }
  82. goto exit_now;
  83. }
  84. memset(&addr, 0, sizeof(addr));
  85. addr.inet.family = PR_AF_INET;
  86. addr.inet.ip = PR_htonl(PR_INADDR_ANY);
  87. addr.inet.port = PR_htons(0);
  88. if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
  89. fprintf(stderr, "Can't bind socket\n");
  90. if (!debug_mode) {
  91. failed_already=1;
  92. }
  93. goto exit_now;
  94. }
  95. if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
  96. fprintf(stderr, "PR_GetSockName failed\n");
  97. if (!debug_mode) {
  98. failed_already=1;
  99. }
  100. goto exit_now;
  101. }
  102. listenPort1 = PR_ntohs(addr.inet.port);
  103. if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
  104. fprintf(stderr, "Can't listen on a socket\n");
  105. if (!debug_mode) {
  106. failed_already=1;
  107. }
  108. goto exit_now;
  109. }
  110. if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
  111. fprintf(stderr, "Can't create a new TCP socket\n");
  112. if (!debug_mode) {
  113. failed_already=1;
  114. }
  115. goto exit_now;
  116. }
  117. addr.inet.family = PR_AF_INET;
  118. addr.inet.ip = PR_htonl(PR_INADDR_ANY);
  119. addr.inet.port = PR_htons(0);
  120. if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
  121. fprintf(stderr, "Can't bind socket\n");
  122. if (!debug_mode) {
  123. failed_already=1;
  124. }
  125. goto exit_now;
  126. }
  127. if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
  128. fprintf(stderr, "PR_GetSockName failed\n");
  129. if (!debug_mode) {
  130. failed_already=1;
  131. }
  132. goto exit_now;
  133. }
  134. listenPort2 = PR_ntohs(addr.inet.port);
  135. if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
  136. fprintf(stderr, "Can't listen on a socket\n");
  137. if (!debug_mode) {
  138. failed_already=1;
  139. }
  140. goto exit_now;
  141. }
  142. PR_snprintf(buf, sizeof(buf),
  143. "The server thread is listening on ports %hu and %hu\n\n",
  144. listenPort1, listenPort2);
  145. if (debug_mode) {
  146. printf("%s", buf);
  147. }
  148. /* Set up the poll descriptor array */
  149. pds = pds0;
  150. other_pds = pds1;
  151. memset(pds, 0, sizeof(pds));
  152. pds[0].fd = listenSock1;
  153. pds[0].in_flags = PR_POLL_READ;
  154. pds[1].fd = listenSock2;
  155. pds[1].in_flags = PR_POLL_READ;
  156. npds = 2;
  157. /* Testing timeout */
  158. if (debug_mode) {
  159. printf("PR_Poll should time out in 5 seconds\n");
  160. }
  161. retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5));
  162. if (retVal != 0) {
  163. PR_snprintf(buf, sizeof(buf),
  164. "PR_Poll should time out and return 0, but it returns %ld\n",
  165. retVal);
  166. fprintf(stderr, "%s", buf);
  167. if (!debug_mode) {
  168. failed_already=1;
  169. }
  170. goto exit_now;
  171. }
  172. if (debug_mode) {
  173. printf("PR_Poll timed out. Test passed.\n\n");
  174. }
  175. exit_now:
  176. if (listenSock1) {
  177. PR_Close(listenSock1);
  178. }
  179. if (listenSock2) {
  180. PR_Close(listenSock2);
  181. }
  182. PR_Cleanup();
  183. if(failed_already) {
  184. return 1;
  185. }
  186. else {
  187. return 0;
  188. }
  189. }