Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate C# DefineConstants input #9612

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,38 @@ namespace Microsoft.VisualStudio.ProjectSystem.Debug;

internal static class KeyValuePairListEncoding
{
public static IEnumerable<(string Name, string Value)> Parse(string input)
/// <summary>
/// Parses the input string into a collection of key-value pairs with the given separator.
/// </summary>
/// <param name="input">The input string to parse.</param>
/// <param name="allowsEmptyKey">Indicates whether empty keys are allowed. If this is true, a pair will be returned if an empty key has a non-empty value. ie, =4</param>
adamint marked this conversation as resolved.
Show resolved Hide resolved
/// <param name="separator">The character used to separate entries in the input string.</param>
public static IEnumerable<(string Name, string Value)> Parse(string input, char separator = ',')
{
if (string.IsNullOrWhiteSpace(input))
{
yield break;
}

foreach (var entry in ReadEntries(input))
foreach (var entry in ReadEntries(input, separator))
{
var (entryKey, entryValue) = SplitEntry(entry);
var decodedEntryKey = Decode(entryKey);
var decodedEntryValue = Decode(entryValue);

if (!string.IsNullOrEmpty(decodedEntryKey))
{
yield return (decodedEntryKey, decodedEntryValue);
}
}

static IEnumerable<string> ReadEntries(string rawText)
static IEnumerable<string> ReadEntries(string rawText, char separator)
{
bool escaped = false;
int entryStart = 0;
for (int i = 0; i < rawText.Length; i++)
{
if (rawText[i] == ',' && !escaped)
if (rawText[i] == separator && !escaped)
{
yield return rawText.Substring(entryStart, i - entryStart);
entryStart = i + 1;
Expand Down Expand Up @@ -67,7 +73,7 @@ static IEnumerable<string> ReadEntries(string rawText)
}
}

return (string.Empty, string.Empty);
return (entry, string.Empty);
}

static string Decode(string value)
Expand All @@ -76,12 +82,13 @@ static string Decode(string value)
}
}

