InstallLogic.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears 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 General Public License for more details.
  14. */
  15. #define BOOST_TEST_MODULE VGearsInstallerLogic
  16. #include <boost/test/unit_test.hpp>
  17. #include <boost/filesystem.hpp>
  18. #include <algorithm>
  19. #include <fstream>
  20. #include "installer/cli/InstallCommandLine.h"
  21. #include "installer/steps/InstallLayout.h"
  22. #include "installer/steps/InstallLog.h"
  23. #include "installer/steps/InstallOptions.h"
  24. #include "installer/steps/InstallVerifier.h"
  25. #include "common/InstallStamp.h"
  26. #include "common/OgrePlugins.h"
  27. #include "installer/steps/TaskPool.h"
  28. #include "installer/steps/VideoFiles.h"
  29. #include <atomic>
  30. #include <chrono>
  31. #include <set>
  32. namespace fs = boost::filesystem;
  33. namespace{
  34. bool Contains(const std::vector<std::string>& list, const std::string& value){
  35. return std::find(list.begin(), list.end(), value) != list.end();
  36. }
  37. bool StartsWith(const std::string& text, const std::string& prefix){return text.compare(0, prefix.size(), prefix) == 0;}
  38. /**
  39. * A temporary directory that is removed on destruction.
  40. */
  41. struct TempDir{
  42. fs::path path;
  43. TempDir(): path(fs::temp_directory_path() / fs::unique_path("vgears-test-%%%%-%%%%")){
  44. fs::create_directories(path);
  45. }
  46. ~TempDir(){
  47. boost::system::error_code ec;
  48. fs::remove_all(path, ec);
  49. }
  50. std::string Root() const{return path.string() + "/";}
  51. void Touch(const std::string& relative) const{
  52. fs::create_directories((path / relative).parent_path());
  53. std::ofstream(( path / relative).string()) << "x";
  54. }
  55. };
  56. bool HasProblem(const VerifyResult& result, const std::string& group){
  57. for (const VerifyProblem& problem : result.problems) if (problem.group == group) return true;
  58. return false;
  59. }
  60. }
  61. // ---------------------------------------------------------------------------------------------------------------
  62. // Layout: where things go.
  63. // ---------------------------------------------------------------------------------------------------------------
  64. BOOST_AUTO_TEST_SUITE(InstallLayoutTests)
  65. BOOST_AUTO_TEST_CASE(GameAndTempAreInsideTheInstallationDirectory){
  66. BOOST_CHECK_EQUAL(InstallLayout::GameDir("/home/u/.v-gears/"), "/home/u/.v-gears/game/");
  67. BOOST_CHECK_EQUAL(InstallLayout::TempDir("/home/u/.v-gears/"), "/home/u/.v-gears/temp/");
  68. }
  69. BOOST_AUTO_TEST_CASE(GameSubdirsAreRelativeAndUnique){
  70. const auto& dirs = InstallLayout::GameSubdirs();
  71. for (const std::string& dir : dirs){
  72. BOOST_CHECK_MESSAGE(!dir.empty() && dir[0] != '/', "not relative: " << dir);
  73. BOOST_CHECK_MESSAGE(!StartsWith(dir, "game/") && !StartsWith(dir, "temp"), "wrong root: " << dir);
  74. BOOST_CHECK_MESSAGE(std::count(dirs.begin(), dirs.end(), dir) == 1, "duplicated: " << dir);
  75. }
  76. }
  77. BOOST_AUTO_TEST_CASE(EveryDirectoryTheGameReadsIsCreated){
  78. // The game reads "./engine/<x>", which is shipped with V-Gears, and "./game/<x>", which the installer must create.
  79. for (const std::string& resource : InstallLayout::GameResourceDirs()){
  80. if (StartsWith(resource, "engine/")){
  81. BOOST_CHECK_MESSAGE(
  82. fs::is_directory(fs::path(VGEARS_SOURCE_DIR) / "data" / resource), "not shipped: " << resource
  83. );
  84. }
  85. else{
  86. BOOST_REQUIRE_MESSAGE(StartsWith(resource, "game/"), "unexpected root: " << resource);
  87. const std::string dir = resource.substr(5);
  88. bool created = false;
  89. for (const std::string& sub : InstallLayout::GameSubdirs())
  90. created = created || sub == dir || StartsWith(sub, dir + "/");
  91. BOOST_CHECK_MESSAGE(created, "the installer doesn't create " << resource);
  92. }
  93. }
  94. }
  95. BOOST_AUTO_TEST_CASE(OldSettingsAreMigrated){
  96. // The data directory used to be <installation directory>/data.
  97. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("/home/u/.v-gears/data", "", "/def"), "/home/u/.v-gears");
  98. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("/home/u/.v-gears/data/", "/other", "/def"), "/home/u/.v-gears");
  99. // A data directory that isn't called "data" is kept as is.
  100. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("/games/ff7", "", "/def"), "/games/ff7");
  101. // Without a data directory the configuration directory is used, and then the default.
  102. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("", "/home/u/.v-gears", "/def"), "/home/u/.v-gears");
  103. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("", "", "/def"), "/def");
  104. }
  105. BOOST_AUTO_TEST_SUITE_END()
  106. // ---------------------------------------------------------------------------------------------------------------
  107. // Options: dependencies, presets, estimates.
  108. // ---------------------------------------------------------------------------------------------------------------
  109. BOOST_AUTO_TEST_SUITE(InstallOptionsTests)
  110. BOOST_AUTO_TEST_CASE(ModelsNeedTheirMaps){
  111. InstallOptions o;
  112. o.field_models = true;
  113. o.wm_models = true;
  114. InstallRules::ApplyDependencies(o);
  115. BOOST_CHECK(!o.field_models);
  116. BOOST_CHECK(!o.wm_models);
  117. o.fields = o.wm = o.field_models = o.wm_models = true;
  118. InstallRules::ApplyDependencies(o);
  119. BOOST_CHECK(o.field_models);
  120. BOOST_CHECK(o.wm_models);
  121. }
  122. BOOST_AUTO_TEST_CASE(OneMapDoesNotEnableTheOtherModels){
  123. InstallOptions o;
  124. o.fields = true;
  125. o.wm_models = true;
  126. InstallRules::ApplyDependencies(o);
  127. BOOST_CHECK(!o.wm_models);
  128. }
  129. BOOST_AUTO_TEST_CASE(SelectableMatchesDependencies){
  130. // An option is selectable exactly when applying the rules to it keeps it.
  131. for (int bits = 0; bits < (1 << 2); ++ bits){
  132. InstallOptions o;
  133. o.fields = bits & 1;
  134. o.wm = bits & 2;
  135. o.field_models = o.wm_models = true;
  136. const InstallOptions selectable = InstallRules::Selectable(o);
  137. InstallRules::ApplyDependencies(o);
  138. BOOST_CHECK_EQUAL(o.field_models, selectable.field_models);
  139. BOOST_CHECK_EQUAL(o.wm_models, selectable.wm_models);
  140. }
  141. }
  142. BOOST_AUTO_TEST_CASE(PresetsRespectTheRules){
  143. for (InstallPreset preset : {
  144. InstallPreset::EVERYTHING, InstallPreset::FIELDS, InstallPreset::WORLD_MAP, InstallPreset::BATTLE,
  145. InstallPreset::MEDIA, InstallPreset::CUSTOM
  146. }){
  147. for (bool videos : {false, true}){
  148. InstallOptions o = InstallRules::Preset(preset, videos);
  149. InstallOptions checked = o;
  150. InstallRules::ApplyDependencies(checked);
  151. BOOST_CHECK(o.field_models == checked.field_models && o.wm_models == checked.wm_models);
  152. // Videos are only chosen when the disk has them.
  153. if (!videos) BOOST_CHECK(!o.videos);
  154. }
  155. }
  156. }
  157. BOOST_AUTO_TEST_CASE(PresetContents){
  158. const InstallOptions all = InstallRules::Preset(InstallPreset::EVERYTHING, true);
  159. BOOST_CHECK(all.battle_data && all.battle_models && all.kernel && all.images && all.sounds && all.music);
  160. BOOST_CHECK(all.fields && all.field_models && all.wm && all.wm_models && all.videos);
  161. const InstallOptions world = InstallRules::Preset(InstallPreset::WORLD_MAP, true);
  162. BOOST_CHECK(world.wm && world.wm_models);
  163. BOOST_CHECK(!world.fields && !world.field_models && !world.music && !world.videos);
  164. const InstallOptions fields = InstallRules::Preset(InstallPreset::FIELDS, true);
  165. BOOST_CHECK(fields.fields && fields.field_models && !fields.wm);
  166. const InstallOptions none = InstallRules::Preset(InstallPreset::CUSTOM, true);
  167. BOOST_CHECK(!none.fields && !none.wm && !none.music && !none.sounds && !none.videos);
  168. }
  169. BOOST_AUTO_TEST_CASE(ToolsNeededByTheAudio){
  170. InstallOptions none;
  171. BOOST_CHECK(!InstallRules::ToolsNeeded(none).ffmpeg && !InstallRules::ToolsNeeded(none).timidity);
  172. InstallOptions sounds;
  173. sounds.sounds = true;
  174. BOOST_CHECK(InstallRules::ToolsNeeded(sounds).ffmpeg);
  175. BOOST_CHECK(!InstallRules::ToolsNeeded(sounds).timidity);
  176. // The music needs both: TiMidity renders the MIDI and FFmpeg encodes it.
  177. InstallOptions music;
  178. music.music = true;
  179. BOOST_CHECK(InstallRules::ToolsNeeded(music).ffmpeg && InstallRules::ToolsNeeded(music).timidity);
  180. // Nothing else needs them.
  181. InstallOptions other;
  182. other.fields = other.field_models = other.wm = other.battle_data = other.images = other.kernel = true;
  183. BOOST_CHECK(!InstallRules::ToolsNeeded(other).ffmpeg && !InstallRules::ToolsNeeded(other).timidity);
  184. }
  185. BOOST_AUTO_TEST_CASE(WhatIsLostWithoutTheTools){
  186. InstallOptions all;
  187. all.sounds = all.music = true;
  188. // Nothing is lost when the programs work, or when nothing needs them.
  189. BOOST_CHECK(InstallRules::ToolConsequences(all, true, true).empty());
  190. BOOST_CHECK(InstallRules::ToolConsequences(InstallOptions(), false, false).empty());
  191. // Without FFmpeg, both the sounds and the music are lost.
  192. const auto no_ffmpeg = InstallRules::ToolConsequences(all, false, true);
  193. BOOST_REQUIRE_EQUAL(no_ffmpeg.size(), 2u);
  194. BOOST_CHECK(no_ffmpeg[0].find("sound") != std::string::npos);
  195. BOOST_CHECK(no_ffmpeg[1].find("music") != std::string::npos);
  196. // Without TiMidity only the music is lost.
  197. const auto no_timidity = InstallRules::ToolConsequences(all, true, false);
  198. BOOST_REQUIRE_EQUAL(no_timidity.size(), 1u);
  199. BOOST_CHECK(no_timidity[0].find("music") != std::string::npos);
  200. BOOST_CHECK(no_timidity[0].find("TiMidity") != std::string::npos);
  201. // Without both it is not said twice.
  202. BOOST_CHECK_EQUAL(InstallRules::ToolConsequences(all, false, false).size(), 2u);
  203. // Sound effects don't need TiMidity.
  204. InstallOptions sounds;
  205. sounds.sounds = true;
  206. BOOST_CHECK(InstallRules::ToolConsequences(sounds, true, false).empty());
  207. BOOST_CHECK_EQUAL(InstallRules::ToolConsequences(sounds, false, true).size(), 1u);
  208. // Music alone, without FFmpeg.
  209. InstallOptions music;
  210. music.music = true;
  211. const auto music_only = InstallRules::ToolConsequences(music, false, true);
  212. BOOST_REQUIRE_EQUAL(music_only.size(), 1u);
  213. BOOST_CHECK(music_only[0].find("music") != std::string::npos);
  214. }
  215. BOOST_AUTO_TEST_CASE(WorldMapNeedsFields){
  216. InstallOptions o;
  217. o.wm = true;
  218. BOOST_CHECK_EQUAL(InstallRules::Problems(o, false).size(), 1u);
  219. // Fine if the fields are installed now or were installed before.
  220. BOOST_CHECK(InstallRules::Problems(o, true).empty());
  221. o.fields = true;
  222. BOOST_CHECK(InstallRules::Problems(o, false).empty());
  223. // No world map, no problem.
  224. BOOST_CHECK(InstallRules::Problems(InstallOptions(), false).empty());
  225. }
  226. BOOST_AUTO_TEST_CASE(Estimates){
  227. uint64_t temporary = 99;
  228. BOOST_CHECK_EQUAL(InstallRules::EstimateMegabytes(InstallOptions(), &temporary), 0u);
  229. BOOST_CHECK_EQUAL(temporary, 0u);
  230. // Selecting more never makes it smaller.
  231. const InstallOptions all = InstallRules::Preset(InstallPreset::EVERYTHING, false);
  232. uint64_t all_temporary = 0;
  233. const uint64_t all_size = InstallRules::EstimateMegabytes(all, &all_temporary);
  234. for (InstallPreset preset : {InstallPreset::FIELDS, InstallPreset::WORLD_MAP, InstallPreset::BATTLE, InstallPreset::MEDIA}){
  235. uint64_t t = 0;
  236. BOOST_CHECK_LE(InstallRules::EstimateMegabytes(InstallRules::Preset(preset, false), &t), all_size);
  237. BOOST_CHECK_LE(t, all_temporary);
  238. }
  239. // The pointer is optional.
  240. BOOST_CHECK_EQUAL(InstallRules::EstimateMegabytes(all), all_size);
  241. }
  242. BOOST_AUTO_TEST_SUITE_END()
  243. // ---------------------------------------------------------------------------------------------------------------
  244. // Verifier.
  245. // ---------------------------------------------------------------------------------------------------------------
  246. BOOST_AUTO_TEST_SUITE(InstallVerifierTests)
  247. BOOST_AUTO_TEST_CASE(EmptyDirectoryMissesEverything){
  248. TempDir dir;
  249. const VerifyResult result = VerifyInstallation(dir.Root());
  250. BOOST_CHECK(!result.Ok());
  251. for (const char* group : {
  252. "Configuration", "Game files", "Kernel data", "Battle data", "Battle models", "Field maps", "Field models",
  253. "World map", "World map models", "Sound effects", "Music", "Images", "Fonts"
  254. }) BOOST_CHECK_MESSAGE(HasProblem(result, group), "no problem reported for " << group);
  255. BOOST_CHECK_GT(result.checked, 0);
  256. }
  257. BOOST_AUTO_TEST_CASE(ConfigurationAndPlugins){
  258. TempDir dir;
  259. BOOST_CHECK(HasProblem(VerifyInstallation(dir.Root()), "Configuration"));
  260. // A plugin that can't be in the system, so only the given directories count.
  261. const std::string plugin = "Plugin_VGearsVerifierTest";
  262. VerifyPlugins search;
  263. search.ogre_version = "1.2.3";
  264. const std::string file_name = VGears::OgrePlugins::FileNames(plugin, search.ogre_version).front();
  265. std::ofstream(dir.path.string() + "/plugins.cfg") << "Plugin=" << plugin << "\n";
  266. dir.Touch("resources.cfg");
  267. BOOST_CHECK(HasProblem(VerifyInstallation(dir.Root(), "", search), "Configuration"));
  268. // Without the Ogre version the plugins are not checked.
  269. BOOST_CHECK(!HasProblem(VerifyInstallation(dir.Root()), "Configuration"));
  270. // Next to the executable, as the game looks for it there too.
  271. TempDir executable;
  272. executable.Touch(file_name);
  273. search.executable_dir = executable.path.string();
  274. BOOST_CHECK(!HasProblem(VerifyInstallation(dir.Root(), "", search), "Configuration"));
  275. // In the installation directory.
  276. search.executable_dir.clear();
  277. dir.Touch(file_name);
  278. BOOST_CHECK(!HasProblem(VerifyInstallation(dir.Root(), "", search), "Configuration"));
  279. }
  280. BOOST_AUTO_TEST_CASE(RootWithoutTrailingSlash){
  281. TempDir dir;
  282. dir.Touch("plugins.cfg");
  283. const VerifyResult with = VerifyInstallation(dir.Root());
  284. const VerifyResult without = VerifyInstallation(dir.path.string());
  285. BOOST_CHECK_EQUAL(with.problems.size(), without.problems.size());
  286. }
  287. BOOST_AUTO_TEST_CASE(FilesOfTheDataFolderMustBeCopied){
  288. TempDir installation;
  289. TempDir reference;
  290. reference.Touch("plugins.cfg");
  291. reference.Touch("engine/scripts/a.lua");
  292. BOOST_CHECK(HasProblem(VerifyInstallation(installation.Root(), reference.Root()), "Copied data files"));
  293. installation.Touch("plugins.cfg");
  294. BOOST_CHECK(HasProblem(VerifyInstallation(installation.Root(), reference.Root()), "Copied data files"));
  295. installation.Touch("engine/scripts/a.lua");
  296. BOOST_CHECK(!HasProblem(VerifyInstallation(installation.Root(), reference.Root()), "Copied data files"));
  297. // Without a reference there is nothing to compare.
  298. BOOST_CHECK(!HasProblem(VerifyInstallation(installation.Root()), "Copied data files"));
  299. }
  300. BOOST_AUTO_TEST_CASE(CountsFilesInsteadOfJustLookingForOne){
  301. TempDir dir;
  302. for (int i = 0; i < 3; ++ i) dir.Touch("game/field/maps/f" + std::to_string(i) + "/map.xml");
  303. // Three fields are not an installation of the fields.
  304. BOOST_CHECK(HasProblem(VerifyInstallation(dir.Root()), "Field maps"));
  305. }
  306. BOOST_AUTO_TEST_SUITE_END()
  307. // ---------------------------------------------------------------------------------------------------------------
  308. // Log.
  309. // ---------------------------------------------------------------------------------------------------------------
  310. BOOST_AUTO_TEST_SUITE(InstallLogTests)
  311. BOOST_AUTO_TEST_CASE(Severity){
  312. BOOST_CHECK_EQUAL(InstallLog::Classify("[ERROR] Skip field: x"), InstallLog::ERROR_LEVEL);
  313. BOOST_CHECK_EQUAL(InstallLog::Classify("Exception: Bone not found"), InstallLog::ERROR_LEVEL);
  314. BOOST_CHECK_EQUAL(InstallLog::Classify("[WARNING] Texture not found"), InstallLog::WARNING);
  315. BOOST_CHECK_EQUAL(InstallLog::Classify("Converting field models..."), InstallLog::INFO);
  316. BOOST_CHECK_EQUAL(InstallLog::Classify(""), InstallLog::INFO);
  317. }
  318. BOOST_AUTO_TEST_CASE(CountsFileAndSink){
  319. TempDir dir;
  320. InstallLog& log = InstallLog::Get();
  321. std::vector<std::string> shown;
  322. log.Open(dir.Root() + "installer.log");
  323. log.SetSink([&shown](InstallLog::Level, const std::string& message){shown.push_back(message);});
  324. log.Add("Starting");
  325. log.Add("[WARNING] careful");
  326. log.Add("[ERROR] broken");
  327. // The same problem reported twice in a row counts once.
  328. log.Add("[ERROR] broken");
  329. log.Add("hidden", false);
  330. // What the installer prints is captured too, and only problems reach the sink.
  331. std::cerr << "[ERROR] printed" << std::endl;
  332. std::cout << "plain output" << std::endl;
  333. BOOST_CHECK_EQUAL(log.Count(InstallLog::ERROR_LEVEL), 2u);
  334. BOOST_CHECK_EQUAL(log.Count(InstallLog::WARNING), 1u);
  335. BOOST_CHECK(Contains(shown, "Starting"));
  336. BOOST_CHECK(!Contains(shown, "hidden"));
  337. BOOST_CHECK(!Contains(shown, "plain output"));
  338. BOOST_CHECK(Contains(shown, "[ERROR] printed"));
  339. BOOST_REQUIRE_EQUAL(log.First(InstallLog::ERROR_LEVEL, 1).size(), 1u);
  340. BOOST_CHECK_EQUAL(log.First(InstallLog::ERROR_LEVEL, 1)[0], "[ERROR] broken");
  341. log.SetSink(nullptr);
  342. log.Close();
  343. std::ifstream file(dir.Root() + "installer.log");
  344. std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  345. BOOST_CHECK(content.find("careful") != std::string::npos);
  346. BOOST_CHECK(content.find("hidden") != std::string::npos);
  347. BOOST_CHECK(content.find("plain output") != std::string::npos);
  348. }
  349. BOOST_AUTO_TEST_SUITE_END()
  350. // ---------------------------------------------------------------------------------------------------------------
  351. // Steps of the log.
  352. // ---------------------------------------------------------------------------------------------------------------
  353. BOOST_AUTO_TEST_SUITE(InstallLogStepTests)
  354. BOOST_AUTO_TEST_CASE(StepsReportTheirOwnProblems){
  355. TempDir dir;
  356. InstallLog& log = InstallLog::Get();
  357. std::vector<std::string> shown;
  358. log.Open(dir.Root() + "installer.log");
  359. log.SetSink([&shown](InstallLog::Level, const std::string& message){shown.push_back(message);});
  360. log.BeginStep("Clean step");
  361. log.Add("fine");
  362. log.BeginStep("Bad step");
  363. log.Add("[ERROR] one");
  364. log.Add("[ERROR] two");
  365. log.Add("[WARNING] three");
  366. log.BeginStep("Next step");
  367. log.Add("[WARNING] four");
  368. log.EndStep();
  369. log.SetSink(nullptr);
  370. log.Close();
  371. auto summary = [&shown](const std::string& step){
  372. for (const std::string& line : shown) if (line.find("\"" + step + "\" finished") != std::string::npos) return line;
  373. return std::string();
  374. };
  375. // Steps without problems are not mentioned, the others only count their own.
  376. BOOST_CHECK(summary("Clean step").empty());
  377. BOOST_CHECK(summary("Bad step").find("2 error(s) and 1 warning(s)") != std::string::npos);
  378. BOOST_CHECK(summary("Next step").find("0 error(s) and 1 warning(s)") != std::string::npos);
  379. // The summaries don't count as problems themselves.
  380. BOOST_CHECK_EQUAL(InstallLog::Classify(summary("Bad step")), InstallLog::INFO);
  381. }
  382. BOOST_AUTO_TEST_SUITE_END()
  383. // ---------------------------------------------------------------------------------------------------------------
  384. // Task pool.
  385. // ---------------------------------------------------------------------------------------------------------------
  386. BOOST_AUTO_TEST_SUITE(TaskPoolTests)
  387. BOOST_AUTO_TEST_CASE(RunsEveryTask){
  388. std::atomic<int> done(0);
  389. TaskPool pool(4);
  390. for (int i = 0; i < 200; ++ i) pool.Run([&done](){++ done;});
  391. pool.Wait();
  392. BOOST_CHECK_EQUAL(done.load(), 200);
  393. }
  394. BOOST_AUTO_TEST_CASE(WaitWithoutTasksReturns){
  395. TaskPool pool(2);
  396. pool.Wait();
  397. BOOST_CHECK_EQUAL(pool.Threads(), 2u);
  398. }
  399. BOOST_AUTO_TEST_CASE(RunsInParallel){
  400. // Tasks that wait for each other can only finish if they really run at the same time.
  401. std::atomic<int> arrived(0);
  402. TaskPool pool(3);
  403. for (int i = 0; i < 3; ++ i){
  404. pool.Run([&arrived](){
  405. ++ arrived;
  406. const auto limit = std::chrono::steady_clock::now() + std::chrono::seconds(5);
  407. while (arrived < 3 && std::chrono::steady_clock::now() < limit) std::this_thread::yield();
  408. });
  409. }
  410. pool.Wait();
  411. BOOST_CHECK_EQUAL(arrived.load(), 3);
  412. }
  413. BOOST_AUTO_TEST_CASE(UsesSeveralThreads){
  414. std::mutex mutex;
  415. std::set<std::thread::id> threads;
  416. TaskPool pool(4);
  417. for (int i = 0; i < 50; ++ i){
  418. pool.Run([&](){
  419. std::this_thread::sleep_for(std::chrono::milliseconds(5));
  420. std::lock_guard<std::mutex> lock(mutex);
  421. threads.insert(std::this_thread::get_id());
  422. });
  423. }
  424. pool.Wait();
  425. BOOST_CHECK_GT(threads.size(), 1u);
  426. BOOST_CHECK_LE(threads.size(), 4u);
  427. }
  428. BOOST_AUTO_TEST_CASE(DestructorFinishesPendingTasks){
  429. std::atomic<int> done(0);
  430. {
  431. TaskPool pool(2);
  432. for (int i = 0; i < 20; ++ i) pool.Run([&done](){
  433. std::this_thread::sleep_for(std::chrono::milliseconds(2));
  434. ++ done;
  435. });
  436. }
  437. BOOST_CHECK_EQUAL(done.load(), 20);
  438. }
  439. BOOST_AUTO_TEST_CASE(ATaskThatThrowsDoesNotStopThePool){
  440. std::atomic<int> done(0);
  441. TaskPool pool(2);
  442. pool.Run([](){throw std::runtime_error("boom");});
  443. for (int i = 0; i < 10; ++ i) pool.Run([&done](){++ done;});
  444. pool.Wait();
  445. BOOST_CHECK_EQUAL(done.load(), 10);
  446. }
  447. BOOST_AUTO_TEST_CASE(DefaultNumberOfThreadsIsReasonable){
  448. TaskPool pool;
  449. BOOST_CHECK_GE(pool.Threads(), 1u);
  450. BOOST_CHECK_LE(pool.Threads(), 8u);
  451. }
  452. BOOST_AUTO_TEST_SUITE_END()
  453. // ---------------------------------------------------------------------------------------------------------------
  454. // Videos of the game disks.
  455. // ---------------------------------------------------------------------------------------------------------------
  456. BOOST_AUTO_TEST_SUITE(VideoFilesTests)
  457. BOOST_AUTO_TEST_CASE(RecognizesMovies){
  458. std::string name;
  459. BOOST_CHECK(VideoFiles::IsMovie("ff7/movies/bike.avi", &name));
  460. BOOST_CHECK_EQUAL(name, "bike.avi");
  461. // The disks mix the case, and ISO9660 adds version numbers and dots.
  462. BOOST_CHECK(VideoFiles::IsMovie("FF7/MOVIES/SQLOGO.AVI;1", &name));
  463. BOOST_CHECK_EQUAL(name, "sqlogo.avi");
  464. BOOST_CHECK(VideoFiles::IsMovie("/ff7/Movies/Explode.avi", &name));
  465. BOOST_CHECK_EQUAL(name, "explode.avi");
  466. BOOST_CHECK(VideoFiles::IsMovie("ff7\\movies\\ending1.avi", &name));
  467. BOOST_CHECK_EQUAL(name, "ending1.avi");
  468. // Other files of the folder are copied too (the camera data of the movies).
  469. BOOST_CHECK(VideoFiles::IsMovie("ff7/movies/moviecam.lgp", &name));
  470. BOOST_CHECK_EQUAL(name, "moviecam.lgp");
  471. }
  472. BOOST_AUTO_TEST_CASE(IgnoresEverythingElse){
  473. BOOST_CHECK(!VideoFiles::IsMovie("ff7/movies"));
  474. BOOST_CHECK(!VideoFiles::IsMovie("ff7/movies/"));
  475. BOOST_CHECK(!VideoFiles::IsMovie("ff7/movies/sub/bike.avi"));
  476. BOOST_CHECK(!VideoFiles::IsMovie("ff7/field/char.lgp"));
  477. BOOST_CHECK(!VideoFiles::IsMovie("ff7/moviesx/bike.avi"));
  478. BOOST_CHECK(!VideoFiles::IsMovie("data/movies/bike.avi"));
  479. BOOST_CHECK(!VideoFiles::IsMovie(""));
  480. std::string name = "untouched";
  481. BOOST_CHECK(!VideoFiles::IsMovie("ff7/wm/x.avi", &name));
  482. BOOST_CHECK_EQUAL(name, "untouched");
  483. }
  484. BOOST_AUTO_TEST_CASE(NormalizesPaths){
  485. BOOST_CHECK_EQUAL(VideoFiles::NormalizePath("/FF7/Movies/A.AVI;1"), "ff7/movies/a.avi");
  486. BOOST_CHECK_EQUAL(VideoFiles::NormalizePath("FF7/MOVIES/A."), "ff7/movies/a");
  487. // A semicolon that is not a version stays.
  488. BOOST_CHECK_EQUAL(VideoFiles::NormalizePath("a;b"), "a;b");
  489. }
  490. BOOST_AUTO_TEST_CASE(LogCanBeAppended){
  491. TempDir dir;
  492. InstallLog& log = InstallLog::Get();
  493. log.Open(dir.Root() + "installer.log");
  494. log.Add("first run");
  495. log.Close();
  496. log.Open(dir.Root() + "installer.log", true);
  497. log.Add("second run");
  498. log.Close();
  499. std::ifstream file(dir.Root() + "installer.log");
  500. std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  501. BOOST_CHECK(content.find("first run") != std::string::npos);
  502. BOOST_CHECK(content.find("second run") != std::string::npos);
  503. // Without append the file starts over.
  504. log.Open(dir.Root() + "installer.log");
  505. log.Close();
  506. std::ifstream again(dir.Root() + "installer.log");
  507. std::string content2((std::istreambuf_iterator<char>(again)), std::istreambuf_iterator<char>());
  508. BOOST_CHECK(content2.empty());
  509. }
  510. BOOST_AUTO_TEST_SUITE_END()
  511. // Command line.
  512. BOOST_AUTO_TEST_CASE(CommandLineWithoutInstallerOptionsOpensTheWindow){
  513. using namespace InstallCommandLine;
  514. BOOST_CHECK(Parse({}).mode == Mode::GUI);
  515. // Qt's own arguments are left for the window.
  516. BOOST_CHECK(Parse({"-platform", "xcb"}).mode == Mode::GUI);
  517. }
  518. BOOST_AUTO_TEST_CASE(CommandLineInstallDefaults){
  519. using namespace InstallCommandLine;
  520. const Arguments a = Parse({"--cli", "--iso", "disk.iso"});
  521. BOOST_REQUIRE(a.mode == Mode::INSTALL);
  522. BOOST_CHECK_EQUAL(a.iso, "disk.iso");
  523. BOOST_CHECK(a.output.empty());
  524. BOOST_CHECK(a.copy_data);
  525. BOOST_CHECK(!a.keep_originals);
  526. BOOST_CHECK(!a.clean);
  527. BOOST_CHECK(!a.strict);
  528. BOOST_CHECK(!a.yes);
  529. // Everything, except the videos, which are not on the install disk.
  530. const InstallOptions o = a.options;
  531. BOOST_CHECK(o.kernel && o.fields && o.field_models && o.wm && o.wm_models && o.battle_data && o.battle_models);
  532. BOOST_CHECK(o.images && o.sounds && o.music);
  533. BOOST_CHECK(!o.videos);
  534. }
  535. BOOST_AUTO_TEST_CASE(CommandLineInstallerOptionsImplyCli){
  536. using namespace InstallCommandLine;
  537. const Arguments a = Parse({"-i", "disk.iso", "-o", "/tmp/vg", "-y", "--no-copy-data", "--keep-originals", "--clean"});
  538. BOOST_REQUIRE(a.mode == Mode::INSTALL);
  539. BOOST_CHECK_EQUAL(a.output, "/tmp/vg");
  540. BOOST_CHECK(a.yes);
  541. BOOST_CHECK(!a.copy_data);
  542. BOOST_CHECK(a.keep_originals);
  543. BOOST_CHECK(a.clean);
  544. // Each of them is enough to use the command line.
  545. BOOST_CHECK(Parse({"--clean"}).mode == Mode::INVALID); // No ISO, but not the window.
  546. BOOST_CHECK(Parse({"--keep-originals"}).mode == Mode::INVALID);
  547. BOOST_CHECK(Parse({"-i", "d.iso", "--strict"}).strict);
  548. // "--option=value" works too.
  549. BOOST_CHECK_EQUAL(Parse({"--iso=other.iso"}).iso, "other.iso");
  550. }
  551. BOOST_AUTO_TEST_CASE(CommandLinePresetsAndParts){
  552. using namespace InstallCommandLine;
  553. Arguments a = Parse({"-i", "d.iso", "--preset", "battle", "--with", "music,images", "--without", "kernel"});
  554. BOOST_REQUIRE(a.mode == Mode::INSTALL);
  555. BOOST_CHECK(a.options.battle_data && a.options.battle_models && a.options.music && a.options.images);
  556. BOOST_CHECK(!a.options.kernel && !a.options.fields && !a.options.sounds);
  557. a = Parse({"-i", "d.iso", "-p", "custom", "--with", "fields"});
  558. BOOST_REQUIRE(a.mode == Mode::INSTALL);
  559. BOOST_CHECK(a.options.fields && !a.options.field_models && !a.options.kernel);
  560. // Removing a part removes what depends on it.
  561. a = Parse({"-i", "d.iso", "--without", "world-map"});
  562. BOOST_REQUIRE(a.mode == Mode::INSTALL);
  563. BOOST_CHECK(!a.options.wm && !a.options.wm_models);
  564. }
  565. BOOST_AUTO_TEST_CASE(CommandLineErrors){
  566. using namespace InstallCommandLine;
  567. BOOST_CHECK(Parse({"--cli"}).mode == Mode::INVALID);
  568. BOOST_CHECK(Parse({"--iso"}).mode == Mode::INVALID);
  569. BOOST_CHECK(Parse({"-i", "d.iso", "--preset", "nope"}).mode == Mode::INVALID);
  570. BOOST_CHECK(Parse({"-i", "d.iso", "--with", "nope"}).mode == Mode::INVALID);
  571. BOOST_CHECK(Parse({"-i", "d.iso", "--bogus"}).mode == Mode::INVALID);
  572. // A part can't be added without what it needs.
  573. BOOST_CHECK(Parse({"-i", "d.iso", "-p", "custom", "--with", "field-models"}).mode == Mode::INVALID);
  574. BOOST_CHECK(Parse({"--verify", "-i", "d.iso"}).mode == Mode::INVALID);
  575. BOOST_CHECK(Parse({"--verify", "--with", "music"}).mode == Mode::INVALID);
  576. BOOST_CHECK(Parse({"--verify", "--clean"}).mode == Mode::INVALID);
  577. // The old name of the option is gone.
  578. BOOST_CHECK(Parse({"-i", "d.iso", "--discard-originals"}).mode == Mode::INVALID);
  579. }
  580. BOOST_AUTO_TEST_CASE(CommandLineOtherModes){
  581. using namespace InstallCommandLine;
  582. BOOST_CHECK(Parse({"--verify"}).mode == Mode::VERIFY);
  583. BOOST_CHECK_EQUAL(Parse({"--verify", "-o", "/x"}).output, "/x");
  584. BOOST_CHECK(Parse({"--help"}).mode == Mode::HELP);
  585. BOOST_CHECK(Parse({"-i", "d.iso", "-h"}).mode == Mode::HELP);
  586. BOOST_CHECK(Parse({"--version"}).mode == Mode::VERSION);
  587. BOOST_CHECK(Parse({"--list-parts"}).mode == Mode::LIST_PARTS);
  588. BOOST_CHECK(!Usage("v-gears-installer").empty());
  589. }
  590. // Clean installation.
  591. BOOST_AUTO_TEST_CASE(CleanInstallationDeletesTheDataAndKeepsTheRest){
  592. TempDir dir;
  593. const std::string root = dir.Root();
  594. for (const char* file : {
  595. "plugins.cfg", "resources.cfg", "v-gears.cfg", "logs/installer.log", "user/saves/0.xml", "user/config.cfg",
  596. "game/video/opening/movie.xml", "game/field/maps/md1_1/map.xml", "game/field/models/aerisflowers.mesh",
  597. "game/audio/sounds/1.ogg", "game/scripts/x.lua", "engine/scripts/x.lua", "defaults/config.cfg",
  598. "temp/char/cbba.rsd", "temp/world_models/a.p", "original_data/ff7/ff7.exe", "install.info",
  599. // The files of an older version.
  600. "save/1.xml", "controls.cfg", "data/fields/md1_1/map.xml", "data/video/gold1/movie.xml"
  601. }) dir.Touch(file);
  602. const InstallLayout::CleanResult result = InstallLayout::CleanInstallation(root);
  603. BOOST_CHECK(result.errors.empty());
  604. BOOST_CHECK(result.removed > 0);
  605. // Kept, the files of the older version in their new places.
  606. for (const char* file : {
  607. "plugins.cfg", "resources.cfg", "v-gears.cfg", "logs/installer.log", "user/saves/0.xml", "user/saves/1.xml",
  608. "user/controls.cfg", "user/config.cfg", "game/video/opening/movie.xml", "game/video/gold1/movie.xml"
  609. }) BOOST_CHECK_MESSAGE(fs::exists(dir.path / file), std::string("kept: ") + file);
  610. // Deleted.
  611. for (const char* path : {
  612. "game/field", "game/battle", "game/audio", "game/scripts", "engine", "defaults", "data", "temp",
  613. "original_data", "install.info"
  614. }) BOOST_CHECK_MESSAGE(!fs::exists(dir.path / path), std::string("deleted: ") + path);
  615. BOOST_CHECK(fs::is_directory(dir.path / "game"));
  616. }
  617. BOOST_AUTO_TEST_CASE(CleanInstallationDoesNotTouchOtherDirectories){
  618. // Without the configuration files it is not an installation, and nothing is deleted.
  619. TempDir dir;
  620. dir.Touch("game/my_files/document.txt");
  621. dir.Touch("temp/notes.txt");
  622. BOOST_CHECK(!InstallLayout::IsInstallation(dir.Root()));
  623. const InstallLayout::CleanResult result = InstallLayout::CleanInstallation(dir.Root());
  624. BOOST_CHECK_EQUAL(result.removed, 0u);
  625. BOOST_CHECK(fs::exists(dir.path / "game/my_files/document.txt"));
  626. BOOST_CHECK(fs::exists(dir.path / "temp/notes.txt"));
  627. }
  628. BOOST_AUTO_TEST_CASE(OriginalDataIsInsideTheInstallationDirectory){
  629. BOOST_CHECK_EQUAL(InstallLayout::OriginalDataDir("/x/"), "/x/original_data/");
  630. }
  631. // Record of the installations.
  632. BOOST_AUTO_TEST_CASE(InstallStampIsWrittenAndRead){
  633. namespace stamp = VGears::InstallStamp;
  634. TempDir dir;
  635. stamp::Stamp written;
  636. stamp::Start(written, {"kernel", "fields"}, "1.2.3", "2026-11-26 10:00:00", "Install Disk");
  637. BOOST_REQUIRE(stamp::Write(dir.path, written));
  638. stamp::Stamp read;
  639. BOOST_REQUIRE(stamp::Read(dir.path, read));
  640. BOOST_CHECK(!read.complete);
  641. BOOST_CHECK_EQUAL(read.installer, "1.2.3");
  642. BOOST_CHECK_EQUAL(read.date, "2026-11-26 10:00:00");
  643. BOOST_CHECK_EQUAL(read.disk, "Install Disk");
  644. BOOST_CHECK(read.installing == std::vector<std::string>({"kernel", "fields"}));
  645. stamp::Finish(read);
  646. BOOST_REQUIRE(stamp::Write(dir.path, read));
  647. BOOST_REQUIRE(stamp::Read(dir.path, read));
  648. BOOST_CHECK(read.complete);
  649. BOOST_CHECK(read.installing.empty());
  650. BOOST_CHECK_EQUAL(read.parts["kernel"], stamp::DATA_VERSION);
  651. BOOST_CHECK_EQUAL(read.parts["fields"], stamp::DATA_VERSION);
  652. BOOST_CHECK(stamp::Outdated(read).empty());
  653. BOOST_CHECK(stamp::Problems(dir.path).empty());
  654. // Without a file, there is no record.
  655. TempDir empty;
  656. BOOST_CHECK(!stamp::Read(empty.path, read));
  657. bool found = true;
  658. BOOST_CHECK(stamp::Problems(empty.path, &found).empty());
  659. BOOST_CHECK(!found);
  660. }
  661. BOOST_AUTO_TEST_CASE(InstallStampReportsUnfinishedAndOldInstallations){
  662. namespace stamp = VGears::InstallStamp;
  663. TempDir dir;
  664. stamp::Stamp record;
  665. // A complete installation, then one of other parts that is interrupted.
  666. stamp::Start(record, {"kernel"}, "1", "a", "disk");
  667. stamp::Finish(record);
  668. stamp::Start(record, {"battle-data"}, "1", "b", "disk");
  669. BOOST_REQUIRE(stamp::Write(dir.path, record));
  670. BOOST_CHECK_EQUAL(stamp::Problems(dir.path).size(), 1u);
  671. BOOST_CHECK(stamp::Outdated(record) == std::vector<std::string>({"battle-data"}));
  672. // The next installation doesn't install them again: they stay as not installed.
  673. stamp::Start(record, {"fields"}, "1", "c", "disk");
  674. stamp::Finish(record);
  675. BOOST_CHECK_EQUAL(record.parts["battle-data"], 0);
  676. BOOST_CHECK(stamp::Outdated(record) == std::vector<std::string>({"battle-data"}));
  677. BOOST_CHECK_EQUAL(record.parts["kernel"], stamp::DATA_VERSION);
  678. // A part of an older version of the data.
  679. record.parts["fields"] = stamp::DATA_VERSION - 1;
  680. BOOST_REQUIRE(stamp::Write(dir.path, record));
  681. const std::vector<std::string> problems = stamp::Problems(dir.path);
  682. BOOST_REQUIRE_EQUAL(problems.size(), 1u);
  683. BOOST_CHECK(problems[0].find("battle-data") != std::string::npos);
  684. BOOST_CHECK(problems[0].find("fields") != std::string::npos);
  685. }
  686. BOOST_AUTO_TEST_CASE(VerifierReportsTheRecord){
  687. namespace stamp = VGears::InstallStamp;
  688. TempDir dir;
  689. BOOST_CHECK(HasProblem(VerifyInstallation(dir.Root()), "Installation"));
  690. stamp::Stamp record;
  691. stamp::Start(record, {"kernel"}, "1", "a", "disk");
  692. BOOST_REQUIRE(stamp::Write(dir.path, record));
  693. BOOST_CHECK(HasProblem(VerifyInstallation(dir.Root()), "Installation"));
  694. stamp::Finish(record);
  695. BOOST_REQUIRE(stamp::Write(dir.path, record));
  696. BOOST_CHECK(!HasProblem(VerifyInstallation(dir.Root()), "Installation"));
  697. }