blob: f64e81c0db51d6dab2a011ff01899c792d5a3172 [file] [log] [blame]
Elliott Hughesdec12b22015-02-02 17:31:27 -08001/*
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 Hughes4f713192015-12-04 22:00:26 -080017#include "android-base/file.h"
Dan Albertc007bc32015-03-16 10:08:46 -070018
19#include <gtest/gtest.h>
Elliott Hughesdec12b22015-02-02 17:31:27 -080020
21#include <errno.h>
Elliott Hughesf682b472015-02-06 12:19:48 -080022#include <fcntl.h>
23#include <unistd.h>
Dan Albertc007bc32015-03-16 10:08:46 -070024
25#include <string>
Elliott Hughesdec12b22015-02-02 17:31:27 -080026
liwugangc63cb072018-07-11 13:24:49 +080027#if !defined(_WIN32)
28#include <pwd.h>
29#endif
30
Elliott Hughesdec12b22015-02-02 17:31:27 -080031TEST(file, ReadFileToString_ENOENT) {
32 std::string s("hello");
33 errno = 0;
Dan Albertc007bc32015-03-16 10:08:46 -070034 ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
Elliott Hughesdec12b22015-02-02 17:31:27 -080035 EXPECT_EQ(ENOENT, errno);
Dan Albertc007bc32015-03-16 10:08:46 -070036 EXPECT_EQ("", s); // s was cleared.
Elliott Hughesdec12b22015-02-02 17:31:27 -080037}
38
Spencer Lowcf168a82015-05-24 15:36:28 -070039TEST(file, ReadFileToString_WriteStringToFile) {
Elliott Hughesf682b472015-02-06 12:19:48 -080040 TemporaryFile tf;
41 ASSERT_TRUE(tf.fd != -1);
Alex Vallée47d67c92015-05-06 16:26:00 -040042 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path))
Dan Albert850188f2015-04-29 17:09:53 -070043 << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080044 std::string s;
Alex Vallée47d67c92015-05-06 16:26:00 -040045 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert850188f2015-04-29 17:09:53 -070046 << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080047 EXPECT_EQ("abc", s);
48}
49
Josh Gaoffabc962016-09-14 16:11:45 -070050// symlinks require elevated privileges on Windows.
51#if !defined(_WIN32)
52TEST(file, ReadFileToString_WriteStringToFile_symlink) {
53 TemporaryFile target, link;
54 ASSERT_EQ(0, unlink(link.path));
55 ASSERT_EQ(0, symlink(target.path, link.path));
56 ASSERT_FALSE(android::base::WriteStringToFile("foo", link.path, false));
57 ASSERT_EQ(ELOOP, errno);
58 ASSERT_TRUE(android::base::WriteStringToFile("foo", link.path, true));
59
60 std::string s;
61 ASSERT_FALSE(android::base::ReadFileToString(link.path, &s));
62 ASSERT_EQ(ELOOP, errno);
63 ASSERT_TRUE(android::base::ReadFileToString(link.path, &s, true));
64 ASSERT_EQ("foo", s);
65}
66#endif
67
Dan Albert0c4b3a32015-04-29 11:32:23 -070068// WriteStringToFile2 is explicitly for setting Unix permissions, which make no
69// sense on Windows.
70#if !defined(_WIN32)
Elliott Hughes9d1f5152015-02-17 10:16:04 -080071TEST(file, WriteStringToFile2) {
72 TemporaryFile tf;
73 ASSERT_TRUE(tf.fd != -1);
Alex Vallée47d67c92015-05-06 16:26:00 -040074 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660,
Dan Albertc007bc32015-03-16 10:08:46 -070075 getuid(), getgid()))
Dan Albert850188f2015-04-29 17:09:53 -070076 << strerror(errno);
Elliott Hughes9d1f5152015-02-17 10:16:04 -080077 struct stat sb;
Alex Vallée47d67c92015-05-06 16:26:00 -040078 ASSERT_EQ(0, stat(tf.path, &sb));
Colin Cross56b37342015-04-30 15:12:21 -070079 ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
Elliott Hughes9d1f5152015-02-17 10:16:04 -080080 ASSERT_EQ(getuid(), sb.st_uid);
81 ASSERT_EQ(getgid(), sb.st_gid);
82 std::string s;
Alex Vallée47d67c92015-05-06 16:26:00 -040083 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert850188f2015-04-29 17:09:53 -070084 << strerror(errno);
Elliott Hughes9d1f5152015-02-17 10:16:04 -080085 EXPECT_EQ("abc", s);
86}
Dan Albert0c4b3a32015-04-29 11:32:23 -070087#endif
Elliott Hughes9d1f5152015-02-17 10:16:04 -080088
Elliott Hughesf682b472015-02-06 12:19:48 -080089TEST(file, WriteStringToFd) {
90 TemporaryFile tf;
91 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070092 ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
Elliott Hughesf682b472015-02-06 12:19:48 -080093
Dan Albert850188f2015-04-29 17:09:53 -070094 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080095
96 std::string s;
Dan Albert850188f2015-04-29 17:09:53 -070097 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080098 EXPECT_EQ("abc", s);
99}
Elliott Hughes56085ed2015-04-24 21:57:16 -0700100
Elliott Hughes56085ed2015-04-24 21:57:16 -0700101TEST(file, WriteFully) {
102 TemporaryFile tf;
103 ASSERT_TRUE(tf.fd != -1);
104 ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
Spencer Lowcbf26b72015-08-03 20:43:24 -0700105
106 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
107
Elliott Hughes56085ed2015-04-24 21:57:16 -0700108 std::string s;
Spencer Lowcbf26b72015-08-03 20:43:24 -0700109 s.resize(3);
110 ASSERT_TRUE(android::base::ReadFully(tf.fd, &s[0], s.size()))
Dan Albert850188f2015-04-29 17:09:53 -0700111 << strerror(errno);
Elliott Hughes56085ed2015-04-24 21:57:16 -0700112 EXPECT_EQ("abc", s);
Spencer Lowcbf26b72015-08-03 20:43:24 -0700113
114 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
115
116 s.resize(1024);
117 ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size()));
Elliott Hughes56085ed2015-04-24 21:57:16 -0700118}
Yabin Cuib6e314a2016-01-29 17:25:54 -0800119
liwugangc63cb072018-07-11 13:24:49 +0800120TEST(file, RemoveFileIfExists) {
Yabin Cuib6e314a2016-01-29 17:25:54 -0800121 TemporaryFile tf;
122 ASSERT_TRUE(tf.fd != -1);
123 close(tf.fd);
124 tf.fd = -1;
125 std::string err;
126 ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err)) << err;
127 ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path));
128 TemporaryDir td;
129 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path));
130 ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err));
liwugangc63cb072018-07-11 13:24:49 +0800131 ASSERT_EQ("is not a regular file or symbolic link", err);
Yabin Cuib6e314a2016-01-29 17:25:54 -0800132}
Elliott Hughesd3ff6e52016-08-23 15:53:45 -0700133
liwugangc63cb072018-07-11 13:24:49 +0800134TEST(file, RemoveFileIfExists_ENOTDIR) {
135 TemporaryFile tf;
136 close(tf.fd);
137 tf.fd = -1;
138 std::string err{"xxx"};
139 ASSERT_TRUE(android::base::RemoveFileIfExists(std::string{tf.path} + "/abc", &err));
140 ASSERT_EQ("xxx", err);
141}
142
143#if !defined(_WIN32)
144TEST(file, RemoveFileIfExists_EACCES) {
145 // EACCES -- one of the directories in the path has no search permission
146 // root can bypass permission restrictions, so drop root.
147 if (getuid() == 0) {
148 passwd* shell = getpwnam("shell");
149 setgid(shell->pw_gid);
150 setuid(shell->pw_uid);
151 }
152
153 TemporaryDir td;
154 TemporaryFile tf(td.path);
155 close(tf.fd);
156 tf.fd = -1;
157 std::string err{"xxx"};
158 // Remove dir's search permission.
159 ASSERT_TRUE(chmod(td.path, S_IRUSR | S_IWUSR) == 0);
160 ASSERT_FALSE(android::base::RemoveFileIfExists(tf.path, &err));
161 ASSERT_EQ("Permission denied", err);
162 // Set dir's search permission again.
163 ASSERT_TRUE(chmod(td.path, S_IRWXU) == 0);
164 ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err));
165}
166#endif
167
Elliott Hughesd3ff6e52016-08-23 15:53:45 -0700168TEST(file, Readlink) {
169#if !defined(_WIN32)
170 // Linux doesn't allow empty symbolic links.
171 std::string min("x");
172 // ext2 and ext4 both have PAGE_SIZE limits.
Elliott Hughesa6c65702017-01-11 17:34:40 -0800173 // If file encryption is enabled, there's extra overhead to store the
174 // size of the encrypted symlink target. There's also an off-by-one
175 // in current kernels (and marlin/sailfish where we're seeing this
176 // failure are still on 3.18, far from current). http://b/33306057.
177 std::string max(static_cast<size_t>(4096 - 2 - 1 - 1), 'x');
Elliott Hughesd3ff6e52016-08-23 15:53:45 -0700178
179 TemporaryDir td;
180 std::string min_path{std::string(td.path) + "/" + "min"};
181 std::string max_path{std::string(td.path) + "/" + "max"};
182
183 ASSERT_EQ(0, symlink(min.c_str(), min_path.c_str()));
184 ASSERT_EQ(0, symlink(max.c_str(), max_path.c_str()));
185
186 std::string result;
187
188 result = "wrong";
189 ASSERT_TRUE(android::base::Readlink(min_path, &result));
190 ASSERT_EQ(min, result);
191
192 result = "wrong";
193 ASSERT_TRUE(android::base::Readlink(max_path, &result));
194 ASSERT_EQ(max, result);
195#endif
196}
Elliott Hughes82ff3152016-08-31 15:07:18 -0700197
Dimitry Ivanov840b6012016-09-09 10:49:21 -0700198TEST(file, Realpath) {
199#if !defined(_WIN32)
200 TemporaryDir td;
201 std::string basename = android::base::Basename(td.path);
202 std::string dir_name = android::base::Dirname(td.path);
203 std::string base_dir_name = android::base::Basename(dir_name);
204
205 {
206 std::string path = dir_name + "/../" + base_dir_name + "/" + basename;
207 std::string result;
208 ASSERT_TRUE(android::base::Realpath(path, &result));
209 ASSERT_EQ(td.path, result);
210 }
211
212 {
213 std::string path = std::string(td.path) + "/..";
214 std::string result;
215 ASSERT_TRUE(android::base::Realpath(path, &result));
216 ASSERT_EQ(dir_name, result);
217 }
218
219 {
220 errno = 0;
221 std::string path = std::string(td.path) + "/foo.noent";
222 std::string result = "wrong";
223 ASSERT_TRUE(!android::base::Realpath(path, &result));
224 ASSERT_TRUE(result.empty());
225 ASSERT_EQ(ENOENT, errno);
226 }
227#endif
228}
229
Colin Crossbb3a5152017-02-23 17:41:56 -0800230TEST(file, GetExecutableDirectory) {
231 std::string path = android::base::GetExecutableDirectory();
232 ASSERT_NE("", path);
233 ASSERT_NE(android::base::GetExecutablePath(), path);
234 ASSERT_EQ('/', path[0]);
235 ASSERT_NE('/', path[path.size() - 1]);
236}
237
Elliott Hughes82ff3152016-08-31 15:07:18 -0700238TEST(file, GetExecutablePath) {
239 ASSERT_NE("", android::base::GetExecutablePath());
240}
Colin Cross58021d12017-02-23 21:23:05 -0800241
242TEST(file, Basename) {
243 EXPECT_EQ("sh", android::base::Basename("/system/bin/sh"));
244 EXPECT_EQ("sh", android::base::Basename("sh"));
245 EXPECT_EQ("sh", android::base::Basename("/system/bin/sh/"));
246}
247
248TEST(file, Dirname) {
249 EXPECT_EQ("/system/bin", android::base::Dirname("/system/bin/sh"));
250 EXPECT_EQ(".", android::base::Dirname("sh"));
251 EXPECT_EQ("/system/bin", android::base::Dirname("/system/bin/sh/"));
252}
Elliott Hughes9bb79712017-03-20 19:16:18 -0700253
254TEST(file, ReadFileToString_capacity) {
255 TemporaryFile tf;
256 ASSERT_TRUE(tf.fd != -1);
257
258 // For a huge file, the overhead should still be small.
259 std::string s;
260 size_t size = 16 * 1024 * 1024;
261 ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
262 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
263 EXPECT_EQ(size, s.size());
264 EXPECT_LT(s.capacity(), size + 16);
265
266 // Even for weird badly-aligned sizes.
267 size += 12345;
268 ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
269 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
270 EXPECT_EQ(size, s.size());
271 EXPECT_LT(s.capacity(), size + 16);
272
273 // We'll shrink an enormous string if you read a small file into it.
274 size = 64;
275 ASSERT_TRUE(android::base::WriteStringToFile(std::string(size, 'x'), tf.path));
276 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
277 EXPECT_EQ(size, s.size());
278 EXPECT_LT(s.capacity(), size + 16);
279}
280
281TEST(file, ReadFileToString_capacity_0) {
282 TemporaryFile tf;
283 ASSERT_TRUE(tf.fd != -1);
284
285 // Because /proc reports its files as zero-length, we don't actually trust
286 // any file that claims to be zero-length. Rather than add increasingly
287 // complex heuristics for shrinking the passed-in string in that case, we
288 // currently leave it alone.
289 std::string s;
290 size_t initial_capacity = s.capacity();
291 ASSERT_TRUE(android::base::WriteStringToFile("", tf.path));
292 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s));
293 EXPECT_EQ(0U, s.size());
294 EXPECT_EQ(initial_capacity, s.capacity());
295}