Skip to content

Commit 99a7f35

Browse files
committed
Two way tests for multibase encoding and decoding
1 parent c9329b4 commit 99a7f35

2 files changed

Lines changed: 83 additions & 51 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.ipfs.multibase;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertNotEquals;
6+
7+
public class MultibaseBadInputsTest {
8+
9+
@Test
10+
public void invalidBase16Test() {
11+
String example = "f012"; // hex string of odd length
12+
byte[] output = Multibase.decode(example);
13+
String encoded = Multibase.encode(Multibase.Base.Base16, output);
14+
assertNotEquals(example, encoded);
15+
}
16+
17+
@Test (expected = NumberFormatException.class)
18+
public void invalidWithExceptionBase16Test() {
19+
String example = "f0g"; // g char is not allowed in hex
20+
Multibase.decode(example);
21+
}
22+
23+
}

src/test/java/io/ipfs/multibase/MultibaseTest.java

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,82 @@
33
import org.junit.Test;
44

55
import java.util.Arrays;
6-
import java.util.List;
6+
import java.util.Collection;
77

8-
import static org.junit.Assert.assertEquals;
9-
import static org.junit.Assert.assertNotEquals;
8+
import static org.junit.Assert.*;
109

10+
import org.junit.runner.RunWith;
11+
import org.junit.runners.Parameterized;
12+
import org.junit.runners.Parameterized.Parameters;
13+
14+
@RunWith(Parameterized.class)
1115
public class MultibaseTest {
1216

13-
@Test
14-
public void base58Test() {
15-
List<String> examples = Arrays.asList(
16-
"zQmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB",
17-
"zQmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy",
18-
"z11");
19-
for (String example: examples) {
20-
byte[] output = Multibase.decode(example);
21-
String encoded = Multibase.encode(Multibase.Base.Base58BTC, output);
22-
assertEquals(example, encoded);
23-
}
24-
}
17+
private Multibase.Base base;
18+
private byte[] raw;
19+
private String encoded;
2520

26-
@Test
27-
public void zeroBytesBase58() {
28-
for (int i=0; i < 32; i++) {
29-
String encoded = Multibase.encode(Multibase.Base.Base58BTC, new byte[i]);
30-
byte[] output = Multibase.decode(encoded);
31-
if (! Arrays.equals(output, new byte[i]))
32-
throw new IllegalStateException("Failed to round trip zero array of length " + i);
33-
}
21+
public MultibaseTest(Multibase.Base base, byte[] raw, String encoded) {
22+
this.base = base;
23+
this.raw = raw;
24+
this.encoded = encoded;
3425
}
3526

36-
@Test
37-
public void base16Test() {
38-
List<String> examples = Arrays.asList("f234abed8debede",
39-
"f87ad873defc2b288",
40-
"f",
41-
"f01",
42-
"f0123456789abcdef");
43-
for (String example: examples) {
44-
byte[] output = Multibase.decode(example);
45-
String encoded = Multibase.encode(Multibase.Base.Base16, output);
46-
assertEquals(example, encoded);
47-
}
27+
@Parameters(name = "{index}: {0}, {2}")
28+
public static Collection<Object[]> data() {
29+
return Arrays.asList(new Object[][]{
30+
{Multibase.Base.Base58BTC, hexToBytes("1220120F6AF601D46E10B2D2E11ED71C55D25F3042C22501E41D1246E7A1E9D3D8EC"), "zQmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB"},
31+
{Multibase.Base.Base58BTC, hexToBytes("1220BA8632EF1A07986B171B3C8FAF0F79B3EE01B6C30BBE15A13261AD6CB0D02E3A"), "zQmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy"},
32+
{Multibase.Base.Base58BTC, new byte[1], "z1"},
33+
{Multibase.Base.Base58BTC, new byte[2], "z11"},
34+
{Multibase.Base.Base58BTC, new byte[4], "z1111"},
35+
{Multibase.Base.Base58BTC, new byte[8], "z11111111"},
36+
{Multibase.Base.Base58BTC, new byte[16], "z1111111111111111"},
37+
{Multibase.Base.Base58BTC, new byte[32], "z11111111111111111111111111111111"},
38+
{Multibase.Base.Base16, hexToBytes("234ABED8DEBEDE"), "f234abed8debede"},
39+
{Multibase.Base.Base16, hexToBytes("87AD873DEFC2B288"), "f87ad873defc2b288"},
40+
{Multibase.Base.Base16, hexToBytes(""), "f"},
41+
{Multibase.Base.Base16, hexToBytes("01"), "f01"},
42+
{Multibase.Base.Base16, hexToBytes("0123456789ABCDEF"), "f0123456789abcdef"},
43+
{Multibase.Base.Base32, hexToBytes("01A195B1B1BC81DDBDC9B190"), "bnbswy3dpeb3w64tmmq"},
44+
{Multibase.Base.Base32, hexToBytes("0005C44881FE0EC595FFC7F14EE4B7060522875977F0B52C7E8F59DCA12B77480049B641A4"), "bafyreid7qoywk77r7rj3slobqfekdvs57qwuwh5d2z3sqsw52iabe3mqne"},
45+
});
4846
}
4947

5048
@Test
51-
public void base32Test() {
52-
List<String> examples = Arrays.asList("bnbswy3dpeb3w64tmmq");
53-
for (String example: examples) {
54-
byte[] output = Multibase.decode(example);
55-
String encoded = Multibase.encode(Multibase.Base.Base32, output);
56-
assertEquals(example, encoded);
57-
}
49+
public void testEncode() {
50+
String output = Multibase.encode(base, raw);
51+
assertEquals(encoded, output);
5852
}
5953

6054
@Test
61-
public void invalidBase16Test() {
62-
String example = "f012"; // hex string of odd length
63-
byte[] output = Multibase.decode(example);
64-
String encoded = Multibase.encode(Multibase.Base.Base16, output);
65-
assertNotEquals(example, encoded);
55+
public void testDecode() {
56+
byte[] output = Multibase.decode(encoded);
57+
assertArrayEquals(String.format("Expected %s, but got %s", bytesToHex(raw), bytesToHex(output)), raw, output);
58+
}
6659

60+
//Copied from https://stackoverflow.com/a/140861
61+
private static byte[] hexToBytes(String s) {
62+
int len = s.length();
63+
byte[] data = new byte[len / 2];
64+
for (int i = 0; i < len; i += 2) {
65+
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
66+
+ Character.digit(s.charAt(i + 1), 16));
67+
}
68+
return data;
6769
}
6870

69-
@Test (expected = NumberFormatException.class)
70-
public void invalidWithExceptionBase16Test() {
71-
String example = "f0g"; // g char is not allowed in hex
72-
Multibase.decode(example);
71+
//Copied from https://stackoverflow.com/a/9855338
72+
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
73+
74+
private static String bytesToHex(byte[] bytes) {
75+
char[] hexChars = new char[bytes.length * 2];
76+
for (int j = 0; j < bytes.length; j++) {
77+
int v = bytes[j] & 0xFF;
78+
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
79+
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
80+
}
81+
return new String(hexChars);
7382
}
7483

7584
}

0 commit comments

Comments
 (0)