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
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <sys/sendfile.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23
24 #include <android-base/file.h>
25 #include <gtest/gtest.h>
26
TEST(sys_sendfile,sendfile)27 TEST(sys_sendfile, sendfile) {
28 TemporaryFile src_file;
29 ASSERT_EQ(5, TEMP_FAILURE_RETRY(write(src_file.fd, "hello", 5)));
30
31 TemporaryFile dst_file;
32
33 off_t offset = 2;
34 size_t count = 2;
35 ssize_t rc = sendfile(dst_file.fd, src_file.fd, &offset, count);
36 ASSERT_EQ(2, rc);
37 ASSERT_EQ(4, offset);
38
39 ASSERT_EQ(0, lseek(dst_file.fd, 0, SEEK_SET));
40 char buf[3];
41 buf[2] = '\0';
42 ASSERT_EQ(2, TEMP_FAILURE_RETRY(read(dst_file.fd, &buf, 2)));
43 ASSERT_STREQ("ll", buf);
44 }
45
TEST(sys_sendfile,sendfile64)46 TEST(sys_sendfile, sendfile64) {
47 TemporaryFile src_file;
48 ASSERT_EQ(5, TEMP_FAILURE_RETRY(write(src_file.fd, "hello", 5)));
49
50 TemporaryFile dst_file;
51
52 off64_t offset = 2;
53 size_t count = 2;
54 ssize_t rc = sendfile64(dst_file.fd, src_file.fd, &offset, count);
55 ASSERT_EQ(2, rc);
56 ASSERT_EQ(4, offset);
57
58 ASSERT_EQ(0, lseek(dst_file.fd, 0, SEEK_SET));
59 char buf[3];
60 buf[2] = '\0';
61 ASSERT_EQ(2, TEMP_FAILURE_RETRY(read(dst_file.fd, &buf, 2)));
62 ASSERT_STREQ("ll", buf);
63 }
64