Skip to content

Commit 0e501fd

Browse files
committed
Test cbor merkle link invariants
1 parent 058e25b commit 0e501fd

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<attribute name="Class-Path" value="${manifest_cp}"/>
4141
<attribute name="Implementation-Vendor" value="io.ipfs"/>
4242
<attribute name="Implementation-Title" value="api"/>
43-
<attribute name="Implementation-Version" value="1.0.0"/>
43+
<attribute name="Implementation-Version" value="1.1.0"/>
4444
</manifest>
4545
</jar>
4646
</target>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>io.ipfs</groupId>
66
<artifactId>api</artifactId>
7-
<version>v1.0.0</version>
7+
<version>v1.1.0</version>
88
<packaging>jar</packaging>
99

1010
<name>java-ipfs-api</name>

src/test/java/io/ipfs/api/APITest.java

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)