Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [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> |
| 20 | #include <fcntl.h> |
| 21 | |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 22 | #include "TemporaryFile.h" |
| 23 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 24 | TEST(fcntl, fcntl_smoke) { |
| 25 | int fd = open("/proc/version", O_RDONLY); |
| 26 | ASSERT_TRUE(fd != -1); |
| 27 | |
| 28 | int flags = fcntl(fd, F_GETFD); |
| 29 | ASSERT_TRUE(flags != -1); |
| 30 | ASSERT_EQ(0, flags & FD_CLOEXEC); |
| 31 | |
| 32 | int rc = fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 33 | ASSERT_EQ(0, rc); |
| 34 | |
| 35 | flags = fcntl(fd, F_GETFD); |
| 36 | ASSERT_TRUE(flags != -1); |
| 37 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 38 | |
| 39 | close(fd); |
| 40 | } |
| 41 | |
| 42 | TEST(fcntl, open_open64) { |
| 43 | int fd; |
| 44 | |
| 45 | fd = open("/proc/version", O_RDONLY); |
| 46 | ASSERT_TRUE(fd != -1); |
| 47 | close(fd); |
| 48 | |
| 49 | fd = open64("/proc/version", O_RDONLY); |
| 50 | ASSERT_TRUE(fd != -1); |
| 51 | close(fd); |
| 52 | } |
| 53 | |
| 54 | TEST(fcntl, openat_openat64) { |
| 55 | int fd; |
| 56 | |
| 57 | fd = openat(AT_FDCWD, "/proc/version", O_RDONLY); |
| 58 | ASSERT_TRUE(fd != -1); |
| 59 | close(fd); |
| 60 | |
| 61 | fd = openat64(AT_FDCWD, "/proc/version", O_RDONLY); |
| 62 | ASSERT_TRUE(fd != -1); |
| 63 | close(fd); |
| 64 | } |
| 65 | |
| 66 | TEST(fcntl, creat_creat64) { |
| 67 | ASSERT_EQ(-1, creat("", 0666)); |
| 68 | ASSERT_EQ(ENOENT, errno); |
| 69 | ASSERT_EQ(-1, creat64("", 0666)); |
| 70 | ASSERT_EQ(ENOENT, errno); |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 71 | } |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 72 | |
| 73 | TEST(fcntl, fallocate_EINVAL) { |
| 74 | TemporaryFile tf; |
| 75 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 76 | #if defined(__BIONIC__) |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 77 | errno = 0; |
| 78 | ASSERT_EQ(-1, fallocate(tf.fd, 0, 0, -1)); |
| 79 | ASSERT_EQ(EINVAL, errno); |
| 80 | |
| 81 | errno = 0; |
| 82 | ASSERT_EQ(-1, fallocate64(tf.fd, 0, 0, -1)); |
| 83 | ASSERT_EQ(EINVAL, errno); |
| 84 | #endif |
| 85 | |
| 86 | errno = 0; |
| 87 | ASSERT_EQ(EINVAL, posix_fallocate(tf.fd, 0, -1)); |
| 88 | ASSERT_EQ(0, errno); |
| 89 | |
| 90 | errno = 0; |
| 91 | ASSERT_EQ(EINVAL, posix_fallocate64(tf.fd, 0, -1)); |
| 92 | ASSERT_EQ(0, errno); |
| 93 | } |
| 94 | |
| 95 | TEST(fcntl, fallocate) { |
| 96 | TemporaryFile tf; |
| 97 | struct stat sb; |
| 98 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 99 | ASSERT_EQ(0, sb.st_size); |
| 100 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 101 | #if defined(__BIONIC__) |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 102 | ASSERT_EQ(0, fallocate(tf.fd, 0, 0, 1)); |
| 103 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 104 | ASSERT_EQ(1, sb.st_size); |
| 105 | |
| 106 | ASSERT_EQ(0, fallocate64(tf.fd, 0, 0, 2)); |
| 107 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 108 | ASSERT_EQ(2, sb.st_size); |
| 109 | #endif |
| 110 | |
| 111 | ASSERT_EQ(0, posix_fallocate(tf.fd, 0, 3)); |
| 112 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 113 | ASSERT_EQ(3, sb.st_size); |
| 114 | |
| 115 | ASSERT_EQ(0, posix_fallocate64(tf.fd, 0, 4)); |
| 116 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 117 | ASSERT_EQ(4, sb.st_size); |
| 118 | } |
Serban Constantinescu | 48501af | 2014-03-14 13:16:25 +0000 | [diff] [blame] | 119 | |
| 120 | TEST(fcntl, f_getlk64) { |
| 121 | int fd = open64("/proc/version", O_RDONLY); |
| 122 | ASSERT_TRUE(fd != -1); |
| 123 | |
| 124 | struct flock64 check_lock; |
| 125 | check_lock.l_type = F_WRLCK; |
| 126 | check_lock.l_start = 0; |
| 127 | check_lock.l_whence = SEEK_SET; |
| 128 | check_lock.l_len = 0; |
| 129 | |
| 130 | int rc = fcntl(fd, F_GETLK64, &check_lock); |
| 131 | ASSERT_EQ(0, rc); |
| 132 | |
| 133 | close(fd); |
| 134 | } |
Elliott Hughes | 3f525d4 | 2014-06-24 16:32:01 -0700 | [diff] [blame] | 135 | |
| 136 | TEST(fcntl, splice) { |
| 137 | int pipe_fds[2]; |
| 138 | ASSERT_EQ(0, pipe(pipe_fds)); |
| 139 | |
| 140 | int in = open("/proc/cpuinfo", O_RDONLY); |
| 141 | ASSERT_NE(in, -1); |
| 142 | |
| 143 | TemporaryFile tf; |
| 144 | |
| 145 | ssize_t bytes_read = splice(in, 0, pipe_fds[1], NULL, 8*1024, SPLICE_F_MORE | SPLICE_F_MOVE); |
| 146 | ASSERT_NE(bytes_read, -1); |
| 147 | |
| 148 | ssize_t bytes_written = splice(pipe_fds[0], NULL, tf.fd, 0, bytes_read, SPLICE_F_MORE | SPLICE_F_MOVE); |
| 149 | ASSERT_EQ(bytes_read, bytes_written); |
| 150 | |
| 151 | close(pipe_fds[0]); |
| 152 | close(pipe_fds[1]); |
| 153 | close(in); |
| 154 | } |
| 155 | |
| 156 | TEST(fcntl, vmsplice) { |
| 157 | int pipe_fds[2]; |
| 158 | ASSERT_EQ(0, pipe(pipe_fds)); |
| 159 | |
| 160 | iovec v[2]; |
| 161 | v[0].iov_base = const_cast<char*>("hello "); |
| 162 | v[0].iov_len = 6; |
| 163 | v[1].iov_base = const_cast<char*>("world\n"); |
| 164 | v[1].iov_len = 6; |
| 165 | ssize_t bytes_written = vmsplice(pipe_fds[1], v, sizeof(v)/sizeof(iovec), 0); |
| 166 | ASSERT_EQ(v[0].iov_len + v[1].iov_len, static_cast<size_t>(bytes_written)); |
| 167 | close(pipe_fds[1]); |
| 168 | |
| 169 | char buf[BUFSIZ]; |
| 170 | FILE* fp = fdopen(pipe_fds[0], "r"); |
| 171 | ASSERT_TRUE(fp != NULL); |
| 172 | ASSERT_TRUE(fgets(buf, sizeof(buf), fp) != NULL); |
| 173 | fclose(fp); |
| 174 | ASSERT_STREQ("hello world\n", buf); |
| 175 | } |
| 176 | |
| 177 | TEST(fcntl, tee) { |
| 178 | char expected[256]; |
| 179 | FILE* expected_fp = fopen("/proc/version", "r"); |
| 180 | ASSERT_TRUE(expected_fp != NULL); |
| 181 | ASSERT_TRUE(fgets(expected, sizeof(expected), expected_fp) != NULL); |
| 182 | fclose(expected_fp); |
| 183 | |
| 184 | int pipe1[2]; |
| 185 | ASSERT_EQ(0, pipe(pipe1)); |
| 186 | |
| 187 | int pipe2[2]; |
| 188 | ASSERT_EQ(0, pipe(pipe2)); |
| 189 | |
| 190 | int in = open("/proc/version", O_RDONLY); |
| 191 | ASSERT_NE(in, -1); |
| 192 | |
| 193 | // Write /proc/version into pipe1. |
| 194 | ssize_t bytes_read = splice(in, 0, pipe1[1], NULL, 8*1024, SPLICE_F_MORE | SPLICE_F_MOVE); |
| 195 | ASSERT_NE(bytes_read, -1); |
| 196 | close(pipe1[1]); |
| 197 | |
| 198 | // Tee /proc/version from pipe1 into pipe2. |
| 199 | ssize_t bytes_teed = tee(pipe1[0], pipe2[1], SIZE_MAX, 0); |
| 200 | ASSERT_EQ(bytes_read, bytes_teed); |
| 201 | close(pipe2[1]); |
| 202 | |
| 203 | // The out fds of both pipe1 and pipe2 should now contain /proc/version. |
| 204 | char buf1[BUFSIZ]; |
| 205 | FILE* fp1 = fdopen(pipe1[0], "r"); |
| 206 | ASSERT_TRUE(fp1 != NULL); |
| 207 | ASSERT_TRUE(fgets(buf1, sizeof(buf1), fp1) != NULL); |
| 208 | fclose(fp1); |
| 209 | |
| 210 | char buf2[BUFSIZ]; |
| 211 | FILE* fp2 = fdopen(pipe2[0], "r"); |
| 212 | ASSERT_TRUE(fp2 != NULL); |
| 213 | ASSERT_TRUE(fgets(buf2, sizeof(buf2), fp2) != NULL); |
| 214 | fclose(fp2); |
| 215 | |
| 216 | ASSERT_STREQ(expected, buf1); |
| 217 | ASSERT_STREQ(expected, buf2); |
| 218 | } |