xref: /llvm-project/libc/test/src/unistd/lseek_test.cpp (revision 7a7d5481ad5c925d4f31bee3ab66bd1d7d514b73)
1827575a7SSiva Chandra Reddy //===-- Unittests for lseek -----------------------------------------------===//
2827575a7SSiva Chandra Reddy //
3827575a7SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4827575a7SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
5827575a7SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6827575a7SSiva Chandra Reddy //
7827575a7SSiva Chandra Reddy //===----------------------------------------------------------------------===//
8827575a7SSiva Chandra Reddy 
967de5381SSiva Chandra Reddy #include "src/errno/libc_errno.h"
10827575a7SSiva Chandra Reddy #include "src/fcntl/open.h"
11827575a7SSiva Chandra Reddy #include "src/unistd/close.h"
12827575a7SSiva Chandra Reddy #include "src/unistd/lseek.h"
13827575a7SSiva Chandra Reddy #include "src/unistd/read.h"
144f1fe19dSSiva Chandra Reddy #include "test/UnitTest/ErrnoSetterMatcher.h"
15af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
16827575a7SSiva Chandra Reddy 
17827575a7SSiva Chandra Reddy #include <unistd.h>
18827575a7SSiva Chandra Reddy 
TEST(LlvmLibcUniStd,LseekTest)19827575a7SSiva Chandra Reddy TEST(LlvmLibcUniStd, LseekTest) {
20b6bc9d72SGuillaume Chatelet   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
21*7a7d5481Smichaelrj-google   constexpr const char *FILENAME = "testdata/lseek.test";
22*7a7d5481Smichaelrj-google   auto TEST_FILE = libc_make_test_file_path(FILENAME);
23b6bc9d72SGuillaume Chatelet   int fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
2473874f7aSGuillaume Chatelet   ASSERT_ERRNO_SUCCESS();
25827575a7SSiva Chandra Reddy   ASSERT_GT(fd, 0);
26827575a7SSiva Chandra Reddy   constexpr const char LSEEK_TEST[] = "lseek test";
27827575a7SSiva Chandra Reddy   constexpr int LSEEK_TEST_SIZE = sizeof(LSEEK_TEST) - 1;
28827575a7SSiva Chandra Reddy 
29827575a7SSiva Chandra Reddy   char read_buf[20];
30b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::read(fd, read_buf, LSEEK_TEST_SIZE),
31827575a7SSiva Chandra Reddy               Succeeds(LSEEK_TEST_SIZE));
32827575a7SSiva Chandra Reddy   read_buf[LSEEK_TEST_SIZE] = '\0';
33827575a7SSiva Chandra Reddy   EXPECT_STREQ(read_buf, LSEEK_TEST);
34827575a7SSiva Chandra Reddy 
35827575a7SSiva Chandra Reddy   // Seek to the beginning of the file and re-read.
36b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::lseek(fd, 0, SEEK_SET), Succeeds(0));
37b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::read(fd, read_buf, LSEEK_TEST_SIZE),
38827575a7SSiva Chandra Reddy               Succeeds(LSEEK_TEST_SIZE));
39827575a7SSiva Chandra Reddy   read_buf[LSEEK_TEST_SIZE] = '\0';
40827575a7SSiva Chandra Reddy   EXPECT_STREQ(read_buf, LSEEK_TEST);
41827575a7SSiva Chandra Reddy 
42827575a7SSiva Chandra Reddy   // Seek to the beginning of the file from the end and re-read.
43b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::lseek(fd, -LSEEK_TEST_SIZE, SEEK_END),
44b6bc9d72SGuillaume Chatelet               Succeeds(0));
45b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::read(fd, read_buf, LSEEK_TEST_SIZE),
46827575a7SSiva Chandra Reddy               Succeeds(LSEEK_TEST_SIZE));
47827575a7SSiva Chandra Reddy   read_buf[LSEEK_TEST_SIZE] = '\0';
48827575a7SSiva Chandra Reddy   EXPECT_STREQ(read_buf, LSEEK_TEST);
49827575a7SSiva Chandra Reddy 
50b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
51827575a7SSiva Chandra Reddy }
52827575a7SSiva Chandra Reddy 
TEST(LlvmLibcUniStd,LseekFailsTest)53827575a7SSiva Chandra Reddy TEST(LlvmLibcUniStd, LseekFailsTest) {
54b6bc9d72SGuillaume Chatelet   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
55b6bc9d72SGuillaume Chatelet   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
56*7a7d5481Smichaelrj-google   constexpr const char *FILENAME = "testdata/lseek.test";
57*7a7d5481Smichaelrj-google   auto TEST_FILE = libc_make_test_file_path(FILENAME);
58b6bc9d72SGuillaume Chatelet   int fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
5973874f7aSGuillaume Chatelet   ASSERT_ERRNO_SUCCESS();
60827575a7SSiva Chandra Reddy   ASSERT_GT(fd, 0);
61b6bc9d72SGuillaume Chatelet   EXPECT_THAT(LIBC_NAMESPACE::lseek(fd, -1, SEEK_CUR), Fails(EINVAL));
62b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
63827575a7SSiva Chandra Reddy }
64