xref: /llvm-project/libc/test/src/sys/sendfile/sendfile_test.cpp (revision abc49cc19463970d5523d7d3332e4c1f83bc2ef7)
1b8be3dabSSiva Chandra Reddy //===-- Unittests for sendfile --------------------------------------------===//
2b8be3dabSSiva Chandra Reddy //
3b8be3dabSSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b8be3dabSSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
5b8be3dabSSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b8be3dabSSiva Chandra Reddy //
7b8be3dabSSiva Chandra Reddy //===----------------------------------------------------------------------===//
8b8be3dabSSiva Chandra Reddy 
9b8be3dabSSiva Chandra Reddy #include "src/__support/CPP/string_view.h"
10af783db7SSiva Chandra Reddy #include "src/errno/libc_errno.h"
11b8be3dabSSiva Chandra Reddy #include "src/fcntl/open.h"
12b8be3dabSSiva Chandra Reddy #include "src/sys/sendfile/sendfile.h"
13b8be3dabSSiva Chandra Reddy #include "src/unistd/close.h"
14b8be3dabSSiva Chandra Reddy #include "src/unistd/read.h"
15b8be3dabSSiva Chandra Reddy #include "src/unistd/unlink.h"
16b8be3dabSSiva Chandra Reddy #include "src/unistd/write.h"
174f1fe19dSSiva Chandra Reddy #include "test/UnitTest/ErrnoSetterMatcher.h"
18af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
19b8be3dabSSiva Chandra Reddy 
20*abc49cc1SJob Henandez Lara #include "hdr/fcntl_macros.h"
21b8be3dabSSiva Chandra Reddy #include <sys/stat.h>
22b8be3dabSSiva Chandra Reddy 
23b6bc9d72SGuillaume Chatelet namespace cpp = LIBC_NAMESPACE::cpp;
24b8be3dabSSiva Chandra Reddy 
25b8be3dabSSiva Chandra Reddy TEST(LlvmLibcSendfileTest, CreateAndTransfer) {
26b6bc9d72SGuillaume Chatelet   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
27b6bc9d72SGuillaume Chatelet   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
28b8be3dabSSiva Chandra Reddy 
29b8be3dabSSiva Chandra Reddy   // The test strategy is to
30b8be3dabSSiva Chandra Reddy   //   1. Create a temporary file with known data.
31b8be3dabSSiva Chandra Reddy   //   2. Use sendfile to copy it to another file.
32b8be3dabSSiva Chandra Reddy   //   3. Make sure that the data was actually copied.
33b8be3dabSSiva Chandra Reddy   //   4. Clean up the temporary files.
34b8be3dabSSiva Chandra Reddy   constexpr const char *IN_FILE = "testdata/sendfile_in.test";
35b8be3dabSSiva Chandra Reddy   constexpr const char *OUT_FILE = "testdata/sendfile_out.test";
36b8be3dabSSiva Chandra Reddy   const char IN_DATA[] = "sendfile test";
37b8be3dabSSiva Chandra Reddy   constexpr ssize_t IN_SIZE = ssize_t(sizeof(IN_DATA));
383eb1e6d8Smichaelrj-google   LIBC_NAMESPACE::libc_errno = 0;
39b8be3dabSSiva Chandra Reddy 
40b6bc9d72SGuillaume Chatelet   int in_fd = LIBC_NAMESPACE::open(IN_FILE, O_CREAT | O_WRONLY, S_IRWXU);
41b8be3dabSSiva Chandra Reddy   ASSERT_GT(in_fd, 0);
4273874f7aSGuillaume Chatelet   ASSERT_ERRNO_SUCCESS();
43b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::write(in_fd, IN_DATA, IN_SIZE), IN_SIZE);
44b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(in_fd), Succeeds(0));
45b8be3dabSSiva Chandra Reddy 
46b6bc9d72SGuillaume Chatelet   in_fd = LIBC_NAMESPACE::open(IN_FILE, O_RDONLY);
47b8be3dabSSiva Chandra Reddy   ASSERT_GT(in_fd, 0);
4873874f7aSGuillaume Chatelet   ASSERT_ERRNO_SUCCESS();
49b6bc9d72SGuillaume Chatelet   int out_fd = LIBC_NAMESPACE::open(OUT_FILE, O_CREAT | O_WRONLY, S_IRWXU);
50b8be3dabSSiva Chandra Reddy   ASSERT_GT(out_fd, 0);
5173874f7aSGuillaume Chatelet   ASSERT_ERRNO_SUCCESS();
52b6bc9d72SGuillaume Chatelet   ssize_t size = LIBC_NAMESPACE::sendfile(in_fd, out_fd, nullptr, IN_SIZE);
53b8be3dabSSiva Chandra Reddy   ASSERT_EQ(size, IN_SIZE);
54b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(in_fd), Succeeds(0));
55b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(out_fd), Succeeds(0));
56b8be3dabSSiva Chandra Reddy 
57b6bc9d72SGuillaume Chatelet   out_fd = LIBC_NAMESPACE::open(OUT_FILE, O_RDONLY);
58b8be3dabSSiva Chandra Reddy   ASSERT_GT(out_fd, 0);
5973874f7aSGuillaume Chatelet   ASSERT_ERRNO_SUCCESS();
60b8be3dabSSiva Chandra Reddy   char buf[IN_SIZE];
61b6bc9d72SGuillaume Chatelet   ASSERT_EQ(IN_SIZE, LIBC_NAMESPACE::read(out_fd, buf, IN_SIZE));
62b8be3dabSSiva Chandra Reddy   ASSERT_EQ(cpp::string_view(buf), cpp::string_view(IN_DATA));
63b8be3dabSSiva Chandra Reddy 
64b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::unlink(IN_FILE), Succeeds(0));
65b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::unlink(OUT_FILE), Succeeds(0));
66b8be3dabSSiva Chandra Reddy }
67