Elliott Hughes | d0be7c8 | 2013-08-08 17:13:33 -0700 | [diff] [blame] | 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 <gtest/gtest.h> |
| 18 | |
| 19 | #include <errno.h> |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 20 | #include <fcntl.h> |
Elliott Hughes | d0be7c8 | 2013-08-08 17:13:33 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <sys/stat.h> |
| 23 | |
Elliott Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 24 | #include "TemporaryFile.h" |
| 25 | |
Elliott Hughes | d0be7c8 | 2013-08-08 17:13:33 -0700 | [diff] [blame] | 26 | TEST(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 | |
| 48 | TEST(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 Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 57 | |
Elliott Hughes | ca8e84c | 2014-10-23 19:10:23 -0700 | [diff] [blame] | 58 | TEST(sys_stat, mkfifo_failure) { |
| 59 | errno = 0; |
| 60 | ASSERT_EQ(-1, mkfifo("/", 0666)); |
| 61 | ASSERT_EQ(EEXIST, errno); |
| 62 | } |
| 63 | |
| 64 | TEST(sys_stat, mkfifoat_failure) { |
| 65 | errno = 0; |
| 66 | ASSERT_EQ(-1, mkfifoat(-2, "x", 0666)); |
| 67 | ASSERT_EQ(EBADF, errno); |
| 68 | } |
| 69 | |
Elliott Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 70 | TEST(sys_stat, mkfifo) { |
Christopher Ferris | 528ad74 | 2014-09-24 16:01:18 -0700 | [diff] [blame] | 71 | 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 Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 78 | |
Christopher Ferris | 528ad74 | 2014-09-24 16:01:18 -0700 | [diff] [blame] | 79 | 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 Hughes | ca8e84c | 2014-10-23 19:10:23 -0700 | [diff] [blame] | 85 | // SELinux policy forbids us from creating FIFOs. http://b/17646702. |
Christopher Ferris | 528ad74 | 2014-09-24 16:01:18 -0700 | [diff] [blame] | 86 | GTEST_LOG_(INFO) << "This test only performs a test when run as root."; |
| 87 | } |
Elliott Hughes | 594b1a4 | 2013-10-22 10:54:11 -0700 | [diff] [blame] | 88 | } |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 89 | |
| 90 | TEST(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 Kralevich | 3cbc6c6 | 2015-01-31 19:57:46 -0800 | [diff] [blame] | 98 | |
| 99 | TEST(sys_stat, fchmodat_EFAULT_file) { |
| 100 | ASSERT_EQ(-1, fchmodat(AT_FDCWD, (char *) 0x1, 0751, 0)); |
| 101 | ASSERT_EQ(EFAULT, errno); |
| 102 | } |
| 103 | |
| 104 | TEST(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 | |
| 115 | TEST(sys_stat, fchmodat_bad_flags) { |
| 116 | ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~AT_SYMLINK_NOFOLLOW)); |
| 117 | ASSERT_EQ(EINVAL, errno); |
| 118 | } |
| 119 | |
| 120 | TEST(sys_stat, fchmodat_bad_flags_ALL) { |
| 121 | ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, ~0)); |
| 122 | ASSERT_EQ(EINVAL, errno); |
| 123 | } |
| 124 | |
| 125 | TEST(sys_stat, fchmodat_nonexistant_file) { |
| 126 | ASSERT_EQ(-1, fchmodat(AT_FDCWD, "/blah", 0751, 0)); |
| 127 | ASSERT_EQ(ENOENT, errno); |
| 128 | } |
| 129 | |
| 130 | TEST(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 | |
| 141 | TEST(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 | |
| 150 | TEST(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 | |
| 169 | TEST(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 | |
| 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.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 | |
| 197 | TEST(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 | |
| 209 | TEST(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 | } |