GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/virtfs/virtfs1_enumerate.cc Lines: 261 263 99.2 %
Date: 2021-03-17 Branches: 527 1182 44.6 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "unittests/unittests.h"
23
24
#include "fs/files.h"
25
26
#include "fs/virtfs/fs.h"
27
#include "fs/virtfs/list.h"
28
29
#include "utils/checkutils.h"
30
#include "utils/foreach.h"
31
32
#ifndef UNITTESTS_CATCH
33
#include <algorithm>
34
#endif  // UNITTESTS_CATCH
35
36
#include "debug.h"
37
38
3
static void removeTemp(StringVect &restrict list)
39
{
40
3
    int cnt = 0;
41
9
    std::sort(list.begin(), list.end());
42
43
99
    FOR_EACH (StringVectIter, it, list)
44
    {
45
180
        if (*it != "serverlistplus.xml.part")
46
        {
47
90
            logger->log("file: %d %s",
48
                cnt,
49
180
                (*it).c_str());
50
90
            cnt ++;
51
        }
52
    }
53
54
99
    FOR_EACH (StringVectIter, it, list)
55
    {
56
180
        if (*it == "serverlistplus.xml.part")
57
        {
58
            list.erase(it);
59
            return;
60
        }
61
    }
62
}
63
64
static bool inList(const VirtFs::List *const list,
65
                   const std::string &name)
