summaryrefslogtreecommitdiffstats
path: root/adb/sysdeps_win32_test.cpp
diff options
context:
space:
mode:
authorDavid Pursell <dpursell@google.com>2015-10-28 14:29:51 -0700
committerDavid Pursell <dpursell@google.com>2015-11-02 16:44:34 -0800
commitc5b8ad88ce6e74216198f14c6e33d55cb1681370 (patch)
tree7d8d7f501ee3ac62f75be1b8e1c38a38f1bd3c0b /adb/sysdeps_win32_test.cpp
parent36d7af4cf635816ecbcb9bd300f4c9d60cb012d4 (diff)
downloadcore-c5b8ad88ce6e74216198f14c6e33d55cb1681370.tar.gz
core-c5b8ad88ce6e74216198f14c6e33d55cb1681370.tar.bz2
core-c5b8ad88ce6e74216198f14c6e33d55cb1681370.zip
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
Diffstat (limited to 'adb/sysdeps_win32_test.cpp')
-rwxr-xr-xadb/sysdeps_win32_test.cpp44
1 files changed, 44 insertions, 0 deletions
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));
+}