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 <string.h>
18 #include <malloc.h>
19 #include "functionalext.h"
20
21 #define MAXPATH 1000
22 #define BUFFSIZE 4096
23
24 /**
25 * @tc.name : closedir_0100
26 * @tc.desc : The parameter is valid and can close the specified directory.
27 * @tc.level : Level 0
28 */
closedir_0100(void)29 void closedir_0100(void)
30 {
31 char path[MAXPATH];
32 DIR *dir_ptr;
33 char *data = NULL;
34 data = getcwd(path, MAXPATH);
35 dir_ptr = opendir(path);
36 int ret = -1;
37 ret = closedir(dir_ptr);
38 EXPECT_EQ("closedir_0100", ret, 0);
39 }
40
41 /**
42 * @tc.name : closedir_0200
43 * @tc.desc : The dir parameter is invalid, the specified directory cannot be closed.
44 * @tc.level : Level 2
45 */
closedir_0200(void)46 void closedir_0200(void)
47 {
48 int ret = 0;
49 DIR *dir = (DIR *)malloc(BUFFSIZE);
50 if (dir != NULL) {
51 // fill to set dir->fd = -1 (intentionally incorrect file descriptor)
52 memset(dir, 0xFF, BUFFSIZE);
53 ret = closedir(dir);
54 }
55 EXPECT_EQ("closedir_0200", ret, -1);
56 }
57
main(int argc,char * argv[])58 int main(int argc, char *argv[])
59 {
60 closedir_0100();
61 closedir_0200();
62 return t_status;
63 }