1 //===-- Unittests for read and write --------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/errno/libc_errno.h"
10 #include "src/fcntl/open.h"
11 #include "src/unistd/close.h"
12 #include "src/unistd/fsync.h"
13 #include "src/unistd/read.h"
14 #include "src/unistd/write.h"
15 #include "test/UnitTest/ErrnoSetterMatcher.h"
16 #include "test/UnitTest/Test.h"
18 TEST(LlvmLibcUniStd
, WriteAndReadBackTest
) {
19 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
20 constexpr const char *TEST_FILE
= "__unistd_read_write.test";
21 int write_fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
22 ASSERT_EQ(libc_errno
, 0);
23 ASSERT_GT(write_fd
, 0);
24 constexpr const char HELLO
[] = "hello";
25 constexpr int HELLO_SIZE
= sizeof(HELLO
);
26 ASSERT_THAT(LIBC_NAMESPACE::write(write_fd
, HELLO
, HELLO_SIZE
),
27 Succeeds(HELLO_SIZE
));
28 ASSERT_THAT(LIBC_NAMESPACE::fsync(write_fd
), Succeeds(0));
29 ASSERT_THAT(LIBC_NAMESPACE::close(write_fd
), Succeeds(0));
31 int read_fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_RDONLY
);
32 ASSERT_EQ(libc_errno
, 0);
33 ASSERT_GT(read_fd
, 0);
35 ASSERT_THAT(LIBC_NAMESPACE::read(read_fd
, read_buf
, HELLO_SIZE
),
36 Succeeds(HELLO_SIZE
));
37 EXPECT_STREQ(read_buf
, HELLO
);
38 ASSERT_THAT(LIBC_NAMESPACE::close(read_fd
), Succeeds(0));
40 // TODO: 'remove' the test file after the test.
43 TEST(LlvmLibcUniStd
, WriteFails
) {
44 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
46 EXPECT_THAT(LIBC_NAMESPACE::write(-1, "", 1), Fails(EBADF
));
47 EXPECT_THAT(LIBC_NAMESPACE::write(1, reinterpret_cast<const void *>(-1), 1),
51 TEST(LlvmLibcUniStd
, ReadFails
) {
52 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
54 EXPECT_THAT(LIBC_NAMESPACE::read(-1, nullptr, 1), Fails(EBADF
));
55 EXPECT_THAT(LIBC_NAMESPACE::read(0, reinterpret_cast<void *>(-1), 1),