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 <string>
19 #include <vector>
20 #include <fcntl.h>
21 #include <unistd.h>
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 static const char* TEST_FILE = "/data/local/tmp/test_file.txt";
32 static const int BUFFER_SIZE = 128;
33 static const int PATH_MAX_LEN = 128;
34
35 class GetxattrApiTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp();
40 void TearDown();
41 private:
42 };
SetUp()43 void GetxattrApiTest::SetUp()
44 {
45 int fd = open(TEST_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
46 close(fd);
47 }
TearDown()48 void GetxattrApiTest::TearDown()
49 {
50 (void)remove(TEST_FILE);
51 }
SetUpTestCase()52 void GetxattrApiTest::SetUpTestCase()
53 {
54 }
TearDownTestCase()55 void GetxattrApiTest::TearDownTestCase()
56 {
57 }
58
59 /*
60 * @tc.number : SUB_KERNEL_SYSCALL_GETXATTR_0100
61 * @tc.name : GetxattrGetExistAttrSuccess_0001
62 * @tc.desc : Get the exist file attribute value success.
63 * @tc.size : MediumTest
64 * @tc.type : Function
65 * @tc.level : Level 1
66 */
HWTEST_F(GetxattrApiTest, GetxattrGetExistAttrSuccess_0001, Function | MediumTest | Level1)67 HWTEST_F(GetxattrApiTest, GetxattrGetExistAttrSuccess_0001, Function | MediumTest | Level1)
68 {
69 ssize_t size;
70 const char* name = "user.example";
71 const char* setValue = "Hello World";
72 char value[PATH_MAX_LEN] = { 0 };
73 int ret = setxattr(TEST_FILE, name, setValue, strlen(setValue), 0);
74 EXPECT_EQ(ret, 0);
75
76 size = getxattr(TEST_FILE, name, value, sizeof(value) - 1);
77 EXPECT_EQ(size, strlen(setValue));
78 EXPECT_STREQ(value, setValue);
79 }
80
81 /*
82 * @tc.number : SUB_KERNEL_SYSCALL_GETXATTR_0200
83 * @tc.name : GetxattrGetUnknownAttrValueFailed_0002
84 * @tc.desc : Get the unknown file attribute value failed, errno ENODATA.
85 * @tc.size : MediumTest
86 * @tc.type : Function
87 * @tc.level : Level 2
88 */
HWTEST_F(GetxattrApiTest, GetxattrGetUnknownAttrValueFailed_0002, Function | MediumTest | Level2)89 HWTEST_F(GetxattrApiTest, GetxattrGetUnknownAttrValueFailed_0002, Function | MediumTest | Level2)
90 {
91 const char* name = "user.example1";
92 char value[BUFFER_SIZE] = {0};
93 errno = 0;
94 ssize_t size = getxattr(TEST_FILE, name, value, sizeof(value) - 1);
95 EXPECT_TRUE(size == -1);
96 EXPECT_TRUE(errno == ENODATA);
97 }