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