1 package net.pterodactylus.fcp.quelaton;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.contains;
5 import static org.hamcrest.Matchers.containsInAnyOrder;
6 import static org.hamcrest.Matchers.hasSize;
7 import static org.hamcrest.Matchers.is;
8 import static org.hamcrest.Matchers.not;
9 import static org.hamcrest.Matchers.notNullValue;
10 import static org.hamcrest.Matchers.startsWith;
12 import java.io.ByteArrayInputStream;
14 import java.io.IOException;
16 import java.nio.charset.StandardCharsets;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Optional;
20 import java.util.concurrent.CopyOnWriteArrayList;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.Future;
25 import java.util.stream.Collectors;
27 import net.pterodactylus.fcp.ARK;
28 import net.pterodactylus.fcp.ConfigData;
29 import net.pterodactylus.fcp.DSAGroup;
30 import net.pterodactylus.fcp.FcpKeyPair;
31 import net.pterodactylus.fcp.Key;
32 import net.pterodactylus.fcp.NodeData;
33 import net.pterodactylus.fcp.NodeRef;
34 import net.pterodactylus.fcp.Peer;
35 import net.pterodactylus.fcp.PeerNote;
36 import net.pterodactylus.fcp.PluginInfo;
37 import net.pterodactylus.fcp.Priority;
38 import net.pterodactylus.fcp.fake.FakeTcpServer;
39 import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
41 import com.google.common.io.ByteStreams;
42 import com.google.common.io.Files;
43 import org.hamcrest.Description;
44 import org.hamcrest.Matcher;
45 import org.hamcrest.TypeSafeDiagnosingMatcher;
46 import org.junit.After;
47 import org.junit.Assert;
48 import org.junit.Test;
51 * Unit test for {@link DefaultFcpClient}.
53 * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
55 public class DefaultFcpClientTest {
57 private static final String INSERT_URI =
58 "SSK@RVCHbJdkkyTCeNN9AYukEg76eyqmiosSaNKgE3U9zUw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQECAAE/";
59 private static final String REQUEST_URI =
60 "SSK@wtbgd2loNcJCXvtQVOftl2tuWBomDQHfqS6ytpPRhfw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQACAAE/";
62 private int threadCounter = 0;
63 private final ExecutorService threadPool =
64 Executors.newCachedThreadPool(r -> new Thread(r, "Test-Thread-" + threadCounter++));
65 private final FakeTcpServer fcpServer;
66 private final DefaultFcpClient fcpClient;
68 public DefaultFcpClientTest() throws IOException {
69 fcpServer = new FakeTcpServer(threadPool);
70 fcpClient = new DefaultFcpClient(threadPool, "localhost", fcpServer.getPort(), () -> "Test");
74 public void tearDown() throws IOException {
76 threadPool.shutdown();
79 @Test(expected = ExecutionException.class)
80 public void defaultFcpClientThrowsExceptionIfItCanNotConnect()
81 throws IOException, ExecutionException, InterruptedException {
82 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
83 fcpServer.connect().get();
84 fcpServer.collectUntil(is("EndMessage"));
86 "CloseConnectionDuplicateClientName",
92 @Test(expected = ExecutionException.class)
93 public void defaultFcpClientThrowsExceptionIfConnectionIsClosed()
94 throws IOException, ExecutionException, InterruptedException {
95 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
96 fcpServer.connect().get();
97 fcpServer.collectUntil(is("EndMessage"));
103 public void defaultFcpClientCanGenerateKeypair() throws ExecutionException, InterruptedException, IOException {
104 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
106 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
107 String identifier = extractIdentifier(lines);
108 fcpServer.writeLine("SSKKeypair",
109 "InsertURI=" + INSERT_URI + "",
110 "RequestURI=" + REQUEST_URI + "",
111 "Identifier=" + identifier,
113 FcpKeyPair keyPair = keyPairFuture.get();
114 assertThat(keyPair.getPublicKey(), is(REQUEST_URI));
115 assertThat(keyPair.getPrivateKey(), is(INSERT_URI));
118 private void connectNode() throws InterruptedException, ExecutionException, IOException {
119 fcpServer.connect().get();
120 fcpServer.collectUntil(is("EndMessage"));
121 fcpServer.writeLine("NodeHello",
122 "CompressionCodecs=4 - GZIP(0), BZIP2(1), LZMA(2), LZMA_NEW(3)",
123 "Revision=build01466",
125 "Version=Fred,0.7,1.0,1466",
127 "ConnectionIdentifier=14318898267048452a81b36e7f13a3f0",
131 "NodeLanguage=ENGLISH",
138 public void clientGetCanDownloadData() throws InterruptedException, ExecutionException, IOException {
139 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
141 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
142 assertThat(lines, matchesFcpMessage("ClientGet", "ReturnType=direct", "URI=KSK@foo.txt"));
143 String identifier = extractIdentifier(lines);
146 "Identifier=" + identifier,
148 "StartupTime=1435610539000",
149 "CompletionTime=1435610540000",
150 "Metadata.ContentType=text/plain;charset=utf-8",
154 Optional<Data> data = dataFuture.get();
155 assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
156 assertThat(data.get().size(), is(6L));
157 assertThat(ByteStreams.toByteArray(data.get().getInputStream()),
158 is("Hello\n".getBytes(StandardCharsets.UTF_8)));
161 private String extractIdentifier(List<String> lines) {
162 return lines.stream()
163 .filter(s -> s.startsWith("Identifier="))
164 .map(s -> s.substring(s.indexOf('=') + 1))
170 public void clientGetDownloadsDataForCorrectIdentifier()
171 throws InterruptedException, ExecutionException, IOException {
172 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
174 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
175 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
176 String identifier = extractIdentifier(lines);
179 "Identifier=not-test",
181 "StartupTime=1435610539000",
182 "CompletionTime=1435610540000",
183 "Metadata.ContentType=text/plain;charset=latin-9",
189 "Identifier=" + identifier,
191 "StartupTime=1435610539000",
192 "CompletionTime=1435610540000",
193 "Metadata.ContentType=text/plain;charset=utf-8",
197 Optional<Data> data = dataFuture.get();
198 assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
199 assertThat(data.get().size(), is(6L));
200 assertThat(ByteStreams.toByteArray(data.get().getInputStream()),
201 is("Hello\n".getBytes(StandardCharsets.UTF_8)));
205 public void clientGetRecognizesGetFailed() throws InterruptedException, ExecutionException, IOException {
206 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
208 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
209 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
210 String identifier = extractIdentifier(lines);
213 "Identifier=" + identifier,
217 Optional<Data> data = dataFuture.get();
218 assertThat(data.isPresent(), is(false));
222 public void clientGetRecognizesGetFailedForCorrectIdentifier()
223 throws InterruptedException, ExecutionException, IOException {
224 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
226 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
227 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
228 String identifier = extractIdentifier(lines);
231 "Identifier=not-test",
237 "Identifier=" + identifier,
241 Optional<Data> data = dataFuture.get();
242 assertThat(data.isPresent(), is(false));
245 @Test(expected = ExecutionException.class)
246 public void clientGetRecognizesConnectionClosed() throws InterruptedException, ExecutionException, IOException {
247 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
249 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
250 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
256 public void defaultFcpClientReusesConnection() throws InterruptedException, ExecutionException, IOException {
257 Future<FcpKeyPair> keyPair = fcpClient.generateKeypair().execute();
259 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
260 String identifier = extractIdentifier(lines);
263 "InsertURI=" + INSERT_URI + "",
264 "RequestURI=" + REQUEST_URI + "",
265 "Identifier=" + identifier,
269 keyPair = fcpClient.generateKeypair().execute();
270 lines = fcpServer.collectUntil(is("EndMessage"));
271 identifier = extractIdentifier(lines);
274 "InsertURI=" + INSERT_URI + "",
275 "RequestURI=" + REQUEST_URI + "",
276 "Identifier=" + identifier,
283 public void defaultFcpClientCanReconnectAfterConnectionHasBeenClosed()
284 throws InterruptedException, ExecutionException, IOException {
285 Future<FcpKeyPair> keyPair = fcpClient.generateKeypair().execute();
287 fcpServer.collectUntil(is("EndMessage"));
292 } catch (ExecutionException e) {
294 keyPair = fcpClient.generateKeypair().execute();
296 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
297 String identifier = extractIdentifier(lines);
300 "InsertURI=" + INSERT_URI + "",
301 "RequestURI=" + REQUEST_URI + "",
302 "Identifier=" + identifier,
309 public void clientGetWithIgnoreDataStoreSettingSendsCorrectCommands()
310 throws InterruptedException, ExecutionException, IOException {
311 fcpClient.clientGet().ignoreDataStore().uri("KSK@foo.txt").execute();
313 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
314 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "IgnoreDS=true"));
318 public void clientGetWithDataStoreOnlySettingSendsCorrectCommands()
319 throws InterruptedException, ExecutionException, IOException {
320 fcpClient.clientGet().dataStoreOnly().uri("KSK@foo.txt").execute();
322 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
323 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "DSonly=true"));
327 public void clientGetWithMaxSizeSettingSendsCorrectCommands()
328 throws InterruptedException, ExecutionException, IOException {
329 fcpClient.clientGet().maxSize(1048576).uri("KSK@foo.txt").execute();
331 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
332 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "MaxSize=1048576"));
336 public void clientGetWithPrioritySettingSendsCorrectCommands()
337 throws InterruptedException, ExecutionException, IOException {
338 fcpClient.clientGet().priority(Priority.interactive).uri("KSK@foo.txt").execute();
340 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
341 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "PriorityClass=1"));
345 public void clientGetWithRealTimeSettingSendsCorrectCommands()
346 throws InterruptedException, ExecutionException, IOException {
347 fcpClient.clientGet().realTime().uri("KSK@foo.txt").execute();
349 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
350 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "RealTimeFlag=true"));
354 public void clientGetWithGlobalSettingSendsCorrectCommands()
355 throws InterruptedException, ExecutionException, IOException {
356 fcpClient.clientGet().global().uri("KSK@foo.txt").execute();
358 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
359 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "Global=true"));
362 private Matcher<List<String>> matchesFcpMessage(String name, String... requiredLines) {
363 return new TypeSafeDiagnosingMatcher<List<String>>() {
365 protected boolean matchesSafely(List<String> item, Description mismatchDescription) {
366 if (!item.get(0).equals(name)) {
367 mismatchDescription.appendText("FCP message is named ").appendValue(item.get(0));
370 for (String requiredLine : requiredLines) {
371 if (item.indexOf(requiredLine) < 1) {
372 mismatchDescription.appendText("FCP message does not contain ").appendValue(requiredLine);
380 public void describeTo(Description description) {
381 description.appendText("FCP message named ").appendValue(name);
382 description.appendValueList(", containing the lines ", ", ", "", requiredLines);
388 public void clientPutWithDirectDataSendsCorrectCommand()
389 throws IOException, ExecutionException, InterruptedException {
390 fcpClient.clientPut()
391 .from(new ByteArrayInputStream("Hello\n".getBytes()))
396 List<String> lines = fcpServer.collectUntil(is("Hello"));
397 assertThat(lines, matchesFcpMessage("ClientPut", "UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt"));
401 public void clientPutWithDirectDataSucceedsOnCorrectIdentifier()
402 throws InterruptedException, ExecutionException, IOException {
403 Future<Optional<Key>> key = fcpClient.clientPut()
404 .from(new ByteArrayInputStream("Hello\n".getBytes()))
409 List<String> lines = fcpServer.collectUntil(is("Hello"));
410 String identifier = extractIdentifier(lines);
413 "Identifier=not-the-right-one",
419 "Identifier=" + identifier,
422 assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
426 public void clientPutWithDirectDataFailsOnCorrectIdentifier()
427 throws InterruptedException, ExecutionException, IOException {
428 Future<Optional<Key>> key = fcpClient.clientPut()
429 .from(new ByteArrayInputStream("Hello\n".getBytes()))
434 List<String> lines = fcpServer.collectUntil(is("Hello"));
435 String identifier = extractIdentifier(lines);
438 "Identifier=not-the-right-one",
444 "Identifier=" + identifier,
447 assertThat(key.get().isPresent(), is(false));
451 public void clientPutWithRenamedDirectDataSendsCorrectCommand()
452 throws InterruptedException, ExecutionException, IOException {
453 fcpClient.clientPut()
454 .named("otherName.txt")
455 .from(new ByteArrayInputStream("Hello\n".getBytes()))
460 List<String> lines = fcpServer.collectUntil(is("Hello"));
461 assertThat(lines, matchesFcpMessage("ClientPut", "TargetFilename=otherName.txt", "UploadFrom=direct",
462 "DataLength=6", "URI=KSK@foo.txt"));
466 public void clientPutWithRedirectSendsCorrectCommand()
467 throws IOException, ExecutionException, InterruptedException {
468 fcpClient.clientPut().redirectTo("KSK@bar.txt").uri("KSK@foo.txt").execute();
470 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
472 matchesFcpMessage("ClientPut", "UploadFrom=redirect", "URI=KSK@foo.txt", "TargetURI=KSK@bar.txt"));
476 public void clientPutWithFileSendsCorrectCommand() throws InterruptedException, ExecutionException, IOException {
477 fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
479 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
481 matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt", "Filename=/tmp/data.txt"));
485 public void clientPutWithFileCanCompleteTestDdaSequence()
486 throws IOException, ExecutionException, InterruptedException {
487 File tempFile = createTempFile();
488 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
490 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
491 String identifier = extractIdentifier(lines);
494 "Identifier=" + identifier,
498 lines = fcpServer.collectUntil(is("EndMessage"));
499 assertThat(lines, matchesFcpMessage(
501 "Directory=" + tempFile.getParent(),
502 "WantReadDirectory=true",
503 "WantWriteDirectory=false",
508 "Directory=" + tempFile.getParent(),
509 "ReadFilename=" + tempFile,
512 lines = fcpServer.collectUntil(is("EndMessage"));
513 assertThat(lines, matchesFcpMessage(
515 "Directory=" + tempFile.getParent(),
516 "ReadContent=test-content",
521 "Directory=" + tempFile.getParent(),
522 "ReadDirectoryAllowed=true",
525 lines = fcpServer.collectUntil(is("EndMessage"));
527 matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt",
528 "Filename=" + new File(tempFile.getParent(), "test.dat")));
531 private File createTempFile() throws IOException {
532 File tempFile = File.createTempFile("test-dda-", ".dat");
533 tempFile.deleteOnExit();
534 Files.write("test-content", tempFile, StandardCharsets.UTF_8);
539 public void clientPutDoesNotReactToProtocolErrorForDifferentIdentifier()
540 throws InterruptedException, ExecutionException, IOException {
541 Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
543 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
544 String identifier = extractIdentifier(lines);
547 "Identifier=not-the-right-one",
553 "Identifier=" + identifier,
557 assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
561 public void clientPutAbortsOnProtocolErrorOtherThan25()
562 throws InterruptedException, ExecutionException, IOException {
563 Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
565 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
566 String identifier = extractIdentifier(lines);
569 "Identifier=" + identifier,
573 assertThat(key.get().isPresent(), is(false));
577 public void clientPutDoesNotReplyToWrongTestDdaReply() throws IOException, ExecutionException,
578 InterruptedException {
579 File tempFile = createTempFile();
580 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
582 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
583 String identifier = extractIdentifier(lines);
586 "Identifier=" + identifier,
590 lines = fcpServer.collectUntil(is("EndMessage"));
591 assertThat(lines, matchesFcpMessage(
593 "Directory=" + tempFile.getParent(),
594 "WantReadDirectory=true",
595 "WantWriteDirectory=false",
600 "Directory=/some-other-directory",
601 "ReadFilename=" + tempFile,
606 "Directory=" + tempFile.getParent(),
607 "ReadFilename=" + tempFile,
610 lines = fcpServer.collectUntil(is("EndMessage"));
611 assertThat(lines, matchesFcpMessage(
613 "Directory=" + tempFile.getParent(),
614 "ReadContent=test-content",
620 public void clientPutSendsResponseEvenIfFileCanNotBeRead()
621 throws IOException, ExecutionException, InterruptedException {
622 File tempFile = createTempFile();
623 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
625 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
626 String identifier = extractIdentifier(lines);
629 "Identifier=" + identifier,
633 lines = fcpServer.collectUntil(is("EndMessage"));
634 assertThat(lines, matchesFcpMessage(
636 "Directory=" + tempFile.getParent(),
637 "WantReadDirectory=true",
638 "WantWriteDirectory=false",
643 "Directory=" + tempFile.getParent(),
644 "ReadFilename=" + tempFile + ".foo",
647 lines = fcpServer.collectUntil(is("EndMessage"));
648 assertThat(lines, matchesFcpMessage(
650 "Directory=" + tempFile.getParent(),
651 "ReadContent=failed-to-read",
657 public void clientPutDoesNotResendOriginalClientPutOnTestDDACompleteWithWrongDirectory()
658 throws IOException, ExecutionException, InterruptedException {
659 File tempFile = createTempFile();
660 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
662 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
663 String identifier = extractIdentifier(lines);
666 "Directory=/some-other-directory",
671 "Identifier=" + identifier,
675 lines = fcpServer.collectUntil(is("EndMessage"));
676 assertThat(lines, matchesFcpMessage(
678 "Directory=" + tempFile.getParent(),
679 "WantReadDirectory=true",
680 "WantWriteDirectory=false",
686 public void clientPutSendsNotificationsForGeneratedKeys()
687 throws InterruptedException, ExecutionException, IOException {
688 List<String> generatedKeys = new CopyOnWriteArrayList<>();
689 Future<Optional<Key>> key = fcpClient.clientPut()
690 .onKeyGenerated(generatedKeys::add)
691 .from(new ByteArrayInputStream("Hello\n".getBytes()))
696 List<String> lines = fcpServer.collectUntil(is("Hello"));
697 String identifier = extractIdentifier(lines);
700 "Identifier=" + identifier,
707 "Identifier=" + identifier,
710 assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
711 assertThat(generatedKeys, contains("KSK@foo.txt"));
715 public void clientCanListPeers() throws IOException, ExecutionException, InterruptedException {
716 Future<Collection<Peer>> peers = fcpClient.listPeers().execute();
718 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
719 assertThat(lines, matchesFcpMessage(
721 "WithVolatile=false",
722 "WithMetadata=false",
725 String identifier = extractIdentifier(lines);
728 "Identifier=" + identifier,
734 "Identifier=" + identifier,
740 "Identifier=" + identifier,
743 assertThat(peers.get(), hasSize(2));
744 assertThat(peers.get().stream().map(Peer::getIdentity).collect(Collectors.toList()),
745 containsInAnyOrder("id1", "id2"));
749 public void clientCanListPeersWithMetadata() throws IOException, ExecutionException, InterruptedException {
750 Future<Collection<Peer>> peers = fcpClient.listPeers().includeMetadata().execute();
752 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
753 assertThat(lines, matchesFcpMessage(
755 "WithVolatile=false",
759 String identifier = extractIdentifier(lines);
762 "Identifier=" + identifier,
769 "Identifier=" + identifier,
776 "Identifier=" + identifier,
779 assertThat(peers.get(), hasSize(2));
780 assertThat(peers.get().stream().map(peer -> peer.getMetadata("foo")).collect(Collectors.toList()),
781 containsInAnyOrder("bar1", "bar2"));
785 public void clientCanListPeersWithVolatiles() throws IOException, ExecutionException, InterruptedException {
786 Future<Collection<Peer>> peers = fcpClient.listPeers().includeVolatile().execute();
788 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
789 assertThat(lines, matchesFcpMessage(
792 "WithMetadata=false",
795 String identifier = extractIdentifier(lines);
798 "Identifier=" + identifier,
805 "Identifier=" + identifier,
812 "Identifier=" + identifier,
815 assertThat(peers.get(), hasSize(2));
816 assertThat(peers.get().stream().map(peer -> peer.getVolatile("foo")).collect(Collectors.toList()),
817 containsInAnyOrder("bar1", "bar2"));
821 public void defaultFcpClientCanGetNodeInformation() throws InterruptedException, ExecutionException, IOException {
822 Future<NodeData> nodeData = fcpClient.getNode().execute();
824 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
825 String identifier = extractIdentifier(lines);
826 assertThat(lines, matchesFcpMessage(
828 "Identifier=" + identifier,
829 "GiveOpennetRef=false",
831 "WithVolatile=false",
836 "Identifier=" + identifier,
837 "ark.pubURI=SSK@3YEf.../ark",
840 "version=Fred,0.7,1.0,1466",
841 "lastGoodVersion=Fred,0.7,1.0,1466",
844 assertThat(nodeData.get(), notNullValue());
848 public void defaultFcpClientCanGetNodeInformationWithOpennetRef()
849 throws InterruptedException, ExecutionException, IOException {
850 Future<NodeData> nodeData = fcpClient.getNode().opennetRef().execute();
852 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
853 String identifier = extractIdentifier(lines);
854 assertThat(lines, matchesFcpMessage(
856 "Identifier=" + identifier,
857 "GiveOpennetRef=true",
859 "WithVolatile=false",
864 "Identifier=" + identifier,
866 "ark.pubURI=SSK@3YEf.../ark",
869 "version=Fred,0.7,1.0,1466",
870 "lastGoodVersion=Fred,0.7,1.0,1466",
873 assertThat(nodeData.get().getVersion().toString(), is("Fred,0.7,1.0,1466"));
877 public void defaultFcpClientCanGetNodeInformationWithPrivateData()
878 throws InterruptedException, ExecutionException, IOException {
879 Future<NodeData> nodeData = fcpClient.getNode().includePrivate().execute();
881 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
882 String identifier = extractIdentifier(lines);
883 assertThat(lines, matchesFcpMessage(
885 "Identifier=" + identifier,
886 "GiveOpennetRef=false",
888 "WithVolatile=false",
893 "Identifier=" + identifier,
895 "ark.pubURI=SSK@3YEf.../ark",
898 "version=Fred,0.7,1.0,1466",
899 "lastGoodVersion=Fred,0.7,1.0,1466",
900 "ark.privURI=SSK@XdHMiRl",
903 assertThat(nodeData.get().getARK().getPrivateURI(), is("SSK@XdHMiRl"));
907 public void defaultFcpClientCanGetNodeInformationWithVolatileData()
908 throws InterruptedException, ExecutionException, IOException {
909 Future<NodeData> nodeData = fcpClient.getNode().includeVolatile().execute();
911 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
912 String identifier = extractIdentifier(lines);
913 assertThat(lines, matchesFcpMessage(
915 "Identifier=" + identifier,
916 "GiveOpennetRef=false",
923 "Identifier=" + identifier,
925 "ark.pubURI=SSK@3YEf.../ark",
928 "version=Fred,0.7,1.0,1466",
929 "lastGoodVersion=Fred,0.7,1.0,1466",
930 "volatile.freeJavaMemory=205706528",
933 assertThat(nodeData.get().getVolatile("freeJavaMemory"), is("205706528"));
937 public void defaultFcpClientCanListSinglePeerByIdentity()
938 throws InterruptedException, ExecutionException, IOException {
939 Future<Optional<Peer>> peer = fcpClient.listPeer().byIdentity("id1").execute();
941 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
942 String identifier = extractIdentifier(lines);
943 assertThat(lines, matchesFcpMessage(
945 "Identifier=" + identifier,
946 "NodeIdentifier=id1",
951 "Identifier=" + identifier,
954 "ark.pubURI=SSK@3YEf.../ark",
957 "version=Fred,0.7,1.0,1466",
958 "lastGoodVersion=Fred,0.7,1.0,1466",
961 assertThat(peer.get().get().getIdentity(), is("id1"));
965 public void defaultFcpClientCanListSinglePeerByHostAndPort()
966 throws InterruptedException, ExecutionException, IOException {
967 Future<Optional<Peer>> peer = fcpClient.listPeer().byHostAndPort("host.free.net", 12345).execute();
969 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
970 String identifier = extractIdentifier(lines);
971 assertThat(lines, matchesFcpMessage(
973 "Identifier=" + identifier,
974 "NodeIdentifier=host.free.net:12345",
979 "Identifier=" + identifier,
982 "ark.pubURI=SSK@3YEf.../ark",
985 "version=Fred,0.7,1.0,1466",
986 "lastGoodVersion=Fred,0.7,1.0,1466",
989 assertThat(peer.get().get().getIdentity(), is("id1"));
993 public void defaultFcpClientCanListSinglePeerByName()
994 throws InterruptedException, ExecutionException, IOException {
995 Future<Optional<Peer>> peer = fcpClient.listPeer().byName("FriendNode").execute();
997 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
998 String identifier = extractIdentifier(lines);
999 assertThat(lines, matchesFcpMessage(
1001 "Identifier=" + identifier,
1002 "NodeIdentifier=FriendNode",
1005 fcpServer.writeLine(
1007 "Identifier=" + identifier,
1010 "ark.pubURI=SSK@3YEf.../ark",
1013 "version=Fred,0.7,1.0,1466",
1014 "lastGoodVersion=Fred,0.7,1.0,1466",
1017 assertThat(peer.get().get().getIdentity(), is("id1"));
1021 public void defaultFcpClientRecognizesUnknownNodeIdentifiers()
1022 throws InterruptedException, ExecutionException, IOException {
1023 Future<Optional<Peer>> peer = fcpClient.listPeer().byIdentity("id2").execute();
1025 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1026 String identifier = extractIdentifier(lines);
1027 assertThat(lines, matchesFcpMessage(
1029 "Identifier=" + identifier,
1030 "NodeIdentifier=id2",
1033 fcpServer.writeLine(
1034 "UnknownNodeIdentifier",
1035 "Identifier=" + identifier,
1036 "NodeIdentifier=id2",
1039 assertThat(peer.get().isPresent(), is(false));
1043 public void defaultFcpClientCanAddPeerFromFile() throws InterruptedException, ExecutionException, IOException {
1044 Future<Optional<Peer>> peer = fcpClient.addPeer().fromFile(new File("/tmp/ref.txt")).execute();
1046 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1047 String identifier = extractIdentifier(lines);
1048 assertThat(lines, matchesFcpMessage(
1050 "Identifier=" + identifier,
1051 "File=/tmp/ref.txt",
1054 fcpServer.writeLine(
1056 "Identifier=" + identifier,
1059 "ark.pubURI=SSK@3YEf.../ark",
1062 "version=Fred,0.7,1.0,1466",
1063 "lastGoodVersion=Fred,0.7,1.0,1466",
1066 assertThat(peer.get().get().getIdentity(), is("id1"));
1070 public void defaultFcpClientCanAddPeerFromURL() throws InterruptedException, ExecutionException, IOException {
1071 Future<Optional<Peer>> peer = fcpClient.addPeer().fromURL(new URL("http://node.ref/")).execute();
1073 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1074 String identifier = extractIdentifier(lines);
1075 assertThat(lines, matchesFcpMessage(
1077 "Identifier=" + identifier,
1078 "URL=http://node.ref/",
1081 fcpServer.writeLine(
1083 "Identifier=" + identifier,
1086 "ark.pubURI=SSK@3YEf.../ark",
1089 "version=Fred,0.7,1.0,1466",
1090 "lastGoodVersion=Fred,0.7,1.0,1466",
1093 assertThat(peer.get().get().getIdentity(), is("id1"));
1097 public void defaultFcpClientCanAddPeerFromNodeRef() throws InterruptedException, ExecutionException, IOException {
1098 NodeRef nodeRef = new NodeRef();
1099 nodeRef.setIdentity("id1");
1100 nodeRef.setName("name");
1101 nodeRef.setARK(new ARK("public", "1"));
1102 nodeRef.setDSAGroup(new DSAGroup("base", "prime", "subprime"));
1103 nodeRef.setNegotiationTypes(new int[] { 3, 5 });
1104 nodeRef.setPhysicalUDP("1.2.3.4:5678");
1105 nodeRef.setDSAPublicKey("dsa-public");
1106 nodeRef.setSignature("sig");
1107 Future<Optional<Peer>> peer = fcpClient.addPeer().fromNodeRef(nodeRef).execute();
1109 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1110 String identifier = extractIdentifier(lines);
1111 assertThat(lines, matchesFcpMessage(
1113 "Identifier=" + identifier,
1116 "ark.pubURI=public",
1120 "dsaGroup.q=subprime",
1121 "dsaPubKey.y=dsa-public",
1122 "physical.udp=1.2.3.4:5678",
1123 "auth.negTypes=3;5",
1127 fcpServer.writeLine(
1129 "Identifier=" + identifier,
1132 "ark.pubURI=SSK@3YEf.../ark",
1135 "version=Fred,0.7,1.0,1466",
1136 "lastGoodVersion=Fred,0.7,1.0,1466",
1139 assertThat(peer.get().get().getIdentity(), is("id1"));
1143 public void listPeerNotesCanGetPeerNotesByNodeName() throws InterruptedException, ExecutionException, IOException {
1144 Future<Optional<PeerNote>> peerNote = fcpClient.listPeerNotes().byName("Friend1").execute();
1146 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1147 String identifier = extractIdentifier(lines);
1148 assertThat(lines, matchesFcpMessage(
1150 "NodeIdentifier=Friend1",
1153 fcpServer.writeLine(
1155 "Identifier=" + identifier,
1156 "NodeIdentifier=Friend1",
1157 "NoteText=RXhhbXBsZSBUZXh0Lg==",
1161 fcpServer.writeLine(
1163 "Identifier=" + identifier,
1166 assertThat(peerNote.get().get().getNoteText(), is("RXhhbXBsZSBUZXh0Lg=="));
1167 assertThat(peerNote.get().get().getPeerNoteType(), is(1));
1171 public void listPeerNotesReturnsEmptyOptionalWhenNodeIdenfierUnknown()
1172 throws InterruptedException, ExecutionException,
1174 Future<Optional<PeerNote>> peerNote = fcpClient.listPeerNotes().byName("Friend1").execute();
1176 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1177 String identifier = extractIdentifier(lines);
1178 assertThat(lines, matchesFcpMessage(
1180 "NodeIdentifier=Friend1",
1183 fcpServer.writeLine(
1184 "UnknownNodeIdentifier",
1185 "Identifier=" + identifier,
1186 "NodeIdentifier=Friend1",
1189 assertThat(peerNote.get().isPresent(), is(false));
1193 public void listPeerNotesCanGetPeerNotesByNodeIdentifier()
1194 throws InterruptedException, ExecutionException, IOException {
1195 Future<Optional<PeerNote>> peerNote = fcpClient.listPeerNotes().byIdentity("id1").execute();
1197 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1198 String identifier = extractIdentifier(lines);
1199 assertThat(lines, matchesFcpMessage(
1201 "NodeIdentifier=id1",
1204 fcpServer.writeLine(
1206 "Identifier=" + identifier,
1207 "NodeIdentifier=id1",
1208 "NoteText=RXhhbXBsZSBUZXh0Lg==",
1212 fcpServer.writeLine(
1214 "Identifier=" + identifier,
1217 assertThat(peerNote.get().get().getNoteText(), is("RXhhbXBsZSBUZXh0Lg=="));
1218 assertThat(peerNote.get().get().getPeerNoteType(), is(1));
1222 public void listPeerNotesCanGetPeerNotesByHostNameAndPortNumber()
1223 throws InterruptedException, ExecutionException, IOException {
1224 Future<Optional<PeerNote>> peerNote = fcpClient.listPeerNotes().byHostAndPort("1.2.3.4", 5678).execute();
1226 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1227 String identifier = extractIdentifier(lines);
1228 assertThat(lines, matchesFcpMessage(
1230 "NodeIdentifier=1.2.3.4:5678",
1233 fcpServer.writeLine(
1235 "Identifier=" + identifier,
1236 "NodeIdentifier=id1",
1237 "NoteText=RXhhbXBsZSBUZXh0Lg==",
1241 fcpServer.writeLine(
1243 "Identifier=" + identifier,
1246 assertThat(peerNote.get().get().getNoteText(), is("RXhhbXBsZSBUZXh0Lg=="));
1247 assertThat(peerNote.get().get().getPeerNoteType(), is(1));
1251 public void defaultFcpClientCanEnablePeerByName() throws InterruptedException, ExecutionException, IOException {
1252 Future<Optional<Peer>> peer = fcpClient.modifyPeer().enable().byName("Friend1").execute();
1254 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1255 String identifier = extractIdentifier(lines);
1256 assertThat(lines, matchesFcpMessage(
1258 "Identifier=" + identifier,
1259 "NodeIdentifier=Friend1",
1263 fcpServer.writeLine(
1265 "Identifier=" + identifier,
1266 "NodeIdentifier=Friend1",
1270 assertThat(peer.get().get().getIdentity(), is("id1"));
1274 public void defaultFcpClientCanDisablePeerByName() throws InterruptedException, ExecutionException, IOException {
1275 Future<Optional<Peer>> peer = fcpClient.modifyPeer().disable().byName("Friend1").execute();
1277 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1278 String identifier = extractIdentifier(lines);
1279 assertThat(lines, matchesFcpMessage(
1281 "Identifier=" + identifier,
1282 "NodeIdentifier=Friend1",
1286 fcpServer.writeLine(
1288 "Identifier=" + identifier,
1289 "NodeIdentifier=Friend1",
1293 assertThat(peer.get().get().getIdentity(), is("id1"));
1297 public void defaultFcpClientCanEnablePeerByIdentity() throws InterruptedException, ExecutionException, IOException {
1298 Future<Optional<Peer>> peer = fcpClient.modifyPeer().enable().byIdentity("id1").execute();
1300 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1301 String identifier = extractIdentifier(lines);
1302 assertThat(lines, matchesFcpMessage(
1304 "Identifier=" + identifier,
1305 "NodeIdentifier=id1",
1309 fcpServer.writeLine(
1311 "Identifier=" + identifier,
1312 "NodeIdentifier=Friend1",
1316 assertThat(peer.get().get().getIdentity(), is("id1"));
1320 public void defaultFcpClientCanEnablePeerByHostAndPort()
1321 throws InterruptedException, ExecutionException, IOException {
1322 Future<Optional<Peer>> peer = fcpClient.modifyPeer().enable().byHostAndPort("1.2.3.4", 5678).execute();
1324 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1325 String identifier = extractIdentifier(lines);
1326 assertThat(lines, matchesFcpMessage(
1328 "Identifier=" + identifier,
1329 "NodeIdentifier=1.2.3.4:5678",
1333 fcpServer.writeLine(
1335 "Identifier=" + identifier,
1336 "NodeIdentifier=Friend1",
1340 assertThat(peer.get().get().getIdentity(), is("id1"));
1344 public void defaultFcpClientCanNotModifyPeerOfUnknownNode()
1345 throws InterruptedException, ExecutionException, IOException {
1346 Future<Optional<Peer>> peer = fcpClient.modifyPeer().enable().byIdentity("id1").execute();
1348 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1349 String identifier = extractIdentifier(lines);
1350 assertThat(lines, matchesFcpMessage(
1352 "Identifier=" + identifier,
1353 "NodeIdentifier=id1",
1357 fcpServer.writeLine(
1358 "UnknownNodeIdentifier",
1359 "Identifier=" + identifier,
1360 "NodeIdentifier=id1",
1363 assertThat(peer.get().isPresent(), is(false));
1367 public void defaultFcpClientCanAllowLocalAddressesOfPeer()
1368 throws InterruptedException, ExecutionException, IOException {
1369 Future<Optional<Peer>> peer = fcpClient.modifyPeer().allowLocalAddresses().byIdentity("id1").execute();
1371 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1372 String identifier = extractIdentifier(lines);
1373 assertThat(lines, matchesFcpMessage(
1375 "Identifier=" + identifier,
1376 "NodeIdentifier=id1",
1377 "AllowLocalAddresses=true",
1380 assertThat(lines, not(contains(startsWith("IsDisabled="))));
1381 fcpServer.writeLine(
1383 "Identifier=" + identifier,
1384 "NodeIdentifier=Friend1",
1388 assertThat(peer.get().get().getIdentity(), is("id1"));
1392 public void defaultFcpClientCanDisallowLocalAddressesOfPeer()
1393 throws InterruptedException, ExecutionException, IOException {
1394 Future<Optional<Peer>> peer = fcpClient.modifyPeer().disallowLocalAddresses().byIdentity("id1").execute();
1396 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1397 String identifier = extractIdentifier(lines);
1398 assertThat(lines, matchesFcpMessage(
1400 "Identifier=" + identifier,
1401 "NodeIdentifier=id1",
1402 "AllowLocalAddresses=false",
1405 assertThat(lines, not(contains(startsWith("IsDisabled="))));
1406 fcpServer.writeLine(
1408 "Identifier=" + identifier,
1409 "NodeIdentifier=Friend1",
1413 assertThat(peer.get().get().getIdentity(), is("id1"));
1417 public void defaultFcpClientCanSetBurstOnlyForPeer()
1418 throws InterruptedException, ExecutionException, IOException {
1419 Future<Optional<Peer>> peer = fcpClient.modifyPeer().setBurstOnly().byIdentity("id1").execute();
1421 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1422 String identifier = extractIdentifier(lines);
1423 assertThat(lines, matchesFcpMessage(
1425 "Identifier=" + identifier,
1426 "NodeIdentifier=id1",
1430 assertThat(lines, not(contains(startsWith("AllowLocalAddresses="))));
1431 assertThat(lines, not(contains(startsWith("IsDisabled="))));
1432 fcpServer.writeLine(
1434 "Identifier=" + identifier,
1435 "NodeIdentifier=Friend1",
1439 assertThat(peer.get().get().getIdentity(), is("id1"));
1443 public void defaultFcpClientCanClearBurstOnlyForPeer()
1444 throws InterruptedException, ExecutionException, IOException {
1445 Future<Optional<Peer>> peer = fcpClient.modifyPeer().clearBurstOnly().byIdentity("id1").execute();
1447 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1448 String identifier = extractIdentifier(lines);
1449 assertThat(lines, matchesFcpMessage(
1451 "Identifier=" + identifier,
1452 "NodeIdentifier=id1",
1453 "IsBurstOnly=false",
1456 assertThat(lines, not(contains(startsWith("AllowLocalAddresses="))));
1457 assertThat(lines, not(contains(startsWith("IsDisabled="))));
1458 fcpServer.writeLine(
1460 "Identifier=" + identifier,
1461 "NodeIdentifier=Friend1",
1465 assertThat(peer.get().get().getIdentity(), is("id1"));
1469 public void defaultFcpClientCanSetListenOnlyForPeer()
1470 throws InterruptedException, ExecutionException, IOException {
1471 Future<Optional<Peer>> peer = fcpClient.modifyPeer().setListenOnly().byIdentity("id1").execute();
1473 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1474 String identifier = extractIdentifier(lines);
1475 assertThat(lines, matchesFcpMessage(
1477 "Identifier=" + identifier,
1478 "NodeIdentifier=id1",
1479 "IsListenOnly=true",
1482 assertThat(lines, not(contains(startsWith("AllowLocalAddresses="))));
1483 assertThat(lines, not(contains(startsWith("IsDisabled="))));
1484 assertThat(lines, not(contains(startsWith("IsBurstOnly="))));
1485 fcpServer.writeLine(
1487 "Identifier=" + identifier,
1488 "NodeIdentifier=Friend1",
1492 assertThat(peer.get().get().getIdentity(), is("id1"));
1496 public void defaultFcpClientCanClearListenOnlyForPeer()
1497 throws InterruptedException, ExecutionException, IOException {
1498 Future<Optional<Peer>> peer = fcpClient.modifyPeer().clearListenOnly().byIdentity("id1").execute();
1500 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1501 String identifier = extractIdentifier(lines);
1502 assertThat(lines, matchesFcpMessage(
1504 "Identifier=" + identifier,
1505 "NodeIdentifier=id1",
1506 "IsListenOnly=false",
1509 assertThat(lines, not(contains(startsWith("AllowLocalAddresses="))));
1510 assertThat(lines, not(contains(startsWith("IsDisabled="))));
1511 assertThat(lines, not(contains(startsWith("IsBurstOnly="))));
1512 fcpServer.writeLine(
1514 "Identifier=" + identifier,
1515 "NodeIdentifier=Friend1",
1519 assertThat(peer.get().get().getIdentity(), is("id1"));
1523 public void defaultFcpClientCanIgnoreSourceForPeer()
1524 throws InterruptedException, ExecutionException, IOException {
1525 Future<Optional<Peer>> peer = fcpClient.modifyPeer().ignoreSource().byIdentity("id1").execute();
1527 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1528 String identifier = extractIdentifier(lines);
1529 assertThat(lines, matchesFcpMessage(
1531 "Identifier=" + identifier,
1532 "NodeIdentifier=id1",
1533 "IgnoreSourcePort=true",
1536 assertThat(lines, not(contains(startsWith("AllowLocalAddresses="))));
1537 assertThat(lines, not(contains(startsWith("IsDisabled="))));
1538 assertThat(lines, not(contains(startsWith("IsBurstOnly="))));
1539 assertThat(lines, not(contains(startsWith("IsListenOnly="))));
1540 fcpServer.writeLine(
1542 "Identifier=" + identifier,
1543 "NodeIdentifier=Friend1",
1547 assertThat(peer.get().get().getIdentity(), is("id1"));
1551 public void defaultFcpClientCanUseSourceForPeer()
1552 throws InterruptedException, ExecutionException, IOException {
1553 Future<Optional<Peer>> peer = fcpClient.modifyPeer().useSource().byIdentity("id1").execute();
1555 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1556 String identifier = extractIdentifier(lines);
1557 assertThat(lines, matchesFcpMessage(
1559 "Identifier=" + identifier,
1560 "NodeIdentifier=id1",
1561 "IgnoreSourcePort=false",
1564 assertThat(lines, not(contains(startsWith("AllowLocalAddresses="))));
1565 assertThat(lines, not(contains(startsWith("IsDisabled="))));
1566 assertThat(lines, not(contains(startsWith("IsBurstOnly="))));
1567 assertThat(lines, not(contains(startsWith("IsListenOnly="))));
1568 fcpServer.writeLine(
1570 "Identifier=" + identifier,
1571 "NodeIdentifier=Friend1",
1575 assertThat(peer.get().get().getIdentity(), is("id1"));
1579 public void defaultFcpClientCanRemovePeerByName() throws InterruptedException, ExecutionException, IOException {
1580 Future<Boolean> peer = fcpClient.removePeer().byName("Friend1").execute();
1582 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1583 String identifier = extractIdentifier(lines);
1584 assertThat(lines, matchesFcpMessage(
1586 "Identifier=" + identifier,
1587 "NodeIdentifier=Friend1",
1590 fcpServer.writeLine(
1592 "Identifier=" + identifier,
1593 "NodeIdentifier=Friend1",
1596 assertThat(peer.get(), is(true));
1600 public void defaultFcpClientCanNotRemovePeerByInvalidName()
1601 throws InterruptedException, ExecutionException, IOException {
1602 Future<Boolean> peer = fcpClient.removePeer().byName("NotFriend1").execute();
1604 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1605 String identifier = extractIdentifier(lines);
1606 assertThat(lines, matchesFcpMessage(
1608 "Identifier=" + identifier,
1609 "NodeIdentifier=NotFriend1",
1612 fcpServer.writeLine(
1613 "UnknownNodeIdentifier",
1614 "Identifier=" + identifier,
1617 assertThat(peer.get(), is(false));
1621 public void defaultFcpClientCanRemovePeerByIdentity() throws InterruptedException, ExecutionException, IOException {
1622 Future<Boolean> peer = fcpClient.removePeer().byIdentity("id1").execute();
1624 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1625 String identifier = extractIdentifier(lines);
1626 assertThat(lines, matchesFcpMessage(
1628 "Identifier=" + identifier,
1629 "NodeIdentifier=id1",
1632 fcpServer.writeLine(
1634 "Identifier=" + identifier,
1635 "NodeIdentifier=Friend1",
1638 assertThat(peer.get(), is(true));
1642 public void defaultFcpClientCanRemovePeerByHostAndPort()
1643 throws InterruptedException, ExecutionException, IOException {
1644 Future<Boolean> peer = fcpClient.removePeer().byHostAndPort("1.2.3.4", 5678).execute();
1646 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1647 String identifier = extractIdentifier(lines);
1648 assertThat(lines, matchesFcpMessage(
1650 "Identifier=" + identifier,
1651 "NodeIdentifier=1.2.3.4:5678",
1654 fcpServer.writeLine(
1656 "Identifier=" + identifier,
1657 "NodeIdentifier=Friend1",
1660 assertThat(peer.get(), is(true));
1664 public void defaultFcpClientCanModifyPeerNoteByName()
1665 throws InterruptedException, ExecutionException, IOException {
1666 Future<Boolean> noteUpdated = fcpClient.modifyPeerNote().darknetComment("foo").byName("Friend1").execute();
1668 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1669 String identifier = extractIdentifier(lines);
1670 assertThat(lines, matchesFcpMessage(
1672 "Identifier=" + identifier,
1673 "NodeIdentifier=Friend1",
1678 fcpServer.writeLine(
1680 "Identifier=" + identifier,
1681 "NodeIdentifier=Friend1",
1686 assertThat(noteUpdated.get(), is(true));
1690 public void defaultFcpClientKnowsPeerNoteWasNotModifiedOnUnknownNodeIdentifier()
1691 throws InterruptedException, ExecutionException, IOException {
1692 Future<Boolean> noteUpdated = fcpClient.modifyPeerNote().darknetComment("foo").byName("Friend1").execute();
1694 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1695 String identifier = extractIdentifier(lines);
1696 assertThat(lines, matchesFcpMessage(
1698 "Identifier=" + identifier,
1699 "NodeIdentifier=Friend1",
1704 fcpServer.writeLine(
1705 "UnknownNodeIdentifier",
1706 "Identifier=" + identifier,
1707 "NodeIdentifier=Friend1",
1710 assertThat(noteUpdated.get(), is(false));
1714 public void defaultFcpClientFailsToModifyPeerNoteWithoutPeerNote()
1715 throws InterruptedException, ExecutionException, IOException {
1716 Future<Boolean> noteUpdated = fcpClient.modifyPeerNote().byName("Friend1").execute();
1717 assertThat(noteUpdated.get(), is(false));
1721 public void defaultFcpClientCanModifyPeerNoteByIdentifier()
1722 throws InterruptedException, ExecutionException, IOException {
1723 Future<Boolean> noteUpdated = fcpClient.modifyPeerNote().darknetComment("foo").byIdentifier("id1").execute();
1725 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1726 String identifier = extractIdentifier(lines);
1727 assertThat(lines, matchesFcpMessage(
1729 "Identifier=" + identifier,
1730 "NodeIdentifier=id1",
1735 fcpServer.writeLine(
1737 "Identifier=" + identifier,
1738 "NodeIdentifier=id1",
1743 assertThat(noteUpdated.get(), is(true));
1747 public void defaultFcpClientCanModifyPeerNoteByHostAndPort()
1748 throws InterruptedException, ExecutionException, IOException {
1749 Future<Boolean> noteUpdated =
1750 fcpClient.modifyPeerNote().darknetComment("foo").byHostAndPort("1.2.3.4", 5678).execute();
1752 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1753 String identifier = extractIdentifier(lines);
1754 assertThat(lines, matchesFcpMessage(
1756 "Identifier=" + identifier,
1757 "NodeIdentifier=1.2.3.4:5678",
1762 fcpServer.writeLine(
1764 "Identifier=" + identifier,
1765 "NodeIdentifier=1.2.3.4:5678",
1770 assertThat(noteUpdated.get(), is(true));
1774 public void defaultFcpClientCanGetConfigWithoutDetails()
1775 throws InterruptedException, ExecutionException, IOException {
1776 Future<ConfigData> configData = fcpClient.getConfig().execute();
1778 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1779 String identifier = extractIdentifier(lines);
1780 assertThat(lines, matchesFcpMessage(
1782 "Identifier=" + identifier,
1785 fcpServer.writeLine(
1787 "Identifier=" + identifier,
1790 assertThat(configData.get(), notNullValue());
1794 public void defaultFcpClientCanGetConfigWithCurrent()
1795 throws InterruptedException, ExecutionException, IOException {
1796 Future<ConfigData> configData = fcpClient.getConfig().withCurrent().execute();
1798 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1799 String identifier = extractIdentifier(lines);
1800 assertThat(lines, matchesFcpMessage(
1802 "Identifier=" + identifier,
1806 fcpServer.writeLine(
1808 "Identifier=" + identifier,
1812 assertThat(configData.get().getCurrent("foo"), is("bar"));
1816 public void defaultFcpClientCanGetConfigWithDefaults()
1817 throws InterruptedException, ExecutionException, IOException {
1818 Future<ConfigData> configData = fcpClient.getConfig().withDefaults().execute();
1820 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1821 String identifier = extractIdentifier(lines);
1822 assertThat(lines, matchesFcpMessage(
1824 "Identifier=" + identifier,
1825 "WithDefaults=true",
1828 fcpServer.writeLine(
1830 "Identifier=" + identifier,
1834 assertThat(configData.get().getDefault("foo"), is("bar"));
1838 public void defaultFcpClientCanGetConfigWithSortOrder()
1839 throws InterruptedException, ExecutionException, IOException {
1840 Future<ConfigData> configData = fcpClient.getConfig().withSortOrder().execute();
1842 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1843 String identifier = extractIdentifier(lines);
1844 assertThat(lines, matchesFcpMessage(
1846 "Identifier=" + identifier,
1847 "WithSortOrder=true",
1850 fcpServer.writeLine(
1852 "Identifier=" + identifier,
1856 assertThat(configData.get().getSortOrder("foo"), is(17));
1860 public void defaultFcpClientCanGetConfigWithExpertFlag()
1861 throws InterruptedException, ExecutionException, IOException {
1862 Future<ConfigData> configData = fcpClient.getConfig().withExpertFlag().execute();
1864 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1865 String identifier = extractIdentifier(lines);
1866 assertThat(lines, matchesFcpMessage(
1868 "Identifier=" + identifier,
1869 "WithExpertFlag=true",
1872 fcpServer.writeLine(
1874 "Identifier=" + identifier,
1875 "expertFlag.foo=true",
1878 assertThat(configData.get().getExpertFlag("foo"), is(true));
1882 public void defaultFcpClientCanGetConfigWithForceWriteFlag()
1883 throws InterruptedException, ExecutionException, IOException {
1884 Future<ConfigData> configData = fcpClient.getConfig().withForceWriteFlag().execute();
1886 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1887 String identifier = extractIdentifier(lines);
1888 assertThat(lines, matchesFcpMessage(
1890 "Identifier=" + identifier,
1891 "WithForceWriteFlag=true",
1894 fcpServer.writeLine(
1896 "Identifier=" + identifier,
1897 "forceWriteFlag.foo=true",
1900 assertThat(configData.get().getForceWriteFlag("foo"), is(true));
1904 public void defaultFcpClientCanGetConfigWithShortDescription()
1905 throws InterruptedException, ExecutionException, IOException {
1906 Future<ConfigData> configData = fcpClient.getConfig().withShortDescription().execute();
1908 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1909 String identifier = extractIdentifier(lines);
1910 assertThat(lines, matchesFcpMessage(
1912 "Identifier=" + identifier,
1913 "WithShortDescription=true",
1916 fcpServer.writeLine(
1918 "Identifier=" + identifier,
1919 "shortDescription.foo=bar",
1922 assertThat(configData.get().getShortDescription("foo"), is("bar"));
1926 public void defaultFcpClientCanGetConfigWithLongDescription()
1927 throws InterruptedException, ExecutionException, IOException {
1928 Future<ConfigData> configData = fcpClient.getConfig().withLongDescription().execute();
1930 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1931 String identifier = extractIdentifier(lines);
1932 assertThat(lines, matchesFcpMessage(
1934 "Identifier=" + identifier,
1935 "WithLongDescription=true",
1938 fcpServer.writeLine(
1940 "Identifier=" + identifier,
1941 "longDescription.foo=bar",
1944 assertThat(configData.get().getLongDescription("foo"), is("bar"));
1948 public void defaultFcpClientCanGetConfigWithDataTypes()
1949 throws InterruptedException, ExecutionException, IOException {
1950 Future<ConfigData> configData = fcpClient.getConfig().withDataTypes().execute();
1952 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1953 String identifier = extractIdentifier(lines);
1954 assertThat(lines, matchesFcpMessage(
1956 "Identifier=" + identifier,
1957 "WithDataTypes=true",
1960 fcpServer.writeLine(
1962 "Identifier=" + identifier,
1963 "dataType.foo=number",
1966 assertThat(configData.get().getDataType("foo"), is("number"));
1970 public void defaultFcpClientCanModifyConfigData() throws InterruptedException, ExecutionException, IOException {
1971 Future<ConfigData> newConfigData = fcpClient.modifyConfig().set("foo.bar").to("baz").execute();
1973 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1974 String identifier = extractIdentifier(lines);
1975 assertThat(lines, matchesFcpMessage(
1977 "Identifier=" + identifier,
1981 fcpServer.writeLine(
1983 "Identifier=" + identifier,
1984 "current.foo.bar=baz",
1987 assertThat(newConfigData.get().getCurrent("foo.bar"), is("baz"));
1991 public void defaultFcpClientCanLoadPluginFromFreenet() throws ExecutionException, InterruptedException,
1993 Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute();
1995 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1996 String identifier = extractIdentifier(lines);
1997 assertThat(lines, matchesFcpMessage(
1999 "Identifier=" + identifier,
2000 "PluginURL=superPlugin",
2002 "OfficialSource=freenet",
2005 assertThat(lines, not(contains(startsWith("Store="))));
2006 fcpServer.writeLine(
2008 "Identifier=" + identifier,
2009 "PluginName=superPlugin",
2011 "LongVersion=1.2.3",
2013 "OriginUri=superPlugin",
2017 assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
2018 assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
2019 assertThat(pluginInfo.get().get().isTalkable(), is(true));
2020 assertThat(pluginInfo.get().get().getVersion(), is("42"));
2021 assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
2022 assertThat(pluginInfo.get().get().isStarted(), is(true));
2026 public void defaultFcpClientCanLoadPersistentPluginFromFreenet() throws ExecutionException, InterruptedException,
2028 Future<Optional<PluginInfo>> pluginInfo =
2029 fcpClient.loadPlugin().addToConfig().officialFromFreenet("superPlugin").execute();
2031 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
2032 String identifier = extractIdentifier(lines);
2033 assertThat(lines, matchesFcpMessage(
2035 "Identifier=" + identifier,
2036 "PluginURL=superPlugin",
2038 "OfficialSource=freenet",
2042 fcpServer.writeLine(
2044 "Identifier=" + identifier,
2045 "PluginName=superPlugin",
2047 "LongVersion=1.2.3",
2049 "OriginUri=superPlugin",
2053 assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
2054 assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
2055 assertThat(pluginInfo.get().get().isTalkable(), is(true));
2056 assertThat(pluginInfo.get().get().getVersion(), is("42"));
2057 assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
2058 assertThat(pluginInfo.get().get().isStarted(), is(true));
2062 public void failedLoadingPluginIsRecognized() throws ExecutionException, InterruptedException,
2064 Future<Optional<PluginInfo>> pluginInfo = fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute();
2066 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
2067 String identifier = extractIdentifier(lines);
2068 fcpServer.writeLine(
2070 "Identifier=" + identifier,
2073 assertThat(pluginInfo.get().isPresent(), is(false));