test-udp-send-unreachable.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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) == &client)
  28. static uv_udp_t client;
  29. static uv_timer_t timer;
  30. static int send_cb_called;
  31. static int recv_cb_called;
  32. static int close_cb_called;
  33. static int alloc_cb_called;
  34. static int timer_cb_called;
  35. static void alloc_cb(uv_handle_t* handle,
  36. size_t suggested_size,
  37. uv_buf_t* buf) {
  38. static char slab[65536];
  39. CHECK_HANDLE(handle);
  40. ASSERT(suggested_size <= sizeof(slab));
  41. buf->base = slab;
  42. buf->len = sizeof(slab);
  43. alloc_cb_called++;
  44. }
  45. static void close_cb(uv_handle_t* handle) {
  46. ASSERT(1 == uv_is_closing(handle));
  47. close_cb_called++;
  48. }
  49. static void send_cb(uv_udp_send_t* req, int status) {
  50. ASSERT(req != NULL);
  51. ASSERT(status == 0);
  52. CHECK_HANDLE(req->handle);
  53. send_cb_called++;
  54. }
  55. static void recv_cb(uv_udp_t* handle,
  56. ssize_t nread,
  57. const uv_buf_t* rcvbuf,
  58. const struct sockaddr* addr,
  59. unsigned flags) {
  60. CHECK_HANDLE(handle);
  61. recv_cb_called++;
  62. if (nread < 0) {
  63. ASSERT(0 && "unexpected error");
  64. } else if (nread == 0) {
  65. /* Returning unused buffer */
  66. ASSERT(addr == NULL);
  67. } else {
  68. ASSERT(addr != NULL);
  69. }
  70. }
  71. static void timer_cb(uv_timer_t* h) {
  72. ASSERT(h == &timer);
  73. timer_cb_called++;
  74. uv_close((uv_handle_t*) &client, close_cb);
  75. uv_close((uv_handle_t*) h, close_cb);
  76. }
  77. TEST_IMPL(udp_send_unreachable) {
  78. struct sockaddr_in addr;
  79. struct sockaddr_in addr2;
  80. uv_udp_send_t req1, req2;
  81. uv_buf_t buf;
  82. int r;
  83. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  84. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT_2, &addr2));
  85. r = uv_timer_init( uv_default_loop(), &timer );
  86. ASSERT(r == 0);
  87. r = uv_timer_start( &timer, timer_cb, 1000, 0 );
  88. ASSERT(r == 0);
  89. r = uv_udp_init(uv_default_loop(), &client);
  90. ASSERT(r == 0);
  91. r = uv_udp_bind(&client, (const struct sockaddr*) &addr2, 0);
  92. ASSERT(r == 0);
  93. r = uv_udp_recv_start(&client, alloc_cb, recv_cb);
  94. ASSERT(r == 0);
  95. /* client sends "PING", then "PANG" */
  96. buf = uv_buf_init("PING", 4);
  97. r = uv_udp_send(&req1,
  98. &client,
  99. &buf,
  100. 1,
  101. (const struct sockaddr*) &addr,
  102. send_cb);
  103. ASSERT(r == 0);
  104. buf = uv_buf_init("PANG", 4);
  105. r = uv_udp_send(&req2,
  106. &client,
  107. &buf,
  108. 1,
  109. (const struct sockaddr*) &addr,
  110. send_cb);
  111. ASSERT(r == 0);
  112. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  113. ASSERT(send_cb_called == 2);
  114. ASSERT(recv_cb_called == alloc_cb_called);
  115. ASSERT(timer_cb_called == 1);
  116. ASSERT(close_cb_called == 2);
  117. MAKE_VALGRIND_HAPPY();
  118. return 0;
  119. }