peek.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. * A test case for the PR_MSG_PEEK flag of PR_Recv().
  7. *
  8. * Test both blocking and non-blocking sockets.
  9. */
  10. #include "nspr.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #define BUFFER_SIZE 1024
  15. static int iterations = 10;
  16. /*
  17. * In iteration i, recv_amount[i] is the number of bytes we
  18. * wish to receive, and send_amount[i] is the number of bytes
  19. * we actually send. Therefore, the number of elements in the
  20. * recv_amount or send_amount array should equal to 'iterations'.
  21. * For this test to pass we need to ensure that
  22. * recv_amount[i] <= BUFFER_SIZE,
  23. * send_amount[i] <= BUFFER_SIZE,
  24. * send_amount[i] <= recv_amount[i].
  25. */
  26. static PRInt32 recv_amount[10] = {
  27. 16, 128, 256, 1024, 512, 512, 128, 256, 32, 32
  28. };
  29. static PRInt32 send_amount[10] = {
  30. 16, 64, 128, 1024, 512, 256, 128, 64, 16, 32
  31. };
  32. /* Blocking I/O */
  33. static void ServerB(void *arg)
  34. {
  35. PRFileDesc *listenSock = (PRFileDesc *) arg;
  36. PRFileDesc *sock;
  37. char buf[BUFFER_SIZE];
  38. PRInt32 nbytes;
  39. int i;
  40. int j;
  41. sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
  42. if (NULL == sock) {
  43. fprintf(stderr, "PR_Accept failed\n");
  44. exit(1);
  45. }
  46. for (i = 0; i < iterations; i++) {
  47. memset(buf, 0, sizeof(buf));
  48. nbytes = PR_Recv(sock, buf, recv_amount[i],
  49. PR_MSG_PEEK, PR_INTERVAL_NO_TIMEOUT);
  50. if (-1 == nbytes) {
  51. fprintf(stderr, "PR_Recv failed\n");
  52. exit(1);
  53. }
  54. if (send_amount[i] != nbytes) {
  55. fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
  56. exit(1);
  57. }
  58. for (j = 0; j < nbytes; j++) {
  59. if (buf[j] != 2*i) {
  60. fprintf(stderr, "byte %d should be %d but is %d\n",
  61. j, 2*i, buf[j]);
  62. exit(1);
  63. }
  64. }
  65. fprintf(stderr, "server: peeked expected data\n");
  66. memset(buf, 0, sizeof(buf));
  67. nbytes = PR_Recv(sock, buf, recv_amount[i],
  68. PR_MSG_PEEK, PR_INTERVAL_NO_TIMEOUT);
  69. if (-1 == nbytes) {
  70. fprintf(stderr, "PR_Recv failed\n");
  71. exit(1);
  72. }
  73. if (send_amount[i] != nbytes) {
  74. fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
  75. exit(1);
  76. }
  77. for (j = 0; j < nbytes; j++) {
  78. if (buf[j] != 2*i) {
  79. fprintf(stderr, "byte %d should be %d but is %d\n",
  80. j, 2*i, buf[j]);
  81. exit(1);
  82. }
  83. }
  84. fprintf(stderr, "server: peeked expected data\n");
  85. memset(buf, 0, sizeof(buf));
  86. nbytes = PR_Recv(sock, buf, recv_amount[i],
  87. 0, PR_INTERVAL_NO_TIMEOUT);
  88. if (-1 == nbytes) {
  89. fprintf(stderr, "PR_Recv failed\n");
  90. exit(1);
  91. }
  92. if (send_amount[i] != nbytes) {
  93. fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
  94. exit(1);
  95. }
  96. for (j = 0; j < nbytes; j++) {
  97. if (buf[j] != 2*i) {
  98. fprintf(stderr, "byte %d should be %d but is %d\n",
  99. j, 2*i, buf[j]);
  100. exit(1);
  101. }
  102. }
  103. fprintf(stderr, "server: received expected data\n");
  104. PR_Sleep(PR_SecondsToInterval(1));
  105. memset(buf, 2*i+1, send_amount[i]);
  106. nbytes = PR_Send(sock, buf, send_amount[i],
  107. 0, PR_INTERVAL_NO_TIMEOUT);
  108. if (-1 == nbytes) {
  109. fprintf(stderr, "PR_Send failed\n");
  110. exit(1);
  111. }
  112. if (send_amount[i] != nbytes) {
  113. fprintf(stderr, "PR_Send returned %d, absurd!\n", nbytes);
  114. exit(1);
  115. }
  116. }
  117. if (PR_Close(sock) == PR_FAILURE) {
  118. fprintf(stderr, "PR_Close failed\n");
  119. exit(1);
  120. }
  121. }
  122. /* Non-blocking I/O */
  123. static void ClientNB(void *arg)
  124. {
  125. PRFileDesc *sock;
  126. PRSocketOptionData opt;
  127. PRUint16 port = (PRUint16) arg;
  128. PRNetAddr addr;
  129. char buf[BUFFER_SIZE];
  130. PRPollDesc pd;
  131. PRInt32 npds;
  132. PRInt32 nbytes;
  133. int i;
  134. int j;
  135. sock = PR_OpenTCPSocket(PR_AF_INET6);
  136. if (NULL == sock) {
  137. fprintf(stderr, "PR_OpenTCPSocket failed\n");
  138. exit(1);
  139. }
  140. opt.option = PR_SockOpt_Nonblocking;
  141. opt.value.non_blocking = PR_TRUE;
  142. if (PR_SetSocketOption(sock, &opt) == PR_FAILURE) {
  143. fprintf(stderr, "PR_SetSocketOption failed\n");
  144. exit(1);
  145. }
  146. memset(&addr, 0, sizeof(addr));
  147. if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, port, &addr)
  148. == PR_FAILURE) {
  149. fprintf(stderr, "PR_SetNetAddr failed\n");
  150. exit(1);
  151. }
  152. if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
  153. if (PR_GetError() != PR_IN_PROGRESS_ERROR) {
  154. fprintf(stderr, "PR_Connect failed\n");
  155. exit(1);
  156. }
  157. pd.fd = sock;
  158. pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT;
  159. npds = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
  160. if (-1 == npds) {
  161. fprintf(stderr, "PR_Poll failed\n");
  162. exit(1);
  163. }
  164. if (1 != npds) {
  165. fprintf(stderr, "PR_Poll returned %d, absurd!\n", npds);
  166. exit(1);
  167. }
  168. if (PR_GetConnectStatus(&pd) == PR_FAILURE) {
  169. fprintf(stderr, "PR_GetConnectStatus failed\n");
  170. exit(1);
  171. }
  172. }
  173. for (i = 0; i < iterations; i++) {
  174. PR_Sleep(PR_SecondsToInterval(1));
  175. memset(buf, 2*i, send_amount[i]);
  176. while ((nbytes = PR_Send(sock, buf, send_amount[i],
  177. 0, PR_INTERVAL_NO_TIMEOUT)) == -1) {
  178. if (PR_GetError() != PR_WOULD_BLOCK_ERROR) {
  179. fprintf(stderr, "PR_Send failed\n");
  180. exit(1);
  181. }
  182. pd.fd = sock;
  183. pd.in_flags = PR_POLL_WRITE;
  184. npds = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
  185. if (-1 == npds) {
  186. fprintf(stderr, "PR_Poll failed\n");
  187. exit(1);
  188. }
  189. if (1 != npds) {
  190. fprintf(stderr, "PR_Poll returned %d, absurd!\n", npds);
  191. exit(1);
  192. }
  193. }
  194. if (send_amount[i] != nbytes) {
  195. fprintf(stderr, "PR_Send returned %d, absurd!\n", nbytes);
  196. exit(1);
  197. }
  198. memset(buf, 0, sizeof(buf));
  199. while ((nbytes = PR_Recv(sock, buf, recv_amount[i],
  200. PR_MSG_PEEK, PR_INTERVAL_NO_TIMEOUT)) == -1) {
  201. if (PR_GetError() != PR_WOULD_BLOCK_ERROR) {
  202. fprintf(stderr, "PR_Recv failed\n");
  203. exit(1);
  204. }
  205. pd.fd = sock;
  206. pd.in_flags = PR_POLL_READ;
  207. npds = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
  208. if (-1 == npds) {
  209. fprintf(stderr, "PR_Poll failed\n");
  210. exit(1);
  211. }
  212. if (1 != npds) {
  213. fprintf(stderr, "PR_Poll returned %d, absurd!\n", npds);
  214. exit(1);
  215. }
  216. }
  217. if (send_amount[i] != nbytes) {
  218. fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
  219. exit(1);
  220. }
  221. for (j = 0; j < nbytes; j++) {
  222. if (buf[j] != 2*i+1) {
  223. fprintf(stderr, "byte %d should be %d but is %d\n",
  224. j, 2*i+1, buf[j]);
  225. exit(1);
  226. }
  227. }
  228. fprintf(stderr, "client: peeked expected data\n");
  229. memset(buf, 0, sizeof(buf));
  230. nbytes = PR_Recv(sock, buf, recv_amount[i],
  231. PR_MSG_PEEK, PR_INTERVAL_NO_TIMEOUT);
  232. if (-1 == nbytes) {
  233. fprintf(stderr, "PR_Recv failed\n");
  234. exit(1);
  235. }
  236. if (send_amount[i] != nbytes) {
  237. fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
  238. exit(1);
  239. }
  240. for (j = 0; j < nbytes; j++) {
  241. if (buf[j] != 2*i+1) {
  242. fprintf(stderr, "byte %d should be %d but is %d\n",
  243. j, 2*i+1, buf[j]);
  244. exit(1);
  245. }
  246. }
  247. fprintf(stderr, "client: peeked expected data\n");
  248. memset(buf, 0, sizeof(buf));
  249. nbytes = PR_Recv(sock, buf, recv_amount[i],
  250. 0, PR_INTERVAL_NO_TIMEOUT);
  251. if (-1 == nbytes) {
  252. fprintf(stderr, "PR_Recv failed\n");
  253. exit(1);
  254. }
  255. if (send_amount[i] != nbytes) {
  256. fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
  257. exit(1);
  258. }
  259. for (j = 0; j < nbytes; j++) {
  260. if (buf[j] != 2*i+1) {
  261. fprintf(stderr, "byte %d should be %d but is %d\n",
  262. j, 2*i+1, buf[j]);
  263. exit(1);
  264. }
  265. }
  266. fprintf(stderr, "client: received expected data\n");
  267. }
  268. if (PR_Close(sock) == PR_FAILURE) {
  269. fprintf(stderr, "PR_Close failed\n");
  270. exit(1);
  271. }
  272. }
  273. static void
  274. RunTest(PRThreadScope scope, PRFileDesc *listenSock, PRUint16 port)
  275. {
  276. PRThread *server, *client;
  277. server = PR_CreateThread(PR_USER_THREAD, ServerB, listenSock,
  278. PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
  279. if (NULL == server) {
  280. fprintf(stderr, "PR_CreateThread failed\n");
  281. exit(1);
  282. }
  283. client = PR_CreateThread(
  284. PR_USER_THREAD, ClientNB, (void *) port,
  285. PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
  286. if (NULL == client) {
  287. fprintf(stderr, "PR_CreateThread failed\n");
  288. exit(1);
  289. }
  290. if (PR_JoinThread(server) == PR_FAILURE) {
  291. fprintf(stderr, "PR_JoinThread failed\n");
  292. exit(1);
  293. }
  294. if (PR_JoinThread(client) == PR_FAILURE) {
  295. fprintf(stderr, "PR_JoinThread failed\n");
  296. exit(1);
  297. }
  298. }
  299. int main(int argc, char **argv)
  300. {
  301. PRFileDesc *listenSock;
  302. PRNetAddr addr;
  303. PRUint16 port;
  304. listenSock = PR_OpenTCPSocket(PR_AF_INET6);
  305. if (NULL == listenSock) {
  306. fprintf(stderr, "PR_OpenTCPSocket failed\n");
  307. exit(1);
  308. }
  309. memset(&addr, 0, sizeof(addr));
  310. if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
  311. fprintf(stderr, "PR_SetNetAddr failed\n");
  312. exit(1);
  313. }
  314. if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
  315. fprintf(stderr, "PR_Bind failed\n");
  316. exit(1);
  317. }
  318. if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
  319. fprintf(stderr, "PR_GetSockName failed\n");
  320. exit(1);
  321. }
  322. port = PR_ntohs(addr.ipv6.port);
  323. if (PR_Listen(listenSock, 5) == PR_FAILURE) {
  324. fprintf(stderr, "PR_Listen failed\n");
  325. exit(1);
  326. }
  327. fprintf(stderr, "Running the test with local threads\n");
  328. RunTest(PR_LOCAL_THREAD, listenSock, port);
  329. fprintf(stderr, "Running the test with global threads\n");
  330. RunTest(PR_GLOBAL_THREAD, listenSock, port);
  331. if (PR_Close(listenSock) == PR_FAILURE) {
  332. fprintf(stderr, "PR_Close failed\n");
  333. exit(1);
  334. }
  335. printf("PASS\n");
  336. return 0;
  337. }