test-udp-ipv6.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 \
  28. || (uv_udp_t*)(handle) == &client \
  29. || (uv_timer_t*)(handle) == &timeout)
  30. #define CHECK_REQ(req) \
  31. ASSERT((req) == &req_);
  32. static uv_udp_t client;
  33. static uv_udp_t server;
  34. static uv_udp_send_t req_;
  35. static uv_timer_t timeout;
  36. static int send_cb_called;
  37. static int recv_cb_called;
  38. static int close_cb_called;
  39. static void alloc_cb(uv_handle_t* handle,
  40. size_t suggested_size,
  41. uv_buf_t* buf) {
  42. static char slab[65536];
  43. CHECK_HANDLE(handle);
  44. buf->base = slab;
  45. buf->len = sizeof(slab);
  46. }
  47. static void close_cb(uv_handle_t* handle) {
  48. CHECK_HANDLE(handle);
  49. close_cb_called++;
  50. }
  51. static void send_cb(uv_udp_send_t* req, int status) {
  52. CHECK_REQ(req);
  53. CHECK_HANDLE(req->handle);
  54. ASSERT(status == 0);
  55. send_cb_called++;
  56. }
  57. static void ipv6_recv_fail(uv_udp_t* handle,
  58. ssize_t nread,
  59. const uv_buf_t* buf,
  60. const struct sockaddr* addr,
  61. unsigned flags) {
  62. ASSERT(0 && "this function should not have been called");
  63. }
  64. static void ipv6_recv_ok(uv_udp_t* handle,
  65. ssize_t nread,
  66. const uv_buf_t* buf,
  67. const struct sockaddr* addr,
  68. unsigned flags) {
  69. CHECK_HANDLE(handle);
  70. ASSERT(nread >= 0);
  71. if (nread)
  72. recv_cb_called++;
  73. }
  74. static void timeout_cb(uv_timer_t* timer, int status) {
  75. uv_close((uv_handle_t*)&server, close_cb);
  76. uv_close((uv_handle_t*)&client, close_cb);
  77. uv_close((uv_handle_t*)&timeout, close_cb);
  78. }
  79. static void do_test(uv_udp_recv_cb recv_cb, int bind_flags) {
  80. struct sockaddr_in6 addr6;
  81. struct sockaddr_in addr;
  82. uv_buf_t buf;
  83. int r;
  84. ASSERT(0 == uv_ip6_addr("::0", TEST_PORT, &addr6));
  85. r = uv_udp_init(uv_default_loop(), &server);
  86. ASSERT(r == 0);
  87. r = uv_udp_bind(&server, (const struct sockaddr*) &addr6, bind_flags);
  88. ASSERT(r == 0);
  89. r = uv_udp_recv_start(&server, alloc_cb, recv_cb);
  90. ASSERT(r == 0);
  91. r = uv_udp_init(uv_default_loop(), &client);
  92. ASSERT(r == 0);
  93. buf = uv_buf_init("PING", 4);
  94. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  95. r = uv_udp_send(&req_,
  96. &client,
  97. &buf,
  98. 1,
  99. (const struct sockaddr*) &addr,
  100. send_cb);
  101. ASSERT(r == 0);
  102. r = uv_timer_init(uv_default_loop(), &timeout);
  103. ASSERT(r == 0);
  104. r = uv_timer_start(&timeout, timeout_cb, 500, 0);
  105. ASSERT(r == 0);
  106. ASSERT(close_cb_called == 0);
  107. ASSERT(send_cb_called == 0);
  108. ASSERT(recv_cb_called == 0);
  109. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  110. ASSERT(close_cb_called == 3);
  111. MAKE_VALGRIND_HAPPY();
  112. }
  113. TEST_IMPL(udp_dual_stack) {
  114. do_test(ipv6_recv_ok, 0);
  115. ASSERT(recv_cb_called == 1);
  116. ASSERT(send_cb_called == 1);
  117. return 0;
  118. }
  119. TEST_IMPL(udp_ipv6_only) {
  120. do_test(ipv6_recv_fail, UV_UDP_IPV6ONLY);
  121. ASSERT(recv_cb_called == 0);
  122. ASSERT(send_cb_called == 1);
  123. return 0;
  124. }