test_map_settings_manager.cpp 7.8 KB

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