test-udp-open.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef _WIN32
  27. # include <unistd.h>
  28. #endif
  29. static int send_cb_called = 0;
  30. static int close_cb_called = 0;
  31. static uv_udp_send_t send_req;
  32. static void startup(void) {
  33. #ifdef _WIN32
  34. struct WSAData wsa_data;
  35. int r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
  36. ASSERT(r == 0);
  37. #endif
  38. }
  39. static uv_os_sock_t create_udp_socket(void) {
  40. uv_os_sock_t sock;
  41. sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  42. #ifdef _WIN32
  43. ASSERT(sock != INVALID_SOCKET);
  44. #else
  45. ASSERT(sock >= 0);
  46. #endif
  47. #ifndef _WIN32
  48. {
  49. /* Allow reuse of the port. */
  50. int yes = 1;
  51. int r = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
  52. ASSERT(r == 0);
  53. }
  54. #endif
  55. return sock;
  56. }
  57. static void alloc_cb(uv_handle_t* handle,
  58. size_t suggested_size,
  59. uv_buf_t* buf) {
  60. static char slab[65536];
  61. ASSERT(suggested_size <= sizeof(slab));
  62. buf->base = slab;
  63. buf->len = sizeof(slab);
  64. }
  65. static void close_cb(uv_handle_t* handle) {
  66. ASSERT(handle != NULL);
  67. close_cb_called++;
  68. }
  69. static void recv_cb(uv_udp_t* handle,
  70. ssize_t nread,
  71. const uv_buf_t* buf,
  72. const struct sockaddr* addr,
  73. unsigned flags) {
  74. int r;
  75. if (nread < 0) {
  76. ASSERT(0 && "unexpected error");
  77. }
  78. if (nread == 0) {
  79. /* Returning unused buffer */
  80. /* Don't count towards sv_recv_cb_called */
  81. ASSERT(addr == NULL);
  82. return;
  83. }
  84. ASSERT(flags == 0);
  85. ASSERT(addr != NULL);
  86. ASSERT(nread == 4);
  87. ASSERT(memcmp("PING", buf->base, nread) == 0);
  88. r = uv_udp_recv_stop(handle);
  89. ASSERT(r == 0);
  90. uv_close((uv_handle_t*) handle, close_cb);
  91. }
  92. static void send_cb(uv_udp_send_t* req, int status) {
  93. ASSERT(req != NULL);
  94. ASSERT(status == 0);
  95. send_cb_called++;
  96. }
  97. TEST_IMPL(udp_open) {
  98. struct sockaddr_in addr;
  99. uv_buf_t buf = uv_buf_init("PING", 4);
  100. uv_udp_t client;
  101. uv_os_sock_t sock;
  102. int r;
  103. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  104. startup();
  105. sock = create_udp_socket();
  106. r = uv_udp_init(uv_default_loop(), &client);
  107. ASSERT(r == 0);
  108. r = uv_udp_open(&client, sock);
  109. ASSERT(r == 0);
  110. r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0);
  111. ASSERT(r == 0);
  112. r = uv_udp_recv_start(&client, alloc_cb, recv_cb);
  113. ASSERT(r == 0);
  114. r = uv_udp_send(&send_req,
  115. &client,
  116. &buf,
  117. 1,
  118. (const struct sockaddr*) &addr,
  119. send_cb);
  120. ASSERT(r == 0);
  121. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  122. ASSERT(send_cb_called == 1);
  123. ASSERT(close_cb_called == 1);
  124. MAKE_VALGRIND_HAPPY();
  125. return 0;
  126. }