blob: c16ab74317df9cbebe6d6d44850a7e988473c503 [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
Yongqin Liudbe88e72016-12-28 16:06:19 +080023#include <android-base/stringprintf.h>
Tom Cherry53089aa2017-03-31 15:47:33 -070024#include <android-base/test_utils.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) {
33 std::string s("hello");
34 std::string err;
35 errno = 0;
36 EXPECT_FALSE(ReadFile("/proc/does-not-exist", &s, &err));
37 EXPECT_EQ("Unable to open '/proc/does-not-exist': No such file or directory", err);
38 EXPECT_EQ(ENOENT, errno);
39 EXPECT_EQ("", s); // s was cleared.
Elliott Hughesf682b472015-02-06 12:19:48 -080040}
41
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070042TEST(util, ReadFileGroupWriteable) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080043 std::string s("hello");
44 TemporaryFile tf;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070045 std::string err;
Yongqin Liudbe88e72016-12-28 16:06:19 +080046 ASSERT_TRUE(tf.fd != -1);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070047 EXPECT_TRUE(WriteFile(tf.path, s, &err)) << strerror(errno);
48 EXPECT_EQ("", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +080049 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070050 EXPECT_FALSE(ReadFile(tf.path, &s, &err)) << strerror(errno);
51 EXPECT_EQ("Skipping insecure file '"s + tf.path + "'", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +080052 EXPECT_EQ("", s); // s was cleared.
53}
54
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070055TEST(util, ReadFileWorldWiteable) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080056 std::string s("hello");
57 TemporaryFile tf;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070058 std::string err;
Yongqin Liudbe88e72016-12-28 16:06:19 +080059 ASSERT_TRUE(tf.fd != -1);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070060 EXPECT_TRUE(WriteFile(tf.path, s, &err)) << strerror(errno);
61 EXPECT_EQ("", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +080062 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070063 EXPECT_FALSE(ReadFile(tf.path, &s, &err)) << strerror(errno);
64 EXPECT_EQ("Skipping insecure file '"s + tf.path + "'", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +080065 EXPECT_EQ("", s); // s was cleared.
66}
67
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070068TEST(util, ReadFileSymbolicLink) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080069 std::string s("hello");
70 errno = 0;
71 // lrwxrwxrwx 1 root root 13 1970-01-01 00:00 charger -> /sbin/healthd
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070072 std::string err;
73 EXPECT_FALSE(ReadFile("/charger", &s, &err));
74 EXPECT_EQ("Unable to open '/charger': Too many symbolic links encountered", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +080075 EXPECT_EQ(ELOOP, errno);
76 EXPECT_EQ("", s); // s was cleared.
77}
78
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070079TEST(util, ReadFileSuccess) {
80 std::string s("hello");
81 std::string err;
82 EXPECT_TRUE(ReadFile("/proc/version", &s, &err));
83 EXPECT_EQ("", err);
84 EXPECT_GT(s.length(), 6U);
85 EXPECT_EQ('\n', s[s.length() - 1]);
86 s[5] = 0;
87 EXPECT_STREQ("Linux", s.c_str());
Elliott Hughesf682b472015-02-06 12:19:48 -080088}
Elliott Hughes8d82ea02015-02-06 20:15:18 -080089
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070090TEST(util, WriteFileBinary) {
Tom Cherry53089aa2017-03-31 15:47:33 -070091 std::string contents("abcd");
92 contents.push_back('\0');
93 contents.push_back('\0');
94 contents.append("dcba");
95 ASSERT_EQ(10u, contents.size());
96
97 TemporaryFile tf;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070098 std::string err;
Tom Cherry53089aa2017-03-31 15:47:33 -070099 ASSERT_TRUE(tf.fd != -1);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700100 EXPECT_TRUE(WriteFile(tf.path, contents, &err)) << strerror(errno);
101 EXPECT_EQ("", err);
Tom Cherry53089aa2017-03-31 15:47:33 -0700102
103 std::string read_back_contents;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700104 EXPECT_TRUE(ReadFile(tf.path, &read_back_contents, &err)) << strerror(errno);
105 EXPECT_EQ("", err);
Tom Cherry53089aa2017-03-31 15:47:33 -0700106 EXPECT_EQ(contents, read_back_contents);
107 EXPECT_EQ(10u, read_back_contents.size());
108}
109
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700110TEST(util, WriteFileNotExist) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800111 std::string s("hello");
112 std::string s2("hello");
113 TemporaryDir test_dir;
114 std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700115 std::string err;
116 EXPECT_TRUE(WriteFile(path, s, &err));
117 EXPECT_EQ("", err);
118 EXPECT_TRUE(ReadFile(path, &s2, &err));
119 EXPECT_EQ("", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +0800120 EXPECT_EQ(s, s2);
121 struct stat sb;
122 int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
123 EXPECT_NE(-1, fd);
124 EXPECT_EQ(0, fstat(fd, &sb));
125 EXPECT_EQ((const unsigned int)(S_IRUSR | S_IWUSR), sb.st_mode & 0777);
126 EXPECT_EQ(0, unlink(path.c_str()));
127}
128
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700129TEST(util, WriteFileExist) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800130 std::string s2("");
131 TemporaryFile tf;
132 ASSERT_TRUE(tf.fd != -1);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700133 std::string err;
134 EXPECT_TRUE(WriteFile(tf.path, "1hello1", &err)) << strerror(errno);
135 EXPECT_EQ("", err);
136 EXPECT_TRUE(ReadFile(tf.path, &s2, &err));
137 EXPECT_EQ("", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +0800138 EXPECT_STREQ("1hello1", s2.c_str());
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700139 EXPECT_TRUE(WriteFile(tf.path, "2ll2", &err));
140 EXPECT_EQ("", err);
141 EXPECT_TRUE(ReadFile(tf.path, &s2, &err));
142 EXPECT_EQ("", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +0800143 EXPECT_STREQ("2ll2", s2.c_str());
144}
145
Tom Cherry517e1f12017-05-04 17:40:33 -0700146TEST(util, DecodeUid) {
147 uid_t decoded_uid;
148 std::string err;
149
150 EXPECT_TRUE(DecodeUid("root", &decoded_uid, &err));
151 EXPECT_EQ("", err);
152 EXPECT_EQ(0U, decoded_uid);
153
154 EXPECT_FALSE(DecodeUid("toot", &decoded_uid, &err));
155 EXPECT_EQ("getpwnam failed: No such file or directory", err);
156 EXPECT_EQ(UINT_MAX, decoded_uid);
157
158 EXPECT_TRUE(DecodeUid("123", &decoded_uid, &err));
159 EXPECT_EQ("", err);
160 EXPECT_EQ(123U, decoded_uid);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800161}
Tom Cherry060b74b2017-04-12 14:27:51 -0700162
163TEST(util, is_dir) {
164 TemporaryDir test_dir;
165 EXPECT_TRUE(is_dir(test_dir.path));
166 TemporaryFile tf;
167 EXPECT_FALSE(is_dir(tf.path));
168}
169
Tom Cherry060b74b2017-04-12 14:27:51 -0700170TEST(util, mkdir_recursive) {
171 TemporaryDir test_dir;
172 std::string path = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
Tom Cherrye7656b72017-05-01 17:10:09 -0700173 EXPECT_EQ(0, mkdir_recursive(path, 0755, nullptr));
Tom Cherry060b74b2017-04-12 14:27:51 -0700174 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
175 EXPECT_TRUE(is_dir(path1.c_str()));
176 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
177 EXPECT_TRUE(is_dir(path1.c_str()));
178 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
179 EXPECT_TRUE(is_dir(path1.c_str()));
180}
181
182TEST(util, mkdir_recursive_extra_slashes) {
183 TemporaryDir test_dir;
184 std::string path = android::base::StringPrintf("%s/three////directories/deep//", test_dir.path);
Tom Cherrye7656b72017-05-01 17:10:09 -0700185 EXPECT_EQ(0, mkdir_recursive(path, 0755, nullptr));
Tom Cherry060b74b2017-04-12 14:27:51 -0700186 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
187 EXPECT_TRUE(is_dir(path1.c_str()));
188 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
189 EXPECT_TRUE(is_dir(path1.c_str()));
190 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
191 EXPECT_TRUE(is_dir(path1.c_str()));
192}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700193
194} // namespace init
195} // namespace android