GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/virtfs/virtfs.cc Lines: 679 681 99.7 %
Date: 2017-11-29 Branches: 2232 5520 40.4 %

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
#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
6
TEST_CASE("VirtFs dirSeparator", "")
39
{
40



8
    REQUIRE(dirSeparator != nullptr);
41




14
    REQUIRE(VirtFs::getDirSeparator() == std::string(dirSeparator));
42
2
    VirtFs::updateDirSeparator();
43



8
    REQUIRE(dirSeparator != nullptr);
44




14
    REQUIRE(VirtFs::getDirSeparator() == std::string(dirSeparator));
45
2
}
46
47
6
TEST_CASE("VirtFs getBaseDir", "")
48
{
49



8
    REQUIRE(VirtFs::getBaseDir() != nullptr);
50
2
}
51
52
6
TEST_CASE("VirtFs getUserDir", "")
53
{
54



8
    REQUIRE(VirtFs::getUserDir() != nullptr);
55
2
}
56
57
6
TEST_CASE("VirtFs exists1", "")
58
{
59

8
    VirtFs::mountDirSilent("data", Append_false);
60

8
    VirtFs::mountDirSilent("../data", Append_false);
61
62




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




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




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




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




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




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




14
    REQUIRE(VirtFs::exists("units.xml") == false);
69
70

8
    VirtFs::mountDirSilent("data/test", Append_false);
71

8
    VirtFs::mountDirSilent("../data/test", Append_false);
72
73




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




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




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




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




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




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




14
    REQUIRE(VirtFs::exists("units.xml") == true);
80
81

8
    VirtFs::unmountDirSilent("data/test");
82

8
    VirtFs::unmountDirSilent("../data/test");
83
84




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




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




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




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




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




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




14
    REQUIRE(VirtFs::exists("units.xml") == false);
91
92

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

8
    VirtFs::unmountDirSilent("../data");
94
2
}
95
96
6
TEST_CASE("VirtFs exists2", "")
97
{
98
8
    std::string name("data/test/test.zip");
99
4
    std::string prefix;
100

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

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




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




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




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




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




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




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




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




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




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

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

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

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

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




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




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




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




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




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




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




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




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




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




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




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

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

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

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

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

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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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

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

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

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

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

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




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

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

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




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




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




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




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




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




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




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




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




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




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




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




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




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

10
    VirtFs::unmountZip2(prefix + "data/test/test2.zip",
212
        "dir");
213
2
}
214
215
20
static void removeTemp(StringVect &restrict list)
216
{
217
20
    int cnt = 0;
218
60
    std::sort(list.begin(), list.end());
219
220
540
    FOR_EACH (StringVectIter, it, list)
221
    {
222
960
        if (*it != "serverlistplus.xml.part")
223
        {
224
960
            logger->log("file: %d %s",
225
                cnt,
226
480
                (*it).c_str());
227
480
            cnt ++;
228
        }
229
    }
230
231
540
    FOR_EACH (StringVectIter, it, list)
232
    {
233
960
        if (*it == "serverlistplus.xml.part")
234
        {
235
            list.erase(it);
236
            return;
237
        }
238
    }
239
}
240
241
22
static bool inList(const VirtFs::List *const list,
242
                   const std::string &name)
