Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dirwalk_test.c « storage « unit_tests « applications - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: db3d91a96e0eb07c91514badee3a3a66f1dc84b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "../minunit.h"
#include <furi.h>
#include <m-dict.h>
#include <toolbox/dir_walk.h>

static const char* const storage_test_dirwalk_paths[] = {
    "1",
    "11",
    "111",
    "1/2",
    "1/22",
    "1/222",
    "11/2",
    "111/2",
    "111/22",
    "111/22/33",
};

static const char* const storage_test_dirwalk_files[] = {
    "file1.test",
    "file2.test",
    "file3.ext_test",
    "1/file1.test",
    "111/22/33/file1.test",
    "111/22/33/file2.test",
    "111/22/33/file3.ext_test",
    "111/22/33/file4.ext_test",
};

typedef struct {
    const char* const path;
    bool is_dir;
} StorageTestPathDesc;

const StorageTestPathDesc storage_test_dirwalk_full[] = {
    {.path = "1", .is_dir = true},
    {.path = "11", .is_dir = true},
    {.path = "111", .is_dir = true},
    {.path = "1/2", .is_dir = true},
    {.path = "1/22", .is_dir = true},
    {.path = "1/222", .is_dir = true},
    {.path = "11/2", .is_dir = true},
    {.path = "111/2", .is_dir = true},
    {.path = "111/22", .is_dir = true},
    {.path = "111/22/33", .is_dir = true},
    {.path = "file1.test", .is_dir = false},
    {.path = "file2.test", .is_dir = false},
    {.path = "file3.ext_test", .is_dir = false},
    {.path = "1/file1.test", .is_dir = false},
    {.path = "111/22/33/file1.test", .is_dir = false},
    {.path = "111/22/33/file2.test", .is_dir = false},
    {.path = "111/22/33/file3.ext_test", .is_dir = false},
    {.path = "111/22/33/file4.ext_test", .is_dir = false},
};

const StorageTestPathDesc storage_test_dirwalk_no_recursive[] = {
    {.path = "1", .is_dir = true},
    {.path = "11", .is_dir = true},
    {.path = "111", .is_dir = true},
    {.path = "file1.test", .is_dir = false},
    {.path = "file2.test", .is_dir = false},
    {.path = "file3.ext_test", .is_dir = false},
};

const StorageTestPathDesc storage_test_dirwalk_filter[] = {
    {.path = "file1.test", .is_dir = false},
    {.path = "file2.test", .is_dir = false},
    {.path = "1/file1.test", .is_dir = false},
    {.path = "111/22/33/file1.test", .is_dir = false},
    {.path = "111/22/33/file2.test", .is_dir = false},
};

typedef struct {
    bool is_dir;
    bool visited;
} StorageTestPath;

DICT_DEF2(StorageTestPathDict, string_t, STRING_OPLIST, StorageTestPath, M_POD_OPLIST)

static StorageTestPathDict_t*
    storage_test_paths_alloc(const StorageTestPathDesc paths[], size_t paths_count) {
    StorageTestPathDict_t* data = malloc(sizeof(StorageTestPathDict_t));
    StorageTestPathDict_init(*data);

    for(size_t i = 0; i < paths_count; i++) {
        string_t key;
        string_init_set(key, paths[i].path);
        StorageTestPath value = {
            .is_dir = paths[i].is_dir,
            .visited = false,
        };

        StorageTestPathDict_set_at(*data, key, value);
        string_clear(key);
    }

    return data;
}

static void storage_test_paths_free(StorageTestPathDict_t* data) {
    StorageTestPathDict_clear(*data);
    free(data);
}

static bool storage_test_paths_mark(StorageTestPathDict_t* data, string_t path, bool is_dir) {
    bool found = false;

    StorageTestPath* record = StorageTestPathDict_get(*data, path);
    if(record) {
        if(is_dir == record->is_dir) {
            if(record->visited == false) {
                record->visited = true;
                found = true;
            }
        }
    }

    return found;
}

static bool storage_test_paths_check(StorageTestPathDict_t* data) {
    bool error = false;

    StorageTestPathDict_it_t it;
    for(StorageTestPathDict_it(it, *data); !StorageTestPathDict_end_p(it);
        StorageTestPathDict_next(it)) {
        const StorageTestPathDict_itref_t* itref = StorageTestPathDict_cref(it);

        if(itref->value.visited == false) {
            error = true;
            break;
        }
    }

    return error;
}

static bool write_file_13DA(Storage* storage, const char* path) {
    File* file = storage_file_alloc(storage);
    bool result = false;
    if(storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
        result = storage_file_write(file, "13DA", 4) == 4;
    }
    storage_file_close(file);
    storage_file_free(file);

    return result;
}

