Skip to content

Commit

Permalink
Various fixes for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMcPMS committed Jan 16, 2025
1 parent e148380 commit 0bdb3c0
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/Commands/DebugCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ namespace AppInstaller::CLI
OutputProxyStubInterfaceRegistration<winrt::Windows::Foundation::Collections::IIterable<winrt::Microsoft::Management::Configuration::TestConfigurationUnitResult>>(context);
OutputProxyStubInterfaceRegistration<winrt::Windows::Foundation::Collections::IIterable<winrt::Microsoft::Management::Configuration::IApplyGroupMemberSettingsResult>>(context);
OutputProxyStubInterfaceRegistration<winrt::Windows::Foundation::Collections::IIterable<winrt::Microsoft::Management::Configuration::ITestSettingsResult>>(context);
OutputProxyStubInterfaceRegistration<winrt::Windows::Foundation::Collections::IIterable<winrt::Microsoft::Management::Configuration::IConfigurationEnvironmentView>>(context);
OutputProxyStubInterfaceRegistration<winrt::Microsoft::Management::Configuration::IConfigurationUnitProcessorDetails2>(context);
OutputProxyStubInterfaceRegistration<winrt::Microsoft::Management::Configuration::IGetAllSettingsConfigurationUnitProcessor>(context);
OutputProxyStubInterfaceRegistration<winrt::Microsoft::Management::Configuration::IConfigurationStatics2>(context);
Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCLIPackage/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Interface Name="Windows.Foundation.Collections.IIterable`1&lt;Microsoft.Management.Configuration.TestConfigurationUnitResult&gt;" InterfaceId="73848262-86D4-5FFC-8353-8408C4E649DE" />
<Interface Name="Windows.Foundation.Collections.IIterable`1&lt;Microsoft.Management.Configuration.IApplyGroupMemberSettingsResult&gt;" InterfaceId="5086070C-F468-5B00-8352-50FB420BA8B0" />
<Interface Name="Windows.Foundation.Collections.IIterable`1&lt;Microsoft.Management.Configuration.ITestSettingsResult&gt;" InterfaceId="2D28E6AA-7036-5D78-9B58-9456F1E332FE" />
<Interface Name="Windows.Foundation.Collections.IIterable`1&lt;Microsoft.Management.Configuration.IConfigurationEnvironmentView&gt;" InterfaceId="F1344DB2-5194-5A6A-B36C-EDD05D28FE4C" />
<Interface Name="Microsoft.Management.Configuration.IConfigurationUnitProcessorDetails2" InterfaceId="E89623ED-76E2-5145-B920-D09659554E35" />
<Interface Name="Microsoft.Management.Configuration.IGetAllSettingsConfigurationUnitProcessor" InterfaceId="72EB8304-D8D3-57D4-9940-7C1C4AD8C40C" />
<Interface Name="Microsoft.Management.Configuration.IConfigurationStatics2" InterfaceId="540BE073-F2EF-5375-83AA-8E23086B0669" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// -----------------------------------------------------------------------------
// <copyright file="DictionaryExtensions.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------

