blob: 08343dcb5bae29577ab8c0fd79d5ab63610980d5 [file] [log] [blame]
Elliott Hughesf682b472015-02-06 12:19:48 -08001/*
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 "util.h"
18
19#include <errno.h>
Yongqin Liudbe88e72016-12-28 16:06:19 +080020#include <fcntl.h>
21#include <sys/stat.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070022
Mark Salyzyn9f1cf252018-11-12 12:45:59 -080023#include <android-base/file.h>
Yongqin Liudbe88e72016-12-28 16:06:19 +080024#include <android-base/stringprintf.h>
Elliott Hughesf682b472015-02-06 12:19:48 -080025#include <gtest/gtest.h>
26
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070027using namespace std::literals::string_literals;
28
Tom Cherry81f5d3e2017-06-22 12:53:17 -070029namespace android {
30namespace init {
31
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070032TEST(util, ReadFile_ENOENT) {
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070033 errno = 0;
Tom Cherry11a3aee2017-08-03 12:54:07 -070034 auto file_contents = ReadFile("/proc/does-not-exist");
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070035 EXPECT_EQ(ENOENT, errno);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090036 ASSERT_FALSE(file_contents.ok());
Jiyong Park8fd64c82019-05-31 03:43:34 +090037 EXPECT_EQ("open() failed: No such file or directory", file_contents.error().message());
Elliott Hughesf682b472015-02-06 12:19:48 -080038}
39
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070040TEST(util, ReadFileGroupWriteable) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080041 std::string s("hello");
42 TemporaryFile tf;
43 ASSERT_TRUE(tf.fd != -1);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090044 EXPECT_RESULT_OK(WriteFile(tf.path, s));
Yongqin Liudbe88e72016-12-28 16:06:19 +080045 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
Tom Cherry11a3aee2017-08-03 12:54:07 -070046 auto file_contents = ReadFile(tf.path);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090047 ASSERT_FALSE(file_contents.ok()) << strerror(errno);
Jiyong Park8fd64c82019-05-31 03:43:34 +090048 EXPECT_EQ("Skipping insecure file", file_contents.error().message());
Yongqin Liudbe88e72016-12-28 16:06:19 +080049}
50
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070051TEST(util, ReadFileWorldWiteable) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080052 std::string s("hello");
53 TemporaryFile tf;
54 ASSERT_TRUE(tf.fd != -1);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090055 EXPECT_RESULT_OK(WriteFile(tf.path, s));
Yongqin Liudbe88e72016-12-28 16:06:19 +080056 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
Tom Cherry11a3aee2017-08-03 12:54:07 -070057 auto file_contents = ReadFile(tf.path);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090058 ASSERT_FALSE(file_contents.ok());
Jiyong Park8fd64c82019-05-31 03:43:34 +090059 EXPECT_EQ("Skipping insecure file", file_contents.error().message());
Yongqin Liudbe88e72016-12-28 16:06:19 +080060}
61
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070062TEST(util, ReadFileSymbolicLink) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080063 errno = 0;
Boleyn Su9285b492020-06-26 18:08:07 +090064 // lrwxrwxrwx 1 root shell 6 2020-06-26 09:55 /system/bin/ps -> toybox
65 auto file_contents = ReadFile("/system/bin/ps");
66
Yongqin Liudbe88e72016-12-28 16:06:19 +080067 EXPECT_EQ(ELOOP, errno);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090068 ASSERT_FALSE(file_contents.ok());
Tom Cherry9949ec52019-05-16 16:54:49 -070069 EXPECT_EQ("open() failed: Too many symbolic links encountered",
Jiyong Park8fd64c82019-05-31 03:43:34 +090070 file_contents.error().message());
Yongqin Liudbe88e72016-12-28 16:06:19 +080071}
72
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070073TEST(util, ReadFileSuccess) {
Tom Cherry11a3aee2017-08-03 12:54:07 -070074 auto file_contents = ReadFile("/proc/version");
Bernie Innocenticecebbb2020-02-06 03:49:33 +090075 ASSERT_TRUE(file_contents.ok());
Tom Cherry11a3aee2017-08-03 12:54:07 -070076 EXPECT_GT(file_contents->length(), 6U);
77 EXPECT_EQ('\n', file_contents->at(file_contents->length() - 1));
78 (*file_contents)[5] = 0;
79 EXPECT_STREQ("Linux", file_contents->c_str());
Elliott Hughesf682b472015-02-06 12:19:48 -080080}
Elliott Hughes8d82ea02015-02-06 20:15:18 -080081
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070082TEST(util, WriteFileBinary) {
Tom Cherry53089aa2017-03-31 15:47:33 -070083 std::string contents("abcd");
84 contents.push_back('\0');
85 contents.push_back('\0');
86 contents.append("dcba");
87 ASSERT_EQ(10u, contents.size());
88
89 TemporaryFile tf;
90 ASSERT_TRUE(tf.fd != -1);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090091 EXPECT_RESULT_OK(WriteFile(tf.path, contents));
Tom Cherry53089aa2017-03-31 15:47:33 -070092
Tom Cherry11a3aee2017-08-03 12:54:07 -070093 auto read_back_contents = ReadFile(tf.path);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090094 ASSERT_RESULT_OK(read_back_contents);
Tom Cherry11a3aee2017-08-03 12:54:07 -070095 EXPECT_EQ(contents, *read_back_contents);
96 EXPECT_EQ(10u, read_back_contents->size());
Tom Cherry53089aa2017-03-31 15:47:33 -070097}
98
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070099TEST(util, WriteFileNotExist) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800100 std::string s("hello");
Yongqin Liudbe88e72016-12-28 16:06:19 +0800101 TemporaryDir test_dir;
102 std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900103 EXPECT_RESULT_OK(WriteFile(path, s));
Tom Cherry11a3aee2017-08-03 12:54:07 -0700104 auto file_contents = ReadFile(path);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900105 ASSERT_RESULT_OK(file_contents);
Tom Cherry11a3aee2017-08-03 12:54:07 -0700106 EXPECT_EQ(s, *file_contents);
Yongqin Liudbe88e72016-12-28 16:06:19 +0800107 struct stat sb;
108 int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
109 EXPECT_NE(-1, fd);
110 EXPECT_EQ(0, fstat(fd, &sb));
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900111 EXPECT_EQ(0, close(fd));
Yongqin Liudbe88e72016-12-28 16:06:19 +0800112 EXPECT_EQ((const unsigned int)(S_IRUSR | S_IWUSR), sb.st_mode & 0777);
113 EXPECT_EQ(0, unlink(path.c_str()));
114}
115
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700116TEST(util, WriteFileExist) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800117 TemporaryFile tf;
118 ASSERT_TRUE(tf.fd != -1);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900119 EXPECT_RESULT_OK(WriteFile(tf.path, "1hello1"));
Tom Cherry11a3aee2017-08-03 12:54:07 -0700120 auto file_contents = ReadFile(tf.path);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900121 ASSERT_RESULT_OK(file_contents);
Tom Cherry11a3aee2017-08-03 12:54:07 -0700122 EXPECT_EQ("1hello1", *file_contents);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900123 EXPECT_RESULT_OK(WriteFile(tf.path, "2ll2"));
Tom Cherry11a3aee2017-08-03 12:54:07 -0700124 file_contents = ReadFile(tf.path);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900125 ASSERT_RESULT_OK(file_contents);
Tom Cherry11a3aee2017-08-03 12:54:07 -0700126 EXPECT_EQ("2ll2", *file_contents);
Yongqin Liudbe88e72016-12-28 16:06:19 +0800127}
128
Tom Cherry517e1f12017-05-04 17:40:33 -0700129TEST(util, DecodeUid) {
Tom Cherry11a3aee2017-08-03 12:54:07 -0700130 auto decoded_uid = DecodeUid("root");
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900131 EXPECT_TRUE(decoded_uid.ok());
Tom Cherry11a3aee2017-08-03 12:54:07 -0700132 EXPECT_EQ(0U, *decoded_uid);
Tom Cherry517e1f12017-05-04 17:40:33 -0700133
Tom Cherry11a3aee2017-08-03 12:54:07 -0700134 decoded_uid = DecodeUid("toot");
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900135 EXPECT_FALSE(decoded_uid.ok());
Jiyong Park8fd64c82019-05-31 03:43:34 +0900136 EXPECT_EQ("getpwnam failed: No such file or directory", decoded_uid.error().message());
Tom Cherry517e1f12017-05-04 17:40:33 -0700137
Tom Cherry11a3aee2017-08-03 12:54:07 -0700138 decoded_uid = DecodeUid("123");
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900139 EXPECT_RESULT_OK(decoded_uid);
Tom Cherry11a3aee2017-08-03 12:54:07 -0700140 EXPECT_EQ(123U, *decoded_uid);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800141}
Tom Cherry060b74b2017-04-12 14:27:51 -0700142
143TEST(util, is_dir) {
144 TemporaryDir test_dir;
145 EXPECT_TRUE(is_dir(test_dir.path));
146 TemporaryFile tf;
147 EXPECT_FALSE(is_dir(tf.path));
148}
149
Tom Cherry060b74b2017-04-12 14:27:51 -0700150TEST(util, mkdir_recursive) {
151 TemporaryDir test_dir;
152 std::string path = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700153 EXPECT_TRUE(mkdir_recursive(path, 0755));
Tom Cherry060b74b2017-04-12 14:27:51 -0700154 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
155 EXPECT_TRUE(is_dir(path1.c_str()));
156 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
157 EXPECT_TRUE(is_dir(path1.c_str()));
158 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
159 EXPECT_TRUE(is_dir(path1.c_str()));
160}
161
162TEST(util, mkdir_recursive_extra_slashes) {
163 TemporaryDir test_dir;
164 std::string path = android::base::StringPrintf("%s/three////directories/deep//", test_dir.path);
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700165 EXPECT_TRUE(mkdir_recursive(path, 0755));
Tom Cherry060b74b2017-04-12 14:27:51 -0700166 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
167 EXPECT_TRUE(is_dir(path1.c_str()));
168 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
169 EXPECT_TRUE(is_dir(path1.c_str()));
170 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
171 EXPECT_TRUE(is_dir(path1.c_str()));
172}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700173
174} // namespace init
175} // namespace android