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 */
17 #include <gtest/gtest.h>
19 #include <errno.h>
20 #include <fcntl.h>
22 #include "TemporaryFile.h"
24 TEST(fcntl, fcntl_smoke) {
25 int fd = open("/proc/version", O_RDONLY);
26 ASSERT_TRUE(fd != -1);
28 int flags = fcntl(fd, F_GETFD);
29 ASSERT_TRUE(flags != -1);
30 ASSERT_EQ(0, flags & FD_CLOEXEC);
32 int rc = fcntl(fd, F_SETFD, FD_CLOEXEC);
33 ASSERT_EQ(0, rc);
35 flags = fcntl(fd, F_GETFD);
36 ASSERT_TRUE(flags != -1);
37 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
39 close(fd);
40 }
42 TEST(fcntl, open_open64) {
43 int fd;
45 fd = open("/proc/version", O_RDONLY);
46 ASSERT_TRUE(fd != -1);
47 close(fd);
49 fd = open64("/proc/version", O_RDONLY);
50 ASSERT_TRUE(fd != -1);
51 close(fd);
52 }
54 TEST(fcntl, openat_openat64) {
55 int fd;
57 fd = openat(AT_FDCWD, "/proc/version", O_RDONLY);
58 ASSERT_TRUE(fd != -1);
59 close(fd);
61 fd = openat64(AT_FDCWD, "/proc/version", O_RDONLY);
62 ASSERT_TRUE(fd != -1);
63 close(fd);
64 }
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);
71 }
73 TEST(fcntl, fallocate_EINVAL) {
74 TemporaryFile tf;
76 #if defined(__BIONIC__)
77 errno = 0;
78 ASSERT_EQ(-1, fallocate(tf.fd, 0, 0, -1));
79 ASSERT_EQ(EINVAL, errno);
81 errno = 0;
82 ASSERT_EQ(-1, fallocate64(tf.fd, 0, 0, -1));
83 ASSERT_EQ(EINVAL, errno);
84 #endif
86 errno = 0;
87 ASSERT_EQ(EINVAL, posix_fallocate(tf.fd, 0, -1));
88 ASSERT_EQ(0, errno);
90 errno = 0;
91 ASSERT_EQ(EINVAL, posix_fallocate64(tf.fd, 0, -1));
92 ASSERT_EQ(0, errno);
93 }
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);
101 #if defined(__BIONIC__)
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);
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
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);
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 }
120 TEST(fcntl, f_getlk64) {
121 int fd = open64("/proc/version", O_RDONLY);
122 ASSERT_TRUE(fd != -1);
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;
130 int rc = fcntl(fd, F_GETLK64, &check_lock);
131 ASSERT_EQ(0, rc);
133 close(fd);
134 }
136 TEST(fcntl, splice) {
137 int pipe_fds[2];
138 ASSERT_EQ(0, pipe(pipe_fds));
140 int in = open("/proc/cpuinfo", O_RDONLY);
141 ASSERT_NE(in, -1);
143 TemporaryFile tf;
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);
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);
151 close(pipe_fds[0]);
152 close(pipe_fds[1]);
153 close(in);
154 }
156 TEST(fcntl, vmsplice) {
157 int pipe_fds[2];
158 ASSERT_EQ(0, pipe(pipe_fds));
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]);
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 }
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);
184 int pipe1[2];
185 ASSERT_EQ(0, pipe(pipe1));
187 int pipe2[2];
188 ASSERT_EQ(0, pipe(pipe2));
190 int in = open("/proc/version", O_RDONLY);
191 ASSERT_NE(in, -1);
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]);
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]);
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);
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);
216 ASSERT_STREQ(expected, buf1);
217 ASSERT_STREQ(expected, buf2);
218 }