1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
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 <fcntl.h>
17#include <sys/xattr.h>
18#include "functionalext.h"
19
20const char *path = "file.txt";
21const char *name = "user.txt";
22const char *value = "dat";
23const int size = 3;
24
25/**
26 * @tc.name      : lsetxattr_0100
27 * @tc.desc      : Verify lsetxattr process success.
28 * @tc.level     : Level 0
29 */
30void lsetxattr_0100(void)
31{
32    int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
33    if (fd < 0) {
34        t_error("%s failed: fd = %d\n", __func__, fd);
35        return;
36    }
37
38    char str[] = "dat";
39    write(fd, str, sizeof(str));
40    close(fd);
41
42    int ret = lsetxattr(path, name, value, strlen(value), XATTR_CREATE);
43    if (ret != 0) {
44        t_error("%s failed: ret = %d\n", __func__, ret);
45        return;
46    }
47
48    char buf[BUFSIZ] = {0};
49
50    ret = lgetxattr(path, name, buf, sizeof(buf));
51    EXPECT_EQ("lgetxattr_0100", ret, size);
52    EXPECT_STREQ("lgetxattr_0100", buf, str);
53    ret = remove(path);
54    EXPECT_EQ("lgetxattr_0100", ret, 0);
55}
56
57/**
58 * @tc.name      : lsetxattr_0200
59 * @tc.desc      : Verify lsetxattr process fail bacause param is null.
60 * @tc.level     : Level 0
61 */
62void lsetxattr_0200(void)
63{
64    int ret = lsetxattr(NULL, NULL, NULL, -1, -1);
65    EXPECT_EQ("lgetxattr_0200", ret, -1);
66}
67
68int main(void)
69{
70    lsetxattr_0100();
71    lsetxattr_0200();
72    return t_status;
73}
74