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

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

















227
    FOR_EACH (StringVectCIter, it, list->names)
67
    {
68

















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

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



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



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



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



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



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



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



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



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



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

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

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



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




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




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

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

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

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

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



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




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




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




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




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




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

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

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

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

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



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




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

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

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

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



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




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




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




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




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

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

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

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



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




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




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




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




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




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




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

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

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

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

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



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




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




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




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




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

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

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

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

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

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



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




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




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

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

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

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

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



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




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




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

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



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



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



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



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

4
}