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.is;
7 import java.io.IOException;
8 import java.nio.charset.StandardCharsets;
10 import java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.Future;
16 import net.pterodactylus.fcp.FcpKeyPair;
17 import net.pterodactylus.fcp.Priority;
18 import net.pterodactylus.fcp.fake.FakeTcpServer;
19 import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
21 import com.google.common.io.ByteStreams;
22 import org.junit.After;
23 import org.junit.Test;
26 * Unit test for {@link DefaultFcpClient}.
28 * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
30 public class DefaultFcpClientTest {
32 private static final String INSERT_URI = "SSK@RVCHbJdkkyTCeNN9AYukEg76eyqmiosSaNKgE3U9zUw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQECAAE/";
33 private static final String REQUEST_URI = "SSK@wtbgd2loNcJCXvtQVOftl2tuWBomDQHfqS6ytpPRhfw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQACAAE/";
35 private static int threadCounter = 0;
36 private final ExecutorService threadPool = Executors.newCachedThreadPool((r) -> new Thread(r, "Test-Thread-" + threadCounter++));
37 private final FakeTcpServer fcpServer;
38 private final DefaultFcpClient fcpClient;
40 public DefaultFcpClientTest() throws IOException {
41 fcpServer = new FakeTcpServer(threadPool);
42 fcpClient = new DefaultFcpClient(threadPool, "localhost", fcpServer.getPort(), () -> "Test", () -> "2.0");
46 public void tearDown() throws IOException {
51 public void defaultFcpClientCanGenerateKeypair() throws ExecutionException, InterruptedException, IOException {
52 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
54 fcpServer.collectUntil(is("EndMessage"));
55 fcpServer.writeLine("SSKKeypair",
56 "InsertURI=" + INSERT_URI + "",
57 "RequestURI=" + REQUEST_URI + "",
58 "Identifier=My Identifier from GenerateSSK",
60 FcpKeyPair keyPair = keyPairFuture.get();
61 assertThat(keyPair.getPublicKey(), is(REQUEST_URI));
62 assertThat(keyPair.getPrivateKey(), is(INSERT_URI));
65 private void connectNode() throws InterruptedException, ExecutionException, IOException {
66 fcpServer.connect().get();
67 fcpServer.collectUntil(is("EndMessage"));
68 fcpServer.writeLine("NodeHello",
69 "CompressionCodecs=4 - GZIP(0), BZIP2(1), LZMA(2), LZMA_NEW(3)",
70 "Revision=build01466",
72 "Version=Fred,0.7,1.0,1466",
74 "ConnectionIdentifier=14318898267048452a81b36e7f13a3f0",
78 "NodeLanguage=ENGLISH",
85 public void clientGetCanDownloadData() throws InterruptedException, ExecutionException, IOException {
86 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
88 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
89 assertThat(lines, containsInAnyOrder(
100 "StartupTime=1435610539000",
101 "CompletionTime=1435610540000",
102 "Metadata.ContentType=text/plain;charset=utf-8",
106 Optional<Data> data = dataFuture.get();
107 assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
108 assertThat(data.get().size(), is(6L));
109 assertThat(ByteStreams.toByteArray(data.get().getInputStream()), is("Hello\n".getBytes(StandardCharsets.UTF_8)));
113 public void clientGetDownloadsDataForCorrectIdentifier() throws InterruptedException, ExecutionException, IOException {
114 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
116 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
117 assertThat(lines, containsInAnyOrder(
126 "Identifier=not-test",
128 "StartupTime=1435610539000",
129 "CompletionTime=1435610540000",
130 "Metadata.ContentType=text/plain;charset=latin-9",
138 "StartupTime=1435610539000",
139 "CompletionTime=1435610540000",
140 "Metadata.ContentType=text/plain;charset=utf-8",
144 Optional<Data> data = dataFuture.get();
145 assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
146 assertThat(data.get().size(), is(6L));
147 assertThat(ByteStreams.toByteArray(data.get().getInputStream()), is("Hello\n".getBytes(StandardCharsets.UTF_8)));
151 public void clientGetRecognizesGetFailed() throws InterruptedException, ExecutionException, IOException {
152 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
154 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
155 assertThat(lines, containsInAnyOrder(
168 Optional<Data> data = dataFuture.get();
169 assertThat(data.isPresent(), is(false));
173 public void clientGetRecognizesGetFailedForCorrectIdentifier() throws InterruptedException, ExecutionException, IOException {
174 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
176 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
177 assertThat(lines, containsInAnyOrder(
186 "Identifier=not-test",
196 Optional<Data> data = dataFuture.get();
197 assertThat(data.isPresent(), is(false));
201 public void clientGetRecognizesConnectionClosed() throws InterruptedException, ExecutionException, IOException {
202 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
204 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
205 assertThat(lines, containsInAnyOrder(
213 Optional<Data> data = dataFuture.get();
214 assertThat(data.isPresent(), is(false));
218 public void clientGetWithIgnoreDataStoreSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
219 fcpClient.clientGet().ignoreDataStore().identifier("test").uri("KSK@foo.txt");
221 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
222 assertThat(lines, containsInAnyOrder(
233 public void clientGetWithDataStoreOnlySettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
234 fcpClient.clientGet().dataStoreOnly().identifier("test").uri("KSK@foo.txt");
236 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
237 assertThat(lines, containsInAnyOrder(
248 public void clientGetWithMaxSizeSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
249 fcpClient.clientGet().maxSize(1048576).identifier("test").uri("KSK@foo.txt");
251 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
252 assertThat(lines, containsInAnyOrder(
263 public void clientGetWithPrioritySettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
264 fcpClient.clientGet().priority(Priority.interactive).identifier("test").uri("KSK@foo.txt");
266 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
267 assertThat(lines, containsInAnyOrder(
278 public void clientGetWithRealTimeSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
279 fcpClient.clientGet().realTime().identifier("test").uri("KSK@foo.txt");
281 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
282 assertThat(lines, containsInAnyOrder(
293 public void clientGetWithGlobalSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
294 fcpClient.clientGet().global().identifier("test").uri("KSK@foo.txt");
296 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
297 assertThat(lines, containsInAnyOrder(