|
23 | 23 | import static org.junit.jupiter.api.Assertions.assertThrows; |
24 | 24 |
|
25 | 25 | import java.io.ByteArrayOutputStream; |
| 26 | +import java.io.IOException; |
26 | 27 | import java.io.OutputStream; |
27 | 28 | import java.nio.charset.StandardCharsets; |
28 | 29 | import java.util.Arrays; |
29 | 30 |
|
| 31 | +import org.apache.commons.lang3.ArrayFill; |
30 | 32 | import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.ValueSource; |
31 | 35 |
|
32 | 36 | /** |
33 | 37 | * Tests {@link Base58OutputStream}. |
@@ -160,6 +164,39 @@ private void testByteByByte(final byte[] encoded, final byte[] decoded, final in |
160 | 164 | assertArrayEquals(decoded, output, "Streaming byte-by-byte Base58 wrap-wrap-wrap!"); |
161 | 165 | } |
162 | 166 |
|
| 167 | + @ParameterizedTest |
| 168 | + @ValueSource(ints = { 0, 1, 2, 3, 4 }) |
| 169 | + void testDecodeByte49(final int len) throws IOException { |
| 170 | + // Sanity check, each step from scratch: |
| 171 | + final byte[] zeros = new byte[len]; |
| 172 | + final byte[] encoded0s = ArrayFill.fill(zeros.clone(), (byte) '1'); |
| 173 | + assertArrayEquals(encoded0s, Base58.builder().get().encode(zeros)); |
| 174 | + final byte[] decoded = Base58.builder().get().decode(encoded0s); |
| 175 | + assertArrayEquals(zeros, decoded, () -> String.format("zeros=%s, decoded=%s", Arrays.toString(zeros), Arrays.toString(decoded))); |
| 176 | + // Version 1.21.1: |
| 177 | + // AssertionFailedError: Streaming byte-by-byte Base58 decode, chunkSize=0, separator=[10], encoded=[49], decoded=[0], output=[0, 0] ==> array lengths |
| 178 | + // differ, expected: <1> but was: <2> |
| 179 | + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); |
| 180 | + final byte[] byteData = new byte[len]; |
| 181 | + try (OutputStream out = Base58OutputStream.builder().setOutputStream(byteOut).setEncode(true).get()) { |
| 182 | + for (final byte element : byteData) { |
| 183 | + out.write(element); |
| 184 | + } |
| 185 | + } |
| 186 | + final byte[] output0 = byteOut.toByteArray(); |
| 187 | + assertArrayEquals(encoded0s, output0, () -> String.format("Streaming byte-by-byte Base58 decode, encoded=%s, decoded=%s, output=%s", |
| 188 | + Arrays.toString(encoded0s), Arrays.toString(byteData), Arrays.toString(output0))); |
| 189 | + byteOut = new ByteArrayOutputStream(); |
| 190 | + try (OutputStream out = Base58OutputStream.builder().setOutputStream(byteOut).setEncode(false).get()) { |
| 191 | + for (final byte element : encoded0s) { |
| 192 | + out.write(element); |
| 193 | + } |
| 194 | + } |
| 195 | + final byte[] output1 = byteOut.toByteArray(); |
| 196 | + assertArrayEquals(byteData, output1, () -> String.format("Streaming byte-by-byte Base58 decode, encoded=%s, decoded=%s, output=%s", |
| 197 | + Arrays.toString(encoded0s), Arrays.toString(byteData), Arrays.toString(output1))); |
| 198 | + } |
| 199 | + |
163 | 200 | @Test |
164 | 201 | void testRfcTestVector1() { |
165 | 202 | assertEquals("2NEpo7TZRRrLZSi2U", Base58.builder().get().encodeToString("Hello World!".getBytes(StandardCharsets.US_ASCII))); |
|
0 commit comments