test-udp-ipv6.c 4.0 KB

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