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 <dirent.h>
17 #include <signal.h>
18 #include <stdlib.h>
19 #include <sys/wait.h>
20
21 #include "filepath_util.h"
22 #include "functionalext.h"
23
handler(int sig)24 void handler(int sig)
25 {
26 exit(t_status);
27 }
28
29 /**
30 * @tc.name : readdir_r_0100
31 * @tc.desc : read a directory
32 * @tc.level : Level 0
33 */
readdir_r_0100(void)34 void readdir_r_0100(void)
35 {
36 char name[PATH_MAX] = {0};
37 FILE_ABSOLUTE_DIR(name);
38 DIR *dir = opendir(name);
39 if (dir == NULL) {
40 t_error("%s failed: opendir. name = %s\n", __func__, name);
41 return;
42 }
43
44 struct dirent buf;
45 struct dirent *res;
46 int result = readdir_r(dir, &buf, &res);
47 if (result != 0) {
48 t_error("%s failed: readdir_r. result = %s\n", __func__, result);
49 return;
50 }
51 }
52
53 /**
54 * @tc.name : readdir_r_0200
55 * @tc.desc : read a directory with a NULL dir
56 * @tc.level : Level 2
57 */
readdir_r_0200(void)58 void readdir_r_0200(void)
59 {
60 pid_t pid = fork();
61 if (pid == -1) {
62 t_error("readdir_0100: Error forking process");
63 } else if (pid == 0) {
64 DIR *dir = NULL;
65 struct dirent buf;
66 struct dirent *res;
67 readdir_r(dir, &buf, &res);
68 } else {
69 int status;
70 waitpid(pid, &status, 0);
71 if (WIFSIGNALED(status)) {
72 int sig = WTERMSIG(status);
73 EXPECT_EQ("readdir_0100", SIGABRT, sig);
74 }
75 }
76 }
77
main(int argc,char * argv[])78 int main(int argc, char *argv[])
79 {
80 readdir_r_0100();
81 readdir_r_0200();
82
83 return t_status;
84 }
85