test-pipe-bind-error.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifdef _WIN32
  26. # define BAD_PIPENAME "bad-pipe"
  27. #else
  28. # define BAD_PIPENAME "/path/to/unix/socket/that/really/should/not/be/there"
  29. #endif
  30. static int close_cb_called = 0;
  31. static void close_cb(uv_handle_t* handle) {
  32. ASSERT(handle != NULL);
  33. close_cb_called++;
  34. }
  35. TEST_IMPL(pipe_bind_error_addrinuse) {
  36. uv_pipe_t server1, server2;
  37. int r;
  38. r = uv_pipe_init(uv_default_loop(), &server1, 0);
  39. ASSERT(r == 0);
  40. r = uv_pipe_bind(&server1, TEST_PIPENAME);
  41. ASSERT(r == 0);
  42. r = uv_pipe_init(uv_default_loop(), &server2, 0);
  43. ASSERT(r == 0);
  44. r = uv_pipe_bind(&server2, TEST_PIPENAME);
  45. ASSERT(r == UV_EADDRINUSE);
  46. r = uv_listen((uv_stream_t*)&server1, SOMAXCONN, NULL);
  47. ASSERT(r == 0);
  48. r = uv_listen((uv_stream_t*)&server2, SOMAXCONN, NULL);
  49. ASSERT(r == UV_EINVAL);
  50. uv_close((uv_handle_t*)&server1, close_cb);
  51. uv_close((uv_handle_t*)&server2, close_cb);
  52. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  53. ASSERT(close_cb_called == 2);
  54. MAKE_VALGRIND_HAPPY();
  55. return 0;
  56. }
  57. TEST_IMPL(pipe_bind_error_addrnotavail) {
  58. uv_pipe_t server;
  59. int r;
  60. r = uv_pipe_init(uv_default_loop(), &server, 0);
  61. ASSERT(r == 0);
  62. r = uv_pipe_bind(&server, BAD_PIPENAME);
  63. ASSERT(r == UV_EACCES);
  64. uv_close((uv_handle_t*)&server, close_cb);
  65. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  66. ASSERT(close_cb_called == 1);
  67. MAKE_VALGRIND_HAPPY();
  68. return 0;
  69. }
  70. TEST_IMPL(pipe_bind_error_inval) {
  71. uv_pipe_t server;
  72. int r;
  73. r = uv_pipe_init(uv_default_loop(), &server, 0);
  74. ASSERT(r == 0);
  75. r = uv_pipe_bind(&server, TEST_PIPENAME);
  76. ASSERT(r == 0);
  77. r = uv_pipe_bind(&server, TEST_PIPENAME_2);
  78. ASSERT(r == UV_EINVAL);
  79. uv_close((uv_handle_t*)&server, close_cb);
  80. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  81. ASSERT(close_cb_called == 1);
  82. MAKE_VALGRIND_HAPPY();
  83. return 0;
  84. }
  85. TEST_IMPL(pipe_listen_without_bind) {
  86. uv_pipe_t server;
  87. int r;
  88. r = uv_pipe_init(uv_default_loop(), &server, 0);
  89. ASSERT(r == 0);
  90. r = uv_listen((uv_stream_t*)&server, SOMAXCONN, NULL);
  91. ASSERT(r == UV_EINVAL);
  92. uv_close((uv_handle_t*)&server, close_cb);
  93. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  94. ASSERT(close_cb_called == 1);
  95. MAKE_VALGRIND_HAPPY();
  96. return 0;
  97. }