From c5b8ad88ce6e74216198f14c6e33d55cb1681370 Mon Sep 17 00:00:00 2001 From: David Pursell Date: Wed, 28 Oct 2015 14:29:51 -0700 Subject: adb: create unix_isatty() function. Our Windows code has several different ways it checks whether an FD is a console or not, some of which aren't exactly correct as they may treat character devices (e.g. NUL) as consoles. This CL disallows using the builtin isatty() function and provides unix_isatty() instead which correctly checks these corner cases. Change-Id: I6d551c745dae691c7eb3446b585265d62c1e62fa --- adb/sysdeps_win32_test.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'adb/sysdeps_win32_test.cpp') diff --git a/adb/sysdeps_win32_test.cpp b/adb/sysdeps_win32_test.cpp index 66d1ba881..55b5eb462 100755 --- a/adb/sysdeps_win32_test.cpp +++ b/adb/sysdeps_win32_test.cpp @@ -18,6 +18,8 @@ #include "sysdeps.h" +#include "base/test_utils.h" + TEST(sysdeps_win32, adb_getenv) { // Insert all test env vars before first call to adb_getenv() which will // read the env var block only once. @@ -93,3 +95,45 @@ TEST(sysdeps_win32, adb_strerror) { // adb_strerror() returns. TestAdbStrError(ECONNRESET, "Connection reset by peer"); } + +TEST(sysdeps_win32, unix_isatty) { + // stdin and stdout should be consoles. Use CONIN$ and CONOUT$ special files + // so that we can test this even if stdin/stdout have been redirected. Read + // permissions are required for unix_isatty(). + int conin_fd = unix_open("CONIN$", O_RDONLY); + int conout_fd = unix_open("CONOUT$", O_RDWR); + for (const int fd : {conin_fd, conout_fd}) { + EXPECT_TRUE(fd >= 0); + EXPECT_EQ(1, unix_isatty(fd)); + EXPECT_EQ(0, unix_close(fd)); + } + + // nul returns 1 from isatty(), make sure unix_isatty() corrects that. + for (auto flags : {O_RDONLY, O_RDWR}) { + int nul_fd = unix_open("nul", flags); + EXPECT_TRUE(nul_fd >= 0); + EXPECT_EQ(0, unix_isatty(nul_fd)); + EXPECT_EQ(0, unix_close(nul_fd)); + } + + // Check a real file, both read-write and read-only. + TemporaryFile temp_file; + EXPECT_TRUE(temp_file.fd >= 0); + EXPECT_EQ(0, unix_isatty(temp_file.fd)); + + int temp_file_ro_fd = unix_open(temp_file.path, O_RDONLY); + EXPECT_TRUE(temp_file_ro_fd >= 0); + EXPECT_EQ(0, unix_isatty(temp_file_ro_fd)); + EXPECT_EQ(0, unix_close(temp_file_ro_fd)); + + // Check a real OS pipe. + int pipe_fds[2]; + EXPECT_EQ(0, _pipe(pipe_fds, 64, _O_BINARY)); + EXPECT_EQ(0, unix_isatty(pipe_fds[0])); + EXPECT_EQ(0, unix_isatty(pipe_fds[1])); + EXPECT_EQ(0, _close(pipe_fds[0])); + EXPECT_EQ(0, _close(pipe_fds[1])); + + // Make sure an invalid FD is handled correctly. + EXPECT_EQ(0, unix_isatty(-1)); +} -- cgit v1.2.3