GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/virtfs/virtfs1_enumerate.cc Lines: 237 239 99.2 %
Date: 2017-11-29 Branches: 478 1120 42.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2017  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
6
static void removeTemp(StringVect &restrict list)
38
{
39
6
    int cnt = 0;
40
18
    std::sort(list.begin(), list.end());
41
42
186
    FOR_EACH (StringVectIter, it, list)
43
    {
44
336
        if (*it != "serverlistplus.xml.part")
45
        {
46
336
            logger->log("file: %d %s",
47
                cnt,
48
168
                (*it).c_str());
49
168
            cnt ++;
50
        }
51
    }
52
53
186
    FOR_EACH (StringVectIter, it, list)
54
    {
55
336
        if (*it == "serverlistplus.xml.part")
56
        {
57
            list.erase(it);
58
            return;
59
        }
60
    }
61
}
62
63
68
static bool inList(const VirtFs::List *const list,
64
                   const std::string &name)
65
{
66
430
    FOR_EACH (StringVectCIter, it, list->names)
67
    {
68
224
        if (*it == name)
69
            return true;
70
    }
71
    return false;
72
}
73
74
6
TEST_CASE("VirtFs1 enumerateFiles1", "")
75
{
76

8
    VirtFs::init(".");
77
78

8
    VirtFs::mountDirSilent("data",
79
        Append_false);
80

8
    VirtFs::mountDirSilent("../data",
81
        Append_false);
82
83
2
    VirtFs::List *list = nullptr;
84
85

8
    const int cnt1 = VirtFs::exists("test/test2.txt") ? 28 : 27;
86
2
    const int cnt2 = 28;
87
88
2
    VirtFs::permitLinks(false);
89

8
    list = VirtFs::enumerateFiles("test");
90
2
    removeTemp(list->names);
91
4
    const size_t sz = list->names.size();
92



8
    REQUIRE(sz == cnt1);
93
2
    VirtFs::freeList(list);
94
95
2
    VirtFs::permitLinks(true);
96

8
    list = VirtFs::enumerateFiles("test/");
97
2
    removeTemp(list->names);
98



10
    REQUIRE(list->names.size() == cnt2);
99
2
    VirtFs::freeList(list);
100
101
2
    VirtFs::permitLinks(true);
102

8
    list = VirtFs::enumerateFiles("test/units.xml");
103



8
    REQUIRE(list->names.empty());
104
2
    VirtFs::freeList(list);
105
106
2
    VirtFs::permitLinks(false);
107

8
    list = VirtFs::enumerateFiles("test\\");
108
2
    removeTemp(list->names);
109



10
    REQUIRE(list->names.size() == cnt1);
110
2
    VirtFs::freeList(list);
111
112

8
    VirtFs::unmountDirSilent("data");
113

8
    VirtFs::unmountDirSilent("../data");
114
2
    VirtFs::deinit();
115
2
}
116
117
6
TEST_CASE("VirtFs1 enumerateFiles2", "")
118
{
119

8
    VirtFs::init(".");
120
121

8
    VirtFs::mountDirSilent("data/test/dir1",
122
        Append_false);
123

8
    VirtFs::mountDirSilent("../data/test/dir1",
124
        Append_false);
125
126
2
    VirtFs::List *list = nullptr;
127
128

8
    list = VirtFs::enumerateFiles("/");
129



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



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



10
    REQUIRE_FALSE(inList(list, "file2.txt"));
132
2
    VirtFs::freeList(list);
133
2
    VirtFs::deinit();
134
2
}
135
136
6
TEST_CASE("VirtFs1 enumerateFiles3", "")
137
{
138

8
    VirtFs::init(".");
139
140

8
    VirtFs::mountDirSilent("data/test/dir1",
141
        Append_false);
142

8
    VirtFs::mountDirSilent("../data/test/dir1",
143
        Append_false);
144

8
    VirtFs::mountDirSilent("data/test/dir2",
145
        Append_false);
146

8
    VirtFs::mountDirSilent("../data/test/dir2",
147
        Append_false);
148
149
2
    VirtFs::List *list = nullptr;
150
151

8
    list = VirtFs::enumerateFiles("/");
152



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



12
    REQUIRE(inList(list, "file2.txt"));
154
2
    VirtFs::freeList(list);
155
2
    VirtFs::deinit();
156
2
}
157
158
6
TEST_CASE("VirtFsZip enumerateFiles4", "")
159
{
160

8
    VirtFs::init(".");
161
8
    std::string name("data/test/test.zip");
162
8
    std::string prefix("data\\test/");
163

2
    if (Files::existsLocal(name) == false)
164
2
        prefix = "../" + prefix;
165
166

4
    VirtFs::mountZip(prefix + "test.zip",
167
        Append_false);
168
169
2
    VirtFs::List *list = nullptr;
170
171

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



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




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




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

4
    VirtFs::unmountZip(prefix + "test.zip");
178
2
    VirtFs::deinit();
179
2
}
180
181
6
TEST_CASE("VirtFsZip enumerateFiles5", "")
182
{
183

8
    VirtFs::init(".");
184
8
    std::string name("data/test/test.zip");
185
8
    std::string prefix("data//test/");
186

2
    if (Files::existsLocal(name) == false)
187
2
        prefix = "../" + prefix;
188
189

4
    VirtFs::mountZip(prefix + "test.zip",
190
        Append_true);
191

4
    VirtFs::mountZip(prefix + "test2.zip",
192
        Append_true);
193
194
2
    VirtFs::List *list = nullptr;
195
196

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

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



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




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




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




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




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




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

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

4
    VirtFs::unmountZip(prefix + "test2.zip");
212
2
    VirtFs::deinit();
213
2
}
214
215
6
TEST_CASE("VirtFsZip enumerateFiles6", "")
216
{
217

8
    VirtFs::init(".");
218
8
    std::string name("data/test/test.zip");
219
8
    std::string prefix("data\\test/");
220

2
    if (Files::existsLocal(name) == false)
221
2
        prefix = "../" + prefix;
222
223

4
    VirtFs::mountZip(prefix + "test.zip",
224
        Append_false);
225
226
2
    VirtFs::List *list = nullptr;
227
228

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



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




12
    REQUIRE(inList(list, "dir"));
231
2
    VirtFs::freeList(list);
232
233

4
    VirtFs::unmountZip(prefix + "test.zip");
234
2
    VirtFs::deinit();
235
2
}
236
237
6
TEST_CASE("VirtFsZip enumerateFiles7", "")
238
{
239

8
    VirtFs::init(".");
240
8
    std::string name("data/test/test.zip");
241
8
    std::string prefix("data\\test/");
242

2
    if (Files::existsLocal(name) == false)
243
2
        prefix = "../" + prefix;
244
245

4
    VirtFs::mountZip(prefix + "test2.zip",
246
        Append_false);
247
248
2
    VirtFs::List *list = nullptr;
249
250

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



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




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




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




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




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

4
    VirtFs::unmountZip(prefix + "test2.zip");
259
2
    VirtFs::deinit();
260
2
}
261
262
6
TEST_CASE("VirtFsZip enumerateFiles8", "")
263
{
264

8
    VirtFs::init(".");
265
8
    std::string name("data/test/test.zip");
266
4
    std::string prefix;
267

2
    if (Files::existsLocal(name) == false)
268
2
        prefix = "../" + prefix;
269
270

4
    VirtFs::mountZip(prefix + "data/test/test2.zip",
271
        Append_false);
272

4
    VirtFs::mountDirSilent(prefix + "data/test",
273
        Append_false);
274
275
2
    VirtFs::List *list = nullptr;
276
277

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



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




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




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




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




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




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




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

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

4
    VirtFs::unmountDir(prefix + "data/test");
289
2
    VirtFs::deinit();
290
2
}
291
292
6
TEST_CASE("VirtFsZip enumerateFiles9", "")
293
{
294

8
    VirtFs::init(".");
295
8
    std::string name("data/test/test.zip");
296
4
    std::string prefix;
297

2
    if (Files::existsLocal(name) == false)
298
2
        prefix = "../" + prefix;
299
300

4
    VirtFs::mountZip(prefix + "data/test/test2.zip",
301
        Append_false);
302

4
    VirtFs::mountDirSilent(prefix + "data/test",
303
        Append_false);
304
305
2
    VirtFs::List *list = nullptr;
306
307

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



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




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




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




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




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

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

4
    VirtFs::unmountDir(prefix + "data/test");
317
2
    VirtFs::deinit();
318
2
}
319
320
6
TEST_CASE("VirtFsZip enumerateFiles10", "")
321
{
322

8
    VirtFs::init(".");
323
8
    std::string name("data/test/test.zip");
324
8
    std::string prefix("data\\test/");
325

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

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

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



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




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




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

10
    VirtFs::unmountZip2(prefix + "test.zip",
341
        "dir");
342
2
    VirtFs::deinit();
343
2
}
344
345
6
TEST_CASE("VirtFsZip enumerateFiles11", "")
346
{
347

8
    VirtFs::init(".");
348
8
    std::string name("data/test/test.zip");
349
8
    std::string prefix("data\\test/");
350

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

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

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



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




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




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

10
    VirtFs::unmountZip2(prefix + "test2.zip",
366
        "dir");
367
2
    VirtFs::deinit();
368
2
}
369
370
6
TEST_CASE("VirtFs1 enumerateFiles12", "")
371
{
372

8
    VirtFs::init(".");
373
374

14
    VirtFs::mountDirSilent2("data/test",
375
        "dir2",
376
        Append_false);
377

14
    VirtFs::mountDirSilent2("../data/test",
378
        "dir2",
379
        Append_false);
380
381
2
    VirtFs::List *list = nullptr;
382
383

8
    list = VirtFs::enumerateFiles("/");
384



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



12
    REQUIRE(inList(list, "file2.txt"));
386
2
    VirtFs::freeList(list);
387
388
2
    VirtFs::deinit();
389
2
}
390
391
6
TEST_CASE("VirtFs1 enumerateFiles13", "")
392
{
393

8
    VirtFs::init(".");
394
395

14
    VirtFs::mountDirSilent2("data",
396
        "test",
397
        Append_false);
398

14
    VirtFs::mountDirSilent2("../data",
399
        "test",
400
        Append_false);
401
402
2
    VirtFs::List *list = nullptr;
403
404

8
    list = VirtFs::enumerateFiles("dir2");
405



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



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

8
}