public static string Format(IEnumerable<(string Name, string Value)> pairs)
public static string Format(IEnumerable<(string Name, string Value)> pairs, char separator = ',')
{
// Copied from ActiveLaunchProfileEnvironmentVariableValueProvider in the .NET Project System.
// In future, EnvironmentVariablesNameValueListEncoding should be exported from that code base and imported here.

return string.Join(",", pairs.Select(kvp => $"{Encode(kvp.Name)}={Encode(kvp.Value)}"));
return string.Join(
separator.ToString(),
pairs.Select(kvp => string.IsNullOrEmpty(kvp.Value)
? Encode(kvp.Name)
: $"{Encode(kvp.Name)}={Encode(kvp.Value)}"));

static string Encode(string value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@ namespace Microsoft.VisualStudio.ProjectSystem.Properties;

[ExportInterceptingPropertyValueProvider(ConfiguredBrowseObject.DefineConstantsProperty, ExportInterceptingPropertyValueProviderFile.ProjectFile)]
[AppliesTo(ProjectCapability.CSharpOrFSharp)]
internal class DefineConstantsValueProvider : InterceptingPropertyValueProviderBase
[method: ImportingConstructor]
internal class DefineConstantsCAndFSharpValueProvider(IProjectAccessor projectAccessor, ConfiguredProject project) : InterceptingPropertyValueProviderBase
{
private readonly IProjectAccessor _projectAccessor;
private readonly ConfiguredProject _project;
private const string DefineConstantsRecursivePrefix = "$(DefineConstants)";

internal const string DefineConstantsRecursivePrefix = "$(DefineConstants)";

[ImportingConstructor]
public DefineConstantsValueProvider(IProjectAccessor projectAccessor, ConfiguredProject project)
private static IEnumerable<string> ParseDefinedConstantsFromUnevaluatedValue(string unevaluatedValue)
{
_projectAccessor = projectAccessor;
_project = project;
}
string substring = unevaluatedValue.Length <= DefineConstantsRecursivePrefix.Length || !unevaluatedValue.StartsWith(DefineConstantsRecursivePrefix)
? unevaluatedValue
: unevaluatedValue.Substring(DefineConstantsRecursivePrefix.Length);

internal static IEnumerable<string> ParseDefinedConstantsFromUnevaluatedValue(string unevaluatedValue)
{
return unevaluatedValue.Length <= DefineConstantsRecursivePrefix.Length || !unevaluatedValue.StartsWith(DefineConstantsRecursivePrefix)
? Array.Empty<string>()
: unevaluatedValue.Substring(DefineConstantsRecursivePrefix.Length).Split(';').Where(x => x.Length > 0);
return substring.Split(';').Where(x => x.Length > 0);
}

public override async Task<string> OnGetUnevaluatedPropertyValueAsync(string propertyName, string unevaluatedPropertyValue, IProjectProperties defaultProperties)
Expand All @@ -38,10 +31,11 @@ public override async Task<string> OnGetUnevaluatedPropertyValueAsync(string pro
return string.Empty;
}

return KeyValuePairListEncoding.Format(
ParseDefinedConstantsFromUnevaluatedValue(unevaluatedDefineConstantsValue)
.Select(symbol => (Key: symbol, Value: bool.FalseString))
);
var pairs = KeyValuePairListEncoding.Parse(unevaluatedDefineConstantsValue, separator: ';').Select(pair => pair.Name)
.Where(symbol => !string.IsNullOrEmpty(symbol))
.Select(symbol => (symbol, bool.FalseString)).ToList();

return KeyValuePairListEncoding.Format(pairs, separator: ',');
}

// We cannot rely on the unevaluated property value as obtained through Project.GetProperty.UnevaluatedValue - the reason is that for a recursively-defined
Expand All @@ -51,11 +45,12 @@ public override async Task<string> OnGetUnevaluatedPropertyValueAsync(string pro
// 2. to override IsValueDefinedInContextAsync, as this will always return false
private async Task<string?> GetUnevaluatedDefineConstantsPropertyValueAsync()
{
await ((ConfiguredProject2)_project).EnsureProjectEvaluatedAsync();
return await _projectAccessor.OpenProjectForReadAsync(_project, project =>
await ((ConfiguredProject2)project).EnsureProjectEvaluatedAsync();

return await projectAccessor.OpenProjectForReadAsync(project, msbuildProject =>
{
project.ReevaluateIfNecessary();
ProjectProperty defineConstantsProperty = project.GetProperty(ConfiguredBrowseObject.DefineConstantsProperty);
msbuildProject.ReevaluateIfNecessary();
ProjectProperty defineConstantsProperty = msbuildProject.GetProperty(ConfiguredBrowseObject.DefineConstantsProperty);
while (defineConstantsProperty.IsImported && defineConstantsProperty.Predecessor is not null)
{
defineConstantsProperty = defineConstantsProperty.Predecessor;
Expand All @@ -76,18 +71,21 @@ public override async Task<bool> IsValueDefinedInContextAsync(string propertyNam
IEnumerable<string> innerConstants =
ParseDefinedConstantsFromUnevaluatedValue(await defaultProperties.GetUnevaluatedPropertyValueAsync(ConfiguredBrowseObject.DefineConstantsProperty) ?? string.Empty);

IEnumerable<string> constantsToWrite = KeyValuePairListEncoding.Parse(unevaluatedPropertyValue)
var foundConstants = KeyValuePairListEncoding.Parse(unevaluatedPropertyValue, separator: ',')
.Select(pair => pair.Name)
.Where(x => !innerConstants.Contains(x))
.Where(pair => !string.IsNullOrEmpty(pair))
.Select(constant => constant.Trim(';')) // trim any leading or trailing semicolons, because we will add our own separating semicolons
.Where(constant => !string.IsNullOrEmpty(constant)) // you aren't allowed to add a semicolon as a constant
.Distinct()
.ToList();

if (!constantsToWrite.Any())
var writeableConstants = foundConstants.Where(constant => !innerConstants.Contains(constant)).ToList();
if (writeableConstants.Count == 0)
{
await defaultProperties.DeletePropertyAsync(propertyName, dimensionalConditions);
return null;
}

return $"{DefineConstantsRecursivePrefix};" + string.Join(";", constantsToWrite);
return string.Join(";", writeableConstants);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<NameValuePair Name="AllowsCustomStrings" Value="True" />
<NameValuePair Name="ShouldDisplayEvaluatedPreview" Value="True" />
<NameValuePair Name="SingleValueConfigurationCommandEnabled" Value="False" />
<NameValuePair Name="MultiValueSplitRegex" Value=";" />
</ValueEditor.Metadata>
</ValueEditor>
</StringProperty.ValueEditors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ internal static class ConfiguredProjectFactory
{
public static ConfiguredProject Create(IProjectCapabilitiesScope? capabilities = null, ProjectConfiguration? projectConfiguration = null, ConfiguredProjectServices? services = null, UnconfiguredProject? unconfiguredProject = null)
{
var mock = new Mock<ConfiguredProject>();
var mock2 = new Mock<ConfiguredProject2>();
mock2.Setup(c => c.EnsureProjectEvaluatedAsync()).Returns(Task.CompletedTask);

var mock = mock2.As<ConfiguredProject>();
mock.Setup(c => c.Capabilities).Returns(capabilities!);
mock.Setup(c => c.ProjectConfiguration).Returns(projectConfiguration!);
mock.Setup(c => c.Services).Returns(services!);
mock.SetupGet(c => c.UnconfiguredProject).Returns(unconfiguredProject ?? UnconfiguredProjectFactory.Create());

return mock.Object;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.

using Microsoft.Build.Construction;
using Microsoft.VisualStudio.ProjectSystem.Properties;

namespace Microsoft.VisualStudio.ProjectSystem.VS.Properties;

public class DefineConstantsCAndFSharpValueProviderTests
{
private const string PropertyName = "DefineConstants";

[Theory]
[InlineData("DEBUG;TRACE", "DEBUG=False,TRACE=False")]
[InlineData("", "")]
public async Task GetExistingUnevaluatedValue(string? defineConstantsValue, string expectedFormattedValue)
{
var provider = CreateInstance(defineConstantsValue, out _, out _);

var actualPropertyValue = await provider.OnGetUnevaluatedPropertyValueAsync(string.Empty, string.Empty, null!);
Assert.Equal(expectedFormattedValue, actualPropertyValue);
}

[Theory]
[InlineData("DEBUG,TRACE", null, "DEBUG;TRACE", "DEBUG=False,TRACE=False")]
[InlineData("$(DefineConstants),DEBUG,TRACE", "PROP1;PROP2", "$(DefineConstants);DEBUG;TRACE", "$(DefineConstants)=False,DEBUG=False,TRACE=False")]
public async Task SetUnevaluatedValue(string unevaluatedValueToSet, string? defineConstantsValue, string? expectedSetUnevaluatedValue, string expectedFormattedValue)
{
var provider = CreateInstance(null, out var projectAccessor, out var project);
Mock<IProjectProperties> mockProjectProperties = new Mock<IProjectProperties>();
mockProjectProperties
.Setup(p => p.GetUnevaluatedPropertyValueAsync(ConfiguredBrowseObject.DefineConstantsProperty))
.ReturnsAsync(defineConstantsValue);

var setPropertyValue = await provider.OnSetPropertyValueAsync(PropertyName, unevaluatedValueToSet, mockProjectProperties.Object);
Assert.Equal(expectedSetUnevaluatedValue, setPropertyValue);

await SetDefineConstantsPropertyAsync(projectAccessor, project, setPropertyValue);

var actualPropertyFormattedValue = await provider.OnGetUnevaluatedPropertyValueAsync(string.Empty, string.Empty, null!);
Assert.Equal(expectedFormattedValue, actualPropertyFormattedValue);
}

private static DefineConstantsCAndFSharpValueProvider CreateInstance(string? defineConstantsValue, out IProjectAccessor projectAccessor, out ConfiguredProject project)
{
var projectXml = defineConstantsValue is not null
? $"""
<Project>
<PropertyGroup>
<{ConfiguredBrowseObject.DefineConstantsProperty}>{defineConstantsValue}</{ConfiguredBrowseObject.DefineConstantsProperty}>
</PropertyGroup>
</Project>
"""
: "<Project></Project>";

projectAccessor = IProjectAccessorFactory.Create(ProjectRootElementFactory.Create(projectXml));
project = ConfiguredProjectFactory.Create();

return new DefineConstantsCAndFSharpValueProvider(projectAccessor, project);
}

private static async Task SetDefineConstantsPropertyAsync(IProjectAccessor projectAccessor, ConfiguredProject project, string? setPropertyValue)
{
await projectAccessor.OpenProjectXmlForWriteAsync(project.UnconfiguredProject, projectXml =>
{
projectXml.AddProperty(PropertyName, setPropertyValue);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.

using Microsoft.VisualStudio.ProjectSystem.Debug;

namespace Microsoft.VisualStudio.ProjectSystem.VS.Properties;

public class KeyValuePairListEncodingTests
{
[Theory]
[InlineData("key1=value1;key2=value2", new[] { "key1", "value1", "key2", "value2" })]
[InlineData("key1=value1;;key2=value2", new[] { "key1", "value1", "key2", "value2" })]
[InlineData("key1=value1;;;key2=value2", new[] { "key1", "value1", "key2", "value2" })]
[InlineData("key1=value1;key2=value2;key3=value3", new[] { "key1", "value1", "key2", "value2", "key3", "value3" })]
[InlineData("key1;key2=value2", new[] { "key1", "", "key2", "value2" })]
[InlineData("key1;key2;key3=value3", new[] { "key1", "", "key2", "", "key3", "value3" })]
[InlineData("key1;;;key3;;", new[] { "key1", "", "key3", "" })]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to cover some more inputs:

  • ""
  • " "
  • "="
  • ";"

If there are invalid inputs, a test that ensures that Parse throws would be good. For example, "==", or null.

Copy link
Member Author

@adamint adamint Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added test cases on those inputs except null, as the method accepts a non-nullable input string. But I wouldn't necessarily agree that there are invalid inputs possible here, since == should parse to an empty key, value =.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding more tests.

since == should parse to an empty key, value =

I'm less confident about that than you. I feel like == should be an invalid and ambiguous input that throws. If values are expected to contain delimiters, then they should be escaped.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like == should be an invalid and ambiguous input that throws.

I'm not entirely in agreement; the difference between = and the other delimiters is that = has no meaning other than as part of a value after already encountering an =

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that it's ambiguous whether the name or value is =. Values also cannot contain commas without escaping.

What happens if the Format method is given names/values that contain = or ,? My guess is that these values would not round-trip correctly.

For robustness, we should either throw on invalid inputs, or implement escaping so that these values are handled correctly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you enter strings in the UI containing = and ,?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points. I switched back to escaping the =

[InlineData("", new string[0])]
[InlineData(" ", new string[0])]
[InlineData("=", new string[0])]
[InlineData("/=", new[] { "=", "" })]
[InlineData("key1=value1;/=value2=", new[] { "key1", "value1", "=value2", "" })]
[InlineData("key1=value1;=value2", new[] { "key1", "value1" })]
[InlineData("==", new string[0])]
[InlineData("=/=", new string[0])]
[InlineData("/==", new[] { "=", "" })]
[InlineData(";", new string[0])]
public void Parse_ValidInput_ReturnsExpectedPairs(string input, string[] expectedPairs)
{
var result = KeyValuePairListEncoding.Parse(input, ';').SelectMany(pair => new[] { pair.Name, pair.Value }).ToArray();
Assert.Equal(expectedPairs, result);
}

[Theory]
[InlineData(new[] { "key1", "value1", "key2", "value2" }, "key1=value1;key2=value2")]
[InlineData(new[] { "key1", "value1", "key2", "value2", "key3", "value3" }, "key1=value1;key2=value2;key3=value3")]
[InlineData(new[] { "key1", "", "key2", "value2" }, "key1;key2=value2")]
[InlineData(new[] { "key1=", "", "key2=", "value2=" }, "key1/=;key2/==value2/=")]
[InlineData(new[] { "key1", "", "key2", "", "key3", "value3" }, "key1;key2;key3=value3")]
adamint marked this conversation as resolved.
Show resolved Hide resolved
[InlineData(new string[0], "")]
public void Format_ValidPairs_ReturnsExpectedString(string[] pairs, string expectedString)
{
var nameValuePairs = ToNameValues(pairs);
var result = KeyValuePairListEncoding.Format(nameValuePairs, ';');
Assert.Equal(expectedString, result);
return;

static IEnumerable<(string Name, string Value)> ToNameValues(IEnumerable<string> pairs)
{
using var e = pairs.GetEnumerator();
while (e.MoveNext())
{
var name = e.Current;
Assert.True(e.MoveNext());
var value = e.Current;
yield return (name, value);
}
}
}
}
Loading