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 <errno.h>
17 #include <dirent.h>
18 #include <string.h>
19 #include "test.h"
20 #include <sys/stat.h>
21 #include <stdio.h>
22 /*
23  * @tc.name      : opendir_0100
24  * @tc.desc      : Verify that the parameters are valid to open the specified directory
25  * @tc.level     : Level 0
26  */
opendir_0100(void)27 void opendir_0100(void)
28 {
29     errno = 0;
30     char *path = "/data/local/tmp/opendirtest";
31     if (access(path, F_OK) == 0) {
32         remove(path);
33     }
34     int ret = mkdir(path, 0777);
35     if (ret != 0) {
36         t_error("opendirtest create failed: errno = %d\n", errno);
37     }
38     DIR *dir = opendir(path);
39     if (!dir) {
40         t_error("%s opendir failed\n", __func__);
41     }
42     struct dirent *r;
43     int issuccess = 0;
44     while ((r = readdir(dir)) != NULL) {
45         if (strcmp(r->d_name, ".") == 0) {
46             issuccess = 1;
47             break;
48         }
49     }
50     if (issuccess != 1) {
51         t_error("%s opendir is not right", __func__);
52     }
53     closedir(dir);
54     ret = remove(path);
55     if (ret != 0) {
56         t_error("remove failed: errno = %d\n", errno);
57     }
58 }
59 
60 /*
61  * @tc.name      : opendir_0200
62  * @tc.desc      : When the path is not a folder or not exit, test the return value of the function
63  * @tc.level     : Level 2
64  */
opendir_0200(void)65 void opendir_0200(void)
66 {
67     DIR *dir1 = opendir("/dev/null");
68     if (dir1) {
69         t_error("%s dir should be NULL\n", __func__);
70     }
71 
72     errno = 0;
73     DIR *dir2 = opendir("/does/not/exit/folder");
74     if (dir2) {
75         t_error("%s dir should be NULL\n", __func__);
76     }
77     if (errno != ENOENT) {
78         t_error("%s errno should be ENOTDIR, but now is %d\n", __func__, errno);
79     }
80 }
81 
82 /*
83  * @tc.name      : opendir_0300
84  * @tc.desc      : When the path is NULL, test the return value of the function
85  * @tc.level     : Level 2
86  */
opendir_0300(void)87 void opendir_0300(void)
88 {
89     DIR *dir = opendir(NULL);
90     if (dir) {
91         t_error("%s dir should be NULL\n", __func__);
92     }
93 }
94 
main(int argc, char *argv[])95 int main(int argc, char *argv[])
96 {
97     opendir_0100();
98     opendir_0200();
99     opendir_0300();
100 
101     return t_status;
102 }