InstallLogic.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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/InstallCommandLine.h"
  21. #include "installer/InstallLayout.h"
  22. #include "installer/InstallLog.h"
  23. #include "installer/InstallOptions.h"
  24. #include "installer/InstallVerifier.h"
  25. #include "installer/TaskPool.h"
  26. #include "installer/VideoFiles.h"
  27. #include <atomic>
  28. #include <chrono>
  29. #include <set>
  30. namespace fs = boost::filesystem;
  31. namespace{
  32. bool Contains(const std::vector<std::string>& list, const std::string& value){
  33. return std::find(list.begin(), list.end(), value) != list.end();
  34. }
  35. bool StartsWith(const std::string& text, const std::string& prefix){return text.compare(0, prefix.size(), prefix) == 0;}
  36. /**
  37. * A temporary directory that is removed on destruction.
  38. */
  39. struct TempDir{
  40. fs::path path;
  41. TempDir(): path(fs::temp_directory_path() / fs::unique_path("vgears-test-%%%%-%%%%")){
  42. fs::create_directories(path);
  43. }
  44. ~TempDir(){
  45. boost::system::error_code ec;
  46. fs::remove_all(path, ec);
  47. }
  48. std::string Root() const{return path.string() + "/";}
  49. void Touch(const std::string& relative) const{
  50. fs::create_directories((path / relative).parent_path());
  51. std::ofstream(( path / relative).string()) << "x";
  52. }
  53. };
  54. bool HasProblem(const VerifyResult& result, const std::string& group){
  55. for (const VerifyProblem& problem : result.problems) if (problem.group == group) return true;
  56. return false;
  57. }
  58. }
  59. // ---------------------------------------------------------------------------------------------------------------
  60. // Layout: where things go.
  61. // ---------------------------------------------------------------------------------------------------------------
  62. BOOST_AUTO_TEST_SUITE(InstallLayoutTests)
  63. BOOST_AUTO_TEST_CASE(DataAndTempAreInsideTheInstallationDirectory){
  64. BOOST_CHECK_EQUAL(InstallLayout::DataDir("/home/u/.v-gears/"), "/home/u/.v-gears/data/");
  65. BOOST_CHECK_EQUAL(InstallLayout::TempDir("/home/u/.v-gears/"), "/home/u/.v-gears/temp/");
  66. }
  67. BOOST_AUTO_TEST_CASE(DataSubdirsAreRelativeAndUnique){
  68. const auto& dirs = InstallLayout::DataSubdirs();
  69. for (const std::string& dir : dirs){
  70. BOOST_CHECK_MESSAGE(!dir.empty() && dir[0] != '/', "not relative: " << dir);
  71. BOOST_CHECK_MESSAGE(!StartsWith(dir, "data/") && !StartsWith(dir, "temp"), "wrong root: " << dir);
  72. BOOST_CHECK_MESSAGE(std::count(dirs.begin(), dirs.end(), dir) == 1, "duplicated: " << dir);
  73. }
  74. }
  75. BOOST_AUTO_TEST_CASE(EveryDirectoryTheGameReadsIsCreated){
  76. // The game reads "./data/<x>" and "./temp/<x>"; the installer must create them (or be able to write in them).
  77. for (const std::string& resource : InstallLayout::GameResourceDirs()){
  78. if (StartsWith(resource, "data/")){
  79. const std::string dir = resource.substr(5);
  80. bool created = false;
  81. for (const std::string& sub : InstallLayout::DataSubdirs())
  82. created = created || sub == dir || StartsWith(sub, dir + "/");
  83. BOOST_CHECK_MESSAGE(created, "the installer doesn't create " << resource);
  84. }
  85. else{
  86. BOOST_REQUIRE_MESSAGE(StartsWith(resource, "temp/"), "unexpected root: " << resource);
  87. BOOST_CHECK_MESSAGE(Contains(InstallLayout::TempSubdirs(), resource.substr(5)), "not created: " << resource);
  88. }
  89. }
  90. }
  91. BOOST_AUTO_TEST_CASE(CleaningKeepsWhatTheGameNeeds){
  92. const auto removable = InstallLayout::RemovableTempSubdirs();
  93. BOOST_CHECK(!Contains(removable, "world_models"));
  94. for (const std::string& kept : InstallLayout::KeptTempSubdirs()){
  95. BOOST_CHECK(Contains(InstallLayout::TempSubdirs(), kept));
  96. BOOST_CHECK(!Contains(removable, kept));
  97. }
  98. BOOST_CHECK_EQUAL(removable.size() + InstallLayout::KeptTempSubdirs().size(), InstallLayout::TempSubdirs().size());
  99. // The game reads temp/world_models, so it must be a kept directory.
  100. BOOST_CHECK(Contains(InstallLayout::KeptTempSubdirs(), "world_models"));
  101. }
  102. BOOST_AUTO_TEST_CASE(OldSettingsAreMigrated){
  103. // The data directory used to be <installation directory>/data.
  104. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("/home/u/.v-gears/data", "", "/def"), "/home/u/.v-gears");
  105. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("/home/u/.v-gears/data/", "/other", "/def"), "/home/u/.v-gears");
  106. // A data directory that isn't called "data" is kept as is.
  107. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("/games/ff7", "", "/def"), "/games/ff7");
  108. // Without a data directory the configuration directory is used, and then the default.
  109. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("", "/home/u/.v-gears", "/def"), "/home/u/.v-gears");
  110. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("", "", "/def"), "/def");
  111. }
  112. BOOST_AUTO_TEST_SUITE_END()
  113. // ---------------------------------------------------------------------------------------------------------------
  114. // Options: dependencies, presets, estimates.
  115. // ---------------------------------------------------------------------------------------------------------------
  116. BOOST_AUTO_TEST_SUITE(InstallOptionsTests)
  117. BOOST_AUTO_TEST_CASE(ModelsNeedTheirMaps){
  118. InstallOptions o;
  119. o.field_models = true;
  120. o.wm_models = true;
  121. InstallRules::ApplyDependencies(o);
  122. BOOST_CHECK(!o.field_models);
  123. BOOST_CHECK(!o.wm_models);
  124. o.fields = o.wm = o.field_models = o.wm_models = true;
  125. InstallRules::ApplyDependencies(o);
  126. BOOST_CHECK(o.field_models);
  127. BOOST_CHECK(o.wm_models);
  128. }
  129. BOOST_AUTO_TEST_CASE(OneMapDoesNotEnableTheOtherModels){
  130. InstallOptions o;
  131. o.fields = true;
  132. o.wm_models = true;
  133. InstallRules::ApplyDependencies(o);
  134. BOOST_CHECK(!o.wm_models);
  135. }
  136. BOOST_AUTO_TEST_CASE(SelectableMatchesDependencies){
  137. // An option is selectable exactly when applying the rules to it keeps it.
  138. for (int bits = 0; bits < (1 << 2); ++ bits){
  139. InstallOptions o;
  140. o.fields = bits & 1;
  141. o.wm = bits & 2;
  142. o.field_models = o.wm_models = true;
  143. const InstallOptions selectable = InstallRules::Selectable(o);
  144. InstallRules::ApplyDependencies(o);
  145. BOOST_CHECK_EQUAL(o.field_models, selectable.field_models);
  146. BOOST_CHECK_EQUAL(o.wm_models, selectable.wm_models);
  147. }
  148. }
  149. BOOST_AUTO_TEST_CASE(PresetsRespectTheRules){
  150. for (InstallPreset preset : {
  151. InstallPreset::EVERYTHING, InstallPreset::FIELDS, InstallPreset::WORLD_MAP, InstallPreset::BATTLE,
  152. InstallPreset::MEDIA, InstallPreset::CUSTOM
  153. }){
  154. for (bool videos : {false, true}){
  155. InstallOptions o = InstallRules::Preset(preset, videos);
  156. InstallOptions checked = o;
  157. InstallRules::ApplyDependencies(checked);
  158. BOOST_CHECK(o.field_models == checked.field_models && o.wm_models == checked.wm_models);
  159. // Videos are only chosen when the disk has them.
  160. if (!videos) BOOST_CHECK(!o.videos);
  161. }
  162. }
  163. }
  164. BOOST_AUTO_TEST_CASE(PresetContents){
  165. const InstallOptions all = InstallRules::Preset(InstallPreset::EVERYTHING, true);
  166. BOOST_CHECK(all.battle_data && all.battle_models && all.kernel && all.images && all.sounds && all.music);
  167. BOOST_CHECK(all.fields && all.field_models && all.wm && all.wm_models && all.videos);
  168. const InstallOptions world = InstallRules::Preset(InstallPreset::WORLD_MAP, true);
  169. BOOST_CHECK(world.wm && world.wm_models);
  170. BOOST_CHECK(!world.fields && !world.field_models && !world.music && !world.videos);
  171. const InstallOptions fields = InstallRules::Preset(InstallPreset::FIELDS, true);
  172. BOOST_CHECK(fields.fields && fields.field_models && !fields.wm);
  173. const InstallOptions none = InstallRules::Preset(InstallPreset::CUSTOM, true);
  174. BOOST_CHECK(!none.fields && !none.wm && !none.music && !none.sounds && !none.videos);
  175. }
  176. BOOST_AUTO_TEST_CASE(ToolsNeededByTheAudio){
  177. InstallOptions none;
  178. BOOST_CHECK(!InstallRules::ToolsNeeded(none).ffmpeg && !InstallRules::ToolsNeeded(none).timidity);
  179. InstallOptions sounds;
  180. sounds.sounds = true;
  181. BOOST_CHECK(InstallRules::ToolsNeeded(sounds).ffmpeg);
  182. BOOST_CHECK(!InstallRules::ToolsNeeded(sounds).timidity);
  183. // The music needs both: TiMidity renders the MIDI and FFmpeg encodes it.
  184. InstallOptions music;
  185. music.music = true;
  186. BOOST_CHECK(InstallRules::ToolsNeeded(music).ffmpeg && InstallRules::ToolsNeeded(music).timidity);
  187. // Nothing else needs them.
  188. InstallOptions other;
  189. other.fields = other.field_models = other.wm = other.battle_data = other.images = other.kernel = true;
  190. BOOST_CHECK(!InstallRules::ToolsNeeded(other).ffmpeg && !InstallRules::ToolsNeeded(other).timidity);
  191. }
  192. BOOST_AUTO_TEST_CASE(WhatIsLostWithoutTheTools){
  193. InstallOptions all;
  194. all.sounds = all.music = true;
  195. // Nothing is lost when the programs work, or when nothing needs them.
  196. BOOST_CHECK(InstallRules::ToolConsequences(all, true, true).empty());
  197. BOOST_CHECK(InstallRules::ToolConsequences(InstallOptions(), false, false).empty());
  198. // Without FFmpeg, both the sounds and the music are lost.
  199. const auto no_ffmpeg = InstallRules::ToolConsequences(all, false, true);
  200. BOOST_REQUIRE_EQUAL(no_ffmpeg.size(), 2u);
  201. BOOST_CHECK(no_ffmpeg[0].find("sound") != std::string::npos);
  202. BOOST_CHECK(no_ffmpeg[1].find("music") != std::string::npos);
  203. // Without TiMidity only the music is lost.
  204. const auto no_timidity = InstallRules::ToolConsequences(all, true, false);
  205. BOOST_REQUIRE_EQUAL(no_timidity.size(), 1u);
  206. BOOST_CHECK(no_timidity[0].find("music") != std::string::npos);
  207. BOOST_CHECK(no_timidity[0].find("TiMidity") != std::string::npos);
  208. // Without both it is not said twice.
  209. BOOST_CHECK_EQUAL(InstallRules::ToolConsequences(all, false, false).size(), 2u);
  210. // Sound effects don't need TiMidity.
  211. InstallOptions sounds;
  212. sounds.sounds = true;
  213. BOOST_CHECK(InstallRules::ToolConsequences(sounds, true, false).empty());
  214. BOOST_CHECK_EQUAL(InstallRules::ToolConsequences(sounds, false, true).size(), 1u);
  215. // Music alone, without FFmpeg.
  216. InstallOptions music;
  217. music.music = true;
  218. const auto music_only = InstallRules::ToolConsequences(music, false, true);
  219. BOOST_REQUIRE_EQUAL(music_only.size(), 1u);
  220. BOOST_CHECK(music_only[0].find("music") != std::string::npos);
  221. }
  222. BOOST_AUTO_TEST_CASE(WorldMapNeedsFields){
  223. InstallOptions o;
  224. o.wm = true;
  225. BOOST_CHECK_EQUAL(InstallRules::Problems(o, false).size(), 1u);
  226. // Fine if the fields are installed now or were installed before.
  227. BOOST_CHECK(InstallRules::Problems(o, true).empty());
  228. o.fields = true;
  229. BOOST_CHECK(InstallRules::Problems(o, false).empty());
  230. // No world map, no problem.
  231. BOOST_CHECK(InstallRules::Problems(InstallOptions(), false).empty());
  232. }
  233. BOOST_AUTO_TEST_CASE(Estimates){
  234. uint64_t temporary = 99;
  235. BOOST_CHECK_EQUAL(InstallRules::EstimateMegabytes(InstallOptions(), &temporary), 0u);
  236. BOOST_CHECK_EQUAL(temporary, 0u);
  237. // Selecting more never makes it smaller.
  238. const InstallOptions all = InstallRules::Preset(InstallPreset::EVERYTHING, false);
  239. uint64_t all_temporary = 0;
  240. const uint64_t all_size = InstallRules::EstimateMegabytes(all, &all_temporary);
  241. for (InstallPreset preset : {InstallPreset::FIELDS, InstallPreset::WORLD_MAP, InstallPreset::BATTLE, InstallPreset::MEDIA}){
  242. uint64_t t = 0;
  243. BOOST_CHECK_LE(InstallRules::EstimateMegabytes(InstallRules::Preset(preset, false), &t), all_size);
  244. BOOST_CHECK_LE(t, all_temporary);
  245. }
  246. // The pointer is optional.
  247. BOOST_CHECK_EQUAL(InstallRules::EstimateMegabytes(all), all_size);
  248. }
  249. BOOST_AUTO_TEST_SUITE_END()
  250. // ---------------------------------------------------------------------------------------------------------------
  251. // Verifier.
  252. // ---------------------------------------------------------------------------------------------------------------
  253. BOOST_AUTO_TEST_SUITE(InstallVerifierTests)
  254. BOOST_AUTO_TEST_CASE(EmptyDirectoryMissesEverything){
  255. TempDir dir;
  256. const VerifyResult result = VerifyInstallation(dir.Root());
  257. BOOST_CHECK(!result.Ok());
  258. for (const char* group : {
  259. "Configuration", "Game files", "Kernel data", "Battle data", "Battle models", "Field maps", "Field models",
  260. "World map", "World map models", "Sound effects", "Music", "Images", "Fonts"
  261. }) BOOST_CHECK_MESSAGE(HasProblem(result, group), "no problem reported for " << group);
  262. BOOST_CHECK_GT(result.checked, 0);
  263. }
  264. BOOST_AUTO_TEST_CASE(ConfigurationAndPlugins){
  265. TempDir dir;
  266. dir.Touch("plugins.cfg");
  267. dir.Touch("resources.cfg");
  268. BOOST_CHECK(HasProblem(VerifyInstallation(dir.Root()), "Configuration"));
  269. // The plugins have a version suffix.
  270. dir.Touch("RenderSystem_GL.so.1.12.10");
  271. dir.Touch("Codec_STBI.so.1.12.10");
  272. BOOST_CHECK(!HasProblem(VerifyInstallation(dir.Root()), "Configuration"));
  273. }
  274. BOOST_AUTO_TEST_CASE(RootWithoutTrailingSlash){
  275. TempDir dir;
  276. dir.Touch("plugins.cfg");
  277. const VerifyResult with = VerifyInstallation(dir.Root());
  278. const VerifyResult without = VerifyInstallation(dir.path.string());
  279. BOOST_CHECK_EQUAL(with.problems.size(), without.problems.size());
  280. }
  281. BOOST_AUTO_TEST_CASE(FilesOfTheDataFolderMustBeCopied){
  282. TempDir installation;
  283. TempDir reference;
  284. reference.Touch("plugins.cfg");
  285. reference.Touch("data/scripts/a.lua");
  286. BOOST_CHECK(HasProblem(VerifyInstallation(installation.Root(), reference.Root()), "Copied data files"));
  287. installation.Touch("plugins.cfg");
  288. BOOST_CHECK(HasProblem(VerifyInstallation(installation.Root(), reference.Root()), "Copied data files"));
  289. installation.Touch("data/scripts/a.lua");
  290. BOOST_CHECK(!HasProblem(VerifyInstallation(installation.Root(), reference.Root()), "Copied data files"));
  291. // Without a reference there is nothing to compare.
  292. BOOST_CHECK(!HasProblem(VerifyInstallation(installation.Root()), "Copied data files"));
  293. }
  294. BOOST_AUTO_TEST_CASE(CountsFilesInsteadOfJustLookingForOne){
  295. TempDir dir;
  296. for (int i = 0; i < 3; ++ i) dir.Touch("data/fields/f" + std::to_string(i) + "/map.xml");
  297. // Three fields are not an installation of the fields.
  298. BOOST_CHECK(HasProblem(VerifyInstallation(dir.Root()), "Field maps"));
  299. }
  300. BOOST_AUTO_TEST_SUITE_END()
  301. // ---------------------------------------------------------------------------------------------------------------
  302. // Log.
  303. // ---------------------------------------------------------------------------------------------------------------
  304. BOOST_AUTO_TEST_SUITE(InstallLogTests)
  305. BOOST_AUTO_TEST_CASE(Severity){
  306. BOOST_CHECK_EQUAL(InstallLog::Classify("[ERROR] Skip field: x"), InstallLog::ERROR_LEVEL);
  307. BOOST_CHECK_EQUAL(InstallLog::Classify("Exception: Bone not found"), InstallLog::ERROR_LEVEL);
  308. BOOST_CHECK_EQUAL(InstallLog::Classify("[WARNING] Texture not found"), InstallLog::WARNING);
  309. BOOST_CHECK_EQUAL(InstallLog::Classify("Converting field models..."), InstallLog::INFO);
  310. BOOST_CHECK_EQUAL(InstallLog::Classify(""), InstallLog::INFO);
  311. }
  312. BOOST_AUTO_TEST_CASE(CountsFileAndSink){
  313. TempDir dir;
  314. InstallLog& log = InstallLog::Get();
  315. std::vector<std::string> shown;
  316. log.Open(dir.Root() + "installer.log");
  317. log.SetSink([&shown](InstallLog::Level, const std::string& message){shown.push_back(message);});
  318. log.Add("Starting");
  319. log.Add("[WARNING] careful");
  320. log.Add("[ERROR] broken");
  321. // The same problem reported twice in a row counts once.
  322. log.Add("[ERROR] broken");
  323. log.Add("hidden", false);
  324. // What the installer prints is captured too, and only problems reach the sink.
  325. std::cerr << "[ERROR] printed" << std::endl;
  326. std::cout << "plain output" << std::endl;
  327. BOOST_CHECK_EQUAL(log.Count(InstallLog::ERROR_LEVEL), 2u);
  328. BOOST_CHECK_EQUAL(log.Count(InstallLog::WARNING), 1u);
  329. BOOST_CHECK(Contains(shown, "Starting"));
  330. BOOST_CHECK(!Contains(shown, "hidden"));
  331. BOOST_CHECK(!Contains(shown, "plain output"));
  332. BOOST_CHECK(Contains(shown, "[ERROR] printed"));
  333. BOOST_REQUIRE_EQUAL(log.First(InstallLog::ERROR_LEVEL, 1).size(), 1u);
  334. BOOST_CHECK_EQUAL(log.First(InstallLog::ERROR_LEVEL, 1)[0], "[ERROR] broken");
  335. log.SetSink(nullptr);
  336. log.Close();
  337. std::ifstream file(dir.Root() + "installer.log");
  338. std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  339. BOOST_CHECK(content.find("careful") != std::string::npos);
  340. BOOST_CHECK(content.find("hidden") != std::string::npos);
  341. BOOST_CHECK(content.find("plain output") != std::string::npos);
  342. }
  343. BOOST_AUTO_TEST_SUITE_END()
  344. // ---------------------------------------------------------------------------------------------------------------
  345. // Steps of the log.
  346. // ---------------------------------------------------------------------------------------------------------------
  347. BOOST_AUTO_TEST_SUITE(InstallLogStepTests)
  348. BOOST_AUTO_TEST_CASE(StepsReportTheirOwnProblems){
  349. TempDir dir;
  350. InstallLog& log = InstallLog::Get();
  351. std::vector<std::string> shown;
  352. log.Open(dir.Root() + "installer.log");
  353. log.SetSink([&shown](InstallLog::Level, const std::string& message){shown.push_back(message);});
  354. log.BeginStep("Clean step");
  355. log.Add("fine");
  356. log.BeginStep("Bad step");
  357. log.Add("[ERROR] one");
  358. log.Add("[ERROR] two");
  359. log.Add("[WARNING] three");
  360. log.BeginStep("Next step");
  361. log.Add("[WARNING] four");
  362. log.EndStep();
  363. log.SetSink(nullptr);
  364. log.Close();
  365. auto summary = [&shown](const std::string& step){
  366. for (const std::string& line : shown) if (line.find("\"" + step + "\" finished") != std::string::npos) return line;
  367. return std::string();
  368. };
  369. // Steps without problems are not mentioned, the others only count their own.
  370. BOOST_CHECK(summary("Clean step").empty());
  371. BOOST_CHECK(summary("Bad step").find("2 error(s) and 1 warning(s)") != std::string::npos);
  372. BOOST_CHECK(summary("Next step").find("0 error(s) and 1 warning(s)") != std::string::npos);
  373. // The summaries don't count as problems themselves.
  374. BOOST_CHECK_EQUAL(InstallLog::Classify(summary("Bad step")), InstallLog::INFO);
  375. }
  376. BOOST_AUTO_TEST_SUITE_END()
  377. // ---------------------------------------------------------------------------------------------------------------
  378. // Task pool.
  379. // ---------------------------------------------------------------------------------------------------------------
  380. BOOST_AUTO_TEST_SUITE(TaskPoolTests)
  381. BOOST_AUTO_TEST_CASE(RunsEveryTask){
  382. std::atomic<int> done(0);
  383. TaskPool pool(4);
  384. for (int i = 0; i < 200; ++ i) pool.Run([&done](){++ done;});
  385. pool.Wait();
  386. BOOST_CHECK_EQUAL(done.load(), 200);
  387. }
  388. BOOST_AUTO_TEST_CASE(WaitWithoutTasksReturns){
  389. TaskPool pool(2);
  390. pool.Wait();
  391. BOOST_CHECK_EQUAL(pool.Threads(), 2u);
  392. }
  393. BOOST_AUTO_TEST_CASE(RunsInParallel){
  394. // Tasks that wait for each other can only finish if they really run at the same time.
  395. std::atomic<int> arrived(0);
  396. TaskPool pool(3);
  397. for (int i = 0; i < 3; ++ i){
  398. pool.Run([&arrived](){
  399. ++ arrived;
  400. const auto limit = std::chrono::steady_clock::now() + std::chrono::seconds(5);
  401. while (arrived < 3 && std::chrono::steady_clock::now() < limit) std::this_thread::yield();
  402. });
  403. }
  404. pool.Wait();
  405. BOOST_CHECK_EQUAL(arrived.load(), 3);
  406. }
  407. BOOST_AUTO_TEST_CASE(UsesSeveralThreads){
  408. std::mutex mutex;
  409. std::set<std::thread::id> threads;
  410. TaskPool pool(4);
  411. for (int i = 0; i < 50; ++ i){
  412. pool.Run([&](){
  413. std::this_thread::sleep_for(std::chrono::milliseconds(5));
  414. std::lock_guard<std::mutex> lock(mutex);
  415. threads.insert(std::this_thread::get_id());
  416. });
  417. }
  418. pool.Wait();
  419. BOOST_CHECK_GT(threads.size(), 1u);
  420. BOOST_CHECK_LE(threads.size(), 4u);
  421. }
  422. BOOST_AUTO_TEST_CASE(DestructorFinishesPendingTasks){
  423. std::atomic<int> done(0);
  424. {
  425. TaskPool pool(2);
  426. for (int i = 0; i < 20; ++ i) pool.Run([&done](){
  427. std::this_thread::sleep_for(std::chrono::milliseconds(2));
  428. ++ done;
  429. });
  430. }
  431. BOOST_CHECK_EQUAL(done.load(), 20);
  432. }
  433. BOOST_AUTO_TEST_CASE(ATaskThatThrowsDoesNotStopThePool){
  434. std::atomic<int> done(0);
  435. TaskPool pool(2);
  436. pool.Run([](){throw std::runtime_error("boom");});
  437. for (int i = 0; i < 10; ++ i) pool.Run([&done](){++ done;});
  438. pool.Wait();
  439. BOOST_CHECK_EQUAL(done.load(), 10);
  440. }
  441. BOOST_AUTO_TEST_CASE(DefaultNumberOfThreadsIsReasonable){
  442. TaskPool pool;
  443. BOOST_CHECK_GE(pool.Threads(), 1u);
  444. BOOST_CHECK_LE(pool.Threads(), 8u);
  445. }
  446. BOOST_AUTO_TEST_SUITE_END()
  447. // ---------------------------------------------------------------------------------------------------------------
  448. // Videos of the game disks.
  449. // ---------------------------------------------------------------------------------------------------------------
  450. BOOST_AUTO_TEST_SUITE(VideoFilesTests)
  451. BOOST_AUTO_TEST_CASE(RecognizesMovies){
  452. std::string name;
  453. BOOST_CHECK(VideoFiles::IsMovie("ff7/movies/bike.avi", &name));
  454. BOOST_CHECK_EQUAL(name, "bike.avi");
  455. // The disks mix the case, and ISO9660 adds version numbers and dots.
  456. BOOST_CHECK(VideoFiles::IsMovie("FF7/MOVIES/SQLOGO.AVI;1", &name));
  457. BOOST_CHECK_EQUAL(name, "sqlogo.avi");
  458. BOOST_CHECK(VideoFiles::IsMovie("/ff7/Movies/Explode.avi", &name));
  459. BOOST_CHECK_EQUAL(name, "explode.avi");
  460. BOOST_CHECK(VideoFiles::IsMovie("ff7\\movies\\ending1.avi", &name));
  461. BOOST_CHECK_EQUAL(name, "ending1.avi");
  462. // Other files of the folder are copied too (the camera data of the movies).
  463. BOOST_CHECK(VideoFiles::IsMovie("ff7/movies/moviecam.lgp", &name));
  464. BOOST_CHECK_EQUAL(name, "moviecam.lgp");
  465. }
  466. BOOST_AUTO_TEST_CASE(IgnoresEverythingElse){
  467. BOOST_CHECK(!VideoFiles::IsMovie("ff7/movies"));
  468. BOOST_CHECK(!VideoFiles::IsMovie("ff7/movies/"));
  469. BOOST_CHECK(!VideoFiles::IsMovie("ff7/movies/sub/bike.avi"));
  470. BOOST_CHECK(!VideoFiles::IsMovie("ff7/field/char.lgp"));
  471. BOOST_CHECK(!VideoFiles::IsMovie("ff7/moviesx/bike.avi"));
  472. BOOST_CHECK(!VideoFiles::IsMovie("data/movies/bike.avi"));
  473. BOOST_CHECK(!VideoFiles::IsMovie(""));
  474. std::string name = "untouched";
  475. BOOST_CHECK(!VideoFiles::IsMovie("ff7/wm/x.avi", &name));
  476. BOOST_CHECK_EQUAL(name, "untouched");
  477. }
  478. BOOST_AUTO_TEST_CASE(NormalizesPaths){
  479. BOOST_CHECK_EQUAL(VideoFiles::NormalizePath("/FF7/Movies/A.AVI;1"), "ff7/movies/a.avi");
  480. BOOST_CHECK_EQUAL(VideoFiles::NormalizePath("FF7/MOVIES/A."), "ff7/movies/a");
  481. // A semicolon that is not a version stays.
  482. BOOST_CHECK_EQUAL(VideoFiles::NormalizePath("a;b"), "a;b");
  483. }
  484. BOOST_AUTO_TEST_CASE(LogCanBeAppended){
  485. TempDir dir;
  486. InstallLog& log = InstallLog::Get();
  487. log.Open(dir.Root() + "installer.log");
  488. log.Add("first run");
  489. log.Close();
  490. log.Open(dir.Root() + "installer.log", true);
  491. log.Add("second run");
  492. log.Close();
  493. std::ifstream file(dir.Root() + "installer.log");
  494. std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  495. BOOST_CHECK(content.find("first run") != std::string::npos);
  496. BOOST_CHECK(content.find("second run") != std::string::npos);
  497. // Without append the file starts over.
  498. log.Open(dir.Root() + "installer.log");
  499. log.Close();
  500. std::ifstream again(dir.Root() + "installer.log");
  501. std::string content2((std::istreambuf_iterator<char>(again)), std::istreambuf_iterator<char>());
  502. BOOST_CHECK(content2.empty());
  503. }
  504. BOOST_AUTO_TEST_SUITE_END()
  505. // Command line.
  506. BOOST_AUTO_TEST_CASE(CommandLineWithoutInstallerOptionsOpensTheWindow){
  507. using namespace InstallCommandLine;
  508. BOOST_CHECK(Parse({}).mode == Mode::GUI);
  509. // Qt's own arguments are left for the window.
  510. BOOST_CHECK(Parse({"-platform", "xcb"}).mode == Mode::GUI);
  511. }
  512. BOOST_AUTO_TEST_CASE(CommandLineInstallDefaults){
  513. using namespace InstallCommandLine;
  514. const Arguments a = Parse({"--cli", "--iso", "disk.iso"});
  515. BOOST_REQUIRE(a.mode == Mode::INSTALL);
  516. BOOST_CHECK_EQUAL(a.iso, "disk.iso");
  517. BOOST_CHECK(a.output.empty());
  518. BOOST_CHECK(a.copy_data);
  519. BOOST_CHECK(a.keep_originals);
  520. BOOST_CHECK(!a.yes);
  521. // Everything, except the videos, which are not on the install disk.
  522. const InstallOptions o = a.options;
  523. BOOST_CHECK(o.kernel && o.fields && o.field_models && o.wm && o.wm_models && o.battle_data && o.battle_models);
  524. BOOST_CHECK(o.images && o.sounds && o.music);
  525. BOOST_CHECK(!o.videos);
  526. }
  527. BOOST_AUTO_TEST_CASE(CommandLineInstallerOptionsImplyCli){
  528. using namespace InstallCommandLine;
  529. const Arguments a = Parse({"-i", "disk.iso", "-o", "/tmp/vg", "-y", "--no-copy-data", "--discard-originals"});
  530. BOOST_REQUIRE(a.mode == Mode::INSTALL);
  531. BOOST_CHECK_EQUAL(a.output, "/tmp/vg");
  532. BOOST_CHECK(a.yes);
  533. BOOST_CHECK(!a.copy_data);
  534. BOOST_CHECK(!a.keep_originals);
  535. // "--option=value" works too.
  536. BOOST_CHECK_EQUAL(Parse({"--iso=other.iso"}).iso, "other.iso");
  537. }
  538. BOOST_AUTO_TEST_CASE(CommandLinePresetsAndParts){
  539. using namespace InstallCommandLine;
  540. Arguments a = Parse({"-i", "d.iso", "--preset", "battle", "--with", "music,images", "--without", "kernel"});
  541. BOOST_REQUIRE(a.mode == Mode::INSTALL);
  542. BOOST_CHECK(a.options.battle_data && a.options.battle_models && a.options.music && a.options.images);
  543. BOOST_CHECK(!a.options.kernel && !a.options.fields && !a.options.sounds);
  544. a = Parse({"-i", "d.iso", "-p", "custom", "--with", "fields"});
  545. BOOST_REQUIRE(a.mode == Mode::INSTALL);
  546. BOOST_CHECK(a.options.fields && !a.options.field_models && !a.options.kernel);
  547. // Removing a part removes what depends on it.
  548. a = Parse({"-i", "d.iso", "--without", "world-map"});
  549. BOOST_REQUIRE(a.mode == Mode::INSTALL);
  550. BOOST_CHECK(!a.options.wm && !a.options.wm_models);
  551. }
  552. BOOST_AUTO_TEST_CASE(CommandLineErrors){
  553. using namespace InstallCommandLine;
  554. BOOST_CHECK(Parse({"--cli"}).mode == Mode::INVALID);
  555. BOOST_CHECK(Parse({"--iso"}).mode == Mode::INVALID);
  556. BOOST_CHECK(Parse({"-i", "d.iso", "--preset", "nope"}).mode == Mode::INVALID);
  557. BOOST_CHECK(Parse({"-i", "d.iso", "--with", "nope"}).mode == Mode::INVALID);
  558. BOOST_CHECK(Parse({"-i", "d.iso", "--bogus"}).mode == Mode::INVALID);
  559. // A part can't be added without what it needs.
  560. BOOST_CHECK(Parse({"-i", "d.iso", "-p", "custom", "--with", "field-models"}).mode == Mode::INVALID);
  561. BOOST_CHECK(Parse({"--verify", "-i", "d.iso"}).mode == Mode::INVALID);
  562. BOOST_CHECK(Parse({"--verify", "--with", "music"}).mode == Mode::INVALID);
  563. }
  564. BOOST_AUTO_TEST_CASE(CommandLineOtherModes){
  565. using namespace InstallCommandLine;
  566. BOOST_CHECK(Parse({"--verify"}).mode == Mode::VERIFY);
  567. BOOST_CHECK_EQUAL(Parse({"--verify", "-o", "/x"}).output, "/x");
  568. BOOST_CHECK(Parse({"--help"}).mode == Mode::HELP);
  569. BOOST_CHECK(Parse({"-i", "d.iso", "-h"}).mode == Mode::HELP);
  570. BOOST_CHECK(Parse({"--version"}).mode == Mode::VERSION);
  571. BOOST_CHECK(Parse({"--list-parts"}).mode == Mode::LIST_PARTS);
  572. BOOST_CHECK(!Usage("v-gears-installer").empty());
  573. }