11package io .ipfs .cid ;
22
3+ import io .ipfs .multihash .*;
34import org .junit .*;
45import io .ipfs .multibase .*;
56
67import java .io .*;
8+ import java .security .*;
79import java .util .*;
810
911public 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