TestSettings.h (4510B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <winget/Settings.h> 5 #include <winget/UserSettings.h> 6 #include <wil/resource.h> 7 #include <string> 8 9 namespace TestCommon 10 { 11 // Repeat the policy values here so we can catch unintended changes in the source. 12 const std::wstring WinGetPolicyValueName = L"EnableAppInstaller"; 13 const std::wstring WinGetSettingsPolicyValueName = L"EnableSettings"; 14 const std::wstring ExperimentalFeaturesPolicyValueName = L"EnableExperimentalFeatures"; 15 const std::wstring LocalManifestsPolicyValueName = L"EnableLocalManifestFiles"; 16 const std::wstring EnableHashOverridePolicyValueName = L"EnableHashOverride"; 17 const std::wstring EnableLocalArchiveMalwareScanOverridePolicyValueName = L"EnableLocalArchiveMalwareScanOverride"; 18 const std::wstring DefaultSourcePolicyValueName = L"EnableDefaultSource"; 19 const std::wstring MSStoreSourcePolicyValueName = L"EnableMicrosoftStoreSource"; 20 const std::wstring AdditionalSourcesPolicyValueName = L"EnableAdditionalSources"; 21 const std::wstring AllowedSourcesPolicyValueName = L"EnableAllowedSources"; 22 const std::wstring BypassCertificatePinningForMicrosoftStoreValueName = L"EnableBypassCertificatePinningForMicrosoftStore"; 23 const std::wstring EnableWindowsPackageManagerCommandLineInterfaces = L"EnableWindowsPackageManagerCommandLineInterfaces"; 24 const std::wstring ConfigurationPolicyValueName = L"EnableWindowsPackageManagerConfiguration"; 25 const std::wstring ProxyCommandLineOptionsPolicyValueName = L"EnableWindowsPackageManagerProxyCommandLineOptions"; 26 const std::wstring McpServerValueName = L"EnableWindowsPackageManagerMcpServer"; 27 28 const std::wstring SourceUpdateIntervalPolicyValueName = L"SourceAutoUpdateInterval"; 29 const std::wstring SourceUpdateIntervalPolicyOldValueName = L"SourceAutoUpdateIntervalInMinutes"; 30 31 const std::wstring AdditionalSourcesPolicyKeyName = L"AdditionalSources"; 32 const std::wstring AllowedSourcesPolicyKeyName = L"AllowedSources"; 33 34 void SetSetting(const AppInstaller::Settings::StreamDefinition& stream, std::string_view value); 35 void RemoveSetting(const AppInstaller::Settings::StreamDefinition& stream); 36 std::filesystem::path GetPathTo(const AppInstaller::Settings::StreamDefinition& stream); 37 38 // This type removes the settings file on creation and destruction to ensure that a test that modifies them can do so cleanly. 39 struct UserSettingsFileGuard 40 { 41 UserSettingsFileGuard(); 42 ~UserSettingsFileGuard(); 43 }; 44 45 [[nodiscard]] UserSettingsFileGuard DeleteUserSettingsFiles(); 46 47 struct UserSettingsTest : AppInstaller::Settings::UserSettings 48 { 49 }; 50 51 struct GroupPolicyTestOverride : AppInstaller::Settings::GroupPolicy 52 { 53 GroupPolicyTestOverride() : GroupPolicyTestOverride(RegCreateVolatileTestRoot().get()) {} 54 GroupPolicyTestOverride(const AppInstaller::Registry::Key& key); 55 ~GroupPolicyTestOverride(); 56 57 template<AppInstaller::Settings::ValuePolicy P> 58 void SetValue(const ValueType<P>& value) 59 { 60 m_values.Add<P>(value); 61 } 62 63 template<AppInstaller::Settings::ValuePolicy P> 64 void SetValue(ValueType<P> &&value) 65 { 66 m_values.Add<P>(std::move(value)); 67 } 68 69 void SetState(AppInstaller::Settings::TogglePolicy::Policy policy, AppInstaller::Settings::PolicyState state); 70 }; 71 72 // Matcher that lets us verify GroupPolicyExceptions. 73 struct GroupPolicyExceptionMatcher : public Catch::Matchers::MatcherBase<AppInstaller::Settings::GroupPolicyException> 74 { 75 GroupPolicyExceptionMatcher(AppInstaller::Settings::TogglePolicy::Policy policy) : m_expectedPolicy(policy) {} 76 77 bool match(const AppInstaller::Settings::GroupPolicyException& e) const override 78 { 79 return e.Policy() == m_expectedPolicy; 80 } 81 82 std::string describe() const override 83 { 84 std::ostringstream result; 85 result << "has policy == " << m_expectedPolicy; 86 return result.str(); 87 } 88 89 private: 90 AppInstaller::Settings::TogglePolicy::Policy m_expectedPolicy; 91 }; 92 93 #define REQUIRE_POLICY_EXCEPTION(_expr_, _policy_) REQUIRE_THROWS_MATCHES(_expr_, AppInstaller::Settings::GroupPolicyException, TestCommon::GroupPolicyExceptionMatcher(_policy_)) 94 }