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 // lrwxr-xr-x 1 root shell 6 2009-01-01 09:00 /system/bin/ps -> toybox
65 auto file_contents = ReadFile("/system/bin/ps");
66 EXPECT_EQ(ELOOP, errno);
67 ASSERT_FALSE(file_contents.ok());
68 EXPECT_EQ("open() failed: Too many symbolic links encountered",
69 file_contents.error().message());
70 }
71
TEST(util,ReadFileSuccess)72 TEST(util, ReadFileSuccess) {
73 auto file_contents = ReadFile("/proc/version");
74 ASSERT_TRUE(file_contents.ok());
75 EXPECT_GT(file_contents->length(), 6U);
76 EXPECT_EQ('\n', file_contents->at(file_contents->length() - 1));
77 (*file_contents)[5] = 0;
78 EXPECT_STREQ("Linux", file_contents->c_str());
79 }
80
TEST(util,WriteFileBinary)81 TEST(util, WriteFileBinary) {
82 std::string contents("abcd");
83 contents.push_back('\0');
84 contents.push_back('\0');
85 contents.append("dcba");
86 ASSERT_EQ(10u, contents.size());
87
88 TemporaryFile tf;
89 ASSERT_TRUE(tf.fd != -1);
90 EXPECT_RESULT_OK(WriteFile(tf.path, contents));
91
92 auto read_back_contents = ReadFile(tf.path);
93 ASSERT_RESULT_OK(read_back_contents);
94 EXPECT_EQ(contents, *read_back_contents);
95 EXPECT_EQ(10u, read_back_contents->size());
96 }
97
TEST(util,WriteFileNotExist)98 TEST(util, WriteFileNotExist) {
99 std::string s("hello");
100 TemporaryDir test_dir;
101 std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
102 EXPECT_RESULT_OK(WriteFile(path, s));
103 auto file_contents = ReadFile(path);
104 ASSERT_RESULT_OK(file_contents);
105 EXPECT_EQ(s, *file_contents);
106 struct stat sb;
107 int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
108 EXPECT_NE(-1, fd);
109 EXPECT_EQ(0, fstat(fd, &sb));
110 EXPECT_EQ(0, close(fd));
111 EXPECT_EQ((const unsigned int)(S_IRUSR | S_IWUSR), sb.st_mode & 0777);
112 EXPECT_EQ(0, unlink(path.c_str()));
113 }
114
TEST(util,WriteFileExist)115 TEST(util, WriteFileExist) {
116 TemporaryFile tf;
117 ASSERT_TRUE(tf.fd != -1);
118 EXPECT_RESULT_OK(WriteFile(tf.path, "1hello1"));
119 auto file_contents = ReadFile(tf.path);
120 ASSERT_RESULT_OK(file_contents);
121 EXPECT_EQ("1hello1", *file_contents);
122 EXPECT_RESULT_OK(WriteFile(tf.path, "2ll2"));
123 file_contents = ReadFile(tf.path);
124 ASSERT_RESULT_OK(file_contents);
125 EXPECT_EQ("2ll2", *file_contents);
126 }
127
TEST(util,DecodeUid)128 TEST(util, DecodeUid) {
129 auto decoded_uid = DecodeUid("root");
130 EXPECT_TRUE(decoded_uid.ok());
131 EXPECT_EQ(0U, *decoded_uid);
132
133 decoded_uid = DecodeUid("toot");
134 EXPECT_FALSE(decoded_uid.ok());
135 EXPECT_EQ("getpwnam failed: No such file or directory", decoded_uid.error().message());
136
137 decoded_uid = DecodeUid("123");
138 EXPECT_RESULT_OK(decoded_uid);
139 EXPECT_EQ(123U, *decoded_uid);
140 }
141
TEST(util,is_dir)142 TEST(util, is_dir) {
143 TemporaryDir test_dir;
144 EXPECT_TRUE(is_dir(test_dir.path));
145 TemporaryFile tf;
146 EXPECT_FALSE(is_dir(tf.path));
147 }
148
TEST(util,mkdir_recursive)149 TEST(util, mkdir_recursive) {
150 TemporaryDir test_dir;
151 std::string path = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
152 EXPECT_TRUE(mkdir_recursive(path, 0755));
153 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
154 EXPECT_TRUE(is_dir(path1.c_str()));
155 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
156 EXPECT_TRUE(is_dir(path1.c_str()));
157 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
158 EXPECT_TRUE(is_dir(path1.c_str()));
159 }
160
TEST(util,mkdir_recursive_extra_slashes)161 TEST(util, mkdir_recursive_extra_slashes) {
162 TemporaryDir test_dir;
163 std::string path = android::base::StringPrintf("%s/three////directories/deep//", test_dir.path);
164 EXPECT_TRUE(mkdir_recursive(path, 0755));
165 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
166 EXPECT_TRUE(is_dir(path1.c_str()));
167 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
168 EXPECT_TRUE(is_dir(path1.c_str()));
169 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
170 EXPECT_TRUE(is_dir(path1.c_str()));
171 }
172
173 } // namespace init
174 } // namespace android
175