InstallLogic.cpp 34 KB

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