73 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/fcntl/open.h"10#include "src/unistd/close.h"11#include "src/unistd/dup3.h"12#include "src/unistd/read.h"13#include "src/unistd/unlink.h"14#include "src/unistd/write.h"15#include "test/UnitTest/ErrnoCheckingTest.h"16#include "test/UnitTest/ErrnoSetterMatcher.h"17#include "test/UnitTest/Test.h"18 19#include <sys/stat.h>20 21using LlvmLibcdupTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;22 23// The tests here are exactly the same as those of dup2. We only test the24// plumbing of the dup3 syscall and not the dup3 functionality itself as it is25// a simple syscall wrapper. Testing dup3 functionality is beyond the scope of26// this test.27 28TEST_F(LlvmLibcdupTest, ReadAndWriteViaDup) {29 constexpr int DUPFD = 0xD0;30 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;31 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;32 constexpr const char *FILENAME = "dup3.test";33 auto TEST_FILE = libc_make_test_file_path(FILENAME);34 int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);35 ASSERT_ERRNO_SUCCESS();36 ASSERT_GT(fd, 0);37 int dupfd = LIBC_NAMESPACE::dup3(fd, DUPFD, 0);38 ASSERT_ERRNO_SUCCESS();39 ASSERT_EQ(dupfd, DUPFD);40 41 // Write something via the dup42 constexpr char WRITE_DATA[] = "Hello, dup!";43 constexpr size_t WRITE_SIZE = sizeof(WRITE_DATA);44 ASSERT_EQ(ssize_t(WRITE_SIZE),45 LIBC_NAMESPACE::write(dupfd, WRITE_DATA, WRITE_SIZE));46 ASSERT_THAT(LIBC_NAMESPACE::close(dupfd), Succeeds(0));47 48 // Reopen the file for reading and create a dup.49 fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);50 ASSERT_ERRNO_SUCCESS();51 ASSERT_GT(fd, 0);52 dupfd = LIBC_NAMESPACE::dup3(fd, DUPFD, 0);53 ASSERT_ERRNO_SUCCESS();54 ASSERT_EQ(dupfd, DUPFD);55 56 // Read the file content via the dup.57 char buf[WRITE_SIZE];58 ASSERT_THAT(LIBC_NAMESPACE::read(dupfd, buf, WRITE_SIZE),59 Succeeds(WRITE_SIZE));60 ASSERT_STREQ(buf, WRITE_DATA);61 62 // Verify that, unlike dup2, duping to the same fd value with dup3 fails.63 ASSERT_THAT(LIBC_NAMESPACE::dup3(dupfd, dupfd, 0), Fails(EINVAL));64 65 ASSERT_THAT(LIBC_NAMESPACE::close(dupfd), Succeeds(0));66 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));67}68 69TEST_F(LlvmLibcdupTest, DupBadFD) {70 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;71 ASSERT_THAT(LIBC_NAMESPACE::dup3(-1, 123, 0), Fails(EBADF));72}73