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
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <stdlib.h>
20 #include <sys/stat.h>
21
22 #include <android-base/file.h>
23 #include <gtest/gtest.h>
24
25 #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
TEST(sys_stat,futimens)33 TEST(sys_stat, futimens) {
34 FILE* fp = tmpfile();
35 ASSERT_TRUE(fp != nullptr);
36
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
TEST(sys_stat,futimens_EBADF)55 TEST(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 }
64
TEST(sys_stat,mkfifo_failure)65 TEST(sys_stat, mkfifo_failure) {
66 errno = 0;
67 ASSERT_EQ(-1, mkfifo("/", 0666));
68 ASSERT_EQ(EEXIST, errno);
69 }
70
TEST(sys_stat,mkfifoat_failure)71 TEST(sys_stat, mkfifoat_failure) {
72 errno = 0;
73 ASSERT_EQ(-1, mkfifoat(-2, "x", 0666));
74 ASSERT_EQ(EBADF, errno);
75 }
76
TEST(sys_stat,mkfifo)77 TEST(sys_stat, mkfifo) {
78 if (getuid() == 0) {
79 // Racy but probably sufficient way to get a suitable filename.
80 std::string path;
81 {
82 TemporaryFile tf;
83 path = tf.path;
84 }
85
86 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 {
92 // SELinux policy forbids us from creating FIFOs. http://b/17646702.
93 GTEST_SKIP() << "SELinux policy forbids non-root from creating FIFOs";
94 }
95 }
96
TEST(sys_stat,stat64_lstat64_fstat64)97 TEST(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 }
105
TEST(sys_stat,statx)106 TEST(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";
112 }
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
TEST(sys_stat,fchmod_EBADF)123 TEST(sys_stat, fchmod_EBADF) {
124 ASSERT_EQ(-1, fchmod(-1, 0751));
125 ASSERT_EQ(EBADF, errno);
126 }
127
TEST(sys_stat,fchmodat_EFAULT_file)128 TEST(sys_stat, fchmodat_EFAULT_file) {
129 ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, 0));
130 ASSERT_EQ(EFAULT, errno);
131 }
132
TEST(sys_stat,fchmodat_AT_SYMLINK_NOFOLLOW_EFAULT_file)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__)
136 ASSERT_EQ(EFAULT, errno);
137 #else
138 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
139 // returns ENOTSUP
140 ASSERT_EQ(ENOTSUP, errno);
141 #endif
142 }
143
TEST(sys_stat,fchmodat_bad_flags)144 TEST(sys_stat, fchmodat_bad_flags) {
145 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~AT_SYMLINK_NOFOLLOW));
146 ASSERT_EQ(EINVAL, errno);
147 }
148
TEST(sys_stat,fchmodat_bad_flags_ALL)149 TEST(sys_stat, fchmodat_bad_flags_ALL) {
150 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~0));
151 ASSERT_EQ(EINVAL, errno);
152 }
153
TEST(sys_stat,fchmodat_nonexistant_file)154 TEST(sys_stat, fchmodat_nonexistant_file) {
155 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, 0));
156 ASSERT_EQ(ENOENT, errno);
157 }
158
TEST(sys_stat,fchmodat_AT_SYMLINK_NOFOLLOW_nonexistant_file)159 TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_nonexistant_file) {
160 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, AT_SYMLINK_NOFOLLOW));
161 #if defined(__BIONIC__)
162 ASSERT_EQ(ENOENT, errno);
163 #else
164 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
165 // returns ENOTSUP
166 ASSERT_EQ(ENOTSUP, errno);
167 #endif
168 }
169
AssertFileModeEquals(mode_t expected_mode,const char * filename)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
TEST(sys_stat,fchmodat_file)177 TEST(sys_stat, fchmodat_file) {
178 TemporaryFile tf;
179
180 ASSERT_EQ(0, fchmodat(AT_FDCWD, tf.path, 0751, 0));
181 AssertFileModeEquals(0751, tf.path);
182 }
183
TEST(sys_stat,fchmodat_AT_SYMLINK_NOFOLLOW_file)184 TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_file) {
185 TemporaryFile tf;
186 errno = 0;
187 int result = fchmodat(AT_FDCWD, tf.path, 0751, AT_SYMLINK_NOFOLLOW);
188
189 #if defined(__BIONIC__)
190 ASSERT_EQ(0, result);
191 ASSERT_EQ(0, errno);
192 AssertFileModeEquals(0751, tf.path);
193 #else
194 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
195 // returns ENOTSUP
196 ASSERT_EQ(-1, result);
197 ASSERT_EQ(ENOTSUP, errno);
198 #endif
199 }
200
TEST(sys_stat,fchmodat_symlink)201 TEST(sys_stat, fchmodat_symlink) {
202 TemporaryFile tf;
203 char linkname[255];
204
205 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
206
207 ASSERT_EQ(0, symlink(tf.path, linkname));
208 ASSERT_EQ(0, fchmodat(AT_FDCWD, linkname, 0751, 0));
209 AssertFileModeEquals(0751, tf.path);
210 unlink(linkname);
211 }
212
TEST(sys_stat,fchmodat_dangling_symlink)213 TEST(sys_stat, fchmodat_dangling_symlink) {
214 TemporaryFile tf;
215 char linkname[255];
216 char target[255];
217
218 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
219 snprintf(target, sizeof(target), "%s.doesnotexist", tf.path);
220
221 ASSERT_EQ(0, symlink(target, linkname));
222 ASSERT_EQ(-1, fchmodat(AT_FDCWD, linkname, 0751, 0));
223 ASSERT_EQ(ENOENT, errno);
224 unlink(linkname);
225 }
226
AssertSymlinkModeEquals(mode_t expected_mode,const char * linkname)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
TEST(sys_stat,fchmodat_AT_SYMLINK_NOFOLLOW_with_symlink)234 TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_symlink) {
235 TemporaryFile tf;
236 struct stat tf_sb;
237 ASSERT_EQ(0, stat(tf.path, &tf_sb));
238
239 char linkname[255];
240 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
241
242 ASSERT_EQ(0, symlink(tf.path, linkname));
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);
249 ASSERT_EQ(ENOTSUP, errno);
250 }
251
252 // Target file mode shouldn't be modified.
253 AssertFileModeEquals(tf_sb.st_mode, tf.path);
254 unlink(linkname);
255 }
256
TEST(sys_stat,fchmodat_AT_SYMLINK_NOFOLLOW_with_dangling_symlink)257 TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_dangling_symlink) {
258 TemporaryFile tf;
259
260 char linkname[255];
261 char target[255];
262 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
263 snprintf(target, sizeof(target), "%s.doesnotexist", tf.path);
264
265 ASSERT_EQ(0, symlink(target, linkname));
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);
272 ASSERT_EQ(ENOTSUP, errno);
273 }
274
275 unlink(linkname);
276 }
277
TEST(sys_stat,faccessat_EINVAL)278 TEST(sys_stat, faccessat_EINVAL) {
279 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", F_OK, ~AT_SYMLINK_NOFOLLOW));
280 ASSERT_EQ(EINVAL, errno);
281 #if defined(__BIONIC__)
282 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), 0));
283 ASSERT_EQ(EINVAL, errno);
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));
287 ASSERT_EQ(EINVAL, errno);
288 #endif
289 }
290
TEST(sys_stat,faccessat_AT_SYMLINK_NOFOLLOW_EINVAL)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));
295 ASSERT_EQ(EINVAL, errno);
296 #else
297 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", F_OK, AT_SYMLINK_NOFOLLOW));
298 #endif
299 }
300
TEST(sys_stat,faccessat_dev_null)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
TEST(sys_stat,faccessat_nonexistant)308 TEST(sys_stat, faccessat_nonexistant) {
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
312 ASSERT_EQ(EINVAL, errno);
313 #else
314 ASSERT_EQ(ENOENT, errno);
315 #endif
316 }
317