• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <fcntl.h>
2 #include <gtest/gtest.h>
3 #include <stdio.h>
4 #include <sys/xattr.h>
5 
6 using namespace testing::ext;
7 
8 class LinuxGetxattrTest : public testing::Test {
SetUp()9     void SetUp() override {}
TearDown()10     void TearDown() override {}
11 };
12 
13 constexpr int SIZE = 4;
14 
15 /**
16  * @tc.name: getxattr_001
17  * @tc.desc: This test verifies whether the getxattr function can correctly return -1 when passing invalid parameters,
18  *           that is, whether it can handle invalid attribute query requests correctly.
19  * @tc.type: FUNC
20  */
21 HWTEST_F(LinuxGetxattrTest, getxattr_001, TestSize.Level1)
22 {
23     int result = getxattr(nullptr, nullptr, nullptr, -1);
24     EXPECT_EQ(result, -1);
25 }
26 
27 /**
28  * @tc.name: getxattr_002
29  * @tc.desc: This test tests whether the getxattr function can correctly obtain the extension properties of a file.
30  * @tc.type: FUNC
31  */
32 HWTEST_F(LinuxGetxattrTest, getxattr_002, TestSize.Level1)
33 {
34     const char* filePath = "getxattr.txt";
35     int fileDescriptor = open(filePath, O_RDWR | O_CREAT, 0666);
36     char data[] = "musl";
37     write(fileDescriptor, data, sizeof(data));
38     close(fileDescriptor);
39     const char* attrName = "user.test";
40     const char* attrValue = "musl";
41     int setResult = setxattr(filePath, attrName, attrValue, strlen(attrValue), XATTR_CREATE);
42     EXPECT_EQ(setResult, 0);
43     char buffer[BUFSIZ] = { 0 };
44     int getResult = getxattr(filePath, attrName, buffer, sizeof(buffer));
45     EXPECT_EQ(getResult, SIZE);
46     EXPECT_STREQ(buffer, data);
47     int removeResult = remove(filePath);
48     EXPECT_EQ(removeResult, 0);
49 }