winget-cli

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

TestHooks.h (11444B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include <SourceFactory.h>
      5 #include <filesystem>
      6 #include <functional>
      7 #include <memory>
      8 #include <string>
      9 
     10 #include <AppInstallerTelemetry.h>
     11 #include <AppInstallerRuntime.h>
     12 #include <AppInstallerDownloader.h>
     13 #include <winget/UserSettings.h>
     14 #include <winget/Filesystem.h>
     15 #include <winget/IconExtraction.h>
     16 #include <winget/Authentication.h>
     17 #include <winget/HttpClientHelper.h>
     18 #include <sfsclient/SFSClient.h>
     19 
     20 #ifdef AICLI_DISABLE_TEST_HOOKS
     21 static_assert(false, "Test hooks have been disabled");
     22 #endif
     23 
     24 namespace AppInstaller
     25 {
     26     // Don't forget to clear the overrides after use!
     27     // A good way is to create a helper struct that cleans when destroyed
     28 
     29     namespace Runtime
     30     {
     31         void TestHook_SetPathOverride(PathName target, const std::filesystem::path& path);
     32         void TestHook_SetPathOverride(PathName target, const Filesystem::PathDetails& details);
     33         void TestHook_ClearPathOverrides();
     34     }
     35 
     36     namespace Repository
     37     {
     38         void TestHook_SetSourceFactoryOverride(const std::string& type, std::function<std::unique_ptr<ISourceFactory>()>&& factory);
     39         void TestHook_ClearSourceFactoryOverrides();
     40         void TestHook_SetExtractIconFromArpEntryResult_Override(std::vector<AppInstaller::Repository::ExtractedIconInfo>* result);
     41     }
     42 
     43     namespace Repository::Microsoft
     44     {
     45         void TestHook_SetPinningIndex_Override(std::optional<std::filesystem::path>&& indexPath);
     46 
     47         using GetARPKeyFunc = std::function<Registry::Key(Manifest::ScopeEnum, Utility::Architecture)>;
     48         void SetGetARPKeyOverride(GetARPKeyFunc value);
     49     }
     50 
     51     namespace Logging
     52     {
     53         void TestHook_SetTelemetryOverride(std::shared_ptr<TelemetryTraceLogger> ttl);
     54     }
     55 
     56     namespace Settings
     57     {
     58         void SetUserSettingsOverride(UserSettings* value);
     59     }
     60 
     61     namespace Filesystem
     62     {
     63         void TestHook_SetCreateSymlinkResult_Override(bool* status);
     64     }
     65 
     66     namespace Archive
     67     {
     68         void TestHook_SetScanArchiveResult_Override(bool* status);
     69     }
     70 
     71     namespace CLI::Workflow
     72     {
     73         void TestHook_SetEnableWindowsFeatureResult_Override(std::optional<DWORD>&& result);
     74         void TestHook_SetDoesWindowsFeatureExistResult_Override(std::optional<DWORD>&& result);
     75         void TestHook_SetExtractArchiveWithTarResult_Override(std::optional<DWORD>&& result);
     76     }
     77 
     78     namespace Reboot
     79     {
     80         void TestHook_SetInitiateRebootResult_Override(bool* status);
     81         void TestHook_SetRegisterForRestartResult_Override(bool* status);
     82     }
     83 
     84     namespace Authentication
     85     {
     86         void TestHook_SetAuthenticationResult_Override(Authentication::AuthenticationResult* authResult);
     87     }
     88 
     89     namespace MSStore::TestHooks
     90     {
     91         void SetDisplayCatalogHttpPipelineStage_Override(std::shared_ptr<web::http::http_pipeline_stage> value);
     92 
     93         void SetSfsClientAppContents_Override(std::function<std::vector<SFS::AppContent>(std::string_view)>* value);
     94 
     95         void SetLicensingHttpPipelineStage_Override(std::shared_ptr<web::http::http_pipeline_stage> value);
     96     }
     97 
     98     namespace Utility::TestHooks
     99     {
    100         void SetDownloadResult_Function_Override(std::function<DownloadResult(
    101             const std::string& url,
    102             const std::filesystem::path& dest,
    103             DownloadType type,
    104             IProgressCallback& progress,
    105             std::optional<DownloadInfo> info)>* value);
    106     }
    107 }
    108 
    109 namespace TestHook
    110 {
    111     struct SetCreateSymlinkResult_Override
    112     {
    113         SetCreateSymlinkResult_Override(bool status) : m_status(status)
    114         {
    115             AppInstaller::Filesystem::TestHook_SetCreateSymlinkResult_Override(&m_status);
    116         }
    117 
    118         ~SetCreateSymlinkResult_Override()
    119         {
    120             AppInstaller::Filesystem::TestHook_SetCreateSymlinkResult_Override(nullptr);
    121         }
    122 
    123     private:
    124         bool m_status;
    125     };
    126 
    127     struct SetScanArchiveResult_Override
    128     {
    129         SetScanArchiveResult_Override(bool status) : m_status(status)
    130         {
    131             AppInstaller::Archive::TestHook_SetScanArchiveResult_Override(&m_status);
    132         }
    133 
    134         ~SetScanArchiveResult_Override()
    135         {
    136             AppInstaller::Archive::TestHook_SetScanArchiveResult_Override(nullptr);
    137         }
    138 
    139     private:
    140         bool m_status;
    141     };
    142 
    143     struct SetPinningIndex_Override
    144     {
    145         SetPinningIndex_Override(const std::filesystem::path& indexPath)
    146         {
    147             AppInstaller::Repository::Microsoft::TestHook_SetPinningIndex_Override(indexPath);
    148         }
    149 
    150         ~SetPinningIndex_Override()
    151         {
    152             AppInstaller::Repository::Microsoft::TestHook_SetPinningIndex_Override({});
    153         }
    154     };
    155 
    156     struct SetExtractIconFromArpEntryResult_Override
    157     {
    158         SetExtractIconFromArpEntryResult_Override(std::vector<AppInstaller::Repository::ExtractedIconInfo> extractedIcons) : m_extractedIcons(std::move(extractedIcons))
    159         {
    160             AppInstaller::Repository::TestHook_SetExtractIconFromArpEntryResult_Override(&m_extractedIcons);
    161         }
    162 
    163         ~SetExtractIconFromArpEntryResult_Override()
    164         {
    165             AppInstaller::Repository::TestHook_SetExtractIconFromArpEntryResult_Override(nullptr);
    166         }
    167 
    168     private:
    169         std::vector<AppInstaller::Repository::ExtractedIconInfo> m_extractedIcons;
    170     };
    171 
    172     struct SetEnableWindowsFeatureResult_Override
    173     {
    174         SetEnableWindowsFeatureResult_Override(DWORD result)
    175         {
    176             AppInstaller::CLI::Workflow::TestHook_SetEnableWindowsFeatureResult_Override(result);
    177         }
    178 
    179         ~SetEnableWindowsFeatureResult_Override()
    180         {
    181             AppInstaller::CLI::Workflow::TestHook_SetEnableWindowsFeatureResult_Override({});
    182         }
    183     };
    184 
    185     struct SetDoesWindowsFeatureExistResult_Override
    186     {
    187         SetDoesWindowsFeatureExistResult_Override(DWORD result)
    188         {
    189             AppInstaller::CLI::Workflow::TestHook_SetDoesWindowsFeatureExistResult_Override(result);
    190         }
    191 
    192         ~SetDoesWindowsFeatureExistResult_Override()
    193         {
    194             AppInstaller::CLI::Workflow::TestHook_SetDoesWindowsFeatureExistResult_Override({});
    195         }
    196     };
    197 
    198     struct SetExtractArchiveWithTarResult_Override
    199     {
    200         SetExtractArchiveWithTarResult_Override(DWORD result)
    201         {
    202             AppInstaller::CLI::Workflow::TestHook_SetExtractArchiveWithTarResult_Override(result);
    203         }
    204 
    205         ~SetExtractArchiveWithTarResult_Override()
    206         {
    207             AppInstaller::CLI::Workflow::TestHook_SetExtractArchiveWithTarResult_Override({});
    208         }
    209     };
    210 
    211     struct SetInitiateRebootResult_Override
    212     {
    213         SetInitiateRebootResult_Override(bool status) : m_status(status)
    214         {
    215             AppInstaller::Reboot::TestHook_SetInitiateRebootResult_Override(&m_status);
    216         }
    217 
    218         ~SetInitiateRebootResult_Override()
    219         {
    220             AppInstaller::Reboot::TestHook_SetInitiateRebootResult_Override(nullptr);
    221         }
    222 
    223     private:
    224         bool m_status;
    225     };
    226 
    227     struct SetGetARPKey_Override
    228     {
    229         SetGetARPKey_Override(std::function<AppInstaller::Registry::Key(AppInstaller::Manifest::ScopeEnum, AppInstaller::Utility::Architecture)> function)
    230         {
    231             AppInstaller::Repository::Microsoft::SetGetARPKeyOverride(function);
    232         }
    233 
    234         ~SetGetARPKey_Override()
    235         {
    236             AppInstaller::Repository::Microsoft::SetGetARPKeyOverride({});
    237         }
    238 
    239     private:
    240     };
    241 
    242     struct SetRegisterForRestartResult_Override
    243     {
    244         SetRegisterForRestartResult_Override(bool status) : m_status(status)
    245         {
    246             AppInstaller::Reboot::TestHook_SetRegisterForRestartResult_Override(&m_status);
    247         }
    248 
    249         ~SetRegisterForRestartResult_Override()
    250         {
    251             AppInstaller::Reboot::TestHook_SetRegisterForRestartResult_Override(nullptr);
    252         }
    253 
    254     private:
    255         bool m_status;
    256     };
    257 
    258     struct SetAuthenticationResult_Override
    259     {
    260         SetAuthenticationResult_Override(AppInstaller::Authentication::AuthenticationResult authResult) : m_authResult(authResult)
    261         {
    262             AppInstaller::Authentication::TestHook_SetAuthenticationResult_Override(&m_authResult);
    263         }
    264 
    265         ~SetAuthenticationResult_Override()
    266         {
    267             AppInstaller::Authentication::TestHook_SetAuthenticationResult_Override(nullptr);
    268         }
    269 
    270     private:
    271         AppInstaller::Authentication::AuthenticationResult m_authResult;
    272     };
    273 
    274     struct SetDisplayCatalogHttpPipelineStage_Override
    275     {
    276         SetDisplayCatalogHttpPipelineStage_Override(std::shared_ptr<web::http::http_pipeline_stage> value)
    277         {
    278             AppInstaller::MSStore::TestHooks::SetDisplayCatalogHttpPipelineStage_Override(value);
    279         }
    280 
    281         ~SetDisplayCatalogHttpPipelineStage_Override()
    282         {
    283             AppInstaller::MSStore::TestHooks::SetDisplayCatalogHttpPipelineStage_Override(nullptr);
    284         }
    285     };
    286 
    287     struct SetSfsClientAppContents_Override
    288     {
    289         SetSfsClientAppContents_Override(std::function<std::vector<SFS::AppContent>(std::string_view)> value) : m_appContentsFunction(std::move(value))
    290         {
    291             AppInstaller::MSStore::TestHooks::SetSfsClientAppContents_Override(&m_appContentsFunction);
    292         }
    293 
    294         ~SetSfsClientAppContents_Override()
    295         {
    296             AppInstaller::MSStore::TestHooks::SetSfsClientAppContents_Override(nullptr);
    297         }
    298 
    299     private:
    300         std::function<std::vector<SFS::AppContent>(std::string_view)> m_appContentsFunction;
    301     };
    302 
    303     struct SetLicensingHttpPipelineStage_Override
    304     {
    305         SetLicensingHttpPipelineStage_Override(std::shared_ptr<web::http::http_pipeline_stage> value)
    306         {
    307             AppInstaller::MSStore::TestHooks::SetLicensingHttpPipelineStage_Override(value);
    308         }
    309 
    310         ~SetLicensingHttpPipelineStage_Override()
    311         {
    312             AppInstaller::MSStore::TestHooks::SetLicensingHttpPipelineStage_Override(nullptr);
    313         }
    314     };
    315 
    316     struct SetDownloadResult_Function_Override
    317     {
    318         SetDownloadResult_Function_Override(std::function<AppInstaller::Utility::DownloadResult(
    319             const std::string& url,
    320             const std::filesystem::path& dest,
    321             AppInstaller::Utility::DownloadType type,
    322             AppInstaller::IProgressCallback& progress,
    323             std::optional<AppInstaller::Utility::DownloadInfo> info)> value) : m_downloadFunction(std::move(value))
    324         {
    325             AppInstaller::Utility::TestHooks::SetDownloadResult_Function_Override(&m_downloadFunction);
    326         }
    327 
    328         ~SetDownloadResult_Function_Override()
    329         {
    330             AppInstaller::Utility::TestHooks::SetDownloadResult_Function_Override(nullptr);
    331         }
    332 
    333     private:
    334         std::function<AppInstaller::Utility::DownloadResult(
    335             const std::string& url,
    336             const std::filesystem::path& dest,
    337             AppInstaller::Utility::DownloadType type,
    338             AppInstaller::IProgressCallback& progress,
    339             std::optional<AppInstaller::Utility::DownloadInfo> info)> m_downloadFunction;
    340     };
    341 }