1 #include <fcntl.h> 2 #include <gtest/gtest.h> 3 #include <string.h> 4 #include <unistd.h> 5 6 using namespace testing::ext; 7 8 constexpr int BUF_SIZE = 1024; 9 10 class StdioGetsTest : public testing::Test { SetUp()11 void SetUp() override {} TearDown()12 void TearDown() override {} 13 }; 14 15 /** 16 * @tc.name: gets_001 17 * @tc.desc: Verify whether the read operation of this gets is normal. 18 * @tc.type: FUNC 19 * */ 20 HWTEST_F(StdioGetsTest, gets_001, TestSize.Level1) 21 { 22 pid_t pid; 23 pid = fork(); 24 ASSERT_LE(0, pid); 25 if (pid == 0) { 26 char str[BUF_SIZE]; 27 int fd = open("/proc/version", O_RDONLY); 28 int orgFd = dup(STDIN_FILENO); 29 dup2(fd, STDIN_FILENO); 30 gets(str); 31 EXPECT_TRUE(str); 32 dup2(orgFd, STDIN_FILENO); 33 close(fd); 34 _exit(0); 35 } else { 36 waitpid(pid, nullptr, 0); 37 } 38 }