using System.Text; using Centron.Common.TextCoding; using Centron.Tests.EndToEnd.Infrastructure; using Xunit; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.Tests.CryptoLogics { public class CryptoLogicTests : CentronTest { public CryptoLogicTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } [Theory] [InlineData(null)] [InlineData("MySuperSecretPassword")] public void Encrypt_Decrypt_Text(string secret) { var inputText = "Irgendein Text den ich verschlüsseln will, zB ein Connection-String!"; var encryptedText = new AESCryptoLogic().EncryptText(inputText, secret); this.Verifier.Verify($"EncryptedText_{secret}", encryptedText); var decryptedText = new AESCryptoLogic().DecryptText(encryptedText, secret); Assert.Equal(inputText, decryptedText); } [Theory] [InlineData(null)] [InlineData("")] [InlineData(" ")] [InlineData("\t")] [InlineData("\r\n")] public void Encrypt_Decrypt_Text_Null(string text) { var encryptedText = new AESCryptoLogic().EncryptText(text); Assert.Equal(string.Empty, encryptedText); var decryptedText = new AESCryptoLogic().DecryptText(text); Assert.Equal(string.Empty, decryptedText); } [Fact] public void Decrypt_Text_InvalidDataReturnsEmptyString() { var decryptedText = new AESCryptoLogic().DecryptText("Haha"); Assert.Equal(string.Empty, decryptedText); } [Theory] [InlineData(null)] [InlineData("MySuperSecretPassword")] public void Encrypt_Decrypt_ByteArray(string secret) { var inputByteArray = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; var encryptedByteArray = new AESCryptoLogic().EncryptByteArray(inputByteArray, secret); this.Verifier.Verify($"EncryptedByteArray_{secret}", encryptedByteArray); var decryptedByteArray = new AESCryptoLogic().DecryptByteArray(encryptedByteArray, secret); Assert.Equal(inputByteArray, decryptedByteArray); } [Fact] public void Encrypt_Decrypt_ByteArray_Null() { var encryptedByteArray = new AESCryptoLogic().EncryptByteArray(null); Assert.Null(encryptedByteArray); var decryptedByteArray = new AESCryptoLogic().DecryptByteArray(null); Assert.Null(decryptedByteArray); } [Fact] public void Decrypt_ByteArray_InvalidDataReturnsNull() { var decryptedByteArray = new AESCryptoLogic().DecryptByteArray(new byte[] {1, 2, 3}); Assert.Null(decryptedByteArray); } } }