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