66
{
67

















224
    FOR_EACH (StringVectCIter, it, list->names)
68
    {
69

















121
        if (*it == name)
70
            return true;
71
    }
72
    return false;
73
}
74
75
3
TEST_CASE("VirtFs1 enumerateFiles1", "")
76
{
77
4
    VirtFs::init(".");
78
79
4
    VirtFs::mountDirSilent("data",
80
1
        Append_false);
81
4
    VirtFs::mountDirSilent("../data",
82
1
        Append_false);
83
84
1
    VirtFs::List *list = nullptr;
85
86

4
    const int cnt1 = VirtFs::exists("test/test2.txt") ? 30 : 29;
87
1
    const int cnt2 = 30;
88
89
1
    VirtFs::permitLinks(false);
90
4
    list = VirtFs::enumerateFiles("test");
91
1
    removeTemp(list->names);
92
2
    const size_t sz = list->names.size();
93



4
    REQUIRE(sz == cnt1);
94
1
    VirtFs::freeList(list);
95
96
1
    VirtFs::permitLinks(true);
97
4
    list = VirtFs::enumerateFiles("test/");
98
1
    removeTemp(list->names);
99



5
    REQUIRE(list->names.size() == cnt2);
100
1
    VirtFs::freeList(list);
101
102
1
    VirtFs::permitLinks(true);
103
4
    list = VirtFs::enumerateFiles("test/units.xml");
104



4
    REQUIRE(list->names.empty());
105
1
    VirtFs::freeList(list);
106
107
1
    VirtFs::permitLinks(false);
108
4
    list = VirtFs::enumerateFiles("test\\");
109
1
    removeTemp(list->names);
110



5
    REQUIRE(list->names.size() == cnt1);
111
1
    VirtFs::freeList(list);
112
113
4
    VirtFs::unmountDirSilent("data");
114
4
    VirtFs::unmountDirSilent("../data");
115
1
    VirtFs::deinit();
116
1
}
117
118
3
TEST_CASE("VirtFs1 enumerateFiles2", "")
119
{
120
4
    VirtFs::init(".");
121
122
4
    VirtFs::mountDirSilent("data/test/dir1",
123
1
        Append_false);
124
4
    VirtFs::mountDirSilent("../data/test/dir1",
125
1
        Append_false);
126
127
1
    VirtFs::List *list = nullptr;
128
129
4
    list = VirtFs::enumerateFiles("/");
130



5
    REQUIRE(list->names.size() == 5);
131



6
    REQUIRE(inList(list, "file1.txt"));
132



7
    REQUIRE_FALSE(inList(list, "file2.txt"));
133
1
    VirtFs::freeList(list);
134
1
    VirtFs::deinit();
135
1
}
136
137
3
TEST_CASE("VirtFs1 enumerateFiles3", "")
138
{
139
3
    VirtFs::init(".");
140
141
4
    VirtFs::mountDirSilent("data/test/dir1",
142
1
        Append_false);
143
4
    VirtFs::mountDirSilent("../data/test/dir1",
144
1
        Append_false);
145
4
    VirtFs::mountDirSilent("data/test/dir2",
146
1
        Append_false);
147
4
    VirtFs::mountDirSilent("../data/test/dir2",
148
1
        Append_false);
149
150
1
    VirtFs::List *list = nullptr;
151
152
4
    list = VirtFs::enumerateFiles("/");
153



7
    REQUIRE(inList(list, "file1.txt"));
154



7
    REQUIRE(inList(list, "file2.txt"));
155
1
    VirtFs::freeList(list);
156
1
    VirtFs::deinit();
157
1
}
158
159
3
TEST_CASE("VirtFsZip enumerateFiles4", "")
160
{
161
4
    VirtFs::init(".");
162
4
    std::string name("data/test/test.zip");
163
4
    std::string prefix("data\\test/");
164

1
    if (Files::existsLocal(name) == false)
165
2
        prefix = "../" + prefix;
166
167
2
    VirtFs::mountZip(prefix + "test.zip",
168
1
        Append_false);
169
170
1
    VirtFs::List *list = nullptr;
171
172

4
    list = VirtFs::enumerateFiles("dir");
173



5
    REQUIRE(list->names.size() == 2);
174




7
    REQUIRE(inList(list, "brimmedhat.png"));
175




7
    REQUIRE(inList(list, "hide.png"));
176
1
    VirtFs::freeList(list);
177
178

2
    VirtFs::unmountZip(prefix + "test.zip");
179
1
    VirtFs::deinit();
180
1
}
181
182
3
TEST_CASE("VirtFsZip enumerateFiles5", "")
183
{
184
4
    VirtFs::init(".");
185
4
    std::string name("data/test/test.zip");
186
4
    std::string prefix("data//test/");
187

1
    if (Files::existsLocal(name) == false)
188
2
        prefix = "../" + prefix;
189
190
2
    VirtFs::mountZip(prefix + "test.zip",
191
1
        Append_true);
192
2
    VirtFs::mountZip(prefix + "test2.zip",
193
1
        Append_true);
194
195
1
    VirtFs::List *list = nullptr;
196
197

4
    list = VirtFs::enumerateFiles("dir");
198
11
    FOR_EACH (StringVectCIter, it, list->names)
199
    {
200

10
        logger->log("filename: " + *it);
201
    }
202
203



5
    REQUIRE(list->names.size() == 5);
204




7
    REQUIRE(inList(list, "brimmedhat.png"));
205




7
    REQUIRE(inList(list, "hide.png"));
206




7
    REQUIRE(inList(list, "1"));
207




7
    REQUIRE(inList(list, "gpl"));
208




7
    REQUIRE(inList(list, "dye.png"));
209
1
    VirtFs::freeList(list);
210
211

2
    VirtFs::unmountZip(prefix + "test.zip");
212

2
    VirtFs::unmountZip(prefix + "test2.zip");
213
1
    VirtFs::deinit();
214
1
}
215
216
3
TEST_CASE("VirtFsZip enumerateFiles6", "")
217
{
218
4
    VirtFs::init(".");
219
4
    std::string name("data/test/test.zip");
220
4
    std::string prefix("data\\test/");
221

1
    if (Files::existsLocal(name) == false)
222
2
        prefix = "../" + prefix;
223
224
2
    VirtFs::mountZip(prefix + "test.zip",
225
1
        Append_false);
226
227
1
    VirtFs::List *list = nullptr;
228
229

4
    list = VirtFs::enumerateFiles("/");
230



5
    REQUIRE(list->names.size() == 1);
231




7
    REQUIRE(inList(list, "dir"));
232
1
    VirtFs::freeList(list);
233
234

2
    VirtFs::unmountZip(prefix + "test.zip");
235
1
    VirtFs::deinit();
236
1
}
237
238
3
TEST_CASE("VirtFsZip enumerateFiles7", "")
239
{
240
4
    VirtFs::init(".");
241
4
    std::string name("data/test/test.zip");
242
4
    std::string prefix("data\\test/");
243

1
    if (Files::existsLocal(name) == false)
244
2
        prefix = "../" + prefix;
245
246
2
    VirtFs::mountZip(prefix + "test2.zip",
247
1
        Append_false);
248
249
1
    VirtFs::List *list = nullptr;
250
251

4
    list = VirtFs::enumerateFiles("/");
252



5
    REQUIRE(list->names.size() == 4);
253




7
    REQUIRE(inList(list, "dir"));
254




7
    REQUIRE(inList(list, "dir2"));
255




7
    REQUIRE(inList(list, "test.txt"));
256




7
    REQUIRE(inList(list, "units.xml"));
257
1
    VirtFs::freeList(list);
258
259

2
    VirtFs::unmountZip(prefix + "test2.zip");
260
1
    VirtFs::deinit();
261
1
}
262
263
3
TEST_CASE("VirtFsZip enumerateFiles8", "")
264
{
265
4
    VirtFs::init(".");
266
4
    std::string name("data/test/test.zip");
267
2
    std::string prefix;
268

1
    if (Files::existsLocal(name) == false)
269
2
        prefix = "../" + prefix;
270
271
2
    VirtFs::mountZip(prefix + "data/test/test2.zip",
272
1
        Append_false);
273
2
    VirtFs::mountDirSilent(prefix + "data/test",
274
1
        Append_false);
275
276
1
    VirtFs::List *list = nullptr;
277
278

4
    list = VirtFs::enumerateFiles("dir2");
279



5
    REQUIRE(list->names.size() >= 6);
280




7
    REQUIRE(inList(list, "file1.txt"));
281




7
    REQUIRE(inList(list, "file2.txt"));
282




7
    REQUIRE(inList(list, "hide.png"));
283




7
    REQUIRE(inList(list, "paths.xml"));
284




7
    REQUIRE(inList(list, "test.txt"));
285




7
    REQUIRE(inList(list, "units.xml"));
286
1
    VirtFs::freeList(list);
287
288

2
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
289

2
    VirtFs::unmountDir(prefix + "data/test");
290
1
    VirtFs::deinit();
291
1
}
292
293
3
TEST_CASE("VirtFsZip enumerateFiles9", "")
294
{
295
4
    VirtFs::init(".");
296
4
    std::string name("data/test/test.zip");
297
2
    std::string prefix;
298

1
    if (Files::existsLocal(name) == false)
299
2
        prefix = "../" + prefix;
300
301
2
    VirtFs::mountZip(prefix + "data/test/test2.zip",
302
1
        Append_false);
303
2
    VirtFs::mountDirSilent(prefix + "data/test",
304
1
        Append_false);
305
306
1
    VirtFs::List *list = nullptr;
307
308

4
    list = VirtFs::enumerateFiles("dir");
309



5
    REQUIRE(list->names.size() == 4);
310




7
    REQUIRE(inList(list, "1"));
311




7
    REQUIRE(inList(list, "gpl"));
312




7
    REQUIRE(inList(list, "dye.png"));
313




7
    REQUIRE(inList(list, "hide.png"));
314
1
    VirtFs::freeList(list);
315
316

2
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
317

2
    VirtFs::unmountDir(prefix + "data/test");
318
1
    VirtFs::deinit();
319
1
}
320
321
3
TEST_CASE("VirtFsZip enumerateFiles10", "")
322
{
323
4
    VirtFs::init(".");
324
4
    std::string name("data/test/test.zip");
325
4
    std::string prefix("data\\test/");
326

1
    if (Files::existsLocal(name) == false)
327
2
        prefix = "../" + prefix;
328
329

5
    VirtFs::mountZip2(prefix + "test.zip",
330
        "dir",
331
1
        Append_false);
332
333
1
    VirtFs::List *list = nullptr;
334
335

4
    list = VirtFs::enumerateFiles("/");
336



5
    REQUIRE(list->names.size() == 2);
337




7
    REQUIRE(inList(list, "brimmedhat.png"));
338




7
    REQUIRE(inList(list, "hide.png"));
339
1
    VirtFs::freeList(list);
340
341

5
    VirtFs::unmountZip2(prefix + "test.zip",
342
1
        "dir");
343
1
    VirtFs::deinit();
344
1
}
345
346
3
TEST_CASE("VirtFsZip enumerateFiles11", "")
347
{
348
4
    VirtFs::init(".");
349
4
    std::string name("data/test/test.zip");
350
4
    std::string prefix("data\\test/");
351

1
    if (Files::existsLocal(name) == false)
352
2
        prefix = "../" + prefix;
353
354

5
    VirtFs::mountZip2(prefix + "test2.zip",
355
        "dir",
356
1
        Append_false);
357
358
1
    VirtFs::List *list = nullptr;
359
360

4
    list = VirtFs::enumerateFiles("1");
361



5
    REQUIRE(list->names.size() == 2);
362




7
    REQUIRE(inList(list, "file1.txt"));
363




7
    REQUIRE(inList(list, "test.txt"));
364
1
    VirtFs::freeList(list);
365
366

5
    VirtFs::unmountZip2(prefix + "test2.zip",
367
1
        "dir");
368
1
    VirtFs::deinit();
369
1
}
370
371
3
TEST_CASE("VirtFs1 enumerateFiles12", "")
372
{
373
4
    VirtFs::init(".");
374
375
7
    VirtFs::mountDirSilent2("data/test",
376
        "dir2",
377
1
        Append_false);
378
7
    VirtFs::mountDirSilent2("../data/test",
379
        "dir2",
380
1
        Append_false);
381
382
1
    VirtFs::List *list = nullptr;
383
384
4
    list = VirtFs::enumerateFiles("/");
385



7
    REQUIRE(inList(list, "file1.txt"));
386



7
    REQUIRE(inList(list, "file2.txt"));
387
1
    VirtFs::freeList(list);
388
389
1
    VirtFs::deinit();
390
1
}
391
392
3
TEST_CASE("VirtFs1 enumerateFiles13", "")
393
{
394
4
    VirtFs::init(".");
395
396
7
    VirtFs::mountDirSilent2("data",
397
        "test",
398
1
        Append_false);
399
7
    VirtFs::mountDirSilent2("../data",
400
        "test",
401
1
        Append_false);
402
403
1
    VirtFs::List *list = nullptr;
404
405
4
    list = VirtFs::enumerateFiles("dir2");
406



7
    REQUIRE(inList(list, "file1.txt"));
407



7
    REQUIRE(inList(list, "file2.txt"));
408
1
    VirtFs::freeList(list);
409
410
1
    VirtFs::deinit();
411

4
}