Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 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 "adb_utils.h" |
| 18 | |
Spencer Low | c9ddd81 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 19 | #ifdef _WIN32 |
| 20 | #include <windows.h> |
| 21 | #include <userenv.h> |
| 22 | #endif |
| 23 | |
| 24 | #include <string> |
| 25 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include "sysdeps.h" |
| 32 | |
Mark Salyzyn | 739991d | 2018-11-12 12:45:59 -0800 | [diff] [blame] | 33 | #include <android-base/file.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 34 | #include <android-base/macros.h> |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 35 | |
Spencer Low | c9ddd81 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 36 | #ifdef _WIN32 |
| 37 | static std::string subdir(const char* parent, const char* child) { |
| 38 | std::string str(parent); |
| 39 | str += OS_PATH_SEPARATOR; |
| 40 | str += child; |
| 41 | return str; |
| 42 | } |
| 43 | #endif |
| 44 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 45 | TEST(adb_utils, directory_exists) { |
Spencer Low | c9ddd81 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 46 | #ifdef _WIN32 |
| 47 | char profiles_dir[MAX_PATH]; |
| 48 | DWORD cch = arraysize(profiles_dir); |
| 49 | |
| 50 | // On typical Windows 7, returns C:\Users |
Spencer Low | f96845c | 2015-08-03 20:43:24 -0700 | [diff] [blame] | 51 | ASSERT_TRUE(GetProfilesDirectoryA(profiles_dir, &cch)); |
Spencer Low | c9ddd81 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 52 | |
| 53 | ASSERT_TRUE(directory_exists(profiles_dir)); |
| 54 | |
Spencer Low | c9ddd81 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 55 | ASSERT_FALSE(directory_exists(subdir(profiles_dir, "does-not-exist"))); |
| 56 | #else |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 57 | ASSERT_TRUE(directory_exists("/proc")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 58 | ASSERT_FALSE(directory_exists("/proc/does-not-exist")); |
Spencer Low | c9ddd81 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 59 | #endif |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Josh Gao | cde85bf | 2016-07-28 18:30:46 -0700 | [diff] [blame] | 62 | #if defined(_WIN32) |
| 63 | TEST(adb_utils, directory_exists_win32_symlink_junction) { |
| 64 | char profiles_dir[MAX_PATH]; |
| 65 | DWORD cch = arraysize(profiles_dir); |
| 66 | |
| 67 | // On typical Windows 7, returns C:\Users |
| 68 | ASSERT_TRUE(GetProfilesDirectoryA(profiles_dir, &cch)); |
| 69 | |
| 70 | // On modern (English?) Windows, this is a directory symbolic link to |
| 71 | // C:\ProgramData. Symbolic links are rare on Windows and the user requires |
| 72 | // a special permission (by default granted to Administrative users) to |
| 73 | // create symbolic links. |
| 74 | EXPECT_FALSE(directory_exists(subdir(profiles_dir, "All Users"))); |
| 75 | |
| 76 | // On modern (English?) Windows, this is a directory junction to |
| 77 | // C:\Users\Default. Junctions are used throughout user profile directories |
| 78 | // for backwards compatibility and they don't require any special permissions |
| 79 | // to create. |
| 80 | EXPECT_FALSE(directory_exists(subdir(profiles_dir, "Default User"))); |
| 81 | } |
| 82 | #endif |
| 83 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 84 | TEST(adb_utils, escape_arg) { |
Ryan Prichard | 934d4b1 | 2018-07-09 23:51:58 -0700 | [diff] [blame] | 85 | EXPECT_EQ(R"('')", escape_arg("")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 86 | |
Ryan Prichard | 934d4b1 | 2018-07-09 23:51:58 -0700 | [diff] [blame] | 87 | EXPECT_EQ(R"('abc')", escape_arg("abc")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 88 | |
Ryan Prichard | 934d4b1 | 2018-07-09 23:51:58 -0700 | [diff] [blame] | 89 | auto wrap = [](const std::string& x) { return '\'' + x + '\''; }; |
| 90 | const std::string q = R"('\'')"; |
| 91 | EXPECT_EQ(wrap(q), escape_arg("'")); |
| 92 | EXPECT_EQ(wrap(q + q), escape_arg("''")); |
| 93 | EXPECT_EQ(wrap(q + "abc" + q), escape_arg("'abc'")); |
| 94 | EXPECT_EQ(wrap(q + "abc"), escape_arg("'abc")); |
| 95 | EXPECT_EQ(wrap("abc" + q), escape_arg("abc'")); |
| 96 | EXPECT_EQ(wrap("abc" + q + "def"), escape_arg("abc'def")); |
| 97 | EXPECT_EQ(wrap("a" + q + "b" + q + "c"), escape_arg("a'b'c")); |
| 98 | EXPECT_EQ(wrap("a" + q + "bcde" + q + "f"), escape_arg("a'bcde'f")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 99 | |
Ryan Prichard | 934d4b1 | 2018-07-09 23:51:58 -0700 | [diff] [blame] | 100 | EXPECT_EQ(R"(' abc')", escape_arg(" abc")); |
| 101 | EXPECT_EQ(R"('"abc')", escape_arg("\"abc")); |
| 102 | EXPECT_EQ(R"('\abc')", escape_arg("\\abc")); |
| 103 | EXPECT_EQ(R"('(abc')", escape_arg("(abc")); |
| 104 | EXPECT_EQ(R"(')abc')", escape_arg(")abc")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 105 | |
Ryan Prichard | 934d4b1 | 2018-07-09 23:51:58 -0700 | [diff] [blame] | 106 | EXPECT_EQ(R"('abc abc')", escape_arg("abc abc")); |
| 107 | EXPECT_EQ(R"('abc"abc')", escape_arg("abc\"abc")); |
| 108 | EXPECT_EQ(R"('abc\abc')", escape_arg("abc\\abc")); |
| 109 | EXPECT_EQ(R"('abc(abc')", escape_arg("abc(abc")); |
| 110 | EXPECT_EQ(R"('abc)abc')", escape_arg("abc)abc")); |
| 111 | |
| 112 | EXPECT_EQ(R"('abc ')", escape_arg("abc ")); |
| 113 | EXPECT_EQ(R"('abc"')", escape_arg("abc\"")); |
| 114 | EXPECT_EQ(R"('abc\')", escape_arg("abc\\")); |
| 115 | EXPECT_EQ(R"('abc(')", escape_arg("abc(")); |
| 116 | EXPECT_EQ(R"('abc)')", escape_arg("abc)")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 117 | } |
Elliott Hughes | 09ccf1f | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 118 | |
Chih-Hung Hsieh | 90b40f6 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 119 | void test_mkdirs(const std::string& basepath) { |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 120 | // Test creating a directory hierarchy. |
Josh Gao | a4f9c17 | 2016-07-28 18:09:48 -0700 | [diff] [blame] | 121 | ASSERT_TRUE(mkdirs(basepath)); |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 122 | // Test finding an existing directory hierarchy. |
Josh Gao | a4f9c17 | 2016-07-28 18:09:48 -0700 | [diff] [blame] | 123 | ASSERT_TRUE(mkdirs(basepath)); |
| 124 | // Test mkdirs on an existing hierarchy with a trailing slash. |
| 125 | ASSERT_TRUE(mkdirs(basepath + '/')); |
| 126 | #if defined(_WIN32) |
| 127 | ASSERT_TRUE(mkdirs(basepath + '\\')); |
| 128 | #endif |
| 129 | |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 130 | const std::string filepath = basepath + "/file"; |
| 131 | // Verify that the hierarchy was created by trying to create a file in it. |
Josh Gao | a4f9c17 | 2016-07-28 18:09:48 -0700 | [diff] [blame] | 132 | ASSERT_NE(-1, adb_creat(filepath.c_str(), 0600)); |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 133 | // If a file exists where we want a directory, the operation should fail. |
Josh Gao | a4f9c17 | 2016-07-28 18:09:48 -0700 | [diff] [blame] | 134 | ASSERT_FALSE(mkdirs(filepath)); |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 137 | TEST(adb_utils, mkdirs) { |
| 138 | TemporaryDir td; |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 139 | |
| 140 | // Absolute paths. |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 141 | test_mkdirs(std::string(td.path) + "/dir/subdir"); |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 142 | |
| 143 | // Relative paths. |
| 144 | ASSERT_EQ(0, chdir(td.path)) << strerror(errno); |
Spencer Low | 1ec9ceb | 2016-02-10 15:03:50 -0800 | [diff] [blame] | 145 | test_mkdirs(std::string("relative/subrel")); |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 146 | } |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 147 | |
| 148 | #if !defined(_WIN32) |
| 149 | TEST(adb_utils, set_file_block_mode) { |
Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 150 | unique_fd fd(adb_open("/dev/null", O_RDWR | O_APPEND)); |
| 151 | ASSERT_GE(fd, 0); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 152 | int flags = fcntl(fd.get(), F_GETFL, 0); |
Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 153 | ASSERT_EQ(O_RDWR | O_APPEND, (flags & (O_RDWR | O_APPEND))); |
| 154 | ASSERT_TRUE(set_file_block_mode(fd, false)); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 155 | int new_flags = fcntl(fd.get(), F_GETFL, 0); |
Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 156 | ASSERT_EQ(flags | O_NONBLOCK, new_flags); |
| 157 | ASSERT_TRUE(set_file_block_mode(fd, true)); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 158 | new_flags = fcntl(fd.get(), F_GETFL, 0); |
Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 159 | ASSERT_EQ(flags, new_flags); |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 160 | } |
| 161 | #endif |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 162 | |
| 163 | TEST(adb_utils, test_forward_targets_are_valid) { |
| 164 | std::string error; |
| 165 | |
| 166 | // Source port can be >= 0. |
| 167 | EXPECT_FALSE(forward_targets_are_valid("tcp:-1", "tcp:9000", &error)); |
| 168 | EXPECT_TRUE(forward_targets_are_valid("tcp:0", "tcp:9000", &error)); |
| 169 | EXPECT_TRUE(forward_targets_are_valid("tcp:8000", "tcp:9000", &error)); |
| 170 | |
| 171 | // Destination port must be >0. |
| 172 | EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:-1", &error)); |
| 173 | EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:0", &error)); |
| 174 | |
| 175 | // Port must be a number. |
| 176 | EXPECT_FALSE(forward_targets_are_valid("tcp:", "tcp:9000", &error)); |
| 177 | EXPECT_FALSE(forward_targets_are_valid("tcp:a", "tcp:9000", &error)); |
| 178 | EXPECT_FALSE(forward_targets_are_valid("tcp:22x", "tcp:9000", &error)); |
| 179 | EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:", &error)); |
| 180 | EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:a", &error)); |
| 181 | EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:22x", &error)); |
| 182 | } |
Josh Gao | 4219a7b | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 183 | |
| 184 | void TestParseUint(std::string_view string, bool expected_success, uint32_t expected_value = 0) { |
| 185 | // Standalone. |
| 186 | { |
| 187 | uint32_t value; |
| 188 | std::string_view remaining; |
| 189 | bool success = ParseUint(&value, string, &remaining); |
| 190 | EXPECT_EQ(success, expected_success); |
| 191 | if (expected_success) { |
| 192 | EXPECT_EQ(value, expected_value); |
| 193 | } |
| 194 | EXPECT_TRUE(remaining.empty()); |
| 195 | } |
| 196 | |
| 197 | // With trailing text. |
| 198 | { |
| 199 | std::string text = std::string(string) + "foo"; |
| 200 | uint32_t value; |
| 201 | std::string_view remaining; |
| 202 | bool success = ParseUint(&value, text, &remaining); |
| 203 | EXPECT_EQ(success, expected_success); |
| 204 | if (expected_success) { |
| 205 | EXPECT_EQ(value, expected_value); |
| 206 | EXPECT_EQ(remaining, "foo"); |
| 207 | } |
| 208 | } |
Josh Gao | 8477cb4 | 2019-02-20 19:46:26 -0800 | [diff] [blame] | 209 | |
| 210 | // With trailing text, without remaining. |
| 211 | { |
| 212 | std::string text = std::string(string) + "foo"; |
| 213 | uint32_t value; |
| 214 | bool success = ParseUint(&value, text, nullptr); |
| 215 | EXPECT_EQ(success, false); |
| 216 | } |
Josh Gao | 4219a7b | 2018-12-20 13:17:04 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | TEST(adb_utils, ParseUint) { |
| 220 | TestParseUint("", false); |
| 221 | TestParseUint("foo", false); |
| 222 | TestParseUint("foo123", false); |
| 223 | TestParseUint("-1", false); |
| 224 | |
| 225 | TestParseUint("123", true, 123); |
| 226 | TestParseUint("9999999999999999999999999", false); |
| 227 | TestParseUint(std::to_string(UINT32_MAX), true, UINT32_MAX); |
| 228 | TestParseUint("0" + std::to_string(UINT32_MAX), true, UINT32_MAX); |
| 229 | TestParseUint(std::to_string(static_cast<uint64_t>(UINT32_MAX) + 1), false); |
| 230 | TestParseUint("0" + std::to_string(static_cast<uint64_t>(UINT32_MAX) + 1), false); |
| 231 | |
| 232 | std::string x = std::to_string(UINT32_MAX) + "123"; |
| 233 | std::string_view substr = std::string_view(x).substr(0, std::to_string(UINT32_MAX).size()); |
| 234 | TestParseUint(substr, true, UINT32_MAX); |
| 235 | } |