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