1 #include <fcntl.h> 2 #include <gtest/gtest.h> 3 #include <stdlib.h> 4 #include <sys/auxv.h> 5 #include <sys/uio.h> 6 #include <unistd.h> 7 8 using namespace testing::ext; 9 10 constexpr int BUF_SIZE = 256; 11 12 class UnistdPipeTest : public testing::Test { SetUp()13 void SetUp() override {} TearDown()14 void TearDown() override {} 15 }; 16 17 /** 18 * @tc.name: pipe_001 19 * @tc.desc: checks whether the pipe function creates a pipe successfully and whether data can be written to and read 20 * from the pipe correctly in a child process and a parent process. 21 * @tc.type: FUNC 22 * */ 23 HWTEST_F(UnistdPipeTest, pipe_001, TestSize.Level1) 24 { 25 int fd[2]; 26 char buffer[BUF_SIZE]; 27 EXPECT_EQ(0, pipe(fd)); 28 pid_t pid = fork(); 29 ASSERT_LE(0, pid); 30 if (pid == 0) { 31 const char* message = "test pipe"; 32 EXPECT_EQ(0, close(fd[0])); 33 write(fd[1], message, strlen(message)); 34 EXPECT_EQ(0, close(fd[1])); 35 _exit(0); 36 } else { 37 EXPECT_EQ(0, close(fd[1])); 38 read(fd[0], buffer, sizeof(buffer)); 39 EXPECT_STREQ("test pipe", buffer); 40 EXPECT_EQ(0, close(fd[0])); 41 } 42 }