GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/virtfs/virtfs1_getrealdir.cc Lines: 87 101 86.1 %
Date: 2018-12-09 Branches: 382 1194 32.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2018  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "unittests/unittests.h"
22
23
#include "fs/files.h"
24
25
#include "fs/virtfs/fs.h"
26
27
#include "utils/checkutils.h"
28
29
#ifndef UNITTESTS_CATCH
30
#include <algorithm>
31
#endif  // UNITTESTS_CATCH
32
33
#include "debug.h"
34
35
3
TEST_CASE("VirtFs1 getRealDir1", "")
36
{
37
4
    VirtFs::init(".");
38
4
    const std::string sep = dirSeparator;
39




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




8
    REQUIRE(VirtFs::getRealDir("..").empty());
41
4
    const bool dir1 = VirtFs::mountDirSilent("data",
42
1
        Append_false);
43






6
    REQUIRE((dir1 || VirtFs::mountDirSilent("../data",
44
        Append_false)) == true);
45
1
    if (dir1 == true)
46
    {
47
        REQUIRE(VirtFs::getRealDir("test") == "data");
48
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
49
            "data");
50
        REQUIRE(VirtFs::getRealDir("test\\test.txt") ==
51
            "data");
52
        REQUIRE(VirtFs::getRealDir("test//test.txt") ==
53
            "data");
54
    }
55
    else
56
    {
57




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




8
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
59
            "../data");
60




8
        REQUIRE(VirtFs::getRealDir("test\\test.txt") ==
61
            "../data");
62




8
        REQUIRE(VirtFs::getRealDir("test//test.txt") ==
63
            "../data");
64
    }
65




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
66
67
4
    VirtFs::mountDirSilent("data/test",
68
1
        Append_false);
69
4
    VirtFs::mountDirSilent("../data/test",
70
1
        Append_false);
71
1
    if (dir1 == true)
72
    {
73
        REQUIRE(VirtFs::getRealDir("test") == "data");
74
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
75
            "data");
76
        REQUIRE(VirtFs::getRealDir("test\\test.txt") ==
77
            "data");
78
        REQUIRE(VirtFs::getRealDir("test.txt") ==
79
            "data" + sep + "test");
80
    }
81
    else
82
    {
83





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





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





10
        REQUIRE(VirtFs::getRealDir("test\\test.txt") ==
87
            ".." + sep + "data");
88






12
        REQUIRE(VirtFs::getRealDir("test.txt") ==
89
            ".." + sep + "data" + sep + "test");
90
    }
91




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
92
93

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

4
    VirtFs::unmountDirSilent("../data/test");
95
96
1
    if (dir1 == true)
97
    {
98
        REQUIRE(VirtFs::getRealDir("test") == "data");
99
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
100
            "data");
101
    }
102
    else
103
    {
104





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





10
        REQUIRE(VirtFs::getRealDir("test/test.txt") ==
106
            ".." + sep + "data");
107
    }
108




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
109
110

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

4
    VirtFs::unmountDirSilent("../data");
112
1
    VirtFs::deinit();
113
1
}
114
115
3
TEST_CASE("VirtFs1 getRealDir2", "")
116
{
117
4
    VirtFs::init(".");
118
4
    const std::string sep = dirSeparator;
119
4
    std::string name("data/test/test.zip");
120

4
    std::string prefix("data" + sep + "test" + sep);
121

1
    if (Files::existsLocal(name) == false)
122

3
        prefix = ".." + sep + prefix;
123
2
    VirtFs::mountZip(prefix + "test2.zip",
124
1
        Append_false);
125
126




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




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





9
    REQUIRE(VirtFs::getRealDir("test.txt") == prefix + "test2.zip");
129





9
    REQUIRE(VirtFs::getRealDir("dir/1") == prefix + "test2.zip");
130





9
    REQUIRE(VirtFs::getRealDir("dir\\dye.png") ==
131
        prefix + "test2.zip");
132




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
133
134
2
    VirtFs::mountZip(prefix + "test.zip",
135
1
        Append_false);
136





9
    REQUIRE(VirtFs::getRealDir("dir//dye.png") ==
137
        prefix + "test2.zip");
138





9
    REQUIRE(VirtFs::getRealDir("dir///hide.png") ==
139
        prefix + "test.zip");
140





9
    REQUIRE(VirtFs::getRealDir("dir\\\\brimmedhat.png") ==
141
        prefix + "test.zip");
142




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
143
144

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




8
    REQUIRE(VirtFs::getRealDir("dir/brimmedhat.png").empty());
147





9
    REQUIRE(VirtFs::getRealDir("test.txt") == prefix + "test2.zip");
148





9
    REQUIRE(VirtFs::getRealDir("dir//dye.png") ==
149
        prefix + "test2.zip");
150




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
151
152

2
    VirtFs::unmountZip(prefix + "test2.zip");
153
1
    VirtFs::deinit();
154
1
}
155
156
3
TEST_CASE("VirtFs1 getRealDir3", "")
157
{
158
4
    VirtFs::init(".");
159
4
    const std::string sep = dirSeparator;
160




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




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

7
    const bool dir1 = VirtFs::mountDirSilent2("data",
163
        "test",
164
1
        Append_false);
165








8
    REQUIRE((dir1 || VirtFs::mountDirSilent2("../data",
166
        "test",
167
        Append_false)) == true);
168




8
    REQUIRE(VirtFs::getRealDir("file1.txt").empty());
169
1
    if (dir1 == true)
170
    {
171
        REQUIRE(VirtFs::getRealDir("dir1") == "data");
172
        REQUIRE(VirtFs::getRealDir("simplefile.txt") == "data");
173
    }
174
    else
175
    {
176





10
        REQUIRE(VirtFs::getRealDir("dir1") == ".." + sep + "data");
177





10
        REQUIRE(VirtFs::getRealDir("simplefile.txt") == ".." + sep + "data");
178
    }
179




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
180
181

7
    VirtFs::mountDirSilent2("data/test",
182
        "dir2",
183
1
        Append_false);
184

7
    VirtFs::mountDirSilent2("../data/test",
185
        "dir2",
186
1
        Append_false);
187




8
    REQUIRE(VirtFs::getRealDir("dir").empty());
188
1
    if (dir1 == true)
189
    {
190
        REQUIRE(VirtFs::getRealDir("file1.txt") == "data" + sep + "test");
191
        REQUIRE(VirtFs::getRealDir("simplefile.txt") == "data");
192
    }
193
    else
194
    {
195






12
        REQUIRE(VirtFs::getRealDir("file1.txt") ==
196
            ".." + sep + "data" + sep + "test");
197





10
        REQUIRE(VirtFs::getRealDir("simplefile.txt") == ".." + sep + "data");
198
    }
199




8
    REQUIRE(VirtFs::getRealDir("zzz").empty());
200
201
1
    VirtFs::deinit();
202

4
}