blob: 5f20295690a958cab03a99656d978e4dc22d792a [file] [log] [blame]
Elliott Hughes06209252013-11-06 16:20:54 -08001/*
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 Hughesf64b8ea2014-02-03 16:20:46 -080022#include "TemporaryFile.h"
23
Elliott Hughes06209252013-11-06 16:20:54 -080024TEST(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 Hughesdb1ea342014-01-17 18:42:49 -080038
39 close(fd);
40}
41
42TEST(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
54TEST(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
66TEST(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 Hughes06209252013-11-06 16:20:54 -080071}
Elliott Hughesf64b8ea2014-02-03 16:20:46 -080072
73TEST(fcntl, fallocate_EINVAL) {
74 TemporaryFile tf;
75
Elliott Hughes063525c2014-05-13 11:19:57 -070076#if defined(__BIONIC__)
Elliott Hughesf64b8ea2014-02-03 16:20:46 -080077 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
95TEST(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 Hughes063525c2014-05-13 11:19:57 -0700101#if defined(__BIONIC__)
Elliott Hughesf64b8ea2014-02-03 16:20:46 -0800102 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 Constantinescu48501af2014-03-14 13:16:25 +0000119
120TEST(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 Hughes3f525d42014-06-24 16:32:01 -0700135
136TEST(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
156TEST(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
177TEST(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}