Elliott Hughes | d0be7c8 | 2013-08-08 17:13:33 -0700 | [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 | d0be7c8 | 2013-08-08 17:13:33 -0700 | [diff] [blame] | 17 | #include <errno.h> |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 18 | #include <fcntl.h> |
Elliott Hughes | d0be7c8 | 2013-08-08 17:13:33 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <sys/stat.h> |
| 21 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 22 | #include <android-base/file.h> |
| 23 | #include <gtest/gtest.h> |
Elliott Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 25 | #include "utils.h" |
| 26 | |
Elliott Hughes | 733cedd | 2020-02-21 23:21:28 -0800 | [diff] [blame] | 27 | #if defined(__BIONIC__) |
| 28 | #define HAVE_STATX |
| 29 | #elif defined(__GLIBC_PREREQ) |
| 30 | #if __GLIBC_PREREQ(2, 28) |
| 31 | #define HAVE_STATX |
| 32 | #endif |
| 33 | #endif |
| 34 | |
Elliott Hughes | d0be7c8 | 2013-08-08 17:13:33 -0700 | [diff] [blame] | 35 | TEST(sys_stat, futimens) { |
| 36 | FILE* fp = tmpfile(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 37 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | d0be7c8 | 2013-08-08 17:13:33 -0700 | [diff] [blame] | 38 | |
| 39 | int fd = fileno(fp); |
| 40 | ASSERT_NE(fd, -1); |
| 41 | |
| 42 | timespec times[2]; |
| 43 | times[0].tv_sec = 123; |
| 44 | times[0].tv_nsec = 0; |
| 45 | times[1].tv_sec = 456; |
| 46 | times[1].tv_nsec = 0; |
| 47 | ASSERT_EQ(0, futimens(fd, times)) << strerror(errno); |
| 48 | |
| 49 | struct stat sb; |
| 50 | ASSERT_EQ(0, fstat(fd, &sb)); |
| 51 | ASSERT_EQ(times[0].tv_sec, static_cast<long>(sb.st_atime)); |
| 52 | ASSERT_EQ(times[1].tv_sec, static_cast<long>(sb.st_mtime)); |
| 53 | |
| 54 | fclose(fp); |
| 55 | } |
| 56 | |
| 57 | TEST(sys_stat, futimens_EBADF) { |
| 58 | timespec times[2]; |
| 59 | times[0].tv_sec = 123; |
| 60 | times[0].tv_nsec = 0; |
| 61 | times[1].tv_sec = 456; |
| 62 | times[1].tv_nsec = 0; |
| 63 | ASSERT_EQ(-1, futimens(-1, times)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 64 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | d0be7c8 | 2013-08-08 17:13:33 -0700 | [diff] [blame] | 65 | } |
Elliott Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 66 | |
Elliott Hughes | ca8e84c | 2014-10-23 19:10:23 -0700 | [diff] [blame] | 67 | TEST(sys_stat, mkfifo_failure) { |
| 68 | errno = 0; |
| 69 | ASSERT_EQ(-1, mkfifo("/", 0666)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 70 | ASSERT_ERRNO(EEXIST); |
Elliott Hughes | ca8e84c | 2014-10-23 19:10:23 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(sys_stat, mkfifoat_failure) { |
| 74 | errno = 0; |
| 75 | ASSERT_EQ(-1, mkfifoat(-2, "x", 0666)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 76 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | ca8e84c | 2014-10-23 19:10:23 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Elliott Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 79 | TEST(sys_stat, mkfifo) { |
Christopher Ferris | 528ad74 | 2014-09-24 16:01:18 -0700 | [diff] [blame] | 80 | if (getuid() == 0) { |
| 81 | // Racy but probably sufficient way to get a suitable filename. |
| 82 | std::string path; |
| 83 | { |
| 84 | TemporaryFile tf; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 85 | path = tf.path; |
Christopher Ferris | 528ad74 | 2014-09-24 16:01:18 -0700 | [diff] [blame] | 86 | } |
Elliott Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 87 | |
Christopher Ferris | 528ad74 | 2014-09-24 16:01:18 -0700 | [diff] [blame] | 88 | ASSERT_EQ(0, mkfifo(path.c_str(), 0666)); |
| 89 | struct stat sb; |
| 90 | ASSERT_EQ(0, stat(path.c_str(), &sb)); |
| 91 | ASSERT_TRUE(S_ISFIFO(sb.st_mode)); |
| 92 | unlink(path.c_str()); |
| 93 | } else { |
Elliott Hughes | ca8e84c | 2014-10-23 19:10:23 -0700 | [diff] [blame] | 94 | // SELinux policy forbids us from creating FIFOs. http://b/17646702. |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 95 | GTEST_SKIP() << "SELinux policy forbids non-root from creating FIFOs"; |
Christopher Ferris | 528ad74 | 2014-09-24 16:01:18 -0700 | [diff] [blame] | 96 | } |
Elliott Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 97 | } |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 98 | |
| 99 | TEST(sys_stat, stat64_lstat64_fstat64) { |
| 100 | struct stat64 sb; |
| 101 | ASSERT_EQ(0, stat64("/proc/version", &sb)); |
| 102 | ASSERT_EQ(0, lstat64("/proc/version", &sb)); |
| 103 | int fd = open("/proc/version", O_RDONLY); |
| 104 | ASSERT_EQ(0, fstat64(fd, &sb)); |
| 105 | close(fd); |
| 106 | } |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 107 | |
Elliott Hughes | 733cedd | 2020-02-21 23:21:28 -0800 | [diff] [blame] | 108 | TEST(sys_stat, statx) { |
| 109 | #if defined(HAVE_STATX) |
| 110 | struct statx sx; |
| 111 | int rc = statx(AT_FDCWD, "/proc/version", AT_STATX_SYNC_AS_STAT, STATX_ALL, &sx); |
Elliott Hughes | 4ae4be9 | 2023-09-22 17:15:25 -0700 | [diff] [blame] | 112 | if (rc == -1 && errno == ENOSYS) GTEST_SKIP() << "no statx() in this kernel"; |
Elliott Hughes | 733cedd | 2020-02-21 23:21:28 -0800 | [diff] [blame] | 113 | ASSERT_EQ(0, rc); |
| 114 | struct stat64 sb; |
| 115 | ASSERT_EQ(0, stat64("/proc/version", &sb)); |
| 116 | EXPECT_EQ(sb.st_ino, sx.stx_ino); |
| 117 | EXPECT_EQ(sb.st_mode, sx.stx_mode); |
| 118 | #else |
| 119 | GTEST_SKIP() << "statx not available"; |
| 120 | #endif |
| 121 | } |
| 122 | |
Elliott Hughes | db6223f | 2021-03-08 14:10:46 -0800 | [diff] [blame] | 123 | TEST(sys_stat, fchmod_EBADF) { |
| 124 | ASSERT_EQ(-1, fchmod(-1, 0751)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 125 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | db6223f | 2021-03-08 14:10:46 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 128 | TEST(sys_stat, fchmodat_EFAULT_file) { |
| 129 | ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 130 | ASSERT_ERRNO(EFAULT); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_EFAULT_file) { |
| 134 | ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, AT_SYMLINK_NOFOLLOW)); |
| 135 | #if defined(__BIONIC__) |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 136 | ASSERT_ERRNO(EFAULT); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 137 | #else |
| 138 | // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always |
| 139 | // returns ENOTSUP |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 140 | ASSERT_ERRNO(ENOTSUP); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 141 | #endif |
| 142 | } |
| 143 | |
| 144 | TEST(sys_stat, fchmodat_bad_flags) { |
| 145 | ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~AT_SYMLINK_NOFOLLOW)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 146 | ASSERT_ERRNO(EINVAL); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | TEST(sys_stat, fchmodat_bad_flags_ALL) { |
| 150 | ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 151 | ASSERT_ERRNO(EINVAL); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Elliott Hughes | 219e602 | 2024-08-27 19:12:08 +0000 | [diff] [blame] | 154 | TEST(sys_stat, fchmodat_nonexistent_file) { |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 155 | ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 156 | ASSERT_ERRNO(ENOENT); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Elliott Hughes | 219e602 | 2024-08-27 19:12:08 +0000 | [diff] [blame] | 159 | TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_nonexistent_file) { |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 160 | ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, AT_SYMLINK_NOFOLLOW)); |
| 161 | #if defined(__BIONIC__) |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 162 | ASSERT_ERRNO(ENOENT); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 163 | #else |
| 164 | // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always |
| 165 | // returns ENOTSUP |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 166 | ASSERT_ERRNO(ENOTSUP); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 167 | #endif |
| 168 | } |
| 169 | |
Yabin Cui | c6e5874 | 2015-03-09 13:55:18 -0700 | [diff] [blame] | 170 | static void AssertFileModeEquals(mode_t expected_mode, const char* filename) { |
| 171 | struct stat sb; |
| 172 | ASSERT_EQ(0, stat(filename, &sb)); |
| 173 | mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; |
| 174 | ASSERT_EQ(expected_mode & mask, static_cast<mode_t>(sb.st_mode) & mask); |
| 175 | } |
| 176 | |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 177 | TEST(sys_stat, fchmodat_file) { |
| 178 | TemporaryFile tf; |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 179 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 180 | ASSERT_EQ(0, fchmodat(AT_FDCWD, tf.path, 0751, 0)); |
| 181 | AssertFileModeEquals(0751, tf.path); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_file) { |
| 185 | TemporaryFile tf; |
| 186 | errno = 0; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 187 | int result = fchmodat(AT_FDCWD, tf.path, 0751, AT_SYMLINK_NOFOLLOW); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 188 | |
| 189 | #if defined(__BIONIC__) |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 190 | ASSERT_EQ(0, result); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 191 | ASSERT_ERRNO(0); |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 192 | AssertFileModeEquals(0751, tf.path); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 193 | #else |
| 194 | // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always |
| 195 | // returns ENOTSUP |
| 196 | ASSERT_EQ(-1, result); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 197 | ASSERT_ERRNO(ENOTSUP); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 198 | #endif |
| 199 | } |
| 200 | |
| 201 | TEST(sys_stat, fchmodat_symlink) { |
| 202 | TemporaryFile tf; |
| 203 | char linkname[255]; |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 204 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 205 | snprintf(linkname, sizeof(linkname), "%s.link", tf.path); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 206 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 207 | ASSERT_EQ(0, symlink(tf.path, linkname)); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 208 | ASSERT_EQ(0, fchmodat(AT_FDCWD, linkname, 0751, 0)); |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 209 | AssertFileModeEquals(0751, tf.path); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 210 | unlink(linkname); |
| 211 | } |
| 212 | |
| 213 | TEST(sys_stat, fchmodat_dangling_symlink) { |
| 214 | TemporaryFile tf; |
| 215 | char linkname[255]; |
| 216 | char target[255]; |
| 217 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 218 | snprintf(linkname, sizeof(linkname), "%s.link", tf.path); |
| 219 | snprintf(target, sizeof(target), "%s.doesnotexist", tf.path); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 220 | |
| 221 | ASSERT_EQ(0, symlink(target, linkname)); |
| 222 | ASSERT_EQ(-1, fchmodat(AT_FDCWD, linkname, 0751, 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 223 | ASSERT_ERRNO(ENOENT); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 224 | unlink(linkname); |
| 225 | } |
| 226 | |
Yabin Cui | c6e5874 | 2015-03-09 13:55:18 -0700 | [diff] [blame] | 227 | static void AssertSymlinkModeEquals(mode_t expected_mode, const char* linkname) { |
| 228 | struct stat sb; |
| 229 | ASSERT_EQ(0, fstatat(AT_FDCWD, linkname, &sb, AT_SYMLINK_NOFOLLOW)); |
| 230 | mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; |
| 231 | ASSERT_EQ(expected_mode & mask, static_cast<mode_t>(sb.st_mode) & mask); |
| 232 | } |
| 233 | |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 234 | TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_symlink) { |
| 235 | TemporaryFile tf; |
Yabin Cui | c6e5874 | 2015-03-09 13:55:18 -0700 | [diff] [blame] | 236 | struct stat tf_sb; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 237 | ASSERT_EQ(0, stat(tf.path, &tf_sb)); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 238 | |
Yabin Cui | c6e5874 | 2015-03-09 13:55:18 -0700 | [diff] [blame] | 239 | char linkname[255]; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 240 | snprintf(linkname, sizeof(linkname), "%s.link", tf.path); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 241 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 242 | ASSERT_EQ(0, symlink(tf.path, linkname)); |
Yabin Cui | c6e5874 | 2015-03-09 13:55:18 -0700 | [diff] [blame] | 243 | int result = fchmodat(AT_FDCWD, linkname, 0751, AT_SYMLINK_NOFOLLOW); |
| 244 | // It depends on the kernel whether chmod operation on symlink is allowed. |
| 245 | if (result == 0) { |
| 246 | AssertSymlinkModeEquals(0751, linkname); |
| 247 | } else { |
| 248 | ASSERT_EQ(-1, result); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 249 | ASSERT_ERRNO(ENOTSUP); |
Yabin Cui | c6e5874 | 2015-03-09 13:55:18 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Target file mode shouldn't be modified. |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 253 | AssertFileModeEquals(tf_sb.st_mode, tf.path); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 254 | unlink(linkname); |
| 255 | } |
| 256 | |
| 257 | TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_dangling_symlink) { |
| 258 | TemporaryFile tf; |
Yabin Cui | c6e5874 | 2015-03-09 13:55:18 -0700 | [diff] [blame] | 259 | |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 260 | char linkname[255]; |
| 261 | char target[255]; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 262 | snprintf(linkname, sizeof(linkname), "%s.link", tf.path); |
| 263 | snprintf(target, sizeof(target), "%s.doesnotexist", tf.path); |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 264 | |
| 265 | ASSERT_EQ(0, symlink(target, linkname)); |
Yabin Cui | c6e5874 | 2015-03-09 13:55:18 -0700 | [diff] [blame] | 266 | int result = fchmodat(AT_FDCWD, linkname, 0751, AT_SYMLINK_NOFOLLOW); |
| 267 | // It depends on the kernel whether chmod operation on symlink is allowed. |
| 268 | if (result == 0) { |
| 269 | AssertSymlinkModeEquals(0751, linkname); |
| 270 | } else { |
| 271 | ASSERT_EQ(-1, result); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 272 | ASSERT_ERRNO(ENOTSUP); |
Yabin Cui | c6e5874 | 2015-03-09 13:55:18 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Nick Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 275 | unlink(linkname); |
| 276 | } |
Nick Kralevich | 3577825 | 2015-02-24 13:40:43 -0800 | [diff] [blame] | 277 | |
| 278 | TEST(sys_stat, faccessat_EINVAL) { |
| 279 | ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", F_OK, ~AT_SYMLINK_NOFOLLOW)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 280 | ASSERT_ERRNO(EINVAL); |
Nick Kralevich | 3577825 | 2015-02-24 13:40:43 -0800 | [diff] [blame] | 281 | #if defined(__BIONIC__) |
| 282 | ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 283 | ASSERT_ERRNO(EINVAL); |
Nick Kralevich | 3577825 | 2015-02-24 13:40:43 -0800 | [diff] [blame] | 284 | #else |
| 285 | ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), AT_SYMLINK_NOFOLLOW)); |
| 286 | ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 287 | ASSERT_ERRNO(EINVAL); |
Nick Kralevich | 3577825 | 2015-02-24 13:40:43 -0800 | [diff] [blame] | 288 | #endif |
| 289 | } |
| 290 | |
| 291 | TEST(sys_stat, faccessat_AT_SYMLINK_NOFOLLOW_EINVAL) { |
| 292 | #if defined(__BIONIC__) |
| 293 | // Android doesn't support AT_SYMLINK_NOFOLLOW |
| 294 | ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", F_OK, AT_SYMLINK_NOFOLLOW)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 295 | ASSERT_ERRNO(EINVAL); |
Nick Kralevich | 3577825 | 2015-02-24 13:40:43 -0800 | [diff] [blame] | 296 | #else |
| 297 | ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 298 | #endif |
| 299 | } |
| 300 | |
| 301 | TEST(sys_stat, faccessat_dev_null) { |
| 302 | ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", F_OK, 0)); |
| 303 | ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", R_OK, 0)); |
| 304 | ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", W_OK, 0)); |
| 305 | ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", R_OK|W_OK, 0)); |
| 306 | } |
| 307 | |
Elliott Hughes | 219e602 | 2024-08-27 19:12:08 +0000 | [diff] [blame] | 308 | TEST(sys_stat, faccessat_nonexistent) { |
Nick Kralevich | 3577825 | 2015-02-24 13:40:43 -0800 | [diff] [blame] | 309 | ASSERT_EQ(-1, faccessat(AT_FDCWD, "/blah", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 310 | #if defined(__BIONIC__) |
| 311 | // Android doesn't support AT_SYMLINK_NOFOLLOW |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 312 | ASSERT_ERRNO(EINVAL); |
Nick Kralevich | 3577825 | 2015-02-24 13:40:43 -0800 | [diff] [blame] | 313 | #else |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 314 | ASSERT_ERRNO(ENOENT); |
Nick Kralevich | 3577825 | 2015-02-24 13:40:43 -0800 | [diff] [blame] | 315 | #endif |
| 316 | } |
Elliott Hughes | 219e602 | 2024-08-27 19:12:08 +0000 | [diff] [blame] | 317 | |
| 318 | TEST(sys_stat, lchmod) { |
| 319 | TemporaryFile tf; |
| 320 | struct stat tf_sb; |
| 321 | ASSERT_EQ(0, stat(tf.path, &tf_sb)); |
| 322 | |
| 323 | char linkname[255]; |
| 324 | snprintf(linkname, sizeof(linkname), "%s.link", tf.path); |
| 325 | |
| 326 | ASSERT_EQ(0, symlink(tf.path, linkname)); |
| 327 | int result = lchmod(linkname, 0751); |
| 328 | // Whether or not chmod is allowed on a symlink depends on the kernel. |
| 329 | if (result == 0) { |
| 330 | AssertSymlinkModeEquals(0751, linkname); |
| 331 | } else { |
| 332 | ASSERT_EQ(-1, result); |
| 333 | ASSERT_ERRNO(ENOTSUP); |
| 334 | } |
| 335 | |
| 336 | // The target file mode shouldn't be modified. |
| 337 | AssertFileModeEquals(tf_sb.st_mode, tf.path); |
| 338 | unlink(linkname); |
| 339 | } |