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 LremovexattrApiTest : 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 LremovexattrApiTest::SetUp()
40 {
41 }
TearDown()42 void LremovexattrApiTest::TearDown()
43 {
44 }
SetUpTestCase()45 void LremovexattrApiTest::SetUpTestCase()
46 {
47 }
TearDownTestCase()48 void LremovexattrApiTest::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_LREMOVEXATTR_0100
57  * @tc.name   : LremovexattrDeleteExtentAttSuccess_0001
58  * @tc.desc   : delete the specified path extension attribute success.
59  * @tc.size   : MediumTest
60  * @tc.type   : Function
61  * @tc.level  : Level 1
62  */
HWTEST_F(LremovexattrApiTest, LremovexattrDeleteExtentAttSuccess_0001, Function | MediumTest | Level1)63 HWTEST_F(LremovexattrApiTest, LremovexattrDeleteExtentAttSuccess_0001, Function | MediumTest | Level1)
64 {
65     const char* name = "user.example";
66     const char* setValue = "Hello World";
67     ssize_t size = strlen(setValue) + 1;
68     int result = setxattr(OPEN_API_TEST_FILE, name, setValue, size, 0);
69     EXPECT_TRUE(result != -1);
70 
71     int ret = lremovexattr(OPEN_API_TEST_FILE, name);
72     EXPECT_TRUE(ret == 0);
73 }
74 
75 /*
76  * @tc.number : SUB_KERNEL_SYSCALL_LREMOVEXATTR_0200
77  * @tc.name   : LremovexattrDeleteNonExtentAttrFailed_0002
78  * @tc.desc   : delete the specified path non extension attribute failed.
79  * @tc.size   : MediumTest
80  * @tc.type   : Function
81  * @tc.level  : Level 2
82  */
HWTEST_F(LremovexattrApiTest, LremovexattrDeleteNonExtentAttrFailed_0002, Function | MediumTest | Level2)83 HWTEST_F(LremovexattrApiTest, LremovexattrDeleteNonExtentAttrFailed_0002, Function | MediumTest | Level2)
84 {
85     const char* name = "user.example";
86     errno = 0;
87     int ret = lremovexattr(OPEN_API_TEST_FILE, name);
88     EXPECT_TRUE(ret == -1);
89     EXPECT_TRUE(errno == ENODATA);
90 }
91 
92 /*
93  * @tc.number : SUB_KERNEL_SYSCALL_LREMOVEXATTR_0300
94  * @tc.name   : LremovexattrDeleteNonPathExtensionAttributeFailed_0003
95  * @tc.desc   : delete the non path extension attribute failed.
96  * @tc.size   : MediumTest
97  * @tc.type   : Function
98  * @tc.level  : Level 2
99  */
HWTEST_F(LremovexattrApiTest, LremovexattrDeleteNonPathExtensionAttributeFailed_0003, Function | MediumTest | Level2)100 HWTEST_F(LremovexattrApiTest, LremovexattrDeleteNonPathExtensionAttributeFailed_0003, Function | MediumTest | Level2)
101 {
102     const char* name = "user.example";
103     char tmpPath[BUFFER_SIZE] = {0};
104     int num = sprintf_s(tmpPath, BUFFER_SIZE, "%s/non_existing_dir", OPEN_API_TEST_FILE);
105     EXPECT_TRUE(num > 0);
106 
107     errno = 0;
108     int ret = lremovexattr(tmpPath, name);
109     EXPECT_TRUE(ret == -1);
110     EXPECT_TRUE(errno == ENOENT);
111 }
112 
113 /*
114  * @tc.number : SUB_KERNEL_SYSCALL_LREMOVEXATTR_0400
115  * @tc.name   : LremovexattrDeleteFileExtentAttrSuccess_0004
116  * @tc.desc   : delete the specified file extension attribute success.
117  * @tc.size   : MediumTest
118  * @tc.type   : Function
119  * @tc.level  : Level 1
120  */
HWTEST_F(LremovexattrApiTest, LremovexattrDeleteFileExtentAttrSuccess_0004, Function | MediumTest | Level1)121 HWTEST_F(LremovexattrApiTest, LremovexattrDeleteFileExtentAttrSuccess_0004, Function | MediumTest | Level1)
122 {
123     char tmpPath[BUFFER_SIZE] = {0};
124     int num = sprintf_s(tmpPath, BUFFER_SIZE, "%s/existing_file.txt", OPEN_API_TEST_FILE);
125     EXPECT_TRUE(num > 0);
126 
127     int fd = open(tmpPath, O_WRONLY | O_CREAT, 0644);
128     EXPECT_TRUE(fd > 0);
129     close(fd);
130 
131     const char* name = "user.example";
132     const char* setValue = "Hello World";
133     ssize_t size = strlen(setValue) + 1;
134     int result = setxattr(tmpPath, name, setValue, size, 0);
135     EXPECT_TRUE(result != -1);
136 
137     int ret = lremovexattr(tmpPath, name);
138     EXPECT_TRUE(ret == 0);
139 
140     remove(tmpPath);
141 }
142 
143 /*
144  * @tc.number : SUB_KERNEL_SYSCALL_LREMOVEXATTR_0500
145  * @tc.name   : LremovexattrDeleteSNonExtentAttrFailed_0005
146  * @tc.desc   : delete the specified file non extension attribute failed.
147  * @tc.size   : MediumTest
148  * @tc.type   : Function
149  * @tc.level  : Level 2
150  */
HWTEST_F(LremovexattrApiTest, LremovexattrDeleteSNonExtentAttrFailed_0005, Function | MediumTest | Level2)151 HWTEST_F(LremovexattrApiTest, LremovexattrDeleteSNonExtentAttrFailed_0005, Function | MediumTest | Level2)
152 {
153     const char* name = "user.example";
154 
155     char tmpPath[BUFFER_SIZE] = {0};
156     int num = sprintf_s(tmpPath, BUFFER_SIZE, "%s/existing_file.txt", OPEN_API_TEST_FILE);
157     EXPECT_TRUE(num > 0);
158 
159     int fd = open(tmpPath, O_WRONLY | O_CREAT, 0644);
160     EXPECT_TRUE(fd > 0);
161     close(fd);
162 
163     errno = 0;
164     int ret = lremovexattr(tmpPath, name);
165     EXPECT_TRUE(ret == -1);
166     EXPECT_TRUE(errno == ENODATA);
167     remove(tmpPath);
168 }
169 
170 /*
171  * @tc.number : SUB_KERNEL_SYSCALL_LREMOVEXATTR_0600
172  * @tc.name   : LremovexattrDeleteNonFileExtensionAttributeFailed_0006
173  * @tc.desc   : delete the non file extension attribute failed.
174  * @tc.size   : MediumTest
175  * @tc.type   : Function
176  * @tc.level  : Level 2
177  */
HWTEST_F(LremovexattrApiTest, LremovexattrDeleteNonFileExtensionAttributeFailed_0006, Function | MediumTest | Level2)178 HWTEST_F(LremovexattrApiTest, LremovexattrDeleteNonFileExtensionAttributeFailed_0006, Function | MediumTest | Level2)
179 {
180     const char* name = "user.example";
181     char tmpPath[BUFFER_SIZE] = {0};
182     int num = sprintf_s(tmpPath, BUFFER_SIZE, "%s/non_existing_file.txt", OPEN_API_TEST_FILE);
183     EXPECT_TRUE(num > 0);
184 
185     errno = 0;
186     int ret = lremovexattr(tmpPath, name);
187     EXPECT_TRUE(ret == -1);
188     EXPECT_TRUE(errno == ENOENT);
189 }
190 
191