2015-04-17 20:57:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "adb_utils.h"
|
|
|
|
|
2015-05-24 22:36:28 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <userenv.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2015-04-17 20:57:15 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2015-05-06 20:26:00 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2015-12-05 06:00:26 +00:00
|
|
|
#include <android-base/macros.h>
|
|
|
|
#include <android-base/test_utils.h>
|
2015-05-06 20:26:00 +00:00
|
|
|
|
2015-05-24 22:36:28 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
static std::string subdir(const char* parent, const char* child) {
|
|
|
|
std::string str(parent);
|
|
|
|
str += OS_PATH_SEPARATOR;
|
|
|
|
str += child;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-17 20:57:15 +00:00
|
|
|
TEST(adb_utils, directory_exists) {
|
2015-05-24 22:36:28 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
char profiles_dir[MAX_PATH];
|
|
|
|
DWORD cch = arraysize(profiles_dir);
|
|
|
|
|
|
|
|
// On typical Windows 7, returns C:\Users
|
2015-08-04 03:43:24 +00:00
|
|
|
ASSERT_TRUE(GetProfilesDirectoryA(profiles_dir, &cch));
|
2015-05-24 22:36:28 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(directory_exists(profiles_dir));
|
|
|
|
|
|
|
|
// On modern (English?) Windows, this is a directory symbolic link to
|
|
|
|
// C:\ProgramData. Symbolic links are rare on Windows and the user requires
|
|
|
|
// a special permission (by default granted to Administrative users) to
|
|
|
|
// create symbolic links.
|
|
|
|
ASSERT_FALSE(directory_exists(subdir(profiles_dir, "All Users")));
|
|
|
|
|
|
|
|
// On modern (English?) Windows, this is a directory junction to
|
|
|
|
// C:\Users\Default. Junctions are used throughout user profile directories
|
|
|
|
// for backwards compatibility and they don't require any special permissions
|
|
|
|
// to create.
|
|
|
|
ASSERT_FALSE(directory_exists(subdir(profiles_dir, "Default User")));
|
|
|
|
|
|
|
|
ASSERT_FALSE(directory_exists(subdir(profiles_dir, "does-not-exist")));
|
|
|
|
#else
|
2015-04-17 20:57:15 +00:00
|
|
|
ASSERT_TRUE(directory_exists("/proc"));
|
|
|
|
ASSERT_FALSE(directory_exists("/proc/self")); // Symbolic link.
|
|
|
|
ASSERT_FALSE(directory_exists("/proc/does-not-exist"));
|
2015-05-24 22:36:28 +00:00
|
|
|
#endif
|
2015-04-17 20:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(adb_utils, escape_arg) {
|
2015-04-18 03:50:11 +00:00
|
|
|
ASSERT_EQ(R"('')", escape_arg(""));
|
|
|
|
|
|
|
|
ASSERT_EQ(R"('abc')", escape_arg("abc"));
|
|
|
|
|
|
|
|
ASSERT_EQ(R"(' abc')", escape_arg(" abc"));
|
2015-05-15 19:06:00 +00:00
|
|
|
ASSERT_EQ(R"(''\''abc')", escape_arg("'abc"));
|
2015-04-18 03:50:11 +00:00
|
|
|
ASSERT_EQ(R"('"abc')", escape_arg("\"abc"));
|
|
|
|
ASSERT_EQ(R"('\abc')", escape_arg("\\abc"));
|
|
|
|
ASSERT_EQ(R"('(abc')", escape_arg("(abc"));
|
|
|
|
ASSERT_EQ(R"(')abc')", escape_arg(")abc"));
|
|
|
|
|
|
|
|
ASSERT_EQ(R"('abc abc')", escape_arg("abc abc"));
|
2015-05-15 19:06:00 +00:00
|
|
|
ASSERT_EQ(R"('abc'\''abc')", escape_arg("abc'abc"));
|
2015-04-18 03:50:11 +00:00
|
|
|
ASSERT_EQ(R"('abc"abc')", escape_arg("abc\"abc"));
|
|
|
|
ASSERT_EQ(R"('abc\abc')", escape_arg("abc\\abc"));
|
|
|
|
ASSERT_EQ(R"('abc(abc')", escape_arg("abc(abc"));
|
|
|
|
ASSERT_EQ(R"('abc)abc')", escape_arg("abc)abc"));
|
|
|
|
|
|
|
|
ASSERT_EQ(R"('abc ')", escape_arg("abc "));
|
2015-05-15 19:06:00 +00:00
|
|
|
ASSERT_EQ(R"('abc'\''')", escape_arg("abc'"));
|
2015-04-18 03:50:11 +00:00
|
|
|
ASSERT_EQ(R"('abc"')", escape_arg("abc\""));
|
|
|
|
ASSERT_EQ(R"('abc\')", escape_arg("abc\\"));
|
|
|
|
ASSERT_EQ(R"('abc(')", escape_arg("abc("));
|
|
|
|
ASSERT_EQ(R"('abc)')", escape_arg("abc)"));
|
2015-04-17 20:57:15 +00:00
|
|
|
}
|
2015-07-18 19:21:30 +00:00
|
|
|
|
2015-07-31 00:42:01 +00:00
|
|
|
TEST(adb_utils, adb_basename) {
|
|
|
|
EXPECT_EQ("sh", adb_basename("/system/bin/sh"));
|
|
|
|
EXPECT_EQ("sh", adb_basename("sh"));
|
2015-11-04 02:52:35 +00:00
|
|
|
EXPECT_EQ("sh", adb_basename("/system/bin/sh/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(adb_utils, adb_dirname) {
|
|
|
|
EXPECT_EQ("/system/bin", adb_dirname("/system/bin/sh"));
|
|
|
|
EXPECT_EQ(".", adb_dirname("sh"));
|
|
|
|
EXPECT_EQ("/system/bin", adb_dirname("/system/bin/sh/"));
|
2015-07-31 00:42:01 +00:00
|
|
|
}
|
|
|
|
|
2015-08-02 00:29:23 +00:00
|
|
|
void test_mkdirs(const std::string basepath) {
|
2016-02-08 19:22:50 +00:00
|
|
|
EXPECT_TRUE(mkdirs(adb_dirname(basepath)));
|
2015-08-02 00:29:23 +00:00
|
|
|
EXPECT_NE(-1, adb_creat(basepath.c_str(), 0600));
|
|
|
|
EXPECT_FALSE(mkdirs(basepath + "/subdir/"));
|
|
|
|
}
|
|
|
|
|
2015-05-06 20:26:00 +00:00
|
|
|
TEST(adb_utils, mkdirs) {
|
|
|
|
TemporaryDir td;
|
2015-08-02 00:29:23 +00:00
|
|
|
|
|
|
|
// Absolute paths.
|
|
|
|
test_mkdirs(std::string(td.path) + "/dir/subdir/file");
|
|
|
|
|
|
|
|
// Relative paths.
|
|
|
|
ASSERT_EQ(0, chdir(td.path)) << strerror(errno);
|
|
|
|
test_mkdirs(std::string("relative/subrel/file"));
|
2015-05-06 20:26:00 +00:00
|
|
|
}
|
2015-10-06 22:10:05 +00:00
|
|
|
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
TEST(adb_utils, set_file_block_mode) {
|
|
|
|
int fd = adb_open("/dev/null", O_RDWR | O_APPEND);
|
|
|
|
ASSERT_GE(fd, 0);
|
|
|
|
int flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
ASSERT_EQ(O_RDWR | O_APPEND, (flags & (O_RDWR | O_APPEND)));
|
|
|
|
ASSERT_TRUE(set_file_block_mode(fd, false));
|
|
|
|
int new_flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
ASSERT_EQ(flags | O_NONBLOCK, new_flags);
|
|
|
|
ASSERT_TRUE(set_file_block_mode(fd, true));
|
|
|
|
new_flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
ASSERT_EQ(flags, new_flags);
|
|
|
|
ASSERT_EQ(0, adb_close(fd));
|
|
|
|
}
|
|
|
|
#endif
|