blob: b00f6e280838f5aa4de0e1e79e486542603d7360 [file] [log] [blame]
Elliott Hughesd0be7c82013-08-08 17:13:33 -07001/*
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 Hughesd0be7c82013-08-08 17:13:33 -070017#include <errno.h>
Elliott Hughesdb1ea342014-01-17 18:42:49 -080018#include <fcntl.h>
Elliott Hughesd0be7c82013-08-08 17:13:33 -070019#include <stdlib.h>
20#include <sys/stat.h>
21
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080022#include <android-base/file.h>
23#include <gtest/gtest.h>
Elliott Hughes594b1a42013-10-22 10:54:11 -070024
Elliott Hughes95646e62023-09-21 14:11:19 -070025#include "utils.h"
26
Elliott Hughes733cedd2020-02-21 23:21:28 -080027#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 Hughesd0be7c82013-08-08 17:13:33 -070035TEST(sys_stat, futimens) {
36 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -070037 ASSERT_TRUE(fp != nullptr);
Elliott Hughesd0be7c82013-08-08 17:13:33 -070038
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
57TEST(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 Hughes95646e62023-09-21 14:11:19 -070064 ASSERT_ERRNO(EBADF);
Elliott Hughesd0be7c82013-08-08 17:13:33 -070065}
Elliott Hughes594b1a42013-10-22 10:54:11 -070066
Elliott Hughesca8e84c2014-10-23 19:10:23 -070067TEST(sys_stat, mkfifo_failure) {
68 errno = 0;
69 ASSERT_EQ(-1, mkfifo("/", 0666));
Elliott Hughes95646e62023-09-21 14:11:19 -070070 ASSERT_ERRNO(EEXIST);
Elliott Hughesca8e84c2014-10-23 19:10:23 -070071}
72
73TEST(sys_stat, mkfifoat_failure) {
74 errno = 0;
75 ASSERT_EQ(-1, mkfifoat(-2, "x", 0666));
Elliott Hughes95646e62023-09-21 14:11:19 -070076 ASSERT_ERRNO(EBADF);
Elliott Hughesca8e84c2014-10-23 19:10:23 -070077}
78
Elliott Hughes594b1a42013-10-22 10:54:11 -070079TEST(sys_stat, mkfifo) {
Christopher Ferris528ad742014-09-24 16:01:18 -070080 if (getuid() == 0) {
81 // Racy but probably sufficient way to get a suitable filename.
82 std::string path;
83 {
84 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080085 path = tf.path;
Christopher Ferris528ad742014-09-24 16:01:18 -070086 }
Elliott Hughes594b1a42013-10-22 10:54:11 -070087
Christopher Ferris528ad742014-09-24 16:01:18 -070088 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 Hughesca8e84c2014-10-23 19:10:23 -070094 // SELinux policy forbids us from creating FIFOs. http://b/17646702.
Elliott Hughesbcaa4542019-03-08 15:20:23 -080095 GTEST_SKIP() << "SELinux policy forbids non-root from creating FIFOs";
Christopher Ferris528ad742014-09-24 16:01:18 -070096 }
Elliott Hughes594b1a42013-10-22 10:54:11 -070097}
Elliott Hughesdb1ea342014-01-17 18:42:49 -080098
99TEST(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 Kralevich3cbc6c62015-01-31 19:57:46 -0800107
Elliott Hughes733cedd2020-02-21 23:21:28 -0800108TEST(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);
112 if (rc == -1 && errno == ENOSYS) {
113 GTEST_SKIP() << "statx returned ENOSYS";
Elliott Hughes733cedd2020-02-21 23:21:28 -0800114 }
115 ASSERT_EQ(0, rc);
116 struct stat64 sb;
117 ASSERT_EQ(0, stat64("/proc/version", &sb));
118 EXPECT_EQ(sb.st_ino, sx.stx_ino);
119 EXPECT_EQ(sb.st_mode, sx.stx_mode);
120#else
121 GTEST_SKIP() << "statx not available";
122#endif
123}
124
Elliott Hughesdb6223f2021-03-08 14:10:46 -0800125TEST(sys_stat, fchmod_EBADF) {
126 ASSERT_EQ(-1, fchmod(-1, 0751));
Elliott Hughes95646e62023-09-21 14:11:19 -0700127 ASSERT_ERRNO(EBADF);
Elliott Hughesdb6223f2021-03-08 14:10:46 -0800128}
129
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800130TEST(sys_stat, fchmodat_EFAULT_file) {
131 ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700132 ASSERT_ERRNO(EFAULT);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800133}
134
135TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_EFAULT_file) {
136 ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, AT_SYMLINK_NOFOLLOW));
137#if defined(__BIONIC__)
Elliott Hughes95646e62023-09-21 14:11:19 -0700138 ASSERT_ERRNO(EFAULT);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800139#else
140 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
141 // returns ENOTSUP
Elliott Hughes95646e62023-09-21 14:11:19 -0700142 ASSERT_ERRNO(ENOTSUP);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800143#endif
144}
145
146TEST(sys_stat, fchmodat_bad_flags) {
147 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~AT_SYMLINK_NOFOLLOW));
Elliott Hughes95646e62023-09-21 14:11:19 -0700148 ASSERT_ERRNO(EINVAL);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800149}
150
151TEST(sys_stat, fchmodat_bad_flags_ALL) {
152 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700153 ASSERT_ERRNO(EINVAL);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800154}
155
156TEST(sys_stat, fchmodat_nonexistant_file) {
157 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700158 ASSERT_ERRNO(ENOENT);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800159}
160
161TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_nonexistant_file) {
162 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, AT_SYMLINK_NOFOLLOW));
163#if defined(__BIONIC__)
Elliott Hughes95646e62023-09-21 14:11:19 -0700164 ASSERT_ERRNO(ENOENT);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800165#else
166 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
167 // returns ENOTSUP
Elliott Hughes95646e62023-09-21 14:11:19 -0700168 ASSERT_ERRNO(ENOTSUP);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800169#endif
170}
171
Yabin Cuic6e58742015-03-09 13:55:18 -0700172static void AssertFileModeEquals(mode_t expected_mode, const char* filename) {
173 struct stat sb;
174 ASSERT_EQ(0, stat(filename, &sb));
175 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;
176 ASSERT_EQ(expected_mode & mask, static_cast<mode_t>(sb.st_mode) & mask);
177}
178
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800179TEST(sys_stat, fchmodat_file) {
180 TemporaryFile tf;
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800181
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800182 ASSERT_EQ(0, fchmodat(AT_FDCWD, tf.path, 0751, 0));
183 AssertFileModeEquals(0751, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800184}
185
186TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_file) {
187 TemporaryFile tf;
188 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800189 int result = fchmodat(AT_FDCWD, tf.path, 0751, AT_SYMLINK_NOFOLLOW);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800190
191#if defined(__BIONIC__)
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800192 ASSERT_EQ(0, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700193 ASSERT_ERRNO(0);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800194 AssertFileModeEquals(0751, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800195#else
196 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
197 // returns ENOTSUP
198 ASSERT_EQ(-1, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700199 ASSERT_ERRNO(ENOTSUP);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800200#endif
201}
202
203TEST(sys_stat, fchmodat_symlink) {
204 TemporaryFile tf;
205 char linkname[255];
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800206
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800207 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800208
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800209 ASSERT_EQ(0, symlink(tf.path, linkname));
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800210 ASSERT_EQ(0, fchmodat(AT_FDCWD, linkname, 0751, 0));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800211 AssertFileModeEquals(0751, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800212 unlink(linkname);
213}
214
215TEST(sys_stat, fchmodat_dangling_symlink) {
216 TemporaryFile tf;
217 char linkname[255];
218 char target[255];
219
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800220 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
221 snprintf(target, sizeof(target), "%s.doesnotexist", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800222
223 ASSERT_EQ(0, symlink(target, linkname));
224 ASSERT_EQ(-1, fchmodat(AT_FDCWD, linkname, 0751, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700225 ASSERT_ERRNO(ENOENT);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800226 unlink(linkname);
227}
228
Yabin Cuic6e58742015-03-09 13:55:18 -0700229static void AssertSymlinkModeEquals(mode_t expected_mode, const char* linkname) {
230 struct stat sb;
231 ASSERT_EQ(0, fstatat(AT_FDCWD, linkname, &sb, AT_SYMLINK_NOFOLLOW));
232 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;
233 ASSERT_EQ(expected_mode & mask, static_cast<mode_t>(sb.st_mode) & mask);
234}
235
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800236TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_symlink) {
237 TemporaryFile tf;
Yabin Cuic6e58742015-03-09 13:55:18 -0700238 struct stat tf_sb;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800239 ASSERT_EQ(0, stat(tf.path, &tf_sb));
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800240
Yabin Cuic6e58742015-03-09 13:55:18 -0700241 char linkname[255];
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800242 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800243
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800244 ASSERT_EQ(0, symlink(tf.path, linkname));
Yabin Cuic6e58742015-03-09 13:55:18 -0700245 int result = fchmodat(AT_FDCWD, linkname, 0751, AT_SYMLINK_NOFOLLOW);
246 // It depends on the kernel whether chmod operation on symlink is allowed.
247 if (result == 0) {
248 AssertSymlinkModeEquals(0751, linkname);
249 } else {
250 ASSERT_EQ(-1, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700251 ASSERT_ERRNO(ENOTSUP);
Yabin Cuic6e58742015-03-09 13:55:18 -0700252 }
253
254 // Target file mode shouldn't be modified.
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800255 AssertFileModeEquals(tf_sb.st_mode, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800256 unlink(linkname);
257}
258
259TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_dangling_symlink) {
260 TemporaryFile tf;
Yabin Cuic6e58742015-03-09 13:55:18 -0700261
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800262 char linkname[255];
263 char target[255];
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800264 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
265 snprintf(target, sizeof(target), "%s.doesnotexist", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800266
267 ASSERT_EQ(0, symlink(target, linkname));
Yabin Cuic6e58742015-03-09 13:55:18 -0700268 int result = fchmodat(AT_FDCWD, linkname, 0751, AT_SYMLINK_NOFOLLOW);
269 // It depends on the kernel whether chmod operation on symlink is allowed.
270 if (result == 0) {
271 AssertSymlinkModeEquals(0751, linkname);
272 } else {
273 ASSERT_EQ(-1, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700274 ASSERT_ERRNO(ENOTSUP);
Yabin Cuic6e58742015-03-09 13:55:18 -0700275 }
276
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800277 unlink(linkname);
278}
Nick Kralevich35778252015-02-24 13:40:43 -0800279
280TEST(sys_stat, faccessat_EINVAL) {
281 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", F_OK, ~AT_SYMLINK_NOFOLLOW));
Elliott Hughes95646e62023-09-21 14:11:19 -0700282 ASSERT_ERRNO(EINVAL);
Nick Kralevich35778252015-02-24 13:40:43 -0800283#if defined(__BIONIC__)
284 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700285 ASSERT_ERRNO(EINVAL);
Nick Kralevich35778252015-02-24 13:40:43 -0800286#else
287 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), AT_SYMLINK_NOFOLLOW));
288 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700289 ASSERT_ERRNO(EINVAL);
Nick Kralevich35778252015-02-24 13:40:43 -0800290#endif
291}
292
293TEST(sys_stat, faccessat_AT_SYMLINK_NOFOLLOW_EINVAL) {
294#if defined(__BIONIC__)
295 // Android doesn't support AT_SYMLINK_NOFOLLOW
296 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", F_OK, AT_SYMLINK_NOFOLLOW));
Elliott Hughes95646e62023-09-21 14:11:19 -0700297 ASSERT_ERRNO(EINVAL);
Nick Kralevich35778252015-02-24 13:40:43 -0800298#else
299 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", F_OK, AT_SYMLINK_NOFOLLOW));
300#endif
301}
302
303TEST(sys_stat, faccessat_dev_null) {
304 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", F_OK, 0));
305 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", R_OK, 0));
306 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", W_OK, 0));
307 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", R_OK|W_OK, 0));
308}
309
310TEST(sys_stat, faccessat_nonexistant) {
311 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/blah", F_OK, AT_SYMLINK_NOFOLLOW));
312#if defined(__BIONIC__)
313 // Android doesn't support AT_SYMLINK_NOFOLLOW
Elliott Hughes95646e62023-09-21 14:11:19 -0700314 ASSERT_ERRNO(EINVAL);
Nick Kralevich35778252015-02-24 13:40:43 -0800315#else
Elliott Hughes95646e62023-09-21 14:11:19 -0700316 ASSERT_ERRNO(ENOENT);
Nick Kralevich35778252015-02-24 13:40:43 -0800317#endif
318}