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 #ifdef __APPLE__
     26      1.1  christos 
     27      1.1  christos #include <sys/ioctl.h>
     28      1.1  christos #include <string.h>
     29      1.1  christos 
     30      1.1  christos static int read_count;
     31      1.1  christos 
     32      1.1  christos 
     33      1.1  christos static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
     34      1.1  christos   static char slab[1024];
     35      1.1  christos   buf->base = slab;
     36      1.1  christos   buf->len = sizeof(slab);
     37      1.1  christos }
     38      1.1  christos 
     39      1.1  christos 
     40      1.1  christos static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
     41      1.1  christos   fprintf(stdout, "got data %d\n", ++read_count);
     42      1.1  christos   fflush(stdout);
     43      1.1  christos 
     44      1.1  christos   if (read_count == 3)
     45      1.1  christos     uv_close((uv_handle_t*) stream, NULL);
     46      1.1  christos }
     47      1.1  christos 
     48      1.1  christos 
     49      1.1  christos TEST_IMPL(osx_select) {
     50      1.1  christos   int r;
     51      1.1  christos   int fd;
     52      1.1  christos   size_t i;
     53      1.1  christos   size_t len;
     54      1.1  christos   const char* str;
     55      1.1  christos   uv_tty_t tty;
     56      1.1  christos 
     57      1.1  christos   fd = open("/dev/tty", O_RDONLY);
     58      1.1  christos   if (fd < 0) {
     59      1.1  christos     fprintf(stderr, "Cannot open /dev/tty as read-only: %s\n", strerror(errno));
     60      1.1  christos     fflush(stderr);
     61      1.1  christos     return TEST_SKIP;
     62      1.1  christos   }
     63      1.1  christos 
     64      1.1  christos   r = uv_tty_init(uv_default_loop(), &tty, fd, 1);
     65  1.1.1.2  christos   ASSERT_OK(r);
     66      1.1  christos 
     67      1.1  christos   uv_read_start((uv_stream_t*) &tty, alloc_cb, read_cb);
     68      1.1  christos 
     69      1.1  christos   /* Emulate user-input */
     70      1.1  christos   str = "got some input\n"
     71      1.1  christos         "with a couple of lines\n"
     72      1.1  christos         "feel pretty happy\n";
     73      1.1  christos   for (i = 0, len = strlen(str); i < len; i++) {
     74      1.1  christos     r = ioctl(fd, TIOCSTI, str + i);
     75  1.1.1.2  christos     ASSERT_OK(r);
     76      1.1  christos   }
     77      1.1  christos 
     78      1.1  christos   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
     79      1.1  christos 
     80  1.1.1.2  christos   ASSERT_EQ(3, read_count);
     81      1.1  christos 
     82  1.1.1.2  christos   MAKE_VALGRIND_HAPPY(uv_default_loop());
     83      1.1  christos   return 0;
     84      1.1  christos }
     85      1.1  christos 
     86      1.1  christos 
     87      1.1  christos TEST_IMPL(osx_select_many_fds) {
     88      1.1  christos   int r;
     89      1.1  christos   int fd;
     90      1.1  christos   size_t i;
     91      1.1  christos   size_t len;
     92      1.1  christos   const char* str;
     93      1.1  christos   struct sockaddr_in addr;
     94      1.1  christos   uv_tty_t tty;
     95      1.1  christos   uv_tcp_t tcps[1500];
     96      1.1  christos 
     97      1.1  christos   TEST_FILE_LIMIT(ARRAY_SIZE(tcps) + 100);
     98      1.1  christos 
     99      1.1  christos   r = uv_ip4_addr("127.0.0.1", 0, &addr);
    100  1.1.1.2  christos   ASSERT_OK(r);
    101      1.1  christos 
    102      1.1  christos   for (i = 0; i < ARRAY_SIZE(tcps); i++) {
    103      1.1  christos     r = uv_tcp_init(uv_default_loop(), &tcps[i]);
    104  1.1.1.2  christos     ASSERT_OK(r);
    105      1.1  christos     r = uv_tcp_bind(&tcps[i], (const struct sockaddr *) &addr, 0);
    106  1.1.1.2  christos     ASSERT_OK(r);
    107      1.1  christos     uv_unref((uv_handle_t*) &tcps[i]);
    108      1.1  christos   }
    109      1.1  christos 
    110      1.1  christos   fd = open("/dev/tty", O_RDONLY);
    111      1.1  christos   if (fd < 0) {
    112      1.1  christos     fprintf(stderr, "Cannot open /dev/tty as read-only: %s\n", strerror(errno));
    113      1.1  christos     fflush(stderr);
    114      1.1  christos     return TEST_SKIP;
    115      1.1  christos   }
    116      1.1  christos 
    117      1.1  christos   r = uv_tty_init(uv_default_loop(), &tty, fd, 1);
    118  1.1.1.2  christos   ASSERT_OK(r);
    119      1.1  christos 
    120      1.1  christos   r = uv_read_start((uv_stream_t*) &tty, alloc_cb, read_cb);
    121  1.1.1.2  christos   ASSERT_OK(r);
    122      1.1  christos 
    123      1.1  christos   /* Emulate user-input */
    124      1.1  christos   str = "got some input\n"
    125      1.1  christos         "with a couple of lines\n"
    126      1.1  christos         "feel pretty happy\n";
    127      1.1  christos   for (i = 0, len = strlen(str); i < len; i++) {
    128      1.1  christos     r = ioctl(fd, TIOCSTI, str + i);
    129  1.1.1.2  christos     ASSERT_OK(r);
    130      1.1  christos   }
    131      1.1  christos 
    132      1.1  christos   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    133      1.1  christos 
    134  1.1.1.2  christos   ASSERT_EQ(3, read_count);
    135      1.1  christos 
    136  1.1.1.2  christos   MAKE_VALGRIND_HAPPY(uv_default_loop());
    137      1.1  christos   return 0;
    138      1.1  christos }
    139      1.1  christos 
    140      1.1  christos #endif /* __APPLE__ */
    141