GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/virtfs/virtfs.cc Lines: 679 693 98.0 %
Date: 2018-12-09 Branches: 2087 5434 38.4 %

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
#include "fs/paths.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
TEST_CASE("VirtFs dirSeparator", "")
39
{
40



4
    REQUIRE(dirSeparator != nullptr);
41




7
    REQUIRE(VirtFs::getDirSeparator() == std::string(dirSeparator));
42
1
    VirtFs::updateDirSeparator();
43



4
    REQUIRE(dirSeparator != nullptr);
44




7
    REQUIRE(VirtFs::getDirSeparator() == std::string(dirSeparator));
45
1
}
46
47
3
TEST_CASE("VirtFs getBaseDir", "")
48
{
49



4
    REQUIRE(VirtFs::getBaseDir() != nullptr);
50
1
}
51
52
3
TEST_CASE("VirtFs getUserDir", "")
53
{
54



4
    REQUIRE(VirtFs::getUserDir() != nullptr);
55
1
}
56
57
3
TEST_CASE("VirtFs exists1", "")
58
{
59
4
    VirtFs::mountDirSilent("data", Append_false);
60
4
    VirtFs::mountDirSilent("../data", Append_false);
61
62




7
    REQUIRE(VirtFs::exists("test") == true);
63




7
    REQUIRE(VirtFs::exists("test/dir1") == true);
64




7
    REQUIRE(VirtFs::exists("test/dir") == false);
65




7
    REQUIRE(VirtFs::exists("test/units.xml") == true);
66




7
    REQUIRE(VirtFs::exists("test/units123.xml") == false);
67




7
    REQUIRE(VirtFs::exists("tesQ/units.xml") == false);
68




7
    REQUIRE(VirtFs::exists("units.xml") == false);
69
70
4
    VirtFs::mountDirSilent("data/test", Append_false);
71
4
    VirtFs::mountDirSilent("../data/test", Append_false);
72
73




7
    REQUIRE(VirtFs::exists("test") == true);
74




7
    REQUIRE(VirtFs::exists("test/dir1") == true);
75




7
    REQUIRE(VirtFs::exists("test/dir") == false);
76




7
    REQUIRE(VirtFs::exists("test/units.xml") == true);
77




7
    REQUIRE(VirtFs::exists("test/units123.xml") == false);
78




7
    REQUIRE(VirtFs::exists("tesQ/units.xml") == false);
79




7
    REQUIRE(VirtFs::exists("units.xml") == true);
80
81
4
    VirtFs::unmountDirSilent("data/test");
82
4
    VirtFs::unmountDirSilent("../data/test");
83
84




7
    REQUIRE(VirtFs::exists("test") == true);
85




7
    REQUIRE(VirtFs::exists("test/dir1") == true);
86




7
    REQUIRE(VirtFs::exists("test/dir") == false);
87




7
    REQUIRE(VirtFs::exists("test/units.xml") == true);
88




7
    REQUIRE(VirtFs::exists("test/units123.xml") == false);
89




7
    REQUIRE(VirtFs::exists("tesQ/units.xml") == false);
90




7
    REQUIRE(VirtFs::exists("units.xml") == false);
91
92
4
    VirtFs::unmountDirSilent("data");
93
4
    VirtFs::unmountDirSilent("../data");
94
1
}
95
96
3
TEST_CASE("VirtFs exists2", "")
97
{
98
4
    std::string name("data/test/test.zip");
99
2
    std::string prefix;
100

1
    if (Files::existsLocal(name) == false)
101
2
        prefix = "../" + prefix;
102
103

2
    VirtFs::mountZip(prefix + "data/test/test2.zip", Append_false);
104
105




7
    REQUIRE(VirtFs::exists("test") == false);
106




7
    REQUIRE(VirtFs::exists("test/units.xml") == false);
107




7
    REQUIRE(VirtFs::exists("test.txt") == true);
108




7
    REQUIRE(VirtFs::exists("dir/hide.png") == true);
109




7
    REQUIRE(VirtFs::exists("dir/gpl") == true);
110




7
    REQUIRE(VirtFs::exists("dir/gpl/zzz") == false);
111




7
    REQUIRE(VirtFs::exists("units.xml") == true);
112




7
    REQUIRE(VirtFs::exists("units.xml.") == false);
113




7
    REQUIRE(VirtFs::exists("units.xml2") == false);
114
115

2
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
116
1
}
117
118
3
TEST_CASE("VirtFs exists3", "")
119
{
120
4
    std::string name("data/test/test.zip");
121
2
    std::string prefix;
122

1
    if (Files::existsLocal(name) == false)
123
2
        prefix = "../" + prefix;
124
125

2
    VirtFs::mountZip(prefix + "data/test/test.zip", Append_false);
126

2
    VirtFs::mountZip(prefix + "data/test/test2.zip", Append_false);
127
128




7
    REQUIRE(VirtFs::exists("test") == false);
129




7
    REQUIRE(VirtFs::exists("test/units.xml") == false);
130




6
    REQUIRE(VirtFs::exists("dir/brimmedhat.png"));
131




5
    REQUIRE(VirtFs::exists("dir//brimmedhat.png"));
132




6
    REQUIRE(VirtFs::exists("dir//hide.png"));
133




6
    REQUIRE(VirtFs::exists("dir/1"));
134




6
    REQUIRE(VirtFs::exists("dir/gpl"));
135




6
    REQUIRE(VirtFs::exists("dir/dye.png"));
136




7
    REQUIRE(VirtFs::exists("dir/2") == false);
137




7
    REQUIRE(VirtFs::exists("dir2/2") == false);
138




6
    REQUIRE(VirtFs::exists("dir2/paths.xml"));
139
140

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

2
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
142
1
}
143
144
3
TEST_CASE("VirtFs exists4", "")
145
{
146
4
    std::string name("data/test/test.zip");
147
2
    std::string prefix;
148

1
    if (Files::existsLocal(name) == false)
149
2
        prefix = "../" + prefix;
150
151

2
    VirtFs::mountZip(prefix + "data/test/test.zip", Append_false);
152

2
    VirtFs::mountDirSilent(prefix + "data/test", Append_false);
153
154




7
    REQUIRE(VirtFs::exists("test") == false);
155




7
    REQUIRE(VirtFs::exists("test/units.xml") == false);
156




6
    REQUIRE(VirtFs::exists("dir/brimmedhat.png"));
157




6
    REQUIRE(VirtFs::exists("dir//brimmedhat.png"));
158




6
    REQUIRE(VirtFs::exists("dir//hide.png"));
159




7
    REQUIRE(VirtFs::exists("dir/1") == false);
160




7
    REQUIRE(VirtFs::exists("dir/gpl") == false);
161




7
    REQUIRE(VirtFs::exists("dir/dye.png") == false);
162




7
    REQUIRE(VirtFs::exists("dir/2") == false);
163




7
    REQUIRE(VirtFs::exists("dir2/2") == false);
164




7
    REQUIRE(VirtFs::exists("dir2/paths.xml") == false);
165




6
    REQUIRE(VirtFs::exists("units.xml"));
166




6
    REQUIRE(VirtFs::exists("dir1/file1.txt"));
167




6
    REQUIRE(VirtFs::exists("dir2/file2.txt"));
168




7
    REQUIRE(VirtFs::exists("dir2/file3.txt") == false);
169
170

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

2
    VirtFs::unmountDirSilent(prefix + "data/test");
172
1
}
173
174
3
TEST_CASE("VirtFs exists5", "")
175
{
176
4
    std::string name("data/test/test.zip");
177
2
    std::string prefix;
178

1
    if (Files::existsLocal(name) == false)
179
2
        prefix = "../" + prefix;
180
181

3
    const std::string realDir = getRealPath(prefix + "data");
182

2
    logger->log("real dir: " + realDir);
183




5
    REQUIRE_FALSE(VirtFs::exists(realDir));
184
1
}
185
186
3
TEST_CASE("VirtFs exists6", "")
187
{
188
4
    std::string name("data/test/test.zip");
189
2
    std::string prefix;
190

1
    if (Files::existsLocal(name) == false)
191
2
        prefix = "../" + prefix;
192
193

5
    VirtFs::mountZip2(prefix + "data/test/test2.zip",
194
        "dir",
195
1
        Append_false);
196
197




7
    REQUIRE(VirtFs::exists("test") == false);
198




7
    REQUIRE(VirtFs::exists("test/units.xml") == false);
199




7
    REQUIRE(VirtFs::exists("test.txt") == false);
200




7
    REQUIRE(VirtFs::exists("dir/hide.png") == false);
201




7
    REQUIRE(VirtFs::exists("dir/gpl") == false);
202




7
    REQUIRE(VirtFs::exists("dir/gpl/zzz") == false);
203




7
    REQUIRE(VirtFs::exists("units.xml") == false);
204




7
    REQUIRE(VirtFs::exists("units.xml.") == false);
205




7
    REQUIRE(VirtFs::exists("units.xml2") == false);
206




6
    REQUIRE(VirtFs::exists("hide.png"));
207




6
    REQUIRE(VirtFs::exists("dye.png"));
208




6
    REQUIRE(VirtFs::exists("gpl"));
209




7
    REQUIRE(VirtFs::exists("gpl/zzz") == false);
210
211

5
    VirtFs::unmountZip2(prefix + "data/test/test2.zip",
212
1
        "dir");
213
1
}
214
215
10
static void removeTemp(StringVect &restrict list)
216
{
217
10
    int cnt = 0;
218
30
    std::sort(list.begin(), list.end());
219
220
282
    FOR_EACH (StringVectIter, it, list)
221
    {
222
504
        if (*it != "serverlistplus.xml.part")
223
        {
224
252
            logger->log("file: %d %s",
225
                cnt,
226
504
                (*it).c_str());
227
252
            cnt ++;
228
        }
229
    }
230
231
282
    FOR_EACH (StringVectIter, it, list)
232
    {
233
504
        if (*it == "serverlistplus.xml.part")
234
        {
235
            list.erase(it);
236
            return;
237
        }
238
    }
239
}
240
241
static bool inList(const VirtFs::List *const list,
242
                   const std::string &name)
