diff --git a/src/Core/Utilities/EncryptedStringAttribute.cs b/src/Core/Utilities/EncryptedStringAttribute.cs index 841ab3be62..f31aa39692 100644 --- a/src/Core/Utilities/EncryptedStringAttribute.cs +++ b/src/Core/Utilities/EncryptedStringAttribute.cs @@ -1,6 +1,5 @@ using System.Buffers; using System.ComponentModel.DataAnnotations; -using System.Diagnostics; using Bit.Core.Enums; #nullable enable @@ -12,27 +11,16 @@ namespace Bit.Core.Utilities; /// public class EncryptedStringAttribute : ValidationAttribute { - private static readonly Dictionary _encryptionTypeMap; - - static EncryptedStringAttribute() + internal static readonly Dictionary _encryptionTypeMap = new() { - _encryptionTypeMap = new() - { - [EncryptionType.AesCbc256_B64] = 2, // iv|ct - [EncryptionType.AesCbc128_HmacSha256_B64] = 3, // iv|ct|mac - [EncryptionType.AesCbc256_HmacSha256_B64] = 3, // iv|ct|mac - [EncryptionType.Rsa2048_OaepSha256_B64] = 1, // rsaCt - [EncryptionType.Rsa2048_OaepSha1_B64] = 1, // rsaCt - [EncryptionType.Rsa2048_OaepSha256_HmacSha256_B64] = 2, // rsaCt|mac - [EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64] = 2, // rsaCt|mac - }; - -#if DEBUG - var enumValues = Enum.GetValues(); - Debug.Assert(enumValues.Length == _encryptionTypeMap.Count, - $"New {nameof(EncryptionType)} enums should be added to the {nameof(_encryptionTypeMap)}"); -#endif - } + [EncryptionType.AesCbc256_B64] = 2, // iv|ct + [EncryptionType.AesCbc128_HmacSha256_B64] = 3, // iv|ct|mac + [EncryptionType.AesCbc256_HmacSha256_B64] = 3, // iv|ct|mac + [EncryptionType.Rsa2048_OaepSha256_B64] = 1, // rsaCt + [EncryptionType.Rsa2048_OaepSha1_B64] = 1, // rsaCt + [EncryptionType.Rsa2048_OaepSha256_HmacSha256_B64] = 2, // rsaCt|mac + [EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64] = 2, // rsaCt|mac + }; public EncryptedStringAttribute() : base("{0} is not a valid encrypted string.") diff --git a/test/Core.Test/Utilities/EncryptedStringAttributeTests.cs b/test/Core.Test/Utilities/EncryptedStringAttributeTests.cs index 194e817bfd..8d534650ab 100644 --- a/test/Core.Test/Utilities/EncryptedStringAttributeTests.cs +++ b/test/Core.Test/Utilities/EncryptedStringAttributeTests.cs @@ -1,4 +1,5 @@ -using Bit.Core.Utilities; +using Bit.Core.Enums; +using Bit.Core.Utilities; using Xunit; namespace Bit.Core.Test.Utilities; @@ -69,4 +70,18 @@ public class EncryptedStringAttributeTests Assert.False(actual); } + + [Fact] + public void EncryptionTypeMap_HasEntry_ForEachEnumValue() + { + var enumValues = Enum.GetValues(); + Assert.Equal(enumValues.Length, EncryptedStringAttribute._encryptionTypeMap.Count); + + foreach (var enumValue in enumValues) + { + // Go a step further and ensure that the map contains a value for each value instead of just casting + // a random number for one of the keys. + Assert.True(EncryptedStringAttribute._encryptionTypeMap.ContainsKey(enumValue)); + } + } }