Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Haik committed Oct 27, 2023
1 parent 5cc95f6 commit 75c9d14
Showing 1 changed file with 55 additions and 36 deletions.
91 changes: 55 additions & 36 deletions Pandatech.Crypto.Tests/Aes256Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public void Generate_ShouldReturnByteArray()
Assert.Equal(length, randomBytes.Length);
}



[Fact]
public void EncryptDecryptWithParameter_ShouldReturnOriginalString()
Expand All @@ -35,6 +34,61 @@ public void EncryptDecryptWithoutParameter_ShouldReturnOriginalString()

Assert.Equal(original, decrypted);
}

[Fact]
public void EncryptWithParameterAndHash_ShouldReturnByteArrayWithHash()
{
var key = Random.GenerateAes256KeyString();
const string original = "MySensitiveData";
var encryptedWithHash = Aes256.EncryptWithHash(original, key);

Assert.NotNull(encryptedWithHash);
Assert.True(encryptedWithHash.Length > original.Length);
Assert.True(encryptedWithHash.Length > 64);
}

[Fact]
public void EncryptWithoutParameterAndHash_ShouldReturnByteArrayWithHash()
{
Environment.SetEnvironmentVariable("AES_KEY", Random.GenerateAes256KeyString());
const string original = "MySensitiveData";
var encryptedWithHash = Aes256.EncryptWithHash(original);

Assert.NotNull(encryptedWithHash);
Assert.True(encryptedWithHash.Length > original.Length);
Assert.True(encryptedWithHash.Length > 64);
}

[Fact]
public void DecryptWithParameterAndIgnoringHash_ShouldReturnOriginalString()
{
var key = Random.GenerateAes256KeyString();
const string original = "MySensitiveData";
var encryptedWithHash = Aes256.EncryptWithHash(original, key);
var decrypted = Aes256.DecryptIgnoringHash(encryptedWithHash, key);

Assert.Equal(original, decrypted);
}

[Fact]
public void DecryptWithoutParameterAndIgnoringHash_ShouldReturnOriginalString()
{
Environment.SetEnvironmentVariable("AES_KEY", Random.GenerateAes256KeyString());
const string original = "MySensitiveData";
var encryptedWithHash = Aes256.EncryptWithHash(original);
var decrypted = Aes256.DecryptIgnoringHash(encryptedWithHash);

Assert.Equal(original, decrypted);
}

[Fact]
public void DecryptIgnoringHashWithInvalidData_ShouldThrowException()
{
const string invalidKey = "InvalidKey";
var invalidData = new byte[50];

Assert.Throws<ArgumentException>(() => Aes256.DecryptIgnoringHash(invalidData, invalidKey));
}

[Fact]
public void EncryptDecryptWithInvalidKey_ShouldThrowException()
Expand Down Expand Up @@ -73,39 +127,4 @@ public void EncryptDecryptWithNullCipher_ShouldThrowException()

Assert.Throws<ArgumentException>(() => Aes256.Decrypt(null!, key));
}

[Fact]
public void EncryptWithHash_ShouldReturnByteArrayWithHash()
{
var key = Random.GenerateAes256KeyString();
const string original = "MySensitiveData";
var encryptedWithHash = Aes256.EncryptWithHash(original, key);

Assert.NotNull(encryptedWithHash);
Assert.True(encryptedWithHash.Length > original.Length);
Assert.True(encryptedWithHash.Length > 64);
}

[Fact]
public void DecryptIgnoringHash_ShouldReturnOriginalString()
{
var key = Random.GenerateAes256KeyString();
const string original = "MySensitiveData";
var encryptedWithHash = Aes256.EncryptWithHash(original, key);
var decrypted = Aes256.DecryptIgnoringHash(encryptedWithHash, key);

Assert.Equal(original, decrypted);
}

[Fact]
public void DecryptIgnoringHashWithInvalidData_ShouldThrowException()
{
const string invalidKey = "InvalidKey";
var invalidData = new byte[50];

Assert.Throws<ArgumentException>(() => Aes256.DecryptIgnoringHash(invalidData, invalidKey));
}



}

0 comments on commit 75c9d14

Please sign in to comment.