-
Notifications
You must be signed in to change notification settings - Fork 841
Introduce type converter for DataClassification #5887
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
Changes from 3 commits
859042b
736df75
5f7f28d
fddeecb
4edc94b
77aac5c
c30c587
ffd482f
e708e88
1e43155
e35d2ad
72d1c8b
73e3ec1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.ComponentModel; | ||
| using System.Globalization; | ||
| using Microsoft.Shared.Diagnostics; | ||
|
|
||
| namespace Microsoft.Extensions.Compliance.Classification; | ||
|
|
||
| /// <summary> | ||
| /// Provides a way to convert a <see cref="DataClassification"/> to and from a string. | ||
| /// </summary> | ||
| public class DataClassificationTypeConverter : TypeConverter | ||
|
Check failure on line 14 in src/Libraries/Microsoft.Extensions.Compliance.Abstractions/Classification/DataClassificationTypeConverter.cs
|
||
| { | ||
| private const char Delimiter = ':'; | ||
| private const int MinimumCharactersWithDelimiter = 3; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) | ||
| { | ||
| return sourceType == typeof(string); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) | ||
| { | ||
| return destinationType == typeof(DataClassification); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) | ||
| { | ||
| if (value is string stringValue) | ||
| { | ||
| if (stringValue == nameof(DataClassification.None)) | ||
| { | ||
| return DataClassification.None; | ||
| } | ||
|
|
||
| ReadOnlySpan<char> valueSpan = stringValue.AsSpan(); | ||
| int index = valueSpan.IndexOf(Delimiter); | ||
|
|
||
| if (index >= 0) | ||
| { | ||
| // Convert the string with format "TaxonomyName:Value" to a DataClassification | ||
| string taxonomyName = valueSpan.Slice(0, index).ToString(); | ||
| string taxonomyValue = valueSpan.Slice(index + 1).ToString(); | ||
|
|
||
| _ = Throw.IfNullOrWhitespace(taxonomyName); | ||
| _ = Throw.IfNullOrWhitespace(taxonomyValue); | ||
|
|
||
| return new DataClassification(taxonomyName, taxonomyValue); | ||
| } | ||
| } | ||
|
|
||
| return DataClassification.Unknown; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override bool IsValid(ITypeDescriptorContext? context, object? value) | ||
| { | ||
| if (value is not string stringValue) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (stringValue == nameof(DataClassification.None)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| #if !NET8_0_OR_GREATER | ||
| if (stringValue.Contains($"{Delimiter}")) | ||
| #else | ||
| if (stringValue.Contains(Delimiter, StringComparison.Ordinal)) | ||
| #endif | ||
| { | ||
| return stringValue.Length >= MinimumCharactersWithDelimiter; | ||
| } | ||
|
|
||
| return stringValue.Length > 0; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using FluentAssertions; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Options; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Extensions.Compliance.Classification.Tests; | ||
|
|
||
| public class DataClassificationTypeConverterTests | ||
| { | ||
| public static IEnumerable<object[]> DefaultDataClassificationTestData() | ||
| { | ||
| yield return new object[] { "None", DataClassification.None }; | ||
| yield return new object[] { "Unknown", DataClassification.Unknown }; | ||
| yield return new object[] { "Invalid", DataClassification.Unknown }; | ||
| } | ||
|
|
||
| public static IEnumerable<object[]> CustomDataClassificationTestData() | ||
| { | ||
| yield return new object[] { "Example:Test", new DataClassification("Example", "Test") }; | ||
| yield return new object[] { "Taxonomy:Value", new DataClassification("Taxonomy", "Value") }; | ||
| yield return new object[] { "Custom:Data", new DataClassification("Custom", "Data") }; | ||
| } | ||
|
|
||
| [Fact] | ||
| public void BindServiceCollection_ShouldReturnIOptionsWithExpectedDataClassification() | ||
| { | ||
| // Arrange | ||
| var configuration = new ConfigurationBuilder() | ||
| .AddInMemoryCollection(new[] | ||
| { | ||
| new KeyValuePair<string, string?>("Key:Example", "Example:Test"), | ||
| new KeyValuePair<string, string?>("Key:Facts:Value", "Taxonomy:Value"), | ||
| new KeyValuePair<string, string?>("Key:Facts:Data", "Custom:Data"), | ||
| new KeyValuePair<string, string?>("Key:Facts:None", "None"), | ||
| new KeyValuePair<string, string?>("Key:Facts:Unknown", "Unknown"), | ||
| new KeyValuePair<string, string?>("Key:Facts:Invalid", "Invalid"), | ||
| }) | ||
| .Build(); | ||
|
|
||
| IServiceCollection serviceCollection = new ServiceCollection() | ||
| .Configure<TestOptions>(configuration.GetSection("Key")); | ||
|
|
||
| // Act | ||
| using var sp = serviceCollection.BuildServiceProvider(); | ||
| var options = sp.GetRequiredService<IOptions<TestOptions>>(); | ||
|
|
||
| var expected = new Dictionary<string, DataClassification> | ||
| { | ||
| { "Value", new DataClassification("Taxonomy", "Value") }, | ||
| { "Data", new DataClassification("Custom", "Data") }, | ||
| { "None", DataClassification.None }, | ||
| { "Unknown", DataClassification.Unknown }, | ||
| { "Invalid", DataClassification.Unknown }, | ||
| }; | ||
|
|
||
| // Assert | ||
| options.Value.Example.Should().NotBeNull().And.Be(new DataClassification("Example", "Test")); | ||
| options.Value.Facts.Should().NotBeEmpty().And.Equal(expected); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CanConvertTo_ShouldReturnTrue_WhenDestinationTypeIsDataClassification() | ||
| { | ||
| // Arrange | ||
| var converter = new DataClassificationTypeConverter(); | ||
|
|
||
| // Act | ||
| var canConvert = converter.CanConvertTo(null, typeof(DataClassification)); | ||
|
|
||
| // Assert | ||
| canConvert.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CanConvertTo_ShouldReturnFalse_WhenDestinationTypeIsNotDataClassification() | ||
| { | ||
| // Arrange | ||
| var converter = new DataClassificationTypeConverter(); | ||
|
|
||
| // Act | ||
| var canConvert = converter.CanConvertTo(null, typeof(DateTimeOffset)); | ||
|
|
||
| // Assert | ||
| canConvert.Should().BeFalse(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ConvertFrom_ShouldReturnDataClassificationUnknown_WhenValueIsNotString() | ||
| { | ||
| // Arrange | ||
| var converter = new DataClassificationTypeConverter(); | ||
|
|
||
| // Act | ||
| var result = converter.ConvertFrom(null, null, 123); | ||
|
|
||
| // Assert | ||
| result.Should().Be(DataClassification.Unknown); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CanConvertFrom_ShouldReturnTrue_WhenSourceTypeIsString() | ||
| { | ||
| // Arrange | ||
| var converter = new DataClassificationTypeConverter(); | ||
|
|
||
| // Act | ||
| var canConvert = converter.CanConvertFrom(null, typeof(string)); | ||
|
|
||
| // Assert | ||
| canConvert.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CanConvertFrom_ShouldReturnFalse_WhenSourceTypeIsNotString() | ||
| { | ||
| // Arrange | ||
| var converter = new DataClassificationTypeConverter(); | ||
|
|
||
| // Act | ||
| var canConvert = converter.CanConvertFrom(null, typeof(int)); | ||
|
|
||
| // Assert | ||
| canConvert.Should().BeFalse(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(DefaultDataClassificationTestData))] | ||
| [MemberData(nameof(CustomDataClassificationTestData))] | ||
| public void ConvertFrom_ShouldReturnCorrectDataClassification_WhenValueIsValidString(string input, DataClassification expected) | ||
| { | ||
| // Arrange | ||
| var converter = new DataClassificationTypeConverter(); | ||
|
|
||
| // Act | ||
| var result = converter.ConvertFrom(null, null, input); | ||
|
|
||
| // Assert | ||
| result.Should().Be(expected); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ConvertFrom_ShouldReturnUnknown_WhenInputIsInvalidFormat() | ||
| { | ||
| // Arrange | ||
| var converter = new DataClassificationTypeConverter(); | ||
| string input = "InvalidFormatWithoutDelimiter"; | ||
|
|
||
| // Act | ||
| var result = converter.ConvertFrom(null, null, input); | ||
|
|
||
| // Assert | ||
| result.Should().Be(DataClassification.Unknown); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("None")] | ||
| [InlineData("ValidString")] | ||
| public void IsValid_ShouldReturnTrue_ForValidStringWithoutDelimiter(string input) | ||
| { | ||
| // Arrange | ||
| var converter = new DataClassificationTypeConverter(); | ||
|
|
||
| // Act | ||
| var isValid = converter.IsValid(null, input); | ||
|
|
||
| // Assert | ||
| isValid.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsValid_ShouldReturnTrue_ForValidStringWithDelimiter() | ||
| { | ||
| // Arrange | ||
| var converter = new DataClassificationTypeConverter(); | ||
| string input = "Taxonomy:Value"; | ||
|
|
||
| // Act | ||
| var isValid = converter.IsValid(null, input); | ||
|
|
||
| // Assert | ||
| isValid.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("", false)] | ||
| [InlineData(":", false)] | ||
| [InlineData("A:", false)] | ||
| [InlineData(":A", false)] | ||
| public void IsValid_ShouldReturnExpectedResult_ForInsufficientLengthInput(string input, bool expected) | ||
| { | ||
| // Arrange | ||
| var converter = new DataClassificationTypeConverter(); | ||
|
|
||
| // Act | ||
| var isValid = converter.IsValid(null, input); | ||
|
|
||
| // Assert | ||
| isValid.Should().Be(expected); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsValid_ShouldReturnFalse_ForNonStringInput() | ||
| { | ||
| // Arrange | ||
| var converter = new DataClassificationTypeConverter(); | ||
| int input = 456; | ||
|
|
||
| // Act | ||
| var isValid = converter.IsValid(null, input); | ||
|
|
||
| // Assert | ||
| isValid.Should().BeFalse(); | ||
| } | ||
|
|
||
| private class TestOptions | ||
| { | ||
| #pragma warning disable S3459 // Unassigned members should be removed | ||
| #pragma warning disable S1144 // Unused private types or members should be removed | ||
| public DataClassification? Example { get; set; } | ||
| #pragma warning restore S1144 // Unused private types or members should be removed | ||
| #pragma warning restore S3459 // Unassigned members should be removed | ||
| public IDictionary<string, DataClassification> Facts { get; set; } = new Dictionary<string, DataClassification>(); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.