243
{
244





87
    FOR_EACH (StringVectCIter, it, list->names)
245
    {
246





54
        if (*it == name)
247
            return true;
248
    }
249
    return false;
250
}
251
252
3
TEST_CASE("VirtFs enumerateFiles1", "")
253
{
254
4
    VirtFs::mountDirSilent("data", Append_false);
255
4
    VirtFs::mountDirSilent("../data", Append_false);
256
257
1
    VirtFs::List *list = nullptr;
258
259

4
    const int cnt1 = VirtFs::exists("test/test2.txt") ? 30 : 29;
260
1
    const int cnt2 = 30;
261
262
1
    VirtFs::permitLinks(false);
263
4
    list = VirtFs::enumerateFiles("test");
264
1
    removeTemp(list->names);
265
2
    const size_t sz = list->names.size();
266



4
    REQUIRE(sz == cnt1);
267
1
    VirtFs::freeList(list);
268
269
1
    VirtFs::permitLinks(true);
270
4
    list = VirtFs::enumerateFiles("test");
271
1
    removeTemp(list->names);
272



5
    REQUIRE(list->names.size() == cnt2);
273
1
    VirtFs::freeList(list);
274
275
1
    VirtFs::permitLinks(false);
276
4
    list = VirtFs::enumerateFiles("test");
277
1
    removeTemp(list->names);
278



5
    REQUIRE(list->names.size() == cnt1);
279
1
    VirtFs::freeList(list);
280
281
4
    list = VirtFs::enumerateFiles("test/units.xml");
282
1
    removeTemp(list->names);
283



4
    REQUIRE(list->names.empty());
284
1
    VirtFs::freeList(list);
285
286
4
    VirtFs::unmountDirSilent("data");
287
4
    VirtFs::unmountDirSilent("../data");
288
1
}
289
290
3
TEST_CASE("VirtFs enumerateFiles2", "")
291
{
292
4
    VirtFs::mountDirSilent("data/test/dir1",
293
1
        Append_false);
294
4
    VirtFs::mountDirSilent("../data/test/dir1",
295
1
        Append_false);
296
297
1
    VirtFs::List *list = nullptr;
298
299
4
    list = VirtFs::enumerateFiles("/");
300



5
    REQUIRE(list->names.size() == 5);
301
1
    VirtFs::freeList(list);
302
303
4
    VirtFs::unmountDirSilent("data/test/dir1");
304
4
    VirtFs::unmountDirSilent("../data/test/dir1");
305
1
}
306
307
3
TEST_CASE("VirtFs enumerateFiles3", "")
308
{
309
4
    std::string name("data/test/test.zip");
310
2
    std::string prefix;
311

1
    if (Files::existsLocal(name) == false)
312
2
        prefix = "../" + prefix;
313
314
2
    VirtFs::mountZip(prefix + "data/test/test.zip",
315
1
        Append_false);
316
317
1
    VirtFs::List *list = nullptr;
318
319

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



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




7
    REQUIRE(inList(list, "dir"));
322
1
    VirtFs::freeList(list);
323
324

2
    VirtFs::unmountZip(prefix + "data/test/test.zip");
325
1
}
326
327
3
TEST_CASE("VirtFs enumerateFiles4", "")
328
{
329
4
    std::string name("data/test/test.zip");
330
2
    std::string prefix;
331

1
    if (Files::existsLocal(name) == false)
332
2
        prefix = "../" + prefix;
333
334
2
    VirtFs::mountZip(prefix + "data/test/test2.zip",
335
1
        Append_false);
336
337
1
    VirtFs::List *list = nullptr;
338
339

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



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




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




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




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




7
    REQUIRE(inList(list, "units.xml"));
345
1
    VirtFs::freeList(list);
346
347

2
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
348
1
}
349
350
3
TEST_CASE("VirtFs enumerateFiles5", "")
351
{
352
4
    std::string name("data/test/test.zip");
353
2
    std::string prefix;
354

1
    if (Files::existsLocal(name) == false)
355
2
        prefix = "../" + prefix;
356
357
2
    VirtFs::mountZip(prefix + "data/test/test2.zip",
358
1
        Append_false);
359

2
    VirtFs::mountDirSilent(prefix + "data/test", Append_false);
360
361
1
    VirtFs::List *list = nullptr;
362
363

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




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




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




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




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




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




7
    REQUIRE(inList(list, "units.xml"));
370
1
    VirtFs::freeList(list);
371
372

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

2
    VirtFs::unmountDirSilent(prefix + "data/test");
374
1
}
375
376
3
TEST_CASE("VirtFs isDirectory1", "")
377
{
378
4
    VirtFs::mountDirSilent("data", Append_false);
379
4
    VirtFs::mountDirSilent("../data", Append_false);
380
381




7
    REQUIRE(VirtFs::isDirectory("test/units.xml") == false);
382




7
    REQUIRE(VirtFs::isDirectory("test/units.xml/") == false);
383




7
    REQUIRE(VirtFs::isDirectory("test//units.xml") == false);
384




6
    REQUIRE(VirtFs::isDirectory("test/units123.xml") == false);
385




7
    REQUIRE(VirtFs::isDirectory("test//units123.xml") == false);
386




7
    REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false);
