test-udp-try-send.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 || (uv_udp_t*)(handle) == &client)
  28. static uv_udp_t server;
  29. static uv_udp_t client;
  30. static int sv_recv_cb_called;
  31. static int close_cb_called;
  32. static void alloc_cb(uv_handle_t* handle,
  33. size_t suggested_size,
  34. uv_buf_t* buf) {
  35. static char slab[65536];
  36. CHECK_HANDLE(handle);
  37. ASSERT(suggested_size <= sizeof(slab));
  38. buf->base = slab;
  39. buf->len = sizeof(slab);
  40. }
  41. static void close_cb(uv_handle_t* handle) {
  42. CHECK_HANDLE(handle);
  43. ASSERT(uv_is_closing(handle));
  44. close_cb_called++;
  45. }
  46. static void sv_recv_cb(uv_udp_t* handle,
  47. ssize_t nread,
  48. const uv_buf_t* rcvbuf,
  49. const struct sockaddr* addr,
  50. unsigned flags) {
  51. ASSERT(nread > 0);
  52. if (nread == 0) {
  53. ASSERT(addr == NULL);
  54. return;
  55. }
  56. ASSERT(nread == 4);
  57. ASSERT(addr != NULL);
  58. ASSERT(memcmp("EXIT", rcvbuf->base, nread) == 0);
  59. uv_close((uv_handle_t*) handle, close_cb);
  60. uv_close((uv_handle_t*) &client, close_cb);
  61. sv_recv_cb_called++;
  62. }
  63. TEST_IMPL(udp_try_send) {
  64. struct sockaddr_in addr;
  65. static char buffer[64 * 1024];
  66. uv_buf_t buf;
  67. int r;
  68. ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
  69. r = uv_udp_init(uv_default_loop(), &server);
  70. ASSERT(r == 0);
  71. r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
  72. ASSERT(r == 0);
  73. r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb);
  74. ASSERT(r == 0);
  75. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  76. r = uv_udp_init(uv_default_loop(), &client);
  77. ASSERT(r == 0);
  78. buf = uv_buf_init(buffer, sizeof(buffer));
  79. r = uv_udp_try_send(&client, &buf, 1, (const struct sockaddr*) &addr);
  80. ASSERT(r == UV_EMSGSIZE);
  81. buf = uv_buf_init("EXIT", 4);
  82. r = uv_udp_try_send(&client, &buf, 1, (const struct sockaddr*) &addr);
  83. ASSERT(r == 4);
  84. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  85. ASSERT(close_cb_called == 2);
  86. ASSERT(sv_recv_cb_called == 1);
  87. ASSERT(client.send_queue_size == 0);
  88. ASSERT(server.send_queue_size == 0);
  89. MAKE_VALGRIND_HAPPY();
  90. return 0;
  91. }