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 <stdlib.h>
18 #include <stdio.h>
19 #include "functionalext.h"
20 #include "filepath_util.h"
21
22 typedef void (*TEST_FUN)();
23 const int SUCCESS = 0;
24 const int FAILED = -1;
25
26 /**
27 * @tc.name : faccessat_0100
28 * @tc.desc : Test file exists, amode is F_OK.
29 * @tc.level : Level 0
30 */
faccessat_0100(void)31 void faccessat_0100(void)
32 {
33 char ptr[PATH_MAX] = {0};
34 FILE_ABSOLUTE_PATH(STR_FACCESSAT_TEST_TXT, ptr);
35 int fd = open(ptr, O_RDWR | O_CREAT, TEST_MODE);
36 EXPECT_TRUE("faccessat_0100", fd >= 0);
37 int isExist = faccessat(fd, ptr, F_OK, 0);
38 EXPECT_EQ("faccessat_0100", isExist, SUCCESS);
39 close(fd);
40 remove(ptr);
41 }
42
43 /**
44 * @tc.name : faccessat_0200
45 * @tc.desc : The test file does not exist, amode is F_OK.
46 * @tc.level : Level 2
47 */
faccessat_0200(void)48 void faccessat_0200(void)
49 {
50 char ptr[PATH_MAX] = {0};
51 FILE_ABSOLUTE_PATH(STR_FACCESSAT_TEST_TXT, ptr);
52 int fd = -1;
53 int isExist = faccessat(fd, ptr, F_OK, 0);
54 EXPECT_EQ("faccessat_0200", isExist, FAILED);
55 close(fd);
56 remove(ptr);
57 }
58
59 /**
60 * @tc.name : faccessat_0300
61 * @tc.desc : The test file has read permission and amode is R_OK.
62 * @tc.level : Level 0
63 */
faccessat_0300(void)64 void faccessat_0300(void)
65 {
66 char ptr[PATH_MAX] = {0};
67 FILE_ABSOLUTE_PATH(STR_FACCESSAT_TEST_TXT, ptr);
68 int fd = open(ptr, O_RDWR | O_CREAT, 00040);
69 EXPECT_TRUE("faccessat_0300", fd >= 0);
70 int isRead = faccessat(fd, ptr, R_OK, 0);
71 EXPECT_EQ("faccessat_0300", isRead, SUCCESS);
72 close(fd);
73 remove(ptr);
74 }
75
76 /**
77 * @tc.name : faccessat_0400
78 * @tc.desc : The test file has write permission, and amode is W_OK.
79 * @tc.level : Level 0
80 */
faccessat_0400(void)81 void faccessat_0400(void)
82 {
83 char ptr[PATH_MAX] = {0};
84 FILE_ABSOLUTE_PATH(STR_FACCESSAT_TEST_TXT, ptr);
85 int fd = open(ptr, O_RDWR | O_CREAT, 00020);
86 EXPECT_TRUE("faccessat_0400", fd >= 0);
87 int isWrite = faccessat(fd, ptr, W_OK, 0);
88 EXPECT_EQ("faccessat_0400", isWrite, SUCCESS);
89 close(fd);
90 remove(ptr);
91 }
92
93 /**
94 * @tc.name : faccessat_0500
95 * @tc.desc : The test file has executable permission, amode is X_OK.
96 * @tc.level : Level 0
97 */
faccessat_0500(void)98 void faccessat_0500(void)
99 {
100 char ptr[PATH_MAX] = {0};
101 FILE_ABSOLUTE_PATH(STR_FACCESSAT_TEST_TXT, ptr);
102 int fd = open(ptr, O_RDWR | O_CREAT, 00010);
103 char cmd[256] = {0};
104 snprintf(cmd, sizeof(cmd), "chmod 777 %s", ptr);
105 system(cmd);
106 EXPECT_TRUE("faccessat_0500", fd >= 0);
107 int isExecute = faccessat(fd, ptr, X_OK, 0);
108 EXPECT_EQ("faccessat_0500", isExecute, SUCCESS);
109 close(fd);
110 remove(ptr);
111 }
112
113 /**
114 * @tc.name : faccessat_0600
115 * @tc.desc : The test file has no executable permission, amode is X_OK.
116 * @tc.level : Level 2
117 */
faccessat_0600(void)118 void faccessat_0600(void)
119 {
120 char ptr[PATH_MAX] = {0};
121 FILE_ABSOLUTE_PATH(STR_FACCESSAT_TEST_TXT, ptr);
122 int fd = open(ptr, O_RDWR | O_CREAT, TEST_MODE);
123 EXPECT_TRUE("faccessat_0600", fd >= 0);
124 int isExecute = faccessat(fd, ptr, X_OK, 0);
125 EXPECT_EQ("faccessat_0600", isExecute, FAILED);
126 close(fd);
127 remove(ptr);
128 }
129
130 /**
131 * @tc.name : faccessat_0700
132 * @tc.desc : The test file has read, write and executable permissions, and amode is R_OK|W_OK|X_Ok.
133 * @tc.level : Level 0
134 */
faccessat_0700(void)135 void faccessat_0700(void)
136 {
137 char ptr[PATH_MAX] = {0};
138 FILE_ABSOLUTE_PATH(STR_FACCESSAT_TEST_TXT, ptr);
139 int fd = open(ptr, O_RDWR | O_CREAT, 00070);
140 EXPECT_TRUE("faccessat_0700", fd >= 0);
141 int isExecute = faccessat(fd, ptr, R_OK | W_OK | X_OK, 0);
142 EXPECT_EQ("faccessat_0700", isExecute, SUCCESS);
143 close(fd);
144 remove(ptr);
145 }
146
147 /**
148 * @tc.name : faccessat_0800
149 * @tc.desc : The test file does not have read/write executable permission, and amode is R_OK|W_OK|X_OK.
150 * @tc.level : Level 2
151 */
faccessat_0800(void)152 void faccessat_0800(void)
153 {
154 char ptr[PATH_MAX] = {0};
155 FILE_ABSOLUTE_PATH(STR_FACCESSAT_TEST_TXT, ptr);
156 int fd = open(ptr, O_RDWR | O_CREAT, TEST_MODE);
157 EXPECT_TRUE("faccessat_0800", fd >= 0);
158 int isExecute = faccessat(fd, ptr, R_OK | W_OK | X_OK, 0);
159 EXPECT_EQ("faccessat_0800", isExecute, FAILED);
160 close(fd);
161 remove(ptr);
162 }
163
164 /**
165 * @tc.name : faccessat_0900
166 * @tc.desc : The test file has read permissions whith invalid flag parameter (0), and amode is R_OK.
167 * @tc.level : Level 2
168 */
faccessat_0900(void)169 void faccessat_0900(void)
170 {
171 char ptr[PATH_MAX] = {0};
172 FILE_ABSOLUTE_PATH(STR_FACCESSAT_TEST_TXT, ptr);
173 int fd = open(ptr, O_RDWR | O_CREAT, 00070);
174 EXPECT_TRUE("faccessat_0900", fd >= 0);
175 int ret = faccessat(fd, ptr, R_OK, 0);
176 EXPECT_EQ("faccessat_0900", ret, 0);
177 EXPECT_EQ("faccessat_0900", errno, 13);
178 close(fd);
179 remove(ptr);
180 }
181
182 /**
183 * @tc.name : faccessat_1000
184 * @tc.desc : The test file has read, write and executable permissions, fd is AT_FDCWD,
185 * and amode is R_OK|W_OK|X_OK.
186 * @tc.level : Level 0
187 */
faccessat_1000(void)188 void faccessat_1000(void)
189 {
190 char ptr[PATH_MAX] = {0};
191 FILE_ABSOLUTE_PATH(STR_FACCESSAT_TEST_TXT, ptr);
192 int fd = open(ptr, O_RDWR | O_CREAT, 00070);
193 EXPECT_TRUE("faccessat_1000", fd >= 0);
194 int isExecute = faccessat(AT_FDCWD, ptr, R_OK | W_OK | X_OK, AT_EACCESS);
195 EXPECT_EQ("faccessat_1000", isExecute, SUCCESS);
196 close(fd);
197 remove(ptr);
198 }
199
200 /**
201 * @tc.name : faccessat_1100
202 * @tc.desc : The test file does not have read, write and execute permissions, fd is AT_FDCWD,
203 * and amode is R_OK|W_OK|X_OK.
204 * @tc.level : Level 2
205 */
faccessat_1100(void)206 void faccessat_1100(void)
207 {
208 char ptr[PATH_MAX] = {0};
209 FILE_ABSOLUTE_PATH(STR_FACCESSAT_TEST_TXT, ptr);
210 int fd = open(ptr, O_RDWR | O_CREAT, TEST_MODE);
211 EXPECT_TRUE("faccessat_1100", fd >= 0);
212 int isExecute = faccessat(AT_FDCWD, ptr, R_OK | W_OK | X_OK, AT_EACCESS);
213 EXPECT_EQ("faccessat_1100", isExecute, FAILED);
214 close(fd);
215 remove(ptr);
216 }
217
218 TEST_FUN G_Fun_Array[] = {
219 faccessat_0100,
220 faccessat_0200,
221 faccessat_0300,
222 faccessat_0400,
223 faccessat_0500,
224 faccessat_0600,
225 faccessat_0700,
226 faccessat_0800,
227 faccessat_0900,
228 faccessat_1000,
229 faccessat_1100,
230 };
231
main(int argc,char * argv[])232 int main(int argc, char *argv[])
233 {
234 int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
235 for (int pos = 0; pos < num; ++pos) {
236 G_Fun_Array[pos]();
237 }
238
239 return t_status;
240 }