• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 const char *path = "/data";
22 
23 /*
24  * @tc.name      : opendir_0100
25  * @tc.desc      : Verify that the parameters are valid to open the specified directory
26  * @tc.level     : Level 0
27  */
opendir_0100(void)28 void opendir_0100(void)
29 {
30     DIR *dir = opendir(path);
31     if (!dir) {
32         t_error("%s opendir failed\n", __func__);
33     }
34     struct dirent *r = readdir(dir);
35     if (strcmp(r->d_name, ".")) {
36         t_error("%s opendir is not right, d_name is %s\n", __func__, r->d_name);
37     }
38     closedir(dir);
39 }
40 
41 /*
42  * @tc.name      : opendir_0200
43  * @tc.desc      : When the path is not a folder or not exit, test the return value of the function
44  * @tc.level     : Level 2
45  */
opendir_0200(void)46 void opendir_0200(void)
47 {
48     DIR *dir1 = opendir("/dev/null");
49     if (dir1) {
50         t_error("%s dir should be NULL\n", __func__);
51     }
52 
53     errno = 0;
54     DIR *dir2 = opendir("/does/not/exit/folder");
55     if (dir2) {
56         t_error("%s dir should be NULL\n", __func__);
57     }
58     if (errno != ENOENT) {
59         t_error("%s errno should be ENOTDIR, but now is %d\n", __func__, errno);
60     }
61 }
62 
63 /*
64  * @tc.name      : opendir_0300
65  * @tc.desc      : When the path is NULL, test the return value of the function
66  * @tc.level     : Level 2
67  */
opendir_0300(void)68 void opendir_0300(void)
69 {
70     DIR *dir = opendir(NULL);
71     if (dir) {
72         t_error("%s dir should be NULL\n", __func__);
73     }
74 }
75 
main(int argc,char * argv[])76 int main(int argc, char *argv[])
77 {
78     opendir_0100();
79     opendir_0200();
80     opendir_0300();
81 
82     return t_status;
83 }