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 "util.h"
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22
23 #include <android-base/file.h>
24 #include <android-base/stringprintf.h>
25 #include <gtest/gtest.h>
26
27 using namespace std::literals::string_literals;
28
29 namespace android {
30 namespace init {
31
TEST(util,ReadFile_ENOENT)32 TEST(util, ReadFile_ENOENT) {
33 errno = 0;
34 auto file_contents = ReadFile("/proc/does-not-exist");
35 EXPECT_EQ(ENOENT, errno);
36 ASSERT_FALSE(file_contents.ok());
37 EXPECT_EQ("open() failed: No such file or directory", file_contents.error().message());
38 }
39
TEST(util,ReadFileGroupWriteable)40 TEST(util, ReadFileGroupWriteable) {
41 std::string s("hello");
42 TemporaryFile tf;
43 ASSERT_TRUE(tf.fd != -1);
44 EXPECT_RESULT_OK(WriteFile(tf.path, s));
45 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
46 auto file_contents = ReadFile(tf.path);
47 ASSERT_FALSE(file_contents.ok()) << strerror(errno);
48 EXPECT_EQ("Skipping insecure file", file_contents.error().message());
49 }
50
TEST(util,ReadFileWorldWiteable)51 TEST(util, ReadFileWorldWiteable) {
52 std::string s("hello");
53 TemporaryFile tf;
54 ASSERT_TRUE(tf.fd != -1);
55 EXPECT_RESULT_OK(WriteFile(tf.path, s));
56 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
57 auto file_contents = ReadFile(tf.path);
58 ASSERT_FALSE(file_contents.ok());
59 EXPECT_EQ("Skipping insecure file", file_contents.error().message());
60 }
61
TEST(util,ReadFileSymbolicLink)62 TEST(util, ReadFileSymbolicLink) {
63 errno = 0;
64 // lrwxrwxrwx 1 root shell 6 2020-06-26 09:55 /system/bin/ps -> toybox
65 auto file_contents = ReadFile("/system/bin/ps");
66
67 EXPECT_EQ(ELOOP, errno);
68 ASSERT_FALSE(file_contents.ok());
69 EXPECT_EQ("open() failed: Too many symbolic links encountered",
70 file_contents.error().message());
71 }
72
TEST(util,ReadFileSuccess)73 TEST(util, ReadFileSuccess) {
74 auto file_contents = ReadFile("/proc/version");
75 ASSERT_TRUE(file_contents.ok());
76 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());
80 }
81
TEST(util,WriteFileBinary)82 TEST(util, WriteFileBinary) {
83 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);
91 EXPECT_RESULT_OK(WriteFile(tf.path, contents));
92
93 auto read_back_contents = ReadFile(tf.path);
94 ASSERT_RESULT_OK(read_back_contents);
95 EXPECT_EQ(contents, *read_back_contents);
96 EXPECT_EQ(10u, read_back_contents->size());
97 }
98
TEST(util,WriteFileNotExist)99 TEST(util, WriteFileNotExist) {
100 std::string s("hello");
101 TemporaryDir test_dir;
102 std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
103 EXPECT_RESULT_OK(WriteFile(path, s));
104 auto file_contents = ReadFile(path);
105 ASSERT_RESULT_OK(file_contents);
106 EXPECT_EQ(s, *file_contents);
107 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));
111 EXPECT_EQ(0, close(fd));
112 EXPECT_EQ((const unsigned int)(S_IRUSR | S_IWUSR), sb.st_mode & 0777);
113 EXPECT_EQ(0, unlink(path.c_str()));
114 }
115
TEST(util,WriteFileExist)116 TEST(util, WriteFileExist) {
117 TemporaryFile tf;
118 ASSERT_TRUE(tf.fd != -1);
119 EXPECT_RESULT_OK(WriteFile(tf.path, "1hello1"));
120 auto file_contents = ReadFile(tf.path);
121 ASSERT_RESULT_OK(file_contents);
122 EXPECT_EQ("1hello1", *file_contents);
123 EXPECT_RESULT_OK(WriteFile(tf.path, "2ll2"));
124 file_contents = ReadFile(tf.path);
125 ASSERT_RESULT_OK(file_contents);
126 EXPECT_EQ("2ll2", *file_contents);
127 }
128
TEST(util,DecodeUid)129 TEST(util, DecodeUid) {
130 auto decoded_uid = DecodeUid("root");
131 EXPECT_TRUE(decoded_uid.ok());
132 EXPECT_EQ(0U, *decoded_uid);
133
134 decoded_uid = DecodeUid("toot");
135 EXPECT_FALSE(decoded_uid.ok());
136 EXPECT_EQ("getpwnam failed: No such file or directory", decoded_uid.error().message());
137
138 decoded_uid = DecodeUid("123");
139 EXPECT_RESULT_OK(decoded_uid);
140 EXPECT_EQ(123U, *decoded_uid);
141 }
142
TEST(util,is_dir)143 TEST(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
TEST(util,mkdir_recursive)150 TEST(util, mkdir_recursive) {
151 TemporaryDir test_dir;
152 std::string path = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
153 EXPECT_TRUE(mkdir_recursive(path, 0755));
154 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
TEST(util,mkdir_recursive_extra_slashes)162 TEST(util, mkdir_recursive_extra_slashes) {
163 TemporaryDir test_dir;
164 std::string path = android::base::StringPrintf("%s/three////directories/deep//", test_dir.path);
165 EXPECT_TRUE(mkdir_recursive(path, 0755));
166 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 }
173
174 } // namespace init
175 } // namespace android
176