Skip to content

Commit 13f26b2

Browse files
committed
Add tests for encrypting/decrypting byte arrays and SecureStrings.
1 parent bdcd2dc commit 13f26b2

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

test/EntityFrameworkCore.DataEncryption.Test/Helpers/StringHelper.cs renamed to test/EntityFrameworkCore.DataEncryption.Test/Helpers/DataHelper.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@
33

44
namespace Microsoft.EntityFrameworkCore.DataEncryption.Test.Helpers
55
{
6-
public static class StringHelper
6+
public static class DataHelper
77
{
88
private static readonly string Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
99
private static readonly Random Randomizer = new();
1010

11+
public static byte[] RandomBytes(int length)
12+
{
13+
var result = new byte[length];
14+
Randomizer.NextBytes(result);
15+
return result;
16+
}
17+
1118
public static string RandomString(int length)
1219
{
1320
var result = new char[length];

test/EntityFrameworkCore.DataEncryption.Test/Providers/AesProviderTest.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Security;
78
using System.Security.Cryptography;
89
using System.Text;
910
using Microsoft.EntityFrameworkCore.DataEncryption.Internal;
@@ -13,13 +14,32 @@ namespace Microsoft.EntityFrameworkCore.DataEncryption.Test.Providers
1314
{
1415
public class AesProviderTest
1516
{
17+
[Theory]
18+
[InlineData(AesKeySize.AES128Bits)]
19+
[InlineData(AesKeySize.AES192Bits)]
20+
[InlineData(AesKeySize.AES256Bits)]
21+
public void EncryptDecryptByteArrayTest(AesKeySize keySize)
22+
{
23+
byte[] input = DataHelper.RandomBytes(20);
24+
AesKeyInfo encryptionKeyInfo = AesProvider.GenerateKey(keySize);
25+
var provider = new AesProvider(encryptionKeyInfo.Key);
26+
27+
byte[] encryptedData = provider.Encrypt(input, b => b, StandardConverters.StreamToBytes);
28+
Assert.NotNull(encryptedData);
29+
30+
byte[] decryptedData = provider.Decrypt(encryptedData, b => b, StandardConverters.StreamToBytes);
31+
Assert.NotNull(decryptedData);
32+
33+
Assert.Equal(input, decryptedData);
34+
}
35+
1636
[Theory]
1737
[InlineData(AesKeySize.AES128Bits)]
1838
[InlineData(AesKeySize.AES192Bits)]
1939
[InlineData(AesKeySize.AES256Bits)]
2040
public void EncryptDecryptStringTest(AesKeySize keySize)
2141
{
22-
string input = StringHelper.RandomString(20);
42+
string input = DataHelper.RandomString(20);
2343
AesKeyInfo encryptionKeyInfo = AesProvider.GenerateKey(keySize);
2444
var provider = new AesProvider(encryptionKeyInfo.Key);
2545

@@ -32,6 +52,28 @@ public void EncryptDecryptStringTest(AesKeySize keySize)
3252
Assert.Equal(input, decryptedData);
3353
}
3454

55+
[Theory]
56+
[InlineData(AesKeySize.AES128Bits)]
57+
[InlineData(AesKeySize.AES192Bits)]
58+
[InlineData(AesKeySize.AES256Bits)]
59+
public void EncryptDecryptSecureStringTest(AesKeySize keySize)
60+
{
61+
SecureString input = DataHelper.RandomSecureString(20);
62+
AesKeyInfo encryptionKeyInfo = AesProvider.GenerateKey(keySize);
63+
var provider = new AesProvider(encryptionKeyInfo.Key);
64+
65+
string encryptedData = provider.Encrypt(input, Encoding.UTF8.GetBytes, StandardConverters.StreamToBase64String);
66+
Assert.NotNull(encryptedData);
67+
68+
SecureString decryptedData = provider.Decrypt(encryptedData, Convert.FromBase64String, StandardConverters.StreamToSecureString);
69+
Assert.NotNull(decryptedData);
70+
71+
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
72+
byte[] decryptedBytes = Encoding.UTF8.GetBytes(decryptedData);
73+
74+
Assert.Equal(inputBytes, decryptedBytes);
75+
}
76+
3577
[Theory]
3678
[InlineData(AesKeySize.AES128Bits)]
3779
[InlineData(AesKeySize.AES192Bits)]
@@ -98,7 +140,7 @@ private static void ExecuteAesEncryptionTest<TContext>(AesKeySize aesKeyType) wh
98140
var provider = new AesProvider(encryptionKeyInfo.Key, CipherMode.CBC, PaddingMode.Zeros);
99141
var author = new AuthorEntity("John", "Doe", 42)
100142
{
101-
Password = StringHelper.RandomSecureString(10),
143+
Password = DataHelper.RandomSecureString(10),
102144
Books = new List<BookEntity>
103145
{
104146
new("Lorem Ipsum", 300),

0 commit comments

Comments
 (0)