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
20 #include "filepath_util.h"
21
handler(int sig)22 void handler(int sig)
23 {
24 exit(t_status);
25 }
26
27 /**
28 * @tc.name : readdir_r_0100
29 * @tc.desc : read a directory
30 * @tc.level : Level 0
31 */
readdir_r_0100(void)32 void readdir_r_0100(void)
33 {
34 char name[PATH_MAX] = {0};
35 FILE_ABSOLUTE_DIR(name);
36 DIR *dir = opendir(name);
37 if (dir == NULL) {
38 t_error("%s failed: opendir. name = %s\n", __func__, name);
39 return;
40 }
41
42 struct dirent buf;
43 struct dirent *res;
44 int result = readdir_r(dir, &buf, &res);
45 if (result != 0) {
46 t_error("%s failed: readdir_r. result = %s\n", __func__, result);
47 return;
48 }
49 }
50
51 /**
52 * @tc.name : readdir_r_0200
53 * @tc.desc : read a directory with a NULL dir
54 * @tc.level : Level 2
55 */
readdir_r_0200(void)56 void readdir_r_0200(void)
57 {
58 signal(SIGSEGV, handler);
59
60 DIR *dir = NULL;
61 struct dirent buf;
62 struct dirent *res;
63 readdir_r(dir, &buf, &res);
64 }
65
main(int argc,char * argv[])66 int main(int argc, char *argv[])
67 {
68 readdir_r_0100();
69 readdir_r_0200();
70
71 return t_status;
72 }
73