387




7
    REQUIRE(VirtFs::isDirectory("tesQ//units.xml") == false);
388




7
    REQUIRE(VirtFs::isDirectory("units.xml") == false);
389




7
    REQUIRE(VirtFs::isDirectory("test") == true);
390




7
    REQUIRE(VirtFs::isDirectory("test/") == true);
391




7
    REQUIRE(VirtFs::isDirectory("test//") == true);
392




7
    REQUIRE(VirtFs::isDirectory("test/dir1") == true);
393




7
    REQUIRE(VirtFs::isDirectory("test//dir1") == true);
394




7
    REQUIRE(VirtFs::isDirectory("test//dir1/") == true);
395




7
    REQUIRE(VirtFs::isDirectory("test//dir1//") == true);
396




7
    REQUIRE(VirtFs::isDirectory("test/dir1/") == true);
397




7
    REQUIRE(VirtFs::isDirectory("test/dir1//") == true);
398




7
    REQUIRE(VirtFs::isDirectory("testQ") == false);
399




7
    REQUIRE(VirtFs::isDirectory("testQ/") == false);
400




7
    REQUIRE(VirtFs::isDirectory("testQ//") == false);
401
402
4
    VirtFs::mountDirSilent("data/test", Append_false);
403
4
    VirtFs::mountDirSilent("../data/test", Append_false);
