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 LgetxattrApiTest : 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 LgetxattrApiTest::SetUp()
40 {
41 }
TearDown()42 void LgetxattrApiTest::TearDown()
43 {
44 }
SetUpTestCase()45 void LgetxattrApiTest::SetUpTestCase()
46 {
47 }
TearDownTestCase()48 void LgetxattrApiTest::TearDownTestCase()
49 {
50 }
51
52 static const char* OPEN_API_TEST_FILE = "/data/local/tmp/";
53 static const int MAX_LEN = 128;
54
55 /*
56 * @tc.number : SUB_KERNEL_SYSCALL_LGETXATTR_0100
57 * @tc.name : LgetxattrGetValidAttrSuccess_0001
58 * @tc.desc : lgetxattr get valid attr success.
59 * @tc.size : MediumTest
60 * @tc.type : Function
61 * @tc.level : Level 1
62 */
HWTEST_F(LgetxattrApiTest, LgetxattrGetValidAttrSuccess_0001, Function | MediumTest | Level1)63 HWTEST_F(LgetxattrApiTest, LgetxattrGetValidAttrSuccess_0001, Function | MediumTest | Level1)
64 {
65 const char* name = "user.example";
66 const char* setValue = "Hello World";
67 int ret = setxattr(OPEN_API_TEST_FILE, name, setValue, strlen(setValue), 0);
68 EXPECT_EQ(ret, 0);
69
70 char value[MAX_LEN] = {0};
71 ssize_t size = lgetxattr(OPEN_API_TEST_FILE, name, value, sizeof(value) - 1);
72 EXPECT_EQ(size, strlen(setValue));
73 EXPECT_STREQ(value, setValue);
74 }
75
76 /*
77 * @tc.number : SUB_KERNEL_SYSCALL_LGETXATTR_0200
78 * @tc.name : LgetxattrGetInvalidAttrFailed_0002
79 * @tc.desc : lgetxattr get invalid attr failed, errno is ENODATA.
80 * @tc.size : MediumTest
81 * @tc.type : Function
82 * @tc.level : Level 2
83 */
HWTEST_F(LgetxattrApiTest, LgetxattrGetInvalidAttrFailed_0002, Function | MediumTest | Level2)84 HWTEST_F(LgetxattrApiTest, LgetxattrGetInvalidAttrFailed_0002, Function | MediumTest | Level2)
85 {
86 const char* name = "user.example1";
87 char value[MAX_LEN] = {0};
88 errno = 0;
89 ssize_t size = lgetxattr(OPEN_API_TEST_FILE, name, value, sizeof(value) - 1);
90 EXPECT_EQ(size, -1);
91 EXPECT_EQ(errno, ENODATA);
92 }
93