From d4647ba8a74b35e18d45077be25d7d1ef4beb794 Mon Sep 17 00:00:00 2001 From: Mike Hanson <8196328+themikecom@users.noreply.github.com> Date: Thu, 4 Jun 2020 13:22:34 -0700 Subject: [PATCH] Add unit test coverage for EncryptedStringAttribute --- .../EncryptedStringAttributeTests.cs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 test/Core.Test/Utilities/EncryptedStringAttributeTests.cs diff --git a/test/Core.Test/Utilities/EncryptedStringAttributeTests.cs b/test/Core.Test/Utilities/EncryptedStringAttributeTests.cs new file mode 100644 index 0000000000..f2c8ce816b --- /dev/null +++ b/test/Core.Test/Utilities/EncryptedStringAttributeTests.cs @@ -0,0 +1,43 @@ +using Bit.Core.Utilities; +using Xunit; + +namespace Bit.Core.Test.Utilities +{ + public class EncryptedStringAttributeTests + { + [Theory] + [InlineData(null)] + [InlineData("aXY=|Y3Q=")] // Valid AesCbc256_B64 + [InlineData("aXY=|Y3Q=|cnNhQ3Q=")] // Valid AesCbc128_HmacSha256_B64 + [InlineData("Rsa2048_OaepSha256_B64.cnNhQ3Q=")] + public void IsValid_ReturnsTrue_WhenValid(string input) + { + var sut = new EncryptedStringAttribute(); + + var actual = sut.IsValid(input); + + Assert.True(actual); + } + + [Theory] + [InlineData("")] + [InlineData(".")] + [InlineData("|")] + [InlineData("!|!")] // Invalid base 64 + [InlineData("Rsa2048_OaepSha1_HmacSha256_B64.1")] // Invalid length + [InlineData("Rsa2048_OaepSha1_HmacSha256_B64.|")] // Empty iv & ct + [InlineData("AesCbc128_HmacSha256_B64.1")] // Invalid length + [InlineData("AesCbc128_HmacSha256_B64.aXY=|Y3Q=|")] // Empty mac + [InlineData("Rsa2048_OaepSha1_HmacSha256_B64.aXY=|Y3Q=|")] // Empty mac + [InlineData("Rsa2048_OaepSha256_B64.1|2")] // Invalid length + [InlineData("Rsa2048_OaepSha1_HmacSha256_B64.aXY=|")] // Empty mac + public void IsValid_ReturnsFalse_WhenInvalid(string input) + { + var sut = new EncryptedStringAttribute(); + + var actual = sut.IsValid(input); + + Assert.False(actual); + } + } +}