404
405




7
    REQUIRE(VirtFs::isDirectory("test/units.xml") == false);
406




7
    REQUIRE(VirtFs::isDirectory("test/units.xml/") == false);
407




7
    REQUIRE(VirtFs::isDirectory("test//units.xml") == false);
408




7
    REQUIRE(VirtFs::isDirectory("test/units123.xml") == false);
409




7
    REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false);
410




7
    REQUIRE(VirtFs::isDirectory("units.xml") == false);
411




7
    REQUIRE(VirtFs::isDirectory("test") == true);
412




7
    REQUIRE(VirtFs::isDirectory("testQ") == false);
413




7
    REQUIRE(VirtFs::isDirectory("test/dir1") == true);
414
415
4
    VirtFs::unmountDirSilent("data/test");
416
4
    VirtFs::unmountDirSilent("../data/test");
417
418




7
    REQUIRE(VirtFs::isDirectory("test/units.xml") == false);
419




7
    REQUIRE(VirtFs::isDirectory("test/units123.xml") == false);
420




7
    REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false);
421




7
    REQUIRE(VirtFs::isDirectory("units.xml") == false);
422




7
    REQUIRE(VirtFs::isDirectory("units.xml/") == false);
423




7
    REQUIRE(VirtFs::isDirectory("test") == true);
424




7
    REQUIRE(VirtFs::isDirectory("test/") == true);
425




7
    REQUIRE(VirtFs::isDirectory("testQ") == false);
426




7
    REQUIRE(VirtFs::isDirectory("test/dir1") == true);
427
428
4
    VirtFs::unmountDirSilent("data");
429
4
    VirtFs::unmountDirSilent("../data");
