From ca423d643d89e74110dd01b4e8738ed94c653f61 Mon Sep 17 00:00:00 2001 From: --global Date: Tue, 14 May 2024 21:57:10 -0700 Subject: [PATCH 01/12] save work --- .../ConfigurationDynamicRuntimeFactory.cpp | 34 ++++- ...nfigurationSetProcessorFactoryRemoting.cpp | 9 +- .../Workflows/ConfigurationFlow.cpp | 3 +- ...erShellConfigurationSetProcessorFactory.cs | 2 - .../Tests/ConfigurationMixedElevationTests.cs | 129 ++++++++++++++++++ .../ConfigurationProcessorFactoryTests.cs | 1 - .../Tests/ConfigurationProcessorGetTests.cs | 3 +- src/nuget.config | 3 +- 8 files changed, 173 insertions(+), 11 deletions(-) create mode 100644 src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs diff --git a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp index f3ae3e1ce0..c9e3102157 100644 --- a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp +++ b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp @@ -14,6 +14,27 @@ namespace AppInstaller::CLI::ConfigurationRemoting { namespace anonymous { +#ifndef DISABLE_TEST_HOOKS + constexpr std::wstring_view DisableRunAsGuid = L"1e62d683-2999-44e7-81f7-6f8f35e8d731"; + constexpr std::wstring_view DisableSerialization = L"02f64b7d-6c2e-43fa-87dd-1f265800681d"; + + // Checks for a specific guid to control the behavior of a specific flow. + bool GetBehaviorForTestGuid(ConfigurationSet configurationSet, const std::wstring_view& testGuid) + { + auto disableRunAsBehavior = configurationSet.Metadata().TryLookup(testGuid); + if (disableRunAsBehavior) + { + auto disableRunAsProperty = disableRunAsBehavior.try_as(); + if (disableRunAsProperty && disableRunAsProperty.Type() == PropertyType::Boolean) + { + return disableRunAsProperty.GetBoolean(); + } + } + + return false; + } +#endif + struct DynamicProcessorInfo { IConfigurationSetProcessorFactory Factory; @@ -138,7 +159,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting std::vector highIntegrityUnits; auto units = m_configurationSet.Units(); - for (auto unit : units) + for (auto unit : units) { if (unit.IsActive() && GetIntegrityLevelForUnit(unit) == Security::IntegrityLevel::High) { @@ -172,7 +193,16 @@ namespace AppInstaller::CLI::ConfigurationRemoting // If we got here, the only option is that the current integrity level is not High. if (integrityLevel == Security::IntegrityLevel::High) { - factory = CreateOutOfProcessFactory(true, SerializeSetProperties(), SerializeHighIntegrityLevelSet()); + bool useRunAs = true; + +#ifndef DISABLE_TEST_HOOKS + if (GetBehaviorForTestGuid(m_configurationSet, DisableRunAsGuid)) + { + useRunAs = false; + } +#endif + + factory = CreateOutOfProcessFactory(useRunAs, SerializeSetProperties(), SerializeHighIntegrityLevelSet()); } else { diff --git a/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp b/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp index ec606883c9..10fb7529f0 100644 --- a/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp +++ b/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp @@ -190,9 +190,16 @@ namespace AppInstaller::CLI::ConfigurationRemoting execInfo.lpParameters = arguments.c_str(); execInfo.nShow = SW_HIDE; - if (useRunAs) + if (useRunAs && +#ifndef DISABLE_TEST_HOOKS // Always set to false so that userunAs is never included for unit tests. + true +#elif + true +#endif + ) { execInfo.lpVerb = L"runas"; + AICLI_LOG(Config, Verbose, << "Process set with runas verb."); } THROW_LAST_ERROR_IF(!ShellExecuteExW(&execInfo) || !execInfo.hProcess); diff --git a/src/AppInstallerCLICore/Workflows/ConfigurationFlow.cpp b/src/AppInstallerCLICore/Workflows/ConfigurationFlow.cpp index 58736ea4fb..ca57a21e1c 100644 --- a/src/AppInstallerCLICore/Workflows/ConfigurationFlow.cpp +++ b/src/AppInstallerCLICore/Workflows/ConfigurationFlow.cpp @@ -91,8 +91,7 @@ namespace AppInstaller::CLI::Workflow IConfigurationSetProcessorFactory factory; // Since downgrading is not currently supported, only use dynamic if not running as admin. - if (Settings::ExperimentalFeature::IsEnabled(Settings::ExperimentalFeature::Feature::ConfigureSelfElevation) && - !Runtime::IsRunningAsAdmin()) + if (Settings::ExperimentalFeature::IsEnabled(Settings::ExperimentalFeature::Feature::ConfigureSelfElevation) && !Runtime::IsRunningAsAdmin()) { factory = ConfigurationRemoting::CreateDynamicRuntimeFactory(); // TODO: Implement SetProcessorFactory::IPwshConfigurationSetProcessorFactoryProperties on dynamic factory diff --git a/src/Microsoft.Management.Configuration.Processor/Public/PowerShellConfigurationSetProcessorFactory.cs b/src/Microsoft.Management.Configuration.Processor/Public/PowerShellConfigurationSetProcessorFactory.cs index b800e50ea6..226d6c327f 100644 --- a/src/Microsoft.Management.Configuration.Processor/Public/PowerShellConfigurationSetProcessorFactory.cs +++ b/src/Microsoft.Management.Configuration.Processor/Public/PowerShellConfigurationSetProcessorFactory.cs @@ -8,9 +8,7 @@ namespace Microsoft.Management.Configuration.Processor { using System; using System.Collections.Generic; - using System.ComponentModel; using System.IO; - using System.Linq; using System.Management.Automation; using System.Runtime.CompilerServices; using System.Text; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs new file mode 100644 index 0000000000..719e4fddc6 --- /dev/null +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs @@ -0,0 +1,129 @@ +// ----------------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// +// ----------------------------------------------------------------------------- + +namespace Microsoft.Management.Configuration.UnitTests.Tests +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Microsoft.CodeAnalysis; + using Microsoft.Management.Configuration.Processor.Set; + using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; + using Microsoft.VisualBasic; + using Xunit; + using Xunit.Abstractions; + + /// + /// Unit tests for verifying the processor behavior for handling mixed elevation scenarios. + /// + [Collection("UnitTestCollection")] + [OutOfProc] + public class ConfigurationMixedElevationTests : ConfigurationProcessorTestBase + { + private readonly UnitTestFixture fixture; + private readonly ITestOutputHelper log; + + /// + /// Initializes a new instance of the class. + /// + /// Unit test fixture. + /// Log helper. + public ConfigurationMixedElevationTests(UnitTestFixture fixture, ITestOutputHelper log) + : base(fixture, log) + { + this.fixture = fixture; + this.log = log; + } + + /// + /// Verifies that a set of units with mixed elevation can run successfully. + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task ApplyUnitsWithMixedElevation() + { + string resourceName = "E2ETestResource"; + string moduleName = "xE2ETestResource"; + Version version = new Version("0.0.0.1"); + + ConfigurationSet configurationSet = this.ConfigurationSet(); + + ConfigurationUnit configurationUnit1 = this.ConfigurationUnit(); + configurationUnit1.Metadata.Add("securityContext", "elevated"); + configurationUnit1.Metadata.Add("version", version.ToString()); + configurationUnit1.Metadata.Add("module", moduleName); + configurationUnit1.Metadata.Add("secretCode", "123456789"); + configurationUnit1.Type = resourceName; + configurationUnit1.Intent = ConfigurationUnitIntent.Apply; + + ConfigurationUnit configurationUnit2 = this.ConfigurationUnit(); + configurationUnit2.Metadata.Add("version", version.ToString()); + configurationUnit2.Metadata.Add("module", moduleName); + configurationUnit2.Metadata.Add("secretCode", "123456789"); + configurationUnit2.Type = resourceName; + configurationUnit2.Intent = ConfigurationUnitIntent.Apply; + + configurationSet.Units = new ConfigurationUnit[] { configurationUnit1, configurationUnit2 }; + + IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync("{73fea39f-6f4a-41c9-ba94-6fd14d633e40}"); + + ConfigurationProcessor processor = this.CreateConfigurationProcessorWithDiagnostics(dynamicFactory); + + TestConfigurationSetResult result = processor.TestSet(configurationSet); + Assert.NotNull(result); + Assert.Equal(1, result.UnitResults.Count); + + ApplyConfigurationSetResult applyResult = processor.ApplySet(configurationSet, ApplyConfigurationSetFlags.None); + Assert.NotNull(applyResult); + } + + /// + /// Verifies that a unit not in the limitation set will fail. + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task ApplyUnitNotInLimitationSet() + { + string resourceName = "E2ETestResource"; + string moduleName = "xE2ETestResource"; + Version version = new Version("0.0.0.1"); + + ConfigurationSet configurationSet = this.ConfigurationSet(); + + ConfigurationUnit configurationUnit1 = this.ConfigurationUnit(); + configurationUnit1.Metadata.Add("securityContext", "elevated"); + configurationUnit1.Metadata.Add("version", version.ToString()); + configurationUnit1.Metadata.Add("module", moduleName); + configurationUnit1.Metadata.Add("secretCode", "123456789"); + configurationUnit1.Type = resourceName; + configurationUnit1.Intent = ConfigurationUnitIntent.Apply; + + ConfigurationUnit configurationUnit2 = this.ConfigurationUnit(); + configurationUnit2.Metadata.Add("version", version.ToString()); + configurationUnit2.Metadata.Add("module", moduleName); + configurationUnit2.Metadata.Add("secretCode", "123456789"); + configurationUnit2.Type = resourceName; + configurationUnit2.Intent = ConfigurationUnitIntent.Apply; + + configurationSet.Units = new ConfigurationUnit[] { configurationUnit1, configurationUnit2 }; + + IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync("{73fea39f-6f4a-41c9-ba94-6fd14d633e40}"); + + ConfigurationProcessor processor = this.CreateConfigurationProcessorWithDiagnostics(dynamicFactory); + + TestConfigurationSetResult result = processor.TestSet(configurationSet); + Assert.NotNull(result); + Assert.Equal(1, result.UnitResults.Count); + + ApplyConfigurationSetResult applyResult = processor.ApplySet(configurationSet, ApplyConfigurationSetFlags.None); + Assert.NotNull(applyResult); + } + } +} diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorFactoryTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorFactoryTests.cs index adba5c2957..c1c05f9ecc 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorFactoryTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorFactoryTests.cs @@ -10,7 +10,6 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests using Microsoft.Management.Configuration.Processor; using Microsoft.Management.Configuration.Processor.Set; using Microsoft.Management.Configuration.UnitTests.Fixtures; - using Moq; using WinRT; using Xunit; using Xunit.Abstractions; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetTests.cs index bd95079a77..5759c34245 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -6,7 +6,6 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests { - using System.Collections.Generic; using System.IO; using Microsoft.Management.Configuration.UnitTests.Fixtures; using Microsoft.Management.Configuration.UnitTests.Helpers; diff --git a/src/nuget.config b/src/nuget.config index c38f2abef1..095b938597 100644 --- a/src/nuget.config +++ b/src/nuget.config @@ -1,8 +1,9 @@ - + + From 7c20b63fda7548b98a81fb2df167c8c2c11748f7 Mon Sep 17 00:00:00 2001 From: --global Date: Wed, 15 May 2024 13:12:16 -0700 Subject: [PATCH 02/12] save work --- .../ConfigurationDynamicRuntimeFactory.cpp | 28 +++--- ...nfigurationSetProcessorFactoryRemoting.cpp | 10 +- src/AppInstallerCLITests/TestHooks.h | 2 +- .../Helpers/Constants.cs | 17 +++- .../Tests/ConfigurationMixedElevationTests.cs | 96 ++++++++++--------- 5 files changed, 86 insertions(+), 67 deletions(-) diff --git a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp index c9e3102157..86ab62726a 100644 --- a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp +++ b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp @@ -15,19 +15,19 @@ namespace AppInstaller::CLI::ConfigurationRemoting namespace anonymous { #ifndef DISABLE_TEST_HOOKS - constexpr std::wstring_view DisableRunAsGuid = L"1e62d683-2999-44e7-81f7-6f8f35e8d731"; - constexpr std::wstring_view DisableSerialization = L"02f64b7d-6c2e-43fa-87dd-1f265800681d"; + constexpr std::wstring_view DisableRunAsTestGuid = L"1e62d683-2999-44e7-81f7-6f8f35e8d731"; + constexpr std::wstring_view DisableHighIntegritySetSerializationTestGuid = L"02f64b7d-6c2e-43fa-87dd-1f265800681d"; - // Checks for a specific guid to control the behavior of a specific flow. - bool GetBehaviorForTestGuid(ConfigurationSet configurationSet, const std::wstring_view& testGuid) + // Checks the configuration set metadata for a specific test guid that controls the behavior flow. + bool GetTestBehavior(const ConfigurationSet& configurationSet, const std::wstring_view& testGuid) { - auto disableRunAsBehavior = configurationSet.Metadata().TryLookup(testGuid); - if (disableRunAsBehavior) + auto testBehavior = configurationSet.Metadata().TryLookup(testGuid); + if (testBehavior) { - auto disableRunAsProperty = disableRunAsBehavior.try_as(); - if (disableRunAsProperty && disableRunAsProperty.Type() == PropertyType::Boolean) + auto testBehaviorProperty = testBehavior.try_as(); + if (testBehaviorProperty && testBehaviorProperty.Type() == PropertyType::Boolean) { - return disableRunAsProperty.GetBoolean(); + return testBehaviorProperty.GetBoolean(); } } @@ -161,7 +161,13 @@ namespace AppInstaller::CLI::ConfigurationRemoting for (auto unit : units) { - if (unit.IsActive() && GetIntegrityLevelForUnit(unit) == Security::IntegrityLevel::High) + if (unit.IsActive() && GetIntegrityLevelForUnit(unit) == Security::IntegrityLevel::High && +#ifndef DISABLE_TEST_HOOKS + !GetTestBehavior(m_configurationSet, DisableHighIntegritySetSerializationTestGuid) +#elif + true +#endif + ) { highIntegrityUnits.emplace_back(unit); } @@ -196,7 +202,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting bool useRunAs = true; #ifndef DISABLE_TEST_HOOKS - if (GetBehaviorForTestGuid(m_configurationSet, DisableRunAsGuid)) + if (GetTestBehavior(m_configurationSet, DisableRunAsTestGuid)) { useRunAs = false; } diff --git a/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp b/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp index 10fb7529f0..6edce09295 100644 --- a/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp +++ b/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp @@ -190,16 +190,9 @@ namespace AppInstaller::CLI::ConfigurationRemoting execInfo.lpParameters = arguments.c_str(); execInfo.nShow = SW_HIDE; - if (useRunAs && -#ifndef DISABLE_TEST_HOOKS // Always set to false so that userunAs is never included for unit tests. - true -#elif - true -#endif - ) + if (useRunAs) { execInfo.lpVerb = L"runas"; - AICLI_LOG(Config, Verbose, << "Process set with runas verb."); } THROW_LAST_ERROR_IF(!ShellExecuteExW(&execInfo) || !execInfo.hProcess); @@ -332,6 +325,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting IConfigurationSetProcessorFactory CreateOutOfProcessFactory(bool useRunAs, const std::string& properties, const std::string& restrictions) { + AICLI_LOG(CLI, Info, << restrictions); return winrt::make(useRunAs, properties, restrictions); } } diff --git a/src/AppInstallerCLITests/TestHooks.h b/src/AppInstallerCLITests/TestHooks.h index c00b908423..dcf73f91d4 100644 --- a/src/AppInstallerCLITests/TestHooks.h +++ b/src/AppInstallerCLITests/TestHooks.h @@ -234,4 +234,4 @@ namespace TestHook private: AppInstaller::Authentication::AuthenticationResult m_authResult; }; -} \ No newline at end of file +} diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs index 7da2ff083c..4b42e00111 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -20,5 +20,20 @@ public class Constants /// The namespace where xUnit traits will be defined. /// public const string NamespaceNameForTraits = "Microsoft.Management.Configuration.UnitTests.Helpers"; + + /// + /// The dynamic runtime factory handler identifier. + /// + public const string DynamicRuntimeHandlerIdentifier = "{73fea39f-6f4a-41c9-ba94-6fd14d633e40}"; + + /// + /// Test guid for disabling the dynamic factory from setting the 'RunAs' start process verb. + /// + public const string DisableRunAsTestGuid = "1e62d683-2999-44e7-81f7-6f8f35e8d731"; + + /// + /// Test guid for disabling the serialization of high integrity units. + /// + public const string DisableHighIntegriySerializationTestGuid = "02f64b7d-6c2e-43fa-87dd-1f265800681d"; } } diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs index 719e4fddc6..29993e37aa 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs @@ -7,13 +7,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests { using System; - using System.Collections.Generic; - using System.IO; - using System.Linq; - using System.Threading; using System.Threading.Tasks; - using Microsoft.CodeAnalysis; - using Microsoft.Management.Configuration.Processor.Set; using Microsoft.Management.Configuration.UnitTests.Fixtures; using Microsoft.Management.Configuration.UnitTests.Helpers; using Microsoft.VisualBasic; @@ -54,34 +48,43 @@ public async Task ApplyUnitsWithMixedElevation() Version version = new Version("0.0.0.1"); ConfigurationSet configurationSet = this.ConfigurationSet(); + configurationSet.Metadata.Add(Helpers.Constants.DisableRunAsTestGuid, true); - ConfigurationUnit configurationUnit1 = this.ConfigurationUnit(); - configurationUnit1.Metadata.Add("securityContext", "elevated"); - configurationUnit1.Metadata.Add("version", version.ToString()); - configurationUnit1.Metadata.Add("module", moduleName); - configurationUnit1.Metadata.Add("secretCode", "123456789"); - configurationUnit1.Type = resourceName; - configurationUnit1.Intent = ConfigurationUnitIntent.Apply; + ConfigurationUnit elevationRequiredUnit = this.ConfigurationUnit(); + elevationRequiredUnit.Metadata.Add("securityContext", "elevated"); + elevationRequiredUnit.Metadata.Add("version", version.ToString()); + elevationRequiredUnit.Metadata.Add("module", moduleName); + elevationRequiredUnit.Metadata.Add("secretCode", "123456789"); + elevationRequiredUnit.Type = resourceName; + elevationRequiredUnit.Intent = ConfigurationUnitIntent.Apply; - ConfigurationUnit configurationUnit2 = this.ConfigurationUnit(); - configurationUnit2.Metadata.Add("version", version.ToString()); - configurationUnit2.Metadata.Add("module", moduleName); - configurationUnit2.Metadata.Add("secretCode", "123456789"); - configurationUnit2.Type = resourceName; - configurationUnit2.Intent = ConfigurationUnitIntent.Apply; + ConfigurationUnit unit = this.ConfigurationUnit(); + unit.Metadata.Add("version", version.ToString()); + unit.Metadata.Add("module", moduleName); + unit.Metadata.Add("secretCode", "123456789"); + unit.Type = resourceName; + unit.Intent = ConfigurationUnitIntent.Apply; - configurationSet.Units = new ConfigurationUnit[] { configurationUnit1, configurationUnit2 }; + configurationSet.Units = new ConfigurationUnit[] { elevationRequiredUnit, unit }; - IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync("{73fea39f-6f4a-41c9-ba94-6fd14d633e40}"); + IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync(Helpers.Constants.DynamicRuntimeHandlerIdentifier); ConfigurationProcessor processor = this.CreateConfigurationProcessorWithDiagnostics(dynamicFactory); - TestConfigurationSetResult result = processor.TestSet(configurationSet); + ApplyConfigurationSetResult result = processor.ApplySet(configurationSet, ApplyConfigurationSetFlags.None); Assert.NotNull(result); - Assert.Equal(1, result.UnitResults.Count); - - ApplyConfigurationSetResult applyResult = processor.ApplySet(configurationSet, ApplyConfigurationSetFlags.None); - Assert.NotNull(applyResult); + Assert.Null(result.ResultCode); + Assert.Equal(2, result.UnitResults.Count); + + foreach (var unitResult in result.UnitResults) + { + Assert.NotNull(unitResult); + Assert.False(unitResult.PreviouslyInDesiredState); + Assert.False(unitResult.RebootRequired); + Assert.NotNull(unitResult.ResultInformation); + Assert.Null(unitResult.ResultInformation.ResultCode); + Assert.Equal(ConfigurationUnitResultSource.None, unitResult.ResultInformation.ResultSource); + } } /// @@ -96,34 +99,35 @@ public async Task ApplyUnitNotInLimitationSet() Version version = new Version("0.0.0.1"); ConfigurationSet configurationSet = this.ConfigurationSet(); + configurationSet.Metadata.Add(Helpers.Constants.DisableRunAsTestGuid, true); + configurationSet.Metadata.Add(Helpers.Constants.DisableHighIntegriySerializationTestGuid, true); - ConfigurationUnit configurationUnit1 = this.ConfigurationUnit(); - configurationUnit1.Metadata.Add("securityContext", "elevated"); - configurationUnit1.Metadata.Add("version", version.ToString()); - configurationUnit1.Metadata.Add("module", moduleName); - configurationUnit1.Metadata.Add("secretCode", "123456789"); - configurationUnit1.Type = resourceName; - configurationUnit1.Intent = ConfigurationUnitIntent.Apply; + ConfigurationUnit elevationRequiredUnit1 = this.ConfigurationUnit(); + elevationRequiredUnit1.Metadata.Add("securityContext", "elevated"); + elevationRequiredUnit1.Metadata.Add("version", version.ToString()); + elevationRequiredUnit1.Metadata.Add("module", moduleName); + elevationRequiredUnit1.Metadata.Add("secretCode", "123456789"); + elevationRequiredUnit1.Type = resourceName; + elevationRequiredUnit1.Intent = ConfigurationUnitIntent.Apply; - ConfigurationUnit configurationUnit2 = this.ConfigurationUnit(); - configurationUnit2.Metadata.Add("version", version.ToString()); - configurationUnit2.Metadata.Add("module", moduleName); - configurationUnit2.Metadata.Add("secretCode", "123456789"); - configurationUnit2.Type = resourceName; - configurationUnit2.Intent = ConfigurationUnitIntent.Apply; + //ConfigurationUnit elevationRequiredUnit2 = this.ConfigurationUnit(); + //elevationRequiredUnit2.Metadata.Add("securityContext", "elevated"); + //elevationRequiredUnit2.Metadata.Add("version", version.ToString()); + //elevationRequiredUnit2.Metadata.Add("module", moduleName); + //elevationRequiredUnit2.Metadata.Add("secretCode", "123456789"); + //elevationRequiredUnit2.Type = resourceName; + //elevationRequiredUnit2.Intent = ConfigurationUnitIntent.Apply; - configurationSet.Units = new ConfigurationUnit[] { configurationUnit1, configurationUnit2 }; + configurationSet.Units = new ConfigurationUnit[] { elevationRequiredUnit1 }; - IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync("{73fea39f-6f4a-41c9-ba94-6fd14d633e40}"); + IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync(Helpers.Constants.DynamicRuntimeHandlerIdentifier); ConfigurationProcessor processor = this.CreateConfigurationProcessorWithDiagnostics(dynamicFactory); - TestConfigurationSetResult result = processor.TestSet(configurationSet); + ApplyConfigurationSetResult result = processor.ApplySet(configurationSet, ApplyConfigurationSetFlags.None); Assert.NotNull(result); - Assert.Equal(1, result.UnitResults.Count); - - ApplyConfigurationSetResult applyResult = processor.ApplySet(configurationSet, ApplyConfigurationSetFlags.None); - Assert.NotNull(applyResult); + Assert.Null(result.ResultCode); + Assert.Equal(2, result.UnitResults.Count); } } } From 5222c895282a7fda3860f4e7d1e04784134a20fd Mon Sep 17 00:00:00 2001 From: --global Date: Thu, 16 May 2024 13:45:27 -0700 Subject: [PATCH 03/12] try to get tests working --- .github/actions/spelling/expect.txt | 8 +-- azure-pipelines.yml | 1 + .../ConfigurationDynamicRuntimeFactory.cpp | 35 +++++++------ ...nfigurationSetProcessorFactoryRemoting.cpp | 1 - .../Helpers/Constants.cs | 4 +- .../Helpers/Errors.cs | 2 + .../Helpers/OutOfProcOnlyAttribute.cs | 26 ++++++++++ .../Tests/ConfigurationMixedElevationTests.cs | 52 ++++++++++++++----- .../Tests/HashtableExtensionsTests.cs | 3 +- src/nuget.config | 1 - 10 files changed, 95 insertions(+), 38 deletions(-) create mode 100644 src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyAttribute.cs diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt index 7398a1b2b9..e0e79799cd 100644 --- a/.github/actions/spelling/expect.txt +++ b/.github/actions/spelling/expect.txt @@ -362,7 +362,7 @@ pseudocode PSHOST psobject ptstr -publickey +publickey PVD pvk pvm @@ -412,8 +412,8 @@ servercert servercertificate setmetadatabymanifestid SETTINGCHANGE -SETTINGMAPPING -sfs +SETTINGMAPPING +sfs sfsclient SHCONTF SHGDN @@ -435,7 +435,7 @@ Srinivasan srs startswith STARTUPINFOW -STDMETHODCALLTYPE +STDMETHODCALLTYPE storeapps storeorigin STRRET diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 2b6d34c7b4..08cc795c69 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -345,6 +345,7 @@ jobs: testSelector: 'testAssemblies' testAssemblyVer2: '**\Microsoft.Management.Configuration.UnitTests.dll' searchFolder: '$(buildOutDir)\Microsoft.Management.Configuration.UnitTests' + testFiltercriteria: 'Category!=OutOfProcOnly' codeCoverageEnabled: true platform: '$(buildPlatform)' configuration: '$(BuildConfiguration)' diff --git a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp index c1957794c5..8660a263ca 100644 --- a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp +++ b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp @@ -16,18 +16,18 @@ namespace AppInstaller::CLI::ConfigurationRemoting { #ifndef DISABLE_TEST_HOOKS constexpr std::wstring_view DisableRunAsTestGuid = L"1e62d683-2999-44e7-81f7-6f8f35e8d731"; - constexpr std::wstring_view DisableHighIntegritySetSerializationTestGuid = L"02f64b7d-6c2e-43fa-87dd-1f265800681d"; + constexpr std::wstring_view ForceHighIntegrityUnitProcessorsTestGuid = L"02f64b7d-6c2e-43fa-87dd-1f265800681d"; // Checks the configuration set metadata for a specific test guid that controls the behavior flow. - bool GetTestBehavior(const ConfigurationSet& configurationSet, const std::wstring_view& testGuid) + bool GetConfigurationSetMetadataOverride(const ConfigurationSet& configurationSet, const std::wstring_view& testGuid) { - auto testBehavior = configurationSet.Metadata().TryLookup(testGuid); - if (testBehavior) + auto metadataOverride = configurationSet.Metadata().TryLookup(testGuid); + if (metadataOverride) { - auto testBehaviorProperty = testBehavior.try_as(); - if (testBehaviorProperty && testBehaviorProperty.Type() == PropertyType::Boolean) + auto metadataOverrideProperty = metadataOverride.try_as(); + if (metadataOverrideProperty && metadataOverrideProperty.Type() == PropertyType::Boolean) { - return testBehaviorProperty.GetBoolean(); + return metadataOverrideProperty.GetBoolean(); } } @@ -67,8 +67,11 @@ namespace AppInstaller::CLI::ConfigurationRemoting { for (const auto& existingUnit : m_configurationSet.Units()) { +#ifndef DISABLE_TEST_HOOKS + Security::IntegrityLevel requiredIntegrityLevel = GetConfigurationSetMetadataOverride(m_configurationSet, ForceHighIntegrityUnitProcessorsTestGuid) ? Security::IntegrityLevel::High : GetIntegrityLevelForUnit(existingUnit); +#elif Security::IntegrityLevel requiredIntegrityLevel = GetIntegrityLevelForUnit(existingUnit); - +#endif if (m_setProcessors.find(requiredIntegrityLevel) == m_setProcessors.end()) { CreateSetProcessorForIntegrityLevel(requiredIntegrityLevel); @@ -77,7 +80,11 @@ namespace AppInstaller::CLI::ConfigurationRemoting }); // Create set and unit processor for current unit. +#ifndef DISABLE_TEST_HOOKS + Security::IntegrityLevel requiredIntegrityLevel = GetConfigurationSetMetadataOverride(m_configurationSet, ForceHighIntegrityUnitProcessorsTestGuid) ? Security::IntegrityLevel::High : GetIntegrityLevelForUnit(unit); +#elif Security::IntegrityLevel requiredIntegrityLevel = GetIntegrityLevelForUnit(unit); +#endif auto itr = m_setProcessors.find(requiredIntegrityLevel); if (itr == m_setProcessors.end()) @@ -159,15 +166,9 @@ namespace AppInstaller::CLI::ConfigurationRemoting std::vector highIntegrityUnits; auto units = m_configurationSet.Units(); - for (auto unit : units) + for (auto unit : units) { - if (unit.IsActive() && GetIntegrityLevelForUnit(unit) == Security::IntegrityLevel::High && -#ifndef DISABLE_TEST_HOOKS - !GetTestBehavior(m_configurationSet, DisableHighIntegritySetSerializationTestGuid) -#elif - true -#endif - ) + if (unit.IsActive() && GetIntegrityLevelForUnit(unit) == Security::IntegrityLevel::High) { highIntegrityUnits.emplace_back(unit); } @@ -202,7 +203,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting bool useRunAs = true; #ifndef DISABLE_TEST_HOOKS - if (GetTestBehavior(m_configurationSet, DisableRunAsTestGuid)) + if (GetConfigurationSetMetadataOverride(m_configurationSet, DisableRunAsTestGuid)) { useRunAs = false; } diff --git a/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp b/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp index 6edce09295..ec606883c9 100644 --- a/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp +++ b/src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp @@ -325,7 +325,6 @@ namespace AppInstaller::CLI::ConfigurationRemoting IConfigurationSetProcessorFactory CreateOutOfProcessFactory(bool useRunAs, const std::string& properties, const std::string& restrictions) { - AICLI_LOG(CLI, Info, << restrictions); return winrt::make(useRunAs, properties, restrictions); } } diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs index 4b42e00111..d69f6f1faa 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs @@ -32,8 +32,8 @@ public class Constants public const string DisableRunAsTestGuid = "1e62d683-2999-44e7-81f7-6f8f35e8d731"; /// - /// Test guid for disabling the serialization of high integrity units. + /// Test guid for forcing the creation of high integrity unit processors. /// - public const string DisableHighIntegriySerializationTestGuid = "02f64b7d-6c2e-43fa-87dd-1f265800681d"; + public const string ForceHighIntegrityUnitProcessorsTestGuid = "02f64b7d-6c2e-43fa-87dd-1f265800681d"; } } diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs index c512fa32b1..5d422d134b 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs @@ -47,6 +47,8 @@ internal static class Errors public static readonly int WINGET_CONFIG_ERROR_UNIT_IMPORT_MODULE_ADMIN = unchecked((int)0x8A15C111); public static readonly int WINGET_CONFIG_ERROR_NOT_SUPPORTED_BY_PROCESSOR = unchecked((int)0x8A15C112); + // Configuration Remoting Server Errors + public static readonly int COR_E_INVALIDOPERATION = unchecked((int)0x80131509); #pragma warning restore SA1025 // Code should not contain multiple whitespace in a row #pragma warning restore SA1600 // Elements should be documented #pragma warning restore SA1310 // Field names should not contain underscore diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyAttribute.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyAttribute.cs new file mode 100644 index 0000000000..285d7a50ae --- /dev/null +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyAttribute.cs @@ -0,0 +1,26 @@ +// ----------------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// +// ----------------------------------------------------------------------------- + +namespace Microsoft.Management.Configuration.UnitTests.Helpers +{ + using System; + using Xunit.Sdk; + + /// + /// Trait used to mark a test as only for the out of proc scenario. These tests should not run for the in-proc scenario. + /// + [TraitDiscoverer(OutOfProcDiscoverer.TypeName, Constants.AssemblyNameForTraits)] + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] + public class OutOfProcOnlyAttribute : Attribute, ITraitAttribute + { + /// + /// Initializes a new instance of the class. + /// + public OutOfProcOnlyAttribute() + { + } + } +} diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs index ebffad696f..8b5158b8b6 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs @@ -19,6 +19,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// [Collection("UnitTestCollection")] [OutOfProc] + [OutOfProcOnly] public class ConfigurationMixedElevationTests : ConfigurationProcessorTestBase { private readonly UnitTestFixture fixture; @@ -89,11 +90,11 @@ public async Task ApplyUnitsWithMixedElevation() } /// - /// Verifies that a unit not in the limitation set will fail. + /// Verifies that creating a high integrity unit processor for a non elevated unit should return an invalid operation result. /// /// A representing the asynchronous unit test. [Fact] - public async Task ApplyUnitNotInLimitationSet() + public async Task ApplyElevatedUnitNotInLimitationSet() { string resourceName = "E2ETestResource"; string moduleName = "xE2ETestResource"; @@ -101,18 +102,26 @@ public async Task ApplyUnitNotInLimitationSet() ConfigurationSet configurationSet = this.ConfigurationSet(); configurationSet.Metadata.Add(Helpers.Constants.DisableRunAsTestGuid, true); - configurationSet.Metadata.Add(Helpers.Constants.DisableHighIntegriySerializationTestGuid, true); + configurationSet.Metadata.Add(Helpers.Constants.ForceHighIntegrityUnitProcessorsTestGuid, true); - ConfigurationUnit elevationRequiredUnit1 = this.ConfigurationUnit(); - elevationRequiredUnit1.Metadata.Add("securityContext", "elevated"); - elevationRequiredUnit1.Metadata.Add("version", version.ToString()); - elevationRequiredUnit1.Metadata.Add("module", moduleName); + ConfigurationUnit elevationRequiredUnit = this.ConfigurationUnit(); + elevationRequiredUnit.Metadata.Add("securityContext", "elevated"); + elevationRequiredUnit.Metadata.Add("version", version.ToString()); + elevationRequiredUnit.Metadata.Add("module", moduleName); - elevationRequiredUnit1.Settings.Add("secretCode", "123456789"); - elevationRequiredUnit1.Type = resourceName; - elevationRequiredUnit1.Intent = ConfigurationUnitIntent.Apply; + elevationRequiredUnit.Settings.Add("secretCode", "123456789"); + elevationRequiredUnit.Type = resourceName; + elevationRequiredUnit.Intent = ConfigurationUnitIntent.Apply; + + ConfigurationUnit nonElevatedUnit = this.ConfigurationUnit(); + nonElevatedUnit.Metadata.Add("version", version.ToString()); + nonElevatedUnit.Metadata.Add("module", moduleName); + + nonElevatedUnit.Settings.Add("secretCode", "123456789"); + nonElevatedUnit.Type = resourceName; + nonElevatedUnit.Intent = ConfigurationUnitIntent.Apply; - configurationSet.Units = new ConfigurationUnit[] { elevationRequiredUnit1 }; + configurationSet.Units = new ConfigurationUnit[] { elevationRequiredUnit, nonElevatedUnit }; IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync(Helpers.Constants.DynamicRuntimeHandlerIdentifier); @@ -122,7 +131,26 @@ public async Task ApplyUnitNotInLimitationSet() Assert.NotNull(result); Assert.NotNull(result.ResultCode); Assert.Equal(Errors.WINGET_CONFIG_ERROR_SET_APPLY_FAILED, result.ResultCode.HResult); - Assert.Equal(1, result.UnitResults.Count); + Assert.Equal(2, result.UnitResults.Count); + + // First unit should succeed since it is a unit that requires elevation. + ApplyConfigurationUnitResult firstUnitResult = result.UnitResults[0]; + Assert.NotNull(firstUnitResult); + Assert.False(firstUnitResult.PreviouslyInDesiredState); + Assert.False(firstUnitResult.RebootRequired); + Assert.NotNull(firstUnitResult.ResultInformation); + Assert.Null(firstUnitResult.ResultInformation.ResultCode); + Assert.Equal(ConfigurationUnitResultSource.None, firstUnitResult.ResultInformation.ResultSource); + + // Second unit should fail as it was not included in the limitation set. + ApplyConfigurationUnitResult secondUnitResult = result.UnitResults[1]; + Assert.NotNull(secondUnitResult); + Assert.False(secondUnitResult.PreviouslyInDesiredState); + Assert.False(secondUnitResult.RebootRequired); + Assert.NotNull(secondUnitResult.ResultInformation); + Assert.NotNull(secondUnitResult.ResultInformation.ResultCode); + Assert.Equal(Errors.COR_E_INVALIDOPERATION, secondUnitResult.ResultInformation.ResultCode.HResult); + Assert.Equal(ConfigurationUnitResultSource.None, secondUnitResult.ResultInformation.ResultSource); } } } diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/HashtableExtensionsTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/HashtableExtensionsTests.cs index 2c5d21d4fc..334ac6c248 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/HashtableExtensionsTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/HashtableExtensionsTests.cs @@ -67,7 +67,8 @@ public void ToValueSet_InnerHashtable() { var ht = new Hashtable() { - { "hashtableKey", new Hashtable() + { + "hashtableKey", new Hashtable() { { "key1", "value1" }, { "key2", 2 }, diff --git a/src/nuget.config b/src/nuget.config index 095b938597..32ddd30f3b 100644 --- a/src/nuget.config +++ b/src/nuget.config @@ -3,7 +3,6 @@ - From 5df2e77317a44e412f6578862e398068d35b6f32 Mon Sep 17 00:00:00 2001 From: --global Date: Mon, 20 May 2024 13:04:14 -0700 Subject: [PATCH 04/12] try again --- azure-pipelines.yml | 1 + .../Tests/ConfigurationMixedElevationTests.cs | 52 ++++++------------- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index b537d79578..e376e0f35d 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -345,6 +345,7 @@ jobs: testSelector: 'testAssemblies' testAssemblyVer2: '**\Microsoft.Management.Configuration.UnitTests.dll' searchFolder: '$(buildOutDir)\Microsoft.Management.Configuration.UnitTests' + testFiltercriteria: 'Category!~OutOfProcOnly' codeCoverageEnabled: false platform: '$(buildPlatform)' configuration: '$(BuildConfiguration)' diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs index 8b5158b8b6..28ee5de00b 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs @@ -18,8 +18,8 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Unit tests for verifying the processor behavior for handling mixed elevation scenarios. /// [Collection("UnitTestCollection")] - [OutOfProc] [OutOfProcOnly] + [OutOfProc] public class ConfigurationMixedElevationTests : ConfigurationProcessorTestBase { private readonly UnitTestFixture fixture; @@ -104,24 +104,15 @@ public async Task ApplyElevatedUnitNotInLimitationSet() configurationSet.Metadata.Add(Helpers.Constants.DisableRunAsTestGuid, true); configurationSet.Metadata.Add(Helpers.Constants.ForceHighIntegrityUnitProcessorsTestGuid, true); - ConfigurationUnit elevationRequiredUnit = this.ConfigurationUnit(); - elevationRequiredUnit.Metadata.Add("securityContext", "elevated"); - elevationRequiredUnit.Metadata.Add("version", version.ToString()); - elevationRequiredUnit.Metadata.Add("module", moduleName); - - elevationRequiredUnit.Settings.Add("secretCode", "123456789"); - elevationRequiredUnit.Type = resourceName; - elevationRequiredUnit.Intent = ConfigurationUnitIntent.Apply; - - ConfigurationUnit nonElevatedUnit = this.ConfigurationUnit(); - nonElevatedUnit.Metadata.Add("version", version.ToString()); - nonElevatedUnit.Metadata.Add("module", moduleName); + ConfigurationUnit unit = this.ConfigurationUnit(); + unit.Metadata.Add("version", version.ToString()); + unit.Metadata.Add("module", moduleName); - nonElevatedUnit.Settings.Add("secretCode", "123456789"); - nonElevatedUnit.Type = resourceName; - nonElevatedUnit.Intent = ConfigurationUnitIntent.Apply; + unit.Settings.Add("secretCode", "123456789"); + unit.Type = resourceName; + unit.Intent = ConfigurationUnitIntent.Apply; - configurationSet.Units = new ConfigurationUnit[] { elevationRequiredUnit, nonElevatedUnit }; + configurationSet.Units = new ConfigurationUnit[] { unit }; IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync(Helpers.Constants.DynamicRuntimeHandlerIdentifier); @@ -131,26 +122,17 @@ public async Task ApplyElevatedUnitNotInLimitationSet() Assert.NotNull(result); Assert.NotNull(result.ResultCode); Assert.Equal(Errors.WINGET_CONFIG_ERROR_SET_APPLY_FAILED, result.ResultCode.HResult); - Assert.Equal(2, result.UnitResults.Count); - - // First unit should succeed since it is a unit that requires elevation. - ApplyConfigurationUnitResult firstUnitResult = result.UnitResults[0]; - Assert.NotNull(firstUnitResult); - Assert.False(firstUnitResult.PreviouslyInDesiredState); - Assert.False(firstUnitResult.RebootRequired); - Assert.NotNull(firstUnitResult.ResultInformation); - Assert.Null(firstUnitResult.ResultInformation.ResultCode); - Assert.Equal(ConfigurationUnitResultSource.None, firstUnitResult.ResultInformation.ResultSource); + Assert.Equal(1, result.UnitResults.Count); // Second unit should fail as it was not included in the limitation set. - ApplyConfigurationUnitResult secondUnitResult = result.UnitResults[1]; - Assert.NotNull(secondUnitResult); - Assert.False(secondUnitResult.PreviouslyInDesiredState); - Assert.False(secondUnitResult.RebootRequired); - Assert.NotNull(secondUnitResult.ResultInformation); - Assert.NotNull(secondUnitResult.ResultInformation.ResultCode); - Assert.Equal(Errors.COR_E_INVALIDOPERATION, secondUnitResult.ResultInformation.ResultCode.HResult); - Assert.Equal(ConfigurationUnitResultSource.None, secondUnitResult.ResultInformation.ResultSource); + ApplyConfigurationUnitResult unitResult = result.UnitResults[0]; + Assert.NotNull(unitResult); + Assert.False(unitResult.PreviouslyInDesiredState); + Assert.False(unitResult.RebootRequired); + Assert.NotNull(unitResult.ResultInformation); + Assert.NotNull(unitResult.ResultInformation.ResultCode); + Assert.Equal(Errors.COR_E_INVALIDOPERATION, unitResult.ResultInformation.ResultCode.HResult); + Assert.Equal(ConfigurationUnitResultSource.Internal, unitResult.ResultInformation.ResultSource); } } } From 03badf304e981b35460ab091fcf55af2a4946328 Mon Sep 17 00:00:00 2001 From: --global Date: Tue, 21 May 2024 17:26:33 -0700 Subject: [PATCH 05/12] remove test and add category --- azure-pipelines.yml | 2 +- .../ConfigurationDynamicRuntimeFactory.cpp | 10 +--- .../Helpers/Constants.cs | 5 -- .../Helpers/Errors.cs | 2 - .../Helpers/OutOfProcOnlyAttribute.cs | 4 +- .../Helpers/OutOfProcOnlyDiscoverer.cs | 40 ++++++++++++++++ .../Tests/ConfigurationDetailsTests.cs | 2 +- .../Tests/ConfigurationMixedElevationTests.cs | 47 ------------------- 8 files changed, 45 insertions(+), 67 deletions(-) create mode 100644 src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyDiscoverer.cs diff --git a/azure-pipelines.yml b/azure-pipelines.yml index e376e0f35d..71b88a36c6 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -345,7 +345,7 @@ jobs: testSelector: 'testAssemblies' testAssemblyVer2: '**\Microsoft.Management.Configuration.UnitTests.dll' searchFolder: '$(buildOutDir)\Microsoft.Management.Configuration.UnitTests' - testFiltercriteria: 'Category!~OutOfProcOnly' + testFiltercriteria: 'Category!=OutOfProcOnly' codeCoverageEnabled: false platform: '$(buildPlatform)' configuration: '$(BuildConfiguration)' diff --git a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp index 8660a263ca..85f5b1ded5 100644 --- a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp +++ b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp @@ -16,7 +16,6 @@ namespace AppInstaller::CLI::ConfigurationRemoting { #ifndef DISABLE_TEST_HOOKS constexpr std::wstring_view DisableRunAsTestGuid = L"1e62d683-2999-44e7-81f7-6f8f35e8d731"; - constexpr std::wstring_view ForceHighIntegrityUnitProcessorsTestGuid = L"02f64b7d-6c2e-43fa-87dd-1f265800681d"; // Checks the configuration set metadata for a specific test guid that controls the behavior flow. bool GetConfigurationSetMetadataOverride(const ConfigurationSet& configurationSet, const std::wstring_view& testGuid) @@ -67,11 +66,8 @@ namespace AppInstaller::CLI::ConfigurationRemoting { for (const auto& existingUnit : m_configurationSet.Units()) { -#ifndef DISABLE_TEST_HOOKS - Security::IntegrityLevel requiredIntegrityLevel = GetConfigurationSetMetadataOverride(m_configurationSet, ForceHighIntegrityUnitProcessorsTestGuid) ? Security::IntegrityLevel::High : GetIntegrityLevelForUnit(existingUnit); -#elif Security::IntegrityLevel requiredIntegrityLevel = GetIntegrityLevelForUnit(existingUnit); -#endif + if (m_setProcessors.find(requiredIntegrityLevel) == m_setProcessors.end()) { CreateSetProcessorForIntegrityLevel(requiredIntegrityLevel); @@ -80,11 +76,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting }); // Create set and unit processor for current unit. -#ifndef DISABLE_TEST_HOOKS - Security::IntegrityLevel requiredIntegrityLevel = GetConfigurationSetMetadataOverride(m_configurationSet, ForceHighIntegrityUnitProcessorsTestGuid) ? Security::IntegrityLevel::High : GetIntegrityLevelForUnit(unit); -#elif Security::IntegrityLevel requiredIntegrityLevel = GetIntegrityLevelForUnit(unit); -#endif auto itr = m_setProcessors.find(requiredIntegrityLevel); if (itr == m_setProcessors.end()) diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs index d69f6f1faa..46f27314ef 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs @@ -30,10 +30,5 @@ public class Constants /// Test guid for disabling the dynamic factory from setting the 'RunAs' start process verb. /// public const string DisableRunAsTestGuid = "1e62d683-2999-44e7-81f7-6f8f35e8d731"; - - /// - /// Test guid for forcing the creation of high integrity unit processors. - /// - public const string ForceHighIntegrityUnitProcessorsTestGuid = "02f64b7d-6c2e-43fa-87dd-1f265800681d"; } } diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs index 5d422d134b..c512fa32b1 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs @@ -47,8 +47,6 @@ internal static class Errors public static readonly int WINGET_CONFIG_ERROR_UNIT_IMPORT_MODULE_ADMIN = unchecked((int)0x8A15C111); public static readonly int WINGET_CONFIG_ERROR_NOT_SUPPORTED_BY_PROCESSOR = unchecked((int)0x8A15C112); - // Configuration Remoting Server Errors - public static readonly int COR_E_INVALIDOPERATION = unchecked((int)0x80131509); #pragma warning restore SA1025 // Code should not contain multiple whitespace in a row #pragma warning restore SA1600 // Elements should be documented #pragma warning restore SA1310 // Field names should not contain underscore diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyAttribute.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyAttribute.cs index 285d7a50ae..d528cbdaf3 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyAttribute.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyAttribute.cs @@ -10,9 +10,9 @@ namespace Microsoft.Management.Configuration.UnitTests.Helpers using Xunit.Sdk; /// - /// Trait used to mark a test as only for the out of proc scenario. These tests should not run for the in-proc scenario. + /// Trait used to mark a test as only for the out of proc scenario. Should not be run for the in-proc scenario /// - [TraitDiscoverer(OutOfProcDiscoverer.TypeName, Constants.AssemblyNameForTraits)] + [TraitDiscoverer(OutOfProcOnlyDiscoverer.TypeName, Constants.AssemblyNameForTraits)] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] public class OutOfProcOnlyAttribute : Attribute, ITraitAttribute { diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyDiscoverer.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyDiscoverer.cs new file mode 100644 index 0000000000..9e538da756 --- /dev/null +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyDiscoverer.cs @@ -0,0 +1,40 @@ +// ----------------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// +// ----------------------------------------------------------------------------- + +namespace Microsoft.Management.Configuration.UnitTests.Helpers +{ + using System.Collections.Generic; + using Xunit.Abstractions; + using Xunit.Sdk; + + /// + /// Enables integration with xUnit trait system. + /// + public class OutOfProcOnlyDiscoverer : ITraitDiscoverer + { + /// + /// The type name for this discoverer. + /// + public const string TypeName = Constants.NamespaceNameForTraits + ".OutOfProcOnlyDiscoverer"; + + /// + /// Initializes a new instance of the class. + /// + public OutOfProcOnlyDiscoverer() + { + } + + /// + /// Gets the trait information for the OutOfProcOnlyAttribute. + /// + /// The trait information. + /// Trait name/value pairs. + public IEnumerable> GetTraits(IAttributeInfo traitAttribute) + { + yield return new KeyValuePair("Category", "OutOfProcOnly"); + } + } +} diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationDetailsTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationDetailsTests.cs index 047b5a928d..cb20594534 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationDetailsTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationDetailsTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs index 28ee5de00b..abf6ed966d 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs @@ -18,7 +18,6 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Unit tests for verifying the processor behavior for handling mixed elevation scenarios. /// [Collection("UnitTestCollection")] - [OutOfProcOnly] [OutOfProc] public class ConfigurationMixedElevationTests : ConfigurationProcessorTestBase { @@ -88,51 +87,5 @@ public async Task ApplyUnitsWithMixedElevation() Assert.Equal(ConfigurationUnitResultSource.None, unitResult.ResultInformation.ResultSource); } } - - /// - /// Verifies that creating a high integrity unit processor for a non elevated unit should return an invalid operation result. - /// - /// A representing the asynchronous unit test. - [Fact] - public async Task ApplyElevatedUnitNotInLimitationSet() - { - string resourceName = "E2ETestResource"; - string moduleName = "xE2ETestResource"; - Version version = new Version("0.0.0.1"); - - ConfigurationSet configurationSet = this.ConfigurationSet(); - configurationSet.Metadata.Add(Helpers.Constants.DisableRunAsTestGuid, true); - configurationSet.Metadata.Add(Helpers.Constants.ForceHighIntegrityUnitProcessorsTestGuid, true); - - ConfigurationUnit unit = this.ConfigurationUnit(); - unit.Metadata.Add("version", version.ToString()); - unit.Metadata.Add("module", moduleName); - - unit.Settings.Add("secretCode", "123456789"); - unit.Type = resourceName; - unit.Intent = ConfigurationUnitIntent.Apply; - - configurationSet.Units = new ConfigurationUnit[] { unit }; - - IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync(Helpers.Constants.DynamicRuntimeHandlerIdentifier); - - ConfigurationProcessor processor = this.CreateConfigurationProcessorWithDiagnostics(dynamicFactory); - - ApplyConfigurationSetResult result = processor.ApplySet(configurationSet, ApplyConfigurationSetFlags.None); - Assert.NotNull(result); - Assert.NotNull(result.ResultCode); - Assert.Equal(Errors.WINGET_CONFIG_ERROR_SET_APPLY_FAILED, result.ResultCode.HResult); - Assert.Equal(1, result.UnitResults.Count); - - // Second unit should fail as it was not included in the limitation set. - ApplyConfigurationUnitResult unitResult = result.UnitResults[0]; - Assert.NotNull(unitResult); - Assert.False(unitResult.PreviouslyInDesiredState); - Assert.False(unitResult.RebootRequired); - Assert.NotNull(unitResult.ResultInformation); - Assert.NotNull(unitResult.ResultInformation.ResultCode); - Assert.Equal(Errors.COR_E_INVALIDOPERATION, unitResult.ResultInformation.ResultCode.HResult); - Assert.Equal(ConfigurationUnitResultSource.Internal, unitResult.ResultInformation.ResultSource); - } } } From 9ede1487748e2019bb75e9e88138c912641d0dd4 Mon Sep 17 00:00:00 2001 From: --global Date: Wed, 22 May 2024 11:10:34 -0700 Subject: [PATCH 06/12] fix out of proc tag --- .../Tests/ConfigurationMixedElevationTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs index abf6ed966d..4cde4ca411 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs @@ -19,6 +19,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// [Collection("UnitTestCollection")] [OutOfProc] + [OutOfProcOnly] public class ConfigurationMixedElevationTests : ConfigurationProcessorTestBase { private readonly UnitTestFixture fixture; From 65d249a8aae8941bc74f44b90035757978630a7d Mon Sep 17 00:00:00 2001 From: --global Date: Fri, 24 May 2024 12:12:58 -0700 Subject: [PATCH 07/12] respond to feedback --- .../ConfigurationDynamicRuntimeFactory.cpp | 34 +++++-- .../Helpers/Constants.cs | 14 ++- .../Helpers/Errors.cs | 3 + .../Tests/ConfigurationMixedElevationTests.cs | 98 +++++++++++++++++-- 4 files changed, 133 insertions(+), 16 deletions(-) diff --git a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp index 85f5b1ded5..b20ce91e17 100644 --- a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp +++ b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp @@ -15,7 +15,9 @@ namespace AppInstaller::CLI::ConfigurationRemoting namespace anonymous { #ifndef DISABLE_TEST_HOOKS - constexpr std::wstring_view DisableRunAsTestGuid = L"1e62d683-2999-44e7-81f7-6f8f35e8d731"; + constexpr std::wstring_view EnableTestModeTestGuid = L"1e62d683-2999-44e7-81f7-6f8f35e8d731"; + constexpr std::wstring_view ForceHighIntegrityLevelUnitsTestGuid = L"f698d20f-3584-4f28-bc75-28037e08e651"; + constexpr std::wstring_view EnableRestrictedIntegrityLevelTestGuid = L"1e62d683-2999-44e7-81f7-6f8f35e8d731"; // Checks the configuration set metadata for a specific test guid that controls the behavior flow. bool GetConfigurationSetMetadataOverride(const ConfigurationSet& configurationSet, const std::wstring_view& testGuid) @@ -46,7 +48,12 @@ namespace AppInstaller::CLI::ConfigurationRemoting DynamicSetProcessor(IConfigurationSetProcessorFactory defaultRemoteFactory, IConfigurationSetProcessor defaultRemoteSetProcessor, const ConfigurationSet& configurationSet) : m_configurationSet(configurationSet) { +#ifndef DISABLE_TEST_HOOKS + m_currentIntegrityLevel = GetConfigurationSetMetadataOverride(m_configurationSet, EnableTestModeTestGuid) ? Security::IntegrityLevel::Medium : Security::GetEffectiveIntegrityLevel(); +#elif m_currentIntegrityLevel = Security::GetEffectiveIntegrityLevel(); +#endif + m_setProcessors.emplace(m_currentIntegrityLevel, DynamicProcessorInfo{ defaultRemoteFactory, defaultRemoteSetProcessor }); } @@ -76,7 +83,11 @@ namespace AppInstaller::CLI::ConfigurationRemoting }); // Create set and unit processor for current unit. +#ifndef DISABLE_TEST_HOOKS + Security::IntegrityLevel requiredIntegrityLevel = GetConfigurationSetMetadataOverride(m_configurationSet, ForceHighIntegrityLevelUnitsTestGuid) ? Security::IntegrityLevel::High : GetIntegrityLevelForUnit(unit); +#elif Security::IntegrityLevel requiredIntegrityLevel = GetIntegrityLevelForUnit(unit); +#endif auto itr = m_setProcessors.find(requiredIntegrityLevel); if (itr == m_setProcessors.end()) @@ -99,11 +110,23 @@ namespace AppInstaller::CLI::ConfigurationRemoting } else if (securityContextLower == L"restricted") { +#ifndef DISABLE_TEST_HOOKS + if (GetConfigurationSetMetadataOverride(m_configurationSet, EnableRestrictedIntegrityLevelTestGuid)) + { + return Security::IntegrityLevel::Medium; + } + else + { + THROW_WIN32(ERROR_NOT_SUPPORTED); + } +#elif + // Not supporting elevated callers downgrading at the moment. THROW_WIN32(ERROR_NOT_SUPPORTED); // Technically this means the default level of the user token, so if UAC is disabled it would be the only integrity level (aka current). //return Security::IntegrityLevel::Medium; +#endif } else if (securityContextLower == L"current") { @@ -192,13 +215,10 @@ namespace AppInstaller::CLI::ConfigurationRemoting // If we got here, the only option is that the current integrity level is not High. if (integrityLevel == Security::IntegrityLevel::High) { - bool useRunAs = true; - #ifndef DISABLE_TEST_HOOKS - if (GetConfigurationSetMetadataOverride(m_configurationSet, DisableRunAsTestGuid)) - { - useRunAs = false; - } + bool useRunAs = !GetConfigurationSetMetadataOverride(m_configurationSet, EnableTestModeTestGuid); +#elif + bool useRunAs = true; #endif factory = CreateOutOfProcessFactory(useRunAs, SerializeSetProperties(), SerializeHighIntegrityLevelSet()); diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs index 46f27314ef..3a0e4a1587 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Constants.cs @@ -27,8 +27,18 @@ public class Constants public const string DynamicRuntimeHandlerIdentifier = "{73fea39f-6f4a-41c9-ba94-6fd14d633e40}"; /// - /// Test guid for disabling the dynamic factory from setting the 'RunAs' start process verb. + /// Test guid for enabling test mode for the dynamic runtime factory. Forces factory to exclude 'runas' verb and sets current IL to medium. /// - public const string DisableRunAsTestGuid = "1e62d683-2999-44e7-81f7-6f8f35e8d731"; + public const string EnableDynamicFactoryTestMode = "1e62d683-2999-44e7-81f7-6f8f35e8d731"; + + /// + /// Test guid for allowing the restricted integrity level to be supported. + /// + public const string EnableRestrictedIntegrityLevelTestGuid = "5cae3226-185f-4289-815c-3c089d238dc6"; + + /// + /// Test guid for forcing units to have a high integrity level during the final routing of unit processor creation. + /// + public const string ForceHighIntegrityLevelUnitsTestGuid = "f698d20f-3584-4f28-bc75-28037e08e651"; } } diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs index c512fa32b1..b44a039e0d 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/Errors.cs @@ -47,6 +47,9 @@ internal static class Errors public static readonly int WINGET_CONFIG_ERROR_UNIT_IMPORT_MODULE_ADMIN = unchecked((int)0x8A15C111); public static readonly int WINGET_CONFIG_ERROR_NOT_SUPPORTED_BY_PROCESSOR = unchecked((int)0x8A15C112); + // Limitation Set Errors + public static readonly int CORE_INVALID_OPERATION = unchecked((int)0x80131509); + #pragma warning restore SA1025 // Code should not contain multiple whitespace in a row #pragma warning restore SA1600 // Elements should be documented #pragma warning restore SA1310 // Field names should not contain underscore diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs index 4cde4ca411..2d47369576 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs @@ -38,32 +38,72 @@ public ConfigurationMixedElevationTests(UnitTestFixture fixture, ITestOutputHelp } /// - /// Verifies that a set of units with mixed elevation can run successfully. + /// Verifies that creating a high integrity unit processor. Must run elevated. /// /// A representing the asynchronous unit test. [Fact] - public async Task ApplyUnitsWithMixedElevation() + public async Task ApplyElevatedUnit() { string resourceName = "E2ETestResource"; string moduleName = "xE2ETestResource"; Version version = new Version("0.0.0.1"); ConfigurationSet configurationSet = this.ConfigurationSet(); - configurationSet.Metadata.Add(Helpers.Constants.DisableRunAsTestGuid, true); + + ConfigurationUnit unit = this.ConfigurationUnit(); + unit.Metadata.Add("version", version.ToString()); + unit.Metadata.Add("module", moduleName); + unit.Metadata.Add("securityContext", "elevated"); + unit.Type = resourceName; + unit.Intent = ConfigurationUnitIntent.Apply; + + configurationSet.Units = new ConfigurationUnit[] { unit }; + + IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync(Helpers.Constants.DynamicRuntimeHandlerIdentifier); + + ConfigurationProcessor processor = this.CreateConfigurationProcessorWithDiagnostics(dynamicFactory); + + ApplyConfigurationSetResult result = processor.ApplySet(configurationSet, ApplyConfigurationSetFlags.None); + Assert.NotNull(result); + Assert.Null(result.ResultCode); + Assert.Equal(1, result.UnitResults.Count); + + foreach (var unitResult in result.UnitResults) + { + Assert.NotNull(unitResult); + Assert.False(unitResult.PreviouslyInDesiredState); + Assert.False(unitResult.RebootRequired); + Assert.NotNull(unitResult.ResultInformation); + Assert.Null(unitResult.ResultInformation.ResultCode); + Assert.Equal(ConfigurationUnitResultSource.None, unitResult.ResultInformation.ResultSource); + } + } + + /// + /// Verifies the behavior of running a restricted unit. + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task ApplyRestrictedUnit() + { + string resourceName = "E2ETestResource"; + string moduleName = "xE2ETestResource"; + Version version = new Version("0.0.0.1"); + + ConfigurationSet configurationSet = this.ConfigurationSet(); + configurationSet.Metadata.Add(Helpers.Constants.EnableDynamicFactoryTestMode, true); + configurationSet.Metadata.Add(Helpers.Constants.EnableRestrictedIntegrityLevelTestGuid, true); ConfigurationUnit elevationRequiredUnit = this.ConfigurationUnit(); - elevationRequiredUnit.Metadata.Add("securityContext", "elevated"); + elevationRequiredUnit.Metadata.Add("securityContext", "restricted"); elevationRequiredUnit.Metadata.Add("version", version.ToString()); elevationRequiredUnit.Metadata.Add("module", moduleName); - - elevationRequiredUnit.Settings.Add("secretCode", "123456789"); elevationRequiredUnit.Type = resourceName; elevationRequiredUnit.Intent = ConfigurationUnitIntent.Apply; ConfigurationUnit unit = this.ConfigurationUnit(); unit.Metadata.Add("version", version.ToString()); unit.Metadata.Add("module", moduleName); - unit.Settings.Add("secretCode", "123456789"); unit.Type = resourceName; unit.Intent = ConfigurationUnitIntent.Apply; @@ -88,5 +128,49 @@ public async Task ApplyUnitsWithMixedElevation() Assert.Equal(ConfigurationUnitResultSource.None, unitResult.ResultInformation.ResultSource); } } + + /// + /// Verifies that creating a high integrity unit processor for a non elevated unit should return an invalid operation result. + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task ApplyUnitNotInLimitationSet() + { + string resourceName = "E2ETestResource"; + string moduleName = "xE2ETestResource"; + Version version = new Version("0.0.0.1"); + + ConfigurationSet configurationSet = this.ConfigurationSet(); + configurationSet.Metadata.Add(Helpers.Constants.EnableDynamicFactoryTestMode, true); + configurationSet.Metadata.Add(Helpers.Constants.ForceHighIntegrityLevelUnitsTestGuid, true); + configurationSet.Metadata.Add(Helpers.Constants.EnableRestrictedIntegrityLevelTestGuid, true); + + ConfigurationUnit unit = this.ConfigurationUnit(); + unit.Metadata.Add("version", version.ToString()); + unit.Metadata.Add("module", moduleName); + unit.Type = resourceName; + unit.Intent = ConfigurationUnitIntent.Apply; + + configurationSet.Units = new ConfigurationUnit[] { unit }; + + IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync(Helpers.Constants.DynamicRuntimeHandlerIdentifier); + + ConfigurationProcessor processor = this.CreateConfigurationProcessorWithDiagnostics(dynamicFactory); + + ApplyConfigurationSetResult result = processor.ApplySet(configurationSet, ApplyConfigurationSetFlags.None); + Assert.NotNull(result); + Assert.NotNull(result.ResultCode); + Assert.Equal(Errors.WINGET_CONFIG_ERROR_SET_APPLY_FAILED, result.ResultCode.HResult); + Assert.Equal(1, result.UnitResults.Count); + + ApplyConfigurationUnitResult unitResult = result.UnitResults[0]; + Assert.NotNull(unitResult); + Assert.False(unitResult.PreviouslyInDesiredState); + Assert.False(unitResult.RebootRequired); + Assert.NotNull(unitResult.ResultInformation); + Assert.NotNull(unitResult.ResultInformation.ResultCode); + Assert.Equal(Errors.CORE_INVALID_OPERATION, unitResult.ResultInformation.ResultCode.HResult); + Assert.Equal(ConfigurationUnitResultSource.Internal, unitResult.ResultInformation.ResultSource); + } } } From d62ac701af6e9ef5af95a5943d3012091c853adc Mon Sep 17 00:00:00 2001 From: --global Date: Fri, 31 May 2024 14:18:10 -0700 Subject: [PATCH 08/12] add resource to check PID --- .../ConfigurationDynamicRuntimeFactory.cpp | 2 +- .../xE2ETestResource/xE2ETestResource.psd1 | 3 +- .../xE2ETestResource/xE2ETestResource.psm1 | 38 ++++++- .../Tests/ConfigurationMixedElevationTests.cs | 98 ++++++++----------- 4 files changed, 83 insertions(+), 58 deletions(-) diff --git a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp index b20ce91e17..54c6d9e9e2 100644 --- a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp +++ b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp @@ -17,7 +17,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting #ifndef DISABLE_TEST_HOOKS constexpr std::wstring_view EnableTestModeTestGuid = L"1e62d683-2999-44e7-81f7-6f8f35e8d731"; constexpr std::wstring_view ForceHighIntegrityLevelUnitsTestGuid = L"f698d20f-3584-4f28-bc75-28037e08e651"; - constexpr std::wstring_view EnableRestrictedIntegrityLevelTestGuid = L"1e62d683-2999-44e7-81f7-6f8f35e8d731"; + constexpr std::wstring_view EnableRestrictedIntegrityLevelTestGuid = L"5cae3226-185f-4289-815c-3c089d238dc6"; // Checks the configuration set metadata for a specific test guid that controls the behavior flow. bool GetConfigurationSetMetadataOverride(const ConfigurationSet& configurationSet, const std::wstring_view& testGuid) diff --git a/src/AppInstallerCLIE2ETests/TestData/Configuration/Modules/xE2ETestResource/xE2ETestResource.psd1 b/src/AppInstallerCLIE2ETests/TestData/Configuration/Modules/xE2ETestResource/xE2ETestResource.psd1 index 6a438af986..991e73c94c 100644 --- a/src/AppInstallerCLIE2ETests/TestData/Configuration/Modules/xE2ETestResource/xE2ETestResource.psd1 +++ b/src/AppInstallerCLIE2ETests/TestData/Configuration/Modules/xE2ETestResource/xE2ETestResource.psd1 @@ -1,4 +1,4 @@ -# +# # Module manifest for module 'xE2ETestResource' # @@ -22,6 +22,7 @@ DscResourcesToExport = @( 'E2ETestResourceError' 'E2ETestResourceTypes' 'E2ETestResourceCrash' + 'E2ETestResourcePID' ) HelpInfoURI = 'https://www.contoso.com/help' diff --git a/src/AppInstallerCLIE2ETests/TestData/Configuration/Modules/xE2ETestResource/xE2ETestResource.psm1 b/src/AppInstallerCLIE2ETests/TestData/Configuration/Modules/xE2ETestResource/xE2ETestResource.psm1 index 1e34708d62..99d5054190 100644 --- a/src/AppInstallerCLIE2ETests/TestData/Configuration/Modules/xE2ETestResource/xE2ETestResource.psm1 +++ b/src/AppInstallerCLIE2ETests/TestData/Configuration/Modules/xE2ETestResource/xE2ETestResource.psm1 @@ -1,4 +1,4 @@ -# E2E module with resources. +# E2E module with resources. enum Ensure { @@ -288,3 +288,39 @@ class E2ETestResourceCrash [System.Environment]::Exit(0) } } + +# This resource writes the current PID to the provided file path. +[DscResource()] +class E2ETestResourcePID +{ + [DscProperty(Key)] + [string] $key + + [DscProperty(Mandatory)] + [string] $directoryPath + + [E2ETestResourcePID] Get() + { + $result = @{ + key = "E2ETestResourcePID" + directoryPath = $this.directoryPath + } + + return $result + } + + [bool] Test() + { + return $false + } + + [void] Set() + { + if (Test-Path -Path $this.directoryPath) + { + $processId = [System.Diagnostics.Process]::GetCurrentProcess().Id + $filePath = Join-Path -Path $this.directoryPath -ChildPath "$processId.txt" + New-Item -Path $filePath -ItemType File -Force + } + } +} diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs index 2d47369576..f98ce6ca60 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs @@ -7,10 +7,10 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests { using System; + using System.IO; using System.Threading.Tasks; using Microsoft.Management.Configuration.UnitTests.Fixtures; using Microsoft.Management.Configuration.UnitTests.Helpers; - using Microsoft.VisualBasic; using Xunit; using Xunit.Abstractions; @@ -38,82 +38,51 @@ public ConfigurationMixedElevationTests(UnitTestFixture fixture, ITestOutputHelp } /// - /// Verifies that creating a high integrity unit processor. Must run elevated. + /// Verifies that applying units of mixed elevation is successful. Also verifies that the elevated processor has a different process id. /// /// A representing the asynchronous unit test. [Fact] - public async Task ApplyElevatedUnit() + public async Task ApplyMixedElevationUnits() { - string resourceName = "E2ETestResource"; + string resourceName = "E2ETestResourcePID"; string moduleName = "xE2ETestResource"; Version version = new Version("0.0.0.1"); + string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + Directory.CreateDirectory(tempDirectory); + ConfigurationSet configurationSet = this.ConfigurationSet(); + configurationSet.Metadata.Add(Helpers.Constants.EnableDynamicFactoryTestMode, true); ConfigurationUnit unit = this.ConfigurationUnit(); unit.Metadata.Add("version", version.ToString()); unit.Metadata.Add("module", moduleName); - unit.Metadata.Add("securityContext", "elevated"); + unit.Settings.Add("directoryPath", tempDirectory); unit.Type = resourceName; unit.Intent = ConfigurationUnitIntent.Apply; - configurationSet.Units = new ConfigurationUnit[] { unit }; + ConfigurationUnit elevatedUnit = this.ConfigurationUnit(); + elevatedUnit.Metadata.Add("version", version.ToString()); + elevatedUnit.Metadata.Add("module", moduleName); + elevatedUnit.Metadata.Add("securityContext", "elevated"); + elevatedUnit.Settings.Add("directoryPath", tempDirectory); + elevatedUnit.Type = resourceName; + elevatedUnit.Intent = ConfigurationUnitIntent.Apply; + + configurationSet.Units = new ConfigurationUnit[] { unit, elevatedUnit }; IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync(Helpers.Constants.DynamicRuntimeHandlerIdentifier); ConfigurationProcessor processor = this.CreateConfigurationProcessorWithDiagnostics(dynamicFactory); ApplyConfigurationSetResult result = processor.ApplySet(configurationSet, ApplyConfigurationSetFlags.None); - Assert.NotNull(result); - Assert.Null(result.ResultCode); - Assert.Equal(1, result.UnitResults.Count); - foreach (var unitResult in result.UnitResults) - { - Assert.NotNull(unitResult); - Assert.False(unitResult.PreviouslyInDesiredState); - Assert.False(unitResult.RebootRequired); - Assert.NotNull(unitResult.ResultInformation); - Assert.Null(unitResult.ResultInformation.ResultCode); - Assert.Equal(ConfigurationUnitResultSource.None, unitResult.ResultInformation.ResultSource); - } - } + // Get the number of unique PIDs from temp directory. + int pidCount = Directory.GetFiles(tempDirectory).Length; - /// - /// Verifies the behavior of running a restricted unit. - /// - /// A representing the asynchronous unit test. - [Fact] - public async Task ApplyRestrictedUnit() - { - string resourceName = "E2ETestResource"; - string moduleName = "xE2ETestResource"; - Version version = new Version("0.0.0.1"); - - ConfigurationSet configurationSet = this.ConfigurationSet(); - configurationSet.Metadata.Add(Helpers.Constants.EnableDynamicFactoryTestMode, true); - configurationSet.Metadata.Add(Helpers.Constants.EnableRestrictedIntegrityLevelTestGuid, true); - - ConfigurationUnit elevationRequiredUnit = this.ConfigurationUnit(); - elevationRequiredUnit.Metadata.Add("securityContext", "restricted"); - elevationRequiredUnit.Metadata.Add("version", version.ToString()); - elevationRequiredUnit.Metadata.Add("module", moduleName); - elevationRequiredUnit.Type = resourceName; - elevationRequiredUnit.Intent = ConfigurationUnitIntent.Apply; - - ConfigurationUnit unit = this.ConfigurationUnit(); - unit.Metadata.Add("version", version.ToString()); - unit.Metadata.Add("module", moduleName); - unit.Type = resourceName; - unit.Intent = ConfigurationUnitIntent.Apply; - - configurationSet.Units = new ConfigurationUnit[] { elevationRequiredUnit, unit }; - - IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync(Helpers.Constants.DynamicRuntimeHandlerIdentifier); - - ConfigurationProcessor processor = this.CreateConfigurationProcessorWithDiagnostics(dynamicFactory); + // Clean up temp directory folder. + Directory.Delete(tempDirectory, true); - ApplyConfigurationSetResult result = processor.ApplySet(configurationSet, ApplyConfigurationSetFlags.None); Assert.NotNull(result); Assert.Null(result.ResultCode); Assert.Equal(2, result.UnitResults.Count); @@ -127,6 +96,9 @@ public async Task ApplyRestrictedUnit() Assert.Null(unitResult.ResultInformation.ResultCode); Assert.Equal(ConfigurationUnitResultSource.None, unitResult.ResultInformation.ResultSource); } + + // There should be exactly 2 unique PIDs, one for each integrity level. + Assert.Equal(2, pidCount); } /// @@ -151,7 +123,14 @@ public async Task ApplyUnitNotInLimitationSet() unit.Type = resourceName; unit.Intent = ConfigurationUnitIntent.Apply; - configurationSet.Units = new ConfigurationUnit[] { unit }; + ConfigurationUnit elevatedUnit = this.ConfigurationUnit(); + elevatedUnit.Metadata.Add("version", version.ToString()); + elevatedUnit.Metadata.Add("module", moduleName); + elevatedUnit.Metadata.Add("securityContext", "elevated"); + elevatedUnit.Type = resourceName; + elevatedUnit.Intent = ConfigurationUnitIntent.Apply; + + configurationSet.Units = new ConfigurationUnit[] { unit, elevatedUnit }; IConfigurationSetProcessorFactory dynamicFactory = await this.fixture.ConfigurationStatics.CreateConfigurationSetProcessorFactoryAsync(Helpers.Constants.DynamicRuntimeHandlerIdentifier); @@ -161,7 +140,7 @@ public async Task ApplyUnitNotInLimitationSet() Assert.NotNull(result); Assert.NotNull(result.ResultCode); Assert.Equal(Errors.WINGET_CONFIG_ERROR_SET_APPLY_FAILED, result.ResultCode.HResult); - Assert.Equal(1, result.UnitResults.Count); + Assert.Equal(2, result.UnitResults.Count); ApplyConfigurationUnitResult unitResult = result.UnitResults[0]; Assert.NotNull(unitResult); @@ -171,6 +150,15 @@ public async Task ApplyUnitNotInLimitationSet() Assert.NotNull(unitResult.ResultInformation.ResultCode); Assert.Equal(Errors.CORE_INVALID_OPERATION, unitResult.ResultInformation.ResultCode.HResult); Assert.Equal(ConfigurationUnitResultSource.Internal, unitResult.ResultInformation.ResultSource); + + // Elevated unit should still succeed when applied. + ApplyConfigurationUnitResult elevatedUnitResult = result.UnitResults[1]; + Assert.NotNull(elevatedUnitResult); + Assert.False(elevatedUnitResult.PreviouslyInDesiredState); + Assert.False(elevatedUnitResult.RebootRequired); + Assert.NotNull(elevatedUnitResult.ResultInformation); + Assert.Null(elevatedUnitResult.ResultInformation.ResultCode); + Assert.Equal(ConfigurationUnitResultSource.None, elevatedUnitResult.ResultInformation.ResultSource); } } } From ebc649803ebd26f70a537ae848171e240718a904 Mon Sep 17 00:00:00 2001 From: --global Date: Tue, 4 Jun 2024 09:57:10 -0700 Subject: [PATCH 09/12] address yao's comments --- src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp index 54c6d9e9e2..cffd022eec 100644 --- a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp +++ b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp @@ -215,10 +215,9 @@ namespace AppInstaller::CLI::ConfigurationRemoting // If we got here, the only option is that the current integrity level is not High. if (integrityLevel == Security::IntegrityLevel::High) { + bool useRunAs = true; #ifndef DISABLE_TEST_HOOKS bool useRunAs = !GetConfigurationSetMetadataOverride(m_configurationSet, EnableTestModeTestGuid); -#elif - bool useRunAs = true; #endif factory = CreateOutOfProcessFactory(useRunAs, SerializeSetProperties(), SerializeHighIntegrityLevelSet()); From fc1987ced65280ce58483e80062ebe5144ef79e5 Mon Sep 17 00:00:00 2001 From: --global Date: Tue, 4 Jun 2024 10:37:21 -0700 Subject: [PATCH 10/12] fix --- src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp index cffd022eec..40e67cd332 100644 --- a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp +++ b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp @@ -217,7 +217,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting { bool useRunAs = true; #ifndef DISABLE_TEST_HOOKS - bool useRunAs = !GetConfigurationSetMetadataOverride(m_configurationSet, EnableTestModeTestGuid); + useRunAs = !GetConfigurationSetMetadataOverride(m_configurationSet, EnableTestModeTestGuid); #endif factory = CreateOutOfProcessFactory(useRunAs, SerializeSetProperties(), SerializeHighIntegrityLevelSet()); From 4ed24dc1cb52211585667d1e5779b131c1f88535 Mon Sep 17 00:00:00 2001 From: --global Date: Fri, 7 Jun 2024 07:19:04 -0700 Subject: [PATCH 11/12] add inproc label and fix macro statements --- azure-pipelines.yml | 2 +- .../ConfigurationDynamicRuntimeFactory.cpp | 35 +++++++++++-------- ...rocOnlyAttribute.cs => InProcAttribute.cs} | 12 +++---- ...cOnlyDiscoverer.cs => InProcDiscoverer.cs} | 14 ++++---- .../Tests/ConfigurationDetailsTests.cs | 2 ++ .../Tests/ConfigurationMixedElevationTests.cs | 1 - .../Tests/ConfigurationProcessorApplyTests.cs | 3 +- .../ConfigurationProcessorFactoryTests.cs | 2 ++ .../ConfigurationProcessorGetAllTests.cs | 3 +- .../Tests/ConfigurationProcessorGetTests.cs | 1 + .../Tests/ConfigurationProcessorGroupTests.cs | 1 + .../ConfigurationProcessorTelemetryTests.cs | 3 +- .../Tests/ConfigurationProcessorTestTests.cs | 3 +- .../Tests/ConfigurationSetAuthoringTests.cs | 1 + .../Tests/ConfigurationSetProcessorTests.cs | 1 + .../Tests/ConfigurationUnitInternalTests.cs | 3 +- .../Tests/ConfigurationUnitProcessorTests.cs | 2 ++ .../DscModuleV2SimpleFileResourceTests.cs | 3 +- .../Tests/DscModuleV2Tests.cs | 3 +- .../Tests/DscResourceMapTests.cs | 4 ++- .../Tests/ExceptionExtensionsTests.cs | 4 ++- .../Tests/HashtableExtensionsTests.cs | 6 ++-- .../Tests/OpenConfigurationSetTests.cs | 1 + .../Tests/PowerShellHelperTests.cs | 4 ++- .../Tests/ProcessorEnvironmentTests.cs | 4 ++- .../Tests/SemanticVersionTests.cs | 4 ++- .../Tests/TypeHelpersTests.cs | 3 +- .../Tests/ValueSetExtensionsTests.cs | 2 ++ 28 files changed, 83 insertions(+), 44 deletions(-) rename src/Microsoft.Management.Configuration.UnitTests/Helpers/{OutOfProcOnlyAttribute.cs => InProcAttribute.cs} (52%) rename src/Microsoft.Management.Configuration.UnitTests/Helpers/{OutOfProcOnlyDiscoverer.cs => InProcDiscoverer.cs} (70%) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 71b88a36c6..4a99fb3d4a 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -345,7 +345,7 @@ jobs: testSelector: 'testAssemblies' testAssemblyVer2: '**\Microsoft.Management.Configuration.UnitTests.dll' searchFolder: '$(buildOutDir)\Microsoft.Management.Configuration.UnitTests' - testFiltercriteria: 'Category!=OutOfProcOnly' + testFiltercriteria: 'Category=InProc' codeCoverageEnabled: false platform: '$(buildPlatform)' configuration: '$(BuildConfiguration)' diff --git a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp index 40e67cd332..af1860eadf 100644 --- a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp +++ b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp @@ -49,8 +49,12 @@ namespace AppInstaller::CLI::ConfigurationRemoting DynamicSetProcessor(IConfigurationSetProcessorFactory defaultRemoteFactory, IConfigurationSetProcessor defaultRemoteSetProcessor, const ConfigurationSet& configurationSet) : m_configurationSet(configurationSet) { #ifndef DISABLE_TEST_HOOKS - m_currentIntegrityLevel = GetConfigurationSetMetadataOverride(m_configurationSet, EnableTestModeTestGuid) ? Security::IntegrityLevel::Medium : Security::GetEffectiveIntegrityLevel(); -#elif + m_enableTestMode = GetConfigurationSetMetadataOverride(m_configurationSet, EnableTestModeTestGuid); + m_enableRestrictedIntegrityLevel = GetConfigurationSetMetadataOverride(m_configurationSet, EnableRestrictedIntegrityLevelTestGuid); + m_forceHighIntegrityLevelUnits = GetConfigurationSetMetadataOverride(m_configurationSet, ForceHighIntegrityLevelUnitsTestGuid); + + m_currentIntegrityLevel = m_enableTestMode ? Security::IntegrityLevel::Medium : Security::GetEffectiveIntegrityLevel(); +#else m_currentIntegrityLevel = Security::GetEffectiveIntegrityLevel(); #endif @@ -84,8 +88,8 @@ namespace AppInstaller::CLI::ConfigurationRemoting // Create set and unit processor for current unit. #ifndef DISABLE_TEST_HOOKS - Security::IntegrityLevel requiredIntegrityLevel = GetConfigurationSetMetadataOverride(m_configurationSet, ForceHighIntegrityLevelUnitsTestGuid) ? Security::IntegrityLevel::High : GetIntegrityLevelForUnit(unit); -#elif + Security::IntegrityLevel requiredIntegrityLevel = m_forceHighIntegrityLevelUnits ? Security::IntegrityLevel::High : GetIntegrityLevelForUnit(unit); +#else Security::IntegrityLevel requiredIntegrityLevel = GetIntegrityLevelForUnit(unit); #endif @@ -111,22 +115,19 @@ namespace AppInstaller::CLI::ConfigurationRemoting else if (securityContextLower == L"restricted") { #ifndef DISABLE_TEST_HOOKS - if (GetConfigurationSetMetadataOverride(m_configurationSet, EnableRestrictedIntegrityLevelTestGuid)) + if (m_enableRestrictedIntegrityLevel) { return Security::IntegrityLevel::Medium; } else +#endif { + // Not supporting elevated callers downgrading at the moment. THROW_WIN32(ERROR_NOT_SUPPORTED); - } -#elif - - // Not supporting elevated callers downgrading at the moment. - THROW_WIN32(ERROR_NOT_SUPPORTED); - // Technically this means the default level of the user token, so if UAC is disabled it would be the only integrity level (aka current). - //return Security::IntegrityLevel::Medium; -#endif + // Technically this means the default level of the user token, so if UAC is disabled it would be the only integrity level (aka current). + // return Security::IntegrityLevel::Medium; + } } else if (securityContextLower == L"current") { @@ -217,7 +218,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting { bool useRunAs = true; #ifndef DISABLE_TEST_HOOKS - useRunAs = !GetConfigurationSetMetadataOverride(m_configurationSet, EnableTestModeTestGuid); + useRunAs = !m_enableTestMode; #endif factory = CreateOutOfProcessFactory(useRunAs, SerializeSetProperties(), SerializeHighIntegrityLevelSet()); @@ -234,6 +235,12 @@ namespace AppInstaller::CLI::ConfigurationRemoting ProcessorMap m_setProcessors; ConfigurationSet m_configurationSet; std::once_flag m_createUnitSetProcessorsOnce; + +#ifndef DISABLE_TEST_HOOKS + bool m_enableTestMode = false; + bool m_enableRestrictedIntegrityLevel = false; + bool m_forceHighIntegrityLevelUnits = false; +#endif }; // This is implemented completely in the packaged context for now, if we want to make it more configurable, we will probably want to move it to configuration and diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyAttribute.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/InProcAttribute.cs similarity index 52% rename from src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyAttribute.cs rename to src/Microsoft.Management.Configuration.UnitTests/Helpers/InProcAttribute.cs index d528cbdaf3..b5d9da91eb 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyAttribute.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/InProcAttribute.cs @@ -1,5 +1,5 @@ // ----------------------------------------------------------------------------- -// +// // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // // ----------------------------------------------------------------------------- @@ -10,16 +10,16 @@ namespace Microsoft.Management.Configuration.UnitTests.Helpers using Xunit.Sdk; /// - /// Trait used to mark a test as only for the out of proc scenario. Should not be run for the in-proc scenario + /// Trait used to mark a test as only for the in proc scenario. /// - [TraitDiscoverer(OutOfProcOnlyDiscoverer.TypeName, Constants.AssemblyNameForTraits)] + [TraitDiscoverer(InProcDiscoverer.TypeName, Constants.AssemblyNameForTraits)] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] - public class OutOfProcOnlyAttribute : Attribute, ITraitAttribute + public class InProcAttribute : Attribute, ITraitAttribute { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - public OutOfProcOnlyAttribute() + public InProcAttribute() { } } diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyDiscoverer.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/InProcDiscoverer.cs similarity index 70% rename from src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyDiscoverer.cs rename to src/Microsoft.Management.Configuration.UnitTests/Helpers/InProcDiscoverer.cs index 9e538da756..7f7afb6a46 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Helpers/OutOfProcOnlyDiscoverer.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/InProcDiscoverer.cs @@ -1,5 +1,5 @@ // ----------------------------------------------------------------------------- -// +// // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // // ----------------------------------------------------------------------------- @@ -13,28 +13,28 @@ namespace Microsoft.Management.Configuration.UnitTests.Helpers /// /// Enables integration with xUnit trait system. /// - public class OutOfProcOnlyDiscoverer : ITraitDiscoverer + public class InProcDiscoverer : ITraitDiscoverer { /// /// The type name for this discoverer. /// - public const string TypeName = Constants.NamespaceNameForTraits + ".OutOfProcOnlyDiscoverer"; + public const string TypeName = Constants.NamespaceNameForTraits + ".InProcDiscoverer"; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - public OutOfProcOnlyDiscoverer() + public InProcDiscoverer() { } /// - /// Gets the trait information for the OutOfProcOnlyAttribute. + /// Gets the trait information for the InProcAttribute. /// /// The trait information. /// Trait name/value pairs. public IEnumerable> GetTraits(IAttributeInfo traitAttribute) { - yield return new KeyValuePair("Category", "OutOfProcOnly"); + yield return new KeyValuePair("Category", "InProc"); } } } diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationDetailsTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationDetailsTests.cs index cb20594534..a1946f693b 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationDetailsTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationDetailsTests.cs @@ -14,6 +14,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests using Microsoft.Management.Configuration.Processor.Helpers; using Microsoft.Management.Configuration.Processor.Unit; using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; using Windows.Security.Cryptography.Certificates; using Xunit; using Xunit.Abstractions; @@ -22,6 +23,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Tests for ConfigurationUnitProcessorDetails and ConfigurationUnitSettingDetails. /// [Collection("UnitTestCollection")] + [InProc] public class ConfigurationDetailsTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs index f98ce6ca60..7fc4fc8ce8 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationMixedElevationTests.cs @@ -19,7 +19,6 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// [Collection("UnitTestCollection")] [OutOfProc] - [OutOfProcOnly] public class ConfigurationMixedElevationTests : ConfigurationProcessorTestBase { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorApplyTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorApplyTests.cs index 830aeb0c83..962befc100 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorApplyTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorApplyTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -21,6 +21,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Unit tests for running apply on the processor. /// [Collection("UnitTestCollection")] + [InProc] [OutOfProc] public class ConfigurationProcessorApplyTests : ConfigurationProcessorTestBase { diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorFactoryTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorFactoryTests.cs index c1c05f9ecc..5572acd28e 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorFactoryTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorFactoryTests.cs @@ -10,6 +10,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests using Microsoft.Management.Configuration.Processor; using Microsoft.Management.Configuration.Processor.Set; using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; using WinRT; using Xunit; using Xunit.Abstractions; @@ -19,6 +20,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Tests ConfigurationProcessorFactory. /// [Collection("UnitTestCollection")] + [InProc] public class ConfigurationProcessorFactoryTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetAllTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetAllTests.cs index bce049a514..a552c0df6b 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetAllTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetAllTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -19,6 +19,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Unit tests for getting details on processors. /// [Collection("UnitTestCollection")] + [InProc] [OutOfProc] public class ConfigurationProcessorGetAllTests : ConfigurationProcessorTestBase { diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetTests.cs index 5759c34245..e628ab8516 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGetTests.cs @@ -17,6 +17,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Unit tests for getting details on processors. /// [Collection("UnitTestCollection")] + [InProc] [OutOfProc] public class ConfigurationProcessorGetTests : ConfigurationProcessorTestBase { diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGroupTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGroupTests.cs index 54343194df..440c0df320 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGroupTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorGroupTests.cs @@ -19,6 +19,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Unit tests for running group processing. /// [Collection("UnitTestCollection")] + [InProc] public class ConfigurationProcessorGroupTests : ConfigurationProcessorTestBase { /// diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorTelemetryTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorTelemetryTests.cs index 34355f4f58..aff7f271c8 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorTelemetryTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorTelemetryTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -25,6 +25,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Unit tests for running test on the processor. /// [Collection("UnitTestCollection")] + [InProc] public class ConfigurationProcessorTelemetryTests : ConfigurationProcessorTestBase { /// diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorTestTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorTestTests.cs index 7b64ee5f05..530eee8a92 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorTestTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationProcessorTestTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -19,6 +19,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Unit tests for running test on the processor. /// [Collection("UnitTestCollection")] + [InProc] [OutOfProc] public class ConfigurationProcessorTestTests : ConfigurationProcessorTestBase { diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetAuthoringTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetAuthoringTests.cs index 5d56565aa5..4590af2aad 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetAuthoringTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetAuthoringTests.cs @@ -19,6 +19,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Unit tests for configuration set authoring (creating objects). /// [Collection("UnitTestCollection")] + [InProc] [OutOfProc] public class ConfigurationSetAuthoringTests : ConfigurationProcessorTestBase { diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetProcessorTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetProcessorTests.cs index 7fc1b8da56..e59840b278 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetProcessorTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetProcessorTests.cs @@ -28,6 +28,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Unit tests for configuration processor tests. /// [Collection("UnitTestCollection")] + [InProc] public class ConfigurationSetProcessorTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationUnitInternalTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationUnitInternalTests.cs index 959fa5a3d5..1c5ef027a2 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationUnitInternalTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationUnitInternalTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -22,6 +22,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Tests ConfigurationUnitExtensionsTests. /// [Collection("UnitTestCollection")] + [InProc] public class ConfigurationUnitInternalTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationUnitProcessorTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationUnitProcessorTests.cs index 405692bc60..683a026dc7 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationUnitProcessorTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationUnitProcessorTests.cs @@ -16,6 +16,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests using Microsoft.Management.Configuration.Processor.ProcessorEnvironments; using Microsoft.Management.Configuration.Processor.Unit; using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; using Microsoft.PowerShell.Commands; using Moq; using Windows.Foundation.Collections; @@ -26,6 +27,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Configuration unit processor tests. /// [Collection("UnitTestCollection")] + [InProc] public class ConfigurationUnitProcessorTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/DscModuleV2SimpleFileResourceTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/DscModuleV2SimpleFileResourceTests.cs index 6bbf22d2a4..198ecd51c2 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/DscModuleV2SimpleFileResourceTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/DscModuleV2SimpleFileResourceTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -21,6 +21,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Class that tests a little not that complex resource. /// [Collection("UnitTestCollection")] + [InProc] public class DscModuleV2SimpleFileResourceTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/DscModuleV2Tests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/DscModuleV2Tests.cs index 2f472074db..506d3b2a14 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/DscModuleV2Tests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/DscModuleV2Tests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -24,6 +24,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Tests DscModuleV2 with really simple resources. /// [Collection("UnitTestCollection")] + [InProc] public class DscModuleV2Tests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/DscResourceMapTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/DscResourceMapTests.cs index b1880d1b73..ed7e6261ce 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/DscResourceMapTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/DscResourceMapTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -11,6 +11,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests using Microsoft.Management.Configuration.Processor.DscResourcesInfo; using Microsoft.Management.Configuration.Processor.Helpers; using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; using Xunit; using Xunit.Abstractions; @@ -18,6 +19,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// DscResourceMap tests. /// [Collection("UnitTestCollection")] + [InProc] public class DscResourceMapTests { private const string ResourceZoro = "xResourceZoro"; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ExceptionExtensionsTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ExceptionExtensionsTests.cs index fb53c23e80..02c29ae37b 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ExceptionExtensionsTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ExceptionExtensionsTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -9,6 +9,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests using System; using Microsoft.Management.Configuration.Processor.Extensions; using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; using Microsoft.PowerShell.Commands; using Xunit; using Xunit.Abstractions; @@ -17,6 +18,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Exception extension tests. /// [Collection("UnitTestCollection")] + [InProc] public class ExceptionExtensionsTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/HashtableExtensionsTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/HashtableExtensionsTests.cs index 334ac6c248..db2e37ebed 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/HashtableExtensionsTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/HashtableExtensionsTests.cs @@ -9,7 +9,8 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests using System.Collections; using Microsoft.Management.Configuration.Processor.Exceptions; using Microsoft.Management.Configuration.Processor.Extensions; - using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; using Windows.Foundation.Collections; using Xunit; using Xunit.Abstractions; @@ -17,7 +18,8 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// /// Hashtable extension tests. /// - [Collection("UnitTestCollection")] + [Collection("UnitTestCollection")] + [InProc] public class HashtableExtensionsTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/OpenConfigurationSetTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/OpenConfigurationSetTests.cs index 65e6e00803..6a7c6c9478 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/OpenConfigurationSetTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/OpenConfigurationSetTests.cs @@ -25,6 +25,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Unit tests for parsing configuration sets from streams. /// [Collection("UnitTestCollection")] + [InProc] [OutOfProc] public class OpenConfigurationSetTests : ConfigurationProcessorTestBase { diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/PowerShellHelperTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/PowerShellHelperTests.cs index efaa0144da..b399524095 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/PowerShellHelperTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/PowerShellHelperTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -9,6 +9,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests using System; using Microsoft.Management.Configuration.Processor.Helpers; using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; using Xunit; using Xunit.Abstractions; @@ -16,6 +17,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// PowerShell helper tests. /// [Collection("UnitTestCollection")] + [InProc] public class PowerShellHelperTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ProcessorEnvironmentTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ProcessorEnvironmentTests.cs index dfda0fcb84..8688ee4a26 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ProcessorEnvironmentTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ProcessorEnvironmentTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -8,6 +8,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests { using System.Collections.Generic; using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; using Moq; using Xunit; using Xunit.Abstractions; @@ -17,6 +18,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// HostedEnvironment tests, that is more ProcessorEnvironmentBase tests for non forwarding functions. /// [Collection("UnitTestCollection")] + [InProc] public class ProcessorEnvironmentTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/SemanticVersionTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/SemanticVersionTests.cs index c90a6815b0..52502d8d64 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/SemanticVersionTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/SemanticVersionTests.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // @@ -9,6 +9,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests using System; using Microsoft.Management.Configuration.Processor.Helpers; using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; using Xunit; using Xunit.Abstractions; @@ -16,6 +17,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// Semantic version tests. /// [Collection("UnitTestCollection")] + [InProc] public class SemanticVersionTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/TypeHelpersTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/TypeHelpersTests.cs index c18a616b72..b072e369a5 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/TypeHelpersTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/TypeHelpersTests.cs @@ -10,6 +10,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests using System.Collections.Generic; using Microsoft.Management.Configuration.Processor.Helpers; using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; using Windows.Foundation.Collections; using Xunit; using Xunit.Abstractions; @@ -18,7 +19,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// TypeHelpers tests. /// [Collection("UnitTestCollection")] - + [InProc] public class TypeHelpersTests { private readonly UnitTestFixture fixture; diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ValueSetExtensionsTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ValueSetExtensionsTests.cs index 2568f03aab..cef5cbbc2c 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ValueSetExtensionsTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ValueSetExtensionsTests.cs @@ -11,6 +11,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests using System.Collections.Generic; using Microsoft.Management.Configuration.Processor.Extensions; using Microsoft.Management.Configuration.UnitTests.Fixtures; + using Microsoft.Management.Configuration.UnitTests.Helpers; using Windows.Foundation.Collections; using Xunit; using Xunit.Abstractions; @@ -19,6 +20,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests /// ValueSet extension tests. /// [Collection("UnitTestCollection")] + [InProc] public class ValueSetExtensionsTests { private readonly UnitTestFixture fixture; From 8481a79e992ae5738c90b90599290c2c60cef5b4 Mon Sep 17 00:00:00 2001 From: --global Date: Fri, 7 Jun 2024 07:31:49 -0700 Subject: [PATCH 12/12] fix definition --- .../ConfigurationDynamicRuntimeFactory.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp index af1860eadf..d710e14a8a 100644 --- a/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp +++ b/src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp @@ -14,7 +14,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting { namespace anonymous { -#ifndef DISABLE_TEST_HOOKS +#ifndef AICLI_DISABLE_TEST_HOOKS constexpr std::wstring_view EnableTestModeTestGuid = L"1e62d683-2999-44e7-81f7-6f8f35e8d731"; constexpr std::wstring_view ForceHighIntegrityLevelUnitsTestGuid = L"f698d20f-3584-4f28-bc75-28037e08e651"; constexpr std::wstring_view EnableRestrictedIntegrityLevelTestGuid = L"5cae3226-185f-4289-815c-3c089d238dc6"; @@ -48,7 +48,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting DynamicSetProcessor(IConfigurationSetProcessorFactory defaultRemoteFactory, IConfigurationSetProcessor defaultRemoteSetProcessor, const ConfigurationSet& configurationSet) : m_configurationSet(configurationSet) { -#ifndef DISABLE_TEST_HOOKS +#ifndef AICLI_DISABLE_TEST_HOOKS m_enableTestMode = GetConfigurationSetMetadataOverride(m_configurationSet, EnableTestModeTestGuid); m_enableRestrictedIntegrityLevel = GetConfigurationSetMetadataOverride(m_configurationSet, EnableRestrictedIntegrityLevelTestGuid); m_forceHighIntegrityLevelUnits = GetConfigurationSetMetadataOverride(m_configurationSet, ForceHighIntegrityLevelUnitsTestGuid); @@ -87,7 +87,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting }); // Create set and unit processor for current unit. -#ifndef DISABLE_TEST_HOOKS +#ifndef AICLI_DISABLE_TEST_HOOKS Security::IntegrityLevel requiredIntegrityLevel = m_forceHighIntegrityLevelUnits ? Security::IntegrityLevel::High : GetIntegrityLevelForUnit(unit); #else Security::IntegrityLevel requiredIntegrityLevel = GetIntegrityLevelForUnit(unit); @@ -114,7 +114,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting } else if (securityContextLower == L"restricted") { -#ifndef DISABLE_TEST_HOOKS +#ifndef AICLI_DISABLE_TEST_HOOKS if (m_enableRestrictedIntegrityLevel) { return Security::IntegrityLevel::Medium; @@ -217,7 +217,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting if (integrityLevel == Security::IntegrityLevel::High) { bool useRunAs = true; -#ifndef DISABLE_TEST_HOOKS +#ifndef AICLI_DISABLE_TEST_HOOKS useRunAs = !m_enableTestMode; #endif @@ -236,7 +236,7 @@ namespace AppInstaller::CLI::ConfigurationRemoting ConfigurationSet m_configurationSet; std::once_flag m_createUnitSetProcessorsOnce; -#ifndef DISABLE_TEST_HOOKS +#ifndef AICLI_DISABLE_TEST_HOOKS bool m_enableTestMode = false; bool m_enableRestrictedIntegrityLevel = false; bool m_forceHighIntegrityLevelUnits = false;