test-pipe-bind-error.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 == -1);
  46. ASSERT(uv_last_error(uv_default_loop()).code == UV_EADDRINUSE);
  47. r = uv_listen((uv_stream_t*)&server1, SOMAXCONN, NULL);
  48. ASSERT(r == 0);
  49. r = uv_listen((uv_stream_t*)&server2, SOMAXCONN, NULL);
  50. ASSERT(r == -1);
  51. ASSERT(uv_last_error(uv_default_loop()).code == UV_EINVAL);
  52. uv_close((uv_handle_t*)&server1, close_cb);
  53. uv_close((uv_handle_t*)&server2, close_cb);
  54. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  55. ASSERT(close_cb_called == 2);
  56. MAKE_VALGRIND_HAPPY();
  57. return 0;
  58. }
  59. TEST_IMPL(pipe_bind_error_addrnotavail) {
  60. uv_pipe_t server;
  61. int r;
  62. r = uv_pipe_init(uv_default_loop(), &server, 0);
  63. ASSERT(r == 0);
  64. r = uv_pipe_bind(&server, BAD_PIPENAME);
  65. ASSERT(r == -1);
  66. ASSERT(uv_last_error(uv_default_loop()).code == UV_EACCES);
  67. uv_close((uv_handle_t*)&server, close_cb);
  68. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  69. ASSERT(close_cb_called == 1);
  70. MAKE_VALGRIND_HAPPY();
  71. return 0;
  72. }
  73. TEST_IMPL(pipe_bind_error_inval) {
  74. uv_pipe_t server;
  75. int r;
  76. r = uv_pipe_init(uv_default_loop(), &server, 0);
  77. ASSERT(r == 0);
  78. r = uv_pipe_bind(&server, TEST_PIPENAME);
  79. ASSERT(r == 0);
  80. r = uv_pipe_bind(&server, TEST_PIPENAME_2);
  81. ASSERT(r == -1);
  82. ASSERT(uv_last_error(uv_default_loop()).code == UV_EINVAL);
  83. uv_close((uv_handle_t*)&server, close_cb);
  84. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  85. ASSERT(close_cb_called == 1);
  86. MAKE_VALGRIND_HAPPY();
  87. return 0;
  88. }
  89. TEST_IMPL(pipe_listen_without_bind) {
  90. uv_pipe_t server;
  91. int r;
  92. r = uv_pipe_init(uv_default_loop(), &server, 0);
  93. ASSERT(r == 0);
  94. r = uv_listen((uv_stream_t*)&server, SOMAXCONN, NULL);
  95. ASSERT(r == -1);
  96. ASSERT(uv_last_error(uv_default_loop()).code == UV_EINVAL);
  97. uv_close((uv_handle_t*)&server, close_cb);
  98. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  99. ASSERT(close_cb_called == 1);
  100. MAKE_VALGRIND_HAPPY();
  101. return 0;
  102. }