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/macros.h>
34#include <android-base/test_utils.h>
35
36#ifdef _WIN32
37static 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
45TEST(adb_utils, directory_exists) {
46#ifdef _WIN32
47  char profiles_dir[MAX_PATH];
48  DWORD cch = arraysize(profiles_dir);
49
50  // On typical Windows 7, 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("/proc"));
58  ASSERT_FALSE(directory_exists("/proc/does-not-exist"));
59#endif
60}
61
62#if defined(_WIN32)
63TEST(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
84TEST(adb_utils, escape_arg) {
85  ASSERT_EQ(R"('')", escape_arg(""));
86
87  ASSERT_EQ(R"('abc')", escape_arg("abc"));
88
89  ASSERT_EQ(R"(' abc')", escape_arg(" abc"));
90  ASSERT_EQ(R"(''\''abc')", escape_arg("'abc"));
91  ASSERT_EQ(R"('"abc')", escape_arg("\"abc"));
92  ASSERT_EQ(R"('\abc')", escape_arg("\\abc"));
93  ASSERT_EQ(R"('(abc')", escape_arg("(abc"));
94  ASSERT_EQ(R"(')abc')", escape_arg(")abc"));
95
96  ASSERT_EQ(R"('abc abc')", escape_arg("abc abc"));
97  ASSERT_EQ(R"('abc'\''abc')", escape_arg("abc'abc"));
98  ASSERT_EQ(R"('abc"abc')", escape_arg("abc\"abc"));
99  ASSERT_EQ(R"('abc\abc')", escape_arg("abc\\abc"));
100  ASSERT_EQ(R"('abc(abc')", escape_arg("abc(abc"));
101  ASSERT_EQ(R"('abc)abc')", escape_arg("abc)abc"));
102
103  ASSERT_EQ(R"('abc ')", escape_arg("abc "));
104  ASSERT_EQ(R"('abc'\''')", escape_arg("abc'"));
105  ASSERT_EQ(R"('abc"')", escape_arg("abc\""));
106  ASSERT_EQ(R"('abc\')", escape_arg("abc\\"));
107  ASSERT_EQ(R"('abc(')", escape_arg("abc("));
108  ASSERT_EQ(R"('abc)')", escape_arg("abc)"));
109}
110
111void test_mkdirs(const std::string& basepath) {
112  // Test creating a directory hierarchy.
113  ASSERT_TRUE(mkdirs(basepath));
114  // Test finding an existing directory hierarchy.
115  ASSERT_TRUE(mkdirs(basepath));
116  // Test mkdirs on an existing hierarchy with a trailing slash.
117  ASSERT_TRUE(mkdirs(basepath + '/'));
118#if defined(_WIN32)
119  ASSERT_TRUE(mkdirs(basepath + '\\'));
120#endif
121
122  const std::string filepath = basepath + "/file";
123  // Verify that the hierarchy was created by trying to create a file in it.
124  ASSERT_NE(-1, adb_creat(filepath.c_str(), 0600));
125  // If a file exists where we want a directory, the operation should fail.
126  ASSERT_FALSE(mkdirs(filepath));
127}
128
129TEST(adb_utils, mkdirs) {
130  TemporaryDir td;
131
132  // Absolute paths.
133  test_mkdirs(std::string(td.path) + "/dir/subdir");
134
135  // Relative paths.
136  ASSERT_EQ(0, chdir(td.path)) << strerror(errno);
137  test_mkdirs(std::string("relative/subrel"));
138}
139
140#if !defined(_WIN32)
141TEST(adb_utils, set_file_block_mode) {
142  int fd = adb_open("/dev/null", O_RDWR | O_APPEND);
143  ASSERT_GE(fd, 0);
144  int flags = fcntl(fd, F_GETFL, 0);
145  ASSERT_EQ(O_RDWR | O_APPEND, (flags & (O_RDWR | O_APPEND)));
146  ASSERT_TRUE(set_file_block_mode(fd, false));
147  int new_flags = fcntl(fd, F_GETFL, 0);
148  ASSERT_EQ(flags | O_NONBLOCK, new_flags);
149  ASSERT_TRUE(set_file_block_mode(fd, true));
150  new_flags = fcntl(fd, F_GETFL, 0);
151  ASSERT_EQ(flags, new_flags);
152  ASSERT_EQ(0, adb_close(fd));
153}
154#endif
155
156TEST(adb_utils, test_forward_targets_are_valid) {
157    std::string error;
158
159    // Source port can be >= 0.
160    EXPECT_FALSE(forward_targets_are_valid("tcp:-1", "tcp:9000", &error));
161    EXPECT_TRUE(forward_targets_are_valid("tcp:0", "tcp:9000", &error));
162    EXPECT_TRUE(forward_targets_are_valid("tcp:8000", "tcp:9000", &error));
163
164    // Destination port must be >0.
165    EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:-1", &error));
166    EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:0", &error));
167
168    // Port must be a number.
169    EXPECT_FALSE(forward_targets_are_valid("tcp:", "tcp:9000", &error));
170    EXPECT_FALSE(forward_targets_are_valid("tcp:a", "tcp:9000", &error));
171    EXPECT_FALSE(forward_targets_are_valid("tcp:22x", "tcp:9000", &error));
172    EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:", &error));
173    EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:a", &error));
174    EXPECT_FALSE(forward_targets_are_valid("tcp:8000", "tcp:22x", &error));
175}
176