Skip to content

Commit 86bada1

Browse files
committed
more tests
Added cid.toBytes()
1 parent 6841ea7 commit 86bada1

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public Cid(long version, Codec codec, Multihash hash) {
4747
this.hash = hash;
4848
}
4949

50-
public byte[] toBytesV0() {
50+
private byte[] toBytesV0() {
5151
return hash.toBytes();
5252
}
5353

5454
private static final int MAX_VARINT_LEN64 = 10;
5555

56-
public byte[] toBytesV1() {
56+
private byte[] toBytesV1() {
5757
byte[] hashBytes = hash.toBytes();
5858
byte[] res = new byte[2 * MAX_VARINT_LEN64 + hashBytes.length];
5959
int index = putUvarint(res, 0, version);
@@ -62,6 +62,14 @@ public byte[] toBytesV1() {
6262
return Arrays.copyOfRange(res, 0, index + hashBytes.length);
6363
}
6464

65+
public byte[] toBytes() {
66+
if (version == 0)
67+
return toBytesV0();
68+
else if (version == 1)
69+
return toBytesV1();
70+
throw new IllegalStateException("Unknown cid version: " + version);
71+
}
72+
6573
@Override
6674
public String toString() {
6775
if (version == 0) {
@@ -113,7 +121,7 @@ public static Cid decode(String v) throws IOException {
113121
return cast(data);
114122
}
115123

116-
private static Cid cast(byte[] data) throws IOException {
124+
public static Cid cast(byte[] data) throws IOException {
117125
if (data.length == 34 && data[0] == 0x18 && data[1] == 32)
118126
return buildCidV0(new Multihash(data));
119127

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package io.ipfs.cid;
22

3+
import io.ipfs.multihash.*;
34
import org.junit.*;
45
import io.ipfs.multibase.*;
56

67
import java.io.*;
8+
import java.security.*;
79
import java.util.*;
810

911
public class CidTest {
1012

1113
@Test
12-
public void stringTest() throws IOException {
14+
public void validStrings() throws IOException {
1315
List<String> examples = Arrays.asList(
1416
"QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB",
1517
"QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy",
@@ -22,4 +24,49 @@ public void stringTest() throws IOException {
2224
throw new IllegalStateException("Incorrect cid string! " + example + " => " + encoded);
2325
}
2426
}
27+
28+
@Test
29+
public void emptyStringShouldFail() throws IOException {
30+
try {
31+
Cid cid = Cid.decode("");
32+
throw new RuntimeException();
33+
} catch (IllegalStateException e) {}
34+
}
35+
36+
@Test
37+
public void basicMarshalling() throws Exception {
38+
MessageDigest hasher = MessageDigest.getInstance("SHA-512");
39+
byte[] hash = hasher.digest("TEST".getBytes());
40+
Multihash mhash = new Multihash(Multihash.Type.sha2_512, hash);
41+
42+
Cid cid = new Cid(1, Cid.Codec.Raw, mhash);
43+
byte[] data = cid.toBytes();
44+
45+
Cid cast = Cid.cast(data);
46+
Assert.assertTrue("Invertible serialization", cast.equals(cid));
47+
48+
Cid fromString = Cid.decode(cid.toString());
49+
Assert.assertTrue("Invertible toString", fromString.equals(cid));
50+
}
51+
52+
@Test
53+
public void version0Handling() throws Exception {
54+
String hashString = "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB";
55+
Cid cid = Cid.decode(hashString);
56+
57+
Assert.assertTrue("version 0", cid.version == 0);
58+
59+
Assert.assertTrue("Correct hash", cid.hash.toString().equals(hashString));
60+
61+
Assert.assertTrue("Correct toString", cid.toString().equals(hashString));
62+
}
63+
64+
@Test
65+
public void version0Error() throws Exception {
66+
String invalidString = "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII";
67+
try {
68+
Cid cid = Cid.decode(invalidString);
69+
throw new RuntimeException();
70+
} catch (IllegalStateException e) {}
71+
}
2572
}

0 commit comments

Comments
 (0)