Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | #include "util.h" |
| 18 | |
| 19 | #include <errno.h> |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <sys/stat.h> |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 22 | |
Mark Salyzyn | 9f1cf25 | 2018-11-12 12:45:59 -0800 | [diff] [blame] | 23 | #include <android-base/file.h> |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
| 26 | |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 27 | using namespace std::literals::string_literals; |
| 28 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace init { |
| 31 | |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 32 | TEST(util, ReadFile_ENOENT) { |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 33 | errno = 0; |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 34 | auto file_contents = ReadFile("/proc/does-not-exist"); |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 35 | EXPECT_EQ(ENOENT, errno); |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 36 | ASSERT_FALSE(file_contents); |
Tom Cherry | 9949ec5 | 2019-05-16 16:54:49 -0700 | [diff] [blame^] | 37 | EXPECT_EQ("open() failed: No such file or directory", file_contents.error().as_string); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 40 | TEST(util, ReadFileGroupWriteable) { |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 41 | std::string s("hello"); |
| 42 | TemporaryFile tf; |
| 43 | ASSERT_TRUE(tf.fd != -1); |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 44 | EXPECT_TRUE(WriteFile(tf.path, s)) << strerror(errno); |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 45 | EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno); |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 46 | auto file_contents = ReadFile(tf.path); |
| 47 | ASSERT_FALSE(file_contents) << strerror(errno); |
Tom Cherry | 9949ec5 | 2019-05-16 16:54:49 -0700 | [diff] [blame^] | 48 | EXPECT_EQ("Skipping insecure file", file_contents.error().as_string); |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 49 | } |
| 50 | |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 51 | TEST(util, ReadFileWorldWiteable) { |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 52 | std::string s("hello"); |
| 53 | TemporaryFile tf; |
| 54 | ASSERT_TRUE(tf.fd != -1); |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 55 | EXPECT_TRUE(WriteFile(tf.path, s)) << strerror(errno); |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 56 | EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno); |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 57 | auto file_contents = ReadFile(tf.path); |
| 58 | ASSERT_FALSE(file_contents) << strerror(errno); |
Tom Cherry | 9949ec5 | 2019-05-16 16:54:49 -0700 | [diff] [blame^] | 59 | EXPECT_EQ("Skipping insecure file", file_contents.error().as_string); |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 60 | } |
| 61 | |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 62 | TEST(util, ReadFileSymbolicLink) { |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 63 | errno = 0; |
| 64 | // lrwxrwxrwx 1 root root 13 1970-01-01 00:00 charger -> /sbin/healthd |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 65 | auto file_contents = ReadFile("/charger"); |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 66 | EXPECT_EQ(ELOOP, errno); |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 67 | ASSERT_FALSE(file_contents); |
Tom Cherry | 9949ec5 | 2019-05-16 16:54:49 -0700 | [diff] [blame^] | 68 | EXPECT_EQ("open() failed: Too many symbolic links encountered", |
| 69 | file_contents.error().as_string); |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 70 | } |
| 71 | |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 72 | TEST(util, ReadFileSuccess) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 73 | auto file_contents = ReadFile("/proc/version"); |
| 74 | ASSERT_TRUE(file_contents); |
| 75 | EXPECT_GT(file_contents->length(), 6U); |
| 76 | EXPECT_EQ('\n', file_contents->at(file_contents->length() - 1)); |
| 77 | (*file_contents)[5] = 0; |
| 78 | EXPECT_STREQ("Linux", file_contents->c_str()); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 79 | } |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 80 | |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 81 | TEST(util, WriteFileBinary) { |
Tom Cherry | 53089aa | 2017-03-31 15:47:33 -0700 | [diff] [blame] | 82 | std::string contents("abcd"); |
| 83 | contents.push_back('\0'); |
| 84 | contents.push_back('\0'); |
| 85 | contents.append("dcba"); |
| 86 | ASSERT_EQ(10u, contents.size()); |
| 87 | |
| 88 | TemporaryFile tf; |
| 89 | ASSERT_TRUE(tf.fd != -1); |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 90 | EXPECT_TRUE(WriteFile(tf.path, contents)) << strerror(errno); |
Tom Cherry | 53089aa | 2017-03-31 15:47:33 -0700 | [diff] [blame] | 91 | |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 92 | auto read_back_contents = ReadFile(tf.path); |
| 93 | ASSERT_TRUE(read_back_contents) << strerror(errno); |
| 94 | EXPECT_EQ(contents, *read_back_contents); |
| 95 | EXPECT_EQ(10u, read_back_contents->size()); |
Tom Cherry | 53089aa | 2017-03-31 15:47:33 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 98 | TEST(util, WriteFileNotExist) { |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 99 | std::string s("hello"); |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 100 | TemporaryDir test_dir; |
| 101 | std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path); |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 102 | EXPECT_TRUE(WriteFile(path, s)); |
| 103 | auto file_contents = ReadFile(path); |
| 104 | ASSERT_TRUE(file_contents); |
| 105 | EXPECT_EQ(s, *file_contents); |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 106 | struct stat sb; |
| 107 | int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC); |
| 108 | EXPECT_NE(-1, fd); |
| 109 | EXPECT_EQ(0, fstat(fd, &sb)); |
| 110 | EXPECT_EQ((const unsigned int)(S_IRUSR | S_IWUSR), sb.st_mode & 0777); |
| 111 | EXPECT_EQ(0, unlink(path.c_str())); |
| 112 | } |
| 113 | |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 114 | TEST(util, WriteFileExist) { |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 115 | TemporaryFile tf; |
| 116 | ASSERT_TRUE(tf.fd != -1); |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 117 | EXPECT_TRUE(WriteFile(tf.path, "1hello1")) << strerror(errno); |
| 118 | auto file_contents = ReadFile(tf.path); |
| 119 | ASSERT_TRUE(file_contents); |
| 120 | EXPECT_EQ("1hello1", *file_contents); |
| 121 | EXPECT_TRUE(WriteFile(tf.path, "2ll2")); |
| 122 | file_contents = ReadFile(tf.path); |
| 123 | ASSERT_TRUE(file_contents); |
| 124 | EXPECT_EQ("2ll2", *file_contents); |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 125 | } |
| 126 | |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 127 | TEST(util, DecodeUid) { |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 128 | auto decoded_uid = DecodeUid("root"); |
| 129 | EXPECT_TRUE(decoded_uid); |
| 130 | EXPECT_EQ(0U, *decoded_uid); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 131 | |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 132 | decoded_uid = DecodeUid("toot"); |
| 133 | EXPECT_FALSE(decoded_uid); |
Tom Cherry | 9949ec5 | 2019-05-16 16:54:49 -0700 | [diff] [blame^] | 134 | EXPECT_EQ("getpwnam failed: No such file or directory", decoded_uid.error().as_string); |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 135 | |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 136 | decoded_uid = DecodeUid("123"); |
| 137 | EXPECT_TRUE(decoded_uid); |
| 138 | EXPECT_EQ(123U, *decoded_uid); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 139 | } |
Tom Cherry | 060b74b | 2017-04-12 14:27:51 -0700 | [diff] [blame] | 140 | |
| 141 | TEST(util, is_dir) { |
| 142 | TemporaryDir test_dir; |
| 143 | EXPECT_TRUE(is_dir(test_dir.path)); |
| 144 | TemporaryFile tf; |
| 145 | EXPECT_FALSE(is_dir(tf.path)); |
| 146 | } |
| 147 | |
Tom Cherry | 060b74b | 2017-04-12 14:27:51 -0700 | [diff] [blame] | 148 | TEST(util, mkdir_recursive) { |
| 149 | TemporaryDir test_dir; |
| 150 | std::string path = android::base::StringPrintf("%s/three/directories/deep", test_dir.path); |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 151 | EXPECT_TRUE(mkdir_recursive(path, 0755)); |
Tom Cherry | 060b74b | 2017-04-12 14:27:51 -0700 | [diff] [blame] | 152 | std::string path1 = android::base::StringPrintf("%s/three", test_dir.path); |
| 153 | EXPECT_TRUE(is_dir(path1.c_str())); |
| 154 | std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path); |
| 155 | EXPECT_TRUE(is_dir(path1.c_str())); |
| 156 | std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path); |
| 157 | EXPECT_TRUE(is_dir(path1.c_str())); |
| 158 | } |
| 159 | |
| 160 | TEST(util, mkdir_recursive_extra_slashes) { |
| 161 | TemporaryDir test_dir; |
| 162 | std::string path = android::base::StringPrintf("%s/three////directories/deep//", test_dir.path); |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 163 | EXPECT_TRUE(mkdir_recursive(path, 0755)); |
Tom Cherry | 060b74b | 2017-04-12 14:27:51 -0700 | [diff] [blame] | 164 | std::string path1 = android::base::StringPrintf("%s/three", test_dir.path); |
| 165 | EXPECT_TRUE(is_dir(path1.c_str())); |
| 166 | std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path); |
| 167 | EXPECT_TRUE(is_dir(path1.c_str())); |
| 168 | std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path); |
| 169 | EXPECT_TRUE(is_dir(path1.c_str())); |
| 170 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 171 | |
| 172 | } // namespace init |
| 173 | } // namespace android |