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