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 | |
Spencer Low | c9ddd81 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 33 | #include <base/macros.h> |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 34 | #include <base/test_utils.h> |
| 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 | |
| 55 | // On modern (English?) Windows, this is a directory symbolic link to |
| 56 | // C:\ProgramData. Symbolic links are rare on Windows and the user requires |
| 57 | // a special permission (by default granted to Administrative users) to |
| 58 | // create symbolic links. |
| 59 | ASSERT_FALSE(directory_exists(subdir(profiles_dir, "All Users"))); |
| 60 | |
| 61 | // On modern (English?) Windows, this is a directory junction to |
| 62 | // C:\Users\Default. Junctions are used throughout user profile directories |
| 63 | // for backwards compatibility and they don't require any special permissions |
| 64 | // to create. |
| 65 | ASSERT_FALSE(directory_exists(subdir(profiles_dir, "Default User"))); |
| 66 | |
| 67 | ASSERT_FALSE(directory_exists(subdir(profiles_dir, "does-not-exist"))); |
| 68 | #else |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 69 | ASSERT_TRUE(directory_exists("/proc")); |
| 70 | ASSERT_FALSE(directory_exists("/proc/self")); // Symbolic link. |
| 71 | ASSERT_FALSE(directory_exists("/proc/does-not-exist")); |
Spencer Low | c9ddd81 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 72 | #endif |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST(adb_utils, escape_arg) { |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 76 | ASSERT_EQ(R"('')", escape_arg("")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 77 | |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 78 | ASSERT_EQ(R"('abc')", escape_arg("abc")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 79 | |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 80 | ASSERT_EQ(R"(' abc')", escape_arg(" abc")); |
Elliott Hughes | de52ce2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 81 | ASSERT_EQ(R"(''\''abc')", escape_arg("'abc")); |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 82 | ASSERT_EQ(R"('"abc')", escape_arg("\"abc")); |
| 83 | ASSERT_EQ(R"('\abc')", escape_arg("\\abc")); |
| 84 | ASSERT_EQ(R"('(abc')", escape_arg("(abc")); |
| 85 | ASSERT_EQ(R"(')abc')", escape_arg(")abc")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 86 | |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 87 | ASSERT_EQ(R"('abc abc')", escape_arg("abc abc")); |
Elliott Hughes | de52ce2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 88 | ASSERT_EQ(R"('abc'\''abc')", escape_arg("abc'abc")); |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 89 | ASSERT_EQ(R"('abc"abc')", escape_arg("abc\"abc")); |
| 90 | ASSERT_EQ(R"('abc\abc')", escape_arg("abc\\abc")); |
| 91 | ASSERT_EQ(R"('abc(abc')", escape_arg("abc(abc")); |
| 92 | ASSERT_EQ(R"('abc)abc')", escape_arg("abc)abc")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 93 | |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 94 | ASSERT_EQ(R"('abc ')", escape_arg("abc ")); |
Elliott Hughes | de52ce2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 95 | ASSERT_EQ(R"('abc'\''')", escape_arg("abc'")); |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 96 | ASSERT_EQ(R"('abc"')", escape_arg("abc\"")); |
| 97 | ASSERT_EQ(R"('abc\')", escape_arg("abc\\")); |
| 98 | ASSERT_EQ(R"('abc(')", escape_arg("abc(")); |
| 99 | ASSERT_EQ(R"('abc)')", escape_arg("abc)")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 100 | } |
Elliott Hughes | 09ccf1f | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 101 | |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 102 | TEST(adb_utils, adb_basename) { |
| 103 | EXPECT_EQ("sh", adb_basename("/system/bin/sh")); |
| 104 | EXPECT_EQ("sh", adb_basename("sh")); |
| 105 | } |
| 106 | |
Elliott Hughes | 09ccf1f | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 107 | TEST(adb_utils, parse_host_and_port) { |
| 108 | std::string canonical_address; |
| 109 | std::string host; |
| 110 | int port; |
| 111 | std::string error; |
| 112 | |
| 113 | // Name, default port. |
| 114 | port = 123; |
| 115 | ASSERT_TRUE(parse_host_and_port("www.google.com", &canonical_address, &host, &port, &error)); |
| 116 | ASSERT_EQ("www.google.com:123", canonical_address); |
| 117 | ASSERT_EQ("www.google.com", host); |
| 118 | ASSERT_EQ(123, port); |
| 119 | |
| 120 | // Name, explicit port. |
| 121 | ASSERT_TRUE(parse_host_and_port("www.google.com:666", &canonical_address, &host, &port, &error)); |
| 122 | ASSERT_EQ("www.google.com:666", canonical_address); |
| 123 | ASSERT_EQ("www.google.com", host); |
| 124 | ASSERT_EQ(666, port); |
| 125 | |
| 126 | // IPv4, default port. |
| 127 | port = 123; |
| 128 | ASSERT_TRUE(parse_host_and_port("1.2.3.4", &canonical_address, &host, &port, &error)); |
| 129 | ASSERT_EQ("1.2.3.4:123", canonical_address); |
| 130 | ASSERT_EQ("1.2.3.4", host); |
| 131 | ASSERT_EQ(123, port); |
| 132 | |
| 133 | // IPv4, explicit port. |
| 134 | ASSERT_TRUE(parse_host_and_port("1.2.3.4:666", &canonical_address, &host, &port, &error)); |
| 135 | ASSERT_EQ("1.2.3.4:666", canonical_address); |
| 136 | ASSERT_EQ("1.2.3.4", host); |
| 137 | ASSERT_EQ(666, port); |
| 138 | |
| 139 | // Simple IPv6, default port. |
| 140 | port = 123; |
| 141 | ASSERT_TRUE(parse_host_and_port("::1", &canonical_address, &host, &port, &error)); |
| 142 | ASSERT_EQ("[::1]:123", canonical_address); |
| 143 | ASSERT_EQ("::1", host); |
| 144 | ASSERT_EQ(123, port); |
| 145 | |
| 146 | // Simple IPv6, explicit port. |
| 147 | ASSERT_TRUE(parse_host_and_port("[::1]:666", &canonical_address, &host, &port, &error)); |
| 148 | ASSERT_EQ("[::1]:666", canonical_address); |
| 149 | ASSERT_EQ("::1", host); |
| 150 | ASSERT_EQ(666, port); |
| 151 | |
| 152 | // Hairy IPv6, default port. |
| 153 | port = 123; |
| 154 | ASSERT_TRUE(parse_host_and_port("fe80::200:5aee:feaa:20a2", &canonical_address, &host, &port, &error)); |
| 155 | ASSERT_EQ("[fe80::200:5aee:feaa:20a2]:123", canonical_address); |
| 156 | ASSERT_EQ("fe80::200:5aee:feaa:20a2", host); |
| 157 | ASSERT_EQ(123, port); |
| 158 | |
| 159 | // Simple IPv6, explicit port. |
| 160 | ASSERT_TRUE(parse_host_and_port("[fe80::200:5aee:feaa:20a2]:666", &canonical_address, &host, &port, &error)); |
| 161 | ASSERT_EQ("[fe80::200:5aee:feaa:20a2]:666", canonical_address); |
| 162 | ASSERT_EQ("fe80::200:5aee:feaa:20a2", host); |
| 163 | ASSERT_EQ(666, port); |
| 164 | |
| 165 | // Invalid IPv4. |
| 166 | EXPECT_FALSE(parse_host_and_port("1.2.3.4:", &canonical_address, &host, &port, &error)); |
| 167 | EXPECT_FALSE(parse_host_and_port("1.2.3.4::", &canonical_address, &host, &port, &error)); |
| 168 | EXPECT_FALSE(parse_host_and_port("1.2.3.4:hello", &canonical_address, &host, &port, &error)); |
| 169 | EXPECT_FALSE(parse_host_and_port(":123", &canonical_address, &host, &port, &error)); |
| 170 | |
| 171 | // Invalid IPv6. |
| 172 | EXPECT_FALSE(parse_host_and_port(":1", &canonical_address, &host, &port, &error)); |
| 173 | EXPECT_FALSE(parse_host_and_port("::::::::1", &canonical_address, &host, &port, &error)); |
| 174 | EXPECT_FALSE(parse_host_and_port("[::1", &canonical_address, &host, &port, &error)); |
| 175 | EXPECT_FALSE(parse_host_and_port("[::1]", &canonical_address, &host, &port, &error)); |
| 176 | EXPECT_FALSE(parse_host_and_port("[::1]:", &canonical_address, &host, &port, &error)); |
| 177 | EXPECT_FALSE(parse_host_and_port("[::1]::", &canonical_address, &host, &port, &error)); |
| 178 | EXPECT_FALSE(parse_host_and_port("[::1]:hello", &canonical_address, &host, &port, &error)); |
| 179 | |
| 180 | // Invalid ports. |
| 181 | EXPECT_FALSE(parse_host_and_port("[::1]:-1", &canonical_address, &host, &port, &error)); |
| 182 | EXPECT_FALSE(parse_host_and_port("[::1]:0", &canonical_address, &host, &port, &error)); |
| 183 | EXPECT_FALSE(parse_host_and_port("[::1]:65536", &canonical_address, &host, &port, &error)); |
| 184 | EXPECT_FALSE(parse_host_and_port("1.2.3.4:-1", &canonical_address, &host, &port, &error)); |
| 185 | EXPECT_FALSE(parse_host_and_port("1.2.3.4:0", &canonical_address, &host, &port, &error)); |
| 186 | EXPECT_FALSE(parse_host_and_port("1.2.3.4:65536", &canonical_address, &host, &port, &error)); |
| 187 | } |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 188 | |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 189 | void test_mkdirs(const std::string basepath) { |
| 190 | EXPECT_TRUE(mkdirs(basepath)); |
| 191 | EXPECT_NE(-1, adb_creat(basepath.c_str(), 0600)); |
| 192 | EXPECT_FALSE(mkdirs(basepath + "/subdir/")); |
| 193 | } |
| 194 | |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 195 | TEST(adb_utils, mkdirs) { |
| 196 | TemporaryDir td; |
Spencer Low | 939d000 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 197 | |
| 198 | // Absolute paths. |
| 199 | test_mkdirs(std::string(td.path) + "/dir/subdir/file"); |
| 200 | |
| 201 | // Relative paths. |
| 202 | ASSERT_EQ(0, chdir(td.path)) << strerror(errno); |
| 203 | test_mkdirs(std::string("relative/subrel/file")); |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 204 | } |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame^] | 205 | |
| 206 | #if !defined(_WIN32) |
| 207 | TEST(adb_utils, set_file_block_mode) { |
| 208 | int fd = adb_open("/dev/null", O_RDWR | O_APPEND); |
| 209 | ASSERT_GE(fd, 0); |
| 210 | int flags = fcntl(fd, F_GETFL, 0); |
| 211 | ASSERT_EQ(O_RDWR | O_APPEND, (flags & (O_RDWR | O_APPEND))); |
| 212 | ASSERT_TRUE(set_file_block_mode(fd, false)); |
| 213 | int new_flags = fcntl(fd, F_GETFL, 0); |
| 214 | ASSERT_EQ(flags | O_NONBLOCK, new_flags); |
| 215 | ASSERT_TRUE(set_file_block_mode(fd, true)); |
| 216 | new_flags = fcntl(fd, F_GETFL, 0); |
| 217 | ASSERT_EQ(flags, new_flags); |
| 218 | ASSERT_EQ(0, adb_close(fd)); |
| 219 | } |
| 220 | #endif |