1 #include <errno.h> 2 #include <fcntl.h> 3 #include <gtest/gtest.h> 4 #include <stdio.h> 5 #include <sys/xattr.h> 6 7 using namespace testing::ext; 8 9 class LinuxSetxattrTest : public testing::Test { SetUp()10 void SetUp() override {} TearDown()11 void TearDown() override {} 12 }; 13 14 /** 15 * @tc.name: setxattr_001 16 * @tc.desc: This test verifies whether the setxattr function can handle invalid parameters correctly and whether the 17 * errno value can be set correctly. 18 * @tc.type: FUNC 19 */ 20 HWTEST_F(LinuxSetxattrTest, setxattr_001, TestSize.Level1) 21 { 22 errno = 0; 23 int result = setxattr(nullptr, nullptr, nullptr, -1, -1); 24 EXPECT_NE(result, 0); 25 EXPECT_TRUE(errno == EFAULT); 26 } 27 28 /** 29 * @tc.name: setxattr_002 30 * @tc.desc: This test verifies whether the setxattr function can handle invalid parameters correctly and whether the 31 * errno value can be set correctly. 32 * @tc.type: FUNC 33 */ 34 HWTEST_F(LinuxSetxattrTest, setxattr_002, TestSize.Level1) 35 { 36 const char* filePath = "setxattr.txt"; 37 int fileDescriptor = open(filePath, O_RDWR | O_CREAT, 0666); 38 char data[] = "musl"; 39 write(fileDescriptor, data, strlen(data)); 40 close(fileDescriptor); 41 const char* attrName = "user.test"; 42 const char* attrValue = "new_data"; 43 int setResult = setxattr(filePath, attrName, attrValue, strlen(attrValue), XATTR_CREATE); 44 EXPECT_EQ(setResult, 0); 45 int removeResult = remove(filePath); 46 EXPECT_TRUE(removeResult == 0); 47 }