DiskImage.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include <fstream>
  16. #include <boost/filesystem.hpp>
  17. #include <boost/test/unit_test.hpp>
  18. #include "installer/formats/DiskIdentifier.h"
  19. #include "installer/formats/DiskPaths.h"
  20. #include "installer/formats/DiskReleases.h"
  21. #include "installer/formats/Iso9660Reader.h"
  22. namespace{
  23. /**
  24. * A folder that is removed when the test ends.
  25. */
  26. struct TempFolder{
  27. const boost::filesystem::path path
  28. = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
  29. TempFolder(){boost::filesystem::create_directories(path);}
  30. ~TempFolder(){boost::filesystem::remove_all(path);}
  31. };
  32. /**
  33. * Adds a folder record to a sector of an image.
  34. */
  35. void AddRecord(
  36. std::vector<char>& image, size_t& offset, const std::string& name, const uint32_t sector,
  37. const uint32_t size, const bool directory
  38. ){
  39. const size_t length = 33 + name.size() + (name.size() % 2 == 0 ? 1 : 0);
  40. image[offset] = static_cast<char>(length);
  41. for (int byte = 0; byte < 4; ++ byte){
  42. image[offset + 2 + byte] = static_cast<char>(sector >> (8 * byte));
  43. image[offset + 10 + byte] = static_cast<char>(size >> (8 * byte));
  44. }
  45. image[offset + 25] = directory ? 2 : 0;
  46. image[offset + 32] = static_cast<char>(name.size());
  47. std::copy(name.begin(), name.end(), image.begin() + offset + 33);
  48. offset += length;
  49. }
  50. }
  51. BOOST_AUTO_TEST_CASE(TestDiskReleases){
  52. const DiskReleases::Release* install = DiskReleases::FindPcRelease("FF7INST.EXE");
  53. BOOST_REQUIRE(install);
  54. BOOST_CHECK_EQUAL(install->disk, DiskImage::DISK_INSTALL);
  55. const DiskReleases::Release* disk_2 = DiskReleases::FindPcRelease("FF7/MOVIES/BIGLIGHT.AVI");
  56. BOOST_REQUIRE(disk_2);
  57. BOOST_CHECK_EQUAL(disk_2->disk, DiskImage::DISK_2);
  58. BOOST_CHECK(!DiskReleases::FindPcRelease("FF7/MOVIES/OTHER.AVI"));
  59. const DiskReleases::Release* french = DiskReleases::FindPlayStationRelease("SCES-10868");
  60. BOOST_REQUIRE(french);
  61. BOOST_CHECK_EQUAL(french->platform, DiskImage::PLATFORM_PS1);
  62. BOOST_CHECK_EQUAL(french->language, DiskImage::LANGUAGE_FRENCH);
  63. BOOST_CHECK_EQUAL(french->disk, DiskImage::DISK_2);
  64. BOOST_CHECK_EQUAL(std::string(french->warning), "French language is not fully supported in V-Gears.");
  65. BOOST_CHECK(!DiskReleases::FindPlayStationRelease("SCUS-00000"));
  66. }
  67. BOOST_AUTO_TEST_CASE(TestDiskPathsAreNormalized){
  68. BOOST_CHECK_EQUAL(DiskIdentifier::NormalizePath("/ff7\\movies/bike.avi;1 \n"), "FF7/MOVIES/BIKE.AVI");
  69. BOOST_CHECK_EQUAL(DiskIdentifier::RemoveVersion("Readme.;1"), "Readme");
  70. BOOST_CHECK_EQUAL(DiskIdentifier::RemoveVersion("a;b"), "a;b");
  71. BOOST_CHECK_EQUAL(DiskIdentifier::FindGameId("BOOT = cdrom:\\SCUS_941.63;1"), "SCUS-94163");
  72. BOOST_CHECK_EQUAL(DiskIdentifier::FindGameId("BOOT = cdrom:\\PSX.EXE;1"), "");
  73. }
  74. BOOST_AUTO_TEST_CASE(TestDiskFilesAreFound){
  75. TempFolder folder;
  76. boost::filesystem::create_directories(folder.path / "DATA" / "WM");
  77. std::ofstream((folder.path / "DATA" / "WM" / "WORLD_US.LGP").string()) << "x";
  78. std::ofstream((folder.path / "FF7INST.EXE").string()) << "x";
  79. const std::string base = folder.path.string();
  80. const auto world = DiskPaths::Find(base, "data/wm/world_us.lgp");
  81. BOOST_REQUIRE(world);
  82. BOOST_CHECK(world->find("DATA/WM/WORLD_US.LGP") != std::string::npos);
  83. // A name that begins the same way is used if there is no other.
  84. BOOST_CHECK(DiskPaths::Find(base, "ff7.exe"));
  85. std::ofstream((folder.path / "FF7.EXE").string()) << "x";
  86. BOOST_CHECK(DiskPaths::Find(base, "ff7.exe")->find("FF7.EXE") != std::string::npos);
  87. BOOST_CHECK(!DiskPaths::Find(base, "data/wm/world_us.bin"));
  88. }
  89. BOOST_AUTO_TEST_CASE(TestIso9660FilesAreRead){
  90. // The descriptor in the sector 16, the root folder in the 17, a folder in the 18 and a file
  91. // in the 19.
  92. const size_t sector = Iso9660Reader::SECTOR_SIZE;
  93. std::vector<char> image(20 * sector, 0);
  94. image[16 * sector] = 1;
  95. const std::string standard = "CD001";
  96. std::copy(standard.begin(), standard.end(), image.begin() + 16 * sector + 1);
  97. size_t offset = 16 * sector + 156;
  98. AddRecord(image, offset, std::string(1, '\0'), 17, sector, true);
  99. offset = 17 * sector;
  100. AddRecord(image, offset, std::string(1, '\0'), 17, sector, true);
  101. AddRecord(image, offset, "..", 17, sector, true);
  102. AddRecord(image, offset, "DATA", 18, sector, true);
  103. offset = 18 * sector;
  104. AddRecord(image, offset, "FILE.TXT;1", 19, 5, false);
  105. const std::string text = "hello";
  106. std::copy(text.begin(), text.end(), image.begin() + 19 * sector);
  107. TempFolder folder;
  108. const std::string path = (folder.path / "image.iso").string();
  109. std::ofstream(path, std::ios::binary).write(image.data(), image.size());
  110. Iso9660Reader reader(path);
  111. BOOST_REQUIRE(reader.IsValid());
  112. std::vector<std::string> paths;
  113. std::string contents;
  114. const Iso9660Reader::Result result = reader.Walk(
  115. DiskIdentifier::RemoveVersion,
  116. [&](const Iso9660Reader::Entry& entry){
  117. paths.push_back(entry.path);
  118. if (!entry.directory){
  119. reader.Read(entry, [&contents](const char* data, size_t size){
  120. contents.append(data, size);
  121. return true;
  122. });
  123. }
  124. return true;
  125. }
  126. );
  127. // The ".." folder, which would lead outside, is skipped.
  128. BOOST_CHECK(result == Iso9660Reader::Result::COMPLETE);
  129. BOOST_REQUIRE_EQUAL(paths.size(), 2u);
  130. BOOST_CHECK_EQUAL(paths[0], "DATA");
  131. BOOST_CHECK_EQUAL(paths[1], "DATA/FILE.TXT");
  132. BOOST_CHECK_EQUAL(contents, "hello");
  133. // A file past the end of the image can't be read.
  134. Iso9660Reader::Entry missing{"x", false, 100, 5};
  135. BOOST_CHECK(!reader.Read(missing, [](const char*, size_t){return true;}));
  136. }