2
2
3
3
import io .ipfs .api .cbor .*;
4
4
import io .ipfs .cid .*;
5
- import io .ipfs .multibase .Multibase ;
6
5
import io .ipfs .multihash .Multihash ;
7
6
import io .ipfs .multiaddr .MultiAddress ;
8
7
import org .junit .*;
9
8
10
9
import java .io .*;
11
- import java .net .URLEncoder ;
12
10
import java .nio .file .*;
13
11
import java .util .*;
14
12
import java .util .function .*;
15
13
import java .util .stream .*;
16
14
15
+ import static org .junit .Assert .assertNotNull ;
17
16
import static org .junit .Assert .assertTrue ;
17
+ import static org .junit .Assert .assertEquals ;
18
+ import static org .junit .Assert .assertArrayEquals ;
18
19
20
+ @ SuppressWarnings ({"rawtypes" , "unused" })
19
21
public class APITest {
20
22
21
23
private final IPFS ipfs = new IPFS (new MultiAddress ("/ip4/127.0.0.1/tcp/5001" ));
@@ -30,14 +32,14 @@ public void dag() throws IOException {
30
32
Cid expected = Cid .decode ("bafyreidbm2zncsc3j25zn7lofgd4woeh6eygdy73thfosuni2rwr3bhcvu" );
31
33
32
34
Multihash result = put .hash ;
33
- Assert . assertTrue ("Correct cid returned" , result . equals ( expected ) );
35
+ assertEquals ("Correct cid returned" , result , expected );
34
36
35
37
byte [] get = ipfs .dag .get (expected );
36
- Assert . assertTrue ("Raw data equal" , original . equals ( new String (get ).trim () ));
38
+ assertEquals ("Raw data equal" , original , new String (get ).trim ());
37
39
Map res = ipfs .dag .resolve ("bafyreidbm2zncsc3j25zn7lofgd4woeh6eygdy73thfosuni2rwr3bhcvu" );
38
- Assert . assertTrue ("not resolved" , res != null );
40
+ assertNotNull ("not resolved" , res );
39
41
res = ipfs .dag .stat (expected );
40
- Assert . assertTrue ("not found" , res != null );
42
+ assertNotNull ("not found" , res );
41
43
}
42
44
43
45
@ Test
@@ -52,10 +54,11 @@ public void dagCbor() throws IOException {
52
54
Cid cid = (Cid ) put .hash ;
53
55
54
56
byte [] get = ipfs .dag .get (cid );
55
- Assert .assertTrue ("Raw data equal" , ((Map )JSONParser .parse (new String (get ))).get ("data" ).equals (value ));
57
+ assertEquals ("Raw data equal" , ((Map ) JSONParser .parse (new String (get ))).get ("data" ),
58
+ value );
56
59
57
60
Cid expected = Cid .decode ("zdpuApemz4XMURSCkBr9W5y974MXkSbeDfLeZmiQTPpvkatFF" );
58
- Assert . assertTrue ("Correct cid returned" , cid . equals ( expected ) );
61
+ assertEquals ("Correct cid returned" , cid , expected );
59
62
}
60
63
61
64
@ Test
@@ -67,14 +70,14 @@ public void keys() throws IOException {
67
70
Object rename = ipfs .key .rename (name , newName );
68
71
List <KeyInfo > rm = ipfs .key .rm (newName );
69
72
List <KeyInfo > remaining = ipfs .key .list ();
70
- Assert . assertTrue ("removed key" , remaining . equals ( existing ) );
73
+ assertEquals ("removed key" , remaining , existing );
71
74
}
72
75
73
76
@ Test
74
77
@ Ignore ("Not reliable" )
75
78
public void log () throws IOException {
76
79
Map lsResult = ipfs .log .ls ();
77
- Assert .assertTrue ("Log ls" , ! lsResult .isEmpty ());
80
+ Assert .assertFalse ("Log ls" , lsResult .isEmpty ());
78
81
Map levelResult = ipfs .log .level ("all" , "info" );
79
82
Assert .assertTrue ("Log level" , ((String )levelResult .get ("Message" )).startsWith ("Changed log level" ));
80
83
}
@@ -89,7 +92,7 @@ public void ipldNode() {
89
92
90
93
IpldNode .CborIpldNode node = new IpldNode .CborIpldNode (cbor );
91
94
List <String > tree = node .tree ("" , -1 );
92
- Assert . assertTrue ("Correct tree" , tree . equals ( Arrays .asList ("/a/b" , "/c" ) ));
95
+ assertEquals ("Correct tree" , tree , Arrays .asList ("/a/b" , "/c" ));
93
96
}
94
97
95
98
@ Test
@@ -166,15 +169,17 @@ public void directoryTest() throws IOException {
166
169
throw new IllegalStateException ("Different contents!" );
167
170
}
168
171
169
- // @Test
172
+ @ Ignore
173
+ @ Test
170
174
public void largeFileTest () throws IOException {
171
175
byte [] largerData = new byte [100 *1024 *1024 ];
172
176
new Random (1 ).nextBytes (largerData );
173
177
NamedStreamable .ByteArrayWrapper largeFile = new NamedStreamable .ByteArrayWrapper ("nontrivial.txt" , largerData );
174
178
fileTest (largeFile );
175
179
}
176
180
177
- // @Test
181
+ @ Ignore
182
+ @ Test
178
183
public void hugeFileStreamTest () throws IOException {
179
184
byte [] hugeData = new byte [1000 *1024 *1024 ];
180
185
new Random (1 ).nextBytes (hugeData );
@@ -243,7 +248,7 @@ public void filesTest() throws IOException {
243
248
Map stat = ipfs .files .stat ( path );
244
249
Map stat2 = ipfs .files .stat ( path , Optional .of ("<cumulsize>" ), true );
245
250
String readContents = new String (ipfs .files .read (path ));
246
- Assert . assertTrue ("Should be equals" , contents . equals ( readContents ) );
251
+ assertEquals ("Should be equals" , contents , readContents );
247
252
res = ipfs .files .rm (path , false , false );
248
253
249
254
String tempFilename = "temp.txt" ;
@@ -285,14 +290,14 @@ public void filesTest() throws IOException {
285
290
@ Test
286
291
public void multibaseTest () throws IOException {
287
292
List <Map > encodings = ipfs .multibase .list (true , false );
288
- Assert .assertTrue ("multibase/list works" , ! encodings .isEmpty ());
293
+ Assert .assertFalse ("multibase/list works" , encodings .isEmpty ());
289
294
String encoded = ipfs .multibase .encode (Optional .empty (), new NamedStreamable .ByteArrayWrapper ("hello" .getBytes ()));
290
- Assert . assertTrue ("multibase/encode works" , encoded . equals ( "uaGVsbG8" ) );
295
+ assertEquals ("multibase/encode works" , "uaGVsbG8" , encoded );
291
296
String decoded = ipfs .multibase .decode (new NamedStreamable .ByteArrayWrapper (encoded .getBytes ()));
292
- Assert . assertTrue ("multibase/decode works" , decoded . equals ( "hello" ) );
297
+ assertEquals ("multibase/decode works" , "hello" , decoded );
293
298
String input = "f68656c6c6f" ;
294
299
String transcode = ipfs .multibase .transcode (Optional .of ("base64url" ), new NamedStreamable .ByteArrayWrapper (input .getBytes ()));
295
- Assert . assertTrue ("multibase/transcode works" , transcode . equals ( encoded ) );
300
+ assertEquals ("multibase/transcode works" , transcode , encoded );
296
301
}
297
302
298
303
@ Test
@@ -460,8 +465,8 @@ public void objectTest() throws IOException {
460
465
MerkleNode _new = ipfs .object ._new (Optional .empty ());
461
466
Multihash pointer = Multihash .fromBase58 ("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB" );
462
467
MerkleNode object = ipfs .object .get (pointer );
463
- List <MerkleNode > newPointer = ipfs .object .put (Arrays . asList (object .toJSONString ().getBytes ()));
464
- List <MerkleNode > newPointer2 = ipfs .object .put ("json" , Arrays . asList (object .toJSONString ().getBytes ()));
468
+ List <MerkleNode > newPointer = ipfs .object .put (Collections . singletonList (object .toJSONString ().getBytes ()));
469
+ List <MerkleNode > newPointer2 = ipfs .object .put ("json" , Collections . singletonList (object .toJSONString ().getBytes ()));
465
470
MerkleNode links = ipfs .object .links (pointer );
466
471
byte [] data = ipfs .object .data (pointer );
467
472
Map stat = ipfs .object .stat (pointer );
@@ -472,7 +477,7 @@ public void blockTest() throws IOException {
472
477
MerkleNode pointer = new MerkleNode ("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB" );
473
478
Map stat = ipfs .block .stat (pointer .hash );
474
479
byte [] object = ipfs .block .get (pointer .hash );
475
- List <MerkleNode > newPointer = ipfs .block .put (Arrays . asList ("Some random data..." .getBytes ()));
480
+ List <MerkleNode > newPointer = ipfs .block .put (Collections . singletonList ("Some random data..." .getBytes ()));
476
481
}
477
482
478
483
@ Test
@@ -493,18 +498,18 @@ public void publish() throws Exception {
493
498
494
499
// Add a DAG node to IPFS
495
500
MerkleNode merkleNode = ipfs .dag .put ("json" , json .getBytes ());
496
- Assert . assertEquals ("expected to be bafyreiafmbgul64c4nyybvgivswmkuhifamc24cdfuj4ij5xtnhpsfelky" , "bafyreiafmbgul64c4nyybvgivswmkuhifamc24cdfuj4ij5xtnhpsfelky" , merkleNode .hash .toString ());
501
+ assertEquals ("expected to be bafyreiafmbgul64c4nyybvgivswmkuhifamc24cdfuj4ij5xtnhpsfelky" , "bafyreiafmbgul64c4nyybvgivswmkuhifamc24cdfuj4ij5xtnhpsfelky" , merkleNode .hash .toString ());
497
502
498
503
// Get a DAG node
499
504
byte [] res = ipfs .dag .get ((Cid ) merkleNode .hash );
500
- Assert . assertEquals ("Should be equals" , JSONParser .parse (json ), JSONParser .parse (new String (res )));
505
+ assertEquals ("Should be equals" , JSONParser .parse (json ), JSONParser .parse (new String (res )));
501
506
502
507
// Publish to IPNS
503
508
Map result = ipfs .name .publish (merkleNode .hash );
504
509
505
510
// Resolve from IPNS
506
511
String resolved = ipfs .name .resolve (Cid .decode ((String ) result .get ("Name" )));
507
- Assert . assertEquals ("Should be equals" , resolved , "/ipfs/" + merkleNode .hash . toString () );
512
+ assertEquals ("Should be equals" , resolved , "/ipfs/" + merkleNode .hash );
508
513
}
509
514
510
515
@ Test
@@ -536,7 +541,7 @@ public void pubsub() throws Exception {
536
541
ipfs .pubsub .pub (topic , data );
537
542
ipfs .pubsub .pub (topic , "G'day" );
538
543
List <Map > results = sub .limit (2 ).collect (Collectors .toList ());
539
- Assert .assertTrue ( ! results .get (0 ). equals ( Collections .emptyMap () ));
544
+ Assert .assertNotEquals ( results .get (0 ), Collections .emptyMap ());
540
545
}
541
546
542
547
private static String toEscapedHex (byte [] in ) throws IOException {
@@ -556,15 +561,15 @@ public void merkleLinkInMap() throws IOException {
556
561
Random r = new Random ();
557
562
CborObject .CborByteArray target = new CborObject .CborByteArray (("g'day IPFS!" ).getBytes ());
558
563
byte [] rawTarget = target .toByteArray ();
559
- MerkleNode targetRes = ipfs .block .put (Arrays . asList (rawTarget ), Optional .of ("cbor" )).get (0 );
564
+ MerkleNode targetRes = ipfs .block .put (Collections . singletonList (rawTarget ), Optional .of ("cbor" )).get (0 );
560
565
561
566
CborObject .CborMerkleLink link = new CborObject .CborMerkleLink (targetRes .hash );
562
567
Map <String , CborObject > m = new TreeMap <>();
563
568
m .put ("alink" , link );
564
569
m .put ("arr" , new CborObject .CborList (Collections .emptyList ()));
565
570
CborObject .CborMap source = CborObject .CborMap .build (m );
566
571
byte [] rawSource = source .toByteArray ();
567
- MerkleNode sourceRes = ipfs .block .put (Arrays . asList (rawSource ), Optional .of ("cbor" )).get (0 );
572
+ MerkleNode sourceRes = ipfs .block .put (Collections . singletonList (rawSource ), Optional .of ("cbor" )).get (0 );
568
573
569
574
CborObject .fromByteArray (rawSource );
570
575
@@ -576,7 +581,7 @@ public void merkleLinkInMap() throws IOException {
576
581
Assert .assertTrue ("refs returns links" , refs .contains (targetRes .hash ));
577
582
578
583
byte [] bytes = ipfs .block .get (targetRes .hash );
579
- Assert . assertTrue ("same contents after GC" , Arrays . equals ( bytes , rawTarget ) );
584
+ assertArrayEquals ("same contents after GC" , bytes , rawTarget );
580
585
// These commands can be used to reproduce this on the command line
581
586
String reproCommand1 = "printf \" " + toEscapedHex (rawTarget ) + "\" | ipfs block put --format=cbor" ;
582
587
String reproCommand2 = "printf \" " + toEscapedHex (rawSource ) + "\" | ipfs block put --format=cbor" ;
@@ -587,34 +592,34 @@ public void merkleLinkInMap() throws IOException {
587
592
public void recursiveRefs () throws IOException {
588
593
CborObject .CborByteArray leaf1 = new CborObject .CborByteArray (("G'day IPFS!" ).getBytes ());
589
594
byte [] rawLeaf1 = leaf1 .toByteArray ();
590
- MerkleNode leaf1Res = ipfs .block .put (Arrays . asList (rawLeaf1 ), Optional .of ("cbor" )).get (0 );
595
+ MerkleNode leaf1Res = ipfs .block .put (Collections . singletonList (rawLeaf1 ), Optional .of ("cbor" )).get (0 );
591
596
592
597
CborObject .CborMerkleLink link = new CborObject .CborMerkleLink (leaf1Res .hash );
593
598
Map <String , CborObject > m = new TreeMap <>();
594
599
m .put ("link1" , link );
595
600
CborObject .CborMap source = CborObject .CborMap .build (m );
596
- MerkleNode sourceRes = ipfs .block .put (Arrays . asList (source .toByteArray ()), Optional .of ("cbor" )).get (0 );
601
+ MerkleNode sourceRes = ipfs .block .put (Collections . singletonList (source .toByteArray ()), Optional .of ("cbor" )).get (0 );
597
602
598
603
CborObject .CborByteArray leaf2 = new CborObject .CborByteArray (("G'day again, IPFS!" ).getBytes ());
599
604
byte [] rawLeaf2 = leaf2 .toByteArray ();
600
- MerkleNode leaf2Res = ipfs .block .put (Arrays . asList (rawLeaf2 ), Optional .of ("cbor" )).get (0 );
605
+ MerkleNode leaf2Res = ipfs .block .put (Collections . singletonList (rawLeaf2 ), Optional .of ("cbor" )).get (0 );
601
606
602
607
Map <String , CborObject > m2 = new TreeMap <>();
603
608
m2 .put ("link1" , new CborObject .CborMerkleLink (sourceRes .hash ));
604
609
m2 .put ("link2" , new CborObject .CborMerkleLink (leaf2Res .hash ));
605
610
CborObject .CborMap source2 = CborObject .CborMap .build (m2 );
606
- MerkleNode rootRes = ipfs .block .put (Arrays . asList (source2 .toByteArray ()), Optional .of ("cbor" )).get (0 );
611
+ MerkleNode rootRes = ipfs .block .put (Collections . singletonList (source2 .toByteArray ()), Optional .of ("cbor" )).get (0 );
607
612
608
613
List <Multihash > refs = ipfs .refs (rootRes .hash , false );
609
614
boolean correct = refs .contains (sourceRes .hash ) && refs .contains (leaf2Res .hash ) && refs .size () == 2 ;
610
615
Assert .assertTrue ("refs returns links" , correct );
611
616
612
617
List <Multihash > refsRecurse = ipfs .refs (rootRes .hash , true );
613
- boolean correctRecurse = refs .contains (sourceRes .hash )
614
- && refs .contains (leaf1Res .hash )
615
- && refs .contains (leaf2Res .hash )
616
- && refs .size () == 3 ;
617
- Assert .assertTrue ("refs returns links" , correct );
618
+ boolean correctRecurse = refsRecurse .contains (sourceRes .hash )
619
+ && refsRecurse .contains (leaf1Res .hash )
620
+ && refsRecurse .contains (leaf2Res .hash )
621
+ && refsRecurse .size () == 3 ;
622
+ Assert .assertTrue ("refs returns links" , correctRecurse );
618
623
}
619
624
620
625
/**
@@ -625,23 +630,23 @@ public void rootMerkleLink() throws IOException {
625
630
Random r = new Random ();
626
631
CborObject .CborByteArray target = new CborObject .CborByteArray (("g'day IPFS!" + r .nextInt ()).getBytes ());
627
632
byte [] rawTarget = target .toByteArray ();
628
- MerkleNode block1 = ipfs .block .put (Arrays . asList (rawTarget ), Optional .of ("cbor" )).get (0 );
633
+ MerkleNode block1 = ipfs .block .put (Collections . singletonList (rawTarget ), Optional .of ("cbor" )).get (0 );
629
634
Multihash block1Hash = block1 .hash ;
630
635
byte [] retrievedObj1 = ipfs .block .get (block1Hash );
631
- Assert . assertTrue ("get inverse of put" , Arrays . equals ( retrievedObj1 , rawTarget ) );
636
+ assertArrayEquals ("get inverse of put" , retrievedObj1 , rawTarget );
632
637
633
638
CborObject .CborMerkleLink cbor2 = new CborObject .CborMerkleLink (block1 .hash );
634
639
byte [] obj2 = cbor2 .toByteArray ();
635
- MerkleNode block2 = ipfs .block .put (Arrays . asList (obj2 ), Optional .of ("cbor" )).get (0 );
640
+ MerkleNode block2 = ipfs .block .put (Collections . singletonList (obj2 ), Optional .of ("cbor" )).get (0 );
636
641
byte [] retrievedObj2 = ipfs .block .get (block2 .hash );
637
- Assert . assertTrue ("get inverse of put" , Arrays . equals ( retrievedObj2 , obj2 ) );
642
+ assertArrayEquals ("get inverse of put" , retrievedObj2 , obj2 );
638
643
639
644
List <Multihash > add = ipfs .pin .add (block2 .hash );
640
645
ipfs .repo .gc ();
641
646
ipfs .repo .gc ();
642
647
643
648
byte [] bytes = ipfs .block .get (block1 .hash );
644
- Assert . assertTrue ("same contents after GC" , Arrays . equals ( bytes , rawTarget ) );
649
+ assertArrayEquals ("same contents after GC" , bytes , rawTarget );
645
650
// These commands can be used to reproduce this on the command line
646
651
String reproCommand1 = "printf \" " + toEscapedHex (rawTarget ) + "\" | ipfs block put --format=cbor" ;
647
652
String reproCommand2 = "printf \" " + toEscapedHex (obj2 ) + "\" | ipfs block put --format=cbor" ;
@@ -655,9 +660,9 @@ public void rootMerkleLink() throws IOException {
655
660
public void rootNull () throws IOException {
656
661
CborObject .CborNull cbor = new CborObject .CborNull ();
657
662
byte [] obj = cbor .toByteArray ();
658
- MerkleNode block = ipfs .block .put (Arrays . asList (obj ), Optional .of ("cbor" )).get (0 );
663
+ MerkleNode block = ipfs .block .put (Collections . singletonList (obj ), Optional .of ("cbor" )).get (0 );
659
664
byte [] retrievedObj = ipfs .block .get (block .hash );
660
- Assert . assertTrue ("get inverse of put" , Arrays . equals ( retrievedObj , obj ) );
665
+ assertArrayEquals ("get inverse of put" , retrievedObj , obj );
661
666
662
667
List <Multihash > add = ipfs .pin .add (block .hash );
663
668
ipfs .repo .gc ();
@@ -676,19 +681,19 @@ public void merkleLinkInList() throws IOException {
676
681
Random r = new Random ();
677
682
CborObject .CborByteArray target = new CborObject .CborByteArray (("g'day IPFS!" + r .nextInt ()).getBytes ());
678
683
byte [] rawTarget = target .toByteArray ();
679
- MerkleNode targetRes = ipfs .block .put (Arrays . asList (rawTarget ), Optional .of ("cbor" )).get (0 );
684
+ MerkleNode targetRes = ipfs .block .put (Collections . singletonList (rawTarget ), Optional .of ("cbor" )).get (0 );
680
685
681
686
CborObject .CborMerkleLink link = new CborObject .CborMerkleLink (targetRes .hash );
682
- CborObject .CborList source = new CborObject .CborList (Arrays . asList (link ));
687
+ CborObject .CborList source = new CborObject .CborList (Collections . singletonList (link ));
683
688
byte [] rawSource = source .toByteArray ();
684
- MerkleNode sourceRes = ipfs .block .put (Arrays . asList (rawSource ), Optional .of ("cbor" )).get (0 );
689
+ MerkleNode sourceRes = ipfs .block .put (Collections . singletonList (rawSource ), Optional .of ("cbor" )).get (0 );
685
690
686
691
List <Multihash > add = ipfs .pin .add (sourceRes .hash );
687
692
ipfs .repo .gc ();
688
693
ipfs .repo .gc ();
689
694
690
695
byte [] bytes = ipfs .block .get (targetRes .hash );
691
- Assert . assertTrue ("same contents after GC" , Arrays . equals ( bytes , rawTarget ) );
696
+ assertArrayEquals ("same contents after GC" , bytes , rawTarget );
692
697
// These commands can be used to reproduce this on the command line
693
698
String reproCommand1 = "printf \" " + toEscapedHex (rawTarget ) + "\" | ipfs block put --format=cbor" ;
694
699
String reproCommand2 = "printf \" " + toEscapedHex (rawSource ) + "\" | ipfs block put --format=cbor" ;
@@ -819,12 +824,12 @@ public void swarmTestFilters() throws IOException {
819
824
Map rm = ipfs .swarm .rmFilter (multiAddrFilter );
820
825
Map filters = ipfs .swarm .filters ();
821
826
List <String > filtersList = (List <String >)filters .get ("Strings" );
822
- Assert .assertTrue ("Filters empty" , filtersList == null );
827
+ Assert .assertNull ("Filters empty" , filtersList );
823
828
824
829
Map added = ipfs .swarm .addFilter (multiAddrFilter );
825
830
filters = ipfs .swarm .filters ();
826
831
filtersList = (List <String >)filters .get ("Strings" );
827
- Assert .assertTrue ("Filters NOT empty" , ! filtersList .isEmpty ());
832
+ Assert .assertFalse ("Filters NOT empty" , filtersList .isEmpty ());
828
833
rm = ipfs .swarm .rmFilter (multiAddrFilter );
829
834
}
830
835
@@ -838,7 +843,7 @@ public void swarmTestPeering() throws IOException {
838
843
Map addPeering = ipfs .swarm .addPeering (ma );
839
844
Map lsPeering = ipfs .swarm .lsPeering ();
840
845
List <String > peeringList = (List <String >)lsPeering .get ("Peers" );
841
- Assert .assertTrue ("Filters not empty" , ! peeringList .isEmpty ());
846
+ Assert .assertFalse ("Filters not empty" , peeringList .isEmpty ());
842
847
Map rmPeering = ipfs .swarm .rmPeering (hash );
843
848
lsPeering = ipfs .swarm .lsPeering ();
844
849
peeringList = (List <String >)lsPeering .get ("Peers" );
@@ -925,9 +930,9 @@ public void addArgsTest() {
925
930
.setCidVersion (1 )
926
931
.build ();
927
932
String res = args .toString ();
928
- Assert . assertTrue ("args toString() format" , res . equals ( "[cid-version = 1, inline = true]" ) );
933
+ assertEquals ("args toString() format" , "[cid-version = 1, inline = true]" , res );
929
934
String queryStr = args .toQueryString ();
930
- Assert . assertTrue ("args toQueryString() format" , queryStr . equals ( "inline=true&cid-version=1" ) );
935
+ assertEquals ("args toQueryString() format" , "inline=true&cid-version=1" , queryStr );
931
936
}
932
937
933
938
// this api is disabled until deployment over IPFS is enabled
0 commit comments