reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

source.cpp (6497B)


      1 #include "helper.hpp"
      2 
      3 #include <index.hpp>
      4 #include <source.hpp>
      5 #include <version.hpp>
      6 
      7 #include <errors.hpp>
      8 
      9 static const char *M = "[source]";
     10 
     11 #define MAKE_VERSION \
     12   Index ri("Index Name"); \
     13   Category cat("Category Name", &ri); \
     14   Package pkg(Package::DataType, "Package Name", &cat); \
     15   Version ver("1.0", &pkg);
     16 
     17 TEST_CASE("source platform", M) {
     18   MAKE_VERSION;
     19 
     20   Source src({}, "url", &ver);
     21   REQUIRE(src.platform() == Platform::Generic);
     22 
     23   src.setPlatform(Platform::Unknown);
     24   REQUIRE(src.platform() == Platform::Unknown);
     25 
     26   src.setPlatform(Platform::Windows_Any);
     27   REQUIRE(src.platform() == Platform::Windows_Any);
     28 }
     29 
     30 TEST_CASE("source type override", M) {
     31   MAKE_VERSION;
     32 
     33   Source src({}, "url", &ver);
     34   REQUIRE(src.type() == Package::DataType);
     35   REQUIRE(src.typeOverride() == Package::UnknownType);
     36 
     37   src.setTypeOverride(Package::ScriptType);
     38   REQUIRE(src.type() == Package::ScriptType);
     39   REQUIRE(src.typeOverride() == Package::ScriptType);
     40 }
     41 
     42 TEST_CASE("source type from package", M) {
     43   MAKE_VERSION;
     44   Source src({}, "url", &ver);
     45 
     46   REQUIRE(src.version() == &ver);
     47 
     48   REQUIRE(src.type() == Package::DataType);
     49 
     50   src.setTypeOverride(Package::ScriptType);
     51   REQUIRE(src.type() == Package::ScriptType);
     52 }
     53 
     54 TEST_CASE("parse file section", M) {
     55   REQUIRE(Source::getSection("true") == -1);
     56   REQUIRE(Source::getSection("hello") == Source::UnknownSection);
     57   REQUIRE(Source::getSection("main") == Source::MainSection);
     58   REQUIRE(Source::getSection("midi_editor") == Source::MIDIEditorSection);
     59   REQUIRE(Source::getSection("midi_inlineeditor") == Source::MIDIInlineEditorSection);
     60   REQUIRE(Source::getSection("midi_eventlisteditor") == Source::MIDIEventListEditorSection);
     61   REQUIRE(Source::getSection("mediaexplorer") == Source::MediaExplorerSection);
     62 }
     63 
     64 TEST_CASE("explicit source section", M) {
     65   MAKE_VERSION;
     66 
     67   SECTION("script type") {
     68     Source source("filename", "url", &ver);
     69     REQUIRE(source.sections() == 0);
     70 
     71     source.setTypeOverride(Package::ScriptType);
     72     CHECK(source.type() == Package::ScriptType);
     73 
     74     source.setSections(Source::MainSection | Source::MIDIEditorSection);
     75     REQUIRE(source.sections() == (Source::MainSection | Source::MIDIEditorSection));
     76   }
     77 
     78   SECTION("other type") {
     79     Source source("filename", "url", &ver);
     80     source.setSections(Source::MainSection);
     81     CHECK(source.type() == Package::DataType);
     82     REQUIRE(source.sections() == 0);
     83   }
     84 }
     85 
     86 TEST_CASE("implicit section detection (v1.0 compatibility)", M) {
     87   REQUIRE(Source::MainSection == Source::detectSection(Path{"Hello World"}));
     88   REQUIRE(Source::MainSection == Source::detectSection(Path{"Hello/World"}));
     89   REQUIRE(Source::MainSection == Source::detectSection(Path{"Hello/midi editor"}));
     90 
     91   REQUIRE(Source::MIDIEditorSection == Source::detectSection(Path{"midi editor"}));
     92   REQUIRE(Source::MIDIEditorSection == Source::detectSection(Path{"midi editor/Hello"}));
     93 }
     94 
     95 TEST_CASE("implicit section detection from source (v1.0 compatibility)") {
     96   Index ri("Index Name");
     97 
     98   SECTION("main") {
     99     Category cat("Category Name", &ri);
    100     Package pack(Package::ScriptType, "package name", &cat);
    101     Version ver("1.0", &pack);
    102 
    103     Source source("filename", "url", &ver);
    104     source.setSections(Source::ImplicitSection);
    105     REQUIRE(source.sections() == Source::MainSection);
    106   }
    107 
    108   SECTION("midi editor") {
    109     Category cat("MIDI Editor", &ri);
    110     Package pack(Package::ScriptType, "package name", &cat);
    111     Version ver("1.0", &pack);
    112 
    113     Source source("filename", "url", &ver);
    114     source.setSections(Source::ImplicitSection);
    115     REQUIRE(source.sections() == Source::MIDIEditorSection);
    116   }
    117 }
    118 
    119 TEST_CASE("empty source url", M) {
    120   MAKE_VERSION;
    121 
    122   auto test = [ver] {
    123     const Source source("filename", {}, &ver);
    124   };
    125 
    126   CHECK_THROWS_AS(test(), reapack_error);
    127   REQUIRE_THROWS_WITH(test(), "empty source url");
    128 }
    129 
    130 TEST_CASE("source target path", M) {
    131   MAKE_VERSION;
    132 
    133   constexpr std::pair<Package::Type, const char *> tests[] {
    134     {Package::ScriptType,          "Scripts/Index Name/Category Name/file.name"},
    135     {Package::EffectType,          "Effects/Index Name/Category Name/file.name"},
    136     {Package::ExtensionType,       "UserPlugins/file.name"},
    137     {Package::DataType,            "Data/file.name"},
    138     {Package::ThemeType,           "ColorThemes/file.name"},
    139     {Package::LangPackType,        "LangPack/file.name"},
    140     {Package::WebInterfaceType,    "reaper_www_root/file.name"},
    141     {Package::ProjectTemplateType, "ProjectTemplates/file.name"},
    142     {Package::TrackTemplateType,   "TrackTemplates/file.name"},
    143     {Package::MIDINoteNamesType,   "MIDINoteNames/file.name"},
    144     {Package::AutomationItemType,  "AutomationItems/Index Name/Category Name/file.name"},
    145   };
    146 
    147   Source source("file.name", "url", &ver);
    148   for(const auto &[type, path] : tests) {
    149     source.setTypeOverride(type);
    150     REQUIRE(source.targetPath() == Path(path));
    151   }
    152 }
    153 
    154 TEST_CASE("target path with parent directory traversal", M) {
    155   Index ri("Index Name");
    156   Category cat("Category Name", &ri);
    157   Package pack(Package::ScriptType, "package name", &cat);
    158   Version ver("1.0", &pack);
    159   Source source("../../../file.name", "url", &ver);
    160 
    161   SECTION("script") {
    162     Path expected;
    163     expected.append("Scripts");
    164     expected.append("Index Name");
    165     // expected.append("Category Name"); // only the category can be bypassed!
    166     expected.append("file.name");
    167 
    168     REQUIRE(source.targetPath() == expected);
    169   }
    170 
    171   SECTION("extension") {
    172     Path expected;
    173     expected.append("UserPlugins");
    174     expected.append("file.name");
    175     source.setTypeOverride(Package::ExtensionType);
    176     REQUIRE(source.targetPath() == expected);
    177   }
    178 }
    179 
    180 TEST_CASE("target path for unknown package type", M) {
    181   Index ri("name");
    182   Category cat("name", &ri);
    183   Package pack(Package::UnknownType, "a", &cat);
    184   Version ver("1.0", &pack);
    185   Source src({}, "url", &ver);
    186 
    187   REQUIRE(src.targetPath().empty());
    188 }
    189 
    190 TEST_CASE("directory traversal in category name", M) {
    191   Index ri("Remote Name");
    192   Category cat("../..", &ri);
    193   Package pack(Package::ScriptType, "file.name", &cat);
    194   Version ver("1.0", &pack);
    195   Source src({}, "url", &ver);
    196 
    197   Path expected;
    198   expected.append("Scripts");
    199   expected.append("Remote Name");
    200   expected.append("file.name");
    201 
    202   REQUIRE(src.targetPath() == expected);
    203 }
    204 
    205 TEST_CASE("source checksum", M) {
    206   MAKE_VERSION;
    207 
    208   Source src({}, "url", &ver);
    209   REQUIRE(src.checksum().empty());
    210 
    211   src.setChecksum("hello world");
    212   REQUIRE(src.checksum() == "hello world");
    213 }