Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
      2      1.1  christos  *
      3      1.1  christos  * Permission is hereby granted, free of charge, to any person obtaining a copy
      4      1.1  christos  * of this software and associated documentation files (the "Software"), to
      5      1.1  christos  * deal in the Software without restriction, including without limitation the
      6      1.1  christos  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
      7      1.1  christos  * sell copies of the Software, and to permit persons to whom the Software is
      8      1.1  christos  * furnished to do so, subject to the following conditions:
      9      1.1  christos  *
     10      1.1  christos  * The above copyright notice and this permission notice shall be included in
     11      1.1  christos  * all copies or substantial portions of the Software.
     12      1.1  christos  *
     13      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     14      1.1  christos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     15      1.1  christos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     16      1.1  christos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     17      1.1  christos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     18      1.1  christos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     19      1.1  christos  * IN THE SOFTWARE.
     20      1.1  christos  */
     21      1.1  christos 
     22      1.1  christos #include "uv.h"
     23      1.1  christos #include "task.h"
     24      1.1  christos 
     25      1.1  christos 
     26      1.1  christos static int get_tty_fd(void) {
     27      1.1  christos   /* Make sure we have an FD that refers to a tty */
     28      1.1  christos #ifdef _WIN32
     29      1.1  christos   HANDLE handle;
     30      1.1  christos   handle = CreateFileA("conin$",
     31      1.1  christos                        GENERIC_READ | GENERIC_WRITE,
     32      1.1  christos                        FILE_SHARE_READ | FILE_SHARE_WRITE,
     33      1.1  christos                        NULL,
     34      1.1  christos                        OPEN_EXISTING,
     35      1.1  christos                        FILE_ATTRIBUTE_NORMAL,
     36      1.1  christos                        NULL);
     37      1.1  christos   if (handle == INVALID_HANDLE_VALUE)
     38      1.1  christos     return -1;
     39      1.1  christos   return _open_osfhandle((intptr_t) handle, 0);
     40      1.1  christos #else /* unix */
     41      1.1  christos   return open("/dev/tty", O_RDONLY, 0);
     42      1.1  christos #endif
     43      1.1  christos }
     44      1.1  christos 
     45      1.1  christos 
     46      1.1  christos TEST_IMPL(handle_fileno) {
     47      1.1  christos   int r;
     48      1.1  christos   int tty_fd;
     49      1.1  christos   struct sockaddr_in addr;
     50      1.1  christos   uv_os_fd_t fd;
     51      1.1  christos   uv_tcp_t tcp;
     52      1.1  christos   uv_udp_t udp;
     53      1.1  christos   uv_pipe_t pipe;
     54      1.1  christos   uv_tty_t tty;
     55      1.1  christos   uv_idle_t idle;
     56      1.1  christos   uv_loop_t* loop;
     57      1.1  christos 
     58      1.1  christos   loop = uv_default_loop();
     59  1.1.1.2  christos   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
     60      1.1  christos 
     61      1.1  christos   r = uv_idle_init(loop, &idle);
     62  1.1.1.2  christos   ASSERT_OK(r);
     63      1.1  christos   r = uv_fileno((uv_handle_t*) &idle, &fd);
     64  1.1.1.2  christos   ASSERT_EQ(r, UV_EINVAL);
     65      1.1  christos   uv_close((uv_handle_t*) &idle, NULL);
     66      1.1  christos 
     67      1.1  christos   r = uv_tcp_init(loop, &tcp);
     68  1.1.1.2  christos   ASSERT_OK(r);
     69      1.1  christos   r = uv_fileno((uv_handle_t*) &tcp, &fd);
     70  1.1.1.2  christos   ASSERT_EQ(r, UV_EBADF);
     71      1.1  christos   r = uv_tcp_bind(&tcp, (const struct sockaddr*) &addr, 0);
     72  1.1.1.2  christos   ASSERT_OK(r);
     73      1.1  christos   r = uv_fileno((uv_handle_t*) &tcp, &fd);
     74  1.1.1.2  christos   ASSERT_OK(r);
     75      1.1  christos   uv_close((uv_handle_t*) &tcp, NULL);
     76      1.1  christos   r = uv_fileno((uv_handle_t*) &tcp, &fd);
     77  1.1.1.2  christos   ASSERT_EQ(r, UV_EBADF);
     78      1.1  christos 
     79      1.1  christos   r = uv_udp_init(loop, &udp);
     80  1.1.1.2  christos   ASSERT_OK(r);
     81      1.1  christos   r = uv_fileno((uv_handle_t*) &udp, &fd);
     82  1.1.1.2  christos   ASSERT_EQ(r, UV_EBADF);
     83      1.1  christos   r = uv_udp_bind(&udp, (const struct sockaddr*) &addr, 0);
     84  1.1.1.2  christos   ASSERT_OK(r);
     85      1.1  christos   r = uv_fileno((uv_handle_t*) &udp, &fd);
     86  1.1.1.2  christos   ASSERT_OK(r);
     87      1.1  christos   uv_close((uv_handle_t*) &udp, NULL);
     88      1.1  christos   r = uv_fileno((uv_handle_t*) &udp, &fd);
     89  1.1.1.2  christos   ASSERT_EQ(r, UV_EBADF);
     90      1.1  christos 
     91      1.1  christos   r = uv_pipe_init(loop, &pipe, 0);
     92  1.1.1.2  christos   ASSERT_OK(r);
     93      1.1  christos   r = uv_fileno((uv_handle_t*) &pipe, &fd);
     94  1.1.1.2  christos   ASSERT_EQ(r, UV_EBADF);
     95      1.1  christos   r = uv_pipe_bind(&pipe, TEST_PIPENAME);
     96  1.1.1.2  christos   ASSERT_OK(r);
     97      1.1  christos   r = uv_fileno((uv_handle_t*) &pipe, &fd);
     98  1.1.1.2  christos   ASSERT_OK(r);
     99      1.1  christos   uv_close((uv_handle_t*) &pipe, NULL);
    100      1.1  christos   r = uv_fileno((uv_handle_t*) &pipe, &fd);
    101  1.1.1.2  christos   ASSERT_EQ(r, UV_EBADF);
    102      1.1  christos 
    103      1.1  christos   tty_fd = get_tty_fd();
    104      1.1  christos   if (tty_fd < 0) {
    105      1.1  christos     fprintf(stderr, "Cannot open a TTY fd");
    106      1.1  christos     fflush(stderr);
    107      1.1  christos   } else {
    108      1.1  christos     r = uv_tty_init(loop, &tty, tty_fd, 0);
    109  1.1.1.2  christos     ASSERT_OK(r);
    110      1.1  christos     ASSERT(uv_is_readable((uv_stream_t*) &tty));
    111      1.1  christos     ASSERT(!uv_is_writable((uv_stream_t*) &tty));
    112      1.1  christos     r = uv_fileno((uv_handle_t*) &tty, &fd);
    113  1.1.1.2  christos     ASSERT_OK(r);
    114      1.1  christos     uv_close((uv_handle_t*) &tty, NULL);
    115      1.1  christos     r = uv_fileno((uv_handle_t*) &tty, &fd);
    116  1.1.1.2  christos     ASSERT_EQ(r, UV_EBADF);
    117      1.1  christos     ASSERT(!uv_is_readable((uv_stream_t*) &tty));
    118      1.1  christos     ASSERT(!uv_is_writable((uv_stream_t*) &tty));
    119      1.1  christos   }
    120      1.1  christos 
    121      1.1  christos   uv_run(loop, UV_RUN_DEFAULT);
    122      1.1  christos 
    123  1.1.1.2  christos   MAKE_VALGRIND_HAPPY(loop);
    124      1.1  christos   return 0;
    125      1.1  christos }
    126