430
1
}
431
432
3
TEST_CASE("VirtFs isDirectory2", "")
433
{
434
4
    std::string name("data/test/test.zip");
435
2
    std::string prefix;
436

1
    if (Files::existsLocal(name) == false)
437
2
        prefix = "../" + prefix;
438
439

2
    VirtFs::mountZip(prefix + "data/test/test2.zip", Append_false);
440
441




7
    REQUIRE(VirtFs::isDirectory("test/units.xml") == false);
442




7
    REQUIRE(VirtFs::isDirectory("dir") == true);
443




7
    REQUIRE(VirtFs::isDirectory("dir/") == true);
444




7
    REQUIRE(VirtFs::isDirectory("dir//") == true);
445




7
    REQUIRE(VirtFs::isDirectory("dir2") == true);
446




7
    REQUIRE(VirtFs::isDirectory("dir3") == false);
447




7
    REQUIRE(VirtFs::isDirectory("test.txt") == false);
448
449

2
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
450
1
}
451
452
3
TEST_CASE("VirtFs isDirectory3", "")
453
{
454
4
    std::string name("data/test/test.zip");
455
2
    std::string prefix;
456

1
    if (Files::existsLocal(name) == false)
457
2
        prefix = "../" + prefix;
458
459

2
    VirtFs::mountDir(prefix + "data", Append_false);
460

2
    VirtFs::mountZip(prefix + "data/test/test2.zip", Append_false);
461
462




7
    REQUIRE(VirtFs::isDirectory("test/units.xml") == false);
463




6
    REQUIRE(VirtFs::isDirectory("test"));
464




7
    REQUIRE(VirtFs::isDirectory("test//dye.png") == false);
465




6
    REQUIRE(VirtFs::isDirectory("dir"));
466




6
    REQUIRE(VirtFs::isDirectory("dir/"));
467




6
    REQUIRE(VirtFs::isDirectory("dir//"));
468




6
    REQUIRE(VirtFs::isDirectory("dir2"));
469




7
    REQUIRE(VirtFs::isDirectory("dir3") == false);
470




7
    REQUIRE(VirtFs::isDirectory("test.txt") == false);
471




7
    REQUIRE(VirtFs::isDirectory("dir/hide.png") == false);
472
473

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

2
    VirtFs::unmountDir(prefix + "data");
475
1
}
476
477
3
TEST_CASE("VirtFs openRead1", "")
478
{
479
4
    VirtFs::mountDirSilent("data", Append_false);
480
4
    VirtFs::mountDirSilent("../data", Append_false);
481
482
1
    VirtFs::File *file = nullptr;
483
484
4
    file = VirtFs::openRead("test/units.xml");
485



4
    REQUIRE(file != nullptr);
486
1
    VirtFs::close(file);
487
4
    file = VirtFs::openRead("test/units123.xml");
488



4
    REQUIRE(file == nullptr);
489
4
    file = VirtFs::openRead("tesQ/units.xml");
490



4
    REQUIRE(file == nullptr);
491
4
    file = VirtFs::openRead("units.xml");
492



4
    REQUIRE(file == nullptr);
493
//    file = VirtFs::openRead("test");
494
//    REQUIRE(file == nullptr);
495
4
    file = VirtFs::openRead("testQ");
496



4
    REQUIRE(file == nullptr);
497
498
4
    VirtFs::mountDirSilent("data/test", Append_false);
499
4
    VirtFs::mountDirSilent("../data/test", Append_false);
500
501
4
    file = VirtFs::openRead("test/units.xml");
502



4
    REQUIRE(file != nullptr);
503
1
    VirtFs::close(file);
504
4
    file = VirtFs::openRead("test/units123.xml");
505



4
    REQUIRE(file == nullptr);
506
4
    file = VirtFs::openRead("tesQ/units.xml");
507



4
    REQUIRE(file == nullptr);
508
4
    file = VirtFs::openRead("units.xml");
509



4
    REQUIRE(file != nullptr);
510
1
    VirtFs::close(file);
511
//    file = VirtFs::openRead("test");
512
//    REQUIRE(file == nullptr);
513
4
    file = VirtFs::openRead("testQ");
514



4
    REQUIRE(file == nullptr);
515
516
4
    VirtFs::unmountDirSilent("data/test");
517
4
    VirtFs::unmountDirSilent("../data/test");
518
519
4
    file = VirtFs::openRead("test/units.xml");
520



4
    REQUIRE(file != nullptr);
521
1
    VirtFs::close(file);
522
4
    file = VirtFs::openRead("test/units123.xml");
523



4
    REQUIRE(file == nullptr);
524
4
    file = VirtFs::openRead("tesQ/units.xml");
525



4
    REQUIRE(file == nullptr);
526
4
    file = VirtFs::openRead("units.xml");
527



4
    REQUIRE(file == nullptr);
528
//    file = VirtFs::openRead("test");
529
//    REQUIRE(file == nullptr);
530
4
    file = VirtFs::openRead("testQ");
531



4
    REQUIRE(file == nullptr);
532
533
4
    VirtFs::unmountDirSilent("data");
534
4
    VirtFs::unmountDirSilent("../data");
535
1
}
536
537
3
TEST_CASE("VirtFs openRead2", "")
538
{
539
4
    std::string name("data/test/test.zip");
540
2
    std::string prefix;
541

1
    if (Files::existsLocal(name) == false)
542
2
        prefix = "../" + prefix;
543
544

2
    VirtFs::mountZip(prefix + "data/test/test2.zip", Append_false);
545
546
1
    VirtFs::File *file = nullptr;
547
548

4
    file = VirtFs::openRead("test/units.xml");
549



4
    REQUIRE(file == nullptr);
550

4
    file = VirtFs::openRead("units.xml");
551



4
    REQUIRE(file != nullptr);
552
1
    VirtFs::close(file);
553

4
    file = VirtFs::openRead("dir/hide.png");
554



4
    REQUIRE(file != nullptr);
555
1
    VirtFs::close(file);
556

4
    file = VirtFs::openRead("dir//hide.png");
557



4
    REQUIRE(file != nullptr);
558
1
    VirtFs::close(file);
559
560

2
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
561
1
}
562
563
3
TEST_CASE("VirtFs openRead3", "")
564
{
565
4
    std::string name("data/test/test.zip");
566
2
    std::string prefix;
567

1
    if (Files::existsLocal(name) == false)
568
2
        prefix = "../" + prefix;
569
570

2
    VirtFs::mountZip(prefix + "data/test/test2.zip", Append_false);
571

2
    VirtFs::mountDir(prefix + "data/test", Append_false);
572
573
1
    VirtFs::File *file = nullptr;
574
575

4
    file = VirtFs::openRead("test/units.xml");
576



4
    REQUIRE(file == nullptr);
577

4
    file = VirtFs::openRead("units.xml");
578



4
    REQUIRE(file != nullptr);
579
1
    VirtFs::close(file);
580

4
    file = VirtFs::openRead("dir/hide.png");
581



4
    REQUIRE(file != nullptr);
582
1
    VirtFs::close(file);
583

4
    file = VirtFs::openRead("dir//hide.png");
584



4
    REQUIRE(file != nullptr);
585
1
    VirtFs::close(file);
586

4
    file = VirtFs::openRead("dir/dye.png");
587



4
    REQUIRE(file != nullptr);
588
1
    VirtFs::close(file);
589

4
    file = VirtFs::openRead("dir/dye.pn_");
590



4
    REQUIRE(file == nullptr);
591
592

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

2
    VirtFs::unmountDir(prefix + "data/test");
594
1
}
595
596
3
TEST_CASE("VirtFs getRealDir1", "")
597
{
598
4
    const std::string sep = dirSeparator;
599




8
    REQUIRE(VirtFs::getRealDir(".").empty());
600




8
    REQUIRE(VirtFs::getRealDir("..").empty());
601

4
    const bool dir1 = VirtFs::mountDirSilent("data", Append_false);
602






6
    REQUIRE((dir1 || VirtFs::mountDirSilent("../data", Append_false))
603
        == true);
604
1
    if (dir1 == true)
605
    {
606
        REQUIRE(VirtFs::getRealDir("test") == "data");
607
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
608
            "data");
609
    }
610
    else
611
    {
612




8
        REQUIRE(VirtFs::getRealDir("test") == "../data");
613




8
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
614
            "../data");
615
    }
616




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
617
618

4
    VirtFs::mountDirSilent("data/test", Append_false);
619

4
    VirtFs::mountDirSilent("../data/test", Append_false);
620
1
    if (dir1 == true)
621
    {
622
        REQUIRE(VirtFs::getRealDir("test") == "data");
623
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
624
            "data");
625
        REQUIRE(VirtFs::getRealDir("test.txt") ==
626
            "data" + sep + "test");
627
    }
628
    else
629
    {
630





10
        REQUIRE(VirtFs::getRealDir("test") == ".." + sep + "data");
631





10
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
632
            ".." + sep + "data");
633






12
        REQUIRE(VirtFs::getRealDir("test.txt") ==
634
            ".." + sep + "data" + sep + "test");
