1
0

test-tcp-open.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 shutdown_cb_called = 0;
  30. static int connect_cb_called = 0;
  31. static int write_cb_called = 0;
  32. static int close_cb_called = 0;
  33. static uv_connect_t connect_req;
  34. static uv_shutdown_t shutdown_req;
  35. static uv_write_t write_req;
  36. static void startup(void) {
  37. #ifdef _WIN32
  38. struct WSAData wsa_data;
  39. int r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
  40. ASSERT(r == 0);
  41. #endif
  42. }
  43. static uv_os_sock_t create_tcp_socket(void) {
  44. uv_os_sock_t sock;
  45. sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
  46. #ifdef _WIN32
  47. ASSERT(sock != INVALID_SOCKET);
  48. #else
  49. ASSERT(sock >= 0);
  50. #endif
  51. #ifndef _WIN32
  52. {
  53. /* Allow reuse of the port. */
  54. int yes = 1;
  55. int r = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
  56. ASSERT(r == 0);
  57. }
  58. #endif
  59. return sock;
  60. }
  61. static void alloc_cb(uv_handle_t* handle,
  62. size_t suggested_size,
  63. uv_buf_t* buf) {
  64. static char slab[65536];
  65. ASSERT(suggested_size <= sizeof(slab));
  66. buf->base = slab;
  67. buf->len = sizeof(slab);
  68. }
  69. static void close_cb(uv_handle_t* handle) {
  70. ASSERT(handle != NULL);
  71. close_cb_called++;
  72. }
  73. static void shutdown_cb(uv_shutdown_t* req, int status) {
  74. ASSERT(req == &shutdown_req);
  75. ASSERT(status == 0);
  76. /* Now we wait for the EOF */
  77. shutdown_cb_called++;
  78. }
  79. static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
  80. ASSERT(tcp != NULL);
  81. if (nread >= 0) {
  82. ASSERT(nread == 4);
  83. ASSERT(memcmp("PING", buf->base, nread) == 0);
  84. }
  85. else {
  86. ASSERT(nread == UV_EOF);
  87. printf("GOT EOF\n");
  88. uv_close((uv_handle_t*)tcp, close_cb);
  89. }
  90. }
  91. static void write_cb(uv_write_t* req, int status) {
  92. ASSERT(req != NULL);
  93. if (status) {
  94. fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));
  95. ASSERT(0);
  96. }
  97. write_cb_called++;
  98. }
  99. static void connect_cb(uv_connect_t* req, int status) {
  100. uv_buf_t buf = uv_buf_init("PING", 4);
  101. uv_stream_t* stream;
  102. int r;
  103. ASSERT(req == &connect_req);
  104. ASSERT(status == 0);
  105. stream = req->handle;
  106. connect_cb_called++;
  107. r = uv_write(&write_req, stream, &buf, 1, write_cb);
  108. ASSERT(r == 0);
  109. /* Shutdown on drain. */
  110. r = uv_shutdown(&shutdown_req, stream, shutdown_cb);
  111. ASSERT(r == 0);
  112. /* Start reading */
  113. r = uv_read_start(stream, alloc_cb, read_cb);
  114. ASSERT(r == 0);
  115. }
  116. TEST_IMPL(tcp_open) {
  117. struct sockaddr_in addr;
  118. uv_tcp_t client;
  119. uv_os_sock_t sock;
  120. int r;
  121. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  122. startup();
  123. sock = create_tcp_socket();
  124. r = uv_tcp_init(uv_default_loop(), &client);
  125. ASSERT(r == 0);
  126. r = uv_tcp_open(&client, sock);
  127. ASSERT(r == 0);
  128. r = uv_tcp_connect(&connect_req,
  129. &client,
  130. (const struct sockaddr*) &addr,
  131. connect_cb);
  132. ASSERT(r == 0);
  133. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  134. ASSERT(shutdown_cb_called == 1);
  135. ASSERT(connect_cb_called == 1);
  136. ASSERT(write_cb_called == 1);
  137. ASSERT(close_cb_called == 1);
  138. MAKE_VALGRIND_HAPPY();
  139. return 0;
  140. }