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 <stdlib.h>
18
19 #include "test.h"
20
21 const char *name = "/data/tests/libc-test/src";
22
filter(const struct dirent * entry)23 int filter(const struct dirent *entry)
24 {
25 return !0;
26 }
27
28 /**
29 * @tc.name : scandir_0100
30 * @tc.desc : scan a directory for matching entries with alphasort
31 * @tc.level : Level 0
32 */
scandir_0100(void)33 void scandir_0100(void)
34 {
35 struct dirent **namelist;
36 int n = scandir(name, &namelist, NULL, alphasort);
37 if (n < 0) {
38 t_error("%s failed: scandir. n = %d\n", __func__, n);
39 return;
40 }
41
42 while (n--) {
43 free(namelist[n]);
44 }
45 free(namelist);
46 }
47
48 /**
49 * @tc.name : scandir_0200
50 * @tc.desc : scan a directory for matching entries with versionsort
51 * @tc.level : Level 1
52 */
scandir_0200(void)53 void scandir_0200(void)
54 {
55 struct dirent **namelist;
56 int n = scandir(name, &namelist, NULL, versionsort);
57 if (n < 0) {
58 t_error("%s failed: scandir. n = %d\n", __func__, n);
59 return;
60 }
61
62 while (n--) {
63 free(namelist[n]);
64 }
65 free(namelist);
66 }
67
68 /**
69 * @tc.name : scandir_0300
70 * @tc.desc : scan a directory for matching entries with filter
71 * @tc.level : Level 1
72 */
scandir_0300(void)73 void scandir_0300(void)
74 {
75 struct dirent **namelist;
76 int n = scandir(name, &namelist, filter, versionsort);
77 if (n < 0) {
78 t_error("%s failed: scandir. n = %d\n", __func__, n);
79 return;
80 }
81
82 while (n--) {
83 free(namelist[n]);
84 }
85 free(namelist);
86 }
87
88 /**
89 * @tc.name : scandir_0400
90 * @tc.desc : scan a directory for matching entries with a NULL dir
91 * @tc.level : Level 2
92 */
scandir_0400(void)93 void scandir_0400(void)
94 {
95 struct dirent **namelist;
96 int n = scandir(NULL, &namelist, NULL, alphasort);
97 if (n >= 0) {
98 t_error("%s failed: scandir. n = %d\n", __func__, n);
99
100 while (n--) {
101 free(namelist[n]);
102 }
103 free(namelist);
104 return;
105 }
106 }
107
main(int argc,char * argv[])108 int main(int argc, char *argv[])
109 {
110 scandir_0100();
111 scandir_0200();
112 scandir_0300();
113 scandir_0400();
114
115 return t_status;
116 }
117