1//===-- Unittests for dup3 ------------------------------------------------===//
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/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"
18
19#include <sys/stat.h>
20
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
24// this test.
25
26TEST(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(path: TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
34 ASSERT_ERRNO_SUCCESS();
35 ASSERT_GT(fd, 0);
36 int dupfd = LIBC_NAMESPACE::dup3(oldfd: fd, newfd: DUPFD, flags: 0);
37 ASSERT_ERRNO_SUCCESS();
38 ASSERT_EQ(dupfd, DUPFD);
39
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));
46
47 // Reopen the file for reading and create a dup.
48 fd = LIBC_NAMESPACE::open(path: TEST_FILE, O_RDONLY);
49 ASSERT_ERRNO_SUCCESS();
50 ASSERT_GT(fd, 0);
51 dupfd = LIBC_NAMESPACE::dup3(oldfd: fd, newfd: DUPFD, flags: 0);
52 ASSERT_ERRNO_SUCCESS();
53 ASSERT_EQ(dupfd, DUPFD);
54
55 // Read the file content via the dup.
56 char buf[WRITE_SIZE];
57 ASSERT_THAT(LIBC_NAMESPACE::read(dupfd, buf, WRITE_SIZE),
58 Succeeds(WRITE_SIZE));
59 ASSERT_STREQ(buf, WRITE_DATA);
60
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));
63
64 ASSERT_THAT(LIBC_NAMESPACE::close(dupfd), Succeeds(0));
65 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
66}
67
68TEST(LlvmLibcdupTest, DupBadFD) {
69 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
70 ASSERT_THAT(LIBC_NAMESPACE::dup3(-1, 123, 0), Fails(EBADF));
71}
72

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