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.FcpKeyPair;
24 import net.pterodactylus.fcp.Key;
25 import net.pterodactylus.fcp.NodeData;
26 import net.pterodactylus.fcp.Peer;
27 import net.pterodactylus.fcp.Priority;
28 import net.pterodactylus.fcp.fake.FakeTcpServer;
29 import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
31 import com.google.common.io.ByteStreams;
32 import com.google.common.io.Files;
33 import org.hamcrest.Description;
34 import org.hamcrest.Matcher;
35 import org.hamcrest.TypeSafeDiagnosingMatcher;
36 import org.junit.After;
37 import org.junit.Assert;
38 import org.junit.Test;
41 * Unit test for {@link DefaultFcpClient}.
43 * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
45 public class DefaultFcpClientTest {
47 private static final String INSERT_URI =
48 "SSK@RVCHbJdkkyTCeNN9AYukEg76eyqmiosSaNKgE3U9zUw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQECAAE/";
49 private static final String REQUEST_URI =
50 "SSK@wtbgd2loNcJCXvtQVOftl2tuWBomDQHfqS6ytpPRhfw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQACAAE/";
52 private static int threadCounter = 0;
53 private final ExecutorService threadPool =
54 Executors.newCachedThreadPool(r -> new Thread(r, "Test-Thread-" + threadCounter++));
55 private final FakeTcpServer fcpServer;
56 private final DefaultFcpClient fcpClient;
58 public DefaultFcpClientTest() throws IOException {
59 fcpServer = new FakeTcpServer(threadPool);
60 fcpClient = new DefaultFcpClient(threadPool, "localhost", fcpServer.getPort(), () -> "Test");
64 public void tearDown() throws IOException {
68 @Test(expected = ExecutionException.class)
69 public void defaultFcpClientThrowsExceptionIfItCanNotConnect()
70 throws IOException, ExecutionException, InterruptedException {
71 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
72 fcpServer.connect().get();
73 fcpServer.collectUntil(is("EndMessage"));
75 "CloseConnectionDuplicateClientName",
81 @Test(expected = ExecutionException.class)
82 public void defaultFcpClientThrowsExceptionIfConnectionIsClosed()
83 throws IOException, ExecutionException, InterruptedException {
84 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
85 fcpServer.connect().get();
86 fcpServer.collectUntil(is("EndMessage"));
92 public void defaultFcpClientCanGenerateKeypair() throws ExecutionException, InterruptedException, IOException {
93 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
95 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
96 String identifier = extractIdentifier(lines);
97 fcpServer.writeLine("SSKKeypair",
98 "InsertURI=" + INSERT_URI + "",
99 "RequestURI=" + REQUEST_URI + "",
100 "Identifier=" + identifier,
102 FcpKeyPair keyPair = keyPairFuture.get();
103 assertThat(keyPair.getPublicKey(), is(REQUEST_URI));
104 assertThat(keyPair.getPrivateKey(), is(INSERT_URI));
107 private void connectNode() throws InterruptedException, ExecutionException, IOException {
108 fcpServer.connect().get();
109 fcpServer.collectUntil(is("EndMessage"));
110 fcpServer.writeLine("NodeHello",
111 "CompressionCodecs=4 - GZIP(0), BZIP2(1), LZMA(2), LZMA_NEW(3)",
112 "Revision=build01466",
114 "Version=Fred,0.7,1.0,1466",
116 "ConnectionIdentifier=14318898267048452a81b36e7f13a3f0",
120 "NodeLanguage=ENGLISH",
127 public void clientGetCanDownloadData() throws InterruptedException, ExecutionException, IOException {
128 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
130 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
131 assertThat(lines, matchesFcpMessage("ClientGet", "ReturnType=direct", "URI=KSK@foo.txt"));
132 String identifier = extractIdentifier(lines);
135 "Identifier=" + identifier,
137 "StartupTime=1435610539000",
138 "CompletionTime=1435610540000",
139 "Metadata.ContentType=text/plain;charset=utf-8",
143 Optional<Data> data = dataFuture.get();
144 assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
145 assertThat(data.get().size(), is(6L));
146 assertThat(ByteStreams.toByteArray(data.get().getInputStream()),
147 is("Hello\n".getBytes(StandardCharsets.UTF_8)));
150 private String extractIdentifier(List<String> lines) {
151 return lines.stream()
152 .filter(s -> s.startsWith("Identifier="))
153 .map(s -> s.substring(s.indexOf('=') + 1))
159 public void clientGetDownloadsDataForCorrectIdentifier()
160 throws InterruptedException, ExecutionException, IOException {
161 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
163 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
164 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
165 String identifier = extractIdentifier(lines);
168 "Identifier=not-test",
170 "StartupTime=1435610539000",
171 "CompletionTime=1435610540000",
172 "Metadata.ContentType=text/plain;charset=latin-9",
178 "Identifier=" + identifier,
180 "StartupTime=1435610539000",
181 "CompletionTime=1435610540000",
182 "Metadata.ContentType=text/plain;charset=utf-8",
186 Optional<Data> data = dataFuture.get();
187 assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
188 assertThat(data.get().size(), is(6L));
189 assertThat(ByteStreams.toByteArray(data.get().getInputStream()),
190 is("Hello\n".getBytes(StandardCharsets.UTF_8)));
194 public void clientGetRecognizesGetFailed() throws InterruptedException, ExecutionException, IOException {
195 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
197 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
198 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
199 String identifier = extractIdentifier(lines);
202 "Identifier=" + identifier,
206 Optional<Data> data = dataFuture.get();
207 assertThat(data.isPresent(), is(false));
211 public void clientGetRecognizesGetFailedForCorrectIdentifier()
212 throws InterruptedException, ExecutionException, IOException {
213 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
215 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
216 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
217 String identifier = extractIdentifier(lines);
220 "Identifier=not-test",
226 "Identifier=" + identifier,
230 Optional<Data> data = dataFuture.get();
231 assertThat(data.isPresent(), is(false));
234 @Test(expected = ExecutionException.class)
235 public void clientGetRecognizesConnectionClosed() throws InterruptedException, ExecutionException, IOException {
236 Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
238 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
239 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
245 public void defaultFcpClientReusesConnection() throws InterruptedException, ExecutionException, IOException {
246 Future<FcpKeyPair> keyPair = fcpClient.generateKeypair().execute();
248 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
249 String identifier = extractIdentifier(lines);
252 "InsertURI=" + INSERT_URI + "",
253 "RequestURI=" + REQUEST_URI + "",
254 "Identifier=" + identifier,
258 keyPair = fcpClient.generateKeypair().execute();
259 lines = fcpServer.collectUntil(is("EndMessage"));
260 identifier = extractIdentifier(lines);
263 "InsertURI=" + INSERT_URI + "",
264 "RequestURI=" + REQUEST_URI + "",
265 "Identifier=" + identifier,
272 public void defaultFcpClientCanReconnectAfterConnectionHasBeenClosed()
273 throws InterruptedException, ExecutionException, IOException {
274 Future<FcpKeyPair> keyPair = fcpClient.generateKeypair().execute();
276 fcpServer.collectUntil(is("EndMessage"));
281 } catch (ExecutionException e) {
283 keyPair = fcpClient.generateKeypair().execute();
285 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
286 String identifier = extractIdentifier(lines);
289 "InsertURI=" + INSERT_URI + "",
290 "RequestURI=" + REQUEST_URI + "",
291 "Identifier=" + identifier,
298 public void clientGetWithIgnoreDataStoreSettingSendsCorrectCommands()
299 throws InterruptedException, ExecutionException, IOException {
300 fcpClient.clientGet().ignoreDataStore().uri("KSK@foo.txt").execute();
302 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
303 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "IgnoreDS=true"));
307 public void clientGetWithDataStoreOnlySettingSendsCorrectCommands()
308 throws InterruptedException, ExecutionException, IOException {
309 fcpClient.clientGet().dataStoreOnly().uri("KSK@foo.txt").execute();
311 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
312 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "DSonly=true"));
316 public void clientGetWithMaxSizeSettingSendsCorrectCommands()
317 throws InterruptedException, ExecutionException, IOException {
318 fcpClient.clientGet().maxSize(1048576).uri("KSK@foo.txt").execute();
320 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
321 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "MaxSize=1048576"));
325 public void clientGetWithPrioritySettingSendsCorrectCommands()
326 throws InterruptedException, ExecutionException, IOException {
327 fcpClient.clientGet().priority(Priority.interactive).uri("KSK@foo.txt").execute();
329 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
330 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "PriorityClass=1"));
334 public void clientGetWithRealTimeSettingSendsCorrectCommands()
335 throws InterruptedException, ExecutionException, IOException {
336 fcpClient.clientGet().realTime().uri("KSK@foo.txt").execute();
338 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
339 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "RealTimeFlag=true"));
343 public void clientGetWithGlobalSettingSendsCorrectCommands()
344 throws InterruptedException, ExecutionException, IOException {
345 fcpClient.clientGet().global().uri("KSK@foo.txt").execute();
347 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
348 assertThat(lines, matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "Global=true"));
351 private Matcher<List<String>> matchesFcpMessage(String name, String... requiredLines) {
352 return new TypeSafeDiagnosingMatcher<List<String>>() {
354 protected boolean matchesSafely(List<String> item, Description mismatchDescription) {
355 if (!item.get(0).equals(name)) {
356 mismatchDescription.appendText("FCP message is named ").appendValue(item.get(0));
359 for (String requiredLine : requiredLines) {
360 if (item.indexOf(requiredLine) < 1) {
361 mismatchDescription.appendText("FCP message does not contain ").appendValue(requiredLine);
369 public void describeTo(Description description) {
370 description.appendText("FCP message named ").appendValue(name);
371 description.appendValueList(", containing the lines ", ", ", "", requiredLines);
377 public void clientPutWithDirectDataSendsCorrectCommand()
378 throws IOException, ExecutionException, InterruptedException {
379 fcpClient.clientPut()
380 .from(new ByteArrayInputStream("Hello\n".getBytes()))
385 List<String> lines = fcpServer.collectUntil(is("Hello"));
386 assertThat(lines, matchesFcpMessage("ClientPut", "UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt"));
390 public void clientPutWithDirectDataSucceedsOnCorrectIdentifier()
391 throws InterruptedException, ExecutionException, IOException {
392 Future<Optional<Key>> key = fcpClient.clientPut()
393 .from(new ByteArrayInputStream("Hello\n".getBytes()))
398 List<String> lines = fcpServer.collectUntil(is("Hello"));
399 String identifier = extractIdentifier(lines);
402 "Identifier=not-the-right-one",
408 "Identifier=" + identifier,
411 assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
415 public void clientPutWithDirectDataFailsOnCorrectIdentifier()
416 throws InterruptedException, ExecutionException, IOException {
417 Future<Optional<Key>> key = fcpClient.clientPut()
418 .from(new ByteArrayInputStream("Hello\n".getBytes()))
423 List<String> lines = fcpServer.collectUntil(is("Hello"));
424 String identifier = extractIdentifier(lines);
427 "Identifier=not-the-right-one",
433 "Identifier=" + identifier,
436 assertThat(key.get().isPresent(), is(false));
440 public void clientPutWithRenamedDirectDataSendsCorrectCommand()
441 throws InterruptedException, ExecutionException, IOException {
442 fcpClient.clientPut()
443 .named("otherName.txt")
444 .from(new ByteArrayInputStream("Hello\n".getBytes()))
449 List<String> lines = fcpServer.collectUntil(is("Hello"));
450 assertThat(lines, matchesFcpMessage("ClientPut", "TargetFilename=otherName.txt", "UploadFrom=direct",
451 "DataLength=6", "URI=KSK@foo.txt"));
455 public void clientPutWithRedirectSendsCorrectCommand()
456 throws IOException, ExecutionException, InterruptedException {
457 fcpClient.clientPut().redirectTo("KSK@bar.txt").uri("KSK@foo.txt").execute();
459 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
461 matchesFcpMessage("ClientPut", "UploadFrom=redirect", "URI=KSK@foo.txt", "TargetURI=KSK@bar.txt"));
465 public void clientPutWithFileSendsCorrectCommand() throws InterruptedException, ExecutionException, IOException {
466 fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
468 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
470 matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt", "Filename=/tmp/data.txt"));
474 public void clientPutWithFileCanCompleteTestDdaSequence()
475 throws IOException, ExecutionException, InterruptedException {
476 File tempFile = createTempFile();
477 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
479 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
480 String identifier = extractIdentifier(lines);
483 "Identifier=" + identifier,
487 lines = fcpServer.collectUntil(is("EndMessage"));
488 assertThat(lines, matchesFcpMessage(
490 "Directory=" + tempFile.getParent(),
491 "WantReadDirectory=true",
492 "WantWriteDirectory=false",
497 "Directory=" + tempFile.getParent(),
498 "ReadFilename=" + tempFile,
501 lines = fcpServer.collectUntil(is("EndMessage"));
502 assertThat(lines, matchesFcpMessage(
504 "Directory=" + tempFile.getParent(),
505 "ReadContent=test-content",
510 "Directory=" + tempFile.getParent(),
511 "ReadDirectoryAllowed=true",
514 lines = fcpServer.collectUntil(is("EndMessage"));
516 matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt",
517 "Filename=" + new File(tempFile.getParent(), "test.dat")));
520 private File createTempFile() throws IOException {
521 File tempFile = File.createTempFile("test-dda-", ".dat");
522 tempFile.deleteOnExit();
523 Files.write("test-content", tempFile, StandardCharsets.UTF_8);
528 public void clientPutDoesNotReactToProtocolErrorForDifferentIdentifier()
529 throws InterruptedException, ExecutionException, IOException {
530 Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
532 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
533 String identifier = extractIdentifier(lines);
536 "Identifier=not-the-right-one",
542 "Identifier=" + identifier,
546 assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
550 public void clientPutAbortsOnProtocolErrorOtherThan25()
551 throws InterruptedException, ExecutionException, IOException {
552 Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
554 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
555 String identifier = extractIdentifier(lines);
558 "Identifier=" + identifier,
562 assertThat(key.get().isPresent(), is(false));
566 public void clientPutDoesNotReplyToWrongTestDdaReply() throws IOException, ExecutionException,
567 InterruptedException {
568 File tempFile = createTempFile();
569 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
571 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
572 String identifier = extractIdentifier(lines);
575 "Identifier=" + identifier,
579 lines = fcpServer.collectUntil(is("EndMessage"));
580 assertThat(lines, matchesFcpMessage(
582 "Directory=" + tempFile.getParent(),
583 "WantReadDirectory=true",
584 "WantWriteDirectory=false",
589 "Directory=/some-other-directory",
590 "ReadFilename=" + tempFile,
595 "Directory=" + tempFile.getParent(),
596 "ReadFilename=" + tempFile,
599 lines = fcpServer.collectUntil(is("EndMessage"));
600 assertThat(lines, matchesFcpMessage(
602 "Directory=" + tempFile.getParent(),
603 "ReadContent=test-content",
609 public void clientPutSendsResponseEvenIfFileCanNotBeRead()
610 throws IOException, ExecutionException, InterruptedException {
611 File tempFile = createTempFile();
612 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
614 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
615 String identifier = extractIdentifier(lines);
618 "Identifier=" + identifier,
622 lines = fcpServer.collectUntil(is("EndMessage"));
623 assertThat(lines, matchesFcpMessage(
625 "Directory=" + tempFile.getParent(),
626 "WantReadDirectory=true",
627 "WantWriteDirectory=false",
632 "Directory=" + tempFile.getParent(),
633 "ReadFilename=" + tempFile + ".foo",
636 lines = fcpServer.collectUntil(is("EndMessage"));
637 assertThat(lines, matchesFcpMessage(
639 "Directory=" + tempFile.getParent(),
640 "ReadContent=failed-to-read",
646 public void clientPutDoesNotResendOriginalClientPutOnTestDDACompleteWithWrongDirectory()
647 throws IOException, ExecutionException, InterruptedException {
648 File tempFile = createTempFile();
649 fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
651 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
652 String identifier = extractIdentifier(lines);
655 "Directory=/some-other-directory",
660 "Identifier=" + identifier,
664 lines = fcpServer.collectUntil(is("EndMessage"));
665 assertThat(lines, matchesFcpMessage(
667 "Directory=" + tempFile.getParent(),
668 "WantReadDirectory=true",
669 "WantWriteDirectory=false",
675 public void clientCanListPeers() throws IOException, ExecutionException, InterruptedException {
676 Future<Collection<Peer>> peers = fcpClient.listPeers().execute();
678 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
679 assertThat(lines, matchesFcpMessage(
681 "WithVolatile=false",
682 "WithMetadata=false",
685 String identifier = extractIdentifier(lines);
688 "Identifier=" + identifier,
694 "Identifier=" + identifier,
700 "Identifier=" + identifier,
703 assertThat(peers.get(), hasSize(2));
704 assertThat(peers.get().stream().map(Peer::getIdentity).collect(Collectors.toList()),
705 containsInAnyOrder("id1", "id2"));
709 public void clientCanListPeersWithMetadata() throws IOException, ExecutionException, InterruptedException {
710 Future<Collection<Peer>> peers = fcpClient.listPeers().includeMetadata().execute();
712 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
713 assertThat(lines, matchesFcpMessage(
715 "WithVolatile=false",
719 String identifier = extractIdentifier(lines);
722 "Identifier=" + identifier,
729 "Identifier=" + identifier,
736 "Identifier=" + identifier,
739 assertThat(peers.get(), hasSize(2));
740 assertThat(peers.get().stream().map(peer -> peer.getMetadata("foo")).collect(Collectors.toList()),
741 containsInAnyOrder("bar1", "bar2"));
745 public void clientCanListPeersWithVolatiles() throws IOException, ExecutionException, InterruptedException {
746 Future<Collection<Peer>> peers = fcpClient.listPeers().includeVolatile().execute();
748 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
749 assertThat(lines, matchesFcpMessage(
752 "WithMetadata=false",
755 String identifier = extractIdentifier(lines);
758 "Identifier=" + identifier,
765 "Identifier=" + identifier,
772 "Identifier=" + identifier,
775 assertThat(peers.get(), hasSize(2));
776 assertThat(peers.get().stream().map(peer -> peer.getVolatile("foo")).collect(Collectors.toList()),
777 containsInAnyOrder("bar1", "bar2"));
781 public void defaultFcpClientCanGetNodeInformation() throws InterruptedException, ExecutionException, IOException {
782 Future<NodeData> nodeData = fcpClient.getNode().execute();
784 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
785 String identifier = extractIdentifier(lines);
786 assertThat(lines, matchesFcpMessage(
788 "Identifier=" + identifier,
789 "GiveOpennetRef=false",
791 "WithVolatile=false",
796 "Identifier=" + identifier,
797 "ark.pubURI=SSK@3YEf.../ark",
800 "version=Fred,0.7,1.0,1466",
801 "lastGoodVersion=Fred,0.7,1.0,1466",
804 assertThat(nodeData.get(), notNullValue());
808 public void defaultFcpClientCanGetNodeInformationWithOpennetRef()
809 throws InterruptedException, ExecutionException, IOException {
810 Future<NodeData> nodeData = fcpClient.getNode().opennetRef().execute();
812 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
813 String identifier = extractIdentifier(lines);
814 assertThat(lines, matchesFcpMessage(
816 "Identifier=" + identifier,
817 "GiveOpennetRef=true",
819 "WithVolatile=false",
824 "Identifier=" + identifier,
826 "ark.pubURI=SSK@3YEf.../ark",
829 "version=Fred,0.7,1.0,1466",
830 "lastGoodVersion=Fred,0.7,1.0,1466",
833 assertThat(nodeData.get().getVersion().toString(), is("Fred,0.7,1.0,1466"));
837 public void defaultFcpClientCanGetNodeInformationWithPrivateData()
838 throws InterruptedException, ExecutionException, IOException {
839 Future<NodeData> nodeData = fcpClient.getNode().includePrivate().execute();
841 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
842 String identifier = extractIdentifier(lines);
843 assertThat(lines, matchesFcpMessage(
845 "Identifier=" + identifier,
846 "GiveOpennetRef=false",
848 "WithVolatile=false",
853 "Identifier=" + identifier,
855 "ark.pubURI=SSK@3YEf.../ark",
858 "version=Fred,0.7,1.0,1466",
859 "lastGoodVersion=Fred,0.7,1.0,1466",
860 "ark.privURI=SSK@XdHMiRl",
863 assertThat(nodeData.get().getARK().getPrivateURI(), is("SSK@XdHMiRl"));
867 public void defaultFcpClientCanGetNodeInformationWithVolatileData()
868 throws InterruptedException, ExecutionException, IOException {
869 Future<NodeData> nodeData = fcpClient.getNode().includeVolatile().execute();
871 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
872 String identifier = extractIdentifier(lines);
873 assertThat(lines, matchesFcpMessage(
875 "Identifier=" + identifier,
876 "GiveOpennetRef=false",
883 "Identifier=" + identifier,
885 "ark.pubURI=SSK@3YEf.../ark",
888 "version=Fred,0.7,1.0,1466",
889 "lastGoodVersion=Fred,0.7,1.0,1466",
890 "volatile.freeJavaMemory=205706528",
893 assertThat(nodeData.get().getVolatile("freeJavaMemory").toString(), is("205706528"));
897 public void defaultFcpClientCanListSinglePeerByIdentity()
898 throws InterruptedException, ExecutionException, IOException {
899 Future<Optional<Peer>> peer = fcpClient.listPeer().byIdentity("id1").execute();
901 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
902 String identifier = extractIdentifier(lines);
903 assertThat(lines, matchesFcpMessage(
905 "Identifier=" + identifier,
906 "NodeIdentifier=id1",
911 "Identifier=" + identifier,
914 "ark.pubURI=SSK@3YEf.../ark",
917 "version=Fred,0.7,1.0,1466",
918 "lastGoodVersion=Fred,0.7,1.0,1466",
921 assertThat(peer.get().get().getIdentity().toString(), is("id1"));
925 public void defaultFcpClientCanListSinglePeerByHostAndPort()
926 throws InterruptedException, ExecutionException, IOException {
927 Future<Optional<Peer>> peer = fcpClient.listPeer().byHostAndPort("host.free.net", 12345).execute();
929 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
930 String identifier = extractIdentifier(lines);
931 assertThat(lines, matchesFcpMessage(
933 "Identifier=" + identifier,
934 "NodeIdentifier=host.free.net:12345",
939 "Identifier=" + identifier,
942 "ark.pubURI=SSK@3YEf.../ark",
945 "version=Fred,0.7,1.0,1466",
946 "lastGoodVersion=Fred,0.7,1.0,1466",
949 assertThat(peer.get().get().getIdentity().toString(), is("id1"));
953 public void defaultFcpClientCanListSinglePeerByName()
954 throws InterruptedException, ExecutionException, IOException {
955 Future<Optional<Peer>> peer = fcpClient.listPeer().byName("FriendNode").execute();
957 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
958 String identifier = extractIdentifier(lines);
959 assertThat(lines, matchesFcpMessage(
961 "Identifier=" + identifier,
962 "NodeIdentifier=FriendNode",
967 "Identifier=" + identifier,
970 "ark.pubURI=SSK@3YEf.../ark",
973 "version=Fred,0.7,1.0,1466",
974 "lastGoodVersion=Fred,0.7,1.0,1466",
977 assertThat(peer.get().get().getIdentity().toString(), is("id1"));
981 public void defaultFcpClientRecognizesUnknownNodeIdentifiers()
982 throws InterruptedException, ExecutionException, IOException {
983 Future<Optional<Peer>> peer = fcpClient.listPeer().byIdentity("id2").execute();
985 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
986 String identifier = extractIdentifier(lines);
987 assertThat(lines, matchesFcpMessage(
989 "Identifier=" + identifier,
990 "NodeIdentifier=id2",
994 "UnknownNodeIdentifier",
995 "Identifier=" + identifier,
996 "NodeIdentifier=id2",
999 assertThat(peer.get().isPresent(), is(false));
1003 public void defaultFcpClientCanAddPeerFromFile() throws InterruptedException, ExecutionException, IOException {
1004 Future<Optional<Peer>> peer = fcpClient.addPeer().fromFile(new File("/tmp/ref.txt")).execute();
1006 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1007 String identifier = extractIdentifier(lines);
1008 assertThat(lines, matchesFcpMessage(
1010 "Identifier=" + identifier,
1011 "File=/tmp/ref.txt",
1014 fcpServer.writeLine(
1016 "Identifier=" + identifier,
1019 "ark.pubURI=SSK@3YEf.../ark",
1022 "version=Fred,0.7,1.0,1466",
1023 "lastGoodVersion=Fred,0.7,1.0,1466",
1026 assertThat(peer.get().get().getIdentity().toString(), is("id1"));
1030 public void defaultFcpClientCanAddPeerFromURL() throws InterruptedException, ExecutionException, IOException {
1031 Future<Optional<Peer>> peer = fcpClient.addPeer().fromURL(new URL("http://node.ref/")).execute();
1033 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
1034 String identifier = extractIdentifier(lines);
1035 assertThat(lines, matchesFcpMessage(
1037 "Identifier=" + identifier,
1038 "URL=http://node.ref/",
1041 fcpServer.writeLine(
1043 "Identifier=" + identifier,
1046 "ark.pubURI=SSK@3YEf.../ark",
1049 "version=Fred,0.7,1.0,1466",
1050 "lastGoodVersion=Fred,0.7,1.0,1466",
1053 assertThat(peer.get().get().getIdentity().toString(), is("id1"));