zerolen.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. * Test: zerolen.c
  7. *
  8. * Description: a test for Bugzilla bug #17699. We perform
  9. * the same test for PR_Writev, PR_Write, and PR_Send. In
  10. * each test the server thread first fills up the connection
  11. * to the client so that the next write operation will fail
  12. * with EAGAIN. Then it calls PR_Writev, PR_Write, or PR_Send
  13. * with a zero-length buffer. The client thread initially
  14. * does not read so that the connection can be filled up.
  15. * Then it empties the connection so that the server thread's
  16. * PR_Writev, PR_Write, or PR_Send call can succeed.
  17. *
  18. * Bug #17699 is specific to the pthreads version on Unix,
  19. * so on other platforms this test does nothing.
  20. */
  21. #ifndef XP_UNIX
  22. #include <stdio.h>
  23. int main(int argc, char **argv)
  24. {
  25. printf("PASS\n");
  26. return 0;
  27. }
  28. #else /* XP_UNIX */
  29. #include "nspr.h"
  30. #include "private/pprio.h"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <errno.h>
  35. #include <unistd.h>
  36. static void ClientThread(void *arg)
  37. {
  38. PRFileDesc *sock;
  39. PRNetAddr addr;
  40. PRUint16 port = (PRUint16) arg;
  41. char buf[1024];
  42. PRInt32 nbytes;
  43. sock = PR_NewTCPSocket();
  44. if (NULL == sock) {
  45. fprintf(stderr, "PR_NewTCPSocket failed\n");
  46. exit(1);
  47. }
  48. if (PR_InitializeNetAddr(PR_IpAddrLoopback, port, &addr) == PR_FAILURE) {
  49. fprintf(stderr, "PR_InitializeNetAddr failed\n");
  50. exit(1);
  51. }
  52. if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
  53. fprintf(stderr, "PR_Connect failed\n");
  54. exit(1);
  55. }
  56. /*
  57. * Sleep 5 seconds to force the server thread to get EAGAIN.
  58. */
  59. if (PR_Sleep(PR_SecondsToInterval(5)) == PR_FAILURE) {
  60. fprintf(stderr, "PR_Sleep failed\n");
  61. exit(1);
  62. }
  63. /*
  64. * Then start reading.
  65. */
  66. while (nbytes = PR_Read(sock, buf, sizeof(buf)) > 0) {
  67. /* empty loop body */
  68. }
  69. if (-1 == nbytes) {
  70. fprintf(stderr, "PR_Read failed\n");
  71. exit(1);
  72. }
  73. if (PR_Close(sock) == PR_FAILURE) {
  74. fprintf(stderr, "PR_Close failed\n");
  75. exit(1);
  76. }
  77. }
  78. int main()
  79. {
  80. PRFileDesc *listenSock;
  81. PRFileDesc *acceptSock;
  82. int osfd;
  83. PRThread *clientThread;
  84. PRNetAddr addr;
  85. char buf[1024];
  86. PRInt32 nbytes;
  87. PRIOVec iov;
  88. memset(buf, 0, sizeof(buf)); /* Initialize the buffer. */
  89. listenSock = PR_NewTCPSocket();
  90. if (NULL == listenSock) {
  91. fprintf(stderr, "PR_NewTCPSocket failed\n");
  92. exit(1);
  93. }
  94. if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
  95. fprintf(stderr, "PR_InitializeNetAddr failed\n");
  96. exit(1);
  97. }
  98. if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
  99. fprintf(stderr, "PR_Bind failed\n");
  100. exit(1);
  101. }
  102. /* Find out what port number we are bound to. */
  103. if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
  104. fprintf(stderr, "PR_GetSockName failed\n");
  105. exit(1);
  106. }
  107. if (PR_Listen(listenSock, 5) == PR_FAILURE) {
  108. fprintf(stderr, "PR_Listen failed\n");
  109. exit(1);
  110. }
  111. /*
  112. * First test PR_Writev.
  113. */
  114. clientThread = PR_CreateThread(PR_USER_THREAD,
  115. ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
  116. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
  117. if (NULL == clientThread) {
  118. fprintf(stderr, "PR_CreateThread failed\n");
  119. exit(1);
  120. }
  121. acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
  122. if (NULL == acceptSock) {
  123. fprintf(stderr, "PR_Accept failed\n");
  124. exit(1);
  125. }
  126. osfd = PR_FileDesc2NativeHandle(acceptSock);
  127. while (write(osfd, buf, sizeof(buf)) != -1) {
  128. /* empty loop body */
  129. }
  130. if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
  131. fprintf(stderr, "write failed\n");
  132. exit(1);
  133. }
  134. iov.iov_base = buf;
  135. iov.iov_len = 0;
  136. printf("calling PR_Writev with a zero-length buffer\n");
  137. fflush(stdout);
  138. nbytes = PR_Writev(acceptSock, &iov, 1, PR_INTERVAL_NO_TIMEOUT);
  139. if (nbytes != 0) {
  140. fprintf(stderr, "PR_Writev should return 0 but returns %d\n", nbytes);
  141. exit(1);
  142. }
  143. if (PR_Close(acceptSock) == PR_FAILURE) {
  144. fprintf(stderr, "PR_Close failed\n");
  145. exit(1);
  146. }
  147. if (PR_JoinThread(clientThread) == PR_FAILURE) {
  148. fprintf(stderr, "PR_JoinThread failed\n");
  149. exit(1);
  150. }
  151. /*
  152. * Then test PR_Write.
  153. */
  154. clientThread = PR_CreateThread(PR_USER_THREAD,
  155. ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
  156. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
  157. if (NULL == clientThread) {
  158. fprintf(stderr, "PR_CreateThread failed\n");
  159. exit(1);
  160. }
  161. acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
  162. if (NULL == acceptSock) {
  163. fprintf(stderr, "PR_Accept failed\n");
  164. exit(1);
  165. }
  166. osfd = PR_FileDesc2NativeHandle(acceptSock);
  167. while (write(osfd, buf, sizeof(buf)) != -1) {
  168. /* empty loop body */
  169. }
  170. if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
  171. fprintf(stderr, "write failed\n");
  172. exit(1);
  173. }
  174. printf("calling PR_Write with a zero-length buffer\n");
  175. fflush(stdout);
  176. nbytes = PR_Write(acceptSock, buf, 0);
  177. if (nbytes != 0) {
  178. fprintf(stderr, "PR_Write should return 0 but returns %d\n", nbytes);
  179. exit(1);
  180. }
  181. if (PR_Close(acceptSock) == PR_FAILURE) {
  182. fprintf(stderr, "PR_Close failed\n");
  183. exit(1);
  184. }
  185. if (PR_JoinThread(clientThread) == PR_FAILURE) {
  186. fprintf(stderr, "PR_JoinThread failed\n");
  187. exit(1);
  188. }
  189. /*
  190. * Finally test PR_Send.
  191. */
  192. clientThread = PR_CreateThread(PR_USER_THREAD,
  193. ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
  194. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
  195. if (NULL == clientThread) {
  196. fprintf(stderr, "PR_CreateThread failed\n");
  197. exit(1);
  198. }
  199. acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
  200. if (NULL == acceptSock) {
  201. fprintf(stderr, "PR_Accept failed\n");
  202. exit(1);
  203. }
  204. osfd = PR_FileDesc2NativeHandle(acceptSock);
  205. while (write(osfd, buf, sizeof(buf)) != -1) {
  206. /* empty loop body */
  207. }
  208. if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
  209. fprintf(stderr, "write failed\n");
  210. exit(1);
  211. }
  212. printf("calling PR_Send with a zero-length buffer\n");
  213. fflush(stdout);
  214. nbytes = PR_Send(acceptSock, buf, 0, 0, PR_INTERVAL_NO_TIMEOUT);
  215. if (nbytes != 0) {
  216. fprintf(stderr, "PR_Send should return 0 but returns %d\n", nbytes);
  217. exit(1);
  218. }
  219. if (PR_Close(acceptSock) == PR_FAILURE) {
  220. fprintf(stderr, "PR_Close failed\n");
  221. exit(1);
  222. }
  223. if (PR_JoinThread(clientThread) == PR_FAILURE) {
  224. fprintf(stderr, "PR_JoinThread failed\n");
  225. exit(1);
  226. }
  227. if (PR_Close(listenSock) == PR_FAILURE) {
  228. fprintf(stderr, "PR_Close failed\n");
  229. exit(1);
  230. }
  231. printf("PASS\n");
  232. return 0;
  233. }
  234. #endif /* XP_UNIX */