winget-cli

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

DownloadFlow.cpp (5987B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "TestHooks.h"
      5 #include "AppInstallerRuntime.h"
      6 #include "WorkflowCommon.h"
      7 #include <Commands/DownloadCommand.h>
      8 
      9 using namespace TestCommon;
     10 using namespace AppInstaller;
     11 using namespace AppInstaller::Authentication;
     12 using namespace AppInstaller::CLI;
     13 
     14 TEST_CASE("DownloadFlow_DownloadCommandProhibited", "[DownloadFlow][workflow]")
     15 {
     16     std::ostringstream downloadOutput;
     17     TestContext context{ downloadOutput, std::cin };
     18     auto previousThreadGlobals = context.SetForCurrentThread();
     19     context.Args.AddArg(Execution::Args::Type::Manifest, TestDataFile("DownloadFlowTest_DownloadCommandProhibited.yaml").GetPath().u8string());
     20 
     21     DownloadCommand download({});
     22     download.Execute(context);
     23     INFO(downloadOutput.str());
     24 
     25     // Verify AppInfo is printed
     26     REQUIRE_TERMINATED_WITH(context, APPINSTALLER_CLI_ERROR_DOWNLOAD_COMMAND_PROHIBITED);
     27     REQUIRE(downloadOutput.str().find(CLI::Resource::LocString(CLI::Resource::String::InstallerDownloadCommandProhibited).get()) != std::string::npos);
     28 }
     29 
     30 AppInstaller::Utility::DownloadResult ValidateAzureBlobStorageAuthHeaders(
     31     const std::string&,
     32     const std::filesystem::path& dest,
     33     AppInstaller::Utility::DownloadType,
     34     AppInstaller::IProgressCallback&,
     35     std::optional<AppInstaller::Utility::DownloadInfo> info)
     36 {
     37     REQUIRE(info);
     38     REQUIRE(info->RequestHeaders.size() > 0);
     39     REQUIRE(info->RequestHeaders[0].IsAuth);
     40     REQUIRE(info->RequestHeaders[0].Name == "Authorization");
     41     REQUIRE(info->RequestHeaders[0].Value == "Bearer TestToken");
     42     REQUIRE_FALSE(info->RequestHeaders[1].IsAuth);
     43     REQUIRE(info->RequestHeaders[1].Name == "x-ms-version");
     44     // Not validating x-ms-version value
     45 
     46     std::ofstream file(dest, std::ofstream::out);
     47     file << "test";
     48     file.close();
     49 
     50     AppInstaller::Utility::DownloadResult result;
     51     result.Sha256Hash = AppInstaller::Utility::SHA256::ConvertToBytes("65DB2F2AC2686C7F2FD69D4A4C6683B888DC55BFA20A0E32CA9F838B51689A3B");
     52     return result;
     53 }
     54 
     55 TEST_CASE("DownloadFlow_DownloadWithInstallerAuthenticationSuccess", "[DownloadFlow][workflow]")
     56 {
     57     if (Runtime::IsRunningAsSystem())
     58     {
     59         WARN("Test does not support running as system. Skipped.");
     60         return;
     61     }
     62 
     63     // Set authentication success result override
     64     std::string expectedToken = "TestToken";
     65     AuthenticationResult authResultOverride;
     66     authResultOverride.Status = S_OK;
     67     authResultOverride.Token = expectedToken;
     68     TestHook::SetAuthenticationResult_Override setAuthenticationResultOverride(authResultOverride);
     69 
     70     // Set auth header validation override
     71     TestHook::SetDownloadResult_Function_Override downloadFunctionOverride({ &ValidateAzureBlobStorageAuthHeaders });
     72 
     73     std::ostringstream downloadOutput;
     74     TestContext context{ downloadOutput, std::cin };
     75     auto previousThreadGlobals = context.SetForCurrentThread();
     76     context.Args.AddArg(Execution::Args::Type::Manifest, TestDataFile("ManifestV1_10-InstallerAuthentication.yaml").GetPath().u8string());
     77     TestCommon::TempDirectory tempDirectory("TempDownload");
     78     context.Args.AddArg(Execution::Args::Type::DownloadDirectory, tempDirectory.GetPath().u8string());
     79 
     80     DownloadCommand download({});
     81     download.Execute(context);
     82     INFO(downloadOutput.str());
     83 
     84     // Verify success
     85     REQUIRE_FALSE(context.IsTerminated());
     86     REQUIRE(context.GetTerminationHR() == S_OK);
     87 }
     88 
     89 TEST_CASE("DownloadFlow_DownloadWithInstallerAuthenticationNotSupported", "[DownloadFlow][workflow]")
     90 {
     91     if (Runtime::IsRunningAsSystem())
     92     {
     93         WARN("Test does not support running as system. Skipped.");
     94         return;
     95     }
     96 
     97     // Set authentication failed result
     98     AuthenticationResult authResultOverride;
     99     authResultOverride.Status = APPINSTALLER_CLI_ERROR_AUTHENTICATION_TYPE_NOT_SUPPORTED;
    100     TestHook::SetAuthenticationResult_Override setAuthenticationResultOverride(authResultOverride);
    101 
    102     std::ostringstream downloadOutput;
    103     TestContext context{ downloadOutput, std::cin };
    104     auto previousThreadGlobals = context.SetForCurrentThread();
    105     context.Args.AddArg(Execution::Args::Type::Manifest, TestDataFile("ManifestV1_10-InstallerAuthentication.yaml").GetPath().u8string());
    106 
    107     DownloadCommand download({});
    108     download.Execute(context);
    109     INFO(downloadOutput.str());
    110 
    111     // Verify AppInfo is printed
    112     REQUIRE_TERMINATED_WITH(context, APPINSTALLER_CLI_ERROR_AUTHENTICATION_TYPE_NOT_SUPPORTED);
    113     REQUIRE(downloadOutput.str().find(CLI::Resource::LocString(CLI::Resource::String::InstallerDownloadAuthenticationNotSupported).get()) != std::string::npos);
    114 }
    115 
    116 TEST_CASE("DownloadFlow_DownloadWithInstallerAuthenticationFailed", "[DownloadFlow][workflow]")
    117 {
    118     if (Runtime::IsRunningAsSystem())
    119     {
    120         WARN("Test does not support running as system. Skipped.");
    121         return;
    122     }
    123 
    124     // Set authentication failed result
    125     AuthenticationResult authResultOverride;
    126     authResultOverride.Status = APPINSTALLER_CLI_ERROR_AUTHENTICATION_FAILED;
    127     TestHook::SetAuthenticationResult_Override setAuthenticationResultOverride(authResultOverride);
    128 
    129     std::ostringstream downloadOutput;
    130     TestContext context{ downloadOutput, std::cin };
    131     auto previousThreadGlobals = context.SetForCurrentThread();
    132     context.Args.AddArg(Execution::Args::Type::Manifest, TestDataFile("ManifestV1_10-InstallerAuthentication.yaml").GetPath().u8string());
    133 
    134     DownloadCommand download({});
    135     download.Execute(context);
    136     INFO(downloadOutput.str());
    137 
    138     // Verify AppInfo is printed
    139     REQUIRE_TERMINATED_WITH(context, APPINSTALLER_CLI_ERROR_AUTHENTICATION_FAILED);
    140     REQUIRE(downloadOutput.str().find(CLI::Resource::LocString(CLI::Resource::String::InstallerDownloadAuthenticationFailed).get()) != std::string::npos);
    141 }