poll_er.c 6.2 KB

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