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 <string>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <sys/stat.h>
21 #include <sys/xattr.h>
22 #include "securec.h"
23
24 using namespace testing::ext;
25 using namespace std;
26
27 static const char *TEST_FILE_PATH = "/data/local/tmp/test_file";
28 static const char *TEST_ATTRIBUTE = "user.testattribute";
29 static const char *TEST_ATTRIBUTE_VALUE = "testvalue";
30
31 class SetxattrApiTest : 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 SetxattrApiTest::SetUp()
40 {
41 int fd = open(TEST_FILE_PATH, O_CREAT | O_WRONLY, 0644);
42 close(fd);
43 }
TearDown()44 void SetxattrApiTest::TearDown()
45 {
46 unlink(TEST_FILE_PATH);
47 }
SetUpTestCase()48 void SetxattrApiTest::SetUpTestCase()
49 {
50 }
TearDownTestCase()51 void SetxattrApiTest::TearDownTestCase()
52 {
53 }
54
55 /*
56 * @tc.number : SUB_KERNEL_SYSCALL_SETXATTR_0100
57 * @tc.name : SetxattrSuccess_0001
58 * @tc.desc : Setxattr success.
59 * @tc.size : MediumTest
60 * @tc.type : Function
61 * @tc.level : Level 1
62 */
HWTEST_F(SetxattrApiTest, SetxattrSuccess_0001, Function | MediumTest | Level1)63 HWTEST_F(SetxattrApiTest, SetxattrSuccess_0001, Function | MediumTest | Level1)
64 {
65 int ret;
66 ret = setxattr(TEST_FILE_PATH, TEST_ATTRIBUTE, TEST_ATTRIBUTE_VALUE, strlen(TEST_ATTRIBUTE_VALUE) + 1, 0);
67 EXPECT_EQ(ret, 0);
68
69 char value[256];
70 ssize_t size = getxattr(TEST_FILE_PATH, TEST_ATTRIBUTE, value, sizeof(value));
71 EXPECT_GT(size, 0);
72 EXPECT_STREQ(TEST_ATTRIBUTE_VALUE, value);
73 }
74
75 /*
76 * @tc.number : SUB_KERNEL_SYSCALL_STATX_0200
77 * @tc.name : SetxattrInvalidPath_0002
78 * @tc.desc : statx invalid path.
79 * @tc.size : MediumTest
80 * @tc.type : Function
81 * @tc.level : Level 2
82 */
HWTEST_F(SetxattrApiTest, SetxattrInvalidPath_0002, Function | MediumTest | Level2)83 HWTEST_F(SetxattrApiTest, SetxattrInvalidPath_0002, Function | MediumTest | Level2)
84 {
85 int ret;
86 ret = setxattr("/non/existent/path", TEST_ATTRIBUTE, TEST_ATTRIBUTE_VALUE, strlen(TEST_ATTRIBUTE_VALUE) + 1, 0);
87 EXPECT_EQ(ret, -1);
88 EXPECT_EQ(errno, ENOENT);
89 }