GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/virtfs/zip.cc Lines: 157 157 100.0 %
Date: 2018-09-20 Branches: 545 1370 39.8 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2017-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 "logger.h"
24
25
#include "fs/files.h"
26
27
#include "fs/virtfs/fszip.h"
28
#include "fs/virtfs/zipentry.h"
29
#include "fs/virtfs/zipreader.h"
30
#include "fs/virtfs/ziplocalheader.h"
31
32
#include "debug.h"
33
34
extern const char *dirSeparator;
35
36
6
TEST_CASE("Zip readArchiveInfo", "")
37
{
38
16
    std::string name("data/test/test.zip");
39
8
    std::string prefix;
40
16
    const std::string sep = dirSeparator;
41

4
    if (Files::existsLocal(name) == false)
42
        prefix = "../";
43
44



28
    SECTION("test.zip")
45
    {
46
2
        name = prefix + "data/test/test.zip";
47
48
        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
49
            dirSeparator,
50


4
            VirtFs::FsZip::getFuncs());
51
1
        STD_VECTOR<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;
52
53




3
        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
54



5
        REQUIRE(headers.size() == 2);
55



4
        REQUIRE(entry->root == name);
56




6
        REQUIRE(headers[0]->fileName == "dir" + sep + "hide.png");
57



4
        REQUIRE(headers[0]->compressSize == 365);
58



4
        REQUIRE(headers[0]->uncompressSize == 368);
59




6
        REQUIRE(headers[1]->fileName == "dir" + sep + "brimmedhat.png");
60



4
        REQUIRE(headers[1]->compressSize == 1959);
61



4
        REQUIRE(headers[1]->uncompressSize == 1959);
62
63
1
        delete entry;
64
    }
65
66



28
    SECTION("test2.zip")
67
    {
68
2
        name = prefix + "data/test/test2.zip";
69
70
        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
71
            dirSeparator,
72


4
            VirtFs::FsZip::getFuncs());
73
1
        STD_VECTOR<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;
74
75




3
        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
76



5
        REQUIRE(headers.size() == 11);
77



4
        REQUIRE(entry->root == name);
78



4
        REQUIRE(headers[0]->fileName == "test.txt");
79



4
        REQUIRE(headers[0]->compressSize == 17);
80



4
        REQUIRE(headers[0]->uncompressSize == 23);
81
82




6
        REQUIRE(headers[1]->fileName == "dir2" + sep + "hide.png");
83



4
        REQUIRE(headers[1]->compressSize == 365);
84



4
        REQUIRE(headers[1]->uncompressSize == 368);
85
86




6
        REQUIRE(headers[2]->fileName == "dir2" + sep + "test.txt");
87



4
        REQUIRE(headers[2]->compressSize == 17);
88



4
        REQUIRE(headers[2]->uncompressSize == 23);
89
90




6
        REQUIRE(headers[3]->fileName == "dir2" + sep + "paths.xml");
91



4
        REQUIRE(headers[3]->compressSize == 154);
92



4
        REQUIRE(headers[3]->uncompressSize == 185);
93
94




6
        REQUIRE(headers[4]->fileName == "dir2" + sep + "units.xml");
95



4
        REQUIRE(headers[4]->compressSize == 202);
96



4
        REQUIRE(headers[4]->uncompressSize == 306);
97
98




6
        REQUIRE(headers[5]->fileName == "dir" + sep + "hide.png");
99



4
        REQUIRE(headers[5]->compressSize == 365);
100



4
        REQUIRE(headers[5]->uncompressSize == 368);
101
102





8
        REQUIRE(headers[6]->fileName ==
103
            "dir" + sep + "1" + sep + "test.txt");
104



4
        REQUIRE(headers[6]->compressSize == 17);
105



4
        REQUIRE(headers[6]->uncompressSize == 23);
106
107





8
        REQUIRE(headers[7]->fileName ==
108
            "dir" + sep + "1" + sep + "file1.txt");
109



4
        REQUIRE(headers[7]->compressSize == 17);
110



4
        REQUIRE(headers[7]->uncompressSize == 23);
111
112





8
        REQUIRE(headers[8]->fileName ==
113
            "dir" + sep + "gpl" + sep + "palette.gpl");
114



4
        REQUIRE(headers[8]->compressSize == 128);
115



4
        REQUIRE(headers[8]->uncompressSize == 213);
116
117




6
        REQUIRE(headers[9]->fileName ==
118
            "dir" + sep + "dye.png");
119



4
        REQUIRE(headers[9]->compressSize == 794);
120



4
        REQUIRE(headers[9]->uncompressSize == 794);
121
122



4
        REQUIRE(headers[10]->fileName == "units.xml");
123



4
        REQUIRE(headers[10]->compressSize == 202);
124



4
        REQUIRE(headers[10]->uncompressSize == 306);
125
126
1
        delete entry;
127
    }
128
129



28
    SECTION("test3.zip")
130
    {
131
2
        name = prefix + "data/test/test3.zip";
132
133
        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
134
            dirSeparator,
135


4
            VirtFs::FsZip::getFuncs());
136
1
        STD_VECTOR<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;
137
138




3
        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
139



5
        REQUIRE(headers.size() == 2);
