blob: 97bf58026a8da29c1767ca79d0865dde73c283a3 [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 Hughesd0be7c82013-08-08 17:13:33 -070025TEST(sys_stat, futimens) {
26 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -070027 ASSERT_TRUE(fp != nullptr);
Elliott Hughesd0be7c82013-08-08 17:13:33 -070028
29 int fd = fileno(fp);
30 ASSERT_NE(fd, -1);
31
32 timespec times[2];
33 times[0].tv_sec = 123;
34 times[0].tv_nsec = 0;
35 times[1].tv_sec = 456;
36 times[1].tv_nsec = 0;
37 ASSERT_EQ(0, futimens(fd, times)) << strerror(errno);
38
39 struct stat sb;
40 ASSERT_EQ(0, fstat(fd, &sb));
41 ASSERT_EQ(times[0].tv_sec, static_cast<long>(sb.st_atime));
42 ASSERT_EQ(times[1].tv_sec, static_cast<long>(sb.st_mtime));
43
44 fclose(fp);
45}
46
47TEST(sys_stat, futimens_EBADF) {
48 timespec times[2];
49 times[0].tv_sec = 123;
50 times[0].tv_nsec = 0;
51 times[1].tv_sec = 456;
52 times[1].tv_nsec = 0;
53 ASSERT_EQ(-1, futimens(-1, times));
54 ASSERT_EQ(EBADF, errno);
55}
Elliott Hughes594b1a42013-10-22 10:54:11 -070056
Elliott Hughesca8e84c2014-10-23 19:10:23 -070057TEST(sys_stat, mkfifo_failure) {
58 errno = 0;
59 ASSERT_EQ(-1, mkfifo("/", 0666));
60 ASSERT_EQ(EEXIST, errno);
61}
62
63TEST(sys_stat, mkfifoat_failure) {
64 errno = 0;
65 ASSERT_EQ(-1, mkfifoat(-2, "x", 0666));
66 ASSERT_EQ(EBADF, errno);
67}
68
Elliott Hughes594b1a42013-10-22 10:54:11 -070069TEST(sys_stat, mkfifo) {
Christopher Ferris528ad742014-09-24 16:01:18 -070070 if (getuid() == 0) {
71 // Racy but probably sufficient way to get a suitable filename.
72 std::string path;
73 {
74 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080075 path = tf.path;
Christopher Ferris528ad742014-09-24 16:01:18 -070076 }
Elliott Hughes594b1a42013-10-22 10:54:11 -070077
Christopher Ferris528ad742014-09-24 16:01:18 -070078 ASSERT_EQ(0, mkfifo(path.c_str(), 0666));
79 struct stat sb;
80 ASSERT_EQ(0, stat(path.c_str(), &sb));
81 ASSERT_TRUE(S_ISFIFO(sb.st_mode));
82 unlink(path.c_str());
83 } else {
Elliott Hughesca8e84c2014-10-23 19:10:23 -070084 // SELinux policy forbids us from creating FIFOs. http://b/17646702.
Elliott Hughesbcaa4542019-03-08 15:20:23 -080085 GTEST_SKIP() << "SELinux policy forbids non-root from creating FIFOs";
Christopher Ferris528ad742014-09-24 16:01:18 -070086 }
Elliott Hughes594b1a42013-10-22 10:54:11 -070087}
Elliott Hughesdb1ea342014-01-17 18:42:49 -080088
89TEST(sys_stat, stat64_lstat64_fstat64) {
90 struct stat64 sb;
91 ASSERT_EQ(0, stat64("/proc/version", &sb));
92 ASSERT_EQ(0, lstat64("/proc/version", &sb));
93 int fd = open("/proc/version", O_RDONLY);
94 ASSERT_EQ(0, fstat64(fd, &sb));
95 close(fd);
96}
Nick Kralevich3cbc6c62015-01-31 19:57:46 -080097
98TEST(sys_stat, fchmodat_EFAULT_file) {
99 ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, 0));
100 ASSERT_EQ(EFAULT, errno);
101}
102
103TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_EFAULT_file) {
104 ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, AT_SYMLINK_NOFOLLOW));
105#if defined(__BIONIC__)
106 ASSERT_EQ(EFAULT, errno);
107#else
108 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
109 // returns ENOTSUP
110 ASSERT_EQ(ENOTSUP, errno);
111#endif
112}
113
114TEST(sys_stat, fchmodat_bad_flags) {
115 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~AT_SYMLINK_NOFOLLOW));
116 ASSERT_EQ(EINVAL, errno);
117}
118
119TEST(sys_stat, fchmodat_bad_flags_ALL) {
120 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~0));
121 ASSERT_EQ(EINVAL, errno);
122}
123
124TEST(sys_stat, fchmodat_nonexistant_file) {
125 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, 0));
126 ASSERT_EQ(ENOENT, errno);
127}
128
129TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_nonexistant_file) {
130 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, AT_SYMLINK_NOFOLLOW));
131#if defined(__BIONIC__)
132 ASSERT_EQ(ENOENT, errno);
133#else
134 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
135 // returns ENOTSUP
136 ASSERT_EQ(ENOTSUP, errno);
137#endif
138}
139
Yabin Cuic6e58742015-03-09 13:55:18 -0700140static void AssertFileModeEquals(mode_t expected_mode, const char* filename) {
141 struct stat sb;
142 ASSERT_EQ(0, stat(filename, &sb));
143 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;
144 ASSERT_EQ(expected_mode & mask, static_cast<mode_t>(sb.st_mode) & mask);
145}
146
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800147TEST(sys_stat, fchmodat_file) {
148 TemporaryFile tf;
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800149
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800150 ASSERT_EQ(0, fchmodat(AT_FDCWD, tf.path, 0751, 0));
151 AssertFileModeEquals(0751, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800152}
153
154TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_file) {
155 TemporaryFile tf;
156 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800157 int result = fchmodat(AT_FDCWD, tf.path, 0751, AT_SYMLINK_NOFOLLOW);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800158
159#if defined(__BIONIC__)
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800160 ASSERT_EQ(0, result);
161 ASSERT_EQ(0, errno);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800162 AssertFileModeEquals(0751, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800163#else
164 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
165 // returns ENOTSUP
166 ASSERT_EQ(-1, result);
167 ASSERT_EQ(ENOTSUP, errno);
168#endif
169}
170
171TEST(sys_stat, fchmodat_symlink) {
172 TemporaryFile tf;
173 char linkname[255];
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800174
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800175 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800176
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800177 ASSERT_EQ(0, symlink(tf.path, linkname));
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800178 ASSERT_EQ(0, fchmodat(AT_FDCWD, linkname, 0751, 0));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800179 AssertFileModeEquals(0751, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800180 unlink(linkname);
181}
182
183TEST(sys_stat, fchmodat_dangling_symlink) {
184 TemporaryFile tf;
185 char linkname[255];
186 char target[255];
187
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800188 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
189 snprintf(target, sizeof(target), "%s.doesnotexist", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800190
191 ASSERT_EQ(0, symlink(target, linkname));
192 ASSERT_EQ(-1, fchmodat(AT_FDCWD, linkname, 0751, 0));
193 ASSERT_EQ(ENOENT, errno);
194 unlink(linkname);
195}
196
Yabin Cuic6e58742015-03-09 13:55:18 -0700197static void AssertSymlinkModeEquals(mode_t expected_mode, const char* linkname) {
198 struct stat sb;
199 ASSERT_EQ(0, fstatat(AT_FDCWD, linkname, &sb, AT_SYMLINK_NOFOLLOW));
200 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;
201 ASSERT_EQ(expected_mode & mask, static_cast<mode_t>(sb.st_mode) & mask);
202}
203
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800204TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_symlink) {
205 TemporaryFile tf;
Yabin Cuic6e58742015-03-09 13:55:18 -0700206 struct stat tf_sb;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800207 ASSERT_EQ(0, stat(tf.path, &tf_sb));
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800208
Yabin Cuic6e58742015-03-09 13:55:18 -0700209 char linkname[255];
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800210 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800211
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800212 ASSERT_EQ(0, symlink(tf.path, linkname));
Yabin Cuic6e58742015-03-09 13:55:18 -0700213 int result = fchmodat(AT_FDCWD, linkname, 0751, AT_SYMLINK_NOFOLLOW);
214 // It depends on the kernel whether chmod operation on symlink is allowed.
215 if (result == 0) {
216 AssertSymlinkModeEquals(0751, linkname);
217 } else {
218 ASSERT_EQ(-1, result);
219 ASSERT_EQ(ENOTSUP, errno);
220 }
221
222 // Target file mode shouldn't be modified.
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800223 AssertFileModeEquals(tf_sb.st_mode, tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800224 unlink(linkname);
225}
226
227TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_dangling_symlink) {
228 TemporaryFile tf;
Yabin Cuic6e58742015-03-09 13:55:18 -0700229
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800230 char linkname[255];
231 char target[255];
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800232 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
233 snprintf(target, sizeof(target), "%s.doesnotexist", tf.path);
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800234
235 ASSERT_EQ(0, symlink(target, linkname));
Yabin Cuic6e58742015-03-09 13:55:18 -0700236 int result = fchmodat(AT_FDCWD, linkname, 0751, AT_SYMLINK_NOFOLLOW);
237 // It depends on the kernel whether chmod operation on symlink is allowed.
238 if (result == 0) {
239 AssertSymlinkModeEquals(0751, linkname);
240 } else {
241 ASSERT_EQ(-1, result);
242 ASSERT_EQ(ENOTSUP, errno);
243 }
244
Nick Kralevich3cbc6c62015-01-31 19:57:46 -0800245 unlink(linkname);
246}
Nick Kralevich35778252015-02-24 13:40:43 -0800247
248TEST(sys_stat, faccessat_EINVAL) {
249 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", F_OK, ~AT_SYMLINK_NOFOLLOW));
250 ASSERT_EQ(EINVAL, errno);
251#if defined(__BIONIC__)
252 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), 0));
253 ASSERT_EQ(EINVAL, errno);
254#else
255 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), AT_SYMLINK_NOFOLLOW));
256 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), 0));
257 ASSERT_EQ(EINVAL, errno);
258#endif
259}
260
261TEST(sys_stat, faccessat_AT_SYMLINK_NOFOLLOW_EINVAL) {
262#if defined(__BIONIC__)
263 // Android doesn't support AT_SYMLINK_NOFOLLOW
264 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", F_OK, AT_SYMLINK_NOFOLLOW));
265 ASSERT_EQ(EINVAL, errno);
266#else
267 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", F_OK, AT_SYMLINK_NOFOLLOW));
268#endif
269}
270
271TEST(sys_stat, faccessat_dev_null) {
272 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", F_OK, 0));
273 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", R_OK, 0));
274 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", W_OK, 0));
275 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", R_OK|W_OK, 0));
276}
277
278TEST(sys_stat, faccessat_nonexistant) {
279 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/blah", F_OK, AT_SYMLINK_NOFOLLOW));
280#if defined(__BIONIC__)
281 // Android doesn't support AT_SYMLINK_NOFOLLOW
282 ASSERT_EQ(EINVAL, errno);
283#else
284 ASSERT_EQ(ENOENT, errno);
285#endif
286}