Skip to content

Commit

Permalink
src: Update tests for empty inputs.
Browse files Browse the repository at this point in the history
Plus a few other test improvements.
  • Loading branch information
samuel-lucas6 committed Aug 12, 2022
1 parent e88a290 commit ad51ae8
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 199 deletions.
44 changes: 15 additions & 29 deletions src/Geralt.Tests/Argon2idTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public void DeriveKey_ValidInputs()
}

[TestMethod]
public void DeriveKey_DifferentPassword()
public void DeriveKey_EmptyPassword()
{
Span<byte> key = stackalloc byte[Argon2id.KeySize];
Span<byte> password = Password.ToArray();
password[0]++;
Span<byte> password = Span<byte>.Empty;
Argon2id.DeriveKey(key, password, Salt, Iterations, MemorySize);
Assert.IsFalse(key.SequenceEqual(new byte[key.Length]));
Assert.IsFalse(key.SequenceEqual(Argon2idKey));
}

Expand Down Expand Up @@ -68,15 +68,7 @@ public void DeriveKey_InvalidKey()
var key = new byte[Argon2id.MinKeySize - 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => Argon2id.DeriveKey(key, Password, Salt, Iterations, MemorySize));
}

[TestMethod]
public void DeriveKey_InvalidPassword()
{
var key = new byte[Argon2id.KeySize];
var password = Array.Empty<byte>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => Argon2id.DeriveKey(key, password, Salt, Iterations, MemorySize));
}


[TestMethod]
public void DeriveKey_InvalidSalt()
{
Expand Down Expand Up @@ -111,6 +103,16 @@ public void ComputeHash_ValidInputs()
bool rehash = Argon2id.NeedsRehash(hash, Iterations, MemorySize);
Assert.IsFalse(rehash);
}

[TestMethod]
public void ComputeHash_EmptyPassword()
{
Span<byte> hash = stackalloc byte[Argon2id.MaxHashSize];
Span<byte> password = Span<byte>.Empty;
Argon2id.ComputeHash(hash, password, Iterations, MemorySize);
bool valid = Argon2id.VerifyHash(hash, password);
Assert.IsTrue(valid);
}

[TestMethod]
public void ComputeHash_InvalidHash()
Expand All @@ -121,14 +123,6 @@ public void ComputeHash_InvalidHash()
Assert.ThrowsException<ArgumentOutOfRangeException>(() => Argon2id.ComputeHash(hash, Password, Iterations, MemorySize));
}

[TestMethod]
public void ComputeHash_InvalidPassword()
{
var hash = new byte[Argon2id.MaxHashSize];
var password = Array.Empty<byte>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => Argon2id.ComputeHash(hash, password, Iterations, MemorySize));
}

[TestMethod]
public void ComputeHash_InvalidIterations()
{
Expand Down Expand Up @@ -177,15 +171,7 @@ public void VerifyHash_InvalidHash()
hash = new byte[Argon2id.MaxHashSize + 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => Argon2id.VerifyHash(hash, Password));
}

[TestMethod]
public void VerifyHash_InvalidPassword()
{
var hash = Encoding.UTF8.GetBytes(Argon2idHash);
var password = Array.Empty<byte>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => Argon2id.VerifyHash(hash, password));
}


[TestMethod]
public void NeedsRehash_CorrectParameters()
{
Expand Down
126 changes: 55 additions & 71 deletions src/Geralt.Tests/BLAKE2bTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public void ComputeHash_ValidInputs()
}

[TestMethod]
public void ComputeHash_DifferentMessage()
public void ComputeHash_EmptyMessage()
{
Span<byte> hash = stackalloc byte[BLAKE2b.MaxHashSize];
BLAKE2b.ComputeHash(hash, Hash);
Span<byte> message = Span<byte>.Empty;
BLAKE2b.ComputeHash(hash, message);
Assert.IsFalse(hash.SequenceEqual(new byte[hash.Length]));
Assert.IsFalse(hash.SequenceEqual(Hash));
}

[TestMethod]
public void ComputeHash_InvalidHash()
{
Expand All @@ -47,14 +49,6 @@ public void ComputeHash_InvalidHash()
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeHash(hash, Message));
}

[TestMethod]
public void ComputeHash_InvalidMessage()
{
var hash = new byte[BLAKE2b.MaxHashSize];
var message = Array.Empty<byte>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeHash(hash, message));
}

[TestMethod]
public void ComputeHashStream_ValidInputs()
{
Expand All @@ -66,12 +60,13 @@ public void ComputeHashStream_ValidInputs()
}

[TestMethod]
public void ComputeHashStream_DifferentMessage()
public void ComputeHashStream_EmptyMessage()
{
var hash = new byte[BLAKE2b.MaxHashSize];
using var blake2b = new BLAKE2bHashAlgorithm(hash.Length);
using var memoryStream = new MemoryStream(Hash, writable: false);
using var memoryStream = new MemoryStream(Array.Empty<byte>(), writable: false);
hash = blake2b.ComputeHash(memoryStream);
Assert.IsFalse(hash.SequenceEqual(new byte[hash.Length]));
Assert.IsFalse(hash.SequenceEqual(Hash));
}

