Elliott Hughes | 5830577 | 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 | cf168a8 | 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 | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include "sysdeps.h" |
| 32 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 33 | #include <android-base/macros.h> |
| 34 | #include <android-base/test_utils.h> |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 35 | |
Spencer Low | cf168a8 | 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 | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 45 | TEST(adb_utils, directory_exists) { |
Spencer Low | cf168a8 | 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 | cbf26b7 | 2015-08-03 20:43:24 -0700 | [diff] [blame] | 51 | ASSERT_TRUE(GetProfilesDirectoryA(profiles_dir, &cch)); |
Spencer Low | cf168a8 | 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 | 5830577 | 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 | cf168a8 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 72 | #endif |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST(adb_utils, escape_arg) { |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 76 | ASSERT_EQ(R"('')", escape_arg("")); |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 77 | |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 78 | ASSERT_EQ(R"('abc')", escape_arg("abc")); |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 79 | |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 80 | ASSERT_EQ(R"(' abc')", escape_arg(" abc")); |
Elliott Hughes | 84b0bf2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 81 | ASSERT_EQ(R"(''\''abc')", escape_arg("'abc")); |
Elliott Hughes | 5498ade | 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 | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 86 | |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 87 | ASSERT_EQ(R"('abc abc')", escape_arg("abc abc")); |
Elliott Hughes | 84b0bf2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 88 | ASSERT_EQ(R"('abc'\''abc')", escape_arg("abc'abc")); |
Elliott Hughes | 5498ade | 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 | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 93 | |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 94 | ASSERT_EQ(R"('abc ')", escape_arg("abc ")); |
Elliott Hughes | 84b0bf2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 95 | ASSERT_EQ(R"('abc'\''')", escape_arg("abc'")); |
Elliott Hughes | 5498ade | 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 | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 100 | } |
Elliott Hughes | 3d5f60d | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 101 | |
Elliott Hughes | 5c74270 | 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")); |
Josh Gao | 787f344 | 2015-11-03 18:52:35 -0800 | [diff] [blame] | 105 | EXPECT_EQ("sh", adb_basename("/system/bin/sh/")); |
| 106 | } |
| 107 | |
| 108 | TEST(adb_utils, adb_dirname) { |
| 109 | EXPECT_EQ("/system/bin", adb_dirname("/system/bin/sh")); |
| 110 | EXPECT_EQ(".", adb_dirname("sh")); |
| 111 | EXPECT_EQ("/system/bin", adb_dirname("/system/bin/sh/")); |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 114 | void test_mkdirs(const std::string basepath) { |
Spencer Low | 85c45bd | 2016-02-10 15:03:50 -0800 | [diff] [blame^] | 115 | // Test creating a directory hierarchy. |
| 116 | EXPECT_TRUE(mkdirs(basepath)); |
| 117 | // Test finding an existing directory hierarchy. |
| 118 | EXPECT_TRUE(mkdirs(basepath)); |
| 119 | const std::string filepath = basepath + "/file"; |
| 120 | // Verify that the hierarchy was created by trying to create a file in it. |
| 121 | EXPECT_NE(-1, adb_creat(filepath.c_str(), 0600)); |
| 122 | // If a file exists where we want a directory, the operation should fail. |
| 123 | EXPECT_FALSE(mkdirs(filepath)); |
Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 126 | TEST(adb_utils, mkdirs) { |
| 127 | TemporaryDir td; |
Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 128 | |
| 129 | // Absolute paths. |
Spencer Low | 85c45bd | 2016-02-10 15:03:50 -0800 | [diff] [blame^] | 130 | test_mkdirs(std::string(td.path) + "/dir/subdir"); |
Spencer Low | 22191c3 | 2015-08-01 17:29:23 -0700 | [diff] [blame] | 131 | |
| 132 | // Relative paths. |
| 133 | ASSERT_EQ(0, chdir(td.path)) << strerror(errno); |
Spencer Low | 85c45bd | 2016-02-10 15:03:50 -0800 | [diff] [blame^] | 134 | test_mkdirs(std::string("relative/subrel")); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 135 | } |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 136 | |
| 137 | #if !defined(_WIN32) |
| 138 | TEST(adb_utils, set_file_block_mode) { |
| 139 | int fd = adb_open("/dev/null", O_RDWR | O_APPEND); |
| 140 | ASSERT_GE(fd, 0); |
| 141 | int flags = fcntl(fd, F_GETFL, 0); |
| 142 | ASSERT_EQ(O_RDWR | O_APPEND, (flags & (O_RDWR | O_APPEND))); |
| 143 | ASSERT_TRUE(set_file_block_mode(fd, false)); |
| 144 | int new_flags = fcntl(fd, F_GETFL, 0); |
| 145 | ASSERT_EQ(flags | O_NONBLOCK, new_flags); |
| 146 | ASSERT_TRUE(set_file_block_mode(fd, true)); |
| 147 | new_flags = fcntl(fd, F_GETFL, 0); |
| 148 | ASSERT_EQ(flags, new_flags); |
| 149 | ASSERT_EQ(0, adb_close(fd)); |
| 150 | } |
| 151 | #endif |