static void storage_dirs_create(Storage* storage, const char* base) {
    string_t path;
    string_init(path);

    storage_common_mkdir(storage, base);

    for(size_t i = 0; i < COUNT_OF(storage_test_dirwalk_paths); i++) {
        string_printf(path, "%s/%s", base, storage_test_dirwalk_paths[i]);
        storage_common_mkdir(storage, string_get_cstr(path));
    }

    for(size_t i = 0; i < COUNT_OF(storage_test_dirwalk_files); i++) {
        string_printf(path, "%s/%s", base, storage_test_dirwalk_files[i]);
        write_file_13DA(storage, string_get_cstr(path));
    }

    string_clear(path);
}

MU_TEST_1(test_dirwalk_full, Storage* storage) {
    string_t path;
    string_init(path);
    FileInfo fileinfo;

    StorageTestPathDict_t* paths =
        storage_test_paths_alloc(storage_test_dirwalk_full, COUNT_OF(storage_test_dirwalk_full));

    DirWalk* dir_walk = dir_walk_alloc(storage);
    mu_check(dir_walk_open(dir_walk, EXT_PATH("dirwalk")));

    while(dir_walk_read(dir_walk, path, &fileinfo) == DirWalkOK) {
        string_right(path, strlen(EXT_PATH("dirwalk/")));
        mu_check(storage_test_paths_mark(paths, path, (fileinfo.flags & FSF_DIRECTORY)));
    }

    dir_walk_free(dir_walk);
    string_clear(path);

    mu_check(storage_test_paths_check(paths) == false);

    storage_test_paths_free(paths);
}

MU_TEST_1(test_dirwalk_no_recursive, Storage* storage) {
    string_t path;
    string_init(path);
    FileInfo fileinfo;

    StorageTestPathDict_t* paths = storage_test_paths_alloc(
        storage_test_dirwalk_no_recursive, COUNT_OF(storage_test_dirwalk_no_recursive));

    DirWalk* dir_walk = dir_walk_alloc(storage);
    dir_walk_set_recursive(dir_walk, false);
    mu_check(dir_walk_open(dir_walk, EXT_PATH("dirwalk")));

    while(dir_walk_read(dir_walk, path, &fileinfo) == DirWalkOK) {
        string_right(path, strlen(EXT_PATH("dirwalk/")));
        mu_check(storage_test_paths_mark(paths, path, (fileinfo.flags & FSF_DIRECTORY)));
    }

    dir_walk_free(dir_walk);
    string_clear(path);

    mu_check(storage_test_paths_check(paths) == false);

    storage_test_paths_free(paths);
}

static bool test_dirwalk_filter_no_folder_ext(const char* name, FileInfo* fileinfo, void* ctx) {
    UNUSED(ctx);

    // only files
    if(!(fileinfo->flags & FSF_DIRECTORY)) {
        // with ".test" in name
        if(strstr(name, ".test") != NULL) {
            return true;
        }
    }

    return false;
}

MU_TEST_1(test_dirwalk_filter, Storage* storage) {
    string_t path;
    string_init(path);
    FileInfo fileinfo;

    StorageTestPathDict_t* paths = storage_test_paths_alloc(
        storage_test_dirwalk_filter, COUNT_OF(storage_test_dirwalk_filter));

    DirWalk* dir_walk = dir_walk_alloc(storage);
    dir_walk_set_filter_cb(dir_walk, test_dirwalk_filter_no_folder_ext, NULL);
    mu_check(dir_walk_open(dir_walk, EXT_PATH("dirwalk")));

    while(dir_walk_read(dir_walk, path, &fileinfo) == DirWalkOK) {
        string_right(path, strlen(EXT_PATH("dirwalk/")));
        mu_check(storage_test_paths_mark(paths, path, (fileinfo.flags & FSF_DIRECTORY)));
    }

    dir_walk_free(dir_walk);
    string_clear(path);

    mu_check(storage_test_paths_check(paths) == false);

    storage_test_paths_free(paths);
}

MU_TEST_SUITE(test_dirwalk_suite) {
    Storage* storage = furi_record_open(RECORD_STORAGE);
    storage_dirs_create(storage, EXT_PATH("dirwalk"));

    MU_RUN_TEST_1(test_dirwalk_full, storage);
    MU_RUN_TEST_1(test_dirwalk_no_recursive, storage);
    MU_RUN_TEST_1(test_dirwalk_filter, storage);

    storage_simply_remove_recursive(storage, EXT_PATH("dirwalk"));
    furi_record_close(RECORD_STORAGE);
}

int run_minunit_test_dirwalk() {
    MU_RUN_SUITE(test_dirwalk_suite);
    return MU_EXIT_CODE;
}