test-tty.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #include "uv.h"
  22. #include "task.h"
  23. #ifdef _WIN32
  24. # include <io.h>
  25. # include <windows.h>
  26. #else /* Unix */
  27. # include <fcntl.h>
  28. # include <unistd.h>
  29. # if (defined(__linux__) || defined(__GLIBC__)) && !defined(__ANDROID__)
  30. # include <pty.h>
  31. # elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  32. # include <util.h>
  33. # elif defined(__FreeBSD__) || defined(__DragonFly__)
  34. # include <libutil.h>
  35. # endif
  36. #endif
  37. #include <string.h>
  38. #include <errno.h>
  39. TEST_IMPL(tty) {
  40. int r, width, height;
  41. int ttyin_fd, ttyout_fd;
  42. uv_tty_t tty_in, tty_out;
  43. uv_loop_t* loop = uv_default_loop();
  44. /* Make sure we have an FD that refers to a tty */
  45. #ifdef _WIN32
  46. HANDLE handle;
  47. handle = CreateFileA("conin$",
  48. GENERIC_READ | GENERIC_WRITE,
  49. FILE_SHARE_READ | FILE_SHARE_WRITE,
  50. NULL,
  51. OPEN_EXISTING,
  52. FILE_ATTRIBUTE_NORMAL,
  53. NULL);
  54. ASSERT(handle != INVALID_HANDLE_VALUE);
  55. ttyin_fd = _open_osfhandle((intptr_t) handle, 0);
  56. handle = CreateFileA("conout$",
  57. GENERIC_READ | GENERIC_WRITE,
  58. FILE_SHARE_READ | FILE_SHARE_WRITE,
  59. NULL,
  60. OPEN_EXISTING,
  61. FILE_ATTRIBUTE_NORMAL,
  62. NULL);
  63. ASSERT(handle != INVALID_HANDLE_VALUE);
  64. ttyout_fd = _open_osfhandle((intptr_t) handle, 0);
  65. #else /* unix */
  66. ttyin_fd = open("/dev/tty", O_RDONLY, 0);
  67. if (ttyin_fd < 0) {
  68. fprintf(stderr, "Cannot open /dev/tty as read-only: %s\n", strerror(errno));
  69. fflush(stderr);
  70. return TEST_SKIP;
  71. }
  72. ttyout_fd = open("/dev/tty", O_WRONLY, 0);
  73. if (ttyout_fd < 0) {
  74. fprintf(stderr, "Cannot open /dev/tty as write-only: %s\n", strerror(errno));
  75. fflush(stderr);
  76. return TEST_SKIP;
  77. }
  78. #endif
  79. ASSERT(ttyin_fd >= 0);
  80. ASSERT(ttyout_fd >= 0);
  81. ASSERT(UV_UNKNOWN_HANDLE == uv_guess_handle(-1));
  82. ASSERT(UV_TTY == uv_guess_handle(ttyin_fd));
  83. ASSERT(UV_TTY == uv_guess_handle(ttyout_fd));
  84. r = uv_tty_init(uv_default_loop(), &tty_in, ttyin_fd, 1); /* Readable. */
  85. ASSERT(r == 0);
  86. ASSERT(uv_is_readable((uv_stream_t*) &tty_in));
  87. ASSERT(!uv_is_writable((uv_stream_t*) &tty_in));
  88. r = uv_tty_init(uv_default_loop(), &tty_out, ttyout_fd, 0); /* Writable. */
  89. ASSERT(r == 0);
  90. ASSERT(!uv_is_readable((uv_stream_t*) &tty_out));
  91. ASSERT(uv_is_writable((uv_stream_t*) &tty_out));
  92. r = uv_tty_get_winsize(&tty_out, &width, &height);
  93. ASSERT(r == 0);
  94. printf("width=%d height=%d\n", width, height);
  95. if (width == 0 && height == 0) {
  96. /* Some environments such as containers or Jenkins behave like this
  97. * sometimes */
  98. MAKE_VALGRIND_HAPPY();
  99. return TEST_SKIP;
  100. }
  101. /*
  102. * Is it a safe assumption that most people have terminals larger than
  103. * 10x10?
  104. */
  105. ASSERT(width > 10);
  106. ASSERT(height > 10);
  107. /* Turn on raw mode. */
  108. r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_RAW);
  109. ASSERT(r == 0);
  110. /* Turn off raw mode. */
  111. r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_NORMAL);
  112. ASSERT(r == 0);
  113. /* Calling uv_tty_reset_mode() repeatedly should not clobber errno. */
  114. errno = 0;
  115. ASSERT(0 == uv_tty_reset_mode());
  116. ASSERT(0 == uv_tty_reset_mode());
  117. ASSERT(0 == uv_tty_reset_mode());
  118. ASSERT(0 == errno);
  119. /* TODO check the actual mode! */
  120. uv_close((uv_handle_t*) &tty_in, NULL);
  121. uv_close((uv_handle_t*) &tty_out, NULL);
  122. uv_run(loop, UV_RUN_DEFAULT);
  123. MAKE_VALGRIND_HAPPY();
  124. return 0;
  125. }
  126. #ifdef _WIN32
  127. static void tty_raw_alloc(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
  128. buf->base = malloc(size);
  129. buf->len = size;
  130. }
  131. static void tty_raw_read(uv_stream_t* tty_in, ssize_t nread, const uv_buf_t* buf) {
  132. if (nread > 0) {
  133. ASSERT(nread == 1);
  134. ASSERT(buf->base[0] == ' ');
  135. uv_close((uv_handle_t*) tty_in, NULL);
  136. } else {
  137. ASSERT(nread == 0);
  138. }
  139. }
  140. TEST_IMPL(tty_raw) {
  141. int r;
  142. int ttyin_fd;
  143. uv_tty_t tty_in;
  144. uv_loop_t* loop = uv_default_loop();
  145. HANDLE handle;
  146. INPUT_RECORD record;
  147. DWORD written;
  148. /* Make sure we have an FD that refers to a tty */
  149. handle = CreateFileA("conin$",
  150. GENERIC_READ | GENERIC_WRITE,
  151. FILE_SHARE_READ | FILE_SHARE_WRITE,
  152. NULL,
  153. OPEN_EXISTING,
  154. FILE_ATTRIBUTE_NORMAL,
  155. NULL);
  156. ASSERT(handle != INVALID_HANDLE_VALUE);
  157. ttyin_fd = _open_osfhandle((intptr_t) handle, 0);
  158. ASSERT(ttyin_fd >= 0);
  159. ASSERT(UV_TTY == uv_guess_handle(ttyin_fd));
  160. r = uv_tty_init(uv_default_loop(), &tty_in, ttyin_fd, 1); /* Readable. */
  161. ASSERT(r == 0);
  162. ASSERT(uv_is_readable((uv_stream_t*) &tty_in));
  163. ASSERT(!uv_is_writable((uv_stream_t*) &tty_in));
  164. r = uv_read_start((uv_stream_t*)&tty_in, tty_raw_alloc, tty_raw_read);
  165. ASSERT(r == 0);
  166. /* Give uv_tty_line_read_thread time to block on ReadConsoleW */
  167. Sleep(100);
  168. /* Turn on raw mode. */
  169. r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_RAW);
  170. ASSERT(r == 0);
  171. /* Write ' ' that should be read in raw mode */
  172. record.EventType = KEY_EVENT;
  173. record.Event.KeyEvent.bKeyDown = TRUE;
  174. record.Event.KeyEvent.wRepeatCount = 1;
  175. record.Event.KeyEvent.wVirtualKeyCode = VK_SPACE;
  176. record.Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW(VK_SPACE, MAPVK_VK_TO_VSC);
  177. record.Event.KeyEvent.uChar.UnicodeChar = L' ';
  178. record.Event.KeyEvent.dwControlKeyState = 0;
  179. WriteConsoleInputW(handle, &record, 1, &written);
  180. uv_run(loop, UV_RUN_DEFAULT);
  181. MAKE_VALGRIND_HAPPY();
  182. return 0;
  183. }
  184. TEST_IMPL(tty_empty_write) {
  185. int r;
  186. int ttyout_fd;
  187. uv_tty_t tty_out;
  188. char dummy[1];
  189. uv_buf_t bufs[1];
  190. uv_loop_t* loop;
  191. /* Make sure we have an FD that refers to a tty */
  192. HANDLE handle;
  193. loop = uv_default_loop();
  194. handle = CreateFileA("conout$",
  195. GENERIC_READ | GENERIC_WRITE,
  196. FILE_SHARE_READ | FILE_SHARE_WRITE,
  197. NULL,
  198. OPEN_EXISTING,
  199. FILE_ATTRIBUTE_NORMAL,
  200. NULL);
  201. ASSERT(handle != INVALID_HANDLE_VALUE);
  202. ttyout_fd = _open_osfhandle((intptr_t) handle, 0);
  203. ASSERT(ttyout_fd >= 0);
  204. ASSERT(UV_TTY == uv_guess_handle(ttyout_fd));
  205. r = uv_tty_init(uv_default_loop(), &tty_out, ttyout_fd, 0); /* Writable. */
  206. ASSERT(r == 0);
  207. ASSERT(!uv_is_readable((uv_stream_t*) &tty_out));
  208. ASSERT(uv_is_writable((uv_stream_t*) &tty_out));
  209. bufs[0].len = 0;
  210. bufs[0].base = &dummy[0];
  211. r = uv_try_write((uv_stream_t*) &tty_out, bufs, 1);
  212. ASSERT(r == 0);
  213. uv_close((uv_handle_t*) &tty_out, NULL);
  214. uv_run(loop, UV_RUN_DEFAULT);
  215. MAKE_VALGRIND_HAPPY();
  216. return 0;
  217. }
  218. TEST_IMPL(tty_large_write) {
  219. int r;
  220. int ttyout_fd;
  221. uv_tty_t tty_out;
  222. char dummy[10000];
  223. uv_buf_t bufs[1];
  224. uv_loop_t* loop;
  225. /* Make sure we have an FD that refers to a tty */
  226. HANDLE handle;
  227. loop = uv_default_loop();
  228. handle = CreateFileA("conout$",
  229. GENERIC_READ | GENERIC_WRITE,
  230. FILE_SHARE_READ | FILE_SHARE_WRITE,
  231. NULL,
  232. OPEN_EXISTING,
  233. FILE_ATTRIBUTE_NORMAL,
  234. NULL);
  235. ASSERT(handle != INVALID_HANDLE_VALUE);
  236. ttyout_fd = _open_osfhandle((intptr_t) handle, 0);
  237. ASSERT(ttyout_fd >= 0);
  238. ASSERT(UV_TTY == uv_guess_handle(ttyout_fd));
  239. r = uv_tty_init(uv_default_loop(), &tty_out, ttyout_fd, 0); /* Writable. */
  240. ASSERT(r == 0);
  241. memset(dummy, '.', sizeof(dummy) - 1);
  242. dummy[sizeof(dummy) - 1] = '\n';
  243. bufs[0] = uv_buf_init(dummy, sizeof(dummy));
  244. r = uv_try_write((uv_stream_t*) &tty_out, bufs, 1);
  245. ASSERT(r == 10000);
  246. uv_close((uv_handle_t*) &tty_out, NULL);
  247. uv_run(loop, UV_RUN_DEFAULT);
  248. MAKE_VALGRIND_HAPPY();
  249. return 0;
  250. }
  251. TEST_IMPL(tty_raw_cancel) {
  252. int r;
  253. int ttyin_fd;
  254. uv_tty_t tty_in;
  255. HANDLE handle;
  256. /* Make sure we have an FD that refers to a tty */
  257. handle = CreateFileA("conin$",
  258. GENERIC_READ | GENERIC_WRITE,
  259. FILE_SHARE_READ | FILE_SHARE_WRITE,
  260. NULL,
  261. OPEN_EXISTING,
  262. FILE_ATTRIBUTE_NORMAL,
  263. NULL);
  264. ASSERT(handle != INVALID_HANDLE_VALUE);
  265. ttyin_fd = _open_osfhandle((intptr_t) handle, 0);
  266. ASSERT(ttyin_fd >= 0);
  267. ASSERT(UV_TTY == uv_guess_handle(ttyin_fd));
  268. r = uv_tty_init(uv_default_loop(), &tty_in, ttyin_fd, 1); /* Readable. */
  269. ASSERT(r == 0);
  270. r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_RAW);
  271. ASSERT(r == 0);
  272. r = uv_read_start((uv_stream_t*)&tty_in, tty_raw_alloc, tty_raw_read);
  273. ASSERT(r == 0);
  274. r = uv_read_stop((uv_stream_t*) &tty_in);
  275. ASSERT(r == 0);
  276. MAKE_VALGRIND_HAPPY();
  277. return 0;
  278. }
  279. #endif
  280. TEST_IMPL(tty_file) {
  281. #ifndef _WIN32
  282. uv_loop_t loop;
  283. uv_tty_t tty;
  284. uv_tty_t tty_ro;
  285. uv_tty_t tty_wo;
  286. int fd;
  287. ASSERT(0 == uv_loop_init(&loop));
  288. fd = open("test/fixtures/empty_file", O_RDONLY);
  289. if (fd != -1) {
  290. ASSERT(UV_EINVAL == uv_tty_init(&loop, &tty, fd, 1));
  291. ASSERT(0 == close(fd));
  292. /* test EBADF handling */
  293. ASSERT(UV_EINVAL == uv_tty_init(&loop, &tty, fd, 1));
  294. }
  295. /* Bug on AIX where '/dev/random' returns 1 from isatty() */
  296. #ifndef _AIX
  297. fd = open("/dev/random", O_RDONLY);
  298. if (fd != -1) {
  299. ASSERT(UV_EINVAL == uv_tty_init(&loop, &tty, fd, 1));
  300. ASSERT(0 == close(fd));
  301. }
  302. #endif /* _AIX */
  303. fd = open("/dev/zero", O_RDONLY);
  304. if (fd != -1) {
  305. ASSERT(UV_EINVAL == uv_tty_init(&loop, &tty, fd, 1));
  306. ASSERT(0 == close(fd));
  307. }
  308. fd = open("/dev/tty", O_RDWR);
  309. if (fd != -1) {
  310. ASSERT(0 == uv_tty_init(&loop, &tty, fd, 1));
  311. ASSERT(0 == close(fd)); /* TODO: it's indeterminate who owns fd now */
  312. ASSERT(uv_is_readable((uv_stream_t*) &tty));
  313. ASSERT(uv_is_writable((uv_stream_t*) &tty));
  314. uv_close((uv_handle_t*) &tty, NULL);
  315. ASSERT(!uv_is_readable((uv_stream_t*) &tty));
  316. ASSERT(!uv_is_writable((uv_stream_t*) &tty));
  317. }
  318. fd = open("/dev/tty", O_RDONLY);
  319. if (fd != -1) {
  320. ASSERT(0 == uv_tty_init(&loop, &tty_ro, fd, 1));
  321. ASSERT(0 == close(fd)); /* TODO: it's indeterminate who owns fd now */
  322. ASSERT(uv_is_readable((uv_stream_t*) &tty_ro));
  323. ASSERT(!uv_is_writable((uv_stream_t*) &tty_ro));
  324. uv_close((uv_handle_t*) &tty_ro, NULL);
  325. ASSERT(!uv_is_readable((uv_stream_t*) &tty_ro));
  326. ASSERT(!uv_is_writable((uv_stream_t*) &tty_ro));
  327. }
  328. fd = open("/dev/tty", O_WRONLY);
  329. if (fd != -1) {
  330. ASSERT(0 == uv_tty_init(&loop, &tty_wo, fd, 0));
  331. ASSERT(0 == close(fd)); /* TODO: it's indeterminate who owns fd now */
  332. ASSERT(!uv_is_readable((uv_stream_t*) &tty_wo));
  333. ASSERT(uv_is_writable((uv_stream_t*) &tty_wo));
  334. uv_close((uv_handle_t*) &tty_wo, NULL);
  335. ASSERT(!uv_is_readable((uv_stream_t*) &tty_wo));
  336. ASSERT(!uv_is_writable((uv_stream_t*) &tty_wo));
  337. }
  338. ASSERT(0 == uv_run(&loop, UV_RUN_DEFAULT));
  339. ASSERT(0 == uv_loop_close(&loop));
  340. MAKE_VALGRIND_HAPPY();
  341. #endif
  342. return 0;
  343. }
  344. TEST_IMPL(tty_pty) {
  345. #if defined(__APPLE__) || \
  346. defined(__DragonFly__) || \
  347. defined(__FreeBSD__) || \
  348. defined(__FreeBSD_kernel__) || \
  349. (defined(__linux__) && !defined(__ANDROID__)) || \
  350. defined(__NetBSD__) || \
  351. defined(__OpenBSD__)
  352. int master_fd, slave_fd, r;
  353. struct winsize w;
  354. uv_loop_t loop;
  355. uv_tty_t master_tty, slave_tty;
  356. ASSERT(0 == uv_loop_init(&loop));
  357. r = openpty(&master_fd, &slave_fd, NULL, NULL, &w);
  358. if (r != 0)
  359. RETURN_SKIP("No pty available, skipping.");
  360. ASSERT(0 == uv_tty_init(&loop, &slave_tty, slave_fd, 0));
  361. ASSERT(0 == uv_tty_init(&loop, &master_tty, master_fd, 0));
  362. ASSERT(uv_is_readable((uv_stream_t*) &slave_tty));
  363. ASSERT(uv_is_writable((uv_stream_t*) &slave_tty));
  364. ASSERT(uv_is_readable((uv_stream_t*) &master_tty));
  365. ASSERT(uv_is_writable((uv_stream_t*) &master_tty));
  366. /* Check if the file descriptor was reopened. If it is,
  367. * UV_HANDLE_BLOCKING_WRITES (value 0x100000) isn't set on flags.
  368. */
  369. ASSERT(0 == (slave_tty.flags & 0x100000));
  370. /* The master_fd of a pty should never be reopened.
  371. */
  372. ASSERT(master_tty.flags & 0x100000);
  373. ASSERT(0 == close(slave_fd));
  374. uv_close((uv_handle_t*) &slave_tty, NULL);
  375. ASSERT(0 == close(master_fd));
  376. uv_close((uv_handle_t*) &master_tty, NULL);
  377. ASSERT(0 == uv_run(&loop, UV_RUN_DEFAULT));
  378. MAKE_VALGRIND_HAPPY();
  379. #endif
  380. return 0;
  381. }