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 #include <errno.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <string.h>
19 #include <sys/wait.h>
20 #include "fortify_test.h"
21 #include "functionalext.h"
22 #include "test.h"
23
24 #define FILE_MODE_ALL (0777)
25
26 #ifdef open64
27 #undef open64
28 #endif
29
30 #ifdef openat64
31 #undef openat64
32 #endif
33
34 /**
35 * @tc.name : open_0010
36 * @tc.desc : test open normal condition
37 * @tc.level : Level 0
38 */
open_0010(void)39 static void open_0010(void)
40 {
41 int fd = open("/proc/version", O_RDWR | O_CREAT, FILE_MODE_ALL);
42 TEST(fd != -1);
43 close(fd);
44
45 return;
46 }
47
48 /**
49 * @tc.name : open_0020
50 * @tc.desc : test open O_CREAT without mode
51 * @tc.level : Level 2
52 */
open_0020(void)53 static void open_0020(void)
54 {
55 struct sigaction sigabrt = {
56 .sa_handler = SignalHandler,
57 };
58 sigaction(SIGABRT, &sigabrt, NULL);
59
60 int flags = O_CREAT;
61 int status;
62 int pid = fork();
63 switch (pid) {
64 case -1:
65 t_error("fork failed: %s\n", strerror(errno));
66 break;
67 case 0:
68 open("/proc/version", flags);
69 exit(0);
70 default:
71 waitpid(pid, &status, WUNTRACED);
72 TEST(WIFEXITED(status) == 0);
73 TEST(WIFSTOPPED(status) == 1);
74 TEST(WSTOPSIG(status) == SIGSTOP);
75 kill(pid, SIGCONT);
76 break;
77 }
78
79 return;
80 }
81
82 /**
83 * @tc.name : open_0030
84 * @tc.desc : test open O_TMPFILE without mode
85 * @tc.level : Level 2
86 */
open_0030(void)87 static void open_0030(void)
88 {
89 struct sigaction sigabrt = {
90 .sa_handler = SignalHandler,
91 };
92 sigaction(SIGABRT, &sigabrt, NULL);
93
94 int flags = O_TMPFILE;
95 int status;
96 int pid = fork();
97 switch (pid) {
98 case -1:
99 t_error("fork failed: %s\n", strerror(errno));
100 break;
101 case 0:
102 open("/proc/version", flags);
103 exit(0);
104 default:
105 waitpid(pid, &status, WUNTRACED);
106 TEST(WIFEXITED(status) == 0);
107 TEST(WIFSTOPPED(status) == 1);
108 TEST(WSTOPSIG(status) == SIGSTOP);
109 kill(pid, SIGCONT);
110 break;
111 }
112
113 return;
114 }
115
116 /**
117 * @tc.name : open_0040
118 * @tc.desc : test open only O_RDWR
119 * @tc.level : Level 1
120 */
open_0040(void)121 static void open_0040(void)
122 {
123 int fd = open("/proc/version", O_RDWR);
124 EXPECT_NE(open_0040, fd, -1);
125 close(fd);
126
127 return;
128 }
129
130 /**
131 * @tc.name : openat_0010
132 * @tc.desc : test openat normal condition
133 * @tc.level : Level 0
134 */
openat_0010(void)135 static void openat_0010(void)
136 {
137 int fd = openat(AT_FDCWD, "/proc/version", O_RDWR | O_CREAT, FILE_MODE_ALL);
138 TEST(fd != -1);
139 close(fd);
140
141 return;
142 }
143
144 /**
145 * @tc.name : openat_0020
146 * @tc.desc : test openat O_CREAT without mode
147 * @tc.level : Level 2
148 */
openat_0020(void)149 static void openat_0020(void)
150 {
151 struct sigaction sigabrt = {
152 .sa_handler = SignalHandler,
153 };
154 sigaction(SIGABRT, &sigabrt, NULL);
155
156 int flags = O_CREAT;
157 int status;
158 int pid = fork();
159 switch (pid) {
160 case -1:
161 t_error("fork failed: %s\n", strerror(errno));
162 break;
163 case 0:
164 openat(AT_FDCWD, "/proc/version", flags);
165 exit(0);
166 default:
167 waitpid(pid, &status, WUNTRACED);
168 TEST(WIFEXITED(status) == 0);
169 TEST(WIFSTOPPED(status) == 1);
170 TEST(WSTOPSIG(status) == SIGSTOP);
171 kill(pid, SIGCONT);
172 break;
173 }
174
175 return;
176 }
177
178 /**
179 * @tc.name : openat_0030
180 * @tc.desc : test openat O_TMPFILE without mode
181 * @tc.level : Level 2
182 */
openat_0030(void)183 static void openat_0030(void)
184 {
185 struct sigaction sigabrt = {
186 .sa_handler = SignalHandler,
187 };
188 sigaction(SIGABRT, &sigabrt, NULL);
189
190 int flags = O_TMPFILE;
191 int status;
192 int pid = fork();
193 switch (pid) {
194 case -1:
195 t_error("fork failed: %s\n", strerror(errno));
196 break;
197 case 0:
198 openat(AT_FDCWD, "/proc/version", flags);
199 exit(0);
200 default:
201 waitpid(pid, &status, WUNTRACED);
202 TEST(WIFEXITED(status) == 0);
203 TEST(WIFSTOPPED(status) == 1);
204 TEST(WSTOPSIG(status) == SIGSTOP);
205 kill(pid, SIGCONT);
206 break;
207 }
208
209 return;
210 }
211
212 /**
213 * @tc.name : openat_0040
214 * @tc.desc : test openat only O_RDWR
215 * @tc.level : Level 1
216 */
openat_0040(void)217 static void openat_0040(void)
218 {
219 int fd = openat(AT_FDCWD, "/proc/version", O_RDWR);
220 EXPECT_NE(openat_0040, fd, -1);
221 close(fd);
222
223 return;
224 }
225
226 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
227 /**
228 * @tc.name : open64_0010
229 * @tc.desc : test open64 normal condition
230 * @tc.level : Level 0
231 */
open64_0010(void)232 static void open64_0010(void)
233 {
234 int fd = open64("/proc/version", O_RDWR | O_CREAT, FILE_MODE_ALL);
235 TEST(fd != -1);
236 close(fd);
237
238 return;
239 }
240
241 /**
242 * @tc.name : open64_0020
243 * @tc.desc : test open64 O_CREAT without mode
244 * @tc.level : Level 2
245 */
open64_0020(void)246 static void open64_0020(void)
247 {
248 struct sigaction sigabrt = {
249 .sa_handler = SignalHandler,
250 };
251 sigaction(SIGABRT, &sigabrt, NULL);
252
253 int flags = O_CREAT;
254 int status;
255 int pid = fork();
256 switch (pid) {
257 case -1:
258 t_error("fork failed: %s\n", strerror(errno));
259 break;
260 case 0:
261 open64("/proc/version", flags);
262 exit(0);
263 default:
264 waitpid(pid, &status, WUNTRACED);
265 TEST(WIFEXITED(status) == 0);
266 TEST(WIFSTOPPED(status) == 1);
267 TEST(WSTOPSIG(status) == SIGSTOP);
268 kill(pid, SIGCONT);
269 break;
270 }
271
272 return;
273 }
274
275 /**
276 * @tc.name : open64_0030
277 * @tc.desc : test open64 O_TMPFILE without mode
278 * @tc.level : Level 2
279 */
open64_0030(void)280 static void open64_0030(void)
281 {
282 struct sigaction sigabrt = {
283 .sa_handler = SignalHandler,
284 };
285 sigaction(SIGABRT, &sigabrt, NULL);
286
287 int flags = O_TMPFILE;
288 int status;
289 int pid = fork();
290 switch (pid) {
291 case -1:
292 t_error("fork failed: %s\n", strerror(errno));
293 break;
294 case 0:
295 open64("/proc/version", flags);
296 exit(0);
297 default:
298 waitpid(pid, &status, WUNTRACED);
299 TEST(WIFEXITED(status) == 0);
300 TEST(WIFSTOPPED(status) == 1);
301 TEST(WSTOPSIG(status) == SIGSTOP);
302 kill(pid, SIGCONT);
303 break;
304 }
305
306 return;
307 }
308
309 /**
310 * @tc.name : open64_0040
311 * @tc.desc : test open64 only O_RDWR
312 * @tc.level : Level 1
313 */
open64_0040(void)314 static void open64_0040(void)
315 {
316 int fd = open64("/proc/version", O_RDWR);
317 EXPECT_NE(open64_0040, fd, -1);
318 close(fd);
319
320 return;
321 }
322
323 /**
324 * @tc.name : openat64_0010
325 * @tc.desc : test openat64 normal condition
326 * @tc.level : Level 0
327 */
openat64_0010(void)328 static void openat64_0010(void)
329 {
330 int fd = openat64(AT_FDCWD, "/proc/version", O_RDWR | O_CREAT, FILE_MODE_ALL);
331 TEST(fd != -1);
332 close(fd);
333
334 return;
335 }
336
337 /**
338 * @tc.name : openat64_0020
339 * @tc.desc : test openat64 O_CREAT without mode
340 * @tc.level : Level 2
341 */
openat64_0020(void)342 static void openat64_0020(void)
343 {
344 struct sigaction sigabrt = {
345 .sa_handler = SignalHandler,
346 };
347 sigaction(SIGABRT, &sigabrt, NULL);
348
349 int flags = O_CREAT;
350 int status;
351 int pid = fork();
352 switch (pid) {
353 case -1:
354 t_error("fork failed: %s\n", strerror(errno));
355 break;
356 case 0:
357 openat64(AT_FDCWD, "/proc/version", flags);
358 exit(0);
359 default:
360 waitpid(pid, &status, WUNTRACED);
361 TEST(WIFEXITED(status) == 0);
362 TEST(WIFSTOPPED(status) == 1);
363 TEST(WSTOPSIG(status) == SIGSTOP);
364 kill(pid, SIGCONT);
365 break;
366 }
367
368 return;
369 }
370
371 /**
372 * @tc.name : openat64_0030
373 * @tc.desc : test openat64 O_TMPFILE without mode
374 * @tc.level : Level 2
375 */
openat64_0030(void)376 static void openat64_0030(void)
377 {
378 struct sigaction sigabrt = {
379 .sa_handler = SignalHandler,
380 };
381 sigaction(SIGABRT, &sigabrt, NULL);
382
383 int flags = O_TMPFILE;
384 int status;
385 int pid = fork();
386 switch (pid) {
387 case -1:
388 t_error("fork failed: %s\n", strerror(errno));
389 break;
390 case 0:
391 openat64(AT_FDCWD, "/proc/version", flags);
392 exit(0);
393 default:
394 waitpid(pid, &status, WUNTRACED);
395 TEST(WIFEXITED(status) == 0);
396 TEST(WIFSTOPPED(status) == 1);
397 TEST(WSTOPSIG(status) == SIGSTOP);
398 kill(pid, SIGCONT);
399 break;
400 }
401
402 return;
403 }
404
405 /**
406 * @tc.name : openat64_0040
407 * @tc.desc : test openat64 only O_RDWR
408 * @tc.level : Level 1
409 */
openat64_0040(void)410 static void openat64_0040(void)
411 {
412 int fd = openat64(AT_FDCWD, "/proc/version", O_RDWR);
413 EXPECT_NE(openat64_0040, fd, -1);
414 close(fd);
415
416 return;
417 }
418 #endif
419
main(int argc,char * argv[])420 int main(int argc, char *argv[]) {
421 open_0010();
422 open_0020();
423 open_0030();
424 open_0040();
425 openat_0010();
426 openat_0020();
427 openat_0030();
428 openat_0040();
429 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
430 open64_0010();
431 open64_0020();
432 open64_0030();
433 open64_0040();
434 openat64_0010();
435 openat64_0020();
436 openat64_0030();
437 openat64_0040();
438 #endif
439 return t_status;
440 }