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