test-udp-alloc-cb-fail.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright libuv project 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 void sv_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. buf->base = slab;
  41. buf->len = sizeof(slab);
  42. }
  43. static void cl_alloc_cb(uv_handle_t* handle,
  44. size_t suggested_size,
  45. uv_buf_t* buf) {
  46. /* Do nothing, recv_cb should be called with UV_ENOBUFS. */
  47. }
  48. static void close_cb(uv_handle_t* handle) {
  49. CHECK_HANDLE(handle);
  50. ASSERT(1 == uv_is_closing(handle));
  51. close_cb_called++;
  52. }
  53. static void cl_recv_cb(uv_udp_t* handle,
  54. ssize_t nread,
  55. const uv_buf_t* buf,
  56. const struct sockaddr* addr,
  57. unsigned flags) {
  58. CHECK_HANDLE(handle);
  59. ASSERT(flags == 0);
  60. ASSERT(nread == UV_ENOBUFS);
  61. cl_recv_cb_called++;
  62. uv_close((uv_handle_t*) handle, close_cb);
  63. }
  64. static void cl_send_cb(uv_udp_send_t* req, int status) {
  65. int r;
  66. ASSERT(req != NULL);
  67. ASSERT(status == 0);
  68. CHECK_HANDLE(req->handle);
  69. r = uv_udp_recv_start(req->handle, cl_alloc_cb, cl_recv_cb);
  70. ASSERT(r == 0);
  71. cl_send_cb_called++;
  72. }
  73. static void sv_send_cb(uv_udp_send_t* req, int status) {
  74. ASSERT(req != NULL);
  75. ASSERT(status == 0);
  76. CHECK_HANDLE(req->handle);
  77. uv_close((uv_handle_t*) req->handle, close_cb);
  78. free(req);
  79. sv_send_cb_called++;
  80. }
  81. static void sv_recv_cb(uv_udp_t* handle,
  82. ssize_t nread,
  83. const uv_buf_t* rcvbuf,
  84. const struct sockaddr* addr,
  85. unsigned flags) {
  86. uv_udp_send_t* req;
  87. uv_buf_t sndbuf;
  88. int r;
  89. if (nread < 0) {
  90. ASSERT(0 && "unexpected error");
  91. }
  92. if (nread == 0) {
  93. /* Returning unused buffer. Don't count towards sv_recv_cb_called */
  94. ASSERT(addr == NULL);
  95. return;
  96. }
  97. CHECK_HANDLE(handle);
  98. ASSERT(flags == 0);
  99. ASSERT(addr != NULL);
  100. ASSERT(nread == 4);
  101. ASSERT(!memcmp("PING", rcvbuf->base, nread));
  102. r = uv_udp_recv_stop(handle);
  103. ASSERT(r == 0);
  104. req = malloc(sizeof *req);
  105. ASSERT(req != NULL);
  106. sndbuf = uv_buf_init("PONG", 4);
  107. r = uv_udp_send(req, handle, &sndbuf, 1, addr, sv_send_cb);
  108. ASSERT(r == 0);
  109. sv_recv_cb_called++;
  110. }
  111. TEST_IMPL(udp_alloc_cb_fail) {
  112. struct sockaddr_in addr;
  113. uv_udp_send_t req;
  114. uv_buf_t buf;
  115. int r;
  116. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  117. r = uv_udp_init(uv_default_loop(), &server);
  118. ASSERT(r == 0);
  119. r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
  120. ASSERT(r == 0);
  121. r = uv_udp_recv_start(&server, sv_alloc_cb, sv_recv_cb);
  122. ASSERT(r == 0);
  123. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  124. r = uv_udp_init(uv_default_loop(), &client);
  125. ASSERT(r == 0);
  126. buf = uv_buf_init("PING", 4);
  127. r = uv_udp_send(&req,
  128. &client,
  129. &buf,
  130. 1,
  131. (const struct sockaddr*) &addr,
  132. cl_send_cb);
  133. ASSERT(r == 0);
  134. ASSERT(close_cb_called == 0);
  135. ASSERT(cl_send_cb_called == 0);
  136. ASSERT(cl_recv_cb_called == 0);
  137. ASSERT(sv_send_cb_called == 0);
  138. ASSERT(sv_recv_cb_called == 0);
  139. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  140. ASSERT(cl_send_cb_called == 1);
  141. ASSERT(cl_recv_cb_called == 1);
  142. ASSERT(sv_send_cb_called == 1);
  143. ASSERT(sv_recv_cb_called == 1);
  144. ASSERT(close_cb_called == 2);
  145. MAKE_VALGRIND_HAPPY();
  146. return 0;
  147. }