blob: 28c7c526289491a6f247ed816c4623b61ffb670b [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
17#include <gtest/gtest.h>
18
19#include <errno.h>
Elliott Hughesdb1ea342014-01-17 18:42:49 -080020#include <fcntl.h>
Elliott Hughesd0be7c82013-08-08 17:13:33 -070021#include <stdlib.h>
22#include <sys/stat.h>
23
Elliott Hughes594b1a42013-10-22 10:54:11 -070024#include "TemporaryFile.h"
25
Elliott Hughesd0be7c82013-08-08 17:13:33 -070026TEST(sys_stat, futimens) {
27 FILE* fp = tmpfile();
28 ASSERT_TRUE(fp != NULL);
29
30 int fd = fileno(fp);
31 ASSERT_NE(fd, -1);
32
33 timespec times[2];
34 times[0].tv_sec = 123;
35 times[0].tv_nsec = 0;
36 times[1].tv_sec = 456;
37 times[1].tv_nsec = 0;
38 ASSERT_EQ(0, futimens(fd, times)) << strerror(errno);
39
40 struct stat sb;
41 ASSERT_EQ(0, fstat(fd, &sb));
42 ASSERT_EQ(times[0].tv_sec, static_cast<long>(sb.st_atime));
43 ASSERT_EQ(times[1].tv_sec, static_cast<long>(sb.st_mtime));
44
45 fclose(fp);
46}
47
48TEST(sys_stat, futimens_EBADF) {
49 timespec times[2];
50 times[0].tv_sec = 123;
51 times[0].tv_nsec = 0;
52 times[1].tv_sec = 456;
53 times[1].tv_nsec = 0;
54 ASSERT_EQ(-1, futimens(-1, times));
55 ASSERT_EQ(EBADF, errno);
56}
Elliott Hughes594b1a42013-10-22 10:54:11 -070057
Elliott Hughesca8e84c2014-10-23 19:10:23 -070058TEST(sys_stat, mkfifo_failure) {
59 errno = 0;
60 ASSERT_EQ(-1, mkfifo("/", 0666));
61 ASSERT_EQ(EEXIST, errno);
62}
63
64TEST(sys_stat, mkfifoat_failure) {
65 errno = 0;
66 ASSERT_EQ(-1, mkfifoat(-2, "x", 0666));
67 ASSERT_EQ(EBADF, errno);
68}
69
Elliott Hughes594b1a42013-10-22 10:54:11 -070070TEST(sys_stat, mkfifo) {
Christopher Ferris528ad742014-09-24 16:01:18 -070071 if (getuid() == 0) {
72 // Racy but probably sufficient way to get a suitable filename.
73 std::string path;
74 {
75 TemporaryFile tf;
76 path = tf.filename;
77 }
Elliott Hughes594b1a42013-10-22 10:54:11 -070078
Christopher Ferris528ad742014-09-24 16:01:18 -070079 ASSERT_EQ(0, mkfifo(path.c_str(), 0666));
80 struct stat sb;
81 ASSERT_EQ(0, stat(path.c_str(), &sb));
82 ASSERT_TRUE(S_ISFIFO(sb.st_mode));
83 unlink(path.c_str());
84 } else {
Elliott Hughesca8e84c2014-10-23 19:10:23 -070085 // SELinux policy forbids us from creating FIFOs. http://b/17646702.
Christopher Ferris528ad742014-09-24 16:01:18 -070086 GTEST_LOG_(INFO) << "This test only performs a test when run as root.";
87 }
Elliott Hughes594b1a42013-10-22 10:54:11 -070088}
Elliott Hughesdb1ea342014-01-17 18:42:49 -080089
90TEST(sys_stat, stat64_lstat64_fstat64) {
91 struct stat64 sb;
92 ASSERT_EQ(0, stat64("/proc/version", &sb));
93 ASSERT_EQ(0, lstat64("/proc/version", &sb));
94 int fd = open("/proc/version", O_RDONLY);
95 ASSERT_EQ(0, fstat64(fd, &sb));
96 close(fd);
97}
Nick Kralevich3cbc6c62015-01-31 19:57:46 -080098
99TEST(sys_stat, fchmodat_EFAULT_file) {
100 ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, 0));
101 ASSERT_EQ(EFAULT, errno);
102}
103
104TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_EFAULT_file) {
105 ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, AT_SYMLINK_NOFOLLOW));
106#if defined(__BIONIC__)
107 ASSERT_EQ(EFAULT, errno);
108#else
109 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
110 // returns ENOTSUP
111 ASSERT_EQ(ENOTSUP, errno);
112#endif
113}
114
115TEST(sys_stat, fchmodat_bad_flags) {
116 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~AT_SYMLINK_NOFOLLOW));
117 ASSERT_EQ(EINVAL, errno);
118}
119
120TEST(sys_stat, fchmodat_bad_flags_ALL) {
121 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~0));
122 ASSERT_EQ(EINVAL, errno);
123}
124
125TEST(sys_stat, fchmodat_nonexistant_file) {
126 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, 0));
127 ASSERT_EQ(ENOENT, errno);
128}
129
130TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_nonexistant_file) {
131 ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, AT_SYMLINK_NOFOLLOW));
132#if defined(__BIONIC__)
133 ASSERT_EQ(ENOENT, errno);
134#else
135 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
136 // returns ENOTSUP
137 ASSERT_EQ(ENOTSUP, errno);
138#endif
139}
140
141TEST(sys_stat, fchmodat_file) {
142 TemporaryFile tf;
143 struct stat sb;
144
145 ASSERT_EQ(0, fchmodat(AT_FDCWD, tf.filename, 0751, 0));
146 ASSERT_EQ(0, fstat(tf.fd, &sb));
147 ASSERT_TRUE(0751 == (sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)));
148}
149
150TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_file) {
151 TemporaryFile tf;
152 errno = 0;
153 int result = fchmodat(AT_FDCWD, tf.filename, 0751, AT_SYMLINK_NOFOLLOW);
154
155#if defined(__BIONIC__)
156 struct stat sb;
157 ASSERT_EQ(0, result);
158 ASSERT_EQ(0, errno);
159 ASSERT_EQ(0, fstat(tf.fd, &sb));
160 ASSERT_TRUE(0751 == (sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)));
161#else
162 // glibc 2.19 does not implement AT_SYMLINK_NOFOLLOW and always
163 // returns ENOTSUP
164 ASSERT_EQ(-1, result);
165 ASSERT_EQ(ENOTSUP, errno);
166#endif
167}
168
169TEST(sys_stat, fchmodat_symlink) {
170 TemporaryFile tf;
171 char linkname[255];
172 struct stat sb;
173
174 snprintf(linkname, sizeof(linkname), "%s.link", tf.filename);
175
176 ASSERT_EQ(0, symlink(tf.filename, linkname));
177 ASSERT_EQ(0, fchmodat(AT_FDCWD, linkname, 0751, 0));
178 ASSERT_EQ(0, fstat(tf.fd, &sb));
179 ASSERT_TRUE(0751 == (sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)));
180 unlink(linkname);
181}
182
183TEST(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.filename);
189 snprintf(target, sizeof(target), "%s.doesnotexist", tf.filename);
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
197TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_symlink) {
198 TemporaryFile tf;
199 char linkname[255];
200
201 snprintf(linkname, sizeof(linkname), "%s.link", tf.filename);
202
203 ASSERT_EQ(0, symlink(tf.filename, linkname));
204 ASSERT_EQ(-1, fchmodat(AT_FDCWD, linkname, 0751, AT_SYMLINK_NOFOLLOW));
205 ASSERT_EQ(ENOTSUP, errno);
206 unlink(linkname);
207}
208
209TEST(sys_stat, fchmodat_AT_SYMLINK_NOFOLLOW_with_dangling_symlink) {
210 TemporaryFile tf;
211 char linkname[255];
212 char target[255];
213
214 snprintf(linkname, sizeof(linkname), "%s.link", tf.filename);
215 snprintf(target, sizeof(target), "%s.doesnotexist", tf.filename);
216
217 ASSERT_EQ(0, symlink(target, linkname));
218 ASSERT_EQ(-1, fchmodat(AT_FDCWD, linkname, 0751, AT_SYMLINK_NOFOLLOW));
219 ASSERT_EQ(ENOTSUP, errno);
220 unlink(linkname);
221}
Nick Kralevich35778252015-02-24 13:40:43 -0800222
223TEST(sys_stat, faccessat_EINVAL) {
224 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", F_OK, ~AT_SYMLINK_NOFOLLOW));
225 ASSERT_EQ(EINVAL, errno);
226#if defined(__BIONIC__)
227 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), 0));
228 ASSERT_EQ(EINVAL, errno);
229#else
230 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), AT_SYMLINK_NOFOLLOW));
231 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", ~(R_OK | W_OK | X_OK), 0));
232 ASSERT_EQ(EINVAL, errno);
233#endif
234}
235
236TEST(sys_stat, faccessat_AT_SYMLINK_NOFOLLOW_EINVAL) {
237#if defined(__BIONIC__)
238 // Android doesn't support AT_SYMLINK_NOFOLLOW
239 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/dev/null", F_OK, AT_SYMLINK_NOFOLLOW));
240 ASSERT_EQ(EINVAL, errno);
241#else
242 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", F_OK, AT_SYMLINK_NOFOLLOW));
243#endif
244}
245
246TEST(sys_stat, faccessat_dev_null) {
247 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", F_OK, 0));
248 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", R_OK, 0));
249 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", W_OK, 0));
250 ASSERT_EQ(0, faccessat(AT_FDCWD, "/dev/null", R_OK|W_OK, 0));
251}
252
253TEST(sys_stat, faccessat_nonexistant) {
254 ASSERT_EQ(-1, faccessat(AT_FDCWD, "/blah", F_OK, AT_SYMLINK_NOFOLLOW));
255#if defined(__BIONIC__)
256 // Android doesn't support AT_SYMLINK_NOFOLLOW
257 ASSERT_EQ(EINVAL, errno);
258#else
259 ASSERT_EQ(ENOENT, errno);
260#endif
261}