Add command that retrieves data from Freenet
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / quelaton / DefaultFcpClientTest.java
1 package net.pterodactylus.fcp.quelaton;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.containsInAnyOrder;
5 import static org.hamcrest.Matchers.is;
6
7 import java.io.IOException;
8 import java.nio.charset.StandardCharsets;
9 import java.util.List;
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;
15
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;
20
21 import com.google.common.io.ByteStreams;
22 import org.junit.After;
23 import org.junit.Test;
24
25 /**
26  * Unit test for {@link DefaultFcpClient}.
27  *
28  * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
29  */
30 public class DefaultFcpClientTest {
31
32         private static final String INSERT_URI = "SSK@RVCHbJdkkyTCeNN9AYukEg76eyqmiosSaNKgE3U9zUw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQECAAE/";
33         private static final String REQUEST_URI = "SSK@wtbgd2loNcJCXvtQVOftl2tuWBomDQHfqS6ytpPRhfw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQACAAE/";
34
35         private final ExecutorService threadPool = Executors.newCachedThreadPool();
36         private final FakeTcpServer fcpServer;
37         private final DefaultFcpClient fcpClient;
38
39         public DefaultFcpClientTest() throws IOException {
40                 fcpServer = new FakeTcpServer(threadPool);
41                 fcpClient = new DefaultFcpClient(threadPool, "localhost", fcpServer.getPort(), () -> "Test", () -> "2.0");
42         }
43
44         @After
45         public void tearDown() throws IOException {
46                 fcpServer.close();
47         }
48
49         @Test
50         public void defaultFcpClientCanGenerateKeypair() throws ExecutionException, InterruptedException, IOException {
51                 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
52                 connectNode();
53                 fcpServer.collectUntil(is("EndMessage"));
54                 fcpServer.writeLine("SSKKeypair",
55                         "InsertURI=" + INSERT_URI + "",
56                         "RequestURI=" + REQUEST_URI + "",
57                         "Identifier=My Identifier from GenerateSSK",
58                         "EndMessage");
59                 FcpKeyPair keyPair = keyPairFuture.get();
60                 assertThat(keyPair.getPublicKey(), is(REQUEST_URI));
61                 assertThat(keyPair.getPrivateKey(), is(INSERT_URI));
62         }
63
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",
70                         "Testnet=false",
71                         "Version=Fred,0.7,1.0,1466",
72                         "Build=1466",
73                         "ConnectionIdentifier=14318898267048452a81b36e7f13a3f0",
74                         "Node=Fred",
75                         "ExtBuild=29",
76                         "FCPVersion=2.0",
77                         "NodeLanguage=ENGLISH",
78                         "ExtRevision=v29",
79                         "EndMessage"
80                 );
81         }
82
83         @Test
84         public void clientGetCanDownloadData() throws InterruptedException, ExecutionException, IOException {
85                 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
86                 connectNode();
87                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
88                 assertThat(lines, containsInAnyOrder(
89                         "ClientGet",
90                         "Identifier=test",
91                         "ReturnType=direct",
92                         "URI=KSK@foo.txt",
93                         "EndMessage"
94                 ));
95                 fcpServer.writeLine(
96                         "AllData",
97                         "Identifier=test",
98                         "DataLength=6",
99                         "StartupTime=1435610539000",
100                         "CompletionTime=1435610540000",
101                         "Metadata.ContentType=text/plain;charset=utf-8",
102                         "Data",
103                         "Hello"
104                 );
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)));
109         }
110
111         @Test
112         public void clientGetRecognizesGetFailed() throws InterruptedException, ExecutionException, IOException {
113                 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
114                 connectNode();
115                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
116                 assertThat(lines, containsInAnyOrder(
117                         "ClientGet",
118                         "Identifier=test",
119                         "ReturnType=direct",
120                         "URI=KSK@foo.txt",
121                         "EndMessage"
122                 ));
123                 fcpServer.writeLine(
124                         "GetFailed",
125                         "Identifier=test",
126                         "Code=3",
127                         "EndMessage"
128                 );
129                 Optional<Data> data = dataFuture.get();
130                 assertThat(data.isPresent(), is(false));
131         }
132
133         @Test
134         public void clientGetRecognizesConnectionClosed() throws InterruptedException, ExecutionException, IOException {
135                 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
136                 connectNode();
137                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
138                 assertThat(lines, containsInAnyOrder(
139                         "ClientGet",
140                         "Identifier=test",
141                         "ReturnType=direct",
142                         "URI=KSK@foo.txt",
143                         "EndMessage"
144                 ));
145                 fcpServer.close();
146                 Optional<Data> data = dataFuture.get();
147                 assertThat(data.isPresent(), is(false));
148         }
149
150         @Test
151         public void clientGetWithIgnoreDataStoreSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
152                 fcpClient.clientGet().ignoreDataStore().identifier("test").uri("KSK@foo.txt");
153                 connectNode();
154                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
155                 assertThat(lines, containsInAnyOrder(
156                         "ClientGet",
157                         "Identifier=test",
158                         "ReturnType=direct",
159                         "URI=KSK@foo.txt",
160                         "IgnoreDS=true",
161                         "EndMessage"
162                 ));
163         }
164
165         @Test
166         public void clientGetWithDataStoreOnlySettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
167                 fcpClient.clientGet().dataStoreOnly().identifier("test").uri("KSK@foo.txt");
168                 connectNode();
169                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
170                 assertThat(lines, containsInAnyOrder(
171                         "ClientGet",
172                         "Identifier=test",
173                         "ReturnType=direct",
174                         "URI=KSK@foo.txt",
175                         "DSonly=true",
176                         "EndMessage"
177                 ));
178         }
179
180         @Test
181         public void clientGetWithMaxSizeSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
182                 fcpClient.clientGet().maxSize(1048576).identifier("test").uri("KSK@foo.txt");
183                 connectNode();
184                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
185                 assertThat(lines, containsInAnyOrder(
186                         "ClientGet",
187                         "Identifier=test",
188                         "ReturnType=direct",
189                         "URI=KSK@foo.txt",
190                         "MaxSize=1048576",
191                         "EndMessage"
192                 ));
193         }
194
195         @Test
196         public void clientGetWithPrioritySettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
197                 fcpClient.clientGet().priority(Priority.interactive).identifier("test").uri("KSK@foo.txt");
198                 connectNode();
199                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
200                 assertThat(lines, containsInAnyOrder(
201                         "ClientGet",
202                         "Identifier=test",
203                         "ReturnType=direct",
204                         "URI=KSK@foo.txt",
205                         "PriorityClass=1",
206                         "EndMessage"
207                 ));
208         }
209
210         @Test
211         public void clientGetWithRealTimeSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
212                 fcpClient.clientGet().realTime().identifier("test").uri("KSK@foo.txt");
213                 connectNode();
214                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
215                 assertThat(lines, containsInAnyOrder(
216                         "ClientGet",
217                         "Identifier=test",
218                         "ReturnType=direct",
219                         "URI=KSK@foo.txt",
220                         "RealTimeFlag=true",
221                         "EndMessage"
222                 ));
223         }
224
225         @Test
226         public void clientGetWithGlobalSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
227                 fcpClient.clientGet().global().identifier("test").uri("KSK@foo.txt");
228                 connectNode();
229                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
230                 assertThat(lines, containsInAnyOrder(
231                         "ClientGet",
232                         "Identifier=test",
233                         "ReturnType=direct",
234                         "URI=KSK@foo.txt",
235                         "Global=true",
236                         "EndMessage"
237                 ));
238         }
239
240 }