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 <stdlib.h>
17 #include <unistd.h>
18 #include "functionalext.h"
19
20 typedef void (*TEST_FUN)();
21
22 const int SUCCESS = 0;
23 const int FAILED = -1;
24
25 /**
26 * @tc.name : access_0100
27 * @tc.desc : The parameter filename is the specified file and exists,
28 * the parameter amode is F_OK,function returns successfully.
29 * @tc.level : Level 0
30 */
access_0100(void)31 void access_0100(void)
32 {
33 const char *ptr = "accesstest.txt";
34 FILE *fptr = fopen(ptr, "w");
35 EXPECT_PTRNE("access_0100", fptr, NULL);
36 int isExist = access(ptr, F_OK);
37 EXPECT_EQ("access_0100", isExist, SUCCESS);
38 fclose(fptr);
39 remove(ptr);
40 fptr = NULL;
41 ptr = NULL;
42 }
43
44 /**
45 * @tc.name : access_0200
46 * @tc.desc : The parameter filename is the specified directory and exists,
47 * the parameter amode is F_OK,function returns SUCCESSfully.
48 * @tc.level : Level 0
49 */
access_0200(void)50 void access_0200(void)
51 {
52 char passbuff[90];
53 const char *ptr = "accesstest.txt";
54 FILE *fptr = fopen(ptr, "w");
55 EXPECT_PTRNE("access_0200", fptr, NULL);
56 getcwd(passbuff, sizeof(passbuff));
57 int isExist = access(passbuff, F_OK);
58 EXPECT_EQ("access_0200", isExist, SUCCESS);
59 fclose(fptr);
60 remove(ptr);
61 fptr = NULL;
62 ptr = NULL;
63 }
64
65 /**
66 * @tc.name : access_0300
67 * @tc.desc : The parameter filename is the specified file and not exists,
68 * the parameter amode is F_OK,function returns failure.
69 * @tc.level : Level 2
70 */
access_0300(void)71 void access_0300(void)
72 {
73 const char *ptr = "noaccesstest.txt";
74 int isExist = access(ptr, F_OK);
75 EXPECT_EQ("access_0300", isExist, FAILED);
76 remove(ptr);
77 }
78
79 /**
80 * @tc.name : access_0400
81 * @tc.desc : The parameter filename is the specified directory and not exists,
82 * the parameter amode is F_OK,function returns failure.
83 * @tc.level : Level 2
84 */
access_0400(void)85 void access_0400(void)
86 {
87 const char *ptr = "./noaccesstest";
88 int isExist = access(ptr, F_OK);
89 EXPECT_EQ("access_0400", isExist, FAILED);
90 remove(ptr);
91 }
92
93 /**
94 * @tc.name : access_0500
95 * @tc.desc : The file attribute specified by the parameter filename is readable,
96 * the parameter amode is R_OK,function returns successfully.
97 * @tc.level : Level 0
98 */
access_0500(void)99 void access_0500(void)
100 {
101 const char *ptr = "accesstest.txt";
102 FILE *fptr = fopen(ptr, "w");
103 EXPECT_PTRNE("access_0500", fptr, NULL);
104 int isRead = access(ptr, R_OK);
105 EXPECT_EQ("access_0500", isRead, SUCCESS);
106 fclose(fptr);
107 remove(ptr);
108 fptr = NULL;
109 ptr = NULL;
110 }
111
112 /**
113 * @tc.name : access_0600
114 * @tc.desc : The file attribute specified by the parameter filename is writable,
115 * the parameter amode is W_OK,function returns successfully.
116 * @tc.level : Level 0
117 */
access_0600(void)118 void access_0600(void)
119 {
120 const char *ptr = "accesstest.txt";
121 FILE *fptr = fopen(ptr, "w");
122 EXPECT_PTRNE("access_0600", fptr, NULL);
123 int isWrite = access(ptr, W_OK);
124 EXPECT_EQ("access_0600", isWrite, SUCCESS);
125 fclose(fptr);
126 remove(ptr);
127 fptr = NULL;
128 ptr = NULL;
129 }
130
131 /**
132 * @tc.name : access_0700
133 * @tc.desc : The file attribute specified by the parameter filename is executable,
134 * the parameter amode is W_OK,function returns successfully.
135 * @tc.level : Level 0
136 */
access_0700(void)137 void access_0700(void)
138 {
139 const char *ptr = "accesstest.txt";
140 FILE *fptr = fopen(ptr, "w");
141 EXPECT_PTRNE("access_0700", fptr, NULL);
142 system("chmod 777 ./accesstest.txt");
143 int isExecute = access(ptr, X_OK);
144 EXPECT_EQ("access_0700", isExecute, SUCCESS);
145 fclose(fptr);
146 remove(ptr);
147 fptr = NULL;
148 ptr = NULL;
149 }
150
151 /**
152 * @tc.name : access_0800
153 * @tc.desc : The file attribute specified by the parameter filename is not executable,
154 * the parameter amode is R_OK,function returns failure.
155 * @tc.level : Level 2
156 */
access_0800(void)157 void access_0800(void)
158 {
159 const char *ptr = "accesstest.txt";
160 FILE *fptr = fopen(ptr, "w");
161 EXPECT_PTRNE("access_0800", fptr, NULL);
162 int isExecute = access(ptr, X_OK);
163 EXPECT_EQ("access_0800", isExecute, FAILED);
164 fclose(fptr);
165 remove(ptr);
166 fptr = NULL;
167 ptr = NULL;
168 }
169
170 TEST_FUN G_Fun_Array[] = {
171 access_0100,
172 access_0200,
173 access_0300,
174 access_0400,
175 access_0500,
176 access_0600,
177 access_0700,
178 access_0800,
179 };
180
main(int argc,char * argv[])181 int main(int argc, char *argv[])
182 {
183 int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
184 for (int pos = 0; pos < num; ++pos) {
185 G_Fun_Array[pos]();
186 }
187 return t_status;
188 }