@@ -309,6 +309,20 @@ public void blockTest() {
309309 }
310310 }
311311
312+ @ org .junit .Test
313+ public void bulkBlockTest () {
314+ try {
315+ CborObject cbor = new CborObject .CborString ("G'day IPFS!" );
316+ byte [] raw = cbor .toByteArray ();
317+ List <MerkleNode > bulkPut = ipfs .block .put (Arrays .asList (raw , raw , raw , raw , raw ), Optional .of ("cbor" ));
318+ List <Multihash > hashes = bulkPut .stream ().map (m -> m .hash ).collect (Collectors .toList ());
319+ byte [] result = ipfs .block .get (hashes .get (0 ));
320+ System .out .println ();
321+ } catch (IOException e ) {
322+ throw new RuntimeException (e );
323+ }
324+ }
325+
312326 private static String toEscapedHex (byte [] in ) throws IOException {
313327 StringBuilder res = new StringBuilder ();
314328 for (byte b : in ) {
@@ -325,20 +339,27 @@ private static String toEscapedHex(byte[] in) throws IOException {
325339 public void merkleLinkInMap () {
326340 try {
327341 Random r = new Random ();
328- CborObject .CborByteArray target = new CborObject .CborByteArray (("g'day IPFS!" + r . nextInt () ).getBytes ());
342+ CborObject .CborByteArray target = new CborObject .CborByteArray (("g'day IPFS!" ).getBytes ());
329343 byte [] rawTarget = target .toByteArray ();
330344 MerkleNode targetRes = ipfs .block .put (Arrays .asList (rawTarget ), Optional .of ("cbor" )).get (0 );
331345
332346 CborObject .CborMerkleLink link = new CborObject .CborMerkleLink (targetRes .hash );
333- CborObject .CborMap source = CborObject .CborMap .build (Stream .of (link )
334- .collect (Collectors .toMap (l -> l .target .toString (), l -> l )));
347+ Map <String , CborObject > m = new TreeMap <>();
348+ m .put ("alink" , link );
349+ m .put ("arr" , new CborObject .CborList (Collections .emptyList ()));
350+ CborObject .CborMap source = CborObject .CborMap .build (m );
335351 byte [] rawSource = source .toByteArray ();
336352 MerkleNode sourceRes = ipfs .block .put (Arrays .asList (rawSource ), Optional .of ("cbor" )).get (0 );
337353
354+ CborObject .fromByteArray (rawSource );
355+
338356 List <Multihash > add = ipfs .pin .add (sourceRes .hash );
339357 ipfs .repo .gc ();
340358 ipfs .repo .gc ();
341359
360+ List <Multihash > refs = ipfs .refs (sourceRes .hash , true );
361+ Assert .assertTrue ("refs returns links" , refs .contains (targetRes .hash ));
362+
342363 byte [] bytes = ipfs .block .get (targetRes .hash );
343364 Assert .assertTrue ("same contents after GC" , Arrays .equals (bytes , rawTarget ));
344365 // These commands can be used to reproduce this on the command line
@@ -350,6 +371,44 @@ public void merkleLinkInMap() {
350371 }
351372 }
352373
374+ @ org .junit .Test
375+ public void recursiveRefs () {
376+ try {
377+ CborObject .CborByteArray leaf1 = new CborObject .CborByteArray (("G'day IPFS!" ).getBytes ());
378+ byte [] rawLeaf1 = leaf1 .toByteArray ();
379+ MerkleNode leaf1Res = ipfs .block .put (Arrays .asList (rawLeaf1 ), Optional .of ("cbor" )).get (0 );
380+
381+ CborObject .CborMerkleLink link = new CborObject .CborMerkleLink (leaf1Res .hash );
382+ Map <String , CborObject > m = new TreeMap <>();
383+ m .put ("link1" , link );
384+ CborObject .CborMap source = CborObject .CborMap .build (m );
385+ MerkleNode sourceRes = ipfs .block .put (Arrays .asList (source .toByteArray ()), Optional .of ("cbor" )).get (0 );
386+
387+ CborObject .CborByteArray leaf2 = new CborObject .CborByteArray (("G'day again, IPFS!" ).getBytes ());
388+ byte [] rawLeaf2 = leaf2 .toByteArray ();
389+ MerkleNode leaf2Res = ipfs .block .put (Arrays .asList (rawLeaf2 ), Optional .of ("cbor" )).get (0 );
390+
391+ Map <String , CborObject > m2 = new TreeMap <>();
392+ m2 .put ("link1" , new CborObject .CborMerkleLink (sourceRes .hash ));
393+ m2 .put ("link2" , new CborObject .CborMerkleLink (leaf2Res .hash ));
394+ CborObject .CborMap source2 = CborObject .CborMap .build (m2 );
395+ MerkleNode rootRes = ipfs .block .put (Arrays .asList (source2 .toByteArray ()), Optional .of ("cbor" )).get (0 );
396+
397+ List <Multihash > refs = ipfs .refs (rootRes .hash , false );
398+ boolean correct = refs .contains (sourceRes .hash ) && refs .contains (leaf2Res .hash ) && refs .size () == 2 ;
399+ Assert .assertTrue ("refs returns links" , correct );
400+
401+ List <Multihash > refsRecurse = ipfs .refs (rootRes .hash , true );
402+ boolean correctRecurse = refs .contains (sourceRes .hash )
403+ && refs .contains (leaf1Res .hash )
404+ && refs .contains (leaf2Res .hash )
405+ && refs .size () == 3 ;
406+ Assert .assertTrue ("refs returns links" , correct );
407+ } catch (IOException e ) {
408+ throw new RuntimeException (e );
409+ }
410+ }
411+
353412 /**
354413 * Test that merkle links as a root object are followed during recursive pins
355414 */
@@ -385,6 +444,30 @@ public void rootMerkleLink() {
385444 }
386445 }
387446
447+ /**
448+ * Test that merkle links as a root object are followed during recursive pins
449+ */
450+ @ org .junit .Test
451+ public void rootNull () {
452+ try {
453+ CborObject .CborNull cbor = new CborObject .CborNull ();
454+ byte [] obj = cbor .toByteArray ();
455+ MerkleNode block = ipfs .block .put (Arrays .asList (obj ), Optional .of ("cbor" )).get (0 );
456+ byte [] retrievedObj = ipfs .block .get (block .hash );
457+ Assert .assertTrue ("get inverse of put" , Arrays .equals (retrievedObj , obj ));
458+
459+ List <Multihash > add = ipfs .pin .add (block .hash );
460+ ipfs .repo .gc ();
461+ ipfs .repo .gc ();
462+
463+ // These commands can be used to reproduce this on the command line
464+ String reproCommand1 = "printf \" " + toEscapedHex (obj ) + "\" | ipfs block put --format=cbor" ;
465+ System .out .println ();
466+ } catch (IOException e ) {
467+ throw new RuntimeException (e );
468+ }
469+ }
470+
388471 /**
389472 * Test that merkle links in a cbor list are followed during recursive pins
390473 */
0 commit comments