1 #include <fcntl.h> 2 #include <gtest/gtest.h> 3 #include <sys/stat.h> 4 5 using namespace testing::ext; 6 7 class StatFstatTest : public testing::Test { SetUp()8 void SetUp() override {} TearDown()9 void TearDown() override {} 10 }; 11 12 /** 13 * @tc.name: fstat_001 14 * @tc.desc: Verify that the "fstat" function correctly retrieves file-related information such as file size, file 15 * permissions, timestamps, and other attributes associated with the specified file descriptor. 16 * @tc.type: FUNC 17 **/ 18 HWTEST_F(StatFstatTest, fstat_001, TestSize.Level1) 19 { 20 int fd = open("test_fstat", O_RDWR | O_CREAT, 0755); 21 EXPECT_GE(fd, 0); 22 struct stat fileStat; 23 EXPECT_EQ(0, fstat(fd, &fileStat)); 24 close(fd); 25 remove("test_fstat"); 26 } 27 28 /** 29 * @tc.name: fstat_002 30 * @tc.desc: Verify the error handling of the fstat() function when attempting to retrieve the status of a n 31 * on-existent file, and it checks if the appropriate error code (-1) is returned. 32 * @tc.type: FUNC 33 **/ 34 HWTEST_F(StatFstatTest, fstat_002, TestSize.Level1) 35 { 36 const char* filename = "not_exist.txt"; 37 int fd = open(filename, O_RDONLY); 38 struct stat fileStat; 39 EXPECT_EQ(-1, fstat(fd, &fileStat)); 40 close(fd); 41 }