2015-02-06 21:19:48 +01: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 "util.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
2016-12-28 09:06:19 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2016-10-27 16:45:34 +02:00
|
|
|
|
2018-11-12 21:45:59 +01:00
|
|
|
#include <android-base/file.h>
|
2016-12-28 09:06:19 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-02-06 21:19:48 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2017-05-05 03:17:33 +02:00
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2017-05-05 03:17:33 +02:00
|
|
|
TEST(util, ReadFile_ENOENT) {
|
|
|
|
errno = 0;
|
2017-08-03 21:54:07 +02:00
|
|
|
auto file_contents = ReadFile("/proc/does-not-exist");
|
2017-05-05 03:17:33 +02:00
|
|
|
EXPECT_EQ(ENOENT, errno);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_FALSE(file_contents.ok());
|
2019-05-30 20:43:34 +02:00
|
|
|
EXPECT_EQ("open() failed: No such file or directory", file_contents.error().message());
|
2015-02-06 21:19:48 +01:00
|
|
|
}
|
|
|
|
|
2017-05-05 03:17:33 +02:00
|
|
|
TEST(util, ReadFileGroupWriteable) {
|
2016-12-28 09:06:19 +01:00
|
|
|
std::string s("hello");
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2020-02-05 19:49:33 +01:00
|
|
|
EXPECT_RESULT_OK(WriteFile(tf.path, s));
|
2016-12-28 09:06:19 +01:00
|
|
|
EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
|
2017-08-03 21:54:07 +02:00
|
|
|
auto file_contents = ReadFile(tf.path);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_FALSE(file_contents.ok()) << strerror(errno);
|
2019-05-30 20:43:34 +02:00
|
|
|
EXPECT_EQ("Skipping insecure file", file_contents.error().message());
|
2016-12-28 09:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-05-05 03:17:33 +02:00
|
|
|
TEST(util, ReadFileWorldWiteable) {
|
2016-12-28 09:06:19 +01:00
|
|
|
std::string s("hello");
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2020-02-05 19:49:33 +01:00
|
|
|
EXPECT_RESULT_OK(WriteFile(tf.path, s));
|
2016-12-28 09:06:19 +01:00
|
|
|
EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
|
2017-08-03 21:54:07 +02:00
|
|
|
auto file_contents = ReadFile(tf.path);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_FALSE(file_contents.ok());
|
2019-05-30 20:43:34 +02:00
|
|
|
EXPECT_EQ("Skipping insecure file", file_contents.error().message());
|
2016-12-28 09:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-05-05 03:17:33 +02:00
|
|
|
TEST(util, ReadFileSymbolicLink) {
|
2016-12-28 09:06:19 +01:00
|
|
|
errno = 0;
|
2020-05-11 09:47:05 +02:00
|
|
|
// lrwxr-xr-x 1 root shell 6 2009-01-01 09:00 /system/bin/ps -> toybox
|
|
|
|
auto file_contents = ReadFile("/system/bin/ps");
|
2016-12-28 09:06:19 +01:00
|
|
|
EXPECT_EQ(ELOOP, errno);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_FALSE(file_contents.ok());
|
2019-05-17 01:54:49 +02:00
|
|
|
EXPECT_EQ("open() failed: Too many symbolic links encountered",
|
2019-05-30 20:43:34 +02:00
|
|
|
file_contents.error().message());
|
2016-12-28 09:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-05-05 03:17:33 +02:00
|
|
|
TEST(util, ReadFileSuccess) {
|
2017-08-03 21:54:07 +02:00
|
|
|
auto file_contents = ReadFile("/proc/version");
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_TRUE(file_contents.ok());
|
2017-08-03 21:54:07 +02:00
|
|
|
EXPECT_GT(file_contents->length(), 6U);
|
|
|
|
EXPECT_EQ('\n', file_contents->at(file_contents->length() - 1));
|
|
|
|
(*file_contents)[5] = 0;
|
|
|
|
EXPECT_STREQ("Linux", file_contents->c_str());
|
2015-02-06 21:19:48 +01:00
|
|
|
}
|
2015-02-07 05:15:18 +01:00
|
|
|
|
2017-05-05 03:17:33 +02:00
|
|
|
TEST(util, WriteFileBinary) {
|
2017-04-01 00:47:33 +02:00
|
|
|
std::string contents("abcd");
|
|
|
|
contents.push_back('\0');
|
|
|
|
contents.push_back('\0');
|
|
|
|
contents.append("dcba");
|
|
|
|
ASSERT_EQ(10u, contents.size());
|
|
|
|
|
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2020-02-05 19:49:33 +01:00
|
|
|
EXPECT_RESULT_OK(WriteFile(tf.path, contents));
|
2017-08-03 21:54:07 +02:00
|
|
|
|
|
|
|
auto read_back_contents = ReadFile(tf.path);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_RESULT_OK(read_back_contents);
|
2017-08-03 21:54:07 +02:00
|
|
|
EXPECT_EQ(contents, *read_back_contents);
|
|
|
|
EXPECT_EQ(10u, read_back_contents->size());
|
2017-04-01 00:47:33 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 03:17:33 +02:00
|
|
|
TEST(util, WriteFileNotExist) {
|
2016-12-28 09:06:19 +01:00
|
|
|
std::string s("hello");
|
|
|
|
TemporaryDir test_dir;
|
|
|
|
std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
|
2020-02-05 19:49:33 +01:00
|
|
|
EXPECT_RESULT_OK(WriteFile(path, s));
|
2017-08-03 21:54:07 +02:00
|
|
|
auto file_contents = ReadFile(path);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_RESULT_OK(file_contents);
|
2017-08-03 21:54:07 +02:00
|
|
|
EXPECT_EQ(s, *file_contents);
|
2016-12-28 09:06:19 +01:00
|
|
|
struct stat sb;
|
|
|
|
int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
|
|
|
EXPECT_NE(-1, fd);
|
|
|
|
EXPECT_EQ(0, fstat(fd, &sb));
|
2020-02-05 19:49:33 +01:00
|
|
|
EXPECT_EQ(0, close(fd));
|
2016-12-28 09:06:19 +01:00
|
|
|
EXPECT_EQ((const unsigned int)(S_IRUSR | S_IWUSR), sb.st_mode & 0777);
|
|
|
|
EXPECT_EQ(0, unlink(path.c_str()));
|
|
|
|
}
|
|
|
|
|
2017-05-05 03:17:33 +02:00
|
|
|
TEST(util, WriteFileExist) {
|
2016-12-28 09:06:19 +01:00
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
2020-02-05 19:49:33 +01:00
|
|
|
EXPECT_RESULT_OK(WriteFile(tf.path, "1hello1"));
|
2017-08-03 21:54:07 +02:00
|
|
|
auto file_contents = ReadFile(tf.path);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_RESULT_OK(file_contents);
|
2017-08-03 21:54:07 +02:00
|
|
|
EXPECT_EQ("1hello1", *file_contents);
|
2020-02-05 19:49:33 +01:00
|
|
|
EXPECT_RESULT_OK(WriteFile(tf.path, "2ll2"));
|
2017-08-03 21:54:07 +02:00
|
|
|
file_contents = ReadFile(tf.path);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_RESULT_OK(file_contents);
|
2017-08-03 21:54:07 +02:00
|
|
|
EXPECT_EQ("2ll2", *file_contents);
|
2016-12-28 09:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-05-05 02:40:33 +02:00
|
|
|
TEST(util, DecodeUid) {
|
2017-08-03 21:54:07 +02:00
|
|
|
auto decoded_uid = DecodeUid("root");
|
2020-02-05 19:49:33 +01:00
|
|
|
EXPECT_TRUE(decoded_uid.ok());
|
2017-08-03 21:54:07 +02:00
|
|
|
EXPECT_EQ(0U, *decoded_uid);
|
2017-05-05 02:40:33 +02:00
|
|
|
|
2017-08-03 21:54:07 +02:00
|
|
|
decoded_uid = DecodeUid("toot");
|
2020-02-05 19:49:33 +01:00
|
|
|
EXPECT_FALSE(decoded_uid.ok());
|
2019-05-30 20:43:34 +02:00
|
|
|
EXPECT_EQ("getpwnam failed: No such file or directory", decoded_uid.error().message());
|
2017-05-05 02:40:33 +02:00
|
|
|
|
2017-08-03 21:54:07 +02:00
|
|
|
decoded_uid = DecodeUid("123");
|
2020-02-05 19:49:33 +01:00
|
|
|
EXPECT_RESULT_OK(decoded_uid);
|
2017-08-03 21:54:07 +02:00
|
|
|
EXPECT_EQ(123U, *decoded_uid);
|
2015-02-07 05:15:18 +01:00
|
|
|
}
|
2017-04-12 23:27:51 +02:00
|
|
|
|
|
|
|
TEST(util, is_dir) {
|
|
|
|
TemporaryDir test_dir;
|
|
|
|
EXPECT_TRUE(is_dir(test_dir.path));
|
|
|
|
TemporaryFile tf;
|
|
|
|
EXPECT_FALSE(is_dir(tf.path));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(util, mkdir_recursive) {
|
|
|
|
TemporaryDir test_dir;
|
|
|
|
std::string path = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
|
2017-08-10 21:22:44 +02:00
|
|
|
EXPECT_TRUE(mkdir_recursive(path, 0755));
|
2017-04-12 23:27:51 +02:00
|
|
|
std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
|
|
|
|
EXPECT_TRUE(is_dir(path1.c_str()));
|
|
|
|
std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
|
|
|
|
EXPECT_TRUE(is_dir(path1.c_str()));
|
|
|
|
std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
|
|
|
|
EXPECT_TRUE(is_dir(path1.c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(util, mkdir_recursive_extra_slashes) {
|
|
|
|
TemporaryDir test_dir;
|
|
|
|
std::string path = android::base::StringPrintf("%s/three////directories/deep//", test_dir.path);
|
2017-08-10 21:22:44 +02:00
|
|
|
EXPECT_TRUE(mkdir_recursive(path, 0755));
|
2017-04-12 23:27:51 +02:00
|
|
|
std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
|
|
|
|
EXPECT_TRUE(is_dir(path1.c_str()));
|
|
|
|
std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
|
|
|
|
EXPECT_TRUE(is_dir(path1.c_str()));
|
|
|
|
std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
|
|
|
|
EXPECT_TRUE(is_dir(path1.c_str()));
|
|
|
|
}
|
2017-06-22 21:53:17 +02:00
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|