prpoll.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #ifdef WIN32
  6. #include <windows.h>
  7. #endif
  8. #ifdef XP_UNIX
  9. #include <unistd.h> /* for close() */
  10. #endif
  11. #include "prinit.h"
  12. #include "prio.h"
  13. #include "prlog.h"
  14. #include "prprf.h"
  15. #include "prnetdb.h"
  16. #include "private/pprio.h"
  17. #define CLIENT_LOOPS 5
  18. #define BUF_SIZE 128
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #ifdef WINCE
  23. int main(int argc, char **argv)
  24. {
  25. fprintf(stderr, "Invalid/Broken Test for WinCE/WinMobile\n");
  26. exit(1);
  27. }
  28. #else
  29. static void
  30. clientThreadFunc(void *arg)
  31. {
  32. PRUint16 port = (PRUint16) arg;
  33. PRFileDesc *sock;
  34. PRNetAddr addr;
  35. char buf[BUF_SIZE];
  36. int i;
  37. addr.inet.family = PR_AF_INET;
  38. addr.inet.port = PR_htons(port);
  39. addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
  40. PR_snprintf(buf, sizeof(buf), "%hu", port);
  41. for (i = 0; i < 5; i++) {
  42. sock = PR_NewTCPSocket();
  43. PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
  44. PR_Write(sock, buf, sizeof(buf));
  45. PR_Close(sock);
  46. }
  47. }
  48. int main(int argc, char **argv)
  49. {
  50. PRFileDesc *listenSock1, *listenSock2;
  51. PRFileDesc *badFD;
  52. PRUint16 listenPort1, listenPort2;
  53. PRNetAddr addr;
  54. char buf[BUF_SIZE];
  55. PRThread *clientThread;
  56. PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
  57. PRIntn npds;
  58. PRInt32 retVal;
  59. PRInt32 rv;
  60. PROsfd sd;
  61. struct sockaddr_in saddr;
  62. PRIntn saddr_len;
  63. PRUint16 listenPort3;
  64. PRFileDesc *socket_poll_fd;
  65. PRIntn i, j;
  66. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  67. PR_STDIO_INIT();
  68. printf("This program tests PR_Poll with sockets.\n");
  69. printf("Timeout, error reporting, and normal operation are tested.\n\n");
  70. /* Create two listening sockets */
  71. if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
  72. fprintf(stderr, "Can't create a new TCP socket\n");
  73. exit(1);
  74. }
  75. addr.inet.family = PR_AF_INET;
  76. addr.inet.ip = PR_htonl(PR_INADDR_ANY);
  77. addr.inet.port = PR_htons(0);
  78. if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
  79. fprintf(stderr, "Can't bind socket\n");
  80. exit(1);
  81. }
  82. if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
  83. fprintf(stderr, "PR_GetSockName failed\n");
  84. exit(1);
  85. }
  86. listenPort1 = PR_ntohs(addr.inet.port);
  87. if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
  88. fprintf(stderr, "Can't listen on a socket\n");
  89. exit(1);
  90. }
  91. if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
  92. fprintf(stderr, "Can't create a new TCP socket\n");
  93. exit(1);
  94. }
  95. addr.inet.family = PR_AF_INET;
  96. addr.inet.ip = PR_htonl(PR_INADDR_ANY);
  97. addr.inet.port = PR_htons(0);
  98. if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
  99. fprintf(stderr, "Can't bind socket\n");
  100. exit(1);
  101. }
  102. if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
  103. fprintf(stderr, "PR_GetSockName failed\n");
  104. exit(1);
  105. }
  106. listenPort2 = PR_ntohs(addr.inet.port);
  107. if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
  108. fprintf(stderr, "Can't listen on a socket\n");
  109. exit(1);
  110. }
  111. /* Set up the poll descriptor array */
  112. pds = pds0;
  113. other_pds = pds1;
  114. memset(pds, 0, sizeof(pds));
  115. npds = 0;
  116. pds[npds].fd = listenSock1;
  117. pds[npds].in_flags = PR_POLL_READ;
  118. npds++;
  119. pds[npds].fd = listenSock2;
  120. pds[npds].in_flags = PR_POLL_READ;
  121. npds++;
  122. sd = socket(AF_INET, SOCK_STREAM, 0);
  123. PR_ASSERT(sd >= 0);
  124. memset((char *) &saddr, 0, sizeof(saddr));
  125. saddr.sin_family = AF_INET;
  126. saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  127. saddr.sin_port = htons(0);
  128. rv = bind(sd, (struct sockaddr *)&saddr, sizeof(saddr));
  129. PR_ASSERT(rv == 0);
  130. saddr_len = sizeof(saddr);
  131. rv = getsockname(sd, (struct sockaddr *) &saddr, &saddr_len);
  132. PR_ASSERT(rv == 0);
  133. listenPort3 = ntohs(saddr.sin_port);
  134. rv = listen(sd, 5);
  135. PR_ASSERT(rv == 0);
  136. pds[npds].fd = socket_poll_fd = PR_CreateSocketPollFd(sd);
  137. PR_ASSERT(pds[npds].fd);
  138. pds[npds].in_flags = PR_POLL_READ;
  139. npds++;
  140. PR_snprintf(buf, sizeof(buf),
  141. "The server thread is listening on ports %hu, %hu and %hu\n\n",
  142. listenPort1, listenPort2, listenPort3);
  143. printf("%s", buf);
  144. /* Testing timeout */
  145. printf("PR_Poll should time out in 5 seconds\n");
  146. retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5));
  147. if (retVal != 0) {
  148. PR_snprintf(buf, sizeof(buf),
  149. "PR_Poll should time out and return 0, but it returns %ld\n",
  150. retVal);
  151. fprintf(stderr, "%s", buf);
  152. exit(1);
  153. }
  154. printf("PR_Poll timed out. Test passed.\n\n");
  155. /* Testing bad fd */
  156. printf("PR_Poll should detect a bad file descriptor\n");
  157. if ((badFD = PR_NewTCPSocket()) == NULL) {
  158. fprintf(stderr, "Can't create a TCP socket\n");
  159. exit(1);
  160. }
  161. pds[npds].fd = badFD;
  162. pds[npds].in_flags = PR_POLL_READ;
  163. npds++;
  164. PR_Close(badFD); /* make the fd bad */
  165. #if 0
  166. retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
  167. if (retVal != 1 || (unsigned short) pds[2].out_flags != PR_POLL_NVAL) {
  168. fprintf(stderr, "Failed to detect the bad fd: "
  169. "PR_Poll returns %d, out_flags is 0x%hx\n",
  170. retVal, pds[npds - 1].out_flags);
  171. exit(1);
  172. }
  173. printf("PR_Poll detected the bad fd. Test passed.\n\n");
  174. #endif
  175. npds--;
  176. clientThread = PR_CreateThread(PR_USER_THREAD,
  177. clientThreadFunc, (void *) listenPort1,
  178. PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
  179. PR_UNJOINABLE_THREAD, 0);
  180. if (clientThread == NULL) {
  181. fprintf(stderr, "can't create thread\n");
  182. exit(1);
  183. }
  184. clientThread = PR_CreateThread(PR_USER_THREAD,
  185. clientThreadFunc, (void *) listenPort2,
  186. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  187. PR_UNJOINABLE_THREAD, 0);
  188. if (clientThread == NULL) {
  189. fprintf(stderr, "can't create thread\n");
  190. exit(1);
  191. }
  192. clientThread = PR_CreateThread(PR_USER_THREAD,
  193. clientThreadFunc, (void *) listenPort3,
  194. PR_PRIORITY_NORMAL, PR_GLOBAL_BOUND_THREAD,
  195. PR_UNJOINABLE_THREAD, 0);
  196. if (clientThread == NULL) {
  197. fprintf(stderr, "can't create thread\n");
  198. exit(1);
  199. }
  200. printf("Three client threads are created. Each of them will\n");
  201. printf("send data to one of the three ports the server is listening on.\n");
  202. printf("The data they send is the port number. Each of them send\n");
  203. printf("the data five times, so you should see ten lines below,\n");
  204. printf("interleaved in an arbitrary order.\n");
  205. /* 30 events total */
  206. i = 0;
  207. while (i < 30) {
  208. PRPollDesc *tmp;
  209. int nextIndex;
  210. int nEvents = 0;
  211. retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
  212. PR_ASSERT(retVal != 0); /* no timeout */
  213. if (retVal == -1) {
  214. fprintf(stderr, "PR_Poll failed\n");
  215. exit(1);
  216. }
  217. nextIndex = 3;
  218. /* the three listening sockets */
  219. for (j = 0; j < 3; j++) {
  220. other_pds[j] = pds[j];
  221. PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
  222. && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
  223. if (pds[j].out_flags & PR_POLL_READ) {
  224. PRFileDesc *sock;
  225. nEvents++;
  226. if (j == 2) {
  227. PROsfd newsd;
  228. newsd = accept(PR_FileDesc2NativeHandle(pds[j].fd), NULL, 0);
  229. if (newsd == -1) {
  230. fprintf(stderr, "accept() failed\n");
  231. exit(1);
  232. }
  233. other_pds[nextIndex].fd = PR_CreateSocketPollFd(newsd);
  234. PR_ASSERT(other_pds[nextIndex].fd);
  235. other_pds[nextIndex].in_flags = PR_POLL_READ;
  236. } else {
  237. sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT);
  238. if (sock == NULL) {
  239. fprintf(stderr, "PR_Accept() failed\n");
  240. exit(1);
  241. }
  242. other_pds[nextIndex].fd = sock;
  243. other_pds[nextIndex].in_flags = PR_POLL_READ;
  244. }
  245. nextIndex++;
  246. } else if (pds[j].out_flags & PR_POLL_ERR) {
  247. fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
  248. exit(1);
  249. } else if (pds[j].out_flags & PR_POLL_NVAL) {
  250. fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n",
  251. PR_FileDesc2NativeHandle(pds[j].fd));
  252. exit(1);
  253. }
  254. }
  255. for (j = 3; j < npds; j++) {
  256. PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
  257. && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
  258. if (pds[j].out_flags & PR_POLL_READ) {
  259. PRInt32 nBytes;
  260. nEvents++;
  261. /* XXX: This call is a hack and should be fixed */
  262. if (PR_GetDescType(pds[j].fd) == (PRDescType) 0) {
  263. nBytes = recv(PR_FileDesc2NativeHandle(pds[j].fd), buf,
  264. sizeof(buf), 0);
  265. if (nBytes == -1) {
  266. fprintf(stderr, "recv() failed\n");
  267. exit(1);
  268. }
  269. printf("Server read %d bytes from native fd %d\n",nBytes,
  270. PR_FileDesc2NativeHandle(pds[j].fd));
  271. #ifdef WIN32
  272. closesocket((SOCKET)PR_FileDesc2NativeHandle(pds[j].fd));
  273. #else
  274. close(PR_FileDesc2NativeHandle(pds[j].fd));
  275. #endif
  276. PR_DestroySocketPollFd(pds[j].fd);
  277. } else {
  278. nBytes = PR_Read(pds[j].fd, buf, sizeof(buf));
  279. if (nBytes == -1) {
  280. fprintf(stderr, "PR_Read() failed\n");
  281. exit(1);
  282. }
  283. PR_Close(pds[j].fd);
  284. }
  285. /* Just to be safe */
  286. buf[BUF_SIZE - 1] = '\0';
  287. printf("The server received \"%s\" from a client\n", buf);
  288. } else if (pds[j].out_flags & PR_POLL_ERR) {
  289. fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
  290. exit(1);
  291. } else if (pds[j].out_flags & PR_POLL_NVAL) {
  292. fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n");
  293. exit(1);
  294. } else {
  295. other_pds[nextIndex] = pds[j];
  296. nextIndex++;
  297. }
  298. }
  299. PR_ASSERT(retVal == nEvents);
  300. /* swap */
  301. tmp = pds;
  302. pds = other_pds;
  303. other_pds = tmp;
  304. npds = nextIndex;
  305. i += nEvents;
  306. }
  307. PR_DestroySocketPollFd(socket_poll_fd);
  308. printf("All tests finished\n");
  309. PR_Cleanup();
  310. return 0;
  311. }
  312. #endif /* ifdef WINCE */