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

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

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



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


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




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



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



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




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



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



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




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



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



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



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


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




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



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



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



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



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



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




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



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



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




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



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



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




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



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



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




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



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



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




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



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



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





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



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



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





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



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



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





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



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



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




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



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



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



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



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



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



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


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




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



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



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



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



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



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



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



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



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



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


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




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



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



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

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



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


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




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



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



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



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

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



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


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




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



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



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



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



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


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




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



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



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



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



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



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



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


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




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



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



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



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

6
}