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 | |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 29 | TEST(file, ReadFileToString_ENOENT) { |
| 30 | std::string s("hello"); |
| 31 | errno = 0; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 32 | ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s)); |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 33 | EXPECT_EQ(ENOENT, errno); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 34 | EXPECT_EQ("", s); // s was cleared. |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Spencer Low | cf168a8 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 37 | TEST(file, ReadFileToString_WriteStringToFile) { |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 38 | TemporaryFile tf; |
| 39 | ASSERT_TRUE(tf.fd != -1); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 40 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path)) |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 41 | << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 42 | std::string s; |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 43 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s)) |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 44 | << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 45 | EXPECT_EQ("abc", s); |
| 46 | } |
| 47 | |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 48 | // symlinks require elevated privileges on Windows. |
| 49 | #if !defined(_WIN32) |
| 50 | TEST(file, ReadFileToString_WriteStringToFile_symlink) { |
| 51 | TemporaryFile target, link; |
| 52 | ASSERT_EQ(0, unlink(link.path)); |
| 53 | ASSERT_EQ(0, symlink(target.path, link.path)); |
| 54 | ASSERT_FALSE(android::base::WriteStringToFile("foo", link.path, false)); |
| 55 | ASSERT_EQ(ELOOP, errno); |
| 56 | ASSERT_TRUE(android::base::WriteStringToFile("foo", link.path, true)); |
| 57 | |
| 58 | std::string s; |
| 59 | ASSERT_FALSE(android::base::ReadFileToString(link.path, &s)); |
| 60 | ASSERT_EQ(ELOOP, errno); |
| 61 | ASSERT_TRUE(android::base::ReadFileToString(link.path, &s, true)); |
| 62 | ASSERT_EQ("foo", s); |
| 63 | } |
| 64 | #endif |
| 65 | |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 66 | // WriteStringToFile2 is explicitly for setting Unix permissions, which make no |
| 67 | // sense on Windows. |
| 68 | #if !defined(_WIN32) |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 69 | TEST(file, WriteStringToFile2) { |
| 70 | TemporaryFile tf; |
| 71 | ASSERT_TRUE(tf.fd != -1); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 72 | ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660, |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 73 | getuid(), getgid())) |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 74 | << strerror(errno); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 75 | struct stat sb; |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 76 | ASSERT_EQ(0, stat(tf.path, &sb)); |
Colin Cross | 56b3734 | 2015-04-30 15:12:21 -0700 | [diff] [blame] | 77 | ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT)); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 78 | ASSERT_EQ(getuid(), sb.st_uid); |
| 79 | ASSERT_EQ(getgid(), sb.st_gid); |
| 80 | std::string s; |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 81 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s)) |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 82 | << strerror(errno); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 83 | EXPECT_EQ("abc", s); |
| 84 | } |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 85 | #endif |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 86 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 87 | TEST(file, WriteStringToFd) { |
| 88 | TemporaryFile tf; |
| 89 | ASSERT_TRUE(tf.fd != -1); |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 90 | ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd)); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 91 | |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 92 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 93 | |
| 94 | std::string s; |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 95 | ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 96 | EXPECT_EQ("abc", s); |
| 97 | } |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 98 | |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 99 | TEST(file, WriteFully) { |
| 100 | TemporaryFile tf; |
| 101 | ASSERT_TRUE(tf.fd != -1); |
| 102 | ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3)); |
Spencer Low | cbf26b7 | 2015-08-03 20:43:24 -0700 | [diff] [blame] | 103 | |
| 104 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); |
| 105 | |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 106 | std::string s; |
Spencer Low | cbf26b7 | 2015-08-03 20:43:24 -0700 | [diff] [blame] | 107 | s.resize(3); |
| 108 | ASSERT_TRUE(android::base::ReadFully(tf.fd, &s[0], s.size())) |
Dan Albert | 850188f | 2015-04-29 17:09:53 -0700 | [diff] [blame] | 109 | << strerror(errno); |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 110 | EXPECT_EQ("abc", s); |
Spencer Low | cbf26b7 | 2015-08-03 20:43:24 -0700 | [diff] [blame] | 111 | |
| 112 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno); |
| 113 | |
| 114 | s.resize(1024); |
| 115 | ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size())); |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 116 | } |
Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 117 | |
| 118 | TEST(file, RemoveFileIfExist) { |
| 119 | TemporaryFile tf; |
| 120 | ASSERT_TRUE(tf.fd != -1); |
| 121 | close(tf.fd); |
| 122 | tf.fd = -1; |
| 123 | std::string err; |
| 124 | ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err)) << err; |
| 125 | ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path)); |
| 126 | TemporaryDir td; |
| 127 | ASSERT_FALSE(android::base::RemoveFileIfExists(td.path)); |
| 128 | ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err)); |
| 129 | ASSERT_EQ("is not a regular or symbol link file", err); |
| 130 | } |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 131 | |
| 132 | TEST(file, Readlink) { |
| 133 | #if !defined(_WIN32) |
| 134 | // Linux doesn't allow empty symbolic links. |
| 135 | std::string min("x"); |
| 136 | // ext2 and ext4 both have PAGE_SIZE limits. |
Elliott Hughes | a6c6570 | 2017-01-11 17:34:40 -0800 | [diff] [blame] | 137 | // If file encryption is enabled, there's extra overhead to store the |
| 138 | // size of the encrypted symlink target. There's also an off-by-one |
| 139 | // in current kernels (and marlin/sailfish where we're seeing this |
| 140 | // failure are still on 3.18, far from current). http://b/33306057. |
| 141 | std::string max(static_cast<size_t>(4096 - 2 - 1 - 1), 'x'); |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 142 | |
| 143 | TemporaryDir td; |
| 144 | std::string min_path{std::string(td.path) + "/" + "min"}; |
| 145 | std::string max_path{std::string(td.path) + "/" + "max"}; |
| 146 | |
| 147 | ASSERT_EQ(0, symlink(min.c_str(), min_path.c_str())); |
| 148 | ASSERT_EQ(0, symlink(max.c_str(), max_path.c_str())); |
| 149 | |
| 150 | std::string result; |
| 151 | |
| 152 | result = "wrong"; |
| 153 | ASSERT_TRUE(android::base::Readlink(min_path, &result)); |
| 154 | ASSERT_EQ(min, result); |
| 155 | |
| 156 | result = "wrong"; |
| 157 | ASSERT_TRUE(android::base::Readlink(max_path, &result)); |
| 158 | ASSERT_EQ(max, result); |
| 159 | #endif |
| 160 | } |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 161 | |
Colin Cross | bb3a515 | 2017-02-23 17:41:56 -0800 | [diff] [blame] | 162 | TEST(file, GetExecutableDirectory) { |
| 163 | std::string path = android::base::GetExecutableDirectory(); |
| 164 | ASSERT_NE("", path); |
| 165 | ASSERT_NE(android::base::GetExecutablePath(), path); |
| 166 | ASSERT_EQ('/', path[0]); |
| 167 | ASSERT_NE('/', path[path.size() - 1]); |
| 168 | } |
| 169 | |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 170 | TEST(file, GetExecutablePath) { |
| 171 | ASSERT_NE("", android::base::GetExecutablePath()); |
| 172 | } |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 173 | |
| 174 | TEST(file, Basename) { |
| 175 | EXPECT_EQ("sh", android::base::Basename("/system/bin/sh")); |
| 176 | EXPECT_EQ("sh", android::base::Basename("sh")); |
| 177 | EXPECT_EQ("sh", android::base::Basename("/system/bin/sh/")); |
| 178 | } |
| 179 | |
| 180 | TEST(file, Dirname) { |
| 181 | EXPECT_EQ("/system/bin", android::base::Dirname("/system/bin/sh")); |
| 182 | EXPECT_EQ(".", android::base::Dirname("sh")); |
| 183 | EXPECT_EQ("/system/bin", android::base::Dirname("/system/bin/sh/")); |
| 184 | } |