Skip to content

Commit

Permalink
Initial implementation of the RsaCryptographicProvider (#7724)
Browse files Browse the repository at this point in the history
Resolves #7662
  • Loading branch information
heaths authored Sep 24, 2019
1 parent 9641c0b commit fc80db1
Show file tree
Hide file tree
Showing 48 changed files with 11,469 additions and 143 deletions.
53 changes: 53 additions & 0 deletions sdk/core/Azure.Core/tests/FieldsAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Azure.Core.Testing;
using NUnit.Framework;

namespace Azure.Core.Tests
{
public class FieldsAttributeTests
{
[Test]
public void DiscoversFields([Fields]Data data)
{
Assert.That(data, Is.AnyOf(Data.Data1, Data.Data2));
}

[Test]
public void IncludesNamedFields([Fields(nameof(Data.Data1), nameof(Data.InternalData))]Data data)
{
Assert.AreEqual(Data.Data1, data);
}

[Test]
public void ExcludesNamedFields([Fields(Exclude = new[] { nameof(Data.Data2) })]Data data)
{
Assert.AreEqual(Data.Data1, data);
}

[Test]
public void ExcludesNamedFieldsOverride([Fields(nameof(Data.Data1), nameof(Data.Data2), Exclude = new[] { nameof(Data.Data2) })]Data data)
{
Assert.AreEqual(Data.Data1, data);
}

public readonly struct Data : IEquatable<Data>
{
public static readonly Data Data1 = new Data(1);
public static readonly Data Data2 = new Data(2);
internal static readonly Data InternalData = new Data(3);
private static readonly Data PrivateData = new Data(4);

private readonly int _value;

private Data(int value)
{
_value = value;
}

public bool Equals(Data other) => _value == other._value;
}
}
}
33 changes: 31 additions & 2 deletions sdk/core/Azure.Core/tests/TestFramework/FieldsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@ public class FieldsAttribute : Attribute, IParameterDataSource
{
private readonly string[] _names;

/// <summary>
/// Creates a new instance of the <see cref="FieldsAttribute"/> class.
/// All public, static, read-only fields of the declared type are included.
/// </summary>
public FieldsAttribute()
{
}

/// <summary>
/// Creates a new instance of the <see cref="FieldsAttribute"/> class.
/// Only public, static, read-only fields of the declared type with any of the specified <paramref name="names"/> are included.
/// </summary>
public FieldsAttribute(params string[] names)
{
_names = names;
}

/// <summary>
/// Gets or sets a list of field names to exclude. Fields names specified here will be excluded even if specified in the <see cref="FieldsAttribute(string[])"/> constructor.
/// </summary>
public string[] Exclude { get; set; }

public IEnumerable GetData(IParameterInfo parameter)
{
Type type = parameter.ParameterType;
Expand All @@ -35,15 +48,15 @@ public IEnumerable GetData(IParameterInfo parameter)
for (int i = 0; i < fields.Length; ++i)
{
FieldInfo field = fields[i];
if (field.FieldType == type && Contains(field.Name))
if (field.FieldType == type && Includes(field.Name) && !Excludes(field.Name))
{
yield return field.GetValue(null);
}
}
}
}

private bool Contains(string name)
private bool Includes(string name)
{
if (_names is null || _names.Length == 0)
{
Expand All @@ -60,5 +73,21 @@ private bool Contains(string name)

return false;
}

private bool Excludes(string name)
{
if (Exclude != null)
{
for (int i = 0; i < Exclude.Length; ++i)
{
if (string.Equals(Exclude[i], name, StringComparison.Ordinal))
{
return true;
}
}
}

return false;
}
}
}
Loading

0 comments on commit fc80db1

Please sign in to comment.