blob: 55b5eb462f88118668b7f87cc2cc8776cd459f09 [file] [log] [blame]
Spencer Lowe6ae5732015-09-08 17:13:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include "sysdeps.h"
20
David Pursellc5b8ad82015-10-28 14:29:51 -070021#include "base/test_utils.h"
22
Spencer Lowe6ae5732015-09-08 17:13:04 -070023TEST(sysdeps_win32, adb_getenv) {
24 // Insert all test env vars before first call to adb_getenv() which will
25 // read the env var block only once.
26 ASSERT_EQ(0, _putenv("SYSDEPS_WIN32_TEST_UPPERCASE=1"));
27 ASSERT_EQ(0, _putenv("sysdeps_win32_test_lowercase=2"));
28 ASSERT_EQ(0, _putenv("Sysdeps_Win32_Test_MixedCase=3"));
29
30 // UTF-16 value
31 ASSERT_EQ(0, _wputenv(L"SYSDEPS_WIN32_TEST_UNICODE=\u00a1\u0048\u006f\u006c"
32 L"\u0061\u0021\u03b1\u03b2\u03b3\u0061\u006d\u0062"
33 L"\u0075\u006c\u014d\u043f\u0440\u0438\u0432\u0435"
34 L"\u0442"));
35
36 // Search for non-existant env vars.
37 EXPECT_STREQ(nullptr, adb_getenv("SYSDEPS_WIN32_TEST_NONEXISTANT"));
38
39 // Search for existing env vars.
40
41 // There is no test for an env var with a value of a zero-length string
42 // because _putenv() does not support inserting such an env var.
43
44 // Search for env var that is uppercase.
45 EXPECT_STREQ("1", adb_getenv("SYSDEPS_WIN32_TEST_UPPERCASE"));
46 EXPECT_STREQ("1", adb_getenv("sysdeps_win32_test_uppercase"));
47 EXPECT_STREQ("1", adb_getenv("Sysdeps_Win32_Test_Uppercase"));
48
49 // Search for env var that is lowercase.
50 EXPECT_STREQ("2", adb_getenv("SYSDEPS_WIN32_TEST_LOWERCASE"));
51 EXPECT_STREQ("2", adb_getenv("sysdeps_win32_test_lowercase"));
52 EXPECT_STREQ("2", adb_getenv("Sysdeps_Win32_Test_Lowercase"));
53
54 // Search for env var that is mixed-case.
55 EXPECT_STREQ("3", adb_getenv("SYSDEPS_WIN32_TEST_MIXEDCASE"));
56 EXPECT_STREQ("3", adb_getenv("sysdeps_win32_test_mixedcase"));
57 EXPECT_STREQ("3", adb_getenv("Sysdeps_Win32_Test_MixedCase"));
58
59 // Check that UTF-16 was converted to UTF-8.
60 EXPECT_STREQ("\xc2\xa1\x48\x6f\x6c\x61\x21\xce\xb1\xce\xb2\xce\xb3\x61\x6d"
61 "\x62\x75\x6c\xc5\x8d\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5"
62 "\xd1\x82",
63 adb_getenv("SYSDEPS_WIN32_TEST_UNICODE"));
64
65 // Check an env var that should always be set.
66 const char* path_val = adb_getenv("PATH");
67 EXPECT_NE(nullptr, path_val);
68 if (path_val != nullptr) {
69 EXPECT_GT(strlen(path_val), 0);
70 }
71}
Spencer Low0a796002015-10-18 16:45:09 -070072
73void TestAdbStrError(int err, const char* expected) {
74 errno = 12345;
75 const char* result = adb_strerror(err);
76 // Check that errno is not overwritten.
77 EXPECT_EQ(12345, errno);
78 EXPECT_STREQ(expected, result);
79}
80
81TEST(sysdeps_win32, adb_strerror) {
82 // Test an error code that should not have a mapped string. Use an error
83 // code that is not used by the internal implementation of adb_strerror().
84 TestAdbStrError(-2, "Unknown error");
85 // adb_strerror() uses -1 internally, so test that it can still be passed
86 // as a parameter.
87 TestAdbStrError(-1, "Unknown error");
88 // Test very big, positive unknown error.
89 TestAdbStrError(1000000, "Unknown error");
90 // Test success case.
91 TestAdbStrError(0, "No error");
92 // Test error that regular strerror() should have a string for.
93 TestAdbStrError(EPERM, "Operation not permitted");
94 // Test error that regular strerror() doesn't have a string for, but that
95 // adb_strerror() returns.
96 TestAdbStrError(ECONNRESET, "Connection reset by peer");
97}
David Pursellc5b8ad82015-10-28 14:29:51 -070098
99TEST(sysdeps_win32, unix_isatty) {
100 // stdin and stdout should be consoles. Use CONIN$ and CONOUT$ special files
101 // so that we can test this even if stdin/stdout have been redirected. Read
102 // permissions are required for unix_isatty().
103 int conin_fd = unix_open("CONIN$", O_RDONLY);
104 int conout_fd = unix_open("CONOUT$", O_RDWR);
105 for (const int fd : {conin_fd, conout_fd}) {
106 EXPECT_TRUE(fd >= 0);
107 EXPECT_EQ(1, unix_isatty(fd));
108 EXPECT_EQ(0, unix_close(fd));
109 }
110
111 // nul returns 1 from isatty(), make sure unix_isatty() corrects that.
112 for (auto flags : {O_RDONLY, O_RDWR}) {
113 int nul_fd = unix_open("nul", flags);
114 EXPECT_TRUE(nul_fd >= 0);
115 EXPECT_EQ(0, unix_isatty(nul_fd));
116 EXPECT_EQ(0, unix_close(nul_fd));
117 }
118
119 // Check a real file, both read-write and read-only.
120 TemporaryFile temp_file;
121 EXPECT_TRUE(temp_file.fd >= 0);
122 EXPECT_EQ(0, unix_isatty(temp_file.fd));
123
124 int temp_file_ro_fd = unix_open(temp_file.path, O_RDONLY);
125 EXPECT_TRUE(temp_file_ro_fd >= 0);
126 EXPECT_EQ(0, unix_isatty(temp_file_ro_fd));
127 EXPECT_EQ(0, unix_close(temp_file_ro_fd));
128
129 // Check a real OS pipe.
130 int pipe_fds[2];
131 EXPECT_EQ(0, _pipe(pipe_fds, 64, _O_BINARY));
132 EXPECT_EQ(0, unix_isatty(pipe_fds[0]));
133 EXPECT_EQ(0, unix_isatty(pipe_fds[1]));
134 EXPECT_EQ(0, _close(pipe_fds[0]));
135 EXPECT_EQ(0, _close(pipe_fds[1]));
136
137 // Make sure an invalid FD is handled correctly.
138 EXPECT_EQ(0, unix_isatty(-1));
139}