Skip to content

Commit b67fe7d

Browse files
committed
Fix UTs
1 parent 3b77fca commit b67fe7d

3 files changed

Lines changed: 45 additions & 22 deletions

File tree

src/main/java/io/ipfs/api/IPFS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ public List<MultiAddress> add() throws IOException {
832832
}
833833

834834
public List<MultiAddress> list() throws IOException {
835-
return ((List<String>)retrieveMap("bootstrap/list").get("Peers"))
835+
return ((List<String>)retrieveMap("bootstrap/list?expand-auto=true").get("Peers"))
836836
.stream().map(x -> new MultiAddress(x)).collect(Collectors.toList());
837837
}
838838

src/main/java/io/ipfs/api/Peer.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ public static Peer fromJSON(Object json) {
2727
throw new IllegalStateException("Incorrect json for Peer: " + JSONParser.toString(json));
2828
Map m = (Map) json;
2929
Function<String, String> val = key -> (String) m.get(key);
30-
long latency = val.apply("Latency").length() > 0 ? Long.parseLong(val.apply("Latency")) : -1;
31-
return new Peer(new MultiAddress(val.apply("Addr")), Cid.decode(val.apply("Peer")), latency, val.apply("Muxer"), val.apply("Streams"));
30+
String peerId = val.apply("Peer");
31+
Multihash peer; // Multihash bug? Throws IAEx Base1 not supported
32+
try {
33+
peer = Multihash.fromBase58(peerId);
34+
} catch (Exception e) {
35+
peer = Multihash.decode(peerId);
36+
}
37+
long latency = m.containsKey("Latency") ? Long.parseLong(val.apply("Latency")) : -1;
38+
return new Peer(new MultiAddress(val.apply("Addr")), peer, latency, val.apply("Muxer"), val.apply("Streams"));
3239
}
3340

3441
@Override

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

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.*;
88

99
import java.io.*;
10+
import java.nio.charset.StandardCharsets;
1011
import java.nio.file.*;
1112
import java.util.*;
1213
import java.util.function.*;
@@ -396,31 +397,40 @@ public void rawLeafNodePinUpdate() throws IOException {
396397

397398
@Test
398399
public void indirectPinTest() throws IOException {
399-
Multihash EMPTY = ipfs.object._new(Optional.empty()).hash;
400-
io.ipfs.api.MerkleNode data = ipfs.object.patch(EMPTY, "set-data", Optional.of("childdata".getBytes()), Optional.empty(), Optional.empty());
401-
Multihash child = data.hash;
400+
String path = "/test/indirectPinTest-" + UUID.randomUUID();
401+
ipfs.files.write(path + "/content", new NamedStreamable.ByteArrayWrapper("something".getBytes(StandardCharsets.UTF_8)), true, true);
402+
Multihash content = Multihash.decode((String) ipfs.files.stat(path + "/content").get("Hash"));
402403

403-
io.ipfs.api.MerkleNode tmp1 = ipfs.object.patch(EMPTY, "set-data", Optional.of("parent1_data".getBytes()), Optional.empty(), Optional.empty());
404-
Multihash parent1 = ipfs.object.patch(tmp1.hash, "add-link", Optional.empty(), Optional.of(child.toString()), Optional.of(child)).hash;
405-
ipfs.pin.add(parent1);
404+
// adding one more extra entry to parent1 to keep its hash different from parent2
405+
ipfs.files.mkdir(path + "/parent1", true);
406+
ipfs.files.write(path + "/parent1/content1", new NamedStreamable.ByteArrayWrapper("somethingelse".getBytes(StandardCharsets.UTF_8)), true, true);
407+
ipfs.files.cp("/ipfs/" + content, path + "/parent1/content2", true);
408+
409+
ipfs.files.mkdir(path + "/parent2", true);
410+
ipfs.files.cp("/ipfs/" + content, path + "/parent2/content", true);
406411

407-
io.ipfs.api.MerkleNode tmp2 = ipfs.object.patch(EMPTY, "set-data", Optional.of("parent2_data".getBytes()), Optional.empty(), Optional.empty());
408-
Multihash parent2 = ipfs.object.patch(tmp2.hash, "add-link", Optional.empty(), Optional.of(child.toString()), Optional.of(child)).hash;
412+
Multihash parent1 = Multihash.decode((String) ipfs.files.stat(path + "/parent1").get("Hash"));
413+
Multihash parent2 = Multihash.decode((String) ipfs.files.stat(path + "/parent2").get("Hash"));
414+
415+
ipfs.pin.add(parent1);
409416
ipfs.pin.add(parent2);
410417
ipfs.pin.rm(parent1, true);
411418

412419
Map<Multihash, Object> ls = ipfs.pin.ls(IPFS.PinType.all);
413-
boolean childPresent = ls.containsKey(child);
420+
boolean childPresent = ls.containsKey(content);
414421
if (!childPresent)
415-
throw new IllegalStateException("Child not present!");
422+
throw new IllegalStateException("Child not present: " + ls);
416423

417424
ipfs.repo.gc();
418425
Map<Multihash, Object> ls2 = ipfs.pin.ls(IPFS.PinType.all);
419-
boolean childPresentAfterGC = ls2.containsKey(child);
426+
boolean childPresentAfterGC = ls2.containsKey(content);
420427
if (!childPresentAfterGC)
421-
throw new IllegalStateException("Child not present!");
422-
}
428+
throw new IllegalStateException("Child not present:" + ls);
423429

430+
ipfs.files.rm(path, true, true);
431+
}
432+
433+
@Ignore("RPC API removed")
424434
@Test
425435
public void objectPatch() throws IOException {
426436
MerkleNode obj = ipfs.object._new(Optional.empty());
@@ -462,6 +472,7 @@ public void refsTest() throws IOException {
462472
}
463473
}
464474

