sys_sendfile_test.cpp revision b4f7616fd618875768b8fffc122b58bdb84a9969
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
26TEST(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
45#if __BIONIC__
46TEST(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#endif
65