1 /* 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 21 #include <android-base/test_utils.h> 22 23 TEST(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), 0U); 70 } 71 } 72 73 void 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 81 TEST(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 91 // Test success case. 92 // Wine returns "Success" for strerror(0), Windows returns "No error", so accept both. 93 std::string success = adb_strerror(0); 94 EXPECT_TRUE(success == "Success" || success == "No error") << "strerror(0) = " << success; 95 96 // Test error that regular strerror() should have a string for. 97 TestAdbStrError(EPERM, "Operation not permitted"); 98 // Test error that regular strerror() doesn't have a string for, but that 99 // adb_strerror() returns. 100 TestAdbStrError(ECONNRESET, "Connection reset by peer"); 101 } 102 103 TEST(sysdeps_win32, unix_isatty) { 104 // stdin and stdout should be consoles. Use CONIN$ and CONOUT$ special files 105 // so that we can test this even if stdin/stdout have been redirected. Read 106 // permissions are required for unix_isatty(). 107 int conin_fd = unix_open("CONIN$", O_RDONLY); 108 int conout_fd = unix_open("CONOUT$", O_RDWR); 109 for (const int fd : {conin_fd, conout_fd}) { 110 EXPECT_TRUE(fd >= 0); 111 EXPECT_EQ(1, unix_isatty(fd)); 112 EXPECT_EQ(0, unix_close(fd)); 113 } 114 115 // nul returns 1 from isatty(), make sure unix_isatty() corrects that. 116 for (auto flags : {O_RDONLY, O_RDWR}) { 117 int nul_fd = unix_open("nul", flags); 118 EXPECT_TRUE(nul_fd >= 0); 119 EXPECT_EQ(0, unix_isatty(nul_fd)); 120 EXPECT_EQ(0, unix_close(nul_fd)); 121 } 122 123 // Check a real file, both read-write and read-only. 124 TemporaryFile temp_file; 125 EXPECT_TRUE(temp_file.fd >= 0); 126 EXPECT_EQ(0, unix_isatty(temp_file.fd)); 127 128 int temp_file_ro_fd = unix_open(temp_file.path, O_RDONLY); 129 EXPECT_TRUE(temp_file_ro_fd >= 0); 130 EXPECT_EQ(0, unix_isatty(temp_file_ro_fd)); 131 EXPECT_EQ(0, unix_close(temp_file_ro_fd)); 132 133 // Check a real OS pipe. 134 int pipe_fds[2]; 135 EXPECT_EQ(0, _pipe(pipe_fds, 64, _O_BINARY)); 136 EXPECT_EQ(0, unix_isatty(pipe_fds[0])); 137 EXPECT_EQ(0, unix_isatty(pipe_fds[1])); 138 EXPECT_EQ(0, _close(pipe_fds[0])); 139 EXPECT_EQ(0, _close(pipe_fds[1])); 140 141 // Make sure an invalid FD is handled correctly. 142 EXPECT_EQ(0, unix_isatty(-1)); 143 } 144 145 void TestParseCompleteUTF8(const char* buf, const size_t buf_size, 146 const size_t expected_complete_bytes, 147 const std::vector<char>& expected_remaining_bytes) { 148 std::vector<char> remaining_bytes; 149 const size_t complete_bytes = internal::ParseCompleteUTF8(buf, buf + buf_size, 150 &remaining_bytes); 151 EXPECT_EQ(expected_complete_bytes, complete_bytes); 152 EXPECT_EQ(expected_remaining_bytes, remaining_bytes); 153 } 154 155 TEST(sysdeps_win32, ParseCompleteUTF8) { 156 const std::vector<std::vector<char>> multi_byte_sequences = { 157 { '\xc2', '\xa9' }, // 2 byte UTF-8 sequence 158 { '\xe1', '\xb4', '\xa8' }, // 3 byte UTF-8 sequence 159 { '\xf0', '\x9f', '\x98', '\x80' }, // 4 byte UTF-8 sequence 160 }; 161 std::vector<std::vector<char>> all_sequences = { 162 {}, // 0 bytes 163 { '\0' }, // NULL byte 164 { 'a' }, // 1 byte UTF-8 sequence 165 }; 166 all_sequences.insert(all_sequences.end(), multi_byte_sequences.begin(), 167 multi_byte_sequences.end()); 168 169 // Vary a prefix of bytes in front of the sequence that we're actually interested in parsing. 170 for (const auto& prefix : all_sequences) { 171 // Parse (prefix + one byte of the sequence at a time) 172 for (const auto& seq : multi_byte_sequences) { 173 std::vector<char> buffer(prefix); 174 175 // For every byte of the sequence except the last 176 for (size_t i = 0; i < seq.size() - 1; ++i) { 177 buffer.push_back(seq[i]); 178 179 // When parsing an incomplete UTF-8 sequence, the amount of the buffer preceding 180 // the start of the incomplete UTF-8 sequence is valid. The remaining bytes are the 181 // bytes of the incomplete UTF-8 sequence. 182 TestParseCompleteUTF8(buffer.data(), buffer.size(), prefix.size(), 183 std::vector<char>(seq.begin(), seq.begin() + i + 1)); 184 } 185 186 // For the last byte of the sequence 187 buffer.push_back(seq.back()); 188 TestParseCompleteUTF8(buffer.data(), buffer.size(), buffer.size(), std::vector<char>()); 189 } 190 191 // Parse (prefix (aka sequence) + invalid trailing bytes) to verify that the invalid 192 // trailing bytes are immediately "returned" to prevent them from being stuck in some 193 // buffer. 194 std::vector<char> buffer(prefix); 195 for (size_t i = 0; i < 8; ++i) { 196 buffer.push_back(0x80); // trailing byte 197 TestParseCompleteUTF8(buffer.data(), buffer.size(), buffer.size(), std::vector<char>()); 198 } 199 } 200 } 201