]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_map_settings_manager.cpp
Remove an unused method and header includes
[minetest.git] / src / unittest / test_map_settings_manager.cpp
1  /*
2 Minetest
3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "test.h"
21
22 #include "noise.h"
23 #include "settings.h"
24 #include "mapgen/mapgen_v5.h"
25 #include "util/sha1.h"
26 #include "map_settings_manager.h"
27
28 class TestMapSettingsManager : public TestBase {
29 public:
30         TestMapSettingsManager() { TestManager::registerTestModule(this); }
31         const char *getName() { return "TestMapSettingsManager"; }
32
33         void makeUserConfig();
34         std::string makeMetaFile(bool make_corrupt);
35
36         void runTests(IGameDef *gamedef);
37
38         void testMapSettingsManager();
39         void testMapMetaSaveLoad();
40         void testMapMetaFailures();
41 };
42
43 static TestMapSettingsManager g_test_instance;
44
45 void TestMapSettingsManager::runTests(IGameDef *gamedef)
46 {
47         TEST(testMapSettingsManager);
48         TEST(testMapMetaSaveLoad);
49         TEST(testMapMetaFailures);
50 }
51
52 ////////////////////////////////////////////////////////////////////////////////
53
54
55 void check_noise_params(const NoiseParams *np1, const NoiseParams *np2)
56 {
57         UASSERTEQ(float, np1->offset, np2->offset);
58         UASSERTEQ(float, np1->scale, np2->scale);
59         UASSERT(np1->spread == np2->spread);
60         UASSERTEQ(s32, np1->seed, np2->seed);
61         UASSERTEQ(u16, np1->octaves, np2->octaves);
62         UASSERTEQ(float, np1->persist, np2->persist);
63         UASSERTEQ(float, np1->lacunarity, np2->lacunarity);
64         UASSERTEQ(u32, np1->flags, np2->flags);
65 }
66
67
68 void TestMapSettingsManager::makeUserConfig()
69 {
70         delete Settings::getLayer(SL_GLOBAL);
71         Settings *conf = Settings::createLayer(SL_GLOBAL);
72
73         conf->set("mg_name", "v7");
74         conf->set("seed", "5678");
75         conf->set("water_level", "20");
76         conf->set("mgv5_np_factor", "0, 12,  (500, 250, 500), 920382, 5, 0.45, 3.0");
77         conf->set("mgv5_np_height", "0, 15, (500, 250, 500), 841746,  5, 0.5,  3.0");
78         conf->set("mgv5_np_filler_depth", "20, 1, (150, 150, 150), 261, 4, 0.7,  1.0");
79         conf->set("mgv5_np_ground", "-43, 40, (80,  80,  80),  983240, 4, 0.55, 2.0");
80 }
81
82
83 std::string TestMapSettingsManager::makeMetaFile(bool make_corrupt)
84 {
85         std::string metafile = getTestTempFile();
86
87         const char *metafile_contents =
88                 "mg_name = v5\n"
89                 "seed = 1234\n"
90                 "mg_flags = light\n"
91                 "mgv5_np_filler_depth = 20, 1, (150, 150, 150), 261, 4, 0.7,  1.0\n"
92                 "mgv5_np_height = 20, 10, (250, 250, 250), 84174,  4, 0.5,  1.0\n";
93
94         FILE *f = fopen(metafile.c_str(), "wb");
95         UASSERT(f != NULL);
96
97         fputs(metafile_contents, f);
98         if (!make_corrupt)
99                 fputs("[end_of_params]\n", f);
100
101         fclose(f);
102
103         return metafile;
104 }
105
106
107 void TestMapSettingsManager::testMapSettingsManager()
108 {
109         makeUserConfig();
110
111         std::string test_mapmeta_path = makeMetaFile(false);
112
113         MapSettingsManager mgr(test_mapmeta_path);
114         std::string value;
115
116         UASSERT(mgr.getMapSetting("mg_name", &value));
117         UASSERT(value == "v7");
118
119         // Pretend we're initializing the ServerMap
120         UASSERT(mgr.loadMapMeta());
121
122         // Pretend some scripts are requesting mapgen params
123         UASSERT(mgr.getMapSetting("mg_name", &value));
124         UASSERT(value == "v5");
125         UASSERT(mgr.getMapSetting("seed", &value));
126         UASSERT(value == "1234");
127         UASSERT(mgr.getMapSetting("water_level", &value));
128         UASSERT(value == "20");
129
130     // Pretend we have some mapgen settings configured from the scripting
131         UASSERT(mgr.setMapSetting("water_level", "15"));
132         UASSERT(mgr.setMapSetting("seed", "02468"));
133         UASSERT(mgr.setMapSetting("mg_flags", "nolight", true));
134
135         NoiseParams script_np_filler_depth(0, 100, v3f(200, 100, 200), 261, 4, 0.7, 2.0);
136         NoiseParams script_np_factor(0, 100, v3f(50, 50, 50), 920381, 3, 0.45, 2.0);
137         NoiseParams script_np_height(0, 100, v3f(450, 450, 450), 84174, 4, 0.5, 2.0);
138         NoiseParams meta_np_height(20, 10, v3f(250, 250, 250), 84174,  4, 0.5,  1.0);
139         NoiseParams user_np_ground(-43, 40, v3f(80,  80,  80),  983240, 4, 0.55, 2.0, NOISE_FLAG_EASED);
140
141         mgr.setMapSettingNoiseParams("mgv5_np_filler_depth", &script_np_filler_depth, true);
142         mgr.setMapSettingNoiseParams("mgv5_np_height", &script_np_height);
143         mgr.setMapSettingNoiseParams("mgv5_np_factor", &script_np_factor);
144
145         {
146                 NoiseParams dummy;
147                 mgr.getMapSettingNoiseParams("mgv5_np_factor", &dummy);
148                 check_noise_params(&dummy, &script_np_factor);
149         }
150
151         // The settings manager MUST leave user settings alone
152         mgr.setMapSetting("testname", "1");
153         mgr.setMapSetting("testname", "1", true);
154         UASSERT(!Settings::getLayer(SL_GLOBAL)->exists("testname"));
155
156         // Now make our Params and see if the values are correctly sourced
157         MapgenParams *params = mgr.makeMapgenParams();
158         UASSERT(params->mgtype == MAPGEN_V5);
159         UASSERT(params->chunksize == 5);
160         UASSERT(params->water_level == 15);
161         UASSERT(params->seed == 1234);
162         UASSERT((params->flags & MG_LIGHT) == 0);
163
164         MapgenV5Params *v5params = (MapgenV5Params *)params;
165
166         check_noise_params(&v5params->np_filler_depth, &script_np_filler_depth);
167         check_noise_params(&v5params->np_factor, &script_np_factor);
168         check_noise_params(&v5params->np_height, &meta_np_height);
169         check_noise_params(&v5params->np_ground, &user_np_ground);
170
171         UASSERT(mgr.setMapSetting("foobar", "25") == false);
172
173         // Pretend the ServerMap is shutting down
174         UASSERT(mgr.saveMapMeta());
175
176         // Make sure our interface expectations are met
177         UASSERT(mgr.mapgen_params == params);
178         UASSERT(mgr.makeMapgenParams() == params);
179
180 #if 0
181         // TODO(paramat or hmmmm): change this to compare the result against a static file
182
183         // Load the resulting map_meta.txt and make sure it contains what we expect
184         unsigned char expected_contents_hash[20] = {
185                 0x48, 0x3f, 0x88, 0x5a, 0xc0, 0x7a, 0x14, 0x48, 0xa4, 0x71,
186                 0x78, 0x56, 0x95, 0x2d, 0xdc, 0x6a, 0xf7, 0x61, 0x36, 0x5f
187         };
188
189         SHA1 ctx;
190         std::string metafile_contents;
191         UASSERT(fs::ReadFile(test_mapmeta_path, metafile_contents));
192         ctx.addBytes(&metafile_contents[0], metafile_contents.size());
193         unsigned char *sha1_result = ctx.getDigest();
194         int resultdiff = memcmp(sha1_result, expected_contents_hash, 20);
195         free(sha1_result);
196
197         UASSERT(!resultdiff);
198 #endif
199 }
200
201
202 void TestMapSettingsManager::testMapMetaSaveLoad()
203 {
204         std::string path = getTestTempDirectory()
205                 + DIR_DELIM + "foobar" + DIR_DELIM + "map_meta.txt";
206
207         makeUserConfig();
208         Settings &conf = *Settings::getLayer(SL_GLOBAL);
209
210         // There cannot be two MapSettingsManager
211         // copy the mapgen params to compare them
212         MapgenParams params1, params2;
213         // Create a set of mapgen params and save them to map meta
214         {
215                 conf.set("seed", "12345");
216                 conf.set("water_level", "5");
217                 MapSettingsManager mgr(path);
218                 MapgenParams *params = mgr.makeMapgenParams();
219                 UASSERT(params);
220                 params1 = *params;
221                 params1.bparams = nullptr; // No double-free
222                 UASSERT(mgr.saveMapMeta());
223         }
224
225         // Now try loading the map meta to mapgen params
226         {
227                 conf.set("seed", "67890");
228                 conf.set("water_level", "32");
229                 MapSettingsManager mgr(path);
230                 UASSERT(mgr.loadMapMeta());
231                 MapgenParams *params = mgr.makeMapgenParams();
232                 UASSERT(params);
233                 params2 = *params;
234                 params2.bparams = nullptr; // No double-free
235         }
236
237         // Check that both results are correct
238         UASSERTEQ(u64, params1.seed, 12345);
239         UASSERTEQ(s16, params1.water_level, 5);
240         UASSERTEQ(u64, params2.seed, 12345);
241         UASSERTEQ(s16, params2.water_level, 5);
242 }
243
244
245 void TestMapSettingsManager::testMapMetaFailures()
246 {
247         std::string test_mapmeta_path;
248
249         // Check to see if it'll fail on a non-existent map meta file
250         {
251                 test_mapmeta_path = "woobawooba/fgdfg/map_meta.txt";
252                 UASSERT(!fs::PathExists(test_mapmeta_path));
253
254                 MapSettingsManager mgr1(test_mapmeta_path);
255                 UASSERT(!mgr1.loadMapMeta());
256         }
257
258         // Check to see if it'll fail on a corrupt map meta file
259         {
260                 test_mapmeta_path = makeMetaFile(true);
261                 UASSERT(fs::PathExists(test_mapmeta_path));
262
263                 MapSettingsManager mgr2(test_mapmeta_path);
264                 UASSERT(!mgr2.loadMapMeta());
265         }
266 }