Expand All @@ -85,96 +80,93 @@ public void ComputeHashStream_InvalidHash()
}

[TestMethod]
public void ComputeTagStream_ValidInputs()
public void ComputeTag_ValidInputs()
{
var tag = new byte[BLAKE2b.TagSize];
using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, Key);
using var memoryStream = new MemoryStream(Message, writable: false);
tag = blake2b.ComputeHash(memoryStream);
Span<byte> tag = stackalloc byte[BLAKE2b.TagSize];
BLAKE2b.ComputeTag(tag, Message, Key);
Assert.IsTrue(tag.SequenceEqual(Tag));
}

[TestMethod]
public void ComputeTagStream_DifferentKey()
public void ComputeTag_EmptyMessage()
{
var tag = new byte[BLAKE2b.TagSize];
using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, DifferentKey);
using var memoryStream = new MemoryStream(Message, writable: false);
tag = blake2b.ComputeHash(memoryStream);
Span<byte> tag = stackalloc byte[BLAKE2b.TagSize];
Span<byte> message = Span<byte>.Empty;
BLAKE2b.ComputeTag(tag, message, Key);
Assert.IsFalse(tag.SequenceEqual(new byte[tag.Length]));
Assert.IsFalse(tag.SequenceEqual(Tag));
bool valid = BLAKE2b.VerifyTag(tag, message, Key);
Assert.IsTrue(valid);
}

[TestMethod]
public void ComputeTagStream_DifferentMessage()
public void ComputeTag_DifferentKey()
{
var tag = new byte[BLAKE2b.TagSize];
using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, Key);
using var memoryStream = new MemoryStream(Hash, writable: false);
tag = blake2b.ComputeHash(memoryStream);
Span<byte> tag = stackalloc byte[BLAKE2b.TagSize];
BLAKE2b.ComputeTag(tag, Message, DifferentKey);
Assert.IsFalse(tag.SequenceEqual(Tag));
}

[TestMethod]
public void ComputeTagStream_InvalidKey()
{
var tag = new byte[BLAKE2b.TagSize];
var key = new byte[BLAKE2b.MinKeySize - 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => new BLAKE2bHashAlgorithm(tag.Length, key));
key = new byte[BLAKE2b.MaxKeySize + 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => new BLAKE2bHashAlgorithm(tag.Length, key));
}

[TestMethod]
public void ComputeTag_ValidInputs()
public void ComputeTag_InvalidTag()
{
Span<byte> tag = stackalloc byte[BLAKE2b.TagSize];
BLAKE2b.ComputeTag(tag, Message, Key);
Assert.IsTrue(tag.SequenceEqual(Tag));
var tag = new byte[BLAKE2b.MinTagSize - 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeTag(tag, Message, Key));
tag = new byte[BLAKE2b.MaxTagSize + 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeTag(tag, Message, Key));
}

[TestMethod]
public void ComputeTag_DifferentMessage()
public void ComputeTag_InvalidKey()
{
Span<byte> tag = stackalloc byte[BLAKE2b.TagSize];
BLAKE2b.ComputeTag(tag, Hash, Key);
Assert.IsFalse(tag.SequenceEqual(Tag));
var tag = new byte[BLAKE2b.TagSize];
var key = new byte[BLAKE2b.MinKeySize - 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeTag(tag, Message, key));
key = new byte[BLAKE2b.MaxKeySize + 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeTag(tag, Message, key));
}

[TestMethod]
public void ComputeTag_DifferentKey()
public void ComputeTagStream_ValidInputs()
{
Span<byte> tag = stackalloc byte[BLAKE2b.TagSize];
BLAKE2b.ComputeTag(tag, Hash, DifferentKey);
Assert.IsFalse(tag.SequenceEqual(Tag));
var tag = new byte[BLAKE2b.TagSize];
using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, Key);
using var memoryStream = new MemoryStream(Message, writable: false);
tag = blake2b.ComputeHash(memoryStream);
Assert.IsTrue(tag.SequenceEqual(Tag));
}

[TestMethod]
public void ComputeTag_InvalidTag()
public void ComputeTagStream_DifferentKey()
{
var tag = new byte[BLAKE2b.MinTagSize - 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeTag(tag, Message, Key));
tag = new byte[BLAKE2b.MaxTagSize + 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeTag(tag, Message, Key));
var tag = new byte[BLAKE2b.TagSize];
using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, DifferentKey);
using var memoryStream = new MemoryStream(Message, writable: false);
tag = blake2b.ComputeHash(memoryStream);
Assert.IsFalse(tag.SequenceEqual(Tag));
}

