|
6 | 6 |
|
7 | 7 | import com.hedera.pbj.runtime.io.buffer.BufferedData; |
8 | 8 | import com.hedera.pbj.runtime.io.buffer.Bytes; |
9 | | -import com.hedera.pbj.runtime.io.buffer.RandomAccessData; |
10 | 9 | import java.nio.ByteBuffer; |
11 | 10 | import java.security.MessageDigest; |
12 | 11 | import java.security.NoSuchAlgorithmException; |
| 12 | +import java.util.Arrays; |
13 | 13 | import java.util.stream.Stream; |
14 | 14 | import org.junit.jupiter.api.Test; |
15 | 15 | import org.junit.jupiter.params.ParameterizedTest; |
@@ -66,10 +66,6 @@ private static Stream<byte[]> provideByteArrays() { |
66 | 66 | FULL_RANGE_ARRAY); |
67 | 67 | } |
68 | 68 |
|
69 | | - private static Stream<Arguments> provideByteArrayArguments() { |
70 | | - return provideByteArrays().map(Arguments::of); |
71 | | - } |
72 | | - |
73 | 69 | @ParameterizedTest |
74 | 70 | @MethodSource("provideByteArrays") |
75 | 71 | void testWriteByteArray(byte[] array) { |
@@ -155,8 +151,29 @@ void testWriteRandomAccessData(byte[] array) { |
155 | 151 | final Bytes bytes = Bytes.wrap(array); |
156 | 152 | final MessageDigest testDigest = buildDigest(); |
157 | 153 | final WritableMessageDigest wmd = new WritableMessageDigest(testDigest); |
158 | | - wmd.writeBytes((RandomAccessData) bytes); |
| 154 | + wmd.writeBytes(bytes); |
159 | 155 |
|
160 | 156 | assertArrayEquals(ctrlDigest.digest(), testDigest.digest()); |
161 | 157 | } |
| 158 | + |
| 159 | + @ParameterizedTest |
| 160 | + @MethodSource("provideByteArrays") |
| 161 | + void testDigestInto(byte[] array) { |
| 162 | + final MessageDigest ctrlDigest = buildDigest(); |
| 163 | + ctrlDigest.update(array); |
| 164 | + final byte[] expectedDigest = ctrlDigest.digest(); |
| 165 | + |
| 166 | + final WritableMessageDigest wmd = new WritableMessageDigest(buildDigest()); |
| 167 | + wmd.writeBytes(array); |
| 168 | + assertEquals(array.length, wmd.position()); |
| 169 | + |
| 170 | + final byte[] output = new byte[expectedDigest.length + 4]; |
| 171 | + Arrays.fill(output, (byte) 0x55); |
| 172 | + wmd.digestInto(output, 2); |
| 173 | + |
| 174 | + assertEquals(0, wmd.position()); |
| 175 | + assertArrayEquals(expectedDigest, Arrays.copyOfRange(output, 2, 2 + expectedDigest.length)); |
| 176 | + assertArrayEquals(new byte[] {(byte) 0x55, (byte) 0x55}, Arrays.copyOfRange(output, 0, 2)); |
| 177 | + assertArrayEquals(new byte[] {(byte) 0x55, (byte) 0x55}, Arrays.copyOfRange(output, 2 + expectedDigest.length, output.length)); |
| 178 | + } |
162 | 179 | } |
0 commit comments