635
    }
636




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
637
638
1
    if (dir1 == true)
639
    {
640
        VirtFs::mountZip("data/test/test.zip", Append_false);
641
        REQUIRE(VirtFs::getRealDir("dir/brimmedhat.png") ==
642
            "data" + sep + "test" + sep + "test.zip");
643
        REQUIRE(VirtFs::getRealDir("hide.png") ==
644
            "data" + sep + "test");
645
    }
646
    else
647
    {
648

4
        VirtFs::mountZip("../data/test/test.zip", Append_false);
649







14
        REQUIRE(VirtFs::getRealDir("dir/brimmedhat.png") ==
650
            ".." + sep + "data" + sep + "test" + sep + "test.zip");
651






12
        REQUIRE(VirtFs::getRealDir("hide.png") ==
652
            ".." + sep + "data" + sep + "test");
653
    }
654
655

4
    VirtFs::unmountDirSilent("data/test");
656

4
    VirtFs::unmountDirSilent("../data/test");
657
658
1
    if (dir1 == true)
659
    {
660
        REQUIRE(VirtFs::getRealDir("test") == "data");
661
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
662
            "data");
663
        REQUIRE(VirtFs::getRealDir("dir/hide.png") ==
664
            "data" + sep + "test" + sep + "test.zip");
665
    }
666
    else
667
    {
668





10
        REQUIRE(VirtFs::getRealDir("test") == ".." + sep + "data");
669





10
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
670
            ".." + sep + "data");
671







14
        REQUIRE(VirtFs::getRealDir("dir/hide.png") ==
672
            ".." + sep + "data" + sep + "test" + sep + "test.zip");
673
    }
674




6
    REQUIRE(VirtFs::exists("dir/hide.png"));
675




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
676
677

4
    VirtFs::unmountDirSilent("data");
678

4
    VirtFs::unmountDirSilent("../data");
679
1
    if (dir1 == true)
680
        VirtFs::unmountZip("data/test/test.zip");
681
    else
682

4
        VirtFs::unmountZip("../data/test/test.zip");
683
1
}
684
685
3
TEST_CASE("VirtFs getrealDir2", "")
686
{
687
4
    const std::string sep = dirSeparator;
688
4
    std::string name("data/test/test.zip");
689
2
    std::string prefix;
690

1
    if (Files::existsLocal(name) == false)
691
2
        prefix = "../" + prefix;
692
693

2
    VirtFs::mountZip(prefix + "data/test/test2.zip", Append_false);
694

2
    VirtFs::mountDir(prefix + "data/test", Append_false);
695

2
    VirtFs::mountDir(prefix + "data", Append_false);
696
697




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
698
699






11
    REQUIRE(VirtFs::getRealDir("dir1/file1.txt") ==
700
        prefix + "data" + sep + "test");
701






11
    REQUIRE(VirtFs::getRealDir("hide.png") ==
702
        prefix + "data" + sep + "test");
703







13
    REQUIRE(VirtFs::getRealDir("dir//hide.png") ==
704
        prefix + "data" + sep + "test" + sep + "test2.zip");
705







13
    REQUIRE(VirtFs::getRealDir("dir/1//test.txt") ==
706
        prefix + "data" + sep + "test" + sep + "test2.zip");
707
708

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

2
    VirtFs::unmountDir(prefix + "data/test");
710

2
    VirtFs::unmountDir(prefix + "data");
711
1
}
712
713
3
TEST_CASE("VirtFs getrealDir3", "")
714
{
715
4
    const std::string sep = dirSeparator;
716
4
    std::string name("data/test/test.zip");
717
2
    std::string prefix;
718

1
    if (Files::existsLocal(name) == false)
719
2
        prefix = "../" + prefix;
720
721

5
    VirtFs::mountZip2(prefix + "data/test/test2.zip",
722
        "dir",
723
1
        Append_false);
724

2
    VirtFs::mountDir(prefix + "data/test", Append_false);
725
726




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
727
728






11
    REQUIRE(VirtFs::getRealDir("dir1/file1.txt") ==
729
        prefix + "data" + sep + "test");
730






11
    REQUIRE(VirtFs::getRealDir("hide.png") ==
731
        prefix + "data" + sep + "test");
732






11
    REQUIRE(VirtFs::getRealDir("hide.png") ==
733
        prefix + "data" + sep + "test");
734







13
    REQUIRE(VirtFs::getRealDir("1//test.txt") ==
735
        prefix + "data" + sep + "test" + sep + "test2.zip");
736
737

5
    VirtFs::unmountZip2(prefix + "data/test/test2.zip",
738
1
        "dir");
739

2
    VirtFs::unmountDir(prefix + "data/test");
740
1
}
741
742
3
TEST_CASE("VirtFs permitLinks1", "")
743
{
744
4
    VirtFs::mountDirSilent("data", Append_false);
745
4
    VirtFs::mountDirSilent("../data", Append_false);
746
747

4
    const int cnt1 = VirtFs::exists("test/test2.txt") ? 27 : 26;
748
1
    const int cnt2 = 27;
749
750
2
    StringVect list;
751
1
    VirtFs::permitLinks(false);
752

4
    VirtFs::getFiles("test", list);
753
1
    removeTemp(list);
754
2
    const size_t sz = list.size();
755



4
    REQUIRE(sz == cnt1);
756
757
1
    list.clear();
758
1
    VirtFs::permitLinks(true);
759

4
    VirtFs::getFiles("test", list);
760
1
    removeTemp(list);
761



5
    REQUIRE(list.size() == cnt2);
762
763
1
    list.clear();
764
1
    VirtFs::permitLinks(false);
765

4
    VirtFs::getFiles("test", list);
766
1
    removeTemp(list);
767



5
    REQUIRE(list.size() == cnt1);
768
769

4
    VirtFs::unmountDirSilent("data");
770

4
    VirtFs::unmountDirSilent("../data");
771
1
}
772
773
3
TEST_CASE("VirtFs permitLinks2", "")
774
{
775
7
    VirtFs::mountDirSilent2("data",
776
        "test",
777
1
        Append_false);
778
7
    VirtFs::mountDirSilent2("../data",
779
        "test",
780
1
        Append_false);
781
782

4
    const int cnt1 = VirtFs::exists("test2.txt") ? 27 : 26;
783
1
    const int cnt2 = 27;
784
785
2
    StringVect list;
786
1
    VirtFs::permitLinks(false);
787

4
    VirtFs::getFiles(dirSeparator, list);
788
1
    removeTemp(list);
789
2
    const size_t sz = list.size();
790



4
    REQUIRE(sz == cnt1);
791
792
1
    list.clear();
793
1
    VirtFs::permitLinks(true);
794

4
    VirtFs::getFiles(dirSeparator, list);
795
1
    removeTemp(list);
796



5
    REQUIRE(list.size() == cnt2);
797
798
1
    list.clear();
799
1
    VirtFs::permitLinks(false);
800

4
    VirtFs::getFiles(dirSeparator, list);
801
1
    removeTemp(list);
802



5
    REQUIRE(list.size() == cnt1);
803
804

7
    VirtFs::unmountDirSilent2("data",
805
1
        "test");
806

7
    VirtFs::unmountDirSilent2("../data",
807
1
        "test");
808
1
}
809
810
3
TEST_CASE("VirtFs read1", "")
811
{
812
4
    VirtFs::mountDirSilent("data", Append_false);
813
4
    VirtFs::mountDirSilent("../data", Append_false);
814
815
4
    VirtFs::File *file = VirtFs::openRead("test/test.txt");
816



4
    REQUIRE(file != nullptr);
817



4
    REQUIRE(VirtFs::fileLength(file) == 23);
818
1
    const int fileSize = VirtFs::fileLength(file);
819
820
1
    void *restrict buffer = calloc(fileSize + 1, 1);
821



4
    REQUIRE(VirtFs::read(file, buffer, 1, fileSize) == fileSize);
822



4
    REQUIRE(strcmp(static_cast<char*>(buffer),
823
        "test line 1\ntest line 2") == 0);
824



4
    REQUIRE(VirtFs::tell(file) == fileSize);
825



4
    REQUIRE(VirtFs::eof(file) == true);
826
827
1
    free(buffer);
828
1
    buffer = calloc(fileSize + 1, 1);
829



4
    REQUIRE(VirtFs::seek(file, 12) != 0);
830



4
    REQUIRE(VirtFs::eof(file) == false);
831



4
    REQUIRE(VirtFs::tell(file) == 12);
832



4
    REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11);
