libuv-svace-build
468 строк · 13.3 Кб
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 _WIN3226# include <io.h>27# include <windows.h>28#else /* Unix */29# include <fcntl.h>30# include <unistd.h>31# if defined(__linux__) && !defined(__ANDROID__)32# include <pty.h>33# elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)34# include <util.h>35# elif defined(__FreeBSD__) || defined(__DragonFly__)36# include <libutil.h>37# endif38#endif39
40#include <string.h>41#include <errno.h>42
43
44TEST_IMPL(tty) {45int r, width, height;46int ttyin_fd, ttyout_fd;47uv_tty_t tty_in, tty_out;48uv_loop_t* loop = uv_default_loop();49
50/* Make sure we have an FD that refers to a tty */51#ifdef _WIN3252HANDLE handle;53handle = CreateFileA("conin$",54GENERIC_READ | GENERIC_WRITE,55FILE_SHARE_READ | FILE_SHARE_WRITE,56NULL,57OPEN_EXISTING,58FILE_ATTRIBUTE_NORMAL,59NULL);60ASSERT_PTR_NE(handle, INVALID_HANDLE_VALUE);61ttyin_fd = _open_osfhandle((intptr_t) handle, 0);62
63handle = CreateFileA("conout$",64GENERIC_READ | GENERIC_WRITE,65FILE_SHARE_READ | FILE_SHARE_WRITE,66NULL,67OPEN_EXISTING,68FILE_ATTRIBUTE_NORMAL,69NULL);70ASSERT_PTR_NE(handle, INVALID_HANDLE_VALUE);71ttyout_fd = _open_osfhandle((intptr_t) handle, 0);72
73#else /* unix */74ttyin_fd = open("/dev/tty", O_RDONLY, 0);75if (ttyin_fd < 0) {76fprintf(stderr, "Cannot open /dev/tty as read-only: %s\n", strerror(errno));77fflush(stderr);78return TEST_SKIP;79}80
81ttyout_fd = open("/dev/tty", O_WRONLY, 0);82if (ttyout_fd < 0) {83fprintf(stderr, "Cannot open /dev/tty as write-only: %s\n", strerror(errno));84fflush(stderr);85return TEST_SKIP;86}87#endif88
89ASSERT_GE(ttyin_fd, 0);90ASSERT_GE(ttyout_fd, 0);91
92ASSERT_EQ(UV_UNKNOWN_HANDLE, uv_guess_handle(-1));93
94ASSERT_EQ(UV_TTY, uv_guess_handle(ttyin_fd));95ASSERT_EQ(UV_TTY, uv_guess_handle(ttyout_fd));96
97r = uv_tty_init(loop, &tty_in, ttyin_fd, 1); /* Readable. */98ASSERT_OK(r);99ASSERT(uv_is_readable((uv_stream_t*) &tty_in));100ASSERT(!uv_is_writable((uv_stream_t*) &tty_in));101
102r = uv_tty_init(loop, &tty_out, ttyout_fd, 0); /* Writable. */103ASSERT_OK(r);104ASSERT(!uv_is_readable((uv_stream_t*) &tty_out));105ASSERT(uv_is_writable((uv_stream_t*) &tty_out));106
107r = uv_tty_get_winsize(&tty_out, &width, &height);108ASSERT_OK(r);109
110printf("width=%d height=%d\n", width, height);111
112if (width == 0 && height == 0) {113/* Some environments such as containers or Jenkins behave like this114* sometimes */
115MAKE_VALGRIND_HAPPY(loop);116return TEST_SKIP;117}118
119ASSERT_GT(width, 0);120ASSERT_GT(height, 0);121
122/* Turn on raw mode. */123r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_RAW);124ASSERT_OK(r);125
126/* Turn off raw mode. */127r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_NORMAL);128ASSERT_OK(r);129
130/* Calling uv_tty_reset_mode() repeatedly should not clobber errno. */131errno = 0;132ASSERT_OK(uv_tty_reset_mode());133ASSERT_OK(uv_tty_reset_mode());134ASSERT_OK(uv_tty_reset_mode());135ASSERT_OK(errno);136
137/* TODO check the actual mode! */138
139uv_close((uv_handle_t*) &tty_in, NULL);140uv_close((uv_handle_t*) &tty_out, NULL);141
142uv_run(loop, UV_RUN_DEFAULT);143
144MAKE_VALGRIND_HAPPY(uv_default_loop());145return 0;146}
147
148
149#ifdef _WIN32150static void tty_raw_alloc(uv_handle_t* handle, size_t size, uv_buf_t* buf) {151buf->base = malloc(size);152buf->len = size;153}
154
155static void tty_raw_read(uv_stream_t* tty_in, ssize_t nread, const uv_buf_t* buf) {156if (nread > 0) {157ASSERT_EQ(1, nread );158ASSERT_EQ(buf->base[0], ' ');159uv_close((uv_handle_t*) tty_in, NULL);160} else {161ASSERT_OK(nread);162}163}
164
165TEST_IMPL(tty_raw) {166int r;167int ttyin_fd;168uv_tty_t tty_in;169uv_loop_t* loop = uv_default_loop();170HANDLE handle;171INPUT_RECORD record;172DWORD written;173
174/* Make sure we have an FD that refers to a tty */175handle = CreateFileA("conin$",176GENERIC_READ | GENERIC_WRITE,177FILE_SHARE_READ | FILE_SHARE_WRITE,178NULL,179OPEN_EXISTING,180FILE_ATTRIBUTE_NORMAL,181NULL);182ASSERT_PTR_NE(handle, INVALID_HANDLE_VALUE);183ttyin_fd = _open_osfhandle((intptr_t) handle, 0);184ASSERT_GE(ttyin_fd, 0);185ASSERT_EQ(UV_TTY, uv_guess_handle(ttyin_fd));186
187r = uv_tty_init(loop, &tty_in, ttyin_fd, 1); /* Readable. */188ASSERT_OK(r);189ASSERT(uv_is_readable((uv_stream_t*) &tty_in));190ASSERT(!uv_is_writable((uv_stream_t*) &tty_in));191
192r = uv_read_start((uv_stream_t*)&tty_in, tty_raw_alloc, tty_raw_read);193ASSERT_OK(r);194
195/* Give uv_tty_line_read_thread time to block on ReadConsoleW */196Sleep(100);197
198/* Turn on raw mode. */199r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_RAW);200ASSERT_OK(r);201
202/* Write ' ' that should be read in raw mode */203record.EventType = KEY_EVENT;204record.Event.KeyEvent.bKeyDown = TRUE;205record.Event.KeyEvent.wRepeatCount = 1;206record.Event.KeyEvent.wVirtualKeyCode = VK_SPACE;207record.Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW(VK_SPACE, MAPVK_VK_TO_VSC);208record.Event.KeyEvent.uChar.UnicodeChar = L' ';209record.Event.KeyEvent.dwControlKeyState = 0;210WriteConsoleInputW(handle, &record, 1, &written);211
212uv_run(loop, UV_RUN_DEFAULT);213
214MAKE_VALGRIND_HAPPY(loop);215return 0;216}
217
218TEST_IMPL(tty_empty_write) {219int r;220int ttyout_fd;221uv_tty_t tty_out;222char dummy[1];223uv_buf_t bufs[1];224uv_loop_t* loop;225
226/* Make sure we have an FD that refers to a tty */227HANDLE handle;228
229loop = uv_default_loop();230
231handle = CreateFileA("conout$",232GENERIC_READ | GENERIC_WRITE,233FILE_SHARE_READ | FILE_SHARE_WRITE,234NULL,235OPEN_EXISTING,236FILE_ATTRIBUTE_NORMAL,237NULL);238ASSERT_PTR_NE(handle, INVALID_HANDLE_VALUE);239ttyout_fd = _open_osfhandle((intptr_t) handle, 0);240
241ASSERT_GE(ttyout_fd, 0);242
243ASSERT_EQ(UV_TTY, uv_guess_handle(ttyout_fd));244
245r = uv_tty_init(loop, &tty_out, ttyout_fd, 0); /* Writable. */246ASSERT_OK(r);247ASSERT(!uv_is_readable((uv_stream_t*) &tty_out));248ASSERT(uv_is_writable((uv_stream_t*) &tty_out));249
250bufs[0].len = 0;251bufs[0].base = &dummy[0];252
253r = uv_try_write((uv_stream_t*) &tty_out, bufs, 1);254ASSERT_OK(r);255
256uv_close((uv_handle_t*) &tty_out, NULL);257
258uv_run(loop, UV_RUN_DEFAULT);259
260MAKE_VALGRIND_HAPPY(loop);261return 0;262}
263
264TEST_IMPL(tty_large_write) {265int r;266int ttyout_fd;267uv_tty_t tty_out;268char dummy[10000];269uv_buf_t bufs[1];270uv_loop_t* loop;271
272/* Make sure we have an FD that refers to a tty */273HANDLE handle;274
275loop = uv_default_loop();276
277handle = CreateFileA("conout$",278GENERIC_READ | GENERIC_WRITE,279FILE_SHARE_READ | FILE_SHARE_WRITE,280NULL,281OPEN_EXISTING,282FILE_ATTRIBUTE_NORMAL,283NULL);284ASSERT_PTR_NE(handle, INVALID_HANDLE_VALUE);285ttyout_fd = _open_osfhandle((intptr_t) handle, 0);286
287ASSERT_GE(ttyout_fd, 0);288
289ASSERT_EQ(UV_TTY, uv_guess_handle(ttyout_fd));290
291r = uv_tty_init(loop, &tty_out, ttyout_fd, 0); /* Writable. */292ASSERT_OK(r);293
294memset(dummy, '.', sizeof(dummy) - 1);295dummy[sizeof(dummy) - 1] = '\n';296
297bufs[0] = uv_buf_init(dummy, sizeof(dummy));298
299r = uv_try_write((uv_stream_t*) &tty_out, bufs, 1);300ASSERT_EQ(10000, r);301
302uv_close((uv_handle_t*) &tty_out, NULL);303
304uv_run(loop, UV_RUN_DEFAULT);305
306MAKE_VALGRIND_HAPPY(loop);307return 0;308}
309
310TEST_IMPL(tty_raw_cancel) {311int r;312int ttyin_fd;313uv_tty_t tty_in;314HANDLE handle;315
316/* Make sure we have an FD that refers to a tty */317handle = CreateFileA("conin$",318GENERIC_READ | GENERIC_WRITE,319FILE_SHARE_READ | FILE_SHARE_WRITE,320NULL,321OPEN_EXISTING,322FILE_ATTRIBUTE_NORMAL,323NULL);324ASSERT_PTR_NE(handle, INVALID_HANDLE_VALUE);325ttyin_fd = _open_osfhandle((intptr_t) handle, 0);326ASSERT_GE(ttyin_fd, 0);327ASSERT_EQ(UV_TTY, uv_guess_handle(ttyin_fd));328
329r = uv_tty_init(uv_default_loop(), &tty_in, ttyin_fd, 1); /* Readable. */330ASSERT_OK(r);331r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_RAW);332ASSERT_OK(r);333r = uv_read_start((uv_stream_t*)&tty_in, tty_raw_alloc, tty_raw_read);334ASSERT_OK(r);335
336r = uv_read_stop((uv_stream_t*) &tty_in);337ASSERT_OK(r);338
339MAKE_VALGRIND_HAPPY(uv_default_loop());340return 0;341}
342#endif343
344
345TEST_IMPL(tty_file) {346#ifndef _WIN32347uv_loop_t loop;348uv_tty_t tty;349uv_tty_t tty_ro;350uv_tty_t tty_wo;351int fd;352
353ASSERT_OK(uv_loop_init(&loop));354
355fd = open("test/fixtures/empty_file", O_RDONLY);356if (fd != -1) {357ASSERT_EQ(UV_EINVAL, uv_tty_init(&loop, &tty, fd, 1));358ASSERT_OK(close(fd));359/* test EBADF handling */360ASSERT_EQ(UV_EINVAL, uv_tty_init(&loop, &tty, fd, 1));361}362
363/* Bug on AIX where '/dev/random' returns 1 from isatty() */
364#ifndef _AIX365fd = open("/dev/random", O_RDONLY);366if (fd != -1) {367ASSERT_EQ(UV_EINVAL, uv_tty_init(&loop, &tty, fd, 1));368ASSERT_OK(close(fd));369}370#endif /* _AIX */371
372fd = open("/dev/zero", O_RDONLY);373if (fd != -1) {374ASSERT_EQ(UV_EINVAL, uv_tty_init(&loop, &tty, fd, 1));375ASSERT_OK(close(fd));376}377
378fd = open("/dev/tty", O_RDWR);379if (fd != -1) {380ASSERT_OK(uv_tty_init(&loop, &tty, fd, 1));381ASSERT_OK(close(fd)); /* TODO: it's indeterminate who owns fd now */382ASSERT(uv_is_readable((uv_stream_t*) &tty));383ASSERT(uv_is_writable((uv_stream_t*) &tty));384uv_close((uv_handle_t*) &tty, NULL);385ASSERT(!uv_is_readable((uv_stream_t*) &tty));386ASSERT(!uv_is_writable((uv_stream_t*) &tty));387}388
389fd = open("/dev/tty", O_RDONLY);390if (fd != -1) {391ASSERT_OK(uv_tty_init(&loop, &tty_ro, fd, 1));392ASSERT_OK(close(fd)); /* TODO: it's indeterminate who owns fd now */393ASSERT(uv_is_readable((uv_stream_t*) &tty_ro));394ASSERT(!uv_is_writable((uv_stream_t*) &tty_ro));395uv_close((uv_handle_t*) &tty_ro, NULL);396ASSERT(!uv_is_readable((uv_stream_t*) &tty_ro));397ASSERT(!uv_is_writable((uv_stream_t*) &tty_ro));398}399
400fd = open("/dev/tty", O_WRONLY);401if (fd != -1) {402ASSERT_OK(uv_tty_init(&loop, &tty_wo, fd, 0));403ASSERT_OK(close(fd)); /* TODO: it's indeterminate who owns fd now */404ASSERT(!uv_is_readable((uv_stream_t*) &tty_wo));405ASSERT(uv_is_writable((uv_stream_t*) &tty_wo));406uv_close((uv_handle_t*) &tty_wo, NULL);407ASSERT(!uv_is_readable((uv_stream_t*) &tty_wo));408ASSERT(!uv_is_writable((uv_stream_t*) &tty_wo));409}410
411
412ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT));413
414MAKE_VALGRIND_HAPPY(&loop);415#endif416return 0;417}
418
419TEST_IMPL(tty_pty) {420/* TODO(gengjiawen): Fix test on QEMU. */
421#if defined(__QEMU__)422RETURN_SKIP("Test does not currently work in QEMU");423#endif424#if defined(__ASAN__)425RETURN_SKIP("Test does not currently work in ASAN");426#endif427
428#if defined(__APPLE__) || \429defined(__DragonFly__) || \430defined(__FreeBSD__) || \431(defined(__linux__) && !defined(__ANDROID__)) || \432defined(__NetBSD__) || \433defined(__OpenBSD__)434int master_fd, slave_fd, r;435struct winsize w;436uv_loop_t loop;437uv_tty_t master_tty, slave_tty;438
439ASSERT_OK(uv_loop_init(&loop));440
441r = openpty(&master_fd, &slave_fd, NULL, NULL, &w);442if (r != 0)443RETURN_SKIP("No pty available, skipping.");444
445ASSERT_OK(uv_tty_init(&loop, &slave_tty, slave_fd, 0));446ASSERT_OK(uv_tty_init(&loop, &master_tty, master_fd, 0));447ASSERT(uv_is_readable((uv_stream_t*) &slave_tty));448ASSERT(uv_is_writable((uv_stream_t*) &slave_tty));449ASSERT(uv_is_readable((uv_stream_t*) &master_tty));450ASSERT(uv_is_writable((uv_stream_t*) &master_tty));451/* Check if the file descriptor was reopened. If it is,452* UV_HANDLE_BLOCKING_WRITES (value 0x100000) isn't set on flags.
453*/
454ASSERT_OK((slave_tty.flags & 0x100000));455/* The master_fd of a pty should never be reopened.456*/
457ASSERT(master_tty.flags & 0x100000);458ASSERT_OK(close(slave_fd));459uv_close((uv_handle_t*) &slave_tty, NULL);460ASSERT_OK(close(master_fd));461uv_close((uv_handle_t*) &master_tty, NULL);462
463ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT));464
465MAKE_VALGRIND_HAPPY(&loop);466#endif467return 0;468}
469