blob: 529b21215a4d817295228313a2afbac01a6220cb [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
Elliott Hughes4f713192015-12-04 22:00:26 -080021#include <android-base/test_utils.h>
David Pursellc5b8ad82015-10-28 14:29:51 -070022
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) {
Dan Albertbac7bb52016-01-15 12:14:24 -080069 EXPECT_GT(strlen(path_val), 0U);
Spencer Lowe6ae5732015-09-08 17:13:04 -070070 }
71}
Spencer Low0a796002015-10-18 16:45:09 -070072
David Pursellc5b8ad82015-10-28 14:29:51 -070073TEST(sysdeps_win32, unix_isatty) {
74 // stdin and stdout should be consoles. Use CONIN$ and CONOUT$ special files
75 // so that we can test this even if stdin/stdout have been redirected. Read
76 // permissions are required for unix_isatty().
77 int conin_fd = unix_open("CONIN$", O_RDONLY);
78 int conout_fd = unix_open("CONOUT$", O_RDWR);
79 for (const int fd : {conin_fd, conout_fd}) {
80 EXPECT_TRUE(fd >= 0);
81 EXPECT_EQ(1, unix_isatty(fd));
82 EXPECT_EQ(0, unix_close(fd));
83 }
84
85 // nul returns 1 from isatty(), make sure unix_isatty() corrects that.
86 for (auto flags : {O_RDONLY, O_RDWR}) {
87 int nul_fd = unix_open("nul", flags);
88 EXPECT_TRUE(nul_fd >= 0);
89 EXPECT_EQ(0, unix_isatty(nul_fd));
90 EXPECT_EQ(0, unix_close(nul_fd));
91 }
92
93 // Check a real file, both read-write and read-only.
94 TemporaryFile temp_file;
95 EXPECT_TRUE(temp_file.fd >= 0);
96 EXPECT_EQ(0, unix_isatty(temp_file.fd));
97
98 int temp_file_ro_fd = unix_open(temp_file.path, O_RDONLY);
99 EXPECT_TRUE(temp_file_ro_fd >= 0);
100 EXPECT_EQ(0, unix_isatty(temp_file_ro_fd));
101 EXPECT_EQ(0, unix_close(temp_file_ro_fd));
102
103 // Check a real OS pipe.
104 int pipe_fds[2];
105 EXPECT_EQ(0, _pipe(pipe_fds, 64, _O_BINARY));
106 EXPECT_EQ(0, unix_isatty(pipe_fds[0]));
107 EXPECT_EQ(0, unix_isatty(pipe_fds[1]));
108 EXPECT_EQ(0, _close(pipe_fds[0]));
109 EXPECT_EQ(0, _close(pipe_fds[1]));
110
111 // Make sure an invalid FD is handled correctly.
112 EXPECT_EQ(0, unix_isatty(-1));
113}
Spencer Lowa30b79a2015-11-15 16:29:36 -0800114
115void TestParseCompleteUTF8(const char* buf, const size_t buf_size,
116 const size_t expected_complete_bytes,
117 const std::vector<char>& expected_remaining_bytes) {
118 std::vector<char> remaining_bytes;
119 const size_t complete_bytes = internal::ParseCompleteUTF8(buf, buf + buf_size,
120 &remaining_bytes);
121 EXPECT_EQ(expected_complete_bytes, complete_bytes);
122 EXPECT_EQ(expected_remaining_bytes, remaining_bytes);
123}
124
125TEST(sysdeps_win32, ParseCompleteUTF8) {
126 const std::vector<std::vector<char>> multi_byte_sequences = {
127 { '\xc2', '\xa9' }, // 2 byte UTF-8 sequence
128 { '\xe1', '\xb4', '\xa8' }, // 3 byte UTF-8 sequence
129 { '\xf0', '\x9f', '\x98', '\x80' }, // 4 byte UTF-8 sequence
130 };
131 std::vector<std::vector<char>> all_sequences = {
132 {}, // 0 bytes
133 { '\0' }, // NULL byte
134 { 'a' }, // 1 byte UTF-8 sequence
135 };
136 all_sequences.insert(all_sequences.end(), multi_byte_sequences.begin(),
137 multi_byte_sequences.end());
138
139 // Vary a prefix of bytes in front of the sequence that we're actually interested in parsing.
140 for (const auto& prefix : all_sequences) {
141 // Parse (prefix + one byte of the sequence at a time)
142 for (const auto& seq : multi_byte_sequences) {
143 std::vector<char> buffer(prefix);
144
145 // For every byte of the sequence except the last
146 for (size_t i = 0; i < seq.size() - 1; ++i) {
147 buffer.push_back(seq[i]);
148
149 // When parsing an incomplete UTF-8 sequence, the amount of the buffer preceding
150 // the start of the incomplete UTF-8 sequence is valid. The remaining bytes are the
151 // bytes of the incomplete UTF-8 sequence.
152 TestParseCompleteUTF8(buffer.data(), buffer.size(), prefix.size(),
153 std::vector<char>(seq.begin(), seq.begin() + i + 1));
154 }
155
156 // For the last byte of the sequence
157 buffer.push_back(seq.back());
158 TestParseCompleteUTF8(buffer.data(), buffer.size(), buffer.size(), std::vector<char>());
159 }
160
161 // Parse (prefix (aka sequence) + invalid trailing bytes) to verify that the invalid
162 // trailing bytes are immediately "returned" to prevent them from being stuck in some
163 // buffer.
164 std::vector<char> buffer(prefix);
165 for (size_t i = 0; i < 8; ++i) {
166 buffer.push_back(0x80); // trailing byte
167 TestParseCompleteUTF8(buffer.data(), buffer.size(), buffer.size(), std::vector<char>());
168 }
169 }
170}