833



4
    REQUIRE(strcmp(static_cast<char*>(buffer),
834
        "test line 2") == 0);
835



4
    REQUIRE(VirtFs::eof(file) == true);
836
837
1
    VirtFs::close(file);
838
1
    free(buffer);
839
840
4
    VirtFs::unmountDirSilent("data");
841
4
    VirtFs::unmountDirSilent("../data");
842
1
}
843
844
3
TEST_CASE("VirtFs read2", "")
845
{
846
4
    std::string name("data/test/test.zip");
847
2
    std::string prefix;
848

1
    if (Files::existsLocal(name) == false)
849
2
        prefix = "../" + prefix;
850
851

2
    VirtFs::mountZip(prefix + "data/test/test2.zip", Append_false);
852
853

4
    VirtFs::File *file = VirtFs::openRead("dir2/test.txt");
854



4
    REQUIRE(file != nullptr);
855




4
    REQUIRE(VirtFs::fileLength(file) == 23);
856
1
    const int fileSize = VirtFs::fileLength(file);
857
858
1
    void *restrict buffer = calloc(fileSize + 1, 1);
859




4
    REQUIRE(VirtFs::read(file, buffer, 1, fileSize) == fileSize);
860



4
    REQUIRE(strcmp(static_cast<char*>(buffer),
861
        "test line 1\ntest line 2") == 0);
862




4
    REQUIRE(VirtFs::tell(file) == fileSize);
863




4
    REQUIRE(VirtFs::eof(file) == true);
864
865
1
    free(buffer);
866
1
    buffer = calloc(fileSize + 1, 1);
867




4
    REQUIRE(VirtFs::seek(file, 12) != 0);
868




4
    REQUIRE(VirtFs::eof(file) == false);
869




4
    REQUIRE(VirtFs::tell(file) == 12);
870




4
    REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11);
871



4
    REQUIRE(strcmp(static_cast<char*>(buffer),
872
        "test line 2") == 0);
873




4
    REQUIRE(VirtFs::eof(file) == true);
874
875
1
    VirtFs::close(file);
876
1
    free(buffer);
877
878

2
    VirtFs::unmountZip(prefix + "data/test/test2.zip");
