|
1 | 1 | using System;
|
2 | 2 | using System.Text;
|
3 | 3 | using Algorithms.Crypto.Digests;
|
| 4 | +using Algorithms.Crypto.Exceptions; |
4 | 5 | using FluentAssertions;
|
5 | 6 | using NUnit.Framework;
|
6 | 7 |
|
@@ -42,6 +43,116 @@ public void AsconHashA_ReturnsCorrectValue(string input, string expected)
|
42 | 43 | result.Should().Be(expected);
|
43 | 44 | }
|
44 | 45 |
|
| 46 | + [Test] |
| 47 | + public void BlockUpdate_WithValidOffsetAndLength_ShouldProcessCorrectly() |
| 48 | + { |
| 49 | + // Arrange |
| 50 | + var input = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99 }; |
| 51 | + var offset = 2; |
| 52 | + var length = 6; // Picking 6 bytes starting from offset 2 |
| 53 | + |
| 54 | + // Act |
| 55 | + var act = () => asconHash.BlockUpdate(input, offset, length); |
| 56 | + |
| 57 | + // Assert |
| 58 | + act.Should().NotThrow(); // Ensure no exceptions are thrown during processing |
| 59 | + |
| 60 | + // Finalize the hash and check the output size |
| 61 | + var output = new byte[asconHash.GetDigestSize()]; |
| 62 | + asconHash.DoFinal(output, 0); |
| 63 | + output.Should().HaveCount(32); // Ascon hash size is 32 bytes |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public void BlockUpdate_WithInvalidOffset_ShouldThrowDataLengthException() |
| 68 | + { |
| 69 | + // Arrange |
| 70 | + var input = new byte[] { 0x00, 0x11, 0x22, 0x33 }; |
| 71 | + var offset = 3; // Offset goes too close to the end |
| 72 | + var length = 3; // Length would exceed buffer size |
| 73 | + |
| 74 | + // Act |
| 75 | + var act = () => asconHash.BlockUpdate(input, offset, length); |
| 76 | + |
| 77 | + // Assert |
| 78 | + act.Should().Throw<DataLengthException>() |
| 79 | + .WithMessage("input buffer too short"); |
| 80 | + } |
| 81 | + |
| 82 | + [Test] |
| 83 | + public void BlockUpdate_WithInvalidLength_ShouldThrowDataLengthException() |
| 84 | + { |
| 85 | + // Arrange |
| 86 | + var input = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; |
| 87 | + var offset = 1; // Valid offset |
| 88 | + var length = 10; // Invalid length (exceeds buffer) |
| 89 | + |
| 90 | + // Act |
| 91 | + var act = () => asconHash.BlockUpdate(input, offset, length); |
| 92 | + |
| 93 | + // Assert |
| 94 | + act.Should().Throw<DataLengthException>() |
| 95 | + .WithMessage("input buffer too short"); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + public void BlockUpdate_WithPartialBlock_ShouldProcessCorrectly() |
| 100 | + { |
| 101 | + // Arrange |
| 102 | + var input = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44 }; |
| 103 | + var offset = 0; |
| 104 | + var length = 5; // Less than 8 bytes, partial block |
| 105 | + |
| 106 | + // Act |
| 107 | + asconHash.BlockUpdate(input, offset, length); |
| 108 | + |
| 109 | + // Assert |
| 110 | + var output = new byte[asconHash.GetDigestSize()]; |
| 111 | + asconHash.DoFinal(output, 0); |
| 112 | + output.Should().HaveCount(32); // Ensure valid hash output |
| 113 | + } |
| 114 | + |
| 115 | + [Test] |
| 116 | + public void BlockUpdate_WithFullBlock_ShouldProcessCorrectly() |
| 117 | + { |
| 118 | + // Arrange |
| 119 | + var input = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 }; |
| 120 | + var offset = 0; |
| 121 | + var length = 8; // Full block |
| 122 | + |
| 123 | + // Act |
| 124 | + asconHash.BlockUpdate(input, offset, length); |
| 125 | + |
| 126 | + // Assert |
| 127 | + var output = new byte[asconHash.GetDigestSize()]; |
| 128 | + asconHash.DoFinal(output, 0); |
| 129 | + output.Should().HaveCount(32); // Ensure valid hash output |
| 130 | + } |
| 131 | + |
| 132 | + [Test] |
| 133 | + public void BlockUpdate_MultipleCalls_ShouldProcessCorrectly() |
| 134 | + { |
| 135 | + // Arrange |
| 136 | + var input1 = new byte[] { 0x00, 0x11, 0x22 }; |
| 137 | + var input2 = new byte[] { 0x33, 0x44, 0x55, 0x66, 0x77 }; |
| 138 | + |
| 139 | + // Act |
| 140 | + asconHash.BlockUpdate(input1, 0, input1.Length); |
| 141 | + asconHash.BlockUpdate(input2, 0, input2.Length); |
| 142 | + |
| 143 | + // Assert |
| 144 | + var output = new byte[asconHash.GetDigestSize()]; |
| 145 | + asconHash.DoFinal(output, 0); |
| 146 | + output.Should().HaveCount(32); // Ensure valid hash output |
| 147 | + } |
| 148 | + |
| 149 | + [Test] |
| 150 | + public void AsconHash_WhenGetNameIsCalled_ReturnsCorrectValue() |
| 151 | + { |
| 152 | + asconHash.AlgorithmName.Should().Be("Ascon-Hash"); |
| 153 | + asconHashA.AlgorithmName.Should().Be("Ascon-HashA"); |
| 154 | + } |
| 155 | + |
45 | 156 | private static string ToHexString(byte[] bytes)
|
46 | 157 | {
|
47 | 158 | return BitConverter.ToString(bytes).Replace("-", "").ToLower();
|
|
0 commit comments