1 #include <bits/alltypes.h> 2 #include <fcntl.h> 3 #include <gtest/gtest.h> 4 #include <stdio.h> 5 #include <unistd.h> 6 using namespace testing::ext; 7 constexpr int NUM = 9; 8 class UnistdDup2Test : public testing::Test { SetUp()9 void SetUp() override {} TearDown()10 void TearDown() override {} 11 }; 12 13 /** 14 * @tc.name: dup2_001 15 * @tc.desc: Verify the functionality of the dup2() function by duplicating a file descriptor onto another, 16 * performing I/O operations, and checking for successful duplication and I/O. 17 * @tc.type: FUNC 18 **/ 19 HWTEST_F(UnistdDup2Test, dup2_001, TestSize.Level1) 20 { 21 int fd = open("/dev/null", O_RDONLY); 22 ASSERT_NE(-1, fd); 23 FILE* fp = tmpfile(); 24 EXPECT_TRUE(fp != nullptr); 25 setbuf(fp, nullptr); 26 int result1 = fprintf(fp, "musl_test"); 27 EXPECT_EQ(NUM, result1); 28 int result2 = dup2(fd, fileno(fp)); 29 EXPECT_NE(-1, result2); 30 fclose(fp); 31 close(fd); 32 } 33 34 /** 35 * @tc.name: dup2_002 36 * @tc.desc: Verify the error handling of the dup2() function when invalid file descriptors are provided as arguments. 37 * It checks if the appropriate error code (-1) is returned. 38 * @tc.type: FUNC 39 **/ 40 HWTEST_F(UnistdDup2Test, dup2_002, TestSize.Level1) 41 { 42 int result = dup2(-1, -1); 43 EXPECT_EQ(-1, result); 44 }