winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

PreIndexedPackageSource.cpp (6223B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "TestSource.h"
      5 #include "TestCommon.h"
      6 #include "TestSettings.h"
      7 #include <winget/RepositorySource.h>
      8 #include <AppInstallerRuntime.h>
      9 #include <AppInstallerStrings.h>
     10 #include <Microsoft/PreIndexedPackageSourceFactory.h>
     11 #include <winget/Settings.h>
     12 
     13 using namespace std::string_literals;
     14 using namespace std::string_view_literals;
     15 using namespace TestCommon;
     16 using namespace AppInstaller;
     17 using namespace AppInstaller::Repository;
     18 using namespace AppInstaller::Runtime;
     19 using namespace AppInstaller::Settings;
     20 using namespace AppInstaller::Utility;
     21 
     22 namespace fs = std::filesystem;
     23 
     24 constexpr std::string_view s_RepositorySettings_UserSources = "usersources"sv;
     25 
     26 constexpr std::string_view s_MsixFile_1 = "index.1.0.0.0.signed.msix";
     27 constexpr std::string_view s_MsixFile_2 = "index.2.0.0.0.signed.msix";
     28 constexpr std::string_view s_Msix_FamilyName = "AppInstallerCLITestsFakeIndex_8wekyb3d8bbwe";
     29 constexpr std::string_view s_IndexMsixName = "source.msix"sv;
     30 
     31 void CopyIndexFileToDirectory(const fs::path& from, const fs::path& to)
     32 {
     33     fs::path toFile = to;
     34     toFile /= s_IndexMsixName;
     35     if (fs::exists(toFile))
     36     {
     37         fs::remove(toFile);
     38     }
     39     fs::copy_file(from, toFile);
     40 }
     41 
     42 fs::path GetPathToFileDir()
     43 {
     44     fs::path result = GetPathTo(Runtime::PathName::LocalState);
     45     result /= AppInstaller::Repository::Microsoft::PreIndexedPackageSourceFactory::Type();
     46     result /= s_Msix_FamilyName;
     47     return result;
     48 }
     49 
     50 std::string GetContents(const fs::path& file)
     51 {
     52     REQUIRE(fs::exists(file));
     53     std::ifstream stream(file);
     54     return ReadEntireStream(stream);
     55 }
     56 
     57 void CleanSources()
     58 {
     59     RemoveSetting(Stream::UserSources);
     60     RemoveSetting(Stream::SourcesMetadata);
     61     fs::remove_all(GetPathToFileDir());
     62 }
     63 
     64 TEST_CASE("PIPS_Add", "[pips]")
     65 {
     66     if (!Runtime::IsRunningAsAdmin())
     67     {
     68         WARN("Test requires admin privilege. Skipped.");
     69         return;
     70     }
     71 
     72     CleanSources();
     73 
     74     TempDirectory dir("pipssource");
     75     TestDataFile index(s_MsixFile_1);
     76     CopyIndexFileToDirectory(index, dir);
     77 
     78     bool shouldCleanCert = InstallCertFromSignedPackage(index);
     79 
     80     SourceDetails details;
     81     details.Name = "TestName";
     82     details.Type = AppInstaller::Repository::Microsoft::PreIndexedPackageSourceFactory::Type();
     83     details.Arg = dir;
     84     ProgressCallback callback;
     85 
     86     AddSource(details, callback);
     87 
     88     fs::path state = GetPathToFileDir();
     89     REQUIRE(fs::exists(state));
     90 
     91     fs::path indexMsix = state;
     92     indexMsix /= s_IndexMsixName;
     93     REQUIRE(fs::exists(indexMsix));
     94     REQUIRE(fs::file_size(indexMsix) > 0);
     95 
     96     if (shouldCleanCert)
     97     {
     98         UninstallCertFromSignedPackage(index);
     99     }
    100 }
    101 
    102 TEST_CASE("PIPS_UpdateSameVersion", "[pips]")
    103 {
    104     if (!Runtime::IsRunningAsAdmin())
    105     {
    106         WARN("Test requires admin privilege. Skipped.");
    107         return;
    108     }
    109 
    110     CleanSources();
    111 
    112     TempDirectory dir("pipssource");
    113     TestDataFile index(s_MsixFile_1);
    114     CopyIndexFileToDirectory(index, dir);
    115 
    116     bool shouldCleanCert = InstallCertFromSignedPackage(index);
    117 
    118     SourceDetails details;
    119     details.Name = "TestName";
    120     details.Type = AppInstaller::Repository::Microsoft::PreIndexedPackageSourceFactory::Type();
    121     details.Arg = dir;
    122     TestProgress callback;
    123 
    124     AddSource(details, callback);
    125 
    126     fs::path state = GetPathToFileDir();
    127     REQUIRE(fs::exists(state));
    128 
    129     bool progressCalled = false;
    130     callback.m_OnProgress = [&](uint64_t, uint64_t, ProgressType) { progressCalled = true; };
    131 
    132     UpdateSource(details.Name, callback);
    133     REQUIRE(!progressCalled);
    134 
    135     if (shouldCleanCert)
    136     {
    137         UninstallCertFromSignedPackage(index);
    138     }
    139 }
    140 
    141 TEST_CASE("PIPS_UpdateNewVersion", "[pips]")
    142 {
    143     if (!Runtime::IsRunningAsAdmin())
    144     {
    145         WARN("Test requires admin privilege. Skipped.");
    146         return;
    147     }
    148 
    149     CleanSources();
    150 
    151     TempDirectory dir("pipssource");
    152     TestDataFile indexMsix1(s_MsixFile_1);
    153     CopyIndexFileToDirectory(indexMsix1, dir);
    154 
    155     bool shouldCleanCert = InstallCertFromSignedPackage(indexMsix1);
    156 
    157     SourceDetails details;
    158     details.Name = "TestName";
    159     details.Type = AppInstaller::Repository::Microsoft::PreIndexedPackageSourceFactory::Type();
    160     details.Arg = dir;
    161     TestProgress callback;
    162 
    163     AddSource(details, callback);
    164 
    165     fs::path state = GetPathToFileDir();
    166     REQUIRE(fs::exists(state));
    167 
    168     fs::path indexMsix = state;
    169     indexMsix /= s_IndexMsixName;
    170     std::string indexContents1 = GetContents(indexMsix);
    171 
    172     TestDataFile indexMsix2(s_MsixFile_2);
    173     CopyIndexFileToDirectory(indexMsix2, dir);
    174 
    175     bool progressCalled = false;
    176     callback.m_OnProgress = [&](uint64_t, uint64_t, ProgressType) { progressCalled = true; };
    177 
    178     UpdateSource(details.Name, callback);
    179     REQUIRE(progressCalled);
    180 
    181     std::string indexContents2 = GetContents(indexMsix);
    182     REQUIRE(indexContents1 != indexContents2);
    183 
    184     if (shouldCleanCert)
    185     {
    186         UninstallCertFromSignedPackage(indexMsix1);
    187     }
    188 }
    189 
    190 TEST_CASE("PIPS_Remove", "[pips]")
    191 {
    192     if (!Runtime::IsRunningAsAdmin())
    193     {
    194         WARN("Test requires admin privilege. Skipped.");
    195         return;
    196     }
    197 
    198     CleanSources();
    199 
    200     TempDirectory dir("pipssource");
    201     TestDataFile index(s_MsixFile_1);
    202     CopyIndexFileToDirectory(index, dir);
    203 
    204     bool shouldCleanCert = InstallCertFromSignedPackage(index);
    205 
    206     SourceDetails details;
    207     details.Name = "TestName";
    208     details.Type = AppInstaller::Repository::Microsoft::PreIndexedPackageSourceFactory::Type();
    209     details.Arg = dir;
    210     ProgressCallback callback;
    211 
    212     AddSource(details, callback);
    213 
    214     fs::path state = GetPathToFileDir();
    215     REQUIRE(fs::exists(state));
    216 
    217     fs::path indexMsix = state;
    218     indexMsix /= s_IndexMsixName;
    219     REQUIRE(fs::exists(indexMsix));
    220 
    221     RemoveSource(details.Name, callback);
    222     REQUIRE(!fs::exists(state));
    223 
    224     if (shouldCleanCert)
    225     {
    226         UninstallCertFromSignedPackage(index);
    227     }
    228 }