Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/file.h" |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 18 | |
| 19 | #include <gtest/gtest.h> |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 20 | |
| 21 | #include <errno.h> |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <unistd.h> |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 24 | |
| 25 | #include <string> |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 26 | |
liwugang | c63cb07 | 2018-07-11 13:24:49 +0800 | [diff] [blame] | 27 | #if !defined(_WIN32) |
| 28 | #include <pwd.h> |
| 29 | #endif |
| 30 | |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 31 | TEST(file, ReadFileToString_ENOENT) { |
| 32 | std::string s("hello"); |
| 33 | errno = 0; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 34 | ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s)); |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 35 | EXPECT_EQ(ENOENT, errno); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 36 | EXPECT_EQ("", s); // s was cleared. |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Spencer Low | cf168a8 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 39 | TEST(file, ReadFileToString_WriteStringToFile) { |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 40 | TemporaryFile tf; |
| 41 | ASSERT_TRUE(tf.fd != -1); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 42 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path)) |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 43 | << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 44 | std::string s; |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 45 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s)) |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 46 | << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 47 | EXPECT_EQ("abc", s); |
| 48 | } |
| 49 | |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 50 | // symlinks require elevated privileges on Windows. |
| 51 | #if !defined(_WIN32) |
| 52 | TEST(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 Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 68 | // WriteStringToFile2 is explicitly for setting Unix permissions, which make no |
| 69 | // sense on Windows. |
| 70 | #if !defined(_WIN32) |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 71 | TEST(file, WriteStringToFile2) { |
| 72 | TemporaryFile tf; |
| 73 | ASSERT_TRUE(tf.fd != -1); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 74 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660, |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 75 | getuid(), getgid())) |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 76 | << strerror(errno); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 77 | struct stat sb; |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 78 | ASSERT_EQ(0, stat(tf.path, &sb)); |
Colin Cross | 56b3734 | 2015-04-30 15:12:21 -0700 | [diff] [blame] | 79 | ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT)); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 80 | ASSERT_EQ(getuid(), sb.st_uid); |
| 81 | ASSERT_EQ(getgid(), sb.st_gid); |
| 82 | std::string s; |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 83 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s)) |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 84 | << strerror(errno); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 85 | EXPECT_EQ("abc", s); |
| 86 | } |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 87 | #endif |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 88 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 89 | TEST(file, WriteStringToFd) { |
| 90 | TemporaryFile tf; |
| 91 | ASSERT_TRUE(tf.fd != -1); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 92 | ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 93 | |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 94 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 95 | |
| 96 | std::string s; |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 97 | ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 98 | EXPECT_EQ("abc", s); |
| 99 | } |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 100 | |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 101 | TEST(file, WriteFully) { |
| 102 | TemporaryFile tf; |
| 103 | ASSERT_TRUE(tf.fd != -1); |
| 104 | ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3)); |
Spencer Low | cbf26b7 | 2015-08-03 20:43:24 -0700 | [diff] [blame] | 105 | |
| 106 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); |
| 107 | |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 108 | std::string s; |
Spencer Low | cbf26b7 | 2015-08-03 20:43:24 -0700 | [diff] [blame] | 109 | s.resize(3); |
| 110 | ASSERT_TRUE(android::base::ReadFully(tf.fd, &s[0], s.size())) |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 111 | << strerror(errno); |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 112 | EXPECT_EQ("abc", s); |
Spencer Low | cbf26b7 | 2015-08-03 20:43:24 -0700 | [diff] [blame] | 113 | |
| 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 Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 118 | } |
Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 119 | |
liwugang | c63cb07 | 2018-07-11 13:24:49 +0800 | [diff] [blame] | 120 | TEST(file, RemoveFileIfExists) { |
Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 121 | 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)); |
liwugang | c63cb07 | 2018-07-11 13:24:49 +0800 | [diff] [blame] | 131 | ASSERT_EQ("is not a regular file or symbolic link", err); |
Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 132 | } |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 133 | |
liwugang | c63cb07 | 2018-07-11 13:24:49 +0800 | [diff] [blame] | 134 | TEST(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) |
| 144 | TEST(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 Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 168 | TEST(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 Hughes | a6c6570 | 2017-01-11 17:34:40 -0800 | [diff] [blame] | 173 | // 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 Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 178 | |
| 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 Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 197 | |
Dimitry Ivanov | 840b601 | 2016-09-09 10:49:21 -0700 | [diff] [blame] | 198 | TEST(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 Cross | bb3a515 | 2017-02-23 17:41:56 -0800 | [diff] [blame] | 230 | TEST(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 Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 238 | TEST(file, GetExecutablePath) { |
| 239 | ASSERT_NE("", android::base::GetExecutablePath()); |
| 240 | } |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 241 | |
| 242 | TEST(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 | |
| 248 | TEST(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 Hughes | 9bb7971 | 2017-03-20 19:16:18 -0700 | [diff] [blame] | 253 | |
| 254 | TEST(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 | |
| 281 | TEST(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 | } |