475+
@Ignore("RPC API removed")
465476
@Test
466477
public void objectTest() throws IOException {
467478
MerkleNode _new = ipfs.object._new(Optional.empty());
@@ -489,10 +500,8 @@ public void bulkBlockTest() throws IOException {
489500
List<MerkleNode> bulkPut = ipfs.block.put(Arrays.asList(raw, raw, raw, raw, raw), Optional.of("cbor"));
490501
List<Multihash> hashes = bulkPut.stream().map(m -> m.hash).collect(Collectors.toList());
491502
byte[] result = ipfs.block.get(hashes.get(0));
492-
System.out.println();
493503
}
494504

495-
// @Ignore // Ignored because ipfs frequently times out internally in the publish call
496505
@Test
497506
public void publish() throws Exception {
498507
// JSON document
@@ -514,6 +523,15 @@ public void publish() throws Exception {
514523
assertEquals("Should be equals", resolved, "/ipfs/" + merkleNode.hash);
515524
}
516525

526+
@Test
527+
public void resolveName() throws Exception {
528+
// Resolve from DNSLinked domain
529+
String resolved = ipfs.name.resolve("docs.ipfs.tech");
530+
assertNotNull(resolved);
531+
assertTrue(resolved.startsWith("/ipfs/"));
532+
assertTrue(resolved.length() > 20); // this may change (content and encoding as well)
533+
}
534+
517535
@Test
518536
public void pubsubSynchronous() {
519537
String topic = "topic" + System.nanoTime();
@@ -587,7 +605,6 @@ public void merkleLinkInMap() throws IOException {
587605
// These commands can be used to reproduce this on the command line
588606
String reproCommand1 = "printf \"" + toEscapedHex(rawTarget) + "\" | ipfs block put --format=cbor";
589607
String reproCommand2 = "printf \"" + toEscapedHex(rawSource) + "\" | ipfs block put --format=cbor";
590-
System.out.println();
591608
}
592609

593610
@Test
@@ -652,7 +669,6 @@ public void rootMerkleLink() throws IOException {
652669
// These commands can be used to reproduce this on the command line
653670
String reproCommand1 = "printf \"" + toEscapedHex(rawTarget) + "\" | ipfs block put --format=cbor";
654671
String reproCommand2 = "printf \"" + toEscapedHex(obj2) + "\" | ipfs block put --format=cbor";
655-
System.out.println();
656672
}
657673

658674
/**
@@ -672,7 +688,6 @@ public void rootNull() throws IOException {
672688

673689
// These commands can be used to reproduce this on the command line
674690
String reproCommand1 = "printf \"" + toEscapedHex(obj) + "\" | ipfs block put --format=cbor";
675-
System.out.println();
676691
}
677692

678693
/**
@@ -754,7 +769,6 @@ public void dhtTest() throws IOException {
754769
@Test
755770
public void localId() throws Exception {
756771
Map id = ipfs.id();
757-
System.out.println();
758772
}
759773

760774
@Test
@@ -856,6 +870,8 @@ public void bitswapTest() throws IOException {
856870
Map stat = ipfs.bitswap.stat();
857871
Map stat2 = ipfs.bitswap.stat(true);
858872
}
873+
874+
@Ignore("AutoConf.Enabled=true is default; prevents bootstrap removal")
859875
@Test
860876
public void bootstrapTest() throws IOException {
861877
List<MultiAddress> bootstrap = ipfs.bootstrap.list();

0 commit comments

Comments
 (0)