140



4
        REQUIRE(entry->root == name);
141



4
        REQUIRE(headers[0]->fileName == "test.txt");
142



4
        REQUIRE(headers[0]->compressSize == 17);
143



4
        REQUIRE(headers[0]->uncompressSize == 23);
144



4
        REQUIRE(headers[1]->fileName == "units.xml");
145



4
        REQUIRE(headers[1]->compressSize == 202);
146



4
        REQUIRE(headers[1]->uncompressSize == 306);
147
148
1
        delete entry;
149
    }
150
151



28
    SECTION("test4.zip")
152
    {
153
2
        name = prefix + "data/test/test4.zip";
154
155
        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
156
            dirSeparator,
157


4
            VirtFs::FsZip::getFuncs());
158
1
        STD_VECTOR<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;
159
160




3
        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
161



4
        REQUIRE(entry->root == name);
162



4
        REQUIRE(headers.empty());
163
164
1
        delete entry;
165
    }
166
4
}
167
168
3
TEST_CASE("Zip readCompressedFile", "")
169
{
170
4
    std::string name("data/test/test.zip");
171
2
    std::string prefix;
172

1
    if (Files::existsLocal(name) == false)
173
        prefix = "../";
174
175



7
    SECTION("test2.zip")
176
    {
177
2
        name = prefix + "data/test/test2.zip";
178
179
        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
180
            dirSeparator,
181


4
            VirtFs::FsZip::getFuncs());
182
1
        STD_VECTOR<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;
183
184




3
        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
185



5
        REQUIRE(headers.size() == 11);
186



4
        REQUIRE(entry->root == name);
187
        // test.txt
188
1
        uint8_t *const buf = VirtFs::ZipReader::readCompressedFile(headers[0]);
189



4
        REQUIRE(buf != nullptr);
190
1
        delete [] buf;
191
1
        delete entry;
192
    }
193
1
}
194
195
5
TEST_CASE("Zip readFile", "")
196
{
197
12
    std::string name("data/test/test.zip");
198
6
    std::string prefix;
199

3
    if (Files::existsLocal(name) == false)
200
        prefix = "../";
201
202



21
    SECTION("test.zip")
203
    {
204
2
        name = prefix + "data/test/test.zip";
205
206
        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
207
            dirSeparator,
208


4
            VirtFs::FsZip::getFuncs());
209
1
        STD_VECTOR<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;
210
211




3
        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
212



5
        REQUIRE(headers.size() == 2);
213



4
        REQUIRE(entry->root == name);
214
3
        for (int f = 0; f < 2; f ++)
215
        {
216
8
            logger->log("test header: %s, %u, %u",
217
2
                headers[f]->fileName.c_str(),
218
2
                headers[f]->compressSize,
219
6
                headers[f]->uncompressSize);
220
4
            const uint8_t *const buf = VirtFs::ZipReader::readFile(headers[f]);
221



8
            REQUIRE(buf != nullptr);
222
2
            delete [] buf;
223
        }
224
1
        delete entry;
225
    }
226
227



21
    SECTION("test2.zip")
228
    {
229
2
        name = prefix + "data/test/test2.zip";
230
231
        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
232
            dirSeparator,
233


4
            VirtFs::FsZip::getFuncs());
234
1
        STD_VECTOR<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;
235
236




3
        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
237



5
        REQUIRE(headers.size() == 11);
238



4
        REQUIRE(entry->root == name);
239
        // test.txt
240
1
        const uint8_t *buf = VirtFs::ZipReader::readFile(headers[0]);
241



4
        REQUIRE(buf != nullptr);
242
        const std::string str = std::string(reinterpret_cast<const char*>(buf),
243
5
            headers[0]->uncompressSize);
244



4
        REQUIRE(str == "test line 1\ntest line 2");
245
1
        delete [] buf;
246
23
        for (int f = 0; f < 11; f ++)
247
        {
248
44
            logger->log("test header: %s, %u, %u",
249
11
                headers[f]->fileName.c_str(),
250
11
                headers[f]->compressSize,
251
33
                headers[f]->uncompressSize);
252
22
            buf = VirtFs::ZipReader::readFile(headers[f]);
253



44
            REQUIRE(buf != nullptr);
254
11
            delete [] buf;
255
        }
256
1
        delete entry;
257
    }
258
259



21
    SECTION("test3.zip")
260
    {
261
2
        name = prefix + "data/test/test3.zip";
262
263
        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
264
            dirSeparator,
265


4
            VirtFs::FsZip::getFuncs());
266
1
        STD_VECTOR<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;
267
268




3
        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
269



5
        REQUIRE(headers.size() == 2);
270



4
        REQUIRE(entry->root == name);
271
3
        for (int f = 0; f < 2; f ++)
272
        {
273
8
            logger->log("test header: %s, %u, %u",
274
2
                headers[f]->fileName.c_str(),
275
2
                headers[f]->compressSize,
276
6
                headers[f]->uncompressSize);
277
4
            const uint8_t *const buf = VirtFs::ZipReader::readFile(headers[f]);
278



8
            REQUIRE(buf != nullptr);
279
2
            delete [] buf;
280
        }
281
1
        delete entry;
282
    }
283

6
}