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
28using namespace testing::ext;
29using namespace std;
30
31class LsetxattrApiTest : public testing::Test {
32public:
33    static void SetUpTestCase();
34    static void TearDownTestCase();
35    void SetUp();
36    void TearDown();
37private:
38};
39void LsetxattrApiTest::SetUp()
40{
41}
42void LsetxattrApiTest::TearDown()
43{
44}
45void LsetxattrApiTest::SetUpTestCase()
46{
47}
48void LsetxattrApiTest::TearDownTestCase()
49{
50}
51
52int SetExtendedAttribute(const char* linkPath, const char* attrName, const char* attrValue, int flags = 0)
53{
54    size_t attrSize = strlen(attrValue) + 1;
55    int ret = lsetxattr(linkPath, attrName, attrValue, attrSize, flags);
56    if (ret == -1) {
57        return -1;
58    }
59    return 0;
60}
61
62int GetExtendedAttribute(const char* linkPath, const char* attrName, char* buffer, size_t bufferSize)
63{
64    ssize_t ret = lgetxattr(linkPath, attrName, buffer, bufferSize);
65    if (ret == -1) {
66        return -1;
67    }
68    buffer[ret] = '\0';
69    return 0;
70}
71
72int RemoveExtendedAttribute(const char* linkPath, const char* attrName)
73{
74    int ret = lremovexattr(linkPath, attrName);
75    if (ret == -1) {
76        return -1;
77    }
78    return 0;
79}
80
81static const char* OPEN_API_TEST_FILE = "/data/local/tmp";
82const int BUFFER_SIZE = 128;
83
84/*
85 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0100
86 * @tc.name   : LsetxattrSetExtAttrSuccess_0001
87 * @tc.desc   : set file extended attribute success.
88 * @tc.size   : MediumTest
89 * @tc.type   : Function
90 * @tc.level  : Level 1
91 */
92HWTEST_F(LsetxattrApiTest, LsetxattrSetExtAttrSuccess_0001, Function | MediumTest | Level1)
93{
94    const char* targetFile = "target_file.txt";
95    char targetFilePath[BUFFER_SIZE] = {0};
96    int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile);
97    EXPECT_TRUE(num > 0);
98
99    int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644);
100    EXPECT_TRUE(fd > 0);
101    close(fd);
102
103    const char* attrName = "user.myattr";
104    const char* attrValue = "Hello, xattr!";
105    int ret = SetExtendedAttribute(targetFilePath, attrName, attrValue);
106    EXPECT_TRUE(ret == 0);
107
108    char buffer[64] = {0};
109    ret = GetExtendedAttribute(targetFilePath, attrName, buffer, sizeof(buffer));
110    EXPECT_TRUE(ret == 0);
111
112    ret = strncmp(attrValue, buffer, strlen(attrValue));
113    EXPECT_TRUE(ret == 0);
114
115    remove(targetFilePath);
116}
117
118/*
119 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0200
120 * @tc.name   : LsetxattrSetMoreExtAttrSuccess_0002
121 * @tc.desc   : set file more extended attribute success.
122 * @tc.size   : MediumTest
123 * @tc.type   : Function
124 * @tc.level  : Level 1
125 */
126HWTEST_F(LsetxattrApiTest, LsetxattrSetMoreExtAttrSuccess_0002, Function | MediumTest | Level1)
127{
128    const char* targetFile = "target_file.txt";
129    char targetFilePath[BUFFER_SIZE] = {0};
130    int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile);
131    EXPECT_TRUE(num > 0);
132
133    int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644);
134    EXPECT_TRUE(fd > 0);
135    close(fd);
136
137    const char* attrName1 = "user.myattr1";
138    const char* attrValue1 = "Hello, xattr1!";
139    int ret = SetExtendedAttribute(targetFilePath, attrName1, attrValue1);
140    EXPECT_TRUE(ret == 0);
141    char buffer[64] = {0};
142    ret = GetExtendedAttribute(targetFilePath, attrName1, buffer, sizeof(buffer));
143    EXPECT_TRUE(ret == 0);
144
145    ret = strncmp(attrValue1, buffer, strlen(attrValue1));
146    EXPECT_TRUE(ret == 0);
147
148    const char* attrName2 = "user.myattr2";
149    const char* attrValue2 = "Hello, xattr2!";
150    ret = SetExtendedAttribute(targetFilePath, attrName2, attrValue2);
151    EXPECT_TRUE(ret == 0);
152
153    memset_s(&buffer, sizeof(buffer), 0, sizeof(buffer));
154    ret = GetExtendedAttribute(targetFilePath, attrName2, buffer, sizeof(buffer));
155    EXPECT_TRUE(ret == 0);
156
157    ret = strncmp(attrValue2, buffer, strlen(attrValue2));
158    EXPECT_TRUE(ret == 0);
159
160    remove(targetFilePath);
161}
162
163/*
164 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0300
165 * @tc.name   : LsetxattrCreateExtAttrSuccess_0003
166 * @tc.desc   : create file extended attribute success.
167 * @tc.size   : MediumTest
168 * @tc.type   : Function
169 * @tc.level  : Level 1
170 */
171HWTEST_F(LsetxattrApiTest, LsetxattrCreateExtAttrSuccess_0003, Function | MediumTest | Level1)
172{
173    const char* targetFile = "target_file.txt";
174    char targetFilePath[BUFFER_SIZE] = {0};
175    int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile);
176    EXPECT_TRUE(num > 0);
177
178    int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644);
179    EXPECT_TRUE(fd > 0);
180    close(fd);
181
182    const char* attrName = "user.myattr";
183    const char* attrValue = "Hello, xattr!";
184    int ret = SetExtendedAttribute(targetFilePath, attrName, attrValue, XATTR_CREATE);
185    EXPECT_TRUE(ret == 0);
186
187    char buffer[64] = {0};
188    ret = GetExtendedAttribute(targetFilePath, attrName, buffer, sizeof(buffer));
189    EXPECT_TRUE(ret == 0);
190
191    ret = strncmp(attrValue, buffer, strlen(attrValue));
192    EXPECT_TRUE(ret == 0);
193
194    remove(targetFilePath);
195}
196
197/*
198 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0400
199 * @tc.name   : LsetxattrCreateMoreExtAttrSuccess_0004
200 * @tc.desc   : create file more extended attribute success.
201 * @tc.size   : MediumTest
202 * @tc.type   : Function
203 * @tc.level  : Level 1
204 */
205HWTEST_F(LsetxattrApiTest, LsetxattrCreateMoreExtAttrSuccess_0004, Function | MediumTest | Level1)
206{
207    const char* targetFile = "target_file.txt";
208    char targetFilePath[BUFFER_SIZE] = {0};
209    int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile);
210    EXPECT_TRUE(num > 0);
211
212    int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644);
213    EXPECT_TRUE(fd > 0);
214    close(fd);
215
216    const char* attrName1 = "user.myattr1";
217    const char* attrValue1 = "Hello, xattr1!";
218    int ret = SetExtendedAttribute(targetFilePath, attrName1, attrValue1, XATTR_CREATE);
219    EXPECT_TRUE(ret == 0);
220
221    const char* attrName2 = "user.myattr2";
222    const char* attrValue2 = "Hello, xattr2!";
223    ret = SetExtendedAttribute(targetFilePath, attrName2, attrValue2, XATTR_CREATE);
224    EXPECT_TRUE(ret == 0);
225
226    remove(targetFilePath);
227}
228
229/*
230 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0500
231 * @tc.name   : LsetxattrReplaceExtAttrSuccess_0005
232 * @tc.desc   : replace file more extended attribute success.
233 * @tc.size   : MediumTest
234 * @tc.type   : Function
235 * @tc.level  : Level 1
236 */
237HWTEST_F(LsetxattrApiTest, LsetxattrReplaceExtAttrSuccess_0005, Function | MediumTest | Level1)
238{
239    const char* targetFile = "target_file.txt";
240    char targetFilePath[BUFFER_SIZE] = {0};
241    int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile);
242    EXPECT_TRUE(num > 0);
243
244    int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644);
245    EXPECT_TRUE(fd > 0);
246    close(fd);
247
248    const char* attrName = "user.myattr";
249    const char* attrValue1 = "Hello, xattr1!";
250    int ret = SetExtendedAttribute(targetFilePath, attrName, attrValue1, XATTR_CREATE);
251    EXPECT_TRUE(ret == 0);
252
253    const char* attrValue2 = "Hello, xattr2!";
254    ret = SetExtendedAttribute(targetFilePath, attrName, attrValue2, XATTR_REPLACE);
255    EXPECT_TRUE(ret == 0);
256
257    char buffer[64] = {0};
258    ret = GetExtendedAttribute(targetFilePath, attrName, buffer, sizeof(buffer));
259    EXPECT_TRUE(ret == 0);
260
261    ret = strncmp(attrValue2, buffer, strlen(attrValue2));
262    EXPECT_TRUE(ret == 0);
263
264    remove(targetFilePath);
265}
266
267/*
268 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0600
269 * @tc.name   : LsetxattrSetLExtAttrSuccess_0006
270 * @tc.desc   : set link file extended attribute success.
271 * @tc.size   : MediumTest
272 * @tc.type   : Function
273 * @tc.level  : Level 1
274 */
275HWTEST_F(LsetxattrApiTest, LsetxattrSetLExtAttrSuccess_0006, Function | MediumTest | Level1)
276{
277    const char* targetFile = "target_file.txt";
278    const char* linkPath = "link_to_file.txt";
279
280    char targetFilePath[BUFFER_SIZE] = {0};
281    int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile);
282    EXPECT_TRUE(num > 0);
283
284    char linkFilePath[BUFFER_SIZE] = {0};
285    num = sprintf_s(linkFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, linkPath);
286    EXPECT_TRUE(num > 0);
287
288    int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644);
289    EXPECT_TRUE(fd > 0);
290    close(fd);
291
292    int newFd = open(OPEN_API_TEST_FILE, O_RDONLY, 0644);
293    EXPECT_TRUE(newFd > 0);
294
295    int ret = linkat(newFd, targetFile, newFd, linkPath, 0);
296    EXPECT_TRUE(ret == 0);
297
298    const char* attrName = "user.myattr";
299    const char* attrValue = "Hello, xattr!";
300    ret = SetExtendedAttribute(linkFilePath, attrName, attrValue, 0);
301    EXPECT_TRUE(ret == 0);
302
303    remove(targetFilePath);
304    remove(linkFilePath);
305    close(newFd);
306}
307
308/*
309 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0700
310 * @tc.name   : LsetxattrSetLMoreExtAttrSuccess_0007
311 * @tc.desc   : set link file more extended attribute success.
312 * @tc.size   : MediumTest
313 * @tc.type   : Function
314 * @tc.level  : Level 1
315 */
316HWTEST_F(LsetxattrApiTest, LsetxattrSetLMoreExtAttrSuccess_0007, Function | MediumTest | Level1)
317{
318    const char* targetFile = "target_file.txt";
319    const char* linkPath = "link_to_file.txt";
320
321    char targetFilePath[BUFFER_SIZE] = {0};
322    int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile);
323    EXPECT_TRUE(num > 0);
324
325    char linkFilePath[BUFFER_SIZE] = {0};
326    num = sprintf_s(linkFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, linkPath);
327    EXPECT_TRUE(num > 0);
328
329    int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644);
330    EXPECT_TRUE(fd > 0);
331    close(fd);
332
333    int newFd = open(OPEN_API_TEST_FILE, O_RDONLY, 0644);
334    EXPECT_TRUE(newFd > 0);
335
336    int ret = linkat(newFd, targetFile, newFd, linkPath, 0);
337    EXPECT_TRUE(ret == 0);
338
339    const char* attrName1 = "user.myattr1";
340    const char* attrValue1 = "Hello, xattr1!";
341    ret = SetExtendedAttribute(linkFilePath, attrName1, attrValue1);
342    EXPECT_TRUE(ret == 0);
343
344    const char* attrName2 = "user.myattr2";
345    const char* attrValue2 = "Hello, xattr2!";
346    ret = SetExtendedAttribute(linkFilePath, attrName2, attrValue2);
347    EXPECT_TRUE(ret == 0);
348
349    remove(targetFilePath);
350    remove(linkFilePath);
351    close(newFd);
352}
353
354/*
355 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0800
356 * @tc.name   : LsetxattrCreateLExtAttrSuccess_0008
357 * @tc.desc   : create link file extended attribute success.
358 * @tc.size   : MediumTest
359 * @tc.type   : Function
360 * @tc.level  : Level 1
361 */
362HWTEST_F(LsetxattrApiTest, LsetxattrCreateLExtAttrSuccess_0008, Function | MediumTest | Level1)
363{
364    const char* targetFile = "target_file.txt";
365    const char* linkPath = "link_to_file.txt";
366
367    char targetFilePath[BUFFER_SIZE] = {0};
368    int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile);
369    EXPECT_TRUE(num > 0);
370
371    char linkFilePath[BUFFER_SIZE] = {0};
372    num = sprintf_s(linkFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, linkPath);
373    EXPECT_TRUE(num > 0);
374
375    int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644);
376    EXPECT_TRUE(fd > 0);
377    close(fd);
378
379    int newFd = open(OPEN_API_TEST_FILE, O_RDONLY, 0644);
380    EXPECT_TRUE(newFd > 0);
381
382    int ret = linkat(newFd, targetFile, newFd, linkPath, 0);
383    EXPECT_TRUE(ret == 0);
384
385    const char* attrName = "user.myattr";
386    const char* attrValue = "Hello, xattr!";
387    ret = SetExtendedAttribute(linkFilePath, attrName, attrValue, XATTR_CREATE);
388    EXPECT_TRUE(ret == 0);
389
390    remove(targetFilePath);
391    remove(linkFilePath);
392    close(newFd);
393}
394
395/*
396 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_0900
397 * @tc.name   : LsetxattrCreateLMoreExtAttrSuccess_0009
398 * @tc.desc   : create link file more extended attribute success.
399 * @tc.size   : MediumTest
400 * @tc.type   : Function
401 * @tc.level  : Level 1
402 */
403HWTEST_F(LsetxattrApiTest, LsetxattrCreateLMoreExtAttrSuccess_0009, Function | MediumTest | Level1)
404{
405    const char* targetFile = "target_file.txt";
406    const char* linkPath = "link_to_file.txt";
407
408    char targetFilePath[BUFFER_SIZE] = {0};
409    int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile);
410    EXPECT_TRUE(num > 0);
411
412    char linkFilePath[BUFFER_SIZE] = {0};
413    num = sprintf_s(linkFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, linkPath);
414    EXPECT_TRUE(num > 0);
415
416    int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644);
417    EXPECT_TRUE(fd > 0);
418    close(fd);
419
420    int newFd = open(OPEN_API_TEST_FILE, O_RDONLY, 0644);
421    EXPECT_TRUE(newFd > 0);
422
423    int ret = linkat(newFd, targetFile, newFd, linkPath, 0);
424    EXPECT_TRUE(ret == 0);
425
426    const char* attrName1 = "user.myattr1";
427    const char* attrValue1 = "Hello, xattr1!";
428    ret = SetExtendedAttribute(linkFilePath, attrName1, attrValue1, XATTR_CREATE);
429    EXPECT_TRUE(ret == 0);
430
431    const char* attrName2 = "user.myattr2";
432    const char* attrValue2 = "Hello, xattr2!";
433    ret = SetExtendedAttribute(linkFilePath, attrName2, attrValue2, XATTR_CREATE);
434    EXPECT_TRUE(ret == 0);
435
436    remove(targetFilePath);
437    remove(linkFilePath);
438    close(newFd);
439}
440
441/*
442 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1000
443 * @tc.name   : LsetxattrReplaceLExtAttrSuccess_0010
444 * @tc.desc   : replace link file more extended attribute success.
445 * @tc.size   : MediumTest
446 * @tc.type   : Function
447 * @tc.level  : Level 1
448 */
449HWTEST_F(LsetxattrApiTest, LsetxattrReplaceLExtAttrSuccess_0010, Function | MediumTest | Level1)
450{
451    const char* targetFile = "target_file.txt";
452    const char* linkPath = "link_to_file.txt";
453
454    char targetFilePath[BUFFER_SIZE] = {0};
455    int num = sprintf_s(targetFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, targetFile);
456    EXPECT_TRUE(num > 0);
457
458    char linkFilePath[BUFFER_SIZE] = {0};
459    num = sprintf_s(linkFilePath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, linkPath);
460    EXPECT_TRUE(num > 0);
461
462    int fd = open(targetFilePath, O_WRONLY | O_CREAT, 0644);
463    EXPECT_TRUE(fd > 0);
464    close(fd);
465
466    int newFd = open(OPEN_API_TEST_FILE, O_RDONLY, 0644);
467    EXPECT_TRUE(newFd > 0);
468
469    int ret = linkat(newFd, targetFile, newFd, linkPath, 0);
470    EXPECT_TRUE(ret == 0);
471
472    const char* attrName = "user.myattr";
473    const char* attrValue1 = "Hello, xattr1!";
474    ret = SetExtendedAttribute(linkFilePath, attrName, attrValue1, XATTR_CREATE);
475    EXPECT_TRUE(ret == 0);
476
477    const char* attrValue2 = "Hello, xattr2!";
478    ret = SetExtendedAttribute(linkFilePath, attrName, attrValue2, XATTR_REPLACE);
479    EXPECT_TRUE(ret == 0);
480
481    remove(targetFilePath);
482    remove(linkFilePath);
483    close(newFd);
484}
485
486/*
487 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1100
488 * @tc.name   : LsetxattrSetPExtAttrSuccess_0011
489 * @tc.desc   : set path extended attribute success.
490 * @tc.size   : MediumTest
491 * @tc.type   : Function
492 * @tc.level  : Level 1
493 */
494HWTEST_F(LsetxattrApiTest, LsetxattrSetPExtAttrSuccess_0011, Function | MediumTest | Level1)
495{
496    const char* attrName = "user.myattr";
497    const char* attrValue = "Hello, xattr!";
498    int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue);
499    EXPECT_TRUE(ret == 0);
500
501    ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName);
502    EXPECT_TRUE(ret == 0);
503}
504
505/*
506 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1200
507 * @tc.name   : LsetxattrSetPMoreExtAttrSuccess_0012
508 * @tc.desc   : set path more extended attribute success.
509 * @tc.size   : MediumTest
510 * @tc.type   : Function
511 * @tc.level  : Level 1
512 */
513HWTEST_F(LsetxattrApiTest, LsetxattrSetPMoreExtAttrSuccess_0012, Function | MediumTest | Level1)
514{
515    const char* attrName1 = "user.myattr1";
516    const char* attrValue1 = "Hello, xattr1!";
517    int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName1, attrValue1);
518    EXPECT_TRUE(ret == 0);
519
520    const char* attrName2 = "user.myattr2";
521    const char* attrValue2 = "Hello, xattr2!";
522    ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName2, attrValue2);
523    EXPECT_TRUE(ret == 0);
524
525    ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName1);
526    EXPECT_TRUE(ret == 0);
527
528    ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName2);
529    EXPECT_TRUE(ret == 0);
530}
531
532/*
533 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1300
534 * @tc.name   : LsetxattrCreatePExtAttrSuccess_0013
535 * @tc.desc   : create path extended attribute success.
536 * @tc.size   : MediumTest
537 * @tc.type   : Function
538 * @tc.level  : Level 1
539 */
540HWTEST_F(LsetxattrApiTest, LsetxattrCreatePExtAttrSuccess_0013, Function | MediumTest | Level1)
541{
542    const char* attrName = "user.myattr";
543    const char* attrValue = "Hello, xattr!";
544    int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue, XATTR_CREATE);
545    EXPECT_TRUE(ret == 0);
546
547    ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName);
548    EXPECT_TRUE(ret == 0);
549}
550
551/*
552 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1400
553 * @tc.name   : LsetxattrCreatePMoreExtAttrSuccess_0014
554 * @tc.desc   : create path more extended attribute success.
555 * @tc.size   : MediumTest
556 * @tc.type   : Function
557 * @tc.level  : Level 1
558 */
559HWTEST_F(LsetxattrApiTest, LsetxattrCreatePMoreExtAttrSuccess_0014, Function | MediumTest | Level1)
560{
561    const char* attrName1 = "user.myattr1";
562    const char* attrValue1 = "Hello, xattr1!";
563    int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName1, attrValue1, XATTR_CREATE);
564    EXPECT_TRUE(ret == 0);
565
566    const char* attrName2 = "user.myattr2";
567    const char* attrValue2 = "Hello, xattr2!";
568    ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName2, attrValue2, XATTR_CREATE);
569    EXPECT_TRUE(ret == 0);
570
571    ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName1);
572    EXPECT_TRUE(ret == 0);
573
574    ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName2);
575    EXPECT_TRUE(ret == 0);
576}
577
578/*
579 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1500
580 * @tc.name   : LsetxattrReplacePExtAttrSuccess_0015
581 * @tc.desc   : replace path more extended attribute success.
582 * @tc.size   : MediumTest
583 * @tc.type   : Function
584 * @tc.level  : Level 1
585 */
586HWTEST_F(LsetxattrApiTest, LsetxattrReplacePExtAttrSuccess_0015, Function | MediumTest | Level1)
587{
588    const char* attrName = "user.myattr";
589    const char* attrValue1 = "Hello, xattr1!";
590    int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue1, XATTR_CREATE);
591    EXPECT_TRUE(ret == 0);
592
593    char buffer[64] = {0};
594    ret = GetExtendedAttribute(OPEN_API_TEST_FILE, attrName, buffer, sizeof(buffer));
595    EXPECT_TRUE(ret == 0);
596
597    ret = strncmp(attrValue1, buffer, strlen(attrValue1));
598    EXPECT_TRUE(ret == 0);
599
600    const char* attrValue2 = "Hello, xattr2!";
601    ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue2, XATTR_REPLACE);
602    EXPECT_TRUE(ret == 0);
603
604    memset_s(&buffer, sizeof(buffer), 0, sizeof(buffer));
605    ret = GetExtendedAttribute(OPEN_API_TEST_FILE, attrName, buffer, sizeof(buffer));
606    EXPECT_TRUE(ret == 0);
607
608    ret = strncmp(attrValue2, buffer, strlen(attrValue2));
609    EXPECT_TRUE(ret == 0);
610
611    ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName);
612    EXPECT_TRUE(ret == 0);
613}
614
615/*
616 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1600
617 * @tc.name   : LsetxattrCreateExtAttrFailed_0016
618 * @tc.desc   : create non path extended attribute failed.
619 * @tc.size   : MediumTest
620 * @tc.type   : Function
621 * @tc.level  : Level 2
622 */
623HWTEST_F(LsetxattrApiTest, LsetxattrCreateExtAttrFailed_0016, Function | MediumTest | Level2)
624{
625    const char* nonDirPath = "non_existing_dir";
626    char tmpNonDirPath[BUFFER_SIZE] = {0};
627    int num = sprintf_s(tmpNonDirPath, BUFFER_SIZE, "%s/%s", OPEN_API_TEST_FILE, nonDirPath);
628    EXPECT_TRUE(num > 0);
629
630    errno = 0;
631    const char* attrName = "user.myattr";
632    const char* attrValue = "Hello, xattr!";
633    int ret = SetExtendedAttribute(tmpNonDirPath, attrName, attrValue, XATTR_CREATE);
634    EXPECT_TRUE(ret == -1);
635    EXPECT_TRUE(errno == ENOENT);
636}
637
638/*
639 * @tc.number : SUB_KERNEL_SYSCALL_LSETXATTR_1700
640 * @tc.name   : LsetxattrCreateSameExtAttrFailed_0017
641 * @tc.desc   : create file same extended attribute failed.
642 * @tc.size   : MediumTest
643 * @tc.type   : Function
644 * @tc.level  : Level 2
645 */
646HWTEST_F(LsetxattrApiTest, LsetxattrCreateSameExtAttrFailed_0017, Function | MediumTest | Level2)
647{
648    const char* attrName = "user.myattr";
649    const char* attrValue = "Hello, xattr1!";
650    int ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue, XATTR_CREATE);
651    EXPECT_TRUE(ret == 0);
652
653    errno = 0;
654    ret = SetExtendedAttribute(OPEN_API_TEST_FILE, attrName, attrValue, XATTR_CREATE);
655    EXPECT_TRUE(ret == -1);
656    EXPECT_TRUE(errno == EEXIST);
657
658    ret = RemoveExtendedAttribute(OPEN_API_TEST_FILE, attrName);
659    EXPECT_TRUE(ret == 0);
660}