namespace Microsoft.Management.Configuration.Processor.Extensions
{
using System;
using System.Collections;
using System.Collections.Generic;
using Windows.Foundation.Collections;

/// <summary>
/// Extensions for dictionaries.
/// </summary>
internal static class DictionaryExtensions
{
/// <summary>
/// Performs a deep compare of the dictionaries.
/// </summary>
/// <param name="first">First dictionary.</param>
/// <param name="second">Second dictionary.</param>
/// <returns>Whether the two dictionaries equal.</returns>
internal static bool ContentEquals(this IDictionary<string, string> first, IDictionary<string, string> second)
{
if (first.Count != second.Count)
{
return false;
}

foreach (var keyValuePair in first)
{
string key = keyValuePair.Key;
if (!second.ContainsKey(key))
{
return false;
}

var firstValue = keyValuePair.Value;
var secondValue = second[key];

// Empty value check.
if (firstValue == null && secondValue == null)
{
continue;
}
else if (firstValue == null || secondValue == null)
{
return false;
}

if (firstValue != secondValue)
{
return false;
}
}

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ public static bool ContentEquals(this ValueSet first, ValueSet second)

foreach (var keyValuePair in first)
{
if (!second.ContainsKey(keyValuePair.Key))
string key = keyValuePair.Key;
if (!second.ContainsKey(key))
{
return false;
}

var firstValue = keyValuePair.Value;
var secondValue = second[keyValuePair.Key];
var secondValue = second[key];

// Empty value check.
if (firstValue == null && secondValue == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,25 @@ private static bool IsUnitTypeResourceName(string? schemaVersion)

private static bool ConfigurationUnitEquals(ConfigurationUnit first, ConfigurationUnit second)
{
if (first.Identifier != second.Identifier ||
first.Type != second.Type ||
first.Intent != second.Intent)
var firstIdentifier = first.Identifier;
var firstIntent = first.Intent;
var firstType = first.Type;
var secondIdentifier = second.Identifier;
var secondType = second.Type;
var secondIntent = second.Intent;

if (firstIdentifier != secondIdentifier ||
firstType != secondType ||
firstIntent != secondIntent)
{
return false;
}

var firstEnvironment = first.Environment;
var secondEnvironment = second.Environment;
if (firstEnvironment.Context != secondEnvironment.Context ||
firstEnvironment.ProcessorIdentifier != secondEnvironment.ProcessorIdentifier ||
!firstEnvironment.ProcessorProperties.ContentEquals(secondEnvironment.ProcessorProperties))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public async Task ApplyMixedElevationUnits()
ConfigurationUnit elevatedUnit = this.ConfigurationUnit();
elevatedUnit.Metadata.Add("version", version.ToString());
elevatedUnit.Metadata.Add("module", moduleName);
elevatedUnit.Metadata.Add("securityContext", "elevated");
elevatedUnit.Environment.Context = SecurityContext.Elevated;
elevatedUnit.Settings.Add("directoryPath", tempDirectory);
elevatedUnit.Type = resourceName;
elevatedUnit.Intent = ConfigurationUnitIntent.Apply;
Expand Down Expand Up @@ -101,6 +101,69 @@ public async Task ApplyMixedElevationUnits()
Assert.Equal(2, pidCount);
}

/// <summary>
/// Verifies that applying units of mixed elevation is successful. Also verifies that the elevated processor has a different process id.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task ApplyMixedElevationUnits_Schema_0_3()
{
string resourceName = "xE2ETestResource/E2ETestResourcePID";
Version version = new Version("0.0.0.1");

string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);

ConfigurationSet configurationSet = this.ConfigurationSet();
configurationSet.SchemaVersion = "0.3";
configurationSet.Metadata.Add(Helpers.Constants.EnableDynamicFactoryTestMode, true);

ConfigurationUnit unit = this.ConfigurationUnit();
unit.Metadata.Add("version", version.ToString());
unit.Settings.Add("directoryPath", tempDirectory);
unit.Type = resourceName;
unit.Identifier = "current";

ConfigurationUnit elevatedUnit = this.ConfigurationUnit();
elevatedUnit.Intent = ConfigurationUnitIntent.Unknown;
elevatedUnit.Metadata.Add("version", version.ToString());
elevatedUnit.Environment.Context = SecurityContext.Elevated;
elevatedUnit.Settings.Add("directoryPath", tempDirectory);
elevatedUnit.Type = resourceName;
elevatedUnit.Identifier = "elevated";

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);

// Get the number of unique PIDs from temp directory.
int pidCount = Directory.GetFiles(tempDirectory).Length;

// Clean up temp directory folder.
Directory.Delete(tempDirectory, true);

Assert.NotNull(result);
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);
}

// There should be exactly 2 unique PIDs, one for each integrity level.
Assert.Equal(2, pidCount);
}

/// <summary>
/// Verifies that creating a high integrity unit processor for a non elevated unit should return an invalid operation result.
/// </summary>
Expand All @@ -121,13 +184,14 @@ public async Task ApplyUnitNotInLimitationSet()
ConfigurationUnit unit = this.ConfigurationUnit();
unit.Metadata.Add("version", version.ToString());
unit.Metadata.Add("module", moduleName);
unit.Metadata.Add("unique", "value");
unit.Type = resourceName;
unit.Intent = ConfigurationUnitIntent.Apply;

ConfigurationUnit elevatedUnit = this.ConfigurationUnit();
elevatedUnit.Metadata.Add("version", version.ToString());
elevatedUnit.Metadata.Add("module", moduleName);
elevatedUnit.Metadata.Add("securityContext", "elevated");
elevatedUnit.Environment.Context = SecurityContext.Elevated;
elevatedUnit.Type = resourceName;
elevatedUnit.Intent = ConfigurationUnitIntent.Apply;

Expand Down Expand Up @@ -182,7 +246,7 @@ public async Task SecureParameterAcrossIntegrityBoundaryFails()
ConfigurationUnit elevatedUnit = this.ConfigurationUnit();
elevatedUnit.Metadata.Add("version", version.ToString());
elevatedUnit.Metadata.Add("module", moduleName);
elevatedUnit.Metadata.Add("securityContext", "elevated");
elevatedUnit.Environment.Context = SecurityContext.Elevated;
elevatedUnit.Settings.Add("directoryPath", tempDirectory);
elevatedUnit.Type = resourceName;
elevatedUnit.Intent = ConfigurationUnitIntent.Apply;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,10 +888,6 @@ public void SetMetadataEnvironmentInheritance_0_3()
OpenConfigurationSetResult serializedSetResult = processor.OpenConfigurationSet(this.CreateStream(yamlOutput));

this.ValidateEnvironments(serializedSetResult, expectedEnvironments);
foreach (var unit in serializedSetResult.Set.Units)
{
Assert.False(unit.Metadata.ContainsKey("winget"));
}
}

