1 /*
2 * Copyright (C) 2024 HiHope Open Source Organization.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cstdio>
17 #include <cstdlib>
18 #include <fcntl.h>
19 #include <string>
20 #include <unistd.h>
21 #include <vector>
22 #include <gtest/gtest.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/xattr.h>
26 #include "securec.h"
27
28 using namespace testing::ext;
29 using namespace std;
30
31 class LlistxattrApiTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp();
36 void TearDown();
37 private:
38 };
SetUp()39 void LlistxattrApiTest::SetUp()
40 {
41 }
TearDown()42 void LlistxattrApiTest::TearDown()
43 {
44 }
SetUpTestCase()45 void LlistxattrApiTest::SetUpTestCase()
46 {
47 }
TearDownTestCase()48 void LlistxattrApiTest::TearDownTestCase()
49 {
50 }
51
52 static const char* OPEN_API_TEST_FILE = "/data/local/tmp";
53 const int BUFFER_SIZE = 128;
54
55 /*
56 * @tc.number : SUB_KERNEL_SYSCALL_LLISTXATTR_0100
57 * @tc.name : LlistxattrListExtendedAttrSuccess_0001
58 * @tc.desc : list the extended attributes of the link specified path success.
59 * @tc.size : MediumTest
60 * @tc.type : Function
61 * @tc.level : Level 1
62 */
HWTEST_F(LlistxattrApiTest, LlistxattrListExtendedAttrSuccess_0001, Function | MediumTest | Level1)63 HWTEST_F(LlistxattrApiTest, LlistxattrListExtendedAttrSuccess_0001, Function | MediumTest | Level1)
64 {
65 ssize_t attrlistLen = llistxattr(OPEN_API_TEST_FILE, nullptr, 0);
66 EXPECT_TRUE(attrlistLen > 0);
67
68 char* attrlist = new char[attrlistLen + 1];
69 memset_s(attrlist, attrlistLen + 1, 0, attrlistLen + 1);
70
71 attrlistLen = llistxattr(OPEN_API_TEST_FILE, attrlist, attrlistLen);
72 EXPECT_TRUE(attrlistLen > 0);
73
74 delete[] attrlist;
75 }
76
77 /*
78 * @tc.number : SUB_KERNEL_SYSCALL_LLISTXATTR_0200
79 * @tc.name : LlistxattrListExtendAttrSuccess_0002
80 * @tc.desc : list the extended attributes of the specified link file success.
81 * @tc.size : MediumTest
82 * @tc.type : Function
83 * @tc.level : Level 1
84 */
HWTEST_F(LlistxattrApiTest, LlistxattrListExtendAttrSuccess_0002, Function | MediumTest | Level1)85 HWTEST_F(LlistxattrApiTest, LlistxattrListExtendAttrSuccess_0002, Function | MediumTest | Level1)
86 {
87 char tmpPath[BUFFER_SIZE] = {0};
88 int num = sprintf_s(tmpPath, BUFFER_SIZE, "%s/existing_file.txt", OPEN_API_TEST_FILE);
89 EXPECT_TRUE(num > 0);
90
91 int fd = open(tmpPath, O_WRONLY | O_CREAT, 0644);
92 EXPECT_TRUE(fd > 0);
93 close(fd);
94
95 ssize_t attrlistLen = llistxattr(tmpPath, nullptr, 0);
96 EXPECT_TRUE(attrlistLen > 0);
97
98 char* attrlist = new char[attrlistLen + 1];
99 memset_s(attrlist, attrlistLen + 1, 0, attrlistLen + 1);
100
101 attrlistLen = llistxattr(tmpPath, attrlist, attrlistLen);
102 EXPECT_TRUE(attrlistLen > 0);
103
104 delete[] attrlist;
105 remove(tmpPath);
106 }
107
108 /*
109 * @tc.number : SUB_KERNEL_SYSCALL_LLISTXATTR_0300
110 * @tc.name : LlistxattrListExtendAttrNonExistFileFailed_0003
111 * @tc.desc : list the extended attributes for non-existent link file failed.
112 * @tc.size : MediumTest
113 * @tc.type : Function
114 * @tc.level : Level 2
115 */
HWTEST_F(LlistxattrApiTest, LlistxattrListExtendAttrNonExistFileFailed_0003, Function | MediumTest | Level2)116 HWTEST_F(LlistxattrApiTest, LlistxattrListExtendAttrNonExistFileFailed_0003, Function | MediumTest | Level2)
117 {
118 char tmpPath[BUFFER_SIZE] = {0};
119 int num = sprintf_s(tmpPath, BUFFER_SIZE, "%s/non_existing_file.txt", OPEN_API_TEST_FILE);
120 EXPECT_TRUE(num > 0);
121 errno = 0;
122 ssize_t attrlistLen = llistxattr(tmpPath, nullptr, 0);
123 EXPECT_TRUE(attrlistLen == -1);
124 EXPECT_TRUE(errno == ENOENT);
125 }
126