Skip to content

Commit ff11772

Browse files
committed
Added test and toString()
1 parent 6f039e5 commit ff11772

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/main/java/io/ipfs/cid/Cid.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ public byte[] toBytesV1() {
6262
return Arrays.copyOfRange(res, 0, index + hashBytes.length);
6363
}
6464

65+
@Override
66+
public String toString() {
67+
if (version == 0) {
68+
return hash.toString();
69+
} else if (version == 1) {
70+
return Multibase.encode(Multibase.Base.Base58BTC, toBytesV1());
71+
}
72+
throw new IllegalStateException("Unknown Cid version: " + version);
73+
}
74+
6575
@Override
6676
public boolean equals(Object o) {
6777
if (this == o) return true;

src/test/java/io/ipfs/cid/CidTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22

33
import org.junit.*;
44
import io.ipfs.multibase.*;
5+
6+
import java.io.*;
57
import java.util.*;
68

79
public class CidTest {
810

911
@Test
10-
public void base58Test() {
11-
List<String> examples = Arrays.asList("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB",
12-
"QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy");
12+
public void stringTest() throws IOException {
13+
List<String> examples = Arrays.asList(
14+
"QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB",
15+
"QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy",
16+
"zdpuAyvkgEDQm9TenwGkd5eNaosSxjgEYd8QatfPetgB1CdEZ"
17+
);
1318
for (String example: examples) {
14-
byte[] output = Base58.decode(example);
15-
String encoded = Base58.encode(output);
16-
if (!encoded.equals(encoded))
17-
throw new IllegalStateException("Incorrect base58! " + example + " => " + encoded);
19+
Cid cid = Cid.decode(example);
20+
String encoded = cid.toString();
21+
if (!encoded.equals(example))
22+
throw new IllegalStateException("Incorrect cid string! " + example + " => " + encoded);
1823
}
1924
}
2025
}

0 commit comments

Comments
 (0)