1
0

test-udp-open.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
  58. static char slab[65536];
  59. ASSERT(suggested_size <= sizeof slab);
  60. return uv_buf_init(slab, sizeof slab);
  61. }
  62. static void close_cb(uv_handle_t* handle) {
  63. ASSERT(handle != NULL);
  64. close_cb_called++;
  65. }
  66. static void recv_cb(uv_udp_t* handle,
  67. ssize_t nread,
  68. uv_buf_t buf,
  69. struct sockaddr* addr,
  70. unsigned flags) {
  71. int r;
  72. if (nread < 0) {
  73. ASSERT(0 && "unexpected error");
  74. }
  75. if (nread == 0) {
  76. /* Returning unused buffer */
  77. /* Don't count towards sv_recv_cb_called */
  78. ASSERT(addr == NULL);
  79. return;
  80. }
  81. ASSERT(flags == 0);
  82. ASSERT(addr != NULL);
  83. ASSERT(nread == 4);
  84. ASSERT(memcmp("PING", buf.base, nread) == 0);
  85. r = uv_udp_recv_stop(handle);
  86. ASSERT(r == 0);
  87. uv_close((uv_handle_t*) handle, close_cb);
  88. }
  89. static void send_cb(uv_udp_send_t* req, int status) {
  90. ASSERT(req != NULL);
  91. ASSERT(status == 0);
  92. send_cb_called++;
  93. }
  94. TEST_IMPL(udp_open) {
  95. struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  96. uv_buf_t buf = uv_buf_init("PING", 4);
  97. uv_udp_t client;
  98. uv_os_sock_t sock;
  99. int r;
  100. startup();
  101. sock = create_udp_socket();
  102. r = uv_udp_init(uv_default_loop(), &client);
  103. ASSERT(r == 0);
  104. r = uv_udp_open(&client, sock);
  105. ASSERT(r == 0);
  106. r = uv_udp_bind(&client, addr, 0);
  107. ASSERT(r == 0);
  108. r = uv_udp_recv_start(&client, alloc_cb, recv_cb);
  109. ASSERT(r == 0);
  110. r = uv_udp_send(&send_req, &client, &buf, 1, addr, send_cb);
  111. ASSERT(r == 0);
  112. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  113. ASSERT(send_cb_called == 1);
  114. ASSERT(close_cb_called == 1);
  115. MAKE_VALGRIND_HAPPY();
  116. return 0;
  117. }