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