@@ -318,15 +318,51 @@ private static String toEscapedHex(byte[] in) throws IOException {
318318 return res .toString ();
319319 }
320320
321+ /**
322+ * Test that merkle links in values of a cbor map are followed during recursive pins
323+ */
321324 @ org .junit .Test
322- public void cborBlockTest () {
325+ public void merkleLinkInMap () {
323326 try {
324- CborObject .CborByteArray cbor1 = new CborObject .CborByteArray ("g'day IPFS!" .getBytes ());
325- byte [] obj1 = cbor1 .toByteArray ();
326- MerkleNode block1 = ipfs .block .put (Arrays .asList (obj1 ), Optional .of ("cbor" )).get (0 );
327+ Random r = new Random ();
328+ CborObject .CborByteArray target = new CborObject .CborByteArray (("g'day IPFS!" + r .nextInt ()).getBytes ());
329+ byte [] rawTarget = target .toByteArray ();
330+ MerkleNode targetRes = ipfs .block .put (Arrays .asList (rawTarget ), Optional .of ("cbor" )).get (0 );
331+
332+ 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 )));
335+ byte [] rawSource = source .toByteArray ();
336+ MerkleNode sourceRes = ipfs .block .put (Arrays .asList (rawSource ), Optional .of ("cbor" )).get (0 );
337+
338+ List <Multihash > add = ipfs .pin .add (sourceRes .hash );
339+ ipfs .repo .gc ();
340+ ipfs .repo .gc ();
341+
342+ byte [] bytes = ipfs .block .get (targetRes .hash );
343+ Assert .assertTrue ("same contents after GC" , Arrays .equals (bytes , rawTarget ));
344+ // These commands can be used to reproduce this on the command line
345+ String reproCommand1 = "printf \" " + toEscapedHex (rawTarget ) + "\" | ipfs block put --format=cbor" ;
346+ String reproCommand2 = "printf \" " + toEscapedHex (rawSource ) + "\" | ipfs block put --format=cbor" ;
347+ System .out .println ();
348+ } catch (IOException e ) {
349+ throw new RuntimeException (e );
350+ }
351+ }
352+
353+ /**
354+ * Test that merkle links as a root object are followed during recursive pins
355+ */
356+ @ org .junit .Test
357+ public void rootMerkleLink () {
358+ try {
359+ Random r = new Random ();
360+ CborObject .CborByteArray target = new CborObject .CborByteArray (("g'day IPFS!" + r .nextInt ()).getBytes ());
361+ byte [] rawTarget = target .toByteArray ();
362+ MerkleNode block1 = ipfs .block .put (Arrays .asList (rawTarget ), Optional .of ("cbor" )).get (0 );
327363 Multihash block1Hash = block1 .hash ;
328364 byte [] retrievedObj1 = ipfs .block .get (block1Hash );
329- Assert .assertTrue ("get inverse of put" , Arrays .equals (retrievedObj1 , obj1 ));
365+ Assert .assertTrue ("get inverse of put" , Arrays .equals (retrievedObj1 , rawTarget ));
330366
331367 CborObject .CborMerkleLink cbor2 = new CborObject .CborMerkleLink (block1 .hash );
332368 byte [] obj2 = cbor2 .toByteArray ();
@@ -336,17 +372,50 @@ public void cborBlockTest() {
336372
337373 List <Multihash > add = ipfs .pin .add (block2 .hash );
338374 ipfs .repo .gc ();
375+ ipfs .repo .gc ();
339376
340377 byte [] bytes = ipfs .block .get (block1 .hash );
378+ Assert .assertTrue ("same contents after GC" , Arrays .equals (bytes , rawTarget ));
341379 // These commands can be used to reproduce this on the command line
342- String reproCommand1 = "printf \" " + toEscapedHex (obj1 ) + "\" | ipfs block put --format=cbor" ;
380+ String reproCommand1 = "printf \" " + toEscapedHex (rawTarget ) + "\" | ipfs block put --format=cbor" ;
343381 String reproCommand2 = "printf \" " + toEscapedHex (obj2 ) + "\" | ipfs block put --format=cbor" ;
344382 System .out .println ();
345383 } catch (IOException e ) {
346384 throw new RuntimeException (e );
347385 }
348386 }
349387
388+ /**
389+ * Test that merkle links in a cbor list are followed during recursive pins
390+ */
391+ @ org .junit .Test
392+ public void merkleLinkInList () {
393+ try {
394+ Random r = new Random ();
395+ CborObject .CborByteArray target = new CborObject .CborByteArray (("g'day IPFS!" + r .nextInt ()).getBytes ());
396+ byte [] rawTarget = target .toByteArray ();
397+ MerkleNode targetRes = ipfs .block .put (Arrays .asList (rawTarget ), Optional .of ("cbor" )).get (0 );
398+
399+ CborObject .CborMerkleLink link = new CborObject .CborMerkleLink (targetRes .hash );
400+ CborObject .CborList source = new CborObject .CborList (Arrays .asList (link ));
401+ byte [] rawSource = source .toByteArray ();
402+ MerkleNode sourceRes = ipfs .block .put (Arrays .asList (rawSource ), Optional .of ("cbor" )).get (0 );
403+
404+ List <Multihash > add = ipfs .pin .add (sourceRes .hash );
405+ ipfs .repo .gc ();
406+ ipfs .repo .gc ();
407+
408+ byte [] bytes = ipfs .block .get (targetRes .hash );
409+ Assert .assertTrue ("same contents after GC" , Arrays .equals (bytes , rawTarget ));
410+ // These commands can be used to reproduce this on the command line
411+ String reproCommand1 = "printf \" " + toEscapedHex (rawTarget ) + "\" | ipfs block put --format=cbor" ;
412+ String reproCommand2 = "printf \" " + toEscapedHex (rawSource ) + "\" | ipfs block put --format=cbor" ;
413+ System .out .println ();
414+ } catch (IOException e ) {
415+ throw new RuntimeException (e );
416+ }
417+ }
418+
350419 @ org .junit .Test
351420 public void fileContentsTest () {
352421 try {
0 commit comments