GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/integrity.cc Lines: 168 184 91.3 %
Date: 2017-11-29 Branches: 395 958 41.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-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 "configmanager.h"
24
#include "configuration.h"
25
#include "client.h"
26
#include "dirs.h"
27
#include "graphicsmanager.h"
28
29
#include "being/actorsprite.h"
30
31
#include "fs/files.h"
32
33
#include "fs/virtfs/fs.h"
34
#include "fs/virtfs/rwops.h"
35
36
#include "gui/userpalette.h"
37
#include "gui/theme.h"
38
39
#include "input/inputactionmap.h"
40
41
#include "resources/image/image.h"
42
43
#include "resources/loaders/imageloader.h"
44
45
#include "resources/sdlimagehelper.h"
46
#ifdef USE_SDL2
47
#include "resources/surfaceimagehelper.h"
48
#endif  // USE_SDL2
49
50
#include "resources/resourcemanager/resourcemanager.h"
51
52
#include "utils/env.h"
53
#include "utils/delete2.h"
54
#include "utils/foreach.h"
55
56
PRAGMA48(GCC diagnostic push)
57
PRAGMA48(GCC diagnostic ignored "-Wshadow")
58
#include <SDL_image.h>
59
PRAGMA48(GCC diagnostic pop)
60
61
#ifndef UNITTESTS_CATCH
62
#include <algorithm>
63
#endif  // UNITTESTS_CATCH
64
65
#include "debug.h"
66
67
namespace
68
{
69
    class InputActionSortFunctorTest final
70
    {
71
        public:
72
            bool operator() (const InputActionT key1,
73
                             const InputActionT key2) const
74
            {
75
                REQUIRE(CAST_SIZE(key1) < CAST_SIZE(InputAction::TOTAL));
76
                REQUIRE(CAST_SIZE(key2) < CAST_SIZE(InputAction::TOTAL));
77
                REQUIRE(key1 < InputAction::TOTAL);
78
                REQUIRE(key2 < InputAction::TOTAL);
79
                return keys[CAST_SIZE(key1)].priority
80
                    >= keys[CAST_SIZE(key2)].priority;
81
            }
82
83
            const InputActionData *keys A_NONNULLPOINTER;
84
    };
85
    InputActionSortFunctorTest inputActionDataSorterTest;
86
}  // namespace
87
88
4
static bool compareBuffers(const unsigned char *const buf2)
89
{
90
4
    bool isCorrect(true);
91
4
    int sz = 0;
92
    const unsigned char *buf1 = reinterpret_cast<const unsigned char*>(
93

16
        VirtFs::loadFile("hide.png", sz));
94



16
    REQUIRE(buf1 != nullptr);
95



16
    REQUIRE(sz == 368);
96
1476
    for (int f = 0; f < sz; f ++)
97
    {
98
1472
        if (buf1[f] != buf2[f])
99
        {
100
            isCorrect = false;
101
            logger->log("Wrong buffer chars: 0x%x vs 0x%x",
102
                buf1[f],
103
                buf2[f]);
104
        }
105
    }
106
4
    delete [] buf1;
107
4
    return isCorrect;
108
}
109
110
30
TEST_CASE("integrity tests", "integrity")
111
{
112
26
    setEnv("SDL_VIDEODRIVER", "dummy");
113
114
26
    client = new Client;
115
26
    XML::initXML();
116
26
    SDL_Init(SDL_INIT_VIDEO);
117
104
    std::string name("data/test/test.zip");
118
52
    std::string prefix;
119

26
    if (Files::existsLocal(name) == false)
120
26
        prefix = "../" + prefix;
121
122

104
    VirtFs::mountDirSilent("data", Append_false);
123

104
    VirtFs::mountDirSilent("../data", Append_false);
124
125
26
    Dirs::initRootDir();
126
26
    Dirs::initHomeDir();
127
128
26
    setBrandingDefaults(branding);
129
26
    ConfigManager::initConfiguration();
130
131
#ifdef USE_SDL2
132
    imageHelper = new SurfaceImageHelper;
133
134
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
135
        graphicsManager.createWindow(640, 480, 0,
136
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
137
#else  // USE_SDL2
138
139
52
    imageHelper = new SDLImageHelper();
140
141
26
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
142
#endif  // USE_SDL2
143
144

26
    userPalette = new UserPalette;
145
146

104
    config.setValue("fontSize", 16);
147

26
    theme = new Theme;
148
26
    Theme::selectSkin();
149
150
26
    ActorSprite::load();
151
26
    const char *const name1 = "dir/hide.png";
152
26
    const int size1 = 368;
153
154



182
    SECTION("integrity test 1")
155
    {
156



8
        REQUIRE(sizeof(inputActionData) / sizeof(InputActionData) ==
157
            CAST_SIZE(InputAction::TOTAL));
158
    }
159
160



182
    SECTION("integrity test 2")
161
    {
162
4
        KeyToActionMap actionMap;
163
2
        const size_t sz = CAST_SIZE(InputAction::TOTAL);
164
1338
        for (size_t i = 0; i < sz; i ++)
165
        {
166
1336
            InputActionT val = static_cast<InputActionT>(i);
167



5344
            REQUIRE(val < InputAction::TOTAL);
168



5344
            REQUIRE(val > InputAction::NO_VALUE);
169



5344
            REQUIRE(val > InputAction::UNDEFINED_VALUE);
170



5344
            REQUIRE(CAST_SIZE(val) < CAST_SIZE(InputAction::TOTAL));
171



5344
            REQUIRE(CAST_SIZE(val) < CAST_SIZE(InputAction::NO_VALUE));
172



5344
            REQUIRE(CAST_SIZE(val) < CAST_SIZE(InputAction::UNDEFINED_VALUE));
173



5344
            REQUIRE(CAST_S32(val) < CAST_S32(InputAction::TOTAL));
174



5344
            REQUIRE(CAST_S32(val) > CAST_S32(InputAction::NO_VALUE));
175



5344
            REQUIRE(CAST_S32(val) > CAST_S32(InputAction::UNDEFINED_VALUE));
176
        }
177
    }
178
179



182
    SECTION("integrity test 3")
180
    {
181
4
        KeyToActionMap actionMap;
182
2
        int cnt = 0;
183
2
        const size_t sz = CAST_SIZE(InputAction::TOTAL);
184
1338
        for (size_t i = 0; i < sz; i ++)
185
        {
186
2672
            actionMap[cnt++].push_back(static_cast<InputActionT>(i));
187
1336
            if (cnt > 3)
188
334
                cnt = 0;
189
        }
190
4
        FOR_EACH (KeyToActionMapIter, it, actionMap)
191
        {
192
8
            KeysVector *const keys = &it->second;
193
1360
            FOR_EACHP (KeysVectorIter, itk, keys)
194
            {
195
1336
                const size_t val = CAST_SIZE(*itk);
196



5344
                REQUIRE(val < sz);
197
            }
198
        }
199
    }
200
201



182
    SECTION("integrity test 4")
202
    {
203
4
        KeyToActionMap actionMap;
204
4
        KeyTimeMap keyTimeMap;
205
2
        actionMap.clear();
206
2
        keyTimeMap.clear();
207
208
1338
        for (size_t i = 0; i < CAST_SIZE(InputAction::TOTAL); i ++)
209
        {
210
4008
            actionMap[10].push_back(
211
2672
                static_cast<InputActionT>(i));
212
        }
213
214
2
        KeysVector *const keys = &actionMap[0];
215
2
        inputActionDataSorterTest.keys = &inputActionData[0];
216
6
        std::sort(keys->begin(), keys->end(), inputActionDataSorterTest);
217
    }
218
219



182
    SECTION("integrity test 5")
220
    {
221
4
        KeyToActionMap mKeyToAction;
222
4
        KeyToIdMap mKeyToId;
223
4
        KeyTimeMap mKeyTimeMap;
224
2
        inputManager.updateKeyActionMap(mKeyToAction, mKeyToId,
225
            mKeyTimeMap, InputType::KEYBOARD);
226
    }
227
228



182
    SECTION("integrity Loader::getImage test 1")
229
    {
230

8
        Image *const image = Loader::getImage(
231
2
            "graphics/images/login_wallpaper.png");
232



8
        REQUIRE(image != nullptr);
233



10
        REQUIRE(image->getSDLSurface() != nullptr);
234
2
        image->decRef();
235
    }
236
237



182
    SECTION("integrity Loader::getImage test 2")
238
    {
239

4
        VirtFs::mountZip(prefix + "data/test/test.zip", Append_false);
240

8
        Image *const image = Loader::getImage(
241
2
            "dir/hide.png");
242

4
        VirtFs::unmountZip(prefix + "data/test/test.zip");
243



8
        REQUIRE(image != nullptr);
244



10
        REQUIRE(image->getSDLSurface() != nullptr);
245
2
        image->decRef();
246
    }
247
248



182
    SECTION("integrity Loader::getImage test 3")
249
    {
250

4
        VirtFs::mountZip(prefix + "data/test/test.zip", Append_false);
251

8
        Image *const image = Loader::getImage(
252
2
            "dir/brimmedhat.png");
253

4
        VirtFs::unmountZip(prefix + "data/test/test.zip");
254



8
        REQUIRE(image != nullptr);
255



10
        REQUIRE(image->getSDLSurface() != nullptr);
256
2
        image->decRef();
257
    }
258
259



182
    SECTION("integrity Loader::getImage test 4")
260
    {
261

4
        VirtFs::mountZip(prefix + "data/test/test.zip", Append_false);
262
263

8
        SDL_RWops *const rw = VirtFs::rwopsOpenRead(name1);
264



8
        REQUIRE(rw != nullptr);
265
        unsigned char buf[size1];
266
2
        const size_t sz = SDL_RWread(rw, buf, 1, size1);
267
2
        if (sz != size1)
268
            SDL_RWclose(rw);
269



8
        REQUIRE(sz == size1);
270
2
        SDL_RWclose(rw);
271

4
        VirtFs::unmountZip(prefix + "data/test/test.zip");
272

4
        VirtFs::mountDirSilent(prefix + "data/test", Append_true);
273




6
        REQUIRE(compareBuffers(buf));
274

4
        VirtFs::unmountDirSilent(prefix + "data/test");
275
    }
276
277



182
    SECTION("integrity Loader::getImage test 7")
278
    {
279

4
        VirtFs::mountZip(prefix + "data/test/test.zip", Append_false);
280
281

8
        SDL_RWops *const rw = VirtFs::rwopsOpenRead(name1);
282



8
        REQUIRE(rw != nullptr);
283
2
        int64_t seek = SDL_RWseek(rw, 0, RW_SEEK_END);
284
2
        if (seek == -1)
285
            SDL_RWclose(rw);
286



8
        REQUIRE(seek != -1);
287
2
        const int64_t pos = SDL_RWtell(rw);
288
2
        if (pos != size1)
289
            SDL_RWclose(rw);
290



8
        REQUIRE(pos == size1);
291
292
2
        seek = SDL_RWseek(rw, 0, RW_SEEK_SET);
293
2
        if (seek == -1)
294
            SDL_RWclose(rw);
295



8
        REQUIRE(seek != -1);
296
297
        unsigned char buf[size1];
298
2
        const size_t sz = SDL_RWread(rw, buf, 1, pos);
299
2
        if (sz != size1)
300
            SDL_RWclose(rw);
301



8
        REQUIRE(sz == size1);
302
303
2
        SDL_RWclose(rw);
304

4
        VirtFs::unmountZip(prefix + "data/test/test.zip");
305

8
        VirtFs::mountDirSilent("data/test", Append_true);
306

8
        VirtFs::mountDirSilent("../data/test", Append_true);
307




6
        REQUIRE(compareBuffers(buf));
308

8
        VirtFs::unmountDirSilent("data/test");
309

8
        VirtFs::unmountDirSilent("../data/test");
310
    }
311
312



182
    SECTION("integrity Loader::getImage test 8")
313
    {
314

4
        VirtFs::mountZip(prefix + "data/test/test.zip", Append_false);
315
316

8
        SDL_RWops *const rw = VirtFs::rwopsOpenRead(name1);
317



8
        REQUIRE(rw != nullptr);
318

2
        if (IMG_isPNG(rw) == 0)
319
        {
320
            SDL_RWclose(rw);
321
            REQUIRE(false);
322
        }
323
2
        SDL_Surface *const tmpImage = IMG_LoadPNG_RW(rw);
324
2
        SDL_RWclose(rw);
325

4
        VirtFs::unmountZip(prefix + "data/test/test.zip");
326



8
        REQUIRE(tmpImage != nullptr);
327
2
        SDL_FreeSurface(tmpImage);
328
    }
329
330



182
    SECTION("integrity Loader::getImage test 9")
331
    {
332

4
        VirtFs::mountZip(prefix + "data/test/test.zip", Append_false);
333
334

8
        SDL_RWops *const rw = VirtFs::rwopsOpenRead(name1);
335



8
        REQUIRE(rw != nullptr);
336
2
        Resource *const res = imageHelper->load(rw);
337

4
        VirtFs::unmountZip(prefix + "data/test/test.zip");
338



8
        REQUIRE(res != nullptr);
339
2
        delete res;
340
    }
341
342



182
    SECTION("integrity Loader::getImage test 10")
343
    {
344

4
        VirtFs::mountZip(prefix + "data/test/test.zip", Append_false);
345

8
        Image *const image = Loader::getImage(
346
2
            name1);
347

4
        VirtFs::unmountZip(prefix + "data/test/test.zip");
348



8
        REQUIRE(image != nullptr);
349



10
        REQUIRE(image->getSDLSurface() != nullptr);
350
2
        image->decRef();
351
    }
352
353
26
    ResourceManager::cleanOrphans(true);
354
355
26
    delete2(userPalette);
356
26
    delete2(client);
357
358

104
    VirtFs::unmountDirSilent("data");
359

104
    VirtFs::unmountDirSilent("../data");
360

32
}