test-tcp-write-fail.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #ifndef _WIN32
  26. # include <unistd.h>
  27. #endif
  28. static int connect_cb_called = 0;
  29. static int write_cb_called = 0;
  30. static int close_cb_called = 0;
  31. static uv_connect_t connect_req;
  32. static uv_write_t write_req;
  33. static void close_socket(uv_tcp_t* sock) {
  34. uv_os_fd_t fd;
  35. int r;
  36. r = uv_fileno((uv_handle_t*)sock, &fd);
  37. ASSERT(r == 0);
  38. #ifdef _WIN32
  39. r = closesocket((uv_os_sock_t)fd);
  40. #else
  41. r = close(fd);
  42. #endif
  43. ASSERT(r == 0);
  44. }
  45. static void close_cb(uv_handle_t* handle) {
  46. ASSERT(handle != NULL);
  47. close_cb_called++;
  48. }
  49. static void write_cb(uv_write_t* req, int status) {
  50. ASSERT(req != NULL);
  51. ASSERT(status != 0);
  52. fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));
  53. write_cb_called++;
  54. uv_close((uv_handle_t*)(req->handle), close_cb);
  55. }
  56. static void connect_cb(uv_connect_t* req, int status) {
  57. uv_buf_t buf;
  58. uv_stream_t* stream;
  59. int r;
  60. ASSERT(req == &connect_req);
  61. ASSERT(status == 0);
  62. stream = req->handle;
  63. connect_cb_called++;
  64. /* close the socket, the hard way */
  65. close_socket((uv_tcp_t*)stream);
  66. buf = uv_buf_init("hello\n", 6);
  67. r = uv_write(&write_req, stream, &buf, 1, write_cb);
  68. ASSERT(r == 0);
  69. }
  70. TEST_IMPL(tcp_write_fail) {
  71. struct sockaddr_in addr;
  72. uv_tcp_t client;
  73. int r;
  74. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  75. r = uv_tcp_init(uv_default_loop(), &client);
  76. ASSERT(r == 0);
  77. r = uv_tcp_connect(&connect_req,
  78. &client,
  79. (const struct sockaddr*) &addr,
  80. connect_cb);
  81. ASSERT(r == 0);
  82. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  83. ASSERT(connect_cb_called == 1);
  84. ASSERT(write_cb_called == 1);
  85. ASSERT(close_cb_called == 1);
  86. MAKE_VALGRIND_HAPPY();
  87. return 0;
  88. }