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
TEST(sys_stat,futimens)25 TEST(sys_stat, futimens) {
26 FILE* fp = tmpfile();
27 ASSERT_TRUE(fp != nullptr);
28
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
TEST(sys_stat,futimens_EBADF)47 TEST(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 }
56
TEST(sys_stat,mkfifo_failure)57 TEST(sys_stat, mkfifo_failure) {
58 errno = 0;
59 ASSERT_EQ(-1, mkfifo("/", 0666));
60 ASSERT_EQ(EEXIST, errno);
61 }
62
TEST(sys_stat,mkfifoat_failure)63 TEST(sys_stat, mkfifoat_failure) {
64 errno = 0;
65 ASSERT_EQ(-1, mkfifoat(-2, "x", 0666));
66 ASSERT_EQ(EBADF, errno);
67 }
68
TEST(sys_stat,mkfifo)69 TEST(sys_stat, mkfifo) {
70 if (getuid() == 0) {
71 // Racy but probably sufficient way to get a suitable filename.
72 std::string path;
73 {
74 TemporaryFile tf;
75 path = tf.path;
76 }
77
78 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 {
84 // SELinux policy forbids us from creating FIFOs. http://b/17646702.
85 GTEST_SKIP() << "SELinux policy forbids non-root from creating FIFOs";
86 }
87 }
88
TEST(sys_stat,stat64_lstat64_fstat64)89 TEST(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 }
97
TEST(sys_stat,fchmodat_EFAULT_file)98 TEST(sys_stat, fchmodat_EFAULT_file) {
99 ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, 0));
100 ASSERT_EQ(EFAULT, errno);
101 }
102
TEST(sys_stat,fchmodat_AT_SYMLINK_NOFOLLOW_EFAULT_file)103 TEST(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
TEST(sys_stat,fchmodat_bad_flags)114 TEST(sys_stat, fchmodat_bad_flags) {
115 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~AT_SYMLINK_NOFOLLOW));
116 ASSERT_EQ(EINVAL, errno);
117 }
118
TEST(sys_stat,fchmodat_bad_flags_ALL)119 TEST(sys_stat, fchmodat_bad_flags_ALL) {
120 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~0));
121 ASSERT_EQ(EINVAL, errno);
122 }
123
TEST(sys_stat,fchmodat_nonexistant_file)124 TEST(sys_stat, fchmodat_nonexistant_file) {
125 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, 0));
126 ASSERT_EQ(ENOENT, errno);
127 }
128
TEST(sys_stat,fchmodat_AT_SYMLINK_NOFOLLOW_nonexistant_file)129 TEST(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
AssertFileModeEquals(mode_t expected_mode,const char * filename)140 static 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
TEST(sys_stat,fchmodat_file)147 TEST(sys_stat, fchmodat_file) {
148 TemporaryFile tf;
149
150 ASSERT_EQ(0, fchmodat(AT_FDCWD, tf.path, 0751, 0));
151 AssertFileModeEquals(0751, tf.path);
152 }
153
TEST(sys_stat,fchmodat_AT_SYMLINK_NOFOLLOW_file)154 TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_file) {
155 TemporaryFile tf;
156 errno = 0;
157 int result = fchmodat(AT_FDCWD, tf.path, 0751, AT_SYMLINK_NOFOLLOW);
158
159 #if defined(__BIONIC__)
160 ASSERT_EQ(0, result);
161 ASSERT_EQ(0, errno);
162 AssertFileModeEquals(0751, tf.path);
163 #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
TEST(sys_stat,fchmodat_symlink)171 TEST(sys_stat, fchmodat_symlink) {
172 TemporaryFile tf;
173 char linkname[255];
174
175 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
176
177 ASSERT_EQ(0, symlink(tf.path, linkname));
178 ASSERT_EQ(0, fchmodat(AT_FDCWD, linkname, 0751, 0));
179 AssertFileModeEquals(0751, tf.path);
180 unlink(linkname);
181 }
182
TEST(sys_stat,fchmodat_dangling_symlink)183 TEST(sys_stat, fchmodat_dangling_symlink) {
184 TemporaryFile tf;
185 char linkname[255];
186 char target[255];
187
188 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
189 snprintf(target, sizeof(target), "%s.doesnotexist", tf.path);
190
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
AssertSymlinkModeEquals(mode_t expected_mode,const char * linkname)197 static 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
TEST(sys_stat,fchmodat_AT_SYMLINK_NOFOLLOW_with_symlink)204 TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_symlink) {
205 TemporaryFile tf;
206 struct stat tf_sb;
207 ASSERT_EQ(0, stat(tf.path, &tf_sb));
208
209 char linkname[255];
210 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
211
212 ASSERT_EQ(0, symlink(tf.path, linkname));
213 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.
223 AssertFileModeEquals(tf_sb.st_mode, tf.path);
224 unlink(linkname);
225 }
226
TEST(sys_stat,fchmodat_AT_SYMLINK_NOFOLLOW_with_dangling_symlink)227 TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_dangling_symlink) {
228 TemporaryFile tf;
229
230 char linkname[255];
231 char target[255];
232 snprintf(linkname, sizeof(linkname), "%s.link", tf.path);
233 snprintf(target, sizeof(target), "%s.doesnotexist", tf.path);
234
235 ASSERT_EQ(0, symlink(target, linkname));
236 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
245 unlink(linkname);
246 }
247
TEST(sys_stat,faccessat_EINVAL)248 TEST(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
TEST(sys_stat,faccessat_AT_SYMLINK_NOFOLLOW_EINVAL)261 TEST(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
TEST(sys_stat,faccessat_dev_null)271 TEST(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
TEST(sys_stat,faccessat_nonexistant)278 TEST(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 }
287