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