GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/fs/virtfs/zip.cc Lines: 157 157 100.0 %
Date: 2017-11-29 Branches: 540 1354 39.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 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 "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
12
TEST_CASE("Zip readArchiveInfo", "")
37
{
38
32
    std::string name("data/test/test.zip");
39
16
    std::string prefix;
40
32
    const std::string sep = dirSeparator;
41

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



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


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




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



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



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




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



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



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




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



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



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



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


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




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



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



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



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



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



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




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



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



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




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



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



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




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



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



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




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



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



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




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



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



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





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



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



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





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



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



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





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



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



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




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



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



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



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



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



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



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


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




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



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



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



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



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



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



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



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



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



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


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




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



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



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

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



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


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




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



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



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



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

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



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


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




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



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



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



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



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


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




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



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



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



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



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



88
            REQUIRE(buf != nullptr);
254
22
            delete [] buf;
255
        }
256
2
        delete entry;
257
    }
258
259



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


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




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



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



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



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

12
}