[TestMethod]
public void ComputeTag_InvalidMessage()
public void ComputeTagStream_EmptyMessage()
{
var tag = new byte[BLAKE2b.TagSize];
var message = Array.Empty<byte>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeTag(tag, message, Key));
using var blake2b = new BLAKE2bHashAlgorithm(tag.Length, Key);
using var memoryStream = new MemoryStream(Array.Empty<byte>(), writable: false);
tag = blake2b.ComputeHash(memoryStream);
Assert.IsFalse(tag.SequenceEqual(new byte[tag.Length]));
Assert.IsFalse(tag.SequenceEqual(Tag));
}

[TestMethod]
public void ComputeTag_InvalidKey()
public void ComputeTagStream_InvalidKey()
{
var tag = new byte[BLAKE2b.TagSize];
var key = new byte[BLAKE2b.MinKeySize - 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeTag(tag, Message, key));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => new BLAKE2bHashAlgorithm(tag.Length, key));
key = new byte[BLAKE2b.MaxKeySize + 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeTag(tag, Message, key));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => new BLAKE2bHashAlgorithm(tag.Length, key));
}

[TestMethod]
public void VerifyTag_ValidInputs()
{
Expand Down Expand Up @@ -211,15 +203,7 @@ public void VerifyTag_InvalidTag()
tag = new byte[BLAKE2b.MaxTagSize + 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.VerifyTag(tag, Message, Key));
}

[TestMethod]
public void VerifyTag_InvalidMessage()
{
var tag = new byte[BLAKE2b.TagSize];
var message = Array.Empty<byte>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.VerifyTag(tag, message, Key));
}


[TestMethod]
public void VerifyTag_InvalidKey()
{
Expand Down
42 changes: 16 additions & 26 deletions src/Geralt.Tests/ChaCha20Poly1305Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public void Encrypt_ValidInputs()
}

[TestMethod]
public void Encrypt_DifferentPlaintext()
public void Encrypt_EmptyPlaintext()
{
Span<byte> ciphertext = stackalloc byte[Plaintext.Length + ChaCha20Poly1305.TagSize];
Span<byte> plaintext = Plaintext.ToArray();
plaintext[0]++;
Span<byte> plaintext = Span<byte>.Empty;
Span<byte> ciphertext = stackalloc byte[plaintext.Length + ChaCha20Poly1305.TagSize];
ChaCha20Poly1305.Encrypt(ciphertext, plaintext, Nonce, Key);
Assert.IsFalse(ciphertext.SequenceEqual(Ciphertext));
Assert.IsFalse(ciphertext.SequenceEqual(new byte[ciphertext.Length]));
Span<byte> decrypted = stackalloc byte[plaintext.Length];
ChaCha20Poly1305.Decrypt(decrypted, ciphertext, Nonce, Key);
Assert.IsTrue(plaintext.SequenceEqual(decrypted));
}

[TestMethod]
Expand Down Expand Up @@ -66,18 +68,12 @@ public void Encrypt_DifferentAssociatedData()
[TestMethod]
public void Encrypt_InvalidCiphertext()
{
var ciphertext = Array.Empty<byte>();
var ciphertext = new byte[Plaintext.Length + ChaCha20Poly1305.TagSize + 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => ChaCha20Poly1305.Encrypt(ciphertext, Plaintext, Nonce, Key));
ciphertext = new byte[Plaintext.Length + ChaCha20Poly1305.TagSize - 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => ChaCha20Poly1305.Encrypt(ciphertext, Plaintext, Nonce, Key));
}

[TestMethod]
public void Encrypt_InvalidPlaintext()
{
var ciphertext = new byte[Plaintext.Length + ChaCha20Poly1305.TagSize];
var plaintext = Array.Empty<byte>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => ChaCha20Poly1305.Encrypt(ciphertext, plaintext, Nonce, Key));
}


[TestMethod]
public void Encrypt_InvalidNonce()
{
Expand Down Expand Up @@ -105,7 +101,7 @@ public void Decrypt_ValidInputs()
ChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, Key, AssociatedData);
Assert.IsTrue(plaintext.SequenceEqual(Plaintext));
}

[TestMethod]
public void Decrypt_WrongCiphertext()
{
Expand Down Expand Up @@ -154,18 +150,12 @@ public void Decrypt_WrongAssociatedData()
[TestMethod]
public void Decrypt_InvalidPlaintext()
{
var plaintext = Array.Empty<byte>();
var plaintext = new byte[Ciphertext.Length - ChaCha20Poly1305.TagSize + 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => ChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, Key));
plaintext = new byte[Ciphertext.Length - ChaCha20Poly1305.TagSize - 1];
Assert.ThrowsException<ArgumentOutOfRangeException>(() => ChaCha20Poly1305.Decrypt(plaintext, Ciphertext, Nonce, Key));
}

[TestMethod]
public void Decrypt_InvalidCiphertext()
{
var plaintext = new byte[Ciphertext.Length - ChaCha20Poly1305.TagSize];
var ciphertext = Array.Empty<byte>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => ChaCha20Poly1305.Decrypt(plaintext, ciphertext, Nonce, Key));
}


[TestMethod]
public void Decrypt_InvalidNonce()
{
Expand Down
Loading

0 comments on commit ad51ae8

Please sign in to comment.