1 #include <errno.h> 2 #include <fcntl.h> 3 #include <gtest/gtest.h> 4 #include <sys/eventfd.h> 5 6 using namespace testing::ext; 7 8 class LinuxEventfdTest : public testing::Test { SetUp()9 void SetUp() override {} TearDown()10 void TearDown() override {} 11 }; 12 13 constexpr unsigned int VALUE = 2; 14 15 /** 16 * @tc.name: eventfd_001 17 * @tc.desc: This test verifies that the eventfd instance created by the eventfd function can correctly set the flag bit 18 * and read the correct initial value, while also setting the EFD_The CLOEXEC flag bit ensures that the 19 * eventfd instance will automatically close when executing the exec series of functions. 20 * @tc.type: FUNC 21 */ 22 HWTEST_F(LinuxEventfdTest, eventfd_001, TestSize.Level1) 23 { 24 int eventFd = eventfd(VALUE, EFD_CLOEXEC); 25 ASSERT_NE(eventFd, -1); 26 int flags = fcntl(eventFd, F_GETFD); 27 EXPECT_NE(flags, -1); 28 EXPECT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); 29 close(eventFd); 30 } 31 32 /** 33 * @tc.name: eventfd_002 34 * @tc.desc: The test case aims to verify the functionality and correctness of creating an eventfd instance 35 * with the EFD_NONBLOCK and EFD_CLOEXEC flags. It ensures that the eventfd instance can work properly and 36 * will be automatically closed when executing exec series functions. 37 * @tc.type: FUNC 38 */ 39 HWTEST_F(LinuxEventfdTest, eventfd_002, TestSize.Level1) 40 { 41 int eventFd = eventfd(VALUE, EFD_NONBLOCK | EFD_CLOEXEC); 42 ASSERT_NE(eventFd, -1); 43 int flags = fcntl(eventFd, F_GETFD); 44 EXPECT_NE(flags, -1); 45 EXPECT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); 46 close(eventFd); 47 } 48 49 /** 50 * @tc.name: eventfd_003 51 * @tc.desc: The testing viewpoint of this test case is to create an eventfd object of semaphore type by calling the 52 * eventfd function and asserting a return value greater than 0 to verify whether the eventfd object was 53 * successfully created. 54 * @tc.type: FUNC 55 */ 56 HWTEST_F(LinuxEventfdTest, eventfd_003, TestSize.Level1) 57 { 58 int result = eventfd(0, EFD_SEMAPHORE); 59 EXPECT_GE(result, 0); 60 } 61