PageRenderTime 55ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test-udp-ipv6.c

https://github.com/mmalecki/libuv
C | 173 lines | 111 code | 42 blank | 20 comment | 29 complexity | 1d41391cda190a1858f8266813b5c5a6 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 \
  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) {
  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. #if defined(__DragonFly__) || \
  115. defined(__FreeBSD__) || \
  116. defined(__OpenBSD__) || \
  117. defined(__NetBSD__)
  118. RETURN_SKIP("dual stack not enabled by default in this OS.");
  119. #else
  120. do_test(ipv6_recv_ok, 0);
  121. ASSERT(recv_cb_called == 1);
  122. ASSERT(send_cb_called == 1);
  123. return 0;
  124. #endif
  125. }
  126. TEST_IMPL(udp_ipv6_only) {
  127. do_test(ipv6_recv_fail, UV_UDP_IPV6ONLY);
  128. ASSERT(recv_cb_called == 0);
  129. ASSERT(send_cb_called == 1);
  130. return 0;
  131. }