1//===-- Unittests for ftruncate -------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/__support/CPP/string_view.h"
10#include "src/fcntl/open.h"
11#include "src/unistd/close.h"
12#include "src/unistd/ftruncate.h"
13#include "src/unistd/read.h"
14#include "src/unistd/unlink.h"
15#include "src/unistd/write.h"
16#include "test/UnitTest/ErrnoCheckingTest.h"
17#include "test/UnitTest/ErrnoSetterMatcher.h"
18#include "test/UnitTest/Test.h"
19
20#include <sys/stat.h>
21
22namespace cpp = LIBC_NAMESPACE::cpp;
23using LlvmLibcFtruncateTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
24
25TEST_F(LlvmLibcFtruncateTest, CreateAndTruncate) {
26 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
27 constexpr const char *FILENAME = "ftruncate.test";
28 auto TEST_FILE = libc_make_test_file_path(FILENAME);
29 constexpr const char WRITE_DATA[] = "hello, ftruncate";
30 constexpr size_t WRITE_SIZE = sizeof(WRITE_DATA);
31 char buf[WRITE_SIZE];
32
33 // The test strategy is as follows:
34 // 1. Create a normal file with some data in it.
35 // 2. Read it to make sure what was written is actually in the file.
36 // 3. Truncate to 1 byte.
37 // 4. Try to read more than 1 byte and fail.
38 int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
39 ASSERT_ERRNO_SUCCESS();
40 ASSERT_GT(fd, 0);
41 ASSERT_EQ(ssize_t(WRITE_SIZE),
42 LIBC_NAMESPACE::write(fd, WRITE_DATA, WRITE_SIZE));
43 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
44
45 fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
46 ASSERT_ERRNO_SUCCESS();
47 ASSERT_GT(fd, 0);
48 ASSERT_EQ(ssize_t(WRITE_SIZE), LIBC_NAMESPACE::read(fd, buf, WRITE_SIZE));
49 ASSERT_EQ(cpp::string_view(buf), cpp::string_view(WRITE_DATA));
50 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
51
52 // For ftruncate operation to succeed, the file should be opened for
53 // writing.
54 fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY);
55 ASSERT_GT(fd, 0);
56 ASSERT_ERRNO_SUCCESS();
57 ASSERT_THAT(LIBC_NAMESPACE::ftruncate(fd, off_t(1)), Succeeds(0));
58 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
59
60 fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
61 ASSERT_ERRNO_SUCCESS();
62 ASSERT_GT(fd, 0);
63 ASSERT_EQ(ssize_t(1), LIBC_NAMESPACE::read(fd, buf, WRITE_SIZE));
64 ASSERT_EQ(buf[0], WRITE_DATA[0]);
65 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
66
67 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
68}
69
70TEST_F(LlvmLibcFtruncateTest, TruncateBadFD) {
71 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
72 ASSERT_THAT(LIBC_NAMESPACE::ftruncate(0, off_t(1)), Fails(EINVAL));
73}
74

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of libc/test/src/unistd/ftruncate_test.cpp