blob: 67946523c50996ab826ac196569b661c72f62825 [file] [log] [blame]
Dan Albertaac6b7c2015-03-16 10:08:46 -07001/*
2 * Copyright (C) 2013 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
Elliott Hughesb6351622015-12-04 22:00:26 -080017#include "android-base/file.h"
Dan Albertaac6b7c2015-03-16 10:08:46 -070018
19#include <gtest/gtest.h>
20
21#include <errno.h>
22#include <fcntl.h>
23#include <unistd.h>
24
25#include <string>
26
Elliott Hughesb6351622015-12-04 22:00:26 -080027#include "android-base/test_utils.h"
Dan Albertaac6b7c2015-03-16 10:08:46 -070028
liwugang78cea682018-07-11 13:24:49 +080029#if !defined(_WIN32)
30#include <pwd.h>
31#endif
32
Dan Albertaac6b7c2015-03-16 10:08:46 -070033TEST(file, ReadFileToString_ENOENT) {
34 std::string s("hello");
35 errno = 0;
36 ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
37 EXPECT_EQ(ENOENT, errno);
38 EXPECT_EQ("", s); // s was cleared.
39}
40
Spencer Low482fa6d2015-05-24 15:36:28 -070041TEST(file, ReadFileToString_WriteStringToFile) {
Dan Albertaac6b7c2015-03-16 10:08:46 -070042 TemporaryFile tf;
43 ASSERT_TRUE(tf.fd != -1);
Alex Valléed0ac74c2015-05-06 16:26:00 -040044 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path))
Dan Albert1a932622015-04-29 17:09:53 -070045 << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -070046 std::string s;
Alex Valléed0ac74c2015-05-06 16:26:00 -040047 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert1a932622015-04-29 17:09:53 -070048 << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -070049 EXPECT_EQ("abc", s);
50}
51
Josh Gao8e1f0d82016-09-14 16:11:45 -070052// symlinks require elevated privileges on Windows.
53#if !defined(_WIN32)
54TEST(file, ReadFileToString_WriteStringToFile_symlink) {
55 TemporaryFile target, link;
56 ASSERT_EQ(0, unlink(link.path));
57 ASSERT_EQ(0, symlink(target.path, link.path));
58 ASSERT_FALSE(android::base::WriteStringToFile("foo", link.path, false));
59 ASSERT_EQ(ELOOP, errno);
60 ASSERT_TRUE(android::base::WriteStringToFile("foo", link.path, true));
61
62 std::string s;
63 ASSERT_FALSE(android::base::ReadFileToString(link.path, &s));
64 ASSERT_EQ(ELOOP, errno);
65 ASSERT_TRUE(android::base::ReadFileToString(link.path, &s, true));
66 ASSERT_EQ("foo", s);
67}
68#endif
69
Dan Albert26cb8ff2015-04-29 11:32:23 -070070// WriteStringToFile2 is explicitly for setting Unix permissions, which make no
71// sense on Windows.
72#if !defined(_WIN32)
Dan Albertaac6b7c2015-03-16 10:08:46 -070073TEST(file, WriteStringToFile2) {
74 TemporaryFile tf;
75 ASSERT_TRUE(tf.fd != -1);
Alex Valléed0ac74c2015-05-06 16:26:00 -040076 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660,
Dan Albertaac6b7c2015-03-16 10:08:46 -070077 getuid(), getgid()))
Dan Albert1a932622015-04-29 17:09:53 -070078 << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -070079 struct stat sb;
Alex Valléed0ac74c2015-05-06 16:26:00 -040080 ASSERT_EQ(0, stat(tf.path, &sb));
Colin Crosseebcd142015-04-30 15:12:21 -070081 ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
Dan Albertaac6b7c2015-03-16 10:08:46 -070082 ASSERT_EQ(getuid(), sb.st_uid);
83 ASSERT_EQ(getgid(), sb.st_gid);
84 std::string s;
Alex Valléed0ac74c2015-05-06 16:26:00 -040085 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert1a932622015-04-29 17:09:53 -070086 << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -070087 EXPECT_EQ("abc", s);
88}
Dan Albert26cb8ff2015-04-29 11:32:23 -070089#endif
Dan Albertaac6b7c2015-03-16 10:08:46 -070090
91TEST(file, WriteStringToFd) {
92 TemporaryFile tf;
93 ASSERT_TRUE(tf.fd != -1);
94 ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
95
Dan Albert1a932622015-04-29 17:09:53 -070096 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -070097
98 std::string s;
Dan Albert1a932622015-04-29 17:09:53 -070099 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
Dan Albertaac6b7c2015-03-16 10:08:46 -0700100 EXPECT_EQ("abc", s);
101}
Elliott Hughes20abc872015-04-24 21:57:16 -0700102
Elliott Hughes20abc872015-04-24 21:57:16 -0700103TEST(file, WriteFully) {
104 TemporaryFile tf;
105 ASSERT_TRUE(tf.fd != -1);
106 ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
Spencer Low38d002e2015-08-03 20:43:24 -0700107
108 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
109
Elliott Hughes20abc872015-04-24 21:57:16 -0700110 std::string s;
Spencer Low38d002e2015-08-03 20:43:24 -0700111 s.resize(3);
112 ASSERT_TRUE(android::base::ReadFully(tf.fd, &s[0], s.size()))
Dan Albert1a932622015-04-29 17:09:53 -0700113 << strerror(errno);
Elliott Hughes20abc872015-04-24 21:57:16 -0700114 EXPECT_EQ("abc", s);
Spencer Low38d002e2015-08-03 20:43:24 -0700115
116 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
117
118 s.resize(1024);
119 ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size()));
Elliott Hughes20abc872015-04-24 21:57:16 -0700120}
Yabin Cui8f6a5a02016-01-29 17:25:54 -0800121
liwugang78cea682018-07-11 13:24:49 +0800122TEST(file, RemoveFileIfExists) {
Yabin Cui8f6a5a02016-01-29 17:25:54 -0800123 TemporaryFile tf;
124 ASSERT_TRUE(tf.fd != -1);
125 close(tf.fd);
126 tf.fd = -1;
127 std::string err;
128 ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err)) << err;
129 ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path));
130 TemporaryDir td;
131 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path));
132 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err));
liwugang78cea682018-07-11 13:24:49 +0800133 ASSERT_EQ("is not a regular file or symbolic link", err);
Yabin Cui8f6a5a02016-01-29 17:25:54 -0800134}
Elliott Hughesa634a9a2016-08-23 15:53:45 -0700135
liwugang78cea682018-07-11 13:24:49 +0800136TEST(file, RemoveFileIfExists_ENOTDIR) {
137 TemporaryFile tf;
138 close(tf.fd);
139 tf.fd = -1;
140 std::string err{"xxx"};
141 ASSERT_TRUE(android::base::RemoveFileIfExists(std::string{tf.path} + "/abc", &err));
142 ASSERT_EQ("xxx", err);
143}
144
145#if !defined(_WIN32)
146TEST(file, RemoveFileIfExists_EACCES) {
147 // EACCES -- one of the directories in the path has no search permission
148 // root can bypass permission restrictions, so drop root.
149 if (getuid() == 0) {
150 passwd* shell = getpwnam("shell");
151 setgid(shell->pw_gid);
152 setuid(shell->pw_uid);
153 }
154
155 TemporaryDir td;
156 TemporaryFile tf(td.path);
157 close(tf.fd);
158 tf.fd = -1;
159 std::string err{"xxx"};
160 // Remove dir's search permission.
161 ASSERT_TRUE(chmod(td.path, S_IRUSR | S_IWUSR) == 0);
162 ASSERT_FALSE(android::base::RemoveFileIfExists(tf.path, &err));
163 ASSERT_EQ("Permission denied", err);
164 // Set dir's search permission again.
165 ASSERT_TRUE(chmod(td.path, S_IRWXU) == 0);
166 ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err));
167}
168#endif
169
Elliott Hughesa634a9a2016-08-23 15:53:45 -0700170TEST(file, Readlink) {
171#if !defined(_WIN32)
172 // Linux doesn't allow empty symbolic links.
173 std::string min("x");
174 // ext2 and ext4 both have PAGE_SIZE limits.
Elliott Hughes7c2a67d2017-01-11 17:34:40 -0800175 // If file encryption is enabled, there's extra overhead to store the
176 // size of the encrypted symlink target. There's also an off-by-one
177 // in current kernels (and marlin/sailfish where we're seeing this
178 // failure are still on 3.18, far from current). http://b/33306057.
179 std::string max(static_cast<size_t>(4096 - 2 - 1 - 1), 'x');
Elliott Hughesa634a9a2016-08-23 15:53:45 -0700180
181 TemporaryDir td;
182 std::string min_path{std::string(td.path) + "/" + "min"};
183 std::string max_path{std::string(td.path) + "/" + "max"};
184
185 ASSERT_EQ(0, symlink(min.c_str(), min_path.c_str()));
186 ASSERT_EQ(0, symlink(max.c_str(), max_path.c_str()));
187
188 std::string result;
189
190 result = "wrong";
191 ASSERT_TRUE(android::base::Readlink(min_path, &result));
192 ASSERT_EQ(min, result);
193
194 result = "wrong";
195 ASSERT_TRUE(android::base::Readlink(max_path, &result));
196 ASSERT_EQ(max, result);
197#endif
198}
Elliott Hughes48f0eb52016-08-31 15:07:18 -0700199
Dimitry Ivanov4edc04f2016-09-09 10:49:21 -0700200TEST(file, Realpath) {
201#if !defined(_WIN32)
202 TemporaryDir td;
203 std::string basename = android::base::Basename(td.path);
204 std::string dir_name = android::base::Dirname(td.path);
205 std::string base_dir_name = android::base::Basename(dir_name);
206
207 {
208 std::string path = dir_name + "/../" + base_dir_name + "/" + basename;
209 std::string result;
210 ASSERT_TRUE(android::base::Realpath(path, &result));
211 ASSERT_EQ(td.path, result);
212 }
213
214 {
215 std::string path = std::string(td.path) + "/..";
216 std::string result;
217 ASSERT_TRUE(android::base::Realpath(path, &result));
218 ASSERT_EQ(dir_name, result);
219 }
220
221 {
222 errno = 0;
223 std::string path = std::string(td.path) + "/foo.noent";
224 std::string result = "wrong";
225 ASSERT_TRUE(!android::base::Realpath(path, &result));
226 ASSERT_TRUE(result.empty());
227 ASSERT_EQ(ENOENT, errno);
228 }
229#endif
230}
231
Colin Cross2909be02017-02-23 17:41:56 -0800232TEST(file, GetExecutableDirectory) {
233 std::string path = android::base::GetExecutableDirectory();
234 ASSERT_NE("", path);
235 ASSERT_NE(android::base::GetExecutablePath(), path);
236 ASSERT_EQ('/', path[0]);
237 ASSERT_NE('/', path[path.size() - 1]);
238}
239
Elliott Hughes48f0eb52016-08-31 15:07:18 -0700240TEST(file, GetExecutablePath) {
241 ASSERT_NE("", android::base::GetExecutablePath());
242}
Colin Cross2e732e22017-02-23 21:23:05 -0800243
244TEST(file, Basename) {
245 EXPECT_EQ("sh", android::base::Basename("/system/bin/sh"));
246 EXPECT_EQ("sh", android::base::Basename("sh"));
247 EXPECT_EQ("sh", android::base::Basename("/system/bin/sh/"));
248}
249
250TEST(file, Dirname) {
251 EXPECT_EQ("/system/bin", android::base::Dirname("/system/bin/sh"));
252 EXPECT_EQ(".", android::base::Dirname("sh"));
253 EXPECT_EQ("/system/bin", android::base::Dirname("/system/bin/sh/"));
254}
Elliott Hughese9ab7ad2017-03-20 19:16:18 -0700255
256TEST(file, ReadFileToString_capacity) {
257 TemporaryFile tf;
258 ASSERT_TRUE(tf.fd != -1);
259
260 // For a huge file, the overhead should still be small.
261 std::string s;
262 size_t size = 16 * 1024 * 1024;
263 ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
264 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
265 EXPECT_EQ(size, s.size());
266 EXPECT_LT(s.capacity(), size + 16);
267
268 // Even for weird badly-aligned sizes.
269 size += 12345;
270 ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
271 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
272 EXPECT_EQ(size, s.size());
273 EXPECT_LT(s.capacity(), size + 16);
274
275 // We'll shrink an enormous string if you read a small file into it.
276 size = 64;
277 ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
278 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
279 EXPECT_EQ(size, s.size());
280 EXPECT_LT(s.capacity(), size + 16);
281}
282
283TEST(file, ReadFileToString_capacity_0) {
284 TemporaryFile tf;
285 ASSERT_TRUE(tf.fd != -1);
286
287 // Because /proc reports its files as zero-length, we don't actually trust
288 // any file that claims to be zero-length. Rather than add increasingly
289 // complex heuristics for shrinking the passed-in string in that case, we
290 // currently leave it alone.
291 std::string s;
292 size_t initial_capacity = s.capacity();
293 ASSERT_TRUE(android::base::WriteStringToFile("", tf.path));
294 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
295 EXPECT_EQ(0U, s.size());
296 EXPECT_EQ(initial_capacity, s.capacity());
297}