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 final ExecutorService threadPool = Executors.newCachedThreadPool();
36 private final FakeTcpServer fcpServer;
37 private final DefaultFcpClient fcpClient;
39 public DefaultFcpClientTest() throws IOException {
40 fcpServer = new FakeTcpServer(threadPool);
41 fcpClient = new DefaultFcpClient(threadPool, "localhost", fcpServer.getPort(), () -> "Test", () -> "2.0");
45 public void tearDown() throws IOException {
50 public void defaultFcpClientCanGenerateKeypair() throws ExecutionException, InterruptedException, IOException {
51 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
53 fcpServer.collectUntil(is("EndMessage"));
54 fcpServer.writeLine("SSKKeypair",
55 "InsertURI=" + INSERT_URI + "",
56 "RequestURI=" + REQUEST_URI + "",
57 "Identifier=My Identifier from GenerateSSK",
59 FcpKeyPair keyPair = keyPairFuture.get();
60 assertThat(keyPair.getPublicKey(), is(REQUEST_URI));
61 assertThat(keyPair.getPrivateKey(), is(INSERT_URI));
64 private void connectNode() throws InterruptedException, ExecutionException, IOException {
65 fcpServer.connect().get();
66 fcpServer.collectUntil(is("EndMessage"));
67 fcpServer.writeLine("NodeHello",
68 "CompressionCodecs=4 - GZIP(0), BZIP2(1), LZMA(2), LZMA_NEW(3)",
69 "Revision=build01466",
71 "Version=Fred,0.7,1.0,1466",
73 "ConnectionIdentifier=14318898267048452a81b36e7f13a3f0",
77 "NodeLanguage=ENGLISH",
84 public void clientGetCanDownloadData() throws InterruptedException, ExecutionException, IOException {
85 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
87 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
88 assertThat(lines, containsInAnyOrder(
99 "StartupTime=1435610539000",
100 "CompletionTime=1435610540000",
101 "Metadata.ContentType=text/plain;charset=utf-8",
105 Optional<Data> data = dataFuture.get();
106 assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
107 assertThat(data.get().size(), is(6L));
108 assertThat(ByteStreams.toByteArray(data.get().getInputStream()), is("Hello\n".getBytes(StandardCharsets.UTF_8)));
112 public void clientGetDownloadsDataForCorrectIdentifier() throws InterruptedException, ExecutionException, IOException {
113 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
115 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
116 assertThat(lines, containsInAnyOrder(
125 "Identifier=not-test",
127 "StartupTime=1435610539000",
128 "CompletionTime=1435610540000",
129 "Metadata.ContentType=text/plain;charset=latin-9",
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()), is("Hello\n".getBytes(StandardCharsets.UTF_8)));
150 public void clientGetRecognizesGetFailed() throws InterruptedException, ExecutionException, IOException {
151 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
153 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
154 assertThat(lines, containsInAnyOrder(
167 Optional<Data> data = dataFuture.get();
168 assertThat(data.isPresent(), is(false));
172 public void clientGetRecognizesGetFailedForCorrectIdentifier() throws InterruptedException, ExecutionException, IOException {
173 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
175 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
176 assertThat(lines, containsInAnyOrder(
185 "Identifier=not-test",
195 Optional<Data> data = dataFuture.get();
196 assertThat(data.isPresent(), is(false));
200 public void clientGetRecognizesConnectionClosed() throws InterruptedException, ExecutionException, IOException {
201 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
203 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
204 assertThat(lines, containsInAnyOrder(
212 Optional<Data> data = dataFuture.get();
213 assertThat(data.isPresent(), is(false));
217 public void clientGetWithIgnoreDataStoreSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
218 fcpClient.clientGet().ignoreDataStore().identifier("test").uri("KSK@foo.txt");
220 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
221 assertThat(lines, containsInAnyOrder(
232 public void clientGetWithDataStoreOnlySettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
233 fcpClient.clientGet().dataStoreOnly().identifier("test").uri("KSK@foo.txt");
235 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
236 assertThat(lines, containsInAnyOrder(
247 public void clientGetWithMaxSizeSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
248 fcpClient.clientGet().maxSize(1048576).identifier("test").uri("KSK@foo.txt");
250 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
251 assertThat(lines, containsInAnyOrder(
262 public void clientGetWithPrioritySettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
263 fcpClient.clientGet().priority(Priority.interactive).identifier("test").uri("KSK@foo.txt");
265 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
266 assertThat(lines, containsInAnyOrder(
277 public void clientGetWithRealTimeSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
278 fcpClient.clientGet().realTime().identifier("test").uri("KSK@foo.txt");
280 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
281 assertThat(lines, containsInAnyOrder(
292 public void clientGetWithGlobalSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
293 fcpClient.clientGet().global().identifier("test").uri("KSK@foo.txt");
295 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
296 assertThat(lines, containsInAnyOrder(