GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/virtfs/virtfs.cc Lines: 679 693 98.0 %
Date: 2021-03-17 Branches: 2087 5430 38.4 %

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
#include "fs/paths.h"
26
27
#include "fs/virtfs/fs.h"
28
#include "fs/virtfs/list.h"
29
30
#include "utils/checkutils.h"
31
#include "utils/foreach.h"
32
33
#ifndef UNITTESTS_CATCH
34
#include <algorithm>
35
#endif  // UNITTESTS_CATCH
36
37
#include "debug.h"
38
39
3
TEST_CASE("VirtFs dirSeparator", "")
40
{
41



4
    REQUIRE(dirSeparator != nullptr);
42




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



4
    REQUIRE(dirSeparator != nullptr);
45




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



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



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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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

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

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




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




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




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




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




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




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




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




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




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

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

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

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

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




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




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




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




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




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




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




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




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




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




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



5
    REQUIRE(VirtFs::exists("dir2/paths.xml"));
140
141

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

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

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

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

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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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

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

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

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

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

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




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

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

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




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




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




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




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




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




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




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




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




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




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




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




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




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

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





86
    FOR_EACH (StringVectCIter, it, list->names)
246
    {
247





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

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



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



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



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



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



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

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

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



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




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

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

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

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



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




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




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




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




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

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

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

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

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




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




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




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




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




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




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

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

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




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




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




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




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




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




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




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




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




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




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




7
    REQUIRE(VirtFs::isDirectory("test//") == 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("test/dir1//") == true);
399




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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

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

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




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




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




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




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




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




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




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

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

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

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

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




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




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




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




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




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




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




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




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




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




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

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

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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

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

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

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



4
    REQUIRE(file == nullptr);
551

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



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

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



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

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



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

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

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

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

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

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



4
    REQUIRE(file == nullptr);
578

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



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

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



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

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



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

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



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

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



4
    REQUIRE(file == nullptr);
592
593

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

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




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




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

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






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




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




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




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

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

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





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





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






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




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

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







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






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

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

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





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





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







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




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




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

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

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

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

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

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

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

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




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






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






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







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







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

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

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

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

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

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

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




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






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






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






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







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

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

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

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

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



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

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



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

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



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

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

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

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

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



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

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



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

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



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

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

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



4
    REQUIRE(file != nullptr);
818



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



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



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



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



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



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



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



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



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



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



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

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

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

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



4
    REQUIRE(file != nullptr);
856




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




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



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




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




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




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




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




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




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



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




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

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

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

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

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

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



4
    REQUIRE(file != nullptr);
894




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




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



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




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




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




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




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




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




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



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




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

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

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

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

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

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

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



4
    REQUIRE(file != nullptr);
933




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




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



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




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




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




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




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




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




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



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




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

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

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

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

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

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

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



4
    REQUIRE(file != nullptr);
972




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




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



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




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




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




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




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




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




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



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




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

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

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

4
}