/// <summary>
Expand Down Expand Up @@ -1044,10 +1040,6 @@ public void CommonEnvironmentElevatedToSetMetadata_0_3()
OpenConfigurationSetResult serializedSetResult = processor.OpenConfigurationSet(this.CreateStream(yamlOutput));

this.ValidateEnvironments(serializedSetResult, expectedEnvironments);
foreach (var unit in serializedSetResult.Set.Units)
{
Assert.False(unit.Metadata.ContainsKey("winget"));
}
}

/// <summary>
Expand Down Expand Up @@ -1096,15 +1088,6 @@ public void CommonProcessorElevatedToSetMetadata_0_3()
OpenConfigurationSetResult serializedSetResult = processor.OpenConfigurationSet(this.CreateStream(yamlOutput));

this.ValidateEnvironments(serializedSetResult, expectedEnvironments);
foreach (var unit in serializedSetResult.Set.Units)
{
object? wingetItem = null;
Assert.True(unit.Metadata.TryGetValue("winget", out wingetItem));
ValueSet? wingetValueSet = wingetItem as ValueSet;
Assert.NotNull(wingetValueSet);
Assert.True(wingetValueSet.ContainsKey("securityContext"));
Assert.False(wingetValueSet.ContainsKey("processor"));
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,12 @@ namespace winrt::Microsoft::Management::Configuration::implementation

SecurityContext computedContext = defaultContext;

auto securityContext = TryLookupProperty(unit->Metadata(), ConfigurationField::SecurityContextMetadata, Windows::Foundation::PropertyType::String);
Windows::Foundation::Collections::ValueSet metadata = unit->Metadata();
auto securityContext = TryLookupProperty(metadata, ConfigurationField::SecurityContextMetadata, Windows::Foundation::PropertyType::String);
if (securityContext)
{
TryParseSecurityContext(securityContext.GetString(), computedContext);
metadata.Remove(GetConfigurationFieldNameHString(ConfigurationField::SecurityContextMetadata));
}

unit->EnvironmentInternal().Context(computedContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,12 @@ namespace winrt::Microsoft::Management::Configuration::implementation
{
targetEnvironment.Context(computedContext);
}
root.Remove(GetConfigurationFieldNameHString(ConfigurationField::SecurityContextMetadata));
}

// Get processor
IInspectable processor = root.TryLookup(GetConfigurationFieldNameHString(ConfigurationField::ProcessorMetadata));
hstring processorFieldName = GetConfigurationFieldNameHString(ConfigurationField::ProcessorMetadata);
IInspectable processor = root.TryLookup(processorFieldName);
Collections::ValueSet processorValueSet = processor.try_as<Collections::ValueSet>();
if (processorValueSet)
{
Expand All @@ -271,6 +273,8 @@ namespace winrt::Microsoft::Management::Configuration::implementation
targetEnvironment.ProcessorProperties(processorSettings);
}
}

root.Remove(processorFieldName);
}
else
{
Expand All @@ -279,8 +283,14 @@ namespace winrt::Microsoft::Management::Configuration::implementation
{
targetEnvironment.ProcessorIdentifier(processorProperty.GetString());
targetEnvironment.ProcessorProperties().Clear();
root.Remove(processorFieldName);
}
}

if (root.Size() == 0)
{
metadata.Remove(GetConfigurationFieldNameHString(ConfigurationField::WingetMetadataRoot));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ namespace Microsoft.Management.Configuration
interface Windows.Foundation.Collections.IVectorView<ApplyConfigurationUnitResult>;
interface Windows.Foundation.Collections.IVectorView<TestConfigurationUnitResult>;
interface Windows.Foundation.Collections.IVectorView<ConfigurationSet>;
interface Windows.Foundation.Collections.IVector<IConfigurationEnvironmentView>;
}

// Provides a way to centralize the distribution of interfaces relevant to specific implementations of IConfigurationSetProcessorFactory.
Expand Down

0 comments on commit 0bdb3c0

Please sign in to comment.