blob: 4bb8a837814dcd597f55397c2f9d68e98be97eff [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
29TEST(util, ReadFile_ENOENT) {
30 std::string s("hello");
31 std::string err;
32 errno = 0;
33 EXPECT_FALSE(ReadFile("/proc/does-not-exist", &s, &err));
34 EXPECT_EQ("Unable to open '/proc/does-not-exist': No such file or directory", err);
35 EXPECT_EQ(ENOENT, errno);
36 EXPECT_EQ("", s); // s was cleared.
Elliott Hughesf682b472015-02-06 12:19:48 -080037}
38
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070039TEST(util, ReadFileGroupWriteable) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080040 std::string s("hello");
41 TemporaryFile tf;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070042 std::string err;
Yongqin Liudbe88e72016-12-28 16:06:19 +080043 ASSERT_TRUE(tf.fd != -1);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070044 EXPECT_TRUE(WriteFile(tf.path, s, &err)) << strerror(errno);
45 EXPECT_EQ("", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +080046 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070047 EXPECT_FALSE(ReadFile(tf.path, &s, &err)) << strerror(errno);
48 EXPECT_EQ("Skipping insecure file '"s + tf.path + "'", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +080049 EXPECT_EQ("", s); // s was cleared.
50}
51
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070052TEST(util, ReadFileWorldWiteable) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080053 std::string s("hello");
54 TemporaryFile tf;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070055 std::string err;
Yongqin Liudbe88e72016-12-28 16:06:19 +080056 ASSERT_TRUE(tf.fd != -1);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070057 EXPECT_TRUE(WriteFile(tf.path, s, &err)) << strerror(errno);
58 EXPECT_EQ("", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +080059 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070060 EXPECT_FALSE(ReadFile(tf.path, &s, &err)) << strerror(errno);
61 EXPECT_EQ("Skipping insecure file '"s + tf.path + "'", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +080062 EXPECT_EQ("", s); // s was cleared.
63}
64
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070065TEST(util, ReadFileSymbolicLink) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080066 std::string s("hello");
67 errno = 0;
68 // lrwxrwxrwx 1 root root 13 1970-01-01 00:00 charger -> /sbin/healthd
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070069 std::string err;
70 EXPECT_FALSE(ReadFile("/charger", &s, &err));
71 EXPECT_EQ("Unable to open '/charger': Too many symbolic links encountered", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +080072 EXPECT_EQ(ELOOP, errno);
73 EXPECT_EQ("", s); // s was cleared.
74}
75
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070076TEST(util, ReadFileSuccess) {
77 std::string s("hello");
78 std::string err;
79 EXPECT_TRUE(ReadFile("/proc/version", &s, &err));
80 EXPECT_EQ("", err);
81 EXPECT_GT(s.length(), 6U);
82 EXPECT_EQ('\n', s[s.length() - 1]);
83 s[5] = 0;
84 EXPECT_STREQ("Linux", s.c_str());
Elliott Hughesf682b472015-02-06 12:19:48 -080085}
Elliott Hughes8d82ea02015-02-06 20:15:18 -080086
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070087TEST(util, WriteFileBinary) {
Tom Cherry53089aa2017-03-31 15:47:33 -070088 std::string contents("abcd");
89 contents.push_back('\0');
90 contents.push_back('\0');
91 contents.append("dcba");
92 ASSERT_EQ(10u, contents.size());
93
94 TemporaryFile tf;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070095 std::string err;
Tom Cherry53089aa2017-03-31 15:47:33 -070096 ASSERT_TRUE(tf.fd != -1);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070097 EXPECT_TRUE(WriteFile(tf.path, contents, &err)) << strerror(errno);
98 EXPECT_EQ("", err);
Tom Cherry53089aa2017-03-31 15:47:33 -070099
100 std::string read_back_contents;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700101 EXPECT_TRUE(ReadFile(tf.path, &read_back_contents, &err)) << strerror(errno);
102 EXPECT_EQ("", err);
Tom Cherry53089aa2017-03-31 15:47:33 -0700103 EXPECT_EQ(contents, read_back_contents);
104 EXPECT_EQ(10u, read_back_contents.size());
105}
106
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700107TEST(util, WriteFileNotExist) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800108 std::string s("hello");
109 std::string s2("hello");
110 TemporaryDir test_dir;
111 std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700112 std::string err;
113 EXPECT_TRUE(WriteFile(path, s, &err));
114 EXPECT_EQ("", err);
115 EXPECT_TRUE(ReadFile(path, &s2, &err));
116 EXPECT_EQ("", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +0800117 EXPECT_EQ(s, s2);
118 struct stat sb;
119 int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
120 EXPECT_NE(-1, fd);
121 EXPECT_EQ(0, fstat(fd, &sb));
122 EXPECT_EQ((const unsigned int)(S_IRUSR | S_IWUSR), sb.st_mode & 0777);
123 EXPECT_EQ(0, unlink(path.c_str()));
124}
125
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700126TEST(util, WriteFileExist) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800127 std::string s2("");
128 TemporaryFile tf;
129 ASSERT_TRUE(tf.fd != -1);
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700130 std::string err;
131 EXPECT_TRUE(WriteFile(tf.path, "1hello1", &err)) << strerror(errno);
132 EXPECT_EQ("", err);
133 EXPECT_TRUE(ReadFile(tf.path, &s2, &err));
134 EXPECT_EQ("", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +0800135 EXPECT_STREQ("1hello1", s2.c_str());
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700136 EXPECT_TRUE(WriteFile(tf.path, "2ll2", &err));
137 EXPECT_EQ("", err);
138 EXPECT_TRUE(ReadFile(tf.path, &s2, &err));
139 EXPECT_EQ("", err);
Yongqin Liudbe88e72016-12-28 16:06:19 +0800140 EXPECT_STREQ("2ll2", s2.c_str());
141}
142
Tom Cherry517e1f12017-05-04 17:40:33 -0700143TEST(util, DecodeUid) {
144 uid_t decoded_uid;
145 std::string err;
146
147 EXPECT_TRUE(DecodeUid("root", &decoded_uid, &err));
148 EXPECT_EQ("", err);
149 EXPECT_EQ(0U, decoded_uid);
150
151 EXPECT_FALSE(DecodeUid("toot", &decoded_uid, &err));
152 EXPECT_EQ("getpwnam failed: No such file or directory", err);
153 EXPECT_EQ(UINT_MAX, decoded_uid);
154
155 EXPECT_TRUE(DecodeUid("123", &decoded_uid, &err));
156 EXPECT_EQ("", err);
157 EXPECT_EQ(123U, decoded_uid);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800158}
Tom Cherry060b74b2017-04-12 14:27:51 -0700159
160TEST(util, is_dir) {
161 TemporaryDir test_dir;
162 EXPECT_TRUE(is_dir(test_dir.path));
163 TemporaryFile tf;
164 EXPECT_FALSE(is_dir(tf.path));
165}
166
Tom Cherry060b74b2017-04-12 14:27:51 -0700167TEST(util, mkdir_recursive) {
168 TemporaryDir test_dir;
169 std::string path = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
Tom Cherrye7656b72017-05-01 17:10:09 -0700170 EXPECT_EQ(0, mkdir_recursive(path, 0755, nullptr));
Tom Cherry060b74b2017-04-12 14:27:51 -0700171 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
172 EXPECT_TRUE(is_dir(path1.c_str()));
173 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
174 EXPECT_TRUE(is_dir(path1.c_str()));
175 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
176 EXPECT_TRUE(is_dir(path1.c_str()));
177}
178
179TEST(util, mkdir_recursive_extra_slashes) {
180 TemporaryDir test_dir;
181 std::string path = android::base::StringPrintf("%s/three////directories/deep//", test_dir.path);
Tom Cherrye7656b72017-05-01 17:10:09 -0700182 EXPECT_EQ(0, mkdir_recursive(path, 0755, nullptr));
Tom Cherry060b74b2017-04-12 14:27:51 -0700183 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
184 EXPECT_TRUE(is_dir(path1.c_str()));
185 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
186 EXPECT_TRUE(is_dir(path1.c_str()));
187 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
188 EXPECT_TRUE(is_dir(path1.c_str()));
189}