2015-02-03 09:31:27 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 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.
|
|
|
|
*/
|
|
|
|
|
2015-12-05 14:00:26 +08:00
|
|
|
#include "android-base/file.h"
|
2015-03-17 01:08:46 +08:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2015-02-03 09:31:27 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-02-07 04:19:48 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2015-03-17 01:08:46 +08:00
|
|
|
|
|
|
|
#include <string>
|
2015-02-03 09:31:27 +08:00
|
|
|
|
2015-12-05 14:00:26 +08:00
|
|
|
#include "android-base/test_utils.h"
|
2015-02-07 04:19:48 +08:00
|
|
|
|
2015-02-03 09:31:27 +08:00
|
|
|
TEST(file, ReadFileToString_ENOENT) {
|
|
|
|
std::string s("hello");
|
|
|
|
errno = 0;
|
2015-03-17 01:08:46 +08:00
|
|
|
ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
|
2015-02-03 09:31:27 +08:00
|
|
|
EXPECT_EQ(ENOENT, errno);
|
2015-03-17 01:08:46 +08:00
|
|
|
EXPECT_EQ("", s); // s was cleared.
|
2015-02-03 09:31:27 +08:00
|
|
|
}
|
|
|
|
|
2015-05-25 06:36:28 +08:00
|
|
|
TEST(file, ReadFileToString_WriteStringToFile) {
|
2015-02-07 04:19:48 +08:00
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2015-05-07 04:26:00 +08:00
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path))
|
2015-04-30 08:09:53 +08:00
|
|
|
<< strerror(errno);
|
2015-02-07 04:19:48 +08:00
|
|
|
std::string s;
|
2015-05-07 04:26:00 +08:00
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
|
2015-04-30 08:09:53 +08:00
|
|
|
<< strerror(errno);
|
2015-02-07 04:19:48 +08:00
|
|
|
EXPECT_EQ("abc", s);
|
|
|
|
}
|
|
|
|
|
2016-09-15 07:11:45 +08:00
|
|
|
// symlinks require elevated privileges on Windows.
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
TEST(file, ReadFileToString_WriteStringToFile_symlink) {
|
|
|
|
TemporaryFile target, link;
|
|
|
|
ASSERT_EQ(0, unlink(link.path));
|
|
|
|
ASSERT_EQ(0, symlink(target.path, link.path));
|
|
|
|
ASSERT_FALSE(android::base::WriteStringToFile("foo", link.path, false));
|
|
|
|
ASSERT_EQ(ELOOP, errno);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile("foo", link.path, true));
|
|
|
|
|
|
|
|
std::string s;
|
|
|
|
ASSERT_FALSE(android::base::ReadFileToString(link.path, &s));
|
|
|
|
ASSERT_EQ(ELOOP, errno);
|
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(link.path, &s, true));
|
|
|
|
ASSERT_EQ("foo", s);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-30 02:32:23 +08:00
|
|
|
// WriteStringToFile2 is explicitly for setting Unix permissions, which make no
|
|
|
|
// sense on Windows.
|
|
|
|
#if !defined(_WIN32)
|
2015-02-18 02:16:04 +08:00
|
|
|
TEST(file, WriteStringToFile2) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2015-05-07 04:26:00 +08:00
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660,
|
2015-03-17 01:08:46 +08:00
|
|
|
getuid(), getgid()))
|
2015-04-30 08:09:53 +08:00
|
|
|
<< strerror(errno);
|
2015-02-18 02:16:04 +08:00
|
|
|
struct stat sb;
|
2015-05-07 04:26:00 +08:00
|
|
|
ASSERT_EQ(0, stat(tf.path, &sb));
|
2015-05-01 06:12:21 +08:00
|
|
|
ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
|
2015-02-18 02:16:04 +08:00
|
|
|
ASSERT_EQ(getuid(), sb.st_uid);
|
|
|
|
ASSERT_EQ(getgid(), sb.st_gid);
|
|
|
|
std::string s;
|
2015-05-07 04:26:00 +08:00
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
|
2015-04-30 08:09:53 +08:00
|
|
|
<< strerror(errno);
|
2015-02-18 02:16:04 +08:00
|
|
|
EXPECT_EQ("abc", s);
|
|
|
|
}
|
2015-04-30 02:32:23 +08:00
|
|
|
#endif
|
2015-02-18 02:16:04 +08:00
|
|
|
|
2015-02-07 04:19:48 +08:00
|
|
|
TEST(file, WriteStringToFd) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2015-03-17 01:08:46 +08:00
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
|
2015-02-07 04:19:48 +08:00
|
|
|
|
2015-04-30 08:09:53 +08:00
|
|
|
ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
|
2015-02-07 04:19:48 +08:00
|
|
|
|
|
|
|
std::string s;
|
2015-04-30 08:09:53 +08:00
|
|
|
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
|
2015-02-07 04:19:48 +08:00
|
|
|
EXPECT_EQ("abc", s);
|
|
|
|
}
|
2015-04-25 12:57:16 +08:00
|
|
|
|
|
|
|
TEST(file, WriteFully) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
|
2015-08-04 11:43:24 +08:00
|
|
|
|
|
|
|
ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
|
|
|
|
|
2015-04-25 12:57:16 +08:00
|
|
|
std::string s;
|
2015-08-04 11:43:24 +08:00
|
|
|
s.resize(3);
|
|
|
|
ASSERT_TRUE(android::base::ReadFully(tf.fd, &s[0], s.size()))
|
2015-04-30 08:09:53 +08:00
|
|
|
<< strerror(errno);
|
2015-04-25 12:57:16 +08:00
|
|
|
EXPECT_EQ("abc", s);
|
2015-08-04 11:43:24 +08:00
|
|
|
|
|
|
|
ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
|
|
|
|
|
|
|
|
s.resize(1024);
|
|
|
|
ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size()));
|
2015-04-25 12:57:16 +08:00
|
|
|
}
|
2016-01-30 09:25:54 +08:00
|
|
|
|
|
|
|
TEST(file, RemoveFileIfExist) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
|
|
|
close(tf.fd);
|
|
|
|
tf.fd = -1;
|
|
|
|
std::string err;
|
|
|
|
ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err)) << err;
|
|
|
|
ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path));
|
|
|
|
TemporaryDir td;
|
|
|
|
ASSERT_FALSE(android::base::RemoveFileIfExists(td.path));
|
|
|
|
ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err));
|
|
|
|
ASSERT_EQ("is not a regular or symbol link file", err);
|
|
|
|
}
|
2016-08-24 06:53:45 +08:00
|
|
|
|
|
|
|
TEST(file, Readlink) {
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
// Linux doesn't allow empty symbolic links.
|
|
|
|
std::string min("x");
|
|
|
|
// ext2 and ext4 both have PAGE_SIZE limits.
|
2017-01-12 09:34:40 +08:00
|
|
|
// If file encryption is enabled, there's extra overhead to store the
|
|
|
|
// size of the encrypted symlink target. There's also an off-by-one
|
|
|
|
// in current kernels (and marlin/sailfish where we're seeing this
|
|
|
|
// failure are still on 3.18, far from current). http://b/33306057.
|
|
|
|
std::string max(static_cast<size_t>(4096 - 2 - 1 - 1), 'x');
|
2016-08-24 06:53:45 +08:00
|
|
|
|
|
|
|
TemporaryDir td;
|
|
|
|
std::string min_path{std::string(td.path) + "/" + "min"};
|
|
|
|
std::string max_path{std::string(td.path) + "/" + "max"};
|
|
|
|
|
|
|
|
ASSERT_EQ(0, symlink(min.c_str(), min_path.c_str()));
|
|
|
|
ASSERT_EQ(0, symlink(max.c_str(), max_path.c_str()));
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
result = "wrong";
|
|
|
|
ASSERT_TRUE(android::base::Readlink(min_path, &result));
|
|
|
|
ASSERT_EQ(min, result);
|
|
|
|
|
|
|
|
result = "wrong";
|
|
|
|
ASSERT_TRUE(android::base::Readlink(max_path, &result));
|
|
|
|
ASSERT_EQ(max, result);
|
|
|
|
#endif
|
|
|
|
}
|
2016-09-01 06:07:18 +08:00
|
|
|
|
2016-09-10 01:49:21 +08:00
|
|
|
TEST(file, Realpath) {
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
TemporaryDir td;
|
|
|
|
std::string basename = android::base::Basename(td.path);
|
|
|
|
std::string dir_name = android::base::Dirname(td.path);
|
|
|
|
std::string base_dir_name = android::base::Basename(dir_name);
|
|
|
|
|
|
|
|
{
|
|
|
|
std::string path = dir_name + "/../" + base_dir_name + "/" + basename;
|
|
|
|
std::string result;
|
|
|
|
ASSERT_TRUE(android::base::Realpath(path, &result));
|
|
|
|
ASSERT_EQ(td.path, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::string path = std::string(td.path) + "/..";
|
|
|
|
std::string result;
|
|
|
|
ASSERT_TRUE(android::base::Realpath(path, &result));
|
|
|
|
ASSERT_EQ(dir_name, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
std::string path = std::string(td.path) + "/foo.noent";
|
|
|
|
std::string result = "wrong";
|
|
|
|
ASSERT_TRUE(!android::base::Realpath(path, &result));
|
|
|
|
ASSERT_TRUE(result.empty());
|
|
|
|
ASSERT_EQ(ENOENT, errno);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-02-24 09:41:56 +08:00
|
|
|
TEST(file, GetExecutableDirectory) {
|
|
|
|
std::string path = android::base::GetExecutableDirectory();
|
|
|
|
ASSERT_NE("", path);
|
|
|
|
ASSERT_NE(android::base::GetExecutablePath(), path);
|
|
|
|
ASSERT_EQ('/', path[0]);
|
|
|
|
ASSERT_NE('/', path[path.size() - 1]);
|
|
|
|
}
|
|
|
|
|
2016-09-01 06:07:18 +08:00
|
|
|
TEST(file, GetExecutablePath) {
|
|
|
|
ASSERT_NE("", android::base::GetExecutablePath());
|
|
|
|
}
|
2017-02-24 13:23:05 +08:00
|
|
|
|
|
|
|
TEST(file, Basename) {
|
|
|
|
EXPECT_EQ("sh", android::base::Basename("/system/bin/sh"));
|
|
|
|
EXPECT_EQ("sh", android::base::Basename("sh"));
|
|
|
|
EXPECT_EQ("sh", android::base::Basename("/system/bin/sh/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(file, Dirname) {
|
|
|
|
EXPECT_EQ("/system/bin", android::base::Dirname("/system/bin/sh"));
|
|
|
|
EXPECT_EQ(".", android::base::Dirname("sh"));
|
|
|
|
EXPECT_EQ("/system/bin", android::base::Dirname("/system/bin/sh/"));
|
|
|
|
}
|
2017-03-21 10:16:18 +08:00
|
|
|
|
|
|
|
TEST(file, ReadFileToString_capacity) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
|
|
|
|
|
|
|
// For a huge file, the overhead should still be small.
|
|
|
|
std::string s;
|
|
|
|
size_t size = 16 * 1024 * 1024;
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
|
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
|
|
|
|
EXPECT_EQ(size, s.size());
|
|
|
|
EXPECT_LT(s.capacity(), size + 16);
|
|
|
|
|
|
|
|
// Even for weird badly-aligned sizes.
|
|
|
|
size += 12345;
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
|
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
|
|
|
|
EXPECT_EQ(size, s.size());
|
|
|
|
EXPECT_LT(s.capacity(), size + 16);
|
|
|
|
|
|
|
|
// We'll shrink an enormous string if you read a small file into it.
|
|
|
|
size = 64;
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
|
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
|
|
|
|
EXPECT_EQ(size, s.size());
|
|
|
|
EXPECT_LT(s.capacity(), size + 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(file, ReadFileToString_capacity_0) {
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
|
|
|
|
|
|
|
// Because /proc reports its files as zero-length, we don't actually trust
|
|
|
|
// any file that claims to be zero-length. Rather than add increasingly
|
|
|
|
// complex heuristics for shrinking the passed-in string in that case, we
|
|
|
|
// currently leave it alone.
|
|
|
|
std::string s;
|
|
|
|
size_t initial_capacity = s.capacity();
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFile("", tf.path));
|
|
|
|
ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
|
|
|
|
EXPECT_EQ(0U, s.size());
|
|
|
|
EXPECT_EQ(initial_capacity, s.capacity());
|
|
|
|
}
|