blob: 8f1437b1bda1bc60698b1a8da71585a306f6da66 [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 Hughes733cedd2020-02-21 23:21:28 -080025#if defined(__BIONIC__)
26#define HAVE_STATX
27#elif defined(__GLIBC_PREREQ)
28#if __GLIBC_PREREQ(2, 28)
29#define HAVE_STATX
30#endif
31#endif
32
Elliott Hughesd0be7c82013-08-08 17:13:33 -070033TEST(sys_stat, futimens) {
34 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -070035 ASSERT_TRUE(fp != nullptr);
Elliott Hughesd0be7c82013-08-08 17:13:33 -070036
37 int fd = fileno(fp);
38 ASSERT_NE(fd, -1);
39
40 timespec times[2];
41 times[0].tv_sec = 123;
42 times[0].tv_nsec = 0;
43 times[1].tv_sec = 456;
44 times[1].tv_nsec = 0;
45 ASSERT_EQ(0, futimens(fd, times)) << strerror(errno);
46
47 struct stat sb;
48 ASSERT_EQ(0, fstat(fd, &sb));
49 ASSERT_EQ(times[0].tv_sec, static_cast<long>(sb.st_atime));
50 ASSERT_EQ(times[1].tv_sec, static_cast<long>(sb.st_mtime));
51
52 fclose(fp);
53}
54
55TEST(sys_stat, futimens_EBADF) {
56 timespec times[2];
57 times[0].tv_sec = 123;
58 times[0].tv_nsec = 0;
59 times[1].tv_sec = 456;
60 times[1].tv_nsec = 0;
61 ASSERT_EQ(-1, futimens(-1, times));
62 ASSERT_EQ(EBADF, errno);
63}
Elliott Hughes594b1a42013-10-22 10:54:11 -070064
Elliott Hughesca8e84c2014-10-23 19:10:23 -070065TEST(sys_stat, mkfifo_failure) {
66 errno = 0;
67 ASSERT_EQ(-1, mkfifo("/", 0666));
68 ASSERT_EQ(EEXIST, errno);
69}
70
71TEST(sys_stat, mkfifoat_failure) {
72 errno = 0;
73 ASSERT_EQ(-1, mkfifoat(-2, "x", 0666));
74 ASSERT_EQ(EBADF, errno);
75}
76
Elliott Hughes594b1a42013-10-22 10:54:11 -070077TEST(sys_stat, mkfifo) {
Christopher Ferris528ad742014-09-24 16:01:18 -070078 if (getuid() == 0) {
79 // Racy but probably sufficient way to get a suitable filename.
80 std::string path;
81 {
82 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080083 path = tf.path;
Christopher Ferris528ad742014-09-24 16:01:18 -070084 }
Elliott Hughes594b1a42013-10-22 10:54:11 -070085
Christopher Ferris528ad742014-09-24 16:01:18 -070086 ASSERT_EQ(0, mkfifo(path.c_str(), 0666));
87 struct stat sb;
88 ASSERT_EQ(0, stat(path.c_str(), &sb));
89 ASSERT_TRUE(S_ISFIFO(sb.st_mode));
90 unlink(path.c_str());
91 } else {
Elliott Hughesca8e84c2014-10-23 19:10:23 -070092 // SELinux policy forbids us from creating FIFOs. http://b/17646702.
Elliott Hughesbcaa4542019-03-08 15:20:23 -080093 GTEST_SKIP() << "SELinux policy forbids non-root from creating FIFOs";
Christopher Ferris528ad742014-09-24 16:01:18 -070094 }
Elliott Hughes594b1a42013-10-22 10:54:11 -070095}
Elliott Hughesdb1ea342014-01-17 18:42:49 -080096
97TEST(sys_stat, stat64_lstat64_fstat64) {
98 struct stat64 sb;
99 ASSERT_EQ(0, stat64("/proc/version", &sb));
100 ASSERT_EQ(0, lstat64("/proc/version", &sb));
101 int fd = open("/proc/version", O_RDONLY);
102 ASSERT_EQ(0, fstat64(fd, &sb));
103 close(fd);
104}
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800105
Elliott Hughes733cedd2020-02-21 23:21:28 -0800106TEST(sys_stat, statx) {
107#if defined(HAVE_STATX)
108 struct statx sx;
109 int rc = statx(AT_FDCWD, "/proc/version", AT_STATX_SYNC_AS_STAT, STATX_ALL, &sx);
110 if (rc == -1 && errno == ENOSYS) {
111 GTEST_SKIP() << "statx returned ENOSYS";
Elliott Hughes733cedd2020-02-21 23:21:28 -0800112 }
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
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800123TEST(sys_stat, fchmodat_EFAULT_file) {
124 ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, 0));
125 ASSERT_EQ(EFAULT, errno);
126}
127
128TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_EFAULT_file) {
129 ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, AT_SYMLINK_NOFOLLOW));
130#if defined(__BIONIC__)
131 ASSERT_EQ(EFAULT, errno);
132#else
133 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
134 // returns ENOTSUP
135 ASSERT_EQ(ENOTSUP, errno);
136#endif
137}
138
139TEST(sys_stat, fchmodat_bad_flags) {
140 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~AT_SYMLINK_NOFOLLOW));
141 ASSERT_EQ(EINVAL, errno);
142}
143
144TEST(sys_stat, fchmodat_bad_flags_ALL) {
145 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~0));
146 ASSERT_EQ(EINVAL, errno);
147}
148
149TEST(sys_stat, fchmodat_nonexistant_file) {
150 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, 0));
151 ASSERT_EQ(ENOENT, errno);
152}
153
154TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_nonexistant_file) {
155 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, AT_SYMLINK_NOFOLLOW));
156#if defined(__BIONIC__)
157 ASSERT_EQ(ENOENT, errno);
158#else
159 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
160 // returns ENOTSUP
161 ASSERT_EQ(ENOTSUP, errno);
162#endif
163}
164
Yabin Cuic6e58742015-03-09 13:55:18 -0700165static void AssertFileModeEquals(mode_t expected_mode, const char* filename) {
166 struct stat sb;
167 ASSERT_EQ(0, stat(filename, &sb));
168 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;
169 ASSERT_EQ(expected_mode & mask, static_cast<mode_t>(sb.st_mode) & mask);
170}
171
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800172TEST(sys_stat, fchmodat_file) {
173 TemporaryFile tf;
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800174
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800175 ASSERT_EQ(0, fchmodat(AT_FDCWD, tf.path, 0751, 0));
176 AssertFileModeEquals(0751, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800177}
178
179TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_file) {
180 TemporaryFile tf;
181 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800182 int result = fchmodat(AT_FDCWD, tf.path, 0751, AT_SYMLINK_NOFOLLOW);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800183
184#if defined(__BIONIC__)
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800185 ASSERT_EQ(0, result);
186 ASSERT_EQ(0, errno);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800187 AssertFileModeEquals(0751, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800188#else
189 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
190 // returns ENOTSUP
191 ASSERT_EQ(-1, result);
192 ASSERT_EQ(ENOTSUP, errno);
193#endif
194}
195
196TEST(sys_stat, fchmodat_symlink) {
197 TemporaryFile tf;
198 char linkname[255];
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800199
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800200 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800201
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800202 ASSERT_EQ(0, symlink(tf.path, linkname));
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800203 ASSERT_EQ(0, fchmodat(AT_FDCWD, linkname, 0751, 0));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800204 AssertFileModeEquals(0751, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800205 unlink(linkname);
206}
207
208TEST(sys_stat, fchmodat_dangling_symlink) {
209 TemporaryFile tf;
210 char linkname[255];
211 char target[255];
212
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800213 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
214 snprintf(target, sizeof(target), "%s.doesnotexist", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800215
216 ASSERT_EQ(0, symlink(target, linkname));
217 ASSERT_EQ(-1, fchmodat(AT_FDCWD, linkname, 0751, 0));
218 ASSERT_EQ(ENOENT, errno);
219 unlink(linkname);
220}
221
Yabin Cuic6e58742015-03-09 13:55:18 -0700222static void AssertSymlinkModeEquals(mode_t expected_mode, const char* linkname) {
223 struct stat sb;
224 ASSERT_EQ(0, fstatat(AT_FDCWD, linkname, &sb, AT_SYMLINK_NOFOLLOW));
225 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;
226 ASSERT_EQ(expected_mode & mask, static_cast<mode_t>(sb.st_mode) & mask);
227}
228
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800229TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_symlink) {
230 TemporaryFile tf;
Yabin Cuic6e58742015-03-09 13:55:18 -0700231 struct stat tf_sb;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800232 ASSERT_EQ(0, stat(tf.path, &tf_sb));
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800233
Yabin Cuic6e58742015-03-09 13:55:18 -0700234 char linkname[255];
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800235 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800236
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800237 ASSERT_EQ(0, symlink(tf.path, linkname));
Yabin Cuic6e58742015-03-09 13:55:18 -0700238 int result = fchmodat(AT_FDCWD, linkname, 0751, AT_SYMLINK_NOFOLLOW);
239 // It depends on the kernel whether chmod operation on symlink is allowed.
240 if (result == 0) {
241 AssertSymlinkModeEquals(0751, linkname);
242 } else {
243 ASSERT_EQ(-1, result);
244 ASSERT_EQ(ENOTSUP, errno);
245 }
246
247 // Target file mode shouldn't be modified.
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800248 AssertFileModeEquals(tf_sb.st_mode, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800249 unlink(linkname);
250}
251
252TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_dangling_symlink) {
253 TemporaryFile tf;
Yabin Cuic6e58742015-03-09 13:55:18 -0700254
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800255 char linkname[255];
256 char target[255];
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800257 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
258 snprintf(target, sizeof(target), "%s.doesnotexist", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800259
260 ASSERT_EQ(0, symlink(target, linkname));
Yabin Cuic6e58742015-03-09 13:55:18 -0700261 int result = fchmodat(AT_FDCWD, linkname, 0751, AT_SYMLINK_NOFOLLOW);
262 // It depends on the kernel whether chmod operation on symlink is allowed.
263 if (result == 0) {
264 AssertSymlinkModeEquals(0751, linkname);
265 } else {
266 ASSERT_EQ(-1, result);
267 ASSERT_EQ(ENOTSUP, errno);
268 }
269
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800270 unlink(linkname);
271}
Nick Kralevich35778252015-02-24 13:40:43 -0800272
273TEST(sys_stat, faccessat_EINVAL) {
274 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", F_OK, ~AT_SYMLINK_NOFOLLOW));
275 ASSERT_EQ(EINVAL, errno);
276#if defined(__BIONIC__)
277 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), 0));
278 ASSERT_EQ(EINVAL, errno);
279#else
280 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), AT_SYMLINK_NOFOLLOW));
281 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), 0));
282 ASSERT_EQ(EINVAL, errno);
283#endif
284}
285
286TEST(sys_stat, faccessat_AT_SYMLINK_NOFOLLOW_EINVAL) {
287#if defined(__BIONIC__)
288 // Android doesn't support AT_SYMLINK_NOFOLLOW
289 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", F_OK, AT_SYMLINK_NOFOLLOW));
290 ASSERT_EQ(EINVAL, errno);
291#else
292 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", F_OK, AT_SYMLINK_NOFOLLOW));
293#endif
294}
295
296TEST(sys_stat, faccessat_dev_null) {
297 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", F_OK, 0));
298 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", R_OK, 0));
299 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", W_OK, 0));
300 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", R_OK|W_OK, 0));
301}
302
303TEST(sys_stat, faccessat_nonexistant) {
304 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/blah", F_OK, AT_SYMLINK_NOFOLLOW));
305#if defined(__BIONIC__)
306 // Android doesn't support AT_SYMLINK_NOFOLLOW
307 ASSERT_EQ(EINVAL, errno);
308#else
309 ASSERT_EQ(ENOENT, errno);
310#endif
311}