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