GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/integrity.cc Lines: 168 185 90.8 %
Date: 2018-07-14 Branches: 393 956 41.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-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 "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
2
static bool compareBuffers(const unsigned char *const buf2)
89
{
90
2
    bool isCorrect(true);
91
2
    int sz = 0;
92
    const unsigned char *buf1 = reinterpret_cast<const unsigned char*>(
93
8
        VirtFs::loadFile("hide.png", sz));
94



8
    REQUIRE(buf1 != nullptr);
95



8
    REQUIRE(sz == 368);
96
738
    for (int f = 0; f < sz; f ++)
97
    {
98
736
        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
2
    delete [] buf1;
107
2
    return isCorrect;
108
}
109
110
15
TEST_CASE("integrity tests", "integrity")
111
{
112
13
    setEnv("SDL_VIDEODRIVER", "dummy");
113
114
13
    client = new Client;
115
13
    XML::initXML();
116
13
    SDL_Init(SDL_INIT_VIDEO);
117
52
    std::string name("data/test/test.zip");
118
26
    std::string prefix;
119

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

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

52
    VirtFs::mountDirSilent("../data", Append_false);
124
125
13
    Dirs::initRootDir();
126
13
    Dirs::initHomeDir();
127
128
13
    setBrandingDefaults(branding);
129
13
    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
26
    imageHelper = new SDLImageHelper();
140
141
13
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
142
#endif  // USE_SDL2
143
144

13
    userPalette = new UserPalette;
145
146

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

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



4
        REQUIRE(image != nullptr);
233



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



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

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

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

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



4
        REQUIRE(image != nullptr);
244



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



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

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

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

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



4
        REQUIRE(image != nullptr);
255



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



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

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

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



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



4
        REQUIRE(sz == size1);
270
1
        SDL_RWclose(rw);
271

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

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




3
        REQUIRE(compareBuffers(buf));
274

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



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

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

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



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



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



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



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



4
        REQUIRE(sz == size1);
302
303
1
        SDL_RWclose(rw);
304

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

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

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




3
        REQUIRE(compareBuffers(buf));
308

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

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



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

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

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



4
        REQUIRE(rw != nullptr);
318

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

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



4
        REQUIRE(tmpImage != nullptr);
327
1
        SDL_FreeSurface(tmpImage);
328
    }
329
330



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

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

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



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

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



4
        REQUIRE(res != nullptr);
339
1
        delete res;
340
    }
341
342



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

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

4
        Image *const image = Loader::getImage(
346
1
            name1);
347

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



4
        REQUIRE(image != nullptr);
349



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

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

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

16
}