/test/test-udp-send-and-recv.c

http://github.com/joyent/libuv · C · 209 lines · 126 code · 54 blank · 29 comment · 42 complexity · e1762166dd4a43d3d32c9720de107aec MD5 · raw file

  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #define CHECK_HANDLE(handle) \
  27. ASSERT((uv_udp_t*)(handle) == &server || (uv_udp_t*)(handle) == &client)
  28. static uv_udp_t server;
  29. static uv_udp_t client;
  30. static int cl_send_cb_called;
  31. static int cl_recv_cb_called;
  32. static int sv_send_cb_called;
  33. static int sv_recv_cb_called;
  34. static int close_cb_called;
  35. static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
  36. static char slab[65536];
  37. CHECK_HANDLE(handle);
  38. ASSERT(suggested_size <= sizeof slab);
  39. return uv_buf_init(slab, sizeof slab);
  40. }
  41. static void close_cb(uv_handle_t* handle) {
  42. CHECK_HANDLE(handle);
  43. close_cb_called++;
  44. }
  45. static void cl_recv_cb(uv_udp_t* handle,
  46. ssize_t nread,
  47. uv_buf_t buf,
  48. struct sockaddr* addr,
  49. unsigned flags) {
  50. CHECK_HANDLE(handle);
  51. ASSERT(flags == 0);
  52. if (nread < 0) {
  53. ASSERT(0 && "unexpected error");
  54. }
  55. if (nread == 0) {
  56. /* Returning unused buffer */
  57. /* Don't count towards cl_recv_cb_called */
  58. ASSERT(addr == NULL);
  59. return;
  60. }
  61. ASSERT(addr != NULL);
  62. ASSERT(nread == 4);
  63. ASSERT(!memcmp("PONG", buf.base, nread));
  64. cl_recv_cb_called++;
  65. uv_close((uv_handle_t*) handle, close_cb);
  66. }
  67. static void cl_send_cb(uv_udp_send_t* req, int status) {
  68. int r;
  69. ASSERT(req != NULL);
  70. ASSERT(status == 0);
  71. CHECK_HANDLE(req->handle);
  72. r = uv_udp_recv_start(req->handle, alloc_cb, cl_recv_cb);
  73. ASSERT(r == 0);
  74. cl_send_cb_called++;
  75. }
  76. static void sv_send_cb(uv_udp_send_t* req, int status) {
  77. ASSERT(req != NULL);
  78. ASSERT(status == 0);
  79. CHECK_HANDLE(req->handle);
  80. uv_close((uv_handle_t*) req->handle, close_cb);
  81. free(req);
  82. sv_send_cb_called++;
  83. }
  84. static void sv_recv_cb(uv_udp_t* handle,
  85. ssize_t nread,
  86. uv_buf_t buf,
  87. struct sockaddr* addr,
  88. unsigned flags) {
  89. uv_udp_send_t* req;
  90. int r;
  91. if (nread < 0) {
  92. ASSERT(0 && "unexpected error");
  93. }
  94. if (nread == 0) {
  95. /* Returning unused buffer */
  96. /* Don't count towards sv_recv_cb_called */
  97. ASSERT(addr == NULL);
  98. return;
  99. }
  100. CHECK_HANDLE(handle);
  101. ASSERT(flags == 0);
  102. ASSERT(addr != NULL);
  103. ASSERT(nread == 4);
  104. ASSERT(!memcmp("PING", buf.base, nread));
  105. /* FIXME? `uv_udp_recv_stop` does what it says: recv_cb is not called
  106. * anymore. That's problematic because the read buffer won't be returned
  107. * either... Not sure I like that but it's consistent with `uv_read_stop`.
  108. */
  109. r = uv_udp_recv_stop(handle);
  110. ASSERT(r == 0);
  111. req = malloc(sizeof *req);
  112. ASSERT(req != NULL);
  113. buf = uv_buf_init("PONG", 4);
  114. r = uv_udp_send(req,
  115. handle,
  116. &buf,
  117. 1,
  118. *(struct sockaddr_in*)addr,
  119. sv_send_cb);
  120. ASSERT(r == 0);
  121. sv_recv_cb_called++;
  122. }
  123. TEST_IMPL(udp_send_and_recv) {
  124. struct sockaddr_in addr;
  125. uv_udp_send_t req;
  126. uv_buf_t buf;
  127. int r;
  128. addr = uv_ip4_addr("0.0.0.0", TEST_PORT);
  129. r = uv_udp_init(uv_default_loop(), &server);
  130. ASSERT(r == 0);
  131. r = uv_udp_bind(&server, addr, 0);
  132. ASSERT(r == 0);
  133. r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb);
  134. ASSERT(r == 0);
  135. addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  136. r = uv_udp_init(uv_default_loop(), &client);
  137. ASSERT(r == 0);
  138. /* client sends "PING", expects "PONG" */
  139. buf = uv_buf_init("PING", 4);
  140. r = uv_udp_send(&req, &client, &buf, 1, addr, cl_send_cb);
  141. ASSERT(r == 0);
  142. ASSERT(close_cb_called == 0);
  143. ASSERT(cl_send_cb_called == 0);
  144. ASSERT(cl_recv_cb_called == 0);
  145. ASSERT(sv_send_cb_called == 0);
  146. ASSERT(sv_recv_cb_called == 0);
  147. uv_run(uv_default_loop());
  148. ASSERT(cl_send_cb_called == 1);
  149. ASSERT(cl_recv_cb_called == 1);
  150. ASSERT(sv_send_cb_called == 1);
  151. ASSERT(sv_recv_cb_called == 1);
  152. ASSERT(close_cb_called == 2);
  153. MAKE_VALGRIND_HAPPY();
  154. return 0;
  155. }