summaryrefslogtreecommitdiff
path: root/src/fs/virtfs/zip_unittest.cc
blob: 52629d482e12c9852f933c6ad70f00b093a4ef9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 *  The ManaPlus Client
 *  Copyright (C) 2017  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "test/unittests.h"

#include "logger.h"

#include "fs/files.h"

#include "fs/virtfs/fszip.h"
#include "fs/virtfs/zipentry.h"
#include "fs/virtfs/zipreader.h"
#include "fs/virtfs/ziplocalheader.h"

#include "utils/delete2.h"

#include "debug.h"

extern const char *dirSeparator;

TEST_CASE("Zip readArchiveInfo", "")
{
    logger = new Logger();
    std::string name("data/test/test.zip");
    std::string prefix;
    const std::string sep = dirSeparator;
    if (Files::existsLocal(name) == false)
        prefix = "../";

    SECTION("test.zip")
    {
        name = prefix + "data/test/test.zip";

        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
            dirSeparator,
            VirtFs::FsZip::getFuncs());
        std::vector<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;

        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
        REQUIRE(headers.size() == 2);
        REQUIRE(entry->root == name);
        REQUIRE(headers[0]->fileName == "dir" + sep + "hide.png");
        REQUIRE(headers[0]->compressSize == 365);
        REQUIRE(headers[0]->uncompressSize == 368);
        REQUIRE(headers[1]->fileName == "dir" + sep + "brimmedhat.png");
        REQUIRE(headers[1]->compressSize == 1959);
        REQUIRE(headers[1]->uncompressSize == 1959);

        delete entry;
    }

    SECTION("test2.zip")
    {
        name = prefix + "data/test/test2.zip";

        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
            dirSeparator,
            VirtFs::FsZip::getFuncs());
        std::vector<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;

        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
        REQUIRE(headers.size() == 11);
        REQUIRE(entry->root == name);
        REQUIRE(headers[0]->fileName == "test.txt");
        REQUIRE(headers[0]->compressSize == 17);
        REQUIRE(headers[0]->uncompressSize == 23);

        REQUIRE(headers[1]->fileName == "dir2" + sep + "hide.png");
        REQUIRE(headers[1]->compressSize == 365);
        REQUIRE(headers[1]->uncompressSize == 368);

        REQUIRE(headers[2]->fileName == "dir2" + sep + "test.txt");
        REQUIRE(headers[2]->compressSize == 17);
        REQUIRE(headers[2]->uncompressSize == 23);

        REQUIRE(headers[3]->fileName == "dir2" + sep + "paths.xml");
        REQUIRE(headers[3]->compressSize == 154);
        REQUIRE(headers[3]->uncompressSize == 185);

        REQUIRE(headers[4]->fileName == "dir2" + sep + "units.xml");
        REQUIRE(headers[4]->compressSize == 202);
        REQUIRE(headers[4]->uncompressSize == 306);

        REQUIRE(headers[5]->fileName == "dir" + sep + "hide.png");
        REQUIRE(headers[5]->compressSize == 365);
        REQUIRE(headers[5]->uncompressSize == 368);

        REQUIRE(headers[6]->fileName ==
            "dir" + sep + "1" + sep + "test.txt");
        REQUIRE(headers[6]->compressSize == 17);
        REQUIRE(headers[6]->uncompressSize == 23);

        REQUIRE(headers[7]->fileName ==
            "dir" + sep + "1" + sep + "file1.txt");
        REQUIRE(headers[7]->compressSize == 17);
        REQUIRE(headers[7]->uncompressSize == 23);

        REQUIRE(headers[8]->fileName ==
            "dir" + sep + "gpl" + sep + "palette.gpl");
        REQUIRE(headers[8]->compressSize == 128);
        REQUIRE(headers[8]->uncompressSize == 213);

        REQUIRE(headers[9]->fileName ==
            "dir" + sep + "dye.png");
        REQUIRE(headers[9]->compressSize == 794);
        REQUIRE(headers[9]->uncompressSize == 794);

        REQUIRE(headers[10]->fileName == "units.xml");
        REQUIRE(headers[10]->compressSize == 202);
        REQUIRE(headers[10]->uncompressSize == 306);

        delete entry;
    }

    SECTION("test3.zip")
    {
        name = prefix + "data/test/test3.zip";

        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
            dirSeparator,
            VirtFs::FsZip::getFuncs());
        std::vector<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;

        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
        REQUIRE(headers.size() == 2);
        REQUIRE(entry->root == name);
        REQUIRE(headers[0]->fileName == "test.txt");
        REQUIRE(headers[0]->compressSize == 17);
        REQUIRE(headers[0]->uncompressSize == 23);
        REQUIRE(headers[1]->fileName == "units.xml");
        REQUIRE(headers[1]->compressSize == 202);
        REQUIRE(headers[1]->uncompressSize == 306);

        delete entry;
    }

    SECTION("test4.zip")
    {
        name = prefix + "data/test/test4.zip";

        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
            dirSeparator,
            VirtFs::FsZip::getFuncs());
        std::vector<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;

        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
        REQUIRE(entry->root == name);
        REQUIRE(headers.size() == 0);

        delete entry;
    }

    delete2(logger);
}