243
{
244
168
    FOR_EACH (StringVectCIter, it, list->names)
245
    {
246
102
        if (*it == name)
247
            return true;
248
    }
249
    return false;
250
}
251
252
6
TEST_CASE("VirtFs enumerateFiles1", "")
253
{
254

8
    VirtFs::mountDirSilent("data", Append_false);
255

8
    VirtFs::mountDirSilent("../data", Append_false);
256
257
2
    VirtFs::List *list = nullptr;
258
259

8
    const int cnt1 = VirtFs::exists("test/test2.txt") ? 28 : 27;
260
2
    const int cnt2 = 28;
261
262
2
    VirtFs::permitLinks(false);
263

8
    list = VirtFs::enumerateFiles("test");
264
2
    removeTemp(list->names);
265
4
    const size_t sz = list->names.size();
266



8
    REQUIRE(sz == cnt1);
267
2
    VirtFs::freeList(list);
268
269
2
    VirtFs::permitLinks(true);
270

8
    list = VirtFs::enumerateFiles("test");
271
2
    removeTemp(list->names);
272



10
    REQUIRE(list->names.size() == cnt2);
273
2
    VirtFs::freeList(list);
274
275
2
    VirtFs::permitLinks(false);
276

8
    list = VirtFs::enumerateFiles("test");
277
2
    removeTemp(list->names);
278



10
    REQUIRE(list->names.size() == cnt1);
279
2
    VirtFs::freeList(list);
280
281

8
    list = VirtFs::enumerateFiles("test/units.xml");
282
2
    removeTemp(list->names);
283



8
    REQUIRE(list->names.empty());
284
2
    VirtFs::freeList(list);
285
286

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

8
    VirtFs::unmountDirSilent("../data");
288
2
}
289
290
6
TEST_CASE("VirtFs enumerateFiles2", "")
291
{
292

8
    VirtFs::mountDirSilent("data/test/dir1",
293
        Append_false);
294

8
    VirtFs::mountDirSilent("../data/test/dir1",
295
        Append_false);
296
297
2
    VirtFs::List *list = nullptr;
298
299

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



10
    REQUIRE(list->names.size() == 5);
301
2
    VirtFs::freeList(list);
302
303

8
    VirtFs::unmountDirSilent("data/test/dir1");
304

8
    VirtFs::unmountDirSilent("../data/test/dir1");
305
2
}
306
307
6
TEST_CASE("VirtFs enumerateFiles3", "")
308
{
309
8
    std::string name("data/test/test.zip");
310
4
    std::string prefix;
311

2
    if (Files::existsLocal(name) == false)
312
2
        prefix = "../" + prefix;
313
314

4
    VirtFs::mountZip(prefix + "data/test/test.zip",
315
        Append_false);
316
317
2
    VirtFs::List *list = nullptr;
318
319

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



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




12
    REQUIRE(inList(list, "dir"));
322
2
    VirtFs::freeList(list);
323
324

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

2
    if (Files::existsLocal(name) == false)
332
2
        prefix = "../" + prefix;
333
334

4
    VirtFs::mountZip(prefix + "data/test/test2.zip",
335
        Append_false);
336
337
2
    VirtFs::List *list = nullptr;
338
339

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



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




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




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




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




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

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

2
    if (Files::existsLocal(name) == false)
355
2
        prefix = "../" + prefix;
356
357

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

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

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




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




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




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




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




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




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

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

4
    VirtFs::unmountDirSilent(prefix + "data/test");
374
2
}
375
376
6
TEST_CASE("VirtFs isDirectory1", "")
377
{
378

8
    VirtFs::mountDirSilent("data", Append_false);
379

8
    VirtFs::mountDirSilent("../data", Append_false);
380
381




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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

8
    VirtFs::mountDirSilent("data/test", Append_false);
403

8
    VirtFs::mountDirSilent("../data/test", Append_false);
404
405




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




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




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




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




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




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




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




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




14
    REQUIRE(VirtFs::isDirectory("test/dir1") == true);
414
415

8
    VirtFs::unmountDirSilent("data/test");
416

8
    VirtFs::unmountDirSilent("../data/test");
417
418




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




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




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




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




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




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




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




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




14
    REQUIRE(VirtFs::isDirectory("test/dir1") == true);
427
428

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

8
    VirtFs::unmountDirSilent("../data");
430
2
}
431
432
6
TEST_CASE("VirtFs isDirectory2", "")
433
{
434
8
    std::string name("data/test/test.zip");
435
4
    std::string prefix;
436

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

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




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




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




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




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




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




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




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

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

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

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

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




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




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




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




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




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




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




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




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




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




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

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

4
    VirtFs::unmountDir(prefix + "data");
475
2
}
476
477
6
TEST_CASE("VirtFs openRead1", "")
478
{
479

8
    VirtFs::mountDirSilent("data", Append_false);
480

8
    VirtFs::mountDirSilent("../data", Append_false);
481
482
2
    VirtFs::File *file = nullptr;
483
484

8
    file = VirtFs::openRead("test/units.xml");
485



8
    REQUIRE(file != nullptr);
486
2
    VirtFs::close(file);
487

8
    file = VirtFs::openRead("test/units123.xml");
488



8
    REQUIRE(file == nullptr);
489

8
    file = VirtFs::openRead("tesQ/units.xml");
490



8
    REQUIRE(file == nullptr);
491

8
    file = VirtFs::openRead("units.xml");
492



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

8
    file = VirtFs::openRead("testQ");
496



8
    REQUIRE(file == nullptr);
497
498

8
    VirtFs::mountDirSilent("data/test", Append_false);
499

8
    VirtFs::mountDirSilent("../data/test", Append_false);
500
501

8
    file = VirtFs::openRead("test/units.xml");
502



8
    REQUIRE(file != nullptr);
503
2
    VirtFs::close(file);
504

8
    file = VirtFs::openRead("test/units123.xml");
505



8
    REQUIRE(file == nullptr);
506

8
    file = VirtFs::openRead("tesQ/units.xml");
507



8
    REQUIRE(file == nullptr);
508

8
    file = VirtFs::openRead("units.xml");
509



8
    REQUIRE(file != nullptr);
510
2
    VirtFs::close(file);
511
//    file = VirtFs::openRead("test");
512
//    REQUIRE(file == nullptr);
513

8
    file = VirtFs::openRead("testQ");
514



8
    REQUIRE(file == nullptr);
515
516

8
    VirtFs::unmountDirSilent("data/test");
517

8
    VirtFs::unmountDirSilent("../data/test");
518
519

8
    file = VirtFs::openRead("test/units.xml");
520



8
    REQUIRE(file != nullptr);
521
2
    VirtFs::close(file);
522

8
    file = VirtFs::openRead("test/units123.xml");
523



8
    REQUIRE(file == nullptr);
524

8
    file = VirtFs::openRead("tesQ/units.xml");
525



8
    REQUIRE(file == nullptr);
526

8
    file = VirtFs::openRead("units.xml");
527



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

8
    file = VirtFs::openRead("testQ");
531



8
    REQUIRE(file == nullptr);
532
533

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

8
    VirtFs::unmountDirSilent("../data");
535
2
}
536
537
6
TEST_CASE("VirtFs openRead2", "")
538
{
539
8
    std::string name("data/test/test.zip");
540
4
    std::string prefix;
541

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

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

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



8
    REQUIRE(file == nullptr);
550

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



8
    REQUIRE(file != nullptr);
552
2
    VirtFs::close(file);
553

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



8
    REQUIRE(file != nullptr);
555
2
    VirtFs::close(file);
556

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



8
    REQUIRE(file != nullptr);
558
2
    VirtFs::close(file);
559
560

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

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

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

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

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



8
    REQUIRE(file == nullptr);
577

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



8
    REQUIRE(file != nullptr);
579
2
    VirtFs::close(file);
580

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



8
    REQUIRE(file != nullptr);
582
2
    VirtFs::close(file);
583

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



8
    REQUIRE(file != nullptr);
585
2
    VirtFs::close(file);
586

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



8
    REQUIRE(file != nullptr);
588
2
    VirtFs::close(file);
589

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



8
    REQUIRE(file == nullptr);
591
592

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

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




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




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

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






11
    REQUIRE((dir1 || VirtFs::mountDirSilent("../data", Append_false))
603
        == true);
604
2
    if (dir1 == true)
605
    {
606




8
        REQUIRE(VirtFs::getRealDir("test") == "data");
607




8
        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




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

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

8
    VirtFs::mountDirSilent("../data/test", Append_false);
620
2
    if (dir1 == true)
621
    {
622




8
        REQUIRE(VirtFs::getRealDir("test") == "data");
623




8
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
624
            "data");
625





10
        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




16
    REQUIRE(VirtFs::getRealDir("zzz").empty());
637
638
2
    if (dir1 == true)
639
    {
640

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






12
        REQUIRE(VirtFs::getRealDir("dir/brimmedhat.png") ==
642
            "data" + sep + "test" + sep + "test.zip");
643





10
        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

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

8
    VirtFs::unmountDirSilent("../data/test");
657
658
2
    if (dir1 == true)
659
    {
660




8
        REQUIRE(VirtFs::getRealDir("test") == "data");
661




8
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
662
            "data");
663






12
        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




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




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

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

8
    VirtFs::unmountDirSilent("../data");
679
2
    if (dir1 == true)
680

4
        VirtFs::unmountZip("data/test/test.zip");
681
    else
682

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

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

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

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

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




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






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






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







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







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

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

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

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

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

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

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




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






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






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






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







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

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

4
    VirtFs::unmountDir(prefix + "data/test");
740
2
}
741
742
6
TEST_CASE("VirtFs permitLinks1", "")
743
{
744

8
    VirtFs::mountDirSilent("data", Append_false);
745

8
    VirtFs::mountDirSilent("../data", Append_false);
746
747

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

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



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

8
    VirtFs::getFiles("test", list);
760
2
    removeTemp(list);
761



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

8
    VirtFs::getFiles("test", list);
766
2
    removeTemp(list);
767



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

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

8
    VirtFs::unmountDirSilent("../data");
771
2
}
772
773
6
TEST_CASE("VirtFs permitLinks2", "")
774
{
775

14
    VirtFs::mountDirSilent2("data",
776
        "test",
777
        Append_false);
778

14
    VirtFs::mountDirSilent2("../data",
779
        "test",
780
        Append_false);
781
782

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

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



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

8
    VirtFs::getFiles(dirSeparator, list);
795
2
    removeTemp(list);
796



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

8
    VirtFs::getFiles(dirSeparator, list);
801
2
    removeTemp(list);
802



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

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

14
    VirtFs::unmountDirSilent2("../data",
807
        "test");
808
2
}
809
810
6
TEST_CASE("VirtFs read1", "")
811
{
812

8
    VirtFs::mountDirSilent("data", Append_false);
813

8
    VirtFs::mountDirSilent("../data", Append_false);
814
815

8
    VirtFs::File *file = VirtFs::openRead("test/test.txt");
816



8
    REQUIRE(file != nullptr);
817



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



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



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



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



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



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



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



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



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



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



8
    REQUIRE(VirtFs::eof(file) == true);
836
837
2
    VirtFs::close(file);
838
2
    free(buffer);
839
840

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

8
    VirtFs::unmountDirSilent("../data");
842
2
}
843
844
6
TEST_CASE("VirtFs read2", "")
845
{
846
8
    std::string name("data/test/test.zip");
847
4
    std::string prefix;
848

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

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

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



8
    REQUIRE(file != nullptr);
855




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




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



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




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




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




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




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




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




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



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




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

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

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

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

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

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



8
    REQUIRE(file != nullptr);
893




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




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



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




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




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




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




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




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




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



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




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

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

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

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

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

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

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



8
    REQUIRE(file != nullptr);
932




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




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



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




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




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




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




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




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




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



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




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

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

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

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

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

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

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



8
    REQUIRE(file != nullptr);
971




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




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



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




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




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




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




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




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




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



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




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

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

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

8
}