test-handle-fileno.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. static int get_tty_fd(void) {
  24. /* Make sure we have an FD that refers to a tty */
  25. #ifdef _WIN32
  26. HANDLE handle;
  27. handle = CreateFileA("conin$",
  28. GENERIC_READ | GENERIC_WRITE,
  29. FILE_SHARE_READ | FILE_SHARE_WRITE,
  30. NULL,
  31. OPEN_EXISTING,
  32. FILE_ATTRIBUTE_NORMAL,
  33. NULL);
  34. if (handle == INVALID_HANDLE_VALUE)
  35. return -1;
  36. return _open_osfhandle((intptr_t) handle, 0);
  37. #else /* unix */
  38. return open("/dev/tty", O_RDONLY, 0);
  39. #endif
  40. }
  41. TEST_IMPL(handle_fileno) {
  42. int r;
  43. int tty_fd;
  44. struct sockaddr_in addr;
  45. uv_os_fd_t fd;
  46. uv_tcp_t tcp;
  47. uv_udp_t udp;
  48. uv_pipe_t pipe;
  49. uv_tty_t tty;
  50. uv_idle_t idle;
  51. uv_loop_t* loop;
  52. loop = uv_default_loop();
  53. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  54. r = uv_idle_init(loop, &idle);
  55. ASSERT(r == 0);
  56. r = uv_fileno((uv_handle_t*) &idle, &fd);
  57. ASSERT(r == UV_EINVAL);
  58. uv_close((uv_handle_t*) &idle, NULL);
  59. r = uv_tcp_init(loop, &tcp);
  60. ASSERT(r == 0);
  61. r = uv_fileno((uv_handle_t*) &tcp, &fd);
  62. ASSERT(r == UV_EBADF);
  63. r = uv_tcp_bind(&tcp, (const struct sockaddr*) &addr, 0);
  64. ASSERT(r == 0);
  65. r = uv_fileno((uv_handle_t*) &tcp, &fd);
  66. ASSERT(r == 0);
  67. uv_close((uv_handle_t*) &tcp, NULL);
  68. r = uv_fileno((uv_handle_t*) &tcp, &fd);
  69. ASSERT(r == UV_EBADF);
  70. r = uv_udp_init(loop, &udp);
  71. ASSERT(r == 0);
  72. r = uv_fileno((uv_handle_t*) &udp, &fd);
  73. ASSERT(r == UV_EBADF);
  74. r = uv_udp_bind(&udp, (const struct sockaddr*) &addr, 0);
  75. ASSERT(r == 0);
  76. r = uv_fileno((uv_handle_t*) &udp, &fd);
  77. ASSERT(r == 0);
  78. uv_close((uv_handle_t*) &udp, NULL);
  79. r = uv_fileno((uv_handle_t*) &udp, &fd);
  80. ASSERT(r == UV_EBADF);
  81. r = uv_pipe_init(loop, &pipe, 0);
  82. ASSERT(r == 0);
  83. r = uv_fileno((uv_handle_t*) &pipe, &fd);
  84. ASSERT(r == UV_EBADF);
  85. r = uv_pipe_bind(&pipe, TEST_PIPENAME);
  86. ASSERT(r == 0);
  87. r = uv_fileno((uv_handle_t*) &pipe, &fd);
  88. ASSERT(r == 0);
  89. uv_close((uv_handle_t*) &pipe, NULL);
  90. r = uv_fileno((uv_handle_t*) &pipe, &fd);
  91. ASSERT(r == UV_EBADF);
  92. tty_fd = get_tty_fd();
  93. if (tty_fd < 0) {
  94. fprintf(stderr, "Cannot open a TTY fd");
  95. fflush(stderr);
  96. } else {
  97. r = uv_tty_init(loop, &tty, tty_fd, 0);
  98. ASSERT(r == 0);
  99. ASSERT(uv_is_readable((uv_stream_t*) &tty));
  100. ASSERT(!uv_is_writable((uv_stream_t*) &tty));
  101. r = uv_fileno((uv_handle_t*) &tty, &fd);
  102. ASSERT(r == 0);
  103. uv_close((uv_handle_t*) &tty, NULL);
  104. r = uv_fileno((uv_handle_t*) &tty, &fd);
  105. ASSERT(r == UV_EBADF);
  106. ASSERT(!uv_is_readable((uv_stream_t*) &tty));
  107. ASSERT(!uv_is_writable((uv_stream_t*) &tty));
  108. }
  109. uv_run(loop, UV_RUN_DEFAULT);
  110. MAKE_VALGRIND_HAPPY();
  111. return 0;
  112. }