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 <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <string>
20 #include <vector>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <gtest/gtest.h>
24 #include <sys/file.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/xattr.h>
28 #include "securec.h"
29
30 using namespace testing::ext;
31 using namespace std;
32
33 static const int LIST_LEN = 128;
34 static const char *TEST_FILE = "/data/local/tmp/flistxattr.txt";
35 mode_t MODE_0644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
36 static const char *XATTR_NAME = "user.author";
37 static const char *AUTHOR_NAME = "sun";
38 size_t g_length = strlen(AUTHOR_NAME) + 1;
39
40 class FlistxattrApiTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 private:
47 };
SetUp()48 void FlistxattrApiTest::SetUp()
49 {
50 }
TearDown()51 void FlistxattrApiTest::TearDown()
52 {
53 }
SetUpTestCase()54 void FlistxattrApiTest::SetUpTestCase()
55 {
56 }
TearDownTestCase()57 void FlistxattrApiTest::TearDownTestCase()
58 {
59 }
60
61 /*
62 * @tc.number : SUB_KERNEL_SYSCALL_FLISTXATTR_0100
63 * @tc.name : FlistxattrValidFdAttrSuccess_0001
64 * @tc.desc : flistxattr list valid test file attribute success.
65 * @tc.size : MediumTest
66 * @tc.type : Function
67 * @tc.level : Level 1
68 */
HWTEST_F(FlistxattrApiTest, FlistxattrValidFdAttrSuccess_0001, Function | MediumTest | Level1)69 HWTEST_F(FlistxattrApiTest, FlistxattrValidFdAttrSuccess_0001, Function | MediumTest | Level1)
70 {
71 ssize_t ret = -1;
72 char list[LIST_LEN];
73 int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644);
74 EXPECT_TRUE(fd > 0);
75 ret = flistxattr(fd, list, 1024);
76 EXPECT_NE(ret, -1);
77
78 close(fd);
79 remove(TEST_FILE);
80 }
81
82 /*
83 * @tc.number : SUB_KERNEL_SYSCALL_FLISTXATTR_0200
84 * @tc.name : FlistxattrInvalidFdAttrFail_0002
85 * @tc.desc : flistxattr list invalid fd attribute fail, errno EBADF.
86 * @tc.size : MediumTest
87 * @tc.type : Function
88 * @tc.level : Level 2
89 */
HWTEST_F(FlistxattrApiTest, FlistxattrInvalidFdAttrFail_0002, Function | MediumTest | Level2)90 HWTEST_F(FlistxattrApiTest, FlistxattrInvalidFdAttrFail_0002, Function | MediumTest | Level2)
91 {
92 char list[LIST_LEN];
93 int invalidFd = -1;
94 errno = 0;
95 ssize_t ret = flistxattr(invalidFd, list, 1024);
96 EXPECT_EQ(ret, -1);
97 EXPECT_EQ(errno, EBADF);
98 }
99
100 /*
101 * @tc.number : SUB_KERNEL_SYSCALL_FLISTXATTR_0300
102 * @tc.name : FlistxattrListAttrInNullFail_0003
103 * @tc.desc : flistxattr can't list file attribute in nullptr, errno EFAULT.
104 * @tc.size : MediumTest
105 * @tc.type : Function
106 * @tc.level : Level 2
107 */
HWTEST_F(FlistxattrApiTest, FlistxattrListAttrInNullFail_0003, Function | MediumTest | Level2)108 HWTEST_F(FlistxattrApiTest, FlistxattrListAttrInNullFail_0003, Function | MediumTest | Level2)
109 {
110 ssize_t ret = -1;
111 char *list = nullptr;
112 int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644);
113 EXPECT_TRUE(fd > 0);
114 errno = 0;
115 ret = flistxattr(fd, list, 1024);
116 EXPECT_EQ(ret, -1);
117 EXPECT_EQ(list, nullptr);
118 EXPECT_EQ(errno, EFAULT);
119
120 close(fd);
121 remove(TEST_FILE);
122 }
123
124 /*
125 * @tc.number : SUB_KERNEL_SYSCALL_FLISTXATTR_0400
126 * @tc.name : FlistxattrListShorterAttrFail_0004
127 * @tc.desc : flistxattr can't list file attribute with size 1 shorter than expected, errno ERANGE.
128 * @tc.size : MediumTest
129 * @tc.type : Function
130 * @tc.level : Level 2
131 */
HWTEST_F(FlistxattrApiTest, FlistxattrListShorterAttrFail_0004, Function | MediumTest | Level2)132 HWTEST_F(FlistxattrApiTest, FlistxattrListShorterAttrFail_0004, Function | MediumTest | Level2)
133 {
134 ssize_t ret = -1;
135 char list[LIST_LEN];
136 memset_s(list, LIST_LEN, 0, sizeof(list));
137 int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644);
138 EXPECT_TRUE(fd > 0);
139 ret = setxattr(TEST_FILE, XATTR_NAME, AUTHOR_NAME, g_length, 0);
140 EXPECT_EQ(ret, 0);
141
142 errno = 0;
143 ret = flistxattr(fd, list, 1);
144 EXPECT_EQ(errno, ERANGE);
145 EXPECT_EQ(ret, -1);
146 EXPECT_EQ(list[0], '\0');
147
148 close(fd);
149 remove(TEST_FILE);
150 }
151