1 //===-- Unittests for dup3 ------------------------------------------------===//
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/dup3.h"
13 #include "src/unistd/read.h"
14 #include "src/unistd/unlink.h"
15 #include "src/unistd/write.h"
16 #include "test/UnitTest/ErrnoSetterMatcher.h"
17 #include "test/UnitTest/Test.h"
21 // The tests here are exactly the same as those of dup2. We only test the
22 // plumbing of the dup3 syscall and not the dup3 functionality itself as it is
23 // a simple syscall wrapper. Testing dup3 functionality is beyond the scope of
26 TEST(LlvmLibcdupTest
, ReadAndWriteViaDup
) {
27 constexpr int DUPFD
= 0xD0;
28 LIBC_NAMESPACE::libc_errno
= 0;
29 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
30 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
31 constexpr const char *FILENAME
= "dup3.test";
32 auto TEST_FILE
= libc_make_test_file_path(FILENAME
);
33 int fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
34 ASSERT_ERRNO_SUCCESS();
36 int dupfd
= LIBC_NAMESPACE::dup3(fd
, DUPFD
, 0);
37 ASSERT_ERRNO_SUCCESS();
38 ASSERT_EQ(dupfd
, DUPFD
);
40 // Write something via the dup
41 constexpr char WRITE_DATA
[] = "Hello, dup!";
42 constexpr size_t WRITE_SIZE
= sizeof(WRITE_DATA
);
43 ASSERT_EQ(ssize_t(WRITE_SIZE
),
44 LIBC_NAMESPACE::write(dupfd
, WRITE_DATA
, WRITE_SIZE
));
45 ASSERT_THAT(LIBC_NAMESPACE::close(dupfd
), Succeeds(0));
47 // Reopen the file for reading and create a dup.
48 fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_RDONLY
);
49 ASSERT_ERRNO_SUCCESS();
51 dupfd
= LIBC_NAMESPACE::dup3(fd
, DUPFD
, 0);
52 ASSERT_ERRNO_SUCCESS();
53 ASSERT_EQ(dupfd
, DUPFD
);
55 // Read the file content via the dup.
57 ASSERT_THAT(LIBC_NAMESPACE::read(dupfd
, buf
, WRITE_SIZE
),
58 Succeeds(WRITE_SIZE
));
59 ASSERT_STREQ(buf
, WRITE_DATA
);
61 // Verify that, unlike dup2, duping to the same fd value with dup3 fails.
62 ASSERT_THAT(LIBC_NAMESPACE::dup3(dupfd
, dupfd
, 0), Fails(EINVAL
));
64 ASSERT_THAT(LIBC_NAMESPACE::close(dupfd
), Succeeds(0));
65 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE
), Succeeds(0));
68 TEST(LlvmLibcdupTest
, DupBadFD
) {
69 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
70 ASSERT_THAT(LIBC_NAMESPACE::dup3(-1, 123, 0), Fails(EBADF
));