879
1
}
880
881
3
TEST_CASE("VirtFs read3", "")
882
{
883
4
    std::string name("data/test/test.zip");
884
2
    std::string prefix;
885

1
    if (Files::existsLocal(name) == false)
886
2
        prefix = "../" + prefix;
887
888

2
    VirtFs::mountZip(prefix + "data/test/test2.zip", Append_false);
889

2
    VirtFs::mountDir(prefix + "data", Append_false);
890
891

4
    VirtFs::File *file = VirtFs::openRead("dir2/test.txt");
892



4
    REQUIRE(file != nullptr);
893




4
    REQUIRE(VirtFs::fileLength(file) == 23);
894
1
    const int fileSize = VirtFs::fileLength(file);
895
896
1
    void *restrict buffer = calloc(fileSize + 1, 1);
897




4
    REQUIRE(VirtFs::read(file, buffer, 1, fileSize) == fileSize);
898



4
    REQUIRE(strcmp(static_cast<char*>(buffer),
899
        "test line 1\ntest line 2") == 0);
900




4
    REQUIRE(VirtFs::tell(file) == fileSize);
901




4
    REQUIRE(VirtFs::eof(file) == true);
902
903
1
    free(buffer);
904
1
    buffer = calloc(fileSize + 1, 1);
905




4
    REQUIRE(VirtFs::seek(file, 12) != 0);
906




4
    REQUIRE(VirtFs::eof(file) == false);
907




4
    REQUIRE(VirtFs::tell(file) == 12);
908




4
    REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11);
909



4
    REQUIRE(strcmp(static_cast<char*>(buffer),
910
        "test line 2") == 0);
911




4
    REQUIRE(VirtFs::eof(file) == true);
912
913
1
    VirtFs::close(file);
914
1
    free(buffer);
915
916

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

2
    VirtFs::unmountDir(prefix + "data");
918
1
}
919
920
3
TEST_CASE("VirtFs read4", "")
921
{
922
4
    std::string name("data/test/test.zip");
923
2
    std::string prefix;
924

1
    if (Files::existsLocal(name) == false)
925
2
        prefix = "../" + prefix;
926
927

2
    VirtFs::mountDir(prefix + "data/test", Append_true);
928

2
    VirtFs::mountZip(prefix + "data/test/test5.zip", Append_true);
929
930

4
    VirtFs::File *file = VirtFs::openRead("dir1/file1.txt");
931



4
    REQUIRE(file != nullptr);
932




4
    REQUIRE(VirtFs::fileLength(file) == 23);
933
1
    const int fileSize = VirtFs::fileLength(file);
934
935
1
    void *restrict buffer = calloc(fileSize + 1, 1);
936




4
    REQUIRE(VirtFs::read(file, buffer, 1, fileSize) == fileSize);
937



4
    REQUIRE(strcmp(static_cast<char*>(buffer),
938
        "test line 1\ntest line 2") == 0);
939




4
    REQUIRE(VirtFs::tell(file) == fileSize);
940




4
    REQUIRE(VirtFs::eof(file) == true);
941
942
1
    free(buffer);
943
1
    buffer = calloc(fileSize + 1, 1);
944




4
    REQUIRE(VirtFs::seek(file, 12) != 0);
945




4
    REQUIRE(VirtFs::eof(file) == false);
946




4
    REQUIRE(VirtFs::tell(file) == 12);
947




4
    REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11);
948



4
    REQUIRE(strcmp(static_cast<char*>(buffer),
949
        "test line 2") == 0);
950




4
    REQUIRE(VirtFs::eof(file) == true);
951
952
1
    VirtFs::close(file);
953
1
    free(buffer);
954
955

2
    VirtFs::unmountZip(prefix + "data/test/test5.zip");
956

2
    VirtFs::unmountDir(prefix + "data/test");
957
1
}
958
959
3
TEST_CASE("VirtFs read5", "")
960
{
961
4
    std::string name("data/test/test.zip");
962
2
    std::string prefix;
963

1
    if (Files::existsLocal(name) == false)
964
2
        prefix = "../" + prefix;
965
966

2
    VirtFs::mountZip(prefix + "data/test/test5.zip", Append_true);
967

2
    VirtFs::mountDir(prefix + "data/test", Append_true);
968
969

4
    VirtFs::File *file = VirtFs::openRead("dir1/file1.txt");
970



4
    REQUIRE(file != nullptr);
971




4
    REQUIRE(VirtFs::fileLength(file) == 23);
972
1
    const int fileSize = VirtFs::fileLength(file);
973
974
1
    void *restrict buffer = calloc(fileSize + 1, 1);
975




4
    REQUIRE(VirtFs::read(file, buffer, 1, fileSize) == fileSize);
976



4
    REQUIRE(strcmp(static_cast<char*>(buffer),
977
        "test line 3\ntest line 4") == 0);
978




4
    REQUIRE(VirtFs::tell(file) == fileSize);
979




4
    REQUIRE(VirtFs::eof(file) == true);
980
981
1
    free(buffer);
982
1
    buffer = calloc(fileSize + 1, 1);
983




4
    REQUIRE(VirtFs::seek(file, 12) != 0);
984




4
    REQUIRE(VirtFs::eof(file) == false);
985




4
    REQUIRE(VirtFs::tell(file) == 12);
986




4
    REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11);
987



4
    REQUIRE(strcmp(static_cast<char*>(buffer),
988
        "test line 4") == 0);
989




4
    REQUIRE(VirtFs::eof(file) == true);
990
991
1
    VirtFs::close(file);
992
1
    free(buffer);
993
994

2
    VirtFs::unmountZip(prefix + "data/test/test5.zip");
995

2
    VirtFs::unmountDir(prefix + "data/test");
996

4
}