TEST_CASE("Zip readCompressedFile", "")
{
    logger = new Logger();
    std::string name("data/test/test.zip");
    std::string prefix;
    if (Files::existsLocal(name) == false)
        prefix = "../";

    SECTION("empty")
    {
        REQUIRE_THROWS(VirtFs::ZipReader::readCompressedFile(nullptr));
    }

    SECTION("test2.zip")
    {
        name = prefix + "data/test/test2.zip";

        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
            dirSeparator,
            VirtFs::FsZip::getFuncs());
        std::vector<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;

        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
        REQUIRE(headers.size() == 11);
        REQUIRE(entry->root == name);
        // test.txt
        uint8_t *const buf = VirtFs::ZipReader::readCompressedFile(headers[0]);
        REQUIRE(buf != nullptr);
        delete [] buf;
        delete entry;
    }

    delete2(logger);
}

TEST_CASE("Zip readFile", "")
{
    logger = new Logger();
    std::string name("data/test/test.zip");
    std::string prefix;
    if (Files::existsLocal(name) == false)
        prefix = "../";

    SECTION("empty")
    {
        REQUIRE_THROWS(VirtFs::ZipReader::readFile(nullptr));
    }

    SECTION("test.zip")
    {
        name = prefix + "data/test/test.zip";

        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
            dirSeparator,
            VirtFs::FsZip::getFuncs());
        std::vector<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;

        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
        REQUIRE(headers.size() == 2);
        REQUIRE(entry->root == name);
        for (int f = 0; f < 2; f ++)
        {
            logger->log("test header: %s, %u, %u",
                headers[f]->fileName.c_str(),
                headers[f]->compressSize,
                headers[f]->uncompressSize);
            const uint8_t *const buf = VirtFs::ZipReader::readFile(headers[f]);
            REQUIRE(buf != nullptr);
            delete [] buf;
        }
        delete entry;
    }

    SECTION("test2.zip")
    {
        name = prefix + "data/test/test2.zip";

        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
            dirSeparator,
            VirtFs::FsZip::getFuncs());
        std::vector<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;

        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
        REQUIRE(headers.size() == 11);
        REQUIRE(entry->root == name);
        // test.txt
        const uint8_t *buf = VirtFs::ZipReader::readFile(headers[0]);
        REQUIRE(buf != nullptr);
        const std::string str = std::string(reinterpret_cast<const char*>(buf),
            headers[0]->uncompressSize);
        REQUIRE(str == "test line 1\ntest line 2");
        delete [] buf;
        for (int f = 0; f < 11; f ++)
        {
            logger->log("test header: %s, %u, %u",
                headers[f]->fileName.c_str(),
                headers[f]->compressSize,
                headers[f]->uncompressSize);
            buf = VirtFs::ZipReader::readFile(headers[f]);
            REQUIRE(buf != nullptr);
            delete [] buf;
        }
        delete entry;
    }

    SECTION("test3.zip")
    {
        name = prefix + "data/test/test3.zip";

        VirtFs::ZipEntry *const entry = new VirtFs::ZipEntry(name,
            dirSeparator,
            VirtFs::FsZip::getFuncs());
        std::vector<VirtFs::ZipLocalHeader*> &headers = entry->mHeaders;

        REQUIRE(VirtFs::ZipReader::readArchiveInfo(entry));
        REQUIRE(headers.size() == 2);
        REQUIRE(entry->root == name);
        for (int f = 0; f < 2; f ++)
        {
            logger->log("test header: %s, %u, %u",
                headers[f]->fileName.c_str(),
                headers[f]->compressSize,
                headers[f]->uncompressSize);
            const uint8_t *const buf = VirtFs::ZipReader::readFile(headers[f]);
            REQUIRE(buf != nullptr);
            delete [] buf;
        }
        delete entry;
    }

    delete2(logger);
}