1 package net.pterodactylus.fcp.quelaton;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.containsInAnyOrder;
5 import static org.hamcrest.Matchers.hasSize;
6 import static org.hamcrest.Matchers.is;
7 import static org.hamcrest.Matchers.notNullValue;
9 import java.io.ByteArrayInputStream;
11 import java.io.IOException;
13 import java.nio.charset.StandardCharsets;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.Future;
21 import java.util.stream.Collectors;
23 import net.pterodactylus.fcp.ARK;
24 import net.pterodactylus.fcp.DSAGroup;
25 import net.pterodactylus.fcp.FcpKeyPair;
26 import net.pterodactylus.fcp.Key;
27 import net.pterodactylus.fcp.NodeData;
28 import net.pterodactylus.fcp.NodeRef;
29 import net.pterodactylus.fcp.Peer;
30 import net.pterodactylus.fcp.Priority;
31 import net.pterodactylus.fcp.fake.FakeTcpServer;
32 import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
34 import com.google.common.io.ByteStreams;
35 import com.google.common.io.Files;
36 import org.hamcrest.Description;
37 import org.hamcrest.Matcher;
38 import org.hamcrest.TypeSafeDiagnosingMatcher;
39 import org.junit.After;
40 import org.junit.Assert;
41 import org.junit.Test;
44 * Unit test for {@link DefaultFcpClient}.
46 * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
48 public class DefaultFcpClientTest {
50 private static final String INSERT_URI =
51 "SSK@RVCHbJdkkyTCeNN9AYukEg76eyqmiosSaNKgE3U9zUw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQECAAE/";
52 private static final String REQUEST_URI =
53 "SSK@wtbgd2loNcJCXvtQVOftl2tuWBomDQHfqS6ytpPRhfw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQACAAE/";
55 private static int threadCounter = 0;
56 private final FakeTcpServer fcpServer;
57 private final DefaultFcpClient fcpClient;
59 public DefaultFcpClientTest() throws IOException {
60 ExecutorService threadPool =
61 Executors.newCachedThreadPool(r -> new Thread(r, "Test-Thread-" + threadCounter++));
62 fcpServer = new FakeTcpServer(threadPool);
63 fcpClient = new DefaultFcpClient(threadPool, "localhost", fcpServer.getPort(), () -> "Test");
67 public void tearDown() throws IOException {
71 @Test(expected = ExecutionException.class)
72 public void defaultFcpClientThrowsExceptionIfItCanNotConnect()
73 throws IOException, ExecutionException, InterruptedException {
74 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
75 fcpServer.connect().get();
76 fcpServer.collectUntil(is("EndMessage"));
78 "CloseConnectionDuplicateClientName",
84 @Test(expected = ExecutionException.class)
85 public void defaultFcpClientThrowsExceptionIfConnectionIsClosed()
86 throws IOException, ExecutionException, InterruptedException {
87 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
88 fcpServer.connect().get();
89 fcpServer.collectUntil(is("EndMessage"));
95 public void defaultFcpClientCanGenerateKeypair() throws ExecutionException, InterruptedException, IOException {
96 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
98 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
99 String identifier = extractIdentifier(lines);
100 fcpServer.writeLine("SSKKeypair",
101 "InsertURI=" + INSERT_URI + "",
102 "RequestURI=" + REQUEST_URI + "",
103 "Identifier=" + identifier,
105 FcpKeyPair keyPair = keyPairFuture.get();
106 assertThat(keyPair.getPublicKey(), is(REQUEST_URI));
107 assertThat(keyPair.getPrivateKey(), is(INSERT_URI));
110 private void connectNode() throws InterruptedException, ExecutionException, IOException {
111 fcpServer.connect().get();
112 fcpServer.collectUntil(is("EndMessage"));
113 fcpServer.writeLine("NodeHello",
114 "CompressionCodecs=4 - GZIP(0), BZIP2(1), LZMA(2), LZMA_NEW(3)",
115 "Revision=build01466",
117 "Version=Fred,0.7,1.0,1466",
119 "ConnectionIdentifier=14318898267048452a81b36e7f13a3f0",
123 "NodeLanguage=ENGLISH",
130 public void clientGetCanDownloadData() throws InterruptedException, ExecutionException, IOException {
131 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
133 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
134 assertThat(lines, matchesFcpMessage("ClientGet", "ReturnType=direct", "URI=KSK@foo.txt"));
135 String identifier = extractIdentifier(lines);
138 "Identifier=" + identifier,
140 "StartupTime=1435610539000",
141 "CompletionTime=1435610540000",
142 "Metadata.ContentType=text/plain;charset=utf-8",
146 Optional<Data> data = dataFuture.get();
147 assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
148 assertThat(data.get().size(), is(6L));
149 assertThat(ByteStreams.toByteArray(data.get().getInputStream()),
150 is("Hello\n".getBytes(StandardCharsets.UTF_8)));
153 private String extractIdentifier(List<String> lines) {
154 return lines.stream()
155 .filter(s -> s.startsWith("Identifier="))
156 .map(s -> s.substring(s.indexOf('=') + 1))
162 public void clientGetDownloadsDataForCorrectIdentifier()
163 throws InterruptedException, ExecutionException, IOException {
164 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
166 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
167 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
168 String identifier = extractIdentifier(lines);
171 "Identifier=not-test",
173 "StartupTime=1435610539000",
174 "CompletionTime=1435610540000",
175 "Metadata.ContentType=text/plain;charset=latin-9",
181 "Identifier=" + identifier,
183 "StartupTime=1435610539000",
184 "CompletionTime=1435610540000",
185 "Metadata.ContentType=text/plain;charset=utf-8",
189 Optional<Data> data = dataFuture.get();
190 assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
191 assertThat(data.get().size(), is(6L));
192 assertThat(ByteStreams.toByteArray(data.get().getInputStream()),
193 is("Hello\n".getBytes(StandardCharsets.UTF_8)));
197 public void clientGetRecognizesGetFailed() throws InterruptedException, ExecutionException, IOException {
198 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
200 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
201 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
202 String identifier = extractIdentifier(lines);
205 "Identifier=" + identifier,
209 Optional<Data> data = dataFuture.get();
210 assertThat(data.isPresent(), is(false));
214 public void clientGetRecognizesGetFailedForCorrectIdentifier()
215 throws InterruptedException, ExecutionException, IOException {
216 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
218 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
219 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
220 String identifier = extractIdentifier(lines);
223 "Identifier=not-test",
229 "Identifier=" + identifier,
233 Optional<Data> data = dataFuture.get();
234 assertThat(data.isPresent(), is(false));
237 @Test(expected = ExecutionException.class)
238 public void clientGetRecognizesConnectionClosed() throws InterruptedException, ExecutionException, IOException {
239 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
241 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
242 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
248 public void defaultFcpClientReusesConnection() throws InterruptedException, ExecutionException, IOException {
249 Future<FcpKeyPair> keyPair = fcpClient.generateKeypair().execute();
251 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
252 String identifier = extractIdentifier(lines);
255 "InsertURI=" + INSERT_URI + "",
256 "RequestURI=" + REQUEST_URI + "",
257 "Identifier=" + identifier,
261 keyPair = fcpClient.generateKeypair().execute();
262 lines = fcpServer.collectUntil(is("EndMessage"));
263 identifier = extractIdentifier(lines);
266 "InsertURI=" + INSERT_URI + "",
267 "RequestURI=" + REQUEST_URI + "",
268 "Identifier=" + identifier,
275 public void defaultFcpClientCanReconnectAfterConnectionHasBeenClosed()
276 throws InterruptedException, ExecutionException, IOException {
277 Future<FcpKeyPair> keyPair = fcpClient.generateKeypair().execute();
279 fcpServer.collectUntil(is("EndMessage"));
284 } catch (ExecutionException e) {
286 keyPair = fcpClient.generateKeypair().execute();
288 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
289 String identifier = extractIdentifier(lines);
292 "InsertURI=" + INSERT_URI + "",
293 "RequestURI=" + REQUEST_URI + "",
294 "Identifier=" + identifier,
301 public void clientGetWithIgnoreDataStoreSettingSendsCorrectCommands()
302 throws InterruptedException, ExecutionException, IOException {
303 fcpClient.clientGet().ignoreDataStore().uri("KSK@foo.txt").execute();
305 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
306 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "IgnoreDS=true"));
310 public void clientGetWithDataStoreOnlySettingSendsCorrectCommands()
311 throws InterruptedException, ExecutionException, IOException {
312 fcpClient.clientGet().dataStoreOnly().uri("KSK@foo.txt").execute();
314 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
315 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "DSonly=true"));
319 public void clientGetWithMaxSizeSettingSendsCorrectCommands()
320 throws InterruptedException, ExecutionException, IOException {
321 fcpClient.clientGet().maxSize(1048576).uri("KSK@foo.txt").execute();
323 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
324 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "MaxSize=1048576"));
328 public void clientGetWithPrioritySettingSendsCorrectCommands()
329 throws InterruptedException, ExecutionException, IOException {
330 fcpClient.clientGet().priority(Priority.interactive).uri("KSK@foo.txt").execute();
332 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
333 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "PriorityClass=1"));
337 public void clientGetWithRealTimeSettingSendsCorrectCommands()
338 throws InterruptedException, ExecutionException, IOException {
339 fcpClient.clientGet().realTime().uri("KSK@foo.txt").execute();
341 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
342 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "RealTimeFlag=true"));
346 public void clientGetWithGlobalSettingSendsCorrectCommands()
347 throws InterruptedException, ExecutionException, IOException {
348 fcpClient.clientGet().global().uri("KSK@foo.txt").execute();
350 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
351 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "Global=true"));
354 private Matcher<List<String>> matchesFcpMessage(String name, String... requiredLines) {
355 return new TypeSafeDiagnosingMatcher<List<String>>() {
357 protected boolean matchesSafely(List<String> item, Description mismatchDescription) {
358 if (!item.get(0).equals(name)) {
359 mismatchDescription.appendText("FCP message is named ").appendValue(item.get(0));
362 for (String requiredLine : requiredLines) {
363 if (item.indexOf(requiredLine) < 1) {
364 mismatchDescription.appendText("FCP message does not contain ").appendValue(requiredLine);
372 public void describeTo(Description description) {
373 description.appendText("FCP message named ").appendValue(name);
374 description.appendValueList(", containing the lines ", ", ", "", requiredLines);
380 public void clientPutWithDirectDataSendsCorrectCommand()
381 throws IOException, ExecutionException, InterruptedException {
382 fcpClient.clientPut()
383 .from(new ByteArrayInputStream("Hello\n".getBytes()))
388 List<String> lines = fcpServer.collectUntil(is("Hello"));
389 assertThat(lines, matchesFcpMessage("ClientPut", "UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt"));
393 public void clientPutWithDirectDataSucceedsOnCorrectIdentifier()
394 throws InterruptedException, ExecutionException, IOException {
395 Future<Optional<Key>> key = fcpClient.clientPut()
396 .from(new ByteArrayInputStream("Hello\n".getBytes()))
401 List<String> lines = fcpServer.collectUntil(is("Hello"));
402 String identifier = extractIdentifier(lines);
405 "Identifier=not-the-right-one",
411 "Identifier=" + identifier,
414 assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
418 public void clientPutWithDirectDataFailsOnCorrectIdentifier()
419 throws InterruptedException, ExecutionException, IOException {
420 Future<Optional<Key>> key = fcpClient.clientPut()
421 .from(new ByteArrayInputStream("Hello\n".getBytes()))
426 List<String> lines = fcpServer.collectUntil(is("Hello"));
427 String identifier = extractIdentifier(lines);
430 "Identifier=not-the-right-one",
436 "Identifier=" + identifier,
439 assertThat(key.get().isPresent(), is(false));
443 public void clientPutWithRenamedDirectDataSendsCorrectCommand()
444 throws InterruptedException, ExecutionException, IOException {
445 fcpClient.clientPut()
446 .named("otherName.txt")
447 .from(new ByteArrayInputStream("Hello\n".getBytes()))
452 List<String> lines = fcpServer.collectUntil(is("Hello"));
453 assertThat(lines, matchesFcpMessage("ClientPut", "TargetFilename=otherName.txt", "UploadFrom=direct",
454 "DataLength=6", "URI=KSK@foo.txt"));
458 public void clientPutWithRedirectSendsCorrectCommand()
459 throws IOException, ExecutionException, InterruptedException {
460 fcpClient.clientPut().redirectTo("KSK@bar.txt").uri("KSK@foo.txt").execute();
462 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
464 matchesFcpMessage("ClientPut", "UploadFrom=redirect", "URI=KSK@foo.txt", "TargetURI=KSK@bar.txt"));
468 public void clientPutWithFileSendsCorrectCommand() throws InterruptedException, ExecutionException, IOException {
469 fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
471 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
473 matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt", "Filename=/tmp/data.txt"));
477 public void clientPutWithFileCanCompleteTestDdaSequence()
478 throws IOException, ExecutionException, InterruptedException {
479 File tempFile = createTempFile();
480 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
482 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
483 String identifier = extractIdentifier(lines);
486 "Identifier=" + identifier,
490 lines = fcpServer.collectUntil(is("EndMessage"));
491 assertThat(lines, matchesFcpMessage(
493 "Directory=" + tempFile.getParent(),
494 "WantReadDirectory=true",
495 "WantWriteDirectory=false",
500 "Directory=" + tempFile.getParent(),
501 "ReadFilename=" + tempFile,
504 lines = fcpServer.collectUntil(is("EndMessage"));
505 assertThat(lines, matchesFcpMessage(
507 "Directory=" + tempFile.getParent(),
508 "ReadContent=test-content",
513 "Directory=" + tempFile.getParent(),
514 "ReadDirectoryAllowed=true",
517 lines = fcpServer.collectUntil(is("EndMessage"));
519 matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt",
520 "Filename=" + new File(tempFile.getParent(), "test.dat")));
523 private File createTempFile() throws IOException {
524 File tempFile = File.createTempFile("test-dda-", ".dat");
525 tempFile.deleteOnExit();
526 Files.write("test-content", tempFile, StandardCharsets.UTF_8);
531 public void clientPutDoesNotReactToProtocolErrorForDifferentIdentifier()
532 throws InterruptedException, ExecutionException, IOException {
533 Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
535 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
536 String identifier = extractIdentifier(lines);
539 "Identifier=not-the-right-one",
545 "Identifier=" + identifier,
549 assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
553 public void clientPutAbortsOnProtocolErrorOtherThan25()
554 throws InterruptedException, ExecutionException, IOException {
555 Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
557 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
558 String identifier = extractIdentifier(lines);
561 "Identifier=" + identifier,
565 assertThat(key.get().isPresent(), is(false));
569 public void clientPutDoesNotReplyToWrongTestDdaReply() throws IOException, ExecutionException,
570 InterruptedException {
571 File tempFile = createTempFile();
572 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
574 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
575 String identifier = extractIdentifier(lines);
578 "Identifier=" + identifier,
582 lines = fcpServer.collectUntil(is("EndMessage"));
583 assertThat(lines, matchesFcpMessage(
585 "Directory=" + tempFile.getParent(),
586 "WantReadDirectory=true",
587 "WantWriteDirectory=false",
592 "Directory=/some-other-directory",
593 "ReadFilename=" + tempFile,
598 "Directory=" + tempFile.getParent(),
599 "ReadFilename=" + tempFile,
602 lines = fcpServer.collectUntil(is("EndMessage"));
603 assertThat(lines, matchesFcpMessage(
605 "Directory=" + tempFile.getParent(),
606 "ReadContent=test-content",
612 public void clientPutSendsResponseEvenIfFileCanNotBeRead()
613 throws IOException, ExecutionException, InterruptedException {
614 File tempFile = createTempFile();
615 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
617 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
618 String identifier = extractIdentifier(lines);
621 "Identifier=" + identifier,
625 lines = fcpServer.collectUntil(is("EndMessage"));
626 assertThat(lines, matchesFcpMessage(
628 "Directory=" + tempFile.getParent(),
629 "WantReadDirectory=true",
630 "WantWriteDirectory=false",
635 "Directory=" + tempFile.getParent(),
636 "ReadFilename=" + tempFile + ".foo",
639 lines = fcpServer.collectUntil(is("EndMessage"));
640 assertThat(lines, matchesFcpMessage(
642 "Directory=" + tempFile.getParent(),
643 "ReadContent=failed-to-read",
649 public void clientPutDoesNotResendOriginalClientPutOnTestDDACompleteWithWrongDirectory()
650 throws IOException, ExecutionException, InterruptedException {
651 File tempFile = createTempFile();
652 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
654 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
655 String identifier = extractIdentifier(lines);
658 "Directory=/some-other-directory",
663 "Identifier=" + identifier,
667 lines = fcpServer.collectUntil(is("EndMessage"));
668 assertThat(lines, matchesFcpMessage(
670 "Directory=" + tempFile.getParent(),
671 "WantReadDirectory=true",
672 "WantWriteDirectory=false",
678 public void clientCanListPeers() throws IOException, ExecutionException, InterruptedException {
679 Future<Collection<Peer>> peers = fcpClient.listPeers().execute();
681 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
682 assertThat(lines, matchesFcpMessage(
684 "WithVolatile=false",
685 "WithMetadata=false",
688 String identifier = extractIdentifier(lines);
691 "Identifier=" + identifier,
697 "Identifier=" + identifier,
703 "Identifier=" + identifier,
706 assertThat(peers.get(), hasSize(2));
707 assertThat(peers.get().stream().map(Peer::getIdentity).collect(Collectors.toList()),
708 containsInAnyOrder("id1", "id2"));
712 public void clientCanListPeersWithMetadata() throws IOException, ExecutionException, InterruptedException {
713 Future<Collection<Peer>> peers = fcpClient.listPeers().includeMetadata().execute();
715 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
716 assertThat(lines, matchesFcpMessage(
718 "WithVolatile=false",
722 String identifier = extractIdentifier(lines);
725 "Identifier=" + identifier,
732 "Identifier=" + identifier,
739 "Identifier=" + identifier,
742 assertThat(peers.get(), hasSize(2));
743 assertThat(peers.get().stream().map(peer -> peer.getMetadata("foo")).collect(Collectors.toList()),
744 containsInAnyOrder("bar1", "bar2"));
748 public void clientCanListPeersWithVolatiles() throws IOException, ExecutionException, InterruptedException {
749 Future<Collection<Peer>> peers = fcpClient.listPeers().includeVolatile().execute();
751 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
752 assertThat(lines, matchesFcpMessage(
755 "WithMetadata=false",
758 String identifier = extractIdentifier(lines);
761 "Identifier=" + identifier,
768 "Identifier=" + identifier,
775 "Identifier=" + identifier,
778 assertThat(peers.get(), hasSize(2));
779 assertThat(peers.get().stream().map(peer -> peer.getVolatile("foo")).collect(Collectors.toList()),
780 containsInAnyOrder("bar1", "bar2"));
784 public void defaultFcpClientCanGetNodeInformation() throws InterruptedException, ExecutionException, IOException {
785 Future<NodeData> nodeData = fcpClient.getNode().execute();
787 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
788 String identifier = extractIdentifier(lines);
789 assertThat(lines, matchesFcpMessage(
791 "Identifier=" + identifier,
792 "GiveOpennetRef=false",
794 "WithVolatile=false",
799 "Identifier=" + identifier,
800 "ark.pubURI=SSK@3YEf.../ark",
803 "version=Fred,0.7,1.0,1466",
804 "lastGoodVersion=Fred,0.7,1.0,1466",
807 assertThat(nodeData.get(), notNullValue());
811 public void defaultFcpClientCanGetNodeInformationWithOpennetRef()
812 throws InterruptedException, ExecutionException, IOException {
813 Future<NodeData> nodeData = fcpClient.getNode().opennetRef().execute();
815 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
816 String identifier = extractIdentifier(lines);
817 assertThat(lines, matchesFcpMessage(
819 "Identifier=" + identifier,
820 "GiveOpennetRef=true",
822 "WithVolatile=false",
827 "Identifier=" + identifier,
829 "ark.pubURI=SSK@3YEf.../ark",
832 "version=Fred,0.7,1.0,1466",
833 "lastGoodVersion=Fred,0.7,1.0,1466",
836 assertThat(nodeData.get().getVersion().toString(), is("Fred,0.7,1.0,1466"));
840 public void defaultFcpClientCanGetNodeInformationWithPrivateData()
841 throws InterruptedException, ExecutionException, IOException {
842 Future<NodeData> nodeData = fcpClient.getNode().includePrivate().execute();
844 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
845 String identifier = extractIdentifier(lines);
846 assertThat(lines, matchesFcpMessage(
848 "Identifier=" + identifier,
849 "GiveOpennetRef=false",
851 "WithVolatile=false",
856 "Identifier=" + identifier,
858 "ark.pubURI=SSK@3YEf.../ark",
861 "version=Fred,0.7,1.0,1466",
862 "lastGoodVersion=Fred,0.7,1.0,1466",
863 "ark.privURI=SSK@XdHMiRl",
866 assertThat(nodeData.get().getARK().getPrivateURI(), is("SSK@XdHMiRl"));
870 public void defaultFcpClientCanGetNodeInformationWithVolatileData()
871 throws InterruptedException, ExecutionException, IOException {
872 Future<NodeData> nodeData = fcpClient.getNode().includeVolatile().execute();
874 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
875 String identifier = extractIdentifier(lines);
876 assertThat(lines, matchesFcpMessage(
878 "Identifier=" + identifier,
879 "GiveOpennetRef=false",
886 "Identifier=" + identifier,
888 "ark.pubURI=SSK@3YEf.../ark",
891 "version=Fred,0.7,1.0,1466",
892 "lastGoodVersion=Fred,0.7,1.0,1466",
893 "volatile.freeJavaMemory=205706528",
896 assertThat(nodeData.get().getVolatile("freeJavaMemory"), is("205706528"));
900 public void defaultFcpClientCanListSinglePeerByIdentity()
901 throws InterruptedException, ExecutionException, IOException {
902 Future<Optional<Peer>> peer = fcpClient.listPeer().byIdentity("id1").execute();
904 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
905 String identifier = extractIdentifier(lines);
906 assertThat(lines, matchesFcpMessage(
908 "Identifier=" + identifier,
909 "NodeIdentifier=id1",
914 "Identifier=" + identifier,
917 "ark.pubURI=SSK@3YEf.../ark",
920 "version=Fred,0.7,1.0,1466",
921 "lastGoodVersion=Fred,0.7,1.0,1466",
924 assertThat(peer.get().get().getIdentity(), is("id1"));
928 public void defaultFcpClientCanListSinglePeerByHostAndPort()
929 throws InterruptedException, ExecutionException, IOException {
930 Future<Optional<Peer>> peer = fcpClient.listPeer().byHostAndPort("host.free.net", 12345).execute();
932 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
933 String identifier = extractIdentifier(lines);
934 assertThat(lines, matchesFcpMessage(
936 "Identifier=" + identifier,
937 "NodeIdentifier=host.free.net:12345",
942 "Identifier=" + identifier,
945 "ark.pubURI=SSK@3YEf.../ark",
948 "version=Fred,0.7,1.0,1466",
949 "lastGoodVersion=Fred,0.7,1.0,1466",
952 assertThat(peer.get().get().getIdentity(), is("id1"));
956 public void defaultFcpClientCanListSinglePeerByName()
957 throws InterruptedException, ExecutionException, IOException {
958 Future<Optional<Peer>> peer = fcpClient.listPeer().byName("FriendNode").execute();
960 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
961 String identifier = extractIdentifier(lines);
962 assertThat(lines, matchesFcpMessage(
964 "Identifier=" + identifier,
965 "NodeIdentifier=FriendNode",
970 "Identifier=" + identifier,
973 "ark.pubURI=SSK@3YEf.../ark",
976 "version=Fred,0.7,1.0,1466",
977 "lastGoodVersion=Fred,0.7,1.0,1466",
980 assertThat(peer.get().get().getIdentity(), is("id1"));
984 public void defaultFcpClientRecognizesUnknownNodeIdentifiers()
985 throws InterruptedException, ExecutionException, IOException {
986 Future<Optional<Peer>> peer = fcpClient.listPeer().byIdentity("id2").execute();
988 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
989 String identifier = extractIdentifier(lines);
990 assertThat(lines, matchesFcpMessage(
992 "Identifier=" + identifier,
993 "NodeIdentifier=id2",
997 "UnknownNodeIdentifier",
998 "Identifier=" + identifier,
999 "NodeIdentifier=id2",
1002 assertThat(peer.get().isPresent(), is(false));
1006 public void defaultFcpClientCanAddPeerFromFile() throws InterruptedException, ExecutionException, IOException {
1007 Future<Optional<Peer>> peer = fcpClient.addPeer().fromFile(new File("/tmp/ref.txt")).execute();
1009 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1010 String identifier = extractIdentifier(lines);
1011 assertThat(lines, matchesFcpMessage(
1013 "Identifier=" + identifier,
1014 "File=/tmp/ref.txt",
1017 fcpServer.writeLine(
1019 "Identifier=" + identifier,
1022 "ark.pubURI=SSK@3YEf.../ark",
1025 "version=Fred,0.7,1.0,1466",
1026 "lastGoodVersion=Fred,0.7,1.0,1466",
1029 assertThat(peer.get().get().getIdentity(), is("id1"));
1033 public void defaultFcpClientCanAddPeerFromURL() throws InterruptedException, ExecutionException, IOException {
1034 Future<Optional<Peer>> peer = fcpClient.addPeer().fromURL(new URL("http://node.ref/")).execute();
1036 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1037 String identifier = extractIdentifier(lines);
1038 assertThat(lines, matchesFcpMessage(
1040 "Identifier=" + identifier,
1041 "URL=http://node.ref/",
1044 fcpServer.writeLine(
1046 "Identifier=" + identifier,
1049 "ark.pubURI=SSK@3YEf.../ark",
1052 "version=Fred,0.7,1.0,1466",
1053 "lastGoodVersion=Fred,0.7,1.0,1466",
1056 assertThat(peer.get().get().getIdentity(), is("id1"));
1060 public void defaultFcpClientCanAddPeerFromNodeRef() throws InterruptedException, ExecutionException, IOException {
1061 NodeRef nodeRef = new NodeRef();
1062 nodeRef.setIdentity("id1");
1063 nodeRef.setName("name");
1064 nodeRef.setARK(new ARK("public", "1"));
1065 nodeRef.setDSAGroup(new DSAGroup("base", "prime", "subprime"));
1066 nodeRef.setNegotiationTypes(new int[] { 3, 5 });
1067 nodeRef.setPhysicalUDP("1.2.3.4:5678");
1068 nodeRef.setDSAPublicKey("dsa-public");
1069 nodeRef.setSignature("sig");
1070 Future<Optional<Peer>> peer = fcpClient.addPeer().fromNodeRef(nodeRef).execute();
1072 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1073 String identifier = extractIdentifier(lines);
1074 assertThat(lines, matchesFcpMessage(
1076 "Identifier=" + identifier,
1079 "ark.pubURI=public",
1083 "dsaGroup.q=subprime",
1084 "dsaPubKey.y=dsa-public",
1085 "physical.udp=1.2.3.4:5678",
1086 "auth.negTypes=3;5",
1090 fcpServer.writeLine(
1092 "Identifier=" + identifier,
1095 "ark.pubURI=SSK@3YEf.../ark",
1098 "version=Fred,0.7,1.0,1466",
1099 "lastGoodVersion=Fred,0.7,1.0,1466",
1102 assertThat(peer.get().get().getIdentity(), is("id1"));